kdb_io.c 21 KB

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