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/module.h>
13 #include <linux/types.h>
14 #include <linux/ctype.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/kdev_t.h>
18 #include <linux/console.h>
19 #include <linux/string.h>
20 #include <linux/sched.h>
21 #include <linux/smp.h>
22 #include <linux/nmi.h>
23 #include <linux/delay.h>
24 #include <linux/kgdb.h>
25 #include <linux/kdb.h>
26 #include <linux/kallsyms.h>
27 #include "kdb_private.h"
29 #define CMD_BUFLEN 256
30 char kdb_prompt_str[CMD_BUFLEN];
33 static void kgdb_transition_check(char *buffer)
35 int slen = strlen(buffer);
36 if (strncmp(buffer, "$?#3f", slen) != 0 &&
37 strncmp(buffer, "$qSupported#37", slen) != 0 &&
38 strncmp(buffer, "+$qSupported#37", slen) != 0) {
39 KDB_STATE_SET(KGDB_TRANS);
40 kdb_printf("%s", buffer);
44 static int kdb_read_get_key(char *buffer, size_t bufsize)
46 #define ESCAPE_UDELAY 1000
47 #define ESCAPE_DELAY (2*1000000/ESCAPE_UDELAY) /* 2 seconds worth of udelays */
48 char escape_data[5]; /* longest vt100 escape sequence is 4 bytes */
49 char *ped = escape_data;
51 get_char_func *f, *f_escape = NULL;
54 for (f = &kdb_poll_funcs[0]; ; ++f) {
56 /* Reset NMI watchdog once per poll loop */
58 f = &kdb_poll_funcs[0];
60 if (escape_delay == 2) {
65 if (escape_delay == 1) {
74 udelay(ESCAPE_UDELAY);
86 if (escape_delay == 0 && key == '\e') {
87 escape_delay = ESCAPE_DELAY;
97 if (ped - escape_data == 1) {
100 } else if (ped - escape_data == 2) {
105 } else if (ped - escape_data == 3) {
109 case 'A': /* \e[A, up arrow */
112 case 'B': /* \e[B, down arrow */
115 case 'C': /* \e[C, right arrow */
118 case 'D': /* \e[D, left arrow */
121 case '1': /* dropthrough */
122 case '3': /* dropthrough */
123 /* \e[<1,3,4>], may be home, del, end */
130 escape_data[0] = mapkey;
131 escape_data[1] = '\0';
136 } else if (ped - escape_data == 4) {
137 /* \e[<1,3,4><something> */
140 switch (escape_data[2]) {
141 case '1': /* \e[1~, home */
144 case '3': /* \e[3~, del */
147 case '4': /* \e[4~, end */
153 escape_data[0] = mapkey;
154 escape_data[1] = '\0';
160 break; /* A key to process */
168 * This function reads a string of characters, terminated by
169 * a newline, or by reaching the end of the supplied buffer,
170 * from the current kernel debugger console device.
172 * buffer - Address of character buffer to receive input characters.
173 * bufsize - size, in bytes, of the character buffer
175 * Returns a pointer to the buffer containing the received
176 * character string. This string will be terminated by a
179 * No locks are required to be held upon entry to this
180 * function. It is not reentrant - it relies on the fact
181 * that while kdb is running on only one "master debug" cpu.
184 * The buffer size must be >= 2. A buffer size of 2 means that the caller only
185 * wants a single key.
187 * An escape key could be the start of a vt100 control sequence such as \e[D
188 * (left arrow) or it could be a character in its own right. The standard
189 * method for detecting the difference is to wait for 2 seconds to see if there
190 * are any other characters. kdb is complicated by the lack of a timer service
191 * (interrupts are off), by multiple input sources and by the need to sometimes
192 * return after just one key. Escape sequence processing has to be done as
193 * states in the polling loop.
196 static char *kdb_read(char *buffer, size_t bufsize)
199 char *bufend = buffer+bufsize-2; /* Reserve space for newline
204 static char tmpbuffer[CMD_BUFLEN];
205 int len = strlen(buffer);
210 int diag, dtab_count;
214 diag = kdbgetintenv("DTABCOUNT", &dtab_count);
220 if (*(buffer+len-1) == '\n')
226 kdb_printf("%s", buffer);
228 key = kdb_read_get_key(buffer, bufsize);
234 case 8: /* backspace */
237 memcpy(tmpbuffer, cp, lastchar - cp);
238 memcpy(cp-1, tmpbuffer, lastchar - cp);
240 *(--lastchar) = '\0';
242 kdb_printf("\b%s \r", cp);
245 kdb_printf(kdb_prompt_str);
246 kdb_printf("%s", buffer);
257 memcpy(tmpbuffer, cp+1, lastchar - cp - 1);
258 memcpy(cp, tmpbuffer, lastchar - cp - 1);
259 *(--lastchar) = '\0';
260 kdb_printf("%s \r", cp);
263 kdb_printf(kdb_prompt_str);
264 kdb_printf("%s", buffer);
271 kdb_printf(kdb_prompt_str);
277 kdb_printf("%s", cp);
288 memset(tmpbuffer, ' ',
289 strlen(kdb_prompt_str) + (lastchar-buffer));
290 *(tmpbuffer+strlen(kdb_prompt_str) +
291 (lastchar-buffer)) = '\0';
292 kdb_printf("\r%s\r", tmpbuffer);
293 *lastchar = (char)key;
294 *(lastchar+1) = '\0';
298 kdb_printf("%c", *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';
315 while (*p_tmp == ' ')
319 memcpy(tmpbuffer, p_tmp, cp-p_tmp);
320 *(tmpbuffer + (cp-p_tmp)) = '\0';
321 p_tmp = strrchr(tmpbuffer, ' ');
327 count = kallsyms_symbol_complete(p_tmp,
329 (p_tmp - tmpbuffer));
330 if (tab == 2 && count > 0) {
331 kdb_printf("\n%d symbols are found.", count);
332 if (count > dtab_count) {
334 kdb_printf(" But only first %d symbols will"
335 " be printed.\nYou can change the"
336 " environment variable DTABCOUNT.",
340 for (i = 0; i < count; i++) {
341 if (kallsyms_symbol_next(p_tmp, i) < 0)
343 kdb_printf("%s ", p_tmp);
344 *(p_tmp + len) = '\0';
349 kdb_printf(kdb_prompt_str);
350 kdb_printf("%s", buffer);
351 } else if (tab != 2 && count > 0) {
352 len_tmp = strlen(p_tmp);
353 strncpy(p_tmp+len_tmp, cp, lastchar-cp+1);
354 len_tmp = strlen(p_tmp);
355 strncpy(cp, p_tmp+len, len_tmp-len + 1);
357 kdb_printf("%s", cp);
361 kdb_nextline = 1; /* reset output line number */
364 if (key >= 32 && lastchar < bufend) {
366 memcpy(tmpbuffer, cp, lastchar - cp);
367 memcpy(cp+1, tmpbuffer, lastchar - cp);
370 kdb_printf("%s\r", cp);
374 kdb_printf(kdb_prompt_str);
375 kdb_printf("%s", buffer);
380 /* The kgdb transition check will hide
381 * printed characters if we think that
382 * kgdb is connecting, until the check
384 if (!KDB_STATE(KGDB_TRANS))
385 kgdb_transition_check(buffer);
387 kdb_printf("%c", key);
389 /* Special escape to kgdb */
390 if (lastchar - buffer >= 5 &&
391 strcmp(lastchar - 5, "$?#3f") == 0) {
392 strcpy(buffer, "kgdb");
393 KDB_STATE_SET(DOING_KGDB);
396 if (lastchar - buffer >= 14 &&
397 strcmp(lastchar - 14, "$qSupported#37") == 0) {
398 strcpy(buffer, "kgdb");
399 KDB_STATE_SET(DOING_KGDB2);
411 * Print the prompt string and read a command from the
415 * buffer Address of buffer to receive command
416 * bufsize Size of buffer in bytes
417 * prompt Pointer to string to use as prompt string
419 * Pointer to command buffer.
423 * For SMP kernels, the processor number will be
424 * substituted for %d, %x or %o in the prompt.
427 char *kdb_getstr(char *buffer, size_t bufsize, char *prompt)
429 if (prompt && kdb_prompt_str != prompt)
430 strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
431 kdb_printf(kdb_prompt_str);
432 kdb_nextline = 1; /* Prompt and input resets line number */
433 return kdb_read(buffer, bufsize);
439 * Get rid of any buffered console input.
448 * Call this function whenever you want to flush input. If there is any
449 * outstanding input, it ignores all characters until there has been no
450 * data for approximately 1ms.
453 static void kdb_input_flush(void)
458 while (flush_delay) {
461 touch_nmi_watchdog();
462 for (f = &kdb_poll_funcs[0]; *f; ++f) {
477 * Print a string to the output device(s).
480 * printf-like format and optional args.
486 * use 'kdbcons->write()' to avoid polluting 'log_buf' with
489 * If the user is doing a cmd args | grep srch
490 * then kdb_grepping_flag is set.
491 * In that case we need to accumulate full lines (ending in \n) before
492 * searching for the pattern.
495 static char kdb_buffer[256]; /* A bit too big to go on stack */
496 static char *next_avail = kdb_buffer;
497 static int size_avail;
498 static int suspend_grep;
501 * search arg1 to see if it contains arg2
502 * (kdmain.c provides flags for ^pat and pat$)
504 * return 1 for found, 0 for not found
506 static int kdb_search_string(char *searched, char *searchfor)
511 /* not counting the newline at the end of "searched" */
512 len1 = strlen(searched)-1;
513 len2 = strlen(searchfor);
516 if (kdb_grep_leading && kdb_grep_trailing && len1 != len2)
518 if (kdb_grep_leading) {
519 if (!strncmp(searched, searchfor, len2))
521 } else if (kdb_grep_trailing) {
522 if (!strncmp(searched+len1-len2, searchfor, len2))
525 firstchar = *searchfor;
527 while ((cp = strchr(cp, firstchar))) {
528 if (!strncmp(cp, searchfor, len2))
536 int kdb_printf(const char *fmt, ...)
541 int logging, saved_loglevel = 0;
542 int got_printf_lock = 0;
545 char *cp, *cp2, *cphold = NULL, replaced_byte = ' ';
546 char *moreprompt = "more> ";
547 struct console *c = console_drivers;
548 static DEFINE_SPINLOCK(kdb_printf_lock);
549 unsigned long uninitialized_var(flags);
552 /* Serialize kdb_printf if multiple cpus try to write at once.
553 * But if any cpu goes recursive in kdb, just print the output,
554 * even if it is interleaved with any other text.
556 if (!KDB_STATE(PRINTF_LOCK)) {
557 KDB_STATE_SET(PRINTF_LOCK);
558 spin_lock_irqsave(&kdb_printf_lock, flags);
560 atomic_inc(&kdb_event);
562 __acquire(kdb_printf_lock);
565 diag = kdbgetintenv("LINES", &linecount);
566 if (diag || linecount <= 1)
569 diag = kdbgetintenv("LOGGING", &logging);
573 if (!kdb_grepping_flag || suspend_grep) {
574 /* normally, every vsnprintf starts a new buffer */
575 next_avail = kdb_buffer;
576 size_avail = sizeof(kdb_buffer);
579 vsnprintf(next_avail, size_avail, fmt, ap);
583 * If kdb_parse() found that the command was cmd xxx | grep yyy
584 * then kdb_grepping_flag is set, and kdb_grep_string contains yyy
586 * Accumulate the print data up to a newline before searching it.
587 * (vsnprintf does null-terminate the string that it generates)
590 /* skip the search if prints are temporarily unconditional */
591 if (!suspend_grep && kdb_grepping_flag) {
592 cp = strchr(kdb_buffer, '\n');
595 * Special cases that don't end with newlines
596 * but should be written without one:
597 * The "[nn]kdb> " prompt should
598 * appear at the front of the buffer.
600 * The "[nn]more " prompt should also be
601 * (MOREPROMPT -> moreprompt)
602 * written * but we print that ourselves,
603 * we set the suspend_grep flag to make
607 if (next_avail == kdb_buffer) {
609 * these should occur after a newline,
610 * so they will be at the front of the
614 len = strlen(kdb_prompt_str);
615 if (!strncmp(cp2, kdb_prompt_str, len)) {
617 * We're about to start a new
618 * command, so we can go back
621 kdb_grepping_flag = 0;
625 /* no newline; don't search/write the buffer
626 until one is there */
627 len = strlen(kdb_buffer);
628 next_avail = kdb_buffer + len;
629 size_avail = sizeof(kdb_buffer) - len;
634 * The newline is present; print through it or discard
635 * it, depending on the results of the search.
637 cp++; /* to byte after the newline */
638 replaced_byte = *cp; /* remember what/where it was */
640 *cp = '\0'; /* end the string for our search */
643 * We now have a newline at the end of the string
644 * Only continue with this output if it contains the
647 fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
650 * At this point the complete line at the start
651 * of kdb_buffer can be discarded, as it does
652 * not contain what the user is looking for.
653 * Shift the buffer left.
655 *cphold = replaced_byte;
656 strcpy(kdb_buffer, cphold);
657 len = strlen(kdb_buffer);
658 next_avail = kdb_buffer + len;
659 size_avail = sizeof(kdb_buffer) - len;
663 * at this point the string is a full line and
664 * should be printed, up to the null.
670 * Write to all consoles.
672 retlen = strlen(kdb_buffer);
673 if (!dbg_kdb_mode && kgdb_connected) {
674 gdbstub_msg_write(kdb_buffer, retlen);
677 c->write(c, kdb_buffer, retlen);
678 touch_nmi_watchdog();
683 saved_loglevel = console_loglevel;
684 console_loglevel = 0;
685 printk(KERN_INFO "%s", kdb_buffer);
688 if (KDB_STATE(PAGER) && strchr(kdb_buffer, '\n'))
691 /* check for having reached the LINES number of printed lines */
692 if (kdb_nextline == linecount) {
694 #if defined(CONFIG_SMP)
698 /* Watch out for recursion here. Any routine that calls
699 * kdb_printf will come back through here. And kdb_read
700 * uses kdb_printf to echo on serial consoles ...
702 kdb_nextline = 1; /* In case of recursion */
707 moreprompt = kdbgetenv("MOREPROMPT");
708 if (moreprompt == NULL)
709 moreprompt = "more> ";
711 #if defined(CONFIG_SMP)
712 if (strchr(moreprompt, '%')) {
713 sprintf(buf2, moreprompt, get_cpu());
723 c->write(c, moreprompt, strlen(moreprompt));
724 touch_nmi_watchdog();
729 printk("%s", moreprompt);
731 kdb_read(buf1, 2); /* '2' indicates to return
732 * immediately after getting one key. */
733 kdb_nextline = 1; /* Really set output line 1 */
735 /* empty and reset the buffer: */
736 kdb_buffer[0] = '\0';
737 next_avail = kdb_buffer;
738 size_avail = sizeof(kdb_buffer);
739 if ((buf1[0] == 'q') || (buf1[0] == 'Q')) {
740 /* user hit q or Q */
741 KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
742 KDB_STATE_CLEAR(PAGER);
743 /* end of command output; back to normal mode */
744 kdb_grepping_flag = 0;
746 } else if (buf1[0] == ' ') {
748 suspend_grep = 1; /* for this recursion */
749 } else if (buf1[0] == '\n') {
750 kdb_nextline = linecount - 1;
752 suspend_grep = 1; /* for this recursion */
753 } else if (buf1[0] && buf1[0] != '\n') {
754 /* user hit something other than enter */
755 suspend_grep = 1; /* for this recursion */
756 kdb_printf("\nOnly 'q' or 'Q' are processed at more "
757 "prompt, input ignored\n");
758 } else if (kdb_grepping_flag) {
760 suspend_grep = 1; /* for this recursion */
767 * For grep searches, shift the printed string left.
768 * replaced_byte contains the character that was overwritten with
769 * the terminating null, and cphold points to the null.
770 * Then adjust the notion of available space in the buffer.
772 if (kdb_grepping_flag && !suspend_grep) {
773 *cphold = replaced_byte;
774 strcpy(kdb_buffer, cphold);
775 len = strlen(kdb_buffer);
776 next_avail = kdb_buffer + len;
777 size_avail = sizeof(kdb_buffer) - len;
781 suspend_grep = 0; /* end of what may have been a recursive call */
783 console_loglevel = saved_loglevel;
784 if (KDB_STATE(PRINTF_LOCK) && got_printf_lock) {
786 spin_unlock_irqrestore(&kdb_printf_lock, flags);
787 KDB_STATE_CLEAR(PRINTF_LOCK);
788 atomic_dec(&kdb_event);
790 __release(kdb_printf_lock);