stacktrace.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * stacktrace.c : stacktracing APIs needed by rest of kernel
  3. * (wrappers over ARC dwarf based unwinder)
  4. *
  5. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * vineetg: aug 2009
  12. * -Implemented CONFIG_STACKTRACE APIs, primarily save_stack_trace_tsk( )
  13. * for displaying task's kernel mode call stack in /proc/<pid>/stack
  14. * -Iterator based approach to have single copy of unwinding core and APIs
  15. * needing unwinding, implement the logic in iterator regarding:
  16. * = which frame onwards to start capture
  17. * = which frame to stop capturing (wchan)
  18. * = specifics of data structs where trace is saved(CONFIG_STACKTRACE etc)
  19. *
  20. * vineetg: March 2009
  21. * -Implemented correct versions of thread_saved_pc() and get_wchan()
  22. *
  23. * rajeshwarr: 2008
  24. * -Initial implementation
  25. */
  26. #include <linux/ptrace.h>
  27. #include <linux/export.h>
  28. #include <linux/stacktrace.h>
  29. #include <linux/kallsyms.h>
  30. #include <linux/sched/debug.h>
  31. #include <asm/arcregs.h>
  32. #include <asm/unwind.h>
  33. #include <asm/switch_to.h>
  34. /*-------------------------------------------------------------------------
  35. * Unwinder Iterator
  36. *-------------------------------------------------------------------------
  37. */
  38. #ifdef CONFIG_ARC_DW2_UNWIND
  39. static int
  40. seed_unwind_frame_info(struct task_struct *tsk, struct pt_regs *regs,
  41. struct unwind_frame_info *frame_info)
  42. {
  43. /*
  44. * synchronous unwinding (e.g. dump_stack)
  45. * - uses current values of SP and friends
  46. */
  47. if (regs == NULL && (tsk == NULL || tsk == current)) {
  48. unsigned long fp, sp, blink, ret;
  49. frame_info->task = current;
  50. __asm__ __volatile__(
  51. "mov %0,r27\n\t"
  52. "mov %1,r28\n\t"
  53. "mov %2,r31\n\t"
  54. "mov %3,r63\n\t"
  55. : "=r"(fp), "=r"(sp), "=r"(blink), "=r"(ret)
  56. );
  57. frame_info->regs.r27 = fp;
  58. frame_info->regs.r28 = sp;
  59. frame_info->regs.r31 = blink;
  60. frame_info->regs.r63 = ret;
  61. frame_info->call_frame = 0;
  62. } else if (regs == NULL) {
  63. /*
  64. * Asynchronous unwinding of a likely sleeping task
  65. * - first ensure it is actually sleeping
  66. * - if so, it will be in __switch_to, kernel mode SP of task
  67. * is safe-kept and BLINK at a well known location in there
  68. */
  69. if (tsk->state == TASK_RUNNING)
  70. return -1;
  71. frame_info->task = tsk;
  72. frame_info->regs.r27 = TSK_K_FP(tsk);
  73. frame_info->regs.r28 = TSK_K_ESP(tsk);
  74. frame_info->regs.r31 = TSK_K_BLINK(tsk);
  75. frame_info->regs.r63 = (unsigned int)__switch_to;
  76. /* In the prologue of __switch_to, first FP is saved on stack
  77. * and then SP is copied to FP. Dwarf assumes cfa as FP based
  78. * but we didn't save FP. The value retrieved above is FP's
  79. * state in previous frame.
  80. * As a work around for this, we unwind from __switch_to start
  81. * and adjust SP accordingly. The other limitation is that
  82. * __switch_to macro is dwarf rules are not generated for inline
  83. * assembly code
  84. */
  85. frame_info->regs.r27 = 0;
  86. frame_info->regs.r28 += 60;
  87. frame_info->call_frame = 0;
  88. } else {
  89. /*
  90. * Asynchronous unwinding of intr/exception
  91. * - Just uses the pt_regs passed
  92. */
  93. frame_info->task = tsk;
  94. frame_info->regs.r27 = regs->fp;
  95. frame_info->regs.r28 = regs->sp;
  96. frame_info->regs.r31 = regs->blink;
  97. frame_info->regs.r63 = regs->ret;
  98. frame_info->call_frame = 0;
  99. }
  100. return 0;
  101. }
  102. #endif
  103. notrace noinline unsigned int
  104. arc_unwind_core(struct task_struct *tsk, struct pt_regs *regs,
  105. int (*consumer_fn) (unsigned int, void *), void *arg)
  106. {
  107. #ifdef CONFIG_ARC_DW2_UNWIND
  108. int ret = 0, cnt = 0;
  109. unsigned int address;
  110. struct unwind_frame_info frame_info;
  111. if (seed_unwind_frame_info(tsk, regs, &frame_info))
  112. return 0;
  113. while (1) {
  114. address = UNW_PC(&frame_info);
  115. if (!address || !__kernel_text_address(address))
  116. break;
  117. if (consumer_fn(address, arg) == -1)
  118. break;
  119. ret = arc_unwind(&frame_info);
  120. if (ret)
  121. break;
  122. frame_info.regs.r63 = frame_info.regs.r31;
  123. if (cnt++ > 128) {
  124. printk("unwinder looping too long, aborting !\n");
  125. return 0;
  126. }
  127. }
  128. return address; /* return the last address it saw */
  129. #else
  130. /* On ARC, only Dward based unwinder works. fp based backtracing is
  131. * not possible (-fno-omit-frame-pointer) because of the way function
  132. * prelogue is setup (callee regs saved and then fp set and not other
  133. * way around
  134. */
  135. pr_warn_once("CONFIG_ARC_DW2_UNWIND needs to be enabled\n");
  136. return 0;
  137. #endif
  138. }
  139. /*-------------------------------------------------------------------------
  140. * callbacks called by unwinder iterator to implement kernel APIs
  141. *
  142. * The callback can return -1 to force the iterator to stop, which by default
  143. * keeps going till the bottom-most frame.
  144. *-------------------------------------------------------------------------
  145. */
  146. /* Call-back which plugs into unwinding core to dump the stack in
  147. * case of panic/OOPs/BUG etc
  148. */
  149. static int __print_sym(unsigned int address, void *unused)
  150. {
  151. printk(" %pS\n", (void *)address);
  152. return 0;
  153. }
  154. #ifdef CONFIG_STACKTRACE
  155. /* Call-back which plugs into unwinding core to capture the
  156. * traces needed by kernel on /proc/<pid>/stack
  157. */
  158. static int __collect_all(unsigned int address, void *arg)
  159. {
  160. struct stack_trace *trace = arg;
  161. if (trace->skip > 0)
  162. trace->skip--;
  163. else
  164. trace->entries[trace->nr_entries++] = address;
  165. if (trace->nr_entries >= trace->max_entries)
  166. return -1;
  167. return 0;
  168. }
  169. static int __collect_all_but_sched(unsigned int address, void *arg)
  170. {
  171. struct stack_trace *trace = arg;
  172. if (in_sched_functions(address))
  173. return 0;
  174. if (trace->skip > 0)
  175. trace->skip--;
  176. else
  177. trace->entries[trace->nr_entries++] = address;
  178. if (trace->nr_entries >= trace->max_entries)
  179. return -1;
  180. return 0;
  181. }
  182. #endif
  183. static int __get_first_nonsched(unsigned int address, void *unused)
  184. {
  185. if (in_sched_functions(address))
  186. return 0;
  187. return -1;
  188. }
  189. /*-------------------------------------------------------------------------
  190. * APIs expected by various kernel sub-systems
  191. *-------------------------------------------------------------------------
  192. */
  193. noinline void show_stacktrace(struct task_struct *tsk, struct pt_regs *regs)
  194. {
  195. pr_info("\nStack Trace:\n");
  196. arc_unwind_core(tsk, regs, __print_sym, NULL);
  197. }
  198. EXPORT_SYMBOL(show_stacktrace);
  199. /* Expected by sched Code */
  200. void show_stack(struct task_struct *tsk, unsigned long *sp)
  201. {
  202. show_stacktrace(tsk, NULL);
  203. }
  204. /* Another API expected by schedular, shows up in "ps" as Wait Channel
  205. * Of course just returning schedule( ) would be pointless so unwind until
  206. * the function is not in schedular code
  207. */
  208. unsigned int get_wchan(struct task_struct *tsk)
  209. {
  210. return arc_unwind_core(tsk, NULL, __get_first_nonsched, NULL);
  211. }
  212. #ifdef CONFIG_STACKTRACE
  213. /*
  214. * API required by CONFIG_STACKTRACE, CONFIG_LATENCYTOP.
  215. * A typical use is when /proc/<pid>/stack is queried by userland
  216. */
  217. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  218. {
  219. /* Assumes @tsk is sleeping so unwinds from __switch_to */
  220. arc_unwind_core(tsk, NULL, __collect_all_but_sched, trace);
  221. }
  222. void save_stack_trace(struct stack_trace *trace)
  223. {
  224. /* Pass NULL for task so it unwinds the current call frame */
  225. arc_unwind_core(NULL, NULL, __collect_all, trace);
  226. }
  227. EXPORT_SYMBOL_GPL(save_stack_trace);
  228. #endif