trace_functions.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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, resched;
  49. int pc;
  50. if (unlikely(!ftrace_function_enabled))
  51. return;
  52. pc = preempt_count();
  53. resched = ftrace_preempt_disable();
  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. ftrace_preempt_enable(resched);
  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. };
  128. static struct ftrace_ops trace_stack_ops __read_mostly =
  129. {
  130. .func = function_stack_trace_call,
  131. };
  132. /* Our two options */
  133. enum {
  134. TRACE_FUNC_OPT_STACK = 0x1,
  135. };
  136. static struct tracer_opt func_opts[] = {
  137. #ifdef CONFIG_STACKTRACE
  138. { TRACER_OPT(func_stack_trace, TRACE_FUNC_OPT_STACK) },
  139. #endif
  140. { } /* Always set a last empty entry */
  141. };
  142. static struct tracer_flags func_flags = {
  143. .val = 0, /* By default: all flags disabled */
  144. .opts = func_opts
  145. };
  146. static void tracing_start_function_trace(void)
  147. {
  148. ftrace_function_enabled = 0;
  149. if (trace_flags & TRACE_ITER_PREEMPTONLY)
  150. trace_ops.func = function_trace_call_preempt_only;
  151. else
  152. trace_ops.func = function_trace_call;
  153. if (func_flags.val & TRACE_FUNC_OPT_STACK)
  154. register_ftrace_function(&trace_stack_ops);
  155. else
  156. register_ftrace_function(&trace_ops);
  157. ftrace_function_enabled = 1;
  158. }
  159. static void tracing_stop_function_trace(void)
  160. {
  161. ftrace_function_enabled = 0;
  162. /* OK if they are not registered */
  163. unregister_ftrace_function(&trace_stack_ops);
  164. unregister_ftrace_function(&trace_ops);
  165. }
  166. static int func_set_flag(u32 old_flags, u32 bit, int set)
  167. {
  168. if (bit == TRACE_FUNC_OPT_STACK) {
  169. /* do nothing if already set */
  170. if (!!set == !!(func_flags.val & TRACE_FUNC_OPT_STACK))
  171. return 0;
  172. if (set) {
  173. unregister_ftrace_function(&trace_ops);
  174. register_ftrace_function(&trace_stack_ops);
  175. } else {
  176. unregister_ftrace_function(&trace_stack_ops);
  177. register_ftrace_function(&trace_ops);
  178. }
  179. return 0;
  180. }
  181. return -EINVAL;
  182. }
  183. static struct tracer function_trace __read_mostly =
  184. {
  185. .name = "function",
  186. .init = function_trace_init,
  187. .reset = function_trace_reset,
  188. .start = function_trace_start,
  189. .flags = &func_flags,
  190. .set_flag = func_set_flag,
  191. #ifdef CONFIG_FTRACE_SELFTEST
  192. .selftest = trace_selftest_startup_function,
  193. #endif
  194. };
  195. #ifdef CONFIG_DYNAMIC_FTRACE
  196. static void
  197. ftrace_traceon(unsigned long ip, unsigned long parent_ip, void **data)
  198. {
  199. long *count = (long *)data;
  200. if (tracing_is_on())
  201. return;
  202. if (!*count)
  203. return;
  204. if (*count != -1)
  205. (*count)--;
  206. tracing_on();
  207. }
  208. static void
  209. ftrace_traceoff(unsigned long ip, unsigned long parent_ip, void **data)
  210. {
  211. long *count = (long *)data;
  212. if (!tracing_is_on())
  213. return;
  214. if (!*count)
  215. return;
  216. if (*count != -1)
  217. (*count)--;
  218. tracing_off();
  219. }
  220. static int
  221. ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
  222. struct ftrace_probe_ops *ops, void *data);
  223. static struct ftrace_probe_ops traceon_probe_ops = {
  224. .func = ftrace_traceon,
  225. .print = ftrace_trace_onoff_print,
  226. };
  227. static struct ftrace_probe_ops traceoff_probe_ops = {
  228. .func = ftrace_traceoff,
  229. .print = ftrace_trace_onoff_print,
  230. };
  231. static int
  232. ftrace_trace_onoff_print(struct seq_file *m, unsigned long ip,
  233. struct ftrace_probe_ops *ops, void *data)
  234. {
  235. char str[KSYM_SYMBOL_LEN];
  236. long count = (long)data;
  237. kallsyms_lookup(ip, NULL, NULL, NULL, str);
  238. seq_printf(m, "%s:", str);
  239. if (ops == &traceon_probe_ops)
  240. seq_printf(m, "traceon");
  241. else
  242. seq_printf(m, "traceoff");
  243. if (count != -1)
  244. seq_printf(m, ":count=%ld", count);
  245. seq_putc(m, '\n');
  246. return 0;
  247. }
  248. static int
  249. ftrace_trace_onoff_unreg(char *glob, char *cmd, char *param)
  250. {
  251. struct ftrace_probe_ops *ops;
  252. /* we register both traceon and traceoff to this callback */
  253. if (strcmp(cmd, "traceon") == 0)
  254. ops = &traceon_probe_ops;
  255. else
  256. ops = &traceoff_probe_ops;
  257. unregister_ftrace_function_probe_func(glob, ops);
  258. return 0;
  259. }
  260. static int
  261. ftrace_trace_onoff_callback(char *glob, char *cmd, char *param, int enable)
  262. {
  263. struct ftrace_probe_ops *ops;
  264. void *count = (void *)-1;
  265. char *number;
  266. int ret;
  267. /* hash funcs only work with set_ftrace_filter */
  268. if (!enable)
  269. return -EINVAL;
  270. if (glob[0] == '!')
  271. return ftrace_trace_onoff_unreg(glob+1, cmd, param);
  272. /* we register both traceon and traceoff to this callback */
  273. if (strcmp(cmd, "traceon") == 0)
  274. ops = &traceon_probe_ops;
  275. else
  276. ops = &traceoff_probe_ops;
  277. if (!param)
  278. goto out_reg;
  279. number = strsep(&param, ":");
  280. if (!strlen(number))
  281. goto out_reg;
  282. /*
  283. * We use the callback data field (which is a pointer)
  284. * as our counter.
  285. */
  286. ret = strict_strtoul(number, 0, (unsigned long *)&count);
  287. if (ret)
  288. return ret;
  289. out_reg:
  290. ret = register_ftrace_function_probe(glob, ops, count);
  291. return ret;
  292. }
  293. static struct ftrace_func_command ftrace_traceon_cmd = {
  294. .name = "traceon",
  295. .func = ftrace_trace_onoff_callback,
  296. };
  297. static struct ftrace_func_command ftrace_traceoff_cmd = {
  298. .name = "traceoff",
  299. .func = ftrace_trace_onoff_callback,
  300. };
  301. static int __init init_func_cmd_traceon(void)
  302. {
  303. int ret;
  304. ret = register_ftrace_command(&ftrace_traceoff_cmd);
  305. if (ret)
  306. return ret;
  307. ret = register_ftrace_command(&ftrace_traceon_cmd);
  308. if (ret)
  309. unregister_ftrace_command(&ftrace_traceoff_cmd);
  310. return ret;
  311. }
  312. #else
  313. static inline int init_func_cmd_traceon(void)
  314. {
  315. return 0;
  316. }
  317. #endif /* CONFIG_DYNAMIC_FTRACE */
  318. static __init int init_function_trace(void)
  319. {
  320. init_func_cmd_traceon();
  321. return register_tracer(&function_trace);
  322. }
  323. device_initcall(init_function_trace);