tracing: Create generic syscall TRACE_EVENTs
[linux-2.6.git] / kernel / trace / trace_syscalls.c
1 #include <trace/syscall.h>
2 #include <trace/events/syscalls.h>
3 #include <linux/kernel.h>
4 #include <linux/ftrace.h>
5 #include <linux/perf_counter.h>
6 #include <asm/syscall.h>
7
8 #include "trace_output.h"
9 #include "trace.h"
10
11 static DEFINE_MUTEX(syscall_trace_lock);
12 static int sys_refcount_enter;
13 static int sys_refcount_exit;
14 static DECLARE_BITMAP(enabled_enter_syscalls, FTRACE_SYSCALL_MAX);
15 static DECLARE_BITMAP(enabled_exit_syscalls, FTRACE_SYSCALL_MAX);
16
17 enum print_line_t
18 print_syscall_enter(struct trace_iterator *iter, int flags)
19 {
20         struct trace_seq *s = &iter->seq;
21         struct trace_entry *ent = iter->ent;
22         struct syscall_trace_enter *trace;
23         struct syscall_metadata *entry;
24         int i, ret, syscall;
25
26         trace = (typeof(trace))ent;
27         syscall = trace->nr;
28         entry = syscall_nr_to_meta(syscall);
29
30         if (!entry)
31                 goto end;
32
33         if (entry->enter_id != ent->type) {
34                 WARN_ON_ONCE(1);
35                 goto end;
36         }
37
38         ret = trace_seq_printf(s, "%s(", entry->name);
39         if (!ret)
40                 return TRACE_TYPE_PARTIAL_LINE;
41
42         for (i = 0; i < entry->nb_args; i++) {
43                 /* parameter types */
44                 if (trace_flags & TRACE_ITER_VERBOSE) {
45                         ret = trace_seq_printf(s, "%s ", entry->types[i]);
46                         if (!ret)
47                                 return TRACE_TYPE_PARTIAL_LINE;
48                 }
49                 /* parameter values */
50                 ret = trace_seq_printf(s, "%s: %lx%s", entry->args[i],
51                                        trace->args[i],
52                                        i == entry->nb_args - 1 ? "" : ", ");
53                 if (!ret)
54                         return TRACE_TYPE_PARTIAL_LINE;
55         }
56
57         ret = trace_seq_putc(s, ')');
58         if (!ret)
59                 return TRACE_TYPE_PARTIAL_LINE;
60
61 end:
62         ret =  trace_seq_putc(s, '\n');
63         if (!ret)
64                 return TRACE_TYPE_PARTIAL_LINE;
65
66         return TRACE_TYPE_HANDLED;
67 }
68
69 enum print_line_t
70 print_syscall_exit(struct trace_iterator *iter, int flags)
71 {
72         struct trace_seq *s = &iter->seq;
73         struct trace_entry *ent = iter->ent;
74         struct syscall_trace_exit *trace;
75         int syscall;
76         struct syscall_metadata *entry;
77         int ret;
78
79         trace = (typeof(trace))ent;
80         syscall = trace->nr;
81         entry = syscall_nr_to_meta(syscall);
82
83         if (!entry) {
84                 trace_seq_printf(s, "\n");
85                 return TRACE_TYPE_HANDLED;
86         }
87
88         if (entry->exit_id != ent->type) {
89                 WARN_ON_ONCE(1);
90                 return TRACE_TYPE_UNHANDLED;
91         }
92
93         ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
94                                 trace->ret);
95         if (!ret)
96                 return TRACE_TYPE_PARTIAL_LINE;
97
98         return TRACE_TYPE_HANDLED;
99 }
100
101 extern char *__bad_type_size(void);
102
103 #define SYSCALL_FIELD(type, name)                                       \
104         sizeof(type) != sizeof(trace.name) ?                            \
105                 __bad_type_size() :                                     \
106                 #type, #name, offsetof(typeof(trace), name), sizeof(trace.name)
107
108 int syscall_enter_format(struct ftrace_event_call *call, struct trace_seq *s)
109 {
110         int i;
111         int nr;
112         int ret;
113         struct syscall_metadata *entry;
114         struct syscall_trace_enter trace;
115         int offset = offsetof(struct syscall_trace_enter, args);
116
117         nr = syscall_name_to_nr(call->data);
118         entry = syscall_nr_to_meta(nr);
119
120         if (!entry)
121                 return 0;
122
123         ret = trace_seq_printf(s, "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n",
124                                SYSCALL_FIELD(int, nr));
125         if (!ret)
126                 return 0;
127
128         for (i = 0; i < entry->nb_args; i++) {
129                 ret = trace_seq_printf(s, "\tfield:%s %s;", entry->types[i],
130                                         entry->args[i]);
131                 if (!ret)
132                         return 0;
133                 ret = trace_seq_printf(s, "\toffset:%d;\tsize:%zu;\n", offset,
134                                        sizeof(unsigned long));
135                 if (!ret)
136                         return 0;
137                 offset += sizeof(unsigned long);
138         }
139
140         trace_seq_puts(s, "\nprint fmt: \"");
141         for (i = 0; i < entry->nb_args; i++) {
142                 ret = trace_seq_printf(s, "%s: 0x%%0%zulx%s", entry->args[i],
143                                         sizeof(unsigned long),
144                                         i == entry->nb_args - 1 ? "" : ", ");
145                 if (!ret)
146                         return 0;
147         }
148         trace_seq_putc(s, '"');
149
150         for (i = 0; i < entry->nb_args; i++) {
151                 ret = trace_seq_printf(s, ", ((unsigned long)(REC->%s))",
152                                        entry->args[i]);
153                 if (!ret)
154                         return 0;
155         }
156
157         return trace_seq_putc(s, '\n');
158 }
159
160 int syscall_exit_format(struct ftrace_event_call *call, struct trace_seq *s)
161 {
162         int ret;
163         struct syscall_trace_exit trace;
164
165         ret = trace_seq_printf(s,
166                                "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
167                                "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n",
168                                SYSCALL_FIELD(int, nr),
169                                SYSCALL_FIELD(unsigned long, ret));
170         if (!ret)
171                 return 0;
172
173         return trace_seq_printf(s, "\nprint fmt: \"0x%%lx\", REC->ret\n");
174 }
175
176 int syscall_enter_define_fields(struct ftrace_event_call *call)
177 {
178         struct syscall_trace_enter trace;
179         struct syscall_metadata *meta;
180         int ret;
181         int nr;
182         int i;
183         int offset = offsetof(typeof(trace), args);
184
185         nr = syscall_name_to_nr(call->data);
186         meta = syscall_nr_to_meta(nr);
187
188         if (!meta)
189                 return 0;
190
191         ret = trace_define_common_fields(call);
192         if (ret)
193                 return ret;
194
195         for (i = 0; i < meta->nb_args; i++) {
196                 ret = trace_define_field(call, meta->types[i],
197                                          meta->args[i], offset,
198                                          sizeof(unsigned long), 0);
199                 offset += sizeof(unsigned long);
200         }
201
202         return ret;
203 }
204
205 int syscall_exit_define_fields(struct ftrace_event_call *call)
206 {
207         struct syscall_trace_exit trace;
208         int ret;
209
210         ret = trace_define_common_fields(call);
211         if (ret)
212                 return ret;
213
214         ret = trace_define_field(call, SYSCALL_FIELD(unsigned long, ret), 0);
215
216         return ret;
217 }
218
219 void ftrace_syscall_enter(struct pt_regs *regs, long id)
220 {
221         struct syscall_trace_enter *entry;
222         struct syscall_metadata *sys_data;
223         struct ring_buffer_event *event;
224         int size;
225         int syscall_nr;
226
227         syscall_nr = syscall_get_nr(current, regs);
228         if (!test_bit(syscall_nr, enabled_enter_syscalls))
229                 return;
230
231         sys_data = syscall_nr_to_meta(syscall_nr);
232         if (!sys_data)
233                 return;
234
235         size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
236
237         event = trace_current_buffer_lock_reserve(sys_data->enter_id, size,
238                                                         0, 0);
239         if (!event)
240                 return;
241
242         entry = ring_buffer_event_data(event);
243         entry->nr = syscall_nr;
244         syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
245
246         if (!filter_current_check_discard(sys_data->enter_event, entry, event))
247                 trace_current_buffer_unlock_commit(event, 0, 0);
248 }
249
250 void ftrace_syscall_exit(struct pt_regs *regs, long ret)
251 {
252         struct syscall_trace_exit *entry;
253         struct syscall_metadata *sys_data;
254         struct ring_buffer_event *event;
255         int syscall_nr;
256
257         syscall_nr = syscall_get_nr(current, regs);
258         if (!test_bit(syscall_nr, enabled_exit_syscalls))
259                 return;
260
261         sys_data = syscall_nr_to_meta(syscall_nr);
262         if (!sys_data)
263                 return;
264
265         event = trace_current_buffer_lock_reserve(sys_data->exit_id,
266                                 sizeof(*entry), 0, 0);
267         if (!event)
268                 return;
269
270         entry = ring_buffer_event_data(event);
271         entry->nr = syscall_nr;
272         entry->ret = syscall_get_return_value(current, regs);
273
274         if (!filter_current_check_discard(sys_data->exit_event, entry, event))
275                 trace_current_buffer_unlock_commit(event, 0, 0);
276 }
277
278 int reg_event_syscall_enter(void *ptr)
279 {
280         int ret = 0;
281         int num;
282         char *name;
283
284         name = (char *)ptr;
285         num = syscall_name_to_nr(name);
286         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
287                 return -ENOSYS;
288         mutex_lock(&syscall_trace_lock);
289         if (!sys_refcount_enter)
290                 ret = register_trace_sys_enter(ftrace_syscall_enter);
291         if (ret) {
292                 pr_info("event trace: Could not activate"
293                                 "syscall entry trace point");
294         } else {
295                 set_bit(num, enabled_enter_syscalls);
296                 sys_refcount_enter++;
297         }
298         mutex_unlock(&syscall_trace_lock);
299         return ret;
300 }
301
302 void unreg_event_syscall_enter(void *ptr)
303 {
304         int num;
305         char *name;
306
307         name = (char *)ptr;
308         num = syscall_name_to_nr(name);
309         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
310                 return;
311         mutex_lock(&syscall_trace_lock);
312         sys_refcount_enter--;
313         clear_bit(num, enabled_enter_syscalls);
314         if (!sys_refcount_enter)
315                 unregister_trace_sys_enter(ftrace_syscall_enter);
316         mutex_unlock(&syscall_trace_lock);
317 }
318
319 int reg_event_syscall_exit(void *ptr)
320 {
321         int ret = 0;
322         int num;
323         char *name;
324
325         name = (char *)ptr;
326         num = syscall_name_to_nr(name);
327         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
328                 return -ENOSYS;
329         mutex_lock(&syscall_trace_lock);
330         if (!sys_refcount_exit)
331                 ret = register_trace_sys_exit(ftrace_syscall_exit);
332         if (ret) {
333                 pr_info("event trace: Could not activate"
334                                 "syscall exit trace point");
335         } else {
336                 set_bit(num, enabled_exit_syscalls);
337                 sys_refcount_exit++;
338         }
339         mutex_unlock(&syscall_trace_lock);
340         return ret;
341 }
342
343 void unreg_event_syscall_exit(void *ptr)
344 {
345         int num;
346         char *name;
347
348         name = (char *)ptr;
349         num = syscall_name_to_nr(name);
350         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
351                 return;
352         mutex_lock(&syscall_trace_lock);
353         sys_refcount_exit--;
354         clear_bit(num, enabled_exit_syscalls);
355         if (!sys_refcount_exit)
356                 unregister_trace_sys_exit(ftrace_syscall_exit);
357         mutex_unlock(&syscall_trace_lock);
358 }
359
360 struct trace_event event_syscall_enter = {
361         .trace                  = print_syscall_enter,
362 };
363
364 struct trace_event event_syscall_exit = {
365         .trace                  = print_syscall_exit,
366 };
367
368 #ifdef CONFIG_EVENT_PROFILE
369
370 static DECLARE_BITMAP(enabled_prof_enter_syscalls, FTRACE_SYSCALL_MAX);
371 static DECLARE_BITMAP(enabled_prof_exit_syscalls, FTRACE_SYSCALL_MAX);
372 static int sys_prof_refcount_enter;
373 static int sys_prof_refcount_exit;
374
375 static void prof_syscall_enter(struct pt_regs *regs, long id)
376 {
377         struct syscall_trace_enter *rec;
378         struct syscall_metadata *sys_data;
379         int syscall_nr;
380         int size;
381
382         syscall_nr = syscall_get_nr(current, regs);
383         if (!test_bit(syscall_nr, enabled_prof_enter_syscalls))
384                 return;
385
386         sys_data = syscall_nr_to_meta(syscall_nr);
387         if (!sys_data)
388                 return;
389
390         /* get the size after alignment with the u32 buffer size field */
391         size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
392         size = ALIGN(size + sizeof(u32), sizeof(u64));
393         size -= sizeof(u32);
394
395         do {
396                 char raw_data[size];
397
398                 /* zero the dead bytes from align to not leak stack to user */
399                 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
400
401                 rec = (struct syscall_trace_enter *) raw_data;
402                 tracing_generic_entry_update(&rec->ent, 0, 0);
403                 rec->ent.type = sys_data->enter_id;
404                 rec->nr = syscall_nr;
405                 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
406                                        (unsigned long *)&rec->args);
407                 perf_tpcounter_event(sys_data->enter_id, 0, 1, rec, size);
408         } while(0);
409 }
410
411 int reg_prof_syscall_enter(char *name)
412 {
413         int ret = 0;
414         int num;
415
416         num = syscall_name_to_nr(name);
417         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
418                 return -ENOSYS;
419
420         mutex_lock(&syscall_trace_lock);
421         if (!sys_prof_refcount_enter)
422                 ret = register_trace_sys_enter(prof_syscall_enter);
423         if (ret) {
424                 pr_info("event trace: Could not activate"
425                                 "syscall entry trace point");
426         } else {
427                 set_bit(num, enabled_prof_enter_syscalls);
428                 sys_prof_refcount_enter++;
429         }
430         mutex_unlock(&syscall_trace_lock);
431         return ret;
432 }
433
434 void unreg_prof_syscall_enter(char *name)
435 {
436         int num;
437
438         num = syscall_name_to_nr(name);
439         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
440                 return;
441
442         mutex_lock(&syscall_trace_lock);
443         sys_prof_refcount_enter--;
444         clear_bit(num, enabled_prof_enter_syscalls);
445         if (!sys_prof_refcount_enter)
446                 unregister_trace_sys_enter(prof_syscall_enter);
447         mutex_unlock(&syscall_trace_lock);
448 }
449
450 static void prof_syscall_exit(struct pt_regs *regs, long ret)
451 {
452         struct syscall_metadata *sys_data;
453         struct syscall_trace_exit rec;
454         int syscall_nr;
455
456         syscall_nr = syscall_get_nr(current, regs);
457         if (!test_bit(syscall_nr, enabled_prof_exit_syscalls))
458                 return;
459
460         sys_data = syscall_nr_to_meta(syscall_nr);
461         if (!sys_data)
462                 return;
463
464         tracing_generic_entry_update(&rec.ent, 0, 0);
465         rec.ent.type = sys_data->exit_id;
466         rec.nr = syscall_nr;
467         rec.ret = syscall_get_return_value(current, regs);
468
469         perf_tpcounter_event(sys_data->exit_id, 0, 1, &rec, sizeof(rec));
470 }
471
472 int reg_prof_syscall_exit(char *name)
473 {
474         int ret = 0;
475         int num;
476
477         num = syscall_name_to_nr(name);
478         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
479                 return -ENOSYS;
480
481         mutex_lock(&syscall_trace_lock);
482         if (!sys_prof_refcount_exit)
483                 ret = register_trace_sys_exit(prof_syscall_exit);
484         if (ret) {
485                 pr_info("event trace: Could not activate"
486                                 "syscall entry trace point");
487         } else {
488                 set_bit(num, enabled_prof_exit_syscalls);
489                 sys_prof_refcount_exit++;
490         }
491         mutex_unlock(&syscall_trace_lock);
492         return ret;
493 }
494
495 void unreg_prof_syscall_exit(char *name)
496 {
497         int num;
498
499         num = syscall_name_to_nr(name);
500         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
501                 return;
502
503         mutex_lock(&syscall_trace_lock);
504         sys_prof_refcount_exit--;
505         clear_bit(num, enabled_prof_exit_syscalls);
506         if (!sys_prof_refcount_exit)
507                 unregister_trace_sys_exit(prof_syscall_exit);
508         mutex_unlock(&syscall_trace_lock);
509 }
510
511 #endif
512
513