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