kdb_bt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.h>
  15. #include <linux/kdb.h>
  16. #include <linux/nmi.h>
  17. #include "kdb_private.h"
  18. static void kdb_show_stack(struct task_struct *p, void *addr)
  19. {
  20. int old_lvl = console_loglevel;
  21. console_loglevel = CONSOLE_LOGLEVEL_MOTORMOUTH;
  22. kdb_trap_printk++;
  23. kdb_set_current_task(p);
  24. if (addr) {
  25. show_stack((struct task_struct *)p, addr);
  26. } else if (kdb_current_regs) {
  27. #ifdef CONFIG_X86
  28. show_stack(p, &kdb_current_regs->sp);
  29. #else
  30. show_stack(p, NULL);
  31. #endif
  32. } else {
  33. show_stack(p, NULL);
  34. }
  35. console_loglevel = old_lvl;
  36. kdb_trap_printk--;
  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. static int
  75. kdb_bt1(struct task_struct *p, unsigned long mask,
  76. int argcount, int 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. int
  99. kdb_bt(int argc, const char **argv)
  100. {
  101. int diag;
  102. int argcount = 5;
  103. int btaprompt = 1;
  104. int nextarg;
  105. unsigned long addr;
  106. long offset;
  107. /* Prompt after each proc in bta */
  108. kdbgetintenv("BTAPROMPT", &btaprompt);
  109. if (strcmp(argv[0], "bta") == 0) {
  110. struct task_struct *g, *p;
  111. unsigned long cpu;
  112. unsigned long mask = kdb_task_state_string(argc ? argv[1] :
  113. NULL);
  114. if (argc == 0)
  115. kdb_ps_suppressed();
  116. /* Run the active tasks first */
  117. for_each_online_cpu(cpu) {
  118. p = kdb_curr_task(cpu);
  119. if (kdb_bt1(p, mask, argcount, btaprompt))
  120. return 0;
  121. }
  122. /* Now the inactive tasks */
  123. kdb_do_each_thread(g, p) {
  124. if (KDB_FLAG(CMD_INTERRUPT))
  125. return 0;
  126. if (task_curr(p))
  127. continue;
  128. if (kdb_bt1(p, mask, argcount, btaprompt))
  129. return 0;
  130. } kdb_while_each_thread(g, p);
  131. } else if (strcmp(argv[0], "btp") == 0) {
  132. struct task_struct *p;
  133. unsigned long pid;
  134. if (argc != 1)
  135. return KDB_ARGCOUNT;
  136. diag = kdbgetularg((char *)argv[1], &pid);
  137. if (diag)
  138. return diag;
  139. p = find_task_by_pid_ns(pid, &init_pid_ns);
  140. if (p) {
  141. kdb_set_current_task(p);
  142. return kdb_bt1(p, ~0UL, argcount, 0);
  143. }
  144. kdb_printf("No process with pid == %ld found\n", pid);
  145. return 0;
  146. } else if (strcmp(argv[0], "btt") == 0) {
  147. if (argc != 1)
  148. return KDB_ARGCOUNT;
  149. diag = kdbgetularg((char *)argv[1], &addr);
  150. if (diag)
  151. return diag;
  152. kdb_set_current_task((struct task_struct *)addr);
  153. return kdb_bt1((struct task_struct *)addr, ~0UL, argcount, 0);
  154. } else if (strcmp(argv[0], "btc") == 0) {
  155. unsigned long cpu = ~0;
  156. struct task_struct *save_current_task = kdb_current_task;
  157. char buf[80];
  158. if (argc > 1)
  159. return KDB_ARGCOUNT;
  160. if (argc == 1) {
  161. diag = kdbgetularg((char *)argv[1], &cpu);
  162. if (diag)
  163. return diag;
  164. }
  165. /* Recursive use of kdb_parse, do not use argv after
  166. * this point */
  167. argv = NULL;
  168. if (cpu != ~0) {
  169. if (cpu >= num_possible_cpus() || !cpu_online(cpu)) {
  170. kdb_printf("no process for cpu %ld\n", cpu);
  171. return 0;
  172. }
  173. sprintf(buf, "btt 0x%p\n", KDB_TSK(cpu));
  174. kdb_parse(buf);
  175. return 0;
  176. }
  177. kdb_printf("btc: cpu status: ");
  178. kdb_parse("cpu\n");
  179. for_each_online_cpu(cpu) {
  180. sprintf(buf, "btt 0x%p\n", KDB_TSK(cpu));
  181. kdb_parse(buf);
  182. touch_nmi_watchdog();
  183. }
  184. kdb_set_current_task(save_current_task);
  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, ~0UL, argcount, 0);
  197. }
  198. }
  199. /* NOTREACHED */
  200. return 0;
  201. }