dumpstack.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  4. */
  5. #include <linux/kallsyms.h>
  6. #include <linux/kprobes.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/utsname.h>
  9. #include <linux/hardirq.h>
  10. #include <linux/kdebug.h>
  11. #include <linux/module.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/kexec.h>
  14. #include <linux/bug.h>
  15. #include <linux/nmi.h>
  16. #include <linux/sysfs.h>
  17. #include <asm/stacktrace.h>
  18. #include "dumpstack.h"
  19. int panic_on_unrecovered_nmi;
  20. unsigned int code_bytes = 64;
  21. int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
  22. static int die_counter;
  23. void printk_address(unsigned long address, int reliable)
  24. {
  25. printk(" [<%p>] %s%pS\n", (void *) address,
  26. reliable ? "" : "? ", (void *) address);
  27. }
  28. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  29. static void
  30. print_ftrace_graph_addr(unsigned long addr, void *data,
  31. const struct stacktrace_ops *ops,
  32. struct thread_info *tinfo, int *graph)
  33. {
  34. struct task_struct *task = tinfo->task;
  35. unsigned long ret_addr;
  36. int index = task->curr_ret_stack;
  37. if (addr != (unsigned long)return_to_handler)
  38. return;
  39. if (!task->ret_stack || index < *graph)
  40. return;
  41. index -= *graph;
  42. ret_addr = task->ret_stack[index].ret;
  43. ops->address(data, ret_addr, 1);
  44. (*graph)++;
  45. }
  46. #else
  47. static inline void
  48. print_ftrace_graph_addr(unsigned long addr, void *data,
  49. const struct stacktrace_ops *ops,
  50. struct thread_info *tinfo, int *graph)
  51. { }
  52. #endif
  53. /*
  54. * x86-64 can have up to three kernel stacks:
  55. * process stack
  56. * interrupt stack
  57. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  58. */
  59. static inline int valid_stack_ptr(struct thread_info *tinfo,
  60. void *p, unsigned int size, void *end)
  61. {
  62. void *t = tinfo;
  63. if (end) {
  64. if (p < end && p >= (end-THREAD_SIZE))
  65. return 1;
  66. else
  67. return 0;
  68. }
  69. return p > t && p < t + THREAD_SIZE - size;
  70. }
  71. unsigned long
  72. print_context_stack(struct thread_info *tinfo,
  73. unsigned long *stack, unsigned long bp,
  74. const struct stacktrace_ops *ops, void *data,
  75. unsigned long *end, int *graph)
  76. {
  77. struct stack_frame *frame = (struct stack_frame *)bp;
  78. while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
  79. unsigned long addr;
  80. addr = *stack;
  81. if (__kernel_text_address(addr)) {
  82. if ((unsigned long) stack == bp + sizeof(long)) {
  83. ops->address(data, addr, 1);
  84. frame = frame->next_frame;
  85. bp = (unsigned long) frame;
  86. } else {
  87. ops->address(data, addr, 0);
  88. }
  89. print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
  90. }
  91. stack++;
  92. }
  93. return bp;
  94. }
  95. static void
  96. print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
  97. {
  98. printk(data);
  99. print_symbol(msg, symbol);
  100. printk("\n");
  101. }
  102. static void print_trace_warning(void *data, char *msg)
  103. {
  104. printk("%s%s\n", (char *)data, msg);
  105. }
  106. static int print_trace_stack(void *data, char *name)
  107. {
  108. printk("%s <%s> ", (char *)data, name);
  109. return 0;
  110. }
  111. /*
  112. * Print one address/symbol entries per line.
  113. */
  114. static void print_trace_address(void *data, unsigned long addr, int reliable)
  115. {
  116. touch_nmi_watchdog();
  117. printk(data);
  118. printk_address(addr, reliable);
  119. }
  120. static const struct stacktrace_ops print_trace_ops = {
  121. .warning = print_trace_warning,
  122. .warning_symbol = print_trace_warning_symbol,
  123. .stack = print_trace_stack,
  124. .address = print_trace_address,
  125. };
  126. void
  127. show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
  128. unsigned long *stack, unsigned long bp, char *log_lvl)
  129. {
  130. printk("%sCall Trace:\n", log_lvl);
  131. dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
  132. }
  133. void show_trace(struct task_struct *task, struct pt_regs *regs,
  134. unsigned long *stack, unsigned long bp)
  135. {
  136. show_trace_log_lvl(task, regs, stack, bp, "");
  137. }
  138. void show_stack(struct task_struct *task, unsigned long *sp)
  139. {
  140. show_stack_log_lvl(task, NULL, sp, 0, "");
  141. }
  142. /*
  143. * The architecture-independent dump_stack generator
  144. */
  145. void dump_stack(void)
  146. {
  147. unsigned long bp = 0;
  148. unsigned long stack;
  149. #ifdef CONFIG_FRAME_POINTER
  150. if (!bp)
  151. get_bp(bp);
  152. #endif
  153. printk("Pid: %d, comm: %.20s %s %s %.*s\n",
  154. current->pid, current->comm, print_tainted(),
  155. init_utsname()->release,
  156. (int)strcspn(init_utsname()->version, " "),
  157. init_utsname()->version);
  158. show_trace(NULL, NULL, &stack, bp);
  159. }
  160. EXPORT_SYMBOL(dump_stack);
  161. static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
  162. static int die_owner = -1;
  163. static unsigned int die_nest_count;
  164. unsigned __kprobes long oops_begin(void)
  165. {
  166. int cpu;
  167. unsigned long flags;
  168. oops_enter();
  169. /* racy, but better than risking deadlock. */
  170. raw_local_irq_save(flags);
  171. cpu = smp_processor_id();
  172. if (!__raw_spin_trylock(&die_lock)) {
  173. if (cpu == die_owner)
  174. /* nested oops. should stop eventually */;
  175. else
  176. __raw_spin_lock(&die_lock);
  177. }
  178. die_nest_count++;
  179. die_owner = cpu;
  180. console_verbose();
  181. bust_spinlocks(1);
  182. return flags;
  183. }
  184. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  185. {
  186. if (regs && kexec_should_crash(current))
  187. crash_kexec(regs);
  188. bust_spinlocks(0);
  189. die_owner = -1;
  190. add_taint(TAINT_DIE);
  191. die_nest_count--;
  192. if (!die_nest_count)
  193. /* Nest count reaches zero, release the lock. */
  194. __raw_spin_unlock(&die_lock);
  195. raw_local_irq_restore(flags);
  196. oops_exit();
  197. if (!signr)
  198. return;
  199. if (in_interrupt())
  200. panic("Fatal exception in interrupt");
  201. if (panic_on_oops)
  202. panic("Fatal exception");
  203. do_exit(signr);
  204. }
  205. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  206. {
  207. #ifdef CONFIG_X86_32
  208. unsigned short ss;
  209. unsigned long sp;
  210. #endif
  211. printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
  212. #ifdef CONFIG_PREEMPT
  213. printk("PREEMPT ");
  214. #endif
  215. #ifdef CONFIG_SMP
  216. printk("SMP ");
  217. #endif
  218. #ifdef CONFIG_DEBUG_PAGEALLOC
  219. printk("DEBUG_PAGEALLOC");
  220. #endif
  221. printk("\n");
  222. sysfs_printk_last_file();
  223. if (notify_die(DIE_OOPS, str, regs, err,
  224. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  225. return 1;
  226. show_registers(regs);
  227. #ifdef CONFIG_X86_32
  228. sp = (unsigned long) (&regs->sp);
  229. savesegment(ss, ss);
  230. if (user_mode(regs)) {
  231. sp = regs->sp;
  232. ss = regs->ss & 0xffff;
  233. }
  234. printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
  235. print_symbol("%s", regs->ip);
  236. printk(" SS:ESP %04x:%08lx\n", ss, sp);
  237. #else
  238. /* Executive summary in case the oops scrolled away */
  239. printk(KERN_ALERT "RIP ");
  240. printk_address(regs->ip, 1);
  241. printk(" RSP <%016lx>\n", regs->sp);
  242. #endif
  243. return 0;
  244. }
  245. /*
  246. * This is gone through when something in the kernel has done something bad
  247. * and is about to be terminated:
  248. */
  249. void die(const char *str, struct pt_regs *regs, long err)
  250. {
  251. unsigned long flags = oops_begin();
  252. int sig = SIGSEGV;
  253. if (!user_mode_vm(regs))
  254. report_bug(regs->ip, regs);
  255. if (__die(str, regs, err))
  256. sig = 0;
  257. oops_end(flags, regs, sig);
  258. }
  259. void notrace __kprobes
  260. die_nmi(char *str, struct pt_regs *regs, int do_panic)
  261. {
  262. unsigned long flags;
  263. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
  264. return;
  265. /*
  266. * We are in trouble anyway, lets at least try
  267. * to get a message out.
  268. */
  269. flags = oops_begin();
  270. printk(KERN_EMERG "%s", str);
  271. printk(" on CPU%d, ip %08lx, registers:\n",
  272. smp_processor_id(), regs->ip);
  273. show_registers(regs);
  274. oops_end(flags, regs, 0);
  275. if (do_panic || panic_on_oops)
  276. panic("Non maskable interrupt");
  277. nmi_exit();
  278. local_irq_enable();
  279. do_exit(SIGBUS);
  280. }
  281. static int __init oops_setup(char *s)
  282. {
  283. if (!s)
  284. return -EINVAL;
  285. if (!strcmp(s, "panic"))
  286. panic_on_oops = 1;
  287. return 0;
  288. }
  289. early_param("oops", oops_setup);
  290. static int __init kstack_setup(char *s)
  291. {
  292. if (!s)
  293. return -EINVAL;
  294. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  295. return 0;
  296. }
  297. early_param("kstack", kstack_setup);
  298. static int __init code_bytes_setup(char *s)
  299. {
  300. code_bytes = simple_strtoul(s, NULL, 0);
  301. if (code_bytes > 8192)
  302. code_bytes = 8192;
  303. return 1;
  304. }
  305. __setup("code_bytes=", code_bytes_setup);