trace_stack.c 8.3 KB

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