Introduce event notifier lib ring buffer client
[lttng-modules.git] / src / lttng-syscalls.c
1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
2 *
3 * lttng-syscalls.c
4 *
5 * LTTng syscall probes.
6 *
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
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>
15 #include <linux/in.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>
25
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>
32
33 #ifndef CONFIG_COMPAT
34 # ifndef is_compat_task
35 # define is_compat_task() (0)
36 # endif
37 #endif
38
39 /* in_compat_syscall appears in kernel 4.6. */
40 #ifndef in_compat_syscall
41 #define in_compat_syscall() is_compat_task()
42 #endif
43
44 enum sc_type {
45 SC_TYPE_ENTRY,
46 SC_TYPE_EXIT,
47 SC_TYPE_COMPAT_ENTRY,
48 SC_TYPE_COMPAT_EXIT,
49 };
50
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_
55
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)
60
61 static
62 void syscall_entry_probe(void *__data, struct pt_regs *regs, long id);
63 static
64 void syscall_exit_probe(void *__data, struct pt_regs *regs, long ret);
65
66 /*
67 * Forward declarations for old kernels.
68 */
69 struct mmsghdr;
70 struct rlimit64;
71 struct oldold_utsname;
72 struct old_utsname;
73 struct sel_arg_struct;
74 struct mmap_arg_struct;
75 struct file_handle;
76 struct user_msghdr;
77
78 /*
79 * Forward declaration for kernels >= 5.6
80 */
81 struct timex;
82 struct timeval;
83 struct itimerval;
84 struct itimerspec;
85
86 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0))
87 typedef __kernel_old_time_t time_t;
88 #endif
89
90 #ifdef IA32_NR_syscalls
91 #define NR_compat_syscalls IA32_NR_syscalls
92 #else
93 #define NR_compat_syscalls NR_syscalls
94 #endif
95
96 /*
97 * Create LTTng tracepoint probes.
98 */
99 #define LTTNG_PACKAGE_BUILD
100 #define CREATE_TRACE_POINTS
101 #define TP_MODULE_NOINIT
102 #define TRACE_INCLUDE_PATH instrumentation/syscalls/headers
103
104 #define PARAMS(args...) args
105
106 /* Handle unknown syscalls */
107 #undef TRACE_SYSTEM
108 #define TRACE_SYSTEM syscalls_unknown
109 #include <instrumentation/syscalls/headers/syscalls_unknown.h>
110 #undef TRACE_SYSTEM
111
112 #define SC_ENTER
113
114 #undef sc_exit
115 #define sc_exit(...)
116 #undef sc_in
117 #define sc_in(...) __VA_ARGS__
118 #undef sc_out
119 #define sc_out(...)
120 #undef sc_inout
121 #define sc_inout(...) __VA_ARGS__
122
123 /* Hijack probe callback for system call enter */
124 #undef TP_PROBE_CB
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), \
128 PARAMS(_fields))
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))
140 #undef TRACE_SYSTEM
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
145 #undef TRACE_SYSTEM
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
150 #undef TRACE_SYSTEM
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
156 #undef TP_PROBE_CB
157 #undef _TRACE_SYSCALLS_INTEGERS_H
158 #undef _TRACE_SYSCALLS_POINTERS_H
159
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), \
165 PARAMS(_fields))
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
180 #undef TRACE_SYSTEM
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
185 #undef TRACE_SYSTEM
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
191 #undef TP_PROBE_CB
192 #undef _TRACE_SYSCALLS_INTEGERS_H
193 #undef _TRACE_SYSCALLS_POINTERS_H
194 #undef LTTNG_SC_COMPAT
195
196 #undef SC_ENTER
197
198 #define SC_EXIT
199
200 #undef sc_exit
201 #define sc_exit(...) __VA_ARGS__
202 #undef sc_in
203 #define sc_in(...)
204 #undef sc_out
205 #define sc_out(...) __VA_ARGS__
206 #undef sc_inout
207 #define sc_inout(...) __VA_ARGS__
208
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), \
213 PARAMS(_fields))
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
228 #undef TRACE_SYSTEM
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
233 #undef TRACE_SYSTEM
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
239 #undef TP_PROBE_CB
240 #undef _TRACE_SYSCALLS_INTEGERS_H
241 #undef _TRACE_SYSCALLS_POINTERS_H
242
243
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), \
249 PARAMS(_fields))
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
264 #undef TRACE_SYSTEM
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
269 #undef TRACE_SYSTEM
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
275 #undef TP_PROBE_CB
276 #undef _TRACE_SYSCALLS_INTEGERS_H
277 #undef _TRACE_SYSCALLS_POINTERS_H
278 #undef LTTNG_SC_COMPAT
279
280 #undef SC_EXIT
281
282 #undef TP_MODULE_NOINIT
283 #undef LTTNG_PACKAGE_BUILD
284 #undef CREATE_TRACE_POINTS
285
286 struct trace_syscall_entry {
287 void *func;
288 const struct lttng_event_desc *desc;
289 const struct lttng_event_field *fields;
290 unsigned int nrargs;
291 };
292
293 #define CREATE_SYSCALL_TABLE
294
295 #define SC_ENTER
296
297 #undef sc_exit
298 #define sc_exit(...)
299
300 #undef TRACE_SYSCALL_TABLE
301 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
302 [ _nr ] = { \
303 .func = __event_probe__syscall_entry_##_template, \
304 .nrargs = (_nrargs), \
305 .fields = __event_fields___syscall_entry_##_template, \
306 .desc = &__event_desc___syscall_entry_##_name, \
307 },
308
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>
313 };
314
315 #undef TRACE_SYSCALL_TABLE
316 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
317 [ _nr ] = { \
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, \
322 },
323
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>
328 };
329
330 #undef SC_ENTER
331
332 #define SC_EXIT
333
334 #undef sc_exit
335 #define sc_exit(...) __VA_ARGS__
336
337 #undef TRACE_SYSCALL_TABLE
338 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
339 [ _nr ] = { \
340 .func = __event_probe__syscall_exit_##_template, \
341 .nrargs = (_nrargs), \
342 .fields = __event_fields___syscall_exit_##_template, \
343 .desc = &__event_desc___syscall_exit_##_name, \
344 },
345
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>
350 };
351
352 #undef TRACE_SYSCALL_TABLE
353 #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \
354 [ _nr ] = { \
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, \
359 },
360
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>
365 };
366
367 #undef SC_EXIT
368
369 #undef CREATE_SYSCALL_TABLE
370
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);
376 };
377
378 static void syscall_entry_unknown(struct lttng_event *event,
379 struct pt_regs *regs, unsigned int id)
380 {
381 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
382
383 lttng_syscall_get_arguments(current, regs, args);
384 if (unlikely(in_compat_syscall()))
385 __event_probe__compat_syscall_entry_unknown(event, id, args);
386 else
387 __event_probe__syscall_entry_unknown(event, id, args);
388 }
389
390 void syscall_entry_probe(void *__data, struct pt_regs *regs, long id)
391 {
392 struct lttng_channel *chan = __data;
393 struct lttng_event *event, *unknown_event;
394 const struct trace_syscall_entry *table, *entry;
395 size_t table_len;
396
397 if (unlikely(in_compat_syscall())) {
398 struct lttng_syscall_filter *filter = chan->sc_filter;
399
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. */
403 return;
404 }
405 table = compat_sc_table;
406 table_len = ARRAY_SIZE(compat_sc_table);
407 unknown_event = chan->sc_compat_unknown;
408 } else {
409 struct lttng_syscall_filter *filter = chan->sc_filter;
410
411 if (id < 0 || id >= NR_syscalls
412 || (!READ_ONCE(chan->syscall_all) && !test_bit(id, filter->sc_entry))) {
413 /* System call filtered out. */
414 return;
415 }
416 table = sc_table;
417 table_len = ARRAY_SIZE(sc_table);
418 unknown_event = chan->sc_unknown;
419 }
420 if (unlikely(id < 0 || id >= table_len)) {
421 syscall_entry_unknown(unknown_event, regs, id);
422 return;
423 }
424 if (unlikely(in_compat_syscall()))
425 event = chan->compat_sc_table[id];
426 else
427 event = chan->sc_table[id];
428 if (unlikely(!event)) {
429 syscall_entry_unknown(unknown_event, regs, id);
430 return;
431 }
432 entry = &table[id];
433 WARN_ON_ONCE(!entry);
434
435 switch (entry->nrargs) {
436 case 0:
437 {
438 void (*fptr)(void *__data) = entry->func;
439
440 fptr(event);
441 break;
442 }
443 case 1:
444 {
445 void (*fptr)(void *__data, unsigned long arg0) = entry->func;
446 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
447
448 lttng_syscall_get_arguments(current, regs, args);
449 fptr(event, args[0]);
450 break;
451 }
452 case 2:
453 {
454 void (*fptr)(void *__data,
455 unsigned long arg0,
456 unsigned long arg1) = entry->func;
457 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
458
459 lttng_syscall_get_arguments(current, regs, args);
460 fptr(event, args[0], args[1]);
461 break;
462 }
463 case 3:
464 {
465 void (*fptr)(void *__data,
466 unsigned long arg0,
467 unsigned long arg1,
468 unsigned long arg2) = entry->func;
469 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
470
471 lttng_syscall_get_arguments(current, regs, args);
472 fptr(event, args[0], args[1], args[2]);
473 break;
474 }
475 case 4:
476 {
477 void (*fptr)(void *__data,
478 unsigned long arg0,
479 unsigned long arg1,
480 unsigned long arg2,
481 unsigned long arg3) = entry->func;
482 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
483
484 lttng_syscall_get_arguments(current, regs, args);
485 fptr(event, args[0], args[1], args[2], args[3]);
486 break;
487 }
488 case 5:
489 {
490 void (*fptr)(void *__data,
491 unsigned long arg0,
492 unsigned long arg1,
493 unsigned long arg2,
494 unsigned long arg3,
495 unsigned long arg4) = entry->func;
496 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
497
498 lttng_syscall_get_arguments(current, regs, args);
499 fptr(event, args[0], args[1], args[2], args[3], args[4]);
500 break;
501 }
502 case 6:
503 {
504 void (*fptr)(void *__data,
505 unsigned long arg0,
506 unsigned long arg1,
507 unsigned long arg2,
508 unsigned long arg3,
509 unsigned long arg4,
510 unsigned long arg5) = entry->func;
511 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
512
513 lttng_syscall_get_arguments(current, regs, args);
514 fptr(event, args[0], args[1], args[2],
515 args[3], args[4], args[5]);
516 break;
517 }
518 default:
519 break;
520 }
521 }
522
523 static void syscall_exit_unknown(struct lttng_event *event,
524 struct pt_regs *regs, int id, long ret)
525 {
526 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
527
528 lttng_syscall_get_arguments(current, regs, args);
529 if (unlikely(in_compat_syscall()))
530 __event_probe__compat_syscall_exit_unknown(event, id, ret,
531 args);
532 else
533 __event_probe__syscall_exit_unknown(event, id, ret, args);
534 }
535
536 void syscall_exit_probe(void *__data, struct pt_regs *regs, long ret)
537 {
538 struct lttng_channel *chan = __data;
539 struct lttng_event *event, *unknown_event;
540 const struct trace_syscall_entry *table, *entry;
541 size_t table_len;
542 long id;
543
544 id = syscall_get_nr(current, regs);
545 if (unlikely(in_compat_syscall())) {
546 struct lttng_syscall_filter *filter = chan->sc_filter;
547
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. */
551 return;
552 }
553 table = compat_sc_exit_table;
554 table_len = ARRAY_SIZE(compat_sc_exit_table);
555 unknown_event = chan->compat_sc_exit_unknown;
556 } else {
557 struct lttng_syscall_filter *filter = chan->sc_filter;
558
559 if (id < 0 || id >= NR_syscalls
560 || (!READ_ONCE(chan->syscall_all) && !test_bit(id, filter->sc_exit))) {
561 /* System call filtered out. */
562 return;
563 }
564 table = sc_exit_table;
565 table_len = ARRAY_SIZE(sc_exit_table);
566 unknown_event = chan->sc_exit_unknown;
567 }
568 if (unlikely(id < 0 || id >= table_len)) {
569 syscall_exit_unknown(unknown_event, regs, id, ret);
570 return;
571 }
572 if (unlikely(in_compat_syscall()))
573 event = chan->compat_sc_exit_table[id];
574 else
575 event = chan->sc_exit_table[id];
576 if (unlikely(!event)) {
577 syscall_exit_unknown(unknown_event, regs, id, ret);
578 return;
579 }
580 entry = &table[id];
581 WARN_ON_ONCE(!entry);
582
583 switch (entry->nrargs) {
584 case 0:
585 {
586 void (*fptr)(void *__data, long ret) = entry->func;
587
588 fptr(event, ret);
589 break;
590 }
591 case 1:
592 {
593 void (*fptr)(void *__data,
594 long ret,
595 unsigned long arg0) = entry->func;
596 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
597
598 lttng_syscall_get_arguments(current, regs, args);
599 fptr(event, ret, args[0]);
600 break;
601 }
602 case 2:
603 {
604 void (*fptr)(void *__data,
605 long ret,
606 unsigned long arg0,
607 unsigned long arg1) = entry->func;
608 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
609
610 lttng_syscall_get_arguments(current, regs, args);
611 fptr(event, ret, args[0], args[1]);
612 break;
613 }
614 case 3:
615 {
616 void (*fptr)(void *__data,
617 long ret,
618 unsigned long arg0,
619 unsigned long arg1,
620 unsigned long arg2) = entry->func;
621 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
622
623 lttng_syscall_get_arguments(current, regs, args);
624 fptr(event, ret, args[0], args[1], args[2]);
625 break;
626 }
627 case 4:
628 {
629 void (*fptr)(void *__data,
630 long ret,
631 unsigned long arg0,
632 unsigned long arg1,
633 unsigned long arg2,
634 unsigned long arg3) = entry->func;
635 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
636
637 lttng_syscall_get_arguments(current, regs, args);
638 fptr(event, ret, args[0], args[1], args[2], args[3]);
639 break;
640 }
641 case 5:
642 {
643 void (*fptr)(void *__data,
644 long ret,
645 unsigned long arg0,
646 unsigned long arg1,
647 unsigned long arg2,
648 unsigned long arg3,
649 unsigned long arg4) = entry->func;
650 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
651
652 lttng_syscall_get_arguments(current, regs, args);
653 fptr(event, ret, args[0], args[1], args[2], args[3], args[4]);
654 break;
655 }
656 case 6:
657 {
658 void (*fptr)(void *__data,
659 long ret,
660 unsigned long arg0,
661 unsigned long arg1,
662 unsigned long arg2,
663 unsigned long arg3,
664 unsigned long arg4,
665 unsigned long arg5) = entry->func;
666 unsigned long args[LTTNG_SYSCALL_NR_ARGS];
667
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]);
671 break;
672 }
673 default:
674 break;
675 }
676 }
677
678 /*
679 * noinline to diminish caller stack size.
680 * Should be called with sessions lock held.
681 */
682 static
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)
686 {
687 const struct lttng_event_desc *desc;
688 unsigned int i;
689
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;
694
695 if (!desc) {
696 /* Unknown syscall */
697 continue;
698 }
699 /*
700 * Skip those already populated by previous failed
701 * register for this channel.
702 */
703 if (chan_table[i])
704 continue;
705 memset(&ev, 0, sizeof(ev));
706 switch (type) {
707 case SC_TYPE_ENTRY:
708 ev.u.syscall.entryexit = LTTNG_KERNEL_SYSCALL_ENTRY;
709 ev.u.syscall.abi = LTTNG_KERNEL_SYSCALL_ABI_NATIVE;
710 break;
711 case SC_TYPE_EXIT:
712 ev.u.syscall.entryexit = LTTNG_KERNEL_SYSCALL_EXIT;
713 ev.u.syscall.abi = LTTNG_KERNEL_SYSCALL_ABI_NATIVE;
714 break;
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;
718 break;
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;
722 break;
723 }
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])) {
731 /*
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.
736 */
737 return PTR_ERR(chan_table[i]);
738 }
739 }
740 return 0;
741 }
742
743 /*
744 * Should be called with sessions lock held.
745 */
746 int lttng_syscalls_register(struct lttng_channel *chan, void *filter)
747 {
748 struct lttng_kernel_event ev;
749 int ret;
750
751 wrapper_vmalloc_sync_mappings();
752
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);
757 if (!chan->sc_table)
758 return -ENOMEM;
759 }
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)
765 return -ENOMEM;
766 }
767
768
769 #ifdef CONFIG_COMPAT
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)
775 return -ENOMEM;
776 }
777
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)
783 return -ENOMEM;
784 }
785 #endif
786 if (!chan->sc_unknown) {
787 const struct lttng_event_desc *desc =
788 &__event_desc___syscall_entry_unknown;
789
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,
797 desc,
798 ev.instrumentation);
799 WARN_ON_ONCE(!chan->sc_unknown);
800 if (IS_ERR(chan->sc_unknown)) {
801 return PTR_ERR(chan->sc_unknown);
802 }
803 }
804
805 if (!chan->sc_compat_unknown) {
806 const struct lttng_event_desc *desc =
807 &__event_desc___compat_syscall_entry_unknown;
808
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,
816 desc,
817 ev.instrumentation);
818 WARN_ON_ONCE(!chan->sc_unknown);
819 if (IS_ERR(chan->sc_compat_unknown)) {
820 return PTR_ERR(chan->sc_compat_unknown);
821 }
822 }
823
824 if (!chan->compat_sc_exit_unknown) {
825 const struct lttng_event_desc *desc =
826 &__event_desc___compat_syscall_exit_unknown;
827
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,
835 filter, desc,
836 ev.instrumentation);
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);
840 }
841 }
842
843 if (!chan->sc_exit_unknown) {
844 const struct lttng_event_desc *desc =
845 &__event_desc___syscall_exit_unknown;
846
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);
858 }
859 }
860
861 ret = fill_table(sc_table, ARRAY_SIZE(sc_table),
862 chan->sc_table, chan, filter, SC_TYPE_ENTRY);
863 if (ret)
864 return ret;
865 ret = fill_table(sc_exit_table, ARRAY_SIZE(sc_exit_table),
866 chan->sc_exit_table, chan, filter, SC_TYPE_EXIT);
867 if (ret)
868 return ret;
869
870 #ifdef CONFIG_COMPAT
871 ret = fill_table(compat_sc_table, ARRAY_SIZE(compat_sc_table),
872 chan->compat_sc_table, chan, filter,
873 SC_TYPE_COMPAT_ENTRY);
874 if (ret)
875 return ret;
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);
879 if (ret)
880 return ret;
881 #endif
882
883 if (!chan->sc_filter) {
884 chan->sc_filter = kzalloc(sizeof(struct lttng_syscall_filter),
885 GFP_KERNEL);
886 if (!chan->sc_filter)
887 return -ENOMEM;
888 }
889
890 if (!chan->sys_enter_registered) {
891 ret = lttng_wrapper_tracepoint_probe_register("sys_enter",
892 (void *) syscall_entry_probe, chan);
893 if (ret)
894 return ret;
895 chan->sys_enter_registered = 1;
896 }
897 /*
898 * We change the name of sys_exit tracepoint due to namespace
899 * conflict with sys_exit syscall entry.
900 */
901 if (!chan->sys_exit_registered) {
902 ret = lttng_wrapper_tracepoint_probe_register("sys_exit",
903 (void *) syscall_exit_probe, chan);
904 if (ret) {
905 WARN_ON_ONCE(lttng_wrapper_tracepoint_probe_unregister("sys_enter",
906 (void *) syscall_entry_probe, chan));
907 return ret;
908 }
909 chan->sys_exit_registered = 1;
910 }
911 return ret;
912 }
913
914 /*
915 * Only called at session destruction.
916 */
917 int lttng_syscalls_unregister(struct lttng_channel *chan)
918 {
919 int ret;
920
921 if (!chan->sc_table)
922 return 0;
923 if (chan->sys_enter_registered) {
924 ret = lttng_wrapper_tracepoint_probe_unregister("sys_enter",
925 (void *) syscall_entry_probe, chan);
926 if (ret)
927 return ret;
928 chan->sys_enter_registered = 0;
929 }
930 if (chan->sys_exit_registered) {
931 ret = lttng_wrapper_tracepoint_probe_unregister("sys_exit",
932 (void *) syscall_exit_probe, chan);
933 if (ret)
934 return ret;
935 chan->sys_exit_registered = 0;
936 }
937 return 0;
938 }
939
940 int lttng_syscalls_destroy(struct lttng_channel *chan)
941 {
942 kfree(chan->sc_table);
943 kfree(chan->sc_exit_table);
944 #ifdef CONFIG_COMPAT
945 kfree(chan->compat_sc_table);
946 kfree(chan->compat_sc_exit_table);
947 #endif
948 kfree(chan->sc_filter);
949 return 0;
950 }
951
952 static
953 int get_syscall_nr(const char *syscall_name)
954 {
955 int syscall_nr = -1;
956 int i;
957
958 for (i = 0; i < ARRAY_SIZE(sc_table); i++) {
959 const struct trace_syscall_entry *entry;
960 const char *it_name;
961
962 entry = &sc_table[i];
963 if (!entry->desc)
964 continue;
965 it_name = entry->desc->name;
966 it_name += strlen(SYSCALL_ENTRY_STR);
967 if (!strcmp(syscall_name, it_name)) {
968 syscall_nr = i;
969 break;
970 }
971 }
972 return syscall_nr;
973 }
974
975 static
976 int get_compat_syscall_nr(const char *syscall_name)
977 {
978 int syscall_nr = -1;
979 int i;
980
981 for (i = 0; i < ARRAY_SIZE(compat_sc_table); i++) {
982 const struct trace_syscall_entry *entry;
983 const char *it_name;
984
985 entry = &compat_sc_table[i];
986 if (!entry->desc)
987 continue;
988 it_name = entry->desc->name;
989 it_name += strlen(COMPAT_SYSCALL_ENTRY_STR);
990 if (!strcmp(syscall_name, it_name)) {
991 syscall_nr = i;
992 break;
993 }
994 }
995 return syscall_nr;
996 }
997
998 static
999 uint32_t get_sc_tables_len(void)
1000 {
1001 return ARRAY_SIZE(sc_table) + ARRAY_SIZE(compat_sc_table);
1002 }
1003
1004 static
1005 const char *get_syscall_name(struct lttng_event *event)
1006 {
1007 size_t prefix_len = 0;
1008
1009 WARN_ON_ONCE(event->instrumentation != LTTNG_KERNEL_SYSCALL);
1010
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);
1016 break;
1017 case LTTNG_SYSCALL_ABI_COMPAT:
1018 prefix_len = strlen(COMPAT_SYSCALL_ENTRY_STR);
1019 break;
1020 }
1021 break;
1022 case LTTNG_SYSCALL_EXIT:
1023 switch (event->u.syscall.abi) {
1024 case LTTNG_SYSCALL_ABI_NATIVE:
1025 prefix_len = strlen(SYSCALL_EXIT_STR);
1026 break;
1027 case LTTNG_SYSCALL_ABI_COMPAT:
1028 prefix_len = strlen(COMPAT_SYSCALL_EXIT_STR);
1029 break;
1030 }
1031 break;
1032 }
1033 WARN_ON_ONCE(prefix_len == 0);
1034 return event->desc->name + prefix_len;
1035 }
1036
1037 int lttng_syscall_filter_enable(struct lttng_channel *chan,
1038 struct lttng_event *event)
1039 {
1040 struct lttng_syscall_filter *filter = chan->sc_filter;
1041 const char *syscall_name;
1042 unsigned long *bitmap;
1043 int syscall_nr;
1044
1045 WARN_ON_ONCE(!chan->sc_table);
1046
1047 syscall_name = get_syscall_name(event);
1048
1049 switch (event->u.syscall.abi) {
1050 case LTTNG_SYSCALL_ABI_NATIVE:
1051 syscall_nr = get_syscall_nr(syscall_name);
1052 break;
1053 case LTTNG_SYSCALL_ABI_COMPAT:
1054 syscall_nr = get_compat_syscall_nr(syscall_name);
1055 break;
1056 default:
1057 return -EINVAL;
1058 }
1059 if (syscall_nr < 0)
1060 return -ENOENT;
1061
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;
1067 break;
1068 case LTTNG_SYSCALL_ABI_COMPAT:
1069 bitmap = filter->sc_compat_entry;
1070 break;
1071 default:
1072 return -EINVAL;
1073 }
1074 break;
1075 case LTTNG_SYSCALL_EXIT:
1076 switch (event->u.syscall.abi) {
1077 case LTTNG_SYSCALL_ABI_NATIVE:
1078 bitmap = filter->sc_exit;
1079 break;
1080 case LTTNG_SYSCALL_ABI_COMPAT:
1081 bitmap = filter->sc_compat_exit;
1082 break;
1083 default:
1084 return -EINVAL;
1085 }
1086 break;
1087 default:
1088 return -EINVAL;
1089 }
1090 if (test_bit(syscall_nr, bitmap))
1091 return -EEXIST;
1092 bitmap_set(bitmap, syscall_nr, 1);
1093 return 0;
1094 }
1095
1096 int lttng_syscall_filter_disable(struct lttng_channel *chan,
1097 struct lttng_event *event)
1098 {
1099 struct lttng_syscall_filter *filter = chan->sc_filter;
1100 const char *syscall_name;
1101 unsigned long *bitmap;
1102 int syscall_nr;
1103
1104 WARN_ON_ONCE(!chan->sc_table);
1105
1106 syscall_name = get_syscall_name(event);
1107
1108 switch (event->u.syscall.abi) {
1109 case LTTNG_SYSCALL_ABI_NATIVE:
1110 syscall_nr = get_syscall_nr(syscall_name);
1111 break;
1112 case LTTNG_SYSCALL_ABI_COMPAT:
1113 syscall_nr = get_compat_syscall_nr(syscall_name);
1114 break;
1115 default:
1116 return -EINVAL;
1117 }
1118 if (syscall_nr < 0)
1119 return -ENOENT;
1120
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;
1126 break;
1127 case LTTNG_SYSCALL_ABI_COMPAT:
1128 bitmap = filter->sc_compat_entry;
1129 break;
1130 default:
1131 return -EINVAL;
1132 }
1133 break;
1134 case LTTNG_SYSCALL_EXIT:
1135 switch (event->u.syscall.abi) {
1136 case LTTNG_SYSCALL_ABI_NATIVE:
1137 bitmap = filter->sc_exit;
1138 break;
1139 case LTTNG_SYSCALL_ABI_COMPAT:
1140 bitmap = filter->sc_compat_exit;
1141 break;
1142 default:
1143 return -EINVAL;
1144 }
1145 break;
1146 default:
1147 return -EINVAL;
1148 }
1149 if (!test_bit(syscall_nr, bitmap))
1150 return -EEXIST;
1151 bitmap_clear(bitmap, syscall_nr, 1);
1152
1153 return 0;
1154 }
1155
1156 static
1157 const struct trace_syscall_entry *syscall_list_get_entry(loff_t *pos)
1158 {
1159 const struct trace_syscall_entry *entry;
1160 int iter = 0;
1161
1162 for (entry = sc_table;
1163 entry < sc_table + ARRAY_SIZE(sc_table);
1164 entry++) {
1165 if (iter++ >= *pos)
1166 return entry;
1167 }
1168 for (entry = compat_sc_table;
1169 entry < compat_sc_table + ARRAY_SIZE(compat_sc_table);
1170 entry++) {
1171 if (iter++ >= *pos)
1172 return entry;
1173 }
1174 /* End of list */
1175 return NULL;
1176 }
1177
1178 static
1179 void *syscall_list_start(struct seq_file *m, loff_t *pos)
1180 {
1181 return (void *) syscall_list_get_entry(pos);
1182 }
1183
1184 static
1185 void *syscall_list_next(struct seq_file *m, void *p, loff_t *ppos)
1186 {
1187 (*ppos)++;
1188 return (void *) syscall_list_get_entry(ppos);
1189 }
1190
1191 static
1192 void syscall_list_stop(struct seq_file *m, void *p)
1193 {
1194 }
1195
1196 static
1197 int get_sc_table(const struct trace_syscall_entry *entry,
1198 const struct trace_syscall_entry **table,
1199 unsigned int *bitness)
1200 {
1201 if (entry >= sc_table && entry < sc_table + ARRAY_SIZE(sc_table)) {
1202 if (bitness)
1203 *bitness = BITS_PER_LONG;
1204 if (table)
1205 *table = sc_table;
1206 return 0;
1207 }
1208 if (!(entry >= compat_sc_table
1209 && entry < compat_sc_table + ARRAY_SIZE(compat_sc_table))) {
1210 return -EINVAL;
1211 }
1212 if (bitness)
1213 *bitness = 32;
1214 if (table)
1215 *table = compat_sc_table;
1216 return 0;
1217 }
1218
1219 static
1220 int syscall_list_show(struct seq_file *m, void *p)
1221 {
1222 const struct trace_syscall_entry *table, *entry = p;
1223 unsigned int bitness;
1224 unsigned long index;
1225 int ret;
1226 const char *name;
1227
1228 ret = get_sc_table(entry, &table, &bitness);
1229 if (ret)
1230 return ret;
1231 if (!entry->desc)
1232 return 0;
1233 if (table == sc_table) {
1234 index = entry - table;
1235 name = &entry->desc->name[strlen(SYSCALL_ENTRY_STR)];
1236 } else {
1237 index = (entry - table) + ARRAY_SIZE(sc_table);
1238 name = &entry->desc->name[strlen(COMPAT_SYSCALL_ENTRY_STR)];
1239 }
1240 seq_printf(m, "syscall { index = %lu; name = %s; bitness = %u; };\n",
1241 index, name, bitness);
1242 return 0;
1243 }
1244
1245 static
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,
1251 };
1252
1253 static
1254 int lttng_syscall_list_open(struct inode *inode, struct file *file)
1255 {
1256 return seq_open(file, &lttng_syscall_list_seq_ops);
1257 }
1258
1259 const struct file_operations lttng_syscall_list_fops = {
1260 .owner = THIS_MODULE,
1261 .open = lttng_syscall_list_open,
1262 .read = seq_read,
1263 .llseek = seq_lseek,
1264 .release = seq_release,
1265 };
1266
1267 /*
1268 * A syscall is enabled if it is traced for either entry or exit.
1269 */
1270 long lttng_channel_syscall_mask(struct lttng_channel *channel,
1271 struct lttng_kernel_syscall_mask __user *usyscall_mask)
1272 {
1273 uint32_t len, sc_tables_len, bitmask_len;
1274 int ret = 0, bit;
1275 char *tmp_mask;
1276 struct lttng_syscall_filter *filter;
1277
1278 ret = get_user(len, &usyscall_mask->len);
1279 if (ret)
1280 return ret;
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);
1285 }
1286 /* Array is large enough, we can copy array to user-space. */
1287 tmp_mask = kzalloc(bitmask_len, GFP_KERNEL);
1288 if (!tmp_mask)
1289 return -ENOMEM;
1290 filter = channel->sc_filter;
1291
1292 for (bit = 0; bit < ARRAY_SIZE(sc_table); bit++) {
1293 char state;
1294
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);
1299 else
1300 state = 1;
1301 } else {
1302 state = 0;
1303 }
1304 bt_bitfield_write_be(tmp_mask, char, bit, 1, state);
1305 }
1306 for (; bit < sc_tables_len; bit++) {
1307 char state;
1308
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);
1315 else
1316 state = 1;
1317 } else {
1318 state = 0;
1319 }
1320 bt_bitfield_write_be(tmp_mask, char, bit, 1, state);
1321 }
1322 if (copy_to_user(usyscall_mask->mask, tmp_mask, bitmask_len))
1323 ret = -EFAULT;
1324 kfree(tmp_mask);
1325 return ret;
1326 }
1327
1328 int lttng_abi_syscall_list(void)
1329 {
1330 struct file *syscall_list_file;
1331 int file_fd, ret;
1332
1333 file_fd = lttng_get_unused_fd();
1334 if (file_fd < 0) {
1335 ret = file_fd;
1336 goto fd_error;
1337 }
1338
1339 syscall_list_file = anon_inode_getfile("[lttng_syscall_list]",
1340 &lttng_syscall_list_fops,
1341 NULL, O_RDWR);
1342 if (IS_ERR(syscall_list_file)) {
1343 ret = PTR_ERR(syscall_list_file);
1344 goto file_error;
1345 }
1346 ret = lttng_syscall_list_fops.open(NULL, syscall_list_file);
1347 if (ret < 0)
1348 goto open_error;
1349 fd_install(file_fd, syscall_list_file);
1350 return file_fd;
1351
1352 open_error:
1353 fput(syscall_list_file);
1354 file_error:
1355 put_unused_fd(file_fd);
1356 fd_error:
1357 return ret;
1358 }
This page took 0.075899 seconds and 4 git commands to generate.