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;
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;
44 * This function implements the 'bt' command. Print a stack
47 * bt [<address-expression>] (addr-exp is for alternate stacks)
48 * btp <pid> Kernel stack for <pid>
49 * btt <address-expression> Kernel stack for task structure at
50 * <address-expression>
51 * bta [DRSTCZEUIMA] All useful processes, optionally
53 * btc [<cpu>] The current process on one cpu,
56 * bt <address-expression> refers to a address on the stack, that location
57 * is assumed to contain a return address.
59 * btt <address-expression> refers to the address of a struct task.
63 * argv argument vector
67 * zero for success, a kdb diagnostic if error
71 * Backtrack works best when the code uses frame pointers. But even
72 * without frame pointers we should get a reasonable trace.
74 * mds comes in handy when examining the stack to do a manual traceback or
75 * to get a starting point for bt <address-expression>.
79 kdb_bt1(struct task_struct *p, unsigned long mask,
80 int argcount, int btaprompt)
83 if (kdb_getarea(buffer[0], (unsigned long)p) ||
84 kdb_getarea(buffer[0], (unsigned long)(p+1)-1))
86 if (!kdb_task_state(p, mask))
88 kdb_printf("Stack traceback for pid %d\n", p->pid);
90 kdb_show_stack(p, NULL);
92 kdb_getstr(buffer, sizeof(buffer),
93 "Enter <q> to end, <cr> to continue:");
94 if (buffer[0] == 'q') {
104 kdb_bt(int argc, const char **argv)
113 kdbgetintenv("BTARGS", &argcount); /* Arguments to print */
114 kdbgetintenv("BTAPROMPT", &btaprompt); /* Prompt after each
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);