Merge tag 'for_linux-3.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jwesse...
[linux-3.10.git] / kernel / debug / kdb / kdb_io.c
index 9e3cec7..0a69d2a 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/smp.h>
 #include <linux/nmi.h>
 #include <linux/delay.h>
+#include <linux/kgdb.h>
 #include <linux/kdb.h>
 #include <linux/kallsyms.h>
 #include "kdb_private.h"
 #define CMD_BUFLEN 256
 char kdb_prompt_str[CMD_BUFLEN];
 
+int kdb_trap_printk;
 
-static void kgdb_transition_check(char *buffer)
+static int kgdb_transition_check(char *buffer)
 {
-       int slen = strlen(buffer);
-       if (strncmp(buffer, "$?#3f", slen) != 0 &&
-           strncmp(buffer, "$qSupported#37", slen) != 0 &&
-           strncmp(buffer, "+$qSupported#37", slen) != 0) {
+       if (buffer[0] != '+' && buffer[0] != '$') {
                KDB_STATE_SET(KGDB_TRANS);
                kdb_printf("%s", buffer);
+       } else {
+               int slen = strlen(buffer);
+               if (slen > 3 && buffer[slen - 3] == '#') {
+                       kdb_gdb_state_pass(buffer);
+                       strcpy(buffer, "kgdb");
+                       KDB_STATE_SET(DOING_KGDB);
+                       return 1;
+               }
        }
+       return 0;
 }
 
 static int kdb_read_get_key(char *buffer, size_t bufsize)
@@ -249,6 +257,10 @@ poll_again:
        case 13: /* enter */
                *lastchar++ = '\n';
                *lastchar++ = '\0';
+               if (!KDB_STATE(KGDB_TRANS)) {
+                       KDB_STATE_SET(KGDB_TRANS);
+                       kdb_printf("%s", buffer);
+               }
                kdb_printf("\n");
                return buffer;
        case 4: /* Del */
@@ -380,22 +392,26 @@ poll_again:
                                 * printed characters if we think that
                                 * kgdb is connecting, until the check
                                 * fails */
-                               if (!KDB_STATE(KGDB_TRANS))
-                                       kgdb_transition_check(buffer);
-                               else
+                               if (!KDB_STATE(KGDB_TRANS)) {
+                                       if (kgdb_transition_check(buffer))
+                                               return buffer;
+                               } else {
                                        kdb_printf("%c", key);
+                               }
                        }
                        /* Special escape to kgdb */
                        if (lastchar - buffer >= 5 &&
                            strcmp(lastchar - 5, "$?#3f") == 0) {
+                               kdb_gdb_state_pass(lastchar - 5);
                                strcpy(buffer, "kgdb");
                                KDB_STATE_SET(DOING_KGDB);
                                return buffer;
                        }
-                       if (lastchar - buffer >= 14 &&
-                           strcmp(lastchar - 14, "$qSupported#37") == 0) {
+                       if (lastchar - buffer >= 11 &&
+                           strcmp(lastchar - 11, "$qSupported") == 0) {
+                               kdb_gdb_state_pass(lastchar - 11);
                                strcpy(buffer, "kgdb");
-                               KDB_STATE_SET(DOING_KGDB2);
+                               KDB_STATE_SET(DOING_KGDB);
                                return buffer;
                        }
                }
@@ -532,12 +548,12 @@ static int kdb_search_string(char *searched, char *searchfor)
        return 0;
 }
 
-int kdb_printf(const char *fmt, ...)
+int vkdb_printf(const char *fmt, va_list ap)
 {
-       va_list ap;
        int diag;
        int linecount;
        int logging, saved_loglevel = 0;
+       int saved_trap_printk;
        int got_printf_lock = 0;
        int retlen = 0;
        int fnd, len;
@@ -548,6 +564,9 @@ int kdb_printf(const char *fmt, ...)
        unsigned long uninitialized_var(flags);
 
        preempt_disable();
+       saved_trap_printk = kdb_trap_printk;
+       kdb_trap_printk = 0;
+
        /* Serialize kdb_printf if multiple cpus try to write at once.
         * But if any cpu goes recursive in kdb, just print the output,
         * even if it is interleaved with any other text.
@@ -574,9 +593,7 @@ int kdb_printf(const char *fmt, ...)
                next_avail = kdb_buffer;
                size_avail = sizeof(kdb_buffer);
        }
-       va_start(ap, fmt);
        vsnprintf(next_avail, size_avail, fmt, ap);
-       va_end(ap);
 
        /*
         * If kdb_parse() found that the command was cmd xxx | grep yyy
@@ -669,10 +686,22 @@ kdb_printit:
         * Write to all consoles.
         */
        retlen = strlen(kdb_buffer);
-       while (c) {
-               c->write(c, kdb_buffer, retlen);
-               touch_nmi_watchdog();
-               c = c->next;
+       if (!dbg_kdb_mode && kgdb_connected) {
+               gdbstub_msg_write(kdb_buffer, retlen);
+       } else {
+               if (dbg_io_ops && !dbg_io_ops->is_console) {
+                       len = strlen(kdb_buffer);
+                       cp = kdb_buffer;
+                       while (len--) {
+                               dbg_io_ops->write_char(*cp);
+                               cp++;
+                       }
+               }
+               while (c) {
+                       c->write(c, kdb_buffer, retlen);
+                       touch_nmi_watchdog();
+                       c = c->next;
+               }
        }
        if (logging) {
                saved_loglevel = console_loglevel;
@@ -686,9 +715,6 @@ kdb_printit:
        /* check for having reached the LINES number of printed lines */
        if (kdb_nextline == linecount) {
                char buf1[16] = "";
-#if defined(CONFIG_SMP)
-               char buf2[32];
-#endif
 
                /* Watch out for recursion here.  Any routine that calls
                 * kdb_printf will come back through here.  And kdb_read
@@ -703,17 +729,17 @@ kdb_printit:
                if (moreprompt == NULL)
                        moreprompt = "more> ";
 
-#if defined(CONFIG_SMP)
-               if (strchr(moreprompt, '%')) {
-                       sprintf(buf2, moreprompt, get_cpu());
-                       put_cpu();
-                       moreprompt = buf2;
-               }
-#endif
-
                kdb_input_flush();
                c = console_drivers;
 
+               if (dbg_io_ops && !dbg_io_ops->is_console) {
+                       len = strlen(moreprompt);
+                       cp = moreprompt;
+                       while (len--) {
+                               dbg_io_ops->write_char(*cp);
+                               cp++;
+                       }
+               }
                while (c) {
                        c->write(c, moreprompt, strlen(moreprompt));
                        touch_nmi_watchdog();
@@ -784,6 +810,20 @@ kdb_print_out:
        } else {
                __release(kdb_printf_lock);
        }
+       kdb_trap_printk = saved_trap_printk;
        preempt_enable();
        return retlen;
 }
+
+int kdb_printf(const char *fmt, ...)
+{
+       va_list ap;
+       int r;
+
+       va_start(ap, fmt);
+       r = vkdb_printf(fmt, ap);
+       va_end(ap);
+
+       return r;
+}
+EXPORT_SYMBOL_GPL(kdb_printf);