9e3cec7a925c7bfced9ace00b6f90ed551df6d3b
[safe/jmp/linux-2.6] / 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/kdb.h>
25 #include <linux/kallsyms.h>
26 #include "kdb_private.h"
27
28 #define CMD_BUFLEN 256
29 char kdb_prompt_str[CMD_BUFLEN];
30
31
32 static void kgdb_transition_check(char *buffer)
33 {
34         int slen = strlen(buffer);
35         if (strncmp(buffer, "$?#3f", slen) != 0 &&
36             strncmp(buffer, "$qSupported#37", slen) != 0 &&
37             strncmp(buffer, "+$qSupported#37", slen) != 0) {
38                 KDB_STATE_SET(KGDB_TRANS);
39                 kdb_printf("%s", buffer);
40         }
41 }
42
43 static int kdb_read_get_key(char *buffer, size_t bufsize)
44 {
45 #define ESCAPE_UDELAY 1000
46 #define ESCAPE_DELAY (2*1000000/ESCAPE_UDELAY) /* 2 seconds worth of udelays */
47         char escape_data[5];    /* longest vt100 escape sequence is 4 bytes */
48         char *ped = escape_data;
49         int escape_delay = 0;
50         get_char_func *f, *f_escape = NULL;
51         int key;
52
53         for (f = &kdb_poll_funcs[0]; ; ++f) {
54                 if (*f == NULL) {
55                         /* Reset NMI watchdog once per poll loop */
56                         touch_nmi_watchdog();
57                         f = &kdb_poll_funcs[0];
58                 }
59                 if (escape_delay == 2) {
60                         *ped = '\0';
61                         ped = escape_data;
62                         --escape_delay;
63                 }
64                 if (escape_delay == 1) {
65                         key = *ped++;
66                         if (!*ped)
67                                 --escape_delay;
68                         break;
69                 }
70                 key = (*f)();
71                 if (key == -1) {
72                         if (escape_delay) {
73                                 udelay(ESCAPE_UDELAY);
74                                 --escape_delay;
75                         }
76                         continue;
77                 }
78                 if (bufsize <= 2) {
79                         if (key == '\r')
80                                 key = '\n';
81                         *buffer++ = key;
82                         *buffer = '\0';
83                         return -1;
84                 }
85                 if (escape_delay == 0 && key == '\e') {
86                         escape_delay = ESCAPE_DELAY;
87                         ped = escape_data;
88                         f_escape = f;
89                 }
90                 if (escape_delay) {
91                         *ped++ = key;
92                         if (f_escape != f) {
93                                 escape_delay = 2;
94                                 continue;
95                         }
96                         if (ped - escape_data == 1) {
97                                 /* \e */
98                                 continue;
99                         } else if (ped - escape_data == 2) {
100                                 /* \e<something> */
101                                 if (key != '[')
102                                         escape_delay = 2;
103                                 continue;
104                         } else if (ped - escape_data == 3) {
105                                 /* \e[<something> */
106                                 int mapkey = 0;
107                                 switch (key) {
108                                 case 'A': /* \e[A, up arrow */
109                                         mapkey = 16;
110                                         break;
111                                 case 'B': /* \e[B, down arrow */
112                                         mapkey = 14;
113                                         break;
114                                 case 'C': /* \e[C, right arrow */
115                                         mapkey = 6;
116                                         break;
117                                 case 'D': /* \e[D, left arrow */
118                                         mapkey = 2;
119                                         break;
120                                 case '1': /* dropthrough */
121                                 case '3': /* dropthrough */
122                                 /* \e[<1,3,4>], may be home, del, end */
123                                 case '4':
124                                         mapkey = -1;
125                                         break;
126                                 }
127                                 if (mapkey != -1) {
128                                         if (mapkey > 0) {
129                                                 escape_data[0] = mapkey;
130                                                 escape_data[1] = '\0';
131                                         }
132                                         escape_delay = 2;
133                                 }
134                                 continue;
135                         } else if (ped - escape_data == 4) {
136                                 /* \e[<1,3,4><something> */
137                                 int mapkey = 0;
138                                 if (key == '~') {
139                                         switch (escape_data[2]) {
140                                         case '1': /* \e[1~, home */
141                                                 mapkey = 1;
142                                                 break;
143                                         case '3': /* \e[3~, del */
144                                                 mapkey = 4;
145                                                 break;
146                                         case '4': /* \e[4~, end */
147                                                 mapkey = 5;
148                                                 break;
149                                         }
150                                 }
151                                 if (mapkey > 0) {
152                                         escape_data[0] = mapkey;
153                                         escape_data[1] = '\0';
154                                 }
155                                 escape_delay = 2;
156                                 continue;
157                         }
158                 }
159                 break;  /* A key to process */
160         }
161         return key;
162 }
163
164 /*
165  * kdb_read
166  *
167  *      This function reads a string of characters, terminated by
168  *      a newline, or by reaching the end of the supplied buffer,
169  *      from the current kernel debugger console device.
170  * Parameters:
171  *      buffer  - Address of character buffer to receive input characters.
172  *      bufsize - size, in bytes, of the character buffer
173  * Returns:
174  *      Returns a pointer to the buffer containing the received
175  *      character string.  This string will be terminated by a
176  *      newline character.
177  * Locking:
178  *      No locks are required to be held upon entry to this
179  *      function.  It is not reentrant - it relies on the fact
180  *      that while kdb is running on only one "master debug" cpu.
181  * Remarks:
182  *
183  * The buffer size must be >= 2.  A buffer size of 2 means that the caller only
184  * wants a single key.
185  *
186  * An escape key could be the start of a vt100 control sequence such as \e[D
187  * (left arrow) or it could be a character in its own right.  The standard
188  * method for detecting the difference is to wait for 2 seconds to see if there
189  * are any other characters.  kdb is complicated by the lack of a timer service
190  * (interrupts are off), by multiple input sources and by the need to sometimes
191  * return after just one key.  Escape sequence processing has to be done as
192  * states in the polling loop.
193  */
194
195 static char *kdb_read(char *buffer, size_t bufsize)
196 {
197         char *cp = buffer;
198         char *bufend = buffer+bufsize-2;        /* Reserve space for newline
199                                                  * and null byte */
200         char *lastchar;
201         char *p_tmp;
202         char tmp;
203         static char tmpbuffer[CMD_BUFLEN];
204         int len = strlen(buffer);
205         int len_tmp;
206         int tab = 0;
207         int count;
208         int i;
209         int diag, dtab_count;
210         int key;
211
212
213         diag = kdbgetintenv("DTABCOUNT", &dtab_count);
214         if (diag)
215                 dtab_count = 30;
216
217         if (len > 0) {
218                 cp += len;
219                 if (*(buffer+len-1) == '\n')
220                         cp--;
221         }
222
223         lastchar = cp;
224         *cp = '\0';
225         kdb_printf("%s", buffer);
226 poll_again:
227         key = kdb_read_get_key(buffer, bufsize);
228         if (key == -1)
229                 return buffer;
230         if (key != 9)
231                 tab = 0;
232         switch (key) {
233         case 8: /* backspace */
234                 if (cp > buffer) {
235                         if (cp < lastchar) {
236                                 memcpy(tmpbuffer, cp, lastchar - cp);
237                                 memcpy(cp-1, tmpbuffer, lastchar - cp);
238                         }
239                         *(--lastchar) = '\0';
240                         --cp;
241                         kdb_printf("\b%s \r", cp);
242                         tmp = *cp;
243                         *cp = '\0';
244                         kdb_printf(kdb_prompt_str);
245                         kdb_printf("%s", buffer);
246                         *cp = tmp;
247                 }
248                 break;
249         case 13: /* enter */
250                 *lastchar++ = '\n';
251                 *lastchar++ = '\0';
252                 kdb_printf("\n");
253                 return buffer;
254         case 4: /* Del */
255                 if (cp < lastchar) {
256                         memcpy(tmpbuffer, cp+1, lastchar - cp - 1);
257                         memcpy(cp, tmpbuffer, lastchar - cp - 1);
258                         *(--lastchar) = '\0';
259                         kdb_printf("%s \r", cp);
260                         tmp = *cp;
261                         *cp = '\0';
262                         kdb_printf(kdb_prompt_str);
263                         kdb_printf("%s", buffer);
264                         *cp = tmp;
265                 }
266                 break;
267         case 1: /* Home */
268                 if (cp > buffer) {
269                         kdb_printf("\r");
270                         kdb_printf(kdb_prompt_str);
271                         cp = buffer;
272                 }
273                 break;
274         case 5: /* End */
275                 if (cp < lastchar) {
276                         kdb_printf("%s", cp);
277                         cp = lastchar;
278                 }
279                 break;
280         case 2: /* Left */
281                 if (cp > buffer) {
282                         kdb_printf("\b");
283                         --cp;
284                 }
285                 break;
286         case 14: /* Down */
287                 memset(tmpbuffer, ' ',
288                        strlen(kdb_prompt_str) + (lastchar-buffer));
289                 *(tmpbuffer+strlen(kdb_prompt_str) +
290                   (lastchar-buffer)) = '\0';
291                 kdb_printf("\r%s\r", tmpbuffer);
292                 *lastchar = (char)key;
293                 *(lastchar+1) = '\0';
294                 return lastchar;
295         case 6: /* Right */
296                 if (cp < lastchar) {
297                         kdb_printf("%c", *cp);
298                         ++cp;
299                 }
300                 break;
301         case 16: /* Up */
302                 memset(tmpbuffer, ' ',
303                        strlen(kdb_prompt_str) + (lastchar-buffer));
304                 *(tmpbuffer+strlen(kdb_prompt_str) +
305                   (lastchar-buffer)) = '\0';
306                 kdb_printf("\r%s\r", tmpbuffer);
307                 *lastchar = (char)key;
308                 *(lastchar+1) = '\0';
309                 return lastchar;
310         case 9: /* Tab */
311                 if (tab < 2)
312                         ++tab;
313                 p_tmp = buffer;
314                 while (*p_tmp == ' ')
315                         p_tmp++;
316                 if (p_tmp > cp)
317                         break;
318                 memcpy(tmpbuffer, p_tmp, cp-p_tmp);
319                 *(tmpbuffer + (cp-p_tmp)) = '\0';
320                 p_tmp = strrchr(tmpbuffer, ' ');
321                 if (p_tmp)
322                         ++p_tmp;
323                 else
324                         p_tmp = tmpbuffer;
325                 len = strlen(p_tmp);
326                 count = kallsyms_symbol_complete(p_tmp,
327                                                  sizeof(tmpbuffer) -
328                                                  (p_tmp - tmpbuffer));
329                 if (tab == 2 && count > 0) {
330                         kdb_printf("\n%d symbols are found.", count);
331                         if (count > dtab_count) {
332                                 count = dtab_count;
333                                 kdb_printf(" But only first %d symbols will"
334                                            " be printed.\nYou can change the"
335                                            " environment variable DTABCOUNT.",
336                                            count);
337                         }
338                         kdb_printf("\n");
339                         for (i = 0; i < count; i++) {
340                                 if (kallsyms_symbol_next(p_tmp, i) < 0)
341                                         break;
342                                 kdb_printf("%s ", p_tmp);
343                                 *(p_tmp + len) = '\0';
344                         }
345                         if (i >= dtab_count)
346                                 kdb_printf("...");
347                         kdb_printf("\n");
348                         kdb_printf(kdb_prompt_str);
349                         kdb_printf("%s", buffer);
350                 } else if (tab != 2 && count > 0) {
351                         len_tmp = strlen(p_tmp);
352                         strncpy(p_tmp+len_tmp, cp, lastchar-cp+1);
353                         len_tmp = strlen(p_tmp);
354                         strncpy(cp, p_tmp+len, len_tmp-len + 1);
355                         len = len_tmp - len;
356                         kdb_printf("%s", cp);
357                         cp += len;
358                         lastchar += len;
359                 }
360                 kdb_nextline = 1; /* reset output line number */
361                 break;
362         default:
363                 if (key >= 32 && lastchar < bufend) {
364                         if (cp < lastchar) {
365                                 memcpy(tmpbuffer, cp, lastchar - cp);
366                                 memcpy(cp+1, tmpbuffer, lastchar - cp);
367                                 *++lastchar = '\0';
368                                 *cp = key;
369                                 kdb_printf("%s\r", cp);
370                                 ++cp;
371                                 tmp = *cp;
372                                 *cp = '\0';
373                                 kdb_printf(kdb_prompt_str);
374                                 kdb_printf("%s", buffer);
375                                 *cp = tmp;
376                         } else {
377                                 *++lastchar = '\0';
378                                 *cp++ = key;
379                                 /* The kgdb transition check will hide
380                                  * printed characters if we think that
381                                  * kgdb is connecting, until the check
382                                  * fails */
383                                 if (!KDB_STATE(KGDB_TRANS))
384                                         kgdb_transition_check(buffer);
385                                 else
386                                         kdb_printf("%c", key);
387                         }
388                         /* Special escape to kgdb */
389                         if (lastchar - buffer >= 5 &&
390                             strcmp(lastchar - 5, "$?#3f") == 0) {
391                                 strcpy(buffer, "kgdb");
392                                 KDB_STATE_SET(DOING_KGDB);
393                                 return buffer;
394                         }
395                         if (lastchar - buffer >= 14 &&
396                             strcmp(lastchar - 14, "$qSupported#37") == 0) {
397                                 strcpy(buffer, "kgdb");
398                                 KDB_STATE_SET(DOING_KGDB2);
399                                 return buffer;
400                         }
401                 }
402                 break;
403         }
404         goto poll_again;
405 }
406
407 /*
408  * kdb_getstr
409  *
410  *      Print the prompt string and read a command from the
411  *      input device.
412  *
413  * Parameters:
414  *      buffer  Address of buffer to receive command
415  *      bufsize Size of buffer in bytes
416  *      prompt  Pointer to string to use as prompt string
417  * Returns:
418  *      Pointer to command buffer.
419  * Locking:
420  *      None.
421  * Remarks:
422  *      For SMP kernels, the processor number will be
423  *      substituted for %d, %x or %o in the prompt.
424  */
425
426 char *kdb_getstr(char *buffer, size_t bufsize, char *prompt)
427 {
428         if (prompt && kdb_prompt_str != prompt)
429                 strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
430         kdb_printf(kdb_prompt_str);
431         kdb_nextline = 1;       /* Prompt and input resets line number */
432         return kdb_read(buffer, bufsize);
433 }
434
435 /*
436  * kdb_input_flush
437  *
438  *      Get rid of any buffered console input.
439  *
440  * Parameters:
441  *      none
442  * Returns:
443  *      nothing
444  * Locking:
445  *      none
446  * Remarks:
447  *      Call this function whenever you want to flush input.  If there is any
448  *      outstanding input, it ignores all characters until there has been no
449  *      data for approximately 1ms.
450  */
451
452 static void kdb_input_flush(void)
453 {
454         get_char_func *f;
455         int res;
456         int flush_delay = 1;
457         while (flush_delay) {
458                 flush_delay--;
459 empty:
460                 touch_nmi_watchdog();
461                 for (f = &kdb_poll_funcs[0]; *f; ++f) {
462                         res = (*f)();
463                         if (res != -1) {
464                                 flush_delay = 1;
465                                 goto empty;
466                         }
467                 }
468                 if (flush_delay)
469                         mdelay(1);
470         }
471 }
472
473 /*
474  * kdb_printf
475  *
476  *      Print a string to the output device(s).
477  *
478  * Parameters:
479  *      printf-like format and optional args.
480  * Returns:
481  *      0
482  * Locking:
483  *      None.
484  * Remarks:
485  *      use 'kdbcons->write()' to avoid polluting 'log_buf' with
486  *      kdb output.
487  *
488  *  If the user is doing a cmd args | grep srch
489  *  then kdb_grepping_flag is set.
490  *  In that case we need to accumulate full lines (ending in \n) before
491  *  searching for the pattern.
492  */
493
494 static char kdb_buffer[256];    /* A bit too big to go on stack */
495 static char *next_avail = kdb_buffer;
496 static int  size_avail;
497 static int  suspend_grep;
498
499 /*
500  * search arg1 to see if it contains arg2
501  * (kdmain.c provides flags for ^pat and pat$)
502  *
503  * return 1 for found, 0 for not found
504  */
505 static int kdb_search_string(char *searched, char *searchfor)
506 {
507         char firstchar, *cp;
508         int len1, len2;
509
510         /* not counting the newline at the end of "searched" */
511         len1 = strlen(searched)-1;
512         len2 = strlen(searchfor);
513         if (len1 < len2)
514                 return 0;
515         if (kdb_grep_leading && kdb_grep_trailing && len1 != len2)
516                 return 0;
517         if (kdb_grep_leading) {
518                 if (!strncmp(searched, searchfor, len2))
519                         return 1;
520         } else if (kdb_grep_trailing) {
521                 if (!strncmp(searched+len1-len2, searchfor, len2))
522                         return 1;
523         } else {
524                 firstchar = *searchfor;
525                 cp = searched;
526                 while ((cp = strchr(cp, firstchar))) {
527                         if (!strncmp(cp, searchfor, len2))
528                                 return 1;
529                         cp++;
530                 }
531         }
532         return 0;
533 }
534
535 int kdb_printf(const char *fmt, ...)
536 {
537         va_list ap;
538         int diag;
539         int linecount;
540         int logging, saved_loglevel = 0;
541         int got_printf_lock = 0;
542         int retlen = 0;
543         int fnd, len;
544         char *cp, *cp2, *cphold = NULL, replaced_byte = ' ';
545         char *moreprompt = "more> ";
546         struct console *c = console_drivers;
547         static DEFINE_SPINLOCK(kdb_printf_lock);
548         unsigned long uninitialized_var(flags);
549
550         preempt_disable();
551         /* Serialize kdb_printf if multiple cpus try to write at once.
552          * But if any cpu goes recursive in kdb, just print the output,
553          * even if it is interleaved with any other text.
554          */
555         if (!KDB_STATE(PRINTF_LOCK)) {
556                 KDB_STATE_SET(PRINTF_LOCK);
557                 spin_lock_irqsave(&kdb_printf_lock, flags);
558                 got_printf_lock = 1;
559                 atomic_inc(&kdb_event);
560         } else {
561                 __acquire(kdb_printf_lock);
562         }
563
564         diag = kdbgetintenv("LINES", &linecount);
565         if (diag || linecount <= 1)
566                 linecount = 24;
567
568         diag = kdbgetintenv("LOGGING", &logging);
569         if (diag)
570                 logging = 0;
571
572         if (!kdb_grepping_flag || suspend_grep) {
573                 /* normally, every vsnprintf starts a new buffer */
574                 next_avail = kdb_buffer;
575                 size_avail = sizeof(kdb_buffer);
576         }
577         va_start(ap, fmt);
578         vsnprintf(next_avail, size_avail, fmt, ap);
579         va_end(ap);
580
581         /*
582          * If kdb_parse() found that the command was cmd xxx | grep yyy
583          * then kdb_grepping_flag is set, and kdb_grep_string contains yyy
584          *
585          * Accumulate the print data up to a newline before searching it.
586          * (vsnprintf does null-terminate the string that it generates)
587          */
588
589         /* skip the search if prints are temporarily unconditional */
590         if (!suspend_grep && kdb_grepping_flag) {
591                 cp = strchr(kdb_buffer, '\n');
592                 if (!cp) {
593                         /*
594                          * Special cases that don't end with newlines
595                          * but should be written without one:
596                          *   The "[nn]kdb> " prompt should
597                          *   appear at the front of the buffer.
598                          *
599                          *   The "[nn]more " prompt should also be
600                          *     (MOREPROMPT -> moreprompt)
601                          *   written *   but we print that ourselves,
602                          *   we set the suspend_grep flag to make
603                          *   it unconditional.
604                          *
605                          */
606                         if (next_avail == kdb_buffer) {
607                                 /*
608                                  * these should occur after a newline,
609                                  * so they will be at the front of the
610                                  * buffer
611                                  */
612                                 cp2 = kdb_buffer;
613                                 len = strlen(kdb_prompt_str);
614                                 if (!strncmp(cp2, kdb_prompt_str, len)) {
615                                         /*
616                                          * We're about to start a new
617                                          * command, so we can go back
618                                          * to normal mode.
619                                          */
620                                         kdb_grepping_flag = 0;
621                                         goto kdb_printit;
622                                 }
623                         }
624                         /* no newline; don't search/write the buffer
625                            until one is there */
626                         len = strlen(kdb_buffer);
627                         next_avail = kdb_buffer + len;
628                         size_avail = sizeof(kdb_buffer) - len;
629                         goto kdb_print_out;
630                 }
631
632                 /*
633                  * The newline is present; print through it or discard
634                  * it, depending on the results of the search.
635                  */
636                 cp++;                /* to byte after the newline */
637                 replaced_byte = *cp; /* remember what/where it was */
638                 cphold = cp;
639                 *cp = '\0';          /* end the string for our search */
640
641                 /*
642                  * We now have a newline at the end of the string
643                  * Only continue with this output if it contains the
644                  * search string.
645                  */
646                 fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
647                 if (!fnd) {
648                         /*
649                          * At this point the complete line at the start
650                          * of kdb_buffer can be discarded, as it does
651                          * not contain what the user is looking for.
652                          * Shift the buffer left.
653                          */
654                         *cphold = replaced_byte;
655                         strcpy(kdb_buffer, cphold);
656                         len = strlen(kdb_buffer);
657                         next_avail = kdb_buffer + len;
658                         size_avail = sizeof(kdb_buffer) - len;
659                         goto kdb_print_out;
660                 }
661                 /*
662                  * at this point the string is a full line and
663                  * should be printed, up to the null.
664                  */
665         }
666 kdb_printit:
667
668         /*
669          * Write to all consoles.
670          */
671         retlen = strlen(kdb_buffer);
672         while (c) {
673                 c->write(c, kdb_buffer, retlen);
674                 touch_nmi_watchdog();
675                 c = c->next;
676         }
677         if (logging) {
678                 saved_loglevel = console_loglevel;
679                 console_loglevel = 0;
680                 printk(KERN_INFO "%s", kdb_buffer);
681         }
682
683         if (KDB_STATE(PAGER) && strchr(kdb_buffer, '\n'))
684                 kdb_nextline++;
685
686         /* check for having reached the LINES number of printed lines */
687         if (kdb_nextline == linecount) {
688                 char buf1[16] = "";
689 #if defined(CONFIG_SMP)
690                 char buf2[32];
691 #endif
692
693                 /* Watch out for recursion here.  Any routine that calls
694                  * kdb_printf will come back through here.  And kdb_read
695                  * uses kdb_printf to echo on serial consoles ...
696                  */
697                 kdb_nextline = 1;       /* In case of recursion */
698
699                 /*
700                  * Pause until cr.
701                  */
702                 moreprompt = kdbgetenv("MOREPROMPT");
703                 if (moreprompt == NULL)
704                         moreprompt = "more> ";
705
706 #if defined(CONFIG_SMP)
707                 if (strchr(moreprompt, '%')) {
708                         sprintf(buf2, moreprompt, get_cpu());
709                         put_cpu();
710                         moreprompt = buf2;
711                 }
712 #endif
713
714                 kdb_input_flush();
715                 c = console_drivers;
716
717                 while (c) {
718                         c->write(c, moreprompt, strlen(moreprompt));
719                         touch_nmi_watchdog();
720                         c = c->next;
721                 }
722
723                 if (logging)
724                         printk("%s", moreprompt);
725
726                 kdb_read(buf1, 2); /* '2' indicates to return
727                                     * immediately after getting one key. */
728                 kdb_nextline = 1;       /* Really set output line 1 */
729
730                 /* empty and reset the buffer: */
731                 kdb_buffer[0] = '\0';
732                 next_avail = kdb_buffer;
733                 size_avail = sizeof(kdb_buffer);
734                 if ((buf1[0] == 'q') || (buf1[0] == 'Q')) {
735                         /* user hit q or Q */
736                         KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
737                         KDB_STATE_CLEAR(PAGER);
738                         /* end of command output; back to normal mode */
739                         kdb_grepping_flag = 0;
740                         kdb_printf("\n");
741                 } else if (buf1[0] == ' ') {
742                         kdb_printf("\n");
743                         suspend_grep = 1; /* for this recursion */
744                 } else if (buf1[0] == '\n') {
745                         kdb_nextline = linecount - 1;
746                         kdb_printf("\r");
747                         suspend_grep = 1; /* for this recursion */
748                 } else if (buf1[0] && buf1[0] != '\n') {
749                         /* user hit something other than enter */
750                         suspend_grep = 1; /* for this recursion */
751                         kdb_printf("\nOnly 'q' or 'Q' are processed at more "
752                                    "prompt, input ignored\n");
753                 } else if (kdb_grepping_flag) {
754                         /* user hit enter */
755                         suspend_grep = 1; /* for this recursion */
756                         kdb_printf("\n");
757                 }
758                 kdb_input_flush();
759         }
760
761         /*
762          * For grep searches, shift the printed string left.
763          *  replaced_byte contains the character that was overwritten with
764          *  the terminating null, and cphold points to the null.
765          * Then adjust the notion of available space in the buffer.
766          */
767         if (kdb_grepping_flag && !suspend_grep) {
768                 *cphold = replaced_byte;
769                 strcpy(kdb_buffer, cphold);
770                 len = strlen(kdb_buffer);
771                 next_avail = kdb_buffer + len;
772                 size_avail = sizeof(kdb_buffer) - len;
773         }
774
775 kdb_print_out:
776         suspend_grep = 0; /* end of what may have been a recursive call */
777         if (logging)
778                 console_loglevel = saved_loglevel;
779         if (KDB_STATE(PRINTF_LOCK) && got_printf_lock) {
780                 got_printf_lock = 0;
781                 spin_unlock_irqrestore(&kdb_printf_lock, flags);
782                 KDB_STATE_CLEAR(PRINTF_LOCK);
783                 atomic_dec(&kdb_event);
784         } else {
785                 __release(kdb_printf_lock);
786         }
787         preempt_enable();
788         return retlen;
789 }