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