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 <asm/setup.h>
  17. #include "trace.h"
  18. #define STACK_TRACE_ENTRIES 500
  19. #ifdef CC_USING_FENTRY
  20. # define fentry 1
  21. #else
  22. # define fentry 0
  23. #endif
  24. static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
  25. { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
  26. static unsigned stack_dump_index[STACK_TRACE_ENTRIES];
  27. /*
  28. * Reserve one entry for the passed in ip. This will allow
  29. * us to remove most or all of the stack size overhead
  30. * added by the stack tracer itself.
  31. */
  32. static struct stack_trace max_stack_trace = {
  33. .max_entries = STACK_TRACE_ENTRIES - 1,
  34. .entries = &stack_dump_trace[1],
  35. };
  36. static unsigned long max_stack_size;
  37. static arch_spinlock_t max_stack_lock =
  38. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  39. static int stack_trace_disabled __read_mostly;
  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. out:
  123. arch_spin_unlock(&max_stack_lock);
  124. local_irq_restore(flags);
  125. }
  126. static void
  127. stack_trace_call(unsigned long ip, unsigned long parent_ip)
  128. {
  129. unsigned long stack;
  130. int cpu;
  131. if (unlikely(!ftrace_enabled || stack_trace_disabled))
  132. return;
  133. preempt_disable_notrace();
  134. cpu = raw_smp_processor_id();
  135. /* no atomic needed, we only modify this variable by this cpu */
  136. if (per_cpu(trace_active, cpu)++ != 0)
  137. goto out;
  138. /*
  139. * When fentry is used, the traced function does not get
  140. * its stack frame set up, and we lose the parent.
  141. * The ip is pretty useless because the function tracer
  142. * was called before that function set up its stack frame.
  143. * In this case, we use the parent ip.
  144. *
  145. * By adding the return address of either the parent ip
  146. * or the current ip we can disregard most of the stack usage
  147. * caused by the stack tracer itself.
  148. *
  149. * The function tracer always reports the address of where the
  150. * mcount call was, but the stack will hold the return address.
  151. */
  152. if (fentry)
  153. ip = parent_ip;
  154. else
  155. ip += MCOUNT_INSN_SIZE;
  156. check_stack(ip, &stack);
  157. out:
  158. per_cpu(trace_active, cpu)--;
  159. /* prevent recursion in schedule */
  160. preempt_enable_notrace();
  161. }
  162. static struct ftrace_ops trace_ops __read_mostly =
  163. {
  164. .func = stack_trace_call,
  165. };
  166. static ssize_t
  167. stack_max_size_read(struct file *filp, char __user *ubuf,
  168. size_t count, loff_t *ppos)
  169. {
  170. unsigned long *ptr = filp->private_data;
  171. char buf[64];
  172. int r;
  173. r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
  174. if (r > sizeof(buf))
  175. r = sizeof(buf);
  176. return simple_read_from_buffer(ubuf, count, ppos, buf, r);
  177. }
  178. static ssize_t
  179. stack_max_size_write(struct file *filp, const char __user *ubuf,
  180. size_t count, loff_t *ppos)
  181. {
  182. long *ptr = filp->private_data;
  183. unsigned long val, flags;
  184. int ret;
  185. int cpu;
  186. ret = kstrtoul_from_user(ubuf, count, 10, &val);
  187. if (ret)
  188. return ret;
  189. local_irq_save(flags);
  190. /*
  191. * In case we trace inside arch_spin_lock() or after (NMI),
  192. * we will cause circular lock, so we also need to increase
  193. * the percpu trace_active here.
  194. */
  195. cpu = smp_processor_id();
  196. per_cpu(trace_active, cpu)++;
  197. arch_spin_lock(&max_stack_lock);
  198. *ptr = val;
  199. arch_spin_unlock(&max_stack_lock);
  200. per_cpu(trace_active, cpu)--;
  201. local_irq_restore(flags);
  202. return count;
  203. }
  204. static const struct file_operations stack_max_size_fops = {
  205. .open = tracing_open_generic,
  206. .read = stack_max_size_read,
  207. .write = stack_max_size_write,
  208. .llseek = default_llseek,
  209. };
  210. static void *
  211. __next(struct seq_file *m, loff_t *pos)
  212. {
  213. long n = *pos - 1;
  214. if (n >= max_stack_trace.nr_entries || stack_dump_trace[n] == ULONG_MAX)
  215. return NULL;
  216. m->private = (void *)n;
  217. return &m->private;
  218. }
  219. static void *
  220. t_next(struct seq_file *m, void *v, loff_t *pos)
  221. {
  222. (*pos)++;
  223. return __next(m, pos);
  224. }
  225. static void *t_start(struct seq_file *m, loff_t *pos)
  226. {
  227. int cpu;
  228. local_irq_disable();
  229. cpu = smp_processor_id();
  230. per_cpu(trace_active, cpu)++;
  231. arch_spin_lock(&max_stack_lock);
  232. if (*pos == 0)
  233. return SEQ_START_TOKEN;
  234. return __next(m, pos);
  235. }
  236. static void t_stop(struct seq_file *m, void *p)
  237. {
  238. int cpu;
  239. arch_spin_unlock(&max_stack_lock);
  240. cpu = smp_processor_id();
  241. per_cpu(trace_active, cpu)--;
  242. local_irq_enable();
  243. }
  244. static int trace_lookup_stack(struct seq_file *m, long i)
  245. {
  246. unsigned long addr = stack_dump_trace[i];
  247. return seq_printf(m, "%pS\n", (void *)addr);
  248. }
  249. static void print_disabled(struct seq_file *m)
  250. {
  251. seq_puts(m, "#\n"
  252. "# Stack tracer disabled\n"
  253. "#\n"
  254. "# To enable the stack tracer, either add 'stacktrace' to the\n"
  255. "# kernel command line\n"
  256. "# or 'echo 1 > /proc/sys/kernel/stack_tracer_enabled'\n"
  257. "#\n");
  258. }
  259. static int t_show(struct seq_file *m, void *v)
  260. {
  261. long i;
  262. int size;
  263. if (v == SEQ_START_TOKEN) {
  264. seq_printf(m, " Depth Size Location"
  265. " (%d entries)\n"
  266. " ----- ---- --------\n",
  267. max_stack_trace.nr_entries - 1);
  268. if (!stack_tracer_enabled && !max_stack_size)
  269. print_disabled(m);
  270. return 0;
  271. }
  272. i = *(long *)v;
  273. if (i >= max_stack_trace.nr_entries ||
  274. stack_dump_trace[i] == ULONG_MAX)
  275. return 0;
  276. if (i+1 == max_stack_trace.nr_entries ||
  277. stack_dump_trace[i+1] == ULONG_MAX)
  278. size = stack_dump_index[i];
  279. else
  280. size = stack_dump_index[i] - stack_dump_index[i+1];
  281. seq_printf(m, "%3ld) %8d %5d ", i, stack_dump_index[i], size);
  282. trace_lookup_stack(m, i);
  283. return 0;
  284. }
  285. static const struct seq_operations stack_trace_seq_ops = {
  286. .start = t_start,
  287. .next = t_next,
  288. .stop = t_stop,
  289. .show = t_show,
  290. };
  291. static int stack_trace_open(struct inode *inode, struct file *file)
  292. {
  293. return seq_open(file, &stack_trace_seq_ops);
  294. }
  295. static const struct file_operations stack_trace_fops = {
  296. .open = stack_trace_open,
  297. .read = seq_read,
  298. .llseek = seq_lseek,
  299. .release = seq_release,
  300. };
  301. static int
  302. stack_trace_filter_open(struct inode *inode, struct file *file)
  303. {
  304. return ftrace_regex_open(&trace_ops, FTRACE_ITER_FILTER,
  305. inode, file);
  306. }
  307. static const struct file_operations stack_trace_filter_fops = {
  308. .open = stack_trace_filter_open,
  309. .read = seq_read,
  310. .write = ftrace_filter_write,
  311. .llseek = ftrace_filter_lseek,
  312. .release = ftrace_regex_release,
  313. };
  314. int
  315. stack_trace_sysctl(struct ctl_table *table, int write,
  316. void __user *buffer, size_t *lenp,
  317. loff_t *ppos)
  318. {
  319. int ret;
  320. mutex_lock(&stack_sysctl_mutex);
  321. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  322. if (ret || !write ||
  323. (last_stack_tracer_enabled == !!stack_tracer_enabled))
  324. goto out;
  325. last_stack_tracer_enabled = !!stack_tracer_enabled;
  326. if (stack_tracer_enabled)
  327. register_ftrace_function(&trace_ops);
  328. else
  329. unregister_ftrace_function(&trace_ops);
  330. out:
  331. mutex_unlock(&stack_sysctl_mutex);
  332. return ret;
  333. }
  334. static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata;
  335. static __init int enable_stacktrace(char *str)
  336. {
  337. if (strncmp(str, "_filter=", 8) == 0)
  338. strncpy(stack_trace_filter_buf, str+8, COMMAND_LINE_SIZE);
  339. stack_tracer_enabled = 1;
  340. last_stack_tracer_enabled = 1;
  341. return 1;
  342. }
  343. __setup("stacktrace", enable_stacktrace);
  344. static __init int stack_trace_init(void)
  345. {
  346. struct dentry *d_tracer;
  347. d_tracer = tracing_init_dentry();
  348. if (!d_tracer)
  349. return 0;
  350. trace_create_file("stack_max_size", 0644, d_tracer,
  351. &max_stack_size, &stack_max_size_fops);
  352. trace_create_file("stack_trace", 0444, d_tracer,
  353. NULL, &stack_trace_fops);
  354. trace_create_file("stack_trace_filter", 0444, d_tracer,
  355. NULL, &stack_trace_filter_fops);
  356. if (stack_trace_filter_buf[0])
  357. ftrace_set_early_filter(&trace_ops, stack_trace_filter_buf, 1);
  358. if (stack_tracer_enabled)
  359. register_ftrace_function(&trace_ops);
  360. return 0;
  361. }
  362. device_initcall(stack_trace_init);