1 #include <trace/syscall.h>
2 #include <trace/events/syscalls.h>
3 #include <linux/slab.h>
4 #include <linux/kernel.h>
5 #include <linux/ftrace.h>
6 #include <linux/perf_event.h>
7 #include <asm/syscall.h>
9 #include "trace_output.h"
12 static DEFINE_MUTEX(syscall_trace_lock
);
13 static int sys_refcount_enter
;
14 static int sys_refcount_exit
;
15 static DECLARE_BITMAP(enabled_enter_syscalls
, NR_syscalls
);
16 static DECLARE_BITMAP(enabled_exit_syscalls
, NR_syscalls
);
18 static int syscall_enter_register(struct ftrace_event_call
*event
,
20 static int syscall_exit_register(struct ftrace_event_call
*event
,
23 static int syscall_enter_define_fields(struct ftrace_event_call
*call
);
24 static int syscall_exit_define_fields(struct ftrace_event_call
*call
);
26 static struct list_head
*
27 syscall_get_enter_fields(struct ftrace_event_call
*call
)
29 struct syscall_metadata
*entry
= call
->data
;
31 return &entry
->enter_fields
;
34 struct trace_event_functions enter_syscall_print_funcs
= {
35 .trace
= print_syscall_enter
,
38 struct trace_event_functions exit_syscall_print_funcs
= {
39 .trace
= print_syscall_exit
,
42 struct ftrace_event_class event_class_syscall_enter
= {
44 .reg
= syscall_enter_register
,
45 .define_fields
= syscall_enter_define_fields
,
46 .get_fields
= syscall_get_enter_fields
,
47 .raw_init
= init_syscall_trace
,
50 struct ftrace_event_class event_class_syscall_exit
= {
52 .reg
= syscall_exit_register
,
53 .define_fields
= syscall_exit_define_fields
,
54 .fields
= LIST_HEAD_INIT(event_class_syscall_exit
.fields
),
55 .raw_init
= init_syscall_trace
,
58 extern struct syscall_metadata
*__start_syscalls_metadata
[];
59 extern struct syscall_metadata
*__stop_syscalls_metadata
[];
61 static struct syscall_metadata
**syscalls_metadata
;
63 static __init
struct syscall_metadata
*
64 find_syscall_meta(unsigned long syscall
)
66 struct syscall_metadata
**start
;
67 struct syscall_metadata
**stop
;
68 char str
[KSYM_SYMBOL_LEN
];
71 start
= __start_syscalls_metadata
;
72 stop
= __stop_syscalls_metadata
;
73 kallsyms_lookup(syscall
, NULL
, NULL
, NULL
, str
);
75 for ( ; start
< stop
; start
++) {
77 * Only compare after the "sys" prefix. Archs that use
78 * syscall wrappers may have syscalls symbols aliases prefixed
79 * with "SyS" instead of "sys", leading to an unwanted
82 if ((*start
)->name
&& !strcmp((*start
)->name
+ 3, str
+ 3))
88 static struct syscall_metadata
*syscall_nr_to_meta(int nr
)
90 if (!syscalls_metadata
|| nr
>= NR_syscalls
|| nr
< 0)
93 return syscalls_metadata
[nr
];
97 print_syscall_enter(struct trace_iterator
*iter
, int flags
,
98 struct trace_event
*event
)
100 struct trace_seq
*s
= &iter
->seq
;
101 struct trace_entry
*ent
= iter
->ent
;
102 struct syscall_trace_enter
*trace
;
103 struct syscall_metadata
*entry
;
106 trace
= (typeof(trace
))ent
;
108 entry
= syscall_nr_to_meta(syscall
);
113 if (entry
->enter_event
->event
.type
!= ent
->type
) {
118 ret
= trace_seq_printf(s
, "%s(", entry
->name
);
120 return TRACE_TYPE_PARTIAL_LINE
;
122 for (i
= 0; i
< entry
->nb_args
; i
++) {
123 /* parameter types */
124 if (trace_flags
& TRACE_ITER_VERBOSE
) {
125 ret
= trace_seq_printf(s
, "%s ", entry
->types
[i
]);
127 return TRACE_TYPE_PARTIAL_LINE
;
129 /* parameter values */
130 ret
= trace_seq_printf(s
, "%s: %lx%s", entry
->args
[i
],
132 i
== entry
->nb_args
- 1 ? "" : ", ");
134 return TRACE_TYPE_PARTIAL_LINE
;
137 ret
= trace_seq_putc(s
, ')');
139 return TRACE_TYPE_PARTIAL_LINE
;
142 ret
= trace_seq_putc(s
, '\n');
144 return TRACE_TYPE_PARTIAL_LINE
;
146 return TRACE_TYPE_HANDLED
;
150 print_syscall_exit(struct trace_iterator
*iter
, int flags
,
151 struct trace_event
*event
)
153 struct trace_seq
*s
= &iter
->seq
;
154 struct trace_entry
*ent
= iter
->ent
;
155 struct syscall_trace_exit
*trace
;
157 struct syscall_metadata
*entry
;
160 trace
= (typeof(trace
))ent
;
162 entry
= syscall_nr_to_meta(syscall
);
165 trace_seq_printf(s
, "\n");
166 return TRACE_TYPE_HANDLED
;
169 if (entry
->exit_event
->event
.type
!= ent
->type
) {
171 return TRACE_TYPE_UNHANDLED
;
174 ret
= trace_seq_printf(s
, "%s -> 0x%lx\n", entry
->name
,
177 return TRACE_TYPE_PARTIAL_LINE
;
179 return TRACE_TYPE_HANDLED
;
182 extern char *__bad_type_size(void);
184 #define SYSCALL_FIELD(type, name) \
185 sizeof(type) != sizeof(trace.name) ? \
186 __bad_type_size() : \
187 #type, #name, offsetof(typeof(trace), name), \
188 sizeof(trace.name), is_signed_type(type)
191 int __set_enter_print_fmt(struct syscall_metadata
*entry
, char *buf
, int len
)
196 /* When len=0, we just calculate the needed length */
197 #define LEN_OR_ZERO (len ? len - pos : 0)
199 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"");
200 for (i
= 0; i
< entry
->nb_args
; i
++) {
201 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "%s: 0x%%0%zulx%s",
202 entry
->args
[i
], sizeof(unsigned long),
203 i
== entry
->nb_args
- 1 ? "" : ", ");
205 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"");
207 for (i
= 0; i
< entry
->nb_args
; i
++) {
208 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
209 ", ((unsigned long)(REC->%s))", entry
->args
[i
]);
214 /* return the length of print_fmt */
218 static int set_syscall_print_fmt(struct ftrace_event_call
*call
)
222 struct syscall_metadata
*entry
= call
->data
;
224 if (entry
->enter_event
!= call
) {
225 call
->print_fmt
= "\"0x%lx\", REC->ret";
229 /* First: called with 0 length to calculate the needed length */
230 len
= __set_enter_print_fmt(entry
, NULL
, 0);
232 print_fmt
= kmalloc(len
+ 1, GFP_KERNEL
);
236 /* Second: actually write the @print_fmt */
237 __set_enter_print_fmt(entry
, print_fmt
, len
+ 1);
238 call
->print_fmt
= print_fmt
;
243 static void free_syscall_print_fmt(struct ftrace_event_call
*call
)
245 struct syscall_metadata
*entry
= call
->data
;
247 if (entry
->enter_event
== call
)
248 kfree(call
->print_fmt
);
251 static int syscall_enter_define_fields(struct ftrace_event_call
*call
)
253 struct syscall_trace_enter trace
;
254 struct syscall_metadata
*meta
= call
->data
;
257 int offset
= offsetof(typeof(trace
), args
);
259 ret
= trace_define_field(call
, SYSCALL_FIELD(int, nr
), FILTER_OTHER
);
263 for (i
= 0; i
< meta
->nb_args
; i
++) {
264 ret
= trace_define_field(call
, meta
->types
[i
],
265 meta
->args
[i
], offset
,
266 sizeof(unsigned long), 0,
268 offset
+= sizeof(unsigned long);
274 static int syscall_exit_define_fields(struct ftrace_event_call
*call
)
276 struct syscall_trace_exit trace
;
279 ret
= trace_define_field(call
, SYSCALL_FIELD(int, nr
), FILTER_OTHER
);
283 ret
= trace_define_field(call
, SYSCALL_FIELD(long, ret
),
289 void ftrace_syscall_enter(void *ignore
, struct pt_regs
*regs
, long id
)
291 struct syscall_trace_enter
*entry
;
292 struct syscall_metadata
*sys_data
;
293 struct ring_buffer_event
*event
;
294 struct ring_buffer
*buffer
;
298 syscall_nr
= syscall_get_nr(current
, regs
);
301 if (!test_bit(syscall_nr
, enabled_enter_syscalls
))
304 sys_data
= syscall_nr_to_meta(syscall_nr
);
308 size
= sizeof(*entry
) + sizeof(unsigned long) * sys_data
->nb_args
;
310 event
= trace_current_buffer_lock_reserve(&buffer
,
311 sys_data
->enter_event
->event
.type
, size
, 0, 0);
315 entry
= ring_buffer_event_data(event
);
316 entry
->nr
= syscall_nr
;
317 syscall_get_arguments(current
, regs
, 0, sys_data
->nb_args
, entry
->args
);
319 if (!filter_current_check_discard(buffer
, sys_data
->enter_event
,
321 trace_current_buffer_unlock_commit(buffer
, event
, 0, 0);
324 void ftrace_syscall_exit(void *ignore
, struct pt_regs
*regs
, long ret
)
326 struct syscall_trace_exit
*entry
;
327 struct syscall_metadata
*sys_data
;
328 struct ring_buffer_event
*event
;
329 struct ring_buffer
*buffer
;
332 syscall_nr
= syscall_get_nr(current
, regs
);
335 if (!test_bit(syscall_nr
, enabled_exit_syscalls
))
338 sys_data
= syscall_nr_to_meta(syscall_nr
);
342 event
= trace_current_buffer_lock_reserve(&buffer
,
343 sys_data
->exit_event
->event
.type
, sizeof(*entry
), 0, 0);
347 entry
= ring_buffer_event_data(event
);
348 entry
->nr
= syscall_nr
;
349 entry
->ret
= syscall_get_return_value(current
, regs
);
351 if (!filter_current_check_discard(buffer
, sys_data
->exit_event
,
353 trace_current_buffer_unlock_commit(buffer
, event
, 0, 0);
356 int reg_event_syscall_enter(struct ftrace_event_call
*call
)
361 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
362 if (num
< 0 || num
>= NR_syscalls
)
364 mutex_lock(&syscall_trace_lock
);
365 if (!sys_refcount_enter
)
366 ret
= register_trace_sys_enter(ftrace_syscall_enter
, NULL
);
368 set_bit(num
, enabled_enter_syscalls
);
369 sys_refcount_enter
++;
371 mutex_unlock(&syscall_trace_lock
);
375 void unreg_event_syscall_enter(struct ftrace_event_call
*call
)
379 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
380 if (num
< 0 || num
>= NR_syscalls
)
382 mutex_lock(&syscall_trace_lock
);
383 sys_refcount_enter
--;
384 clear_bit(num
, enabled_enter_syscalls
);
385 if (!sys_refcount_enter
)
386 unregister_trace_sys_enter(ftrace_syscall_enter
, NULL
);
387 mutex_unlock(&syscall_trace_lock
);
390 int reg_event_syscall_exit(struct ftrace_event_call
*call
)
395 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
396 if (num
< 0 || num
>= NR_syscalls
)
398 mutex_lock(&syscall_trace_lock
);
399 if (!sys_refcount_exit
)
400 ret
= register_trace_sys_exit(ftrace_syscall_exit
, NULL
);
402 set_bit(num
, enabled_exit_syscalls
);
405 mutex_unlock(&syscall_trace_lock
);
409 void unreg_event_syscall_exit(struct ftrace_event_call
*call
)
413 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
414 if (num
< 0 || num
>= NR_syscalls
)
416 mutex_lock(&syscall_trace_lock
);
418 clear_bit(num
, enabled_exit_syscalls
);
419 if (!sys_refcount_exit
)
420 unregister_trace_sys_exit(ftrace_syscall_exit
, NULL
);
421 mutex_unlock(&syscall_trace_lock
);
424 int init_syscall_trace(struct ftrace_event_call
*call
)
428 if (set_syscall_print_fmt(call
) < 0)
431 id
= trace_event_raw_init(call
);
434 free_syscall_print_fmt(call
);
441 unsigned long __init
arch_syscall_addr(int nr
)
443 return (unsigned long)sys_call_table
[nr
];
446 int __init
init_ftrace_syscalls(void)
448 struct syscall_metadata
*meta
;
452 syscalls_metadata
= kzalloc(sizeof(*syscalls_metadata
) *
453 NR_syscalls
, GFP_KERNEL
);
454 if (!syscalls_metadata
) {
459 for (i
= 0; i
< NR_syscalls
; i
++) {
460 addr
= arch_syscall_addr(i
);
461 meta
= find_syscall_meta(addr
);
465 meta
->syscall_nr
= i
;
466 syscalls_metadata
[i
] = meta
;
471 core_initcall(init_ftrace_syscalls
);
473 #ifdef CONFIG_PERF_EVENTS
475 static DECLARE_BITMAP(enabled_perf_enter_syscalls
, NR_syscalls
);
476 static DECLARE_BITMAP(enabled_perf_exit_syscalls
, NR_syscalls
);
477 static int sys_perf_refcount_enter
;
478 static int sys_perf_refcount_exit
;
480 static void perf_syscall_enter(void *ignore
, struct pt_regs
*regs
, long id
)
482 struct syscall_metadata
*sys_data
;
483 struct syscall_trace_enter
*rec
;
484 struct hlist_head
*head
;
489 syscall_nr
= syscall_get_nr(current
, regs
);
490 if (!test_bit(syscall_nr
, enabled_perf_enter_syscalls
))
493 sys_data
= syscall_nr_to_meta(syscall_nr
);
497 /* get the size after alignment with the u32 buffer size field */
498 size
= sizeof(unsigned long) * sys_data
->nb_args
+ sizeof(*rec
);
499 size
= ALIGN(size
+ sizeof(u32
), sizeof(u64
));
502 if (WARN_ONCE(size
> PERF_MAX_TRACE_SIZE
,
503 "perf buffer not large enough"))
506 rec
= (struct syscall_trace_enter
*)perf_trace_buf_prepare(size
,
507 sys_data
->enter_event
->event
.type
, regs
, &rctx
);
511 rec
->nr
= syscall_nr
;
512 syscall_get_arguments(current
, regs
, 0, sys_data
->nb_args
,
513 (unsigned long *)&rec
->args
);
515 head
= this_cpu_ptr(sys_data
->enter_event
->perf_events
);
516 perf_trace_buf_submit(rec
, size
, rctx
, 0, 1, regs
, head
);
519 int perf_sysenter_enable(struct ftrace_event_call
*call
)
524 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
526 mutex_lock(&syscall_trace_lock
);
527 if (!sys_perf_refcount_enter
)
528 ret
= register_trace_sys_enter(perf_syscall_enter
, NULL
);
530 pr_info("event trace: Could not activate"
531 "syscall entry trace point");
533 set_bit(num
, enabled_perf_enter_syscalls
);
534 sys_perf_refcount_enter
++;
536 mutex_unlock(&syscall_trace_lock
);
540 void perf_sysenter_disable(struct ftrace_event_call
*call
)
544 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
546 mutex_lock(&syscall_trace_lock
);
547 sys_perf_refcount_enter
--;
548 clear_bit(num
, enabled_perf_enter_syscalls
);
549 if (!sys_perf_refcount_enter
)
550 unregister_trace_sys_enter(perf_syscall_enter
, NULL
);
551 mutex_unlock(&syscall_trace_lock
);
554 static void perf_syscall_exit(void *ignore
, struct pt_regs
*regs
, long ret
)
556 struct syscall_metadata
*sys_data
;
557 struct syscall_trace_exit
*rec
;
558 struct hlist_head
*head
;
563 syscall_nr
= syscall_get_nr(current
, regs
);
564 if (!test_bit(syscall_nr
, enabled_perf_exit_syscalls
))
567 sys_data
= syscall_nr_to_meta(syscall_nr
);
571 /* We can probably do that at build time */
572 size
= ALIGN(sizeof(*rec
) + sizeof(u32
), sizeof(u64
));
576 * Impossible, but be paranoid with the future
577 * How to put this check outside runtime?
579 if (WARN_ONCE(size
> PERF_MAX_TRACE_SIZE
,
580 "exit event has grown above perf buffer size"))
583 rec
= (struct syscall_trace_exit
*)perf_trace_buf_prepare(size
,
584 sys_data
->exit_event
->event
.type
, regs
, &rctx
);
588 rec
->nr
= syscall_nr
;
589 rec
->ret
= syscall_get_return_value(current
, regs
);
591 head
= this_cpu_ptr(sys_data
->exit_event
->perf_events
);
592 perf_trace_buf_submit(rec
, size
, rctx
, 0, 1, regs
, head
);
595 int perf_sysexit_enable(struct ftrace_event_call
*call
)
600 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
602 mutex_lock(&syscall_trace_lock
);
603 if (!sys_perf_refcount_exit
)
604 ret
= register_trace_sys_exit(perf_syscall_exit
, NULL
);
606 pr_info("event trace: Could not activate"
607 "syscall exit trace point");
609 set_bit(num
, enabled_perf_exit_syscalls
);
610 sys_perf_refcount_exit
++;
612 mutex_unlock(&syscall_trace_lock
);
616 void perf_sysexit_disable(struct ftrace_event_call
*call
)
620 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
622 mutex_lock(&syscall_trace_lock
);
623 sys_perf_refcount_exit
--;
624 clear_bit(num
, enabled_perf_exit_syscalls
);
625 if (!sys_perf_refcount_exit
)
626 unregister_trace_sys_exit(perf_syscall_exit
, NULL
);
627 mutex_unlock(&syscall_trace_lock
);
630 #endif /* CONFIG_PERF_EVENTS */
632 static int syscall_enter_register(struct ftrace_event_call
*event
,
636 case TRACE_REG_REGISTER
:
637 return reg_event_syscall_enter(event
);
638 case TRACE_REG_UNREGISTER
:
639 unreg_event_syscall_enter(event
);
642 #ifdef CONFIG_PERF_EVENTS
643 case TRACE_REG_PERF_REGISTER
:
644 return perf_sysenter_enable(event
);
645 case TRACE_REG_PERF_UNREGISTER
:
646 perf_sysenter_disable(event
);
653 static int syscall_exit_register(struct ftrace_event_call
*event
,
657 case TRACE_REG_REGISTER
:
658 return reg_event_syscall_exit(event
);
659 case TRACE_REG_UNREGISTER
:
660 unreg_event_syscall_exit(event
);
663 #ifdef CONFIG_PERF_EVENTS
664 case TRACE_REG_PERF_REGISTER
:
665 return perf_sysexit_enable(event
);
666 case TRACE_REG_PERF_UNREGISTER
:
667 perf_sysexit_disable(event
);