trace_functions.c 7.7 KB

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