trace_sysprof.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * trace stack traces
  3. *
  4. * Copyright (C) 2004-2008, Soeren Sandmann
  5. * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
  6. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  7. */
  8. #include <linux/kallsyms.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/hrtimer.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/ftrace.h>
  13. #include <linux/module.h>
  14. #include <linux/irq.h>
  15. #include <linux/fs.h>
  16. #include <asm/stacktrace.h>
  17. #include "trace.h"
  18. static struct trace_array *sysprof_trace;
  19. static int __read_mostly tracer_enabled;
  20. /*
  21. * 1 msec sample interval by default:
  22. */
  23. static unsigned long sample_period = 1000000;
  24. static const unsigned int sample_max_depth = 512;
  25. static DEFINE_MUTEX(sample_timer_lock);
  26. /*
  27. * Per CPU hrtimers that do the profiling:
  28. */
  29. static DEFINE_PER_CPU(struct hrtimer, stack_trace_hrtimer);
  30. struct stack_frame {
  31. const void __user *next_fp;
  32. unsigned long return_address;
  33. };
  34. static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
  35. {
  36. int ret;
  37. if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
  38. return 0;
  39. ret = 1;
  40. pagefault_disable();
  41. if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
  42. ret = 0;
  43. pagefault_enable();
  44. return ret;
  45. }
  46. struct backtrace_info {
  47. struct trace_array_cpu *data;
  48. struct trace_array *tr;
  49. int pos;
  50. };
  51. static void
  52. backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
  53. {
  54. /* Ignore warnings */
  55. }
  56. static void backtrace_warning(void *data, char *msg)
  57. {
  58. /* Ignore warnings */
  59. }
  60. static int backtrace_stack(void *data, char *name)
  61. {
  62. /* Don't bother with IRQ stacks for now */
  63. return -1;
  64. }
  65. static void backtrace_address(void *data, unsigned long addr, int reliable)
  66. {
  67. struct backtrace_info *info = data;
  68. if (info->pos < sample_max_depth && reliable) {
  69. __trace_special(info->tr, info->data, 1, addr, 0);
  70. info->pos++;
  71. }
  72. }
  73. static const struct stacktrace_ops backtrace_ops = {
  74. .warning = backtrace_warning,
  75. .warning_symbol = backtrace_warning_symbol,
  76. .stack = backtrace_stack,
  77. .address = backtrace_address,
  78. };
  79. static int
  80. trace_kernel(struct pt_regs *regs, struct trace_array *tr,
  81. struct trace_array_cpu *data)
  82. {
  83. struct backtrace_info info;
  84. unsigned long bp;
  85. char *stack;
  86. info.tr = tr;
  87. info.data = data;
  88. info.pos = 1;
  89. __trace_special(info.tr, info.data, 1, regs->ip, 0);
  90. stack = ((char *)regs + sizeof(struct pt_regs));
  91. #ifdef CONFIG_FRAME_POINTER
  92. bp = regs->bp;
  93. #else
  94. bp = 0;
  95. #endif
  96. dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, &info);
  97. return info.pos;
  98. }
  99. static void timer_notify(struct pt_regs *regs, int cpu)
  100. {
  101. struct trace_array_cpu *data;
  102. struct stack_frame frame;
  103. struct trace_array *tr;
  104. const void __user *fp;
  105. int is_user;
  106. int i;
  107. if (!regs)
  108. return;
  109. tr = sysprof_trace;
  110. data = tr->data[cpu];
  111. is_user = user_mode(regs);
  112. if (!current || current->pid == 0)
  113. return;
  114. if (is_user && current->state != TASK_RUNNING)
  115. return;
  116. __trace_special(tr, data, 0, 0, current->pid);
  117. if (!is_user)
  118. i = trace_kernel(regs, tr, data);
  119. else
  120. i = 0;
  121. /*
  122. * Trace user stack if we are not a kernel thread
  123. */
  124. if (current->mm && i < sample_max_depth) {
  125. regs = (struct pt_regs *)current->thread.sp0 - 1;
  126. fp = (void __user *)regs->bp;
  127. __trace_special(tr, data, 2, regs->ip, 0);
  128. while (i < sample_max_depth) {
  129. frame.next_fp = NULL;
  130. frame.return_address = 0;
  131. if (!copy_stack_frame(fp, &frame))
  132. break;
  133. if ((unsigned long)fp < regs->sp)
  134. break;
  135. __trace_special(tr, data, 2, frame.return_address,
  136. (unsigned long)fp);
  137. fp = frame.next_fp;
  138. i++;
  139. }
  140. }
  141. /*
  142. * Special trace entry if we overflow the max depth:
  143. */
  144. if (i == sample_max_depth)
  145. __trace_special(tr, data, -1, -1, -1);
  146. __trace_special(tr, data, 3, current->pid, i);
  147. }
  148. static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
  149. {
  150. /* trace here */
  151. timer_notify(get_irq_regs(), smp_processor_id());
  152. hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
  153. return HRTIMER_RESTART;
  154. }
  155. static void start_stack_timer(void *unused)
  156. {
  157. struct hrtimer *hrtimer = &__get_cpu_var(stack_trace_hrtimer);
  158. hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  159. hrtimer->function = stack_trace_timer_fn;
  160. hrtimer_start(hrtimer, ns_to_ktime(sample_period),
  161. HRTIMER_MODE_REL_PINNED);
  162. }
  163. static void start_stack_timers(void)
  164. {
  165. on_each_cpu(start_stack_timer, NULL, 1);
  166. }
  167. static void stop_stack_timer(int cpu)
  168. {
  169. struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
  170. hrtimer_cancel(hrtimer);
  171. }
  172. static void stop_stack_timers(void)
  173. {
  174. int cpu;
  175. for_each_online_cpu(cpu)
  176. stop_stack_timer(cpu);
  177. }
  178. static void stop_stack_trace(struct trace_array *tr)
  179. {
  180. mutex_lock(&sample_timer_lock);
  181. stop_stack_timers();
  182. tracer_enabled = 0;
  183. mutex_unlock(&sample_timer_lock);
  184. }
  185. static int stack_trace_init(struct trace_array *tr)
  186. {
  187. sysprof_trace = tr;
  188. tracing_start_cmdline_record();
  189. mutex_lock(&sample_timer_lock);
  190. start_stack_timers();
  191. tracer_enabled = 1;
  192. mutex_unlock(&sample_timer_lock);
  193. return 0;
  194. }
  195. static void stack_trace_reset(struct trace_array *tr)
  196. {
  197. tracing_stop_cmdline_record();
  198. stop_stack_trace(tr);
  199. }
  200. static struct tracer stack_trace __read_mostly =
  201. {
  202. .name = "sysprof",
  203. .init = stack_trace_init,
  204. .reset = stack_trace_reset,
  205. #ifdef CONFIG_FTRACE_SELFTEST
  206. .selftest = trace_selftest_startup_sysprof,
  207. #endif
  208. };
  209. __init static int init_stack_trace(void)
  210. {
  211. return register_tracer(&stack_trace);
  212. }
  213. device_initcall(init_stack_trace);
  214. #define MAX_LONG_DIGITS 22
  215. static ssize_t
  216. sysprof_sample_read(struct file *filp, char __user *ubuf,
  217. size_t cnt, loff_t *ppos)
  218. {
  219. char buf[MAX_LONG_DIGITS];
  220. int r;
  221. r = sprintf(buf, "%ld\n", nsecs_to_usecs(sample_period));
  222. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  223. }
  224. static ssize_t
  225. sysprof_sample_write(struct file *filp, const char __user *ubuf,
  226. size_t cnt, loff_t *ppos)
  227. {
  228. char buf[MAX_LONG_DIGITS];
  229. unsigned long val;
  230. if (cnt > MAX_LONG_DIGITS-1)
  231. cnt = MAX_LONG_DIGITS-1;
  232. if (copy_from_user(&buf, ubuf, cnt))
  233. return -EFAULT;
  234. buf[cnt] = 0;
  235. val = simple_strtoul(buf, NULL, 10);
  236. /*
  237. * Enforce a minimum sample period of 100 usecs:
  238. */
  239. if (val < 100)
  240. val = 100;
  241. mutex_lock(&sample_timer_lock);
  242. stop_stack_timers();
  243. sample_period = val * 1000;
  244. start_stack_timers();
  245. mutex_unlock(&sample_timer_lock);
  246. return cnt;
  247. }
  248. static const struct file_operations sysprof_sample_fops = {
  249. .read = sysprof_sample_read,
  250. .write = sysprof_sample_write,
  251. };
  252. void init_tracer_sysprof_debugfs(struct dentry *d_tracer)
  253. {
  254. trace_create_file("sysprof_sample_period", 0644,
  255. d_tracer, NULL, &sysprof_sample_fops);
  256. }