1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Kernel Debugger Architecture Independent Console I/O handler 4 * 5 * Copyright (c) 1999-2006 Silicon Graphics, Inc. All Rights Reserved. 6 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved. 7 */ 8 9 #include <linux/types.h> 10 #include <linux/ctype.h> 11 #include <linux/kernel.h> 12 #include <linux/init.h> 13 #include <linux/kdev_t.h> 14 #include <linux/console.h> 15 #include <linux/string.h> 16 #include <linux/sched.h> 17 #include <linux/smp.h> 18 #include <linux/nmi.h> 19 #include <linux/delay.h> 20 #include <linux/kgdb.h> 21 #include <linux/kdb.h> 22 #include <linux/kallsyms.h> 23 #include "kdb_private.h" 24 25 #define CMD_BUFLEN 256 26 char kdb_prompt_str[CMD_BUFLEN]; 27 28 int kdb_trap_printk; 29 int kdb_printf_cpu = -1; 30 31 static int kgdb_transition_check(char *buffer) 32 { 33 if (buffer[0] != '+' && buffer[0] != '$') { 34 KDB_STATE_SET(KGDB_TRANS); 35 kdb_printf("%s", buffer); 36 } else { 37 int slen = strlen(buffer); 38 if (slen > 3 && buffer[slen - 3] == '#') { 39 kdb_gdb_state_pass(buffer); 40 strcpy(buffer, "kgdb"); 41 KDB_STATE_SET(DOING_KGDB); 42 return 1; 43 } 44 } 45 return 0; 46 } 47 48 /** 49 * kdb_handle_escape() - validity check on an accumulated escape sequence. 50 * @buf: Accumulated escape characters to be examined. Note that buf 51 * is not a string, it is an array of characters and need not be 52 * nil terminated. 53 * @sz: Number of accumulated escape characters. 54 * 55 * Return: -1 if the escape sequence is unwanted, 0 if it is incomplete, 56 * otherwise it returns a mapped key value to pass to the upper layers. 57 */ 58 static int kdb_handle_escape(char *buf, size_t sz) 59 { 60 char *lastkey = buf + sz - 1; 61 62 switch (sz) { 63 case 1: 64 if (*lastkey == '\e') 65 return 0; 66 break; 67 68 case 2: /* \e<something> */ 69 if (*lastkey == '[') 70 return 0; 71 break; 72 73 case 3: 74 switch (*lastkey) { 75 case 'A': /* \e[A, up arrow */ 76 return 16; 77 case 'B': /* \e[B, down arrow */ 78 return 14; 79 case 'C': /* \e[C, right arrow */ 80 return 6; 81 case 'D': /* \e[D, left arrow */ 82 return 2; 83 case '1': /* \e[<1,3,4>], may be home, del, end */ 84 case '3': 85 case '4': 86 return 0; 87 } 88 break; 89 90 case 4: 91 if (*lastkey == '~') { 92 switch (buf[2]) { 93 case '1': /* \e[1~, home */ 94 return 1; 95 case '3': /* \e[3~, del */ 96 return 4; 97 case '4': /* \e[4~, end */ 98 return 5; 99 } 100 } 101 break; 102 } 103 104 return -1; 105 } 106 107 /** 108 * kdb_getchar() - Read a single character from a kdb console (or consoles). 109 * 110 * Other than polling the various consoles that are currently enabled, 111 * most of the work done in this function is dealing with escape sequences. 112 * 113 * An escape key could be the start of a vt100 control sequence such as \e[D 114 * (left arrow) or it could be a character in its own right. The standard 115 * method for detecting the difference is to wait for 2 seconds to see if there 116 * are any other characters. kdb is complicated by the lack of a timer service 117 * (interrupts are off), by multiple input sources. Escape sequence processing 118 * has to be done as states in the polling loop. 119 * 120 * Return: The key pressed or a control code derived from an escape sequence. 121 */ 122 char kdb_getchar(void) 123 { 124 #define ESCAPE_UDELAY 1000 125 #define ESCAPE_DELAY (2*1000000/ESCAPE_UDELAY) /* 2 seconds worth of udelays */ 126 char buf[4]; /* longest vt100 escape sequence is 4 bytes */ 127 char *pbuf = buf; 128 int escape_delay = 0; 129 get_char_func *f, *f_prev = NULL; 130 int key; 131 static bool last_char_was_cr; 132 133 for (f = &kdb_poll_funcs[0]; ; ++f) { 134 if (*f == NULL) { 135 /* Reset NMI watchdog once per poll loop */ 136 touch_nmi_watchdog(); 137 f = &kdb_poll_funcs[0]; 138 } 139 140 key = (*f)(); 141 if (key == -1) { 142 if (escape_delay) { 143 udelay(ESCAPE_UDELAY); 144 if (--escape_delay == 0) 145 return '\e'; 146 } 147 continue; 148 } 149 150 /* 151 * The caller expects that newlines are either CR or LF. However 152 * some terminals send _both_ CR and LF. Avoid having to handle 153 * this in the caller by stripping the LF if we saw a CR right 154 * before. 155 */ 156 if (last_char_was_cr && key == '\n') { 157 last_char_was_cr = false; 158 continue; 159 } 160 last_char_was_cr = (key == '\r'); 161 162 /* 163 * When the first character is received (or we get a change 164 * input source) we set ourselves up to handle an escape 165 * sequences (just in case). 166 */ 167 if (f_prev != f) { 168 f_prev = f; 169 pbuf = buf; 170 escape_delay = ESCAPE_DELAY; 171 } 172 173 *pbuf++ = key; 174 key = kdb_handle_escape(buf, pbuf - buf); 175 if (key < 0) /* no escape sequence; return best character */ 176 return buf[pbuf - buf == 2 ? 1 : 0]; 177 if (key > 0) 178 return key; 179 } 180 181 unreachable(); 182 } 183 184 /** 185 * kdb_position_cursor() - Place cursor in the correct horizontal position 186 * @prompt: Nil-terminated string containing the prompt string 187 * @buffer: Nil-terminated string containing the entire command line 188 * @cp: Cursor position, pointer the character in buffer where the cursor 189 * should be positioned. 190 * 191 * The cursor is positioned by sending a carriage-return and then printing 192 * the content of the line until we reach the correct cursor position. 193 * 194 * There is some additional fine detail here. 195 * 196 * Firstly, even though kdb_printf() will correctly format zero-width fields 197 * we want the second call to kdb_printf() to be conditional. That keeps things 198 * a little cleaner when LOGGING=1. 199 * 200 * Secondly, we can't combine everything into one call to kdb_printf() since 201 * that renders into a fixed length buffer and the combined print could result 202 * in unwanted truncation. 203 */ 204 static void kdb_position_cursor(char *prompt, char *buffer, char *cp) 205 { 206 kdb_printf("\r%s", prompt); 207 if (cp > buffer) 208 kdb_printf("%.*s", (int)(cp - buffer), buffer); 209 } 210 211 /* 212 * kdb_read 213 * 214 * This function reads a string of characters, terminated by 215 * a newline, or by reaching the end of the supplied buffer, 216 * from the current kernel debugger console device. 217 * Parameters: 218 * buffer - Address of character buffer to receive input characters. 219 * bufsize - size, in bytes, of the character buffer 220 * Returns: 221 * Returns a pointer to the buffer containing the received 222 * character string. This string will be terminated by a 223 * newline character. 224 * Locking: 225 * No locks are required to be held upon entry to this 226 * function. It is not reentrant - it relies on the fact 227 * that while kdb is running on only one "master debug" cpu. 228 * Remarks: 229 * The buffer size must be >= 2. 230 */ 231 232 static char *kdb_read(char *buffer, size_t bufsize) 233 { 234 char *cp = buffer; 235 char *bufend = buffer+bufsize-2; /* Reserve space for newline 236 * and null byte */ 237 char *lastchar; 238 char *p_tmp; 239 char tmp; 240 static char tmpbuffer[CMD_BUFLEN]; 241 int len = strlen(buffer); 242 int len_tmp; 243 int tab = 0; 244 int count; 245 int i; 246 int diag, dtab_count; 247 int key, ret; 248 249 diag = kdbgetintenv("DTABCOUNT", &dtab_count); 250 if (diag) 251 dtab_count = 30; 252 253 if (len > 0) { 254 cp += len; 255 if (*(buffer+len-1) == '\n') 256 cp--; 257 } 258 259 lastchar = cp; 260 *cp = '\0'; 261 kdb_printf("%s", buffer); 262 poll_again: 263 key = kdb_getchar(); 264 if (key != 9) 265 tab = 0; 266 switch (key) { 267 case 8: /* backspace */ 268 if (cp > buffer) { 269 memmove(cp-1, cp, lastchar - cp + 1); 270 lastchar--; 271 cp--; 272 kdb_printf("\b%s ", cp); 273 kdb_position_cursor(kdb_prompt_str, buffer, cp); 274 } 275 break; 276 case 10: /* linefeed */ 277 case 13: /* carriage return */ 278 *lastchar++ = '\n'; 279 *lastchar++ = '\0'; 280 if (!KDB_STATE(KGDB_TRANS)) { 281 KDB_STATE_SET(KGDB_TRANS); 282 kdb_printf("%s", buffer); 283 } 284 kdb_printf("\n"); 285 return buffer; 286 case 4: /* Del */ 287 if (cp < lastchar) { 288 memmove(cp, cp+1, lastchar - cp); 289 lastchar--; 290 kdb_printf("%s ", cp); 291 kdb_position_cursor(kdb_prompt_str, buffer, cp); 292 } 293 break; 294 case 1: /* Home */ 295 if (cp > buffer) { 296 cp = buffer; 297 kdb_position_cursor(kdb_prompt_str, buffer, cp); 298 } 299 break; 300 case 5: /* End */ 301 if (cp < lastchar) { 302 kdb_printf("%s", cp); 303 cp = lastchar; 304 } 305 break; 306 case 2: /* Left */ 307 if (cp > buffer) { 308 kdb_printf("\b"); 309 --cp; 310 } 311 break; 312 case 14: /* Down */ 313 case 16: /* Up */ 314 kdb_printf("\r%*c\r", 315 (int)(strlen(kdb_prompt_str) + (lastchar - buffer)), 316 ' '); 317 *lastchar = (char)key; 318 *(lastchar+1) = '\0'; 319 return lastchar; 320 case 6: /* Right */ 321 if (cp < lastchar) { 322 kdb_printf("%c", *cp); 323 ++cp; 324 } 325 break; 326 case 9: /* Tab */ 327 if (tab < 2) 328 ++tab; 329 330 tmp = *cp; 331 *cp = '\0'; 332 p_tmp = strrchr(buffer, ' '); 333 p_tmp = (p_tmp ? p_tmp + 1 : buffer); 334 strscpy(tmpbuffer, p_tmp); 335 *cp = tmp; 336 337 len = strlen(tmpbuffer); 338 count = kallsyms_symbol_complete(tmpbuffer, sizeof(tmpbuffer)); 339 if (tab == 2 && count > 0) { 340 kdb_printf("\n%d symbols are found.", count); 341 if (count > dtab_count) { 342 count = dtab_count; 343 kdb_printf(" But only first %d symbols will" 344 " be printed.\nYou can change the" 345 " environment variable DTABCOUNT.", 346 count); 347 } 348 kdb_printf("\n"); 349 for (i = 0; i < count; i++) { 350 ret = kallsyms_symbol_next(tmpbuffer, i, sizeof(tmpbuffer)); 351 if (WARN_ON(!ret)) 352 break; 353 if (ret != -E2BIG) 354 kdb_printf("%s ", tmpbuffer); 355 else 356 kdb_printf("%s... ", tmpbuffer); 357 tmpbuffer[len] = '\0'; 358 } 359 if (i >= dtab_count) 360 kdb_printf("..."); 361 kdb_printf("\n"); 362 kdb_printf("%s", kdb_prompt_str); 363 kdb_printf("%s", buffer); 364 if (cp != lastchar) 365 kdb_position_cursor(kdb_prompt_str, buffer, cp); 366 } else if (tab != 2 && count > 0) { 367 /* How many new characters do we want from tmpbuffer? */ 368 len_tmp = strlen(tmpbuffer) - len; 369 if (lastchar + len_tmp >= bufend) 370 len_tmp = bufend - lastchar; 371 372 if (len_tmp) { 373 /* + 1 ensures the '\0' is memmove'd */ 374 memmove(cp+len_tmp, cp, (lastchar-cp) + 1); 375 memcpy(cp, tmpbuffer+len, len_tmp); 376 kdb_printf("%s", cp); 377 cp += len_tmp; 378 lastchar += len_tmp; 379 if (cp != lastchar) 380 kdb_position_cursor(kdb_prompt_str, 381 buffer, cp); 382 } 383 } 384 kdb_nextline = 1; /* reset output line number */ 385 break; 386 default: 387 if (key >= 32 && lastchar < bufend) { 388 if (cp < lastchar) { 389 memmove(cp+1, cp, lastchar - cp + 1); 390 lastchar++; 391 *cp = key; 392 kdb_printf("%s", cp); 393 ++cp; 394 kdb_position_cursor(kdb_prompt_str, buffer, cp); 395 } else { 396 *++lastchar = '\0'; 397 *cp++ = key; 398 /* The kgdb transition check will hide 399 * printed characters if we think that 400 * kgdb is connecting, until the check 401 * fails */ 402 if (!KDB_STATE(KGDB_TRANS)) { 403 if (kgdb_transition_check(buffer)) 404 return buffer; 405 } else { 406 kdb_printf("%c", key); 407 } 408 } 409 /* Special escape to kgdb */ 410 if (lastchar - buffer >= 5 && 411 strcmp(lastchar - 5, "$?#3f") == 0) { 412 kdb_gdb_state_pass(lastchar - 5); 413 strcpy(buffer, "kgdb"); 414 KDB_STATE_SET(DOING_KGDB); 415 return buffer; 416 } 417 if (lastchar - buffer >= 11 && 418 strcmp(lastchar - 11, "$qSupported") == 0) { 419 kdb_gdb_state_pass(lastchar - 11); 420 strcpy(buffer, "kgdb"); 421 KDB_STATE_SET(DOING_KGDB); 422 return buffer; 423 } 424 } 425 break; 426 } 427 goto poll_again; 428 } 429 430 /* 431 * kdb_getstr 432 * 433 * Print the prompt string and read a command from the 434 * input device. 435 * 436 * Parameters: 437 * buffer Address of buffer to receive command 438 * bufsize Size of buffer in bytes 439 * prompt Pointer to string to use as prompt string 440 * Returns: 441 * Pointer to command buffer. 442 * Locking: 443 * None. 444 * Remarks: 445 * For SMP kernels, the processor number will be 446 * substituted for %d, %x or %o in the prompt. 447 */ 448 449 char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt) 450 { 451 if (prompt && kdb_prompt_str != prompt) 452 strscpy(kdb_prompt_str, prompt); 453 kdb_printf("%s", kdb_prompt_str); 454 kdb_nextline = 1; /* Prompt and input resets line number */ 455 return kdb_read(buffer, bufsize); 456 } 457 458 /* 459 * kdb_input_flush 460 * 461 * Get rid of any buffered console input. 462 * 463 * Parameters: 464 * none 465 * Returns: 466 * nothing 467 * Locking: 468 * none 469 * Remarks: 470 * Call this function whenever you want to flush input. If there is any 471 * outstanding input, it ignores all characters until there has been no 472 * data for approximately 1ms. 473 */ 474 475 static void kdb_input_flush(void) 476 { 477 get_char_func *f; 478 int res; 479 int flush_delay = 1; 480 while (flush_delay) { 481 flush_delay--; 482 empty: 483 touch_nmi_watchdog(); 484 for (f = &kdb_poll_funcs[0]; *f; ++f) { 485 res = (*f)(); 486 if (res != -1) { 487 flush_delay = 1; 488 goto empty; 489 } 490 } 491 if (flush_delay) 492 mdelay(1); 493 } 494 } 495 496 /* 497 * kdb_printf 498 * 499 * Print a string to the output device(s). 500 * 501 * Parameters: 502 * printf-like format and optional args. 503 * Returns: 504 * 0 505 * Locking: 506 * None. 507 * Remarks: 508 * use 'kdbcons->write()' to avoid polluting 'log_buf' with 509 * kdb output. 510 * 511 * If the user is doing a cmd args | grep srch 512 * then kdb_grepping_flag is set. 513 * In that case we need to accumulate full lines (ending in \n) before 514 * searching for the pattern. 515 */ 516 517 static char kdb_buffer[256]; /* A bit too big to go on stack */ 518 static char *next_avail = kdb_buffer; 519 static int size_avail; 520 static int suspend_grep; 521 522 /* 523 * search arg1 to see if it contains arg2 524 * (kdmain.c provides flags for ^pat and pat$) 525 * 526 * return 1 for found, 0 for not found 527 */ 528 static int kdb_search_string(char *searched, char *searchfor) 529 { 530 char firstchar, *cp; 531 int len1, len2; 532 533 /* not counting the newline at the end of "searched" */ 534 len1 = strlen(searched)-1; 535 len2 = strlen(searchfor); 536 if (len1 < len2) 537 return 0; 538 if (kdb_grep_leading && kdb_grep_trailing && len1 != len2) 539 return 0; 540 if (kdb_grep_leading) { 541 if (!strncmp(searched, searchfor, len2)) 542 return 1; 543 } else if (kdb_grep_trailing) { 544 if (!strncmp(searched+len1-len2, searchfor, len2)) 545 return 1; 546 } else { 547 firstchar = *searchfor; 548 cp = searched; 549 while ((cp = strchr(cp, firstchar))) { 550 if (!strncmp(cp, searchfor, len2)) 551 return 1; 552 cp++; 553 } 554 } 555 return 0; 556 } 557 558 static void kdb_msg_write(const char *msg, int msg_len) 559 { 560 struct console *c; 561 const char *cp; 562 int cookie; 563 int len; 564 565 if (msg_len == 0) 566 return; 567 568 cp = msg; 569 len = msg_len; 570 571 while (len--) { 572 dbg_io_ops->write_char(*cp); 573 cp++; 574 } 575 576 /* 577 * The console_srcu_read_lock() only provides safe console list 578 * traversal. The use of the ->write() callback relies on all other 579 * CPUs being stopped at the moment and console drivers being able to 580 * handle reentrance when @oops_in_progress is set. 581 * 582 * There is no guarantee that every console driver can handle 583 * reentrance in this way; the developer deploying the debugger 584 * is responsible for ensuring that the console drivers they 585 * have selected handle reentrance appropriately. 586 */ 587 cookie = console_srcu_read_lock(); 588 for_each_console_srcu(c) { 589 short flags = console_srcu_read_flags(c); 590 591 if (!console_is_usable(c, flags, true)) 592 continue; 593 if (c == dbg_io_ops->cons) 594 continue; 595 596 if (flags & CON_NBCON) { 597 struct nbcon_write_context wctxt = { }; 598 599 /* 600 * Do not continue if the console is NBCON and the context 601 * can't be acquired. 602 */ 603 if (!nbcon_kdb_try_acquire(c, &wctxt)) 604 continue; 605 606 nbcon_write_context_set_buf(&wctxt, (char *)msg, msg_len); 607 608 c->write_atomic(c, &wctxt); 609 nbcon_kdb_release(&wctxt); 610 } else { 611 /* 612 * Set oops_in_progress to encourage the console drivers to 613 * disregard their internal spin locks: in the current calling 614 * context the risk of deadlock is a bigger problem than risks 615 * due to re-entering the console driver. We operate directly on 616 * oops_in_progress rather than using bust_spinlocks() because 617 * the calls bust_spinlocks() makes on exit are not appropriate 618 * for this calling context. 619 */ 620 ++oops_in_progress; 621 c->write(c, msg, msg_len); 622 --oops_in_progress; 623 } 624 touch_nmi_watchdog(); 625 } 626 console_srcu_read_unlock(cookie); 627 } 628 629 int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap) 630 { 631 int diag; 632 int linecount; 633 int colcount; 634 int logging, saved_loglevel = 0; 635 int retlen = 0; 636 int fnd, len; 637 int this_cpu, old_cpu; 638 char *cp, *cp2, *cphold = NULL, replaced_byte = ' '; 639 char *moreprompt = "more> "; 640 unsigned long flags; 641 642 /* Serialize kdb_printf if multiple cpus try to write at once. 643 * But if any cpu goes recursive in kdb, just print the output, 644 * even if it is interleaved with any other text. 645 */ 646 local_irq_save(flags); 647 this_cpu = smp_processor_id(); 648 for (;;) { 649 old_cpu = cmpxchg(&kdb_printf_cpu, -1, this_cpu); 650 if (old_cpu == -1 || old_cpu == this_cpu) 651 break; 652 653 cpu_relax(); 654 } 655 656 diag = kdbgetintenv("LINES", &linecount); 657 if (diag || linecount <= 1) 658 linecount = 24; 659 660 diag = kdbgetintenv("COLUMNS", &colcount); 661 if (diag || colcount <= 1) 662 colcount = 80; 663 664 diag = kdbgetintenv("LOGGING", &logging); 665 if (diag) 666 logging = 0; 667 668 if (!kdb_grepping_flag || suspend_grep) { 669 /* normally, every vsnprintf starts a new buffer */ 670 next_avail = kdb_buffer; 671 size_avail = sizeof(kdb_buffer); 672 } 673 vsnprintf(next_avail, size_avail, fmt, ap); 674 675 /* 676 * If kdb_parse() found that the command was cmd xxx | grep yyy 677 * then kdb_grepping_flag is set, and kdb_grep_string contains yyy 678 * 679 * Accumulate the print data up to a newline before searching it. 680 * (vsnprintf does null-terminate the string that it generates) 681 */ 682 683 /* skip the search if prints are temporarily unconditional */ 684 if (!suspend_grep && kdb_grepping_flag) { 685 cp = strchr(kdb_buffer, '\n'); 686 if (!cp) { 687 /* 688 * Special cases that don't end with newlines 689 * but should be written without one: 690 * The "[nn]kdb> " prompt should 691 * appear at the front of the buffer. 692 * 693 * The "[nn]more " prompt should also be 694 * (MOREPROMPT -> moreprompt) 695 * written * but we print that ourselves, 696 * we set the suspend_grep flag to make 697 * it unconditional. 698 * 699 */ 700 if (next_avail == kdb_buffer) { 701 /* 702 * these should occur after a newline, 703 * so they will be at the front of the 704 * buffer 705 */ 706 cp2 = kdb_buffer; 707 len = strlen(kdb_prompt_str); 708 if (!strncmp(cp2, kdb_prompt_str, len)) { 709 /* 710 * We're about to start a new 711 * command, so we can go back 712 * to normal mode. 713 */ 714 kdb_grepping_flag = 0; 715 goto kdb_printit; 716 } 717 } 718 /* no newline; don't search/write the buffer 719 until one is there */ 720 len = strlen(kdb_buffer); 721 next_avail = kdb_buffer + len; 722 size_avail = sizeof(kdb_buffer) - len; 723 goto kdb_print_out; 724 } 725 726 /* 727 * The newline is present; print through it or discard 728 * it, depending on the results of the search. 729 */ 730 cp++; /* to byte after the newline */ 731 replaced_byte = *cp; /* remember what it was */ 732 cphold = cp; /* remember where it was */ 733 *cp = '\0'; /* end the string for our search */ 734 735 /* 736 * We now have a newline at the end of the string 737 * Only continue with this output if it contains the 738 * search string. 739 */ 740 fnd = kdb_search_string(kdb_buffer, kdb_grep_string); 741 if (!fnd) { 742 /* 743 * At this point the complete line at the start 744 * of kdb_buffer can be discarded, as it does 745 * not contain what the user is looking for. 746 * Shift the buffer left. 747 */ 748 *cphold = replaced_byte; 749 len = strlen(cphold); 750 /* Use memmove() because the buffers overlap */ 751 memmove(kdb_buffer, cphold, len + 1); 752 next_avail = kdb_buffer + len; 753 size_avail = sizeof(kdb_buffer) - len; 754 goto kdb_print_out; 755 } 756 if (kdb_grepping_flag >= KDB_GREPPING_FLAG_SEARCH) { 757 /* 758 * This was a interactive search (using '/' at more 759 * prompt) and it has completed. Replace the \0 with 760 * its original value to ensure multi-line strings 761 * are handled properly, and return to normal mode. 762 */ 763 *cphold = replaced_byte; 764 kdb_grepping_flag = 0; 765 } 766 /* 767 * at this point the string is a full line and 768 * should be printed, up to the null. 769 */ 770 } 771 kdb_printit: 772 773 /* 774 * Write to all consoles. 775 */ 776 retlen = strlen(kdb_buffer); 777 cp = (char *) printk_skip_headers(kdb_buffer); 778 if (!dbg_kdb_mode && kgdb_connected) 779 gdbstub_msg_write(cp, retlen - (cp - kdb_buffer)); 780 else 781 kdb_msg_write(cp, retlen - (cp - kdb_buffer)); 782 783 if (logging) { 784 saved_loglevel = console_loglevel; 785 console_loglevel = CONSOLE_LOGLEVEL_SILENT; 786 if (printk_get_level(kdb_buffer) || src == KDB_MSGSRC_PRINTK) 787 printk("%s", kdb_buffer); 788 else 789 pr_info("%s", kdb_buffer); 790 } 791 792 if (KDB_STATE(PAGER)) { 793 /* 794 * Check printed string to decide how to bump the 795 * kdb_nextline to control when the more prompt should 796 * show up. 797 */ 798 int got = 0; 799 len = retlen; 800 while (len--) { 801 if (kdb_buffer[len] == '\n') { 802 kdb_nextline++; 803 got = 0; 804 } else if (kdb_buffer[len] == '\r') { 805 got = 0; 806 } else { 807 got++; 808 } 809 } 810 kdb_nextline += got / (colcount + 1); 811 } 812 813 /* check for having reached the LINES number of printed lines */ 814 if (kdb_nextline >= linecount) { 815 char ch; 816 817 /* Watch out for recursion here. Any routine that calls 818 * kdb_printf will come back through here. And kdb_read 819 * uses kdb_printf to echo on serial consoles ... 820 */ 821 kdb_nextline = 1; /* In case of recursion */ 822 823 /* 824 * Pause until cr. 825 */ 826 moreprompt = kdbgetenv("MOREPROMPT"); 827 if (moreprompt == NULL) 828 moreprompt = "more> "; 829 830 kdb_input_flush(); 831 kdb_msg_write(moreprompt, strlen(moreprompt)); 832 833 if (logging) 834 printk("%s", moreprompt); 835 836 ch = kdb_getchar(); 837 kdb_nextline = 1; /* Really set output line 1 */ 838 839 /* empty and reset the buffer: */ 840 kdb_buffer[0] = '\0'; 841 next_avail = kdb_buffer; 842 size_avail = sizeof(kdb_buffer); 843 if ((ch == 'q') || (ch == 'Q')) { 844 /* user hit q or Q */ 845 KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */ 846 KDB_STATE_CLEAR(PAGER); 847 /* end of command output; back to normal mode */ 848 kdb_grepping_flag = 0; 849 kdb_printf("\n"); 850 } else if (ch == ' ') { 851 kdb_printf("\r"); 852 suspend_grep = 1; /* for this recursion */ 853 } else if (ch == '\n' || ch == '\r') { 854 kdb_nextline = linecount - 1; 855 kdb_printf("\r"); 856 suspend_grep = 1; /* for this recursion */ 857 } else if (ch == '/' && !kdb_grepping_flag) { 858 kdb_printf("\r"); 859 kdb_getstr(kdb_grep_string, KDB_GREP_STRLEN, 860 kdbgetenv("SEARCHPROMPT") ?: "search> "); 861 *strchrnul(kdb_grep_string, '\n') = '\0'; 862 kdb_grepping_flag += KDB_GREPPING_FLAG_SEARCH; 863 suspend_grep = 1; /* for this recursion */ 864 } else if (ch) { 865 /* user hit something unexpected */ 866 suspend_grep = 1; /* for this recursion */ 867 if (ch != '/') 868 kdb_printf( 869 "\nOnly 'q', 'Q' or '/' are processed at " 870 "more prompt, input ignored\n"); 871 else 872 kdb_printf("\n'/' cannot be used during | " 873 "grep filtering, input ignored\n"); 874 } else if (kdb_grepping_flag) { 875 /* user hit enter */ 876 suspend_grep = 1; /* for this recursion */ 877 kdb_printf("\n"); 878 } 879 kdb_input_flush(); 880 } 881 882 /* 883 * For grep searches, shift the printed string left. 884 * replaced_byte contains the character that was overwritten with 885 * the terminating null, and cphold points to the null. 886 * Then adjust the notion of available space in the buffer. 887 */ 888 if (kdb_grepping_flag && !suspend_grep) { 889 *cphold = replaced_byte; 890 len = strlen(cphold); 891 /* Use memmove() because the buffers overlap */ 892 memmove(kdb_buffer, cphold, len + 1); 893 next_avail = kdb_buffer + len; 894 size_avail = sizeof(kdb_buffer) - len; 895 } 896 897 kdb_print_out: 898 suspend_grep = 0; /* end of what may have been a recursive call */ 899 if (logging) 900 console_loglevel = saved_loglevel; 901 /* kdb_printf_cpu locked the code above. */ 902 smp_store_release(&kdb_printf_cpu, old_cpu); 903 local_irq_restore(flags); 904 return retlen; 905 } 906 907 int kdb_printf(const char *fmt, ...) 908 { 909 va_list ap; 910 int r; 911 912 va_start(ap, fmt); 913 r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap); 914 va_end(ap); 915 916 return r; 917 } 918 EXPORT_SYMBOL_GPL(kdb_printf); 919