xref: /haiku/headers/private/kernel/debug.h (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
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\n", __FILE__, __LINE__, #x);	\
37 		}																	\
38 	} while (0)
39 
40 #define ASSERT_ALWAYS_PRINT(x, format...) \
41 	do {																	\
42 		if (!(x)) {															\
43 			dprintf_no_syslog(format);										\
44 			panic("ASSERT FAILED (%s:%d): %s\n", __FILE__, __LINE__, #x);	\
45 		}																	\
46 	} while (0)
47 
48 #if KDEBUG
49 #	define ASSERT(x)					ASSERT_ALWAYS(x)
50 #	define ASSERT_PRINT(x, format...)	ASSERT_ALWAYS_PRINT(x, format)
51 #else
52 #	define ASSERT(x)					do { } while(0)
53 #	define ASSERT_PRINT(x, format...)	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 struct debugger_module_info {
71 	module_info info;
72 
73 	void (*enter_debugger)(void);
74 	void (*exit_debugger)(void);
75 
76 	// I/O hooks
77 	int (*debugger_puts)(const char *str, int32 length);
78 	int (*debugger_getchar)(void);
79 	// TODO: add hooks for tunnelling gdb ?
80 
81 	// Misc. hooks
82 	bool (*emergency_key_pressed)(char key);
83 };
84 
85 struct debugger_demangle_module_info {
86 	module_info info;
87 
88 	const char* (*demangle_symbol)(const char* name, char* buffer,
89 		size_t bufferSize, bool* _isObjectMethod);
90 	status_t (*get_next_argument)(uint32* _cookie, const char* symbol,
91 		char* name, size_t nameSize, int32* _type, size_t* _argumentLength);
92 };
93 
94 
95 extern int dbg_register_file[B_MAX_CPU_COUNT][14];
96 
97 typedef struct debug_page_fault_info {
98 	addr_t	fault_address;
99 	addr_t	pc;
100 	uint32	flags;
101 } debug_page_fault_info;
102 
103 // debug_page_fault_info::flags
104 #define DEBUG_PAGE_FAULT_WRITE		0x01	/* write fault */
105 #define DEBUG_PAGE_FAULT_NO_INFO	0x02	/* fault address and read/write
106 											   unknown */
107 
108 
109 #ifdef __cplusplus
110 extern "C" {
111 #endif
112 
113 struct kernel_args;
114 
115 extern status_t debug_init(struct kernel_args *args);
116 extern status_t	debug_init_post_vm(struct kernel_args *args);
117 extern status_t	debug_init_post_modules(struct kernel_args *args);
118 extern void		debug_early_boot_message(const char *string);
119 extern void		debug_puts(const char *s, int32 length);
120 extern bool		debug_debugger_running(void);
121 extern bool		debug_screen_output_enabled(void);
122 extern void		debug_stop_screen_debug_output(void);
123 extern void		debug_set_page_fault_info(addr_t faultAddress, addr_t pc,
124 					uint32 flags);
125 extern debug_page_fault_info* debug_get_page_fault_info();
126 extern void		debug_trap_cpu_in_kdl(int32 cpu, bool returnIfHandedOver);
127 extern void		debug_double_fault(int32 cpu);
128 extern bool		debug_emergency_key_pressed(char key);
129 extern bool		debug_is_kernel_memory_accessible(addr_t address, size_t size,
130 					uint32 protection);
131 extern int		debug_call_with_fault_handler(jmp_buf jumpBuffer,
132 					void (*function)(void*), void* parameter);
133 extern status_t	debug_memcpy(void* to, const void* from, size_t size);
134 extern ssize_t	debug_strlcpy(char* to, const char* from, size_t size);
135 
136 extern char		kgetc(void);
137 extern void		kputs(const char *string);
138 extern void		kputs_unfiltered(const char *string);
139 extern void		kprintf_unfiltered(const char *format, ...)
140 					__attribute__ ((format (__printf__, 1, 2)));
141 extern void		dprintf_no_syslog(const char *format, ...)
142 					__attribute__ ((format (__printf__, 1, 2)));
143 
144 extern bool		is_debug_variable_defined(const char* variableName);
145 extern bool		set_debug_variable(const char* variableName, uint64 value);
146 extern uint64	get_debug_variable(const char* variableName,
147 					uint64 defaultValue);
148 extern bool		unset_debug_variable(const char* variableName);
149 extern void		unset_all_debug_variables();
150 
151 extern bool		evaluate_debug_expression(const char* expression,
152 					uint64* result, bool silent);
153 extern int		evaluate_debug_command(const char* command);
154 extern status_t	parse_next_debug_command_argument(const char** expressionString,
155 					char* buffer, size_t bufferSize);
156 
157 extern status_t	add_debugger_command_etc(const char* name,
158 					debugger_command_hook func, const char* description,
159 					const char* usage, uint32 flags);
160 extern status_t	add_debugger_command_alias(const char* newName,
161 					const char* oldName, const char* description);
162 extern bool		print_debugger_command_usage(const char* command);
163 extern bool		has_debugger_command(const char* command);
164 
165 extern const char *debug_demangle_symbol(const char* symbol, char* buffer,
166 					size_t bufferSize, bool* _isObjectMethod);
167 extern status_t	debug_get_next_demangled_argument(uint32* _cookie,
168 					const char* symbol, char* name, size_t nameSize,
169 					int32* _type, size_t* _argumentLength);
170 
171 extern struct thread* debug_set_debugged_thread(struct thread* thread);
172 extern struct thread* debug_get_debugged_thread();
173 
174 extern status_t	_user_kernel_debugger(const char *message);
175 extern void		_user_debug_output(const char *userString);
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 #endif	/* _KERNEL_DEBUG_H */
182