trace_sysprof.c 6.5 KB

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