kdb_io.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. static bool last_char_was_cr;
  124. for (f = &kdb_poll_funcs[0]; ; ++f) {
  125. if (*f == NULL) {
  126. /* Reset NMI watchdog once per poll loop */
  127. touch_nmi_watchdog();
  128. f = &kdb_poll_funcs[0];
  129. }
  130. key = (*f)();
  131. if (key == -1) {
  132. if (escape_delay) {
  133. udelay(ESCAPE_UDELAY);
  134. if (--escape_delay == 0)
  135. return '\e';
  136. }
  137. continue;
  138. }
  139. /*
  140. * The caller expects that newlines are either CR or LF. However
  141. * some terminals send _both_ CR and LF. Avoid having to handle
  142. * this in the caller by stripping the LF if we saw a CR right
  143. * before.
  144. */
  145. if (last_char_was_cr && key == '\n') {
  146. last_char_was_cr = false;
  147. continue;
  148. }
  149. last_char_was_cr = (key == '\r');
  150. /*
  151. * When the first character is received (or we get a change
  152. * input source) we set ourselves up to handle an escape
  153. * sequences (just in case).
  154. */
  155. if (f_prev != f) {
  156. f_prev = f;
  157. pbuf = buf;
  158. escape_delay = ESCAPE_DELAY;
  159. }
  160. *pbuf++ = key;
  161. key = kdb_handle_escape(buf, pbuf - buf);
  162. if (key < 0) /* no escape sequence; return best character */
  163. return buf[pbuf - buf == 2 ? 1 : 0];
  164. if (key > 0)
  165. return key;
  166. }
  167. unreachable();
  168. }
  169. /**
  170. * kdb_position_cursor() - Place cursor in the correct horizontal position
  171. * @prompt: Nil-terminated string containing the prompt string
  172. * @buffer: Nil-terminated string containing the entire command line
  173. * @cp: Cursor position, pointer the character in buffer where the cursor
  174. * should be positioned.
  175. *
  176. * The cursor is positioned by sending a carriage-return and then printing
  177. * the content of the line until we reach the correct cursor position.
  178. *
  179. * There is some additional fine detail here.
  180. *
  181. * Firstly, even though kdb_printf() will correctly format zero-width fields
  182. * we want the second call to kdb_printf() to be conditional. That keeps things
  183. * a little cleaner when LOGGING=1.
  184. *
  185. * Secondly, we can't combine everything into one call to kdb_printf() since
  186. * that renders into a fixed length buffer and the combined print could result
  187. * in unwanted truncation.
  188. */
  189. static void kdb_position_cursor(char *prompt, char *buffer, char *cp)
  190. {
  191. kdb_printf("\r%s", prompt);
  192. if (cp > buffer)
  193. kdb_printf("%.*s", (int)(cp - buffer), buffer);
  194. }
  195. /*
  196. * kdb_read
  197. *
  198. * This function reads a string of characters, terminated by
  199. * a newline, or by reaching the end of the supplied buffer,
  200. * from the current kernel debugger console device.
  201. * Parameters:
  202. * buffer - Address of character buffer to receive input characters.
  203. * bufsize - size, in bytes, of the character buffer
  204. * Returns:
  205. * Returns a pointer to the buffer containing the received
  206. * character string. This string will be terminated by a
  207. * newline character.
  208. * Locking:
  209. * No locks are required to be held upon entry to this
  210. * function. It is not reentrant - it relies on the fact
  211. * that while kdb is running on only one "master debug" cpu.
  212. * Remarks:
  213. * The buffer size must be >= 2.
  214. */
  215. static char *kdb_read(char *buffer, size_t bufsize)
  216. {
  217. char *cp = buffer;
  218. char *bufend = buffer+bufsize-2; /* Reserve space for newline
  219. * and null byte */
  220. char *lastchar;
  221. char *p_tmp;
  222. char tmp;
  223. static char tmpbuffer[CMD_BUFLEN];
  224. int len = strlen(buffer);
  225. int len_tmp;
  226. int tab = 0;
  227. int count;
  228. int i;
  229. int diag, dtab_count;
  230. int key, ret;
  231. diag = kdbgetintenv("DTABCOUNT", &dtab_count);
  232. if (diag)
  233. dtab_count = 30;
  234. if (len > 0) {
  235. cp += len;
  236. if (*(buffer+len-1) == '\n')
  237. cp--;
  238. }
  239. lastchar = cp;
  240. *cp = '\0';
  241. kdb_printf("%s", buffer);
  242. poll_again:
  243. key = kdb_getchar();
  244. if (key != 9)
  245. tab = 0;
  246. switch (key) {
  247. case 8: /* backspace */
  248. if (cp > buffer) {
  249. memmove(cp-1, cp, lastchar - cp + 1);
  250. lastchar--;
  251. cp--;
  252. kdb_printf("\b%s ", cp);
  253. kdb_position_cursor(kdb_prompt_str, buffer, cp);
  254. }
  255. break;
  256. case 10: /* linefeed */
  257. case 13: /* carriage return */
  258. *lastchar++ = '\n';
  259. *lastchar++ = '\0';
  260. if (!KDB_STATE(KGDB_TRANS)) {
  261. KDB_STATE_SET(KGDB_TRANS);
  262. kdb_printf("%s", buffer);
  263. }
  264. kdb_printf("\n");
  265. return buffer;
  266. case 4: /* Del */
  267. if (cp < lastchar) {
  268. memmove(cp, cp+1, lastchar - cp);
  269. lastchar--;
  270. kdb_printf("%s ", cp);
  271. kdb_position_cursor(kdb_prompt_str, buffer, cp);
  272. }
  273. break;
  274. case 1: /* Home */
  275. if (cp > buffer) {
  276. cp = buffer;
  277. kdb_position_cursor(kdb_prompt_str, buffer, cp);
  278. }
  279. break;
  280. case 5: /* End */
  281. if (cp < lastchar) {
  282. kdb_printf("%s", cp);
  283. cp = lastchar;
  284. }
  285. break;
  286. case 2: /* Left */
  287. if (cp > buffer) {
  288. kdb_printf("\b");
  289. --cp;
  290. }
  291. break;
  292. case 14: /* Down */
  293. case 16: /* Up */
  294. kdb_printf("\r%*c\r",
  295. (int)(strlen(kdb_prompt_str) + (lastchar - buffer)),
  296. ' ');
  297. *lastchar = (char)key;
  298. *(lastchar+1) = '\0';
  299. return lastchar;
  300. case 6: /* Right */
  301. if (cp < lastchar) {
  302. kdb_printf("%c", *cp);
  303. ++cp;
  304. }
  305. break;
  306. case 9: /* Tab */
  307. if (tab < 2)
  308. ++tab;
  309. tmp = *cp;
  310. *cp = '\0';
  311. p_tmp = strrchr(buffer, ' ');
  312. p_tmp = (p_tmp ? p_tmp + 1 : buffer);
  313. strscpy(tmpbuffer, p_tmp, sizeof(tmpbuffer));
  314. *cp = tmp;
  315. len = strlen(tmpbuffer);
  316. count = kallsyms_symbol_complete(tmpbuffer, sizeof(tmpbuffer));
  317. if (tab == 2 && count > 0) {
  318. kdb_printf("\n%d symbols are found.", count);
  319. if (count > dtab_count) {
  320. count = dtab_count;
  321. kdb_printf(" But only first %d symbols will"
  322. " be printed.\nYou can change the"
  323. " environment variable DTABCOUNT.",
  324. count);
  325. }
  326. kdb_printf("\n");
  327. for (i = 0; i < count; i++) {
  328. ret = kallsyms_symbol_next(tmpbuffer, i, sizeof(tmpbuffer));
  329. if (WARN_ON(!ret))
  330. break;
  331. if (ret != -E2BIG)
  332. kdb_printf("%s ", tmpbuffer);
  333. else
  334. kdb_printf("%s... ", tmpbuffer);
  335. tmpbuffer[len] = '\0';
  336. }
  337. if (i >= dtab_count)
  338. kdb_printf("...");
  339. kdb_printf("\n");
  340. kdb_printf("%s", kdb_prompt_str);
  341. kdb_printf("%s", buffer);
  342. if (cp != lastchar)
  343. kdb_position_cursor(kdb_prompt_str, buffer, cp);
  344. } else if (tab != 2 && count > 0) {
  345. /* How many new characters do we want from tmpbuffer? */
  346. len_tmp = strlen(tmpbuffer) - len;
  347. if (lastchar + len_tmp >= bufend)
  348. len_tmp = bufend - lastchar;
  349. if (len_tmp) {
  350. /* + 1 ensures the '\0' is memmove'd */
  351. memmove(cp+len_tmp, cp, (lastchar-cp) + 1);
  352. memcpy(cp, tmpbuffer+len, len_tmp);
  353. kdb_printf("%s", cp);
  354. cp += len_tmp;
  355. lastchar += len_tmp;
  356. if (cp != lastchar)
  357. kdb_position_cursor(kdb_prompt_str,
  358. buffer, cp);
  359. }
  360. }
  361. kdb_nextline = 1; /* reset output line number */
  362. break;
  363. default:
  364. if (key >= 32 && lastchar < bufend) {
  365. if (cp < lastchar) {
  366. memmove(cp+1, cp, lastchar - cp + 1);
  367. lastchar++;
  368. *cp = key;
  369. kdb_printf("%s", cp);
  370. ++cp;
  371. kdb_position_cursor(kdb_prompt_str, buffer, cp);
  372. } else {
  373. *++lastchar = '\0';
  374. *cp++ = key;
  375. /* The kgdb transition check will hide
  376. * printed characters if we think that
  377. * kgdb is connecting, until the check
  378. * fails */
  379. if (!KDB_STATE(KGDB_TRANS)) {
  380. if (kgdb_transition_check(buffer))
  381. return buffer;
  382. } else {
  383. kdb_printf("%c", key);
  384. }
  385. }
  386. /* Special escape to kgdb */
  387. if (lastchar - buffer >= 5 &&
  388. strcmp(lastchar - 5, "$?#3f") == 0) {
  389. kdb_gdb_state_pass(lastchar - 5);
  390. strcpy(buffer, "kgdb");
  391. KDB_STATE_SET(DOING_KGDB);
  392. return buffer;
  393. }
  394. if (lastchar - buffer >= 11 &&
  395. strcmp(lastchar - 11, "$qSupported") == 0) {
  396. kdb_gdb_state_pass(lastchar - 11);
  397. strcpy(buffer, "kgdb");
  398. KDB_STATE_SET(DOING_KGDB);
  399. return buffer;
  400. }
  401. }
  402. break;
  403. }
  404. goto poll_again;
  405. }
  406. /*
  407. * kdb_getstr
  408. *
  409. * Print the prompt string and read a command from the
  410. * input device.
  411. *
  412. * Parameters:
  413. * buffer Address of buffer to receive command
  414. * bufsize Size of buffer in bytes
  415. * prompt Pointer to string to use as prompt string
  416. * Returns:
  417. * Pointer to command buffer.
  418. * Locking:
  419. * None.
  420. * Remarks:
  421. * For SMP kernels, the processor number will be
  422. * substituted for %d, %x or %o in the prompt.
  423. */
  424. char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
  425. {
  426. if (prompt && kdb_prompt_str != prompt)
  427. strscpy(kdb_prompt_str, prompt, CMD_BUFLEN);
  428. kdb_printf("%s", kdb_prompt_str);
  429. kdb_nextline = 1; /* Prompt and input resets line number */
  430. return kdb_read(buffer, bufsize);
  431. }
  432. /*
  433. * kdb_input_flush
  434. *
  435. * Get rid of any buffered console input.
  436. *
  437. * Parameters:
  438. * none
  439. * Returns:
  440. * nothing
  441. * Locking:
  442. * none
  443. * Remarks:
  444. * Call this function whenever you want to flush input. If there is any
  445. * outstanding input, it ignores all characters until there has been no
  446. * data for approximately 1ms.
  447. */
  448. static void kdb_input_flush(void)
  449. {
  450. get_char_func *f;
  451. int res;
  452. int flush_delay = 1;
  453. while (flush_delay) {
  454. flush_delay--;
  455. empty:
  456. touch_nmi_watchdog();
  457. for (f = &kdb_poll_funcs[0]; *f; ++f) {
  458. res = (*f)();
  459. if (res != -1) {
  460. flush_delay = 1;
  461. goto empty;
  462. }
  463. }
  464. if (flush_delay)
  465. mdelay(1);
  466. }
  467. }
  468. /*
  469. * kdb_printf
  470. *
  471. * Print a string to the output device(s).
  472. *
  473. * Parameters:
  474. * printf-like format and optional args.
  475. * Returns:
  476. * 0
  477. * Locking:
  478. * None.
  479. * Remarks:
  480. * use 'kdbcons->write()' to avoid polluting 'log_buf' with
  481. * kdb output.
  482. *
  483. * If the user is doing a cmd args | grep srch
  484. * then kdb_grepping_flag is set.
  485. * In that case we need to accumulate full lines (ending in \n) before
  486. * searching for the pattern.
  487. */
  488. static char kdb_buffer[256]; /* A bit too big to go on stack */
  489. static char *next_avail = kdb_buffer;
  490. static int size_avail;
  491. static int suspend_grep;
  492. /*
  493. * search arg1 to see if it contains arg2
  494. * (kdmain.c provides flags for ^pat and pat$)
  495. *
  496. * return 1 for found, 0 for not found
  497. */
  498. static int kdb_search_string(char *searched, char *searchfor)
  499. {
  500. char firstchar, *cp;
  501. int len1, len2;
  502. /* not counting the newline at the end of "searched" */
  503. len1 = strlen(searched)-1;
  504. len2 = strlen(searchfor);
  505. if (len1 < len2)
  506. return 0;
  507. if (kdb_grep_leading && kdb_grep_trailing && len1 != len2)
  508. return 0;
  509. if (kdb_grep_leading) {
  510. if (!strncmp(searched, searchfor, len2))
  511. return 1;
  512. } else if (kdb_grep_trailing) {
  513. if (!strncmp(searched+len1-len2, searchfor, len2))
  514. return 1;
  515. } else {
  516. firstchar = *searchfor;
  517. cp = searched;
  518. while ((cp = strchr(cp, firstchar))) {
  519. if (!strncmp(cp, searchfor, len2))
  520. return 1;
  521. cp++;
  522. }
  523. }
  524. return 0;
  525. }
  526. static void kdb_msg_write(const char *msg, int msg_len)
  527. {
  528. struct console *c;
  529. const char *cp;
  530. int cookie;
  531. int len;
  532. if (msg_len == 0)
  533. return;
  534. cp = msg;
  535. len = msg_len;
  536. while (len--) {
  537. dbg_io_ops->write_char(*cp);
  538. cp++;
  539. }
  540. /*
  541. * The console_srcu_read_lock() only provides safe console list
  542. * traversal. The use of the ->write() callback relies on all other
  543. * CPUs being stopped at the moment and console drivers being able to
  544. * handle reentrance when @oops_in_progress is set.
  545. *
  546. * There is no guarantee that every console driver can handle
  547. * reentrance in this way; the developer deploying the debugger
  548. * is responsible for ensuring that the console drivers they
  549. * have selected handle reentrance appropriately.
  550. */
  551. cookie = console_srcu_read_lock();
  552. for_each_console_srcu(c) {
  553. if (!(console_srcu_read_flags(c) & CON_ENABLED))
  554. continue;
  555. if (c == dbg_io_ops->cons)
  556. continue;
  557. if (!c->write)
  558. continue;
  559. /*
  560. * Set oops_in_progress to encourage the console drivers to
  561. * disregard their internal spin locks: in the current calling
  562. * context the risk of deadlock is a bigger problem than risks
  563. * due to re-entering the console driver. We operate directly on
  564. * oops_in_progress rather than using bust_spinlocks() because
  565. * the calls bust_spinlocks() makes on exit are not appropriate
  566. * for this calling context.
  567. */
  568. ++oops_in_progress;
  569. c->write(c, msg, msg_len);
  570. --oops_in_progress;
  571. touch_nmi_watchdog();
  572. }
  573. console_srcu_read_unlock(cookie);
  574. }
  575. int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
  576. {
  577. int diag;
  578. int linecount;
  579. int colcount;
  580. int logging, saved_loglevel = 0;
  581. int retlen = 0;
  582. int fnd, len;
  583. int this_cpu, old_cpu;
  584. char *cp, *cp2, *cphold = NULL, replaced_byte = ' ';
  585. char *moreprompt = "more> ";
  586. unsigned long flags;
  587. /* Serialize kdb_printf if multiple cpus try to write at once.
  588. * But if any cpu goes recursive in kdb, just print the output,
  589. * even if it is interleaved with any other text.
  590. */
  591. local_irq_save(flags);
  592. this_cpu = smp_processor_id();
  593. for (;;) {
  594. old_cpu = cmpxchg(&kdb_printf_cpu, -1, this_cpu);
  595. if (old_cpu == -1 || old_cpu == this_cpu)
  596. break;
  597. cpu_relax();
  598. }
  599. diag = kdbgetintenv("LINES", &linecount);
  600. if (diag || linecount <= 1)
  601. linecount = 24;
  602. diag = kdbgetintenv("COLUMNS", &colcount);
  603. if (diag || colcount <= 1)
  604. colcount = 80;
  605. diag = kdbgetintenv("LOGGING", &logging);
  606. if (diag)
  607. logging = 0;
  608. if (!kdb_grepping_flag || suspend_grep) {
  609. /* normally, every vsnprintf starts a new buffer */
  610. next_avail = kdb_buffer;
  611. size_avail = sizeof(kdb_buffer);
  612. }
  613. vsnprintf(next_avail, size_avail, fmt, ap);
  614. /*
  615. * If kdb_parse() found that the command was cmd xxx | grep yyy
  616. * then kdb_grepping_flag is set, and kdb_grep_string contains yyy
  617. *
  618. * Accumulate the print data up to a newline before searching it.
  619. * (vsnprintf does null-terminate the string that it generates)
  620. */
  621. /* skip the search if prints are temporarily unconditional */
  622. if (!suspend_grep && kdb_grepping_flag) {
  623. cp = strchr(kdb_buffer, '\n');
  624. if (!cp) {
  625. /*
  626. * Special cases that don't end with newlines
  627. * but should be written without one:
  628. * The "[nn]kdb> " prompt should
  629. * appear at the front of the buffer.
  630. *
  631. * The "[nn]more " prompt should also be
  632. * (MOREPROMPT -> moreprompt)
  633. * written * but we print that ourselves,
  634. * we set the suspend_grep flag to make
  635. * it unconditional.
  636. *
  637. */
  638. if (next_avail == kdb_buffer) {
  639. /*
  640. * these should occur after a newline,
  641. * so they will be at the front of the
  642. * buffer
  643. */
  644. cp2 = kdb_buffer;
  645. len = strlen(kdb_prompt_str);
  646. if (!strncmp(cp2, kdb_prompt_str, len)) {
  647. /*
  648. * We're about to start a new
  649. * command, so we can go back
  650. * to normal mode.
  651. */
  652. kdb_grepping_flag = 0;
  653. goto kdb_printit;
  654. }
  655. }
  656. /* no newline; don't search/write the buffer
  657. until one is there */
  658. len = strlen(kdb_buffer);
  659. next_avail = kdb_buffer + len;
  660. size_avail = sizeof(kdb_buffer) - len;
  661. goto kdb_print_out;
  662. }
  663. /*
  664. * The newline is present; print through it or discard
  665. * it, depending on the results of the search.
  666. */
  667. cp++; /* to byte after the newline */
  668. replaced_byte = *cp; /* remember what/where it was */
  669. cphold = cp;
  670. *cp = '\0'; /* end the string for our search */
  671. /*
  672. * We now have a newline at the end of the string
  673. * Only continue with this output if it contains the
  674. * search string.
  675. */
  676. fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
  677. if (!fnd) {
  678. /*
  679. * At this point the complete line at the start
  680. * of kdb_buffer can be discarded, as it does
  681. * not contain what the user is looking for.
  682. * Shift the buffer left.
  683. */
  684. *cphold = replaced_byte;
  685. strcpy(kdb_buffer, cphold);
  686. len = strlen(kdb_buffer);
  687. next_avail = kdb_buffer + len;
  688. size_avail = sizeof(kdb_buffer) - len;
  689. goto kdb_print_out;
  690. }
  691. if (kdb_grepping_flag >= KDB_GREPPING_FLAG_SEARCH) {
  692. /*
  693. * This was a interactive search (using '/' at more
  694. * prompt) and it has completed. Replace the \0 with
  695. * its original value to ensure multi-line strings
  696. * are handled properly, and return to normal mode.
  697. */
  698. *cphold = replaced_byte;
  699. kdb_grepping_flag = 0;
  700. }
  701. /*
  702. * at this point the string is a full line and
  703. * should be printed, up to the null.
  704. */
  705. }
  706. kdb_printit:
  707. /*
  708. * Write to all consoles.
  709. */
  710. retlen = strlen(kdb_buffer);
  711. cp = (char *) printk_skip_headers(kdb_buffer);
  712. if (!dbg_kdb_mode && kgdb_connected)
  713. gdbstub_msg_write(cp, retlen - (cp - kdb_buffer));
  714. else
  715. kdb_msg_write(cp, retlen - (cp - kdb_buffer));
  716. if (logging) {
  717. saved_loglevel = console_loglevel;
  718. console_loglevel = CONSOLE_LOGLEVEL_SILENT;
  719. if (printk_get_level(kdb_buffer) || src == KDB_MSGSRC_PRINTK)
  720. printk("%s", kdb_buffer);
  721. else
  722. pr_info("%s", kdb_buffer);
  723. }
  724. if (KDB_STATE(PAGER)) {
  725. /*
  726. * Check printed string to decide how to bump the
  727. * kdb_nextline to control when the more prompt should
  728. * show up.
  729. */
  730. int got = 0;
  731. len = retlen;
  732. while (len--) {
  733. if (kdb_buffer[len] == '\n') {
  734. kdb_nextline++;
  735. got = 0;
  736. } else if (kdb_buffer[len] == '\r') {
  737. got = 0;
  738. } else {
  739. got++;
  740. }
  741. }
  742. kdb_nextline += got / (colcount + 1);
  743. }
  744. /* check for having reached the LINES number of printed lines */
  745. if (kdb_nextline >= linecount) {
  746. char ch;
  747. /* Watch out for recursion here. Any routine that calls
  748. * kdb_printf will come back through here. And kdb_read
  749. * uses kdb_printf to echo on serial consoles ...
  750. */
  751. kdb_nextline = 1; /* In case of recursion */
  752. /*
  753. * Pause until cr.
  754. */
  755. moreprompt = kdbgetenv("MOREPROMPT");
  756. if (moreprompt == NULL)
  757. moreprompt = "more> ";
  758. kdb_input_flush();
  759. kdb_msg_write(moreprompt, strlen(moreprompt));
  760. if (logging)
  761. printk("%s", moreprompt);
  762. ch = kdb_getchar();
  763. kdb_nextline = 1; /* Really set output line 1 */
  764. /* empty and reset the buffer: */
  765. kdb_buffer[0] = '\0';
  766. next_avail = kdb_buffer;
  767. size_avail = sizeof(kdb_buffer);
  768. if ((ch == 'q') || (ch == 'Q')) {
  769. /* user hit q or Q */
  770. KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
  771. KDB_STATE_CLEAR(PAGER);
  772. /* end of command output; back to normal mode */
  773. kdb_grepping_flag = 0;
  774. kdb_printf("\n");
  775. } else if (ch == ' ') {
  776. kdb_printf("\r");
  777. suspend_grep = 1; /* for this recursion */
  778. } else if (ch == '\n' || ch == '\r') {
  779. kdb_nextline = linecount - 1;
  780. kdb_printf("\r");
  781. suspend_grep = 1; /* for this recursion */
  782. } else if (ch == '/' && !kdb_grepping_flag) {
  783. kdb_printf("\r");
  784. kdb_getstr(kdb_grep_string, KDB_GREP_STRLEN,
  785. kdbgetenv("SEARCHPROMPT") ?: "search> ");
  786. *strchrnul(kdb_grep_string, '\n') = '\0';
  787. kdb_grepping_flag += KDB_GREPPING_FLAG_SEARCH;
  788. suspend_grep = 1; /* for this recursion */
  789. } else if (ch) {
  790. /* user hit something unexpected */
  791. suspend_grep = 1; /* for this recursion */
  792. if (ch != '/')
  793. kdb_printf(
  794. "\nOnly 'q', 'Q' or '/' are processed at "
  795. "more prompt, input ignored\n");
  796. else
  797. kdb_printf("\n'/' cannot be used during | "
  798. "grep filtering, input ignored\n");
  799. } else if (kdb_grepping_flag) {
  800. /* user hit enter */
  801. suspend_grep = 1; /* for this recursion */
  802. kdb_printf("\n");
  803. }
  804. kdb_input_flush();
  805. }
  806. /*
  807. * For grep searches, shift the printed string left.
  808. * replaced_byte contains the character that was overwritten with
  809. * the terminating null, and cphold points to the null.
  810. * Then adjust the notion of available space in the buffer.
  811. */
  812. if (kdb_grepping_flag && !suspend_grep) {
  813. *cphold = replaced_byte;
  814. strcpy(kdb_buffer, cphold);
  815. len = strlen(kdb_buffer);
  816. next_avail = kdb_buffer + len;
  817. size_avail = sizeof(kdb_buffer) - len;
  818. }
  819. kdb_print_out:
  820. suspend_grep = 0; /* end of what may have been a recursive call */
  821. if (logging)
  822. console_loglevel = saved_loglevel;
  823. /* kdb_printf_cpu locked the code above. */
  824. smp_store_release(&kdb_printf_cpu, old_cpu);
  825. local_irq_restore(flags);
  826. return retlen;
  827. }
  828. int kdb_printf(const char *fmt, ...)
  829. {
  830. va_list ap;
  831. int r;
  832. va_start(ap, fmt);
  833. r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
  834. va_end(ap);
  835. return r;
  836. }
  837. EXPORT_SYMBOL_GPL(kdb_printf);