xref: /haiku/headers/private/kernel/debug.h (revision 508f54795f39c3e7552d87c95aae9dd8ec6f505b)
1 /*
2  * Copyright 2002-2010, 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 #define STATIC_ASSERT(x)								\
57 	do {												\
58 		struct __staticAssertStruct__ {					\
59 			char __static_assert_failed__[2*(x) - 1];	\
60 		};												\
61 	} while (false)
62 
63 #if KDEBUG
64 #	define KDEBUG_ONLY(x)				x
65 #else
66 #	define KDEBUG_ONLY(x)				/* nothing */
67 #endif
68 
69 // command return value
70 #define B_KDEBUG_ERROR			4
71 #define B_KDEBUG_RESTART_PIPE	5
72 
73 // command flags
74 #define B_KDEBUG_DONT_PARSE_ARGUMENTS	(0x01)
75 #define B_KDEBUG_PIPE_FINAL_RERUN		(0x02)
76 
77 
78 struct arch_debug_registers;
79 
80 
81 struct debugger_module_info {
82 	module_info info;
83 
84 	void (*enter_debugger)(void);
85 	void (*exit_debugger)(void);
86 
87 	// I/O hooks
88 	int (*debugger_puts)(const char *str, int32 length);
89 	int (*debugger_getchar)(void);
90 	// TODO: add hooks for tunnelling gdb ?
91 
92 	// Misc. hooks
93 	bool (*emergency_key_pressed)(char key);
94 };
95 
96 struct debugger_demangle_module_info {
97 	module_info info;
98 
99 	const char* (*demangle_symbol)(const char* name, char* buffer,
100 		size_t bufferSize, bool* _isObjectMethod);
101 	status_t (*get_next_argument)(uint32* _cookie, const char* symbol,
102 		char* name, size_t nameSize, int32* _type, size_t* _argumentLength);
103 };
104 
105 
106 typedef struct debug_page_fault_info {
107 	addr_t	fault_address;
108 	addr_t	pc;
109 	uint32	flags;
110 } debug_page_fault_info;
111 
112 // debug_page_fault_info::flags
113 #define DEBUG_PAGE_FAULT_WRITE		0x01	/* write fault */
114 #define DEBUG_PAGE_FAULT_NO_INFO	0x02	/* fault address and read/write
115 											   unknown */
116 
117 
118 #ifdef __cplusplus
119 extern "C" {
120 #endif
121 
122 struct kernel_args;
123 
124 extern void		debug_init(struct kernel_args *args);
125 extern void		debug_init_post_vm(struct kernel_args *args);
126 extern void		debug_init_post_settings(struct kernel_args *args);
127 extern void		debug_init_post_modules(struct kernel_args *args);
128 extern void		debug_early_boot_message(const char *string);
129 extern void		debug_puts(const char *s, int32 length);
130 extern bool		debug_debugger_running(void);
131 extern bool		debug_screen_output_enabled(void);
132 extern void		debug_stop_screen_debug_output(void);
133 extern void		debug_set_page_fault_info(addr_t faultAddress, addr_t pc,
134 					uint32 flags);
135 extern debug_page_fault_info* debug_get_page_fault_info();
136 extern void		debug_trap_cpu_in_kdl(int32 cpu, bool returnIfHandedOver);
137 extern void		debug_double_fault(int32 cpu);
138 extern bool		debug_emergency_key_pressed(char key);
139 extern bool		debug_is_kernel_memory_accessible(addr_t address, size_t size,
140 					uint32 protection);
141 extern int		debug_call_with_fault_handler(jmp_buf jumpBuffer,
142 					void (*function)(void*), void* parameter);
143 extern status_t	debug_memcpy(team_id teamID, void* to, const void* from,
144 					size_t size);
145 extern ssize_t	debug_strlcpy(team_id teamID, char* to, const char* from,
146 					size_t size);
147 
148 extern char		kgetc(void);
149 extern void		kputs(const char *string);
150 extern void		kputs_unfiltered(const char *string);
151 extern void		kprintf_unfiltered(const char *format, ...)
152 					__attribute__ ((format (__printf__, 1, 2)));
153 extern void		dprintf_no_syslog(const char *format, ...)
154 					__attribute__ ((format (__printf__, 1, 2)));
155 
156 extern bool		is_debug_variable_defined(const char* variableName);
157 extern bool		set_debug_variable(const char* variableName, uint64 value);
158 extern uint64	get_debug_variable(const char* variableName,
159 					uint64 defaultValue);
160 extern bool		unset_debug_variable(const char* variableName);
161 extern void		unset_all_debug_variables();
162 
163 extern bool		evaluate_debug_expression(const char* expression,
164 					uint64* result, bool silent);
165 extern int		evaluate_debug_command(const char* command);
166 extern status_t	parse_next_debug_command_argument(const char** expressionString,
167 					char* buffer, size_t bufferSize);
168 
169 extern status_t	add_debugger_command_etc(const char* name,
170 					debugger_command_hook func, const char* description,
171 					const char* usage, uint32 flags);
172 extern status_t	add_debugger_command_alias(const char* newName,
173 					const char* oldName, const char* description);
174 extern bool		print_debugger_command_usage(const char* command);
175 extern bool		has_debugger_command(const char* command);
176 
177 extern const char *debug_demangle_symbol(const char* symbol, char* buffer,
178 					size_t bufferSize, bool* _isObjectMethod);
179 extern status_t	debug_get_next_demangled_argument(uint32* _cookie,
180 					const char* symbol, char* name, size_t nameSize,
181 					int32* _type, size_t* _argumentLength);
182 
183 extern struct thread* debug_set_debugged_thread(struct thread* thread);
184 extern struct thread* debug_get_debugged_thread();
185 extern bool debug_is_debugged_team(team_id teamID);
186 
187 extern struct arch_debug_registers* debug_get_debug_registers(int32 cpu);
188 
189 extern status_t	_user_kernel_debugger(const char *message);
190 extern void		_user_debug_output(const char *userString);
191 
192 #ifdef __cplusplus
193 }
194 #endif
195 
196 
197 #ifdef __cplusplus
198 
199 struct DebuggedThreadSetter {
200 	DebuggedThreadSetter(struct thread* thread)
201 		:
202 		fPreviousThread(debug_set_debugged_thread(thread))
203 	{
204 	}
205 
206 	~DebuggedThreadSetter()
207 	{
208 		debug_set_debugged_thread(fPreviousThread);
209 	}
210 
211 private:
212 	struct thread*	fPreviousThread;
213 };
214 
215 
216 #endif	// __cplusplus
217 
218 
219 #endif	/* _KERNEL_DEBUG_H */
220