blob: d3c5b15c86c1f36d71824de3cbf7c7f7498ee9be [file] [log] [blame]
Jason Wessel5d5314d2010-05-20 21:04:20 -05001/*
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>
Jason Wessela0de0552010-05-20 21:04:24 -050024#include <linux/kgdb.h>
Jason Wessel5d5314d2010-05-20 21:04:20 -050025#include <linux/kdb.h>
26#include <linux/kallsyms.h>
27#include "kdb_private.h"
28
29#define CMD_BUFLEN 256
30char kdb_prompt_str[CMD_BUFLEN];
31
Jason Wesseld37d39a2010-05-20 21:04:27 -050032int kdb_trap_printk;
Jason Wessel5d5314d2010-05-20 21:04:20 -050033
Jason Wessel37f86b42011-05-24 10:43:06 -050034static int kgdb_transition_check(char *buffer)
Jason Wessel5d5314d2010-05-20 21:04:20 -050035{
Jason Wessel37f86b42011-05-24 10:43:06 -050036 if (buffer[0] != '+' && buffer[0] != '$') {
Jason Wessel5d5314d2010-05-20 21:04:20 -050037 KDB_STATE_SET(KGDB_TRANS);
38 kdb_printf("%s", buffer);
Jason Wessel37f86b42011-05-24 10:43:06 -050039 } 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 }
Jason Wessel5d5314d2010-05-20 21:04:20 -050047 }
Jason Wessel37f86b42011-05-24 10:43:06 -050048 return 0;
Jason Wessel5d5314d2010-05-20 21:04:20 -050049}
50
51static int kdb_read_get_key(char *buffer, size_t bufsize)
52{
53#define ESCAPE_UDELAY 1000
54#define ESCAPE_DELAY (2*1000000/ESCAPE_UDELAY) /* 2 seconds worth of udelays */
55 char escape_data[5]; /* longest vt100 escape sequence is 4 bytes */
56 char *ped = escape_data;
57 int escape_delay = 0;
58 get_char_func *f, *f_escape = NULL;
59 int key;
60
61 for (f = &kdb_poll_funcs[0]; ; ++f) {
62 if (*f == NULL) {
63 /* Reset NMI watchdog once per poll loop */
64 touch_nmi_watchdog();
65 f = &kdb_poll_funcs[0];
66 }
67 if (escape_delay == 2) {
68 *ped = '\0';
69 ped = escape_data;
70 --escape_delay;
71 }
72 if (escape_delay == 1) {
73 key = *ped++;
74 if (!*ped)
75 --escape_delay;
76 break;
77 }
78 key = (*f)();
79 if (key == -1) {
80 if (escape_delay) {
81 udelay(ESCAPE_UDELAY);
82 --escape_delay;
83 }
84 continue;
85 }
86 if (bufsize <= 2) {
87 if (key == '\r')
88 key = '\n';
89 *buffer++ = key;
90 *buffer = '\0';
91 return -1;
92 }
93 if (escape_delay == 0 && key == '\e') {
94 escape_delay = ESCAPE_DELAY;
95 ped = escape_data;
96 f_escape = f;
97 }
98 if (escape_delay) {
99 *ped++ = key;
100 if (f_escape != f) {
101 escape_delay = 2;
102 continue;
103 }
104 if (ped - escape_data == 1) {
105 /* \e */
106 continue;
107 } else if (ped - escape_data == 2) {
108 /* \e<something> */
109 if (key != '[')
110 escape_delay = 2;
111 continue;
112 } else if (ped - escape_data == 3) {
113 /* \e[<something> */
114 int mapkey = 0;
115 switch (key) {
116 case 'A': /* \e[A, up arrow */
117 mapkey = 16;
118 break;
119 case 'B': /* \e[B, down arrow */
120 mapkey = 14;
121 break;
122 case 'C': /* \e[C, right arrow */
123 mapkey = 6;
124 break;
125 case 'D': /* \e[D, left arrow */
126 mapkey = 2;
127 break;
128 case '1': /* dropthrough */
129 case '3': /* dropthrough */
130 /* \e[<1,3,4>], may be home, del, end */
131 case '4':
132 mapkey = -1;
133 break;
134 }
135 if (mapkey != -1) {
136 if (mapkey > 0) {
137 escape_data[0] = mapkey;
138 escape_data[1] = '\0';
139 }
140 escape_delay = 2;
141 }
142 continue;
143 } else if (ped - escape_data == 4) {
144 /* \e[<1,3,4><something> */
145 int mapkey = 0;
146 if (key == '~') {
147 switch (escape_data[2]) {
148 case '1': /* \e[1~, home */
149 mapkey = 1;
150 break;
151 case '3': /* \e[3~, del */
152 mapkey = 4;
153 break;
154 case '4': /* \e[4~, end */
155 mapkey = 5;
156 break;
157 }
158 }
159 if (mapkey > 0) {
160 escape_data[0] = mapkey;
161 escape_data[1] = '\0';
162 }
163 escape_delay = 2;
164 continue;
165 }
166 }
167 break; /* A key to process */
168 }
169 return key;
170}
171
172/*
173 * kdb_read
174 *
175 * This function reads a string of characters, terminated by
176 * a newline, or by reaching the end of the supplied buffer,
177 * from the current kernel debugger console device.
178 * Parameters:
179 * buffer - Address of character buffer to receive input characters.
180 * bufsize - size, in bytes, of the character buffer
181 * Returns:
182 * Returns a pointer to the buffer containing the received
183 * character string. This string will be terminated by a
184 * newline character.
185 * Locking:
186 * No locks are required to be held upon entry to this
187 * function. It is not reentrant - it relies on the fact
188 * that while kdb is running on only one "master debug" cpu.
189 * Remarks:
190 *
191 * The buffer size must be >= 2. A buffer size of 2 means that the caller only
192 * wants a single key.
193 *
194 * An escape key could be the start of a vt100 control sequence such as \e[D
195 * (left arrow) or it could be a character in its own right. The standard
196 * method for detecting the difference is to wait for 2 seconds to see if there
197 * are any other characters. kdb is complicated by the lack of a timer service
198 * (interrupts are off), by multiple input sources and by the need to sometimes
199 * return after just one key. Escape sequence processing has to be done as
200 * states in the polling loop.
201 */
202
203static char *kdb_read(char *buffer, size_t bufsize)
204{
205 char *cp = buffer;
206 char *bufend = buffer+bufsize-2; /* Reserve space for newline
207 * and null byte */
208 char *lastchar;
209 char *p_tmp;
210 char tmp;
211 static char tmpbuffer[CMD_BUFLEN];
212 int len = strlen(buffer);
213 int len_tmp;
214 int tab = 0;
215 int count;
216 int i;
217 int diag, dtab_count;
Prarit Bhargava8f9b3dd2018-09-20 08:59:14 -0400218 int key, buf_size, ret;
Colin Cross085886c2012-03-14 19:26:53 -0700219 static int last_crlf;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500220
221 diag = kdbgetintenv("DTABCOUNT", &dtab_count);
222 if (diag)
223 dtab_count = 30;
224
225 if (len > 0) {
226 cp += len;
227 if (*(buffer+len-1) == '\n')
228 cp--;
229 }
230
231 lastchar = cp;
232 *cp = '\0';
233 kdb_printf("%s", buffer);
234poll_again:
235 key = kdb_read_get_key(buffer, bufsize);
236 if (key == -1)
237 return buffer;
238 if (key != 9)
239 tab = 0;
Colin Cross085886c2012-03-14 19:26:53 -0700240 if (key != 10 && key != 13)
241 last_crlf = 0;
242
Jason Wessel5d5314d2010-05-20 21:04:20 -0500243 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;
Colin Cross085886c2012-03-14 19:26:53 -0700260 case 10: /* new line */
261 case 13: /* carriage return */
262 /* handle \n after \r */
263 if (last_crlf && last_crlf != key)
264 break;
265 last_crlf = key;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500266 *lastchar++ = '\n';
267 *lastchar++ = '\0';
Jason Wessel37f86b42011-05-24 10:43:06 -0500268 if (!KDB_STATE(KGDB_TRANS)) {
269 KDB_STATE_SET(KGDB_TRANS);
270 kdb_printf("%s", buffer);
271 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500272 kdb_printf("\n");
273 return buffer;
274 case 4: /* Del */
275 if (cp < lastchar) {
276 memcpy(tmpbuffer, cp+1, lastchar - cp - 1);
277 memcpy(cp, tmpbuffer, lastchar - cp - 1);
278 *(--lastchar) = '\0';
279 kdb_printf("%s \r", cp);
280 tmp = *cp;
281 *cp = '\0';
282 kdb_printf(kdb_prompt_str);
283 kdb_printf("%s", buffer);
284 *cp = tmp;
285 }
286 break;
287 case 1: /* Home */
288 if (cp > buffer) {
289 kdb_printf("\r");
290 kdb_printf(kdb_prompt_str);
291 cp = buffer;
292 }
293 break;
294 case 5: /* End */
295 if (cp < lastchar) {
296 kdb_printf("%s", cp);
297 cp = lastchar;
298 }
299 break;
300 case 2: /* Left */
301 if (cp > buffer) {
302 kdb_printf("\b");
303 --cp;
304 }
305 break;
306 case 14: /* Down */
307 memset(tmpbuffer, ' ',
308 strlen(kdb_prompt_str) + (lastchar-buffer));
309 *(tmpbuffer+strlen(kdb_prompt_str) +
310 (lastchar-buffer)) = '\0';
311 kdb_printf("\r%s\r", tmpbuffer);
312 *lastchar = (char)key;
313 *(lastchar+1) = '\0';
314 return lastchar;
315 case 6: /* Right */
316 if (cp < lastchar) {
317 kdb_printf("%c", *cp);
318 ++cp;
319 }
320 break;
321 case 16: /* Up */
322 memset(tmpbuffer, ' ',
323 strlen(kdb_prompt_str) + (lastchar-buffer));
324 *(tmpbuffer+strlen(kdb_prompt_str) +
325 (lastchar-buffer)) = '\0';
326 kdb_printf("\r%s\r", tmpbuffer);
327 *lastchar = (char)key;
328 *(lastchar+1) = '\0';
329 return lastchar;
330 case 9: /* Tab */
331 if (tab < 2)
332 ++tab;
333 p_tmp = buffer;
334 while (*p_tmp == ' ')
335 p_tmp++;
336 if (p_tmp > cp)
337 break;
338 memcpy(tmpbuffer, p_tmp, cp-p_tmp);
339 *(tmpbuffer + (cp-p_tmp)) = '\0';
340 p_tmp = strrchr(tmpbuffer, ' ');
341 if (p_tmp)
342 ++p_tmp;
343 else
344 p_tmp = tmpbuffer;
345 len = strlen(p_tmp);
Prarit Bhargava8f9b3dd2018-09-20 08:59:14 -0400346 buf_size = sizeof(tmpbuffer) - (p_tmp - tmpbuffer);
347 count = kallsyms_symbol_complete(p_tmp, buf_size);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500348 if (tab == 2 && count > 0) {
349 kdb_printf("\n%d symbols are found.", count);
350 if (count > dtab_count) {
351 count = dtab_count;
352 kdb_printf(" But only first %d symbols will"
353 " be printed.\nYou can change the"
354 " environment variable DTABCOUNT.",
355 count);
356 }
357 kdb_printf("\n");
358 for (i = 0; i < count; i++) {
Prarit Bhargava8f9b3dd2018-09-20 08:59:14 -0400359 ret = kallsyms_symbol_next(p_tmp, i, buf_size);
360 if (WARN_ON(!ret))
Jason Wessel5d5314d2010-05-20 21:04:20 -0500361 break;
Prarit Bhargava8f9b3dd2018-09-20 08:59:14 -0400362 if (ret != -E2BIG)
363 kdb_printf("%s ", p_tmp);
364 else
365 kdb_printf("%s... ", p_tmp);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500366 *(p_tmp + len) = '\0';
367 }
368 if (i >= dtab_count)
369 kdb_printf("...");
370 kdb_printf("\n");
371 kdb_printf(kdb_prompt_str);
372 kdb_printf("%s", buffer);
373 } else if (tab != 2 && count > 0) {
374 len_tmp = strlen(p_tmp);
375 strncpy(p_tmp+len_tmp, cp, lastchar-cp+1);
376 len_tmp = strlen(p_tmp);
377 strncpy(cp, p_tmp+len, len_tmp-len + 1);
378 len = len_tmp - len;
379 kdb_printf("%s", cp);
380 cp += len;
381 lastchar += len;
382 }
383 kdb_nextline = 1; /* reset output line number */
384 break;
385 default:
386 if (key >= 32 && lastchar < bufend) {
387 if (cp < lastchar) {
388 memcpy(tmpbuffer, cp, lastchar - cp);
389 memcpy(cp+1, tmpbuffer, lastchar - cp);
390 *++lastchar = '\0';
391 *cp = key;
392 kdb_printf("%s\r", cp);
393 ++cp;
394 tmp = *cp;
395 *cp = '\0';
396 kdb_printf(kdb_prompt_str);
397 kdb_printf("%s", buffer);
398 *cp = tmp;
399 } else {
400 *++lastchar = '\0';
401 *cp++ = key;
402 /* The kgdb transition check will hide
403 * printed characters if we think that
404 * kgdb is connecting, until the check
405 * fails */
Jason Wessel37f86b42011-05-24 10:43:06 -0500406 if (!KDB_STATE(KGDB_TRANS)) {
407 if (kgdb_transition_check(buffer))
408 return buffer;
409 } else {
Jason Wessel5d5314d2010-05-20 21:04:20 -0500410 kdb_printf("%c", key);
Jason Wessel37f86b42011-05-24 10:43:06 -0500411 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500412 }
413 /* Special escape to kgdb */
414 if (lastchar - buffer >= 5 &&
415 strcmp(lastchar - 5, "$?#3f") == 0) {
Jason Wesself679c492011-05-23 13:17:41 -0500416 kdb_gdb_state_pass(lastchar - 5);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500417 strcpy(buffer, "kgdb");
418 KDB_STATE_SET(DOING_KGDB);
419 return buffer;
420 }
Jason Wesself679c492011-05-23 13:17:41 -0500421 if (lastchar - buffer >= 11 &&
422 strcmp(lastchar - 11, "$qSupported") == 0) {
423 kdb_gdb_state_pass(lastchar - 11);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500424 strcpy(buffer, "kgdb");
Jason Wesseld613d822011-05-23 13:22:54 -0500425 KDB_STATE_SET(DOING_KGDB);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500426 return buffer;
427 }
428 }
429 break;
430 }
431 goto poll_again;
432}
433
434/*
435 * kdb_getstr
436 *
437 * Print the prompt string and read a command from the
438 * input device.
439 *
440 * Parameters:
441 * buffer Address of buffer to receive command
442 * bufsize Size of buffer in bytes
443 * prompt Pointer to string to use as prompt string
444 * Returns:
445 * Pointer to command buffer.
446 * Locking:
447 * None.
448 * Remarks:
449 * For SMP kernels, the processor number will be
450 * substituted for %d, %x or %o in the prompt.
451 */
452
Daniel Thompson32d375f2014-09-11 10:41:12 +0100453char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
Jason Wessel5d5314d2010-05-20 21:04:20 -0500454{
455 if (prompt && kdb_prompt_str != prompt)
456 strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
457 kdb_printf(kdb_prompt_str);
458 kdb_nextline = 1; /* Prompt and input resets line number */
459 return kdb_read(buffer, bufsize);
460}
461
462/*
463 * kdb_input_flush
464 *
465 * Get rid of any buffered console input.
466 *
467 * Parameters:
468 * none
469 * Returns:
470 * nothing
471 * Locking:
472 * none
473 * Remarks:
474 * Call this function whenever you want to flush input. If there is any
475 * outstanding input, it ignores all characters until there has been no
476 * data for approximately 1ms.
477 */
478
479static void kdb_input_flush(void)
480{
481 get_char_func *f;
482 int res;
483 int flush_delay = 1;
484 while (flush_delay) {
485 flush_delay--;
486empty:
487 touch_nmi_watchdog();
488 for (f = &kdb_poll_funcs[0]; *f; ++f) {
489 res = (*f)();
490 if (res != -1) {
491 flush_delay = 1;
492 goto empty;
493 }
494 }
495 if (flush_delay)
496 mdelay(1);
497 }
498}
499
500/*
501 * kdb_printf
502 *
503 * Print a string to the output device(s).
504 *
505 * Parameters:
506 * printf-like format and optional args.
507 * Returns:
508 * 0
509 * Locking:
510 * None.
511 * Remarks:
512 * use 'kdbcons->write()' to avoid polluting 'log_buf' with
513 * kdb output.
514 *
515 * If the user is doing a cmd args | grep srch
516 * then kdb_grepping_flag is set.
517 * In that case we need to accumulate full lines (ending in \n) before
518 * searching for the pattern.
519 */
520
521static char kdb_buffer[256]; /* A bit too big to go on stack */
522static char *next_avail = kdb_buffer;
523static int size_avail;
524static int suspend_grep;
525
526/*
527 * search arg1 to see if it contains arg2
528 * (kdmain.c provides flags for ^pat and pat$)
529 *
530 * return 1 for found, 0 for not found
531 */
532static int kdb_search_string(char *searched, char *searchfor)
533{
534 char firstchar, *cp;
535 int len1, len2;
536
537 /* not counting the newline at the end of "searched" */
538 len1 = strlen(searched)-1;
539 len2 = strlen(searchfor);
540 if (len1 < len2)
541 return 0;
542 if (kdb_grep_leading && kdb_grep_trailing && len1 != len2)
543 return 0;
544 if (kdb_grep_leading) {
545 if (!strncmp(searched, searchfor, len2))
546 return 1;
547 } else if (kdb_grep_trailing) {
548 if (!strncmp(searched+len1-len2, searchfor, len2))
549 return 1;
550 } else {
551 firstchar = *searchfor;
552 cp = searched;
553 while ((cp = strchr(cp, firstchar))) {
554 if (!strncmp(cp, searchfor, len2))
555 return 1;
556 cp++;
557 }
558 }
559 return 0;
560}
561
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000562int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
Jason Wessel5d5314d2010-05-20 21:04:20 -0500563{
Jason Wessel5d5314d2010-05-20 21:04:20 -0500564 int diag;
565 int linecount;
Jason Wessel17b572e82012-08-26 22:37:03 -0500566 int colcount;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500567 int logging, saved_loglevel = 0;
Jason Wesseld37d39a2010-05-20 21:04:27 -0500568 int saved_trap_printk;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500569 int got_printf_lock = 0;
570 int retlen = 0;
571 int fnd, len;
572 char *cp, *cp2, *cphold = NULL, replaced_byte = ' ';
573 char *moreprompt = "more> ";
574 struct console *c = console_drivers;
575 static DEFINE_SPINLOCK(kdb_printf_lock);
576 unsigned long uninitialized_var(flags);
577
578 preempt_disable();
Jason Wesseld37d39a2010-05-20 21:04:27 -0500579 saved_trap_printk = kdb_trap_printk;
580 kdb_trap_printk = 0;
581
Jason Wessel5d5314d2010-05-20 21:04:20 -0500582 /* Serialize kdb_printf if multiple cpus try to write at once.
583 * But if any cpu goes recursive in kdb, just print the output,
584 * even if it is interleaved with any other text.
585 */
586 if (!KDB_STATE(PRINTF_LOCK)) {
587 KDB_STATE_SET(PRINTF_LOCK);
588 spin_lock_irqsave(&kdb_printf_lock, flags);
589 got_printf_lock = 1;
590 atomic_inc(&kdb_event);
591 } else {
592 __acquire(kdb_printf_lock);
593 }
594
595 diag = kdbgetintenv("LINES", &linecount);
596 if (diag || linecount <= 1)
597 linecount = 24;
598
Jason Wessel17b572e82012-08-26 22:37:03 -0500599 diag = kdbgetintenv("COLUMNS", &colcount);
600 if (diag || colcount <= 1)
601 colcount = 80;
602
Jason Wessel5d5314d2010-05-20 21:04:20 -0500603 diag = kdbgetintenv("LOGGING", &logging);
604 if (diag)
605 logging = 0;
606
607 if (!kdb_grepping_flag || suspend_grep) {
608 /* normally, every vsnprintf starts a new buffer */
609 next_avail = kdb_buffer;
610 size_avail = sizeof(kdb_buffer);
611 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500612 vsnprintf(next_avail, size_avail, fmt, ap);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500613
614 /*
615 * If kdb_parse() found that the command was cmd xxx | grep yyy
616 * then kdb_grepping_flag is set, and kdb_grep_string contains yyy
617 *
618 * Accumulate the print data up to a newline before searching it.
619 * (vsnprintf does null-terminate the string that it generates)
620 */
621
622 /* skip the search if prints are temporarily unconditional */
623 if (!suspend_grep && kdb_grepping_flag) {
624 cp = strchr(kdb_buffer, '\n');
625 if (!cp) {
626 /*
627 * Special cases that don't end with newlines
628 * but should be written without one:
629 * The "[nn]kdb> " prompt should
630 * appear at the front of the buffer.
631 *
632 * The "[nn]more " prompt should also be
633 * (MOREPROMPT -> moreprompt)
634 * written * but we print that ourselves,
635 * we set the suspend_grep flag to make
636 * it unconditional.
637 *
638 */
639 if (next_avail == kdb_buffer) {
640 /*
641 * these should occur after a newline,
642 * so they will be at the front of the
643 * buffer
644 */
645 cp2 = kdb_buffer;
646 len = strlen(kdb_prompt_str);
647 if (!strncmp(cp2, kdb_prompt_str, len)) {
648 /*
649 * We're about to start a new
650 * command, so we can go back
651 * to normal mode.
652 */
653 kdb_grepping_flag = 0;
654 goto kdb_printit;
655 }
656 }
657 /* no newline; don't search/write the buffer
658 until one is there */
659 len = strlen(kdb_buffer);
660 next_avail = kdb_buffer + len;
661 size_avail = sizeof(kdb_buffer) - len;
662 goto kdb_print_out;
663 }
664
665 /*
666 * The newline is present; print through it or discard
667 * it, depending on the results of the search.
668 */
669 cp++; /* to byte after the newline */
670 replaced_byte = *cp; /* remember what/where it was */
671 cphold = cp;
672 *cp = '\0'; /* end the string for our search */
673
674 /*
675 * We now have a newline at the end of the string
676 * Only continue with this output if it contains the
677 * search string.
678 */
679 fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
680 if (!fnd) {
681 /*
682 * At this point the complete line at the start
683 * of kdb_buffer can be discarded, as it does
684 * not contain what the user is looking for.
685 * Shift the buffer left.
686 */
687 *cphold = replaced_byte;
688 strcpy(kdb_buffer, cphold);
689 len = strlen(kdb_buffer);
690 next_avail = kdb_buffer + len;
691 size_avail = sizeof(kdb_buffer) - len;
692 goto kdb_print_out;
693 }
Daniel Thompsonfb6daa72014-09-11 10:37:10 +0100694 if (kdb_grepping_flag >= KDB_GREPPING_FLAG_SEARCH)
695 /*
696 * This was a interactive search (using '/' at more
697 * prompt) and it has completed. Clear the flag.
698 */
699 kdb_grepping_flag = 0;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500700 /*
701 * at this point the string is a full line and
702 * should be printed, up to the null.
703 */
704 }
705kdb_printit:
706
707 /*
708 * Write to all consoles.
709 */
710 retlen = strlen(kdb_buffer);
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000711 cp = (char *) printk_skip_level(kdb_buffer);
Jason Wessela0de0552010-05-20 21:04:24 -0500712 if (!dbg_kdb_mode && kgdb_connected) {
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000713 gdbstub_msg_write(cp, retlen - (cp - kdb_buffer));
Jason Wessela0de0552010-05-20 21:04:24 -0500714 } else {
Tim Birdb8adde82011-09-21 13:19:12 -0700715 if (dbg_io_ops && !dbg_io_ops->is_console) {
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000716 len = retlen - (cp - kdb_buffer);
717 cp2 = cp;
Jason Wesselefe2f292010-05-20 21:04:26 -0500718 while (len--) {
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000719 dbg_io_ops->write_char(*cp2);
720 cp2++;
Jason Wesselefe2f292010-05-20 21:04:26 -0500721 }
722 }
Jason Wessela0de0552010-05-20 21:04:24 -0500723 while (c) {
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000724 c->write(c, cp, retlen - (cp - kdb_buffer));
Jason Wessela0de0552010-05-20 21:04:24 -0500725 touch_nmi_watchdog();
726 c = c->next;
727 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500728 }
729 if (logging) {
730 saved_loglevel = console_loglevel;
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700731 console_loglevel = CONSOLE_LOGLEVEL_SILENT;
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000732 if (printk_get_level(kdb_buffer) || src == KDB_MSGSRC_PRINTK)
733 printk("%s", kdb_buffer);
734 else
735 pr_info("%s", kdb_buffer);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500736 }
737
Jason Wessel17b572e82012-08-26 22:37:03 -0500738 if (KDB_STATE(PAGER)) {
739 /*
740 * Check printed string to decide how to bump the
741 * kdb_nextline to control when the more prompt should
742 * show up.
743 */
744 int got = 0;
745 len = retlen;
746 while (len--) {
747 if (kdb_buffer[len] == '\n') {
748 kdb_nextline++;
749 got = 0;
750 } else if (kdb_buffer[len] == '\r') {
751 got = 0;
752 } else {
753 got++;
754 }
755 }
756 kdb_nextline += got / (colcount + 1);
757 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500758
759 /* check for having reached the LINES number of printed lines */
Jason Wessel17b572e82012-08-26 22:37:03 -0500760 if (kdb_nextline >= linecount) {
Jason Wessel5d5314d2010-05-20 21:04:20 -0500761 char buf1[16] = "";
Jason Wessel5d5314d2010-05-20 21:04:20 -0500762
763 /* Watch out for recursion here. Any routine that calls
764 * kdb_printf will come back through here. And kdb_read
765 * uses kdb_printf to echo on serial consoles ...
766 */
767 kdb_nextline = 1; /* In case of recursion */
768
769 /*
770 * Pause until cr.
771 */
772 moreprompt = kdbgetenv("MOREPROMPT");
773 if (moreprompt == NULL)
774 moreprompt = "more> ";
775
Jason Wessel5d5314d2010-05-20 21:04:20 -0500776 kdb_input_flush();
777 c = console_drivers;
778
Jason Wessel78724b82012-03-29 06:17:17 -0500779 if (dbg_io_ops && !dbg_io_ops->is_console) {
Jason Wesselefe2f292010-05-20 21:04:26 -0500780 len = strlen(moreprompt);
781 cp = moreprompt;
782 while (len--) {
783 dbg_io_ops->write_char(*cp);
784 cp++;
785 }
786 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500787 while (c) {
788 c->write(c, moreprompt, strlen(moreprompt));
789 touch_nmi_watchdog();
790 c = c->next;
791 }
792
793 if (logging)
794 printk("%s", moreprompt);
795
796 kdb_read(buf1, 2); /* '2' indicates to return
797 * immediately after getting one key. */
798 kdb_nextline = 1; /* Really set output line 1 */
799
800 /* empty and reset the buffer: */
801 kdb_buffer[0] = '\0';
802 next_avail = kdb_buffer;
803 size_avail = sizeof(kdb_buffer);
804 if ((buf1[0] == 'q') || (buf1[0] == 'Q')) {
805 /* user hit q or Q */
806 KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
807 KDB_STATE_CLEAR(PAGER);
808 /* end of command output; back to normal mode */
809 kdb_grepping_flag = 0;
810 kdb_printf("\n");
811 } else if (buf1[0] == ' ') {
Jason Wessel17b572e82012-08-26 22:37:03 -0500812 kdb_printf("\r");
Jason Wessel5d5314d2010-05-20 21:04:20 -0500813 suspend_grep = 1; /* for this recursion */
814 } else if (buf1[0] == '\n') {
815 kdb_nextline = linecount - 1;
816 kdb_printf("\r");
817 suspend_grep = 1; /* for this recursion */
Daniel Thompsonfb6daa72014-09-11 10:37:10 +0100818 } else if (buf1[0] == '/' && !kdb_grepping_flag) {
819 kdb_printf("\r");
820 kdb_getstr(kdb_grep_string, KDB_GREP_STRLEN,
821 kdbgetenv("SEARCHPROMPT") ?: "search> ");
822 *strchrnul(kdb_grep_string, '\n') = '\0';
823 kdb_grepping_flag += KDB_GREPPING_FLAG_SEARCH;
824 suspend_grep = 1; /* for this recursion */
Jason Wessel5d5314d2010-05-20 21:04:20 -0500825 } else if (buf1[0] && buf1[0] != '\n') {
826 /* user hit something other than enter */
827 suspend_grep = 1; /* for this recursion */
Daniel Thompsonfb6daa72014-09-11 10:37:10 +0100828 if (buf1[0] != '/')
829 kdb_printf(
830 "\nOnly 'q', 'Q' or '/' are processed at "
831 "more prompt, input ignored\n");
832 else
833 kdb_printf("\n'/' cannot be used during | "
834 "grep filtering, input ignored\n");
Jason Wessel5d5314d2010-05-20 21:04:20 -0500835 } else if (kdb_grepping_flag) {
836 /* user hit enter */
837 suspend_grep = 1; /* for this recursion */
838 kdb_printf("\n");
839 }
840 kdb_input_flush();
841 }
842
843 /*
844 * For grep searches, shift the printed string left.
845 * replaced_byte contains the character that was overwritten with
846 * the terminating null, and cphold points to the null.
847 * Then adjust the notion of available space in the buffer.
848 */
849 if (kdb_grepping_flag && !suspend_grep) {
850 *cphold = replaced_byte;
851 strcpy(kdb_buffer, cphold);
852 len = strlen(kdb_buffer);
853 next_avail = kdb_buffer + len;
854 size_avail = sizeof(kdb_buffer) - len;
855 }
856
857kdb_print_out:
858 suspend_grep = 0; /* end of what may have been a recursive call */
859 if (logging)
860 console_loglevel = saved_loglevel;
861 if (KDB_STATE(PRINTF_LOCK) && got_printf_lock) {
862 got_printf_lock = 0;
863 spin_unlock_irqrestore(&kdb_printf_lock, flags);
864 KDB_STATE_CLEAR(PRINTF_LOCK);
865 atomic_dec(&kdb_event);
866 } else {
867 __release(kdb_printf_lock);
868 }
Jason Wesseld37d39a2010-05-20 21:04:27 -0500869 kdb_trap_printk = saved_trap_printk;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500870 preempt_enable();
871 return retlen;
872}
Jason Wesseld37d39a2010-05-20 21:04:27 -0500873
874int kdb_printf(const char *fmt, ...)
875{
876 va_list ap;
877 int r;
878
879 va_start(ap, fmt);
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000880 r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
Jason Wesseld37d39a2010-05-20 21:04:27 -0500881 va_end(ap);
882
883 return r;
884}
Jason Wesself7030bb2010-10-11 10:20:14 -0500885EXPORT_SYMBOL_GPL(kdb_printf);