trace_stack.c 7.7 KB

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