kdb_io.c 21 KB

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