kgdboc,kdb: Allow kdb to work on a non open console port
[linux-3.10.git] / kernel / debug / kdb / kdb_io.c
1 /*
2  * Kernel Debugger Architecture Independent Console I/O handler
3  *
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
6  * for more details.
7  *
8  * Copyright (c) 1999-2006 Silicon Graphics, Inc.  All Rights Reserved.
9  * Copyright (c) 2009 Wind River Systems, Inc.  All Rights Reserved.
10  */
11
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"
28
29 #define CMD_BUFLEN 256
30 char kdb_prompt_str[CMD_BUFLEN];
31
32
33 static void kgdb_transition_check(char *buffer)
34 {
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);
41         }
42 }
43
44 static int kdb_read_get_key(char *buffer, size_t bufsize)
45 {
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;
50         int escape_delay = 0;
51         get_char_func *f, *f_escape = NULL;
52         int key;
53
54         for (f = &kdb_poll_funcs[0]; ; ++f) {
55                 if (*f == NULL) {
56                         /* Reset NMI watchdog once per poll loop */
57                         touch_nmi_watchdog();
58                         f = &kdb_poll_funcs[0];
59                 }
60                 if (escape_delay == 2) {
61                         *ped = '\0';
62                         ped = escape_data;
63                         --escape_delay;
64                 }
65                 if (escape_delay == 1) {
66                         key = *ped++;
67                         if (!*ped)
68                                 --escape_delay;
69                         break;
70                 }
71                 key = (*f)();
72                 if (key == -1) {
73                         if (escape_delay) {
74                                 udelay(ESCAPE_UDELAY);
75                                 --escape_delay;
76                         }
77                         continue;
78                 }
79                 if (bufsize <= 2) {
80                         if (key == '\r')
81                                 key = '\n';
82                         *buffer++ = key;
83                         *buffer = '\0';
84                         return -1;
85                 }
86                 if (escape_delay == 0 && key == '\e') {
87                         escape_delay = ESCAPE_DELAY;
88                         ped = escape_data;
89                         f_escape = f;
90                 }
91                 if (escape_delay) {
92                         *ped++ = key;
93                         if (f_escape != f) {
94                                 escape_delay = 2;
95                                 continue;
96                         }
97                         if (ped - escape_data == 1) {
98                                 /* \e */
99                                 continue;
100                         } else if (ped - escape_data == 2) {
101                                 /* \e<something> */
102                                 if (key != '[')
103                                         escape_delay = 2;
104                                 continue;
105                         } else if (ped - escape_data == 3) {
106                                 /* \e[<something> */
107                                 int mapkey = 0;
108                                 switch (key) {
109                                 case 'A': /* \e[A, up arrow */
110                                         mapkey = 16;
111                                         break;
112                                 case 'B': /* \e[B, down arrow */
113                                         mapkey = 14;
114                                         break;
115                                 case 'C': /* \e[C, right arrow */
116                                         mapkey = 6;
117                                         break;
118                                 case 'D': /* \e[D, left arrow */
119                                         mapkey = 2;
120                                         break;
121                                 case '1': /* dropthrough */
122                                 case '3': /* dropthrough */
123                                 /* \e[<1,3,4>], may be home, del, end */
124                                 case '4':
125                                         mapkey = -1;
126                                         break;
127                                 }
128                                 if (mapkey != -1) {
129                                         if (mapkey > 0) {
130                                                 escape_data[0] = mapkey;
131                                                 escape_data[1] = '\0';
132                                         }
133                                         escape_delay = 2;
134                                 }
135                                 continue;
136                         } else if (ped - escape_data == 4) {
137                                 /* \e[<1,3,4><something> */
138                                 int mapkey = 0;
139                                 if (key == '~') {
140                                         switch (escape_data[2]) {
141                                         case '1': /* \e[1~, home */
142                                                 mapkey = 1;
143                                                 break;
144                                         case '3': /* \e[3~, del */
145                                                 mapkey = 4;
146                                                 break;
147                                         case '4': /* \e[4~, end */
148                                                 mapkey = 5;
149                                                 break;
150                                         }
151                                 }
152                                 if (mapkey > 0) {
153                                         escape_data[0] = mapkey;
154                                         escape_data[1] = '\0';
155                                 }
156                                 escape_delay = 2;
157                                 continue;
158                         }
159                 }
160                 break;  /* A key to process */
161         }
162         return key;
163 }
164
165 /*
166  * kdb_read
167  *
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.
171  * Parameters:
172  *      buffer  - Address of character buffer to receive input characters.
173  *      bufsize - size, in bytes, of the character buffer
174  * Returns:
175  *      Returns a pointer to the buffer containing the received
176  *      character string.  This string will be terminated by a
177  *      newline character.
178  * Locking:
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.
182  * Remarks:
183  *
184  * The buffer size must be >= 2.  A buffer size of 2 means that the caller only
185  * wants a single key.
186  *
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.
194  */
195
196 static char *kdb_read(char *buffer, size_t bufsize)
197 {
198         char *cp = buffer;
199         char *bufend = buffer+bufsize-2;        /* Reserve space for newline
200                                                  * and null byte */
201         char *lastchar;
202         char *p_tmp;
203         char tmp;
204         static char tmpbuffer[CMD_BUFLEN];
205         int len = strlen(buffer);
206         int len_tmp;
207         int tab = 0;
208         int count;
209         int i;
210         int diag, dtab_count;
211         int key;
212
213
214         diag = kdbgetintenv("DTABCOUNT", &dtab_count);
215         if (diag)
216                 dtab_count = 30;
217
218         if (len > 0) {
219                 cp += len;
220                 if (*(buffer+len-1) == '\n')
221                         cp--;
222         }
223
224         lastchar = cp;
225         *cp = '\0';
226         kdb_printf("%s", buffer);
227 poll_again:
228         key = kdb_read_get_key(buffer, bufsize);
229         if (key == -1)
230                 return buffer;
231         if (key != 9)
232                 tab = 0;
233         switch (key) {
234         case 8: /* backspace */
235                 if (cp > buffer) {
236                         if (cp < lastchar) {
237                                 memcpy(tmpbuffer, cp, lastchar - cp);
238                                 memcpy(cp-1, tmpbuffer, lastchar - cp);
239                         }
240                         *(--lastchar) = '\0';
241                         --cp;
242                         kdb_printf("\b%s \r", cp);
243                         tmp = *cp;
244                         *cp = '\0';
245                         kdb_printf(kdb_prompt_str);
246                         kdb_printf("%s", buffer);
247                         *cp = tmp;
248                 }
249                 break;
250         case 13: /* enter */
251                 *lastchar++ = '\n';
252                 *lastchar++ = '\0';
253                 kdb_printf("\n");
254                 return buffer;
255         case 4: /* Del */
256                 if (cp < lastchar) {
257                         memcpy(tmpbuffer, cp+1, lastchar - cp - 1);
258                         memcpy(cp, tmpbuffer, lastchar - cp - 1);
259                         *(--lastchar) = '\0';
260                         kdb_printf("%s \r", cp);
261                         tmp = *cp;
262                         *cp = '\0';
263                         kdb_printf(kdb_prompt_str);
264                         kdb_printf("%s", buffer);
265                         *cp = tmp;
266                 }
267                 break;
268         case 1: /* Home */
269                 if (cp > buffer) {
270                         kdb_printf("\r");
271                         kdb_printf(kdb_prompt_str);
272                         cp = buffer;
273                 }
274                 break;
275         case 5: /* End */
276                 if (cp < lastchar) {
277                         kdb_printf("%s", cp);
278                         cp = lastchar;
279                 }
280                 break;
281         case 2: /* Left */
282                 if (cp > buffer) {
283                         kdb_printf("\b");
284                         --cp;
285                 }
286                 break;
287         case 14: /* Down */
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';
295                 return lastchar;
296         case 6: /* Right */
297                 if (cp < lastchar) {
298                         kdb_printf("%c", *cp);
299                         ++cp;
300                 }
301                 break;
302         case 16: /* Up */
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';
310                 return lastchar;
311         case 9: /* Tab */
312                 if (tab < 2)
313                         ++tab;
314                 p_tmp = buffer;
315                 while (*p_tmp == ' ')
316                         p_tmp++;
317                 if (p_tmp > cp)
318                         break;
319                 memcpy(tmpbuffer, p_tmp, cp-p_tmp);
320                 *(tmpbuffer + (cp-p_tmp)) = '\0';
321                 p_tmp = strrchr(tmpbuffer, ' ');
322                 if (p_tmp)
323                         ++p_tmp;
324                 else
325                         p_tmp = tmpbuffer;
326                 len = strlen(p_tmp);
327                 count = kallsyms_symbol_complete(p_tmp,
328                                                  sizeof(tmpbuffer) -
329                                                  (p_tmp - tmpbuffer));
330                 if (tab == 2 && count > 0) {
331                         kdb_printf("\n%d symbols are found.", count);
332                         if (count > dtab_count) {
333                                 count = dtab_count;
334                                 kdb_printf(" But only first %d symbols will"
335                                            " be printed.\nYou can change the"
336                                            " environment variable DTABCOUNT.",
337                                            count);
338                         }
339                         kdb_printf("\n");
340                         for (i = 0; i < count; i++) {
341                                 if (kallsyms_symbol_next(p_tmp, i) < 0)
342                                         break;
343                                 kdb_printf("%s ", p_tmp);
344                                 *(p_tmp + len) = '\0';
345                         }
346                         if (i >= dtab_count)
347                                 kdb_printf("...");
348                         kdb_printf("\n");
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);
356                         len = len_tmp - len;
357                         kdb_printf("%s", cp);
358                         cp += len;
359                         lastchar += len;
360                 }
361                 kdb_nextline = 1; /* reset output line number */
362                 break;
363         default:
364                 if (key >= 32 && lastchar < bufend) {
365                         if (cp < lastchar) {
366                                 memcpy(tmpbuffer, cp, lastchar - cp);
367                                 memcpy(cp+1, tmpbuffer, lastchar - cp);
368                                 *++lastchar = '\0';
369                                 *cp = key;
370                                 kdb_printf("%s\r", cp);
371                                 ++cp;
372                                 tmp = *cp;
373                                 *cp = '\0';
374                                 kdb_printf(kdb_prompt_str);
375                                 kdb_printf("%s", buffer);
376                                 *cp = tmp;
377                         } else {
378                                 *++lastchar = '\0';
379                                 *cp++ = key;
380                                 /* The kgdb transition check will hide
381                                  * printed characters if we think that
382                                  * kgdb is connecting, until the check
383                                  * fails */
384                                 if (!KDB_STATE(KGDB_TRANS))
385                                         kgdb_transition_check(buffer);
386                                 else
387                                         kdb_printf("%c", key);
388                         }
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);
394                                 return buffer;
395                         }
396                         if (lastchar - buffer >= 14 &&
397                             strcmp(lastchar - 14, "$qSupported#37") == 0) {
398                                 strcpy(buffer, "kgdb");
399                                 KDB_STATE_SET(DOING_KGDB2);
400                                 return buffer;
401                         }
402                 }
403                 break;
404         }
405         goto poll_again;
406 }
407
408 /*
409  * kdb_getstr
410  *
411  *      Print the prompt string and read a command from the
412  *      input device.
413  *
414  * Parameters:
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
418  * Returns:
419  *      Pointer to command buffer.
420  * Locking:
421  *      None.
422  * Remarks:
423  *      For SMP kernels, the processor number will be
424  *      substituted for %d, %x or %o in the prompt.
425  */
426
427 char *kdb_getstr(char *buffer, size_t bufsize, char *prompt)
428 {
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);
434 }
435
436 /*
437  * kdb_input_flush
438  *
439  *      Get rid of any buffered console input.
440  *
441  * Parameters:
442  *      none
443  * Returns:
444  *      nothing
445  * Locking:
446  *      none
447  * Remarks:
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.
451  */
452
453 static void kdb_input_flush(void)
454 {
455         get_char_func *f;
456         int res;
457         int flush_delay = 1;
458         while (flush_delay) {
459                 flush_delay--;
460 empty:
461                 touch_nmi_watchdog();
462                 for (f = &kdb_poll_funcs[0]; *f; ++f) {
463                         res = (*f)();
464                         if (res != -1) {
465                                 flush_delay = 1;
466                                 goto empty;
467                         }
468                 }
469                 if (flush_delay)
470                         mdelay(1);
471         }
472 }
473
474 /*
475  * kdb_printf
476  *
477  *      Print a string to the output device(s).
478  *
479  * Parameters:
480  *      printf-like format and optional args.
481  * Returns:
482  *      0
483  * Locking:
484  *      None.
485  * Remarks:
486  *      use 'kdbcons->write()' to avoid polluting 'log_buf' with
487  *      kdb output.
488  *
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.
493  */
494
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;
499
500 /*
501  * search arg1 to see if it contains arg2
502  * (kdmain.c provides flags for ^pat and pat$)
503  *
504  * return 1 for found, 0 for not found
505  */
506 static int kdb_search_string(char *searched, char *searchfor)
507 {
508         char firstchar, *cp;
509         int len1, len2;
510
511         /* not counting the newline at the end of "searched" */
512         len1 = strlen(searched)-1;
513         len2 = strlen(searchfor);
514         if (len1 < len2)
515                 return 0;
516         if (kdb_grep_leading && kdb_grep_trailing && len1 != len2)
517                 return 0;
518         if (kdb_grep_leading) {
519                 if (!strncmp(searched, searchfor, len2))
520                         return 1;
521         } else if (kdb_grep_trailing) {
522                 if (!strncmp(searched+len1-len2, searchfor, len2))
523                         return 1;
524         } else {
525                 firstchar = *searchfor;
526                 cp = searched;
527                 while ((cp = strchr(cp, firstchar))) {
528                         if (!strncmp(cp, searchfor, len2))
529                                 return 1;
530                         cp++;
531                 }
532         }
533         return 0;
534 }
535
536 int kdb_printf(const char *fmt, ...)
537 {
538         va_list ap;
539         int diag;
540         int linecount;
541         int logging, saved_loglevel = 0;
542         int got_printf_lock = 0;
543         int retlen = 0;
544         int fnd, len;
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);
550
551         preempt_disable();
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.
555          */
556         if (!KDB_STATE(PRINTF_LOCK)) {
557                 KDB_STATE_SET(PRINTF_LOCK);
558                 spin_lock_irqsave(&kdb_printf_lock, flags);
559                 got_printf_lock = 1;
560                 atomic_inc(&kdb_event);
561         } else {
562                 __acquire(kdb_printf_lock);
563         }
564
565         diag = kdbgetintenv("LINES", &linecount);
566         if (diag || linecount <= 1)
567                 linecount = 24;
568
569         diag = kdbgetintenv("LOGGING", &logging);
570         if (diag)
571                 logging = 0;
572
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);
577         }
578         va_start(ap, fmt);
579         vsnprintf(next_avail, size_avail, fmt, ap);
580         va_end(ap);
581
582         /*
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
585          *
586          * Accumulate the print data up to a newline before searching it.
587          * (vsnprintf does null-terminate the string that it generates)
588          */
589
590         /* skip the search if prints are temporarily unconditional */
591         if (!suspend_grep && kdb_grepping_flag) {
592                 cp = strchr(kdb_buffer, '\n');
593                 if (!cp) {
594                         /*
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.
599                          *
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
604                          *   it unconditional.
605                          *
606                          */
607                         if (next_avail == kdb_buffer) {
608                                 /*
609                                  * these should occur after a newline,
610                                  * so they will be at the front of the
611                                  * buffer
612                                  */
613                                 cp2 = kdb_buffer;
614                                 len = strlen(kdb_prompt_str);
615                                 if (!strncmp(cp2, kdb_prompt_str, len)) {
616                                         /*
617                                          * We're about to start a new
618                                          * command, so we can go back
619                                          * to normal mode.
620                                          */
621                                         kdb_grepping_flag = 0;
622                                         goto kdb_printit;
623                                 }
624                         }
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;
630                         goto kdb_print_out;
631                 }
632
633                 /*
634                  * The newline is present; print through it or discard
635                  * it, depending on the results of the search.
636                  */
637                 cp++;                /* to byte after the newline */
638                 replaced_byte = *cp; /* remember what/where it was */
639                 cphold = cp;
640                 *cp = '\0';          /* end the string for our search */
641
642                 /*
643                  * We now have a newline at the end of the string
644                  * Only continue with this output if it contains the
645                  * search string.
646                  */
647                 fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
648                 if (!fnd) {
649                         /*
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.
654                          */
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;
660                         goto kdb_print_out;
661                 }
662                 /*
663                  * at this point the string is a full line and
664                  * should be printed, up to the null.
665                  */
666         }
667 kdb_printit:
668
669         /*
670          * Write to all consoles.
671          */
672         retlen = strlen(kdb_buffer);
673         if (!dbg_kdb_mode && kgdb_connected) {
674                 gdbstub_msg_write(kdb_buffer, retlen);
675         } else {
676                 if (!dbg_io_ops->is_console) {
677                         len = strlen(kdb_buffer);
678                         cp = kdb_buffer;
679                         while (len--) {
680                                 dbg_io_ops->write_char(*cp);
681                                 cp++;
682                         }
683                 }
684                 while (c) {
685                         c->write(c, kdb_buffer, retlen);
686                         touch_nmi_watchdog();
687                         c = c->next;
688                 }
689         }
690         if (logging) {
691                 saved_loglevel = console_loglevel;
692                 console_loglevel = 0;
693                 printk(KERN_INFO "%s", kdb_buffer);
694         }
695
696         if (KDB_STATE(PAGER) && strchr(kdb_buffer, '\n'))
697                 kdb_nextline++;
698
699         /* check for having reached the LINES number of printed lines */
700         if (kdb_nextline == linecount) {
701                 char buf1[16] = "";
702 #if defined(CONFIG_SMP)
703                 char buf2[32];
704 #endif
705
706                 /* Watch out for recursion here.  Any routine that calls
707                  * kdb_printf will come back through here.  And kdb_read
708                  * uses kdb_printf to echo on serial consoles ...
709                  */
710                 kdb_nextline = 1;       /* In case of recursion */
711
712                 /*
713                  * Pause until cr.
714                  */
715                 moreprompt = kdbgetenv("MOREPROMPT");
716                 if (moreprompt == NULL)
717                         moreprompt = "more> ";
718
719 #if defined(CONFIG_SMP)
720                 if (strchr(moreprompt, '%')) {
721                         sprintf(buf2, moreprompt, get_cpu());
722                         put_cpu();
723                         moreprompt = buf2;
724                 }
725 #endif
726
727                 kdb_input_flush();
728                 c = console_drivers;
729
730                 if (!dbg_io_ops->is_console) {
731                         len = strlen(moreprompt);
732                         cp = moreprompt;
733                         while (len--) {
734                                 dbg_io_ops->write_char(*cp);
735                                 cp++;
736                         }
737                 }
738                 while (c) {
739                         c->write(c, moreprompt, strlen(moreprompt));
740                         touch_nmi_watchdog();
741                         c = c->next;
742                 }
743
744                 if (logging)
745                         printk("%s", moreprompt);
746
747                 kdb_read(buf1, 2); /* '2' indicates to return
748                                     * immediately after getting one key. */
749                 kdb_nextline = 1;       /* Really set output line 1 */
750
751                 /* empty and reset the buffer: */
752                 kdb_buffer[0] = '\0';
753                 next_avail = kdb_buffer;
754                 size_avail = sizeof(kdb_buffer);
755                 if ((buf1[0] == 'q') || (buf1[0] == 'Q')) {
756                         /* user hit q or Q */
757                         KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
758                         KDB_STATE_CLEAR(PAGER);
759                         /* end of command output; back to normal mode */
760                         kdb_grepping_flag = 0;
761                         kdb_printf("\n");
762                 } else if (buf1[0] == ' ') {
763                         kdb_printf("\n");
764                         suspend_grep = 1; /* for this recursion */
765                 } else if (buf1[0] == '\n') {
766                         kdb_nextline = linecount - 1;
767                         kdb_printf("\r");
768                         suspend_grep = 1; /* for this recursion */
769                 } else if (buf1[0] && buf1[0] != '\n') {
770                         /* user hit something other than enter */
771                         suspend_grep = 1; /* for this recursion */
772                         kdb_printf("\nOnly 'q' or 'Q' are processed at more "
773                                    "prompt, input ignored\n");
774                 } else if (kdb_grepping_flag) {
775                         /* user hit enter */
776                         suspend_grep = 1; /* for this recursion */
777                         kdb_printf("\n");
778                 }
779                 kdb_input_flush();
780         }
781
782         /*
783          * For grep searches, shift the printed string left.
784          *  replaced_byte contains the character that was overwritten with
785          *  the terminating null, and cphold points to the null.
786          * Then adjust the notion of available space in the buffer.
787          */
788         if (kdb_grepping_flag && !suspend_grep) {
789                 *cphold = replaced_byte;
790                 strcpy(kdb_buffer, cphold);
791                 len = strlen(kdb_buffer);
792                 next_avail = kdb_buffer + len;
793                 size_avail = sizeof(kdb_buffer) - len;
794         }
795
796 kdb_print_out:
797         suspend_grep = 0; /* end of what may have been a recursive call */
798         if (logging)
799                 console_loglevel = saved_loglevel;
800         if (KDB_STATE(PRINTF_LOCK) && got_printf_lock) {
801                 got_printf_lock = 0;
802                 spin_unlock_irqrestore(&kdb_printf_lock, flags);
803                 KDB_STATE_CLEAR(PRINTF_LOCK);
804                 atomic_dec(&kdb_event);
805         } else {
806                 __release(kdb_printf_lock);
807         }
808         preempt_enable();
809         return retlen;
810 }