kdb_io.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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, buf_size, ret;
  211. diag = kdbgetintenv("DTABCOUNT", &dtab_count);
  212. if (diag)
  213. dtab_count = 30;
  214. if (len > 0) {
  215. cp += len;
  216. if (*(buffer+len-1) == '\n')
  217. cp--;
  218. }
  219. lastchar = cp;
  220. *cp = '\0';
  221. kdb_printf("%s", buffer);
  222. poll_again:
  223. key = kdb_read_get_key(buffer, bufsize);
  224. if (key == -1)
  225. return buffer;
  226. if (key != 9)
  227. tab = 0;
  228. switch (key) {
  229. case 8: /* backspace */
  230. if (cp > buffer) {
  231. if (cp < lastchar) {
  232. memcpy(tmpbuffer, cp, lastchar - cp);
  233. memcpy(cp-1, tmpbuffer, lastchar - cp);
  234. }
  235. *(--lastchar) = '\0';
  236. --cp;
  237. kdb_printf("\b%s \r", cp);
  238. tmp = *cp;
  239. *cp = '\0';
  240. kdb_printf(kdb_prompt_str);
  241. kdb_printf("%s", buffer);
  242. *cp = tmp;
  243. }
  244. break;
  245. case 13: /* enter */
  246. *lastchar++ = '\n';
  247. *lastchar++ = '\0';
  248. if (!KDB_STATE(KGDB_TRANS)) {
  249. KDB_STATE_SET(KGDB_TRANS);
  250. kdb_printf("%s", buffer);
  251. }
  252. kdb_printf("\n");
  253. return buffer;
  254. case 4: /* Del */
  255. if (cp < lastchar) {
  256. memcpy(tmpbuffer, cp+1, lastchar - cp - 1);
  257. memcpy(cp, tmpbuffer, lastchar - cp - 1);
  258. *(--lastchar) = '\0';
  259. kdb_printf("%s \r", cp);
  260. tmp = *cp;
  261. *cp = '\0';
  262. kdb_printf(kdb_prompt_str);
  263. kdb_printf("%s", buffer);
  264. *cp = tmp;
  265. }
  266. break;
  267. case 1: /* Home */
  268. if (cp > buffer) {
  269. kdb_printf("\r");
  270. kdb_printf(kdb_prompt_str);
  271. cp = buffer;
  272. }
  273. break;
  274. case 5: /* End */
  275. if (cp < lastchar) {
  276. kdb_printf("%s", cp);
  277. cp = lastchar;
  278. }
  279. break;
  280. case 2: /* Left */
  281. if (cp > buffer) {
  282. kdb_printf("\b");
  283. --cp;
  284. }
  285. break;
  286. case 14: /* Down */
  287. memset(tmpbuffer, ' ',
  288. strlen(kdb_prompt_str) + (lastchar-buffer));
  289. *(tmpbuffer+strlen(kdb_prompt_str) +
  290. (lastchar-buffer)) = '\0';
  291. kdb_printf("\r%s\r", tmpbuffer);
  292. *lastchar = (char)key;
  293. *(lastchar+1) = '\0';
  294. return lastchar;
  295. case 6: /* Right */
  296. if (cp < lastchar) {
  297. kdb_printf("%c", *cp);
  298. ++cp;
  299. }
  300. break;
  301. case 16: /* Up */
  302. memset(tmpbuffer, ' ',
  303. strlen(kdb_prompt_str) + (lastchar-buffer));
  304. *(tmpbuffer+strlen(kdb_prompt_str) +
  305. (lastchar-buffer)) = '\0';
  306. kdb_printf("\r%s\r", tmpbuffer);
  307. *lastchar = (char)key;
  308. *(lastchar+1) = '\0';
  309. return lastchar;
  310. case 9: /* Tab */
  311. if (tab < 2)
  312. ++tab;
  313. p_tmp = buffer;
  314. while (*p_tmp == ' ')
  315. p_tmp++;
  316. if (p_tmp > cp)
  317. break;
  318. memcpy(tmpbuffer, p_tmp, cp-p_tmp);
  319. *(tmpbuffer + (cp-p_tmp)) = '\0';
  320. p_tmp = strrchr(tmpbuffer, ' ');
  321. if (p_tmp)
  322. ++p_tmp;
  323. else
  324. p_tmp = tmpbuffer;
  325. len = strlen(p_tmp);
  326. buf_size = sizeof(tmpbuffer) - (p_tmp - tmpbuffer);
  327. count = kallsyms_symbol_complete(p_tmp, buf_size);
  328. if (tab == 2 && count > 0) {
  329. kdb_printf("\n%d symbols are found.", count);
  330. if (count > dtab_count) {
  331. count = dtab_count;
  332. kdb_printf(" But only first %d symbols will"
  333. " be printed.\nYou can change the"
  334. " environment variable DTABCOUNT.",
  335. count);
  336. }
  337. kdb_printf("\n");
  338. for (i = 0; i < count; i++) {
  339. ret = kallsyms_symbol_next(p_tmp, i, buf_size);
  340. if (WARN_ON(!ret))
  341. break;
  342. if (ret != -E2BIG)
  343. kdb_printf("%s ", p_tmp);
  344. else
  345. kdb_printf("%s... ", p_tmp);
  346. *(p_tmp + len) = '\0';
  347. }
  348. if (i >= dtab_count)
  349. kdb_printf("...");
  350. kdb_printf("\n");
  351. kdb_printf(kdb_prompt_str);
  352. kdb_printf("%s", buffer);
  353. } else if (tab != 2 && count > 0) {
  354. len_tmp = strlen(p_tmp);
  355. strncpy(p_tmp+len_tmp, cp, lastchar-cp+1);
  356. len_tmp = strlen(p_tmp);
  357. strncpy(cp, p_tmp+len, len_tmp-len + 1);
  358. len = len_tmp - len;
  359. kdb_printf("%s", cp);
  360. cp += len;
  361. lastchar += len;
  362. }
  363. kdb_nextline = 1; /* reset output line number */
  364. break;
  365. default:
  366. if (key >= 32 && lastchar < bufend) {
  367. if (cp < lastchar) {
  368. memcpy(tmpbuffer, cp, lastchar - cp);
  369. memcpy(cp+1, tmpbuffer, lastchar - cp);
  370. *++lastchar = '\0';
  371. *cp = key;
  372. kdb_printf("%s\r", cp);
  373. ++cp;
  374. tmp = *cp;
  375. *cp = '\0';
  376. kdb_printf(kdb_prompt_str);
  377. kdb_printf("%s", buffer);
  378. *cp = tmp;
  379. } else {
  380. *++lastchar = '\0';
  381. *cp++ = key;
  382. /* The kgdb transition check will hide
  383. * printed characters if we think that
  384. * kgdb is connecting, until the check
  385. * fails */
  386. if (!KDB_STATE(KGDB_TRANS)) {
  387. if (kgdb_transition_check(buffer))
  388. return buffer;
  389. } else {
  390. kdb_printf("%c", key);
  391. }
  392. }
  393. /* Special escape to kgdb */
  394. if (lastchar - buffer >= 5 &&
  395. strcmp(lastchar - 5, "$?#3f") == 0) {
  396. kdb_gdb_state_pass(lastchar - 5);
  397. strcpy(buffer, "kgdb");
  398. KDB_STATE_SET(DOING_KGDB);
  399. return buffer;
  400. }
  401. if (lastchar - buffer >= 11 &&
  402. strcmp(lastchar - 11, "$qSupported") == 0) {
  403. kdb_gdb_state_pass(lastchar - 11);
  404. strcpy(buffer, "kgdb");
  405. KDB_STATE_SET(DOING_KGDB);
  406. return buffer;
  407. }
  408. }
  409. break;
  410. }
  411. goto poll_again;
  412. }
  413. /*
  414. * kdb_getstr
  415. *
  416. * Print the prompt string and read a command from the
  417. * input device.
  418. *
  419. * Parameters:
  420. * buffer Address of buffer to receive command
  421. * bufsize Size of buffer in bytes
  422. * prompt Pointer to string to use as prompt string
  423. * Returns:
  424. * Pointer to command buffer.
  425. * Locking:
  426. * None.
  427. * Remarks:
  428. * For SMP kernels, the processor number will be
  429. * substituted for %d, %x or %o in the prompt.
  430. */
  431. char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
  432. {
  433. if (prompt && kdb_prompt_str != prompt)
  434. strncpy(kdb_prompt_str, prompt, CMD_BUFLEN);
  435. kdb_printf(kdb_prompt_str);
  436. kdb_nextline = 1; /* Prompt and input resets line number */
  437. return kdb_read(buffer, bufsize);
  438. }
  439. /*
  440. * kdb_input_flush
  441. *
  442. * Get rid of any buffered console input.
  443. *
  444. * Parameters:
  445. * none
  446. * Returns:
  447. * nothing
  448. * Locking:
  449. * none
  450. * Remarks:
  451. * Call this function whenever you want to flush input. If there is any
  452. * outstanding input, it ignores all characters until there has been no
  453. * data for approximately 1ms.
  454. */
  455. static void kdb_input_flush(void)
  456. {
  457. get_char_func *f;
  458. int res;
  459. int flush_delay = 1;
  460. while (flush_delay) {
  461. flush_delay--;
  462. empty:
  463. touch_nmi_watchdog();
  464. for (f = &kdb_poll_funcs[0]; *f; ++f) {
  465. res = (*f)();
  466. if (res != -1) {
  467. flush_delay = 1;
  468. goto empty;
  469. }
  470. }
  471. if (flush_delay)
  472. mdelay(1);
  473. }
  474. }
  475. /*
  476. * kdb_printf
  477. *
  478. * Print a string to the output device(s).
  479. *
  480. * Parameters:
  481. * printf-like format and optional args.
  482. * Returns:
  483. * 0
  484. * Locking:
  485. * None.
  486. * Remarks:
  487. * use 'kdbcons->write()' to avoid polluting 'log_buf' with
  488. * kdb output.
  489. *
  490. * If the user is doing a cmd args | grep srch
  491. * then kdb_grepping_flag is set.
  492. * In that case we need to accumulate full lines (ending in \n) before
  493. * searching for the pattern.
  494. */
  495. static char kdb_buffer[256]; /* A bit too big to go on stack */
  496. static char *next_avail = kdb_buffer;
  497. static int size_avail;
  498. static int suspend_grep;
  499. /*
  500. * search arg1 to see if it contains arg2
  501. * (kdmain.c provides flags for ^pat and pat$)
  502. *
  503. * return 1 for found, 0 for not found
  504. */
  505. static int kdb_search_string(char *searched, char *searchfor)
  506. {
  507. char firstchar, *cp;
  508. int len1, len2;
  509. /* not counting the newline at the end of "searched" */
  510. len1 = strlen(searched)-1;
  511. len2 = strlen(searchfor);
  512. if (len1 < len2)
  513. return 0;
  514. if (kdb_grep_leading && kdb_grep_trailing && len1 != len2)
  515. return 0;
  516. if (kdb_grep_leading) {
  517. if (!strncmp(searched, searchfor, len2))
  518. return 1;
  519. } else if (kdb_grep_trailing) {
  520. if (!strncmp(searched+len1-len2, searchfor, len2))
  521. return 1;
  522. } else {
  523. firstchar = *searchfor;
  524. cp = searched;
  525. while ((cp = strchr(cp, firstchar))) {
  526. if (!strncmp(cp, searchfor, len2))
  527. return 1;
  528. cp++;
  529. }
  530. }
  531. return 0;
  532. }
  533. int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  534. {
  535. int diag;
  536. int linecount;
  537. int colcount;
  538. int logging, saved_loglevel = 0;
  539. int saved_trap_printk;
  540. int got_printf_lock = 0;
  541. int retlen = 0;
  542. int fnd, len;
  543. char *cp, *cp2, *cphold = NULL, replaced_byte = ' ';
  544. char *moreprompt = "more> ";
  545. struct console *c = console_drivers;
  546. static DEFINE_SPINLOCK(kdb_printf_lock);
  547. unsigned long uninitialized_var(flags);
  548. preempt_disable();
  549. saved_trap_printk = kdb_trap_printk;
  550. kdb_trap_printk = 0;
  551. /* Serialize kdb_printf if multiple cpus try to write at once.
  552. * But if any cpu goes recursive in kdb, just print the output,
  553. * even if it is interleaved with any other text.
  554. */
  555. if (!KDB_STATE(PRINTF_LOCK)) {
  556. KDB_STATE_SET(PRINTF_LOCK);
  557. spin_lock_irqsave(&kdb_printf_lock, flags);
  558. got_printf_lock = 1;
  559. atomic_inc(&kdb_event);
  560. } else {
  561. __acquire(kdb_printf_lock);
  562. }
  563. diag = kdbgetintenv("LINES", &linecount);
  564. if (diag || linecount <= 1)
  565. linecount = 24;
  566. diag = kdbgetintenv("COLUMNS", &colcount);
  567. if (diag || colcount <= 1)
  568. colcount = 80;
  569. diag = kdbgetintenv("LOGGING", &logging);
  570. if (diag)
  571. logging = 0;
  572. if (!kdb_grepping_flag || suspend_grep) {
  573. /* normally, every vsnprintf starts a new buffer */
  574. next_avail = kdb_buffer;
  575. size_avail = sizeof(kdb_buffer);
  576. }
  577. vsnprintf(next_avail, size_avail, fmt, ap);
  578. /*
  579. * If kdb_parse() found that the command was cmd xxx | grep yyy
  580. * then kdb_grepping_flag is set, and kdb_grep_string contains yyy
  581. *
  582. * Accumulate the print data up to a newline before searching it.
  583. * (vsnprintf does null-terminate the string that it generates)
  584. */
  585. /* skip the search if prints are temporarily unconditional */
  586. if (!suspend_grep && kdb_grepping_flag) {
  587. cp = strchr(kdb_buffer, '\n');
  588. if (!cp) {
  589. /*
  590. * Special cases that don't end with newlines
  591. * but should be written without one:
  592. * The "[nn]kdb> " prompt should
  593. * appear at the front of the buffer.
  594. *
  595. * The "[nn]more " prompt should also be
  596. * (MOREPROMPT -> moreprompt)
  597. * written * but we print that ourselves,
  598. * we set the suspend_grep flag to make
  599. * it unconditional.
  600. *
  601. */
  602. if (next_avail == kdb_buffer) {
  603. /*
  604. * these should occur after a newline,
  605. * so they will be at the front of the
  606. * buffer
  607. */
  608. cp2 = kdb_buffer;
  609. len = strlen(kdb_prompt_str);
  610. if (!strncmp(cp2, kdb_prompt_str, len)) {
  611. /*
  612. * We're about to start a new
  613. * command, so we can go back
  614. * to normal mode.
  615. */
  616. kdb_grepping_flag = 0;
  617. goto kdb_printit;
  618. }
  619. }
  620. /* no newline; don't search/write the buffer
  621. until one is there */
  622. len = strlen(kdb_buffer);
  623. next_avail = kdb_buffer + len;
  624. size_avail = sizeof(kdb_buffer) - len;
  625. goto kdb_print_out;
  626. }
  627. /*
  628. * The newline is present; print through it or discard
  629. * it, depending on the results of the search.
  630. */
  631. cp++; /* to byte after the newline */
  632. replaced_byte = *cp; /* remember what/where it was */
  633. cphold = cp;
  634. *cp = '\0'; /* end the string for our search */
  635. /*
  636. * We now have a newline at the end of the string
  637. * Only continue with this output if it contains the
  638. * search string.
  639. */
  640. fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
  641. if (!fnd) {
  642. /*
  643. * At this point the complete line at the start
  644. * of kdb_buffer can be discarded, as it does
  645. * not contain what the user is looking for.
  646. * Shift the buffer left.
  647. */
  648. *cphold = replaced_byte;
  649. strcpy(kdb_buffer, cphold);
  650. len = strlen(kdb_buffer);
  651. next_avail = kdb_buffer + len;
  652. size_avail = sizeof(kdb_buffer) - len;
  653. goto kdb_print_out;
  654. }
  655. if (kdb_grepping_flag >= KDB_GREPPING_FLAG_SEARCH)
  656. /*
  657. * This was a interactive search (using '/' at more
  658. * prompt) and it has completed. Clear the flag.
  659. */
  660. kdb_grepping_flag = 0;
  661. /*
  662. * at this point the string is a full line and
  663. * should be printed, up to the null.
  664. */
  665. }
  666. kdb_printit:
  667. /*
  668. * Write to all consoles.
  669. */
  670. retlen = strlen(kdb_buffer);
  671. cp = (char *) printk_skip_level(kdb_buffer);
  672. if (!dbg_kdb_mode && kgdb_connected) {
  673. gdbstub_msg_write(cp, retlen - (cp - kdb_buffer));
  674. } else {
  675. if (dbg_io_ops && !dbg_io_ops->is_console) {
  676. len = retlen - (cp - kdb_buffer);
  677. cp2 = cp;
  678. while (len--) {
  679. dbg_io_ops->write_char(*cp2);
  680. cp2++;
  681. }
  682. }
  683. while (c) {
  684. c->write(c, cp, retlen - (cp - kdb_buffer));
  685. touch_nmi_watchdog();
  686. c = c->next;
  687. }
  688. }
  689. if (logging) {
  690. saved_loglevel = console_loglevel;
  691. console_loglevel = CONSOLE_LOGLEVEL_SILENT;
  692. if (printk_get_level(kdb_buffer) || src == KDB_MSGSRC_PRINTK)
  693. printk("%s", kdb_buffer);
  694. else
  695. pr_info("%s", kdb_buffer);
  696. }
  697. if (KDB_STATE(PAGER)) {
  698. /*
  699. * Check printed string to decide how to bump the
  700. * kdb_nextline to control when the more prompt should
  701. * show up.
  702. */
  703. int got = 0;
  704. len = retlen;
  705. while (len--) {
  706. if (kdb_buffer[len] == '\n') {
  707. kdb_nextline++;
  708. got = 0;
  709. } else if (kdb_buffer[len] == '\r') {
  710. got = 0;
  711. } else {
  712. got++;
  713. }
  714. }
  715. kdb_nextline += got / (colcount + 1);
  716. }
  717. /* check for having reached the LINES number of printed lines */
  718. if (kdb_nextline >= linecount) {
  719. char buf1[16] = "";
  720. /* Watch out for recursion here. Any routine that calls
  721. * kdb_printf will come back through here. And kdb_read
  722. * uses kdb_printf to echo on serial consoles ...
  723. */
  724. kdb_nextline = 1; /* In case of recursion */
  725. /*
  726. * Pause until cr.
  727. */
  728. moreprompt = kdbgetenv("MOREPROMPT");
  729. if (moreprompt == NULL)
  730. moreprompt = "more> ";
  731. kdb_input_flush();
  732. c = console_drivers;
  733. if (dbg_io_ops && !dbg_io_ops->is_console) {
  734. len = strlen(moreprompt);
  735. cp = moreprompt;
  736. while (len--) {
  737. dbg_io_ops->write_char(*cp);
  738. cp++;
  739. }
  740. }
  741. while (c) {
  742. c->write(c, moreprompt, strlen(moreprompt));
  743. touch_nmi_watchdog();
  744. c = c->next;
  745. }
  746. if (logging)
  747. printk("%s", moreprompt);
  748. kdb_read(buf1, 2); /* '2' indicates to return
  749. * immediately after getting one key. */
  750. kdb_nextline = 1; /* Really set output line 1 */
  751. /* empty and reset the buffer: */
  752. kdb_buffer[0] = '\0';
  753. next_avail = kdb_buffer;
  754. size_avail = sizeof(kdb_buffer);
  755. if ((buf1[0] == 'q') || (buf1[0] == 'Q')) {
  756. /* user hit q or Q */
  757. KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
  758. KDB_STATE_CLEAR(PAGER);
  759. /* end of command output; back to normal mode */
  760. kdb_grepping_flag = 0;
  761. kdb_printf("\n");
  762. } else if (buf1[0] == ' ') {
  763. kdb_printf("\r");
  764. suspend_grep = 1; /* for this recursion */
  765. } else if (buf1[0] == '\n') {
  766. kdb_nextline = linecount - 1;
  767. kdb_printf("\r");
  768. suspend_grep = 1; /* for this recursion */
  769. } else if (buf1[0] == '/' && !kdb_grepping_flag) {
  770. kdb_printf("\r");
  771. kdb_getstr(kdb_grep_string, KDB_GREP_STRLEN,
  772. kdbgetenv("SEARCHPROMPT") ?: "search> ");
  773. *strchrnul(kdb_grep_string, '\n') = '\0';
  774. kdb_grepping_flag += KDB_GREPPING_FLAG_SEARCH;
  775. suspend_grep = 1; /* for this recursion */
  776. } else if (buf1[0] && buf1[0] != '\n') {
  777. /* user hit something other than enter */
  778. suspend_grep = 1; /* for this recursion */
  779. if (buf1[0] != '/')
  780. kdb_printf(
  781. "\nOnly 'q', 'Q' or '/' are processed at "
  782. "more prompt, input ignored\n");
  783. else
  784. kdb_printf("\n'/' cannot be used during | "
  785. "grep filtering, input ignored\n");
  786. } else if (kdb_grepping_flag) {
  787. /* user hit enter */
  788. suspend_grep = 1; /* for this recursion */
  789. kdb_printf("\n");
  790. }
  791. kdb_input_flush();
  792. }
  793. /*
  794. * For grep searches, shift the printed string left.
  795. * replaced_byte contains the character that was overwritten with
  796. * the terminating null, and cphold points to the null.
  797. * Then adjust the notion of available space in the buffer.
  798. */
  799. if (kdb_grepping_flag && !suspend_grep) {
  800. *cphold = replaced_byte;
  801. strcpy(kdb_buffer, cphold);
  802. len = strlen(kdb_buffer);
  803. next_avail = kdb_buffer + len;
  804. size_avail = sizeof(kdb_buffer) - len;
  805. }
  806. kdb_print_out:
  807. suspend_grep = 0; /* end of what may have been a recursive call */
  808. if (logging)
  809. console_loglevel = saved_loglevel;
  810. if (KDB_STATE(PRINTF_LOCK) && got_printf_lock) {
  811. got_printf_lock = 0;
  812. spin_unlock_irqrestore(&kdb_printf_lock, flags);
  813. KDB_STATE_CLEAR(PRINTF_LOCK);
  814. atomic_dec(&kdb_event);
  815. } else {
  816. __release(kdb_printf_lock);
  817. }
  818. kdb_trap_printk = saved_trap_printk;
  819. preempt_enable();
  820. return retlen;
  821. }
  822. int kdb_printf(const char *fmt, ...)
  823. {
  824. va_list ap;
  825. int r;
  826. va_start(ap, fmt);
  827. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  828. va_end(ap);
  829. return r;
  830. }
  831. EXPORT_SYMBOL_GPL(kdb_printf);