xref: /openbmc/linux/kernel/debug/kdb/kdb_bt.c (revision 2277b492)
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/signal.h>
16 #include <linux/sched/debug.h>
17 #include <linux/kdb.h>
18 #include <linux/nmi.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 
26 	console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
27 	kdb_trap_printk++;
28 
29 	if (!addr && kdb_task_has_cpu(p))
30 		kdb_dump_stack_on_cpu(kdb_process_cpu(p));
31 	else
32 		show_stack(p, addr);
33 
34 	console_loglevel = old_lvl;
35 	kdb_trap_printk--;
36 }
37 
38 /*
39  * kdb_bt
40  *
41  *	This function implements the 'bt' command.  Print a stack
42  *	traceback.
43  *
44  *	bt [<address-expression>]	(addr-exp is for alternate stacks)
45  *	btp <pid>			Kernel stack for <pid>
46  *	btt <address-expression>	Kernel stack for task structure at
47  *					<address-expression>
48  *	bta [DRSTCZEUIMA]		All useful processes, optionally
49  *					filtered by state
50  *	btc [<cpu>]			The current process on one cpu,
51  *					default is all cpus
52  *
53  *	bt <address-expression> refers to a address on the stack, that location
54  *	is assumed to contain a return address.
55  *
56  *	btt <address-expression> refers to the address of a struct task.
57  *
58  * Inputs:
59  *	argc	argument count
60  *	argv	argument vector
61  * Outputs:
62  *	None.
63  * Returns:
64  *	zero for success, a kdb diagnostic if error
65  * Locking:
66  *	none.
67  * Remarks:
68  *	Backtrack works best when the code uses frame pointers.  But even
69  *	without frame pointers we should get a reasonable trace.
70  *
71  *	mds comes in handy when examining the stack to do a manual traceback or
72  *	to get a starting point for bt <address-expression>.
73  */
74 
75 static int
76 kdb_bt1(struct task_struct *p, unsigned long mask, bool btaprompt)
77 {
78 	char buffer[2];
79 	if (kdb_getarea(buffer[0], (unsigned long)p) ||
80 	    kdb_getarea(buffer[0], (unsigned long)(p+1)-1))
81 		return KDB_BADADDR;
82 	if (!kdb_task_state(p, mask))
83 		return 0;
84 	kdb_printf("Stack traceback for pid %d\n", p->pid);
85 	kdb_ps1(p);
86 	kdb_show_stack(p, NULL);
87 	if (btaprompt) {
88 		kdb_getstr(buffer, sizeof(buffer),
89 			   "Enter <q> to end, <cr> to continue:");
90 		if (buffer[0] == 'q') {
91 			kdb_printf("\n");
92 			return 1;
93 		}
94 	}
95 	touch_nmi_watchdog();
96 	return 0;
97 }
98 
99 static void
100 kdb_bt_cpu(unsigned long cpu)
101 {
102 	struct task_struct *kdb_tsk;
103 
104 	if (cpu >= num_possible_cpus() || !cpu_online(cpu)) {
105 		kdb_printf("WARNING: no process for cpu %ld\n", cpu);
106 		return;
107 	}
108 
109 	/* If a CPU failed to round up we could be here */
110 	kdb_tsk = KDB_TSK(cpu);
111 	if (!kdb_tsk) {
112 		kdb_printf("WARNING: no task for cpu %ld\n", cpu);
113 		return;
114 	}
115 
116 	kdb_set_current_task(kdb_tsk);
117 	kdb_bt1(kdb_tsk, ~0UL, false);
118 }
119 
120 int
121 kdb_bt(int argc, const char **argv)
122 {
123 	int diag;
124 	int btaprompt = 1;
125 	int nextarg;
126 	unsigned long addr;
127 	long offset;
128 
129 	/* Prompt after each proc in bta */
130 	kdbgetintenv("BTAPROMPT", &btaprompt);
131 
132 	if (strcmp(argv[0], "bta") == 0) {
133 		struct task_struct *g, *p;
134 		unsigned long cpu;
135 		unsigned long mask = kdb_task_state_string(argc ? argv[1] :
136 							   NULL);
137 		if (argc == 0)
138 			kdb_ps_suppressed();
139 		/* Run the active tasks first */
140 		for_each_online_cpu(cpu) {
141 			p = kdb_curr_task(cpu);
142 			if (kdb_bt1(p, mask, btaprompt))
143 				return 0;
144 		}
145 		/* Now the inactive tasks */
146 		kdb_do_each_thread(g, p) {
147 			if (KDB_FLAG(CMD_INTERRUPT))
148 				return 0;
149 			if (task_curr(p))
150 				continue;
151 			if (kdb_bt1(p, mask, btaprompt))
152 				return 0;
153 		} kdb_while_each_thread(g, p);
154 	} else if (strcmp(argv[0], "btp") == 0) {
155 		struct task_struct *p;
156 		unsigned long pid;
157 		if (argc != 1)
158 			return KDB_ARGCOUNT;
159 		diag = kdbgetularg((char *)argv[1], &pid);
160 		if (diag)
161 			return diag;
162 		p = find_task_by_pid_ns(pid, &init_pid_ns);
163 		if (p) {
164 			kdb_set_current_task(p);
165 			return kdb_bt1(p, ~0UL, false);
166 		}
167 		kdb_printf("No process with pid == %ld found\n", pid);
168 		return 0;
169 	} else if (strcmp(argv[0], "btt") == 0) {
170 		if (argc != 1)
171 			return KDB_ARGCOUNT;
172 		diag = kdbgetularg((char *)argv[1], &addr);
173 		if (diag)
174 			return diag;
175 		kdb_set_current_task((struct task_struct *)addr);
176 		return kdb_bt1((struct task_struct *)addr, ~0UL, false);
177 	} else if (strcmp(argv[0], "btc") == 0) {
178 		unsigned long cpu = ~0;
179 		struct task_struct *save_current_task = kdb_current_task;
180 		if (argc > 1)
181 			return KDB_ARGCOUNT;
182 		if (argc == 1) {
183 			diag = kdbgetularg((char *)argv[1], &cpu);
184 			if (diag)
185 				return diag;
186 		}
187 		if (cpu != ~0) {
188 			kdb_bt_cpu(cpu);
189 		} else {
190 			/*
191 			 * Recursive use of kdb_parse, do not use argv after
192 			 * this point.
193 			 */
194 			argv = NULL;
195 			kdb_printf("btc: cpu status: ");
196 			kdb_parse("cpu\n");
197 			for_each_online_cpu(cpu) {
198 				kdb_bt_cpu(cpu);
199 				touch_nmi_watchdog();
200 			}
201 			kdb_set_current_task(save_current_task);
202 		}
203 		return 0;
204 	} else {
205 		if (argc) {
206 			nextarg = 1;
207 			diag = kdbgetaddrarg(argc, argv, &nextarg, &addr,
208 					     &offset, NULL);
209 			if (diag)
210 				return diag;
211 			kdb_show_stack(kdb_current_task, (void *)addr);
212 			return 0;
213 		} else {
214 			return kdb_bt1(kdb_current_task, ~0UL, false);
215 		}
216 	}
217 
218 	/* NOTREACHED */
219 	return 0;
220 }
221