kdb_io.c 21 KB

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