kdb_io.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/ctype.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/kdev_t.h>
  17. #include <linux/console.h>
  18. #include <linux/string.h>
  19. #include <linux/sched.h>
  20. #include <linux/smp.h>
  21. #include <linux/nmi.h>
  22. #include <linux/delay.h>
  23. #include <linux/kgdb.h>
  24. #include <linux/kdb.h>
  25. #include <linux/kallsyms.h>
  26. #include "kdb_private.h"
  27. #define CMD_BUFLEN 256
  28. char kdb_prompt_str[CMD_BUFLEN];
  29. int kdb_trap_printk;
  30. static void kgdb_transition_check(char *buffer)
  31. {
  32. int slen = strlen(buffer);
  33. if (strncmp(buffer, "$?#3f", slen) != 0 &&
  34. strncmp(buffer, "$qSupported#37", slen) != 0 &&
  35. strncmp(buffer, "+$qSupported#37", slen) != 0) {
  36. KDB_STATE_SET(KGDB_TRANS);
  37. kdb_printf("%s", buffer);
  38. }
  39. }
  40. static int kdb_read_get_key(char *buffer, size_t bufsize)
  41. {
  42. #define ESCAPE_UDELAY 1000
  43. #define ESCAPE_DELAY (2*1000000/ESCAPE_UDELAY) /* 2 seconds worth of udelays */
  44. char escape_data[5]; /* longest vt100 escape sequence is 4 bytes */
  45. char *ped = escape_data;
  46. int escape_delay = 0;
  47. get_char_func *f, *f_escape = NULL;
  48. int key;
  49. for (f = &kdb_poll_funcs[0]; ; ++f) {
  50. if (*f == NULL) {
  51. /* Reset NMI watchdog once per poll loop */
  52. touch_nmi_watchdog();
  53. f = &kdb_poll_funcs[0];
  54. }
  55. if (escape_delay == 2) {
  56. *ped = '\0';
  57. ped = escape_data;
  58. --escape_delay;
  59. }
  60. if (escape_delay == 1) {
  61. key = *ped++;
  62. if (!*ped)
  63. --escape_delay;
  64. break;
  65. }
  66. key = (*f)();
  67. if (key == -1) {
  68. if (escape_delay) {
  69. udelay(ESCAPE_UDELAY);
  70. --escape_delay;
  71. }
  72. continue;
  73. }
  74. if (bufsize <= 2) {
  75. if (key == '\r')
  76. key = '\n';
  77. *buffer++ = key;
  78. *buffer = '\0';
  79. return -1;
  80. }
  81. if (escape_delay == 0 && key == '\e') {
  82. escape_delay = ESCAPE_DELAY;
  83. ped = escape_data;
  84. f_escape = f;
  85. }
  86. if (escape_delay) {
  87. *ped++ = key;
  88. if (f_escape != f) {
  89. escape_delay = 2;
  90. continue;
  91. }
  92. if (ped - escape_data == 1) {
  93. /* \e */
  94. continue;
  95. } else if (ped - escape_data == 2) {
  96. /* \e<something> */
  97. if (key != '[')
  98. escape_delay = 2;
  99. continue;
  100. } else if (ped - escape_data == 3) {
  101. /* \e[<something> */
  102. int mapkey = 0;
  103. switch (key) {
  104. case 'A': /* \e[A, up arrow */
  105. mapkey = 16;
  106. break;
  107. case 'B': /* \e[B, down arrow */
  108. mapkey = 14;
  109. break;
  110. case 'C': /* \e[C, right arrow */
  111. mapkey = 6;
  112. break;
  113. case 'D': /* \e[D, left arrow */
  114. mapkey = 2;
  115. break;
  116. case '1': /* dropthrough */
  117. case '3': /* dropthrough */
  118. /* \e[<1,3,4>], may be home, del, end */
  119. case '4':
  120. mapkey = -1;
  121. break;
  122. }
  123. if (mapkey != -1) {
  124. if (mapkey > 0) {
  125. escape_data[0] = mapkey;
  126. escape_data[1] = '\0';
  127. }
  128. escape_delay = 2;
  129. }
  130. continue;
  131. } else if (ped - escape_data == 4) {
  132. /* \e[<1,3,4><something> */
  133. int mapkey = 0;
  134. if (key == '~') {
  135. switch (escape_data[2]) {
  136. case '1': /* \e[1~, home */
  137. mapkey = 1;
  138. break;
  139. case '3': /* \e[3~, del */
  140. mapkey = 4;
  141. break;
  142. case '4': /* \e[4~, end */
  143. mapkey = 5;
  144. break;
  145. }
  146. }
  147. if (mapkey > 0) {
  148. escape_data[0] = mapkey;
  149. escape_data[1] = '\0';
  150. }
  151. escape_delay = 2;
  152. continue;
  153. }
  154. }
  155. break; /* A key to process */
  156. }
  157. return key;
  158. }
  159. /*
  160. * kdb_read
  161. *
  162. * This function reads a string of characters, terminated by
  163. * a newline, or by reaching the end of the supplied buffer,
  164. * from the current kernel debugger console device.
  165. * Parameters:
  166. * buffer - Address of character buffer to receive input characters.
  167. * bufsize - size, in bytes, of the character buffer
  168. * Returns:
  169. * Returns a pointer to the buffer containing the received
  170. * character string. This string will be terminated by a
  171. * newline character.
  172. * Locking:
  173. * No locks are required to be held upon entry to this
  174. * function. It is not reentrant - it relies on the fact
  175. * that while kdb is running on only one "master debug" cpu.
  176. * Remarks:
  177. *
  178. * The buffer size must be >= 2. A buffer size of 2 means that the caller only
  179. * wants a single key.
  180. *
  181. * An escape key could be the start of a vt100 control sequence such as \e[D
  182. * (left arrow) or it could be a character in its own right. The standard
  183. * method for detecting the difference is to wait for 2 seconds to see if there
  184. * are any other characters. kdb is complicated by the lack of a timer service
  185. * (interrupts are off), by multiple input sources and by the need to sometimes
  186. * return after just one key. Escape sequence processing has to be done as
  187. * states in the polling loop.
  188. */
  189. static char *kdb_read(char *buffer, size_t bufsize)
  190. {
  191. char *cp = buffer;
  192. char *bufend = buffer+bufsize-2; /* Reserve space for newline
  193. * and null byte */
  194. char *lastchar;
  195. char *p_tmp;
  196. char tmp;
  197. static char tmpbuffer[CMD_BUFLEN];
  198. int len = strlen(buffer);
  199. int len_tmp;
  200. int tab = 0;
  201. int count;
  202. int i;
  203. int diag, dtab_count;
  204. int key;
  205. diag = kdbgetintenv("DTABCOUNT", &dtab_count);
  206. if (diag)
  207. dtab_count = 30;
  208. if (len > 0) {
  209. cp += len;
  210. if (*(buffer+len-1) == '\n')
  211. cp--;
  212. }
  213. lastchar = cp;
  214. *cp = '\0';
  215. kdb_printf("%s", buffer);
  216. poll_again:
  217. key = kdb_read_get_key(buffer, bufsize);
  218. if (key == -1)
  219. return buffer;
  220. if (key != 9)
  221. tab = 0;
  222. switch (key) {
  223. case 8: /* backspace */
  224. if (cp > buffer) {
  225. if (cp < lastchar) {
  226. memcpy(tmpbuffer, cp, lastchar - cp);
  227. memcpy(cp-1, tmpbuffer, lastchar - cp);
  228. }
  229. *(--lastchar) = '\0';
  230. --cp;
  231. kdb_printf("\b%s \r", cp);
  232. tmp = *cp;
  233. *cp = '\0';
  234. kdb_printf(kdb_prompt_str);
  235. kdb_printf("%s", buffer);
  236. *cp = tmp;
  237. }
  238. break;
  239. case 13: /* enter */
  240. *lastchar++ = '\n';
  241. *lastchar++ = '\0';
  242. kdb_printf("\n");
  243. return buffer;
  244. case 4: /* Del */
  245. if (cp < lastchar) {
  246. memcpy(tmpbuffer, cp+1, lastchar - cp - 1);
  247. memcpy(cp, tmpbuffer, lastchar - cp - 1);
  248. *(--lastchar) = '\0';
  249. kdb_printf("%s \r", cp);
  250. tmp = *cp;
  251. *cp = '\0';
  252. kdb_printf(kdb_prompt_str);
  253. kdb_printf("%s", buffer);
  254. *cp = tmp;
  255. }
  256. break;
  257. case 1: /* Home */
  258. if (cp > buffer) {
  259. kdb_printf("\r");
  260. kdb_printf(kdb_prompt_str);
  261. cp = buffer;
  262. }
  263. break;
  264. case 5: /* End */
  265. if (cp < lastchar) {
  266. kdb_printf("%s", cp);
  267. cp = lastchar;
  268. }
  269. break;
  270. case 2: /* Left */
  271. if (cp > buffer) {
  272. kdb_printf("\b");
  273. --cp;
  274. }
  275. break;
  276. case 14: /* Down */
  277. memset(tmpbuffer, ' ',
  278. strlen(kdb_prompt_str) + (lastchar-buffer));
  279. *(tmpbuffer+strlen(kdb_prompt_str) +
  280. (lastchar-buffer)) = '\0';
  281. kdb_printf("\r%s\r", tmpbuffer);
  282. *lastchar = (char)key;
  283. *(lastchar+1) = '\0';
  284. return lastchar;
  285. case 6: /* Right */
  286. if (cp < lastchar) {
  287. kdb_printf("%c", *cp);
  288. ++cp;
  289. }
  290. break;
  291. case 16: /* Up */
  292. memset(tmpbuffer, ' ',
  293. strlen(kdb_prompt_str) + (lastchar-buffer));
  294. *(tmpbuffer+strlen(kdb_prompt_str) +
  295. (lastchar-buffer)) = '\0';
  296. kdb_printf("\r%s\r", tmpbuffer);
  297. *lastchar = (char)key;
  298. *(lastchar+1) = '\0';
  299. return lastchar;
  300. case 9: /* Tab */
  301. if (tab < 2)
  302. ++tab;
  303. p_tmp = buffer;
  304. while (*p_tmp == ' ')
  305. p_tmp++;
  306. if (p_tmp > cp)
  307. break;
  308. memcpy(tmpbuffer, p_tmp, cp-p_tmp);
  309. *(tmpbuffer + (cp-p_tmp)) = '\0';
  310. p_tmp = strrchr(tmpbuffer, ' ');
  311. if (p_tmp)
  312. ++p_tmp;
  313. else
  314. p_tmp = tmpbuffer;
  315. len = strlen(p_tmp);
  316. count = kallsyms_symbol_complete(p_tmp,
  317. sizeof(tmpbuffer) -
  318. (p_tmp - tmpbuffer));
  319. if (tab == 2 && count > 0) {
  320. kdb_printf("\n%d symbols are found.", count);
  321. if (count > dtab_count) {
  322. count = dtab_count;
  323. kdb_printf(" But only first %d symbols will"
  324. " be printed.\nYou can change the"
  325. " environment variable DTABCOUNT.",
  326. count);
  327. }
  328. kdb_printf("\n");
  329. for (i = 0; i < count; i++) {
  330. if (kallsyms_symbol_next(p_tmp, i) < 0)
  331. break;
  332. kdb_printf("%s ", p_tmp);
  333. *(p_tmp + len) = '\0';
  334. }
  335. if (i >= dtab_count)
  336. kdb_printf("...");
  337. kdb_printf("\n");
  338. kdb_printf(kdb_prompt_str);
  339. kdb_printf("%s", buffer);
  340. } else if (tab != 2 && count > 0) {
  341. len_tmp = strlen(p_tmp);
  342. strncpy(p_tmp+len_tmp, cp, lastchar-cp+1);
  343. len_tmp = strlen(p_tmp);
  344. strncpy(cp, p_tmp+len, len_tmp-len + 1);
  345. len = len_tmp - len;
  346. kdb_printf("%s", cp);
  347. cp += len;
  348. lastchar += len;
  349. }
  350. kdb_nextline = 1; /* reset output line number */
  351. break;
  352. default:
  353. if (key >= 32 && lastchar < bufend) {
  354. if (cp < lastchar) {
  355. memcpy(tmpbuffer, cp, lastchar - cp);
  356. memcpy(cp+1, tmpbuffer, lastchar - cp);
  357. *++lastchar = '\0';
  358. *cp = key;
  359. kdb_printf("%s\r", cp);
  360. ++cp;
  361. tmp = *cp;
  362. *cp = '\0';
  363. kdb_printf(kdb_prompt_str);
  364. kdb_printf("%s", buffer);
  365. *cp = tmp;
  366. } else {
  367. *++lastchar = '\0';
  368. *cp++ = key;
  369. /* The kgdb transition check will hide
  370. * printed characters if we think that
  371. * kgdb is connecting, until the check
  372. * fails */
  373. if (!KDB_STATE(KGDB_TRANS))
  374. kgdb_transition_check(buffer);
  375. else
  376. kdb_printf("%c", key);
  377. }
  378. /* Special escape to kgdb */
  379. if (lastchar - buffer >= 5 &&
  380. strcmp(lastchar - 5, "$?#3f") == 0) {
  381. strcpy(buffer, "kgdb");
  382. KDB_STATE_SET(DOING_KGDB);
  383. return buffer;
  384. }
  385. if (lastchar - buffer >= 14 &&
  386. strcmp(lastchar - 14, "$qSupported#37") == 0) {
  387. strcpy(buffer, "kgdb");
  388. KDB_STATE_SET(DOING_KGDB2);
  389. return buffer;
  390. }
  391. }
  392. break;
  393. }
  394. goto poll_again;
  395. }
  396. /*
  397. * kdb_getstr
  398. *
  399. * Print the prompt string and read a command from the
  400. * input device.
  401. *
  402. * Parameters:
  403. * buffer Address of buffer to receive command
  404. * bufsize Size of buffer in bytes
  405. * prompt Pointer to string to use as prompt string
  406. * Returns:
  407. * Pointer to command buffer.
  408. * Locking:
  409. * None.
  410. * Remarks:
  411. * For SMP kernels, the processor number will be
  412. * substituted for %d, %x or %o in the prompt.
  413. */
  414. char *kdb_getstr(char *buffer, size_t bufsize, char *prompt)
  415. {
  416. if (prompt && kdb_prompt_str != prompt)
  417. strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
  418. kdb_printf(kdb_prompt_str);
  419. kdb_nextline = 1; /* Prompt and input resets line number */
  420. return kdb_read(buffer, bufsize);
  421. }
  422. /*
  423. * kdb_input_flush
  424. *
  425. * Get rid of any buffered console input.
  426. *
  427. * Parameters:
  428. * none
  429. * Returns:
  430. * nothing
  431. * Locking:
  432. * none
  433. * Remarks:
  434. * Call this function whenever you want to flush input. If there is any
  435. * outstanding input, it ignores all characters until there has been no
  436. * data for approximately 1ms.
  437. */
  438. static void kdb_input_flush(void)
  439. {
  440. get_char_func *f;
  441. int res;
  442. int flush_delay = 1;
  443. while (flush_delay) {
  444. flush_delay--;
  445. empty:
  446. touch_nmi_watchdog();
  447. for (f = &kdb_poll_funcs[0]; *f; ++f) {
  448. res = (*f)();
  449. if (res != -1) {
  450. flush_delay = 1;
  451. goto empty;
  452. }
  453. }
  454. if (flush_delay)
  455. mdelay(1);
  456. }
  457. }
  458. /*
  459. * kdb_printf
  460. *
  461. * Print a string to the output device(s).
  462. *
  463. * Parameters:
  464. * printf-like format and optional args.
  465. * Returns:
  466. * 0
  467. * Locking:
  468. * None.
  469. * Remarks:
  470. * use 'kdbcons->write()' to avoid polluting 'log_buf' with
  471. * kdb output.
  472. *
  473. * If the user is doing a cmd args | grep srch
  474. * then kdb_grepping_flag is set.
  475. * In that case we need to accumulate full lines (ending in \n) before
  476. * searching for the pattern.
  477. */
  478. static char kdb_buffer[256]; /* A bit too big to go on stack */
  479. static char *next_avail = kdb_buffer;
  480. static int size_avail;
  481. static int suspend_grep;
  482. /*
  483. * search arg1 to see if it contains arg2
  484. * (kdmain.c provides flags for ^pat and pat$)
  485. *
  486. * return 1 for found, 0 for not found
  487. */
  488. static int kdb_search_string(char *searched, char *searchfor)
  489. {
  490. char firstchar, *cp;
  491. int len1, len2;
  492. /* not counting the newline at the end of "searched" */
  493. len1 = strlen(searched)-1;
  494. len2 = strlen(searchfor);
  495. if (len1 < len2)
  496. return 0;
  497. if (kdb_grep_leading && kdb_grep_trailing && len1 != len2)
  498. return 0;
  499. if (kdb_grep_leading) {
  500. if (!strncmp(searched, searchfor, len2))
  501. return 1;
  502. } else if (kdb_grep_trailing) {
  503. if (!strncmp(searched+len1-len2, searchfor, len2))
  504. return 1;
  505. } else {
  506. firstchar = *searchfor;
  507. cp = searched;
  508. while ((cp = strchr(cp, firstchar))) {
  509. if (!strncmp(cp, searchfor, len2))
  510. return 1;
  511. cp++;
  512. }
  513. }
  514. return 0;
  515. }
  516. int vkdb_printf(const char *fmt, va_list ap)
  517. {
  518. int diag;
  519. int linecount;
  520. int logging, saved_loglevel = 0;
  521. int saved_trap_printk;
  522. int got_printf_lock = 0;
  523. int retlen = 0;
  524. int fnd, len;
  525. char *cp, *cp2, *cphold = NULL, replaced_byte = ' ';
  526. char *moreprompt = "more> ";
  527. struct console *c = console_drivers;
  528. static DEFINE_SPINLOCK(kdb_printf_lock);
  529. unsigned long uninitialized_var(flags);
  530. preempt_disable();
  531. saved_trap_printk = kdb_trap_printk;
  532. kdb_trap_printk = 0;
  533. /* Serialize kdb_printf if multiple cpus try to write at once.
  534. * But if any cpu goes recursive in kdb, just print the output,
  535. * even if it is interleaved with any other text.
  536. */
  537. if (!KDB_STATE(PRINTF_LOCK)) {
  538. KDB_STATE_SET(PRINTF_LOCK);
  539. spin_lock_irqsave(&kdb_printf_lock, flags);
  540. got_printf_lock = 1;
  541. atomic_inc(&kdb_event);
  542. } else {
  543. __acquire(kdb_printf_lock);
  544. }
  545. diag = kdbgetintenv("LINES", &linecount);
  546. if (diag || linecount <= 1)
  547. linecount = 24;
  548. diag = kdbgetintenv("LOGGING", &logging);
  549. if (diag)
  550. logging = 0;
  551. if (!kdb_grepping_flag || suspend_grep) {
  552. /* normally, every vsnprintf starts a new buffer */
  553. next_avail = kdb_buffer;
  554. size_avail = sizeof(kdb_buffer);
  555. }
  556. vsnprintf(next_avail, size_avail, fmt, ap);
  557. /*
  558. * If kdb_parse() found that the command was cmd xxx | grep yyy
  559. * then kdb_grepping_flag is set, and kdb_grep_string contains yyy
  560. *
  561. * Accumulate the print data up to a newline before searching it.
  562. * (vsnprintf does null-terminate the string that it generates)
  563. */
  564. /* skip the search if prints are temporarily unconditional */
  565. if (!suspend_grep && kdb_grepping_flag) {
  566. cp = strchr(kdb_buffer, '\n');
  567. if (!cp) {
  568. /*
  569. * Special cases that don't end with newlines
  570. * but should be written without one:
  571. * The "[nn]kdb> " prompt should
  572. * appear at the front of the buffer.
  573. *
  574. * The "[nn]more " prompt should also be
  575. * (MOREPROMPT -> moreprompt)
  576. * written * but we print that ourselves,
  577. * we set the suspend_grep flag to make
  578. * it unconditional.
  579. *
  580. */
  581. if (next_avail == kdb_buffer) {
  582. /*
  583. * these should occur after a newline,
  584. * so they will be at the front of the
  585. * buffer
  586. */
  587. cp2 = kdb_buffer;
  588. len = strlen(kdb_prompt_str);
  589. if (!strncmp(cp2, kdb_prompt_str, len)) {
  590. /*
  591. * We're about to start a new
  592. * command, so we can go back
  593. * to normal mode.
  594. */
  595. kdb_grepping_flag = 0;
  596. goto kdb_printit;
  597. }
  598. }
  599. /* no newline; don't search/write the buffer
  600. until one is there */
  601. len = strlen(kdb_buffer);
  602. next_avail = kdb_buffer + len;
  603. size_avail = sizeof(kdb_buffer) - len;
  604. goto kdb_print_out;
  605. }
  606. /*
  607. * The newline is present; print through it or discard
  608. * it, depending on the results of the search.
  609. */
  610. cp++; /* to byte after the newline */
  611. replaced_byte = *cp; /* remember what/where it was */
  612. cphold = cp;
  613. *cp = '\0'; /* end the string for our search */
  614. /*
  615. * We now have a newline at the end of the string
  616. * Only continue with this output if it contains the
  617. * search string.
  618. */
  619. fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
  620. if (!fnd) {
  621. /*
  622. * At this point the complete line at the start
  623. * of kdb_buffer can be discarded, as it does
  624. * not contain what the user is looking for.
  625. * Shift the buffer left.
  626. */
  627. *cphold = replaced_byte;
  628. strcpy(kdb_buffer, cphold);
  629. len = strlen(kdb_buffer);
  630. next_avail = kdb_buffer + len;
  631. size_avail = sizeof(kdb_buffer) - len;
  632. goto kdb_print_out;
  633. }
  634. /*
  635. * at this point the string is a full line and
  636. * should be printed, up to the null.
  637. */
  638. }
  639. kdb_printit:
  640. /*
  641. * Write to all consoles.
  642. */
  643. retlen = strlen(kdb_buffer);
  644. if (!dbg_kdb_mode && kgdb_connected) {
  645. gdbstub_msg_write(kdb_buffer, retlen);
  646. } else {
  647. if (!dbg_io_ops->is_console) {
  648. len = strlen(kdb_buffer);
  649. cp = kdb_buffer;
  650. while (len--) {
  651. dbg_io_ops->write_char(*cp);
  652. cp++;
  653. }
  654. }
  655. while (c) {
  656. c->write(c, kdb_buffer, retlen);
  657. touch_nmi_watchdog();
  658. c = c->next;
  659. }
  660. }
  661. if (logging) {
  662. saved_loglevel = console_loglevel;
  663. console_loglevel = 0;
  664. printk(KERN_INFO "%s", kdb_buffer);
  665. }
  666. if (KDB_STATE(PAGER) && strchr(kdb_buffer, '\n'))
  667. kdb_nextline++;
  668. /* check for having reached the LINES number of printed lines */
  669. if (kdb_nextline == linecount) {
  670. char buf1[16] = "";
  671. #if defined(CONFIG_SMP)
  672. char buf2[32];
  673. #endif
  674. /* Watch out for recursion here. Any routine that calls
  675. * kdb_printf will come back through here. And kdb_read
  676. * uses kdb_printf to echo on serial consoles ...
  677. */
  678. kdb_nextline = 1; /* In case of recursion */
  679. /*
  680. * Pause until cr.
  681. */
  682. moreprompt = kdbgetenv("MOREPROMPT");
  683. if (moreprompt == NULL)
  684. moreprompt = "more> ";
  685. #if defined(CONFIG_SMP)
  686. if (strchr(moreprompt, '%')) {
  687. sprintf(buf2, moreprompt, get_cpu());
  688. put_cpu();
  689. moreprompt = buf2;
  690. }
  691. #endif
  692. kdb_input_flush();
  693. c = console_drivers;
  694. if (!dbg_io_ops->is_console) {
  695. len = strlen(moreprompt);
  696. cp = moreprompt;
  697. while (len--) {
  698. dbg_io_ops->write_char(*cp);
  699. cp++;
  700. }
  701. }
  702. while (c) {
  703. c->write(c, moreprompt, strlen(moreprompt));
  704. touch_nmi_watchdog();
  705. c = c->next;
  706. }
  707. if (logging)
  708. printk("%s", moreprompt);
  709. kdb_read(buf1, 2); /* '2' indicates to return
  710. * immediately after getting one key. */
  711. kdb_nextline = 1; /* Really set output line 1 */
  712. /* empty and reset the buffer: */
  713. kdb_buffer[0] = '\0';
  714. next_avail = kdb_buffer;
  715. size_avail = sizeof(kdb_buffer);
  716. if ((buf1[0] == 'q') || (buf1[0] == 'Q')) {
  717. /* user hit q or Q */
  718. KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
  719. KDB_STATE_CLEAR(PAGER);
  720. /* end of command output; back to normal mode */
  721. kdb_grepping_flag = 0;
  722. kdb_printf("\n");
  723. } else if (buf1[0] == ' ') {
  724. kdb_printf("\n");
  725. suspend_grep = 1; /* for this recursion */
  726. } else if (buf1[0] == '\n') {
  727. kdb_nextline = linecount - 1;
  728. kdb_printf("\r");
  729. suspend_grep = 1; /* for this recursion */
  730. } else if (buf1[0] && buf1[0] != '\n') {
  731. /* user hit something other than enter */
  732. suspend_grep = 1; /* for this recursion */
  733. kdb_printf("\nOnly 'q' or 'Q' are processed at more "
  734. "prompt, input ignored\n");
  735. } else if (kdb_grepping_flag) {
  736. /* user hit enter */
  737. suspend_grep = 1; /* for this recursion */
  738. kdb_printf("\n");
  739. }
  740. kdb_input_flush();
  741. }
  742. /*
  743. * For grep searches, shift the printed string left.
  744. * replaced_byte contains the character that was overwritten with
  745. * the terminating null, and cphold points to the null.
  746. * Then adjust the notion of available space in the buffer.
  747. */
  748. if (kdb_grepping_flag && !suspend_grep) {
  749. *cphold = replaced_byte;
  750. strcpy(kdb_buffer, cphold);
  751. len = strlen(kdb_buffer);
  752. next_avail = kdb_buffer + len;
  753. size_avail = sizeof(kdb_buffer) - len;
  754. }
  755. kdb_print_out:
  756. suspend_grep = 0; /* end of what may have been a recursive call */
  757. if (logging)
  758. console_loglevel = saved_loglevel;
  759. if (KDB_STATE(PRINTF_LOCK) && got_printf_lock) {
  760. got_printf_lock = 0;
  761. spin_unlock_irqrestore(&kdb_printf_lock, flags);
  762. KDB_STATE_CLEAR(PRINTF_LOCK);
  763. atomic_dec(&kdb_event);
  764. } else {
  765. __release(kdb_printf_lock);
  766. }
  767. kdb_trap_printk = saved_trap_printk;
  768. preempt_enable();
  769. return retlen;
  770. }
  771. int kdb_printf(const char *fmt, ...)
  772. {
  773. va_list ap;
  774. int r;
  775. va_start(ap, fmt);
  776. r = vkdb_printf(fmt, ap);
  777. va_end(ap);
  778. return r;
  779. }
  780. EXPORT_SYMBOL_GPL(kdb_printf);