trace_stack.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 raw_spinlock_t max_stack_lock =
  27. (raw_spinlock_t)__RAW_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. __raw_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. __raw_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, resched;
  92. if (unlikely(!ftrace_enabled || stack_trace_disabled))
  93. return;
  94. resched = ftrace_preempt_disable();
  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. ftrace_preempt_enable(resched);
  104. }
  105. static struct ftrace_ops trace_ops __read_mostly =
  106. {
  107. .func = stack_trace_call,
  108. };
  109. static ssize_t
  110. stack_max_size_read(struct file *filp, char __user *ubuf,
  111. size_t count, loff_t *ppos)
  112. {
  113. unsigned long *ptr = filp->private_data;
  114. char buf[64];
  115. int r;
  116. r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
  117. if (r > sizeof(buf))
  118. r = sizeof(buf);
  119. return simple_read_from_buffer(ubuf, count, ppos, buf, r);
  120. }
  121. static ssize_t
  122. stack_max_size_write(struct file *filp, const char __user *ubuf,
  123. size_t count, loff_t *ppos)
  124. {
  125. long *ptr = filp->private_data;
  126. unsigned long val, flags;
  127. char buf[64];
  128. int ret;
  129. if (count >= sizeof(buf))
  130. return -EINVAL;
  131. if (copy_from_user(&buf, ubuf, count))
  132. return -EFAULT;
  133. buf[count] = 0;
  134. ret = strict_strtoul(buf, 10, &val);
  135. if (ret < 0)
  136. return ret;
  137. local_irq_save(flags);
  138. __raw_spin_lock(&max_stack_lock);
  139. *ptr = val;
  140. __raw_spin_unlock(&max_stack_lock);
  141. local_irq_restore(flags);
  142. return count;
  143. }
  144. static const struct file_operations stack_max_size_fops = {
  145. .open = tracing_open_generic,
  146. .read = stack_max_size_read,
  147. .write = stack_max_size_write,
  148. };
  149. static void *
  150. t_next(struct seq_file *m, void *v, loff_t *pos)
  151. {
  152. long i;
  153. (*pos)++;
  154. if (v == SEQ_START_TOKEN)
  155. i = 0;
  156. else {
  157. i = *(long *)v;
  158. i++;
  159. }
  160. if (i >= max_stack_trace.nr_entries ||
  161. stack_dump_trace[i] == ULONG_MAX)
  162. return NULL;
  163. m->private = (void *)i;
  164. return &m->private;
  165. }
  166. static void *t_start(struct seq_file *m, loff_t *pos)
  167. {
  168. void *t = SEQ_START_TOKEN;
  169. loff_t l = 0;
  170. local_irq_disable();
  171. __raw_spin_lock(&max_stack_lock);
  172. if (*pos == 0)
  173. return SEQ_START_TOKEN;
  174. for (; t && l < *pos; t = t_next(m, t, &l))
  175. ;
  176. return t;
  177. }
  178. static void t_stop(struct seq_file *m, void *p)
  179. {
  180. __raw_spin_unlock(&max_stack_lock);
  181. local_irq_enable();
  182. }
  183. static int trace_lookup_stack(struct seq_file *m, long i)
  184. {
  185. unsigned long addr = stack_dump_trace[i];
  186. #ifdef CONFIG_KALLSYMS
  187. char str[KSYM_SYMBOL_LEN];
  188. sprint_symbol(str, addr);
  189. return seq_printf(m, "%s\n", str);
  190. #else
  191. return seq_printf(m, "%p\n", (void*)addr);
  192. #endif
  193. }
  194. static void print_disabled(struct seq_file *m)
  195. {
  196. seq_puts(m, "#\n"
  197. "# Stack tracer disabled\n"
  198. "#\n"
  199. "# To enable the stack tracer, either add 'stacktrace' to the\n"
  200. "# kernel command line\n"
  201. "# or 'echo 1 > /proc/sys/kernel/stack_tracer_enabled'\n"
  202. "#\n");
  203. }
  204. static int t_show(struct seq_file *m, void *v)
  205. {
  206. long i;
  207. int size;
  208. if (v == SEQ_START_TOKEN) {
  209. seq_printf(m, " Depth Size Location"
  210. " (%d entries)\n"
  211. " ----- ---- --------\n",
  212. max_stack_trace.nr_entries - 1);
  213. if (!stack_tracer_enabled && !max_stack_size)
  214. print_disabled(m);
  215. return 0;
  216. }
  217. i = *(long *)v;
  218. if (i >= max_stack_trace.nr_entries ||
  219. stack_dump_trace[i] == ULONG_MAX)
  220. return 0;
  221. if (i+1 == max_stack_trace.nr_entries ||
  222. stack_dump_trace[i+1] == ULONG_MAX)
  223. size = stack_dump_index[i];
  224. else
  225. size = stack_dump_index[i] - stack_dump_index[i+1];
  226. seq_printf(m, "%3ld) %8d %5d ", i, stack_dump_index[i], size);
  227. trace_lookup_stack(m, i);
  228. return 0;
  229. }
  230. static const struct seq_operations stack_trace_seq_ops = {
  231. .start = t_start,
  232. .next = t_next,
  233. .stop = t_stop,
  234. .show = t_show,
  235. };
  236. static int stack_trace_open(struct inode *inode, struct file *file)
  237. {
  238. int ret;
  239. ret = seq_open(file, &stack_trace_seq_ops);
  240. return ret;
  241. }
  242. static const struct file_operations stack_trace_fops = {
  243. .open = stack_trace_open,
  244. .read = seq_read,
  245. .llseek = seq_lseek,
  246. };
  247. int
  248. stack_trace_sysctl(struct ctl_table *table, int write,
  249. struct file *file, void __user *buffer, size_t *lenp,
  250. loff_t *ppos)
  251. {
  252. int ret;
  253. mutex_lock(&stack_sysctl_mutex);
  254. ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
  255. if (ret || !write ||
  256. (last_stack_tracer_enabled == !!stack_tracer_enabled))
  257. goto out;
  258. last_stack_tracer_enabled = !!stack_tracer_enabled;
  259. if (stack_tracer_enabled)
  260. register_ftrace_function(&trace_ops);
  261. else
  262. unregister_ftrace_function(&trace_ops);
  263. out:
  264. mutex_unlock(&stack_sysctl_mutex);
  265. return ret;
  266. }
  267. static __init int enable_stacktrace(char *str)
  268. {
  269. stack_tracer_enabled = 1;
  270. last_stack_tracer_enabled = 1;
  271. return 1;
  272. }
  273. __setup("stacktrace", enable_stacktrace);
  274. static __init int stack_trace_init(void)
  275. {
  276. struct dentry *d_tracer;
  277. d_tracer = tracing_init_dentry();
  278. trace_create_file("stack_max_size", 0644, d_tracer,
  279. &max_stack_size, &stack_max_size_fops);
  280. trace_create_file("stack_trace", 0444, d_tracer,
  281. NULL, &stack_trace_fops);
  282. if (stack_tracer_enabled)
  283. register_ftrace_function(&trace_ops);
  284. return 0;
  285. }
  286. device_initcall(stack_trace_init);