stacktrace.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <asm/arcregs.h>
  31. #include <asm/unwind.h>
  32. #include <asm/switch_to.h>
  33. /*-------------------------------------------------------------------------
  34. * Unwinder Iterator
  35. *-------------------------------------------------------------------------
  36. */
  37. #ifdef CONFIG_ARC_DW2_UNWIND
  38. static void seed_unwind_frame_info(struct task_struct *tsk,
  39. struct pt_regs *regs,
  40. struct unwind_frame_info *frame_info)
  41. {
  42. /*
  43. * synchronous unwinding (e.g. dump_stack)
  44. * - uses current values of SP and friends
  45. */
  46. if (tsk == NULL && regs == NULL) {
  47. unsigned long fp, sp, blink, ret;
  48. frame_info->task = current;
  49. __asm__ __volatile__(
  50. "mov %0,r27\n\t"
  51. "mov %1,r28\n\t"
  52. "mov %2,r31\n\t"
  53. "mov %3,r63\n\t"
  54. : "=r"(fp), "=r"(sp), "=r"(blink), "=r"(ret)
  55. );
  56. frame_info->regs.r27 = fp;
  57. frame_info->regs.r28 = sp;
  58. frame_info->regs.r31 = blink;
  59. frame_info->regs.r63 = ret;
  60. frame_info->call_frame = 0;
  61. } else if (regs == NULL) {
  62. /*
  63. * Asynchronous unwinding of sleeping task
  64. * - Gets SP etc from task's pt_regs (saved bottom of kernel
  65. * mode stack of task)
  66. */
  67. frame_info->task = tsk;
  68. frame_info->regs.r27 = TSK_K_FP(tsk);
  69. frame_info->regs.r28 = TSK_K_ESP(tsk);
  70. frame_info->regs.r31 = TSK_K_BLINK(tsk);
  71. frame_info->regs.r63 = (unsigned int)__switch_to;
  72. /* In the prologue of __switch_to, first FP is saved on stack
  73. * and then SP is copied to FP. Dwarf assumes cfa as FP based
  74. * but we didn't save FP. The value retrieved above is FP's
  75. * state in previous frame.
  76. * As a work around for this, we unwind from __switch_to start
  77. * and adjust SP accordingly. The other limitation is that
  78. * __switch_to macro is dwarf rules are not generated for inline
  79. * assembly code
  80. */
  81. frame_info->regs.r27 = 0;
  82. frame_info->regs.r28 += 60;
  83. frame_info->call_frame = 0;
  84. } else {
  85. /*
  86. * Asynchronous unwinding of intr/exception
  87. * - Just uses the pt_regs passed
  88. */
  89. frame_info->task = tsk;
  90. frame_info->regs.r27 = regs->fp;
  91. frame_info->regs.r28 = regs->sp;
  92. frame_info->regs.r31 = regs->blink;
  93. frame_info->regs.r63 = regs->ret;
  94. frame_info->call_frame = 0;
  95. }
  96. }
  97. #endif
  98. notrace noinline unsigned int
  99. arc_unwind_core(struct task_struct *tsk, struct pt_regs *regs,
  100. int (*consumer_fn) (unsigned int, void *), void *arg)
  101. {
  102. #ifdef CONFIG_ARC_DW2_UNWIND
  103. int ret = 0;
  104. unsigned int address;
  105. struct unwind_frame_info frame_info;
  106. seed_unwind_frame_info(tsk, regs, &frame_info);
  107. while (1) {
  108. address = UNW_PC(&frame_info);
  109. if (!address || !__kernel_text_address(address))
  110. break;
  111. if (consumer_fn(address, arg) == -1)
  112. break;
  113. ret = arc_unwind(&frame_info);
  114. if (ret)
  115. break;
  116. frame_info.regs.r63 = frame_info.regs.r31;
  117. }
  118. return address; /* return the last address it saw */
  119. #else
  120. /* On ARC, only Dward based unwinder works. fp based backtracing is
  121. * not possible (-fno-omit-frame-pointer) because of the way function
  122. * prelogue is setup (callee regs saved and then fp set and not other
  123. * way around
  124. */
  125. pr_warn_once("CONFIG_ARC_DW2_UNWIND needs to be enabled\n");
  126. return 0;
  127. #endif
  128. }
  129. /*-------------------------------------------------------------------------
  130. * callbacks called by unwinder iterator to implement kernel APIs
  131. *
  132. * The callback can return -1 to force the iterator to stop, which by default
  133. * keeps going till the bottom-most frame.
  134. *-------------------------------------------------------------------------
  135. */
  136. /* Call-back which plugs into unwinding core to dump the stack in
  137. * case of panic/OOPs/BUG etc
  138. */
  139. static int __print_sym(unsigned int address, void *unused)
  140. {
  141. __print_symbol(" %s\n", address);
  142. return 0;
  143. }
  144. #ifdef CONFIG_STACKTRACE
  145. /* Call-back which plugs into unwinding core to capture the
  146. * traces needed by kernel on /proc/<pid>/stack
  147. */
  148. static int __collect_all(unsigned int address, void *arg)
  149. {
  150. struct stack_trace *trace = arg;
  151. if (trace->skip > 0)
  152. trace->skip--;
  153. else
  154. trace->entries[trace->nr_entries++] = address;
  155. if (trace->nr_entries >= trace->max_entries)
  156. return -1;
  157. return 0;
  158. }
  159. static int __collect_all_but_sched(unsigned int address, void *arg)
  160. {
  161. struct stack_trace *trace = arg;
  162. if (in_sched_functions(address))
  163. return 0;
  164. if (trace->skip > 0)
  165. trace->skip--;
  166. else
  167. trace->entries[trace->nr_entries++] = address;
  168. if (trace->nr_entries >= trace->max_entries)
  169. return -1;
  170. return 0;
  171. }
  172. #endif
  173. static int __get_first_nonsched(unsigned int address, void *unused)
  174. {
  175. if (in_sched_functions(address))
  176. return 0;
  177. return -1;
  178. }
  179. /*-------------------------------------------------------------------------
  180. * APIs expected by various kernel sub-systems
  181. *-------------------------------------------------------------------------
  182. */
  183. noinline void show_stacktrace(struct task_struct *tsk, struct pt_regs *regs)
  184. {
  185. pr_info("\nStack Trace:\n");
  186. arc_unwind_core(tsk, regs, __print_sym, NULL);
  187. }
  188. EXPORT_SYMBOL(show_stacktrace);
  189. /* Expected by sched Code */
  190. void show_stack(struct task_struct *tsk, unsigned long *sp)
  191. {
  192. show_stacktrace(tsk, NULL);
  193. }
  194. /* Another API expected by schedular, shows up in "ps" as Wait Channel
  195. * Of course just returning schedule( ) would be pointless so unwind until
  196. * the function is not in schedular code
  197. */
  198. unsigned int get_wchan(struct task_struct *tsk)
  199. {
  200. return arc_unwind_core(tsk, NULL, __get_first_nonsched, NULL);
  201. }
  202. #ifdef CONFIG_STACKTRACE
  203. /*
  204. * API required by CONFIG_STACKTRACE, CONFIG_LATENCYTOP.
  205. * A typical use is when /proc/<pid>/stack is queried by userland
  206. */
  207. void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
  208. {
  209. /* Assumes @tsk is sleeping so unwinds from __switch_to */
  210. arc_unwind_core(tsk, NULL, __collect_all_but_sched, trace);
  211. }
  212. void save_stack_trace(struct stack_trace *trace)
  213. {
  214. /* Pass NULL for task so it unwinds the current call frame */
  215. arc_unwind_core(NULL, NULL, __collect_all, trace);
  216. }
  217. EXPORT_SYMBOL_GPL(save_stack_trace);
  218. #endif