xref: /haiku/headers/private/kernel/debug.h (revision 62f5ba006a08b0df30631375878effaf67ae5dbc)
1 /*
2  * Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de
3  * Distributed under the terms of the Haiku License.
4  *
5  * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
6  * Distributed under the terms of the NewOS License.
7  */
8 #ifndef _KERNEL_DEBUG_H
9 #define _KERNEL_DEBUG_H
10 
11 
12 #include <setjmp.h>
13 
14 #include <KernelExport.h>
15 #include <module.h>
16 
17 #include "kernel_debug_config.h"
18 
19 
20 /*	KDEBUG
21 	The kernel debug level.
22 	Level 1 is usual asserts, > 1 should be used for very expensive runtime
23 	checks
24  */
25 #if !defined(KDEBUG)
26 #	if DEBUG
27 #		define KDEBUG 1
28 #	else
29 #		define KDEBUG 0
30 #	endif
31 #endif
32 
33 #define ASSERT_ALWAYS(x) \
34 	do {																	\
35 		if (!(x)) {															\
36 			panic("ASSERT FAILED (%s:%d): %s", __FILE__, __LINE__, #x);		\
37 		}																	\
38 	} while (0)
39 
40 #define ASSERT_ALWAYS_PRINT(x, format, args...) \
41 	do {																	\
42 		if (!(x)) {															\
43 			panic("ASSERT FAILED (%s:%d): %s; " format, __FILE__, __LINE__,	\
44 				#x, args);													\
45 		}																	\
46 	} while (0)
47 
48 #if KDEBUG
49 #	define ASSERT(x)						ASSERT_ALWAYS(x)
50 #	define ASSERT_PRINT(x, format, args...)	ASSERT_ALWAYS_PRINT(x, format, args)
51 #else
52 #	define ASSERT(x)						do { } while(0)
53 #	define ASSERT_PRINT(x, format, args...)	do { } while(0)
54 #endif
55 
56 #if KDEBUG
57 #	define KDEBUG_ONLY(x)				x
58 #else
59 #	define KDEBUG_ONLY(x)				/* nothing */
60 #endif
61 
62 // command return value
63 #define B_KDEBUG_ERROR			4
64 #define B_KDEBUG_RESTART_PIPE	5
65 
66 // command flags
67 #define B_KDEBUG_DONT_PARSE_ARGUMENTS	(0x01)
68 #define B_KDEBUG_PIPE_FINAL_RERUN		(0x02)
69 
70 
71 struct arch_debug_registers;
72 
73 
74 struct debugger_module_info {
75 	module_info info;
76 
77 	void (*enter_debugger)(void);
78 	void (*exit_debugger)(void);
79 
80 	// I/O hooks
81 	int (*debugger_puts)(const char *str, int32 length);
82 	int (*debugger_getchar)(void);
83 	// TODO: add hooks for tunnelling gdb ?
84 
85 	// Misc. hooks
86 	bool (*emergency_key_pressed)(char key);
87 };
88 
89 struct debugger_demangle_module_info {
90 	module_info info;
91 
92 	const char* (*demangle_symbol)(const char* name, char* buffer,
93 		size_t bufferSize, bool* _isObjectMethod);
94 	status_t (*get_next_argument)(uint32* _cookie, const char* symbol,
95 		char* name, size_t nameSize, int32* _type, size_t* _argumentLength);
96 };
97 
98 
99 typedef struct debug_page_fault_info {
100 	addr_t	fault_address;
101 	addr_t	pc;
102 	uint32	flags;
103 } debug_page_fault_info;
104 
105 // debug_page_fault_info::flags
106 #define DEBUG_PAGE_FAULT_WRITE		0x01	/* write fault */
107 #define DEBUG_PAGE_FAULT_NO_INFO	0x02	/* fault address and read/write
108 											   unknown */
109 
110 
111 #ifdef __cplusplus
112 extern "C" {
113 #endif
114 
115 struct kernel_args;
116 
117 extern status_t debug_init(struct kernel_args *args);
118 extern status_t	debug_init_post_vm(struct kernel_args *args);
119 extern status_t	debug_init_post_modules(struct kernel_args *args);
120 extern void		debug_early_boot_message(const char *string);
121 extern void		debug_puts(const char *s, int32 length);
122 extern bool		debug_debugger_running(void);
123 extern bool		debug_screen_output_enabled(void);
124 extern void		debug_stop_screen_debug_output(void);
125 extern void		debug_set_page_fault_info(addr_t faultAddress, addr_t pc,
126 					uint32 flags);
127 extern debug_page_fault_info* debug_get_page_fault_info();
128 extern void		debug_trap_cpu_in_kdl(int32 cpu, bool returnIfHandedOver);
129 extern void		debug_double_fault(int32 cpu);
130 extern bool		debug_emergency_key_pressed(char key);
131 extern bool		debug_is_kernel_memory_accessible(addr_t address, size_t size,
132 					uint32 protection);
133 extern int		debug_call_with_fault_handler(jmp_buf jumpBuffer,
134 					void (*function)(void*), void* parameter);
135 extern status_t	debug_memcpy(void* to, const void* from, size_t size);
136 extern ssize_t	debug_strlcpy(char* to, const char* from, size_t size);
137 
138 extern char		kgetc(void);
139 extern void		kputs(const char *string);
140 extern void		kputs_unfiltered(const char *string);
141 extern void		kprintf_unfiltered(const char *format, ...)
142 					__attribute__ ((format (__printf__, 1, 2)));
143 extern void		dprintf_no_syslog(const char *format, ...)
144 					__attribute__ ((format (__printf__, 1, 2)));
145 
146 extern bool		is_debug_variable_defined(const char* variableName);
147 extern bool		set_debug_variable(const char* variableName, uint64 value);
148 extern uint64	get_debug_variable(const char* variableName,
149 					uint64 defaultValue);
150 extern bool		unset_debug_variable(const char* variableName);
151 extern void		unset_all_debug_variables();
152 
153 extern bool		evaluate_debug_expression(const char* expression,
154 					uint64* result, bool silent);
155 extern int		evaluate_debug_command(const char* command);
156 extern status_t	parse_next_debug_command_argument(const char** expressionString,
157 					char* buffer, size_t bufferSize);
158 
159 extern status_t	add_debugger_command_etc(const char* name,
160 					debugger_command_hook func, const char* description,
161 					const char* usage, uint32 flags);
162 extern status_t	add_debugger_command_alias(const char* newName,
163 					const char* oldName, const char* description);
164 extern bool		print_debugger_command_usage(const char* command);
165 extern bool		has_debugger_command(const char* command);
166 
167 extern const char *debug_demangle_symbol(const char* symbol, char* buffer,
168 					size_t bufferSize, bool* _isObjectMethod);
169 extern status_t	debug_get_next_demangled_argument(uint32* _cookie,
170 					const char* symbol, char* name, size_t nameSize,
171 					int32* _type, size_t* _argumentLength);
172 
173 extern struct thread* debug_set_debugged_thread(struct thread* thread);
174 extern struct thread* debug_get_debugged_thread();
175 extern struct arch_debug_registers* debug_get_debug_registers(int32 cpu);
176 
177 
178 extern status_t	_user_kernel_debugger(const char *message);
179 extern void		_user_debug_output(const char *userString);
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #endif	/* _KERNEL_DEBUG_H */
186