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_event.h>
6 #include <asm/syscall.h>
8 #include "trace_output.h"
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
, NR_syscalls
);
15 static DECLARE_BITMAP(enabled_exit_syscalls
, NR_syscalls
);
17 extern unsigned long __start_syscalls_metadata
[];
18 extern unsigned long __stop_syscalls_metadata
[];
20 static struct syscall_metadata
**syscalls_metadata
;
22 static struct syscall_metadata
*find_syscall_meta(unsigned long syscall
)
24 struct syscall_metadata
*start
;
25 struct syscall_metadata
*stop
;
26 char str
[KSYM_SYMBOL_LEN
];
29 start
= (struct syscall_metadata
*)__start_syscalls_metadata
;
30 stop
= (struct syscall_metadata
*)__stop_syscalls_metadata
;
31 kallsyms_lookup(syscall
, NULL
, NULL
, NULL
, str
);
33 for ( ; start
< stop
; start
++) {
35 * Only compare after the "sys" prefix. Archs that use
36 * syscall wrappers may have syscalls symbols aliases prefixed
37 * with "SyS" instead of "sys", leading to an unwanted
40 if (start
->name
&& !strcmp(start
->name
+ 3, str
+ 3))
46 static struct syscall_metadata
*syscall_nr_to_meta(int nr
)
48 if (!syscalls_metadata
|| nr
>= NR_syscalls
|| nr
< 0)
51 return syscalls_metadata
[nr
];
55 print_syscall_enter(struct trace_iterator
*iter
, int flags
)
57 struct trace_seq
*s
= &iter
->seq
;
58 struct trace_entry
*ent
= iter
->ent
;
59 struct syscall_trace_enter
*trace
;
60 struct syscall_metadata
*entry
;
63 trace
= (typeof(trace
))ent
;
65 entry
= syscall_nr_to_meta(syscall
);
70 if (entry
->enter_event
->id
!= ent
->type
) {
75 ret
= trace_seq_printf(s
, "%s(", entry
->name
);
77 return TRACE_TYPE_PARTIAL_LINE
;
79 for (i
= 0; i
< entry
->nb_args
; i
++) {
81 if (trace_flags
& TRACE_ITER_VERBOSE
) {
82 ret
= trace_seq_printf(s
, "%s ", entry
->types
[i
]);
84 return TRACE_TYPE_PARTIAL_LINE
;
86 /* parameter values */
87 ret
= trace_seq_printf(s
, "%s: %lx%s", entry
->args
[i
],
89 i
== entry
->nb_args
- 1 ? "" : ", ");
91 return TRACE_TYPE_PARTIAL_LINE
;
94 ret
= trace_seq_putc(s
, ')');
96 return TRACE_TYPE_PARTIAL_LINE
;
99 ret
= trace_seq_putc(s
, '\n');
101 return TRACE_TYPE_PARTIAL_LINE
;
103 return TRACE_TYPE_HANDLED
;
107 print_syscall_exit(struct trace_iterator
*iter
, int flags
)
109 struct trace_seq
*s
= &iter
->seq
;
110 struct trace_entry
*ent
= iter
->ent
;
111 struct syscall_trace_exit
*trace
;
113 struct syscall_metadata
*entry
;
116 trace
= (typeof(trace
))ent
;
118 entry
= syscall_nr_to_meta(syscall
);
121 trace_seq_printf(s
, "\n");
122 return TRACE_TYPE_HANDLED
;
125 if (entry
->exit_event
->id
!= ent
->type
) {
127 return TRACE_TYPE_UNHANDLED
;
130 ret
= trace_seq_printf(s
, "%s -> 0x%lx\n", entry
->name
,
133 return TRACE_TYPE_PARTIAL_LINE
;
135 return TRACE_TYPE_HANDLED
;
138 extern char *__bad_type_size(void);
140 #define SYSCALL_FIELD(type, name) \
141 sizeof(type) != sizeof(trace.name) ? \
142 __bad_type_size() : \
143 #type, #name, offsetof(typeof(trace), name), \
144 sizeof(trace.name), is_signed_type(type)
147 int __set_enter_print_fmt(struct syscall_metadata
*entry
, char *buf
, int len
)
152 /* When len=0, we just calculate the needed length */
153 #define LEN_OR_ZERO (len ? len - pos : 0)
155 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"");
156 for (i
= 0; i
< entry
->nb_args
; i
++) {
157 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "%s: 0x%%0%zulx%s",
158 entry
->args
[i
], sizeof(unsigned long),
159 i
== entry
->nb_args
- 1 ? "" : ", ");
161 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
, "\"");
163 for (i
= 0; i
< entry
->nb_args
; i
++) {
164 pos
+= snprintf(buf
+ pos
, LEN_OR_ZERO
,
165 ", ((unsigned long)(REC->%s))", entry
->args
[i
]);
170 /* return the length of print_fmt */
174 static int set_syscall_print_fmt(struct ftrace_event_call
*call
)
178 struct syscall_metadata
*entry
= call
->data
;
180 if (entry
->enter_event
!= call
) {
181 call
->print_fmt
= "\"0x%lx\", REC->ret";
185 /* First: called with 0 length to calculate the needed length */
186 len
= __set_enter_print_fmt(entry
, NULL
, 0);
188 print_fmt
= kmalloc(len
+ 1, GFP_KERNEL
);
192 /* Second: actually write the @print_fmt */
193 __set_enter_print_fmt(entry
, print_fmt
, len
+ 1);
194 call
->print_fmt
= print_fmt
;
199 static void free_syscall_print_fmt(struct ftrace_event_call
*call
)
201 struct syscall_metadata
*entry
= call
->data
;
203 if (entry
->enter_event
== call
)
204 kfree(call
->print_fmt
);
207 int syscall_enter_define_fields(struct ftrace_event_call
*call
)
209 struct syscall_trace_enter trace
;
210 struct syscall_metadata
*meta
= call
->data
;
213 int offset
= offsetof(typeof(trace
), args
);
215 ret
= trace_define_field(call
, SYSCALL_FIELD(int, nr
), FILTER_OTHER
);
219 for (i
= 0; i
< meta
->nb_args
; i
++) {
220 ret
= trace_define_field(call
, meta
->types
[i
],
221 meta
->args
[i
], offset
,
222 sizeof(unsigned long), 0,
224 offset
+= sizeof(unsigned long);
230 int syscall_exit_define_fields(struct ftrace_event_call
*call
)
232 struct syscall_trace_exit trace
;
235 ret
= trace_define_field(call
, SYSCALL_FIELD(int, nr
), FILTER_OTHER
);
239 ret
= trace_define_field(call
, SYSCALL_FIELD(long, ret
),
245 void ftrace_syscall_enter(struct pt_regs
*regs
, long id
)
247 struct syscall_trace_enter
*entry
;
248 struct syscall_metadata
*sys_data
;
249 struct ring_buffer_event
*event
;
250 struct ring_buffer
*buffer
;
254 syscall_nr
= syscall_get_nr(current
, regs
);
257 if (!test_bit(syscall_nr
, enabled_enter_syscalls
))
260 sys_data
= syscall_nr_to_meta(syscall_nr
);
264 size
= sizeof(*entry
) + sizeof(unsigned long) * sys_data
->nb_args
;
266 event
= trace_current_buffer_lock_reserve(&buffer
,
267 sys_data
->enter_event
->id
, size
, 0, 0);
271 entry
= ring_buffer_event_data(event
);
272 entry
->nr
= syscall_nr
;
273 syscall_get_arguments(current
, regs
, 0, sys_data
->nb_args
, entry
->args
);
275 if (!filter_current_check_discard(buffer
, sys_data
->enter_event
,
277 trace_current_buffer_unlock_commit(buffer
, event
, 0, 0);
280 void ftrace_syscall_exit(struct pt_regs
*regs
, long ret
)
282 struct syscall_trace_exit
*entry
;
283 struct syscall_metadata
*sys_data
;
284 struct ring_buffer_event
*event
;
285 struct ring_buffer
*buffer
;
288 syscall_nr
= syscall_get_nr(current
, regs
);
291 if (!test_bit(syscall_nr
, enabled_exit_syscalls
))
294 sys_data
= syscall_nr_to_meta(syscall_nr
);
298 event
= trace_current_buffer_lock_reserve(&buffer
,
299 sys_data
->exit_event
->id
, sizeof(*entry
), 0, 0);
303 entry
= ring_buffer_event_data(event
);
304 entry
->nr
= syscall_nr
;
305 entry
->ret
= syscall_get_return_value(current
, regs
);
307 if (!filter_current_check_discard(buffer
, sys_data
->exit_event
,
309 trace_current_buffer_unlock_commit(buffer
, event
, 0, 0);
312 int reg_event_syscall_enter(struct ftrace_event_call
*call
)
317 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
318 if (num
< 0 || num
>= NR_syscalls
)
320 mutex_lock(&syscall_trace_lock
);
321 if (!sys_refcount_enter
)
322 ret
= register_trace_sys_enter(ftrace_syscall_enter
);
324 set_bit(num
, enabled_enter_syscalls
);
325 sys_refcount_enter
++;
327 mutex_unlock(&syscall_trace_lock
);
331 void unreg_event_syscall_enter(struct ftrace_event_call
*call
)
335 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
336 if (num
< 0 || num
>= NR_syscalls
)
338 mutex_lock(&syscall_trace_lock
);
339 sys_refcount_enter
--;
340 clear_bit(num
, enabled_enter_syscalls
);
341 if (!sys_refcount_enter
)
342 unregister_trace_sys_enter(ftrace_syscall_enter
);
343 mutex_unlock(&syscall_trace_lock
);
346 int reg_event_syscall_exit(struct ftrace_event_call
*call
)
351 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
352 if (num
< 0 || num
>= NR_syscalls
)
354 mutex_lock(&syscall_trace_lock
);
355 if (!sys_refcount_exit
)
356 ret
= register_trace_sys_exit(ftrace_syscall_exit
);
358 set_bit(num
, enabled_exit_syscalls
);
361 mutex_unlock(&syscall_trace_lock
);
365 void unreg_event_syscall_exit(struct ftrace_event_call
*call
)
369 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
370 if (num
< 0 || num
>= NR_syscalls
)
372 mutex_lock(&syscall_trace_lock
);
374 clear_bit(num
, enabled_exit_syscalls
);
375 if (!sys_refcount_exit
)
376 unregister_trace_sys_exit(ftrace_syscall_exit
);
377 mutex_unlock(&syscall_trace_lock
);
380 int init_syscall_trace(struct ftrace_event_call
*call
)
384 if (set_syscall_print_fmt(call
) < 0)
387 id
= trace_event_raw_init(call
);
390 free_syscall_print_fmt(call
);
397 unsigned long __init
arch_syscall_addr(int nr
)
399 return (unsigned long)sys_call_table
[nr
];
402 int __init
init_ftrace_syscalls(void)
404 struct syscall_metadata
*meta
;
408 syscalls_metadata
= kzalloc(sizeof(*syscalls_metadata
) *
409 NR_syscalls
, GFP_KERNEL
);
410 if (!syscalls_metadata
) {
415 for (i
= 0; i
< NR_syscalls
; i
++) {
416 addr
= arch_syscall_addr(i
);
417 meta
= find_syscall_meta(addr
);
421 meta
->syscall_nr
= i
;
422 syscalls_metadata
[i
] = meta
;
427 core_initcall(init_ftrace_syscalls
);
429 #ifdef CONFIG_PERF_EVENTS
431 static DECLARE_BITMAP(enabled_perf_enter_syscalls
, NR_syscalls
);
432 static DECLARE_BITMAP(enabled_perf_exit_syscalls
, NR_syscalls
);
433 static int sys_perf_refcount_enter
;
434 static int sys_perf_refcount_exit
;
436 static void perf_syscall_enter(struct pt_regs
*regs
, long id
)
438 struct syscall_metadata
*sys_data
;
439 struct syscall_trace_enter
*rec
;
445 syscall_nr
= syscall_get_nr(current
, regs
);
446 if (!test_bit(syscall_nr
, enabled_perf_enter_syscalls
))
449 sys_data
= syscall_nr_to_meta(syscall_nr
);
453 /* get the size after alignment with the u32 buffer size field */
454 size
= sizeof(unsigned long) * sys_data
->nb_args
+ sizeof(*rec
);
455 size
= ALIGN(size
+ sizeof(u32
), sizeof(u64
));
458 if (WARN_ONCE(size
> PERF_MAX_TRACE_SIZE
,
459 "perf buffer not large enough"))
462 rec
= (struct syscall_trace_enter
*)perf_trace_buf_prepare(size
,
463 sys_data
->enter_event
->id
, &rctx
, &flags
);
467 rec
->nr
= syscall_nr
;
468 syscall_get_arguments(current
, regs
, 0, sys_data
->nb_args
,
469 (unsigned long *)&rec
->args
);
470 perf_trace_buf_submit(rec
, size
, rctx
, 0, 1, flags
, regs
);
473 int perf_sysenter_enable(struct ftrace_event_call
*call
)
478 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
480 mutex_lock(&syscall_trace_lock
);
481 if (!sys_perf_refcount_enter
)
482 ret
= register_trace_sys_enter(perf_syscall_enter
);
484 pr_info("event trace: Could not activate"
485 "syscall entry trace point");
487 set_bit(num
, enabled_perf_enter_syscalls
);
488 sys_perf_refcount_enter
++;
490 mutex_unlock(&syscall_trace_lock
);
494 void perf_sysenter_disable(struct ftrace_event_call
*call
)
498 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
500 mutex_lock(&syscall_trace_lock
);
501 sys_perf_refcount_enter
--;
502 clear_bit(num
, enabled_perf_enter_syscalls
);
503 if (!sys_perf_refcount_enter
)
504 unregister_trace_sys_enter(perf_syscall_enter
);
505 mutex_unlock(&syscall_trace_lock
);
508 static void perf_syscall_exit(struct pt_regs
*regs
, long ret
)
510 struct syscall_metadata
*sys_data
;
511 struct syscall_trace_exit
*rec
;
517 syscall_nr
= syscall_get_nr(current
, regs
);
518 if (!test_bit(syscall_nr
, enabled_perf_exit_syscalls
))
521 sys_data
= syscall_nr_to_meta(syscall_nr
);
525 /* We can probably do that at build time */
526 size
= ALIGN(sizeof(*rec
) + sizeof(u32
), sizeof(u64
));
530 * Impossible, but be paranoid with the future
531 * How to put this check outside runtime?
533 if (WARN_ONCE(size
> PERF_MAX_TRACE_SIZE
,
534 "exit event has grown above perf buffer size"))
537 rec
= (struct syscall_trace_exit
*)perf_trace_buf_prepare(size
,
538 sys_data
->exit_event
->id
, &rctx
, &flags
);
542 rec
->nr
= syscall_nr
;
543 rec
->ret
= syscall_get_return_value(current
, regs
);
545 perf_trace_buf_submit(rec
, size
, rctx
, 0, 1, flags
, regs
);
548 int perf_sysexit_enable(struct ftrace_event_call
*call
)
553 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
555 mutex_lock(&syscall_trace_lock
);
556 if (!sys_perf_refcount_exit
)
557 ret
= register_trace_sys_exit(perf_syscall_exit
);
559 pr_info("event trace: Could not activate"
560 "syscall exit trace point");
562 set_bit(num
, enabled_perf_exit_syscalls
);
563 sys_perf_refcount_exit
++;
565 mutex_unlock(&syscall_trace_lock
);
569 void perf_sysexit_disable(struct ftrace_event_call
*call
)
573 num
= ((struct syscall_metadata
*)call
->data
)->syscall_nr
;
575 mutex_lock(&syscall_trace_lock
);
576 sys_perf_refcount_exit
--;
577 clear_bit(num
, enabled_perf_exit_syscalls
);
578 if (!sys_perf_refcount_exit
)
579 unregister_trace_sys_exit(perf_syscall_exit
);
580 mutex_unlock(&syscall_trace_lock
);
583 #endif /* CONFIG_PERF_EVENTS */