blob: 9ee6386cf8429ae9499eb46e4991d1fcd0fd2823 [file] [log] [blame]
Frederic Weisbecker47788c52009-04-08 20:40:59 +02001#include <trace/syscall.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01002#include <linux/kernel.h>
Jason Baronfb34a082009-08-10 16:52:47 -04003#include <linux/ftrace.h>
Jason Baronf4b5ffc2009-08-10 16:53:02 -04004#include <linux/perf_counter.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01005#include <asm/syscall.h>
6
7#include "trace_output.h"
8#include "trace.h"
9
Frederic Weisbecker5be71b62009-03-15 22:10:37 +010010static DEFINE_MUTEX(syscall_trace_lock);
Jason Baronfb34a082009-08-10 16:52:47 -040011static int sys_refcount_enter;
12static int sys_refcount_exit;
13static DECLARE_BITMAP(enabled_enter_syscalls, FTRACE_SYSCALL_MAX);
14static DECLARE_BITMAP(enabled_exit_syscalls, FTRACE_SYSCALL_MAX);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +010015
Frederic Weisbecker64044342009-03-15 22:10:36 +010016/* Option to display the parameters types */
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010017enum {
18 TRACE_SYSCALLS_OPT_TYPES = 0x1,
19};
20
21static struct tracer_opt syscalls_opts[] = {
22 { TRACER_OPT(syscall_arg_type, TRACE_SYSCALLS_OPT_TYPES) },
23 { }
24};
25
26static struct tracer_flags syscalls_flags = {
Frederic Weisbecker64044342009-03-15 22:10:36 +010027 .val = 0, /* By default: no parameters types */
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010028 .opts = syscalls_opts
29};
30
31enum print_line_t
32print_syscall_enter(struct trace_iterator *iter, int flags)
33{
34 struct trace_seq *s = &iter->seq;
35 struct trace_entry *ent = iter->ent;
36 struct syscall_trace_enter *trace;
37 struct syscall_metadata *entry;
38 int i, ret, syscall;
39
Jason Baron64c12e02009-08-10 16:52:53 -040040 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010041 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010042 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -040043
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010044 if (!entry)
45 goto end;
46
Jason Baron64c12e02009-08-10 16:52:53 -040047 if (entry->enter_id != ent->type) {
48 WARN_ON_ONCE(1);
49 goto end;
50 }
51
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010052 ret = trace_seq_printf(s, "%s(", entry->name);
53 if (!ret)
54 return TRACE_TYPE_PARTIAL_LINE;
55
56 for (i = 0; i < entry->nb_args; i++) {
57 /* parameter types */
58 if (syscalls_flags.val & TRACE_SYSCALLS_OPT_TYPES) {
59 ret = trace_seq_printf(s, "%s ", entry->types[i]);
60 if (!ret)
61 return TRACE_TYPE_PARTIAL_LINE;
62 }
63 /* parameter values */
64 ret = trace_seq_printf(s, "%s: %lx%s ", entry->args[i],
65 trace->args[i],
66 i == entry->nb_args - 1 ? ")" : ",");
67 if (!ret)
68 return TRACE_TYPE_PARTIAL_LINE;
69 }
70
71end:
72 trace_seq_printf(s, "\n");
73 return TRACE_TYPE_HANDLED;
74}
75
76enum print_line_t
77print_syscall_exit(struct trace_iterator *iter, int flags)
78{
79 struct trace_seq *s = &iter->seq;
80 struct trace_entry *ent = iter->ent;
81 struct syscall_trace_exit *trace;
82 int syscall;
83 struct syscall_metadata *entry;
84 int ret;
85
Jason Baron64c12e02009-08-10 16:52:53 -040086 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010087 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010088 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -040089
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010090 if (!entry) {
91 trace_seq_printf(s, "\n");
92 return TRACE_TYPE_HANDLED;
93 }
94
Jason Baron64c12e02009-08-10 16:52:53 -040095 if (entry->exit_id != ent->type) {
96 WARN_ON_ONCE(1);
97 return TRACE_TYPE_UNHANDLED;
98 }
99
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100100 ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
101 trace->ret);
102 if (!ret)
103 return TRACE_TYPE_PARTIAL_LINE;
104
105 return TRACE_TYPE_HANDLED;
106}
107
Frederic Weisbeckerdc4ddb42009-08-11 19:03:54 +0200108int ftrace_format_syscall(struct ftrace_event_call *call, struct trace_seq *s)
109{
110 int i;
111 int nr;
112 int ret = 0;
113 struct syscall_metadata *entry;
114 int offset = sizeof(struct trace_entry);
115
116 nr = syscall_name_to_nr((char *)call->data);
117 entry = syscall_nr_to_meta(nr);
118
119 if (!entry)
120 return ret;
121
122 for (i = 0; i < entry->nb_args; i++) {
123 ret = trace_seq_printf(s, "\tfield:%s %s;", entry->types[i],
124 entry->args[i]);
125 if (!ret)
126 return 0;
127 ret = trace_seq_printf(s, "\toffset:%d;\tsize:%lu;\n", offset,
128 sizeof(unsigned long));
129 if (!ret)
130 return 0;
131 offset += sizeof(unsigned long);
132 }
133
134 trace_seq_printf(s, "\nprint fmt: \"");
135 for (i = 0; i < entry->nb_args; i++) {
136 ret = trace_seq_printf(s, "%s: 0x%%0%lulx%s", entry->args[i],
137 sizeof(unsigned long),
138 i == entry->nb_args - 1 ? "\", " : ", ");
139 if (!ret)
140 return 0;
141 }
142
143 for (i = 0; i < entry->nb_args; i++) {
144 ret = trace_seq_printf(s, "((unsigned long)(REC->%s))%s",
145 entry->args[i],
146 i == entry->nb_args - 1 ? "\n" : ", ");
147 if (!ret)
148 return 0;
149 }
150
151 return ret;
152}
153
Jason Baronfb34a082009-08-10 16:52:47 -0400154void ftrace_syscall_enter(struct pt_regs *regs, long id)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100155{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100156 struct syscall_trace_enter *entry;
157 struct syscall_metadata *sys_data;
158 struct ring_buffer_event *event;
159 int size;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100160 int syscall_nr;
161
162 syscall_nr = syscall_get_nr(current, regs);
Jason Baronfb34a082009-08-10 16:52:47 -0400163 if (!test_bit(syscall_nr, enabled_enter_syscalls))
164 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100165
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100166 sys_data = syscall_nr_to_meta(syscall_nr);
167 if (!sys_data)
168 return;
169
170 size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
171
Jason Baron64c12e02009-08-10 16:52:53 -0400172 event = trace_current_buffer_lock_reserve(sys_data->enter_id, size,
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100173 0, 0);
174 if (!event)
175 return;
176
177 entry = ring_buffer_event_data(event);
178 entry->nr = syscall_nr;
179 syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
180
181 trace_current_buffer_unlock_commit(event, 0, 0);
182 trace_wake_up();
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100183}
184
Jason Baronfb34a082009-08-10 16:52:47 -0400185void ftrace_syscall_exit(struct pt_regs *regs, long ret)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100186{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100187 struct syscall_trace_exit *entry;
188 struct syscall_metadata *sys_data;
189 struct ring_buffer_event *event;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100190 int syscall_nr;
191
192 syscall_nr = syscall_get_nr(current, regs);
Jason Baronfb34a082009-08-10 16:52:47 -0400193 if (!test_bit(syscall_nr, enabled_exit_syscalls))
194 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100195
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100196 sys_data = syscall_nr_to_meta(syscall_nr);
197 if (!sys_data)
198 return;
199
Jason Baron64c12e02009-08-10 16:52:53 -0400200 event = trace_current_buffer_lock_reserve(sys_data->exit_id,
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100201 sizeof(*entry), 0, 0);
202 if (!event)
203 return;
204
205 entry = ring_buffer_event_data(event);
206 entry->nr = syscall_nr;
207 entry->ret = syscall_get_return_value(current, regs);
208
209 trace_current_buffer_unlock_commit(event, 0, 0);
210 trace_wake_up();
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100211}
212
Jason Baronfb34a082009-08-10 16:52:47 -0400213int reg_event_syscall_enter(void *ptr)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100214{
Jason Baronfb34a082009-08-10 16:52:47 -0400215 int ret = 0;
216 int num;
217 char *name;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100218
Jason Baronfb34a082009-08-10 16:52:47 -0400219 name = (char *)ptr;
220 num = syscall_name_to_nr(name);
221 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
222 return -ENOSYS;
223 mutex_lock(&syscall_trace_lock);
224 if (!sys_refcount_enter)
225 ret = register_trace_syscall_enter(ftrace_syscall_enter);
226 if (ret) {
227 pr_info("event trace: Could not activate"
228 "syscall entry trace point");
229 } else {
230 set_bit(num, enabled_enter_syscalls);
231 sys_refcount_enter++;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100232 }
Jason Baronfb34a082009-08-10 16:52:47 -0400233 mutex_unlock(&syscall_trace_lock);
234 return ret;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100235}
Jason Baronfb34a082009-08-10 16:52:47 -0400236
237void unreg_event_syscall_enter(void *ptr)
238{
239 int num;
240 char *name;
241
242 name = (char *)ptr;
243 num = syscall_name_to_nr(name);
244 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
245 return;
246 mutex_lock(&syscall_trace_lock);
247 sys_refcount_enter--;
248 clear_bit(num, enabled_enter_syscalls);
249 if (!sys_refcount_enter)
250 unregister_trace_syscall_enter(ftrace_syscall_enter);
251 mutex_unlock(&syscall_trace_lock);
252}
253
254int reg_event_syscall_exit(void *ptr)
255{
256 int ret = 0;
257 int num;
258 char *name;
259
260 name = (char *)ptr;
261 num = syscall_name_to_nr(name);
262 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
263 return -ENOSYS;
264 mutex_lock(&syscall_trace_lock);
265 if (!sys_refcount_exit)
266 ret = register_trace_syscall_exit(ftrace_syscall_exit);
267 if (ret) {
268 pr_info("event trace: Could not activate"
269 "syscall exit trace point");
270 } else {
271 set_bit(num, enabled_exit_syscalls);
272 sys_refcount_exit++;
273 }
274 mutex_unlock(&syscall_trace_lock);
275 return ret;
276}
277
278void unreg_event_syscall_exit(void *ptr)
279{
280 int num;
281 char *name;
282
283 name = (char *)ptr;
284 num = syscall_name_to_nr(name);
285 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
286 return;
287 mutex_lock(&syscall_trace_lock);
288 sys_refcount_exit--;
289 clear_bit(num, enabled_exit_syscalls);
290 if (!sys_refcount_exit)
291 unregister_trace_syscall_exit(ftrace_syscall_exit);
292 mutex_unlock(&syscall_trace_lock);
293}
294
295struct trace_event event_syscall_enter = {
296 .trace = print_syscall_enter,
Jason Baronfb34a082009-08-10 16:52:47 -0400297};
298
299struct trace_event event_syscall_exit = {
300 .trace = print_syscall_exit,
Jason Baronfb34a082009-08-10 16:52:47 -0400301};
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400302
303#ifdef CONFIG_EVENT_PROFILE
304static DECLARE_BITMAP(enabled_prof_enter_syscalls, FTRACE_SYSCALL_MAX);
305static DECLARE_BITMAP(enabled_prof_exit_syscalls, FTRACE_SYSCALL_MAX);
306static int sys_prof_refcount_enter;
307static int sys_prof_refcount_exit;
308
309static void prof_syscall_enter(struct pt_regs *regs, long id)
310{
311 struct syscall_metadata *sys_data;
312 int syscall_nr;
313
314 syscall_nr = syscall_get_nr(current, regs);
315 if (!test_bit(syscall_nr, enabled_prof_enter_syscalls))
316 return;
317
318 sys_data = syscall_nr_to_meta(syscall_nr);
319 if (!sys_data)
320 return;
321
322 perf_tpcounter_event(sys_data->enter_id, 0, 1, NULL, 0);
323}
324
325int reg_prof_syscall_enter(char *name)
326{
327 int ret = 0;
328 int num;
329
330 num = syscall_name_to_nr(name);
331 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
332 return -ENOSYS;
333
334 mutex_lock(&syscall_trace_lock);
335 if (!sys_prof_refcount_enter)
336 ret = register_trace_syscall_enter(prof_syscall_enter);
337 if (ret) {
338 pr_info("event trace: Could not activate"
339 "syscall entry trace point");
340 } else {
341 set_bit(num, enabled_prof_enter_syscalls);
342 sys_prof_refcount_enter++;
343 }
344 mutex_unlock(&syscall_trace_lock);
345 return ret;
346}
347
348void unreg_prof_syscall_enter(char *name)
349{
350 int num;
351
352 num = syscall_name_to_nr(name);
353 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
354 return;
355
356 mutex_lock(&syscall_trace_lock);
357 sys_prof_refcount_enter--;
358 clear_bit(num, enabled_prof_enter_syscalls);
359 if (!sys_prof_refcount_enter)
360 unregister_trace_syscall_enter(prof_syscall_enter);
361 mutex_unlock(&syscall_trace_lock);
362}
363
364static void prof_syscall_exit(struct pt_regs *regs, long ret)
365{
366 struct syscall_metadata *sys_data;
367 int syscall_nr;
368
369 syscall_nr = syscall_get_nr(current, regs);
370 if (!test_bit(syscall_nr, enabled_prof_exit_syscalls))
371 return;
372
373 sys_data = syscall_nr_to_meta(syscall_nr);
374 if (!sys_data)
375 return;
376
377 perf_tpcounter_event(sys_data->exit_id, 0, 1, NULL, 0);
378}
379
380int reg_prof_syscall_exit(char *name)
381{
382 int ret = 0;
383 int num;
384
385 num = syscall_name_to_nr(name);
386 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
387 return -ENOSYS;
388
389 mutex_lock(&syscall_trace_lock);
390 if (!sys_prof_refcount_exit)
391 ret = register_trace_syscall_exit(prof_syscall_exit);
392 if (ret) {
393 pr_info("event trace: Could not activate"
394 "syscall entry trace point");
395 } else {
396 set_bit(num, enabled_prof_exit_syscalls);
397 sys_prof_refcount_exit++;
398 }
399 mutex_unlock(&syscall_trace_lock);
400 return ret;
401}
402
403void unreg_prof_syscall_exit(char *name)
404{
405 int num;
406
407 num = syscall_name_to_nr(name);
408 if (num < 0 || num >= FTRACE_SYSCALL_MAX)
409 return;
410
411 mutex_lock(&syscall_trace_lock);
412 sys_prof_refcount_exit--;
413 clear_bit(num, enabled_prof_exit_syscalls);
414 if (!sys_prof_refcount_exit)
415 unregister_trace_syscall_exit(prof_syscall_exit);
416 mutex_unlock(&syscall_trace_lock);
417}
418
419#endif
420
421