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