xref: /haiku/headers/private/kernel/debug.h (revision adb0d19d561947362090081e81d90dde59142026)
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 #include "kernel_debug_config.h"
12 
13 #include <KernelExport.h>
14 #include <module.h>
15 
16 
17 /*	KDEBUG
18 	The kernel debug level.
19 	Level 1 is usual asserts, > 1 should be used for very expensive runtime
20 	checks
21  */
22 #if !defined(KDEBUG)
23 #	if DEBUG
24 #		define KDEBUG 1
25 #	else
26 #		define KDEBUG 0
27 #	endif
28 #endif
29 
30 #define ASSERT_ALWAYS(x) \
31 	do { if (!(x)) { panic("ASSERT FAILED (%s:%d): %s\n", __FILE__, __LINE__, #x); } } while (0)
32 
33 #define ASSERT_ALWAYS_PRINT(x, format...) \
34 	do {																	\
35 		if (!(x)) {															\
36 			dprintf_no_syslog(format);										\
37 			panic("ASSERT FAILED (%s:%d): %s\n", __FILE__, __LINE__, #x);	\
38 		}																	\
39 	} while (0)
40 
41 #if KDEBUG
42 #	define ASSERT(x)					ASSERT_ALWAYS(x)
43 #	define ASSERT_PRINT(x, format...)	ASSERT_ALWAYS_PRINT(x, format)
44 #else
45 #	define ASSERT(x)					do { } while(0)
46 #	define ASSERT_PRINT(x, format...)	do { } while(0)
47 #endif
48 
49 #if KDEBUG
50 #	define KDEBUG_ONLY(x)				x
51 #else
52 #	define KDEBUG_ONLY(x)				/* nothing */
53 #endif
54 
55 // command return value
56 #define B_KDEBUG_ERROR			4
57 #define B_KDEBUG_RESTART_PIPE	5
58 
59 // command flags
60 #define B_KDEBUG_DONT_PARSE_ARGUMENTS	(0x01)
61 #define B_KDEBUG_PIPE_FINAL_RERUN		(0x02)
62 
63 struct debugger_module_info {
64 	module_info info;
65 
66 	void (*enter_debugger)(void);
67 	void (*exit_debugger)(void);
68 
69 	// io hooks
70 	int (*debugger_puts)(const char *str, int32 length);
71 	int (*debugger_getchar)(void);
72 	// TODO: add hooks for tunnelling gdb ?
73 };
74 
75 struct debugger_demangle_module_info {
76 	module_info info;
77 
78 	const char* (*demangle_symbol)(const char* name, char* buffer,
79 		size_t bufferSize, bool* _isObjectMethod);
80 	status_t (*get_next_argument)(uint32* _cookie, const char* symbol,
81 		char* name, size_t nameSize, int32* _type, size_t* _argumentLength);
82 };
83 
84 
85 extern int dbg_register_file[B_MAX_CPU_COUNT][14];
86 
87 typedef struct debug_page_fault_info {
88 	addr_t	fault_address;
89 	addr_t	pc;
90 	uint32	flags;
91 } debug_page_fault_info;
92 
93 // debug_page_fault_info::flags
94 #define DEBUG_PAGE_FAULT_WRITE		0x01	/* write fault */
95 #define DEBUG_PAGE_FAULT_NO_INFO	0x02	/* fault address and read/write
96 											   unknown */
97 
98 
99 #ifdef __cplusplus
100 extern "C" {
101 #endif
102 
103 struct kernel_args;
104 
105 extern status_t debug_init(struct kernel_args *args);
106 extern status_t	debug_init_post_vm(struct kernel_args *args);
107 extern status_t	debug_init_post_modules(struct kernel_args *args);
108 extern void		debug_early_boot_message(const char *string);
109 extern void		debug_puts(const char *s, int32 length);
110 extern bool		debug_debugger_running(void);
111 extern bool		debug_screen_output_enabled(void);
112 extern void		debug_stop_screen_debug_output(void);
113 extern void		debug_set_page_fault_info(addr_t faultAddress, addr_t pc,
114 					uint32 flags);
115 extern debug_page_fault_info* debug_get_page_fault_info();
116 extern void debug_trap_cpu_in_kdl(bool returnIfHandedOver);
117 
118 extern char		kgetc(void);
119 extern void		kputs(const char *string);
120 extern void		kputs_unfiltered(const char *string);
121 extern void		kprintf_unfiltered(const char *format, ...)
122 					__attribute__ ((format (__printf__, 1, 2)));
123 extern void		dprintf_no_syslog(const char *format, ...)
124 					__attribute__ ((format (__printf__, 1, 2)));
125 
126 extern bool		is_debug_variable_defined(const char* variableName);
127 extern bool		set_debug_variable(const char* variableName, uint64 value);
128 extern uint64	get_debug_variable(const char* variableName,
129 					uint64 defaultValue);
130 extern bool		unset_debug_variable(const char* variableName);
131 extern void		unset_all_debug_variables();
132 
133 extern bool		evaluate_debug_expression(const char* expression,
134 					uint64* result, bool silent);
135 extern int		evaluate_debug_command(const char* command);
136 extern status_t	parse_next_debug_command_argument(const char** expressionString,
137 					char* buffer, size_t bufferSize);
138 
139 extern status_t	add_debugger_command_etc(const char* name,
140 					debugger_command_hook func, const char* description,
141 					const char* usage, uint32 flags);
142 extern status_t	add_debugger_command_alias(const char* newName,
143 					const char* oldName, const char* description);
144 extern bool		print_debugger_command_usage(const char* command);
145 extern bool		has_debugger_command(const char* command);
146 
147 extern const char *debug_demangle_symbol(const char* symbol, char* buffer,
148 					size_t bufferSize, bool* _isObjectMethod);
149 extern status_t	debug_get_next_demangled_argument(uint32* _cookie,
150 					const char* symbol, char* name, size_t nameSize,
151 					int32* _type, size_t* _argumentLength);
152 
153 extern struct thread* debug_set_debugged_thread(struct thread* thread);
154 extern struct thread* debug_get_debugged_thread();
155 
156 extern void		_user_debug_output(const char *userString);
157 
158 #ifdef __cplusplus
159 }
160 #endif
161 
162 #endif	/* _KERNEL_DEBUG_H */
163