blob: 1f9f0e47aedaa6568bee2a85187ee54ab2134067 [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{
Jason Wesseld37d39a2010-05-20 21:04:27 -050024 kdb_trap_printk++;
Douglas Anderson2277b492019-09-25 13:02:20 -070025
Dmitry Safonov77819da2020-06-08 21:32:19 -070026 if (!addr && kdb_task_has_cpu(p)) {
27 int old_lvl = console_loglevel;
Douglas Anderson2277b492019-09-25 13:02:20 -070028
Dmitry Safonov77819da2020-06-08 21:32:19 -070029 console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
30 kdb_dump_stack_on_cpu(kdb_process_cpu(p));
31 console_loglevel = old_lvl;
32 } else {
Dmitry Safonov9cb8f062020-06-08 21:32:29 -070033 show_stack(p, addr, KERN_EMERG);
Dmitry Safonov77819da2020-06-08 21:32:19 -070034 }
35
Jason Wesseld37d39a2010-05-20 21:04:27 -050036 kdb_trap_printk--;
Jason Wessel5d5314d2010-05-20 21:04:20 -050037}
38
39/*
40 * kdb_bt
41 *
42 * This function implements the 'bt' command. Print a stack
43 * traceback.
44 *
45 * bt [<address-expression>] (addr-exp is for alternate stacks)
46 * btp <pid> Kernel stack for <pid>
47 * btt <address-expression> Kernel stack for task structure at
48 * <address-expression>
49 * bta [DRSTCZEUIMA] All useful processes, optionally
50 * filtered by state
51 * btc [<cpu>] The current process on one cpu,
52 * default is all cpus
53 *
54 * bt <address-expression> refers to a address on the stack, that location
55 * is assumed to contain a return address.
56 *
57 * btt <address-expression> refers to the address of a struct task.
58 *
59 * Inputs:
60 * argc argument count
61 * argv argument vector
62 * Outputs:
63 * None.
64 * Returns:
65 * zero for success, a kdb diagnostic if error
66 * Locking:
67 * none.
68 * Remarks:
69 * Backtrack works best when the code uses frame pointers. But even
70 * without frame pointers we should get a reasonable trace.
71 *
72 * mds comes in handy when examining the stack to do a manual traceback or
73 * to get a starting point for bt <address-expression>.
74 */
75
76static int
Douglas Anderson54af3e32019-09-25 13:02:18 -070077kdb_bt1(struct task_struct *p, unsigned long mask, bool btaprompt)
Jason Wessel5d5314d2010-05-20 21:04:20 -050078{
Daniel Thompson4f27e822019-10-25 08:33:26 +010079 char ch;
80
81 if (kdb_getarea(ch, (unsigned long)p) ||
82 kdb_getarea(ch, (unsigned long)(p+1)-1))
Jason Wessel5d5314d2010-05-20 21:04:20 -050083 return KDB_BADADDR;
84 if (!kdb_task_state(p, mask))
85 return 0;
86 kdb_printf("Stack traceback for pid %d\n", p->pid);
87 kdb_ps1(p);
88 kdb_show_stack(p, NULL);
89 if (btaprompt) {
Daniel Thompson4f27e822019-10-25 08:33:26 +010090 kdb_printf("Enter <q> to end, <cr> or <space> to continue:");
91 do {
92 ch = kdb_getchar();
93 } while (!strchr("\r\n q", ch));
94 kdb_printf("\n");
95
96 /* reset the pager */
97 kdb_nextline = 1;
98
99 if (ch == 'q')
Jason Wessel5d5314d2010-05-20 21:04:20 -0500100 return 1;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500101 }
102 touch_nmi_watchdog();
103 return 0;
104}
105
Douglas Anderson55a7e232019-09-25 13:02:19 -0700106static void
107kdb_bt_cpu(unsigned long cpu)
108{
109 struct task_struct *kdb_tsk;
110
111 if (cpu >= num_possible_cpus() || !cpu_online(cpu)) {
112 kdb_printf("WARNING: no process for cpu %ld\n", cpu);
113 return;
114 }
115
116 /* If a CPU failed to round up we could be here */
117 kdb_tsk = KDB_TSK(cpu);
118 if (!kdb_tsk) {
119 kdb_printf("WARNING: no task for cpu %ld\n", cpu);
120 return;
121 }
122
Douglas Anderson55a7e232019-09-25 13:02:19 -0700123 kdb_bt1(kdb_tsk, ~0UL, false);
124}
125
Jason Wessel5d5314d2010-05-20 21:04:20 -0500126int
127kdb_bt(int argc, const char **argv)
128{
129 int diag;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500130 int btaprompt = 1;
131 int nextarg;
132 unsigned long addr;
133 long offset;
134
Jason Wessel3bdb65e2011-06-30 14:12:00 -0500135 /* Prompt after each proc in bta */
136 kdbgetintenv("BTAPROMPT", &btaprompt);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500137
138 if (strcmp(argv[0], "bta") == 0) {
139 struct task_struct *g, *p;
140 unsigned long cpu;
141 unsigned long mask = kdb_task_state_string(argc ? argv[1] :
142 NULL);
143 if (argc == 0)
144 kdb_ps_suppressed();
145 /* Run the active tasks first */
146 for_each_online_cpu(cpu) {
147 p = kdb_curr_task(cpu);
Douglas Anderson54af3e32019-09-25 13:02:18 -0700148 if (kdb_bt1(p, mask, btaprompt))
Jason Wessel5d5314d2010-05-20 21:04:20 -0500149 return 0;
150 }
151 /* Now the inactive tasks */
Davidlohr Buesoece4cea2020-09-07 13:32:06 -0700152 for_each_process_thread(g, p) {
Jason Wesseld1871b32012-08-26 21:43:12 -0500153 if (KDB_FLAG(CMD_INTERRUPT))
154 return 0;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500155 if (task_curr(p))
156 continue;
Douglas Anderson54af3e32019-09-25 13:02:18 -0700157 if (kdb_bt1(p, mask, btaprompt))
Jason Wessel5d5314d2010-05-20 21:04:20 -0500158 return 0;
Davidlohr Buesoece4cea2020-09-07 13:32:06 -0700159 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500160 } else if (strcmp(argv[0], "btp") == 0) {
161 struct task_struct *p;
162 unsigned long pid;
163 if (argc != 1)
164 return KDB_ARGCOUNT;
165 diag = kdbgetularg((char *)argv[1], &pid);
166 if (diag)
167 return diag;
168 p = find_task_by_pid_ns(pid, &init_pid_ns);
Douglas Anderson9441d5f2019-11-09 11:16:43 -0800169 if (p)
Douglas Anderson54af3e32019-09-25 13:02:18 -0700170 return kdb_bt1(p, ~0UL, false);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500171 kdb_printf("No process with pid == %ld found\n", pid);
172 return 0;
173 } else if (strcmp(argv[0], "btt") == 0) {
174 if (argc != 1)
175 return KDB_ARGCOUNT;
176 diag = kdbgetularg((char *)argv[1], &addr);
177 if (diag)
178 return diag;
Douglas Anderson54af3e32019-09-25 13:02:18 -0700179 return kdb_bt1((struct task_struct *)addr, ~0UL, false);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500180 } else if (strcmp(argv[0], "btc") == 0) {
181 unsigned long cpu = ~0;
Jason Wessel5d5314d2010-05-20 21:04:20 -0500182 if (argc > 1)
183 return KDB_ARGCOUNT;
184 if (argc == 1) {
185 diag = kdbgetularg((char *)argv[1], &cpu);
186 if (diag)
187 return diag;
188 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500189 if (cpu != ~0) {
Douglas Anderson55a7e232019-09-25 13:02:19 -0700190 kdb_bt_cpu(cpu);
191 } else {
192 /*
193 * Recursive use of kdb_parse, do not use argv after
194 * this point.
195 */
196 argv = NULL;
197 kdb_printf("btc: cpu status: ");
198 kdb_parse("cpu\n");
199 for_each_online_cpu(cpu) {
200 kdb_bt_cpu(cpu);
201 touch_nmi_watchdog();
Jason Wessel5d5314d2010-05-20 21:04:20 -0500202 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500203 }
Jason Wessel5d5314d2010-05-20 21:04:20 -0500204 return 0;
205 } else {
206 if (argc) {
207 nextarg = 1;
208 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr,
209 &offset, NULL);
210 if (diag)
211 return diag;
212 kdb_show_stack(kdb_current_task, (void *)addr);
213 return 0;
214 } else {
Douglas Anderson54af3e32019-09-25 13:02:18 -0700215 return kdb_bt1(kdb_current_task, ~0UL, false);
Jason Wessel5d5314d2010-05-20 21:04:20 -0500216 }
217 }
218
219 /* NOTREACHED */
220 return 0;
221}