Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Kernel Debugger Architecture Independent Console I/O handler
0003  *
0004  * This file is subject to the terms and conditions of the GNU General Public
0005  * License.  See the file "COPYING" in the main directory of this archive
0006  * for more details.
0007  *
0008  * Copyright (c) 1999-2006 Silicon Graphics, Inc.  All Rights Reserved.
0009  * Copyright (c) 2009 Wind River Systems, Inc.  All Rights Reserved.
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  * kdb_handle_escape() - validity check on an accumulated escape sequence.
0053  * @buf:    Accumulated escape characters to be examined. Note that buf
0054  *      is not a string, it is an array of characters and need not be
0055  *      nil terminated.
0056  * @sz:     Number of accumulated escape characters.
0057  *
0058  * Return: -1 if the escape sequence is unwanted, 0 if it is incomplete,
0059  * otherwise it returns a mapped key value to pass to the upper layers.
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: /* \e<something> */
0072         if (*lastkey == '[')
0073             return 0;
0074         break;
0075 
0076     case 3:
0077         switch (*lastkey) {
0078         case 'A': /* \e[A, up arrow */
0079             return 16;
0080         case 'B': /* \e[B, down arrow */
0081             return 14;
0082         case 'C': /* \e[C, right arrow */
0083             return 6;
0084         case 'D': /* \e[D, left arrow */
0085             return 2;
0086         case '1': /* \e[<1,3,4>], may be home, del, end */
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': /* \e[1~, home */
0097                 return 1;
0098             case '3': /* \e[3~, del */
0099                 return 4;
0100             case '4': /* \e[4~, end */
0101                 return 5;
0102             }
0103         }
0104         break;
0105     }
0106 
0107     return -1;
0108 }
0109 
0110 /**
0111  * kdb_getchar() - Read a single character from a kdb console (or consoles).
0112  *
0113  * Other than polling the various consoles that are currently enabled,
0114  * most of the work done in this function is dealing with escape sequences.
0115  *
0116  * An escape key could be the start of a vt100 control sequence such as \e[D
0117  * (left arrow) or it could be a character in its own right.  The standard
0118  * method for detecting the difference is to wait for 2 seconds to see if there
0119  * are any other characters.  kdb is complicated by the lack of a timer service
0120  * (interrupts are off), by multiple input sources. Escape sequence processing
0121  * has to be done as states in the polling loop.
0122  *
0123  * Return: The key pressed or a control code derived from an escape sequence.
0124  */
0125 char kdb_getchar(void)
0126 {
0127 #define ESCAPE_UDELAY 1000
0128 #define ESCAPE_DELAY (2*1000000/ESCAPE_UDELAY) /* 2 seconds worth of udelays */
0129     char buf[4];    /* longest vt100 escape sequence is 4 bytes */
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             /* Reset NMI watchdog once per poll loop */
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          * When the first character is received (or we get a change
0154          * input source) we set ourselves up to handle an escape
0155          * sequences (just in case).
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) /* no escape sequence; return best character */
0166             return buf[pbuf - buf == 2 ? 1 : 0];
0167         if (key > 0)
0168             return key;
0169     }
0170 
0171     unreachable();
0172 }
0173 
0174 /*
0175  * kdb_read
0176  *
0177  *  This function reads a string of characters, terminated by
0178  *  a newline, or by reaching the end of the supplied buffer,
0179  *  from the current kernel debugger console device.
0180  * Parameters:
0181  *  buffer  - Address of character buffer to receive input characters.
0182  *  bufsize - size, in bytes, of the character buffer
0183  * Returns:
0184  *  Returns a pointer to the buffer containing the received
0185  *  character string.  This string will be terminated by a
0186  *  newline character.
0187  * Locking:
0188  *  No locks are required to be held upon entry to this
0189  *  function.  It is not reentrant - it relies on the fact
0190  *  that while kdb is running on only one "master debug" cpu.
0191  * Remarks:
0192  *  The buffer size must be >= 2.
0193  */
0194 
0195 static char *kdb_read(char *buffer, size_t bufsize)
0196 {
0197     char *cp = buffer;
0198     char *bufend = buffer+bufsize-2;    /* Reserve space for newline
0199                          * and null byte */
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: /* backspace */
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: /* enter */
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: /* Del */
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: /* Home */
0270         if (cp > buffer) {
0271             kdb_printf("\r");
0272             kdb_printf(kdb_prompt_str);
0273             cp = buffer;
0274         }
0275         break;
0276     case 5: /* End */
0277         if (cp < lastchar) {
0278             kdb_printf("%s", cp);
0279             cp = lastchar;
0280         }
0281         break;
0282     case 2: /* Left */
0283         if (cp > buffer) {
0284             kdb_printf("\b");
0285             --cp;
0286         }
0287         break;
0288     case 14: /* Down */
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: /* Right */
0298         if (cp < lastchar) {
0299             kdb_printf("%c", *cp);
0300             ++cp;
0301         }
0302         break;
0303     case 16: /* Up */
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: /* Tab */
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; /* reset output line number */
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                 /* The kgdb transition check will hide
0385                  * printed characters if we think that
0386                  * kgdb is connecting, until the check
0387                  * fails */
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             /* Special escape to kgdb */
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  * kdb_getstr
0418  *
0419  *  Print the prompt string and read a command from the
0420  *  input device.
0421  *
0422  * Parameters:
0423  *  buffer  Address of buffer to receive command
0424  *  bufsize Size of buffer in bytes
0425  *  prompt  Pointer to string to use as prompt string
0426  * Returns:
0427  *  Pointer to command buffer.
0428  * Locking:
0429  *  None.
0430  * Remarks:
0431  *  For SMP kernels, the processor number will be
0432  *  substituted for %d, %x or %o in the prompt.
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;   /* Prompt and input resets line number */
0441     return kdb_read(buffer, bufsize);
0442 }
0443 
0444 /*
0445  * kdb_input_flush
0446  *
0447  *  Get rid of any buffered console input.
0448  *
0449  * Parameters:
0450  *  none
0451  * Returns:
0452  *  nothing
0453  * Locking:
0454  *  none
0455  * Remarks:
0456  *  Call this function whenever you want to flush input.  If there is any
0457  *  outstanding input, it ignores all characters until there has been no
0458  *  data for approximately 1ms.
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  * kdb_printf
0484  *
0485  *  Print a string to the output device(s).
0486  *
0487  * Parameters:
0488  *  printf-like format and optional args.
0489  * Returns:
0490  *  0
0491  * Locking:
0492  *  None.
0493  * Remarks:
0494  *  use 'kdbcons->write()' to avoid polluting 'log_buf' with
0495  *  kdb output.
0496  *
0497  *  If the user is doing a cmd args | grep srch
0498  *  then kdb_grepping_flag is set.
0499  *  In that case we need to accumulate full lines (ending in \n) before
0500  *  searching for the pattern.
0501  */
0502 
0503 static char kdb_buffer[256];    /* A bit too big to go on stack */
0504 static char *next_avail = kdb_buffer;
0505 static int  size_avail;
0506 static int  suspend_grep;
0507 
0508 /*
0509  * search arg1 to see if it contains arg2
0510  * (kdmain.c provides flags for ^pat and pat$)
0511  *
0512  * return 1 for found, 0 for not found
0513  */
0514 static int kdb_search_string(char *searched, char *searchfor)
0515 {
0516     char firstchar, *cp;
0517     int len1, len2;
0518 
0519     /* not counting the newline at the end of "searched" */
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          * Set oops_in_progress to encourage the console drivers to
0568          * disregard their internal spin locks: in the current calling
0569          * context the risk of deadlock is a bigger problem than risks
0570          * due to re-entering the console driver. We operate directly on
0571          * oops_in_progress rather than using bust_spinlocks() because
0572          * the calls bust_spinlocks() makes on exit are not appropriate
0573          * for this calling context.
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     /* Serialize kdb_printf if multiple cpus try to write at once.
0596      * But if any cpu goes recursive in kdb, just print the output,
0597      * even if it is interleaved with any other text.
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         /* normally, every vsnprintf starts a new buffer */
0623         next_avail = kdb_buffer;
0624         size_avail = sizeof(kdb_buffer);
0625     }
0626     vsnprintf(next_avail, size_avail, fmt, ap);
0627 
0628     /*
0629      * If kdb_parse() found that the command was cmd xxx | grep yyy
0630      * then kdb_grepping_flag is set, and kdb_grep_string contains yyy
0631      *
0632      * Accumulate the print data up to a newline before searching it.
0633      * (vsnprintf does null-terminate the string that it generates)
0634      */
0635 
0636     /* skip the search if prints are temporarily unconditional */
0637     if (!suspend_grep && kdb_grepping_flag) {
0638         cp = strchr(kdb_buffer, '\n');
0639         if (!cp) {
0640             /*
0641              * Special cases that don't end with newlines
0642              * but should be written without one:
0643              *   The "[nn]kdb> " prompt should
0644              *   appear at the front of the buffer.
0645              *
0646              *   The "[nn]more " prompt should also be
0647              *     (MOREPROMPT -> moreprompt)
0648              *   written *   but we print that ourselves,
0649              *   we set the suspend_grep flag to make
0650              *   it unconditional.
0651              *
0652              */
0653             if (next_avail == kdb_buffer) {
0654                 /*
0655                  * these should occur after a newline,
0656                  * so they will be at the front of the
0657                  * buffer
0658                  */
0659                 cp2 = kdb_buffer;
0660                 len = strlen(kdb_prompt_str);
0661                 if (!strncmp(cp2, kdb_prompt_str, len)) {
0662                     /*
0663                      * We're about to start a new
0664                      * command, so we can go back
0665                      * to normal mode.
0666                      */
0667                     kdb_grepping_flag = 0;
0668                     goto kdb_printit;
0669                 }
0670             }
0671             /* no newline; don't search/write the buffer
0672                until one is there */
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          * The newline is present; print through it or discard
0681          * it, depending on the results of the search.
0682          */
0683         cp++;            /* to byte after the newline */
0684         replaced_byte = *cp; /* remember what/where it was */
0685         cphold = cp;
0686         *cp = '\0';      /* end the string for our search */
0687 
0688         /*
0689          * We now have a newline at the end of the string
0690          * Only continue with this output if it contains the
0691          * search string.
0692          */
0693         fnd = kdb_search_string(kdb_buffer, kdb_grep_string);
0694         if (!fnd) {
0695             /*
0696              * At this point the complete line at the start
0697              * of kdb_buffer can be discarded, as it does
0698              * not contain what the user is looking for.
0699              * Shift the buffer left.
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              * This was a interactive search (using '/' at more
0711              * prompt) and it has completed. Replace the \0 with
0712              * its original value to ensure multi-line strings
0713              * are handled properly, and return to normal mode.
0714              */
0715             *cphold = replaced_byte;
0716             kdb_grepping_flag = 0;
0717         }
0718         /*
0719          * at this point the string is a full line and
0720          * should be printed, up to the null.
0721          */
0722     }
0723 kdb_printit:
0724 
0725     /*
0726      * Write to all consoles.
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          * Check printed string to decide how to bump the
0747          * kdb_nextline to control when the more prompt should
0748          * show up.
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     /* check for having reached the LINES number of printed lines */
0766     if (kdb_nextline >= linecount) {
0767         char ch;
0768 
0769         /* Watch out for recursion here.  Any routine that calls
0770          * kdb_printf will come back through here.  And kdb_read
0771          * uses kdb_printf to echo on serial consoles ...
0772          */
0773         kdb_nextline = 1;   /* In case of recursion */
0774 
0775         /*
0776          * Pause until cr.
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;   /* Really set output line 1 */
0790 
0791         /* empty and reset the buffer: */
0792         kdb_buffer[0] = '\0';
0793         next_avail = kdb_buffer;
0794         size_avail = sizeof(kdb_buffer);
0795         if ((ch == 'q') || (ch == 'Q')) {
0796             /* user hit q or Q */
0797             KDB_FLAG_SET(CMD_INTERRUPT); /* command interrupted */
0798             KDB_STATE_CLEAR(PAGER);
0799             /* end of command output; back to normal mode */
0800             kdb_grepping_flag = 0;
0801             kdb_printf("\n");
0802         } else if (ch == ' ') {
0803             kdb_printf("\r");
0804             suspend_grep = 1; /* for this recursion */
0805         } else if (ch == '\n' || ch == '\r') {
0806             kdb_nextline = linecount - 1;
0807             kdb_printf("\r");
0808             suspend_grep = 1; /* for this recursion */
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; /* for this recursion */
0816         } else if (ch) {
0817             /* user hit something unexpected */
0818             suspend_grep = 1; /* for this recursion */
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             /* user hit enter */
0828             suspend_grep = 1; /* for this recursion */
0829             kdb_printf("\n");
0830         }
0831         kdb_input_flush();
0832     }
0833 
0834     /*
0835      * For grep searches, shift the printed string left.
0836      *  replaced_byte contains the character that was overwritten with
0837      *  the terminating null, and cphold points to the null.
0838      * Then adjust the notion of available space in the buffer.
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; /* end of what may have been a recursive call */
0850     if (logging)
0851         console_loglevel = saved_loglevel;
0852     /* kdb_printf_cpu locked the code above. */
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);