trace_stack.c 9.1 KB

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