xref: /openbmc/linux/kernel/debug/kdb/kdb_bt.c (revision 1a18374f)
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 ch;
79 
80 	if (kdb_getarea(ch, (unsigned long)p) ||
81 	    kdb_getarea(ch, (unsigned long)(p+1)-1))
82 		return KDB_BADADDR;
83 	if (!kdb_task_state(p, mask))
84 		return 0;
85 	kdb_printf("Stack traceback for pid %d\n", p->pid);
86 	kdb_ps1(p);
87 	kdb_show_stack(p, NULL);
88 	if (btaprompt) {
89 		kdb_printf("Enter <q> to end, <cr> or <space> to continue:");
90 		do {
91 			ch = kdb_getchar();
92 		} while (!strchr("\r\n q", ch));
93 		kdb_printf("\n");
94 
95 		/* reset the pager */
96 		kdb_nextline = 1;
97 
98 		if (ch == 'q')
99 			return 1;
100 	}
101 	touch_nmi_watchdog();
102 	return 0;
103 }
104 
105 static void
106 kdb_bt_cpu(unsigned long cpu)
107 {
108 	struct task_struct *kdb_tsk;
109 
110 	if (cpu >= num_possible_cpus() || !cpu_online(cpu)) {
111 		kdb_printf("WARNING: no process for cpu %ld\n", cpu);
112 		return;
113 	}
114 
115 	/* If a CPU failed to round up we could be here */
116 	kdb_tsk = KDB_TSK(cpu);
117 	if (!kdb_tsk) {
118 		kdb_printf("WARNING: no task for cpu %ld\n", cpu);
119 		return;
120 	}
121 
122 	kdb_set_current_task(kdb_tsk);
123 	kdb_bt1(kdb_tsk, ~0UL, false);
124 }
125 
126 int
127 kdb_bt(int argc, const char **argv)
128 {
129 	int diag;
130 	int btaprompt = 1;
131 	int nextarg;
132 	unsigned long addr;
133 	long offset;
134 
135 	/* Prompt after each proc in bta */
136 	kdbgetintenv("BTAPROMPT", &btaprompt);
137 
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);
148 			if (kdb_bt1(p, mask, btaprompt))
149 				return 0;
150 		}
151 		/* Now the inactive tasks */
152 		kdb_do_each_thread(g, p) {
153 			if (KDB_FLAG(CMD_INTERRUPT))
154 				return 0;
155 			if (task_curr(p))
156 				continue;
157 			if (kdb_bt1(p, mask, btaprompt))
158 				return 0;
159 		} kdb_while_each_thread(g, p);
160 	} 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);
169 		if (p) {
170 			kdb_set_current_task(p);
171 			return kdb_bt1(p, ~0UL, false);
172 		}
173 		kdb_printf("No process with pid == %ld found\n", pid);
174 		return 0;
175 	} else if (strcmp(argv[0], "btt") == 0) {
176 		if (argc != 1)
177 			return KDB_ARGCOUNT;
178 		diag = kdbgetularg((char *)argv[1], &addr);
179 		if (diag)
180 			return diag;
181 		kdb_set_current_task((struct task_struct *)addr);
182 		return kdb_bt1((struct task_struct *)addr, ~0UL, false);
183 	} else if (strcmp(argv[0], "btc") == 0) {
184 		unsigned long cpu = ~0;
185 		struct task_struct *save_current_task = kdb_current_task;
186 		if (argc > 1)
187 			return KDB_ARGCOUNT;
188 		if (argc == 1) {
189 			diag = kdbgetularg((char *)argv[1], &cpu);
190 			if (diag)
191 				return diag;
192 		}
193 		if (cpu != ~0) {
194 			kdb_bt_cpu(cpu);
195 		} else {
196 			/*
197 			 * Recursive use of kdb_parse, do not use argv after
198 			 * this point.
199 			 */
200 			argv = NULL;
201 			kdb_printf("btc: cpu status: ");
202 			kdb_parse("cpu\n");
203 			for_each_online_cpu(cpu) {
204 				kdb_bt_cpu(cpu);
205 				touch_nmi_watchdog();
206 			}
207 			kdb_set_current_task(save_current_task);
208 		}
209 		return 0;
210 	} else {
211 		if (argc) {
212 			nextarg = 1;
213 			diag = kdbgetaddrarg(argc, argv, &nextarg, &addr,
214 					     &offset, NULL);
215 			if (diag)
216 				return diag;
217 			kdb_show_stack(kdb_current_task, (void *)addr);
218 			return 0;
219 		} else {
220 			return kdb_bt1(kdb_current_task, ~0UL, false);
221 		}
222 	}
223 
224 	/* NOTREACHED */
225 	return 0;
226 }
227