kdb_bt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <asm/system.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 = 15;
  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. kdbgetintenv("BTARGS", &argcount); /* Arguments to print */
  109. kdbgetintenv("BTAPROMPT", &btaprompt); /* Prompt after each
  110. * proc in bta */
  111. if (strcmp(argv[0], "bta") == 0) {
  112. struct task_struct *g, *p;
  113. unsigned long cpu;
  114. unsigned long mask = kdb_task_state_string(argc ? argv[1] :
  115. NULL);
  116. if (argc == 0)
  117. kdb_ps_suppressed();
  118. /* Run the active tasks first */
  119. for_each_online_cpu(cpu) {
  120. p = kdb_curr_task(cpu);
  121. if (kdb_bt1(p, mask, argcount, btaprompt))
  122. return 0;
  123. }
  124. /* Now the inactive tasks */
  125. kdb_do_each_thread(g, p) {
  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. }