trace_stack.c 10 KB

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