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