trace_stack.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
  3. *
  4. */
  5. #include <linux/stacktrace.h>
  6. #include <linux/kallsyms.h>
  7. #include <linux/seq_file.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/ftrace.h>
  12. #include <linux/module.h>
  13. #include <linux/sysctl.h>
  14. #include <linux/init.h>
  15. #include <linux/fs.h>
  16. #include <linux/magic.h>
  17. #include <asm/setup.h>
  18. #include "trace.h"
  19. #define STACK_TRACE_ENTRIES 500
  20. #ifdef CC_USING_FENTRY
  21. # define fentry 1
  22. #else
  23. # define fentry 0
  24. #endif
  25. static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
  26. { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
  27. static unsigned stack_dump_index[STACK_TRACE_ENTRIES];
  28. /*
  29. * Reserve one entry for the passed in ip. This will allow
  30. * us to remove most or all of the stack size overhead
  31. * added by the stack tracer itself.
  32. */
  33. static struct stack_trace max_stack_trace = {
  34. .max_entries = STACK_TRACE_ENTRIES - 1,
  35. .entries = &stack_dump_trace[1],
  36. };
  37. static unsigned long max_stack_size;
  38. static arch_spinlock_t max_stack_lock =
  39. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  40. static DEFINE_PER_CPU(int, trace_active);
  41. static DEFINE_MUTEX(stack_sysctl_mutex);
  42. int stack_tracer_enabled;
  43. static int last_stack_tracer_enabled;
  44. static inline void
  45. check_stack(unsigned long ip, unsigned long *stack)
  46. {
  47. unsigned long this_size, flags;
  48. unsigned long *p, *top, *start;
  49. static int tracer_frame;
  50. int frame_size = ACCESS_ONCE(tracer_frame);
  51. int i;
  52. this_size = ((unsigned long)stack) & (THREAD_SIZE-1);
  53. this_size = THREAD_SIZE - this_size;
  54. /* Remove the frame of the tracer */
  55. this_size -= frame_size;
  56. if (this_size <= max_stack_size)
  57. return;
  58. /* we do not handle interrupt stacks yet */
  59. if (!object_is_on_stack(stack))
  60. return;
  61. local_irq_save(flags);
  62. arch_spin_lock(&max_stack_lock);
  63. /* In case another CPU set the tracer_frame on us */
  64. if (unlikely(!frame_size))
  65. this_size -= tracer_frame;
  66. /* a race could have already updated it */
  67. if (this_size <= max_stack_size)
  68. goto out;
  69. max_stack_size = this_size;
  70. max_stack_trace.nr_entries = 0;
  71. max_stack_trace.skip = 3;
  72. save_stack_trace(&max_stack_trace);
  73. /*
  74. * Add the passed in ip from the function tracer.
  75. * Searching for this on the stack will skip over
  76. * most of the overhead from the stack tracer itself.
  77. */
  78. stack_dump_trace[0] = ip;
  79. max_stack_trace.nr_entries++;
  80. /*
  81. * Now find where in the stack these are.
  82. */
  83. i = 0;
  84. start = stack;
  85. top = (unsigned long *)
  86. (((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE);
  87. /*
  88. * Loop through all the entries. One of the entries may
  89. * for some reason be missed on the stack, so we may
  90. * have to account for them. If they are all there, this
  91. * loop will only happen once. This code only takes place
  92. * on a new max, so it is far from a fast path.
  93. */
  94. while (i < max_stack_trace.nr_entries) {
  95. int found = 0;
  96. stack_dump_index[i] = this_size;
  97. p = start;
  98. for (; p < top && i < max_stack_trace.nr_entries; p++) {
  99. if (*p == stack_dump_trace[i]) {
  100. this_size = stack_dump_index[i++] =
  101. (top - p) * sizeof(unsigned long);
  102. found = 1;
  103. /* Start the search from here */
  104. start = p + 1;
  105. /*
  106. * We do not want to show the overhead
  107. * of the stack tracer stack in the
  108. * max stack. If we haven't figured
  109. * out what that is, then figure it out
  110. * now.
  111. */
  112. if (unlikely(!tracer_frame) && i == 1) {
  113. tracer_frame = (p - stack) *
  114. sizeof(unsigned long);
  115. max_stack_size -= tracer_frame;
  116. }
  117. }
  118. }
  119. if (!found)
  120. i++;
  121. }
  122. BUG_ON(current != &init_task &&
  123. *(end_of_stack(current)) != STACK_END_MAGIC);
  124. out:
  125. arch_spin_unlock(&max_stack_lock);
  126. local_irq_restore(flags);
  127. }
  128. static void
  129. stack_trace_call(unsigned long ip, unsigned long parent_ip,
  130. struct ftrace_ops *op, struct pt_regs *pt_regs)
  131. {
  132. unsigned long stack;
  133. int cpu;
  134. preempt_disable_notrace();
  135. cpu = raw_smp_processor_id();
  136. /* no atomic needed, we only modify this variable by this cpu */
  137. if (per_cpu(trace_active, cpu)++ != 0)
  138. goto out;
  139. /*
  140. * When fentry is used, the traced function does not get
  141. * its stack frame set up, and we lose the parent.
  142. * The ip is pretty useless because the function tracer
  143. * was called before that function set up its stack frame.
  144. * In this case, we use the parent ip.
  145. *
  146. * By adding the return address of either the parent ip
  147. * or the current ip we can disregard most of the stack usage
  148. * caused by the stack tracer itself.
  149. *
  150. * The function tracer always reports the address of where the
  151. * mcount call was, but the stack will hold the return address.
  152. */
  153. if (fentry)
  154. ip = parent_ip;
  155. else
  156. ip += MCOUNT_INSN_SIZE;
  157. check_stack(ip, &stack);
  158. out:
  159. per_cpu(trace_active, cpu)--;
  160. /* prevent recursion in schedule */
  161. preempt_enable_notrace();
  162. }
  163. static struct ftrace_ops trace_ops __read_mostly =
  164. {
  165. .func = stack_trace_call,
  166. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  167. };
  168. static ssize_t
  169. stack_max_size_read(struct file *filp, char __user *ubuf,
  170. size_t count, loff_t *ppos)
  171. {
  172. unsigned long *ptr = filp->private_data;
  173. char buf[64];
  174. int r;
  175. r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
  176. if (r > sizeof(buf))
  177. r = sizeof(buf);
  178. return simple_read_from_buffer(ubuf, count, ppos, buf, r);
  179. }
  180. static ssize_t
  181. stack_max_size_write(struct file *filp, const char __user *ubuf,
  182. size_t count, loff_t *ppos)
  183. {
  184. long *ptr = filp->private_data;
  185. unsigned long val, flags;
  186. int ret;
  187. int cpu;
  188. ret = kstrtoul_from_user(ubuf, count, 10, &val);
  189. if (ret)
  190. return ret;
  191. local_irq_save(flags);
  192. /*
  193. * In case we trace inside arch_spin_lock() or after (NMI),
  194. * we will cause circular lock, so we also need to increase
  195. * the percpu trace_active here.
  196. */
  197. cpu = smp_processor_id();
  198. per_cpu(trace_active, cpu)++;
  199. arch_spin_lock(&max_stack_lock);
  200. *ptr = val;
  201. arch_spin_unlock(&max_stack_lock);
  202. per_cpu(trace_active, cpu)--;
  203. local_irq_restore(flags);
  204. return count;
  205. }
  206. static const struct file_operations stack_max_size_fops = {
  207. .open = tracing_open_generic,
  208. .read = stack_max_size_read,
  209. .write = stack_max_size_write,
  210. .llseek = default_llseek,
  211. };
  212. static void *
  213. __next(struct seq_file *m, loff_t *pos)
  214. {
  215. long n = *pos - 1;
  216. if (n >= max_stack_trace.nr_entries || stack_dump_trace[n] == ULONG_MAX)
  217. return NULL;
  218. m->private = (void *)n;
  219. return &m->private;
  220. }
  221. static void *
  222. t_next(struct seq_file *m, void *v, loff_t *pos)
  223. {
  224. (*pos)++;
  225. return __next(m, pos);
  226. }
  227. static void *t_start(struct seq_file *m, loff_t *pos)
  228. {
  229. int cpu;
  230. local_irq_disable();
  231. cpu = smp_processor_id();
  232. per_cpu(trace_active, cpu)++;
  233. arch_spin_lock(&max_stack_lock);
  234. if (*pos == 0)
  235. return SEQ_START_TOKEN;
  236. return __next(m, pos);
  237. }
  238. static void t_stop(struct seq_file *m, void *p)
  239. {
  240. int cpu;
  241. arch_spin_unlock(&max_stack_lock);
  242. cpu = smp_processor_id();
  243. per_cpu(trace_active, cpu)--;
  244. local_irq_enable();
  245. }
  246. static int trace_lookup_stack(struct seq_file *m, long i)
  247. {
  248. unsigned long addr = stack_dump_trace[i];
  249. return seq_printf(m, "%pS\n", (void *)addr);
  250. }
  251. static void print_disabled(struct seq_file *m)
  252. {
  253. seq_puts(m, "#\n"
  254. "# Stack tracer disabled\n"
  255. "#\n"
  256. "# To enable the stack tracer, either add 'stacktrace' to the\n"
  257. "# kernel command line\n"
  258. "# or 'echo 1 > /proc/sys/kernel/stack_tracer_enabled'\n"
  259. "#\n");
  260. }
  261. static int t_show(struct seq_file *m, void *v)
  262. {
  263. long i;
  264. int size;
  265. if (v == SEQ_START_TOKEN) {
  266. seq_printf(m, " Depth Size Location"
  267. " (%d entries)\n"
  268. " ----- ---- --------\n",
  269. max_stack_trace.nr_entries - 1);
  270. if (!stack_tracer_enabled && !max_stack_size)
  271. print_disabled(m);
  272. return 0;
  273. }
  274. i = *(long *)v;
  275. if (i >= max_stack_trace.nr_entries ||
  276. stack_dump_trace[i] == ULONG_MAX)
  277. return 0;
  278. if (i+1 == max_stack_trace.nr_entries ||
  279. stack_dump_trace[i+1] == ULONG_MAX)
  280. size = stack_dump_index[i];
  281. else
  282. size = stack_dump_index[i] - stack_dump_index[i+1];
  283. seq_printf(m, "%3ld) %8d %5d ", i, stack_dump_index[i], size);
  284. trace_lookup_stack(m, i);
  285. return 0;
  286. }
  287. static const struct seq_operations stack_trace_seq_ops = {
  288. .start = t_start,
  289. .next = t_next,
  290. .stop = t_stop,
  291. .show = t_show,
  292. };
  293. static int stack_trace_open(struct inode *inode, struct file *file)
  294. {
  295. return seq_open(file, &stack_trace_seq_ops);
  296. }
  297. static const struct file_operations stack_trace_fops = {
  298. .open = stack_trace_open,
  299. .read = seq_read,
  300. .llseek = seq_lseek,
  301. .release = seq_release,
  302. };
  303. static int
  304. stack_trace_filter_open(struct inode *inode, struct file *file)
  305. {
  306. return ftrace_regex_open(&trace_ops, FTRACE_ITER_FILTER,
  307. inode, file);
  308. }
  309. static const struct file_operations stack_trace_filter_fops = {
  310. .open = stack_trace_filter_open,
  311. .read = seq_read,
  312. .write = ftrace_filter_write,
  313. .llseek = tracing_lseek,
  314. .release = ftrace_regex_release,
  315. };
  316. int
  317. stack_trace_sysctl(struct ctl_table *table, int write,
  318. void __user *buffer, size_t *lenp,
  319. loff_t *ppos)
  320. {
  321. int ret;
  322. mutex_lock(&stack_sysctl_mutex);
  323. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  324. if (ret || !write ||
  325. (last_stack_tracer_enabled == !!stack_tracer_enabled))
  326. goto out;
  327. last_stack_tracer_enabled = !!stack_tracer_enabled;
  328. if (stack_tracer_enabled)
  329. register_ftrace_function(&trace_ops);
  330. else
  331. unregister_ftrace_function(&trace_ops);
  332. out:
  333. mutex_unlock(&stack_sysctl_mutex);
  334. return ret;
  335. }
  336. static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata;
  337. static __init int enable_stacktrace(char *str)
  338. {
  339. if (strncmp(str, "_filter=", 8) == 0)
  340. strncpy(stack_trace_filter_buf, str+8, COMMAND_LINE_SIZE);
  341. stack_tracer_enabled = 1;
  342. last_stack_tracer_enabled = 1;
  343. return 1;
  344. }
  345. __setup("stacktrace", enable_stacktrace);
  346. static __init int stack_trace_init(void)
  347. {
  348. struct dentry *d_tracer;
  349. d_tracer = tracing_init_dentry();
  350. if (!d_tracer)
  351. return 0;
  352. trace_create_file("stack_max_size", 0644, d_tracer,
  353. &max_stack_size, &stack_max_size_fops);
  354. trace_create_file("stack_trace", 0444, d_tracer,
  355. NULL, &stack_trace_fops);
  356. trace_create_file("stack_trace_filter", 0444, d_tracer,
  357. NULL, &stack_trace_filter_fops);
  358. if (stack_trace_filter_buf[0])
  359. ftrace_set_early_filter(&trace_ops, stack_trace_filter_buf, 1);
  360. if (stack_tracer_enabled)
  361. register_ftrace_function(&trace_ops);
  362. return 0;
  363. }
  364. device_initcall(stack_trace_init);