2 * Kernel Debugger Architecture Independent Console I/O handler
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (c) 1999-2006 Silicon Graphics, Inc. All Rights Reserved.
9 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
12 #include <linux/types.h>
13 #include <linux/ctype.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/kdev_t.h>
17 #include <linux/console.h>
18 #include <linux/string.h>
19 #include <linux/sched.h>
20 #include <linux/smp.h>
21 #include <linux/nmi.h>
22 #include <linux/delay.h>
23 #include <linux/kgdb.h>
24 #include <linux/kdb.h>
25 #include <linux/kallsyms.h>
26 #include "kdb_private.h"
28 #define CMD_BUFLEN 256
29 char kdb_prompt_str[CMD_BUFLEN];
32 int kdb_printf_cpu = -1;
34 static int kgdb_transition_check(char *buffer)
36 if (buffer[0] != '+' && buffer[0] != '$') {
37 KDB_STATE_SET(KGDB_TRANS);
38 kdb_printf("%s", buffer);
40 int slen = strlen(buffer);
41 if (slen > 3 && buffer[slen - 3] == '#') {
42 kdb_gdb_state_pass(buffer);
43 strcpy(buffer, "kgdb");
44 KDB_STATE_SET(DOING_KGDB);
52 * kdb_handle_escape() - validity check on an accumulated escape sequence.
53 * @buf: Accumulated escape characters to be examined. Note that buf
54 * is not a string, it is an array of characters and need not be
56 * @sz: Number of accumulated escape characters.
58 * Return: -1 if the escape sequence is unwanted, 0 if it is incomplete,
59 * otherwise it returns a mapped key value to pass to the upper layers.
61 static int kdb_handle_escape(char *buf, size_t sz)
63 char *lastkey = buf + sz - 1;
71 case 2: /* \e<something> */
78 case 'A': /* \e[A, up arrow */
80 case 'B': /* \e[B, down arrow */
82 case 'C': /* \e[C, right arrow */
84 case 'D': /* \e[D, left arrow */
86 case '1': /* \e[<1,3,4>], may be home, del, end */
94 if (*lastkey == '~') {
96 case '1': /* \e[1~, home */
98 case '3': /* \e[3~, del */
100 case '4': /* \e[4~, end */
111 * kdb_getchar() - Read a single character from a kdb console (or consoles).
113 * Other than polling the various consoles that are currently enabled,
114 * most of the work done in this function is dealing with escape sequences.
116 * An escape key could be the start of a vt100 control sequence such as \e[D
117 * (left arrow) or it could be a character in its own right. The standard
118 * method for detecting the difference is to wait for 2 seconds to see if there
119 * are any other characters. kdb is complicated by the lack of a timer service
120 * (interrupts are off), by multiple input sources. Escape sequence processing
121 * has to be done as states in the polling loop.
123 * Return: The key pressed or a control code derived from an escape sequence.
125 char kdb_getchar(void)
127 #define ESCAPE_UDELAY 1000
128 #define ESCAPE_DELAY (2*1000000/ESCAPE_UDELAY) /* 2 seconds worth of udelays */
129 char buf[4]; /* longest vt100 escape sequence is 4 bytes */
131 int escape_delay = 0;
132 get_char_func *f, *f_prev = NULL;
134 static bool last_char_was_cr;
136 for (f = &kdb_poll_funcs[0]; ; ++f) {
138 /* Reset NMI watchdog once per poll loop */
139 touch_nmi_watchdog();
140 f = &kdb_poll_funcs[0];
146 udelay(ESCAPE_UDELAY);
147 if (--escape_delay == 0)
154 * The caller expects that newlines are either CR or LF. However
155 * some terminals send _both_ CR and LF. Avoid having to handle
156 * this in the caller by stripping the LF if we saw a CR right
159 if (last_char_was_cr && key == '\n') {
160 last_char_was_cr = false;
163 last_char_was_cr = (key == '\r');
166 * When the first character is received (or we get a change
167 * input source) we set ourselves up to handle an escape
168 * sequences (just in case).
173 escape_delay = ESCAPE_DELAY;
177 key = kdb_handle_escape(buf, pbuf - buf);
178 if (key < 0) /* no escape sequence; return best character */
179 return buf[pbuf - buf == 2 ? 1 : 0];
190 * This function reads a string of characters, terminated by
191 * a newline, or by reaching the end of the supplied buffer,
192 * from the current kernel debugger console device.
194 * buffer - Address of character buffer to receive input characters.
195 * bufsize - size, in bytes, of the character buffer
197 * Returns a pointer to the buffer containing the received
198 * character string. This string will be terminated by a
201 * No locks are required to be held upon entry to this
202 * function. It is not reentrant - it relies on the fact
203 * that while kdb is running on only one "master debug" cpu.
205 * The buffer size must be >= 2.
208 static char *kdb_read(char *buffer, size_t bufsize)
211 char *bufend = buffer+bufsize-2; /* Reserve space for newline
216 static char tmpbuffer[CMD_BUFLEN];
217 int len = strlen(buffer);
222 int diag, dtab_count;
223 int key, buf_size, ret;
226 diag = kdbgetintenv("DTABCOUNT", &dtab_count);
232 if (*(buffer+len-1) == '\n')
238 kdb_printf("%s", buffer);
244 case 8: /* backspace */
247 memcpy(tmpbuffer, cp, lastchar - cp);
248 memcpy(cp-1, tmpbuffer, lastchar - cp);
250 *(--lastchar) = '\0';
252 kdb_printf("\b%s \r", cp);
255 kdb_printf(kdb_prompt_str);
256 kdb_printf("%s", buffer);
260 case 10: /* linefeed */
261 case 13: /* carriage return */
264 if (!KDB_STATE(KGDB_TRANS)) {
265 KDB_STATE_SET(KGDB_TRANS);
266 kdb_printf("%s", buffer);
272 memcpy(tmpbuffer, cp+1, lastchar - cp - 1);
273 memcpy(cp, tmpbuffer, lastchar - cp - 1);
274 *(--lastchar) = '\0';
275 kdb_printf("%s \r", cp);
278 kdb_printf(kdb_prompt_str);
279 kdb_printf("%s", buffer);
286 kdb_printf(kdb_prompt_str);
292 kdb_printf("%s", cp);
303 memset(tmpbuffer, ' ',
304 strlen(kdb_prompt_str) + (lastchar-buffer));
305 *(tmpbuffer+strlen(kdb_prompt_str) +
306 (lastchar-buffer)) = '\0';
307 kdb_printf("\r%s\r", tmpbuffer);
308 *lastchar = (char)key;
309 *(lastchar+1) = '\0';
313 kdb_printf("%c", *cp);
318 memset(tmpbuffer, ' ',
319 strlen(kdb_prompt_str) + (lastchar-buffer));
320 *(tmpbuffer+strlen(kdb_prompt_str) +
321 (lastchar-buffer)) = '\0';
322 kdb_printf("\r%s\r", tmpbuffer);
323 *lastchar = (char)key;
324 *(lastchar+1) = '\0';
330 while (*p_tmp == ' ')
334 memcpy(tmpbuffer, p_tmp, cp-p_tmp);
335 *(tmpbuffer + (cp-p_tmp)) = '\0';
336 p_tmp = strrchr(tmpbuffer, ' ');
342 buf_size = sizeof(tmpbuffer) - (p_tmp - tmpbuffer);
343 count = kallsyms_symbol_complete(p_tmp, buf_size);
344 if (tab == 2 && count > 0) {
345 kdb_printf("\n%d symbols are found.", count);
346 if (count > dtab_count) {
348 kdb_printf(" But only first %d symbols will"
349 " be printed.\nYou can change the"
350 " environment variable DTABCOUNT.",
354 for (i = 0; i < count; i++) {
355 ret = kallsyms_symbol_next(p_tmp, i, buf_size);
359 kdb_printf("%s ", p_tmp);
361 kdb_printf("%s... ", p_tmp);
362 *(p_tmp + len) = '\0';
367 kdb_printf(kdb_prompt_str);
368 kdb_printf("%s", buffer);
369 } else if (tab != 2 && count > 0) {
370 len_tmp = strlen(p_tmp);
371 strncpy(p_tmp+len_tmp, cp, lastchar-cp+1);
372 len_tmp = strlen(p_tmp);
373 strncpy(cp, p_tmp+len, len_tmp-len + 1);
375 kdb_printf("%s", cp);
379 kdb_nextline = 1; /* reset output line number */
382 if (key >= 32 && lastchar < bufend) {
384 memcpy(tmpbuffer, cp, lastchar - cp);
385 memcpy(cp+1, tmpbuffer, lastchar - cp);
388 kdb_printf("%s\r", cp);
392 kdb_printf(kdb_prompt_str);
393 kdb_printf("%s", buffer);
398 /* The kgdb transition check will hide
399 * printed characters if we think that
400 * kgdb is connecting, until the check
402 if (!KDB_STATE(KGDB_TRANS)) {
403 if (kgdb_transition_check(buffer))
406 kdb_printf("%c", key);
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);
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);
433 * Print the prompt string and read a command from the
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
441 * Pointer to command buffer.
445 * For SMP kernels, the processor number will be
446 * substituted for %d, %x or %o in the prompt.
449 char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
451 if (prompt && kdb_prompt_str != prompt)
452 strscpy(kdb_prompt_str, prompt, CMD_BUFLEN);
453 kdb_printf(kdb_prompt_str);
454 kdb_nextline = 1; /* Prompt and input resets line number */
455 return kdb_read(buffer, bufsize);
461 * Get rid of any buffered console input.
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.
475 static void kdb_input_flush(void)
480 while (flush_delay) {
483 touch_nmi_watchdog();
484 for (f = &kdb_poll_funcs[0]; *f; ++f) {
499 * Print a string to the output device(s).
502 * printf-like format and optional args.
508 * use 'kdbcons->write()' to avoid polluting 'log_buf' with
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.
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;
523 * search arg1 to see if it contains arg2
524 * (kdmain.c provides flags for ^pat and pat$)
526 * return 1 for found, 0 for not found
528 static int kdb_search_string(char *searched, char *searchfor)
533 /* not counting the newline at the end of "searched" */
534 len1 = strlen(searched)-1;
535 len2 = strlen(searchfor);
538 if (kdb_grep_leading && kdb_grep_trailing && len1 != len2)
540 if (kdb_grep_leading) {
541 if (!strncmp(searched, searchfor, len2))
543 } else if (kdb_grep_trailing) {
544 if (!strncmp(searched+len1-len2, searchfor, len2))
547 firstchar = *searchfor;
549 while ((cp = strchr(cp, firstchar))) {
550 if (!strncmp(cp, searchfor, len2))
558 static void kdb_msg_write(const char *msg, int msg_len)
572 dbg_io_ops->write_char(*cp);
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.
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.
587 cookie = console_srcu_read_lock();
588 for_each_console_srcu(c) {
589 if (!(console_srcu_read_flags(c) & CON_ENABLED))
591 if (c == dbg_io_ops->cons)
596 * Set oops_in_progress to encourage the console drivers to
597 * disregard their internal spin locks: in the current calling
598 * context the risk of deadlock is a bigger problem than risks
599 * due to re-entering the console driver. We operate directly on
600 * oops_in_progress rather than using bust_spinlocks() because
601 * the calls bust_spinlocks() makes on exit are not appropriate
602 * for this calling context.
605 c->write(c, msg, msg_len);
607 touch_nmi_watchdog();
609 console_srcu_read_unlock(cookie);
612 int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
617 int logging, saved_loglevel = 0;
620 int this_cpu, old_cpu;
621 char *cp, *cp2, *cphold = NULL, replaced_byte = ' ';
622 char *moreprompt = "more> ";
625 /* Serialize kdb_printf if multiple cpus try to write at once.
626 * But if any cpu goes recursive in kdb, just print the output,
627 * even if it is interleaved with any other text.
629 local_irq_save(flags);
630 this_cpu = smp_processor_id();
632 old_cpu = cmpxchg(&kdb_printf_cpu, -1, this_cpu);
633 if (old_cpu == -1 || old_cpu == this_cpu)
639 diag = kdbgetintenv("LINES", &linecount);
640 if (diag || linecount <= 1)
643 diag = kdbgetintenv("COLUMNS", &colcount);
644 if (diag || colcount <= 1)
647 diag = kdbgetintenv("LOGGING", &logging);
651 if (!kdb_grepping_flag || suspend_grep) {
652 /* normally, every vsnprintf starts a new buffer */
653 next_avail = kdb_buffer;
654 size_avail = sizeof(kdb_buffer);
656 vsnprintf(next_avail, size_avail, fmt, ap);
659 * If kdb_parse() found that the command was cmd xxx | grep yyy
660 * then kdb_grepping_flag is set, and kdb_grep_string contains yyy
662 * Accumulate the print data up to a newline before searching it.
663 * (vsnprintf does null-terminate the string that it generates)
666 /* skip the search if prints are temporarily unconditional */
667 if (!suspend_grep && kdb_grepping_flag) {
668 cp = strchr(kdb_buffer, '\n');
671 * Special cases that don't end with newlines
672 * but should be written without one:
673 * The "[nn]kdb> " prompt should
674 * appear at the front of the buffer.
676 * The "[nn]more " prompt should also be
677 * (MOREPROMPT -> moreprompt)
678 * written * but we print that ourselves,
679 * we set the suspend_grep flag to make
683 if (next_avail == kdb_buffer) {
685 * these should occur after a newline,
686 * so they will be at the front of the
690 len = strlen(kdb_prompt_str);
691 if (!strncmp(cp2, kdb_prompt_str, len)) {
693 * We're about to start a new
694 * command, so we can go back
697 kdb_grepping_flag = 0;
701 /* no newline; don't search/write the buffer
702 until one is there */
703 len = strlen(kdb_buffer);
704 next_avail = kdb_buffer + len;
705 size_avail = sizeof(kdb_buffer) - len;
710 * The newline is present; print through it or discard
711 * it, depending on the results of the search.
713 cp++; /* to byte after the newline */
714 replaced_byte = *cp; /* remember what/where it was */
716 *cp = '\0'; /* end the string for our search */
719 * We now have a newline at the end of the string
720 * Only continue with this output if it contains the
723 fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
726 * At this point the complete line at the start
727 * of kdb_buffer can be discarded, as it does
728 * not contain what the user is looking for.
729 * Shift the buffer left.
731 *cphold = replaced_byte;
732 strcpy(kdb_buffer, cphold);
733 len = strlen(kdb_buffer);
734 next_avail = kdb_buffer + len;
735 size_avail = sizeof(kdb_buffer) - len;
738 if (kdb_grepping_flag >= KDB_GREPPING_FLAG_SEARCH) {
740 * This was a interactive search (using '/' at more
741 * prompt) and it has completed. Replace the \0 with
742 * its original value to ensure multi-line strings
743 * are handled properly, and return to normal mode.
745 *cphold = replaced_byte;
746 kdb_grepping_flag = 0;
749 * at this point the string is a full line and
750 * should be printed, up to the null.
756 * Write to all consoles.
758 retlen = strlen(kdb_buffer);
759 cp = (char *) printk_skip_headers(kdb_buffer);
760 if (!dbg_kdb_mode && kgdb_connected)
761 gdbstub_msg_write(cp, retlen - (cp - kdb_buffer));
763 kdb_msg_write(cp, retlen - (cp - kdb_buffer));
766 saved_loglevel = console_loglevel;
767 console_loglevel = CONSOLE_LOGLEVEL_SILENT;
768 if (printk_get_level(kdb_buffer) || src == KDB_MSGSRC_PRINTK)
769 printk("%s", kdb_buffer);
771 pr_info("%s", kdb_buffer);
774 if (KDB_STATE(PAGER)) {
776 * Check printed string to decide how to bump the
777 * kdb_nextline to control when the more prompt should
783 if (kdb_buffer[len] == '\n') {
786 } else if (kdb_buffer[len] == '\r') {
792 kdb_nextline += got / (colcount + 1);
795 /* check for having reached the LINES number of printed lines */
796 if (kdb_nextline >= linecount) {
799 /* Watch out for recursion here. Any routine that calls
800 * kdb_printf will come back through here. And kdb_read
801 * uses kdb_printf to echo on serial consoles ...
803 kdb_nextline = 1; /* In case of recursion */
808 moreprompt = kdbgetenv("MOREPROMPT");
809 if (moreprompt == NULL)
810 moreprompt = "more> ";
813 kdb_msg_write(moreprompt, strlen(moreprompt));
816 printk("%s", moreprompt);
819 kdb_nextline = 1; /* Really set output line 1 */
821 /* empty and reset the buffer: */
822 kdb_buffer[0] = '\0';
823 next_avail = kdb_buffer;
824 size_avail = sizeof(kdb_buffer);
825 if ((ch == 'q') || (ch == 'Q')) {
826 /* user hit q or Q */
827 KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
828 KDB_STATE_CLEAR(PAGER);
829 /* end of command output; back to normal mode */
830 kdb_grepping_flag = 0;
832 } else if (ch == ' ') {
834 suspend_grep = 1; /* for this recursion */
835 } else if (ch == '\n' || ch == '\r') {
836 kdb_nextline = linecount - 1;
838 suspend_grep = 1; /* for this recursion */
839 } else if (ch == '/' && !kdb_grepping_flag) {
841 kdb_getstr(kdb_grep_string, KDB_GREP_STRLEN,
842 kdbgetenv("SEARCHPROMPT") ?: "search> ");
843 *strchrnul(kdb_grep_string, '\n') = '\0';
844 kdb_grepping_flag += KDB_GREPPING_FLAG_SEARCH;
845 suspend_grep = 1; /* for this recursion */
847 /* user hit something unexpected */
848 suspend_grep = 1; /* for this recursion */
851 "\nOnly 'q', 'Q' or '/' are processed at "
852 "more prompt, input ignored\n");
854 kdb_printf("\n'/' cannot be used during | "
855 "grep filtering, input ignored\n");
856 } else if (kdb_grepping_flag) {
858 suspend_grep = 1; /* for this recursion */
865 * For grep searches, shift the printed string left.
866 * replaced_byte contains the character that was overwritten with
867 * the terminating null, and cphold points to the null.
868 * Then adjust the notion of available space in the buffer.
870 if (kdb_grepping_flag && !suspend_grep) {
871 *cphold = replaced_byte;
872 strcpy(kdb_buffer, cphold);
873 len = strlen(kdb_buffer);
874 next_avail = kdb_buffer + len;
875 size_avail = sizeof(kdb_buffer) - len;
879 suspend_grep = 0; /* end of what may have been a recursive call */
881 console_loglevel = saved_loglevel;
882 /* kdb_printf_cpu locked the code above. */
883 smp_store_release(&kdb_printf_cpu, old_cpu);
884 local_irq_restore(flags);
888 int kdb_printf(const char *fmt, ...)
894 r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
899 EXPORT_SYMBOL_GPL(kdb_printf);