kdb_io.c 19 KB

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