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.h>
16 #include <linux/kdb.h>
17 #include <linux/nmi.h>
18 #include "kdb_private.h"
21 static void kdb_show_stack(struct task_struct
*p
, void *addr
)
23 int old_lvl
= console_loglevel
;
24 console_loglevel
= 15;
26 kdb_set_current_task(p
);
28 show_stack((struct task_struct
*)p
, addr
);
29 } else if (kdb_current_regs
) {
31 show_stack(p
, &kdb_current_regs
->sp
);
38 console_loglevel
= old_lvl
;
45 * This function implements the 'bt' command. Print a stack
48 * bt [<address-expression>] (addr-exp is for alternate stacks)
49 * btp <pid> Kernel stack for <pid>
50 * btt <address-expression> Kernel stack for task structure at
51 * <address-expression>
52 * bta [DRSTCZEUIMA] All useful processes, optionally
54 * btc [<cpu>] The current process on one cpu,
57 * bt <address-expression> refers to a address on the stack, that location
58 * is assumed to contain a return address.
60 * btt <address-expression> refers to the address of a struct task.
64 * argv argument vector
68 * zero for success, a kdb diagnostic if error
72 * Backtrack works best when the code uses frame pointers. But even
73 * without frame pointers we should get a reasonable trace.
75 * mds comes in handy when examining the stack to do a manual traceback or
76 * to get a starting point for bt <address-expression>.
80 kdb_bt1(struct task_struct
*p
, unsigned long mask
,
81 int argcount
, int btaprompt
)
84 if (kdb_getarea(buffer
[0], (unsigned long)p
) ||
85 kdb_getarea(buffer
[0], (unsigned long)(p
+1)-1))
87 if (!kdb_task_state(p
, mask
))
89 kdb_printf("Stack traceback for pid %d\n", p
->pid
);
91 kdb_show_stack(p
, NULL
);
93 kdb_getstr(buffer
, sizeof(buffer
),
94 "Enter <q> to end, <cr> to continue:");
95 if (buffer
[0] == 'q') {
100 touch_nmi_watchdog();
105 kdb_bt(int argc
, const char **argv
)
114 /* Prompt after each proc in bta */
115 kdbgetintenv("BTAPROMPT", &btaprompt
);
117 if (strcmp(argv
[0], "bta") == 0) {
118 struct task_struct
*g
, *p
;
120 unsigned long mask
= kdb_task_state_string(argc
? argv
[1] :
124 /* Run the active tasks first */
125 for_each_online_cpu(cpu
) {
126 p
= kdb_curr_task(cpu
);
127 if (kdb_bt1(p
, mask
, argcount
, btaprompt
))
130 /* Now the inactive tasks */
131 kdb_do_each_thread(g
, p
) {
134 if (kdb_bt1(p
, mask
, argcount
, btaprompt
))
136 } kdb_while_each_thread(g
, p
);
137 } else if (strcmp(argv
[0], "btp") == 0) {
138 struct task_struct
*p
;
142 diag
= kdbgetularg((char *)argv
[1], &pid
);
145 p
= find_task_by_pid_ns(pid
, &init_pid_ns
);
147 kdb_set_current_task(p
);
148 return kdb_bt1(p
, ~0UL, argcount
, 0);
150 kdb_printf("No process with pid == %ld found\n", pid
);
152 } else if (strcmp(argv
[0], "btt") == 0) {
155 diag
= kdbgetularg((char *)argv
[1], &addr
);
158 kdb_set_current_task((struct task_struct
*)addr
);
159 return kdb_bt1((struct task_struct
*)addr
, ~0UL, argcount
, 0);
160 } else if (strcmp(argv
[0], "btc") == 0) {
161 unsigned long cpu
= ~0;
162 struct task_struct
*save_current_task
= kdb_current_task
;
167 diag
= kdbgetularg((char *)argv
[1], &cpu
);
171 /* Recursive use of kdb_parse, do not use argv after
175 if (cpu
>= num_possible_cpus() || !cpu_online(cpu
)) {
176 kdb_printf("no process for cpu %ld\n", cpu
);
179 sprintf(buf
, "btt 0x%p\n", KDB_TSK(cpu
));
183 kdb_printf("btc: cpu status: ");
185 for_each_online_cpu(cpu
) {
186 sprintf(buf
, "btt 0x%p\n", KDB_TSK(cpu
));
188 touch_nmi_watchdog();
190 kdb_set_current_task(save_current_task
);
195 diag
= kdbgetaddrarg(argc
, argv
, &nextarg
, &addr
,
199 kdb_show_stack(kdb_current_task
, (void *)addr
);
202 return kdb_bt1(kdb_current_task
, ~0UL, argcount
, 0);