1 /* 2 * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _DEBUGGER_H 6 #define _DEBUGGER_H 7 8 #include <signal.h> 9 10 #include <image.h> 11 #include <OS.h> 12 13 // include architecture specific definitions 14 #ifdef __INTEL__ 15 #include <arch/x86/arch_debugger.h> 16 #elif __POWERPC__ 17 #include <arch/ppc/arch_debugger.h> 18 #elif __M68K__ 19 #include <arch/m68k/arch_debugger.h> 20 #else 21 #error you need to write a <arch/<cpu>/arch_debugger.h> 22 #endif 23 24 typedef struct debug_cpu_state debug_cpu_state; 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 extern status_t install_default_debugger(port_id debuggerPort); 31 extern port_id install_team_debugger(team_id team, port_id debuggerPort); 32 extern status_t remove_team_debugger(team_id team); 33 extern status_t debug_thread(thread_id thread); 34 extern void wait_for_debugger(void); 35 36 // EXPERIMENTAL: Self-debugging functions. Will fail when a team debugger is 37 // installed. A breakpoint/watchpoint hit will cause the default debugger to 38 // be installed for the team. 39 extern status_t set_debugger_breakpoint(void *address); 40 extern status_t clear_debugger_breakpoint(void *address); 41 extern status_t set_debugger_watchpoint(void *address, uint32 type, 42 int32 length); 43 extern status_t clear_debugger_watchpoint(void *address); 44 45 46 // team debugging flags 47 enum { 48 // event mask: If a flag is set, any of the team's threads will stop when 49 // the respective event occurs. All flags are enabled by default. Always 50 // enabled are debugger() calls and hardware exceptions, as well as the 51 // deletion of the debugged team. 52 B_TEAM_DEBUG_SIGNALS = 0x00010000, 53 B_TEAM_DEBUG_PRE_SYSCALL = 0x00020000, 54 B_TEAM_DEBUG_POST_SYSCALL = 0x00040000, 55 B_TEAM_DEBUG_TEAM_CREATION = 0x00080000, 56 B_TEAM_DEBUG_THREADS = 0x00100000, 57 B_TEAM_DEBUG_IMAGES = 0x00200000, 58 59 // new thread handling 60 B_TEAM_DEBUG_STOP_NEW_THREADS = 0x01000000, 61 62 B_TEAM_DEBUG_USER_FLAG_MASK = 0xffff0000, 63 }; 64 65 // per-thread debugging flags 66 enum { 67 // event mask: If a flag is set, the thread will stop when the respective 68 // event occurs. If there is a corresponding team flag, it is sufficient, 69 // if either is set. Per default none of the flags is set. 70 B_THREAD_DEBUG_PRE_SYSCALL = 0x00010000, 71 B_THREAD_DEBUG_POST_SYSCALL = 0x00020000, 72 73 // child thread handling 74 B_THREAD_DEBUG_STOP_CHILD_THREADS = 0x00100000, 75 B_THREAD_DEBUG_SYSCALL_TRACE_CHILD_THREADS = 0x00200000, 76 77 B_THREAD_DEBUG_USER_FLAG_MASK = 0xffff0000, 78 }; 79 80 // in case of a B_EXCEPTION_OCCURRED event: the type of the exception 81 typedef enum { 82 B_NON_MASKABLE_INTERRUPT = 0, 83 B_MACHINE_CHECK_EXCEPTION, 84 B_SEGMENT_VIOLATION, 85 B_ALIGNMENT_EXCEPTION, 86 B_DIVIDE_ERROR, 87 B_OVERFLOW_EXCEPTION, 88 B_BOUNDS_CHECK_EXCEPTION, 89 B_INVALID_OPCODE_EXCEPTION, 90 B_SEGMENT_NOT_PRESENT, 91 B_STACK_FAULT, 92 B_GENERAL_PROTECTION_FAULT, 93 B_FLOATING_POINT_EXCEPTION, 94 } debug_exception_type; 95 96 // Value indicating how a stopped thread shall continue. 97 enum { 98 B_THREAD_DEBUG_HANDLE_EVENT = 0, // handle the event normally 99 // (e.g. a signal is delivered, a 100 // CPU fault kills the team,...) 101 B_THREAD_DEBUG_IGNORE_EVENT, // ignore the event and continue as if 102 // it didn't occur (e.g. a signal or 103 // a CPU fault will be ignored) 104 }; 105 106 // watchpoint types (ToDo: Check PPC support.) 107 enum { 108 B_DATA_READ_WATCHPOINT = 0, // !x86 109 B_DATA_WRITE_WATCHPOINT, 110 B_DATA_READ_WRITE_WATCHPOINT, 111 }; 112 113 // how to apply signal ignore masks 114 typedef enum { 115 B_DEBUG_SIGNAL_MASK_AND = 0, 116 B_DEBUG_SIGNAL_MASK_OR, 117 B_DEBUG_SIGNAL_MASK_SET, 118 } debug_signal_mask_op; 119 120 #define B_DEBUG_SIGNAL_TO_MASK(signal) (1ULL << ((signal) - 1)) 121 122 // maximal number of bytes to read/write via B_DEBUG_MESSAGE_{READ,WRITE]_MEMORY 123 enum { 124 B_MAX_READ_WRITE_MEMORY_SIZE = 1024, 125 }; 126 127 // messages to the debug nub thread 128 typedef enum { 129 B_DEBUG_MESSAGE_READ_MEMORY = 0, // read from the team's memory 130 B_DEBUG_MESSAGE_WRITE_MEMORY, // write to the team's memory 131 B_DEBUG_MESSAGE_SET_TEAM_FLAGS, // set the team's debugging flags 132 B_DEBUG_MESSAGE_SET_THREAD_FLAGS, // set a thread's debugging flags 133 B_DEBUG_MESSAGE_CONTINUE_THREAD, // continue a stopped thread 134 B_DEBUG_MESSAGE_SET_CPU_STATE, // change a stopped thread's CPU state 135 B_DEBUG_MESSAGE_GET_CPU_STATE, // get the thread's current CPU state 136 B_DEBUG_MESSAGE_SET_BREAKPOINT, // set a breakpoint 137 B_DEBUG_MESSAGE_CLEAR_BREAKPOINT, // clear a breakpoint 138 B_DEBUG_MESSAGE_SET_WATCHPOINT, // set a watchpoint 139 B_DEBUG_MESSAGE_CLEAR_WATCHPOINT, // clear a watchpoint 140 B_DEBUG_MESSAGE_SET_SIGNAL_MASKS, // set/get a thread's masks of signals 141 B_DEBUG_MESSAGE_GET_SIGNAL_MASKS, // the debugger is interested in 142 B_DEBUG_MESSAGE_SET_SIGNAL_HANDLER, // set/get a thread's signal handler for 143 B_DEBUG_MESSAGE_GET_SIGNAL_HANDLER, // a signal 144 145 B_DEBUG_MESSAGE_PREPARE_HANDOVER, // prepares the debugged team for being 146 // handed over to another debugger; 147 // the new debugger can just invoke 148 // install_team_debugger() 149 150 B_DEBUG_START_PROFILER, // start/stop sampling 151 B_DEBUG_STOP_PROFILER // 152 } debug_nub_message; 153 154 // messages sent to the debugger 155 typedef enum { 156 B_DEBUGGER_MESSAGE_THREAD_DEBUGGED = 0, // debugger message in reaction to 157 // an invocation of debug_thread() 158 B_DEBUGGER_MESSAGE_DEBUGGER_CALL, // thread called debugger() 159 B_DEBUGGER_MESSAGE_BREAKPOINT_HIT, // thread hit a breakpoint 160 B_DEBUGGER_MESSAGE_WATCHPOINT_HIT, // thread hit a watchpoint 161 B_DEBUGGER_MESSAGE_SINGLE_STEP, // thread was single-stepped 162 B_DEBUGGER_MESSAGE_PRE_SYSCALL, // begin of a syscall 163 B_DEBUGGER_MESSAGE_POST_SYSCALL, // end of a syscall 164 B_DEBUGGER_MESSAGE_SIGNAL_RECEIVED, // thread received a signal 165 B_DEBUGGER_MESSAGE_EXCEPTION_OCCURRED, // an exception occurred 166 B_DEBUGGER_MESSAGE_TEAM_CREATED, // the debugged team created a new 167 // one 168 B_DEBUGGER_MESSAGE_TEAM_DELETED, // the debugged team is gone 169 B_DEBUGGER_MESSAGE_TEAM_EXEC, // the debugged team executes exec() 170 B_DEBUGGER_MESSAGE_THREAD_CREATED, // a thread has been created 171 B_DEBUGGER_MESSAGE_THREAD_DELETED, // a thread has been deleted 172 B_DEBUGGER_MESSAGE_IMAGE_CREATED, // an image has been created 173 B_DEBUGGER_MESSAGE_IMAGE_DELETED, // an image has been deleted 174 175 B_DEBUGGER_MESSAGE_PROFILER_UPDATE, // flush the profiling buffer for a 176 // thread 177 178 B_DEBUGGER_MESSAGE_HANDED_OVER, // the debugged team has been 179 // handed over to another debugger 180 } debug_debugger_message; 181 182 183 // #pragma mark - 184 // #pragma mark ----- messages to the debug nub thread ----- 185 186 // B_DEBUG_MESSAGE_READ_MEMORY 187 188 typedef struct { 189 port_id reply_port; // port to send the reply to 190 void *address; // address from which to read 191 int32 size; // number of bytes to read 192 } debug_nub_read_memory; 193 194 typedef struct { 195 status_t error; // B_OK, if reading went fine 196 int32 size; // the number of bytes actually read 197 // > 0, iff error == B_OK 198 char data[B_MAX_READ_WRITE_MEMORY_SIZE]; 199 // the read data 200 } debug_nub_read_memory_reply; 201 202 // B_DEBUG_MESSAGE_WRITE_MEMORY 203 204 typedef struct { 205 port_id reply_port; // port to send the reply to 206 void *address; // address to which to write 207 int32 size; // number of bytes to write 208 char data[B_MAX_READ_WRITE_MEMORY_SIZE]; 209 // data to write 210 } debug_nub_write_memory; 211 212 typedef struct { 213 status_t error; // B_OK, if writing went fine 214 int32 size; // the number of bytes actually written 215 } debug_nub_write_memory_reply; 216 217 // B_DEBUG_MESSAGE_SET_TEAM_FLAGS 218 219 typedef struct { 220 int32 flags; // the new team debugging flags 221 } debug_nub_set_team_flags; 222 223 // B_DEBUG_MESSAGE_SET_THREAD_FLAGS 224 225 typedef struct { 226 thread_id thread; // the thread 227 int32 flags; // the new thread debugging flags 228 } debug_nub_set_thread_flags; 229 230 // B_DEBUG_MESSAGE_CONTINUE_THREAD 231 232 typedef struct { 233 thread_id thread; // the thread 234 uint32 handle_event; // how to handle the occurred event 235 bool single_step; // true == single step, false == run full speed 236 } debug_nub_continue_thread; 237 238 // B_DEBUG_MESSAGE_SET_CPU_STATE 239 240 typedef struct { 241 thread_id thread; // the thread 242 debug_cpu_state cpu_state; // the new CPU state 243 } debug_nub_set_cpu_state; 244 245 // B_DEBUG_MESSAGE_GET_CPU_STATE 246 247 typedef struct { 248 port_id reply_port; // port to send the reply to 249 thread_id thread; // the thread 250 } debug_nub_get_cpu_state; 251 252 typedef struct { 253 status_t error; // != B_OK, if something went wrong 254 // (bad thread ID, thread not stopped) 255 debug_debugger_message message; // the reason why the thread stopped 256 debug_cpu_state cpu_state; // the thread's CPU state 257 } debug_nub_get_cpu_state_reply; 258 259 // B_DEBUG_MESSAGE_SET_BREAKPOINT 260 261 typedef struct { 262 port_id reply_port; // port to send the reply to 263 void *address; // breakpoint address 264 } debug_nub_set_breakpoint; 265 266 typedef struct { 267 status_t error; // B_OK, if the breakpoint has been set 268 // successfully 269 } debug_nub_set_breakpoint_reply; 270 271 // B_DEBUG_MESSAGE_CLEAR_BREAKPOINT 272 273 typedef struct { 274 void *address; // breakpoint address 275 } debug_nub_clear_breakpoint; 276 277 // B_DEBUG_MESSAGE_SET_WATCHPOINT 278 279 typedef struct { 280 port_id reply_port; // port to send the reply to 281 void *address; // watchpoint address 282 uint32 type; // watchpoint type (see type constants above) 283 int32 length; // number of bytes to watch (typically 1, 2, 284 // 4); architecture specific alignment 285 // restrictions apply. 286 } debug_nub_set_watchpoint; 287 288 typedef struct { 289 status_t error; // B_OK, if the watchpoint has been set 290 // successfully 291 } debug_nub_set_watchpoint_reply; 292 293 // B_DEBUG_MESSAGE_CLEAR_WATCHPOINT 294 295 typedef struct { 296 void *address; // watchpoint address 297 } debug_nub_clear_watchpoint; 298 299 // B_DEBUG_MESSAGE_SET_SIGNAL_MASKS 300 301 typedef struct { 302 thread_id thread; // the thread 303 uint64 ignore_mask; // the mask for signals the 304 // debugger wishes not to be 305 // notified of 306 uint64 ignore_once_mask; // the mask for signals the 307 // debugger wishes not to be 308 // notified of when they next 309 // occur 310 debug_signal_mask_op ignore_op; // what to do with ignore_mask 311 debug_signal_mask_op ignore_once_op; // what to do with 312 // ignore_once_mask 313 } debug_nub_set_signal_masks; 314 315 // B_DEBUG_MESSAGE_GET_SIGNAL_MASKS 316 317 typedef struct { 318 port_id reply_port; // port to send the reply to 319 thread_id thread; // the thread 320 } debug_nub_get_signal_masks; 321 322 typedef struct { 323 status_t error; // B_OK, if the thread exists 324 uint64 ignore_mask; // the mask for signals the debugger wishes 325 // not to be notified of 326 uint64 ignore_once_mask; // the mask for signals the debugger wishes 327 // not to be notified of when they next 328 // occur 329 } debug_nub_get_signal_masks_reply; 330 331 // B_DEBUG_MESSAGE_SET_SIGNAL_HANDLER 332 333 typedef struct { 334 thread_id thread; // the thread 335 int signal; // the signal 336 struct sigaction handler; // the new signal handler 337 } debug_nub_set_signal_handler; 338 339 // B_DEBUG_MESSAGE_GET_SIGNAL_HANDLER 340 341 typedef struct { 342 port_id reply_port; // port to send the reply to 343 thread_id thread; // the thread 344 int signal; // the signal 345 } debug_nub_get_signal_handler; 346 347 typedef struct { 348 status_t error; // B_OK, if the thread exists 349 struct sigaction handler; // the signal handler 350 } debug_nub_get_signal_handler_reply; 351 352 // B_DEBUG_MESSAGE_PREPARE_HANDOVER 353 354 // no parameters, no reply 355 356 // B_DEBUG_START_PROFILER 357 358 struct debug_profile_function { 359 addr_t base; // function base address 360 size_t size; // function size 361 }; 362 363 typedef struct { 364 port_id reply_port; // port to send the reply to 365 thread_id thread; // thread to profile 366 bigtime_t interval; // sample interval 367 area_id sample_area; // area into which the sample will be 368 // written 369 int32 stack_depth; // number of return address per hit 370 bool variable_stack_depth; 371 // variable number of samples per hit; 372 // cf. debug_profiler_update 373 } debug_nub_start_profiler; 374 375 typedef struct { 376 status_t error; 377 int32 image_event; // number of the last image event 378 bigtime_t interval; // actual sample interval (might 379 // differ from the requested one) 380 } debug_nub_start_profiler_reply; 381 382 // B_DEBUG_STOP_PROFILER 383 384 typedef struct { 385 port_id reply_port; // port to send the reply to 386 thread_id thread; // thread to profile 387 } debug_nub_stop_profiler; 388 389 // reply is debug_profiler_update 390 391 // union of all messages structures sent to the debug nub thread 392 typedef union { 393 debug_nub_read_memory read_memory; 394 debug_nub_write_memory write_memory; 395 debug_nub_set_team_flags set_team_flags; 396 debug_nub_set_thread_flags set_thread_flags; 397 debug_nub_continue_thread continue_thread; 398 debug_nub_set_cpu_state set_cpu_state; 399 debug_nub_get_cpu_state get_cpu_state; 400 debug_nub_set_breakpoint set_breakpoint; 401 debug_nub_clear_breakpoint clear_breakpoint; 402 debug_nub_set_watchpoint set_watchpoint; 403 debug_nub_clear_watchpoint clear_watchpoint; 404 debug_nub_set_signal_masks set_signal_masks; 405 debug_nub_get_signal_masks get_signal_masks; 406 debug_nub_set_signal_handler set_signal_handler; 407 debug_nub_get_signal_handler get_signal_handler; 408 debug_nub_start_profiler start_profiler; 409 debug_nub_stop_profiler stop_profiler; 410 } debug_nub_message_data; 411 412 413 // #pragma mark - 414 // #pragma mark ----- messages to the debugger ----- 415 416 // first member of all debugger messages -- not a message by itself 417 typedef struct { 418 thread_id thread; // the thread being the event origin 419 team_id team; // the thread's team 420 port_id nub_port; // port to debug nub for this team (only set 421 // for synchronous messages) 422 } debug_origin; 423 424 // B_DEBUGGER_MESSAGE_THREAD_DEBUGGED 425 426 typedef struct { 427 debug_origin origin; 428 } debug_thread_debugged; 429 430 // B_DEBUGGER_MESSAGE_DEBUGGER_CALL 431 432 typedef struct { 433 debug_origin origin; 434 void *message; // address of the message passed to 435 // debugger() 436 } debug_debugger_call; 437 438 // B_DEBUGGER_MESSAGE_BREAKPOINT_HIT 439 440 typedef struct { 441 debug_origin origin; 442 debug_cpu_state cpu_state; // cpu state 443 bool software; // true, if the is a software breakpoint 444 // (i.e. caused by a respective trap 445 // instruction) 446 } debug_breakpoint_hit; 447 448 // B_DEBUGGER_MESSAGE_WATCHPOINT_HIT 449 450 typedef struct { 451 debug_origin origin; 452 debug_cpu_state cpu_state; // cpu state 453 } debug_watchpoint_hit; 454 455 // B_DEBUGGER_MESSAGE_SINGLE_STEP 456 457 typedef struct { 458 debug_origin origin; 459 debug_cpu_state cpu_state; // cpu state 460 } debug_single_step; 461 462 // B_DEBUGGER_MESSAGE_PRE_SYSCALL 463 464 typedef struct { 465 debug_origin origin; 466 uint32 syscall; // the syscall number 467 uint32 args[16]; // syscall arguments 468 } debug_pre_syscall; 469 470 // B_DEBUGGER_MESSAGE_POST_SYSCALL 471 472 typedef struct { 473 debug_origin origin; 474 bigtime_t start_time; // time of syscall start 475 bigtime_t end_time; // time of syscall completion 476 uint64 return_value; // the syscall's return value 477 uint32 syscall; // the syscall number 478 uint32 args[16]; // syscall arguments 479 } debug_post_syscall; 480 481 // B_DEBUGGER_MESSAGE_SIGNAL_RECEIVED 482 483 typedef struct { 484 debug_origin origin; 485 int signal; // the signal 486 struct sigaction handler; // the signal handler 487 bool deadly; // true, if handling the signal will kill 488 // the team 489 } debug_signal_received; 490 491 // B_DEBUGGER_MESSAGE_EXCEPTION_OCCURRED 492 493 typedef struct { 494 debug_origin origin; 495 debug_exception_type exception; // the exception 496 int signal; // the signal that will be sent, 497 // when the thread continues 498 // normally 499 } debug_exception_occurred; 500 501 // B_DEBUGGER_MESSAGE_TEAM_CREATED 502 503 typedef struct { 504 debug_origin origin; 505 team_id new_team; // the newly created team 506 } debug_team_created; 507 508 // B_DEBUGGER_MESSAGE_TEAM_DELETED 509 510 typedef struct { 511 debug_origin origin; // thread is < 0, team is the deleted team 512 // (asynchronous message) 513 } debug_team_deleted; 514 515 // B_DEBUGGER_MESSAGE_TEAM_EXEC 516 517 typedef struct { 518 debug_origin origin; 519 int32 image_event; // number of the image event 520 } debug_team_exec; 521 522 // B_DEBUGGER_MESSAGE_THREAD_CREATED 523 524 typedef struct { 525 debug_origin origin; // the thread that created the new thread 526 team_id new_thread; // the newly created thread 527 } debug_thread_created; 528 529 // B_DEBUGGER_MESSAGE_THREAD_DELETED 530 531 typedef struct { 532 debug_origin origin; // the deleted thread (asynchronous message) 533 } debug_thread_deleted; 534 535 // B_DEBUGGER_MESSAGE_IMAGE_CREATED 536 537 typedef struct { 538 debug_origin origin; 539 image_info info; // info for the image 540 int32 image_event; // number of the image event 541 } debug_image_created; 542 543 // B_DEBUGGER_MESSAGE_IMAGE_DELETED 544 545 typedef struct { 546 debug_origin origin; 547 image_info info; // info for the image 548 int32 image_event; // number of the image event 549 } debug_image_deleted; 550 551 // B_DEBUGGER_MESSAGE_PROFILER_UPDATE 552 553 typedef struct { 554 debug_origin origin; 555 int32 image_event; // number of the last image event; all 556 // samples were recorded after this 557 // event and before the next one 558 int32 stack_depth; // number of return addresses per tick 559 int32 sample_count; // number of samples in the buffer 560 int32 dropped_ticks; // number of ticks that had been 561 // dropped, since the buffer was full 562 bool variable_stack_depth; 563 // the number of samples per hit is 564 // variable, i.e. the format for the 565 // samples of a hit in the buffer is 566 // <n> <sample 1> ... <sample n> 567 // instead of 568 // <sample 1> ... <sample stack_depth> 569 bool stopped; // if true, the thread is no longer 570 // being profiled 571 } debug_profiler_update; 572 573 // B_DEBUGGER_MESSAGE_HANDED_OVER 574 575 typedef struct { 576 debug_origin origin; // thread is < 0, team is the deleted team 577 // (asynchronous message) 578 team_id debugger; // the new debugger 579 port_id debugger_port; // the port the new debugger uses 580 } debug_handed_over; 581 582 // union of all messages structures sent to the debugger 583 typedef union { 584 debug_thread_debugged thread_debugged; 585 debug_debugger_call debugger_call; 586 debug_breakpoint_hit breakpoint_hit; 587 debug_watchpoint_hit watchpoint_hit; 588 debug_single_step single_step; 589 debug_pre_syscall pre_syscall; 590 debug_post_syscall post_syscall; 591 debug_signal_received signal_received; 592 debug_exception_occurred exception_occurred; 593 debug_team_created team_created; 594 debug_team_deleted team_deleted; 595 debug_team_exec team_exec; 596 debug_thread_created thread_created; 597 debug_thread_deleted thread_deleted; 598 debug_image_created image_created; 599 debug_image_deleted image_deleted; 600 debug_profiler_update profiler_update; 601 debug_handed_over handed_over; 602 603 debug_origin origin; // for convenience (no real message) 604 } debug_debugger_message_data; 605 606 607 extern void get_debug_message_string(debug_debugger_message message, 608 char *buffer, int32 bufferSize); 609 extern void get_debug_exception_string(debug_exception_type exception, 610 char *buffer, int32 bufferSize); 611 612 613 #ifdef __cplusplus 614 } // extern "C" 615 #endif 616 617 #endif // _DEBUGGER_H 618