trace_stack.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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] =
  18. { [0 ... (STACK_TRACE_ENTRIES-1)] = ULONG_MAX };
  19. static struct stack_trace max_stack_trace = {
  20. .max_entries = STACK_TRACE_ENTRIES,
  21. .entries = stack_dump_trace,
  22. };
  23. static unsigned long max_stack_size;
  24. static raw_spinlock_t max_stack_lock =
  25. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  26. static int stack_trace_disabled __read_mostly;
  27. static DEFINE_PER_CPU(int, trace_active);
  28. static inline void check_stack(void)
  29. {
  30. unsigned long this_size;
  31. unsigned long flags;
  32. this_size = ((unsigned long)&this_size) & (THREAD_SIZE-1);
  33. this_size = THREAD_SIZE - this_size;
  34. if (this_size <= max_stack_size)
  35. return;
  36. raw_local_irq_save(flags);
  37. __raw_spin_lock(&max_stack_lock);
  38. /* a race could have already updated it */
  39. if (this_size <= max_stack_size)
  40. goto out;
  41. max_stack_size = this_size;
  42. max_stack_trace.nr_entries = 0;
  43. max_stack_trace.skip = 1;
  44. save_stack_trace(&max_stack_trace);
  45. out:
  46. __raw_spin_unlock(&max_stack_lock);
  47. raw_local_irq_restore(flags);
  48. }
  49. static void
  50. stack_trace_call(unsigned long ip, unsigned long parent_ip)
  51. {
  52. int cpu, resched;
  53. if (unlikely(!ftrace_enabled || stack_trace_disabled))
  54. return;
  55. resched = need_resched();
  56. preempt_disable_notrace();
  57. cpu = raw_smp_processor_id();
  58. /* no atomic needed, we only modify this variable by this cpu */
  59. if (per_cpu(trace_active, cpu)++ != 0)
  60. goto out;
  61. check_stack();
  62. out:
  63. per_cpu(trace_active, cpu)--;
  64. /* prevent recursion in schedule */
  65. if (resched)
  66. preempt_enable_no_resched_notrace();
  67. else
  68. preempt_enable_notrace();
  69. }
  70. static struct ftrace_ops trace_ops __read_mostly =
  71. {
  72. .func = stack_trace_call,
  73. };
  74. static ssize_t
  75. stack_max_size_read(struct file *filp, char __user *ubuf,
  76. size_t count, loff_t *ppos)
  77. {
  78. unsigned long *ptr = filp->private_data;
  79. char buf[64];
  80. int r;
  81. r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
  82. if (r > sizeof(buf))
  83. r = sizeof(buf);
  84. return simple_read_from_buffer(ubuf, count, ppos, buf, r);
  85. }
  86. static ssize_t
  87. stack_max_size_write(struct file *filp, const char __user *ubuf,
  88. size_t count, loff_t *ppos)
  89. {
  90. long *ptr = filp->private_data;
  91. unsigned long val, flags;
  92. char buf[64];
  93. int ret;
  94. if (count >= sizeof(buf))
  95. return -EINVAL;
  96. if (copy_from_user(&buf, ubuf, count))
  97. return -EFAULT;
  98. buf[count] = 0;
  99. ret = strict_strtoul(buf, 10, &val);
  100. if (ret < 0)
  101. return ret;
  102. raw_local_irq_save(flags);
  103. __raw_spin_lock(&max_stack_lock);
  104. *ptr = val;
  105. __raw_spin_unlock(&max_stack_lock);
  106. raw_local_irq_restore(flags);
  107. return count;
  108. }
  109. static struct file_operations stack_max_size_fops = {
  110. .open = tracing_open_generic,
  111. .read = stack_max_size_read,
  112. .write = stack_max_size_write,
  113. };
  114. static void *
  115. t_next(struct seq_file *m, void *v, loff_t *pos)
  116. {
  117. unsigned long *t = m->private;
  118. (*pos)++;
  119. if (!t || *t == ULONG_MAX)
  120. return NULL;
  121. t++;
  122. m->private = t;
  123. return t;
  124. }
  125. static void *t_start(struct seq_file *m, loff_t *pos)
  126. {
  127. unsigned long *t = m->private;
  128. loff_t l = 0;
  129. local_irq_disable();
  130. __raw_spin_lock(&max_stack_lock);
  131. for (; t && l < *pos; t = t_next(m, t, &l))
  132. ;
  133. return t;
  134. }
  135. static void t_stop(struct seq_file *m, void *p)
  136. {
  137. __raw_spin_unlock(&max_stack_lock);
  138. local_irq_enable();
  139. }
  140. static int trace_lookup_stack(struct seq_file *m, unsigned long addr)
  141. {
  142. #ifdef CONFIG_KALLSYMS
  143. char str[KSYM_SYMBOL_LEN];
  144. sprint_symbol(str, addr);
  145. return seq_printf(m, "[<%p>] %s\n", (void*)addr, str);
  146. #else
  147. return seq_printf(m, "%p\n", (void*)addr);
  148. #endif
  149. }
  150. static int t_show(struct seq_file *m, void *v)
  151. {
  152. unsigned long *t = v;
  153. if (!t || *t == ULONG_MAX)
  154. return 0;
  155. trace_lookup_stack(m, *t);
  156. return 0;
  157. }
  158. static struct seq_operations stack_trace_seq_ops = {
  159. .start = t_start,
  160. .next = t_next,
  161. .stop = t_stop,
  162. .show = t_show,
  163. };
  164. static int stack_trace_open(struct inode *inode, struct file *file)
  165. {
  166. int ret;
  167. ret = seq_open(file, &stack_trace_seq_ops);
  168. if (!ret) {
  169. struct seq_file *m = file->private_data;
  170. m->private = stack_dump_trace;
  171. }
  172. return ret;
  173. }
  174. static struct file_operations stack_trace_fops = {
  175. .open = stack_trace_open,
  176. .read = seq_read,
  177. .llseek = seq_lseek,
  178. };
  179. static __init int stack_trace_init(void)
  180. {
  181. struct dentry *d_tracer;
  182. struct dentry *entry;
  183. d_tracer = tracing_init_dentry();
  184. entry = debugfs_create_file("stack_max_size", 0644, d_tracer,
  185. &max_stack_size, &stack_max_size_fops);
  186. if (!entry)
  187. pr_warning("Could not create debugfs 'stack_max_size' entry\n");
  188. entry = debugfs_create_file("stack_trace", 0444, d_tracer,
  189. NULL, &stack_trace_fops);
  190. if (!entry)
  191. pr_warning("Could not create debugfs 'stack_trace' entry\n");
  192. register_ftrace_function(&trace_ops);
  193. return 0;
  194. }
  195. device_initcall(stack_trace_init);