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