Merge branch 'tracing/core' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[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                                          FILTER_OTHER);
200                 offset += sizeof(unsigned long);
201         }
202
203         return ret;
204 }
205
206 int syscall_exit_define_fields(struct ftrace_event_call *call)
207 {
208         struct syscall_trace_exit trace;
209         int ret;
210
211         ret = trace_define_common_fields(call);
212         if (ret)
213                 return ret;
214
215         ret = trace_define_field(call, SYSCALL_FIELD(unsigned long, ret), 0,
216                                  FILTER_OTHER);
217
218         return ret;
219 }
220
221 void ftrace_syscall_enter(struct pt_regs *regs, long id)
222 {
223         struct syscall_trace_enter *entry;
224         struct syscall_metadata *sys_data;
225         struct ring_buffer_event *event;
226         int size;
227         int syscall_nr;
228
229         syscall_nr = syscall_get_nr(current, regs);
230         if (!test_bit(syscall_nr, enabled_enter_syscalls))
231                 return;
232
233         sys_data = syscall_nr_to_meta(syscall_nr);
234         if (!sys_data)
235                 return;
236
237         size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
238
239         event = trace_current_buffer_lock_reserve(sys_data->enter_id, size,
240                                                         0, 0);
241         if (!event)
242                 return;
243
244         entry = ring_buffer_event_data(event);
245         entry->nr = syscall_nr;
246         syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
247
248         if (!filter_current_check_discard(sys_data->enter_event, entry, event))
249                 trace_current_buffer_unlock_commit(event, 0, 0);
250 }
251
252 void ftrace_syscall_exit(struct pt_regs *regs, long ret)
253 {
254         struct syscall_trace_exit *entry;
255         struct syscall_metadata *sys_data;
256         struct ring_buffer_event *event;
257         int syscall_nr;
258
259         syscall_nr = syscall_get_nr(current, regs);
260         if (!test_bit(syscall_nr, enabled_exit_syscalls))
261                 return;
262
263         sys_data = syscall_nr_to_meta(syscall_nr);
264         if (!sys_data)
265                 return;
266
267         event = trace_current_buffer_lock_reserve(sys_data->exit_id,
268                                 sizeof(*entry), 0, 0);
269         if (!event)
270                 return;
271
272         entry = ring_buffer_event_data(event);
273         entry->nr = syscall_nr;
274         entry->ret = syscall_get_return_value(current, regs);
275
276         if (!filter_current_check_discard(sys_data->exit_event, entry, event))
277                 trace_current_buffer_unlock_commit(event, 0, 0);
278 }
279
280 int reg_event_syscall_enter(void *ptr)
281 {
282         int ret = 0;
283         int num;
284         char *name;
285
286         name = (char *)ptr;
287         num = syscall_name_to_nr(name);
288         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
289                 return -ENOSYS;
290         mutex_lock(&syscall_trace_lock);
291         if (!sys_refcount_enter)
292                 ret = register_trace_sys_enter(ftrace_syscall_enter);
293         if (ret) {
294                 pr_info("event trace: Could not activate"
295                                 "syscall entry trace point");
296         } else {
297                 set_bit(num, enabled_enter_syscalls);
298                 sys_refcount_enter++;
299         }
300         mutex_unlock(&syscall_trace_lock);
301         return ret;
302 }
303
304 void unreg_event_syscall_enter(void *ptr)
305 {
306         int num;
307         char *name;
308
309         name = (char *)ptr;
310         num = syscall_name_to_nr(name);
311         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
312                 return;
313         mutex_lock(&syscall_trace_lock);
314         sys_refcount_enter--;
315         clear_bit(num, enabled_enter_syscalls);
316         if (!sys_refcount_enter)
317                 unregister_trace_sys_enter(ftrace_syscall_enter);
318         mutex_unlock(&syscall_trace_lock);
319 }
320
321 int reg_event_syscall_exit(void *ptr)
322 {
323         int ret = 0;
324         int num;
325         char *name;
326
327         name = (char *)ptr;
328         num = syscall_name_to_nr(name);
329         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
330                 return -ENOSYS;
331         mutex_lock(&syscall_trace_lock);
332         if (!sys_refcount_exit)
333                 ret = register_trace_sys_exit(ftrace_syscall_exit);
334         if (ret) {
335                 pr_info("event trace: Could not activate"
336                                 "syscall exit trace point");
337         } else {
338                 set_bit(num, enabled_exit_syscalls);
339                 sys_refcount_exit++;
340         }
341         mutex_unlock(&syscall_trace_lock);
342         return ret;
343 }
344
345 void unreg_event_syscall_exit(void *ptr)
346 {
347         int num;
348         char *name;
349
350         name = (char *)ptr;
351         num = syscall_name_to_nr(name);
352         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
353                 return;
354         mutex_lock(&syscall_trace_lock);
355         sys_refcount_exit--;
356         clear_bit(num, enabled_exit_syscalls);
357         if (!sys_refcount_exit)
358                 unregister_trace_sys_exit(ftrace_syscall_exit);
359         mutex_unlock(&syscall_trace_lock);
360 }
361
362 struct trace_event event_syscall_enter = {
363         .trace                  = print_syscall_enter,
364 };
365
366 struct trace_event event_syscall_exit = {
367         .trace                  = print_syscall_exit,
368 };
369
370 #ifdef CONFIG_EVENT_PROFILE
371
372 static DECLARE_BITMAP(enabled_prof_enter_syscalls, FTRACE_SYSCALL_MAX);
373 static DECLARE_BITMAP(enabled_prof_exit_syscalls, FTRACE_SYSCALL_MAX);
374 static int sys_prof_refcount_enter;
375 static int sys_prof_refcount_exit;
376
377 static void prof_syscall_enter(struct pt_regs *regs, long id)
378 {
379         struct syscall_trace_enter *rec;
380         struct syscall_metadata *sys_data;
381         int syscall_nr;
382         int size;
383
384         syscall_nr = syscall_get_nr(current, regs);
385         if (!test_bit(syscall_nr, enabled_prof_enter_syscalls))
386                 return;
387
388         sys_data = syscall_nr_to_meta(syscall_nr);
389         if (!sys_data)
390                 return;
391
392         /* get the size after alignment with the u32 buffer size field */
393         size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
394         size = ALIGN(size + sizeof(u32), sizeof(u64));
395         size -= sizeof(u32);
396
397         do {
398                 char raw_data[size];
399
400                 /* zero the dead bytes from align to not leak stack to user */
401                 *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL;
402
403                 rec = (struct syscall_trace_enter *) raw_data;
404                 tracing_generic_entry_update(&rec->ent, 0, 0);
405                 rec->ent.type = sys_data->enter_id;
406                 rec->nr = syscall_nr;
407                 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
408                                        (unsigned long *)&rec->args);
409                 perf_tpcounter_event(sys_data->enter_id, 0, 1, rec, size);
410         } while(0);
411 }
412
413 int reg_prof_syscall_enter(char *name)
414 {
415         int ret = 0;
416         int num;
417
418         num = syscall_name_to_nr(name);
419         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
420                 return -ENOSYS;
421
422         mutex_lock(&syscall_trace_lock);
423         if (!sys_prof_refcount_enter)
424                 ret = register_trace_sys_enter(prof_syscall_enter);
425         if (ret) {
426                 pr_info("event trace: Could not activate"
427                                 "syscall entry trace point");
428         } else {
429                 set_bit(num, enabled_prof_enter_syscalls);
430                 sys_prof_refcount_enter++;
431         }
432         mutex_unlock(&syscall_trace_lock);
433         return ret;
434 }
435
436 void unreg_prof_syscall_enter(char *name)
437 {
438         int num;
439
440         num = syscall_name_to_nr(name);
441         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
442                 return;
443
444         mutex_lock(&syscall_trace_lock);
445         sys_prof_refcount_enter--;
446         clear_bit(num, enabled_prof_enter_syscalls);
447         if (!sys_prof_refcount_enter)
448                 unregister_trace_sys_enter(prof_syscall_enter);
449         mutex_unlock(&syscall_trace_lock);
450 }
451
452 static void prof_syscall_exit(struct pt_regs *regs, long ret)
453 {
454         struct syscall_metadata *sys_data;
455         struct syscall_trace_exit rec;
456         int syscall_nr;
457
458         syscall_nr = syscall_get_nr(current, regs);
459         if (!test_bit(syscall_nr, enabled_prof_exit_syscalls))
460                 return;
461
462         sys_data = syscall_nr_to_meta(syscall_nr);
463         if (!sys_data)
464                 return;
465
466         tracing_generic_entry_update(&rec.ent, 0, 0);
467         rec.ent.type = sys_data->exit_id;
468         rec.nr = syscall_nr;
469         rec.ret = syscall_get_return_value(current, regs);
470
471         perf_tpcounter_event(sys_data->exit_id, 0, 1, &rec, sizeof(rec));
472 }
473
474 int reg_prof_syscall_exit(char *name)
475 {
476         int ret = 0;
477         int num;
478
479         num = syscall_name_to_nr(name);
480         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
481                 return -ENOSYS;
482
483         mutex_lock(&syscall_trace_lock);
484         if (!sys_prof_refcount_exit)
485                 ret = register_trace_sys_exit(prof_syscall_exit);
486         if (ret) {
487                 pr_info("event trace: Could not activate"
488                                 "syscall entry trace point");
489         } else {
490                 set_bit(num, enabled_prof_exit_syscalls);
491                 sys_prof_refcount_exit++;
492         }
493         mutex_unlock(&syscall_trace_lock);
494         return ret;
495 }
496
497 void unreg_prof_syscall_exit(char *name)
498 {
499         int num;
500
501         num = syscall_name_to_nr(name);
502         if (num < 0 || num >= FTRACE_SYSCALL_MAX)
503                 return;
504
505         mutex_lock(&syscall_trace_lock);
506         sys_prof_refcount_exit--;
507         clear_bit(num, enabled_prof_exit_syscalls);
508         if (!sys_prof_refcount_exit)
509                 unregister_trace_sys_exit(prof_syscall_exit);
510         mutex_unlock(&syscall_trace_lock);
511 }
512
513 #endif
514
515