trace_functions.c 8.2 KB

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