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