kdb_io.c 20 KB

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