2 * Kernel Debugger Architecture Independent Stack Traceback
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
9 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
12 #include <linux/ctype.h>
13 #include <linux/string.h>
14 #include <linux/kernel.h>
15 #include <linux/sched/signal.h>
16 #include <linux/sched/debug.h>
17 #include <linux/kdb.h>
18 #include <linux/nmi.h>
19 #include "kdb_private.h"
22 static void kdb_show_stack(struct task_struct *p, void *addr)
26 if (!addr && kdb_task_has_cpu(p)) {
27 int old_lvl = console_loglevel;
29 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
30 kdb_dump_stack_on_cpu(kdb_process_cpu(p));
31 console_loglevel = old_lvl;
33 show_stack(p, addr, KERN_EMERG);
42 * This function implements the 'bt' command. Print a stack
45 * bt [<address-expression>] (addr-exp is for alternate stacks)
46 * btp <pid> Kernel stack for <pid>
47 * btt <address-expression> Kernel stack for task structure at
48 * <address-expression>
49 * bta [DRSTCZEUIMA] All useful processes, optionally
51 * btc [<cpu>] The current process on one cpu,
54 * bt <address-expression> refers to a address on the stack, that location
55 * is assumed to contain a return address.
57 * btt <address-expression> refers to the address of a struct task.
61 * argv argument vector
65 * zero for success, a kdb diagnostic if error
69 * Backtrack works best when the code uses frame pointers. But even
70 * without frame pointers we should get a reasonable trace.
72 * mds comes in handy when examining the stack to do a manual traceback or
73 * to get a starting point for bt <address-expression>.
77 kdb_bt1(struct task_struct *p, unsigned long mask, bool btaprompt)
81 if (kdb_getarea(ch, (unsigned long)p) ||
82 kdb_getarea(ch, (unsigned long)(p+1)-1))
84 if (!kdb_task_state(p, mask))
86 kdb_printf("Stack traceback for pid %d\n", p->pid);
88 kdb_show_stack(p, NULL);
90 kdb_printf("Enter <q> to end, <cr> or <space> to continue:");
93 } while (!strchr("\r\n q", ch));
102 touch_nmi_watchdog();
107 kdb_bt_cpu(unsigned long cpu)
109 struct task_struct *kdb_tsk;
111 if (cpu >= num_possible_cpus() || !cpu_online(cpu)) {
112 kdb_printf("WARNING: no process for cpu %ld\n", cpu);
116 /* If a CPU failed to round up we could be here */
117 kdb_tsk = KDB_TSK(cpu);
119 kdb_printf("WARNING: no task for cpu %ld\n", cpu);
123 kdb_bt1(kdb_tsk, ~0UL, false);
127 kdb_bt(int argc, const char **argv)
135 /* Prompt after each proc in bta */
136 kdbgetintenv("BTAPROMPT", &btaprompt);
138 if (strcmp(argv[0], "bta") == 0) {
139 struct task_struct *g, *p;
141 unsigned long mask = kdb_task_state_string(argc ? argv[1] :
145 /* Run the active tasks first */
146 for_each_online_cpu(cpu) {
147 p = kdb_curr_task(cpu);
148 if (kdb_bt1(p, mask, btaprompt))
151 /* Now the inactive tasks */
152 for_each_process_thread(g, p) {
153 if (KDB_FLAG(CMD_INTERRUPT))
157 if (kdb_bt1(p, mask, btaprompt))
160 } else if (strcmp(argv[0], "btp") == 0) {
161 struct task_struct *p;
165 diag = kdbgetularg((char *)argv[1], &pid);
168 p = find_task_by_pid_ns(pid, &init_pid_ns);
170 return kdb_bt1(p, ~0UL, false);
171 kdb_printf("No process with pid == %ld found\n", pid);
173 } else if (strcmp(argv[0], "btt") == 0) {
176 diag = kdbgetularg((char *)argv[1], &addr);
179 return kdb_bt1((struct task_struct *)addr, ~0UL, false);
180 } else if (strcmp(argv[0], "btc") == 0) {
181 unsigned long cpu = ~0;
185 diag = kdbgetularg((char *)argv[1], &cpu);
193 * Recursive use of kdb_parse, do not use argv after
197 kdb_printf("btc: cpu status: ");
199 for_each_online_cpu(cpu) {
201 touch_nmi_watchdog();
208 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr,
212 kdb_show_stack(kdb_current_task, (void *)addr);
215 return kdb_bt1(kdb_current_task, ~0UL, false);