2 * Copyright 2002-2015, Axel Dörfler, axeld@pinc-software.de
3 * Distributed under the terms of the Haiku License.
5 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
6 * Distributed under the terms of the NewOS License.
8 #ifndef _KERNEL_DEBUG_H
9 #define _KERNEL_DEBUG_H
14 #include <KernelExport.h>
17 #include "kernel_debug_config.h"
20 // We need the BKernel::Thread type below (opaquely) in the exported C
21 // functions below. Since this header is currently still included by plain C
22 // code, we define a dummy type BKernel_Thread in C mode and a equally named
29 using BKernel::Thread
;
30 # define BKernel_Thread Thread
32 typedef struct BKernel_Thread BKernel_Thread
;
37 The kernel debug level.
38 Level 1 is usual asserts, > 1 should be used for very expensive runtime
49 #define ASSERT_ALWAYS(x) \
52 panic("ASSERT FAILED (%s:%d): %s", __FILE__, __LINE__, #x); \
56 #define ASSERT_ALWAYS_PRINT(x, format, args...) \
59 panic("ASSERT FAILED (%s:%d): %s; " format, __FILE__, __LINE__, \
65 # define ASSERT(x) ASSERT_ALWAYS(x)
66 # define ASSERT_PRINT(x, format, args...) ASSERT_ALWAYS_PRINT(x, format, args)
68 # define ASSERT(x) do { } while(0)
69 # define ASSERT_PRINT(x, format, args...) do { } while(0)
72 #define STATIC_ASSERT(x) \
74 struct __staticAssertStruct__ { \
75 char __static_assert_failed__[2*(x) - 1]; \
80 # define KDEBUG_ONLY(x) x
82 # define KDEBUG_ONLY(x) /* nothing */
86 // Macros for for placing marker functions. They can be used to mark the
87 // beginning and end of code sections (e.g. used in the slab code).
88 #define RANGE_MARKER_FUNCTION(functionName) \
89 void functionName() {}
90 #define RANGE_MARKER_FUNCTION_BEGIN(scope) \
91 RANGE_MARKER_FUNCTION(scope##_begin)
92 #define RANGE_MARKER_FUNCTION_END(scope) \
93 RANGE_MARKER_FUNCTION(scope##_end)
95 #define RANGE_MARKER_FUNCTION_PROTOTYPE(functionName) \
97 #define RANGE_MARKER_FUNCTION_PROTOTYPES(scope) \
98 RANGE_MARKER_FUNCTION_PROTOTYPE(scope##_begin) \
99 RANGE_MARKER_FUNCTION_PROTOTYPE(scope##_end)
100 #define RANGE_MARKER_FUNCTION_ADDRESS_RANGE(scope) \
101 (addr_t)&scope##_begin, (addr_t)&scope##_end
104 // command return value
105 #define B_KDEBUG_ERROR 4
106 #define B_KDEBUG_RESTART_PIPE 5
109 #define B_KDEBUG_DONT_PARSE_ARGUMENTS (0x01)
110 #define B_KDEBUG_PIPE_FINAL_RERUN (0x02)
113 struct arch_debug_registers
;
116 struct debugger_module_info
{
119 void (*enter_debugger
)(void);
120 void (*exit_debugger
)(void);
123 int (*debugger_puts
)(const char *str
, int32 length
);
124 int (*debugger_getchar
)(void);
125 // TODO: add hooks for tunnelling gdb ?
128 bool (*emergency_key_pressed
)(char key
);
131 struct debugger_demangle_module_info
{
134 const char* (*demangle_symbol
)(const char* name
, char* buffer
,
135 size_t bufferSize
, bool* _isObjectMethod
);
136 status_t (*get_next_argument
)(uint32
* _cookie
, const char* symbol
,
137 char* name
, size_t nameSize
, int32
* _type
, size_t* _argumentLength
);
141 typedef struct debug_page_fault_info
{
142 addr_t fault_address
;
145 } debug_page_fault_info
;
147 // debug_page_fault_info::flags
148 #define DEBUG_PAGE_FAULT_WRITE 0x01 /* write fault */
149 #define DEBUG_PAGE_FAULT_NO_INFO 0x02 /* fault address and read/write
159 extern void debug_init(struct kernel_args
*args
);
160 extern void debug_init_post_vm(struct kernel_args
*args
);
161 extern void debug_init_post_settings(struct kernel_args
*args
);
162 extern void debug_init_post_modules(struct kernel_args
*args
);
163 extern void debug_early_boot_message(const char *string
);
164 extern void debug_puts(const char *s
, int32 length
);
165 extern bool debug_debugger_running(void);
166 extern bool debug_screen_output_enabled(void);
167 extern void debug_stop_screen_debug_output(void);
168 extern void debug_set_page_fault_info(addr_t faultAddress
, addr_t pc
,
170 extern debug_page_fault_info
* debug_get_page_fault_info();
171 extern void debug_trap_cpu_in_kdl(int32 cpu
, bool returnIfHandedOver
);
172 extern void debug_double_fault(int32 cpu
);
173 extern bool debug_emergency_key_pressed(char key
);
174 extern bool debug_is_kernel_memory_accessible(addr_t address
, size_t size
,
176 extern int debug_call_with_fault_handler(jmp_buf jumpBuffer
,
177 void (*function
)(void*), void* parameter
);
178 extern status_t
debug_memcpy(team_id teamID
, void* to
, const void* from
,
180 extern ssize_t
debug_strlcpy(team_id teamID
, char* to
, const char* from
,
183 extern char kgetc(void);
184 extern void kputs(const char *string
);
185 extern void kputs_unfiltered(const char *string
);
186 extern void kprintf_unfiltered(const char *format
, ...)
187 __attribute__ ((format (__printf__
, 1, 2)));
188 extern void dprintf_no_syslog(const char *format
, ...)
189 __attribute__ ((format (__printf__
, 1, 2)));
191 extern bool is_debug_variable_defined(const char* variableName
);
192 extern bool set_debug_variable(const char* variableName
, uint64 value
);
193 extern uint64
get_debug_variable(const char* variableName
,
194 uint64 defaultValue
);
195 extern bool unset_debug_variable(const char* variableName
);
196 extern void unset_all_debug_variables();
198 extern bool evaluate_debug_expression(const char* expression
,
199 uint64
* result
, bool silent
);
200 extern int evaluate_debug_command(const char* command
);
201 extern status_t
parse_next_debug_command_argument(const char** expressionString
,
202 char* buffer
, size_t bufferSize
);
204 extern status_t
add_debugger_command_etc(const char* name
,
205 debugger_command_hook func
, const char* description
,
206 const char* usage
, uint32 flags
);
207 extern status_t
add_debugger_command_alias(const char* newName
,
208 const char* oldName
, const char* description
);
209 extern bool print_debugger_command_usage(const char* command
);
210 extern bool has_debugger_command(const char* command
);
212 extern const char *debug_demangle_symbol(const char* symbol
, char* buffer
,
213 size_t bufferSize
, bool* _isObjectMethod
);
214 extern status_t
debug_get_next_demangled_argument(uint32
* _cookie
,
215 const char* symbol
, char* name
, size_t nameSize
,
216 int32
* _type
, size_t* _argumentLength
);
218 extern BKernel_Thread
* debug_set_debugged_thread(BKernel_Thread
* thread
);
219 extern BKernel_Thread
* debug_get_debugged_thread();
220 extern bool debug_is_debugged_team(team_id teamID
);
222 extern struct arch_debug_registers
* debug_get_debug_registers(int32 cpu
);
224 extern status_t
_user_kernel_debugger(const char *message
);
225 extern void _user_register_syslog_daemon(port_id port
);
226 extern void _user_debug_output(const char *userString
);
235 struct DebuggedThreadSetter
{
236 DebuggedThreadSetter(Thread
* thread
)
238 fPreviousThread(debug_set_debugged_thread(thread
))
242 ~DebuggedThreadSetter()
244 debug_set_debugged_thread(fPreviousThread
);
248 Thread
* fPreviousThread
;
252 #endif // __cplusplus
255 #endif /* _KERNEL_DEBUG_H */