kdb_bt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include <linux/ctype.h>
  12. #include <linux/string.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/sched/debug.h>
  16. #include <linux/kdb.h>
  17. #include <linux/nmi.h>
  18. #include "kdb_private.h"
  19. static void kdb_show_stack(struct task_struct *p, void *addr)
  20. {
  21. int old_lvl = console_loglevel;
  22. console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
  23. kdb_trap_printk++;
  24. kdb_set_current_task(p);
  25. if (addr) {
  26. show_stack((struct task_struct *)p, addr);
  27. } else if (kdb_current_regs) {
  28. #ifdef CONFIG_X86
  29. show_stack(p, &kdb_current_regs->sp);
  30. #else
  31. show_stack(p, NULL);
  32. #endif
  33. } else {
  34. show_stack(p, NULL);
  35. }
  36. console_loglevel = old_lvl;
  37. kdb_trap_printk--;
  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. static int
  76. kdb_bt1(struct task_struct *p, unsigned long mask,
  77. int argcount, int btaprompt)
  78. {
  79. char buffer[2];
  80. if (kdb_getarea(buffer[0], (unsigned long)p) ||
  81. kdb_getarea(buffer[0], (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_getstr(buffer, sizeof(buffer),
  90. "Enter <q> to end, <cr> to continue:");
  91. if (buffer[0] == 'q') {
  92. kdb_printf("\n");
  93. return 1;
  94. }
  95. }
  96. touch_nmi_watchdog();
  97. return 0;
  98. }
  99. int
  100. kdb_bt(int argc, const char **argv)
  101. {
  102. int diag;
  103. int argcount = 5;
  104. int btaprompt = 1;
  105. int nextarg;
  106. unsigned long addr;
  107. long offset;
  108. /* Prompt after each proc in bta */
  109. kdbgetintenv("BTAPROMPT", &btaprompt);
  110. if (strcmp(argv[0], "bta") == 0) {
  111. struct task_struct *g, *p;
  112. unsigned long cpu;
  113. unsigned long mask = kdb_task_state_string(argc ? argv[1] :
  114. NULL);
  115. if (argc == 0)
  116. kdb_ps_suppressed();
  117. /* Run the active tasks first */
  118. for_each_online_cpu(cpu) {
  119. p = kdb_curr_task(cpu);
  120. if (kdb_bt1(p, mask, argcount, btaprompt))
  121. return 0;
  122. }
  123. /* Now the inactive tasks */
  124. kdb_do_each_thread(g, p) {
  125. if (KDB_FLAG(CMD_INTERRUPT))
  126. return 0;
  127. if (task_curr(p))
  128. continue;
  129. if (kdb_bt1(p, mask, argcount, btaprompt))
  130. return 0;
  131. } kdb_while_each_thread(g, p);
  132. } else if (strcmp(argv[0], "btp") == 0) {
  133. struct task_struct *p;
  134. unsigned long pid;
  135. if (argc != 1)
  136. return KDB_ARGCOUNT;
  137. diag = kdbgetularg((char *)argv[1], &pid);
  138. if (diag)
  139. return diag;
  140. p = find_task_by_pid_ns(pid, &init_pid_ns);
  141. if (p) {
  142. kdb_set_current_task(p);
  143. return kdb_bt1(p, ~0UL, argcount, 0);
  144. }
  145. kdb_printf("No process with pid == %ld found\n", pid);
  146. return 0;
  147. } else if (strcmp(argv[0], "btt") == 0) {
  148. if (argc != 1)
  149. return KDB_ARGCOUNT;
  150. diag = kdbgetularg((char *)argv[1], &addr);
  151. if (diag)
  152. return diag;
  153. kdb_set_current_task((struct task_struct *)addr);
  154. return kdb_bt1((struct task_struct *)addr, ~0UL, argcount, 0);
  155. } else if (strcmp(argv[0], "btc") == 0) {
  156. unsigned long cpu = ~0;
  157. struct task_struct *save_current_task = kdb_current_task;
  158. char buf[80];
  159. if (argc > 1)
  160. return KDB_ARGCOUNT;
  161. if (argc == 1) {
  162. diag = kdbgetularg((char *)argv[1], &cpu);
  163. if (diag)
  164. return diag;
  165. }
  166. /* Recursive use of kdb_parse, do not use argv after
  167. * this point */
  168. argv = NULL;
  169. if (cpu != ~0) {
  170. if (cpu >= num_possible_cpus() || !cpu_online(cpu)) {
  171. kdb_printf("no process for cpu %ld\n", cpu);
  172. return 0;
  173. }
  174. sprintf(buf, "btt 0x%px\n", KDB_TSK(cpu));
  175. kdb_parse(buf);
  176. return 0;
  177. }
  178. kdb_printf("btc: cpu status: ");
  179. kdb_parse("cpu\n");
  180. for_each_online_cpu(cpu) {
  181. void *kdb_tsk = KDB_TSK(cpu);
  182. /* If a CPU failed to round up we could be here */
  183. if (!kdb_tsk) {
  184. kdb_printf("WARNING: no task for cpu %ld\n",
  185. cpu);
  186. continue;
  187. }
  188. sprintf(buf, "btt 0x%px\n", kdb_tsk);
  189. kdb_parse(buf);
  190. touch_nmi_watchdog();
  191. }
  192. kdb_set_current_task(save_current_task);
  193. return 0;
  194. } else {
  195. if (argc) {
  196. nextarg = 1;
  197. diag = kdbgetaddrarg(argc, argv, &nextarg, &addr,
  198. &offset, NULL);
  199. if (diag)
  200. return diag;
  201. kdb_show_stack(kdb_current_task, (void *)addr);
  202. return 0;
  203. } else {
  204. return kdb_bt1(kdb_current_task, ~0UL, argcount, 0);
  205. }
  206. }
  207. /* NOTREACHED */
  208. return 0;
  209. }