1 /* 2 * Copyright 2008-2010, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de. 4 * Distributed under the terms of the MIT License. 5 * 6 * Copyright 2001, Travis Geiselbrecht. All rights reserved. 7 * Distributed under the terms of the NewOS License. 8 */ 9 10 11 /*! This file contains the debugger and debug output facilities */ 12 13 14 #include "blue_screen.h" 15 16 #include <algorithm> 17 18 #include <cpu.h> 19 #include <debug.h> 20 #include <debug_heap.h> 21 #include <debug_paranoia.h> 22 #include <driver_settings.h> 23 #include <frame_buffer_console.h> 24 #include <int.h> 25 #include <kernel.h> 26 #include <ksystem_info.h> 27 #include <safemode.h> 28 #include <smp.h> 29 #include <thread.h> 30 #include <tracing.h> 31 #include <vm/vm.h> 32 #include <vm/VMTranslationMap.h> 33 34 #include <arch/debug_console.h> 35 #include <arch/debug.h> 36 #include <util/AutoLock.h> 37 #include <util/ring_buffer.h> 38 39 #include <syslog_daemon.h> 40 41 #include <ctype.h> 42 #include <stdarg.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <syslog.h> 47 48 #include "debug_builtin_commands.h" 49 #include "debug_commands.h" 50 #include "debug_output_filter.h" 51 #include "debug_variables.h" 52 53 54 #if __GNUC__ == 2 55 # define va_copy(to, from) __va_copy(to, from) 56 #endif 57 58 59 struct debug_memcpy_parameters { 60 void* to; 61 const void* from; 62 size_t size; 63 }; 64 65 struct debug_strlcpy_parameters { 66 char* to; 67 const char* from; 68 size_t size; 69 size_t result; 70 }; 71 72 73 static const char* const kKDLPrompt = "kdebug> "; 74 static const char* const kKDLMessageCommandSeparator = "@!"; 75 // separates panic() message from command list to execute 76 77 extern "C" int kgets(char* buffer, int length); 78 79 void call_modules_hook(bool enter); 80 81 static void syslog_write(const char* text, int32 length, bool notify); 82 83 static arch_debug_registers sDebugRegisters[B_MAX_CPU_COUNT]; 84 85 static debug_page_fault_info sPageFaultInfo; 86 87 static bool sSerialDebugEnabled = true; 88 static bool sSyslogOutputEnabled = true; 89 static bool sBlueScreenEnabled = false; 90 // must always be false on startup 91 static bool sDebugScreenEnabled = false; 92 static bool sSerialInputEnabled = false; 93 static bool sBlueScreenOutput = true; 94 static bool sEmergencyKeysEnabled = true; 95 static spinlock sSpinlock = B_SPINLOCK_INITIALIZER; 96 static int32 sDebuggerOnCPU = -1; 97 98 static sem_id sSyslogNotify = -1; 99 static struct syslog_message* sSyslogMessage; 100 static struct ring_buffer* sSyslogBuffer; 101 static size_t sSyslogBufferOffset = 0; 102 // (relative) buffer offset of the yet unsent syslog messages 103 static bool sSyslogDropped = false; 104 static size_t sSyslogDebuggerOffset = 0; 105 // (relative) buffer offset of the kernel debugger messages of the current 106 // KDL session 107 108 static const char* sCurrentKernelDebuggerMessagePrefix; 109 static const char* sCurrentKernelDebuggerMessage; 110 static va_list sCurrentKernelDebuggerMessageArgs; 111 112 #define DEFAULT_SYSLOG_BUFFER_SIZE 65536 113 #define OUTPUT_BUFFER_SIZE 1024 114 static char sOutputBuffer[OUTPUT_BUFFER_SIZE]; 115 static char sInterruptOutputBuffer[OUTPUT_BUFFER_SIZE]; 116 static char sLastOutputBuffer[OUTPUT_BUFFER_SIZE]; 117 static DebugOutputFilter* sDebugOutputFilter = NULL; 118 DefaultDebugOutputFilter gDefaultDebugOutputFilter; 119 static mutex sOutputLock = MUTEX_INITIALIZER("debug output"); 120 121 static void flush_pending_repeats(bool notifySyslog); 122 static void check_pending_repeats(void* data, int iter); 123 124 static int64 sMessageRepeatFirstTime = 0; 125 static int64 sMessageRepeatLastTime = 0; 126 static int32 sMessageRepeatCount = 0; 127 128 static debugger_module_info* sDebuggerModules[8]; 129 static const uint32 kMaxDebuggerModules = sizeof(sDebuggerModules) 130 / sizeof(sDebuggerModules[0]); 131 132 #define LINE_BUFFER_SIZE 1024 133 #define HISTORY_SIZE 16 134 135 static char sLineBuffer[HISTORY_SIZE][LINE_BUFFER_SIZE] = { "", }; 136 static int32 sCurrentLine = 0; 137 138 static debugger_demangle_module_info* sDemangleModule; 139 140 static struct thread* sDebuggedThread; 141 static vint32 sInDebugger = 0; 142 static bool sPreviousDprintfState; 143 static volatile bool sHandOverKDL = false; 144 static vint32 sHandOverKDLToCPU = -1; 145 static bool sCPUTrapped[B_MAX_CPU_COUNT]; 146 147 148 #define distance(a, b) ((a) < (b) ? (b) - (a) : (a) - (b)) 149 150 151 // #pragma mark - DebugOutputFilter 152 153 154 DebugOutputFilter::DebugOutputFilter() 155 { 156 } 157 158 159 DebugOutputFilter::~DebugOutputFilter() 160 { 161 } 162 163 164 void 165 DebugOutputFilter::PrintString(const char* string) 166 { 167 } 168 169 170 void 171 DebugOutputFilter::Print(const char* format, va_list args) 172 { 173 } 174 175 176 void 177 DefaultDebugOutputFilter::PrintString(const char* string) 178 { 179 size_t length = strlen(string); 180 181 if (sSerialDebugEnabled) 182 arch_debug_serial_puts(string); 183 if (sSyslogOutputEnabled) 184 syslog_write(string, length, false); 185 if (sBlueScreenEnabled || sDebugScreenEnabled) 186 blue_screen_puts(string); 187 188 for (uint32 i = 0; sSerialDebugEnabled && i < kMaxDebuggerModules; i++) { 189 if (sDebuggerModules[i] && sDebuggerModules[i]->debugger_puts) 190 sDebuggerModules[i]->debugger_puts(string, length); 191 } 192 } 193 194 195 void 196 DefaultDebugOutputFilter::Print(const char* format, va_list args) 197 { 198 vsnprintf(sInterruptOutputBuffer, OUTPUT_BUFFER_SIZE, format, args); 199 flush_pending_repeats(false); 200 PrintString(sInterruptOutputBuffer); 201 } 202 203 204 // #pragma mark - 205 206 207 DebugOutputFilter* 208 set_debug_output_filter(DebugOutputFilter* filter) 209 { 210 DebugOutputFilter* oldFilter = sDebugOutputFilter; 211 sDebugOutputFilter = filter; 212 return oldFilter; 213 } 214 215 216 static void 217 kputchar(char c) 218 { 219 if (sSerialDebugEnabled) 220 arch_debug_serial_putchar(c); 221 if (sBlueScreenEnabled || sDebugScreenEnabled) 222 blue_screen_putchar(c); 223 for (uint32 i = 0; sSerialDebugEnabled && i < kMaxDebuggerModules; i++) 224 if (sDebuggerModules[i] && sDebuggerModules[i]->debugger_puts) 225 sDebuggerModules[i]->debugger_puts(&c, sizeof(c)); 226 } 227 228 229 void 230 kputs(const char* s) 231 { 232 if (sDebugOutputFilter != NULL) 233 sDebugOutputFilter->PrintString(s); 234 } 235 236 237 void 238 kputs_unfiltered(const char* s) 239 { 240 gDefaultDebugOutputFilter.PrintString(s); 241 } 242 243 244 static void 245 insert_chars_into_line(char* buffer, int32& position, int32& length, 246 const char* chars, int32 charCount) 247 { 248 // move the following chars to make room for the ones to insert 249 if (position < length) { 250 memmove(buffer + position + charCount, buffer + position, 251 length - position); 252 } 253 254 // insert chars 255 memcpy(buffer + position, chars, charCount); 256 int32 oldPosition = position; 257 position += charCount; 258 length += charCount; 259 260 // print the new chars (and the following ones) 261 kprintf("%.*s", (int)(length - oldPosition), 262 buffer + oldPosition); 263 264 // reposition cursor, if necessary 265 if (position < length) 266 kprintf("\x1b[%ldD", length - position); 267 } 268 269 270 static void 271 insert_char_into_line(char* buffer, int32& position, int32& length, char c) 272 { 273 insert_chars_into_line(buffer, position, length, &c, 1); 274 } 275 276 277 static void 278 remove_char_from_line(char* buffer, int32& position, int32& length) 279 { 280 if (position == length) 281 return; 282 283 length--; 284 285 if (position < length) { 286 // move the subsequent chars 287 memmove(buffer + position, buffer + position + 1, length - position); 288 289 // print the rest of the line again, if necessary 290 for (int32 i = position; i < length; i++) 291 kputchar(buffer[i]); 292 } 293 294 // visually clear the last char 295 kputchar(' '); 296 297 // reposition the cursor 298 kprintf("\x1b[%ldD", length - position + 1); 299 } 300 301 302 class LineEditingHelper { 303 public: 304 virtual ~LineEditingHelper() {} 305 306 virtual void TabCompletion(char* buffer, int32 capacity, int32& position, 307 int32& length) = 0; 308 }; 309 310 311 class CommandLineEditingHelper : public LineEditingHelper { 312 public: 313 CommandLineEditingHelper() 314 { 315 } 316 317 virtual ~CommandLineEditingHelper() {} 318 319 virtual void TabCompletion(char* buffer, int32 capacity, int32& position, 320 int32& length) 321 { 322 // find the first space 323 char tmpChar = buffer[position]; 324 buffer[position] = '\0'; 325 char* firstSpace = strchr(buffer, ' '); 326 buffer[position] = tmpChar; 327 328 bool reprintLine = false; 329 330 if (firstSpace != NULL) { 331 // a complete command -- print its help 332 333 // get the command 334 tmpChar = *firstSpace; 335 *firstSpace = '\0'; 336 bool ambiguous; 337 debugger_command* command = find_debugger_command(buffer, true, ambiguous); 338 *firstSpace = tmpChar; 339 340 if (command != NULL) { 341 kputchar('\n'); 342 print_debugger_command_usage(command->name); 343 } else { 344 if (ambiguous) 345 kprintf("\nambiguous command\n"); 346 else 347 kprintf("\nno such command\n"); 348 } 349 350 reprintLine = true; 351 } else { 352 // a partial command -- look for completions 353 354 // check for possible completions 355 int32 count = 0; 356 int32 longestName = 0; 357 debugger_command* command = NULL; 358 int32 longestCommonPrefix = 0; 359 const char* previousCommandName = NULL; 360 while ((command = next_debugger_command(command, buffer, position)) 361 != NULL) { 362 count++; 363 int32 nameLength = strlen(command->name); 364 longestName = max_c(longestName, nameLength); 365 366 // updated the length of the longest common prefix of the 367 // commands 368 if (count == 1) { 369 longestCommonPrefix = longestName; 370 } else { 371 longestCommonPrefix = min_c(longestCommonPrefix, 372 nameLength); 373 374 for (int32 i = position; i < longestCommonPrefix; i++) { 375 if (previousCommandName[i] != command->name[i]) { 376 longestCommonPrefix = i; 377 break; 378 } 379 } 380 } 381 382 previousCommandName = command->name; 383 } 384 385 if (count == 0) { 386 // no possible completions 387 kprintf("\nno completions\n"); 388 reprintLine = true; 389 } else if (count == 1) { 390 // exactly one completion 391 command = next_debugger_command(NULL, buffer, position); 392 393 // check for sufficient space in the buffer 394 int32 neededSpace = longestName - position + 1; 395 // remainder of the name plus one space 396 // also consider the terminating null char 397 if (length + neededSpace + 1 >= capacity) 398 return; 399 400 insert_chars_into_line(buffer, position, length, 401 command->name + position, longestName - position); 402 insert_char_into_line(buffer, position, length, ' '); 403 } else if (longestCommonPrefix > position) { 404 // multiple possible completions with longer common prefix 405 // -- insert the remainder of the common prefix 406 407 // check for sufficient space in the buffer 408 int32 neededSpace = longestCommonPrefix - position; 409 // also consider the terminating null char 410 if (length + neededSpace + 1 >= capacity) 411 return; 412 413 insert_chars_into_line(buffer, position, length, 414 previousCommandName + position, neededSpace); 415 } else { 416 // multiple possible completions without longer common prefix 417 // -- print them all 418 kprintf("\n"); 419 reprintLine = true; 420 421 int columns = 80 / (longestName + 2); 422 debugger_command* command = NULL; 423 int column = 0; 424 while ((command = next_debugger_command(command, buffer, position)) 425 != NULL) { 426 // spacing 427 if (column > 0 && column % columns == 0) 428 kputchar('\n'); 429 column++; 430 431 kprintf(" %-*s", (int)longestName, command->name); 432 } 433 kputchar('\n'); 434 } 435 } 436 437 // reprint the editing line, if necessary 438 if (reprintLine) { 439 kprintf("%s%.*s", kKDLPrompt, (int)length, buffer); 440 if (position < length) 441 kprintf("\x1b[%ldD", length - position); 442 } 443 } 444 }; 445 446 447 static int 448 read_line(char* buffer, int32 maxLength, 449 LineEditingHelper* editingHelper = NULL) 450 { 451 int32 currentHistoryLine = sCurrentLine; 452 int32 position = 0; 453 int32 length = 0; 454 bool done = false; 455 char c = 0; 456 457 while (!done) { 458 c = kgetc(); 459 460 switch (c) { 461 case '\n': 462 case '\r': 463 buffer[length++] = '\0'; 464 kputchar('\n'); 465 done = true; 466 break; 467 case '\t': 468 { 469 if (editingHelper != NULL) { 470 editingHelper->TabCompletion(buffer, maxLength, 471 position, length); 472 } 473 break; 474 } 475 case 8: // backspace 476 if (position > 0) { 477 kputs("\x1b[1D"); // move to the left one 478 position--; 479 remove_char_from_line(buffer, position, length); 480 } 481 break; 482 case 0x1f & 'K': // CTRL-K -- clear line after current position 483 if (position < length) { 484 // clear chars 485 for (int32 i = position; i < length; i++) 486 kputchar(' '); 487 488 // reposition cursor 489 kprintf("\x1b[%ldD", length - position); 490 491 length = position; 492 } 493 break; 494 case 0x1f & 'L': // CTRL-L -- clear screen 495 if (sBlueScreenOutput) { 496 // All the following needs to be transparent for the 497 // serial debug output. I.e. after clearing the screen 498 // we have to get the on-screen line into the visual state 499 // it should have. 500 501 // clear screen 502 blue_screen_clear_screen(); 503 504 // reprint line 505 buffer[length] = '\0'; 506 blue_screen_puts(kKDLPrompt); 507 blue_screen_puts(buffer); 508 509 // reposition cursor 510 if (position < length) { 511 for (int i = length; i > position; i--) 512 blue_screen_puts("\x1b[1D"); 513 } 514 } 515 break; 516 case 27: // escape sequence 517 c = kgetc(); 518 if (c != '[') { 519 // ignore broken escape sequence 520 break; 521 } 522 c = kgetc(); 523 switch (c) { 524 case 'C': // right arrow 525 if (position < length) { 526 kputs("\x1b[1C"); // move to the right one 527 position++; 528 } 529 break; 530 case 'D': // left arrow 531 if (position > 0) { 532 kputs("\x1b[1D"); // move to the left one 533 position--; 534 } 535 break; 536 case 'A': // up arrow 537 case 'B': // down arrow 538 { 539 int32 historyLine = 0; 540 541 if (c == 'A') { 542 // up arrow 543 historyLine = currentHistoryLine - 1; 544 if (historyLine < 0) 545 historyLine = HISTORY_SIZE - 1; 546 } else { 547 // down arrow 548 if (currentHistoryLine == sCurrentLine) 549 break; 550 551 historyLine = currentHistoryLine + 1; 552 if (historyLine >= HISTORY_SIZE) 553 historyLine = 0; 554 } 555 556 // clear the history again if we're in the current line again 557 // (the buffer we get just is the current line buffer) 558 if (historyLine == sCurrentLine) { 559 sLineBuffer[historyLine][0] = '\0'; 560 } else if (sLineBuffer[historyLine][0] == '\0') { 561 // empty history lines are unused -- so bail out 562 break; 563 } 564 565 // swap the current line with something from the history 566 if (position > 0) 567 kprintf("\x1b[%ldD", position); // move to beginning of line 568 569 strcpy(buffer, sLineBuffer[historyLine]); 570 length = position = strlen(buffer); 571 kprintf("%s\x1b[K", buffer); // print the line and clear the rest 572 currentHistoryLine = historyLine; 573 break; 574 } 575 case '5': // if "5~", it's PAGE UP 576 case '6': // if "6~", it's PAGE DOWN 577 { 578 if (kgetc() != '~') 579 break; 580 581 // PAGE UP: search backward, PAGE DOWN: forward 582 int32 searchDirection = (c == '5' ? -1 : 1); 583 584 bool found = false; 585 int32 historyLine = currentHistoryLine; 586 do { 587 historyLine = (historyLine + searchDirection 588 + HISTORY_SIZE) % HISTORY_SIZE; 589 if (historyLine == sCurrentLine) 590 break; 591 592 if (strncmp(sLineBuffer[historyLine], buffer, 593 position) == 0) { 594 found = true; 595 } 596 } while (!found); 597 598 // bail out, if we've found nothing or hit an empty 599 // (i.e. unused) history line 600 if (!found || strlen(sLineBuffer[historyLine]) == 0) 601 break; 602 603 // found a suitable line -- replace the current buffer 604 // content with it 605 strcpy(buffer, sLineBuffer[historyLine]); 606 length = strlen(buffer); 607 kprintf("%s\x1b[K", buffer + position); 608 // print the line and clear the rest 609 kprintf("\x1b[%ldD", length - position); 610 // reposition cursor 611 currentHistoryLine = historyLine; 612 613 break; 614 } 615 case 'H': // home 616 { 617 if (position > 0) { 618 kprintf("\x1b[%ldD", position); 619 position = 0; 620 } 621 break; 622 } 623 case 'F': // end 624 { 625 if (position < length) { 626 kprintf("\x1b[%ldC", length - position); 627 position = length; 628 } 629 break; 630 } 631 case '3': // if "3~", it's DEL 632 { 633 if (kgetc() != '~') 634 break; 635 636 if (position < length) 637 remove_char_from_line(buffer, position, length); 638 639 break; 640 } 641 default: 642 break; 643 } 644 break; 645 case '$': 646 case '+': 647 if (!sBlueScreenOutput) { 648 /* HACK ALERT!!! 649 * 650 * If we get a $ at the beginning of the line 651 * we assume we are talking with GDB 652 */ 653 if (position == 0) { 654 strcpy(buffer, "gdb"); 655 position = 4; 656 done = true; 657 break; 658 } 659 } 660 /* supposed to fall through */ 661 default: 662 if (isprint(c)) 663 insert_char_into_line(buffer, position, length, c); 664 break; 665 } 666 667 if (length >= maxLength - 2) { 668 buffer[length++] = '\0'; 669 kputchar('\n'); 670 done = true; 671 break; 672 } 673 } 674 675 return length; 676 } 677 678 679 char 680 kgetc(void) 681 { 682 if (sSerialInputEnabled) 683 return arch_debug_serial_getchar(); 684 685 // give the kernel debugger modules a chance first 686 for (uint32 i = 0; i < kMaxDebuggerModules; i++) { 687 if (sDebuggerModules[i] && sDebuggerModules[i]->debugger_getchar) { 688 int getChar = sDebuggerModules[i]->debugger_getchar(); 689 if (getChar >= 0) 690 return (char)getChar; 691 } 692 } 693 694 if (sBlueScreenOutput) 695 return blue_screen_getchar(); 696 697 return arch_debug_serial_getchar(); 698 } 699 700 701 int 702 kgets(char* buffer, int length) 703 { 704 return read_line(buffer, length); 705 } 706 707 708 static void 709 print_kernel_debugger_message() 710 { 711 if (sCurrentKernelDebuggerMessagePrefix != NULL 712 || sCurrentKernelDebuggerMessage != NULL) { 713 if (sCurrentKernelDebuggerMessagePrefix != NULL) 714 kprintf("%s", sCurrentKernelDebuggerMessagePrefix); 715 if (sCurrentKernelDebuggerMessage != NULL 716 && sDebugOutputFilter != NULL) { 717 va_list args; 718 va_copy(args, sCurrentKernelDebuggerMessageArgs); 719 720 if (const char* commandDelimiter = strstr( 721 sCurrentKernelDebuggerMessage, 722 kKDLMessageCommandSeparator)) { 723 // The message string contains a list of commands to be 724 // executed when entering the kernel debugger. We don't 725 // want to print those, so we copy the interesting part of 726 // the format string. 727 if (commandDelimiter != sCurrentKernelDebuggerMessage) { 728 size_t length = commandDelimiter 729 - sCurrentKernelDebuggerMessage; 730 if (char* format = (char*)debug_malloc(length + 1)) { 731 memcpy(format, sCurrentKernelDebuggerMessage, length); 732 format[length] = '\0'; 733 sDebugOutputFilter->Print(format, args); 734 debug_free(format); 735 } else { 736 // allocation failed -- just print everything 737 sDebugOutputFilter->Print(sCurrentKernelDebuggerMessage, 738 args); 739 } 740 } 741 } else 742 sDebugOutputFilter->Print(sCurrentKernelDebuggerMessage, args); 743 744 va_end(args); 745 } 746 747 kprintf("\n"); 748 } 749 } 750 751 752 static void 753 execute_panic_commands() 754 { 755 if (sCurrentKernelDebuggerMessage == NULL 756 || strstr(sCurrentKernelDebuggerMessage, 757 kKDLMessageCommandSeparator) == NULL) { 758 return; 759 } 760 761 // Indeed there are commands to execute. 762 const size_t kCommandBufferSize = 512; 763 char* commandBuffer = (char*)debug_malloc(kCommandBufferSize); 764 if (commandBuffer != NULL) { 765 va_list tempArgs; 766 va_copy(tempArgs, sCurrentKernelDebuggerMessageArgs); 767 768 if (vsnprintf(commandBuffer, kCommandBufferSize, 769 sCurrentKernelDebuggerMessage, tempArgs) 770 < (int)kCommandBufferSize) { 771 const char* commands = strstr(commandBuffer, 772 kKDLMessageCommandSeparator); 773 if (commands != NULL) { 774 commands += strlen(kKDLMessageCommandSeparator); 775 kprintf("initial commands: %s\n", commands); 776 evaluate_debug_command(commands); 777 } 778 } 779 780 va_end(tempArgs); 781 782 debug_free(commandBuffer); 783 } 784 } 785 786 787 static void 788 stack_trace_trampoline(void*) 789 { 790 arch_debug_stack_trace(); 791 } 792 793 794 static void 795 kernel_debugger_loop(const char* messagePrefix, const char* message, 796 va_list args, int32 cpu) 797 { 798 int32 previousCPU = sDebuggerOnCPU; 799 sDebuggerOnCPU = cpu; 800 801 DebugAllocPool* allocPool = create_debug_alloc_pool(); 802 803 sCurrentKernelDebuggerMessagePrefix = messagePrefix; 804 sCurrentKernelDebuggerMessage = message; 805 if (sCurrentKernelDebuggerMessage != NULL) 806 va_copy(sCurrentKernelDebuggerMessageArgs, args); 807 808 sSyslogDebuggerOffset = ring_buffer_readable(sSyslogBuffer); 809 810 print_kernel_debugger_message(); 811 812 kprintf("Welcome to Kernel Debugging Land...\n"); 813 814 // Set a few temporary debug variables and print on which CPU and in which 815 // thread we are running. 816 set_debug_variable("_cpu", sDebuggerOnCPU); 817 818 struct thread* thread = thread_get_current_thread(); 819 if (thread == NULL) { 820 kprintf("Running on CPU %ld\n", sDebuggerOnCPU); 821 } else if (!debug_is_kernel_memory_accessible((addr_t)thread, 822 sizeof(struct thread), B_KERNEL_READ_AREA)) { 823 kprintf("Running on CPU %ld\n", sDebuggerOnCPU); 824 kprintf("Current thread pointer is %p, which is an address we " 825 "can't read from.\n", thread); 826 arch_debug_unset_current_thread(); 827 } else { 828 set_debug_variable("_thread", (uint64)(addr_t)thread); 829 set_debug_variable("_threadID", thread->id); 830 831 kprintf("Thread %ld \"%.64s\" running on CPU %ld\n", thread->id, 832 thread->name, sDebuggerOnCPU); 833 834 if (thread->cpu != gCPU + cpu) { 835 kprintf("The thread's CPU pointer is %p, but should be %p.\n", 836 thread->cpu, gCPU + cpu); 837 arch_debug_unset_current_thread(); 838 } else if (thread->team != NULL) { 839 if (debug_is_kernel_memory_accessible((addr_t)thread->team, 840 sizeof(struct team), B_KERNEL_READ_AREA)) { 841 set_debug_variable("_team", (uint64)(addr_t)thread->team); 842 set_debug_variable("_teamID", thread->team->id); 843 } else { 844 kprintf("The thread's team pointer is %p, which is an " 845 "address we can't read from.\n", thread->team); 846 arch_debug_unset_current_thread(); 847 } 848 } 849 } 850 851 if (!has_debugger_command("help") || message != NULL) { 852 // No commands yet or we came here via a panic(). Always print a stack 853 // trace in these cases. 854 jmp_buf* jumpBuffer = (jmp_buf*)debug_malloc(sizeof(jmp_buf)); 855 if (jumpBuffer != NULL) { 856 debug_call_with_fault_handler(*jumpBuffer, &stack_trace_trampoline, 857 NULL); 858 debug_free(jumpBuffer); 859 } else 860 arch_debug_stack_trace(); 861 } 862 863 if (has_debugger_command("help")) { 864 // Commands are registered already -- execute panic() commands. Do that 865 // with paging disabled, so everything is printed, even if the user 866 // can't use the keyboard. 867 bool pagingEnabled = blue_screen_paging_enabled(); 868 blue_screen_set_paging(false); 869 870 execute_panic_commands(); 871 872 blue_screen_set_paging(pagingEnabled); 873 } 874 875 int32 continuableLine = -1; 876 // Index of the previous command line, if the command returned 877 // B_KDEBUG_CONT, i.e. asked to be repeatable, -1 otherwise. 878 879 for (;;) { 880 CommandLineEditingHelper editingHelper; 881 kprintf(kKDLPrompt); 882 char* line = sLineBuffer[sCurrentLine]; 883 read_line(line, LINE_BUFFER_SIZE, &editingHelper); 884 885 // check, if the line is empty or whitespace only 886 bool whiteSpaceOnly = true; 887 for (int i = 0 ; line[i] != '\0'; i++) { 888 if (!isspace(line[i])) { 889 whiteSpaceOnly = false; 890 break; 891 } 892 } 893 894 if (whiteSpaceOnly) { 895 if (continuableLine < 0) 896 continue; 897 898 // the previous command can be repeated 899 sCurrentLine = continuableLine; 900 line = sLineBuffer[sCurrentLine]; 901 } 902 903 int rc = evaluate_debug_command(line); 904 905 if (rc == B_KDEBUG_QUIT) { 906 // okay, exit now. 907 break; 908 } 909 910 // If the command is continuable, remember the current line index. 911 continuableLine = (rc == B_KDEBUG_CONT ? sCurrentLine : -1); 912 913 int previousLine = sCurrentLine - 1; 914 if (previousLine < 0) 915 previousLine = HISTORY_SIZE - 1; 916 917 // Only use the next slot in the history, if the entries differ 918 if (strcmp(sLineBuffer[sCurrentLine], sLineBuffer[previousLine])) { 919 if (++sCurrentLine >= HISTORY_SIZE) 920 sCurrentLine = 0; 921 } 922 } 923 924 if (sCurrentKernelDebuggerMessage != NULL) 925 va_end(sCurrentKernelDebuggerMessageArgs); 926 927 delete_debug_alloc_pool(allocPool); 928 929 sDebuggerOnCPU = previousCPU; 930 } 931 932 933 static void 934 enter_kernel_debugger(int32 cpu) 935 { 936 while (atomic_add(&sInDebugger, 1) > 0) { 937 // The debugger is already running, find out where... 938 if (sDebuggerOnCPU == cpu) { 939 // We are re-entering the debugger on the same CPU. 940 break; 941 } 942 943 // Some other CPU must have entered the debugger and tried to halt 944 // us. Process ICIs to ensure we get the halt request. Then we are 945 // blocking there until everyone leaves the debugger and we can 946 // try to enter it again. 947 atomic_add(&sInDebugger, -1); 948 smp_intercpu_int_handler(cpu); 949 } 950 951 arch_debug_save_registers(&sDebugRegisters[cpu]); 952 sPreviousDprintfState = set_dprintf_enabled(true); 953 954 if (!gKernelStartup && sDebuggerOnCPU != cpu && smp_get_num_cpus() > 1) { 955 // First entry on a MP system, send a halt request to all of the other 956 // CPUs. Should they try to enter the debugger they will be cought in 957 // the loop above. 958 smp_send_broadcast_ici_interrupts_disabled(cpu, SMP_MSG_CPU_HALT, 0, 0, 959 0, NULL, SMP_MSG_FLAG_SYNC); 960 } 961 962 if (sBlueScreenOutput) { 963 if (blue_screen_enter(false) == B_OK) 964 sBlueScreenEnabled = true; 965 } 966 967 sDebugOutputFilter = &gDefaultDebugOutputFilter; 968 969 sDebuggedThread = NULL; 970 971 // sort the commands 972 sort_debugger_commands(); 973 974 call_modules_hook(true); 975 } 976 977 978 static void 979 exit_kernel_debugger() 980 { 981 call_modules_hook(false); 982 set_dprintf_enabled(sPreviousDprintfState); 983 984 sDebugOutputFilter = NULL; 985 986 sBlueScreenEnabled = false; 987 if (sDebugScreenEnabled) 988 blue_screen_enter(true); 989 990 atomic_add(&sInDebugger, -1); 991 } 992 993 994 static void 995 hand_over_kernel_debugger() 996 { 997 // Wait until the hand-over is complete. 998 // The other CPU gets our sInDebugger reference and will release it when 999 // done. Note, that there's a small race condition: the other CPU could 1000 // hand over to another CPU without us noticing. Since this is only 1001 // initiated by the user, it is harmless, though. 1002 sHandOverKDL = true; 1003 while (sHandOverKDLToCPU >= 0) 1004 PAUSE(); 1005 } 1006 1007 1008 static void 1009 kernel_debugger_internal(const char* messagePrefix, const char* message, 1010 va_list args, int32 cpu) 1011 { 1012 while (true) { 1013 if (sHandOverKDLToCPU == cpu) { 1014 sHandOverKDLToCPU = -1; 1015 sHandOverKDL = false; 1016 } else 1017 enter_kernel_debugger(cpu); 1018 1019 kernel_debugger_loop(messagePrefix, message, args, cpu); 1020 1021 if (sHandOverKDLToCPU < 0) { 1022 exit_kernel_debugger(); 1023 break; 1024 } 1025 1026 hand_over_kernel_debugger(); 1027 1028 debug_trap_cpu_in_kdl(cpu, true); 1029 1030 if (sHandOverKDLToCPU != cpu) 1031 break; 1032 } 1033 } 1034 1035 1036 static int 1037 cmd_dump_kdl_message(int argc, char** argv) 1038 { 1039 print_kernel_debugger_message(); 1040 return 0; 1041 } 1042 1043 1044 static int 1045 cmd_execute_panic_commands(int argc, char** argv) 1046 { 1047 execute_panic_commands(); 1048 return 0; 1049 } 1050 1051 1052 static int 1053 cmd_dump_syslog(int argc, char** argv) 1054 { 1055 if (!sSyslogOutputEnabled) { 1056 kprintf("Syslog is not enabled.\n"); 1057 return 0; 1058 } 1059 1060 bool unsentOnly = false; 1061 bool ignoreKDLOutput = true; 1062 1063 int argi = 1; 1064 for (; argi < argc; argi++) { 1065 if (strcmp(argv[argi], "-n") == 0) 1066 unsentOnly = true; 1067 else if (strcmp(argv[argi], "-k") == 0) 1068 ignoreKDLOutput = false; 1069 else 1070 break; 1071 } 1072 1073 if (argi < argc) { 1074 print_debugger_command_usage(argv[0]); 1075 return 0; 1076 } 1077 1078 size_t debuggerOffset = sSyslogDebuggerOffset; 1079 size_t start = unsentOnly ? sSyslogBufferOffset : 0; 1080 size_t end = ignoreKDLOutput 1081 ? debuggerOffset : ring_buffer_readable(sSyslogBuffer); 1082 1083 // allocate a buffer for processing the syslog output 1084 size_t bufferSize = 1024; 1085 char* buffer = (char*)debug_malloc(bufferSize); 1086 char stackBuffer[64]; 1087 if (buffer == NULL) { 1088 buffer = stackBuffer; 1089 bufferSize = sizeof(stackBuffer); 1090 } 1091 1092 // filter the output 1093 bool newLine = false; 1094 while (start < end) { 1095 size_t bytesRead = ring_buffer_peek(sSyslogBuffer, start, buffer, 1096 std::min(end - start, bufferSize - 1)); 1097 if (bytesRead == 0) 1098 break; 1099 start += bytesRead; 1100 1101 // remove '\0' and 0xcc 1102 size_t toPrint = 0; 1103 for (size_t i = 0; i < bytesRead; i++) { 1104 if (buffer[i] != '\0' && (uint8)buffer[i] != 0xcc) 1105 buffer[toPrint++] = buffer[i]; 1106 } 1107 1108 if (toPrint > 0) { 1109 newLine = buffer[toPrint - 1] == '\n'; 1110 buffer[toPrint] = '\0'; 1111 kputs(buffer); 1112 } 1113 1114 if (debuggerOffset > sSyslogDebuggerOffset) { 1115 // Our output caused older syslog output to be evicted from the 1116 // syslog buffer. We need to adjust our offsets accordingly. Note, 1117 // this can still go wrong, if the buffer was already full and more 1118 // was written to it than we have processed, but we can't help that. 1119 size_t diff = debuggerOffset - sSyslogDebuggerOffset; 1120 start -= std::min(start, diff); 1121 end -= std::min(end, diff); 1122 debuggerOffset = sSyslogDebuggerOffset; 1123 } 1124 } 1125 1126 if (!newLine) 1127 kputs("\n"); 1128 1129 if (buffer != stackBuffer) 1130 debug_free(buffer); 1131 1132 return 0; 1133 } 1134 1135 1136 static int 1137 cmd_serial_input(int argc, char** argv) 1138 { 1139 sSerialInputEnabled = !sSerialInputEnabled; 1140 kprintf("Serial input is turned %s now.\n", 1141 sSerialInputEnabled ? "on" : "off"); 1142 return 0; 1143 } 1144 1145 1146 static int 1147 cmd_switch_cpu(int argc, char** argv) 1148 { 1149 if (argc > 2) { 1150 print_debugger_command_usage(argv[0]); 1151 return 0; 1152 } 1153 1154 if (argc == 1) { 1155 kprintf("running on CPU %ld\n", smp_get_current_cpu()); 1156 return 0; 1157 } 1158 1159 int32 newCPU = parse_expression(argv[1]); 1160 if (newCPU < 0 || newCPU >= smp_get_num_cpus()) { 1161 kprintf("invalid CPU index\n"); 1162 return 0; 1163 } 1164 1165 if (newCPU == smp_get_current_cpu()) { 1166 kprintf("already running on CPU %ld\n", newCPU); 1167 return 0; 1168 } 1169 1170 sHandOverKDLToCPU = newCPU; 1171 1172 return B_KDEBUG_QUIT; 1173 } 1174 1175 1176 static status_t 1177 syslog_sender(void* data) 1178 { 1179 status_t error = B_BAD_PORT_ID; 1180 port_id port = -1; 1181 bool bufferPending = false; 1182 int32 length = 0; 1183 1184 while (true) { 1185 // wait for syslog data to become available 1186 acquire_sem_etc(sSyslogNotify, 1, B_RELATIVE_TIMEOUT, 5000000); 1187 // Note: We time out since in some situations output is added to 1188 // the syslog buffer without being allowed to notify us (e.g. in 1189 // the kernel debugger). 1190 1191 sSyslogMessage->when = real_time_clock(); 1192 1193 if (error == B_BAD_PORT_ID) { 1194 // last message couldn't be sent, try to locate the syslog_daemon 1195 port = find_port(SYSLOG_PORT_NAME); 1196 } 1197 1198 if (port >= B_OK) { 1199 if (!bufferPending) { 1200 // we need to have exclusive access to our syslog buffer 1201 cpu_status state = disable_interrupts(); 1202 acquire_spinlock(&sSpinlock); 1203 1204 length = ring_buffer_readable(sSyslogBuffer) 1205 - sSyslogBufferOffset; 1206 if (length > (int32)SYSLOG_MAX_MESSAGE_LENGTH) 1207 length = SYSLOG_MAX_MESSAGE_LENGTH; 1208 1209 length = ring_buffer_peek(sSyslogBuffer, sSyslogBufferOffset, 1210 (uint8*)sSyslogMessage->message, length); 1211 sSyslogBufferOffset += length; 1212 if (sSyslogDropped) { 1213 // Add drop marker - since parts had to be dropped, it's 1214 // guaranteed that we have enough space in the buffer now. 1215 ring_buffer_write(sSyslogBuffer, (uint8*)"<DROP>", 6); 1216 sSyslogDropped = false; 1217 } 1218 1219 release_spinlock(&sSpinlock); 1220 restore_interrupts(state); 1221 } 1222 1223 if (length == 0) { 1224 // the buffer we came here for might have been sent already 1225 bufferPending = false; 1226 continue; 1227 } 1228 1229 error = write_port_etc(port, SYSLOG_MESSAGE, sSyslogMessage, 1230 sizeof(struct syslog_message) + length, B_RELATIVE_TIMEOUT, 0); 1231 1232 if (error < B_OK) { 1233 // sending has failed - just wait, maybe it'll work later. 1234 bufferPending = true; 1235 continue; 1236 } 1237 1238 if (bufferPending) { 1239 // we could write the last pending buffer, try to read more 1240 // from the syslog ring buffer 1241 release_sem_etc(sSyslogNotify, 1, B_DO_NOT_RESCHEDULE); 1242 bufferPending = false; 1243 } 1244 } 1245 } 1246 1247 return 0; 1248 } 1249 1250 1251 static void 1252 syslog_write(const char* text, int32 length, bool notify) 1253 { 1254 if (sSyslogBuffer == NULL) 1255 return; 1256 1257 if (length > sSyslogBuffer->size) { 1258 text = "<DROP>"; 1259 length = 6; 1260 } 1261 1262 int32 writable = ring_buffer_writable(sSyslogBuffer); 1263 if (writable < length) { 1264 // drop old data 1265 size_t toDrop = length - writable; 1266 ring_buffer_flush(sSyslogBuffer, toDrop); 1267 1268 if (toDrop > sSyslogBufferOffset) { 1269 sSyslogBufferOffset = 0; 1270 sSyslogDropped = true; 1271 } else 1272 sSyslogBufferOffset -= toDrop; 1273 1274 sSyslogDebuggerOffset -= std::min(toDrop, sSyslogDebuggerOffset); 1275 } 1276 1277 ring_buffer_write(sSyslogBuffer, (uint8*)text, length); 1278 1279 if (notify) 1280 release_sem_etc(sSyslogNotify, 1, B_DO_NOT_RESCHEDULE); 1281 } 1282 1283 1284 static status_t 1285 syslog_init_post_threads(void) 1286 { 1287 if (!sSyslogOutputEnabled) 1288 return B_OK; 1289 1290 sSyslogNotify = create_sem(0, "syslog data"); 1291 if (sSyslogNotify >= B_OK) { 1292 thread_id thread = spawn_kernel_thread(syslog_sender, "syslog sender", 1293 B_LOW_PRIORITY, NULL); 1294 if (thread >= B_OK && resume_thread(thread) == B_OK) 1295 return B_OK; 1296 } 1297 1298 // initializing kernel syslog service failed -- disable it 1299 1300 sSyslogOutputEnabled = false; 1301 free(sSyslogMessage); 1302 free(sSyslogBuffer); 1303 delete_sem(sSyslogNotify); 1304 1305 return B_ERROR; 1306 } 1307 1308 1309 static status_t 1310 syslog_init_post_vm(struct kernel_args* args) 1311 { 1312 status_t status; 1313 int32 length = 0; 1314 1315 if (!sSyslogOutputEnabled) 1316 return B_OK; 1317 1318 sSyslogMessage = (syslog_message*)malloc(SYSLOG_MESSAGE_BUFFER_SIZE); 1319 if (sSyslogMessage == NULL) { 1320 status = B_NO_MEMORY; 1321 goto err1; 1322 } 1323 1324 if (sSyslogBuffer == NULL) { 1325 size_t bufferSize = DEFAULT_SYSLOG_BUFFER_SIZE; 1326 void* handle = load_driver_settings("kernel"); 1327 if (handle != NULL) { 1328 const char* sizeString = get_driver_parameter(handle, 1329 "syslog_buffer_size", NULL, NULL); 1330 if (sizeString != NULL) { 1331 bufferSize = strtoul(sizeString, NULL, 0); 1332 if (bufferSize > 262144) 1333 bufferSize = 262144; 1334 else if (bufferSize < SYSLOG_MESSAGE_BUFFER_SIZE) 1335 bufferSize = SYSLOG_MESSAGE_BUFFER_SIZE; 1336 } 1337 1338 unload_driver_settings(handle); 1339 } 1340 1341 sSyslogBuffer = create_ring_buffer(bufferSize); 1342 1343 if (sSyslogBuffer == NULL) { 1344 status = B_NO_MEMORY; 1345 goto err2; 1346 } 1347 } else { 1348 // create an area for the debug syslog buffer 1349 void* base = (void*)ROUNDDOWN((addr_t)args->debug_output, B_PAGE_SIZE); 1350 size_t size = ROUNDUP(args->debug_size, B_PAGE_SIZE); 1351 create_area("syslog debug", &base, B_EXACT_ADDRESS, size, 1352 B_ALREADY_WIRED, B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA); 1353 } 1354 1355 // initialize syslog message 1356 sSyslogMessage->from = 0; 1357 sSyslogMessage->options = LOG_KERN; 1358 sSyslogMessage->priority = LOG_DEBUG; 1359 sSyslogMessage->ident[0] = '\0'; 1360 //strcpy(sSyslogMessage->ident, "KERNEL"); 1361 1362 if (args->debug_output != NULL) 1363 syslog_write((const char*)args->debug_output, args->debug_size, false); 1364 1365 char revisionBuffer[64]; 1366 length = snprintf(revisionBuffer, sizeof(revisionBuffer), 1367 "Welcome to syslog debug output!\nHaiku revision: %lu\n", 1368 get_haiku_revision()); 1369 syslog_write(revisionBuffer, 1370 std::min(length, (ssize_t)sizeof(revisionBuffer) - 1), false); 1371 1372 add_debugger_command_etc("syslog", &cmd_dump_syslog, 1373 "Dumps the syslog buffer.", 1374 "[ \"-n\" ] [ \"-k\" ]\n" 1375 "Dumps the whole syslog buffer, or, if -k is specified, only " 1376 "the part that hasn't been sent yet.\n", 0); 1377 1378 add_debugger_command("serial_input", &cmd_serial_input, 1379 "Enable or disable serial input"); 1380 1381 return B_OK; 1382 1383 err2: 1384 free(sSyslogMessage); 1385 err1: 1386 sSyslogOutputEnabled = false; 1387 return status; 1388 } 1389 1390 1391 static status_t 1392 syslog_init(struct kernel_args* args) 1393 { 1394 if (!args->keep_debug_output_buffer) 1395 return B_OK; 1396 1397 sSyslogBuffer = create_ring_buffer_etc(args->debug_output, args->debug_size, 1398 RING_BUFFER_INIT_FROM_BUFFER); 1399 1400 return B_OK; 1401 } 1402 1403 1404 static void 1405 debug_memcpy_trampoline(void* _parameters) 1406 { 1407 debug_memcpy_parameters* parameters = (debug_memcpy_parameters*)_parameters; 1408 memcpy(parameters->to, parameters->from, parameters->size); 1409 } 1410 1411 1412 static void 1413 debug_strlcpy_trampoline(void* _parameters) 1414 { 1415 debug_strlcpy_parameters* parameters 1416 = (debug_strlcpy_parameters*)_parameters; 1417 parameters->result = strlcpy(parameters->to, parameters->from, 1418 parameters->size); 1419 } 1420 1421 1422 void 1423 call_modules_hook(bool enter) 1424 { 1425 uint32 index = 0; 1426 while (index < kMaxDebuggerModules && sDebuggerModules[index] != NULL) { 1427 debugger_module_info* module = sDebuggerModules[index]; 1428 1429 if (enter && module->enter_debugger != NULL) 1430 module->enter_debugger(); 1431 else if (!enter && module->exit_debugger != NULL) 1432 module->exit_debugger(); 1433 1434 index++; 1435 } 1436 } 1437 1438 1439 //! Must be called with the sSpinlock held. 1440 static void 1441 debug_output(const char* string, int32 length, bool notifySyslog) 1442 { 1443 if (length >= OUTPUT_BUFFER_SIZE) 1444 length = OUTPUT_BUFFER_SIZE - 1; 1445 1446 if (length > 1 && string[length - 1] == '\n' 1447 && strncmp(string, sLastOutputBuffer, length) == 0) { 1448 sMessageRepeatCount++; 1449 sMessageRepeatLastTime = system_time(); 1450 if (sMessageRepeatFirstTime == 0) 1451 sMessageRepeatFirstTime = sMessageRepeatLastTime; 1452 } else { 1453 flush_pending_repeats(notifySyslog); 1454 1455 if (sSerialDebugEnabled) 1456 arch_debug_serial_puts(string); 1457 if (sSyslogOutputEnabled) 1458 syslog_write(string, length, notifySyslog); 1459 if (sBlueScreenEnabled || sDebugScreenEnabled) 1460 blue_screen_puts(string); 1461 if (sSerialDebugEnabled) { 1462 for (uint32 i = 0; i < kMaxDebuggerModules; i++) { 1463 if (sDebuggerModules[i] && sDebuggerModules[i]->debugger_puts) 1464 sDebuggerModules[i]->debugger_puts(string, length); 1465 } 1466 } 1467 1468 memcpy(sLastOutputBuffer, string, length); 1469 sLastOutputBuffer[length] = 0; 1470 } 1471 } 1472 1473 1474 //! Must be called with the sSpinlock held. 1475 static void 1476 flush_pending_repeats(bool notifySyslog) 1477 { 1478 if (sMessageRepeatCount <= 0) 1479 return; 1480 1481 if (sMessageRepeatCount > 1) { 1482 static char temp[40]; 1483 size_t length = snprintf(temp, sizeof(temp), 1484 "Last message repeated %ld times.\n", sMessageRepeatCount); 1485 length = std::min(length, sizeof(temp) - 1); 1486 1487 if (sSerialDebugEnabled) 1488 arch_debug_serial_puts(temp); 1489 if (sSyslogOutputEnabled) 1490 syslog_write(temp, length, notifySyslog); 1491 if (sBlueScreenEnabled || sDebugScreenEnabled) 1492 blue_screen_puts(temp); 1493 if (sSerialDebugEnabled) { 1494 for (uint32 i = 0; i < kMaxDebuggerModules; i++) { 1495 if (sDebuggerModules[i] && sDebuggerModules[i]->debugger_puts) 1496 sDebuggerModules[i]->debugger_puts(temp, length); 1497 } 1498 } 1499 } else { 1500 // if we only have one repeat just reprint the last buffer 1501 size_t length = strlen(sLastOutputBuffer); 1502 1503 if (sSerialDebugEnabled) 1504 arch_debug_serial_puts(sLastOutputBuffer); 1505 if (sSyslogOutputEnabled) 1506 syslog_write(sLastOutputBuffer, length, notifySyslog); 1507 if (sBlueScreenEnabled || sDebugScreenEnabled) 1508 blue_screen_puts(sLastOutputBuffer); 1509 if (sSerialDebugEnabled) { 1510 for (uint32 i = 0; i < kMaxDebuggerModules; i++) { 1511 if (sDebuggerModules[i] && sDebuggerModules[i]->debugger_puts) { 1512 sDebuggerModules[i]->debugger_puts(sLastOutputBuffer, 1513 length); 1514 } 1515 } 1516 } 1517 } 1518 1519 sMessageRepeatFirstTime = 0; 1520 sMessageRepeatCount = 0; 1521 } 1522 1523 1524 static void 1525 check_pending_repeats(void* /*data*/, int /*iteration*/) 1526 { 1527 if (sMessageRepeatCount > 0 1528 && (system_time() - sMessageRepeatLastTime > 1000000 1529 || system_time() - sMessageRepeatFirstTime > 3000000)) { 1530 cpu_status state = disable_interrupts(); 1531 acquire_spinlock(&sSpinlock); 1532 1533 flush_pending_repeats(true); 1534 1535 release_spinlock(&sSpinlock); 1536 restore_interrupts(state); 1537 } 1538 } 1539 1540 1541 static void 1542 dprintf_args(const char* format, va_list args, bool notifySyslog) 1543 { 1544 if (are_interrupts_enabled()) { 1545 MutexLocker locker(sOutputLock); 1546 1547 int32 length = vsnprintf(sOutputBuffer, OUTPUT_BUFFER_SIZE, format, 1548 args); 1549 length = std::min(length, (int32)OUTPUT_BUFFER_SIZE - 1); 1550 1551 InterruptsSpinLocker _(sSpinlock); 1552 debug_output(sOutputBuffer, length, notifySyslog); 1553 } else { 1554 InterruptsSpinLocker _(sSpinlock); 1555 1556 int32 length = vsnprintf(sInterruptOutputBuffer, OUTPUT_BUFFER_SIZE, 1557 format, args); 1558 length = std::min(length, (int32)OUTPUT_BUFFER_SIZE - 1); 1559 1560 debug_output(sInterruptOutputBuffer, length, notifySyslog); 1561 } 1562 } 1563 1564 1565 // #pragma mark - private kernel API 1566 1567 1568 bool 1569 debug_screen_output_enabled(void) 1570 { 1571 return sDebugScreenEnabled; 1572 } 1573 1574 1575 void 1576 debug_stop_screen_debug_output(void) 1577 { 1578 sDebugScreenEnabled = false; 1579 } 1580 1581 1582 bool 1583 debug_debugger_running(void) 1584 { 1585 return sDebuggerOnCPU != -1; 1586 } 1587 1588 1589 void 1590 debug_puts(const char* string, int32 length) 1591 { 1592 InterruptsSpinLocker _(sSpinlock); 1593 debug_output(string, length, true); 1594 } 1595 1596 1597 void 1598 debug_early_boot_message(const char* string) 1599 { 1600 arch_debug_serial_early_boot_message(string); 1601 } 1602 1603 1604 status_t 1605 debug_init(kernel_args* args) 1606 { 1607 new(&gDefaultDebugOutputFilter) DefaultDebugOutputFilter; 1608 1609 syslog_init(args); 1610 1611 debug_paranoia_init(); 1612 return arch_debug_console_init(args); 1613 } 1614 1615 1616 status_t 1617 debug_init_post_vm(kernel_args* args) 1618 { 1619 add_debugger_command_etc("cpu", &cmd_switch_cpu, 1620 "Switches to another CPU.", 1621 "<cpu>\n" 1622 "Switches to CPU with the index <cpu>.\n", 0); 1623 add_debugger_command_etc("message", &cmd_dump_kdl_message, 1624 "Reprint the message printed when entering KDL", 1625 "\n" 1626 "Reprints the message printed when entering KDL.\n", 0); 1627 add_debugger_command_etc("panic_commands", &cmd_execute_panic_commands, 1628 "Execute commands associated with the panic() that caused " 1629 "entering KDL", 1630 "\n" 1631 "Executes the commands associated with the panic() that caused " 1632 "entering KDL.\n", 0); 1633 1634 debug_builtin_commands_init(); 1635 1636 debug_heap_init(); 1637 debug_variables_init(); 1638 frame_buffer_console_init(args); 1639 arch_debug_console_init_settings(args); 1640 tracing_init(); 1641 1642 // get debug settings 1643 1644 sSerialDebugEnabled = get_safemode_boolean("serial_debug_output", 1645 sSerialDebugEnabled); 1646 sSyslogOutputEnabled = get_safemode_boolean("syslog_debug_output", 1647 sSyslogOutputEnabled); 1648 sBlueScreenOutput = get_safemode_boolean("bluescreen", true); 1649 sEmergencyKeysEnabled = get_safemode_boolean("emergency_keys", 1650 sEmergencyKeysEnabled); 1651 sDebugScreenEnabled = get_safemode_boolean("debug_screen", false); 1652 1653 if ((sBlueScreenOutput || sDebugScreenEnabled) 1654 && blue_screen_init() != B_OK) 1655 sBlueScreenOutput = sDebugScreenEnabled = false; 1656 1657 if (sDebugScreenEnabled) 1658 blue_screen_enter(true); 1659 1660 syslog_init_post_vm(args); 1661 1662 return arch_debug_init(args); 1663 } 1664 1665 1666 status_t 1667 debug_init_post_modules(struct kernel_args* args) 1668 { 1669 void* cookie; 1670 1671 // check for dupped lines every 10/10 second 1672 register_kernel_daemon(check_pending_repeats, NULL, 10); 1673 1674 syslog_init_post_threads(); 1675 1676 // load kernel debugger addons 1677 1678 static const char* kDemanglePrefix = "debugger/demangle/"; 1679 1680 cookie = open_module_list("debugger"); 1681 uint32 count = 0; 1682 while (count < kMaxDebuggerModules) { 1683 char name[B_FILE_NAME_LENGTH]; 1684 size_t nameLength = sizeof(name); 1685 1686 if (read_next_module_name(cookie, name, &nameLength) != B_OK) 1687 break; 1688 1689 // get demangle module, if any 1690 if (!strncmp(name, kDemanglePrefix, strlen(kDemanglePrefix))) { 1691 if (sDemangleModule == NULL) 1692 get_module(name, (module_info**)&sDemangleModule); 1693 continue; 1694 } 1695 1696 if (get_module(name, (module_info**)&sDebuggerModules[count]) == B_OK) { 1697 dprintf("kernel debugger extension \"%s\": loaded\n", name); 1698 count++; 1699 } else 1700 dprintf("kernel debugger extension \"%s\": failed to load\n", name); 1701 } 1702 close_module_list(cookie); 1703 1704 return frame_buffer_console_init_post_modules(args); 1705 } 1706 1707 1708 void 1709 debug_set_page_fault_info(addr_t faultAddress, addr_t pc, uint32 flags) 1710 { 1711 sPageFaultInfo.fault_address = faultAddress; 1712 sPageFaultInfo.pc = pc; 1713 sPageFaultInfo.flags = flags; 1714 } 1715 1716 1717 debug_page_fault_info* 1718 debug_get_page_fault_info() 1719 { 1720 return &sPageFaultInfo; 1721 } 1722 1723 1724 void 1725 debug_trap_cpu_in_kdl(int32 cpu, bool returnIfHandedOver) 1726 { 1727 InterruptsLocker locker; 1728 1729 // return, if we've been called recursively (we call 1730 // smp_intercpu_int_handler() below) 1731 if (sCPUTrapped[cpu]) 1732 return; 1733 1734 arch_debug_save_registers(&sDebugRegisters[cpu]); 1735 1736 sCPUTrapped[cpu] = true; 1737 1738 while (sInDebugger != 0) { 1739 if (sHandOverKDL && sHandOverKDLToCPU == cpu) { 1740 if (returnIfHandedOver) 1741 break; 1742 1743 kernel_debugger_internal(NULL, NULL, 1744 sCurrentKernelDebuggerMessageArgs, cpu); 1745 } else 1746 smp_intercpu_int_handler(cpu); 1747 } 1748 1749 sCPUTrapped[cpu] = false; 1750 } 1751 1752 1753 void 1754 debug_double_fault(int32 cpu) 1755 { 1756 kernel_debugger_internal("Double Fault!", NULL, 1757 sCurrentKernelDebuggerMessageArgs, cpu); 1758 } 1759 1760 1761 bool 1762 debug_emergency_key_pressed(char key) 1763 { 1764 if (!sEmergencyKeysEnabled) 1765 return false; 1766 1767 if (key == 'd') { 1768 kernel_debugger("Keyboard Requested Halt."); 1769 return true; 1770 } 1771 1772 // Broadcast to the kernel debugger modules 1773 1774 for (uint32 i = 0; i < kMaxDebuggerModules; i++) { 1775 if (sDebuggerModules[i] && sDebuggerModules[i]->emergency_key_pressed) { 1776 if (sDebuggerModules[i]->emergency_key_pressed(key)) 1777 return true; 1778 } 1779 } 1780 1781 return false; 1782 } 1783 1784 1785 /*! Verifies that the complete given memory range is accessible in the current 1786 context. 1787 1788 Invoked in the kernel debugger only. 1789 1790 \param address The start address of the memory range to be checked. 1791 \param size The size of the memory range to be checked. 1792 \param protection The area protection for which to check. Valid is a bitwise 1793 or of one or more of \c B_KERNEL_READ_AREA or \c B_KERNEL_WRITE_AREA. 1794 \return \c true, if the complete memory range can be accessed in all ways 1795 specified by \a protection, \c false otherwise. 1796 */ 1797 bool 1798 debug_is_kernel_memory_accessible(addr_t address, size_t size, 1799 uint32 protection) 1800 { 1801 addr_t endAddress = ROUNDUP(address + size, B_PAGE_SIZE); 1802 address = ROUNDDOWN(address, B_PAGE_SIZE); 1803 1804 if (!IS_KERNEL_ADDRESS(address) || endAddress < address) 1805 return false; 1806 1807 for (; address < endAddress; address += B_PAGE_SIZE) { 1808 if (!arch_vm_translation_map_is_kernel_page_accessible(address, 1809 protection)) { 1810 return false; 1811 } 1812 } 1813 1814 return true; 1815 } 1816 1817 1818 /*! Calls a function in a setjmp() + fault handler context. 1819 May only be used in the kernel debugger. 1820 1821 \param jumpBuffer Buffer to be used for setjmp()/longjmp(). 1822 \param function The function to be called. 1823 \param parameter The parameter to be passed to the function to be called. 1824 \return 1825 - \c 0, when the function executed without causing a page fault or 1826 calling longjmp(). 1827 - \c 1, when the function caused a page fault. 1828 - Any other value the function passes to longjmp(). 1829 */ 1830 int 1831 debug_call_with_fault_handler(jmp_buf jumpBuffer, void (*function)(void*), 1832 void* parameter) 1833 { 1834 // save current fault handler 1835 cpu_ent* cpu = gCPU + sDebuggerOnCPU; 1836 addr_t oldFaultHandler = cpu->fault_handler; 1837 addr_t oldFaultHandlerStackPointer = cpu->fault_handler_stack_pointer; 1838 1839 int result = setjmp(jumpBuffer); 1840 if (result == 0) { 1841 arch_debug_call_with_fault_handler(cpu, jumpBuffer, function, 1842 parameter); 1843 } 1844 1845 // restore old fault handler 1846 cpu->fault_handler = oldFaultHandler; 1847 cpu->fault_handler_stack_pointer = oldFaultHandlerStackPointer; 1848 1849 return result; 1850 } 1851 1852 1853 /*! Similar to user_memcpy(), but can only be invoked from within the kernel 1854 debugger (and must not be used outside). 1855 */ 1856 status_t 1857 debug_memcpy(void* to, const void* from, size_t size) 1858 { 1859 // don't allow address overflows 1860 if ((addr_t)from + size < (addr_t)from || (addr_t)to + size < (addr_t)to) 1861 return B_BAD_ADDRESS; 1862 1863 debug_memcpy_parameters parameters = {to, from, size}; 1864 1865 if (debug_call_with_fault_handler(gCPU[sDebuggerOnCPU].fault_jump_buffer, 1866 &debug_memcpy_trampoline, ¶meters) != 0) { 1867 return B_BAD_ADDRESS; 1868 } 1869 return B_OK; 1870 } 1871 1872 1873 /*! Similar to user_strlcpy(), but can only be invoked from within the kernel 1874 debugger (and must not be used outside). 1875 */ 1876 ssize_t 1877 debug_strlcpy(char* to, const char* from, size_t size) 1878 { 1879 if (size == 0) 1880 return 0; 1881 if (from == NULL || to == NULL) 1882 return B_BAD_ADDRESS; 1883 1884 // limit size to avoid address overflows 1885 size_t maxSize = std::min(size, 1886 ~(addr_t)0 - std::max((addr_t)from, (addr_t)to) + 1); 1887 // NOTE: Since strlcpy() determines the length of \a from, the source 1888 // address might still overflow. 1889 1890 debug_strlcpy_parameters parameters = {to, from, maxSize}; 1891 1892 if (debug_call_with_fault_handler(gCPU[sDebuggerOnCPU].fault_jump_buffer, 1893 &debug_strlcpy_trampoline, ¶meters) != 0) { 1894 return B_BAD_ADDRESS; 1895 } 1896 1897 // If we hit the address overflow boundary, fail. 1898 if (parameters.result >= maxSize && maxSize < size) 1899 return B_BAD_ADDRESS; 1900 1901 return parameters.result; 1902 } 1903 1904 1905 // #pragma mark - public API 1906 1907 1908 uint64 1909 parse_expression(const char* expression) 1910 { 1911 uint64 result; 1912 return evaluate_debug_expression(expression, &result, true) ? result : 0; 1913 } 1914 1915 1916 void 1917 panic(const char* format, ...) 1918 { 1919 va_list args; 1920 va_start(args, format); 1921 1922 cpu_status state = disable_interrupts(); 1923 1924 kernel_debugger_internal("PANIC: ", format, args, smp_get_current_cpu()); 1925 1926 restore_interrupts(state); 1927 1928 va_end(args); 1929 } 1930 1931 1932 void 1933 kernel_debugger(const char* message) 1934 { 1935 cpu_status state = disable_interrupts(); 1936 1937 kernel_debugger_internal(message, NULL, sCurrentKernelDebuggerMessageArgs, 1938 smp_get_current_cpu()); 1939 1940 restore_interrupts(state); 1941 } 1942 1943 1944 bool 1945 set_dprintf_enabled(bool newState) 1946 { 1947 bool oldState = sSerialDebugEnabled; 1948 sSerialDebugEnabled = newState; 1949 1950 return oldState; 1951 } 1952 1953 1954 void 1955 dprintf(const char* format, ...) 1956 { 1957 va_list args; 1958 1959 if (!sSerialDebugEnabled && !sSyslogOutputEnabled && !sBlueScreenEnabled) 1960 return; 1961 1962 va_start(args, format); 1963 dprintf_args(format, args, true); 1964 va_end(args); 1965 } 1966 1967 1968 void 1969 dprintf_no_syslog(const char* format, ...) 1970 { 1971 va_list args; 1972 1973 if (!sSerialDebugEnabled && !sBlueScreenEnabled) 1974 return; 1975 1976 va_start(args, format); 1977 dprintf_args(format, args, false); 1978 va_end(args); 1979 } 1980 1981 1982 /*! Similar to dprintf() but thought to be used in the kernel 1983 debugger only (it doesn't lock). 1984 */ 1985 void 1986 kprintf(const char* format, ...) 1987 { 1988 if (sDebugOutputFilter != NULL) { 1989 va_list args; 1990 va_start(args, format); 1991 sDebugOutputFilter->Print(format, args); 1992 va_end(args); 1993 } 1994 } 1995 1996 1997 void 1998 kprintf_unfiltered(const char* format, ...) 1999 { 2000 va_list args; 2001 va_start(args, format); 2002 gDefaultDebugOutputFilter.Print(format, args); 2003 va_end(args); 2004 } 2005 2006 2007 const char* 2008 debug_demangle_symbol(const char* symbol, char* buffer, size_t bufferSize, 2009 bool* _isObjectMethod) 2010 { 2011 if (sDemangleModule != NULL && sDemangleModule->demangle_symbol != NULL) { 2012 return sDemangleModule->demangle_symbol(symbol, buffer, bufferSize, 2013 _isObjectMethod); 2014 } 2015 2016 if (_isObjectMethod != NULL) 2017 *_isObjectMethod = false; 2018 2019 return symbol; 2020 } 2021 2022 2023 status_t 2024 debug_get_next_demangled_argument(uint32* _cookie, const char* symbol, 2025 char* name, size_t nameSize, int32* _type, size_t* _argumentLength) 2026 { 2027 if (sDemangleModule != NULL && sDemangleModule->get_next_argument != NULL) { 2028 return sDemangleModule->get_next_argument(_cookie, symbol, name, 2029 nameSize, _type, _argumentLength); 2030 } 2031 2032 return B_NOT_SUPPORTED; 2033 } 2034 2035 2036 struct arch_debug_registers* 2037 debug_get_debug_registers(int32 cpu) 2038 { 2039 if (cpu < 0 || cpu > smp_get_num_cpus()) 2040 return NULL; 2041 2042 return &sDebugRegisters[cpu]; 2043 } 2044 2045 2046 struct thread* 2047 debug_set_debugged_thread(struct thread* thread) 2048 { 2049 struct thread* previous = sDebuggedThread; 2050 sDebuggedThread = thread; 2051 return previous; 2052 } 2053 2054 2055 struct thread* 2056 debug_get_debugged_thread() 2057 { 2058 return sDebuggedThread != NULL 2059 ? sDebuggedThread : thread_get_current_thread(); 2060 } 2061 2062 2063 // #pragma mark - 2064 // userland syscalls 2065 2066 2067 status_t 2068 _user_kernel_debugger(const char *userMessage) 2069 { 2070 if (geteuid() != 0) 2071 return B_NOT_ALLOWED; 2072 2073 char message[512]; 2074 strcpy(message, "USER: "); 2075 size_t len = strlen(message); 2076 2077 if (userMessage == NULL || !IS_USER_ADDRESS(userMessage) 2078 || user_strlcpy(message + len, userMessage, sizeof(message) - len) 2079 < 0) { 2080 return B_BAD_ADDRESS; 2081 } 2082 2083 kernel_debugger(message); 2084 return B_OK; 2085 } 2086 2087 2088 void 2089 _user_debug_output(const char* userString) 2090 { 2091 char string[512]; 2092 int32 length; 2093 2094 if (!sSerialDebugEnabled && !sSyslogOutputEnabled) 2095 return; 2096 2097 if (!IS_USER_ADDRESS(userString)) 2098 return; 2099 2100 do { 2101 length = user_strlcpy(string, userString, sizeof(string)); 2102 debug_puts(string, length); 2103 userString += sizeof(string) - 1; 2104 } while (length >= (ssize_t)sizeof(string)); 2105 } 2106 2107 2108 void 2109 dump_block(const char* buffer, int size, const char* prefix) 2110 { 2111 const int DUMPED_BLOCK_SIZE = 16; 2112 int i; 2113 2114 for (i = 0; i < size;) { 2115 int start = i; 2116 2117 dprintf("%s%04x ", prefix, i); 2118 for (; i < start + DUMPED_BLOCK_SIZE; i++) { 2119 if (!(i % 4)) 2120 dprintf(" "); 2121 2122 if (i >= size) 2123 dprintf(" "); 2124 else 2125 dprintf("%02x", *(unsigned char*)(buffer + i)); 2126 } 2127 dprintf(" "); 2128 2129 for (i = start; i < start + DUMPED_BLOCK_SIZE; i++) { 2130 if (i < size) { 2131 char c = buffer[i]; 2132 2133 if (c < 30) 2134 dprintf("."); 2135 else 2136 dprintf("%c", c); 2137 } else 2138 break; 2139 } 2140 dprintf("\n"); 2141 } 2142 } 2143