]> bbs.cooldavid.org Git - net-next-2.6.git/blob - kernel/debug/kdb/kdb_bt.c
kdb: core for kgdb back end (1 of 2)
[net-next-2.6.git] / kernel / debug / kdb / kdb_bt.c
1 /*
2  * Kernel Debugger Architecture Independent Stack Traceback
3  *
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
6  * for more details.
7  *
8  * Copyright (c) 1999-2004 Silicon Graphics, Inc.  All Rights Reserved.
9  * Copyright (c) 2009 Wind River Systems, Inc.  All Rights Reserved.
10  */
11
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"
20
21
22 static void kdb_show_stack(struct task_struct *p, void *addr)
23 {
24         int old_lvl = console_loglevel;
25         console_loglevel = 15;
26         kdb_set_current_task(p);
27         if (addr) {
28                 show_stack((struct task_struct *)p, addr);
29         } else if (kdb_current_regs) {
30 #ifdef CONFIG_X86
31                 show_stack(p, &kdb_current_regs->sp);
32 #else
33                 show_stack(p, NULL);
34 #endif
35         } else {
36                 show_stack(p, NULL);
37         }
38         console_loglevel = old_lvl;
39 }
40
41 /*
42  * kdb_bt
43  *
44  *      This function implements the 'bt' command.  Print a stack
45  *      traceback.
46  *
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
52  *                                      filtered by state
53  *      btc [<cpu>]                     The current process on one cpu,
54  *                                      default is all cpus
55  *
56  *      bt <address-expression> refers to a address on the stack, that location
57  *      is assumed to contain a return address.
58  *
59  *      btt <address-expression> refers to the address of a struct task.
60  *
61  * Inputs:
62  *      argc    argument count
63  *      argv    argument vector
64  * Outputs:
65  *      None.
66  * Returns:
67  *      zero for success, a kdb diagnostic if error
68  * Locking:
69  *      none.
70  * Remarks:
71  *      Backtrack works best when the code uses frame pointers.  But even
72  *      without frame pointers we should get a reasonable trace.
73  *
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>.
76  */
77
78 static int
79 kdb_bt1(struct task_struct *p, unsigned long mask,
80         int argcount, int btaprompt)
81 {
82         char buffer[2];
83         if (kdb_getarea(buffer[0], (unsigned long)p) ||
84             kdb_getarea(buffer[0], (unsigned long)(p+1)-1))
85                 return KDB_BADADDR;
86         if (!kdb_task_state(p, mask))
87                 return 0;
88         kdb_printf("Stack traceback for pid %d\n", p->pid);
89         kdb_ps1(p);
90         kdb_show_stack(p, NULL);
91         if (btaprompt) {
92                 kdb_getstr(buffer, sizeof(buffer),
93                            "Enter <q> to end, <cr> to continue:");
94                 if (buffer[0] == 'q') {
95                         kdb_printf("\n");
96                         return 1;
97                 }
98         }
99         touch_nmi_watchdog();
100         return 0;
101 }
102
103 int
104 kdb_bt(int argc, const char **argv)
105 {
106         int diag;
107         int argcount = 5;
108         int btaprompt = 1;
109         int nextarg;
110         unsigned long addr;
111         long offset;
112
113         kdbgetintenv("BTARGS", &argcount);      /* Arguments to print */
114         kdbgetintenv("BTAPROMPT", &btaprompt);  /* Prompt after each
115                                                  * proc in bta */
116
117         if (strcmp(argv[0], "bta") == 0) {
118                 struct task_struct *g, *p;
119                 unsigned long cpu;
120                 unsigned long mask = kdb_task_state_string(argc ? argv[1] :
121                                                            NULL);
122                 if (argc == 0)
123                         kdb_ps_suppressed();
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))
128                                 return 0;
129                 }
130                 /* Now the inactive tasks */
131                 kdb_do_each_thread(g, p) {
132                         if (task_curr(p))
133                                 continue;
134                         if (kdb_bt1(p, mask, argcount, btaprompt))
135                                 return 0;
136                 } kdb_while_each_thread(g, p);
137         } else if (strcmp(argv[0], "btp") == 0) {
138                 struct task_struct *p;
139                 unsigned long pid;
140                 if (argc != 1)
141                         return KDB_ARGCOUNT;
142                 diag = kdbgetularg((char *)argv[1], &pid);
143                 if (diag)
144                         return diag;
145                 p = find_task_by_pid_ns(pid, &init_pid_ns);
146                 if (p) {
147                         kdb_set_current_task(p);
148                         return kdb_bt1(p, ~0UL, argcount, 0);
149                 }
150                 kdb_printf("No process with pid == %ld found\n", pid);
151                 return 0;
152         } else if (strcmp(argv[0], "btt") == 0) {
153                 if (argc != 1)
154                         return KDB_ARGCOUNT;
155                 diag = kdbgetularg((char *)argv[1], &addr);
156                 if (diag)
157                         return diag;
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;
163                 char buf[80];
164                 if (argc > 1)
165                         return KDB_ARGCOUNT;
166                 if (argc == 1) {
167                         diag = kdbgetularg((char *)argv[1], &cpu);
168                         if (diag)
169                                 return diag;
170                 }
171                 /* Recursive use of kdb_parse, do not use argv after
172                  * this point */
173                 argv = NULL;
174                 if (cpu != ~0) {
175                         if (cpu >= num_possible_cpus() || !cpu_online(cpu)) {
176                                 kdb_printf("no process for cpu %ld\n", cpu);
177                                 return 0;
178                         }
179                         sprintf(buf, "btt 0x%p\n", KDB_TSK(cpu));
180                         kdb_parse(buf);
181                         return 0;
182                 }
183                 kdb_printf("btc: cpu status: ");
184                 kdb_parse("cpu\n");
185                 for_each_online_cpu(cpu) {
186                         sprintf(buf, "btt 0x%p\n", KDB_TSK(cpu));
187                         kdb_parse(buf);
188                         touch_nmi_watchdog();
189                 }
190                 kdb_set_current_task(save_current_task);
191                 return 0;
192         } else {
193                 if (argc) {
194                         nextarg = 1;
195                         diag = kdbgetaddrarg(argc, argv, &nextarg, &addr,
196                                              &offset, NULL);
197                         if (diag)
198                                 return diag;
199                         kdb_show_stack(kdb_current_task, (void *)addr);
200                         return 0;
201                 } else {
202                         return kdb_bt1(kdb_current_task, ~0UL, argcount, 0);
203                 }
204         }
205
206         /* NOTREACHED */
207         return 0;
208 }