Commit | Line | Data |
---|---|---|
9f36eaed MJ |
1 | /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1) |
2 | * | |
259b6cb3 MD |
3 | * lttng-syscalls.c |
4 | * | |
2faf7d1b | 5 | * LTTng syscall probes. |
259b6cb3 | 6 | * |
886d51a3 | 7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
259b6cb3 MD |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/slab.h> | |
6333ace3 | 12 | #include <linux/compat.h> |
abc0446a | 13 | #include <linux/err.h> |
80f87dd2 | 14 | #include <linux/bitmap.h> |
7ca580f8 MD |
15 | #include <linux/in.h> |
16 | #include <linux/in6.h> | |
2d2464bd | 17 | #include <linux/seq_file.h> |
d4291869 | 18 | #include <linux/stringify.h> |
082d4946 MD |
19 | #include <linux/file.h> |
20 | #include <linux/anon_inodes.h> | |
259b6cb3 MD |
21 | #include <asm/ptrace.h> |
22 | #include <asm/syscall.h> | |
23 | ||
241ae9a8 MD |
24 | #include <lib/bitfield.h> |
25 | #include <wrapper/tracepoint.h> | |
26 | #include <wrapper/file.h> | |
27 | #include <wrapper/rcu.h> | |
9f70b60c | 28 | #include <wrapper/syscall.h> |
241ae9a8 | 29 | #include <lttng-events.h> |
259b6cb3 | 30 | |
6333ace3 | 31 | #ifndef CONFIG_COMPAT |
bfa949bf MD |
32 | # ifndef is_compat_task |
33 | # define is_compat_task() (0) | |
34 | # endif | |
6333ace3 MD |
35 | #endif |
36 | ||
1aa3298b MD |
37 | /* in_compat_syscall appears in kernel 4.6. */ |
38 | #ifndef in_compat_syscall | |
39 | #define in_compat_syscall() is_compat_task() | |
40 | #endif | |
41 | ||
5b7ac358 MD |
42 | enum sc_type { |
43 | SC_TYPE_ENTRY, | |
44 | SC_TYPE_EXIT, | |
45 | SC_TYPE_COMPAT_ENTRY, | |
46 | SC_TYPE_COMPAT_EXIT, | |
47 | }; | |
48 | ||
d4291869 MD |
49 | #define SYSCALL_ENTRY_TOK syscall_entry_ |
50 | #define COMPAT_SYSCALL_ENTRY_TOK compat_syscall_entry_ | |
51 | #define SYSCALL_EXIT_TOK syscall_exit_ | |
52 | #define COMPAT_SYSCALL_EXIT_TOK compat_syscall_exit_ | |
53 | ||
54 | #define SYSCALL_ENTRY_STR __stringify(SYSCALL_ENTRY_TOK) | |
55 | #define COMPAT_SYSCALL_ENTRY_STR __stringify(COMPAT_SYSCALL_ENTRY_TOK) | |
56 | #define SYSCALL_EXIT_STR __stringify(SYSCALL_EXIT_TOK) | |
57 | #define COMPAT_SYSCALL_EXIT_STR __stringify(COMPAT_SYSCALL_EXIT_TOK) | |
5b7ac358 | 58 | |
a93244f8 | 59 | static |
2faf7d1b | 60 | void syscall_entry_probe(void *__data, struct pt_regs *regs, long id); |
5b7ac358 MD |
61 | static |
62 | void syscall_exit_probe(void *__data, struct pt_regs *regs, long ret); | |
259b6cb3 | 63 | |
3a523f5b MD |
64 | /* |
65 | * Forward declarations for old kernels. | |
66 | */ | |
67 | struct mmsghdr; | |
68 | struct rlimit64; | |
69 | struct oldold_utsname; | |
70 | struct old_utsname; | |
71 | struct sel_arg_struct; | |
72 | struct mmap_arg_struct; | |
c0b71117 | 73 | struct file_handle; |
a292e6f1 | 74 | struct user_msghdr; |
3a523f5b | 75 | |
e9169591 MJ |
76 | /* |
77 | * Forward declaration for kernels >= 5.6 | |
78 | */ | |
79 | struct timex; | |
d4967351 MJ |
80 | struct timeval; |
81 | struct itimerval; | |
82 | struct itimerspec; | |
83 | ||
84 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)) | |
85 | typedef __kernel_old_time_t time_t; | |
86 | #endif | |
e9169591 | 87 | |
80f87dd2 MD |
88 | #ifdef IA32_NR_syscalls |
89 | #define NR_compat_syscalls IA32_NR_syscalls | |
90 | #else | |
91 | #define NR_compat_syscalls NR_syscalls | |
92 | #endif | |
93 | ||
259b6cb3 MD |
94 | /* |
95 | * Create LTTng tracepoint probes. | |
96 | */ | |
97 | #define LTTNG_PACKAGE_BUILD | |
98 | #define CREATE_TRACE_POINTS | |
2655f9ad | 99 | #define TP_MODULE_NOINIT |
c075712b | 100 | #define TRACE_INCLUDE_PATH instrumentation/syscalls/headers |
259b6cb3 | 101 | |
a93244f8 MD |
102 | #define PARAMS(args...) args |
103 | ||
5b7ac358 | 104 | /* Handle unknown syscalls */ |
72a52753 | 105 | #undef TRACE_SYSTEM |
5b7ac358 | 106 | #define TRACE_SYSTEM syscalls_unknown |
241ae9a8 | 107 | #include <instrumentation/syscalls/headers/syscalls_unknown.h> |
5b7ac358 MD |
108 | #undef TRACE_SYSTEM |
109 | ||
fc4f7161 MD |
110 | #define SC_ENTER |
111 | ||
fc4f7161 MD |
112 | #undef sc_exit |
113 | #define sc_exit(...) | |
b75d00c4 MD |
114 | #undef sc_in |
115 | #define sc_in(...) __VA_ARGS__ | |
116 | #undef sc_out | |
117 | #define sc_out(...) | |
118 | #undef sc_inout | |
119 | #define sc_inout(...) __VA_ARGS__ | |
5b7ac358 MD |
120 | |
121 | /* Hijack probe callback for system call enter */ | |
a93244f8 | 122 | #undef TP_PROBE_CB |
259b6cb3 | 123 | #define TP_PROBE_CB(_template) &syscall_entry_probe |
57ede728 | 124 | #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \ |
d4291869 | 125 | LTTNG_TRACEPOINT_EVENT(syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \ |
57ede728 | 126 | PARAMS(_fields)) |
265822ae | 127 | #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \ |
d4291869 | 128 | LTTNG_TRACEPOINT_EVENT_CODE(syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \ |
265822ae MD |
129 | PARAMS(_locvar), PARAMS(_code_pre), \ |
130 | PARAMS(_fields), PARAMS(_code_post)) | |
57ede728 MD |
131 | #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \ |
132 | LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(syscall_entry_##_name, PARAMS(_fields)) | |
cb3ef14c | 133 | #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \ |
d4291869 | 134 | LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(syscall_entry_##_template, syscall_entry_##_name) |
141ddf28 MD |
135 | /* Enumerations only defined at first inclusion. */ |
136 | #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values) \ | |
137 | LTTNG_TRACEPOINT_ENUM(_name, PARAMS(_values)) | |
a93244f8 | 138 | #undef TRACE_SYSTEM |
d4291869 | 139 | #define TRACE_SYSTEM syscall_entry_integers |
5b7ac358 | 140 | #define TRACE_INCLUDE_FILE syscalls_integers |
241ae9a8 | 141 | #include <instrumentation/syscalls/headers/syscalls_integers.h> |
5b7ac358 | 142 | #undef TRACE_INCLUDE_FILE |
a93244f8 | 143 | #undef TRACE_SYSTEM |
d4291869 | 144 | #define TRACE_SYSTEM syscall_entry_pointers |
5b7ac358 | 145 | #define TRACE_INCLUDE_FILE syscalls_pointers |
241ae9a8 | 146 | #include <instrumentation/syscalls/headers/syscalls_pointers.h> |
5b7ac358 | 147 | #undef TRACE_INCLUDE_FILE |
a93244f8 | 148 | #undef TRACE_SYSTEM |
141ddf28 | 149 | #undef SC_LTTNG_TRACEPOINT_ENUM |
cb3ef14c MD |
150 | #undef SC_LTTNG_TRACEPOINT_EVENT_CODE |
151 | #undef SC_LTTNG_TRACEPOINT_EVENT | |
152 | #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS | |
153 | #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS | |
5b7ac358 | 154 | #undef TP_PROBE_CB |
3bc29f0a MD |
155 | #undef _TRACE_SYSCALLS_INTEGERS_H |
156 | #undef _TRACE_SYSCALLS_POINTERS_H | |
5b7ac358 MD |
157 | |
158 | /* Hijack probe callback for compat system call enter */ | |
a93244f8 | 159 | #define TP_PROBE_CB(_template) &syscall_entry_probe |
771af27e | 160 | #define LTTNG_SC_COMPAT |
57ede728 | 161 | #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \ |
d4291869 | 162 | LTTNG_TRACEPOINT_EVENT(compat_syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \ |
57ede728 | 163 | PARAMS(_fields)) |
265822ae | 164 | #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \ |
d4291869 | 165 | LTTNG_TRACEPOINT_EVENT_CODE(compat_syscall_entry_##_name, PARAMS(_proto), PARAMS(_args), \ |
265822ae | 166 | PARAMS(_locvar), PARAMS(_code_pre), PARAMS(_fields), PARAMS(_code_post)) |
57ede728 MD |
167 | #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \ |
168 | LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(compat_syscall_entry_##_name, PARAMS(_fields)) | |
cb3ef14c | 169 | #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \ |
d4291869 MD |
170 | LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(compat_syscall_entry_##_template, \ |
171 | compat_syscall_entry_##_name) | |
141ddf28 MD |
172 | /* Enumerations only defined at inital inclusion (not here). */ |
173 | #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values) | |
d4291869 | 174 | #define TRACE_SYSTEM compat_syscall_entry_integers |
5b7ac358 | 175 | #define TRACE_INCLUDE_FILE compat_syscalls_integers |
241ae9a8 | 176 | #include <instrumentation/syscalls/headers/compat_syscalls_integers.h> |
5b7ac358 MD |
177 | #undef TRACE_INCLUDE_FILE |
178 | #undef TRACE_SYSTEM | |
d4291869 | 179 | #define TRACE_SYSTEM compat_syscall_entry_pointers |
5b7ac358 | 180 | #define TRACE_INCLUDE_FILE compat_syscalls_pointers |
241ae9a8 | 181 | #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h> |
5b7ac358 MD |
182 | #undef TRACE_INCLUDE_FILE |
183 | #undef TRACE_SYSTEM | |
141ddf28 | 184 | #undef SC_LTTNG_TRACEPOINT_ENUM |
cb3ef14c MD |
185 | #undef SC_LTTNG_TRACEPOINT_EVENT_CODE |
186 | #undef SC_LTTNG_TRACEPOINT_EVENT | |
187 | #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS | |
188 | #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS | |
5b7ac358 | 189 | #undef TP_PROBE_CB |
3bc29f0a MD |
190 | #undef _TRACE_SYSCALLS_INTEGERS_H |
191 | #undef _TRACE_SYSCALLS_POINTERS_H | |
771af27e | 192 | #undef LTTNG_SC_COMPAT |
5b7ac358 | 193 | |
fc4f7161 MD |
194 | #undef SC_ENTER |
195 | ||
196 | #define SC_EXIT | |
197 | ||
fc4f7161 MD |
198 | #undef sc_exit |
199 | #define sc_exit(...) __VA_ARGS__ | |
b75d00c4 MD |
200 | #undef sc_in |
201 | #define sc_in(...) | |
202 | #undef sc_out | |
203 | #define sc_out(...) __VA_ARGS__ | |
204 | #undef sc_inout | |
205 | #define sc_inout(...) __VA_ARGS__ | |
5b7ac358 MD |
206 | |
207 | /* Hijack probe callback for system call exit */ | |
208 | #define TP_PROBE_CB(_template) &syscall_exit_probe | |
57ede728 | 209 | #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \ |
3bc29f0a | 210 | LTTNG_TRACEPOINT_EVENT(syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \ |
57ede728 | 211 | PARAMS(_fields)) |
265822ae | 212 | #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \ |
3bc29f0a | 213 | LTTNG_TRACEPOINT_EVENT_CODE(syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \ |
265822ae | 214 | PARAMS(_locvar), PARAMS(_code_pre), PARAMS(_fields), PARAMS(_code_post)) |
57ede728 MD |
215 | #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \ |
216 | LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(syscall_exit_##_name, PARAMS(_fields)) | |
cb3ef14c MD |
217 | #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \ |
218 | LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(syscall_exit_##_template, \ | |
5b7ac358 | 219 | syscall_exit_##_name) |
141ddf28 MD |
220 | /* Enumerations only defined at inital inclusion (not here). */ |
221 | #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values) | |
5b7ac358 MD |
222 | #define TRACE_SYSTEM syscall_exit_integers |
223 | #define TRACE_INCLUDE_FILE syscalls_integers | |
241ae9a8 | 224 | #include <instrumentation/syscalls/headers/syscalls_integers.h> |
5b7ac358 MD |
225 | #undef TRACE_INCLUDE_FILE |
226 | #undef TRACE_SYSTEM | |
227 | #define TRACE_SYSTEM syscall_exit_pointers | |
228 | #define TRACE_INCLUDE_FILE syscalls_pointers | |
241ae9a8 | 229 | #include <instrumentation/syscalls/headers/syscalls_pointers.h> |
5b7ac358 MD |
230 | #undef TRACE_INCLUDE_FILE |
231 | #undef TRACE_SYSTEM | |
141ddf28 | 232 | #undef SC_LTTNG_TRACEPOINT_ENUM |
cb3ef14c MD |
233 | #undef SC_LTTNG_TRACEPOINT_EVENT_CODE |
234 | #undef SC_LTTNG_TRACEPOINT_EVENT | |
235 | #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS | |
236 | #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS | |
5b7ac358 | 237 | #undef TP_PROBE_CB |
3bc29f0a MD |
238 | #undef _TRACE_SYSCALLS_INTEGERS_H |
239 | #undef _TRACE_SYSCALLS_POINTERS_H | |
5b7ac358 MD |
240 | |
241 | ||
242 | /* Hijack probe callback for compat system call exit */ | |
243 | #define TP_PROBE_CB(_template) &syscall_exit_probe | |
771af27e | 244 | #define LTTNG_SC_COMPAT |
57ede728 | 245 | #define SC_LTTNG_TRACEPOINT_EVENT(_name, _proto, _args, _fields) \ |
3bc29f0a | 246 | LTTNG_TRACEPOINT_EVENT(compat_syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \ |
57ede728 | 247 | PARAMS(_fields)) |
265822ae | 248 | #define SC_LTTNG_TRACEPOINT_EVENT_CODE(_name, _proto, _args, _locvar, _code_pre, _fields, _code_post) \ |
3bc29f0a | 249 | LTTNG_TRACEPOINT_EVENT_CODE(compat_syscall_exit_##_name, PARAMS(_proto), PARAMS(_args), \ |
265822ae | 250 | PARAMS(_locvar), PARAMS(_code_pre), PARAMS(_fields), PARAMS(_code_post)) |
57ede728 MD |
251 | #define SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(_name, _fields) \ |
252 | LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS(compat_syscall_exit_##_name, PARAMS(_fields)) | |
cb3ef14c | 253 | #define SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(_template, _name) \ |
3bc29f0a | 254 | LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS(compat_syscall_exit_##_template, \ |
5b7ac358 | 255 | compat_syscall_exit_##_name) |
141ddf28 MD |
256 | /* Enumerations only defined at inital inclusion (not here). */ |
257 | #define SC_LTTNG_TRACEPOINT_ENUM(_name, _values) | |
5b7ac358 MD |
258 | #define TRACE_SYSTEM compat_syscall_exit_integers |
259 | #define TRACE_INCLUDE_FILE compat_syscalls_integers | |
241ae9a8 | 260 | #include <instrumentation/syscalls/headers/compat_syscalls_integers.h> |
5b7ac358 | 261 | #undef TRACE_INCLUDE_FILE |
a93244f8 | 262 | #undef TRACE_SYSTEM |
5b7ac358 MD |
263 | #define TRACE_SYSTEM compat_syscall_exit_pointers |
264 | #define TRACE_INCLUDE_FILE compat_syscalls_pointers | |
241ae9a8 | 265 | #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h> |
5b7ac358 | 266 | #undef TRACE_INCLUDE_FILE |
a93244f8 | 267 | #undef TRACE_SYSTEM |
141ddf28 | 268 | #undef SC_LTTNG_TRACEPOINT_ENUM |
cb3ef14c MD |
269 | #undef SC_LTTNG_TRACEPOINT_EVENT_CODE |
270 | #undef SC_LTTNG_TRACEPOINT_EVENT | |
271 | #undef SC_LTTNG_TRACEPOINT_EVENT_CLASS_NOARGS | |
272 | #undef SC_LTTNG_TRACEPOINT_EVENT_INSTANCE_NOARGS | |
a93244f8 | 273 | #undef TP_PROBE_CB |
3bc29f0a MD |
274 | #undef _TRACE_SYSCALLS_INTEGERS_H |
275 | #undef _TRACE_SYSCALLS_POINTERS_H | |
771af27e | 276 | #undef LTTNG_SC_COMPAT |
5b7ac358 | 277 | |
fc4f7161 | 278 | #undef SC_EXIT |
259b6cb3 | 279 | |
2655f9ad | 280 | #undef TP_MODULE_NOINIT |
259b6cb3 MD |
281 | #undef LTTNG_PACKAGE_BUILD |
282 | #undef CREATE_TRACE_POINTS | |
283 | ||
a93244f8 MD |
284 | struct trace_syscall_entry { |
285 | void *func; | |
286 | const struct lttng_event_desc *desc; | |
287 | const struct lttng_event_field *fields; | |
288 | unsigned int nrargs; | |
289 | }; | |
290 | ||
291 | #define CREATE_SYSCALL_TABLE | |
292 | ||
fc4f7161 MD |
293 | #define SC_ENTER |
294 | ||
295 | #undef sc_exit | |
296 | #define sc_exit(...) | |
297 | ||
259b6cb3 | 298 | #undef TRACE_SYSCALL_TABLE |
f7bdf4db | 299 | #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \ |
259b6cb3 | 300 | [ _nr ] = { \ |
d4291869 | 301 | .func = __event_probe__syscall_entry_##_template, \ |
259b6cb3 | 302 | .nrargs = (_nrargs), \ |
d4291869 MD |
303 | .fields = __event_fields___syscall_entry_##_template, \ |
304 | .desc = &__event_desc___syscall_entry_##_name, \ | |
259b6cb3 MD |
305 | }, |
306 | ||
5b7ac358 | 307 | /* Syscall enter tracing table */ |
49c50022 | 308 | static const struct trace_syscall_entry sc_table[] = { |
241ae9a8 MD |
309 | #include <instrumentation/syscalls/headers/syscalls_integers.h> |
310 | #include <instrumentation/syscalls/headers/syscalls_pointers.h> | |
259b6cb3 MD |
311 | }; |
312 | ||
a93244f8 MD |
313 | #undef TRACE_SYSCALL_TABLE |
314 | #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \ | |
315 | [ _nr ] = { \ | |
d4291869 | 316 | .func = __event_probe__compat_syscall_entry_##_template, \ |
a93244f8 | 317 | .nrargs = (_nrargs), \ |
d4291869 MD |
318 | .fields = __event_fields___compat_syscall_entry_##_template, \ |
319 | .desc = &__event_desc___compat_syscall_entry_##_name, \ | |
a93244f8 MD |
320 | }, |
321 | ||
5b7ac358 | 322 | /* Compat syscall enter table */ |
a93244f8 | 323 | const struct trace_syscall_entry compat_sc_table[] = { |
241ae9a8 MD |
324 | #include <instrumentation/syscalls/headers/compat_syscalls_integers.h> |
325 | #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h> | |
a93244f8 | 326 | }; |
259b6cb3 | 327 | |
fc4f7161 MD |
328 | #undef SC_ENTER |
329 | ||
330 | #define SC_EXIT | |
331 | ||
332 | #undef sc_exit | |
333 | #define sc_exit(...) __VA_ARGS__ | |
334 | ||
5b7ac358 MD |
335 | #undef TRACE_SYSCALL_TABLE |
336 | #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \ | |
337 | [ _nr ] = { \ | |
338 | .func = __event_probe__syscall_exit_##_template, \ | |
339 | .nrargs = (_nrargs), \ | |
340 | .fields = __event_fields___syscall_exit_##_template, \ | |
341 | .desc = &__event_desc___syscall_exit_##_name, \ | |
342 | }, | |
343 | ||
344 | /* Syscall exit table */ | |
345 | static const struct trace_syscall_entry sc_exit_table[] = { | |
241ae9a8 MD |
346 | #include <instrumentation/syscalls/headers/syscalls_integers.h> |
347 | #include <instrumentation/syscalls/headers/syscalls_pointers.h> | |
5b7ac358 MD |
348 | }; |
349 | ||
350 | #undef TRACE_SYSCALL_TABLE | |
351 | #define TRACE_SYSCALL_TABLE(_template, _name, _nr, _nrargs) \ | |
352 | [ _nr ] = { \ | |
353 | .func = __event_probe__compat_syscall_exit_##_template, \ | |
354 | .nrargs = (_nrargs), \ | |
355 | .fields = __event_fields___compat_syscall_exit_##_template, \ | |
356 | .desc = &__event_desc___compat_syscall_exit_##_name, \ | |
357 | }, | |
358 | ||
359 | /* Compat syscall exit table */ | |
360 | const struct trace_syscall_entry compat_sc_exit_table[] = { | |
241ae9a8 MD |
361 | #include <instrumentation/syscalls/headers/compat_syscalls_integers.h> |
362 | #include <instrumentation/syscalls/headers/compat_syscalls_pointers.h> | |
5b7ac358 MD |
363 | }; |
364 | ||
fc4f7161 MD |
365 | #undef SC_EXIT |
366 | ||
a93244f8 | 367 | #undef CREATE_SYSCALL_TABLE |
2faf7d1b | 368 | |
80f87dd2 MD |
369 | struct lttng_syscall_filter { |
370 | DECLARE_BITMAP(sc, NR_syscalls); | |
371 | DECLARE_BITMAP(sc_compat, NR_compat_syscalls); | |
372 | }; | |
373 | ||
a90917c3 | 374 | static void syscall_entry_unknown(struct lttng_event *event, |
f405cfce MD |
375 | struct pt_regs *regs, unsigned int id) |
376 | { | |
9f70b60c | 377 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
f405cfce | 378 | |
9f70b60c | 379 | lttng_syscall_get_arguments(current, regs, args); |
1aa3298b | 380 | if (unlikely(in_compat_syscall())) |
d4291869 | 381 | __event_probe__compat_syscall_entry_unknown(event, id, args); |
a93244f8 | 382 | else |
d4291869 | 383 | __event_probe__syscall_entry_unknown(event, id, args); |
f405cfce MD |
384 | } |
385 | ||
2faf7d1b | 386 | void syscall_entry_probe(void *__data, struct pt_regs *regs, long id) |
259b6cb3 | 387 | { |
a90917c3 MD |
388 | struct lttng_channel *chan = __data; |
389 | struct lttng_event *event, *unknown_event; | |
49c50022 MD |
390 | const struct trace_syscall_entry *table, *entry; |
391 | size_t table_len; | |
259b6cb3 | 392 | |
1aa3298b | 393 | if (unlikely(in_compat_syscall())) { |
80f87dd2 MD |
394 | struct lttng_syscall_filter *filter; |
395 | ||
49e2f4c4 | 396 | filter = lttng_rcu_dereference(chan->sc_filter); |
80f87dd2 | 397 | if (filter) { |
74f7b56a | 398 | if (id < 0 || id >= NR_compat_syscalls |
80f87dd2 MD |
399 | || !test_bit(id, filter->sc_compat)) { |
400 | /* System call filtered out. */ | |
401 | return; | |
402 | } | |
403 | } | |
49c50022 | 404 | table = compat_sc_table; |
a93244f8 | 405 | table_len = ARRAY_SIZE(compat_sc_table); |
49c50022 MD |
406 | unknown_event = chan->sc_compat_unknown; |
407 | } else { | |
80f87dd2 MD |
408 | struct lttng_syscall_filter *filter; |
409 | ||
49e2f4c4 | 410 | filter = lttng_rcu_dereference(chan->sc_filter); |
80f87dd2 | 411 | if (filter) { |
74f7b56a | 412 | if (id < 0 || id >= NR_syscalls |
80f87dd2 MD |
413 | || !test_bit(id, filter->sc)) { |
414 | /* System call filtered out. */ | |
415 | return; | |
416 | } | |
417 | } | |
49c50022 MD |
418 | table = sc_table; |
419 | table_len = ARRAY_SIZE(sc_table); | |
420 | unknown_event = chan->sc_unknown; | |
b76dc1a0 | 421 | } |
74f7b56a | 422 | if (unlikely(id < 0 || id >= table_len)) { |
49c50022 | 423 | syscall_entry_unknown(unknown_event, regs, id); |
259b6cb3 | 424 | return; |
f405cfce | 425 | } |
1aa3298b | 426 | if (unlikely(in_compat_syscall())) |
49c50022 MD |
427 | event = chan->compat_sc_table[id]; |
428 | else | |
429 | event = chan->sc_table[id]; | |
f405cfce | 430 | if (unlikely(!event)) { |
49c50022 | 431 | syscall_entry_unknown(unknown_event, regs, id); |
f405cfce MD |
432 | return; |
433 | } | |
49c50022 | 434 | entry = &table[id]; |
f405cfce | 435 | WARN_ON_ONCE(!entry); |
259b6cb3 MD |
436 | |
437 | switch (entry->nrargs) { | |
438 | case 0: | |
439 | { | |
440 | void (*fptr)(void *__data) = entry->func; | |
441 | ||
442 | fptr(event); | |
443 | break; | |
444 | } | |
445 | case 1: | |
446 | { | |
447 | void (*fptr)(void *__data, unsigned long arg0) = entry->func; | |
9f70b60c | 448 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
259b6cb3 | 449 | |
9f70b60c | 450 | lttng_syscall_get_arguments(current, regs, args); |
259b6cb3 MD |
451 | fptr(event, args[0]); |
452 | break; | |
453 | } | |
454 | case 2: | |
455 | { | |
456 | void (*fptr)(void *__data, | |
457 | unsigned long arg0, | |
458 | unsigned long arg1) = entry->func; | |
9f70b60c | 459 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
259b6cb3 | 460 | |
9f70b60c | 461 | lttng_syscall_get_arguments(current, regs, args); |
259b6cb3 MD |
462 | fptr(event, args[0], args[1]); |
463 | break; | |
464 | } | |
465 | case 3: | |
466 | { | |
467 | void (*fptr)(void *__data, | |
468 | unsigned long arg0, | |
469 | unsigned long arg1, | |
470 | unsigned long arg2) = entry->func; | |
9f70b60c | 471 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
259b6cb3 | 472 | |
9f70b60c | 473 | lttng_syscall_get_arguments(current, regs, args); |
259b6cb3 MD |
474 | fptr(event, args[0], args[1], args[2]); |
475 | break; | |
476 | } | |
477 | case 4: | |
478 | { | |
479 | void (*fptr)(void *__data, | |
480 | unsigned long arg0, | |
481 | unsigned long arg1, | |
482 | unsigned long arg2, | |
483 | unsigned long arg3) = entry->func; | |
9f70b60c | 484 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
259b6cb3 | 485 | |
9f70b60c | 486 | lttng_syscall_get_arguments(current, regs, args); |
259b6cb3 MD |
487 | fptr(event, args[0], args[1], args[2], args[3]); |
488 | break; | |
489 | } | |
490 | case 5: | |
491 | { | |
492 | void (*fptr)(void *__data, | |
493 | unsigned long arg0, | |
494 | unsigned long arg1, | |
495 | unsigned long arg2, | |
496 | unsigned long arg3, | |
497 | unsigned long arg4) = entry->func; | |
9f70b60c | 498 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
259b6cb3 | 499 | |
9f70b60c | 500 | lttng_syscall_get_arguments(current, regs, args); |
259b6cb3 MD |
501 | fptr(event, args[0], args[1], args[2], args[3], args[4]); |
502 | break; | |
503 | } | |
504 | case 6: | |
505 | { | |
506 | void (*fptr)(void *__data, | |
507 | unsigned long arg0, | |
508 | unsigned long arg1, | |
509 | unsigned long arg2, | |
510 | unsigned long arg3, | |
511 | unsigned long arg4, | |
512 | unsigned long arg5) = entry->func; | |
9f70b60c | 513 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
259b6cb3 | 514 | |
9f70b60c | 515 | lttng_syscall_get_arguments(current, regs, args); |
259b6cb3 MD |
516 | fptr(event, args[0], args[1], args[2], |
517 | args[3], args[4], args[5]); | |
518 | break; | |
519 | } | |
520 | default: | |
521 | break; | |
522 | } | |
523 | } | |
524 | ||
5b7ac358 | 525 | static void syscall_exit_unknown(struct lttng_event *event, |
74f7b56a | 526 | struct pt_regs *regs, int id, long ret) |
5b7ac358 | 527 | { |
9f70b60c | 528 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
5b7ac358 | 529 | |
9f70b60c | 530 | lttng_syscall_get_arguments(current, regs, args); |
1aa3298b | 531 | if (unlikely(in_compat_syscall())) |
5b7ac358 MD |
532 | __event_probe__compat_syscall_exit_unknown(event, id, ret, |
533 | args); | |
534 | else | |
535 | __event_probe__syscall_exit_unknown(event, id, ret, args); | |
536 | } | |
537 | ||
538 | void syscall_exit_probe(void *__data, struct pt_regs *regs, long ret) | |
539 | { | |
540 | struct lttng_channel *chan = __data; | |
541 | struct lttng_event *event, *unknown_event; | |
542 | const struct trace_syscall_entry *table, *entry; | |
543 | size_t table_len; | |
544 | long id; | |
545 | ||
546 | id = syscall_get_nr(current, regs); | |
1aa3298b | 547 | if (unlikely(in_compat_syscall())) { |
5b7ac358 MD |
548 | struct lttng_syscall_filter *filter; |
549 | ||
49e2f4c4 | 550 | filter = lttng_rcu_dereference(chan->sc_filter); |
5b7ac358 | 551 | if (filter) { |
74f7b56a | 552 | if (id < 0 || id >= NR_compat_syscalls |
5b7ac358 MD |
553 | || !test_bit(id, filter->sc_compat)) { |
554 | /* System call filtered out. */ | |
555 | return; | |
556 | } | |
557 | } | |
558 | table = compat_sc_exit_table; | |
559 | table_len = ARRAY_SIZE(compat_sc_exit_table); | |
560 | unknown_event = chan->compat_sc_exit_unknown; | |
561 | } else { | |
562 | struct lttng_syscall_filter *filter; | |
563 | ||
49e2f4c4 | 564 | filter = lttng_rcu_dereference(chan->sc_filter); |
5b7ac358 | 565 | if (filter) { |
74f7b56a | 566 | if (id < 0 || id >= NR_syscalls |
5b7ac358 MD |
567 | || !test_bit(id, filter->sc)) { |
568 | /* System call filtered out. */ | |
569 | return; | |
570 | } | |
571 | } | |
572 | table = sc_exit_table; | |
573 | table_len = ARRAY_SIZE(sc_exit_table); | |
574 | unknown_event = chan->sc_exit_unknown; | |
575 | } | |
74f7b56a | 576 | if (unlikely(id < 0 || id >= table_len)) { |
5b7ac358 MD |
577 | syscall_exit_unknown(unknown_event, regs, id, ret); |
578 | return; | |
579 | } | |
1aa3298b | 580 | if (unlikely(in_compat_syscall())) |
5b7ac358 MD |
581 | event = chan->compat_sc_exit_table[id]; |
582 | else | |
583 | event = chan->sc_exit_table[id]; | |
584 | if (unlikely(!event)) { | |
585 | syscall_exit_unknown(unknown_event, regs, id, ret); | |
586 | return; | |
587 | } | |
588 | entry = &table[id]; | |
589 | WARN_ON_ONCE(!entry); | |
590 | ||
591 | switch (entry->nrargs) { | |
592 | case 0: | |
593 | { | |
fc4f7161 | 594 | void (*fptr)(void *__data, long ret) = entry->func; |
5b7ac358 | 595 | |
fc4f7161 | 596 | fptr(event, ret); |
5b7ac358 MD |
597 | break; |
598 | } | |
599 | case 1: | |
600 | { | |
601 | void (*fptr)(void *__data, | |
fc4f7161 | 602 | long ret, |
5b7ac358 | 603 | unsigned long arg0) = entry->func; |
9f70b60c | 604 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
5b7ac358 | 605 | |
9f70b60c | 606 | lttng_syscall_get_arguments(current, regs, args); |
fc4f7161 | 607 | fptr(event, ret, args[0]); |
5b7ac358 MD |
608 | break; |
609 | } | |
610 | case 2: | |
611 | { | |
612 | void (*fptr)(void *__data, | |
fc4f7161 | 613 | long ret, |
5b7ac358 MD |
614 | unsigned long arg0, |
615 | unsigned long arg1) = entry->func; | |
9f70b60c | 616 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
5b7ac358 | 617 | |
9f70b60c | 618 | lttng_syscall_get_arguments(current, regs, args); |
fc4f7161 | 619 | fptr(event, ret, args[0], args[1]); |
5b7ac358 MD |
620 | break; |
621 | } | |
622 | case 3: | |
623 | { | |
624 | void (*fptr)(void *__data, | |
fc4f7161 | 625 | long ret, |
5b7ac358 MD |
626 | unsigned long arg0, |
627 | unsigned long arg1, | |
628 | unsigned long arg2) = entry->func; | |
9f70b60c | 629 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
5b7ac358 | 630 | |
9f70b60c | 631 | lttng_syscall_get_arguments(current, regs, args); |
fc4f7161 | 632 | fptr(event, ret, args[0], args[1], args[2]); |
5b7ac358 MD |
633 | break; |
634 | } | |
635 | case 4: | |
636 | { | |
637 | void (*fptr)(void *__data, | |
fc4f7161 | 638 | long ret, |
5b7ac358 MD |
639 | unsigned long arg0, |
640 | unsigned long arg1, | |
641 | unsigned long arg2, | |
642 | unsigned long arg3) = entry->func; | |
9f70b60c | 643 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
5b7ac358 | 644 | |
9f70b60c | 645 | lttng_syscall_get_arguments(current, regs, args); |
fc4f7161 | 646 | fptr(event, ret, args[0], args[1], args[2], args[3]); |
5b7ac358 MD |
647 | break; |
648 | } | |
649 | case 5: | |
650 | { | |
651 | void (*fptr)(void *__data, | |
fc4f7161 | 652 | long ret, |
5b7ac358 MD |
653 | unsigned long arg0, |
654 | unsigned long arg1, | |
655 | unsigned long arg2, | |
656 | unsigned long arg3, | |
657 | unsigned long arg4) = entry->func; | |
9f70b60c | 658 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
5b7ac358 | 659 | |
9f70b60c | 660 | lttng_syscall_get_arguments(current, regs, args); |
fc4f7161 | 661 | fptr(event, ret, args[0], args[1], args[2], args[3], args[4]); |
5b7ac358 MD |
662 | break; |
663 | } | |
664 | case 6: | |
665 | { | |
666 | void (*fptr)(void *__data, | |
fc4f7161 | 667 | long ret, |
5b7ac358 MD |
668 | unsigned long arg0, |
669 | unsigned long arg1, | |
670 | unsigned long arg2, | |
671 | unsigned long arg3, | |
672 | unsigned long arg4, | |
673 | unsigned long arg5) = entry->func; | |
9f70b60c | 674 | unsigned long args[LTTNG_SYSCALL_NR_ARGS]; |
5b7ac358 | 675 | |
9f70b60c | 676 | lttng_syscall_get_arguments(current, regs, args); |
fc4f7161 | 677 | fptr(event, ret, args[0], args[1], args[2], |
5b7ac358 MD |
678 | args[3], args[4], args[5]); |
679 | break; | |
680 | } | |
681 | default: | |
682 | break; | |
683 | } | |
684 | } | |
685 | ||
33a39a3c MD |
686 | /* |
687 | * noinline to diminish caller stack size. | |
688 | * Should be called with sessions lock held. | |
689 | */ | |
49c50022 MD |
690 | static |
691 | int fill_table(const struct trace_syscall_entry *table, size_t table_len, | |
5b7ac358 MD |
692 | struct lttng_event **chan_table, struct lttng_channel *chan, |
693 | void *filter, enum sc_type type) | |
259b6cb3 | 694 | { |
2a0c4816 | 695 | const struct lttng_event_desc *desc; |
259b6cb3 | 696 | unsigned int i; |
49c50022 MD |
697 | |
698 | /* Allocate events for each syscall, insert into table */ | |
699 | for (i = 0; i < table_len; i++) { | |
700 | struct lttng_kernel_event ev; | |
2a0c4816 | 701 | desc = table[i].desc; |
49c50022 MD |
702 | |
703 | if (!desc) { | |
704 | /* Unknown syscall */ | |
705 | continue; | |
706 | } | |
707 | /* | |
708 | * Skip those already populated by previous failed | |
709 | * register for this channel. | |
710 | */ | |
711 | if (chan_table[i]) | |
712 | continue; | |
713 | memset(&ev, 0, sizeof(ev)); | |
5b7ac358 MD |
714 | switch (type) { |
715 | case SC_TYPE_ENTRY: | |
716 | strncpy(ev.name, SYSCALL_ENTRY_STR, | |
717 | LTTNG_KERNEL_SYM_NAME_LEN); | |
718 | break; | |
719 | case SC_TYPE_EXIT: | |
720 | strncpy(ev.name, SYSCALL_EXIT_STR, | |
721 | LTTNG_KERNEL_SYM_NAME_LEN); | |
722 | break; | |
723 | case SC_TYPE_COMPAT_ENTRY: | |
724 | strncpy(ev.name, COMPAT_SYSCALL_ENTRY_STR, | |
725 | LTTNG_KERNEL_SYM_NAME_LEN); | |
726 | break; | |
727 | case SC_TYPE_COMPAT_EXIT: | |
728 | strncpy(ev.name, COMPAT_SYSCALL_EXIT_STR, | |
729 | LTTNG_KERNEL_SYM_NAME_LEN); | |
730 | break; | |
731 | default: | |
732 | BUG_ON(1); | |
733 | break; | |
734 | } | |
735 | strncat(ev.name, desc->name, | |
736 | LTTNG_KERNEL_SYM_NAME_LEN - strlen(ev.name) - 1); | |
f8695253 | 737 | ev.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
33a39a3c MD |
738 | ev.instrumentation = LTTNG_KERNEL_SYSCALL; |
739 | chan_table[i] = _lttng_event_create(chan, &ev, filter, | |
3c997079 | 740 | desc, ev.instrumentation); |
abc0446a MD |
741 | WARN_ON_ONCE(!chan_table[i]); |
742 | if (IS_ERR(chan_table[i])) { | |
49c50022 MD |
743 | /* |
744 | * If something goes wrong in event registration | |
745 | * after the first one, we have no choice but to | |
746 | * leave the previous events in there, until | |
747 | * deleted by session teardown. | |
748 | */ | |
abc0446a | 749 | return PTR_ERR(chan_table[i]); |
49c50022 MD |
750 | } |
751 | } | |
752 | return 0; | |
753 | } | |
754 | ||
33a39a3c MD |
755 | /* |
756 | * Should be called with sessions lock held. | |
757 | */ | |
a90917c3 | 758 | int lttng_syscalls_register(struct lttng_channel *chan, void *filter) |
49c50022 | 759 | { |
2a0c4816 | 760 | struct lttng_kernel_event ev; |
259b6cb3 MD |
761 | int ret; |
762 | ||
da0fcb14 | 763 | wrapper_vmalloc_sync_mappings(); |
259b6cb3 MD |
764 | |
765 | if (!chan->sc_table) { | |
766 | /* create syscall table mapping syscall to events */ | |
a90917c3 | 767 | chan->sc_table = kzalloc(sizeof(struct lttng_event *) |
259b6cb3 MD |
768 | * ARRAY_SIZE(sc_table), GFP_KERNEL); |
769 | if (!chan->sc_table) | |
770 | return -ENOMEM; | |
771 | } | |
5b7ac358 MD |
772 | if (!chan->sc_exit_table) { |
773 | /* create syscall table mapping syscall to events */ | |
774 | chan->sc_exit_table = kzalloc(sizeof(struct lttng_event *) | |
775 | * ARRAY_SIZE(sc_exit_table), GFP_KERNEL); | |
776 | if (!chan->sc_exit_table) | |
777 | return -ENOMEM; | |
778 | } | |
779 | ||
259b6cb3 | 780 | |
49c50022 MD |
781 | #ifdef CONFIG_COMPAT |
782 | if (!chan->compat_sc_table) { | |
783 | /* create syscall table mapping compat syscall to events */ | |
a90917c3 | 784 | chan->compat_sc_table = kzalloc(sizeof(struct lttng_event *) |
a93244f8 | 785 | * ARRAY_SIZE(compat_sc_table), GFP_KERNEL); |
49c50022 MD |
786 | if (!chan->compat_sc_table) |
787 | return -ENOMEM; | |
788 | } | |
5b7ac358 MD |
789 | |
790 | if (!chan->compat_sc_exit_table) { | |
791 | /* create syscall table mapping compat syscall to events */ | |
792 | chan->compat_sc_exit_table = kzalloc(sizeof(struct lttng_event *) | |
793 | * ARRAY_SIZE(compat_sc_exit_table), GFP_KERNEL); | |
794 | if (!chan->compat_sc_exit_table) | |
795 | return -ENOMEM; | |
796 | } | |
49c50022 | 797 | #endif |
f405cfce | 798 | if (!chan->sc_unknown) { |
f405cfce | 799 | const struct lttng_event_desc *desc = |
d4291869 | 800 | &__event_desc___syscall_entry_unknown; |
2f804c0a | 801 | |
f405cfce | 802 | memset(&ev, 0, sizeof(ev)); |
f8695253 MD |
803 | strncpy(ev.name, desc->name, LTTNG_KERNEL_SYM_NAME_LEN); |
804 | ev.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
33a39a3c MD |
805 | ev.instrumentation = LTTNG_KERNEL_SYSCALL; |
806 | chan->sc_unknown = _lttng_event_create(chan, &ev, filter, | |
3c997079 MD |
807 | desc, |
808 | ev.instrumentation); | |
abc0446a MD |
809 | WARN_ON_ONCE(!chan->sc_unknown); |
810 | if (IS_ERR(chan->sc_unknown)) { | |
811 | return PTR_ERR(chan->sc_unknown); | |
f405cfce MD |
812 | } |
813 | } | |
814 | ||
b76dc1a0 | 815 | if (!chan->sc_compat_unknown) { |
b76dc1a0 | 816 | const struct lttng_event_desc *desc = |
d4291869 | 817 | &__event_desc___compat_syscall_entry_unknown; |
b76dc1a0 MD |
818 | |
819 | memset(&ev, 0, sizeof(ev)); | |
f8695253 MD |
820 | strncpy(ev.name, desc->name, LTTNG_KERNEL_SYM_NAME_LEN); |
821 | ev.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
33a39a3c MD |
822 | ev.instrumentation = LTTNG_KERNEL_SYSCALL; |
823 | chan->sc_compat_unknown = _lttng_event_create(chan, &ev, filter, | |
3c997079 MD |
824 | desc, |
825 | ev.instrumentation); | |
abc0446a MD |
826 | WARN_ON_ONCE(!chan->sc_unknown); |
827 | if (IS_ERR(chan->sc_compat_unknown)) { | |
828 | return PTR_ERR(chan->sc_compat_unknown); | |
b76dc1a0 MD |
829 | } |
830 | } | |
831 | ||
5b7ac358 | 832 | if (!chan->compat_sc_exit_unknown) { |
2f804c0a | 833 | const struct lttng_event_desc *desc = |
5b7ac358 | 834 | &__event_desc___compat_syscall_exit_unknown; |
2f804c0a MD |
835 | |
836 | memset(&ev, 0, sizeof(ev)); | |
f8695253 MD |
837 | strncpy(ev.name, desc->name, LTTNG_KERNEL_SYM_NAME_LEN); |
838 | ev.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
33a39a3c MD |
839 | ev.instrumentation = LTTNG_KERNEL_SYSCALL; |
840 | chan->compat_sc_exit_unknown = _lttng_event_create(chan, &ev, | |
3c997079 MD |
841 | filter, desc, |
842 | ev.instrumentation); | |
5b7ac358 MD |
843 | WARN_ON_ONCE(!chan->compat_sc_exit_unknown); |
844 | if (IS_ERR(chan->compat_sc_exit_unknown)) { | |
845 | return PTR_ERR(chan->compat_sc_exit_unknown); | |
846 | } | |
847 | } | |
848 | ||
849 | if (!chan->sc_exit_unknown) { | |
850 | const struct lttng_event_desc *desc = | |
851 | &__event_desc___syscall_exit_unknown; | |
852 | ||
853 | memset(&ev, 0, sizeof(ev)); | |
854 | strncpy(ev.name, desc->name, LTTNG_KERNEL_SYM_NAME_LEN); | |
855 | ev.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
33a39a3c MD |
856 | ev.instrumentation = LTTNG_KERNEL_SYSCALL; |
857 | chan->sc_exit_unknown = _lttng_event_create(chan, &ev, filter, | |
3c997079 | 858 | desc, ev.instrumentation); |
5b7ac358 MD |
859 | WARN_ON_ONCE(!chan->sc_exit_unknown); |
860 | if (IS_ERR(chan->sc_exit_unknown)) { | |
861 | return PTR_ERR(chan->sc_exit_unknown); | |
2f804c0a MD |
862 | } |
863 | } | |
864 | ||
49c50022 | 865 | ret = fill_table(sc_table, ARRAY_SIZE(sc_table), |
5b7ac358 MD |
866 | chan->sc_table, chan, filter, SC_TYPE_ENTRY); |
867 | if (ret) | |
868 | return ret; | |
869 | ret = fill_table(sc_exit_table, ARRAY_SIZE(sc_exit_table), | |
870 | chan->sc_exit_table, chan, filter, SC_TYPE_EXIT); | |
49c50022 MD |
871 | if (ret) |
872 | return ret; | |
5b7ac358 | 873 | |
49c50022 | 874 | #ifdef CONFIG_COMPAT |
a93244f8 | 875 | ret = fill_table(compat_sc_table, ARRAY_SIZE(compat_sc_table), |
5b7ac358 MD |
876 | chan->compat_sc_table, chan, filter, |
877 | SC_TYPE_COMPAT_ENTRY); | |
878 | if (ret) | |
879 | return ret; | |
880 | ret = fill_table(compat_sc_exit_table, ARRAY_SIZE(compat_sc_exit_table), | |
881 | chan->compat_sc_exit_table, chan, filter, | |
882 | SC_TYPE_COMPAT_EXIT); | |
49c50022 MD |
883 | if (ret) |
884 | return ret; | |
885 | #endif | |
80f87dd2 MD |
886 | if (!chan->sys_enter_registered) { |
887 | ret = lttng_wrapper_tracepoint_probe_register("sys_enter", | |
888 | (void *) syscall_entry_probe, chan); | |
889 | if (ret) | |
890 | return ret; | |
891 | chan->sys_enter_registered = 1; | |
892 | } | |
63728b02 MD |
893 | /* |
894 | * We change the name of sys_exit tracepoint due to namespace | |
895 | * conflict with sys_exit syscall entry. | |
896 | */ | |
80f87dd2 MD |
897 | if (!chan->sys_exit_registered) { |
898 | ret = lttng_wrapper_tracepoint_probe_register("sys_exit", | |
5b7ac358 | 899 | (void *) syscall_exit_probe, chan); |
80f87dd2 MD |
900 | if (ret) { |
901 | WARN_ON_ONCE(lttng_wrapper_tracepoint_probe_unregister("sys_enter", | |
902 | (void *) syscall_entry_probe, chan)); | |
903 | return ret; | |
904 | } | |
905 | chan->sys_exit_registered = 1; | |
63728b02 | 906 | } |
259b6cb3 MD |
907 | return ret; |
908 | } | |
909 | ||
910 | /* | |
911 | * Only called at session destruction. | |
912 | */ | |
a90917c3 | 913 | int lttng_syscalls_unregister(struct lttng_channel *chan) |
259b6cb3 MD |
914 | { |
915 | int ret; | |
916 | ||
917 | if (!chan->sc_table) | |
918 | return 0; | |
80f87dd2 | 919 | if (chan->sys_enter_registered) { |
6ad0e68b FD |
920 | ret = lttng_wrapper_tracepoint_probe_unregister("sys_enter", |
921 | (void *) syscall_entry_probe, chan); | |
80f87dd2 MD |
922 | if (ret) |
923 | return ret; | |
924 | chan->sys_enter_registered = 0; | |
925 | } | |
926 | if (chan->sys_exit_registered) { | |
6ad0e68b FD |
927 | ret = lttng_wrapper_tracepoint_probe_unregister("sys_exit", |
928 | (void *) syscall_exit_probe, chan); | |
80f87dd2 MD |
929 | if (ret) |
930 | return ret; | |
931 | chan->sys_exit_registered = 0; | |
932 | } | |
a90917c3 | 933 | /* lttng_event destroy will be performed by lttng_session_destroy() */ |
259b6cb3 | 934 | kfree(chan->sc_table); |
5b7ac358 | 935 | kfree(chan->sc_exit_table); |
49c50022 MD |
936 | #ifdef CONFIG_COMPAT |
937 | kfree(chan->compat_sc_table); | |
5b7ac358 | 938 | kfree(chan->compat_sc_exit_table); |
49c50022 | 939 | #endif |
80f87dd2 MD |
940 | kfree(chan->sc_filter); |
941 | return 0; | |
942 | } | |
943 | ||
944 | static | |
945 | int get_syscall_nr(const char *syscall_name) | |
946 | { | |
947 | int syscall_nr = -1; | |
948 | int i; | |
949 | ||
950 | for (i = 0; i < ARRAY_SIZE(sc_table); i++) { | |
951 | const struct trace_syscall_entry *entry; | |
5b7ac358 | 952 | const char *it_name; |
80f87dd2 MD |
953 | |
954 | entry = &sc_table[i]; | |
955 | if (!entry->desc) | |
956 | continue; | |
5b7ac358 MD |
957 | it_name = entry->desc->name; |
958 | it_name += strlen(SYSCALL_ENTRY_STR); | |
959 | if (!strcmp(syscall_name, it_name)) { | |
80f87dd2 MD |
960 | syscall_nr = i; |
961 | break; | |
962 | } | |
963 | } | |
964 | return syscall_nr; | |
965 | } | |
966 | ||
967 | static | |
968 | int get_compat_syscall_nr(const char *syscall_name) | |
969 | { | |
970 | int syscall_nr = -1; | |
971 | int i; | |
972 | ||
973 | for (i = 0; i < ARRAY_SIZE(compat_sc_table); i++) { | |
974 | const struct trace_syscall_entry *entry; | |
5b7ac358 | 975 | const char *it_name; |
80f87dd2 MD |
976 | |
977 | entry = &compat_sc_table[i]; | |
978 | if (!entry->desc) | |
979 | continue; | |
5b7ac358 MD |
980 | it_name = entry->desc->name; |
981 | it_name += strlen(COMPAT_SYSCALL_ENTRY_STR); | |
982 | if (!strcmp(syscall_name, it_name)) { | |
80f87dd2 MD |
983 | syscall_nr = i; |
984 | break; | |
985 | } | |
986 | } | |
987 | return syscall_nr; | |
988 | } | |
989 | ||
12e579db MD |
990 | static |
991 | uint32_t get_sc_tables_len(void) | |
992 | { | |
993 | return ARRAY_SIZE(sc_table) + ARRAY_SIZE(compat_sc_table); | |
994 | } | |
995 | ||
80f87dd2 MD |
996 | int lttng_syscall_filter_enable(struct lttng_channel *chan, |
997 | const char *name) | |
998 | { | |
999 | int syscall_nr, compat_syscall_nr, ret; | |
1000 | struct lttng_syscall_filter *filter; | |
1001 | ||
1002 | WARN_ON_ONCE(!chan->sc_table); | |
1003 | ||
1004 | if (!name) { | |
1005 | /* Enable all system calls by removing filter */ | |
1006 | if (chan->sc_filter) { | |
1007 | filter = chan->sc_filter; | |
1008 | rcu_assign_pointer(chan->sc_filter, NULL); | |
1009 | synchronize_trace(); | |
1010 | kfree(filter); | |
1011 | } | |
1012 | chan->syscall_all = 1; | |
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | if (!chan->sc_filter) { | |
1017 | if (chan->syscall_all) { | |
1018 | /* | |
1019 | * All syscalls are already enabled. | |
1020 | */ | |
1021 | return -EEXIST; | |
1022 | } | |
1023 | filter = kzalloc(sizeof(struct lttng_syscall_filter), | |
1024 | GFP_KERNEL); | |
1025 | if (!filter) | |
1026 | return -ENOMEM; | |
1027 | } else { | |
1028 | filter = chan->sc_filter; | |
1029 | } | |
1030 | syscall_nr = get_syscall_nr(name); | |
1031 | compat_syscall_nr = get_compat_syscall_nr(name); | |
1032 | if (syscall_nr < 0 && compat_syscall_nr < 0) { | |
1033 | ret = -ENOENT; | |
1034 | goto error; | |
1035 | } | |
1036 | if (syscall_nr >= 0) { | |
1037 | if (test_bit(syscall_nr, filter->sc)) { | |
1038 | ret = -EEXIST; | |
1039 | goto error; | |
1040 | } | |
1041 | bitmap_set(filter->sc, syscall_nr, 1); | |
1042 | } | |
1043 | if (compat_syscall_nr >= 0) { | |
1044 | if (test_bit(compat_syscall_nr, filter->sc_compat)) { | |
1045 | ret = -EEXIST; | |
1046 | goto error; | |
1047 | } | |
1048 | bitmap_set(filter->sc_compat, compat_syscall_nr, 1); | |
1049 | } | |
1050 | if (!chan->sc_filter) | |
1051 | rcu_assign_pointer(chan->sc_filter, filter); | |
1052 | return 0; | |
1053 | ||
1054 | error: | |
1055 | if (!chan->sc_filter) | |
1056 | kfree(filter); | |
1057 | return ret; | |
1058 | } | |
1059 | ||
1060 | int lttng_syscall_filter_disable(struct lttng_channel *chan, | |
1061 | const char *name) | |
1062 | { | |
1063 | int syscall_nr, compat_syscall_nr, ret; | |
1064 | struct lttng_syscall_filter *filter; | |
1065 | ||
1066 | WARN_ON_ONCE(!chan->sc_table); | |
1067 | ||
1068 | if (!chan->sc_filter) { | |
bcde0d5b MD |
1069 | if (!chan->syscall_all) |
1070 | return -EEXIST; | |
80f87dd2 MD |
1071 | filter = kzalloc(sizeof(struct lttng_syscall_filter), |
1072 | GFP_KERNEL); | |
1073 | if (!filter) | |
1074 | return -ENOMEM; | |
1075 | /* Trace all system calls, then apply disable. */ | |
1076 | bitmap_set(filter->sc, 0, NR_syscalls); | |
1077 | bitmap_set(filter->sc_compat, 0, NR_compat_syscalls); | |
1078 | } else { | |
1079 | filter = chan->sc_filter; | |
1080 | } | |
1081 | ||
72814741 | 1082 | if (!name) { |
404e87bf MD |
1083 | /* Fail if all syscalls are already disabled. */ |
1084 | if (bitmap_empty(filter->sc, NR_syscalls) | |
1085 | && bitmap_empty(filter->sc_compat, | |
1086 | NR_compat_syscalls)) { | |
1087 | ret = -EEXIST; | |
1088 | goto error; | |
1089 | } | |
1090 | ||
72814741 MD |
1091 | /* Disable all system calls */ |
1092 | bitmap_clear(filter->sc, 0, NR_syscalls); | |
1093 | bitmap_clear(filter->sc_compat, 0, NR_compat_syscalls); | |
1094 | goto apply_filter; | |
1095 | } | |
80f87dd2 MD |
1096 | syscall_nr = get_syscall_nr(name); |
1097 | compat_syscall_nr = get_compat_syscall_nr(name); | |
1098 | if (syscall_nr < 0 && compat_syscall_nr < 0) { | |
1099 | ret = -ENOENT; | |
1100 | goto error; | |
1101 | } | |
1102 | if (syscall_nr >= 0) { | |
cecef7f8 | 1103 | if (!test_bit(syscall_nr, filter->sc)) { |
80f87dd2 MD |
1104 | ret = -EEXIST; |
1105 | goto error; | |
1106 | } | |
cecef7f8 | 1107 | bitmap_clear(filter->sc, syscall_nr, 1); |
80f87dd2 MD |
1108 | } |
1109 | if (compat_syscall_nr >= 0) { | |
cecef7f8 | 1110 | if (!test_bit(compat_syscall_nr, filter->sc_compat)) { |
80f87dd2 MD |
1111 | ret = -EEXIST; |
1112 | goto error; | |
1113 | } | |
cecef7f8 | 1114 | bitmap_clear(filter->sc_compat, compat_syscall_nr, 1); |
80f87dd2 | 1115 | } |
72814741 | 1116 | apply_filter: |
80f87dd2 MD |
1117 | if (!chan->sc_filter) |
1118 | rcu_assign_pointer(chan->sc_filter, filter); | |
1119 | chan->syscall_all = 0; | |
259b6cb3 | 1120 | return 0; |
80f87dd2 MD |
1121 | |
1122 | error: | |
1123 | if (!chan->sc_filter) | |
1124 | kfree(filter); | |
1125 | return ret; | |
259b6cb3 | 1126 | } |
2d2464bd MD |
1127 | |
1128 | static | |
1129 | const struct trace_syscall_entry *syscall_list_get_entry(loff_t *pos) | |
1130 | { | |
1131 | const struct trace_syscall_entry *entry; | |
1132 | int iter = 0; | |
1133 | ||
1134 | for (entry = sc_table; | |
1135 | entry < sc_table + ARRAY_SIZE(sc_table); | |
1136 | entry++) { | |
1137 | if (iter++ >= *pos) | |
1138 | return entry; | |
1139 | } | |
1140 | for (entry = compat_sc_table; | |
1141 | entry < compat_sc_table + ARRAY_SIZE(compat_sc_table); | |
1142 | entry++) { | |
1143 | if (iter++ >= *pos) | |
1144 | return entry; | |
1145 | } | |
1146 | /* End of list */ | |
1147 | return NULL; | |
1148 | } | |
1149 | ||
1150 | static | |
1151 | void *syscall_list_start(struct seq_file *m, loff_t *pos) | |
1152 | { | |
1153 | return (void *) syscall_list_get_entry(pos); | |
1154 | } | |
1155 | ||
1156 | static | |
1157 | void *syscall_list_next(struct seq_file *m, void *p, loff_t *ppos) | |
1158 | { | |
1159 | (*ppos)++; | |
1160 | return (void *) syscall_list_get_entry(ppos); | |
1161 | } | |
1162 | ||
1163 | static | |
1164 | void syscall_list_stop(struct seq_file *m, void *p) | |
1165 | { | |
1166 | } | |
1167 | ||
12e579db MD |
1168 | static |
1169 | int get_sc_table(const struct trace_syscall_entry *entry, | |
1170 | const struct trace_syscall_entry **table, | |
1171 | unsigned int *bitness) | |
1172 | { | |
1173 | if (entry >= sc_table && entry < sc_table + ARRAY_SIZE(sc_table)) { | |
1174 | if (bitness) | |
1175 | *bitness = BITS_PER_LONG; | |
1176 | if (table) | |
1177 | *table = sc_table; | |
1178 | return 0; | |
1179 | } | |
1180 | if (!(entry >= compat_sc_table | |
1181 | && entry < compat_sc_table + ARRAY_SIZE(compat_sc_table))) { | |
1182 | return -EINVAL; | |
1183 | } | |
1184 | if (bitness) | |
1185 | *bitness = 32; | |
1186 | if (table) | |
1187 | *table = compat_sc_table; | |
1188 | return 0; | |
1189 | } | |
1190 | ||
2d2464bd MD |
1191 | static |
1192 | int syscall_list_show(struct seq_file *m, void *p) | |
1193 | { | |
1194 | const struct trace_syscall_entry *table, *entry = p; | |
1195 | unsigned int bitness; | |
d4291869 | 1196 | unsigned long index; |
12e579db | 1197 | int ret; |
d4291869 | 1198 | const char *name; |
2d2464bd | 1199 | |
12e579db MD |
1200 | ret = get_sc_table(entry, &table, &bitness); |
1201 | if (ret) | |
1202 | return ret; | |
f4855b46 MD |
1203 | if (!entry->desc) |
1204 | return 0; | |
d4291869 MD |
1205 | if (table == sc_table) { |
1206 | index = entry - table; | |
1207 | name = &entry->desc->name[strlen(SYSCALL_ENTRY_STR)]; | |
1208 | } else { | |
1209 | index = (entry - table) + ARRAY_SIZE(sc_table); | |
1210 | name = &entry->desc->name[strlen(COMPAT_SYSCALL_ENTRY_STR)]; | |
1211 | } | |
12e579db | 1212 | seq_printf(m, "syscall { index = %lu; name = %s; bitness = %u; };\n", |
d4291869 | 1213 | index, name, bitness); |
2d2464bd MD |
1214 | return 0; |
1215 | } | |
1216 | ||
1217 | static | |
1218 | const struct seq_operations lttng_syscall_list_seq_ops = { | |
1219 | .start = syscall_list_start, | |
1220 | .next = syscall_list_next, | |
1221 | .stop = syscall_list_stop, | |
1222 | .show = syscall_list_show, | |
1223 | }; | |
1224 | ||
1225 | static | |
1226 | int lttng_syscall_list_open(struct inode *inode, struct file *file) | |
1227 | { | |
1228 | return seq_open(file, <tng_syscall_list_seq_ops); | |
1229 | } | |
1230 | ||
1231 | const struct file_operations lttng_syscall_list_fops = { | |
1232 | .owner = THIS_MODULE, | |
1233 | .open = lttng_syscall_list_open, | |
1234 | .read = seq_read, | |
1235 | .llseek = seq_lseek, | |
1236 | .release = seq_release, | |
1237 | }; | |
12e579db MD |
1238 | |
1239 | long lttng_channel_syscall_mask(struct lttng_channel *channel, | |
1240 | struct lttng_kernel_syscall_mask __user *usyscall_mask) | |
1241 | { | |
1242 | uint32_t len, sc_tables_len, bitmask_len; | |
1243 | int ret = 0, bit; | |
1244 | char *tmp_mask; | |
1245 | struct lttng_syscall_filter *filter; | |
1246 | ||
1247 | ret = get_user(len, &usyscall_mask->len); | |
1248 | if (ret) | |
1249 | return ret; | |
1250 | sc_tables_len = get_sc_tables_len(); | |
1251 | bitmask_len = ALIGN(sc_tables_len, 8) >> 3; | |
1252 | if (len < sc_tables_len) { | |
1253 | return put_user(sc_tables_len, &usyscall_mask->len); | |
1254 | } | |
1255 | /* Array is large enough, we can copy array to user-space. */ | |
1256 | tmp_mask = kzalloc(bitmask_len, GFP_KERNEL); | |
1257 | if (!tmp_mask) | |
1258 | return -ENOMEM; | |
1259 | filter = channel->sc_filter; | |
1260 | ||
1261 | for (bit = 0; bit < ARRAY_SIZE(sc_table); bit++) { | |
e2129868 | 1262 | char state; |
2f25059d MD |
1263 | |
1264 | if (channel->sc_table) { | |
1265 | if (filter) | |
1266 | state = test_bit(bit, filter->sc); | |
1267 | else | |
1268 | state = 1; | |
1269 | } else { | |
1270 | state = 0; | |
1271 | } | |
1272 | bt_bitfield_write_be(tmp_mask, char, bit, 1, state); | |
12e579db MD |
1273 | } |
1274 | for (; bit < sc_tables_len; bit++) { | |
e2129868 | 1275 | char state; |
2f25059d MD |
1276 | |
1277 | if (channel->compat_sc_table) { | |
1278 | if (filter) | |
1279 | state = test_bit(bit - ARRAY_SIZE(sc_table), | |
1280 | filter->sc_compat); | |
1281 | else | |
1282 | state = 1; | |
1283 | } else { | |
1284 | state = 0; | |
1285 | } | |
1286 | bt_bitfield_write_be(tmp_mask, char, bit, 1, state); | |
12e579db MD |
1287 | } |
1288 | if (copy_to_user(usyscall_mask->mask, tmp_mask, bitmask_len)) | |
1289 | ret = -EFAULT; | |
1290 | kfree(tmp_mask); | |
1291 | return ret; | |
1292 | } | |
082d4946 MD |
1293 | |
1294 | int lttng_abi_syscall_list(void) | |
1295 | { | |
1296 | struct file *syscall_list_file; | |
1297 | int file_fd, ret; | |
1298 | ||
4ac10b76 | 1299 | file_fd = lttng_get_unused_fd(); |
082d4946 MD |
1300 | if (file_fd < 0) { |
1301 | ret = file_fd; | |
1302 | goto fd_error; | |
1303 | } | |
1304 | ||
1305 | syscall_list_file = anon_inode_getfile("[lttng_syscall_list]", | |
1306 | <tng_syscall_list_fops, | |
1307 | NULL, O_RDWR); | |
1308 | if (IS_ERR(syscall_list_file)) { | |
1309 | ret = PTR_ERR(syscall_list_file); | |
1310 | goto file_error; | |
1311 | } | |
1312 | ret = lttng_syscall_list_fops.open(NULL, syscall_list_file); | |
1313 | if (ret < 0) | |
1314 | goto open_error; | |
1315 | fd_install(file_fd, syscall_list_file); | |
082d4946 MD |
1316 | return file_fd; |
1317 | ||
1318 | open_error: | |
1319 | fput(syscall_list_file); | |
1320 | file_error: | |
1321 | put_unused_fd(file_fd); | |
1322 | fd_error: | |
1323 | return ret; | |
1324 | } |