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 <asm/system.h>
19 #include "kdb_private.h"
22 static void kdb_show_stack(struct task_struct
*p
, void *addr
)
24 int old_lvl
= console_loglevel
;
25 console_loglevel
= 15;
27 kdb_set_current_task(p
);
29 show_stack((struct task_struct
*)p
, addr
);
30 } else if (kdb_current_regs
) {
32 show_stack(p
, &kdb_current_regs
->sp
);
39 console_loglevel
= old_lvl
;
46 * This function implements the 'bt' command. Print a stack
49 * bt [<address-expression>] (addr-exp is for alternate stacks)
50 * btp <pid> Kernel stack for <pid>
51 * btt <address-expression> Kernel stack for task structure at
52 * <address-expression>
53 * bta [DRSTCZEUIMA] All useful processes, optionally
55 * btc [<cpu>] The current process on one cpu,
58 * bt <address-expression> refers to a address on the stack, that location
59 * is assumed to contain a return address.
61 * btt <address-expression> refers to the address of a struct task.
65 * argv argument vector
69 * zero for success, a kdb diagnostic if error
73 * Backtrack works best when the code uses frame pointers. But even
74 * without frame pointers we should get a reasonable trace.
76 * mds comes in handy when examining the stack to do a manual traceback or
77 * to get a starting point for bt <address-expression>.
81 kdb_bt1(struct task_struct
*p
, unsigned long mask
,
82 int argcount
, int btaprompt
)
85 if (kdb_getarea(buffer
[0], (unsigned long)p
) ||
86 kdb_getarea(buffer
[0], (unsigned long)(p
+1)-1))
88 if (!kdb_task_state(p
, mask
))
90 kdb_printf("Stack traceback for pid %d\n", p
->pid
);
92 kdb_show_stack(p
, NULL
);
94 kdb_getstr(buffer
, sizeof(buffer
),
95 "Enter <q> to end, <cr> to continue:");
96 if (buffer
[0] == 'q') {
101 touch_nmi_watchdog();
106 kdb_bt(int argc
, const char **argv
)
115 /* Prompt after each proc in bta */
116 kdbgetintenv("BTAPROMPT", &btaprompt
);
118 if (strcmp(argv
[0], "bta") == 0) {
119 struct task_struct
*g
, *p
;
121 unsigned long mask
= kdb_task_state_string(argc
? argv
[1] :
125 /* Run the active tasks first */
126 for_each_online_cpu(cpu
) {
127 p
= kdb_curr_task(cpu
);
128 if (kdb_bt1(p
, mask
, argcount
, btaprompt
))
131 /* Now the inactive tasks */
132 kdb_do_each_thread(g
, p
) {
135 if (kdb_bt1(p
, mask
, argcount
, btaprompt
))
137 } kdb_while_each_thread(g
, p
);
138 } else if (strcmp(argv
[0], "btp") == 0) {
139 struct task_struct
*p
;
143 diag
= kdbgetularg((char *)argv
[1], &pid
);
146 p
= find_task_by_pid_ns(pid
, &init_pid_ns
);
148 kdb_set_current_task(p
);
149 return kdb_bt1(p
, ~0UL, argcount
, 0);
151 kdb_printf("No process with pid == %ld found\n", pid
);
153 } else if (strcmp(argv
[0], "btt") == 0) {
156 diag
= kdbgetularg((char *)argv
[1], &addr
);
159 kdb_set_current_task((struct task_struct
*)addr
);
160 return kdb_bt1((struct task_struct
*)addr
, ~0UL, argcount
, 0);
161 } else if (strcmp(argv
[0], "btc") == 0) {
162 unsigned long cpu
= ~0;
163 struct task_struct
*save_current_task
= kdb_current_task
;
168 diag
= kdbgetularg((char *)argv
[1], &cpu
);
172 /* Recursive use of kdb_parse, do not use argv after
176 if (cpu
>= num_possible_cpus() || !cpu_online(cpu
)) {
177 kdb_printf("no process for cpu %ld\n", cpu
);
180 sprintf(buf
, "btt 0x%p\n", KDB_TSK(cpu
));
184 kdb_printf("btc: cpu status: ");
186 for_each_online_cpu(cpu
) {
187 sprintf(buf
, "btt 0x%p\n", KDB_TSK(cpu
));
189 touch_nmi_watchdog();
191 kdb_set_current_task(save_current_task
);
196 diag
= kdbgetaddrarg(argc
, argv
, &nextarg
, &addr
,
200 kdb_show_stack(kdb_current_task
, (void *)addr
);
203 return kdb_bt1(kdb_current_task
, ~0UL, argcount
, 0);