0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/types.h>
0013 #include <linux/ctype.h>
0014 #include <linux/kernel.h>
0015 #include <linux/init.h>
0016 #include <linux/kdev_t.h>
0017 #include <linux/console.h>
0018 #include <linux/string.h>
0019 #include <linux/sched.h>
0020 #include <linux/smp.h>
0021 #include <linux/nmi.h>
0022 #include <linux/delay.h>
0023 #include <linux/kgdb.h>
0024 #include <linux/kdb.h>
0025 #include <linux/kallsyms.h>
0026 #include "kdb_private.h"
0027
0028 #define CMD_BUFLEN 256
0029 char kdb_prompt_str[CMD_BUFLEN];
0030
0031 int kdb_trap_printk;
0032 int kdb_printf_cpu = -1;
0033
0034 static int kgdb_transition_check(char *buffer)
0035 {
0036 if (buffer[0] != '+' && buffer[0] != '$') {
0037 KDB_STATE_SET(KGDB_TRANS);
0038 kdb_printf("%s", buffer);
0039 } else {
0040 int slen = strlen(buffer);
0041 if (slen > 3 && buffer[slen - 3] == '#') {
0042 kdb_gdb_state_pass(buffer);
0043 strcpy(buffer, "kgdb");
0044 KDB_STATE_SET(DOING_KGDB);
0045 return 1;
0046 }
0047 }
0048 return 0;
0049 }
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 static int kdb_handle_escape(char *buf, size_t sz)
0062 {
0063 char *lastkey = buf + sz - 1;
0064
0065 switch (sz) {
0066 case 1:
0067 if (*lastkey == '\e')
0068 return 0;
0069 break;
0070
0071 case 2:
0072 if (*lastkey == '[')
0073 return 0;
0074 break;
0075
0076 case 3:
0077 switch (*lastkey) {
0078 case 'A':
0079 return 16;
0080 case 'B':
0081 return 14;
0082 case 'C':
0083 return 6;
0084 case 'D':
0085 return 2;
0086 case '1':
0087 case '3':
0088 case '4':
0089 return 0;
0090 }
0091 break;
0092
0093 case 4:
0094 if (*lastkey == '~') {
0095 switch (buf[2]) {
0096 case '1':
0097 return 1;
0098 case '3':
0099 return 4;
0100 case '4':
0101 return 5;
0102 }
0103 }
0104 break;
0105 }
0106
0107 return -1;
0108 }
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 char kdb_getchar(void)
0126 {
0127 #define ESCAPE_UDELAY 1000
0128 #define ESCAPE_DELAY (2*1000000/ESCAPE_UDELAY)
0129 char buf[4];
0130 char *pbuf = buf;
0131 int escape_delay = 0;
0132 get_char_func *f, *f_prev = NULL;
0133 int key;
0134
0135 for (f = &kdb_poll_funcs[0]; ; ++f) {
0136 if (*f == NULL) {
0137
0138 touch_nmi_watchdog();
0139 f = &kdb_poll_funcs[0];
0140 }
0141
0142 key = (*f)();
0143 if (key == -1) {
0144 if (escape_delay) {
0145 udelay(ESCAPE_UDELAY);
0146 if (--escape_delay == 0)
0147 return '\e';
0148 }
0149 continue;
0150 }
0151
0152
0153
0154
0155
0156
0157 if (f_prev != f) {
0158 f_prev = f;
0159 pbuf = buf;
0160 escape_delay = ESCAPE_DELAY;
0161 }
0162
0163 *pbuf++ = key;
0164 key = kdb_handle_escape(buf, pbuf - buf);
0165 if (key < 0)
0166 return buf[pbuf - buf == 2 ? 1 : 0];
0167 if (key > 0)
0168 return key;
0169 }
0170
0171 unreachable();
0172 }
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 static char *kdb_read(char *buffer, size_t bufsize)
0196 {
0197 char *cp = buffer;
0198 char *bufend = buffer+bufsize-2;
0199
0200 char *lastchar;
0201 char *p_tmp;
0202 char tmp;
0203 static char tmpbuffer[CMD_BUFLEN];
0204 int len = strlen(buffer);
0205 int len_tmp;
0206 int tab = 0;
0207 int count;
0208 int i;
0209 int diag, dtab_count;
0210 int key, buf_size, ret;
0211
0212
0213 diag = kdbgetintenv("DTABCOUNT", &dtab_count);
0214 if (diag)
0215 dtab_count = 30;
0216
0217 if (len > 0) {
0218 cp += len;
0219 if (*(buffer+len-1) == '\n')
0220 cp--;
0221 }
0222
0223 lastchar = cp;
0224 *cp = '\0';
0225 kdb_printf("%s", buffer);
0226 poll_again:
0227 key = kdb_getchar();
0228 if (key != 9)
0229 tab = 0;
0230 switch (key) {
0231 case 8:
0232 if (cp > buffer) {
0233 if (cp < lastchar) {
0234 memcpy(tmpbuffer, cp, lastchar - cp);
0235 memcpy(cp-1, tmpbuffer, lastchar - cp);
0236 }
0237 *(--lastchar) = '\0';
0238 --cp;
0239 kdb_printf("\b%s \r", cp);
0240 tmp = *cp;
0241 *cp = '\0';
0242 kdb_printf(kdb_prompt_str);
0243 kdb_printf("%s", buffer);
0244 *cp = tmp;
0245 }
0246 break;
0247 case 13:
0248 *lastchar++ = '\n';
0249 *lastchar++ = '\0';
0250 if (!KDB_STATE(KGDB_TRANS)) {
0251 KDB_STATE_SET(KGDB_TRANS);
0252 kdb_printf("%s", buffer);
0253 }
0254 kdb_printf("\n");
0255 return buffer;
0256 case 4:
0257 if (cp < lastchar) {
0258 memcpy(tmpbuffer, cp+1, lastchar - cp - 1);
0259 memcpy(cp, tmpbuffer, lastchar - cp - 1);
0260 *(--lastchar) = '\0';
0261 kdb_printf("%s \r", cp);
0262 tmp = *cp;
0263 *cp = '\0';
0264 kdb_printf(kdb_prompt_str);
0265 kdb_printf("%s", buffer);
0266 *cp = tmp;
0267 }
0268 break;
0269 case 1:
0270 if (cp > buffer) {
0271 kdb_printf("\r");
0272 kdb_printf(kdb_prompt_str);
0273 cp = buffer;
0274 }
0275 break;
0276 case 5:
0277 if (cp < lastchar) {
0278 kdb_printf("%s", cp);
0279 cp = lastchar;
0280 }
0281 break;
0282 case 2:
0283 if (cp > buffer) {
0284 kdb_printf("\b");
0285 --cp;
0286 }
0287 break;
0288 case 14:
0289 memset(tmpbuffer, ' ',
0290 strlen(kdb_prompt_str) + (lastchar-buffer));
0291 *(tmpbuffer+strlen(kdb_prompt_str) +
0292 (lastchar-buffer)) = '\0';
0293 kdb_printf("\r%s\r", tmpbuffer);
0294 *lastchar = (char)key;
0295 *(lastchar+1) = '\0';
0296 return lastchar;
0297 case 6:
0298 if (cp < lastchar) {
0299 kdb_printf("%c", *cp);
0300 ++cp;
0301 }
0302 break;
0303 case 16:
0304 memset(tmpbuffer, ' ',
0305 strlen(kdb_prompt_str) + (lastchar-buffer));
0306 *(tmpbuffer+strlen(kdb_prompt_str) +
0307 (lastchar-buffer)) = '\0';
0308 kdb_printf("\r%s\r", tmpbuffer);
0309 *lastchar = (char)key;
0310 *(lastchar+1) = '\0';
0311 return lastchar;
0312 case 9:
0313 if (tab < 2)
0314 ++tab;
0315 p_tmp = buffer;
0316 while (*p_tmp == ' ')
0317 p_tmp++;
0318 if (p_tmp > cp)
0319 break;
0320 memcpy(tmpbuffer, p_tmp, cp-p_tmp);
0321 *(tmpbuffer + (cp-p_tmp)) = '\0';
0322 p_tmp = strrchr(tmpbuffer, ' ');
0323 if (p_tmp)
0324 ++p_tmp;
0325 else
0326 p_tmp = tmpbuffer;
0327 len = strlen(p_tmp);
0328 buf_size = sizeof(tmpbuffer) - (p_tmp - tmpbuffer);
0329 count = kallsyms_symbol_complete(p_tmp, buf_size);
0330 if (tab == 2 && count > 0) {
0331 kdb_printf("\n%d symbols are found.", count);
0332 if (count > dtab_count) {
0333 count = dtab_count;
0334 kdb_printf(" But only first %d symbols will"
0335 " be printed.\nYou can change the"
0336 " environment variable DTABCOUNT.",
0337 count);
0338 }
0339 kdb_printf("\n");
0340 for (i = 0; i < count; i++) {
0341 ret = kallsyms_symbol_next(p_tmp, i, buf_size);
0342 if (WARN_ON(!ret))
0343 break;
0344 if (ret != -E2BIG)
0345 kdb_printf("%s ", p_tmp);
0346 else
0347 kdb_printf("%s... ", p_tmp);
0348 *(p_tmp + len) = '\0';
0349 }
0350 if (i >= dtab_count)
0351 kdb_printf("...");
0352 kdb_printf("\n");
0353 kdb_printf(kdb_prompt_str);
0354 kdb_printf("%s", buffer);
0355 } else if (tab != 2 && count > 0) {
0356 len_tmp = strlen(p_tmp);
0357 strncpy(p_tmp+len_tmp, cp, lastchar-cp+1);
0358 len_tmp = strlen(p_tmp);
0359 strncpy(cp, p_tmp+len, len_tmp-len + 1);
0360 len = len_tmp - len;
0361 kdb_printf("%s", cp);
0362 cp += len;
0363 lastchar += len;
0364 }
0365 kdb_nextline = 1;
0366 break;
0367 default:
0368 if (key >= 32 && lastchar < bufend) {
0369 if (cp < lastchar) {
0370 memcpy(tmpbuffer, cp, lastchar - cp);
0371 memcpy(cp+1, tmpbuffer, lastchar - cp);
0372 *++lastchar = '\0';
0373 *cp = key;
0374 kdb_printf("%s\r", cp);
0375 ++cp;
0376 tmp = *cp;
0377 *cp = '\0';
0378 kdb_printf(kdb_prompt_str);
0379 kdb_printf("%s", buffer);
0380 *cp = tmp;
0381 } else {
0382 *++lastchar = '\0';
0383 *cp++ = key;
0384
0385
0386
0387
0388 if (!KDB_STATE(KGDB_TRANS)) {
0389 if (kgdb_transition_check(buffer))
0390 return buffer;
0391 } else {
0392 kdb_printf("%c", key);
0393 }
0394 }
0395
0396 if (lastchar - buffer >= 5 &&
0397 strcmp(lastchar - 5, "$?#3f") == 0) {
0398 kdb_gdb_state_pass(lastchar - 5);
0399 strcpy(buffer, "kgdb");
0400 KDB_STATE_SET(DOING_KGDB);
0401 return buffer;
0402 }
0403 if (lastchar - buffer >= 11 &&
0404 strcmp(lastchar - 11, "$qSupported") == 0) {
0405 kdb_gdb_state_pass(lastchar - 11);
0406 strcpy(buffer, "kgdb");
0407 KDB_STATE_SET(DOING_KGDB);
0408 return buffer;
0409 }
0410 }
0411 break;
0412 }
0413 goto poll_again;
0414 }
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435 char *kdb_getstr(char *buffer, size_t bufsize, const char *prompt)
0436 {
0437 if (prompt && kdb_prompt_str != prompt)
0438 strscpy(kdb_prompt_str, prompt, CMD_BUFLEN);
0439 kdb_printf(kdb_prompt_str);
0440 kdb_nextline = 1;
0441 return kdb_read(buffer, bufsize);
0442 }
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461 static void kdb_input_flush(void)
0462 {
0463 get_char_func *f;
0464 int res;
0465 int flush_delay = 1;
0466 while (flush_delay) {
0467 flush_delay--;
0468 empty:
0469 touch_nmi_watchdog();
0470 for (f = &kdb_poll_funcs[0]; *f; ++f) {
0471 res = (*f)();
0472 if (res != -1) {
0473 flush_delay = 1;
0474 goto empty;
0475 }
0476 }
0477 if (flush_delay)
0478 mdelay(1);
0479 }
0480 }
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503 static char kdb_buffer[256];
0504 static char *next_avail = kdb_buffer;
0505 static int size_avail;
0506 static int suspend_grep;
0507
0508
0509
0510
0511
0512
0513
0514 static int kdb_search_string(char *searched, char *searchfor)
0515 {
0516 char firstchar, *cp;
0517 int len1, len2;
0518
0519
0520 len1 = strlen(searched)-1;
0521 len2 = strlen(searchfor);
0522 if (len1 < len2)
0523 return 0;
0524 if (kdb_grep_leading && kdb_grep_trailing && len1 != len2)
0525 return 0;
0526 if (kdb_grep_leading) {
0527 if (!strncmp(searched, searchfor, len2))
0528 return 1;
0529 } else if (kdb_grep_trailing) {
0530 if (!strncmp(searched+len1-len2, searchfor, len2))
0531 return 1;
0532 } else {
0533 firstchar = *searchfor;
0534 cp = searched;
0535 while ((cp = strchr(cp, firstchar))) {
0536 if (!strncmp(cp, searchfor, len2))
0537 return 1;
0538 cp++;
0539 }
0540 }
0541 return 0;
0542 }
0543
0544 static void kdb_msg_write(const char *msg, int msg_len)
0545 {
0546 struct console *c;
0547 const char *cp;
0548 int len;
0549
0550 if (msg_len == 0)
0551 return;
0552
0553 cp = msg;
0554 len = msg_len;
0555
0556 while (len--) {
0557 dbg_io_ops->write_char(*cp);
0558 cp++;
0559 }
0560
0561 for_each_console(c) {
0562 if (!(c->flags & CON_ENABLED))
0563 continue;
0564 if (c == dbg_io_ops->cons)
0565 continue;
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575 ++oops_in_progress;
0576 c->write(c, msg, msg_len);
0577 --oops_in_progress;
0578 touch_nmi_watchdog();
0579 }
0580 }
0581
0582 int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
0583 {
0584 int diag;
0585 int linecount;
0586 int colcount;
0587 int logging, saved_loglevel = 0;
0588 int retlen = 0;
0589 int fnd, len;
0590 int this_cpu, old_cpu;
0591 char *cp, *cp2, *cphold = NULL, replaced_byte = ' ';
0592 char *moreprompt = "more> ";
0593 unsigned long flags;
0594
0595
0596
0597
0598
0599 local_irq_save(flags);
0600 this_cpu = smp_processor_id();
0601 for (;;) {
0602 old_cpu = cmpxchg(&kdb_printf_cpu, -1, this_cpu);
0603 if (old_cpu == -1 || old_cpu == this_cpu)
0604 break;
0605
0606 cpu_relax();
0607 }
0608
0609 diag = kdbgetintenv("LINES", &linecount);
0610 if (diag || linecount <= 1)
0611 linecount = 24;
0612
0613 diag = kdbgetintenv("COLUMNS", &colcount);
0614 if (diag || colcount <= 1)
0615 colcount = 80;
0616
0617 diag = kdbgetintenv("LOGGING", &logging);
0618 if (diag)
0619 logging = 0;
0620
0621 if (!kdb_grepping_flag || suspend_grep) {
0622
0623 next_avail = kdb_buffer;
0624 size_avail = sizeof(kdb_buffer);
0625 }
0626 vsnprintf(next_avail, size_avail, fmt, ap);
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637 if (!suspend_grep && kdb_grepping_flag) {
0638 cp = strchr(kdb_buffer, '\n');
0639 if (!cp) {
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653 if (next_avail == kdb_buffer) {
0654
0655
0656
0657
0658
0659 cp2 = kdb_buffer;
0660 len = strlen(kdb_prompt_str);
0661 if (!strncmp(cp2, kdb_prompt_str, len)) {
0662
0663
0664
0665
0666
0667 kdb_grepping_flag = 0;
0668 goto kdb_printit;
0669 }
0670 }
0671
0672
0673 len = strlen(kdb_buffer);
0674 next_avail = kdb_buffer + len;
0675 size_avail = sizeof(kdb_buffer) - len;
0676 goto kdb_print_out;
0677 }
0678
0679
0680
0681
0682
0683 cp++;
0684 replaced_byte = *cp;
0685 cphold = cp;
0686 *cp = '\0';
0687
0688
0689
0690
0691
0692
0693 fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
0694 if (!fnd) {
0695
0696
0697
0698
0699
0700
0701 *cphold = replaced_byte;
0702 strcpy(kdb_buffer, cphold);
0703 len = strlen(kdb_buffer);
0704 next_avail = kdb_buffer + len;
0705 size_avail = sizeof(kdb_buffer) - len;
0706 goto kdb_print_out;
0707 }
0708 if (kdb_grepping_flag >= KDB_GREPPING_FLAG_SEARCH) {
0709
0710
0711
0712
0713
0714
0715 *cphold = replaced_byte;
0716 kdb_grepping_flag = 0;
0717 }
0718
0719
0720
0721
0722 }
0723 kdb_printit:
0724
0725
0726
0727
0728 retlen = strlen(kdb_buffer);
0729 cp = (char *) printk_skip_headers(kdb_buffer);
0730 if (!dbg_kdb_mode && kgdb_connected)
0731 gdbstub_msg_write(cp, retlen - (cp - kdb_buffer));
0732 else
0733 kdb_msg_write(cp, retlen - (cp - kdb_buffer));
0734
0735 if (logging) {
0736 saved_loglevel = console_loglevel;
0737 console_loglevel = CONSOLE_LOGLEVEL_SILENT;
0738 if (printk_get_level(kdb_buffer) || src == KDB_MSGSRC_PRINTK)
0739 printk("%s", kdb_buffer);
0740 else
0741 pr_info("%s", kdb_buffer);
0742 }
0743
0744 if (KDB_STATE(PAGER)) {
0745
0746
0747
0748
0749
0750 int got = 0;
0751 len = retlen;
0752 while (len--) {
0753 if (kdb_buffer[len] == '\n') {
0754 kdb_nextline++;
0755 got = 0;
0756 } else if (kdb_buffer[len] == '\r') {
0757 got = 0;
0758 } else {
0759 got++;
0760 }
0761 }
0762 kdb_nextline += got / (colcount + 1);
0763 }
0764
0765
0766 if (kdb_nextline >= linecount) {
0767 char ch;
0768
0769
0770
0771
0772
0773 kdb_nextline = 1;
0774
0775
0776
0777
0778 moreprompt = kdbgetenv("MOREPROMPT");
0779 if (moreprompt == NULL)
0780 moreprompt = "more> ";
0781
0782 kdb_input_flush();
0783 kdb_msg_write(moreprompt, strlen(moreprompt));
0784
0785 if (logging)
0786 printk("%s", moreprompt);
0787
0788 ch = kdb_getchar();
0789 kdb_nextline = 1;
0790
0791
0792 kdb_buffer[0] = '\0';
0793 next_avail = kdb_buffer;
0794 size_avail = sizeof(kdb_buffer);
0795 if ((ch == 'q') || (ch == 'Q')) {
0796
0797 KDB_FLAG_SET(CMD_INTERRUPT);
0798 KDB_STATE_CLEAR(PAGER);
0799
0800 kdb_grepping_flag = 0;
0801 kdb_printf("\n");
0802 } else if (ch == ' ') {
0803 kdb_printf("\r");
0804 suspend_grep = 1;
0805 } else if (ch == '\n' || ch == '\r') {
0806 kdb_nextline = linecount - 1;
0807 kdb_printf("\r");
0808 suspend_grep = 1;
0809 } else if (ch == '/' && !kdb_grepping_flag) {
0810 kdb_printf("\r");
0811 kdb_getstr(kdb_grep_string, KDB_GREP_STRLEN,
0812 kdbgetenv("SEARCHPROMPT") ?: "search> ");
0813 *strchrnul(kdb_grep_string, '\n') = '\0';
0814 kdb_grepping_flag += KDB_GREPPING_FLAG_SEARCH;
0815 suspend_grep = 1;
0816 } else if (ch) {
0817
0818 suspend_grep = 1;
0819 if (ch != '/')
0820 kdb_printf(
0821 "\nOnly 'q', 'Q' or '/' are processed at "
0822 "more prompt, input ignored\n");
0823 else
0824 kdb_printf("\n'/' cannot be used during | "
0825 "grep filtering, input ignored\n");
0826 } else if (kdb_grepping_flag) {
0827
0828 suspend_grep = 1;
0829 kdb_printf("\n");
0830 }
0831 kdb_input_flush();
0832 }
0833
0834
0835
0836
0837
0838
0839
0840 if (kdb_grepping_flag && !suspend_grep) {
0841 *cphold = replaced_byte;
0842 strcpy(kdb_buffer, cphold);
0843 len = strlen(kdb_buffer);
0844 next_avail = kdb_buffer + len;
0845 size_avail = sizeof(kdb_buffer) - len;
0846 }
0847
0848 kdb_print_out:
0849 suspend_grep = 0;
0850 if (logging)
0851 console_loglevel = saved_loglevel;
0852
0853 smp_store_release(&kdb_printf_cpu, old_cpu);
0854 local_irq_restore(flags);
0855 return retlen;
0856 }
0857
0858 int kdb_printf(const char *fmt, ...)
0859 {
0860 va_list ap;
0861 int r;
0862
0863 va_start(ap, fmt);
0864 r = vkdb_printf(KDB_MSGSRC_INTERNAL, fmt, ap);
0865 va_end(ap);
0866
0867 return r;
0868 }
0869 EXPORT_SYMBOL_GPL(kdb_printf);