1 /* 2 * Copyright 2005-2011, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2013, Rene Gollent, rene@gollent.com. 4 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de. 5 * Distributed under the terms of the MIT License. 6 */ 7 8 9 #include <ctype.h> 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 #include <strings.h> 14 #include <errno.h> 15 #include <signal.h> 16 17 #include <algorithm> 18 #include <map> 19 #include <string> 20 #include <vector> 21 22 #include <debugger.h> 23 #include <image.h> 24 #include <syscalls.h> 25 26 #include "debug_utils.h" 27 28 #include "Context.h" 29 #include "MemoryReader.h" 30 #include "Syscall.h" 31 #include "TypeHandler.h" 32 33 34 using std::map; 35 using std::string; 36 using std::vector; 37 38 39 struct syscall_stats { 40 bigtime_t time; 41 uint32 count; 42 }; 43 44 45 extern void get_syscalls0(vector<Syscall*> &syscalls); 46 extern void get_syscalls1(vector<Syscall*> &syscalls); 47 extern void get_syscalls2(vector<Syscall*> &syscalls); 48 extern void get_syscalls3(vector<Syscall*> &syscalls); 49 extern void get_syscalls4(vector<Syscall*> &syscalls); 50 extern void get_syscalls5(vector<Syscall*> &syscalls); 51 extern void get_syscalls6(vector<Syscall*> &syscalls); 52 extern void get_syscalls7(vector<Syscall*> &syscalls); 53 extern void get_syscalls8(vector<Syscall*> &syscalls); 54 extern void get_syscalls9(vector<Syscall*> &syscalls); 55 extern void get_syscalls10(vector<Syscall*> &syscalls); 56 extern void get_syscalls11(vector<Syscall*> &syscalls); 57 extern void get_syscalls12(vector<Syscall*> &syscalls); 58 extern void get_syscalls13(vector<Syscall*> &syscalls); 59 extern void get_syscalls14(vector<Syscall*> &syscalls); 60 extern void get_syscalls15(vector<Syscall*> &syscalls); 61 extern void get_syscalls16(vector<Syscall*> &syscalls); 62 extern void get_syscalls17(vector<Syscall*> &syscalls); 63 extern void get_syscalls18(vector<Syscall*> &syscalls); 64 extern void get_syscalls19(vector<Syscall*> &syscalls); 65 66 67 extern const char *__progname; 68 static const char *kCommandName = __progname; 69 70 71 // usage 72 static const char *kUsage = 73 "Usage: %s [ <options> ] [ <thread or team ID> | <executable with args> ]\n" 74 "\n" 75 "Traces the syscalls of a thread or a team. If an executable with\n" 76 "arguments is supplied, it is loaded and it's main thread traced.\n" 77 "\n" 78 "Options:\n" 79 " -a - Don't print syscall arguments.\n" 80 " -c - Record and dump syscall usage statistics.\n" 81 " -C - Same as -c, but also print syscalls as usual.\n" 82 " -d <name> - Filter the types that have their contents retrieved.\n" 83 " <name> is one of: strings, enums, simple, complex or\n" 84 " pointer_values\n" 85 " -f - Fast mode. Syscall arguments contents aren't retrieved.\n" 86 " -h, --help - Print this text.\n" 87 " -i - Print integers in decimal format instead of hexadecimal.\n" 88 " -l - Also trace loading the executable. Only considered when\n" 89 " an executable is provided.\n" 90 " --no-color - Don't colorize output.\n" 91 " -r - Don't print syscall return values.\n" 92 " -s - Also trace all threads spawned by the supplied thread,\n" 93 " respectively the loaded executable's main thread.\n" 94 " -t - Also recursively trace all teams created by a traced\n" 95 " thread or team.\n" 96 " -T - Trace all threads of the supplied or loaded executable's\n" 97 " team. If an ID is supplied, it is interpreted as a team\n" 98 " ID.\n" 99 " -o <file> - directs output into the specified file.\n" 100 " -S - prints output to serial debug line.\n" 101 " -g - turns off signal tracing.\n" 102 ; 103 104 105 // terminal color escape sequences 106 // (http://www.dee.ufcg.edu.br/~rrbrandt/tools/ansi.html) 107 static const char *kTerminalTextNormal = "\33[0m"; 108 static const char *kTerminalTextRed = "\33[31m"; 109 static const char *kTerminalTextMagenta = "\33[35m"; 110 static const char *kTerminalTextBlue = "\33[34m"; 111 112 113 // signal names 114 static const char *kSignalName[] = { 115 /* 0 */ "SIG0", 116 /* 1 */ "SIGHUP", 117 /* 2 */ "SIGINT", 118 /* 3 */ "SIGQUIT", 119 /* 4 */ "SIGILL", 120 /* 5 */ "SIGCHLD", 121 /* 6 */ "SIGABRT", 122 /* 7 */ "SIGPIPE", 123 /* 8 */ "SIGFPE", 124 /* 9 */ "SIGKILL", 125 /* 10 */ "SIGSTOP", 126 /* 11 */ "SIGSEGV", 127 /* 12 */ "SIGCONT", 128 /* 13 */ "SIGTSTP", 129 /* 14 */ "SIGALRM", 130 /* 15 */ "SIGTERM", 131 /* 16 */ "SIGTTIN", 132 /* 17 */ "SIGTTOU", 133 /* 18 */ "SIGUSR1", 134 /* 19 */ "SIGUSR2", 135 /* 20 */ "SIGWINCH", 136 /* 21 */ "SIGKILLTHR", 137 /* 22 */ "SIGTRAP", 138 /* 23 */ "SIGPOLL", 139 /* 24 */ "SIGPROF", 140 /* 25 */ "SIGSYS", 141 /* 26 */ "SIGURG", 142 /* 27 */ "SIGVTALRM", 143 /* 28 */ "SIGXCPU", 144 /* 29 */ "SIGXFSZ", 145 /* 30 */ "SIGBUS", 146 /* 31 */ "SIGRESERVED1", 147 /* 32 */ "SIGRESERVED2", 148 }; 149 150 151 // command line args 152 static int sArgc; 153 static const char *const *sArgv; 154 155 // syscalls 156 static vector<Syscall*> sSyscallVector; 157 static map<string, Syscall*> sSyscallMap; 158 159 // statistics 160 typedef map<string, syscall_stats> StatsMap; 161 static StatsMap sSyscallStats; 162 static bigtime_t sSyscallTime; 163 164 165 struct Team { 166 Team(team_id id) 167 : 168 fID(id), 169 fNubPort(-1) 170 { 171 } 172 173 team_id ID() const 174 { 175 return fID; 176 } 177 178 port_id NubPort() const 179 { 180 return fNubPort; 181 } 182 183 MemoryReader& GetMemoryReader() 184 { 185 return fMemoryReader; 186 } 187 188 status_t InstallDebugger(port_id debuggerPort, bool traceTeam, 189 bool traceChildTeams, bool traceSignal) 190 { 191 fNubPort = install_team_debugger(fID, debuggerPort); 192 if (fNubPort < 0) { 193 fprintf(stderr, "%s: Failed to install team debugger: %s\n", 194 kCommandName, strerror(fNubPort)); 195 return fNubPort; 196 } 197 198 // set team debugging flags 199 int32 teamDebugFlags = (traceTeam ? B_TEAM_DEBUG_POST_SYSCALL : 0) 200 | (traceChildTeams ? B_TEAM_DEBUG_TEAM_CREATION : 0) 201 | (traceSignal ? B_TEAM_DEBUG_SIGNALS : 0); 202 if (set_team_debugging_flags(fNubPort, teamDebugFlags) != B_OK) 203 exit(1); 204 205 return fMemoryReader.Init(fNubPort); 206 } 207 208 private: 209 team_id fID; 210 port_id fNubPort; 211 MemoryReader fMemoryReader; 212 }; 213 214 215 static void 216 print_usage(bool error) 217 { 218 // print usage 219 fprintf((error ? stderr : stdout), kUsage, kCommandName); 220 } 221 222 223 static void 224 print_usage_and_exit(bool error) 225 { 226 print_usage(error); 227 exit(error ? 1 : 0); 228 } 229 230 231 static bool 232 get_id(const char *str, int32 &id) 233 { 234 int32 len = strlen(str); 235 for (int32 i = 0; i < len; i++) { 236 if (!isdigit(str[i])) 237 return false; 238 } 239 240 id = atol(str); 241 return true; 242 } 243 244 245 Syscall * 246 get_syscall(const char *name) 247 { 248 map<string, Syscall *>::const_iterator i = sSyscallMap.find(name); 249 if (i == sSyscallMap.end()) 250 return NULL; 251 252 return i->second; 253 } 254 255 256 static void 257 patch_syscalls() 258 { 259 // instead of having this done here manually we should either add the 260 // patching step to gensyscalls also manually or add metadata to 261 // kernel/syscalls.h and have it parsed automatically 262 extern void patch_ioctl(); 263 264 patch_ioctl(); 265 } 266 267 268 static void 269 init_syscalls() 270 { 271 // init the syscall vector 272 get_syscalls0(sSyscallVector); 273 get_syscalls1(sSyscallVector); 274 get_syscalls2(sSyscallVector); 275 get_syscalls3(sSyscallVector); 276 get_syscalls4(sSyscallVector); 277 get_syscalls5(sSyscallVector); 278 get_syscalls6(sSyscallVector); 279 get_syscalls7(sSyscallVector); 280 get_syscalls8(sSyscallVector); 281 get_syscalls9(sSyscallVector); 282 get_syscalls10(sSyscallVector); 283 get_syscalls11(sSyscallVector); 284 get_syscalls12(sSyscallVector); 285 get_syscalls13(sSyscallVector); 286 get_syscalls14(sSyscallVector); 287 get_syscalls15(sSyscallVector); 288 get_syscalls16(sSyscallVector); 289 get_syscalls17(sSyscallVector); 290 get_syscalls18(sSyscallVector); 291 get_syscalls19(sSyscallVector); 292 293 // init the syscall map 294 int32 count = sSyscallVector.size(); 295 for (int32 i = 0; i < count; i++) { 296 Syscall *syscall = sSyscallVector[i]; 297 sSyscallMap[syscall->Name()] = syscall; 298 } 299 300 patch_syscalls(); 301 } 302 303 304 static void 305 record_syscall_stats(const Syscall& syscall, debug_post_syscall& message) 306 { 307 syscall_stats& stats = sSyscallStats[syscall.Name()]; 308 stats.count++; 309 310 bigtime_t time = message.end_time - message.start_time; 311 stats.time += time; 312 sSyscallTime += time; 313 } 314 315 316 static void 317 print_buffer(FILE *outputFile, char* buffer, int32 length) 318 { 319 // output either to file or serial debug line 320 if (outputFile != NULL) 321 fwrite(buffer, length, 1, outputFile); 322 else 323 _kern_debug_output(buffer); 324 } 325 326 327 static void 328 print_to_string(char **_buffer, int32 *_length, const char *format, ...) 329 { 330 va_list list; 331 va_start(list, format); 332 ssize_t length = vsnprintf(*_buffer, *_length, format, list); 333 va_end(list); 334 335 *_buffer += length; 336 *_length -= length; 337 } 338 339 340 static void 341 print_syscall(FILE *outputFile, Syscall* syscall, debug_post_syscall &message, 342 MemoryReader &memoryReader, bool printArguments, uint32 contentsFlags, 343 bool printReturnValue, bool colorize, bool decimal) 344 { 345 char buffer[4096], *string = buffer; 346 int32 length = (int32)sizeof(buffer); 347 348 Context ctx(syscall, (char *)message.args, memoryReader, 349 contentsFlags, decimal); 350 351 // print syscall name, without the "_kern_" 352 if (colorize) { 353 print_to_string(&string, &length, "[%6" B_PRId32 "] %s%s%s(", 354 message.origin.thread, kTerminalTextBlue, 355 syscall->Name().c_str() + 6, kTerminalTextNormal); 356 } else { 357 print_to_string(&string, &length, "[%6" B_PRId32 "] %s(", 358 message.origin.thread, syscall->Name().c_str() + 6); 359 } 360 361 // print arguments 362 if (printArguments) { 363 int32 count = syscall->CountParameters(); 364 for (int32 i = 0; i < count; i++) { 365 // get the value 366 Parameter *parameter = syscall->ParameterAt(i); 367 TypeHandler *handler = parameter->Handler(); 368 ::string value = 369 handler->GetParameterValue(ctx, parameter, 370 ctx.GetValue(parameter)); 371 372 print_to_string(&string, &length, (i > 0 ? ", %s" : "%s"), 373 value.c_str()); 374 } 375 } 376 377 print_to_string(&string, &length, ")"); 378 379 // print return value 380 if (printReturnValue) { 381 Type *returnType = syscall->ReturnType(); 382 TypeHandler *handler = returnType->Handler(); 383 ::string value = handler->GetReturnValue(ctx, message.return_value); 384 if (value.length() > 0) { 385 print_to_string(&string, &length, " = %s", value.c_str()); 386 387 // if the return type is status_t or ssize_t, print human-readable 388 // error codes 389 if (returnType->TypeName() == "status_t" 390 || ((returnType->TypeName() == "ssize_t" 391 || returnType->TypeName() == "int") 392 && message.return_value < 0)) { 393 print_to_string(&string, &length, " %s", strerror(message.return_value)); 394 } 395 } 396 } 397 398 if (colorize) { 399 print_to_string(&string, &length, " %s(%lld us)%s\n", kTerminalTextMagenta, 400 message.end_time - message.start_time, kTerminalTextNormal); 401 } else { 402 print_to_string(&string, &length, " (%lld us)\n", 403 message.end_time - message.start_time); 404 } 405 406 //for (int32 i = 0; i < 16; i++) { 407 // if (i % 4 == 0) { 408 // if (i > 0) 409 // printf("\n"); 410 // printf(" "); 411 // } else 412 // printf(" "); 413 // printf("%08lx", message.args[i]); 414 //} 415 //printf("\n"); 416 print_buffer(outputFile, buffer, sizeof(buffer) - length); 417 } 418 419 420 static const char * 421 signal_name(int signal) 422 { 423 if (signal >= 0 && signal <= SIGRESERVED2) 424 return kSignalName[signal]; 425 426 static char buffer[32]; 427 sprintf(buffer, "%d", signal); 428 return buffer; 429 } 430 431 432 static void 433 print_signal(FILE *outputFile, debug_signal_received &message, 434 bool colorize) 435 { 436 char buffer[4096], *string = buffer; 437 int32 length = (int32)sizeof(buffer); 438 int signalNumber = message.signal; 439 440 // print signal name 441 if (colorize) { 442 print_to_string(&string, &length, "[%6" B_PRId32 "] --- %s%s (%s) %s---\n", 443 message.origin.thread, kTerminalTextRed, signal_name(signalNumber), 444 strsignal(signalNumber), kTerminalTextNormal); 445 } else { 446 print_to_string(&string, &length, "[%6" B_PRId32 "] --- %s (%s) ---\n", 447 message.origin.thread, signal_name(signalNumber), 448 strsignal(signalNumber)); 449 } 450 451 print_buffer(outputFile, buffer, sizeof(buffer) - length); 452 } 453 454 455 static bool 456 compare_stats_by_time( 457 const std::pair<const std::string*, const syscall_stats*>& a, 458 const std::pair<const std::string*, const syscall_stats*>& b) 459 { 460 return a.second->time > b.second->time; 461 } 462 463 464 static void 465 print_stats(FILE* outputFile) 466 { 467 char buffer[4096], *string = buffer; 468 int32 length = (int32)sizeof(buffer); 469 470 typedef std::vector<std::pair<const std::string*, const syscall_stats*> > 471 StatsRefVector; 472 StatsRefVector calls; 473 StatsMap::const_iterator iterator = sSyscallStats.begin(); 474 for (; iterator != sSyscallStats.end(); iterator++) 475 calls.push_back(std::make_pair(&iterator->first, &iterator->second)); 476 477 // Sort calls by time spent 478 std::sort(calls.begin(), calls.end(), compare_stats_by_time); 479 480 print_to_string(&string, &length, "\n%-6s %-10s %-7s %-10s Syscall\n", 481 "Time %", "Usecs", "Calls", "Usecs/call"); 482 print_to_string(&string, &length, "------ ---------- ------- ---------- " 483 "--------------------\n"); 484 485 StatsRefVector::const_iterator callIterator = calls.begin(); 486 for (; callIterator != calls.end(); callIterator++) { 487 const syscall_stats& stats = *callIterator->second; 488 double percent = stats.time * 100.0 / sSyscallTime; 489 bigtime_t perCall = stats.time / stats.count; 490 491 print_to_string(&string, &length, "%6.2f %10" B_PRIu64 " %7" B_PRIu32 492 " %10" B_PRIu64 " %s\n", percent, stats.time, stats.count, perCall, 493 callIterator->first->c_str()); 494 } 495 496 print_buffer(outputFile, buffer, sizeof(buffer) - length); 497 } 498 499 500 int 501 main(int argc, const char *const *argv) 502 { 503 sArgc = argc; 504 sArgv = argv; 505 506 // parameters 507 const char *const *programArgs = NULL; 508 int32 programArgCount = 0; 509 bool printArguments = true; 510 bool colorize = true; 511 bool stats = false; 512 bool trace = true; 513 uint32 contentsFlags = 0; 514 bool decimalFormat = false; 515 bool fastMode = false; 516 bool traceLoading = false; 517 bool printReturnValues = true; 518 bool traceChildThreads = false; 519 bool traceTeam = false; 520 bool traceChildTeams = false; 521 bool traceSignal = true; 522 bool serialOutput = false; 523 FILE *outputFile = stdout; 524 525 // parse arguments 526 for (int argi = 1; argi < argc; argi++) { 527 const char *arg = argv[argi]; 528 if (arg[0] == '-') { 529 // ToDo: improve option parsing so that ie. "-rsf" would also work 530 if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) { 531 print_usage_and_exit(false); 532 } else if (strcmp(arg, "-a") == 0) { 533 printArguments = false; 534 } else if (strcmp(arg, "-c") == 0) { 535 stats = true; 536 trace = false; 537 } else if (strcmp(arg, "-C") == 0) { 538 stats = true; 539 } else if (strcmp(arg, "--no-color") == 0) { 540 colorize = false; 541 } else if (strcmp(arg, "-d") == 0) { 542 const char *what = NULL; 543 544 if (arg[2] == '\0' 545 && argi + 1 < argc && argv[argi + 1][0] != '-') { 546 // next arg is what 547 what = argv[++argi]; 548 } else 549 print_usage_and_exit(true); 550 551 if (strcasecmp(what, "strings") == 0) 552 contentsFlags |= Context::STRINGS; 553 else if (strcasecmp(what, "enums") == 0) 554 contentsFlags |= Context::ENUMERATIONS; 555 else if (strcasecmp(what, "simple") == 0) 556 contentsFlags |= Context::SIMPLE_STRUCTS; 557 else if (strcasecmp(what, "complex") == 0) 558 contentsFlags |= Context::COMPLEX_STRUCTS; 559 else if (strcasecmp(what, "pointer_values") == 0) 560 contentsFlags |= Context::POINTER_VALUES; 561 else { 562 fprintf(stderr, "%s: Unknown content filter `%s'\n", 563 kCommandName, what); 564 exit(1); 565 } 566 } else if (strcmp(arg, "-f") == 0) { 567 fastMode = true; 568 } else if (strcmp(arg, "-i") == 0) { 569 decimalFormat = true; 570 } else if (strcmp(arg, "-l") == 0) { 571 traceLoading = true; 572 } else if (strcmp(arg, "-r") == 0) { 573 printReturnValues = false; 574 } else if (strcmp(arg, "-s") == 0) { 575 traceChildThreads = true; 576 } else if (strcmp(arg, "-t") == 0) { 577 traceChildTeams = true; 578 } else if (strcmp(arg, "-T") == 0) { 579 traceTeam = true; 580 } else if (strcmp(arg, "-g") == 0) { 581 traceSignal = false; 582 } else if (strcmp(arg, "-S") == 0) { 583 serialOutput = true; 584 outputFile = NULL; 585 } else if (strncmp(arg, "-o", 2) == 0) { 586 // read filename 587 const char *filename = NULL; 588 if (arg[2] == '=') { 589 // name follows 590 filename = arg + 3; 591 } else if (arg[2] == '\0' 592 && argi + 1 < argc && argv[argi + 1][0] != '-') { 593 // next arg is name 594 filename = argv[++argi]; 595 } else 596 print_usage_and_exit(true); 597 598 outputFile = fopen(filename, "w+"); 599 if (outputFile == NULL) { 600 fprintf(stderr, "%s: Could not open `%s': %s\n", 601 kCommandName, filename, strerror(errno)); 602 exit(1); 603 } 604 } else { 605 print_usage_and_exit(true); 606 } 607 } else { 608 programArgs = argv + argi; 609 programArgCount = argc - argi; 610 break; 611 } 612 } 613 614 // check parameters 615 if (!programArgs) 616 print_usage_and_exit(true); 617 618 if (fastMode) 619 contentsFlags = 0; 620 else if (contentsFlags == 0) 621 contentsFlags = Context::ALL; 622 623 // initialize our syscalls vector and map 624 init_syscalls(); 625 626 // don't colorize the output, if we don't have a terminal 627 if (outputFile == stdout) 628 colorize = colorize && isatty(STDOUT_FILENO); 629 else if (outputFile) 630 colorize = false; 631 632 // get thread/team to be debugged 633 thread_id threadID = -1; 634 team_id teamID = -1; 635 if (programArgCount > 1 636 || !get_id(*programArgs, (traceTeam ? teamID : threadID))) { 637 // we've been given an executable and need to load it 638 threadID = load_program(programArgs, programArgCount, traceLoading); 639 if (threadID < 0) { 640 fprintf(stderr, "%s: Failed to start `%s': %s\n", kCommandName, 641 programArgs[0], strerror(threadID)); 642 exit(1); 643 } 644 } 645 646 // get the team ID, if we have none yet 647 if (teamID < 0) { 648 thread_info threadInfo; 649 status_t error = get_thread_info(threadID, &threadInfo); 650 if (error != B_OK) { 651 fprintf(stderr, "%s: Failed to get info for thread %" B_PRId32 652 ": %s\n", kCommandName, threadID, strerror(error)); 653 exit(1); 654 } 655 teamID = threadInfo.team; 656 } 657 658 // create a debugger port 659 port_id debuggerPort = create_port(10, "debugger port"); 660 if (debuggerPort < 0) { 661 fprintf(stderr, "%s: Failed to create debugger port: %s\n", 662 kCommandName, strerror(debuggerPort)); 663 exit(1); 664 } 665 666 // install ourselves as the team debugger 667 typedef map<team_id, Team*> TeamMap; 668 TeamMap debuggedTeams; 669 port_id nubPort; 670 671 { 672 Team* team = new Team(teamID); 673 status_t error = team->InstallDebugger(debuggerPort, traceTeam, 674 traceChildTeams, traceSignal); 675 if (error != B_OK) 676 exit(1); 677 678 debuggedTeams[team->ID()] = team; 679 680 nubPort = team->NubPort(); 681 } 682 683 // set thread debugging flags 684 if (threadID >= 0) { 685 int32 threadDebugFlags = 0; 686 if (!traceTeam) { 687 threadDebugFlags = B_THREAD_DEBUG_POST_SYSCALL 688 | (traceChildThreads 689 ? B_THREAD_DEBUG_SYSCALL_TRACE_CHILD_THREADS : 0); 690 } 691 if (set_thread_debugging_flags(nubPort, threadID, threadDebugFlags) 692 != B_OK) { 693 exit(1); 694 } 695 696 // resume the target thread to be sure, it's running 697 resume_thread(threadID); 698 } 699 700 // debug loop 701 while (true) { 702 bool quitLoop = false; 703 int32 code; 704 debug_debugger_message_data message; 705 ssize_t messageSize = read_port(debuggerPort, &code, &message, 706 sizeof(message)); 707 708 if (messageSize < 0) { 709 if (messageSize == B_INTERRUPTED) 710 continue; 711 712 fprintf(stderr, "%s: Reading from debugger port failed: %s\n", 713 kCommandName, strerror(messageSize)); 714 exit(1); 715 } 716 717 switch (code) { 718 case B_DEBUGGER_MESSAGE_POST_SYSCALL: 719 { 720 TeamMap::iterator it = debuggedTeams.find(message.origin.team); 721 if (it == debuggedTeams.end()) 722 break; 723 724 Team* team = it->second; 725 MemoryReader& memoryReader = team->GetMemoryReader(); 726 727 uint32 syscallNumber = message.post_syscall.syscall; 728 if (syscallNumber >= sSyscallVector.size()) { 729 fprintf(stderr, "%s: invalid syscall %" B_PRIu32 " attempted\n", 730 kCommandName, syscallNumber); 731 break; 732 } 733 Syscall* syscall = sSyscallVector[syscallNumber]; 734 735 if (stats) 736 record_syscall_stats(*syscall, message.post_syscall); 737 738 if (trace) { 739 print_syscall(outputFile, syscall, message.post_syscall, 740 memoryReader, printArguments, contentsFlags, 741 printReturnValues, colorize, decimalFormat); 742 } 743 break; 744 } 745 746 case B_DEBUGGER_MESSAGE_SIGNAL_RECEIVED: 747 { 748 if (traceSignal && trace) 749 print_signal(outputFile, message.signal_received, colorize); 750 break; 751 } 752 753 case B_DEBUGGER_MESSAGE_THREAD_DEBUGGED: 754 case B_DEBUGGER_MESSAGE_DEBUGGER_CALL: 755 case B_DEBUGGER_MESSAGE_BREAKPOINT_HIT: 756 case B_DEBUGGER_MESSAGE_WATCHPOINT_HIT: 757 case B_DEBUGGER_MESSAGE_SINGLE_STEP: 758 case B_DEBUGGER_MESSAGE_PRE_SYSCALL: 759 case B_DEBUGGER_MESSAGE_EXCEPTION_OCCURRED: 760 case B_DEBUGGER_MESSAGE_THREAD_CREATED: 761 case B_DEBUGGER_MESSAGE_THREAD_DELETED: 762 case B_DEBUGGER_MESSAGE_IMAGE_CREATED: 763 case B_DEBUGGER_MESSAGE_IMAGE_DELETED: 764 break; 765 766 case B_DEBUGGER_MESSAGE_TEAM_CREATED: 767 { 768 if (!traceChildTeams) 769 break; 770 771 Team* team = new(std::nothrow) Team( 772 message.team_created.new_team); 773 if (team == NULL) { 774 fprintf(stderr, "%s: Out of memory!\n", kCommandName); 775 break; 776 } 777 778 status_t error = team->InstallDebugger(debuggerPort, true, true, 779 traceSignal); 780 if (error != B_OK) { 781 delete team; 782 break; 783 } 784 785 debuggedTeams[team->ID()] = team; 786 break; 787 } 788 789 case B_DEBUGGER_MESSAGE_TEAM_DELETED: 790 { 791 // a debugged team is gone 792 TeamMap::iterator it = debuggedTeams.find(message.origin.team); 793 if (it == debuggedTeams.end()) 794 break; 795 796 Team* team = it->second; 797 debuggedTeams.erase(it); 798 delete team; 799 800 // if all debugged teams are gone, we're done 801 quitLoop = debuggedTeams.empty(); 802 break; 803 } 804 } 805 806 if (quitLoop) 807 break; 808 809 // tell the thread to continue (only when there is a thread and the 810 // message was synchronous) 811 if (message.origin.thread >= 0 && message.origin.nub_port >= 0) { 812 if (continue_thread(message.origin.nub_port, 813 message.origin.thread) != B_OK) { 814 // the team can already be gone 815 } 816 } 817 } 818 819 if (stats) { 820 // Dump recorded statistics 821 print_stats(outputFile); 822 } 823 824 if (outputFile != NULL && outputFile != stdout) 825 fclose(outputFile); 826 827 return 0; 828 } 829