trace_functions.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * ring buffer based function tracer
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Based on code from the latency_tracer, that is:
  8. *
  9. * Copyright (C) 2004-2006 Ingo Molnar
  10. * Copyright (C) 2004 William Lee Irwin III
  11. */
  12. #include <linux/ring_buffer.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/ftrace.h>
  16. #include <linux/fs.h>
  17. #include "trace.h"
  18. /* function tracing enabled */
  19. static int ftrace_function_enabled;
  20. static struct trace_array *func_trace;
  21. static void tracing_start_function_trace(void);
  22. static void tracing_stop_function_trace(void);
  23. static int function_trace_init(struct trace_array *tr)
  24. {
  25. func_trace = tr;
  26. tr->cpu = get_cpu();
  27. put_cpu();
  28. tracing_start_cmdline_record();
  29. tracing_start_function_trace();
  30. return 0;
  31. }
  32. static void function_trace_reset(struct trace_array *tr)
  33. {
  34. tracing_stop_function_trace();
  35. tracing_stop_cmdline_record();
  36. }
  37. static void function_trace_start(struct trace_array *tr)
  38. {
  39. tracing_reset_online_cpus(tr);
  40. }
  41. static void
  42. function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip,
  43. struct ftrace_ops *op, struct pt_regs *pt_regs)
  44. {
  45. struct trace_array *tr = func_trace;
  46. struct trace_array_cpu *data;
  47. unsigned long flags;
  48. long disabled;
  49. int cpu;
  50. int pc;
  51. if (unlikely(!ftrace_function_enabled))
  52. return;
  53. pc = preempt_count();
  54. preempt_disable_notrace();
  55. local_save_flags(flags);
  56. cpu = raw_smp_processor_id();
  57. data = tr->data[cpu];
  58. disabled = atomic_inc_return(&data->disabled);
  59. if (likely(disabled == 1))
  60. trace_function(tr, ip, parent_ip, flags, pc);
  61. atomic_dec(&data->disabled);
  62. preempt_enable_notrace();
  63. }
  64. static void
  65. function_trace_call(unsigned long ip, unsigned long parent_ip,
  66. struct ftrace_ops *op, struct pt_regs *pt_regs)
  67. {
  68. struct trace_array *tr = func_trace;
  69. struct trace_array_cpu *data;
  70. unsigned long flags;
  71. long disabled;
  72. int cpu;
  73. int pc;
  74. if (unlikely(!ftrace_function_enabled))
  75. return;
  76. /*
  77. * Need to use raw, since this must be called before the
  78. * recursive protection is performed.
  79. */
  80. local_irq_save(flags);
  81. cpu = raw_smp_processor_id();
  82. data = tr->data[cpu];
  83. disabled = atomic_inc_return(&data->disabled);
  84. if (likely(disabled == 1)) {
  85. pc = preempt_count();
  86. trace_function(tr, ip, parent_ip, flags, pc);
  87. }
  88. atomic_dec(&data->disabled);
  89. local_irq_restore(flags);
  90. }
  91. static void
  92. function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
  93. struct ftrace_ops *op, struct pt_regs *pt_regs)
  94. {
  95. struct trace_array *tr = func_trace;
  96. struct trace_array_cpu *data;
  97. unsigned long flags;
  98. long disabled;
  99. int cpu;
  100. int pc;
  101. if (unlikely(!ftrace_function_enabled))
  102. return;
  103. /*
  104. * Need to use raw, since this must be called before the
  105. * recursive protection is performed.
  106. */
  107. local_irq_save(flags);
  108. cpu = raw_smp_processor_id();
  109. data = tr->data[cpu];
  110. disabled = atomic_inc_return(&data->disabled);
  111. if (likely(disabled == 1)) {
  112. pc = preempt_count();
  113. trace_function(tr, ip, parent_ip, flags, pc);
  114. /*
  115. * skip over 5 funcs:
  116. * __ftrace_trace_stack,
  117. * __trace_stack,
  118. * function_stack_trace_call
  119. * ftrace_list_func
  120. * ftrace_call
  121. */
  122. __trace_stack(tr, flags, 5, pc);
  123. }
  124. atomic_dec(&data->disabled);
  125. local_irq_restore(flags);
  126. }
  127. static struct ftrace_ops trace_ops __read_mostly =
  128. {
  129. .func = function_trace_call,
  130. .flags = FTRACE_OPS_FL_GLOBAL,
  131. };
  132. static struct ftrace_ops trace_stack_ops __read_mostly =
  133. {
  134. .func = function_stack_trace_call,
  135. .flags = FTRACE_OPS_FL_GLOBAL,
  136. };
  137. /* Our two options */
  138. enum {
  139. TRACE_FUNC_OPT_STACK = 0x1,
  140. };
  141. static struct tracer_opt func_opts[] = {
  142. #ifdef CONFIG_STACKTRACE
  143. { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
  144. #endif
  145. { } /* Always set a last empty entry */
  146. };
  147. static struct tracer_flags func_flags = {
  148. .val = 0, /* By default: all flags disabled */
  149. .opts = func_opts
  150. };
  151. static void tracing_start_function_trace(void)
  152. {
  153. ftrace_function_enabled = 0;
  154. if (trace_flags & TRACE_ITER_PREEMPTONLY)
  155. trace_ops.func = function_trace_call_preempt_only;
  156. else
  157. trace_ops.func = function_trace_call;
  158. if (func_flags.val & TRACE_FUNC_OPT_STACK)
  159. register_ftrace_function(&trace_stack_ops);
  160. else
  161. register_ftrace_function(&trace_ops);
  162. ftrace_function_enabled = 1;
  163. }
  164. static void tracing_stop_function_trace(void)
  165. {
  166. ftrace_function_enabled = 0;
  167. if (func_flags.val & TRACE_FUNC_OPT_STACK)
  168. unregister_ftrace_function(&trace_stack_ops);
  169. else
  170. unregister_ftrace_function(&trace_ops);
  171. }
  172. static int func_set_flag(u32 old_flags, u32 bit, int set)
  173. {
  174. if (bit == TRACE_FUNC_OPT_STACK) {
  175. /* do nothing if already set */
  176. if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
  177. return 0;
  178. if (set) {
  179. unregister_ftrace_function(&trace_ops);
  180. register_ftrace_function(&trace_stack_ops);
  181. } else {
  182. unregister_ftrace_function(&trace_stack_ops);
  183. register_ftrace_function(&trace_ops);
  184. }
  185. return 0;
  186. }
  187. return -EINVAL;
  188. }
  189. static struct tracer function_trace __read_mostly =
  190. {
  191. .name = "function",
  192. .init = function_trace_init,
  193. .reset = function_trace_reset,
  194. .start = function_trace_start,
  195. .wait_pipe = poll_wait_pipe,
  196. .flags = &func_flags,
  197. .set_flag = func_set_flag,
  198. #ifdef CONFIG_FTRACE_SELFTEST
  199. .selftest = trace_selftest_startup_function,
  200. #endif
  201. };
  202. #ifdef CONFIG_DYNAMIC_FTRACE
  203. static void
  204. ftrace_traceon(unsigned long ip, unsigned long parent_ip, void **data)
  205. {
  206. long *count = (long *)data;
  207. if (tracing_is_on())
  208. return;
  209. if (!*count)
  210. return;
  211. if (*count != -1)
  212. (*count)--;
  213. tracing_on();
  214. }
  215. static void
  216. ftrace_traceoff(unsigned long ip, unsigned long parent_ip, void **data)
  217. {
  218. long *count = (long *)data;
  219. if (!tracing_is_on())
  220. return;
  221. if (!*count)
  222. return;
  223. if (*count != -1)
  224. (*count)--;
  225. tracing_off();
  226. }
  227. static int
  228. ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
  229. struct ftrace_probe_ops *ops, void *data);
  230. static struct ftrace_probe_ops traceon_probe_ops = {
  231. .func = ftrace_traceon,
  232. .print = ftrace_trace_onoff_print,
  233. };
  234. static struct ftrace_probe_ops traceoff_probe_ops = {
  235. .func = ftrace_traceoff,
  236. .print = ftrace_trace_onoff_print,
  237. };
  238. static int
  239. ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
  240. struct ftrace_probe_ops *ops, void *data)
  241. {
  242. long count = (long)data;
  243. seq_printf(m, "%ps:", (void *)ip);
  244. if (ops == &traceon_probe_ops)
  245. seq_printf(m, "traceon");
  246. else
  247. seq_printf(m, "traceoff");
  248. if (count == -1)
  249. seq_printf(m, ":unlimited\n");
  250. else
  251. seq_printf(m, ":count=%ld\n", count);
  252. return 0;
  253. }
  254. static int
  255. ftrace_trace_onoff_unreg(char *glob, char *cmd, char *param)
  256. {
  257. struct ftrace_probe_ops *ops;
  258. /* we register both traceon and traceoff to this callback */
  259. if (strcmp(cmd, "traceon") == 0)
  260. ops = &traceon_probe_ops;
  261. else
  262. ops = &traceoff_probe_ops;
  263. unregister_ftrace_function_probe_func(glob, ops);
  264. return 0;
  265. }
  266. static int
  267. ftrace_trace_onoff_callback(struct ftrace_hash *hash,
  268. char *glob, char *cmd, char *param, int enable)
  269. {
  270. struct ftrace_probe_ops *ops;
  271. void *count = (void *)-1;
  272. char *number;
  273. int ret;
  274. /* hash funcs only work with set_ftrace_filter */
  275. if (!enable)
  276. return -EINVAL;
  277. if (glob[0] == '!')
  278. return ftrace_trace_onoff_unreg(glob+1, cmd, param);
  279. /* we register both traceon and traceoff to this callback */
  280. if (strcmp(cmd, "traceon") == 0)
  281. ops = &traceon_probe_ops;
  282. else
  283. ops = &traceoff_probe_ops;
  284. if (!param)
  285. goto out_reg;
  286. number = strsep(&param, ":");
  287. if (!strlen(number))
  288. goto out_reg;
  289. /*
  290. * We use the callback data field (which is a pointer)
  291. * as our counter.
  292. */
  293. ret = strict_strtoul(number, 0, (unsigned long *)&count);
  294. if (ret)
  295. return ret;
  296. out_reg:
  297. ret = register_ftrace_function_probe(glob, ops, count);
  298. return ret < 0 ? ret : 0;
  299. }
  300. static struct ftrace_func_command ftrace_traceon_cmd = {
  301. .name = "traceon",
  302. .func = ftrace_trace_onoff_callback,
  303. };
  304. static struct ftrace_func_command ftrace_traceoff_cmd = {
  305. .name = "traceoff",
  306. .func = ftrace_trace_onoff_callback,
  307. };
  308. static int __init init_func_cmd_traceon(void)
  309. {
  310. int ret;
  311. ret = register_ftrace_command(&ftrace_traceoff_cmd);
  312. if (ret)
  313. return ret;
  314. ret = register_ftrace_command(&ftrace_traceon_cmd);
  315. if (ret)
  316. unregister_ftrace_command(&ftrace_traceoff_cmd);
  317. return ret;
  318. }
  319. #else
  320. static inline int init_func_cmd_traceon(void)
  321. {
  322. return 0;
  323. }
  324. #endif /* CONFIG_DYNAMIC_FTRACE */
  325. static __init int init_function_trace(void)
  326. {
  327. init_func_cmd_traceon();
  328. return register_tracer(&function_trace);
  329. }
  330. device_initcall(init_function_trace);