blob: 120fc686c91912c53a04f6b436dde38d60286cdc [file] [log] [blame]
Jason Wessel5d5314d2010-05-20 21:04:20 -05001/*
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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010015#include <linux/sched/signal.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010016#include <linux/sched/debug.h>
Jason Wessel5d5314d2010-05-20 21:04:20 -050017#include <linux/kdb.h>
18#include <linux/nmi.h>
Jason Wessel5d5314d2010-05-20 21:04:20 -050019#include "kdb_private.h"
20
21
22static void kdb_show_stack(struct task_struct *p, void *addr)
23{
24 int old_lvl = console_loglevel;
Borislav Petkova8fe19e2014-06-04 16:11:46 -070025 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
Jason Wesseld37d39a2010-05-20 21:04:27 -050026 kdb_trap_printk++;
Jason Wessel5d5314d2010-05-20 21:04:20 -050027 kdb_set_current_task(p);
28 if (addr) {
29 show_stack((struct task_struct *)p, addr);
30 } else if (kdb_current_regs) {
31#ifdef CONFIG_X86
32 show_stack(p, &kdb_current_regs->sp);
33#else
34 show_stack(p, NULL);
35#endif
36 } else {
37 show_stack(p, NULL);
38 }
39 console_loglevel = old_lvl;
Jason Wesseld37d39a2010-05-20 21:04:27 -050040 kdb_trap_printk--;
Jason Wessel5d5314d2010-05-20 21:04:20 -050041}
42
43/*
44 * kdb_bt
45 *
46 * This function implements the 'bt' command. Print a stack
47 * traceback.
48 *
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
54 * filtered by state
55 * btc [<cpu>] The current process on one cpu,
56 * default is all cpus
57 *
58 * bt <address-expression> refers to a address on the stack, that location
59 * is assumed to contain a return address.
60 *
61 * btt <address-expression> refers to the address of a struct task.
62 *
63 * Inputs:
64 * argc argument count
65 * argv argument vector
66 * Outputs:
67 * None.
68 * Returns:
69 * zero for success, a kdb diagnostic if error
70 * Locking:
71 * none.
72 * Remarks:
73 * Backtrack works best when the code uses frame pointers. But even
74 * without frame pointers we should get a reasonable trace.
75 *
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>.
78 */
79
80static int
Douglas Anderson54af3e32019-09-25 13:02:18 -070081kdb_bt1(struct task_struct *p, unsigned long mask, bool btaprompt)
Jason Wessel5d5314d2010-05-20 21:04:20 -050082{
83 char buffer[2];
84 if (kdb_getarea(buffer[0], (unsigned long)p) ||
85 kdb_getarea(buffer[0], (unsigned long)(p+1)-1))
86 return KDB_BADADDR;
87 if (!kdb_task_state(p, mask))
88 return 0;
89 kdb_printf("Stack traceback for pid %d\n", p->pid);
90 kdb_ps1(p);
91 kdb_show_stack(p, NULL);
92 if (btaprompt) {
93 kdb_getstr(buffer, sizeof(buffer),
94 "Enter <q> to end, <cr> to continue:");
95 if (buffer[0] == 'q') {
96 kdb_printf("\n");
97 return 1;
98 }
99 }
100 touch_nmi_watchdog();
101 return 0;
102}
103
104int
105kdb_bt(int argc, const char **argv)
106{
107 int diag;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500108 int btaprompt = 1;
109 int nextarg;
110 unsigned long addr;
111 long offset;
112
Jason Wessel3bdb65e2011-06-30 14:12:00 -0500113 /* Prompt after each proc in bta */
114 kdbgetintenv("BTAPROMPT", &btaprompt);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500115
116 if (strcmp(argv[0], "bta") == 0) {
117 struct task_struct *g, *p;
118 unsigned long cpu;
119 unsigned long mask = kdb_task_state_string(argc ? argv[1] :
120 NULL);
121 if (argc == 0)
122 kdb_ps_suppressed();
123 /* Run the active tasks first */
124 for_each_online_cpu(cpu) {
125 p = kdb_curr_task(cpu);
Douglas Anderson54af3e32019-09-25 13:02:18 -0700126 if (kdb_bt1(p, mask, btaprompt))
Jason Wessel5d5314d2010-05-20 21:04:20 -0500127 return 0;
128 }
129 /* Now the inactive tasks */
130 kdb_do_each_thread(g, p) {
Jason Wesseld1871b32012-08-26 21:43:12 -0500131 if (KDB_FLAG(CMD_INTERRUPT))
132 return 0;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500133 if (task_curr(p))
134 continue;
Douglas Anderson54af3e32019-09-25 13:02:18 -0700135 if (kdb_bt1(p, mask, btaprompt))
Jason Wessel5d5314d2010-05-20 21:04:20 -0500136 return 0;
137 } kdb_while_each_thread(g, p);
138 } else if (strcmp(argv[0], "btp") == 0) {
139 struct task_struct *p;
140 unsigned long pid;
141 if (argc != 1)
142 return KDB_ARGCOUNT;
143 diag = kdbgetularg((char *)argv[1], &pid);
144 if (diag)
145 return diag;
146 p = find_task_by_pid_ns(pid, &init_pid_ns);
147 if (p) {
148 kdb_set_current_task(p);
Douglas Anderson54af3e32019-09-25 13:02:18 -0700149 return kdb_bt1(p, ~0UL, false);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500150 }
151 kdb_printf("No process with pid == %ld found\n", pid);
152 return 0;
153 } else if (strcmp(argv[0], "btt") == 0) {
154 if (argc != 1)
155 return KDB_ARGCOUNT;
156 diag = kdbgetularg((char *)argv[1], &addr);
157 if (diag)
158 return diag;
159 kdb_set_current_task((struct task_struct *)addr);
Douglas Anderson54af3e32019-09-25 13:02:18 -0700160 return kdb_bt1((struct task_struct *)addr, ~0UL, false);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500161 } else if (strcmp(argv[0], "btc") == 0) {
162 unsigned long cpu = ~0;
163 struct task_struct *save_current_task = kdb_current_task;
164 char buf[80];
165 if (argc > 1)
166 return KDB_ARGCOUNT;
167 if (argc == 1) {
168 diag = kdbgetularg((char *)argv[1], &cpu);
169 if (diag)
170 return diag;
171 }
172 /* Recursive use of kdb_parse, do not use argv after
173 * this point */
174 argv = NULL;
175 if (cpu != ~0) {
176 if (cpu >= num_possible_cpus() || !cpu_online(cpu)) {
177 kdb_printf("no process for cpu %ld\n", cpu);
178 return 0;
179 }
Christophe Leroydded2e12018-09-27 17:17:49 +0000180 sprintf(buf, "btt 0x%px\n", KDB_TSK(cpu));
Jason Wessel5d5314d2010-05-20 21:04:20 -0500181 kdb_parse(buf);
182 return 0;
183 }
184 kdb_printf("btc: cpu status: ");
185 kdb_parse("cpu\n");
186 for_each_online_cpu(cpu) {
Douglas Anderson162bc7f2018-12-04 19:38:28 -0800187 void *kdb_tsk = KDB_TSK(cpu);
188
189 /* If a CPU failed to round up we could be here */
190 if (!kdb_tsk) {
191 kdb_printf("WARNING: no task for cpu %ld\n",
192 cpu);
193 continue;
194 }
195
196 sprintf(buf, "btt 0x%px\n", kdb_tsk);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500197 kdb_parse(buf);
198 touch_nmi_watchdog();
199 }
200 kdb_set_current_task(save_current_task);
201 return 0;
202 } else {
203 if (argc) {
204 nextarg = 1;
205 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr,
206 &offset, NULL);
207 if (diag)
208 return diag;
209 kdb_show_stack(kdb_current_task, (void *)addr);
210 return 0;
211 } else {
Douglas Anderson54af3e32019-09-25 13:02:18 -0700212 return kdb_bt1(kdb_current_task, ~0UL, false);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500213 }
214 }
215
216 /* NOTREACHED */
217 return 0;
218}