1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
5 * LTTng syscall probes.
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/compat.h>
13 #include <linux/err.h>
14 #include <linux/bitmap.h>
16 #include <linux/in6.h>
17 #include <linux/seq_file.h>
18 #include <linux/stringify.h>
19 #include <linux/file.h>
20 #include <linux/anon_inodes.h>
21 #include <linux/fcntl.h>
22 #include <linux/mman.h>
23 #include <asm/ptrace.h>
24 #include <asm/syscall.h>
26 #include <lttng/bitfield.h>
27 #include <wrapper/tracepoint.h>
28 #include <wrapper/file.h>
29 #include <wrapper/rcu.h>
30 #include <wrapper/syscall.h>
31 #include <lttng/events.h>
34 # ifndef is_compat_task
35 # define is_compat_task() (0)
39 /* in_compat_syscall appears in kernel 4.6. */
40 #ifndef in_compat_syscall
41 #define in_compat_syscall() is_compat_task()
51 #define SYSCALL_ENTRY_TOK syscall_entry_
52 #define COMPAT_SYSCALL_ENTRY_TOK compat_syscall_entry_
53 #define SYSCALL_EXIT_TOK syscall_exit_
54 #define COMPAT_SYSCALL_EXIT_TOK compat_syscall_exit_
56 #define SYSCALL_ENTRY_STR __stringify(SYSCALL_ENTRY_TOK)
57 #define COMPAT_SYSCALL_ENTRY_STR __stringify(COMPAT_SYSCALL_ENTRY_TOK)
58 #define SYSCALL_EXIT_STR __stringify(SYSCALL_EXIT_TOK)
59 #define COMPAT_SYSCALL_EXIT_STR __stringify(COMPAT_SYSCALL_EXIT_TOK)
62 void syscall_entry_probe(void *__data
, struct pt_regs
*regs
, long id
);
64 void syscall_exit_probe(void *__data
, struct pt_regs
*regs
, long ret
);
67 * Forward declarations for old kernels.
71 struct oldold_utsname
;
73 struct sel_arg_struct
;
74 struct mmap_arg_struct
;
79 * Forward declaration for kernels >= 5.6
86 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
87 typedef __kernel_old_time_t
time_t;
90 #ifdef IA32_NR_syscalls
91 #define NR_compat_syscalls IA32_NR_syscalls
93 #define NR_compat_syscalls NR_syscalls
97 * Create LTTng tracepoint probes.
99 #define LTTNG_PACKAGE_BUILD
100 #define CREATE_TRACE_POINTS
101 #define TP_MODULE_NOINIT
102 #define TRACE_INCLUDE_PATH instrumentation/syscalls/headers
104 #define PARAMS(args...) args
106 /* Handle unknown syscalls */
108 #define TRACE_SYSTEM syscalls_unknown
109 #include <instrumentation/syscalls/headers/syscalls_unknown.h>
117 #define sc_in(...) __VA_ARGS__
121 #define sc_inout(...) __VA_ARGS__
123 /* Hijack probe callback for system call enter */
125 #define TP_PROBE_CB(_template) &syscall_entry_probe
126 #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \
127 LTTNG_TRACEPOINT_EVENT(syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \
129 #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \
130 LTTNG_TRACEPOINT_EVENT_CODE(syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \
131 PARAMS(_locvar), PARAMS(_code_pre), \
132 PARAMS(_fields), PARAMS(_code_post))
133 #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
134 LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(syscall_entry_##_name, PARAMS(_fields))
135 #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \
136 LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(syscall_entry_##_template, syscall_entry_##_name)
137 /* Enumerations only defined at first inclusion. */
138 #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values) \
139 LTTNG_TRACEPOINT_ENUM(_name, PARAMS(_values))
141 #define TRACE_SYSTEM syscall_entry_integers
142 #define TRACE_INCLUDE_FILE syscalls_integers
143 #include <instrumentation/syscalls/headers/syscalls_integers.h>
144 #undef TRACE_INCLUDE_FILE
146 #define TRACE_SYSTEM syscall_entry_pointers
147 #define TRACE_INCLUDE_FILE syscalls_pointers
148 #include <instrumentation/syscalls/headers/syscalls_pointers.h>
149 #undef TRACE_INCLUDE_FILE
151 #undef SC_LTTNG_TRACEPOINT_ENUM
152 #undef SC_LTTNG_TRACEPOINT_EVENT_CODE
153 #undef SC_LTTNG_TRACEPOINT_EVENT
154 #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS
155 #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS
157 #undef _TRACE_SYSCALLS_INTEGERS_H
158 #undef _TRACE_SYSCALLS_POINTERS_H
160 /* Hijack probe callback for compat system call enter */
161 #define TP_PROBE_CB(_template) &syscall_entry_probe
162 #define LTTNG_SC_COMPAT
163 #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \
164 LTTNG_TRACEPOINT_EVENT(compat_syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \
166 #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \
167 LTTNG_TRACEPOINT_EVENT_CODE(compat_syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \
168 PARAMS(_locvar), PARAMS(_code_pre), PARAMS(_fields), PARAMS(_code_post))
169 #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
170 LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(compat_syscall_entry_##_name, PARAMS(_fields))
171 #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \
172 LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(compat_syscall_entry_##_template, \
173 compat_syscall_entry_##_name)
174 /* Enumerations only defined at inital inclusion (not here). */
175 #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values)
176 #define TRACE_SYSTEM compat_syscall_entry_integers
177 #define TRACE_INCLUDE_FILE compat_syscalls_integers
178 #include <instrumentation/syscalls/headers/compat_syscalls_integers.h>
179 #undef TRACE_INCLUDE_FILE
181 #define TRACE_SYSTEM compat_syscall_entry_pointers
182 #define TRACE_INCLUDE_FILE compat_syscalls_pointers
183 #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h>
184 #undef TRACE_INCLUDE_FILE
186 #undef SC_LTTNG_TRACEPOINT_ENUM
187 #undef SC_LTTNG_TRACEPOINT_EVENT_CODE
188 #undef SC_LTTNG_TRACEPOINT_EVENT
189 #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS
190 #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS
192 #undef _TRACE_SYSCALLS_INTEGERS_H
193 #undef _TRACE_SYSCALLS_POINTERS_H
194 #undef LTTNG_SC_COMPAT
201 #define sc_exit(...) __VA_ARGS__
205 #define sc_out(...) __VA_ARGS__
207 #define sc_inout(...) __VA_ARGS__
209 /* Hijack probe callback for system call exit */
210 #define TP_PROBE_CB(_template) &syscall_exit_probe
211 #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \
212 LTTNG_TRACEPOINT_EVENT(syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \
214 #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \
215 LTTNG_TRACEPOINT_EVENT_CODE(syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \
216 PARAMS(_locvar), PARAMS(_code_pre), PARAMS(_fields), PARAMS(_code_post))
217 #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
218 LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(syscall_exit_##_name, PARAMS(_fields))
219 #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \
220 LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(syscall_exit_##_template, \
221 syscall_exit_##_name)
222 /* Enumerations only defined at inital inclusion (not here). */
223 #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values)
224 #define TRACE_SYSTEM syscall_exit_integers
225 #define TRACE_INCLUDE_FILE syscalls_integers
226 #include <instrumentation/syscalls/headers/syscalls_integers.h>
227 #undef TRACE_INCLUDE_FILE
229 #define TRACE_SYSTEM syscall_exit_pointers
230 #define TRACE_INCLUDE_FILE syscalls_pointers
231 #include <instrumentation/syscalls/headers/syscalls_pointers.h>
232 #undef TRACE_INCLUDE_FILE
234 #undef SC_LTTNG_TRACEPOINT_ENUM
235 #undef SC_LTTNG_TRACEPOINT_EVENT_CODE
236 #undef SC_LTTNG_TRACEPOINT_EVENT
237 #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS
238 #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS
240 #undef _TRACE_SYSCALLS_INTEGERS_H
241 #undef _TRACE_SYSCALLS_POINTERS_H
244 /* Hijack probe callback for compat system call exit */
245 #define TP_PROBE_CB(_template) &syscall_exit_probe
246 #define LTTNG_SC_COMPAT
247 #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \
248 LTTNG_TRACEPOINT_EVENT(compat_syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \
250 #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \
251 LTTNG_TRACEPOINT_EVENT_CODE(compat_syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \
252 PARAMS(_locvar), PARAMS(_code_pre), PARAMS(_fields), PARAMS(_code_post))
253 #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \
254 LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(compat_syscall_exit_##_name, PARAMS(_fields))
255 #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \
256 LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(compat_syscall_exit_##_template, \
257 compat_syscall_exit_##_name)
258 /* Enumerations only defined at inital inclusion (not here). */
259 #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values)
260 #define TRACE_SYSTEM compat_syscall_exit_integers
261 #define TRACE_INCLUDE_FILE compat_syscalls_integers
262 #include <instrumentation/syscalls/headers/compat_syscalls_integers.h>
263 #undef TRACE_INCLUDE_FILE
265 #define TRACE_SYSTEM compat_syscall_exit_pointers
266 #define TRACE_INCLUDE_FILE compat_syscalls_pointers
267 #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h>
268 #undef TRACE_INCLUDE_FILE
270 #undef SC_LTTNG_TRACEPOINT_ENUM
271 #undef SC_LTTNG_TRACEPOINT_EVENT_CODE
272 #undef SC_LTTNG_TRACEPOINT_EVENT
273 #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS
274 #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS
276 #undef _TRACE_SYSCALLS_INTEGERS_H
277 #undef _TRACE_SYSCALLS_POINTERS_H
278 #undef LTTNG_SC_COMPAT
282 #undef TP_MODULE_NOINIT
283 #undef LTTNG_PACKAGE_BUILD
284 #undef CREATE_TRACE_POINTS
286 struct trace_syscall_entry
{
288 const struct lttng_event_desc
*desc
;
289 const struct lttng_event_field
*fields
;
293 #define CREATE_SYSCALL_TABLE
300 #undef TRACE_SYSCALL_TABLE
301 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
303 .func = __event_probe__syscall_entry_##_template, \
304 .nrargs = (_nrargs), \
305 .fields = __event_fields___syscall_entry_##_template, \
306 .desc = &__event_desc___syscall_entry_##_name, \
309 /* Syscall enter tracing table */
310 static const struct trace_syscall_entry sc_table
[] = {
311 #include <instrumentation/syscalls/headers/syscalls_integers.h>
312 #include <instrumentation/syscalls/headers/syscalls_pointers.h>
315 #undef TRACE_SYSCALL_TABLE
316 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
318 .func = __event_probe__compat_syscall_entry_##_template, \
319 .nrargs = (_nrargs), \
320 .fields = __event_fields___compat_syscall_entry_##_template, \
321 .desc = &__event_desc___compat_syscall_entry_##_name, \
324 /* Compat syscall enter table */
325 const struct trace_syscall_entry compat_sc_table
[] = {
326 #include <instrumentation/syscalls/headers/compat_syscalls_integers.h>
327 #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h>
335 #define sc_exit(...) __VA_ARGS__
337 #undef TRACE_SYSCALL_TABLE
338 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
340 .func = __event_probe__syscall_exit_##_template, \
341 .nrargs = (_nrargs), \
342 .fields = __event_fields___syscall_exit_##_template, \
343 .desc = &__event_desc___syscall_exit_##_name, \
346 /* Syscall exit table */
347 static const struct trace_syscall_entry sc_exit_table
[] = {
348 #include <instrumentation/syscalls/headers/syscalls_integers.h>
349 #include <instrumentation/syscalls/headers/syscalls_pointers.h>
352 #undef TRACE_SYSCALL_TABLE
353 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
355 .func = __event_probe__compat_syscall_exit_##_template, \
356 .nrargs = (_nrargs), \
357 .fields = __event_fields___compat_syscall_exit_##_template, \
358 .desc = &__event_desc___compat_syscall_exit_##_name, \
361 /* Compat syscall exit table */
362 const struct trace_syscall_entry compat_sc_exit_table
[] = {
363 #include <instrumentation/syscalls/headers/compat_syscalls_integers.h>
364 #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h>
369 #undef CREATE_SYSCALL_TABLE
371 struct lttng_syscall_filter
{
372 DECLARE_BITMAP(sc_entry
, NR_syscalls
);
373 DECLARE_BITMAP(sc_exit
, NR_syscalls
);
374 DECLARE_BITMAP(sc_compat_entry
, NR_compat_syscalls
);
375 DECLARE_BITMAP(sc_compat_exit
, NR_compat_syscalls
);
378 static void syscall_entry_unknown(struct lttng_event
*event
,
379 struct pt_regs
*regs
, unsigned int id
)
381 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
383 lttng_syscall_get_arguments(current
, regs
, args
);
384 if (unlikely(in_compat_syscall()))
385 __event_probe__compat_syscall_entry_unknown(event
, id
, args
);
387 __event_probe__syscall_entry_unknown(event
, id
, args
);
390 void syscall_entry_probe(void *__data
, struct pt_regs
*regs
, long id
)
392 struct lttng_channel
*chan
= __data
;
393 struct lttng_event
*event
, *unknown_event
;
394 const struct trace_syscall_entry
*table
, *entry
;
397 if (unlikely(in_compat_syscall())) {
398 struct lttng_syscall_filter
*filter
= chan
->sc_filter
;
400 if (id
< 0 || id
>= NR_compat_syscalls
401 || (!READ_ONCE(chan
->syscall_all
) && !test_bit(id
, filter
->sc_compat_entry
))) {
402 /* System call filtered out. */
405 table
= compat_sc_table
;
406 table_len
= ARRAY_SIZE(compat_sc_table
);
407 unknown_event
= chan
->sc_compat_unknown
;
409 struct lttng_syscall_filter
*filter
= chan
->sc_filter
;
411 if (id
< 0 || id
>= NR_syscalls
412 || (!READ_ONCE(chan
->syscall_all
) && !test_bit(id
, filter
->sc_entry
))) {
413 /* System call filtered out. */
417 table_len
= ARRAY_SIZE(sc_table
);
418 unknown_event
= chan
->sc_unknown
;
420 if (unlikely(id
< 0 || id
>= table_len
)) {
421 syscall_entry_unknown(unknown_event
, regs
, id
);
424 if (unlikely(in_compat_syscall()))
425 event
= chan
->compat_sc_table
[id
];
427 event
= chan
->sc_table
[id
];
428 if (unlikely(!event
)) {
429 syscall_entry_unknown(unknown_event
, regs
, id
);
433 WARN_ON_ONCE(!entry
);
435 switch (entry
->nrargs
) {
438 void (*fptr
)(void *__data
) = entry
->func
;
445 void (*fptr
)(void *__data
, unsigned long arg0
) = entry
->func
;
446 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
448 lttng_syscall_get_arguments(current
, regs
, args
);
449 fptr(event
, args
[0]);
454 void (*fptr
)(void *__data
,
456 unsigned long arg1
) = entry
->func
;
457 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
459 lttng_syscall_get_arguments(current
, regs
, args
);
460 fptr(event
, args
[0], args
[1]);
465 void (*fptr
)(void *__data
,
468 unsigned long arg2
) = entry
->func
;
469 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
471 lttng_syscall_get_arguments(current
, regs
, args
);
472 fptr(event
, args
[0], args
[1], args
[2]);
477 void (*fptr
)(void *__data
,
481 unsigned long arg3
) = entry
->func
;
482 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
484 lttng_syscall_get_arguments(current
, regs
, args
);
485 fptr(event
, args
[0], args
[1], args
[2], args
[3]);
490 void (*fptr
)(void *__data
,
495 unsigned long arg4
) = entry
->func
;
496 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
498 lttng_syscall_get_arguments(current
, regs
, args
);
499 fptr(event
, args
[0], args
[1], args
[2], args
[3], args
[4]);
504 void (*fptr
)(void *__data
,
510 unsigned long arg5
) = entry
->func
;
511 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
513 lttng_syscall_get_arguments(current
, regs
, args
);
514 fptr(event
, args
[0], args
[1], args
[2],
515 args
[3], args
[4], args
[5]);
523 static void syscall_exit_unknown(struct lttng_event
*event
,
524 struct pt_regs
*regs
, int id
, long ret
)
526 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
528 lttng_syscall_get_arguments(current
, regs
, args
);
529 if (unlikely(in_compat_syscall()))
530 __event_probe__compat_syscall_exit_unknown(event
, id
, ret
,
533 __event_probe__syscall_exit_unknown(event
, id
, ret
, args
);
536 void syscall_exit_probe(void *__data
, struct pt_regs
*regs
, long ret
)
538 struct lttng_channel
*chan
= __data
;
539 struct lttng_event
*event
, *unknown_event
;
540 const struct trace_syscall_entry
*table
, *entry
;
544 id
= syscall_get_nr(current
, regs
);
545 if (unlikely(in_compat_syscall())) {
546 struct lttng_syscall_filter
*filter
= chan
->sc_filter
;
548 if (id
< 0 || id
>= NR_compat_syscalls
549 || (!READ_ONCE(chan
->syscall_all
) && !test_bit(id
, filter
->sc_compat_exit
))) {
550 /* System call filtered out. */
553 table
= compat_sc_exit_table
;
554 table_len
= ARRAY_SIZE(compat_sc_exit_table
);
555 unknown_event
= chan
->compat_sc_exit_unknown
;
557 struct lttng_syscall_filter
*filter
= chan
->sc_filter
;
559 if (id
< 0 || id
>= NR_syscalls
560 || (!READ_ONCE(chan
->syscall_all
) && !test_bit(id
, filter
->sc_exit
))) {
561 /* System call filtered out. */
564 table
= sc_exit_table
;
565 table_len
= ARRAY_SIZE(sc_exit_table
);
566 unknown_event
= chan
->sc_exit_unknown
;
568 if (unlikely(id
< 0 || id
>= table_len
)) {
569 syscall_exit_unknown(unknown_event
, regs
, id
, ret
);
572 if (unlikely(in_compat_syscall()))
573 event
= chan
->compat_sc_exit_table
[id
];
575 event
= chan
->sc_exit_table
[id
];
576 if (unlikely(!event
)) {
577 syscall_exit_unknown(unknown_event
, regs
, id
, ret
);
581 WARN_ON_ONCE(!entry
);
583 switch (entry
->nrargs
) {
586 void (*fptr
)(void *__data
, long ret
) = entry
->func
;
593 void (*fptr
)(void *__data
,
595 unsigned long arg0
) = entry
->func
;
596 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
598 lttng_syscall_get_arguments(current
, regs
, args
);
599 fptr(event
, ret
, args
[0]);
604 void (*fptr
)(void *__data
,
607 unsigned long arg1
) = entry
->func
;
608 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
610 lttng_syscall_get_arguments(current
, regs
, args
);
611 fptr(event
, ret
, args
[0], args
[1]);
616 void (*fptr
)(void *__data
,
620 unsigned long arg2
) = entry
->func
;
621 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
623 lttng_syscall_get_arguments(current
, regs
, args
);
624 fptr(event
, ret
, args
[0], args
[1], args
[2]);
629 void (*fptr
)(void *__data
,
634 unsigned long arg3
) = entry
->func
;
635 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
637 lttng_syscall_get_arguments(current
, regs
, args
);
638 fptr(event
, ret
, args
[0], args
[1], args
[2], args
[3]);
643 void (*fptr
)(void *__data
,
649 unsigned long arg4
) = entry
->func
;
650 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
652 lttng_syscall_get_arguments(current
, regs
, args
);
653 fptr(event
, ret
, args
[0], args
[1], args
[2], args
[3], args
[4]);
658 void (*fptr
)(void *__data
,
665 unsigned long arg5
) = entry
->func
;
666 unsigned long args
[LTTNG_SYSCALL_NR_ARGS
];
668 lttng_syscall_get_arguments(current
, regs
, args
);
669 fptr(event
, ret
, args
[0], args
[1], args
[2],
670 args
[3], args
[4], args
[5]);
679 * noinline to diminish caller stack size.
680 * Should be called with sessions lock held.
683 int fill_table(const struct trace_syscall_entry
*table
, size_t table_len
,
684 struct lttng_event
**chan_table
, struct lttng_channel
*chan
,
685 void *filter
, enum sc_type type
)
687 const struct lttng_event_desc
*desc
;
690 /* Allocate events for each syscall, insert into table */
691 for (i
= 0; i
< table_len
; i
++) {
692 struct lttng_kernel_event ev
;
693 desc
= table
[i
].desc
;
696 /* Unknown syscall */
700 * Skip those already populated by previous failed
701 * register for this channel.
705 memset(&ev
, 0, sizeof(ev
));
708 ev
.u
.syscall
.entryexit
= LTTNG_KERNEL_SYSCALL_ENTRY
;
709 ev
.u
.syscall
.abi
= LTTNG_KERNEL_SYSCALL_ABI_NATIVE
;
712 ev
.u
.syscall
.entryexit
= LTTNG_KERNEL_SYSCALL_EXIT
;
713 ev
.u
.syscall
.abi
= LTTNG_KERNEL_SYSCALL_ABI_NATIVE
;
715 case SC_TYPE_COMPAT_ENTRY
:
716 ev
.u
.syscall
.entryexit
= LTTNG_KERNEL_SYSCALL_ENTRY
;
717 ev
.u
.syscall
.abi
= LTTNG_KERNEL_SYSCALL_ABI_COMPAT
;
719 case SC_TYPE_COMPAT_EXIT
:
720 ev
.u
.syscall
.entryexit
= LTTNG_KERNEL_SYSCALL_EXIT
;
721 ev
.u
.syscall
.abi
= LTTNG_KERNEL_SYSCALL_ABI_COMPAT
;
724 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
- 1);
725 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
726 ev
.instrumentation
= LTTNG_KERNEL_SYSCALL
;
727 chan_table
[i
] = _lttng_event_create(chan
, &ev
, filter
,
728 desc
, ev
.instrumentation
);
729 WARN_ON_ONCE(!chan_table
[i
]);
730 if (IS_ERR(chan_table
[i
])) {
732 * If something goes wrong in event registration
733 * after the first one, we have no choice but to
734 * leave the previous events in there, until
735 * deleted by session teardown.
737 return PTR_ERR(chan_table
[i
]);
744 * Should be called with sessions lock held.
746 int lttng_syscalls_register(struct lttng_channel
*chan
, void *filter
)
748 struct lttng_kernel_event ev
;
751 wrapper_vmalloc_sync_mappings();
753 if (!chan
->sc_table
) {
754 /* create syscall table mapping syscall to events */
755 chan
->sc_table
= kzalloc(sizeof(struct lttng_event
*)
756 * ARRAY_SIZE(sc_table
), GFP_KERNEL
);
760 if (!chan
->sc_exit_table
) {
761 /* create syscall table mapping syscall to events */
762 chan
->sc_exit_table
= kzalloc(sizeof(struct lttng_event
*)
763 * ARRAY_SIZE(sc_exit_table
), GFP_KERNEL
);
764 if (!chan
->sc_exit_table
)
770 if (!chan
->compat_sc_table
) {
771 /* create syscall table mapping compat syscall to events */
772 chan
->compat_sc_table
= kzalloc(sizeof(struct lttng_event
*)
773 * ARRAY_SIZE(compat_sc_table
), GFP_KERNEL
);
774 if (!chan
->compat_sc_table
)
778 if (!chan
->compat_sc_exit_table
) {
779 /* create syscall table mapping compat syscall to events */
780 chan
->compat_sc_exit_table
= kzalloc(sizeof(struct lttng_event
*)
781 * ARRAY_SIZE(compat_sc_exit_table
), GFP_KERNEL
);
782 if (!chan
->compat_sc_exit_table
)
786 if (!chan
->sc_unknown
) {
787 const struct lttng_event_desc
*desc
=
788 &__event_desc___syscall_entry_unknown
;
790 memset(&ev
, 0, sizeof(ev
));
791 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
792 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
793 ev
.instrumentation
= LTTNG_KERNEL_SYSCALL
;
794 ev
.u
.syscall
.entryexit
= LTTNG_KERNEL_SYSCALL_ENTRY
;
795 ev
.u
.syscall
.abi
= LTTNG_KERNEL_SYSCALL_ABI_NATIVE
;
796 chan
->sc_unknown
= _lttng_event_create(chan
, &ev
, filter
,
799 WARN_ON_ONCE(!chan
->sc_unknown
);
800 if (IS_ERR(chan
->sc_unknown
)) {
801 return PTR_ERR(chan
->sc_unknown
);
805 if (!chan
->sc_compat_unknown
) {
806 const struct lttng_event_desc
*desc
=
807 &__event_desc___compat_syscall_entry_unknown
;
809 memset(&ev
, 0, sizeof(ev
));
810 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
811 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
812 ev
.instrumentation
= LTTNG_KERNEL_SYSCALL
;
813 ev
.u
.syscall
.entryexit
= LTTNG_KERNEL_SYSCALL_ENTRY
;
814 ev
.u
.syscall
.abi
= LTTNG_KERNEL_SYSCALL_ABI_COMPAT
;
815 chan
->sc_compat_unknown
= _lttng_event_create(chan
, &ev
, filter
,
818 WARN_ON_ONCE(!chan
->sc_unknown
);
819 if (IS_ERR(chan
->sc_compat_unknown
)) {
820 return PTR_ERR(chan
->sc_compat_unknown
);
824 if (!chan
->compat_sc_exit_unknown
) {
825 const struct lttng_event_desc
*desc
=
826 &__event_desc___compat_syscall_exit_unknown
;
828 memset(&ev
, 0, sizeof(ev
));
829 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
830 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
831 ev
.instrumentation
= LTTNG_KERNEL_SYSCALL
;
832 ev
.u
.syscall
.entryexit
= LTTNG_KERNEL_SYSCALL_EXIT
;
833 ev
.u
.syscall
.abi
= LTTNG_KERNEL_SYSCALL_ABI_COMPAT
;
834 chan
->compat_sc_exit_unknown
= _lttng_event_create(chan
, &ev
,
837 WARN_ON_ONCE(!chan
->compat_sc_exit_unknown
);
838 if (IS_ERR(chan
->compat_sc_exit_unknown
)) {
839 return PTR_ERR(chan
->compat_sc_exit_unknown
);
843 if (!chan
->sc_exit_unknown
) {
844 const struct lttng_event_desc
*desc
=
845 &__event_desc___syscall_exit_unknown
;
847 memset(&ev
, 0, sizeof(ev
));
848 strncpy(ev
.name
, desc
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
849 ev
.name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
850 ev
.instrumentation
= LTTNG_KERNEL_SYSCALL
;
851 ev
.u
.syscall
.entryexit
= LTTNG_KERNEL_SYSCALL_EXIT
;
852 ev
.u
.syscall
.abi
= LTTNG_KERNEL_SYSCALL_ABI_NATIVE
;
853 chan
->sc_exit_unknown
= _lttng_event_create(chan
, &ev
, filter
,
854 desc
, ev
.instrumentation
);
855 WARN_ON_ONCE(!chan
->sc_exit_unknown
);
856 if (IS_ERR(chan
->sc_exit_unknown
)) {
857 return PTR_ERR(chan
->sc_exit_unknown
);
861 ret
= fill_table(sc_table
, ARRAY_SIZE(sc_table
),
862 chan
->sc_table
, chan
, filter
, SC_TYPE_ENTRY
);
865 ret
= fill_table(sc_exit_table
, ARRAY_SIZE(sc_exit_table
),
866 chan
->sc_exit_table
, chan
, filter
, SC_TYPE_EXIT
);
871 ret
= fill_table(compat_sc_table
, ARRAY_SIZE(compat_sc_table
),
872 chan
->compat_sc_table
, chan
, filter
,
873 SC_TYPE_COMPAT_ENTRY
);
876 ret
= fill_table(compat_sc_exit_table
, ARRAY_SIZE(compat_sc_exit_table
),
877 chan
->compat_sc_exit_table
, chan
, filter
,
878 SC_TYPE_COMPAT_EXIT
);
883 if (!chan
->sc_filter
) {
884 chan
->sc_filter
= kzalloc(sizeof(struct lttng_syscall_filter
),
886 if (!chan
->sc_filter
)
890 if (!chan
->sys_enter_registered
) {
891 ret
= lttng_wrapper_tracepoint_probe_register("sys_enter",
892 (void *) syscall_entry_probe
, chan
);
895 chan
->sys_enter_registered
= 1;
898 * We change the name of sys_exit tracepoint due to namespace
899 * conflict with sys_exit syscall entry.
901 if (!chan
->sys_exit_registered
) {
902 ret
= lttng_wrapper_tracepoint_probe_register("sys_exit",
903 (void *) syscall_exit_probe
, chan
);
905 WARN_ON_ONCE(lttng_wrapper_tracepoint_probe_unregister("sys_enter",
906 (void *) syscall_entry_probe
, chan
));
909 chan
->sys_exit_registered
= 1;
915 * Only called at session destruction.
917 int lttng_syscalls_unregister(struct lttng_channel
*chan
)
923 if (chan
->sys_enter_registered
) {
924 ret
= lttng_wrapper_tracepoint_probe_unregister("sys_enter",
925 (void *) syscall_entry_probe
, chan
);
928 chan
->sys_enter_registered
= 0;
930 if (chan
->sys_exit_registered
) {
931 ret
= lttng_wrapper_tracepoint_probe_unregister("sys_exit",
932 (void *) syscall_exit_probe
, chan
);
935 chan
->sys_exit_registered
= 0;
940 int lttng_syscalls_destroy(struct lttng_channel
*chan
)
942 kfree(chan
->sc_table
);
943 kfree(chan
->sc_exit_table
);
945 kfree(chan
->compat_sc_table
);
946 kfree(chan
->compat_sc_exit_table
);
948 kfree(chan
->sc_filter
);
953 int get_syscall_nr(const char *syscall_name
)
958 for (i
= 0; i
< ARRAY_SIZE(sc_table
); i
++) {
959 const struct trace_syscall_entry
*entry
;
962 entry
= &sc_table
[i
];
965 it_name
= entry
->desc
->name
;
966 it_name
+= strlen(SYSCALL_ENTRY_STR
);
967 if (!strcmp(syscall_name
, it_name
)) {
976 int get_compat_syscall_nr(const char *syscall_name
)
981 for (i
= 0; i
< ARRAY_SIZE(compat_sc_table
); i
++) {
982 const struct trace_syscall_entry
*entry
;
985 entry
= &compat_sc_table
[i
];
988 it_name
= entry
->desc
->name
;
989 it_name
+= strlen(COMPAT_SYSCALL_ENTRY_STR
);
990 if (!strcmp(syscall_name
, it_name
)) {
999 uint32_t get_sc_tables_len(void)
1001 return ARRAY_SIZE(sc_table
) + ARRAY_SIZE(compat_sc_table
);
1005 const char *get_syscall_name(struct lttng_event
*event
)
1007 size_t prefix_len
= 0;
1009 WARN_ON_ONCE(event
->instrumentation
!= LTTNG_KERNEL_SYSCALL
);
1011 switch (event
->u
.syscall
.entryexit
) {
1012 case LTTNG_SYSCALL_ENTRY
:
1013 switch (event
->u
.syscall
.abi
) {
1014 case LTTNG_SYSCALL_ABI_NATIVE
:
1015 prefix_len
= strlen(SYSCALL_ENTRY_STR
);
1017 case LTTNG_SYSCALL_ABI_COMPAT
:
1018 prefix_len
= strlen(COMPAT_SYSCALL_ENTRY_STR
);
1022 case LTTNG_SYSCALL_EXIT
:
1023 switch (event
->u
.syscall
.abi
) {
1024 case LTTNG_SYSCALL_ABI_NATIVE
:
1025 prefix_len
= strlen(SYSCALL_EXIT_STR
);
1027 case LTTNG_SYSCALL_ABI_COMPAT
:
1028 prefix_len
= strlen(COMPAT_SYSCALL_EXIT_STR
);
1033 WARN_ON_ONCE(prefix_len
== 0);
1034 return event
->desc
->name
+ prefix_len
;
1037 int lttng_syscall_filter_enable(struct lttng_channel
*chan
,
1038 struct lttng_event
*event
)
1040 struct lttng_syscall_filter
*filter
= chan
->sc_filter
;
1041 const char *syscall_name
;
1042 unsigned long *bitmap
;
1045 WARN_ON_ONCE(!chan
->sc_table
);
1047 syscall_name
= get_syscall_name(event
);
1049 switch (event
->u
.syscall
.abi
) {
1050 case LTTNG_SYSCALL_ABI_NATIVE
:
1051 syscall_nr
= get_syscall_nr(syscall_name
);
1053 case LTTNG_SYSCALL_ABI_COMPAT
:
1054 syscall_nr
= get_compat_syscall_nr(syscall_name
);
1062 switch (event
->u
.syscall
.entryexit
) {
1063 case LTTNG_SYSCALL_ENTRY
:
1064 switch (event
->u
.syscall
.abi
) {
1065 case LTTNG_SYSCALL_ABI_NATIVE
:
1066 bitmap
= filter
->sc_entry
;
1068 case LTTNG_SYSCALL_ABI_COMPAT
:
1069 bitmap
= filter
->sc_compat_entry
;
1075 case LTTNG_SYSCALL_EXIT
:
1076 switch (event
->u
.syscall
.abi
) {
1077 case LTTNG_SYSCALL_ABI_NATIVE
:
1078 bitmap
= filter
->sc_exit
;
1080 case LTTNG_SYSCALL_ABI_COMPAT
:
1081 bitmap
= filter
->sc_compat_exit
;
1090 if (test_bit(syscall_nr
, bitmap
))
1092 bitmap_set(bitmap
, syscall_nr
, 1);
1096 int lttng_syscall_filter_disable(struct lttng_channel
*chan
,
1097 struct lttng_event
*event
)
1099 struct lttng_syscall_filter
*filter
= chan
->sc_filter
;
1100 const char *syscall_name
;
1101 unsigned long *bitmap
;
1104 WARN_ON_ONCE(!chan
->sc_table
);
1106 syscall_name
= get_syscall_name(event
);
1108 switch (event
->u
.syscall
.abi
) {
1109 case LTTNG_SYSCALL_ABI_NATIVE
:
1110 syscall_nr
= get_syscall_nr(syscall_name
);
1112 case LTTNG_SYSCALL_ABI_COMPAT
:
1113 syscall_nr
= get_compat_syscall_nr(syscall_name
);
1121 switch (event
->u
.syscall
.entryexit
) {
1122 case LTTNG_SYSCALL_ENTRY
:
1123 switch (event
->u
.syscall
.abi
) {
1124 case LTTNG_SYSCALL_ABI_NATIVE
:
1125 bitmap
= filter
->sc_entry
;
1127 case LTTNG_SYSCALL_ABI_COMPAT
:
1128 bitmap
= filter
->sc_compat_entry
;
1134 case LTTNG_SYSCALL_EXIT
:
1135 switch (event
->u
.syscall
.abi
) {
1136 case LTTNG_SYSCALL_ABI_NATIVE
:
1137 bitmap
= filter
->sc_exit
;
1139 case LTTNG_SYSCALL_ABI_COMPAT
:
1140 bitmap
= filter
->sc_compat_exit
;
1149 if (!test_bit(syscall_nr
, bitmap
))
1151 bitmap_clear(bitmap
, syscall_nr
, 1);
1157 const struct trace_syscall_entry
*syscall_list_get_entry(loff_t
*pos
)
1159 const struct trace_syscall_entry
*entry
;
1162 for (entry
= sc_table
;
1163 entry
< sc_table
+ ARRAY_SIZE(sc_table
);
1168 for (entry
= compat_sc_table
;
1169 entry
< compat_sc_table
+ ARRAY_SIZE(compat_sc_table
);
1179 void *syscall_list_start(struct seq_file
*m
, loff_t
*pos
)
1181 return (void *) syscall_list_get_entry(pos
);
1185 void *syscall_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
1188 return (void *) syscall_list_get_entry(ppos
);
1192 void syscall_list_stop(struct seq_file
*m
, void *p
)
1197 int get_sc_table(const struct trace_syscall_entry
*entry
,
1198 const struct trace_syscall_entry
**table
,
1199 unsigned int *bitness
)
1201 if (entry
>= sc_table
&& entry
< sc_table
+ ARRAY_SIZE(sc_table
)) {
1203 *bitness
= BITS_PER_LONG
;
1208 if (!(entry
>= compat_sc_table
1209 && entry
< compat_sc_table
+ ARRAY_SIZE(compat_sc_table
))) {
1215 *table
= compat_sc_table
;
1220 int syscall_list_show(struct seq_file
*m
, void *p
)
1222 const struct trace_syscall_entry
*table
, *entry
= p
;
1223 unsigned int bitness
;
1224 unsigned long index
;
1228 ret
= get_sc_table(entry
, &table
, &bitness
);
1233 if (table
== sc_table
) {
1234 index
= entry
- table
;
1235 name
= &entry
->desc
->name
[strlen(SYSCALL_ENTRY_STR
)];
1237 index
= (entry
- table
) + ARRAY_SIZE(sc_table
);
1238 name
= &entry
->desc
->name
[strlen(COMPAT_SYSCALL_ENTRY_STR
)];
1240 seq_printf(m
, "syscall { index = %lu; name = %s; bitness = %u; };\n",
1241 index
, name
, bitness
);
1246 const struct seq_operations lttng_syscall_list_seq_ops
= {
1247 .start
= syscall_list_start
,
1248 .next
= syscall_list_next
,
1249 .stop
= syscall_list_stop
,
1250 .show
= syscall_list_show
,
1254 int lttng_syscall_list_open(struct inode
*inode
, struct file
*file
)
1256 return seq_open(file
, <tng_syscall_list_seq_ops
);
1259 const struct file_operations lttng_syscall_list_fops
= {
1260 .owner
= THIS_MODULE
,
1261 .open
= lttng_syscall_list_open
,
1263 .llseek
= seq_lseek
,
1264 .release
= seq_release
,
1268 * A syscall is enabled if it is traced for either entry or exit.
1270 long lttng_channel_syscall_mask(struct lttng_channel
*channel
,
1271 struct lttng_kernel_syscall_mask __user
*usyscall_mask
)
1273 uint32_t len
, sc_tables_len
, bitmask_len
;
1276 struct lttng_syscall_filter
*filter
;
1278 ret
= get_user(len
, &usyscall_mask
->len
);
1281 sc_tables_len
= get_sc_tables_len();
1282 bitmask_len
= ALIGN(sc_tables_len
, 8) >> 3;
1283 if (len
< sc_tables_len
) {
1284 return put_user(sc_tables_len
, &usyscall_mask
->len
);
1286 /* Array is large enough, we can copy array to user-space. */
1287 tmp_mask
= kzalloc(bitmask_len
, GFP_KERNEL
);
1290 filter
= channel
->sc_filter
;
1292 for (bit
= 0; bit
< ARRAY_SIZE(sc_table
); bit
++) {
1295 if (channel
->sc_table
) {
1296 if (!READ_ONCE(channel
->syscall_all
) && filter
)
1297 state
= test_bit(bit
, filter
->sc_entry
)
1298 || test_bit(bit
, filter
->sc_exit
);
1304 bt_bitfield_write_be(tmp_mask
, char, bit
, 1, state
);
1306 for (; bit
< sc_tables_len
; bit
++) {
1309 if (channel
->compat_sc_table
) {
1310 if (!READ_ONCE(channel
->syscall_all
) && filter
)
1311 state
= test_bit(bit
- ARRAY_SIZE(sc_table
),
1312 filter
->sc_compat_entry
)
1313 || test_bit(bit
- ARRAY_SIZE(sc_table
),
1314 filter
->sc_compat_exit
);
1320 bt_bitfield_write_be(tmp_mask
, char, bit
, 1, state
);
1322 if (copy_to_user(usyscall_mask
->mask
, tmp_mask
, bitmask_len
))
1328 int lttng_abi_syscall_list(void)
1330 struct file
*syscall_list_file
;
1333 file_fd
= lttng_get_unused_fd();
1339 syscall_list_file
= anon_inode_getfile("[lttng_syscall_list]",
1340 <tng_syscall_list_fops
,
1342 if (IS_ERR(syscall_list_file
)) {
1343 ret
= PTR_ERR(syscall_list_file
);
1346 ret
= lttng_syscall_list_fops
.open(NULL
, syscall_list_file
);
1349 fd_install(file_fd
, syscall_list_file
);
1353 fput(syscall_list_file
);
1355 put_unused_fd(file_fd
);