trace_stack.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/init.h>
  14. #include <linux/fs.h>
  15. #include "trace.h"
  16. #define STACK_TRACE_ENTRIES 500
  17. static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
  18. { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
  19. static unsigned stack_dump_index[STACK_TRACE_ENTRIES];
  20. static struct stack_trace max_stack_trace = {
  21. .max_entries = STACK_TRACE_ENTRIES,
  22. .entries = stack_dump_trace,
  23. };
  24. static unsigned long max_stack_size;
  25. static raw_spinlock_t max_stack_lock =
  26. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  27. static int stack_trace_disabled __read_mostly;
  28. static DEFINE_PER_CPU(int, trace_active);
  29. static inline void check_stack(void)
  30. {
  31. unsigned long this_size, flags;
  32. unsigned long *p, *top, *start;
  33. int i;
  34. this_size = ((unsigned long)&this_size) & (THREAD_SIZE-1);
  35. this_size = THREAD_SIZE - this_size;
  36. if (this_size <= max_stack_size)
  37. return;
  38. /* we do not handle interrupt stacks yet */
  39. if (!object_is_on_stack(&this_size))
  40. return;
  41. raw_local_irq_save(flags);
  42. __raw_spin_lock(&max_stack_lock);
  43. /* a race could have already updated it */
  44. if (this_size <= max_stack_size)
  45. goto out;
  46. max_stack_size = this_size;
  47. max_stack_trace.nr_entries = 0;
  48. max_stack_trace.skip = 3;
  49. save_stack_trace(&max_stack_trace);
  50. /*
  51. * Now find where in the stack these are.
  52. */
  53. i = 0;
  54. start = &this_size;
  55. top = (unsigned long *)
  56. (((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE);
  57. /*
  58. * Loop through all the entries. One of the entries may
  59. * for some reason be missed on the stack, so we may
  60. * have to account for them. If they are all there, this
  61. * loop will only happen once. This code only takes place
  62. * on a new max, so it is far from a fast path.
  63. */
  64. while (i < max_stack_trace.nr_entries) {
  65. stack_dump_index[i] = this_size;
  66. p = start;
  67. for (; p < top && i < max_stack_trace.nr_entries; p++) {
  68. if (*p == stack_dump_trace[i]) {
  69. this_size = stack_dump_index[i++] =
  70. (top - p) * sizeof(unsigned long);
  71. /* Start the search from here */
  72. start = p + 1;
  73. }
  74. }
  75. i++;
  76. }
  77. out:
  78. __raw_spin_unlock(&max_stack_lock);
  79. raw_local_irq_restore(flags);
  80. }
  81. static void
  82. stack_trace_call(unsigned long ip, unsigned long parent_ip)
  83. {
  84. int cpu, resched;
  85. if (unlikely(!ftrace_enabled || stack_trace_disabled))
  86. return;
  87. resched = need_resched();
  88. preempt_disable_notrace();
  89. cpu = raw_smp_processor_id();
  90. /* no atomic needed, we only modify this variable by this cpu */
  91. if (per_cpu(trace_active, cpu)++ != 0)
  92. goto out;
  93. check_stack();
  94. out:
  95. per_cpu(trace_active, cpu)--;
  96. /* prevent recursion in schedule */
  97. if (resched)
  98. preempt_enable_no_resched_notrace();
  99. else
  100. preempt_enable_notrace();
  101. }
  102. static struct ftrace_ops trace_ops __read_mostly =
  103. {
  104. .func = stack_trace_call,
  105. };
  106. static ssize_t
  107. stack_max_size_read(struct file *filp, char __user *ubuf,
  108. size_t count, loff_t *ppos)
  109. {
  110. unsigned long *ptr = filp->private_data;
  111. char buf[64];
  112. int r;
  113. r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
  114. if (r > sizeof(buf))
  115. r = sizeof(buf);
  116. return simple_read_from_buffer(ubuf, count, ppos, buf, r);
  117. }
  118. static ssize_t
  119. stack_max_size_write(struct file *filp, const char __user *ubuf,
  120. size_t count, loff_t *ppos)
  121. {
  122. long *ptr = filp->private_data;
  123. unsigned long val, flags;
  124. char buf[64];
  125. int ret;
  126. if (count >= sizeof(buf))
  127. return -EINVAL;
  128. if (copy_from_user(&buf, ubuf, count))
  129. return -EFAULT;
  130. buf[count] = 0;
  131. ret = strict_strtoul(buf, 10, &val);
  132. if (ret < 0)
  133. return ret;
  134. raw_local_irq_save(flags);
  135. __raw_spin_lock(&max_stack_lock);
  136. *ptr = val;
  137. __raw_spin_unlock(&max_stack_lock);
  138. raw_local_irq_restore(flags);
  139. return count;
  140. }
  141. static struct file_operations stack_max_size_fops = {
  142. .open = tracing_open_generic,
  143. .read = stack_max_size_read,
  144. .write = stack_max_size_write,
  145. };
  146. static void *
  147. t_next(struct seq_file *m, void *v, loff_t *pos)
  148. {
  149. long i;
  150. (*pos)++;
  151. if (v == SEQ_START_TOKEN)
  152. i = 0;
  153. else {
  154. i = *(long *)v;
  155. i++;
  156. }
  157. if (i >= max_stack_trace.nr_entries ||
  158. stack_dump_trace[i] == ULONG_MAX)
  159. return NULL;
  160. m->private = (void *)i;
  161. return &m->private;
  162. }
  163. static void *t_start(struct seq_file *m, loff_t *pos)
  164. {
  165. void *t = SEQ_START_TOKEN;
  166. loff_t l = 0;
  167. local_irq_disable();
  168. __raw_spin_lock(&max_stack_lock);
  169. if (*pos == 0)
  170. return SEQ_START_TOKEN;
  171. for (; t && l < *pos; t = t_next(m, t, &l))
  172. ;
  173. return t;
  174. }
  175. static void t_stop(struct seq_file *m, void *p)
  176. {
  177. __raw_spin_unlock(&max_stack_lock);
  178. local_irq_enable();
  179. }
  180. static int trace_lookup_stack(struct seq_file *m, long i)
  181. {
  182. unsigned long addr = stack_dump_trace[i];
  183. #ifdef CONFIG_KALLSYMS
  184. char str[KSYM_SYMBOL_LEN];
  185. sprint_symbol(str, addr);
  186. return seq_printf(m, "%s\n", str);
  187. #else
  188. return seq_printf(m, "%p\n", (void*)addr);
  189. #endif
  190. }
  191. static int t_show(struct seq_file *m, void *v)
  192. {
  193. long i;
  194. int size;
  195. if (v == SEQ_START_TOKEN) {
  196. seq_printf(m, " Depth Size Location"
  197. " (%d entries)\n"
  198. " ----- ---- --------\n",
  199. max_stack_trace.nr_entries);
  200. return 0;
  201. }
  202. i = *(long *)v;
  203. if (i >= max_stack_trace.nr_entries ||
  204. stack_dump_trace[i] == ULONG_MAX)
  205. return 0;
  206. if (i+1 == max_stack_trace.nr_entries ||
  207. stack_dump_trace[i+1] == ULONG_MAX)
  208. size = stack_dump_index[i];
  209. else
  210. size = stack_dump_index[i] - stack_dump_index[i+1];
  211. seq_printf(m, "%3ld) %8d %5d ", i, stack_dump_index[i], size);
  212. trace_lookup_stack(m, i);
  213. return 0;
  214. }
  215. static struct seq_operations stack_trace_seq_ops = {
  216. .start = t_start,
  217. .next = t_next,
  218. .stop = t_stop,
  219. .show = t_show,
  220. };
  221. static int stack_trace_open(struct inode *inode, struct file *file)
  222. {
  223. int ret;
  224. ret = seq_open(file, &stack_trace_seq_ops);
  225. return ret;
  226. }
  227. static struct file_operations stack_trace_fops = {
  228. .open = stack_trace_open,
  229. .read = seq_read,
  230. .llseek = seq_lseek,
  231. };
  232. static __init int stack_trace_init(void)
  233. {
  234. struct dentry *d_tracer;
  235. struct dentry *entry;
  236. d_tracer = tracing_init_dentry();
  237. entry = debugfs_create_file("stack_max_size", 0644, d_tracer,
  238. &max_stack_size, &stack_max_size_fops);
  239. if (!entry)
  240. pr_warning("Could not create debugfs 'stack_max_size' entry\n");
  241. entry = debugfs_create_file("stack_trace", 0444, d_tracer,
  242. NULL, &stack_trace_fops);
  243. if (!entry)
  244. pr_warning("Could not create debugfs 'stack_trace' entry\n");
  245. register_ftrace_function(&trace_ops);
  246. return 0;
  247. }
  248. device_initcall(stack_trace_init);