blob: 3990c1f73e451bd2f5ba7860d594ccc8fac924b6 [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;
218 int key;
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);
346 count = kallsyms_symbol_complete(p_tmp,
347 sizeof(tmpbuffer) -
348 (p_tmp - tmpbuffer));
349 if (tab == 2 && count > 0) {
350 kdb_printf("\n%d symbols are found.", count);
351 if (count > dtab_count) {
352 count = dtab_count;
353 kdb_printf(" But only first %d symbols will"
354 " be printed.\nYou can change the"
355 " environment variable DTABCOUNT.",
356 count);
357 }
358 kdb_printf("\n");
359 for (i = 0; i < count; i++) {
Daniel Thompson30b18ee2015-03-02 14:13:36 +0000360 if (WARN_ON(!kallsyms_symbol_next(p_tmp, i)))
Jason Wessel5d5314d2010-05-20 21:04:20 -0500361 break;
362 kdb_printf("%s ", p_tmp);
363 *(p_tmp + len) = '\0';
364 }
365 if (i >= dtab_count)
366 kdb_printf("...");
367 kdb_printf("\n");
368 kdb_printf(kdb_prompt_str);
369 kdb_printf("%s", buffer);
370 } else if (tab != 2 && count > 0) {
371 len_tmp = strlen(p_tmp);
372 strncpy(p_tmp+len_tmp, cp, lastchar-cp+1);
373 len_tmp = strlen(p_tmp);
374 strncpy(cp, p_tmp+len, len_tmp-len + 1);
375 len = len_tmp - len;
376 kdb_printf("%s", cp);
377 cp += len;
378 lastchar += len;
379 }
380 kdb_nextline = 1; /* reset output line number */
381 break;
382 default:
383 if (key >= 32 && lastchar < bufend) {
384 if (cp < lastchar) {
385 memcpy(tmpbuffer, cp, lastchar - cp);
386 memcpy(cp+1, tmpbuffer, lastchar - cp);
387 *++lastchar = '\0';
388 *cp = key;
389 kdb_printf("%s\r", cp);
390 ++cp;
391 tmp = *cp;
392 *cp = '\0';
393 kdb_printf(kdb_prompt_str);
394 kdb_printf("%s", buffer);
395 *cp = tmp;
396 } else {
397 *++lastchar = '\0';
398 *cp++ = key;
399 /* The kgdb transition check will hide
400 * printed characters if we think that
401 * kgdb is connecting, until the check
402 * fails */
Jason Wessel37f86b42011-05-24 10:43:06 -0500403 if (!KDB_STATE(KGDB_TRANS)) {
404 if (kgdb_transition_check(buffer))
405 return buffer;
406 } else {
Jason Wessel5d5314d2010-05-20 21:04:20 -0500407 kdb_printf("%c", key);
Jason Wessel37f86b42011-05-24 10:43:06 -0500408 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500409 }
410 /* Special escape to kgdb */
411 if (lastchar - buffer >= 5 &&
412 strcmp(lastchar - 5, "$?#3f") == 0) {
Jason Wesself679c492011-05-23 13:17:41 -0500413 kdb_gdb_state_pass(lastchar - 5);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500414 strcpy(buffer, "kgdb");
415 KDB_STATE_SET(DOING_KGDB);
416 return buffer;
417 }
Jason Wesself679c492011-05-23 13:17:41 -0500418 if (lastchar - buffer >= 11 &&
419 strcmp(lastchar - 11, "$qSupported") == 0) {
420 kdb_gdb_state_pass(lastchar - 11);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500421 strcpy(buffer, "kgdb");
Jason Wesseld613d822011-05-23 13:22:54 -0500422 KDB_STATE_SET(DOING_KGDB);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500423 return buffer;
424 }
425 }
426 break;
427 }
428 goto poll_again;
429}
430
431/*
432 * kdb_getstr
433 *
434 * Print the prompt string and read a command from the
435 * input device.
436 *
437 * Parameters:
438 * buffer Address of buffer to receive command
439 * bufsize Size of buffer in bytes
440 * prompt Pointer to string to use as prompt string
441 * Returns:
442 * Pointer to command buffer.
443 * Locking:
444 * None.
445 * Remarks:
446 * For SMP kernels, the processor number will be
447 * substituted for %d, %x or %o in the prompt.
448 */
449
Daniel Thompson32d375f2014-09-11 10:41:12 +0100450char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
Jason Wessel5d5314d2010-05-20 21:04:20 -0500451{
452 if (prompt && kdb_prompt_str != prompt)
453 strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
454 kdb_printf(kdb_prompt_str);
455 kdb_nextline = 1; /* Prompt and input resets line number */
456 return kdb_read(buffer, bufsize);
457}
458
459/*
460 * kdb_input_flush
461 *
462 * Get rid of any buffered console input.
463 *
464 * Parameters:
465 * none
466 * Returns:
467 * nothing
468 * Locking:
469 * none
470 * Remarks:
471 * Call this function whenever you want to flush input. If there is any
472 * outstanding input, it ignores all characters until there has been no
473 * data for approximately 1ms.
474 */
475
476static void kdb_input_flush(void)
477{
478 get_char_func *f;
479 int res;
480 int flush_delay = 1;
481 while (flush_delay) {
482 flush_delay--;
483empty:
484 touch_nmi_watchdog();
485 for (f = &kdb_poll_funcs[0]; *f; ++f) {
486 res = (*f)();
487 if (res != -1) {
488 flush_delay = 1;
489 goto empty;
490 }
491 }
492 if (flush_delay)
493 mdelay(1);
494 }
495}
496
497/*
498 * kdb_printf
499 *
500 * Print a string to the output device(s).
501 *
502 * Parameters:
503 * printf-like format and optional args.
504 * Returns:
505 * 0
506 * Locking:
507 * None.
508 * Remarks:
509 * use 'kdbcons->write()' to avoid polluting 'log_buf' with
510 * kdb output.
511 *
512 * If the user is doing a cmd args | grep srch
513 * then kdb_grepping_flag is set.
514 * In that case we need to accumulate full lines (ending in \n) before
515 * searching for the pattern.
516 */
517
518static char kdb_buffer[256]; /* A bit too big to go on stack */
519static char *next_avail = kdb_buffer;
520static int size_avail;
521static int suspend_grep;
522
523/*
524 * search arg1 to see if it contains arg2
525 * (kdmain.c provides flags for ^pat and pat$)
526 *
527 * return 1 for found, 0 for not found
528 */
529static int kdb_search_string(char *searched, char *searchfor)
530{
531 char firstchar, *cp;
532 int len1, len2;
533
534 /* not counting the newline at the end of "searched" */
535 len1 = strlen(searched)-1;
536 len2 = strlen(searchfor);
537 if (len1 < len2)
538 return 0;
539 if (kdb_grep_leading && kdb_grep_trailing && len1 != len2)
540 return 0;
541 if (kdb_grep_leading) {
542 if (!strncmp(searched, searchfor, len2))
543 return 1;
544 } else if (kdb_grep_trailing) {
545 if (!strncmp(searched+len1-len2, searchfor, len2))
546 return 1;
547 } else {
548 firstchar = *searchfor;
549 cp = searched;
550 while ((cp = strchr(cp, firstchar))) {
551 if (!strncmp(cp, searchfor, len2))
552 return 1;
553 cp++;
554 }
555 }
556 return 0;
557}
558
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000559int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
Jason Wessel5d5314d2010-05-20 21:04:20 -0500560{
Jason Wessel5d5314d2010-05-20 21:04:20 -0500561 int diag;
562 int linecount;
Jason Wessel17b572e82012-08-26 22:37:03 -0500563 int colcount;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500564 int logging, saved_loglevel = 0;
Jason Wesseld37d39a2010-05-20 21:04:27 -0500565 int saved_trap_printk;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500566 int got_printf_lock = 0;
567 int retlen = 0;
568 int fnd, len;
569 char *cp, *cp2, *cphold = NULL, replaced_byte = ' ';
570 char *moreprompt = "more> ";
571 struct console *c = console_drivers;
572 static DEFINE_SPINLOCK(kdb_printf_lock);
573 unsigned long uninitialized_var(flags);
574
575 preempt_disable();
Jason Wesseld37d39a2010-05-20 21:04:27 -0500576 saved_trap_printk = kdb_trap_printk;
577 kdb_trap_printk = 0;
578
Jason Wessel5d5314d2010-05-20 21:04:20 -0500579 /* Serialize kdb_printf if multiple cpus try to write at once.
580 * But if any cpu goes recursive in kdb, just print the output,
581 * even if it is interleaved with any other text.
582 */
583 if (!KDB_STATE(PRINTF_LOCK)) {
584 KDB_STATE_SET(PRINTF_LOCK);
585 spin_lock_irqsave(&kdb_printf_lock, flags);
586 got_printf_lock = 1;
587 atomic_inc(&kdb_event);
588 } else {
589 __acquire(kdb_printf_lock);
590 }
591
592 diag = kdbgetintenv("LINES", &linecount);
593 if (diag || linecount <= 1)
594 linecount = 24;
595
Jason Wessel17b572e82012-08-26 22:37:03 -0500596 diag = kdbgetintenv("COLUMNS", &colcount);
597 if (diag || colcount <= 1)
598 colcount = 80;
599
Jason Wessel5d5314d2010-05-20 21:04:20 -0500600 diag = kdbgetintenv("LOGGING", &logging);
601 if (diag)
602 logging = 0;
603
604 if (!kdb_grepping_flag || suspend_grep) {
605 /* normally, every vsnprintf starts a new buffer */
606 next_avail = kdb_buffer;
607 size_avail = sizeof(kdb_buffer);
608 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500609 vsnprintf(next_avail, size_avail, fmt, ap);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500610
611 /*
612 * If kdb_parse() found that the command was cmd xxx | grep yyy
613 * then kdb_grepping_flag is set, and kdb_grep_string contains yyy
614 *
615 * Accumulate the print data up to a newline before searching it.
616 * (vsnprintf does null-terminate the string that it generates)
617 */
618
619 /* skip the search if prints are temporarily unconditional */
620 if (!suspend_grep && kdb_grepping_flag) {
621 cp = strchr(kdb_buffer, '\n');
622 if (!cp) {
623 /*
624 * Special cases that don't end with newlines
625 * but should be written without one:
626 * The "[nn]kdb> " prompt should
627 * appear at the front of the buffer.
628 *
629 * The "[nn]more " prompt should also be
630 * (MOREPROMPT -> moreprompt)
631 * written * but we print that ourselves,
632 * we set the suspend_grep flag to make
633 * it unconditional.
634 *
635 */
636 if (next_avail == kdb_buffer) {
637 /*
638 * these should occur after a newline,
639 * so they will be at the front of the
640 * buffer
641 */
642 cp2 = kdb_buffer;
643 len = strlen(kdb_prompt_str);
644 if (!strncmp(cp2, kdb_prompt_str, len)) {
645 /*
646 * We're about to start a new
647 * command, so we can go back
648 * to normal mode.
649 */
650 kdb_grepping_flag = 0;
651 goto kdb_printit;
652 }
653 }
654 /* no newline; don't search/write the buffer
655 until one is there */
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 /*
663 * The newline is present; print through it or discard
664 * it, depending on the results of the search.
665 */
666 cp++; /* to byte after the newline */
667 replaced_byte = *cp; /* remember what/where it was */
668 cphold = cp;
669 *cp = '\0'; /* end the string for our search */
670
671 /*
672 * We now have a newline at the end of the string
673 * Only continue with this output if it contains the
674 * search string.
675 */
676 fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
677 if (!fnd) {
678 /*
679 * At this point the complete line at the start
680 * of kdb_buffer can be discarded, as it does
681 * not contain what the user is looking for.
682 * Shift the buffer left.
683 */
684 *cphold = replaced_byte;
685 strcpy(kdb_buffer, cphold);
686 len = strlen(kdb_buffer);
687 next_avail = kdb_buffer + len;
688 size_avail = sizeof(kdb_buffer) - len;
689 goto kdb_print_out;
690 }
Daniel Thompsonfb6daa72014-09-11 10:37:10 +0100691 if (kdb_grepping_flag >= KDB_GREPPING_FLAG_SEARCH)
692 /*
693 * This was a interactive search (using '/' at more
694 * prompt) and it has completed. Clear the flag.
695 */
696 kdb_grepping_flag = 0;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500697 /*
698 * at this point the string is a full line and
699 * should be printed, up to the null.
700 */
701 }
702kdb_printit:
703
704 /*
705 * Write to all consoles.
706 */
707 retlen = strlen(kdb_buffer);
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000708 cp = (char *) printk_skip_level(kdb_buffer);
Jason Wessela0de0552010-05-20 21:04:24 -0500709 if (!dbg_kdb_mode && kgdb_connected) {
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000710 gdbstub_msg_write(cp, retlen - (cp - kdb_buffer));
Jason Wessela0de0552010-05-20 21:04:24 -0500711 } else {
Tim Birdb8adde82011-09-21 13:19:12 -0700712 if (dbg_io_ops && !dbg_io_ops->is_console) {
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000713 len = retlen - (cp - kdb_buffer);
714 cp2 = cp;
Jason Wesselefe2f292010-05-20 21:04:26 -0500715 while (len--) {
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000716 dbg_io_ops->write_char(*cp2);
717 cp2++;
Jason Wesselefe2f292010-05-20 21:04:26 -0500718 }
719 }
Jason Wessela0de0552010-05-20 21:04:24 -0500720 while (c) {
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000721 c->write(c, cp, retlen - (cp - kdb_buffer));
Jason Wessela0de0552010-05-20 21:04:24 -0500722 touch_nmi_watchdog();
723 c = c->next;
724 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500725 }
726 if (logging) {
727 saved_loglevel = console_loglevel;
Borislav Petkova8fe19e2014-06-04 16:11:46 -0700728 console_loglevel = CONSOLE_LOGLEVEL_SILENT;
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000729 if (printk_get_level(kdb_buffer) || src == KDB_MSGSRC_PRINTK)
730 printk("%s", kdb_buffer);
731 else
732 pr_info("%s", kdb_buffer);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500733 }
734
Jason Wessel17b572e82012-08-26 22:37:03 -0500735 if (KDB_STATE(PAGER)) {
736 /*
737 * Check printed string to decide how to bump the
738 * kdb_nextline to control when the more prompt should
739 * show up.
740 */
741 int got = 0;
742 len = retlen;
743 while (len--) {
744 if (kdb_buffer[len] == '\n') {
745 kdb_nextline++;
746 got = 0;
747 } else if (kdb_buffer[len] == '\r') {
748 got = 0;
749 } else {
750 got++;
751 }
752 }
753 kdb_nextline += got / (colcount + 1);
754 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500755
756 /* check for having reached the LINES number of printed lines */
Jason Wessel17b572e82012-08-26 22:37:03 -0500757 if (kdb_nextline >= linecount) {
Jason Wessel5d5314d2010-05-20 21:04:20 -0500758 char buf1[16] = "";
Jason Wessel5d5314d2010-05-20 21:04:20 -0500759
760 /* Watch out for recursion here. Any routine that calls
761 * kdb_printf will come back through here. And kdb_read
762 * uses kdb_printf to echo on serial consoles ...
763 */
764 kdb_nextline = 1; /* In case of recursion */
765
766 /*
767 * Pause until cr.
768 */
769 moreprompt = kdbgetenv("MOREPROMPT");
770 if (moreprompt == NULL)
771 moreprompt = "more> ";
772
Jason Wessel5d5314d2010-05-20 21:04:20 -0500773 kdb_input_flush();
774 c = console_drivers;
775
Jason Wessel78724b82012-03-29 06:17:17 -0500776 if (dbg_io_ops && !dbg_io_ops->is_console) {
Jason Wesselefe2f292010-05-20 21:04:26 -0500777 len = strlen(moreprompt);
778 cp = moreprompt;
779 while (len--) {
780 dbg_io_ops->write_char(*cp);
781 cp++;
782 }
783 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500784 while (c) {
785 c->write(c, moreprompt, strlen(moreprompt));
786 touch_nmi_watchdog();
787 c = c->next;
788 }
789
790 if (logging)
791 printk("%s", moreprompt);
792
793 kdb_read(buf1, 2); /* '2' indicates to return
794 * immediately after getting one key. */
795 kdb_nextline = 1; /* Really set output line 1 */
796
797 /* empty and reset the buffer: */
798 kdb_buffer[0] = '\0';
799 next_avail = kdb_buffer;
800 size_avail = sizeof(kdb_buffer);
801 if ((buf1[0] == 'q') || (buf1[0] == 'Q')) {
802 /* user hit q or Q */
803 KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
804 KDB_STATE_CLEAR(PAGER);
805 /* end of command output; back to normal mode */
806 kdb_grepping_flag = 0;
807 kdb_printf("\n");
808 } else if (buf1[0] == ' ') {
Jason Wessel17b572e82012-08-26 22:37:03 -0500809 kdb_printf("\r");
Jason Wessel5d5314d2010-05-20 21:04:20 -0500810 suspend_grep = 1; /* for this recursion */
811 } else if (buf1[0] == '\n') {
812 kdb_nextline = linecount - 1;
813 kdb_printf("\r");
814 suspend_grep = 1; /* for this recursion */
Daniel Thompsonfb6daa72014-09-11 10:37:10 +0100815 } else if (buf1[0] == '/' && !kdb_grepping_flag) {
816 kdb_printf("\r");
817 kdb_getstr(kdb_grep_string, KDB_GREP_STRLEN,
818 kdbgetenv("SEARCHPROMPT") ?: "search> ");
819 *strchrnul(kdb_grep_string, '\n') = '\0';
820 kdb_grepping_flag += KDB_GREPPING_FLAG_SEARCH;
821 suspend_grep = 1; /* for this recursion */
Jason Wessel5d5314d2010-05-20 21:04:20 -0500822 } else if (buf1[0] && buf1[0] != '\n') {
823 /* user hit something other than enter */
824 suspend_grep = 1; /* for this recursion */
Daniel Thompsonfb6daa72014-09-11 10:37:10 +0100825 if (buf1[0] != '/')
826 kdb_printf(
827 "\nOnly 'q', 'Q' or '/' are processed at "
828 "more prompt, input ignored\n");
829 else
830 kdb_printf("\n'/' cannot be used during | "
831 "grep filtering, input ignored\n");
Jason Wessel5d5314d2010-05-20 21:04:20 -0500832 } else if (kdb_grepping_flag) {
833 /* user hit enter */
834 suspend_grep = 1; /* for this recursion */
835 kdb_printf("\n");
836 }
837 kdb_input_flush();
838 }
839
840 /*
841 * For grep searches, shift the printed string left.
842 * replaced_byte contains the character that was overwritten with
843 * the terminating null, and cphold points to the null.
844 * Then adjust the notion of available space in the buffer.
845 */
846 if (kdb_grepping_flag && !suspend_grep) {
847 *cphold = replaced_byte;
848 strcpy(kdb_buffer, cphold);
849 len = strlen(kdb_buffer);
850 next_avail = kdb_buffer + len;
851 size_avail = sizeof(kdb_buffer) - len;
852 }
853
854kdb_print_out:
855 suspend_grep = 0; /* end of what may have been a recursive call */
856 if (logging)
857 console_loglevel = saved_loglevel;
858 if (KDB_STATE(PRINTF_LOCK) && got_printf_lock) {
859 got_printf_lock = 0;
860 spin_unlock_irqrestore(&kdb_printf_lock, flags);
861 KDB_STATE_CLEAR(PRINTF_LOCK);
862 atomic_dec(&kdb_event);
863 } else {
864 __release(kdb_printf_lock);
865 }
Jason Wesseld37d39a2010-05-20 21:04:27 -0500866 kdb_trap_printk = saved_trap_printk;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500867 preempt_enable();
868 return retlen;
869}
Jason Wesseld37d39a2010-05-20 21:04:27 -0500870
871int kdb_printf(const char *fmt, ...)
872{
873 va_list ap;
874 int r;
875
876 va_start(ap, fmt);
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +0000877 r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
Jason Wesseld37d39a2010-05-20 21:04:27 -0500878 va_end(ap);
879
880 return r;
881}
Jason Wesself7030bb2010-10-11 10:20:14 -0500882EXPORT_SYMBOL_GPL(kdb_printf);