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