/* * Kernel Debugger Architecture Independent Main Code * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * Copyright (C) 1999-2004 Silicon Graphics, Inc. All Rights Reserved. * Copyright (C) 2000 Stephane Eranian <eranian@hpl.hp.com> * Xscale (R) modifications copyright (C) 2003 Intel Corporation. * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
*/
char kdb_grep_string[KDB_GREP_STRLEN]; int kdb_grepping_flag;
EXPORT_SYMBOL(kdb_grepping_flag); int kdb_grep_leading; int kdb_grep_trailing;
/* * Kernel debugger state flags
*/ unsignedint kdb_flags;
/* * kdb_lock protects updates to kdb_initial_cpu. Used to * single thread processors through the kernel debugger.
*/ int kdb_initial_cpu = -1; /* cpu number that owns kdb */ int kdb_nextline = 1; int kdb_state; /* General KDB state */
/* kdb_cmds_head describes the available commands. */ static LIST_HEAD(kdb_cmds_head);
typedefstruct _kdbmsg { int km_diag; /* kdb diagnostic */ char *km_msg; /* Corresponding message text */
} kdbmsg_t;
#define KDBMSG(msgnum, text) \
{ KDB_##msgnum, text }
static kdbmsg_t kdbmsgs[] = {
KDBMSG(NOTFOUND, "Command Not Found"),
KDBMSG(ARGCOUNT, "Improper argument count, see usage."),
KDBMSG(BADWIDTH, "Illegal value for BYTESPERWORD use 1, 2, 4 or 8, " "8 is only allowed on 64 bit systems"),
KDBMSG(BADRADIX, "Illegal value for RADIX use 8, 10 or 16"),
KDBMSG(NOTENV, "Cannot find environment variable"),
KDBMSG(NOENVVALUE, "Environment variable should have value"),
KDBMSG(NOTIMP, "Command not implemented"),
KDBMSG(ENVFULL, "Environment full"),
KDBMSG(KMALLOCFAILED, "Failed to allocate memory"),
KDBMSG(TOOMANYBPT, "Too many breakpoints defined"), #ifdef CONFIG_CPU_XSCALE
KDBMSG(TOOMANYDBREGS, "More breakpoints than ibcr registers defined"), #else
KDBMSG(TOOMANYDBREGS, "More breakpoints than db registers defined"), #endif
KDBMSG(DUPBPT, "Duplicate breakpoint address"),
KDBMSG(BPTNOTFOUND, "Breakpoint not found"),
KDBMSG(BADMODE, "Invalid IDMODE"),
KDBMSG(BADINT, "Illegal numeric value"),
KDBMSG(INVADDRFMT, "Invalid symbolic address format"),
KDBMSG(BADREG, "Invalid register name"),
KDBMSG(BADCPUNUM, "Invalid cpu number"),
KDBMSG(BADLENGTH, "Invalid length field"),
KDBMSG(NOBP, "No Breakpoint exists"),
KDBMSG(BADADDR, "Invalid address"),
KDBMSG(NOPERM, "Permission denied"),
}; #undef KDBMSG
staticconstint __nkdb_err = ARRAY_SIZE(kdbmsgs);
/* * Initial environment. This is all kept static and local to this file. * The entire environment is limited to a fixed number of entries * (add more to __env[] if required)
*/
/* * Update the permissions flags (kdb_cmd_enabled) to match the * current lockdown state. * * Within this function the calls to security_locked_down() are "lazy". We * avoid calling them if the current value of kdb_cmd_enabled already excludes * flags that might be subject to lockdown. Additionally we deliberately check * the lockdown flags independently (even though read lockdown implies write * lockdown) since that results in both simpler code and clearer messages to * the user on first-time debugger entry. * * The permission masks during a read+write lockdown permits the following * flags: INSPECT, SIGNAL, REBOOT (and ALWAYS_SAFE). * * The INSPECT commands are not blocked during lockdown because they are * not arbitrary memory reads. INSPECT covers the backtrace family (sometimes * forcing them to have no arguments) and lsmod. These commands do expose * some kernel state but do not allow the developer seated at the console to * choose what state is reported. SIGNAL and REBOOT should not be controversial, * given these are allowed for root during lockdown already.
*/ staticvoid kdb_check_for_lockdown(void)
{ constint write_flags = KDB_ENABLE_MEM_WRITE |
KDB_ENABLE_REG_WRITE |
KDB_ENABLE_FLOW_CTRL; constint read_flags = KDB_ENABLE_MEM_READ |
KDB_ENABLE_REG_READ;
if (kdb_cmd_enabled & (KDB_ENABLE_ALL | write_flags))
need_to_lockdown_write =
security_locked_down(LOCKDOWN_DBG_WRITE_KERNEL);
if (kdb_cmd_enabled & (KDB_ENABLE_ALL | read_flags))
need_to_lockdown_read =
security_locked_down(LOCKDOWN_DBG_READ_KERNEL);
/* De-compose KDB_ENABLE_ALL if required */ if (need_to_lockdown_write || need_to_lockdown_read) if (kdb_cmd_enabled & KDB_ENABLE_ALL)
kdb_cmd_enabled = KDB_ENABLE_MASK & ~KDB_ENABLE_ALL;
if (need_to_lockdown_write)
kdb_cmd_enabled &= ~write_flags;
if (need_to_lockdown_read)
kdb_cmd_enabled &= ~read_flags;
}
/* * Check whether the flags of the current command, the permissions of the kdb * console and the lockdown state allow a command to be run.
*/ staticbool kdb_check_flags(kdb_cmdflags_t flags, int permissions, bool no_args)
{ /* permissions comes from userspace so needs massaging slightly */
permissions &= KDB_ENABLE_MASK;
permissions |= KDB_ENABLE_ALWAYS_SAFE;
/* some commands change group when launched with no arguments */ if (no_args)
permissions |= permissions << KDB_ENABLE_NO_ARGS_SHIFT;
flags |= KDB_ENABLE_ALL;
return permissions & flags;
}
/* * kdbgetenv - This function will return the character string value of * an environment variable. * Parameters: * match A character string representing an environment variable. * Returns: * NULL No environment variable matches 'match' * char* Pointer to string value of environment variable.
*/ char *kdbgetenv(constchar *match)
{ char **ep = __env; int matchlen = strlen(match); int i;
/* * kdbgetulenv - This function will return the value of an unsigned * long-valued environment variable. * Parameters: * match A character string representing a numeric value * Outputs: * *value the unsigned long representation of the env variable 'match' * Returns: * Zero on success, a kdb diagnostic on failure.
*/ staticint kdbgetulenv(constchar *match, unsignedlong *value)
{ char *ep;
ep = kdbgetenv(match); if (!ep) return KDB_NOTENV; if (strlen(ep) == 0) return KDB_NOENVVALUE; if (kstrtoul(ep, 0, value)) return KDB_BADINT;
return 0;
}
/* * kdbgetintenv - This function will return the value of an * integer-valued environment variable. * Parameters: * match A character string representing an integer-valued env variable * Outputs: * *value the integer representation of the environment variable 'match' * Returns: * Zero on success, a kdb diagnostic on failure.
*/ int kdbgetintenv(constchar *match, int *value)
{ unsignedlong val; int diag;
/* * kdb_setenv() - Alter an existing environment variable or create a new one. * @var: Name of the variable * @val: Value of the variable * * Return: Zero on success, a kdb diagnostic on failure.
*/ staticint kdb_setenv(constchar *var, constchar *val)
{ int i; char *ep;
size_t varlen, vallen;
varlen = strlen(var);
vallen = strlen(val);
ep = kmalloc(varlen + vallen + 2, GFP_KDB); if (!ep) return KDB_KMALLOCFAILED;
sprintf(ep, "%s=%s", var, val);
for (i = 0; i < __nenv; i++) { if (__env[i]
&& ((strncmp(__env[i], var, varlen) == 0)
&& ((__env[i][varlen] == '\0')
|| (__env[i][varlen] == '=')))) {
kfree_const(__env[i]);
__env[i] = ep; return 0;
}
}
/* * Wasn't existing variable. Fit into slot.
*/ for (i = 0; i < __nenv-1; i++) { if (__env[i] == (char *)0) {
__env[i] = ep; return 0;
}
}
return KDB_ENVFULL;
}
/* * kdb_printenv() - Display the current environment variables.
*/ staticvoid kdb_printenv(void)
{ int i;
for (i = 0; i < __nenv; i++) { if (__env[i])
kdb_printf("%s\n", __env[i]);
}
}
/* * kdbgetularg - This function will convert a numeric string into an * unsigned long value. * Parameters: * arg A character string representing a numeric value * Outputs: * *value the unsigned long representation of arg. * Returns: * Zero on success, a kdb diagnostic on failure.
*/ int kdbgetularg(constchar *arg, unsignedlong *value)
{ if (kstrtoul(arg, 0, value)) return KDB_BADINT; return 0;
}
int kdbgetu64arg(constchar *arg, u64 *value)
{ if (kstrtou64(arg, 0, value)) return KDB_BADINT; return 0;
}
/* * kdb_set - This function implements the 'set' command. Alter an * existing environment variable or create a new one.
*/ int kdb_set(int argc, constchar **argv)
{ /* * we can be invoked two ways: * set var=value argv[1]="var", argv[2]="value" * set var = value argv[1]="var", argv[2]="=", argv[3]="value" * - if the latter, shift 'em down.
*/ if (argc == 3) {
argv[2] = argv[3];
argc--;
}
/* * Tokenizer squashed the '=' sign. argv[1] is variable * name, argv[2] = value.
*/ return kdb_setenv(argv[1], argv[2]);
}
staticint kdb_check_regs(void)
{ if (!kdb_current_regs) {
kdb_printf("No current kdb registers." " You may need to select another task\n"); return KDB_BADREG;
} return 0;
}
/* * kdbgetaddrarg - This function is responsible for parsing an * address-expression and returning the value of the expression, * symbol name, and offset to the caller. * * The argument may consist of a numeric value (decimal or * hexadecimal), a symbol name, a register name (preceded by the * percent sign), an environment variable with a numeric value * (preceded by a dollar sign) or a simple arithmetic expression * consisting of a symbol name, +/-, and a numeric constant value * (offset). * Parameters: * argc - count of arguments in argv * argv - argument vector * *nextarg - index to next unparsed argument in argv[] * regs - Register state at time of KDB entry * Outputs: * *value - receives the value of the address-expression * *offset - receives the offset specified, if any * *name - receives the symbol name, if any * *nextarg - index to next unparsed argument in argv[] * Returns: * zero is returned on success, a kdb diagnostic code is * returned on error.
*/ int kdbgetaddrarg(int argc, constchar **argv, int *nextarg, unsignedlong *value, long *offset, char **name)
{ unsignedlong addr; unsignedlong off = 0; int positive; int diag; int found = 0; char *symname; char symbol = '\0'; char *cp;
kdb_symtab_t symtab;
/* * If the enable flags prohibit both arbitrary memory access * and flow control then there are no reasonable grounds to * provide symbol lookup.
*/ if (!kdb_check_flags(KDB_ENABLE_MEM_READ | KDB_ENABLE_FLOW_CTRL,
kdb_cmd_enabled, false)) return KDB_NOPERM;
/* * Process arguments which follow the following syntax: * * symbol | numeric-address [+/- numeric-offset] * %register * $environment-variable
*/
if (*nextarg > argc) return KDB_ARGCOUNT;
symname = (char *)argv[*nextarg];
/* * If there is no whitespace between the symbol * or address and the '+' or '-' symbols, we * remember the character and replace it with a * null so the symbol/value can be properly parsed
*/
cp = strpbrk(symname, "+-"); if (cp != NULL) {
symbol = *cp;
*cp++ = '\0';
}
if (symname[0] == '$') {
diag = kdbgetulenv(&symname[1], &addr); if (diag) return diag;
} elseif (symname[0] == '%') {
diag = kdb_check_regs(); if (diag) return diag; /* Implement register values with % at a later time as it is * arch optional.
*/ return KDB_NOTIMP;
} else {
found = kdbgetsymval(symname, &symtab); if (found) {
addr = symtab.sym_start;
} else {
diag = kdbgetularg(argv[*nextarg], &addr); if (diag) return diag;
}
}
if (!found)
found = kdbnearsym(addr, &symtab);
(*nextarg)++;
if (name)
*name = symname; if (value)
*value = addr; if (offset && name && *name)
*offset = addr - symtab.sym_start;
if ((*nextarg > argc)
&& (symbol == '\0')) return 0;
/* * Now there must be an offset!
*/ if ((*nextarg > argc)
&& (symbol == '\0')) { return KDB_INVADDRFMT;
}
if (!symbol) {
cp = (char *)argv[*nextarg];
(*nextarg)++;
}
diag = kdbgetularg(cp, &off); if (diag) return diag;
if (!positive)
off = -off;
if (offset)
*offset += off;
if (value)
*value += off;
return 0;
}
staticvoid kdb_cmderror(int diag)
{ int i;
if (diag >= 0) {
kdb_printf("no error detected (diagnostic is %d)\n", diag); return;
}
for (i = 0; i < __nkdb_err; i++) { if (kdbmsgs[i].km_diag == diag) {
kdb_printf("diag: %d: %s\n", diag, kdbmsgs[i].km_msg); return;
}
}
kdb_printf("Unknown diag %d\n", -diag);
}
/* * kdb_defcmd, kdb_defcmd2 - This function implements the 'defcmd' * command which defines one command as a set of other commands, * terminated by endefcmd. kdb_defcmd processes the initial * 'defcmd' command, kdb_defcmd2 is invoked from kdb_parse for * the following commands until 'endefcmd'. * Inputs: * argc argument count * argv argument vector * Returns: * zero for success, a kdb diagnostic if error
*/ struct kdb_macro {
kdbtab_t cmd; /* Macro command */ struct list_head statements; /* Associated statement list */
};
struct kdb_macro_statement { char *statement; /* Statement text */ struct list_head list_node; /* Statement list node */
};
INIT_LIST_HEAD(&kdb_macro->statements);
defcmd_in_progress = true; return 0;
fail_help:
kfree(mp->usage);
fail_usage:
kfree(mp->name);
fail_name:
kfree(kdb_macro);
fail_defcmd:
kdb_printf("Could not allocate new kdb_macro entry for %s\n", argv[1]); return KDB_NOTIMP;
}
/* * kdb_exec_defcmd - Execute the set of commands associated with this * defcmd name. * Inputs: * argc argument count * argv argument vector * Returns: * zero for success, a kdb diagnostic if error
*/ staticint kdb_exec_defcmd(int argc, constchar **argv)
{ int ret;
kdbtab_t *kp; struct kdb_macro *kmp; struct kdb_macro_statement *kms;
if (argc != 0) return KDB_ARGCOUNT;
list_for_each_entry(kp, &kdb_cmds_head, list_node) { if (strcmp(kp->name, argv[0]) == 0) break;
} if (list_entry_is_head(kp, &kdb_cmds_head, list_node)) {
kdb_printf("kdb_exec_defcmd: could not find commands for %s\n",
argv[0]); return KDB_NOTIMP;
}
kmp = container_of(kp, struct kdb_macro, cmd);
list_for_each_entry(kms, &kmp->statements, list_node) { /* * Recursive use of kdb_parse, do not use argv after this point.
*/
argv = NULL;
kdb_printf("[%s]kdb> %s\n", kmp->cmd.name, kms->statement);
ret = kdb_parse(kms->statement); if (ret) return ret;
} return 0;
}
/* * The "str" argument may point to something like | grep xyz
*/ staticvoid parse_grep(constchar *str)
{ int len; char *cp = (char *)str, *cp2;
/* sanity check: we should have been called with the \ first */ if (*cp != '|') return;
cp++; while (isspace(*cp))
cp++; if (!str_has_prefix(cp, "grep ")) {
kdb_printf("invalid 'pipe', see grephelp\n"); return;
}
cp += 5; while (isspace(*cp))
cp++;
cp2 = strchr(cp, '\n'); if (cp2)
*cp2 = '\0'; /* remove the trailing newline */
len = strlen(cp); if (len == 0) {
kdb_printf("invalid 'pipe', see grephelp\n"); return;
} /* now cp points to a nonzero length search string */ if (*cp == '"') { /* allow it be "x y z" by removing the "'s - there must
be two of them */
cp++;
cp2 = strchr(cp, '"'); if (!cp2) {
kdb_printf("invalid quoted string, see grephelp\n"); return;
}
*cp2 = '\0'; /* end the string where the 2nd " was */
}
kdb_grep_leading = 0; if (*cp == '^') {
kdb_grep_leading = 1;
cp++;
}
len = strlen(cp);
kdb_grep_trailing = 0; if (*(cp+len-1) == '$') {
kdb_grep_trailing = 1;
*(cp+len-1) = '\0';
}
len = strlen(cp); if (!len) return; if (len >= KDB_GREP_STRLEN) {
kdb_printf("search string too long\n"); return;
}
strcpy(kdb_grep_string, cp);
kdb_grepping_flag++; return;
}
/* * kdb_parse - Parse the command line, search the command table for a * matching command and invoke the command function. This * function may be called recursively, if it is, the second call * will overwrite argv and cbuf. It is the caller's * responsibility to save their argv if they recursively call * kdb_parse(). * Parameters: * cmdstr The input command line to be parsed. * regs The registers at the time kdb was entered. * Returns: * Zero for success, a kdb diagnostic if failure. * Remarks: * Limited to 20 tokens. * * Real rudimentary tokenization. Basically only whitespace * is considered a token delimiter (but special consideration * is taken of the '=' sign as used by the 'set' command). * * The algorithm used to tokenize the input string relies on * there being at least one whitespace (or otherwise useless) * character between tokens as the character immediately following * the token is altered in-place to a null-byte to terminate the * token string.
*/
/* * First tokenize the command string.
*/
cp = (char *)cmdstr;
if (KDB_FLAG(CMD_INTERRUPT)) { /* Previous command was interrupted, newline must not
* repeat the command */
KDB_FLAG_CLEAR(CMD_INTERRUPT);
KDB_STATE_SET(PAGER);
argc = 0; /* no repeat */
}
if (*cp != '\n' && *cp != '\0') {
argc = 0;
cpp = cbuf; while (*cp) { /* skip whitespace */ while (isspace(*cp))
cp++; if ((*cp == '\0') || (*cp == '\n') ||
(*cp == '#' && !defcmd_in_progress)) break; /* special case: check for | grep pattern */ if (*cp == '|') {
check_grep++; break;
} if (cpp >= cbuf + CMD_BUFLEN) {
kdb_printf("kdb_parse: command buffer " "overflow, command ignored\n%s\n",
cmdstr); return KDB_NOTFOUND;
} if (argc >= MAXARGC - 1) {
kdb_printf("kdb_parse: too many arguments, " "command ignored\n%s\n", cmdstr); return KDB_NOTFOUND;
}
argv[argc++] = cpp;
escaped = 0;
quoted = '\0'; /* Copy to next unquoted and unescaped
* whitespace or '=' */ while (*cp && *cp != '\n' &&
(escaped || quoted || !isspace(*cp))) { if (cpp >= cbuf + CMD_BUFLEN) break; if (escaped) {
escaped = 0;
*cpp++ = *cp++; continue;
} if (*cp == '\\') {
escaped = 1;
++cp; continue;
} if (*cp == quoted)
quoted = '\0'; elseif (*cp == '\'' || *cp == '"')
quoted = *cp;
*cpp = *cp++; if (*cpp == '=' && !quoted) break;
++cpp;
}
*cpp++ = '\0'; /* Squash a ws or '=' character */
}
} if (!argc) return 0; if (check_grep)
parse_grep(cp); if (defcmd_in_progress) { int result = kdb_defcmd2(cmdstr, argv[0]); if (!defcmd_in_progress) {
argc = 0; /* avoid repeat on endefcmd */
*(argv[0]) = '\0';
} return result;
} if (argv[0][0] == '-' && argv[0][1] &&
(argv[0][1] < '0' || argv[0][1] > '9')) {
ignore_errors = 1;
++argv[0];
}
list_for_each_entry(tp, &kdb_cmds_head, list_node) { /* * If this command is allowed to be abbreviated, * check to see if this is it.
*/ if (tp->minlen && (strlen(argv[0]) <= tp->minlen) &&
(strncmp(argv[0], tp->name, tp->minlen) == 0)) break;
if (strcmp(argv[0], tp->name) == 0) break;
}
/* * If we don't find a command by this name, see if the first * few characters of this match any of the known commands. * e.g., md1c20 should match md.
*/ if (list_entry_is_head(tp, &kdb_cmds_head, list_node)) {
list_for_each_entry(tp, &kdb_cmds_head, list_node) { if (strncmp(argv[0], tp->name, strlen(tp->name)) == 0) break;
}
}
if (!list_entry_is_head(tp, &kdb_cmds_head, list_node)) { int result;
if (!kdb_check_flags(tp->flags, kdb_cmd_enabled, argc <= 1)) return KDB_NOPERM;
KDB_STATE_SET(CMD);
result = (*tp->func)(argc-1, (constchar **)argv); if (result && ignore_errors && result > KDB_CMD_GO)
result = 0;
KDB_STATE_CLEAR(CMD);
if (tp->flags & KDB_REPEAT_WITH_ARGS) return result;
/* * If the input with which we were presented does not * map to an existing command, attempt to parse it as an * address argument and display the result. Useful for * obtaining the address of a variable, or the nearest symbol * to an address contained in a register.
*/
{ unsignedlong value; char *name = NULL; long offset; int nextarg = 0;
/* * kdb_reboot - This function implements the 'reboot' command. Reboot * the system immediately, or loop for ever on failure.
*/ staticint kdb_reboot(int argc, constchar **argv)
{
emergency_restart();
kdb_printf("Hmm, kdb_reboot did not reboot, spinning here\n"); while (1)
cpu_relax(); /* NOTREACHED */ return 0;
}
staticvoid drop_newline(char *buf)
{
size_t len = strlen(buf);
if (len == 0) return; if (*(buf + len - 1) == '\n')
*(buf + len - 1) = '\0';
}
/* * kdb_local - The main code for kdb. This routine is invoked on a * specific processor, it is not global. The main kdb() routine * ensures that only one processor at a time is in this routine. * This code is called with the real reason code on the first * entry to a kdb session, thereafter it is called with reason * SWITCH, even if the user goes back to the original cpu. * Inputs: * reason The reason KDB was invoked * error The hardware-defined error code * regs The exception frame at time of fault/breakpoint. * db_result Result code from the break or debug point. * Returns: * 0 KDB was invoked for an event which it wasn't responsible * 1 KDB handled the event for which it was invoked. * KDB_CMD_GO User typed 'go'. * KDB_CMD_CPU User switched to another cpu. * KDB_CMD_SS Single step.
*/ staticint kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
kdb_dbtrap_t db_result)
{ char *cmdbuf; int diag; struct task_struct *kdb_current =
curr_task(raw_smp_processor_id());
switch (reason) { case KDB_REASON_DEBUG:
{ /* * If re-entering kdb after a single step * command, don't print the message.
*/ switch (db_result) { case KDB_DB_BPT:
kdb_printf("\nEntering kdb (0x%px, pid %d) ",
kdb_current, kdb_current->pid); #ifdefined(CONFIG_SMP)
kdb_printf("on processor %d ", raw_smp_processor_id()); #endif
kdb_printf("due to Debug @ " kdb_machreg_fmt "\n",
instruction_pointer(regs)); break; case KDB_DB_SS: break; case KDB_DB_SSBPT:
KDB_DEBUG_STATE("kdb_local 4", reason); return 1; /* kdba_db_trap did the work */ default:
kdb_printf("kdb: Bad result from kdba_db_trap: %d\n",
db_result); break;
}
} break; case KDB_REASON_ENTER: if (KDB_STATE(KEYBOARD))
kdb_printf("due to Keyboard Entry\n"); else
kdb_printf("due to KDB_ENTER()\n"); break; case KDB_REASON_KEYBOARD:
KDB_STATE_SET(KEYBOARD);
kdb_printf("due to Keyboard Entry\n"); break; case KDB_REASON_ENTER_SLAVE: /* drop through, slaves only get released via cpu switch */ case KDB_REASON_SWITCH:
kdb_printf("due to cpu switch\n"); break; case KDB_REASON_OOPS:
kdb_printf("Oops: %s\n", kdb_diemsg);
kdb_printf("due to oops @ " kdb_machreg_fmt "\n",
instruction_pointer(regs));
kdb_dumpregs(regs); break; case KDB_REASON_SYSTEM_NMI:
kdb_printf("due to System NonMaskable Interrupt\n"); break; case KDB_REASON_NMI:
kdb_printf("due to NonMaskable Interrupt @ "
kdb_machreg_fmt "\n",
instruction_pointer(regs)); break; case KDB_REASON_SSTEP: case KDB_REASON_BREAK:
kdb_printf("due to %s @ " kdb_machreg_fmt "\n",
reason == KDB_REASON_BREAK ? "Breakpoint" : "SS trap", instruction_pointer(regs)); /* * Determine if this breakpoint is one that we * are interested in.
*/ if (db_result != KDB_DB_BPT) {
kdb_printf("kdb: error return from kdba_bp_trap: %d\n",
db_result);
KDB_DEBUG_STATE("kdb_local 6", reason); return 0; /* Not for us, dismiss it */
} break; case KDB_REASON_RECURSE:
kdb_printf("due to Recursion @ " kdb_machreg_fmt "\n",
instruction_pointer(regs)); break; default:
kdb_printf("kdb: unexpected reason code: %d\n", reason);
KDB_DEBUG_STATE("kdb_local 8", reason); return 0; /* Not for us, dismiss it */
}
while (1) { /* * Initialize pager context.
*/
kdb_nextline = 1;
KDB_STATE_CLEAR(SUPPRESS);
kdb_grepping_flag = 0; /* ensure the old search does not leak into '/' commands */
kdb_grep_string[0] = '\0';
do_full_getstr: /* PROMPT can only be set if we have MEM_READ permission. */
snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
raw_smp_processor_id());
if (diag)
kdb_cmderror(diag);
}
KDB_DEBUG_STATE("kdb_local 9", diag); return diag;
}
/* * kdb_print_state - Print the state data for the current processor * for debugging. * Inputs: * text Identifies the debug point * value Any integer value to be printed, e.g. reason code.
*/ void kdb_print_state(constchar *text, int value)
{
kdb_printf("state: %s cpu %d value %d initial %d state %x\n",
text, raw_smp_processor_id(), value, kdb_initial_cpu,
kdb_state);
}
/* * kdb_main_loop - After initial setup and assignment of the * controlling cpu, all cpus are in this loop. One cpu is in * control and will issue the kdb prompt, the others will spin * until 'go' or cpu switch. * * To get a consistent view of the kernel stacks for all * processes, this routine is invoked from the main kdb code via * an architecture specific routine. kdba_main_loop is * responsible for making the kernel stacks consistent for all * processes, there should be no difference between a blocked * process and a running process as far as kdb is concerned. * Inputs: * reason The reason KDB was invoked * error The hardware-defined error code * reason2 kdb's current reason code. * Initially error but can change * according to kdb state. * db_result Result code from break or debug point. * regs The exception frame at time of fault/breakpoint. * should always be valid. * Returns: * 0 KDB was invoked for an event which it wasn't responsible * 1 KDB handled the event for which it was invoked.
*/ int kdb_main_loop(kdb_reason_t reason, kdb_reason_t reason2, int error,
kdb_dbtrap_t db_result, struct pt_regs *regs)
{ int result = 1; /* Stay in kdb() until 'go', 'ss[b]' or an error */ while (1) { /* * All processors except the one that is in control * will spin here.
*/
KDB_DEBUG_STATE("kdb_main_loop 1", reason); while (KDB_STATE(HOLD_CPU)) { /* state KDB is turned off by kdb_cpu to see if the * other cpus are still live, each cpu in this loop * turns it back on.
*/ if (!KDB_STATE(KDB))
KDB_STATE_SET(KDB);
}
KDB_STATE_CLEAR(SUPPRESS);
KDB_DEBUG_STATE("kdb_main_loop 2", reason); if (KDB_STATE(LEAVING)) break; /* Another cpu said 'go' */ /* Still using kdb, this processor is in control */
result = kdb_local(reason2, error, regs, db_result);
KDB_DEBUG_STATE("kdb_main_loop 3", result);
if (result == KDB_CMD_CPU) break;
if (result == KDB_CMD_SS) {
KDB_STATE_SET(DOING_SS); break;
}
if (result == KDB_CMD_KGDB) { if (!KDB_STATE(DOING_KGDB))
kdb_printf("Entering please attach debugger " "or use $D#44+ or $3#33\n"); break;
} if (result && result != 1 && result != KDB_CMD_GO)
kdb_printf("\nUnexpected kdb_local return code %d\n",
result);
KDB_DEBUG_STATE("kdb_main_loop 4", reason); break;
} if (KDB_STATE(DOING_SS))
KDB_STATE_CLEAR(SSBPT);
/* Clean up any keyboard devices before leaving */
kdb_kbd_cleanup_state();
return result;
}
/* * kdb_mdr - This function implements the guts of the 'mdr', memory * read command. * mdr <addr arg>,<byte count> * Inputs: * addr Start address * count Number of bytes * Returns: * Always 0. Any errors are detected and printed by kdb_getarea.
*/ staticint kdb_mdr(unsignedlong addr, unsignedint count)
{ unsignedchar c; while (count--) { if (kdb_getarea(c, addr)) return 0;
kdb_printf("%02x", c);
addr++;
}
kdb_printf("\n"); return 0;
}
/* * kdb_md - This function implements the 'md', 'md1', 'md2', 'md4', * 'md8' 'mdr' and 'mds' commands. * * md|mds [<addr arg> [<line count> [<radix>]]] * mdWcN [<addr arg> [<line count> [<radix>]]] * where W = is the width (1, 2, 4 or 8) and N is the count. * for eg., md1c20 reads 20 bytes, 1 at a time. * mdr <addr arg>,<byte count>
*/ staticvoid kdb_md_line(constchar *fmtstr, unsignedlong addr, int symbolic, int nosect, int bytesperword, int num, int repeat, int phys)
{ /* print just one line of data */
kdb_symtab_t symtab; char cbuf[32]; char *c = cbuf; int i; int j; unsignedlong word;
if (strcmp(argv[0], "mds") == 0) {
symbolic = 1; /* Do not save these changes as last_*, they are temporary mds * overrides.
*/
bytesperword = KDB_WORD_SIZE;
repeat = mdcount;
kdbgetintenv("NOSECT", &nosect);
}
/* Round address down modulo BYTESPERWORD */
addr &= ~(bytesperword-1);
while (repeat > 0) { unsignedlong a; int n, z, num = (symbolic ? 1 : (16 / bytesperword));
if (KDB_FLAG(CMD_INTERRUPT)) return 0; for (a = addr, z = 0; z < repeat; a += bytesperword, ++z) { if (phys) { if (kdb_getphysword(&word, a, bytesperword)
|| word) break;
} elseif (kdb_getword(&word, a, bytesperword) || word) break;
}
n = min(num, repeat);
kdb_md_line(fmtstr, addr, symbolic, nosect, bytesperword,
num, repeat, phys);
addr += bytesperword * n;
repeat -= n;
z = (z + num - 1) / num; if (z > 2) { int s = num * (z-2);
kdb_printf(kdb_machreg_fmt0 "-" kdb_machreg_fmt0 " zero suppressed\n",
addr, addr + bytesperword * s - 1);
addr += bytesperword * s;
repeat -= s;
}
}
last_addr = addr;
return 0;
}
/* * kdb_mm - This function implements the 'mm' command. * mm address-expression new-value * Remarks: * mm works on machine words, mmW works on bytes.
*/ staticint kdb_mm(int argc, constchar **argv)
{ int diag; unsignedlong addr; long offset = 0; unsignedlong contents; int nextarg; int width;
if (argv[0][2] && !isdigit(argv[0][2])) return KDB_NOTFOUND;
/* * kdb_go - This function implements the 'go' command. * go [address-expression]
*/ staticint kdb_go(int argc, constchar **argv)
{ unsignedlong addr; int diag; int nextarg; long offset;
if (raw_smp_processor_id() != kdb_initial_cpu) {
kdb_printf("go must execute on the entry cpu, " "please use \"cpu %d\" and then execute go\n",
kdb_initial_cpu); return KDB_BADCPUNUM;
} if (argc == 1) {
nextarg = 1;
diag = kdbgetaddrarg(argc, argv, &nextarg,
&addr, &offset, NULL); if (diag) return diag;
} elseif (argc) { return KDB_ARGCOUNT;
}
diag = KDB_CMD_GO; if (KDB_FLAG(CATASTROPHIC)) {
kdb_printf("Catastrophic error detected\n");
kdb_printf("kdb_continue_catastrophic=%d, ",
kdb_continue_catastrophic); if (kdb_continue_catastrophic == 0 && kdb_go_count++ == 0) {
kdb_printf("type go a second time if you really want " "to continue\n"); return 0;
} if (kdb_continue_catastrophic == 2) {
kdb_printf("forcing reboot\n");
kdb_reboot(0, NULL);
}
kdb_printf("attempting to continue\n");
} return diag;
}
/* * kdb_rd - This function implements the 'rd' command.
*/ staticint kdb_rd(int argc, constchar **argv)
{ int len = kdb_check_regs(); #if DBG_MAX_REG_NUM > 0 int i; char *rname; int rsize;
u64 reg64;
u32 reg32;
u16 reg16;
u8 reg8;
if (len) return len;
for (i = 0; i < DBG_MAX_REG_NUM; i++) {
rsize = dbg_reg_def[i].size * 2; if (rsize > 16)
rsize = 2; if (len + strlen(dbg_reg_def[i].name) + 4 + rsize > 80) {
len = 0;
kdb_printf("\n");
} if (len)
len += kdb_printf(" "); switch(dbg_reg_def[i].size * 8) { case 8:
rname = dbg_get_reg(i, ®8, kdb_current_regs); if (!rname) break;
len += kdb_printf("%s: %02x", rname, reg8); break; case 16:
rname = dbg_get_reg(i, ®16, kdb_current_regs); if (!rname) break;
len += kdb_printf("%s: %04x", rname, reg16); break; case 32:
rname = dbg_get_reg(i, ®32, kdb_current_regs); if (!rname) break;
len += kdb_printf("%s: %08x", rname, reg32); break; case 64:
rname = dbg_get_reg(i, ®64, kdb_current_regs); if (!rname) break;
len += kdb_printf("%s: %016llx", rname, reg64); break; default:
len += kdb_printf("%s: ??", dbg_reg_def[i].name);
}
}
kdb_printf("\n"); #else if (len) return len;
/* * kdb_ef - This function implements the 'regs' (display exception * frame) command. This command takes an address and expects to * find an exception frame at that address, formats and prints * it. * regs address-expression * Remarks: * Not done yet.
*/ staticint kdb_ef(int argc, constchar **argv)
{ int diag; unsignedlong addr; long offset; int nextarg;
if (KDB_DEBUG(MASK))
kdb_printf("KDBDEBUG=0x%x\n",
(kdb_flags & KDB_DEBUG(MASK)) >> KDB_DEBUG_FLAG_SHIFT);
return 0;
}
#ifdef CONFIG_PRINTK /* * kdb_dmesg - This function implements the 'dmesg' command to display * the contents of the syslog buffer. * dmesg [lines] [adjust]
*/ staticint kdb_dmesg(int argc, constchar **argv)
{ int diag; int logging; int lines = 0; int adjust = 0; int n = 0; int skip = 0; struct kmsg_dump_iter iter;
size_t len; char buf[201];
if (argc > 2) return KDB_ARGCOUNT; if (argc) { if (kstrtoint(argv[1], 0, &lines))
lines = 0; if (argc > 1 && (kstrtoint(argv[2], 0, &adjust) || adjust < 0))
adjust = 0;
}
/* disable LOGGING if set */
diag = kdbgetintenv("LOGGING", &logging); if (!diag && logging) { constchar *setargs[] = { "set", "LOGGING", "0" };
kdb_set(2, setargs);
}
kmsg_dump_rewind(&iter); while (kmsg_dump_get_line(&iter, 1, NULL, 0, NULL))
n++;
if (lines < 0) { if (adjust >= n)
kdb_printf("buffer only contains %d lines, nothing " "printed\n", n); elseif (adjust - lines >= n)
kdb_printf("buffer only contains %d lines, last %d " "lines printed\n", n, n - adjust);
skip = adjust;
lines = abs(lines);
} elseif (lines > 0) {
skip = n - lines - adjust;
lines = abs(lines); if (adjust >= n) {
kdb_printf("buffer only contains %d lines, " "nothing printed\n", n);
skip = n;
} elseif (skip < 0) {
lines += skip;
skip = 0;
kdb_printf("buffer only contains %d lines, first " "%d lines printed\n", n, lines);
}
} else {
lines = n;
}
if (skip >= n || skip < 0) return 0;
kmsg_dump_rewind(&iter); while (kmsg_dump_get_line(&iter, 1, buf, sizeof(buf), &len)) { if (skip) {
skip--; continue;
} if (!lines--) break; if (KDB_FLAG(CMD_INTERRUPT)) return 0;
kdb_printf("%.*s\n", (int)len - 1, buf);
}
return 0;
} #endif/* CONFIG_PRINTK */ /* * kdb_cpu - This function implements the 'cpu' command. * cpu [<cpunum>] * Returns: * KDB_CMD_CPU for success, a kdb diagnostic if error
*/ staticvoid kdb_cpu_status(void)
{ int i, start_cpu, first_print = 1; char state, prev_state = '?';
kdb_printf("Currently on cpu %d\n", raw_smp_processor_id());
kdb_printf("Available cpus: "); for (start_cpu = -1, i = 0; i < NR_CPUS; i++) { if (!cpu_online(i)) {
state = 'F'; /* cpu is offline */
} elseif (!kgdb_info[i].enter_kgdb) {
state = 'D'; /* cpu is online but unresponsive */
} else {
state = ' '; /* cpu is responding to kdb */ if (kdb_task_state_char(KDB_TSK(i)) == '-')
state = '-'; /* idle task */
} if (state != prev_state) { if (prev_state != '?') { if (!first_print)
kdb_printf(", ");
first_print = 0;
kdb_printf("%d", start_cpu); if (start_cpu < i-1)
kdb_printf("-%d", i-1); if (prev_state != ' ')
kdb_printf("(%c)", prev_state);
}
prev_state = state;
start_cpu = i;
}
} /* print the trailing cpus, ignoring them if they are all offline */ if (prev_state != 'F') { if (!first_print)
kdb_printf(", ");
kdb_printf("%d", start_cpu); if (start_cpu < i-1)
kdb_printf("-%d", i-1); if (prev_state != ' ')
kdb_printf("(%c)", prev_state);
}
kdb_printf("\n");
}
staticint kdb_cpu(int argc, constchar **argv)
{ unsignedlong cpunum; int diag;
if (argc == 0) {
kdb_cpu_status(); return 0;
}
if (argc != 1) return KDB_ARGCOUNT;
diag = kdbgetularg(argv[1], &cpunum); if (diag) return diag;
/* The user may not realize that ps/bta with no parameters does not print idle * or sleeping system daemon processes, so tell them how many were suppressed.
*/ void kdb_ps_suppressed(void)
{ int idle = 0, daemon = 0; unsignedlong cpu; conststruct task_struct *p, *g;
for_each_online_cpu(cpu) {
p = curr_task(cpu); if (kdb_task_state(p, "-"))
++idle;
}
for_each_process_thread(g, p) { if (kdb_task_state(p, "ims"))
++daemon;
} if (idle || daemon) { if (idle)
kdb_printf("%d idle process%s (state -)%s\n",
idle, idle == 1 ? "" : "es",
daemon ? " and " : ""); if (daemon)
kdb_printf("%d sleeping system daemon (state [ims]) " "process%s", daemon,
daemon == 1 ? "" : "es");
kdb_printf(" suppressed,\nuse 'ps A' to see all.\n");
}
}
void kdb_ps1(conststruct task_struct *p)
{ int cpu; unsignedlong tmp;
if (!p ||
copy_from_kernel_nofault(&tmp, (char *)p, sizeof(unsignedlong))) return;
cpu = kdb_process_cpu(p);
kdb_printf("0x%px %8d %8d %d %4d %c 0x%px %c%s\n",
(void *)p, p->pid, p->parent->pid,
kdb_task_has_cpu(p), kdb_process_cpu(p),
kdb_task_state_char(p),
(void *)(&p->thread),
p == curr_task(raw_smp_processor_id()) ? '*' : ' ',
p->comm); if (kdb_task_has_cpu(p)) { if (!KDB_TSK(cpu)) {
kdb_printf(" Error: no saved data for this cpu\n");
} else { if (KDB_TSK(cpu) != p)
kdb_printf(" Error: does not match running " "process table (0x%px)\n", KDB_TSK(cpu));
}
}
}
/* * kdb_ps - This function implements the 'ps' command which shows a * list of the active processes. * * ps [<state_chars>] Show processes, optionally selecting only those whose * state character is found in <state_chars>.
*/ staticint kdb_ps(int argc, constchar **argv)
{ struct task_struct *g, *p; constchar *mask; unsignedlong cpu;
if (argc == 0)
kdb_ps_suppressed();
kdb_printf("%-*s Pid Parent [*] cpu State %-*s Command\n",
(int)(2*sizeof(void *))+2, "Task Addr",
(int)(2*sizeof(void *))+2, "Thread");
mask = argc ? argv[1] : kdbgetenv("PS"); /* Run the active tasks first */
for_each_online_cpu(cpu) { if (KDB_FLAG(CMD_INTERRUPT)) return 0;
p = curr_task(cpu); if (kdb_task_state(p, mask))
kdb_ps1(p);
}
kdb_printf("\n"); /* Now the real tasks */
for_each_process_thread(g, p) { if (KDB_FLAG(CMD_INTERRUPT)) return 0; if (kdb_task_state(p, mask))
kdb_ps1(p);
}
return 0;
}
/* * kdb_pid - This function implements the 'pid' command which switches * the currently active process. * pid [<pid> | R]
*/ staticint kdb_pid(int argc, constchar **argv)
{ struct task_struct *p; unsignedlong val; int diag;
if (argc > 1) return KDB_ARGCOUNT;
if (argc) { if (strcmp(argv[1], "R") == 0) {
p = KDB_TSK(kdb_initial_cpu);
} else {
diag = kdbgetularg(argv[1], &val); if (diag) return KDB_BADINT;
p = find_task_by_pid_ns((pid_t)val, &init_pid_ns); if (!p) {
kdb_printf("No task with pid=%d\n", (pid_t)val); return 0;
}
}
kdb_set_current_task(p);
}
kdb_printf("KDB current process is %s(pid=%d)\n",
kdb_current_task->comm,
kdb_current_task->pid);
/* * kdb_kill - This function implements the 'kill' commands.
*/ staticint kdb_kill(int argc, constchar **argv)
{ long sig, pid; struct task_struct *p;
if (argc != 2) return KDB_ARGCOUNT;
if (kstrtol(argv[1], 0, &sig)) return KDB_BADINT; if ((sig >= 0) || !valid_signal(-sig)) {
kdb_printf("Invalid signal parameter.<-signal>\n"); return 0;
}
sig = -sig;
if (kstrtol(argv[2], 0, &pid)) return KDB_BADINT; if (pid <= 0) {
kdb_printf("Process ID must be large than 0.\n"); return 0;
}
/* Find the process. */
p = find_task_by_pid_ns(pid, &init_pid_ns); if (!p) {
kdb_printf("The specified process isn't found.\n"); return 0;
}
p = p->group_leader;
kdb_send_sig(p, sig); return 0;
}
/* * Most of this code has been lifted from kernel/timer.c::sys_sysinfo(). * I cannot call that code directly from kdb, it has an unconditional * cli()/sti() and calls routines that take locks which can stop the debugger.
*/ staticvoid kdb_sysinfo(struct sysinfo *val)
{
u64 uptime = ktime_get_mono_fast_ns();
/* * display help for the use of cmd | grep pattern
*/ staticint kdb_grep_help(int argc, constchar **argv)
{
kdb_printf("Usage of cmd args | grep pattern:\n");
kdb_printf(" Any command's output may be filtered through an ");
kdb_printf("emulated 'pipe'.\n");
kdb_printf(" 'grep' is just a key word.\n");
kdb_printf(" The pattern may include a very limited set of " "metacharacters:\n");
kdb_printf(" pattern or ^pattern or pattern$ or ^pattern$\n");
kdb_printf(" And if there are spaces in the pattern, you may " "quote it:\n");
kdb_printf(" \"pat tern\" or \"^pat tern\" or \"pat tern$\"" " or \"^pat tern$\"\n"); return 0;
}
/** * kdb_register() - This function is used to register a kernel debugger * command. * @cmd: pointer to kdb command * * Note that it's the job of the caller to keep the memory for the cmd * allocated until unregister is called.
*/ int kdb_register(kdbtab_t *cmd)
{
kdbtab_t *kp;
/** * kdb_register_table() - This function is used to register a kdb command * table. * @kp: pointer to kdb command table * @len: length of kdb command table
*/ void kdb_register_table(kdbtab_t *kp, size_t len)
{ while (len--) {
list_add_tail(&kp->list_node, &kdb_cmds_head);
kp++;
}
}
/** * kdb_unregister() - This function is used to unregister a kernel debugger * command. It is generally called when a module which * implements kdb command is unloaded. * @cmd: pointer to kdb command
*/ void kdb_unregister(kdbtab_t *cmd)
{
list_del(&cmd->list_node);
}
EXPORT_SYMBOL_GPL(kdb_unregister);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.