• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Kernel Debugger Architecture Independent Console I/O handler
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (c) 1999-2006 Silicon Graphics, Inc.  All Rights Reserved.
9  * Copyright (c) 2009 Wind River Systems, Inc.  All Rights Reserved.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/ctype.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/kdev_t.h>
18 #include <linux/console.h>
19 #include <linux/string.h>
20 #include <linux/sched.h>
21 #include <linux/smp.h>
22 #include <linux/nmi.h>
23 #include <linux/delay.h>
24 #include <linux/kgdb.h>
25 #include <linux/kdb.h>
26 #include <linux/kallsyms.h>
27 #include "kdb_private.h"
28 
29 #define CMD_BUFLEN 256
30 char kdb_prompt_str[CMD_BUFLEN];
31 
32 int kdb_trap_printk;
33 int kdb_printf_cpu = -1;
34 
kgdb_transition_check(char * buffer)35 static int kgdb_transition_check(char *buffer)
36 {
37 	if (buffer[0] != '+' && buffer[0] != '$') {
38 		KDB_STATE_SET(KGDB_TRANS);
39 		kdb_printf("%s", buffer);
40 	} else {
41 		int slen = strlen(buffer);
42 		if (slen > 3 && buffer[slen - 3] == '#') {
43 			kdb_gdb_state_pass(buffer);
44 			strcpy(buffer, "kgdb");
45 			KDB_STATE_SET(DOING_KGDB);
46 			return 1;
47 		}
48 	}
49 	return 0;
50 }
51 
kdb_read_get_key(char * buffer,size_t bufsize)52 static int kdb_read_get_key(char *buffer, size_t bufsize)
53 {
54 #define ESCAPE_UDELAY 1000
55 #define ESCAPE_DELAY (2*1000000/ESCAPE_UDELAY) /* 2 seconds worth of udelays */
56 	char escape_data[5];	/* longest vt100 escape sequence is 4 bytes */
57 	char *ped = escape_data;
58 	int escape_delay = 0;
59 	get_char_func *f, *f_escape = NULL;
60 	int key;
61 
62 	for (f = &kdb_poll_funcs[0]; ; ++f) {
63 		if (*f == NULL) {
64 			/* Reset NMI watchdog once per poll loop */
65 			touch_nmi_watchdog();
66 			f = &kdb_poll_funcs[0];
67 		}
68 		if (escape_delay == 2) {
69 			*ped = '\0';
70 			ped = escape_data;
71 			--escape_delay;
72 		}
73 		if (escape_delay == 1) {
74 			key = *ped++;
75 			if (!*ped)
76 				--escape_delay;
77 			break;
78 		}
79 		key = (*f)();
80 		if (key == -1) {
81 			if (escape_delay) {
82 				udelay(ESCAPE_UDELAY);
83 				--escape_delay;
84 			}
85 			continue;
86 		}
87 		if (bufsize <= 2) {
88 			if (key == '\r')
89 				key = '\n';
90 			*buffer++ = key;
91 			*buffer = '\0';
92 			return -1;
93 		}
94 		if (escape_delay == 0 && key == '\e') {
95 			escape_delay = ESCAPE_DELAY;
96 			ped = escape_data;
97 			f_escape = f;
98 		}
99 		if (escape_delay) {
100 			*ped++ = key;
101 			if (f_escape != f) {
102 				escape_delay = 2;
103 				continue;
104 			}
105 			if (ped - escape_data == 1) {
106 				/* \e */
107 				continue;
108 			} else if (ped - escape_data == 2) {
109 				/* \e<something> */
110 				if (key != '[')
111 					escape_delay = 2;
112 				continue;
113 			} else if (ped - escape_data == 3) {
114 				/* \e[<something> */
115 				int mapkey = 0;
116 				switch (key) {
117 				case 'A': /* \e[A, up arrow */
118 					mapkey = 16;
119 					break;
120 				case 'B': /* \e[B, down arrow */
121 					mapkey = 14;
122 					break;
123 				case 'C': /* \e[C, right arrow */
124 					mapkey = 6;
125 					break;
126 				case 'D': /* \e[D, left arrow */
127 					mapkey = 2;
128 					break;
129 				case '1': /* dropthrough */
130 				case '3': /* dropthrough */
131 				/* \e[<1,3,4>], may be home, del, end */
132 				case '4':
133 					mapkey = -1;
134 					break;
135 				}
136 				if (mapkey != -1) {
137 					if (mapkey > 0) {
138 						escape_data[0] = mapkey;
139 						escape_data[1] = '\0';
140 					}
141 					escape_delay = 2;
142 				}
143 				continue;
144 			} else if (ped - escape_data == 4) {
145 				/* \e[<1,3,4><something> */
146 				int mapkey = 0;
147 				if (key == '~') {
148 					switch (escape_data[2]) {
149 					case '1': /* \e[1~, home */
150 						mapkey = 1;
151 						break;
152 					case '3': /* \e[3~, del */
153 						mapkey = 4;
154 						break;
155 					case '4': /* \e[4~, end */
156 						mapkey = 5;
157 						break;
158 					}
159 				}
160 				if (mapkey > 0) {
161 					escape_data[0] = mapkey;
162 					escape_data[1] = '\0';
163 				}
164 				escape_delay = 2;
165 				continue;
166 			}
167 		}
168 		break;	/* A key to process */
169 	}
170 	return key;
171 }
172 
173 /*
174  * kdb_read
175  *
176  *	This function reads a string of characters, terminated by
177  *	a newline, or by reaching the end of the supplied buffer,
178  *	from the current kernel debugger console device.
179  * Parameters:
180  *	buffer	- Address of character buffer to receive input characters.
181  *	bufsize - size, in bytes, of the character buffer
182  * Returns:
183  *	Returns a pointer to the buffer containing the received
184  *	character string.  This string will be terminated by a
185  *	newline character.
186  * Locking:
187  *	No locks are required to be held upon entry to this
188  *	function.  It is not reentrant - it relies on the fact
189  *	that while kdb is running on only one "master debug" cpu.
190  * Remarks:
191  *
192  * The buffer size must be >= 2.  A buffer size of 2 means that the caller only
193  * wants a single key.
194  *
195  * An escape key could be the start of a vt100 control sequence such as \e[D
196  * (left arrow) or it could be a character in its own right.  The standard
197  * method for detecting the difference is to wait for 2 seconds to see if there
198  * are any other characters.  kdb is complicated by the lack of a timer service
199  * (interrupts are off), by multiple input sources and by the need to sometimes
200  * return after just one key.  Escape sequence processing has to be done as
201  * states in the polling loop.
202  */
203 
kdb_read(char * buffer,size_t bufsize)204 static char *kdb_read(char *buffer, size_t bufsize)
205 {
206 	char *cp = buffer;
207 	char *bufend = buffer+bufsize-2;	/* Reserve space for newline
208 						 * and null byte */
209 	char *lastchar;
210 	char *p_tmp;
211 	char tmp;
212 	static char tmpbuffer[CMD_BUFLEN];
213 	int len = strlen(buffer);
214 	int len_tmp;
215 	int tab = 0;
216 	int count;
217 	int i;
218 	int diag, dtab_count;
219 	int key, buf_size, ret;
220 	static int last_crlf;
221 
222 	diag = kdbgetintenv("DTABCOUNT", &dtab_count);
223 	if (diag)
224 		dtab_count = 30;
225 
226 	if (len > 0) {
227 		cp += len;
228 		if (*(buffer+len-1) == '\n')
229 			cp--;
230 	}
231 
232 	lastchar = cp;
233 	*cp = '\0';
234 	kdb_printf("%s", buffer);
235 poll_again:
236 	key = kdb_read_get_key(buffer, bufsize);
237 	if (key == -1)
238 		return buffer;
239 	if (key != 9)
240 		tab = 0;
241 	if (key != 10 && key != 13)
242 		last_crlf = 0;
243 
244 	switch (key) {
245 	case 8: /* backspace */
246 		if (cp > buffer) {
247 			if (cp < lastchar) {
248 				memcpy(tmpbuffer, cp, lastchar - cp);
249 				memcpy(cp-1, tmpbuffer, lastchar - cp);
250 			}
251 			*(--lastchar) = '\0';
252 			--cp;
253 			kdb_printf("\b%s \r", cp);
254 			tmp = *cp;
255 			*cp = '\0';
256 			kdb_printf(kdb_prompt_str);
257 			kdb_printf("%s", buffer);
258 			*cp = tmp;
259 		}
260 		break;
261 	case 10: /* new line */
262 	case 13: /* carriage return */
263 		/* handle \n after \r */
264 		if (last_crlf && last_crlf != key)
265 			break;
266 		last_crlf = key;
267 		*lastchar++ = '\n';
268 		*lastchar++ = '\0';
269 		if (!KDB_STATE(KGDB_TRANS)) {
270 			KDB_STATE_SET(KGDB_TRANS);
271 			kdb_printf("%s", buffer);
272 		}
273 		kdb_printf("\n");
274 		return buffer;
275 	case 4: /* Del */
276 		if (cp < lastchar) {
277 			memcpy(tmpbuffer, cp+1, lastchar - cp - 1);
278 			memcpy(cp, tmpbuffer, lastchar - cp - 1);
279 			*(--lastchar) = '\0';
280 			kdb_printf("%s \r", cp);
281 			tmp = *cp;
282 			*cp = '\0';
283 			kdb_printf(kdb_prompt_str);
284 			kdb_printf("%s", buffer);
285 			*cp = tmp;
286 		}
287 		break;
288 	case 1: /* Home */
289 		if (cp > buffer) {
290 			kdb_printf("\r");
291 			kdb_printf(kdb_prompt_str);
292 			cp = buffer;
293 		}
294 		break;
295 	case 5: /* End */
296 		if (cp < lastchar) {
297 			kdb_printf("%s", cp);
298 			cp = lastchar;
299 		}
300 		break;
301 	case 2: /* Left */
302 		if (cp > buffer) {
303 			kdb_printf("\b");
304 			--cp;
305 		}
306 		break;
307 	case 14: /* Down */
308 		memset(tmpbuffer, ' ',
309 		       strlen(kdb_prompt_str) + (lastchar-buffer));
310 		*(tmpbuffer+strlen(kdb_prompt_str) +
311 		  (lastchar-buffer)) = '\0';
312 		kdb_printf("\r%s\r", tmpbuffer);
313 		*lastchar = (char)key;
314 		*(lastchar+1) = '\0';
315 		return lastchar;
316 	case 6: /* Right */
317 		if (cp < lastchar) {
318 			kdb_printf("%c", *cp);
319 			++cp;
320 		}
321 		break;
322 	case 16: /* Up */
323 		memset(tmpbuffer, ' ',
324 		       strlen(kdb_prompt_str) + (lastchar-buffer));
325 		*(tmpbuffer+strlen(kdb_prompt_str) +
326 		  (lastchar-buffer)) = '\0';
327 		kdb_printf("\r%s\r", tmpbuffer);
328 		*lastchar = (char)key;
329 		*(lastchar+1) = '\0';
330 		return lastchar;
331 	case 9: /* Tab */
332 		if (tab < 2)
333 			++tab;
334 		p_tmp = buffer;
335 		while (*p_tmp == ' ')
336 			p_tmp++;
337 		if (p_tmp > cp)
338 			break;
339 		memcpy(tmpbuffer, p_tmp, cp-p_tmp);
340 		*(tmpbuffer + (cp-p_tmp)) = '\0';
341 		p_tmp = strrchr(tmpbuffer, ' ');
342 		if (p_tmp)
343 			++p_tmp;
344 		else
345 			p_tmp = tmpbuffer;
346 		len = strlen(p_tmp);
347 		buf_size = sizeof(tmpbuffer) - (p_tmp - tmpbuffer);
348 		count = kallsyms_symbol_complete(p_tmp, buf_size);
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++) {
360 				ret = kallsyms_symbol_next(p_tmp, i, buf_size);
361 				if (WARN_ON(!ret))
362 					break;
363 				if (ret != -E2BIG)
364 					kdb_printf("%s ", p_tmp);
365 				else
366 					kdb_printf("%s... ", p_tmp);
367 				*(p_tmp + len) = '\0';
368 			}
369 			if (i >= dtab_count)
370 				kdb_printf("...");
371 			kdb_printf("\n");
372 			kdb_printf(kdb_prompt_str);
373 			kdb_printf("%s", buffer);
374 		} else if (tab != 2 && count > 0) {
375 			len_tmp = strlen(p_tmp);
376 			strncpy(p_tmp+len_tmp, cp, lastchar-cp+1);
377 			len_tmp = strlen(p_tmp);
378 			strncpy(cp, p_tmp+len, len_tmp-len + 1);
379 			len = len_tmp - len;
380 			kdb_printf("%s", cp);
381 			cp += len;
382 			lastchar += len;
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 */
407 				if (!KDB_STATE(KGDB_TRANS)) {
408 					if (kgdb_transition_check(buffer))
409 						return buffer;
410 				} else {
411 					kdb_printf("%c", key);
412 				}
413 			}
414 			/* Special escape to kgdb */
415 			if (lastchar - buffer >= 5 &&
416 			    strcmp(lastchar - 5, "$?#3f") == 0) {
417 				kdb_gdb_state_pass(lastchar - 5);
418 				strcpy(buffer, "kgdb");
419 				KDB_STATE_SET(DOING_KGDB);
420 				return buffer;
421 			}
422 			if (lastchar - buffer >= 11 &&
423 			    strcmp(lastchar - 11, "$qSupported") == 0) {
424 				kdb_gdb_state_pass(lastchar - 11);
425 				strcpy(buffer, "kgdb");
426 				KDB_STATE_SET(DOING_KGDB);
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 
kdb_getstr(char * buffer,size_t bufsize,const char * prompt)454 char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
455 {
456 	if (prompt && kdb_prompt_str != prompt)
457 		strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
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 
kdb_input_flush(void)480 static 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--;
487 empty:
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 
522 static char kdb_buffer[256];	/* A bit too big to go on stack */
523 static char *next_avail = kdb_buffer;
524 static int  size_avail;
525 static 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  */
kdb_search_string(char * searched,char * searchfor)533 static 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 
vkdb_printf(enum kdb_msgsrc src,const char * fmt,va_list ap)563 int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
564 {
565 	int diag;
566 	int linecount;
567 	int colcount;
568 	int logging, saved_loglevel = 0;
569 	int retlen = 0;
570 	int fnd, len;
571 	int this_cpu, old_cpu;
572 	char *cp, *cp2, *cphold = NULL, replaced_byte = ' ';
573 	char *moreprompt = "more> ";
574 	struct console *c = console_drivers;
575 	unsigned long uninitialized_var(flags);
576 
577 	/* Serialize kdb_printf if multiple cpus try to write at once.
578 	 * But if any cpu goes recursive in kdb, just print the output,
579 	 * even if it is interleaved with any other text.
580 	 */
581 	local_irq_save(flags);
582 	this_cpu = smp_processor_id();
583 	for (;;) {
584 		old_cpu = cmpxchg(&kdb_printf_cpu, -1, this_cpu);
585 		if (old_cpu == -1 || old_cpu == this_cpu)
586 			break;
587 
588 		cpu_relax();
589 	}
590 
591 	diag = kdbgetintenv("LINES", &linecount);
592 	if (diag || linecount <= 1)
593 		linecount = 24;
594 
595 	diag = kdbgetintenv("COLUMNS", &colcount);
596 	if (diag || colcount <= 1)
597 		colcount = 80;
598 
599 	diag = kdbgetintenv("LOGGING", &logging);
600 	if (diag)
601 		logging = 0;
602 
603 	if (!kdb_grepping_flag || suspend_grep) {
604 		/* normally, every vsnprintf starts a new buffer */
605 		next_avail = kdb_buffer;
606 		size_avail = sizeof(kdb_buffer);
607 	}
608 	vsnprintf(next_avail, size_avail, fmt, ap);
609 
610 	/*
611 	 * If kdb_parse() found that the command was cmd xxx | grep yyy
612 	 * then kdb_grepping_flag is set, and kdb_grep_string contains yyy
613 	 *
614 	 * Accumulate the print data up to a newline before searching it.
615 	 * (vsnprintf does null-terminate the string that it generates)
616 	 */
617 
618 	/* skip the search if prints are temporarily unconditional */
619 	if (!suspend_grep && kdb_grepping_flag) {
620 		cp = strchr(kdb_buffer, '\n');
621 		if (!cp) {
622 			/*
623 			 * Special cases that don't end with newlines
624 			 * but should be written without one:
625 			 *   The "[nn]kdb> " prompt should
626 			 *   appear at the front of the buffer.
627 			 *
628 			 *   The "[nn]more " prompt should also be
629 			 *     (MOREPROMPT -> moreprompt)
630 			 *   written *   but we print that ourselves,
631 			 *   we set the suspend_grep flag to make
632 			 *   it unconditional.
633 			 *
634 			 */
635 			if (next_avail == kdb_buffer) {
636 				/*
637 				 * these should occur after a newline,
638 				 * so they will be at the front of the
639 				 * buffer
640 				 */
641 				cp2 = kdb_buffer;
642 				len = strlen(kdb_prompt_str);
643 				if (!strncmp(cp2, kdb_prompt_str, len)) {
644 					/*
645 					 * We're about to start a new
646 					 * command, so we can go back
647 					 * to normal mode.
648 					 */
649 					kdb_grepping_flag = 0;
650 					goto kdb_printit;
651 				}
652 			}
653 			/* no newline; don't search/write the buffer
654 			   until one is there */
655 			len = strlen(kdb_buffer);
656 			next_avail = kdb_buffer + len;
657 			size_avail = sizeof(kdb_buffer) - len;
658 			goto kdb_print_out;
659 		}
660 
661 		/*
662 		 * The newline is present; print through it or discard
663 		 * it, depending on the results of the search.
664 		 */
665 		cp++;	 	     /* to byte after the newline */
666 		replaced_byte = *cp; /* remember what/where it was */
667 		cphold = cp;
668 		*cp = '\0';	     /* end the string for our search */
669 
670 		/*
671 		 * We now have a newline at the end of the string
672 		 * Only continue with this output if it contains the
673 		 * search string.
674 		 */
675 		fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
676 		if (!fnd) {
677 			/*
678 			 * At this point the complete line at the start
679 			 * of kdb_buffer can be discarded, as it does
680 			 * not contain what the user is looking for.
681 			 * Shift the buffer left.
682 			 */
683 			*cphold = replaced_byte;
684 			strcpy(kdb_buffer, cphold);
685 			len = strlen(kdb_buffer);
686 			next_avail = kdb_buffer + len;
687 			size_avail = sizeof(kdb_buffer) - len;
688 			goto kdb_print_out;
689 		}
690 		if (kdb_grepping_flag >= KDB_GREPPING_FLAG_SEARCH)
691 			/*
692 			 * This was a interactive search (using '/' at more
693 			 * prompt) and it has completed. Clear the flag.
694 			 */
695 			kdb_grepping_flag = 0;
696 		/*
697 		 * at this point the string is a full line and
698 		 * should be printed, up to the null.
699 		 */
700 	}
701 kdb_printit:
702 
703 	/*
704 	 * Write to all consoles.
705 	 */
706 	retlen = strlen(kdb_buffer);
707 	cp = (char *) printk_skip_headers(kdb_buffer);
708 	if (!dbg_kdb_mode && kgdb_connected) {
709 		gdbstub_msg_write(cp, retlen - (cp - kdb_buffer));
710 	} else {
711 		if (dbg_io_ops && !dbg_io_ops->is_console) {
712 			len = retlen - (cp - kdb_buffer);
713 			cp2 = cp;
714 			while (len--) {
715 				dbg_io_ops->write_char(*cp2);
716 				cp2++;
717 			}
718 		}
719 		while (c) {
720 			c->write(c, cp, retlen - (cp - kdb_buffer));
721 			touch_nmi_watchdog();
722 			c = c->next;
723 		}
724 	}
725 	if (logging) {
726 		saved_loglevel = console_loglevel;
727 		console_loglevel = CONSOLE_LOGLEVEL_SILENT;
728 		if (printk_get_level(kdb_buffer) || src == KDB_MSGSRC_PRINTK)
729 			printk("%s", kdb_buffer);
730 		else
731 			pr_info("%s", kdb_buffer);
732 	}
733 
734 	if (KDB_STATE(PAGER)) {
735 		/*
736 		 * Check printed string to decide how to bump the
737 		 * kdb_nextline to control when the more prompt should
738 		 * show up.
739 		 */
740 		int got = 0;
741 		len = retlen;
742 		while (len--) {
743 			if (kdb_buffer[len] == '\n') {
744 				kdb_nextline++;
745 				got = 0;
746 			} else if (kdb_buffer[len] == '\r') {
747 				got = 0;
748 			} else {
749 				got++;
750 			}
751 		}
752 		kdb_nextline += got / (colcount + 1);
753 	}
754 
755 	/* check for having reached the LINES number of printed lines */
756 	if (kdb_nextline >= linecount) {
757 		char buf1[16] = "";
758 
759 		/* Watch out for recursion here.  Any routine that calls
760 		 * kdb_printf will come back through here.  And kdb_read
761 		 * uses kdb_printf to echo on serial consoles ...
762 		 */
763 		kdb_nextline = 1;	/* In case of recursion */
764 
765 		/*
766 		 * Pause until cr.
767 		 */
768 		moreprompt = kdbgetenv("MOREPROMPT");
769 		if (moreprompt == NULL)
770 			moreprompt = "more> ";
771 
772 		kdb_input_flush();
773 		c = console_drivers;
774 
775 		if (dbg_io_ops && !dbg_io_ops->is_console) {
776 			len = strlen(moreprompt);
777 			cp = moreprompt;
778 			while (len--) {
779 				dbg_io_ops->write_char(*cp);
780 				cp++;
781 			}
782 		}
783 		while (c) {
784 			c->write(c, moreprompt, strlen(moreprompt));
785 			touch_nmi_watchdog();
786 			c = c->next;
787 		}
788 
789 		if (logging)
790 			printk("%s", moreprompt);
791 
792 		kdb_read(buf1, 2); /* '2' indicates to return
793 				    * immediately after getting one key. */
794 		kdb_nextline = 1;	/* Really set output line 1 */
795 
796 		/* empty and reset the buffer: */
797 		kdb_buffer[0] = '\0';
798 		next_avail = kdb_buffer;
799 		size_avail = sizeof(kdb_buffer);
800 		if ((buf1[0] == 'q') || (buf1[0] == 'Q')) {
801 			/* user hit q or Q */
802 			KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
803 			KDB_STATE_CLEAR(PAGER);
804 			/* end of command output; back to normal mode */
805 			kdb_grepping_flag = 0;
806 			kdb_printf("\n");
807 		} else if (buf1[0] == ' ') {
808 			kdb_printf("\r");
809 			suspend_grep = 1; /* for this recursion */
810 		} else if (buf1[0] == '\n') {
811 			kdb_nextline = linecount - 1;
812 			kdb_printf("\r");
813 			suspend_grep = 1; /* for this recursion */
814 		} else if (buf1[0] == '/' && !kdb_grepping_flag) {
815 			kdb_printf("\r");
816 			kdb_getstr(kdb_grep_string, KDB_GREP_STRLEN,
817 				   kdbgetenv("SEARCHPROMPT") ?: "search> ");
818 			*strchrnul(kdb_grep_string, '\n') = '\0';
819 			kdb_grepping_flag += KDB_GREPPING_FLAG_SEARCH;
820 			suspend_grep = 1; /* for this recursion */
821 		} else if (buf1[0] && buf1[0] != '\n') {
822 			/* user hit something other than enter */
823 			suspend_grep = 1; /* for this recursion */
824 			if (buf1[0] != '/')
825 				kdb_printf(
826 				    "\nOnly 'q', 'Q' or '/' are processed at "
827 				    "more prompt, input ignored\n");
828 			else
829 				kdb_printf("\n'/' cannot be used during | "
830 					   "grep filtering, input ignored\n");
831 		} else if (kdb_grepping_flag) {
832 			/* user hit enter */
833 			suspend_grep = 1; /* for this recursion */
834 			kdb_printf("\n");
835 		}
836 		kdb_input_flush();
837 	}
838 
839 	/*
840 	 * For grep searches, shift the printed string left.
841 	 *  replaced_byte contains the character that was overwritten with
842 	 *  the terminating null, and cphold points to the null.
843 	 * Then adjust the notion of available space in the buffer.
844 	 */
845 	if (kdb_grepping_flag && !suspend_grep) {
846 		*cphold = replaced_byte;
847 		strcpy(kdb_buffer, cphold);
848 		len = strlen(kdb_buffer);
849 		next_avail = kdb_buffer + len;
850 		size_avail = sizeof(kdb_buffer) - len;
851 	}
852 
853 kdb_print_out:
854 	suspend_grep = 0; /* end of what may have been a recursive call */
855 	if (logging)
856 		console_loglevel = saved_loglevel;
857 	/* kdb_printf_cpu locked the code above. */
858 	smp_store_release(&kdb_printf_cpu, old_cpu);
859 	local_irq_restore(flags);
860 	return retlen;
861 }
862 
kdb_printf(const char * fmt,...)863 int kdb_printf(const char *fmt, ...)
864 {
865 	va_list ap;
866 	int r;
867 
868 	va_start(ap, fmt);
869 	r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
870 	va_end(ap);
871 
872 	return r;
873 }
874 EXPORT_SYMBOL_GPL(kdb_printf);
875