trace_stack.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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,
  91. struct ftrace_ops *op, struct pt_regs *pt_regs)
  92. {
  93. int cpu;
  94. if (unlikely(!ftrace_enabled || stack_trace_disabled))
  95. return;
  96. preempt_disable_notrace();
  97. cpu = raw_smp_processor_id();
  98. /* no atomic needed, we only modify this variable by this cpu */
  99. if (per_cpu(trace_active, cpu)++ != 0)
  100. goto out;
  101. check_stack();
  102. out:
  103. per_cpu(trace_active, cpu)--;
  104. /* prevent recursion in schedule */
  105. preempt_enable_notrace();
  106. }
  107. static struct ftrace_ops trace_ops __read_mostly =
  108. {
  109. .func = stack_trace_call,
  110. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  111. };
  112. static ssize_t
  113. stack_max_size_read(struct file *filp, char __user *ubuf,
  114. size_t count, loff_t *ppos)
  115. {
  116. unsigned long *ptr = filp->private_data;
  117. char buf[64];
  118. int r;
  119. r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
  120. if (r > sizeof(buf))
  121. r = sizeof(buf);
  122. return simple_read_from_buffer(ubuf, count, ppos, buf, r);
  123. }
  124. static ssize_t
  125. stack_max_size_write(struct file *filp, const char __user *ubuf,
  126. size_t count, loff_t *ppos)
  127. {
  128. long *ptr = filp->private_data;
  129. unsigned long val, flags;
  130. int ret;
  131. int cpu;
  132. ret = kstrtoul_from_user(ubuf, count, 10, &val);
  133. if (ret)
  134. return ret;
  135. local_irq_save(flags);
  136. /*
  137. * In case we trace inside arch_spin_lock() or after (NMI),
  138. * we will cause circular lock, so we also need to increase
  139. * the percpu trace_active here.
  140. */
  141. cpu = smp_processor_id();
  142. per_cpu(trace_active, cpu)++;
  143. arch_spin_lock(&max_stack_lock);
  144. *ptr = val;
  145. arch_spin_unlock(&max_stack_lock);
  146. per_cpu(trace_active, cpu)--;
  147. local_irq_restore(flags);
  148. return count;
  149. }
  150. static const struct file_operations stack_max_size_fops = {
  151. .open = tracing_open_generic,
  152. .read = stack_max_size_read,
  153. .write = stack_max_size_write,
  154. .llseek = default_llseek,
  155. };
  156. static void *
  157. __next(struct seq_file *m, loff_t *pos)
  158. {
  159. long n = *pos - 1;
  160. if (n >= max_stack_trace.nr_entries || stack_dump_trace[n] == ULONG_MAX)
  161. return NULL;
  162. m->private = (void *)n;
  163. return &m->private;
  164. }
  165. static void *
  166. t_next(struct seq_file *m, void *v, loff_t *pos)
  167. {
  168. (*pos)++;
  169. return __next(m, pos);
  170. }
  171. static void *t_start(struct seq_file *m, loff_t *pos)
  172. {
  173. int cpu;
  174. local_irq_disable();
  175. cpu = smp_processor_id();
  176. per_cpu(trace_active, cpu)++;
  177. arch_spin_lock(&max_stack_lock);
  178. if (*pos == 0)
  179. return SEQ_START_TOKEN;
  180. return __next(m, pos);
  181. }
  182. static void t_stop(struct seq_file *m, void *p)
  183. {
  184. int cpu;
  185. arch_spin_unlock(&max_stack_lock);
  186. cpu = smp_processor_id();
  187. per_cpu(trace_active, cpu)--;
  188. local_irq_enable();
  189. }
  190. static int trace_lookup_stack(struct seq_file *m, long i)
  191. {
  192. unsigned long addr = stack_dump_trace[i];
  193. return seq_printf(m, "%pS\n", (void *)addr);
  194. }
  195. static void print_disabled(struct seq_file *m)
  196. {
  197. seq_puts(m, "#\n"
  198. "# Stack tracer disabled\n"
  199. "#\n"
  200. "# To enable the stack tracer, either add 'stacktrace' to the\n"
  201. "# kernel command line\n"
  202. "# or 'echo 1 > /proc/sys/kernel/stack_tracer_enabled'\n"
  203. "#\n");
  204. }
  205. static int t_show(struct seq_file *m, void *v)
  206. {
  207. long i;
  208. int size;
  209. if (v == SEQ_START_TOKEN) {
  210. seq_printf(m, " Depth Size Location"
  211. " (%d entries)\n"
  212. " ----- ---- --------\n",
  213. max_stack_trace.nr_entries - 1);
  214. if (!stack_tracer_enabled && !max_stack_size)
  215. print_disabled(m);
  216. return 0;
  217. }
  218. i = *(long *)v;
  219. if (i >= max_stack_trace.nr_entries ||
  220. stack_dump_trace[i] == ULONG_MAX)
  221. return 0;
  222. if (i+1 == max_stack_trace.nr_entries ||
  223. stack_dump_trace[i+1] == ULONG_MAX)
  224. size = stack_dump_index[i];
  225. else
  226. size = stack_dump_index[i] - stack_dump_index[i+1];
  227. seq_printf(m, "%3ld) %8d %5d ", i, stack_dump_index[i], size);
  228. trace_lookup_stack(m, i);
  229. return 0;
  230. }
  231. static const struct seq_operations stack_trace_seq_ops = {
  232. .start = t_start,
  233. .next = t_next,
  234. .stop = t_stop,
  235. .show = t_show,
  236. };
  237. static int stack_trace_open(struct inode *inode, struct file *file)
  238. {
  239. return seq_open(file, &stack_trace_seq_ops);
  240. }
  241. static const struct file_operations stack_trace_fops = {
  242. .open = stack_trace_open,
  243. .read = seq_read,
  244. .llseek = seq_lseek,
  245. .release = seq_release,
  246. };
  247. static int
  248. stack_trace_filter_open(struct inode *inode, struct file *file)
  249. {
  250. return ftrace_regex_open(&trace_ops, FTRACE_ITER_FILTER,
  251. inode, file);
  252. }
  253. static const struct file_operations stack_trace_filter_fops = {
  254. .open = stack_trace_filter_open,
  255. .read = seq_read,
  256. .write = ftrace_filter_write,
  257. .llseek = ftrace_regex_lseek,
  258. .release = ftrace_regex_release,
  259. };
  260. int
  261. stack_trace_sysctl(struct ctl_table *table, int write,
  262. void __user *buffer, size_t *lenp,
  263. loff_t *ppos)
  264. {
  265. int ret;
  266. mutex_lock(&stack_sysctl_mutex);
  267. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  268. if (ret || !write ||
  269. (last_stack_tracer_enabled == !!stack_tracer_enabled))
  270. goto out;
  271. last_stack_tracer_enabled = !!stack_tracer_enabled;
  272. if (stack_tracer_enabled)
  273. register_ftrace_function(&trace_ops);
  274. else
  275. unregister_ftrace_function(&trace_ops);
  276. out:
  277. mutex_unlock(&stack_sysctl_mutex);
  278. return ret;
  279. }
  280. static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata;
  281. static __init int enable_stacktrace(char *str)
  282. {
  283. if (strncmp(str, "_filter=", 8) == 0)
  284. strncpy(stack_trace_filter_buf, str+8, COMMAND_LINE_SIZE);
  285. stack_tracer_enabled = 1;
  286. last_stack_tracer_enabled = 1;
  287. return 1;
  288. }
  289. __setup("stacktrace", enable_stacktrace);
  290. static __init int stack_trace_init(void)
  291. {
  292. struct dentry *d_tracer;
  293. d_tracer = tracing_init_dentry();
  294. trace_create_file("stack_max_size", 0644, d_tracer,
  295. &max_stack_size, &stack_max_size_fops);
  296. trace_create_file("stack_trace", 0444, d_tracer,
  297. NULL, &stack_trace_fops);
  298. trace_create_file("stack_trace_filter", 0444, d_tracer,
  299. NULL, &stack_trace_filter_fops);
  300. if (stack_trace_filter_buf[0])
  301. ftrace_set_early_filter(&trace_ops, stack_trace_filter_buf, 1);
  302. if (stack_tracer_enabled)
  303. register_ftrace_function(&trace_ops);
  304. return 0;
  305. }
  306. device_initcall(stack_trace_init);