• 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 
kgdb_transition_check(char * buffer)34 static int kgdb_transition_check(char *buffer)
35 {
36 	if (buffer[0] != '+' && buffer[0] != '$') {
37 		KDB_STATE_SET(KGDB_TRANS);
38 		kdb_printf("%s", buffer);
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 		}
47 	}
48 	return 0;
49 }
50 
kdb_read_get_key(char * buffer,size_t bufsize)51 static 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 
kdb_read(char * buffer,size_t bufsize)203 static 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;
219 	static int last_crlf;
220 
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);
234 poll_again:
235 	key = kdb_read_get_key(buffer, bufsize);
236 	if (key == -1)
237 		return buffer;
238 	if (key != 9)
239 		tab = 0;
240 	if (key != 10 && key != 13)
241 		last_crlf = 0;
242 
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;
260 	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;
266 		*lastchar++ = '\n';
267 		*lastchar++ = '\0';
268 		if (!KDB_STATE(KGDB_TRANS)) {
269 			KDB_STATE_SET(KGDB_TRANS);
270 			kdb_printf("%s", buffer);
271 		}
272 		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++) {
360 				if (WARN_ON(!kallsyms_symbol_next(p_tmp, i)))
361 					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 */
403 				if (!KDB_STATE(KGDB_TRANS)) {
404 					if (kgdb_transition_check(buffer))
405 						return buffer;
406 				} else {
407 					kdb_printf("%c", key);
408 				}
409 			}
410 			/* Special escape to kgdb */
411 			if (lastchar - buffer >= 5 &&
412 			    strcmp(lastchar - 5, "$?#3f") == 0) {
413 				kdb_gdb_state_pass(lastchar - 5);
414 				strcpy(buffer, "kgdb");
415 				KDB_STATE_SET(DOING_KGDB);
416 				return buffer;
417 			}
418 			if (lastchar - buffer >= 11 &&
419 			    strcmp(lastchar - 11, "$qSupported") == 0) {
420 				kdb_gdb_state_pass(lastchar - 11);
421 				strcpy(buffer, "kgdb");
422 				KDB_STATE_SET(DOING_KGDB);
423 				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 
kdb_getstr(char * buffer,size_t bufsize,char * prompt)450 char *kdb_getstr(char *buffer, size_t bufsize, char *prompt)
451 {
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 
kdb_input_flush(void)476 static 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--;
483 empty:
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 
518 static char kdb_buffer[256];	/* A bit too big to go on stack */
519 static char *next_avail = kdb_buffer;
520 static int  size_avail;
521 static 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  */
kdb_search_string(char * searched,char * searchfor)529 static 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 
vkdb_printf(const char * fmt,va_list ap)559 int vkdb_printf(const char *fmt, va_list ap)
560 {
561 	int diag;
562 	int linecount;
563 	int colcount;
564 	int logging, saved_loglevel = 0;
565 	int saved_trap_printk;
566 	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();
576 	saved_trap_printk = kdb_trap_printk;
577 	kdb_trap_printk = 0;
578 
579 	/* 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 
596 	diag = kdbgetintenv("COLUMNS", &colcount);
597 	if (diag || colcount <= 1)
598 		colcount = 80;
599 
600 	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 	}
609 	vsnprintf(next_avail, size_avail, fmt, ap);
610 
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 		}
691 		/*
692 		 * at this point the string is a full line and
693 		 * should be printed, up to the null.
694 		 */
695 	}
696 kdb_printit:
697 
698 	/*
699 	 * Write to all consoles.
700 	 */
701 	retlen = strlen(kdb_buffer);
702 	if (!dbg_kdb_mode && kgdb_connected) {
703 		gdbstub_msg_write(kdb_buffer, retlen);
704 	} else {
705 		if (dbg_io_ops && !dbg_io_ops->is_console) {
706 			len = retlen;
707 			cp = kdb_buffer;
708 			while (len--) {
709 				dbg_io_ops->write_char(*cp);
710 				cp++;
711 			}
712 		}
713 		while (c) {
714 			c->write(c, kdb_buffer, retlen);
715 			touch_nmi_watchdog();
716 			c = c->next;
717 		}
718 	}
719 	if (logging) {
720 		saved_loglevel = console_loglevel;
721 		console_loglevel = CONSOLE_LOGLEVEL_SILENT;
722 		printk(KERN_INFO "%s", kdb_buffer);
723 	}
724 
725 	if (KDB_STATE(PAGER)) {
726 		/*
727 		 * Check printed string to decide how to bump the
728 		 * kdb_nextline to control when the more prompt should
729 		 * show up.
730 		 */
731 		int got = 0;
732 		len = retlen;
733 		while (len--) {
734 			if (kdb_buffer[len] == '\n') {
735 				kdb_nextline++;
736 				got = 0;
737 			} else if (kdb_buffer[len] == '\r') {
738 				got = 0;
739 			} else {
740 				got++;
741 			}
742 		}
743 		kdb_nextline += got / (colcount + 1);
744 	}
745 
746 	/* check for having reached the LINES number of printed lines */
747 	if (kdb_nextline >= linecount) {
748 		char buf1[16] = "";
749 
750 		/* Watch out for recursion here.  Any routine that calls
751 		 * kdb_printf will come back through here.  And kdb_read
752 		 * uses kdb_printf to echo on serial consoles ...
753 		 */
754 		kdb_nextline = 1;	/* In case of recursion */
755 
756 		/*
757 		 * Pause until cr.
758 		 */
759 		moreprompt = kdbgetenv("MOREPROMPT");
760 		if (moreprompt == NULL)
761 			moreprompt = "more> ";
762 
763 		kdb_input_flush();
764 		c = console_drivers;
765 
766 		if (dbg_io_ops && !dbg_io_ops->is_console) {
767 			len = strlen(moreprompt);
768 			cp = moreprompt;
769 			while (len--) {
770 				dbg_io_ops->write_char(*cp);
771 				cp++;
772 			}
773 		}
774 		while (c) {
775 			c->write(c, moreprompt, strlen(moreprompt));
776 			touch_nmi_watchdog();
777 			c = c->next;
778 		}
779 
780 		if (logging)
781 			printk("%s", moreprompt);
782 
783 		kdb_read(buf1, 2); /* '2' indicates to return
784 				    * immediately after getting one key. */
785 		kdb_nextline = 1;	/* Really set output line 1 */
786 
787 		/* empty and reset the buffer: */
788 		kdb_buffer[0] = '\0';
789 		next_avail = kdb_buffer;
790 		size_avail = sizeof(kdb_buffer);
791 		if ((buf1[0] == 'q') || (buf1[0] == 'Q')) {
792 			/* user hit q or Q */
793 			KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
794 			KDB_STATE_CLEAR(PAGER);
795 			/* end of command output; back to normal mode */
796 			kdb_grepping_flag = 0;
797 			kdb_printf("\n");
798 		} else if (buf1[0] == ' ') {
799 			kdb_printf("\r");
800 			suspend_grep = 1; /* for this recursion */
801 		} else if (buf1[0] == '\n') {
802 			kdb_nextline = linecount - 1;
803 			kdb_printf("\r");
804 			suspend_grep = 1; /* for this recursion */
805 		} else if (buf1[0] && buf1[0] != '\n') {
806 			/* user hit something other than enter */
807 			suspend_grep = 1; /* for this recursion */
808 			kdb_printf("\nOnly 'q' or 'Q' are processed at more "
809 				   "prompt, input ignored\n");
810 		} else if (kdb_grepping_flag) {
811 			/* user hit enter */
812 			suspend_grep = 1; /* for this recursion */
813 			kdb_printf("\n");
814 		}
815 		kdb_input_flush();
816 	}
817 
818 	/*
819 	 * For grep searches, shift the printed string left.
820 	 *  replaced_byte contains the character that was overwritten with
821 	 *  the terminating null, and cphold points to the null.
822 	 * Then adjust the notion of available space in the buffer.
823 	 */
824 	if (kdb_grepping_flag && !suspend_grep) {
825 		*cphold = replaced_byte;
826 		strcpy(kdb_buffer, cphold);
827 		len = strlen(kdb_buffer);
828 		next_avail = kdb_buffer + len;
829 		size_avail = sizeof(kdb_buffer) - len;
830 	}
831 
832 kdb_print_out:
833 	suspend_grep = 0; /* end of what may have been a recursive call */
834 	if (logging)
835 		console_loglevel = saved_loglevel;
836 	if (KDB_STATE(PRINTF_LOCK) && got_printf_lock) {
837 		got_printf_lock = 0;
838 		spin_unlock_irqrestore(&kdb_printf_lock, flags);
839 		KDB_STATE_CLEAR(PRINTF_LOCK);
840 		atomic_dec(&kdb_event);
841 	} else {
842 		__release(kdb_printf_lock);
843 	}
844 	kdb_trap_printk = saved_trap_printk;
845 	preempt_enable();
846 	return retlen;
847 }
848 
kdb_printf(const char * fmt,...)849 int kdb_printf(const char *fmt, ...)
850 {
851 	va_list ap;
852 	int r;
853 
854 	va_start(ap, fmt);
855 	r = vkdb_printf(fmt, ap);
856 	va_end(ap);
857 
858 	return r;
859 }
860 EXPORT_SYMBOL_GPL(kdb_printf);
861