stacktrace.c 6.6 KB

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