1 /* 2 * Copyright 2002-2015, Axel Dörfler, axeld@pinc-software.de 3 * Distributed under the terms of the MIT 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 // 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 23 // macro in C++ mode. 24 #ifdef __cplusplus 25 namespace BKernel { 26 struct Thread; 27 } 28 29 using BKernel::Thread; 30 # define BKernel_Thread Thread 31 #else 32 typedef struct BKernel_Thread BKernel_Thread; 33 #endif 34 35 36 /* KDEBUG 37 The kernel debug level. 38 Level 1 is usual asserts, > 1 should be used for very expensive runtime 39 checks 40 */ 41 #if !defined(KDEBUG) 42 # if DEBUG 43 # define KDEBUG 1 44 # else 45 # define KDEBUG 0 46 # endif 47 #endif 48 49 #define ASSERT_ALWAYS(x) \ 50 do { \ 51 if (!(x)) { \ 52 panic("ASSERT FAILED (%s:%d): %s", __FILE__, __LINE__, #x); \ 53 } \ 54 } while (0) 55 56 #define ASSERT_ALWAYS_PRINT(x, format, args...) \ 57 do { \ 58 if (!(x)) { \ 59 panic("ASSERT FAILED (%s:%d): %s; " format, __FILE__, __LINE__, \ 60 #x, args); \ 61 } \ 62 } while (0) 63 64 #if KDEBUG 65 # define ASSERT(x) ASSERT_ALWAYS(x) 66 # define ASSERT_PRINT(x, format, args...) ASSERT_ALWAYS_PRINT(x, format, args) 67 # define ASSERT_UNREACHABLE() panic("ASSERT UNREACHABLE (%s:%d)", __FILE__, __LINE__) 68 #else 69 # define ASSERT(x) do { } while(0) 70 # define ASSERT_PRINT(x, format, args...) do { } while(0) 71 # define ASSERT_UNREACHABLE() __builtin_unreachable() 72 #endif 73 74 #if __GNUC__ >= 5 && !defined(__cplusplus) 75 # define STATIC_ASSERT(x) _Static_assert(x, "static assert failed!") 76 #elif defined(__cplusplus) && __cplusplus >= 201103L 77 # define STATIC_ASSERT(x) static_assert(x, "static assert failed!") 78 #else 79 # define STATIC_ASSERT(x) \ 80 do { \ 81 struct __staticAssertStruct__ { \ 82 char __static_assert_failed__[2*(x) - 1]; \ 83 }; \ 84 } while (false) 85 #endif 86 87 #if KDEBUG 88 # define KDEBUG_ONLY(x) x 89 #else 90 # define KDEBUG_ONLY(x) /* nothing */ 91 #endif 92 93 94 // Macros for for placing marker functions. They can be used to mark the 95 // beginning and end of code sections (e.g. used in the slab code). 96 // Note that in order for these to work, the kernel must be built with -fno-toplevel-reorder. 97 #define RANGE_MARKER_FUNCTION(functionName) \ 98 void RANGE_MARKER_##functionName() {} 99 #define RANGE_MARKER_FUNCTION_BEGIN(scope) \ 100 RANGE_MARKER_FUNCTION(scope##_begin) 101 #define RANGE_MARKER_FUNCTION_END(scope) \ 102 RANGE_MARKER_FUNCTION(scope##_end) 103 104 #define RANGE_MARKER_FUNCTION_PROTOTYPE(functionName) \ 105 void RANGE_MARKER_##functionName(); 106 #define RANGE_MARKER_FUNCTION_PROTOTYPES(scope) \ 107 RANGE_MARKER_FUNCTION_PROTOTYPE(scope##_begin) \ 108 RANGE_MARKER_FUNCTION_PROTOTYPE(scope##_end) 109 #define RANGE_MARKER_FUNCTION_ADDRESS_RANGE(scope) \ 110 (addr_t)&RANGE_MARKER_##scope##_begin, (addr_t)&RANGE_MARKER_##scope##_end 111 112 113 // command return value 114 #define B_KDEBUG_ERROR 4 115 #define B_KDEBUG_RESTART_PIPE 5 116 117 // command flags 118 #define B_KDEBUG_DONT_PARSE_ARGUMENTS (0x01) 119 #define B_KDEBUG_PIPE_FINAL_RERUN (0x02) 120 121 122 struct arch_debug_registers; 123 124 125 struct debugger_module_info { 126 module_info info; 127 128 void (*enter_debugger)(void); 129 void (*exit_debugger)(void); 130 131 // I/O hooks 132 int (*debugger_puts)(const char *str, int32 length); 133 int (*debugger_getchar)(void); 134 // TODO: add hooks for tunnelling gdb ? 135 136 // Misc. hooks 137 bool (*emergency_key_pressed)(char key); 138 }; 139 140 struct debugger_demangle_module_info { 141 module_info info; 142 143 const char* (*demangle_symbol)(const char* name, char* buffer, 144 size_t bufferSize, bool* _isObjectMethod); 145 status_t (*get_next_argument)(uint32* _cookie, const char* symbol, 146 char* name, size_t nameSize, int32* _type, size_t* _argumentLength); 147 }; 148 149 150 typedef struct debug_page_fault_info { 151 addr_t fault_address; 152 addr_t pc; 153 uint32 flags; 154 } debug_page_fault_info; 155 156 // debug_page_fault_info::flags 157 #define DEBUG_PAGE_FAULT_WRITE 0x01 /* write fault */ 158 #define DEBUG_PAGE_FAULT_NO_INFO 0x02 /* fault address and read/write 159 unknown */ 160 161 162 #ifdef __cplusplus 163 extern "C" { 164 #endif 165 166 struct kernel_args; 167 168 extern void debug_init(struct kernel_args *args); 169 extern void debug_init_post_vm(struct kernel_args *args); 170 extern void debug_init_post_settings(struct kernel_args *args); 171 extern void debug_init_post_modules(struct kernel_args *args); 172 extern void debug_early_boot_message(const char *string); 173 extern void debug_puts(const char *s, int32 length); 174 extern bool debug_debugger_running(void); 175 extern bool debug_screen_output_enabled(void); 176 extern void debug_stop_screen_debug_output(void); 177 extern void debug_set_page_fault_info(addr_t faultAddress, addr_t pc, 178 uint32 flags); 179 extern debug_page_fault_info* debug_get_page_fault_info(); 180 extern void debug_trap_cpu_in_kdl(int32 cpu, bool returnIfHandedOver); 181 extern void debug_double_fault(int32 cpu); 182 extern bool debug_emergency_key_pressed(char key); 183 extern bool debug_is_kernel_memory_accessible(addr_t address, size_t size, 184 uint32 protection); 185 extern int debug_call_with_fault_handler(jmp_buf jumpBuffer, 186 void (*function)(void*), void* parameter); 187 extern status_t debug_memcpy(team_id teamID, void* to, const void* from, 188 size_t size); 189 extern ssize_t debug_strlcpy(team_id teamID, char* to, const char* from, 190 size_t size); 191 192 extern char kgetc(void); 193 extern void kputs(const char *string); 194 extern void kputs_unfiltered(const char *string); 195 extern void kprintf_unfiltered(const char *format, ...) 196 __attribute__ ((format (__printf__, 1, 2))); 197 extern void dprintf_no_syslog(const char *format, ...) 198 __attribute__ ((format (__printf__, 1, 2))); 199 200 extern bool is_debug_variable_defined(const char* variableName); 201 extern bool set_debug_variable(const char* variableName, uint64 value); 202 extern uint64 get_debug_variable(const char* variableName, 203 uint64 defaultValue); 204 extern bool unset_debug_variable(const char* variableName); 205 extern void unset_all_debug_variables(); 206 207 extern bool evaluate_debug_expression(const char* expression, 208 uint64* result, bool silent); 209 extern int evaluate_debug_command(const char* command); 210 extern status_t parse_next_debug_command_argument(const char** expressionString, 211 char* buffer, size_t bufferSize); 212 213 extern status_t add_debugger_command_etc(const char* name, 214 debugger_command_hook func, const char* description, 215 const char* usage, uint32 flags); 216 extern status_t add_debugger_command_alias(const char* newName, 217 const char* oldName, const char* description); 218 extern bool print_debugger_command_usage(const char* command); 219 extern bool has_debugger_command(const char* command); 220 221 extern const char *debug_demangle_symbol(const char* symbol, char* buffer, 222 size_t bufferSize, bool* _isObjectMethod); 223 extern status_t debug_get_next_demangled_argument(uint32* _cookie, 224 const char* symbol, char* name, size_t nameSize, 225 int32* _type, size_t* _argumentLength); 226 227 extern BKernel_Thread* debug_set_debugged_thread(BKernel_Thread* thread); 228 extern BKernel_Thread* debug_get_debugged_thread(); 229 extern bool debug_is_debugged_team(team_id teamID); 230 231 extern struct arch_debug_registers* debug_get_debug_registers(int32 cpu); 232 233 extern status_t _user_kernel_debugger(const char *message); 234 extern void _user_register_syslog_daemon(port_id port); 235 extern void _user_debug_output(const char *userString); 236 237 #ifdef __cplusplus 238 } 239 #endif 240 241 242 #ifdef __cplusplus 243 244 struct DebuggedThreadSetter { 245 DebuggedThreadSetter(Thread* thread) 246 : 247 fPreviousThread(debug_set_debugged_thread(thread)) 248 { 249 } 250 251 ~DebuggedThreadSetter() 252 { 253 debug_set_debugged_thread(fPreviousThread); 254 } 255 256 private: 257 Thread* fPreviousThread; 258 }; 259 260 261 #endif // __cplusplus 262 263 264 #endif /* _KERNEL_DEBUG_H */ 265