Commit | Line | Data |
---|---|---|
9f36eaed MJ |
1 | /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1) |
2 | * | |
e8951e63 | 3 | * lttng-abi.c |
baf20995 | 4 | * |
e8951e63 | 5 | * LTTng ABI |
baf20995 | 6 | * |
886d51a3 MD |
7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
8 | * | |
baf20995 MD |
9 | * Mimic system calls for: |
10 | * - session creation, returns a file descriptor or failure. | |
ad1c05e1 MD |
11 | * - channel creation, returns a file descriptor or failure. |
12 | * - Operates on a session file descriptor | |
13 | * - Takes all channel options as parameters. | |
14 | * - stream get, returns a file descriptor or failure. | |
15 | * - Operates on a channel file descriptor. | |
16 | * - stream notifier get, returns a file descriptor or failure. | |
17 | * - Operates on a channel file descriptor. | |
18 | * - event creation, returns a file descriptor or failure. | |
19 | * - Operates on a channel file descriptor | |
20 | * - Takes an event name as parameter | |
21 | * - Takes an instrumentation source as parameter | |
22 | * - e.g. tracepoints, dynamic_probes... | |
23 | * - Takes instrumentation source specific arguments. | |
baf20995 MD |
24 | */ |
25 | ||
11b5a3c2 | 26 | #include <linux/module.h> |
e6a17f26 | 27 | #include <linux/proc_fs.h> |
11b5a3c2 MD |
28 | #include <linux/anon_inodes.h> |
29 | #include <linux/file.h> | |
30 | #include <linux/uaccess.h> | |
31 | #include <linux/slab.h> | |
abc0446a | 32 | #include <linux/err.h> |
241ae9a8 MD |
33 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */ |
34 | #include <wrapper/ringbuffer/vfs.h> | |
35 | #include <wrapper/ringbuffer/backend.h> | |
36 | #include <wrapper/ringbuffer/frontend.h> | |
37 | #include <wrapper/poll.h> | |
38 | #include <wrapper/file.h> | |
39 | #include <wrapper/kref.h> | |
4993071a | 40 | #include <lttng-string-utils.h> |
241ae9a8 MD |
41 | #include <lttng-abi.h> |
42 | #include <lttng-abi-old.h> | |
43 | #include <lttng-events.h> | |
44 | #include <lttng-tracer.h> | |
f771eda6 | 45 | #include <lttng-tp-mempool.h> |
241ae9a8 | 46 | #include <lib/ringbuffer/frontend_types.h> |
baf20995 MD |
47 | |
48 | /* | |
49 | * This is LTTng's own personal way to create a system call as an external | |
80996790 | 50 | * module. We use ioctl() on /proc/lttng. |
baf20995 MD |
51 | */ |
52 | ||
e6a17f26 | 53 | static struct proc_dir_entry *lttng_proc_dentry; |
ad1c05e1 MD |
54 | static const struct file_operations lttng_fops; |
55 | static const struct file_operations lttng_session_fops; | |
56 | static const struct file_operations lttng_channel_fops; | |
5dbbdb43 | 57 | static const struct file_operations lttng_metadata_fops; |
305d3e42 | 58 | static const struct file_operations lttng_event_fops; |
ed8d02d6 | 59 | static struct file_operations lttng_stream_ring_buffer_file_operations; |
baf20995 | 60 | |
9616f0bf JD |
61 | static int put_u64(uint64_t val, unsigned long arg); |
62 | ||
a33c9927 MD |
63 | /* |
64 | * Teardown management: opened file descriptors keep a refcount on the module, | |
65 | * so it can only exit when all file descriptors are closed. | |
66 | */ | |
67 | ||
ad1c05e1 | 68 | static |
baf20995 MD |
69 | int lttng_abi_create_session(void) |
70 | { | |
a90917c3 | 71 | struct lttng_session *session; |
c0e31d2e | 72 | struct file *session_file; |
11b5a3c2 | 73 | int session_fd, ret; |
baf20995 | 74 | |
a90917c3 | 75 | session = lttng_session_create(); |
baf20995 MD |
76 | if (!session) |
77 | return -ENOMEM; | |
4ac10b76 | 78 | session_fd = lttng_get_unused_fd(); |
baf20995 MD |
79 | if (session_fd < 0) { |
80 | ret = session_fd; | |
81 | goto fd_error; | |
82 | } | |
c0e31d2e | 83 | session_file = anon_inode_getfile("[lttng_session]", |
ad1c05e1 | 84 | <tng_session_fops, |
baf20995 | 85 | session, O_RDWR); |
c0e31d2e MD |
86 | if (IS_ERR(session_file)) { |
87 | ret = PTR_ERR(session_file); | |
baf20995 MD |
88 | goto file_error; |
89 | } | |
c0e31d2e MD |
90 | session->file = session_file; |
91 | fd_install(session_fd, session_file); | |
baf20995 MD |
92 | return session_fd; |
93 | ||
94 | file_error: | |
95 | put_unused_fd(session_fd); | |
96 | fd_error: | |
a90917c3 | 97 | lttng_session_destroy(session); |
baf20995 MD |
98 | return ret; |
99 | } | |
100 | ||
271b6681 MD |
101 | static |
102 | int lttng_abi_tracepoint_list(void) | |
103 | { | |
104 | struct file *tracepoint_list_file; | |
105 | int file_fd, ret; | |
106 | ||
4ac10b76 | 107 | file_fd = lttng_get_unused_fd(); |
271b6681 MD |
108 | if (file_fd < 0) { |
109 | ret = file_fd; | |
110 | goto fd_error; | |
111 | } | |
30f18bf0 | 112 | |
8ee099b6 | 113 | tracepoint_list_file = anon_inode_getfile("[lttng_tracepoint_list]", |
271b6681 MD |
114 | <tng_tracepoint_list_fops, |
115 | NULL, O_RDWR); | |
116 | if (IS_ERR(tracepoint_list_file)) { | |
117 | ret = PTR_ERR(tracepoint_list_file); | |
118 | goto file_error; | |
119 | } | |
30f18bf0 MD |
120 | ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file); |
121 | if (ret < 0) | |
122 | goto open_error; | |
271b6681 MD |
123 | fd_install(file_fd, tracepoint_list_file); |
124 | return file_fd; | |
125 | ||
30f18bf0 MD |
126 | open_error: |
127 | fput(tracepoint_list_file); | |
271b6681 MD |
128 | file_error: |
129 | put_unused_fd(file_fd); | |
130 | fd_error: | |
131 | return ret; | |
132 | } | |
133 | ||
f127e61e MD |
134 | #ifndef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
135 | static inline | |
136 | int lttng_abi_syscall_list(void) | |
137 | { | |
138 | return -ENOSYS; | |
139 | } | |
140 | #else | |
141 | static | |
142 | int lttng_abi_syscall_list(void) | |
143 | { | |
144 | struct file *syscall_list_file; | |
145 | int file_fd, ret; | |
146 | ||
147 | file_fd = lttng_get_unused_fd(); | |
148 | if (file_fd < 0) { | |
149 | ret = file_fd; | |
150 | goto fd_error; | |
151 | } | |
152 | ||
153 | syscall_list_file = anon_inode_getfile("[lttng_syscall_list]", | |
154 | <tng_syscall_list_fops, | |
155 | NULL, O_RDWR); | |
156 | if (IS_ERR(syscall_list_file)) { | |
157 | ret = PTR_ERR(syscall_list_file); | |
158 | goto file_error; | |
159 | } | |
160 | ret = lttng_syscall_list_fops.open(NULL, syscall_list_file); | |
161 | if (ret < 0) | |
162 | goto open_error; | |
163 | fd_install(file_fd, syscall_list_file); | |
f127e61e MD |
164 | return file_fd; |
165 | ||
166 | open_error: | |
167 | fput(syscall_list_file); | |
168 | file_error: | |
169 | put_unused_fd(file_fd); | |
170 | fd_error: | |
171 | return ret; | |
172 | } | |
173 | #endif | |
174 | ||
80c16bcf | 175 | static |
6dccd6c1 | 176 | void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v) |
80c16bcf | 177 | { |
6dccd6c1 JD |
178 | v->major = LTTNG_MODULES_MAJOR_VERSION; |
179 | v->minor = LTTNG_MODULES_MINOR_VERSION; | |
180 | v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION; | |
80c16bcf MD |
181 | } |
182 | ||
42cabb80 MD |
183 | static |
184 | void lttng_abi_tracer_abi_version(struct lttng_kernel_tracer_abi_version *v) | |
185 | { | |
186 | v->major = LTTNG_MODULES_ABI_MAJOR_VERSION; | |
187 | v->minor = LTTNG_MODULES_ABI_MINOR_VERSION; | |
188 | } | |
189 | ||
8070f5c0 MD |
190 | static |
191 | long lttng_abi_add_context(struct file *file, | |
6dccd6c1 | 192 | struct lttng_kernel_context *context_param, |
a90917c3 | 193 | struct lttng_ctx **ctx, struct lttng_session *session) |
8070f5c0 | 194 | { |
8070f5c0 MD |
195 | |
196 | if (session->been_active) | |
197 | return -EPERM; | |
198 | ||
6dccd6c1 | 199 | switch (context_param->ctx) { |
12a313a5 | 200 | case LTTNG_KERNEL_CONTEXT_PID: |
8070f5c0 | 201 | return lttng_add_pid_to_ctx(ctx); |
a8ad3613 MD |
202 | case LTTNG_KERNEL_CONTEXT_PRIO: |
203 | return lttng_add_prio_to_ctx(ctx); | |
53f1f0ca MD |
204 | case LTTNG_KERNEL_CONTEXT_NICE: |
205 | return lttng_add_nice_to_ctx(ctx); | |
b64bc438 MD |
206 | case LTTNG_KERNEL_CONTEXT_VPID: |
207 | return lttng_add_vpid_to_ctx(ctx); | |
208 | case LTTNG_KERNEL_CONTEXT_TID: | |
209 | return lttng_add_tid_to_ctx(ctx); | |
210 | case LTTNG_KERNEL_CONTEXT_VTID: | |
211 | return lttng_add_vtid_to_ctx(ctx); | |
212 | case LTTNG_KERNEL_CONTEXT_PPID: | |
213 | return lttng_add_ppid_to_ctx(ctx); | |
214 | case LTTNG_KERNEL_CONTEXT_VPPID: | |
215 | return lttng_add_vppid_to_ctx(ctx); | |
12a313a5 | 216 | case LTTNG_KERNEL_CONTEXT_PERF_COUNTER: |
6dccd6c1 JD |
217 | context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
218 | return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type, | |
219 | context_param->u.perf_counter.config, | |
220 | context_param->u.perf_counter.name, | |
c24a0d71 | 221 | ctx); |
a2563e83 MD |
222 | case LTTNG_KERNEL_CONTEXT_PROCNAME: |
223 | return lttng_add_procname_to_ctx(ctx); | |
975da2c0 JD |
224 | case LTTNG_KERNEL_CONTEXT_HOSTNAME: |
225 | return lttng_add_hostname_to_ctx(ctx); | |
b3699d90 MD |
226 | case LTTNG_KERNEL_CONTEXT_CPU_ID: |
227 | return lttng_add_cpu_id_to_ctx(ctx); | |
79150a49 JD |
228 | case LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE: |
229 | return lttng_add_interruptible_to_ctx(ctx); | |
230 | case LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE: | |
231 | return lttng_add_need_reschedule_to_ctx(ctx); | |
232 | case LTTNG_KERNEL_CONTEXT_PREEMPTIBLE: | |
233 | return lttng_add_preemptible_to_ctx(ctx); | |
234 | case LTTNG_KERNEL_CONTEXT_MIGRATABLE: | |
235 | return lttng_add_migratable_to_ctx(ctx); | |
2fa2d39a FG |
236 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL: |
237 | case LTTNG_KERNEL_CONTEXT_CALLSTACK_USER: | |
238 | return lttng_add_callstack_to_ctx(ctx, context_param->ctx); | |
a6cf40a4 MJ |
239 | case LTTNG_KERNEL_CONTEXT_CGROUP_NS: |
240 | return lttng_add_cgroup_ns_to_ctx(ctx); | |
241 | case LTTNG_KERNEL_CONTEXT_IPC_NS: | |
242 | return lttng_add_ipc_ns_to_ctx(ctx); | |
243 | case LTTNG_KERNEL_CONTEXT_MNT_NS: | |
244 | return lttng_add_mnt_ns_to_ctx(ctx); | |
245 | case LTTNG_KERNEL_CONTEXT_NET_NS: | |
246 | return lttng_add_net_ns_to_ctx(ctx); | |
247 | case LTTNG_KERNEL_CONTEXT_PID_NS: | |
248 | return lttng_add_pid_ns_to_ctx(ctx); | |
249 | case LTTNG_KERNEL_CONTEXT_USER_NS: | |
250 | return lttng_add_user_ns_to_ctx(ctx); | |
251 | case LTTNG_KERNEL_CONTEXT_UTS_NS: | |
252 | return lttng_add_uts_ns_to_ctx(ctx); | |
dc923e75 MJ |
253 | case LTTNG_KERNEL_CONTEXT_UID: |
254 | return lttng_add_uid_to_ctx(ctx); | |
255 | case LTTNG_KERNEL_CONTEXT_EUID: | |
256 | return lttng_add_euid_to_ctx(ctx); | |
257 | case LTTNG_KERNEL_CONTEXT_SUID: | |
258 | return lttng_add_suid_to_ctx(ctx); | |
259 | case LTTNG_KERNEL_CONTEXT_GID: | |
260 | return lttng_add_gid_to_ctx(ctx); | |
261 | case LTTNG_KERNEL_CONTEXT_EGID: | |
262 | return lttng_add_egid_to_ctx(ctx); | |
263 | case LTTNG_KERNEL_CONTEXT_SGID: | |
264 | return lttng_add_sgid_to_ctx(ctx); | |
265 | case LTTNG_KERNEL_CONTEXT_VUID: | |
266 | return lttng_add_vuid_to_ctx(ctx); | |
267 | case LTTNG_KERNEL_CONTEXT_VEUID: | |
268 | return lttng_add_veuid_to_ctx(ctx); | |
269 | case LTTNG_KERNEL_CONTEXT_VSUID: | |
270 | return lttng_add_vsuid_to_ctx(ctx); | |
271 | case LTTNG_KERNEL_CONTEXT_VGID: | |
272 | return lttng_add_vgid_to_ctx(ctx); | |
273 | case LTTNG_KERNEL_CONTEXT_VEGID: | |
274 | return lttng_add_vegid_to_ctx(ctx); | |
275 | case LTTNG_KERNEL_CONTEXT_VSGID: | |
276 | return lttng_add_vsgid_to_ctx(ctx); | |
8070f5c0 MD |
277 | default: |
278 | return -EINVAL; | |
279 | } | |
280 | } | |
281 | ||
ad1c05e1 MD |
282 | /** |
283 | * lttng_ioctl - lttng syscall through ioctl | |
284 | * | |
c0e31d2e | 285 | * @file: the file |
ad1c05e1 MD |
286 | * @cmd: the command |
287 | * @arg: command arg | |
288 | * | |
289 | * This ioctl implements lttng commands: | |
38d024ae | 290 | * LTTNG_KERNEL_SESSION |
ad1c05e1 | 291 | * Returns a LTTng trace session file descriptor |
271b6681 MD |
292 | * LTTNG_KERNEL_TRACER_VERSION |
293 | * Returns the LTTng kernel tracer version | |
294 | * LTTNG_KERNEL_TRACEPOINT_LIST | |
295 | * Returns a file descriptor listing available tracepoints | |
360f38ea MD |
296 | * LTTNG_KERNEL_WAIT_QUIESCENT |
297 | * Returns after all previously running probes have completed | |
42cabb80 MD |
298 | * LTTNG_KERNEL_TRACER_ABI_VERSION |
299 | * Returns the LTTng kernel tracer ABI version | |
ad1c05e1 MD |
300 | * |
301 | * The returned session will be deleted when its file descriptor is closed. | |
302 | */ | |
303 | static | |
c0e31d2e | 304 | long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 MD |
305 | { |
306 | switch (cmd) { | |
6dccd6c1 | 307 | case LTTNG_KERNEL_OLD_SESSION: |
38d024ae | 308 | case LTTNG_KERNEL_SESSION: |
ad1c05e1 | 309 | return lttng_abi_create_session(); |
6dccd6c1 JD |
310 | case LTTNG_KERNEL_OLD_TRACER_VERSION: |
311 | { | |
312 | struct lttng_kernel_tracer_version v; | |
313 | struct lttng_kernel_old_tracer_version oldv; | |
314 | struct lttng_kernel_old_tracer_version *uversion = | |
315 | (struct lttng_kernel_old_tracer_version __user *) arg; | |
316 | ||
317 | lttng_abi_tracer_version(&v); | |
318 | oldv.major = v.major; | |
319 | oldv.minor = v.minor; | |
320 | oldv.patchlevel = v.patchlevel; | |
321 | ||
322 | if (copy_to_user(uversion, &oldv, sizeof(oldv))) | |
323 | return -EFAULT; | |
324 | return 0; | |
325 | } | |
80c16bcf | 326 | case LTTNG_KERNEL_TRACER_VERSION: |
6dccd6c1 JD |
327 | { |
328 | struct lttng_kernel_tracer_version version; | |
329 | struct lttng_kernel_tracer_version *uversion = | |
330 | (struct lttng_kernel_tracer_version __user *) arg; | |
331 | ||
332 | lttng_abi_tracer_version(&version); | |
42cabb80 MD |
333 | |
334 | if (copy_to_user(uversion, &version, sizeof(version))) | |
335 | return -EFAULT; | |
336 | return 0; | |
337 | } | |
338 | case LTTNG_KERNEL_TRACER_ABI_VERSION: | |
339 | { | |
340 | struct lttng_kernel_tracer_abi_version version; | |
341 | struct lttng_kernel_tracer_abi_version *uversion = | |
342 | (struct lttng_kernel_tracer_abi_version __user *) arg; | |
343 | ||
344 | lttng_abi_tracer_abi_version(&version); | |
345 | ||
6dccd6c1 JD |
346 | if (copy_to_user(uversion, &version, sizeof(version))) |
347 | return -EFAULT; | |
348 | return 0; | |
349 | } | |
350 | case LTTNG_KERNEL_OLD_TRACEPOINT_LIST: | |
271b6681 MD |
351 | case LTTNG_KERNEL_TRACEPOINT_LIST: |
352 | return lttng_abi_tracepoint_list(); | |
2d2464bd MD |
353 | case LTTNG_KERNEL_SYSCALL_LIST: |
354 | return lttng_abi_syscall_list(); | |
6dccd6c1 | 355 | case LTTNG_KERNEL_OLD_WAIT_QUIESCENT: |
5f7f9078 MD |
356 | case LTTNG_KERNEL_WAIT_QUIESCENT: |
357 | synchronize_trace(); | |
358 | return 0; | |
6dccd6c1 JD |
359 | case LTTNG_KERNEL_OLD_CALIBRATE: |
360 | { | |
361 | struct lttng_kernel_old_calibrate __user *ucalibrate = | |
362 | (struct lttng_kernel_old_calibrate __user *) arg; | |
363 | struct lttng_kernel_old_calibrate old_calibrate; | |
364 | struct lttng_kernel_calibrate calibrate; | |
365 | int ret; | |
366 | ||
367 | if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate))) | |
368 | return -EFAULT; | |
369 | calibrate.type = old_calibrate.type; | |
370 | ret = lttng_calibrate(&calibrate); | |
371 | if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate))) | |
372 | return -EFAULT; | |
373 | return ret; | |
374 | } | |
57105fc2 MD |
375 | case LTTNG_KERNEL_CALIBRATE: |
376 | { | |
3db41b2c MD |
377 | struct lttng_kernel_calibrate __user *ucalibrate = |
378 | (struct lttng_kernel_calibrate __user *) arg; | |
379 | struct lttng_kernel_calibrate calibrate; | |
57105fc2 MD |
380 | int ret; |
381 | ||
382 | if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate))) | |
383 | return -EFAULT; | |
384 | ret = lttng_calibrate(&calibrate); | |
385 | if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate))) | |
386 | return -EFAULT; | |
387 | return ret; | |
388 | } | |
ad1c05e1 MD |
389 | default: |
390 | return -ENOIOCTLCMD; | |
391 | } | |
392 | } | |
393 | ||
ad1c05e1 | 394 | static const struct file_operations lttng_fops = { |
a33c9927 | 395 | .owner = THIS_MODULE, |
ad1c05e1 MD |
396 | .unlocked_ioctl = lttng_ioctl, |
397 | #ifdef CONFIG_COMPAT | |
03037b98 | 398 | .compat_ioctl = lttng_ioctl, |
ad1c05e1 | 399 | #endif |
11b5a3c2 | 400 | }; |
ad1c05e1 | 401 | |
5dbbdb43 | 402 | static |
c0e31d2e | 403 | int lttng_abi_create_channel(struct file *session_file, |
6dccd6c1 | 404 | struct lttng_kernel_channel *chan_param, |
5dbbdb43 | 405 | enum channel_type channel_type) |
baf20995 | 406 | { |
a90917c3 | 407 | struct lttng_session *session = session_file->private_data; |
88dfd899 | 408 | const struct file_operations *fops = NULL; |
5dbbdb43 | 409 | const char *transport_name; |
a90917c3 | 410 | struct lttng_channel *chan; |
c0e31d2e | 411 | struct file *chan_file; |
baf20995 | 412 | int chan_fd; |
ad1c05e1 | 413 | int ret = 0; |
baf20995 | 414 | |
4ac10b76 | 415 | chan_fd = lttng_get_unused_fd(); |
baf20995 MD |
416 | if (chan_fd < 0) { |
417 | ret = chan_fd; | |
418 | goto fd_error; | |
419 | } | |
88dfd899 MD |
420 | switch (channel_type) { |
421 | case PER_CPU_CHANNEL: | |
422 | fops = <tng_channel_fops; | |
423 | break; | |
424 | case METADATA_CHANNEL: | |
425 | fops = <tng_metadata_fops; | |
426 | break; | |
427 | } | |
2470a237 | 428 | |
c0e31d2e | 429 | chan_file = anon_inode_getfile("[lttng_channel]", |
88dfd899 | 430 | fops, |
03037b98 | 431 | NULL, O_RDWR); |
c0e31d2e MD |
432 | if (IS_ERR(chan_file)) { |
433 | ret = PTR_ERR(chan_file); | |
baf20995 MD |
434 | goto file_error; |
435 | } | |
5dbbdb43 MD |
436 | switch (channel_type) { |
437 | case PER_CPU_CHANNEL: | |
6dccd6c1 JD |
438 | if (chan_param->output == LTTNG_KERNEL_SPLICE) { |
439 | transport_name = chan_param->overwrite ? | |
96ba7208 | 440 | "relay-overwrite" : "relay-discard"; |
6dccd6c1 JD |
441 | } else if (chan_param->output == LTTNG_KERNEL_MMAP) { |
442 | transport_name = chan_param->overwrite ? | |
96ba7208 JD |
443 | "relay-overwrite-mmap" : "relay-discard-mmap"; |
444 | } else { | |
445 | return -EINVAL; | |
446 | } | |
5dbbdb43 | 447 | break; |
5dbbdb43 | 448 | case METADATA_CHANNEL: |
6dccd6c1 | 449 | if (chan_param->output == LTTNG_KERNEL_SPLICE) |
96ba7208 | 450 | transport_name = "relay-metadata"; |
6dccd6c1 | 451 | else if (chan_param->output == LTTNG_KERNEL_MMAP) |
96ba7208 JD |
452 | transport_name = "relay-metadata-mmap"; |
453 | else | |
454 | return -EINVAL; | |
5dbbdb43 MD |
455 | break; |
456 | default: | |
457 | transport_name = "<unknown>"; | |
458 | break; | |
459 | } | |
98d7281c MJ |
460 | if (!atomic_long_add_unless(&session_file->f_count, 1, LONG_MAX)) { |
461 | ret = -EOVERFLOW; | |
9c1f4643 MD |
462 | goto refcount_error; |
463 | } | |
03037b98 MD |
464 | /* |
465 | * We tolerate no failure path after channel creation. It will stay | |
466 | * invariant for the rest of the session. | |
467 | */ | |
a90917c3 | 468 | chan = lttng_channel_create(session, transport_name, NULL, |
6dccd6c1 JD |
469 | chan_param->subbuf_size, |
470 | chan_param->num_subbuf, | |
471 | chan_param->switch_timer_interval, | |
d83004aa JD |
472 | chan_param->read_timer_interval, |
473 | channel_type); | |
03037b98 | 474 | if (!chan) { |
f3d01b96 | 475 | ret = -EINVAL; |
03037b98 MD |
476 | goto chan_error; |
477 | } | |
11b5a3c2 | 478 | chan->file = chan_file; |
c0e31d2e MD |
479 | chan_file->private_data = chan; |
480 | fd_install(chan_fd, chan_file); | |
ad1c05e1 | 481 | |
baf20995 MD |
482 | return chan_fd; |
483 | ||
03037b98 | 484 | chan_error: |
9c1f4643 MD |
485 | atomic_long_dec(&session_file->f_count); |
486 | refcount_error: | |
c0e31d2e | 487 | fput(chan_file); |
baf20995 MD |
488 | file_error: |
489 | put_unused_fd(chan_fd); | |
490 | fd_error: | |
baf20995 MD |
491 | return ret; |
492 | } | |
493 | ||
7f859fbf JR |
494 | static |
495 | int lttng_abi_session_set_name(struct lttng_session *session, | |
496 | struct lttng_kernel_session_name *name) | |
497 | { | |
498 | size_t len; | |
499 | ||
500 | len = strnlen(name->name, LTTNG_KERNEL_SESSION_NAME_LEN); | |
501 | ||
502 | if (len == LTTNG_KERNEL_SESSION_NAME_LEN) { | |
503 | /* Name is too long/malformed */ | |
504 | return -EINVAL; | |
505 | } | |
506 | ||
507 | strcpy(session->name, name->name); | |
508 | return 0; | |
509 | } | |
510 | ||
1c88f269 JR |
511 | static |
512 | int lttng_abi_session_set_creation_time(struct lttng_session *session, | |
513 | struct lttng_kernel_session_creation_time *time) | |
514 | { | |
515 | size_t len; | |
516 | ||
517 | len = strnlen(time->iso8601, LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN); | |
518 | ||
519 | if (len == LTTNG_KERNEL_SESSION_CREATION_TIME_ISO8601_LEN) { | |
520 | /* Time is too long/malformed */ | |
521 | return -EINVAL; | |
522 | } | |
523 | ||
524 | strcpy(session->creation_time, time->iso8601); | |
525 | return 0; | |
526 | } | |
527 | ||
d1f652f8 MD |
528 | static |
529 | enum tracker_type get_tracker_type(struct lttng_kernel_tracker_args *tracker) | |
530 | { | |
531 | switch (tracker->type) { | |
532 | case LTTNG_KERNEL_TRACKER_PID: | |
533 | return TRACKER_PID; | |
534 | case LTTNG_KERNEL_TRACKER_VPID: | |
535 | return TRACKER_VPID; | |
536 | case LTTNG_KERNEL_TRACKER_UID: | |
537 | return TRACKER_UID; | |
538 | case LTTNG_KERNEL_TRACKER_VUID: | |
539 | return TRACKER_VUID; | |
540 | case LTTNG_KERNEL_TRACKER_GID: | |
541 | return TRACKER_GID; | |
542 | case LTTNG_KERNEL_TRACKER_VGID: | |
543 | return TRACKER_VGID; | |
544 | default: | |
545 | return TRACKER_UNKNOWN; | |
546 | } | |
547 | } | |
548 | ||
baf20995 | 549 | /** |
ad1c05e1 | 550 | * lttng_session_ioctl - lttng session fd ioctl |
baf20995 | 551 | * |
c0e31d2e | 552 | * @file: the file |
baf20995 MD |
553 | * @cmd: the command |
554 | * @arg: command arg | |
555 | * | |
556 | * This ioctl implements lttng commands: | |
38d024ae | 557 | * LTTNG_KERNEL_CHANNEL |
baf20995 | 558 | * Returns a LTTng channel file descriptor |
e64957da MD |
559 | * LTTNG_KERNEL_ENABLE |
560 | * Enables tracing for a session (weak enable) | |
561 | * LTTNG_KERNEL_DISABLE | |
562 | * Disables tracing for a session (strong disable) | |
8070f5c0 MD |
563 | * LTTNG_KERNEL_METADATA |
564 | * Returns a LTTng metadata file descriptor | |
e0130fab | 565 | * LTTNG_KERNEL_SESSION_TRACK_PID |
d1f652f8 | 566 | * Add PID to session PID tracker |
e0130fab | 567 | * LTTNG_KERNEL_SESSION_UNTRACK_PID |
d1f652f8 MD |
568 | * Remove PID from session PID tracker |
569 | * LTTNG_KERNEL_SESSION_TRACK_ID | |
570 | * Add ID to tracker | |
571 | * LTTNG_KERNEL_SESSION_UNTRACK_ID | |
572 | * Remove ID from tracker | |
ad1c05e1 MD |
573 | * |
574 | * The returned channel will be deleted when its file descriptor is closed. | |
575 | */ | |
576 | static | |
c0e31d2e | 577 | long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ad1c05e1 | 578 | { |
a90917c3 | 579 | struct lttng_session *session = file->private_data; |
2d4c4d15 MD |
580 | struct lttng_kernel_channel chan_param; |
581 | struct lttng_kernel_old_channel old_chan_param; | |
c0e31d2e | 582 | |
ad1c05e1 | 583 | switch (cmd) { |
6dccd6c1 JD |
584 | case LTTNG_KERNEL_OLD_CHANNEL: |
585 | { | |
6dccd6c1 JD |
586 | if (copy_from_user(&old_chan_param, |
587 | (struct lttng_kernel_old_channel __user *) arg, | |
588 | sizeof(struct lttng_kernel_old_channel))) | |
589 | return -EFAULT; | |
590 | chan_param.overwrite = old_chan_param.overwrite; | |
591 | chan_param.subbuf_size = old_chan_param.subbuf_size; | |
592 | chan_param.num_subbuf = old_chan_param.num_subbuf; | |
593 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; | |
594 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; | |
595 | chan_param.output = old_chan_param.output; | |
596 | ||
597 | return lttng_abi_create_channel(file, &chan_param, | |
598 | PER_CPU_CHANNEL); | |
599 | } | |
38d024ae | 600 | case LTTNG_KERNEL_CHANNEL: |
6dccd6c1 | 601 | { |
6dccd6c1 | 602 | if (copy_from_user(&chan_param, |
38d024ae | 603 | (struct lttng_kernel_channel __user *) arg, |
6dccd6c1 JD |
604 | sizeof(struct lttng_kernel_channel))) |
605 | return -EFAULT; | |
606 | return lttng_abi_create_channel(file, &chan_param, | |
5dbbdb43 | 607 | PER_CPU_CHANNEL); |
6dccd6c1 JD |
608 | } |
609 | case LTTNG_KERNEL_OLD_SESSION_START: | |
610 | case LTTNG_KERNEL_OLD_ENABLE: | |
38d024ae | 611 | case LTTNG_KERNEL_SESSION_START: |
e64957da | 612 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 613 | return lttng_session_enable(session); |
6dccd6c1 JD |
614 | case LTTNG_KERNEL_OLD_SESSION_STOP: |
615 | case LTTNG_KERNEL_OLD_DISABLE: | |
38d024ae | 616 | case LTTNG_KERNEL_SESSION_STOP: |
e64957da | 617 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 618 | return lttng_session_disable(session); |
6dccd6c1 JD |
619 | case LTTNG_KERNEL_OLD_METADATA: |
620 | { | |
6dccd6c1 JD |
621 | if (copy_from_user(&old_chan_param, |
622 | (struct lttng_kernel_old_channel __user *) arg, | |
623 | sizeof(struct lttng_kernel_old_channel))) | |
624 | return -EFAULT; | |
625 | chan_param.overwrite = old_chan_param.overwrite; | |
626 | chan_param.subbuf_size = old_chan_param.subbuf_size; | |
627 | chan_param.num_subbuf = old_chan_param.num_subbuf; | |
628 | chan_param.switch_timer_interval = old_chan_param.switch_timer_interval; | |
629 | chan_param.read_timer_interval = old_chan_param.read_timer_interval; | |
630 | chan_param.output = old_chan_param.output; | |
631 | ||
632 | return lttng_abi_create_channel(file, &chan_param, | |
633 | METADATA_CHANNEL); | |
634 | } | |
38d024ae | 635 | case LTTNG_KERNEL_METADATA: |
6dccd6c1 | 636 | { |
6dccd6c1 JD |
637 | if (copy_from_user(&chan_param, |
638 | (struct lttng_kernel_channel __user *) arg, | |
639 | sizeof(struct lttng_kernel_channel))) | |
640 | return -EFAULT; | |
641 | return lttng_abi_create_channel(file, &chan_param, | |
5dbbdb43 | 642 | METADATA_CHANNEL); |
6dccd6c1 | 643 | } |
e0130fab | 644 | case LTTNG_KERNEL_SESSION_TRACK_PID: |
d1f652f8 | 645 | return lttng_session_track_id(session, TRACKER_PID, (int) arg); |
e0130fab | 646 | case LTTNG_KERNEL_SESSION_UNTRACK_PID: |
d1f652f8 MD |
647 | return lttng_session_untrack_id(session, TRACKER_PID, (int) arg); |
648 | case LTTNG_KERNEL_SESSION_TRACK_ID: | |
649 | { | |
650 | struct lttng_kernel_tracker_args tracker; | |
651 | enum tracker_type tracker_type; | |
652 | ||
653 | if (copy_from_user(&tracker, | |
654 | (struct lttng_kernel_tracker_args __user *) arg, | |
655 | sizeof(struct lttng_kernel_tracker_args))) | |
656 | return -EFAULT; | |
657 | tracker_type = get_tracker_type(&tracker); | |
658 | if (tracker_type == TRACKER_UNKNOWN) | |
659 | return -EINVAL; | |
660 | return lttng_session_track_id(session, tracker_type, tracker.id); | |
661 | } | |
662 | case LTTNG_KERNEL_SESSION_UNTRACK_ID: | |
663 | { | |
664 | struct lttng_kernel_tracker_args tracker; | |
665 | enum tracker_type tracker_type; | |
666 | ||
667 | if (copy_from_user(&tracker, | |
668 | (struct lttng_kernel_tracker_args __user *) arg, | |
669 | sizeof(struct lttng_kernel_tracker_args))) | |
670 | return -EFAULT; | |
671 | tracker_type = get_tracker_type(&tracker); | |
672 | if (tracker_type == TRACKER_UNKNOWN) | |
673 | return -EINVAL; | |
674 | return lttng_session_untrack_id(session, tracker_type, | |
675 | tracker.id); | |
676 | } | |
7e6f9ef6 | 677 | case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS: |
d1f652f8 MD |
678 | return lttng_session_list_tracker_ids(session, TRACKER_PID); |
679 | case LTTNG_KERNEL_SESSION_LIST_TRACKER_IDS: | |
680 | { | |
681 | struct lttng_kernel_tracker_args tracker; | |
682 | enum tracker_type tracker_type; | |
683 | ||
684 | if (copy_from_user(&tracker, | |
685 | (struct lttng_kernel_tracker_args __user *) arg, | |
686 | sizeof(struct lttng_kernel_tracker_args))) | |
687 | return -EFAULT; | |
688 | tracker_type = get_tracker_type(&tracker); | |
689 | if (tracker_type == TRACKER_UNKNOWN) | |
690 | return -EINVAL; | |
691 | return lttng_session_list_tracker_ids(session, tracker_type); | |
692 | } | |
9616f0bf JD |
693 | case LTTNG_KERNEL_SESSION_METADATA_REGEN: |
694 | return lttng_session_metadata_regenerate(session); | |
601252cf MD |
695 | case LTTNG_KERNEL_SESSION_STATEDUMP: |
696 | return lttng_session_statedump(session); | |
7f859fbf JR |
697 | case LTTNG_KERNEL_SESSION_SET_NAME: |
698 | { | |
699 | struct lttng_kernel_session_name name; | |
700 | ||
701 | if (copy_from_user(&name, | |
702 | (struct lttng_kernel_session_name __user *) arg, | |
703 | sizeof(struct lttng_kernel_session_name))) | |
704 | return -EFAULT; | |
705 | return lttng_abi_session_set_name(session, &name); | |
706 | } | |
1c88f269 JR |
707 | case LTTNG_KERNEL_SESSION_SET_CREATION_TIME: |
708 | { | |
709 | struct lttng_kernel_session_creation_time time; | |
710 | ||
711 | if (copy_from_user(&time, | |
712 | (struct lttng_kernel_session_creation_time __user *) arg, | |
713 | sizeof(struct lttng_kernel_session_creation_time))) | |
714 | return -EFAULT; | |
715 | return lttng_abi_session_set_creation_time(session, &time); | |
716 | } | |
ad1c05e1 MD |
717 | default: |
718 | return -ENOIOCTLCMD; | |
719 | } | |
720 | } | |
721 | ||
03037b98 MD |
722 | /* |
723 | * Called when the last file reference is dropped. | |
724 | * | |
725 | * Big fat note: channels and events are invariant for the whole session after | |
726 | * their creation. So this session destruction also destroys all channel and | |
727 | * event structures specific to this session (they are not destroyed when their | |
728 | * individual file is released). | |
729 | */ | |
ad1c05e1 | 730 | static |
03037b98 | 731 | int lttng_session_release(struct inode *inode, struct file *file) |
ad1c05e1 | 732 | { |
a90917c3 | 733 | struct lttng_session *session = file->private_data; |
c269fff4 MD |
734 | |
735 | if (session) | |
a90917c3 | 736 | lttng_session_destroy(session); |
11b5a3c2 | 737 | return 0; |
ad1c05e1 | 738 | } |
ad1c05e1 MD |
739 | |
740 | static const struct file_operations lttng_session_fops = { | |
a33c9927 | 741 | .owner = THIS_MODULE, |
03037b98 | 742 | .release = lttng_session_release, |
ad1c05e1 MD |
743 | .unlocked_ioctl = lttng_session_ioctl, |
744 | #ifdef CONFIG_COMPAT | |
03037b98 | 745 | .compat_ioctl = lttng_session_ioctl, |
ad1c05e1 | 746 | #endif |
11b5a3c2 | 747 | }; |
ad1c05e1 | 748 | |
d83004aa JD |
749 | /** |
750 | * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation | |
751 | * @filp: the file | |
752 | * @wait: poll table | |
753 | * | |
754 | * Handles the poll operations for the metadata channels. | |
755 | */ | |
ad1c05e1 | 756 | static |
d83004aa JD |
757 | unsigned int lttng_metadata_ring_buffer_poll(struct file *filp, |
758 | poll_table *wait) | |
759 | { | |
760 | struct lttng_metadata_stream *stream = filp->private_data; | |
761 | struct lib_ring_buffer *buf = stream->priv; | |
762 | int finalized; | |
763 | unsigned int mask = 0; | |
764 | ||
765 | if (filp->f_mode & FMODE_READ) { | |
766 | poll_wait_set_exclusive(wait); | |
767 | poll_wait(filp, &stream->read_wait, wait); | |
768 | ||
769 | finalized = stream->finalized; | |
770 | ||
771 | /* | |
772 | * lib_ring_buffer_is_finalized() contains a smp_rmb() | |
773 | * ordering finalized load before offsets loads. | |
774 | */ | |
775 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); | |
776 | ||
777 | if (finalized) | |
778 | mask |= POLLHUP; | |
779 | ||
92d9f5e6 | 780 | mutex_lock(&stream->metadata_cache->lock); |
d83004aa | 781 | if (stream->metadata_cache->metadata_written > |
f613e3e6 | 782 | stream->metadata_out) |
d83004aa | 783 | mask |= POLLIN; |
92d9f5e6 | 784 | mutex_unlock(&stream->metadata_cache->lock); |
d83004aa JD |
785 | } |
786 | ||
787 | return mask; | |
788 | } | |
789 | ||
f613e3e6 MD |
790 | static |
791 | void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp, | |
792 | unsigned int cmd, unsigned long arg) | |
793 | { | |
794 | struct lttng_metadata_stream *stream = filp->private_data; | |
795 | ||
796 | stream->metadata_out = stream->metadata_in; | |
797 | } | |
798 | ||
d1344afa JD |
799 | /* |
800 | * Reset the counter of how much metadata has been consumed to 0. That way, | |
801 | * the consumer receives the content of the metadata cache unchanged. This is | |
802 | * different from the metadata_regenerate where the offset from epoch is | |
803 | * resampled, here we want the exact same content as the last time the metadata | |
804 | * was generated. This command is only possible if all the metadata written | |
805 | * in the cache has been output to the metadata stream to avoid corrupting the | |
806 | * metadata file. | |
807 | * | |
808 | * Return 0 on success, a negative value on error. | |
809 | */ | |
810 | static | |
811 | int lttng_metadata_cache_dump(struct lttng_metadata_stream *stream) | |
812 | { | |
813 | int ret; | |
814 | struct lttng_metadata_cache *cache = stream->metadata_cache; | |
815 | ||
816 | mutex_lock(&cache->lock); | |
817 | if (stream->metadata_out != cache->metadata_written) { | |
818 | ret = -EBUSY; | |
819 | goto end; | |
820 | } | |
821 | stream->metadata_out = 0; | |
822 | stream->metadata_in = 0; | |
823 | wake_up_interruptible(&stream->read_wait); | |
824 | ret = 0; | |
825 | ||
826 | end: | |
827 | mutex_unlock(&cache->lock); | |
828 | return ret; | |
829 | } | |
830 | ||
d83004aa JD |
831 | static |
832 | long lttng_metadata_ring_buffer_ioctl(struct file *filp, | |
833 | unsigned int cmd, unsigned long arg) | |
834 | { | |
835 | int ret; | |
836 | struct lttng_metadata_stream *stream = filp->private_data; | |
837 | struct lib_ring_buffer *buf = stream->priv; | |
838 | ||
839 | switch (cmd) { | |
d83004aa JD |
840 | case RING_BUFFER_GET_NEXT_SUBBUF: |
841 | { | |
35097f36 JD |
842 | struct lttng_metadata_stream *stream = filp->private_data; |
843 | struct lib_ring_buffer *buf = stream->priv; | |
844 | struct channel *chan = buf->backend.chan; | |
845 | ||
846 | ret = lttng_metadata_output_channel(stream, chan); | |
847 | if (ret > 0) { | |
848 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); | |
849 | ret = 0; | |
850 | } else if (ret < 0) | |
d83004aa JD |
851 | goto err; |
852 | break; | |
853 | } | |
f613e3e6 MD |
854 | case RING_BUFFER_GET_SUBBUF: |
855 | { | |
856 | /* | |
857 | * Random access is not allowed for metadata channel. | |
858 | */ | |
859 | return -ENOSYS; | |
860 | } | |
c6f05468 | 861 | case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */ |
35097f36 JD |
862 | case RING_BUFFER_FLUSH: |
863 | { | |
864 | struct lttng_metadata_stream *stream = filp->private_data; | |
865 | struct lib_ring_buffer *buf = stream->priv; | |
866 | struct channel *chan = buf->backend.chan; | |
867 | ||
868 | /* | |
869 | * Before doing the actual ring buffer flush, write up to one | |
870 | * packet of metadata in the ring buffer. | |
871 | */ | |
872 | ret = lttng_metadata_output_channel(stream, chan); | |
873 | if (ret < 0) | |
874 | goto err; | |
875 | break; | |
876 | } | |
9616f0bf JD |
877 | case RING_BUFFER_GET_METADATA_VERSION: |
878 | { | |
879 | struct lttng_metadata_stream *stream = filp->private_data; | |
880 | ||
881 | return put_u64(stream->version, arg); | |
882 | } | |
d1344afa JD |
883 | case RING_BUFFER_METADATA_CACHE_DUMP: |
884 | { | |
885 | struct lttng_metadata_stream *stream = filp->private_data; | |
886 | ||
887 | return lttng_metadata_cache_dump(stream); | |
888 | } | |
d83004aa JD |
889 | default: |
890 | break; | |
891 | } | |
f613e3e6 MD |
892 | /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */ |
893 | ||
d83004aa | 894 | /* Performing lib ring buffer ioctl after our own. */ |
f613e3e6 MD |
895 | ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf); |
896 | if (ret < 0) | |
897 | goto err; | |
d83004aa | 898 | |
f613e3e6 MD |
899 | switch (cmd) { |
900 | case RING_BUFFER_PUT_NEXT_SUBBUF: | |
901 | { | |
902 | lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp, | |
903 | cmd, arg); | |
904 | break; | |
905 | } | |
906 | default: | |
907 | break; | |
908 | } | |
d83004aa JD |
909 | err: |
910 | return ret; | |
911 | } | |
912 | ||
aeb9064d | 913 | #ifdef CONFIG_COMPAT |
d83004aa JD |
914 | static |
915 | long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp, | |
916 | unsigned int cmd, unsigned long arg) | |
917 | { | |
918 | int ret; | |
919 | struct lttng_metadata_stream *stream = filp->private_data; | |
920 | struct lib_ring_buffer *buf = stream->priv; | |
921 | ||
922 | switch (cmd) { | |
d83004aa JD |
923 | case RING_BUFFER_GET_NEXT_SUBBUF: |
924 | { | |
35097f36 JD |
925 | struct lttng_metadata_stream *stream = filp->private_data; |
926 | struct lib_ring_buffer *buf = stream->priv; | |
927 | struct channel *chan = buf->backend.chan; | |
928 | ||
929 | ret = lttng_metadata_output_channel(stream, chan); | |
930 | if (ret > 0) { | |
931 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE); | |
932 | ret = 0; | |
933 | } else if (ret < 0) | |
d83004aa JD |
934 | goto err; |
935 | break; | |
936 | } | |
f613e3e6 MD |
937 | case RING_BUFFER_GET_SUBBUF: |
938 | { | |
939 | /* | |
940 | * Random access is not allowed for metadata channel. | |
941 | */ | |
942 | return -ENOSYS; | |
943 | } | |
c6f05468 | 944 | case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */ |
96c55c2f MD |
945 | case RING_BUFFER_FLUSH: |
946 | { | |
947 | struct lttng_metadata_stream *stream = filp->private_data; | |
948 | struct lib_ring_buffer *buf = stream->priv; | |
949 | struct channel *chan = buf->backend.chan; | |
950 | ||
951 | /* | |
952 | * Before doing the actual ring buffer flush, write up to one | |
953 | * packet of metadata in the ring buffer. | |
954 | */ | |
955 | ret = lttng_metadata_output_channel(stream, chan); | |
956 | if (ret < 0) | |
957 | goto err; | |
958 | break; | |
959 | } | |
960 | case RING_BUFFER_GET_METADATA_VERSION: | |
961 | { | |
962 | struct lttng_metadata_stream *stream = filp->private_data; | |
963 | ||
964 | return put_u64(stream->version, arg); | |
965 | } | |
d1344afa JD |
966 | case RING_BUFFER_METADATA_CACHE_DUMP: |
967 | { | |
968 | struct lttng_metadata_stream *stream = filp->private_data; | |
969 | ||
970 | return lttng_metadata_cache_dump(stream); | |
971 | } | |
d83004aa JD |
972 | default: |
973 | break; | |
974 | } | |
f613e3e6 MD |
975 | /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */ |
976 | ||
d83004aa | 977 | /* Performing lib ring buffer ioctl after our own. */ |
f613e3e6 MD |
978 | ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf); |
979 | if (ret < 0) | |
980 | goto err; | |
d83004aa | 981 | |
f613e3e6 MD |
982 | switch (cmd) { |
983 | case RING_BUFFER_PUT_NEXT_SUBBUF: | |
984 | { | |
985 | lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp, | |
986 | cmd, arg); | |
987 | break; | |
988 | } | |
989 | default: | |
990 | break; | |
991 | } | |
d83004aa JD |
992 | err: |
993 | return ret; | |
994 | } | |
aeb9064d | 995 | #endif |
d83004aa | 996 | |
b3b8072b MD |
997 | /* |
998 | * This is not used by anonymous file descriptors. This code is left | |
999 | * there if we ever want to implement an inode with open() operation. | |
1000 | */ | |
d83004aa JD |
1001 | static |
1002 | int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file) | |
1003 | { | |
1004 | struct lttng_metadata_stream *stream = inode->i_private; | |
1005 | struct lib_ring_buffer *buf = stream->priv; | |
1006 | ||
1007 | file->private_data = buf; | |
b3b8072b MD |
1008 | /* |
1009 | * Since life-time of metadata cache differs from that of | |
1010 | * session, we need to keep our own reference on the transport. | |
1011 | */ | |
1012 | if (!try_module_get(stream->transport->owner)) { | |
1013 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); | |
1014 | return -EBUSY; | |
1015 | } | |
d83004aa JD |
1016 | return lib_ring_buffer_open(inode, file, buf); |
1017 | } | |
1018 | ||
1019 | static | |
1020 | int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file) | |
1021 | { | |
1022 | struct lttng_metadata_stream *stream = file->private_data; | |
1023 | struct lib_ring_buffer *buf = stream->priv; | |
1024 | ||
1025 | kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy); | |
b3b8072b | 1026 | module_put(stream->transport->owner); |
d83004aa JD |
1027 | return lib_ring_buffer_release(inode, file, buf); |
1028 | } | |
1029 | ||
1030 | static | |
1031 | ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos, | |
1032 | struct pipe_inode_info *pipe, size_t len, | |
1033 | unsigned int flags) | |
1034 | { | |
1035 | struct lttng_metadata_stream *stream = in->private_data; | |
1036 | struct lib_ring_buffer *buf = stream->priv; | |
1037 | ||
1038 | return lib_ring_buffer_splice_read(in, ppos, pipe, len, | |
1039 | flags, buf); | |
1040 | } | |
1041 | ||
1042 | static | |
1043 | int lttng_metadata_ring_buffer_mmap(struct file *filp, | |
1044 | struct vm_area_struct *vma) | |
1045 | { | |
1046 | struct lttng_metadata_stream *stream = filp->private_data; | |
1047 | struct lib_ring_buffer *buf = stream->priv; | |
1048 | ||
1049 | return lib_ring_buffer_mmap(filp, vma, buf); | |
1050 | } | |
1051 | ||
1052 | static | |
1053 | const struct file_operations lttng_metadata_ring_buffer_file_operations = { | |
1054 | .owner = THIS_MODULE, | |
1055 | .open = lttng_metadata_ring_buffer_open, | |
1056 | .release = lttng_metadata_ring_buffer_release, | |
1057 | .poll = lttng_metadata_ring_buffer_poll, | |
1058 | .splice_read = lttng_metadata_ring_buffer_splice_read, | |
1059 | .mmap = lttng_metadata_ring_buffer_mmap, | |
1060 | .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl, | |
1061 | .llseek = vfs_lib_ring_buffer_no_llseek, | |
1062 | #ifdef CONFIG_COMPAT | |
1063 | .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl, | |
1064 | #endif | |
1065 | }; | |
1066 | ||
1067 | static | |
1068 | int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv, | |
1069 | const struct file_operations *fops) | |
ad1c05e1 | 1070 | { |
ad1c05e1 | 1071 | int stream_fd, ret; |
11b5a3c2 | 1072 | struct file *stream_file; |
ad1c05e1 | 1073 | |
4ac10b76 | 1074 | stream_fd = lttng_get_unused_fd(); |
ad1c05e1 MD |
1075 | if (stream_fd < 0) { |
1076 | ret = stream_fd; | |
1077 | goto fd_error; | |
1078 | } | |
d83004aa JD |
1079 | stream_file = anon_inode_getfile("[lttng_stream]", fops, |
1080 | stream_priv, O_RDWR); | |
c0e31d2e MD |
1081 | if (IS_ERR(stream_file)) { |
1082 | ret = PTR_ERR(stream_file); | |
ad1c05e1 MD |
1083 | goto file_error; |
1084 | } | |
409453cb MD |
1085 | /* |
1086 | * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor | |
1087 | * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this | |
1088 | * file descriptor, so we set FMODE_PREAD here. | |
1089 | */ | |
d7b6f197 | 1090 | stream_file->f_mode |= FMODE_PREAD; |
c0e31d2e | 1091 | fd_install(stream_fd, stream_file); |
dda6a249 MD |
1092 | /* |
1093 | * The stream holds a reference to the channel within the generic ring | |
1094 | * buffer library, so no need to hold a refcount on the channel and | |
1095 | * session files here. | |
1096 | */ | |
ad1c05e1 MD |
1097 | return stream_fd; |
1098 | ||
1099 | file_error: | |
1100 | put_unused_fd(stream_fd); | |
d83004aa JD |
1101 | fd_error: |
1102 | return ret; | |
1103 | } | |
1104 | ||
1105 | static | |
1106 | int lttng_abi_open_stream(struct file *channel_file) | |
1107 | { | |
1108 | struct lttng_channel *channel = channel_file->private_data; | |
1109 | struct lib_ring_buffer *buf; | |
1110 | int ret; | |
1111 | void *stream_priv; | |
1112 | ||
1113 | buf = channel->ops->buffer_read_open(channel->chan); | |
1114 | if (!buf) | |
1115 | return -ENOENT; | |
1116 | ||
1117 | stream_priv = buf; | |
1118 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, | |
ed8d02d6 | 1119 | <tng_stream_ring_buffer_file_operations); |
d83004aa JD |
1120 | if (ret < 0) |
1121 | goto fd_error; | |
1122 | ||
1123 | return ret; | |
1124 | ||
1125 | fd_error: | |
1126 | channel->ops->buffer_read_close(buf); | |
1127 | return ret; | |
1128 | } | |
1129 | ||
1130 | static | |
1131 | int lttng_abi_open_metadata_stream(struct file *channel_file) | |
1132 | { | |
1133 | struct lttng_channel *channel = channel_file->private_data; | |
1134 | struct lttng_session *session = channel->session; | |
1135 | struct lib_ring_buffer *buf; | |
1136 | int ret; | |
1137 | struct lttng_metadata_stream *metadata_stream; | |
1138 | void *stream_priv; | |
1139 | ||
1140 | buf = channel->ops->buffer_read_open(channel->chan); | |
1141 | if (!buf) | |
1142 | return -ENOENT; | |
1143 | ||
1144 | metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream), | |
1145 | GFP_KERNEL); | |
b3b8072b MD |
1146 | if (!metadata_stream) { |
1147 | ret = -ENOMEM; | |
1148 | goto nomem; | |
1149 | } | |
d83004aa JD |
1150 | metadata_stream->metadata_cache = session->metadata_cache; |
1151 | init_waitqueue_head(&metadata_stream->read_wait); | |
1152 | metadata_stream->priv = buf; | |
1153 | stream_priv = metadata_stream; | |
b3b8072b MD |
1154 | metadata_stream->transport = channel->transport; |
1155 | ||
1156 | /* | |
1157 | * Since life-time of metadata cache differs from that of | |
1158 | * session, we need to keep our own reference on the transport. | |
1159 | */ | |
1160 | if (!try_module_get(metadata_stream->transport->owner)) { | |
1161 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); | |
1162 | ret = -EINVAL; | |
1163 | goto notransport; | |
1164 | } | |
1165 | ||
901aaa5f FD |
1166 | if (!lttng_kref_get(&session->metadata_cache->refcount)) { |
1167 | ret = -EOVERFLOW; | |
9c1f4643 | 1168 | goto kref_error; |
901aaa5f FD |
1169 | } |
1170 | ||
d83004aa JD |
1171 | ret = lttng_abi_create_stream_fd(channel_file, stream_priv, |
1172 | <tng_metadata_ring_buffer_file_operations); | |
1173 | if (ret < 0) | |
1174 | goto fd_error; | |
1175 | ||
d83004aa JD |
1176 | list_add(&metadata_stream->list, |
1177 | &session->metadata_cache->metadata_stream); | |
1178 | return ret; | |
1179 | ||
ad1c05e1 | 1180 | fd_error: |
9c1f4643 MD |
1181 | kref_put(&session->metadata_cache->refcount, metadata_cache_destroy); |
1182 | kref_error: | |
b3b8072b MD |
1183 | module_put(metadata_stream->transport->owner); |
1184 | notransport: | |
1185 | kfree(metadata_stream); | |
1186 | nomem: | |
11b5a3c2 | 1187 | channel->ops->buffer_read_close(buf); |
ad1c05e1 MD |
1188 | return ret; |
1189 | } | |
1190 | ||
653fe716 | 1191 | static |
c0e31d2e | 1192 | int lttng_abi_create_event(struct file *channel_file, |
6dccd6c1 | 1193 | struct lttng_kernel_event *event_param) |
653fe716 | 1194 | { |
a90917c3 | 1195 | struct lttng_channel *channel = channel_file->private_data; |
653fe716 | 1196 | int event_fd, ret; |
11b5a3c2 | 1197 | struct file *event_file; |
3c997079 | 1198 | void *priv; |
653fe716 | 1199 | |
6dccd6c1 JD |
1200 | event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
1201 | switch (event_param->instrumentation) { | |
7371f44c | 1202 | case LTTNG_KERNEL_KRETPROBE: |
6dccd6c1 | 1203 | event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
7371f44c | 1204 | break; |
ab2277d6 | 1205 | case LTTNG_KERNEL_KPROBE: |
6dccd6c1 | 1206 | event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 | 1207 | break; |
ab2277d6 | 1208 | case LTTNG_KERNEL_FUNCTION: |
6dccd6c1 | 1209 | event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
e0a7a7c4 MD |
1210 | break; |
1211 | default: | |
1212 | break; | |
1213 | } | |
33a39a3c MD |
1214 | event_fd = lttng_get_unused_fd(); |
1215 | if (event_fd < 0) { | |
1216 | ret = event_fd; | |
1217 | goto fd_error; | |
1218 | } | |
1219 | event_file = anon_inode_getfile("[lttng_event]", | |
1220 | <tng_event_fops, | |
1221 | NULL, O_RDWR); | |
1222 | if (IS_ERR(event_file)) { | |
1223 | ret = PTR_ERR(event_file); | |
1224 | goto file_error; | |
1225 | } | |
9c1f4643 | 1226 | /* The event holds a reference on the channel */ |
98d7281c | 1227 | if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) { |
0d2c717f | 1228 | ret = -EOVERFLOW; |
9c1f4643 MD |
1229 | goto refcount_error; |
1230 | } | |
33a39a3c MD |
1231 | if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT |
1232 | || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) { | |
1233 | struct lttng_enabler *enabler; | |
1234 | ||
4993071a PP |
1235 | if (strutils_is_star_glob_pattern(event_param->name)) { |
1236 | /* | |
1237 | * If the event name is a star globbing pattern, | |
1238 | * we create the special star globbing enabler. | |
1239 | */ | |
1240 | enabler = lttng_enabler_create(LTTNG_ENABLER_STAR_GLOB, | |
33a39a3c | 1241 | event_param, channel); |
3c997079 | 1242 | } else { |
33a39a3c MD |
1243 | enabler = lttng_enabler_create(LTTNG_ENABLER_NAME, |
1244 | event_param, channel); | |
1ec65de1 | 1245 | } |
33a39a3c MD |
1246 | priv = enabler; |
1247 | } else { | |
1248 | struct lttng_event *event; | |
4ee2453d | 1249 | |
33a39a3c MD |
1250 | /* |
1251 | * We tolerate no failure path after event creation. It | |
1252 | * will stay invariant for the rest of the session. | |
1253 | */ | |
1254 | event = lttng_event_create(channel, event_param, | |
1255 | NULL, NULL, | |
1256 | event_param->instrumentation); | |
1257 | WARN_ON_ONCE(!event); | |
1258 | if (IS_ERR(event)) { | |
1259 | ret = PTR_ERR(event); | |
1260 | goto event_error; | |
80f87dd2 | 1261 | } |
33a39a3c | 1262 | priv = event; |
03037b98 | 1263 | } |
33a39a3c MD |
1264 | event_file->private_data = priv; |
1265 | fd_install(event_fd, event_file); | |
653fe716 MD |
1266 | return event_fd; |
1267 | ||
03037b98 | 1268 | event_error: |
9c1f4643 MD |
1269 | atomic_long_dec(&channel_file->f_count); |
1270 | refcount_error: | |
c0e31d2e | 1271 | fput(event_file); |
653fe716 MD |
1272 | file_error: |
1273 | put_unused_fd(event_fd); | |
1274 | fd_error: | |
653fe716 MD |
1275 | return ret; |
1276 | } | |
ad1c05e1 MD |
1277 | |
1278 | /** | |
1279 | * lttng_channel_ioctl - lttng syscall through ioctl | |
1280 | * | |
c0e31d2e | 1281 | * @file: the file |
ad1c05e1 MD |
1282 | * @cmd: the command |
1283 | * @arg: command arg | |
1284 | * | |
1285 | * This ioctl implements lttng commands: | |
38d024ae | 1286 | * LTTNG_KERNEL_STREAM |
ad1c05e1 MD |
1287 | * Returns an event stream file descriptor or failure. |
1288 | * (typically, one event stream records events from one CPU) | |
38d024ae | 1289 | * LTTNG_KERNEL_EVENT |
ad1c05e1 | 1290 | * Returns an event file descriptor or failure. |
8070f5c0 MD |
1291 | * LTTNG_KERNEL_CONTEXT |
1292 | * Prepend a context field to each event in the channel | |
e64957da MD |
1293 | * LTTNG_KERNEL_ENABLE |
1294 | * Enable recording for events in this channel (weak enable) | |
1295 | * LTTNG_KERNEL_DISABLE | |
1296 | * Disable recording for events in this channel (strong disable) | |
baf20995 | 1297 | * |
baf20995 MD |
1298 | * Channel and event file descriptors also hold a reference on the session. |
1299 | */ | |
ad1c05e1 | 1300 | static |
c0e31d2e | 1301 | long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
baf20995 | 1302 | { |
a90917c3 | 1303 | struct lttng_channel *channel = file->private_data; |
8070f5c0 | 1304 | |
baf20995 | 1305 | switch (cmd) { |
6dccd6c1 | 1306 | case LTTNG_KERNEL_OLD_STREAM: |
38d024ae | 1307 | case LTTNG_KERNEL_STREAM: |
c0e31d2e | 1308 | return lttng_abi_open_stream(file); |
6dccd6c1 JD |
1309 | case LTTNG_KERNEL_OLD_EVENT: |
1310 | { | |
1311 | struct lttng_kernel_event *uevent_param; | |
1312 | struct lttng_kernel_old_event *old_uevent_param; | |
1313 | int ret; | |
1314 | ||
1315 | uevent_param = kmalloc(sizeof(struct lttng_kernel_event), | |
1316 | GFP_KERNEL); | |
1317 | if (!uevent_param) { | |
1318 | ret = -ENOMEM; | |
1319 | goto old_event_end; | |
1320 | } | |
1321 | old_uevent_param = kmalloc( | |
1322 | sizeof(struct lttng_kernel_old_event), | |
1323 | GFP_KERNEL); | |
1324 | if (!old_uevent_param) { | |
1325 | ret = -ENOMEM; | |
1326 | goto old_event_error_free_param; | |
1327 | } | |
1328 | if (copy_from_user(old_uevent_param, | |
1329 | (struct lttng_kernel_old_event __user *) arg, | |
1330 | sizeof(struct lttng_kernel_old_event))) { | |
1331 | ret = -EFAULT; | |
1332 | goto old_event_error_free_old_param; | |
1333 | } | |
1334 | ||
1335 | memcpy(uevent_param->name, old_uevent_param->name, | |
1336 | sizeof(uevent_param->name)); | |
1337 | uevent_param->instrumentation = | |
1338 | old_uevent_param->instrumentation; | |
1339 | ||
1340 | switch (old_uevent_param->instrumentation) { | |
1341 | case LTTNG_KERNEL_KPROBE: | |
1342 | uevent_param->u.kprobe.addr = | |
1343 | old_uevent_param->u.kprobe.addr; | |
1344 | uevent_param->u.kprobe.offset = | |
1345 | old_uevent_param->u.kprobe.offset; | |
1346 | memcpy(uevent_param->u.kprobe.symbol_name, | |
1347 | old_uevent_param->u.kprobe.symbol_name, | |
1348 | sizeof(uevent_param->u.kprobe.symbol_name)); | |
1349 | break; | |
1350 | case LTTNG_KERNEL_KRETPROBE: | |
1351 | uevent_param->u.kretprobe.addr = | |
1352 | old_uevent_param->u.kretprobe.addr; | |
1353 | uevent_param->u.kretprobe.offset = | |
1354 | old_uevent_param->u.kretprobe.offset; | |
1355 | memcpy(uevent_param->u.kretprobe.symbol_name, | |
1356 | old_uevent_param->u.kretprobe.symbol_name, | |
1357 | sizeof(uevent_param->u.kretprobe.symbol_name)); | |
1358 | break; | |
1359 | case LTTNG_KERNEL_FUNCTION: | |
1360 | memcpy(uevent_param->u.ftrace.symbol_name, | |
1361 | old_uevent_param->u.ftrace.symbol_name, | |
1362 | sizeof(uevent_param->u.ftrace.symbol_name)); | |
1363 | break; | |
1364 | default: | |
1365 | break; | |
1366 | } | |
1367 | ret = lttng_abi_create_event(file, uevent_param); | |
1368 | ||
1369 | old_event_error_free_old_param: | |
1370 | kfree(old_uevent_param); | |
1371 | old_event_error_free_param: | |
1372 | kfree(uevent_param); | |
1373 | old_event_end: | |
1374 | return ret; | |
1375 | } | |
38d024ae | 1376 | case LTTNG_KERNEL_EVENT: |
6dccd6c1 JD |
1377 | { |
1378 | struct lttng_kernel_event uevent_param; | |
1379 | ||
1380 | if (copy_from_user(&uevent_param, | |
1381 | (struct lttng_kernel_event __user *) arg, | |
1382 | sizeof(uevent_param))) | |
1383 | return -EFAULT; | |
1384 | return lttng_abi_create_event(file, &uevent_param); | |
1385 | } | |
1386 | case LTTNG_KERNEL_OLD_CONTEXT: | |
1387 | { | |
1388 | struct lttng_kernel_context *ucontext_param; | |
1389 | struct lttng_kernel_old_context *old_ucontext_param; | |
1390 | int ret; | |
1391 | ||
1392 | ucontext_param = kmalloc(sizeof(struct lttng_kernel_context), | |
1393 | GFP_KERNEL); | |
1394 | if (!ucontext_param) { | |
1395 | ret = -ENOMEM; | |
1396 | goto old_ctx_end; | |
1397 | } | |
1398 | old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context), | |
1399 | GFP_KERNEL); | |
1400 | if (!old_ucontext_param) { | |
1401 | ret = -ENOMEM; | |
1402 | goto old_ctx_error_free_param; | |
1403 | } | |
1404 | ||
1405 | if (copy_from_user(old_ucontext_param, | |
1406 | (struct lttng_kernel_old_context __user *) arg, | |
1407 | sizeof(struct lttng_kernel_old_context))) { | |
1408 | ret = -EFAULT; | |
1409 | goto old_ctx_error_free_old_param; | |
1410 | } | |
1411 | ucontext_param->ctx = old_ucontext_param->ctx; | |
1412 | memcpy(ucontext_param->padding, old_ucontext_param->padding, | |
1413 | sizeof(ucontext_param->padding)); | |
1414 | /* only type that uses the union */ | |
1415 | if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) { | |
1416 | ucontext_param->u.perf_counter.type = | |
1417 | old_ucontext_param->u.perf_counter.type; | |
1418 | ucontext_param->u.perf_counter.config = | |
1419 | old_ucontext_param->u.perf_counter.config; | |
1420 | memcpy(ucontext_param->u.perf_counter.name, | |
1421 | old_ucontext_param->u.perf_counter.name, | |
1422 | sizeof(ucontext_param->u.perf_counter.name)); | |
1423 | } | |
1424 | ||
1425 | ret = lttng_abi_add_context(file, | |
1426 | ucontext_param, | |
1427 | &channel->ctx, channel->session); | |
1428 | ||
1429 | old_ctx_error_free_old_param: | |
1430 | kfree(old_ucontext_param); | |
1431 | old_ctx_error_free_param: | |
1432 | kfree(ucontext_param); | |
1433 | old_ctx_end: | |
1434 | return ret; | |
1435 | } | |
8070f5c0 | 1436 | case LTTNG_KERNEL_CONTEXT: |
6dccd6c1 JD |
1437 | { |
1438 | struct lttng_kernel_context ucontext_param; | |
1439 | ||
1440 | if (copy_from_user(&ucontext_param, | |
8070f5c0 | 1441 | (struct lttng_kernel_context __user *) arg, |
6dccd6c1 JD |
1442 | sizeof(ucontext_param))) |
1443 | return -EFAULT; | |
1444 | return lttng_abi_add_context(file, | |
1445 | &ucontext_param, | |
8070f5c0 | 1446 | &channel->ctx, channel->session); |
6dccd6c1 JD |
1447 | } |
1448 | case LTTNG_KERNEL_OLD_ENABLE: | |
e64957da | 1449 | case LTTNG_KERNEL_ENABLE: |
a90917c3 | 1450 | return lttng_channel_enable(channel); |
6dccd6c1 | 1451 | case LTTNG_KERNEL_OLD_DISABLE: |
e64957da | 1452 | case LTTNG_KERNEL_DISABLE: |
a90917c3 | 1453 | return lttng_channel_disable(channel); |
12e579db MD |
1454 | case LTTNG_KERNEL_SYSCALL_MASK: |
1455 | return lttng_channel_syscall_mask(channel, | |
1456 | (struct lttng_kernel_syscall_mask __user *) arg); | |
baf20995 MD |
1457 | default: |
1458 | return -ENOIOCTLCMD; | |
1459 | } | |
1460 | } | |
1461 | ||
5dbbdb43 MD |
1462 | /** |
1463 | * lttng_metadata_ioctl - lttng syscall through ioctl | |
1464 | * | |
1465 | * @file: the file | |
1466 | * @cmd: the command | |
1467 | * @arg: command arg | |
1468 | * | |
1469 | * This ioctl implements lttng commands: | |
38d024ae | 1470 | * LTTNG_KERNEL_STREAM |
5dbbdb43 MD |
1471 | * Returns an event stream file descriptor or failure. |
1472 | * | |
1473 | * Channel and event file descriptors also hold a reference on the session. | |
1474 | */ | |
1475 | static | |
1476 | long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
1477 | { | |
1478 | switch (cmd) { | |
6dccd6c1 | 1479 | case LTTNG_KERNEL_OLD_STREAM: |
38d024ae | 1480 | case LTTNG_KERNEL_STREAM: |
d83004aa | 1481 | return lttng_abi_open_metadata_stream(file); |
5dbbdb43 MD |
1482 | default: |
1483 | return -ENOIOCTLCMD; | |
1484 | } | |
1485 | } | |
1486 | ||
653fe716 MD |
1487 | /** |
1488 | * lttng_channel_poll - lttng stream addition/removal monitoring | |
1489 | * | |
c0e31d2e | 1490 | * @file: the file |
653fe716 MD |
1491 | * @wait: poll table |
1492 | */ | |
c0e31d2e | 1493 | unsigned int lttng_channel_poll(struct file *file, poll_table *wait) |
653fe716 | 1494 | { |
a90917c3 | 1495 | struct lttng_channel *channel = file->private_data; |
653fe716 MD |
1496 | unsigned int mask = 0; |
1497 | ||
c0e31d2e | 1498 | if (file->f_mode & FMODE_READ) { |
a33e44a6 | 1499 | poll_wait_set_exclusive(wait); |
24cedcfe MD |
1500 | poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan), |
1501 | wait); | |
653fe716 | 1502 | |
254ec7bc MD |
1503 | if (channel->ops->is_disabled(channel->chan)) |
1504 | return POLLERR; | |
24cedcfe | 1505 | if (channel->ops->is_finalized(channel->chan)) |
653fe716 | 1506 | return POLLHUP; |
f71ecafa | 1507 | if (channel->ops->buffer_has_read_closed_stream(channel->chan)) |
653fe716 | 1508 | return POLLIN | POLLRDNORM; |
f71ecafa | 1509 | return 0; |
653fe716 MD |
1510 | } |
1511 | return mask; | |
1512 | ||
1513 | } | |
1514 | ||
0a84a57f MD |
1515 | static |
1516 | int lttng_channel_release(struct inode *inode, struct file *file) | |
1517 | { | |
a90917c3 | 1518 | struct lttng_channel *channel = file->private_data; |
c269fff4 MD |
1519 | |
1520 | if (channel) | |
1521 | fput(channel->session->file); | |
0a84a57f MD |
1522 | return 0; |
1523 | } | |
1524 | ||
d83004aa JD |
1525 | static |
1526 | int lttng_metadata_channel_release(struct inode *inode, struct file *file) | |
1527 | { | |
1528 | struct lttng_channel *channel = file->private_data; | |
1529 | ||
1530 | if (channel) { | |
d83004aa | 1531 | fput(channel->session->file); |
a3381417 | 1532 | lttng_metadata_channel_destroy(channel); |
d83004aa JD |
1533 | } |
1534 | ||
1535 | return 0; | |
1536 | } | |
1537 | ||
ad1c05e1 | 1538 | static const struct file_operations lttng_channel_fops = { |
a33c9927 | 1539 | .owner = THIS_MODULE, |
03037b98 | 1540 | .release = lttng_channel_release, |
653fe716 | 1541 | .poll = lttng_channel_poll, |
ad1c05e1 | 1542 | .unlocked_ioctl = lttng_channel_ioctl, |
baf20995 | 1543 | #ifdef CONFIG_COMPAT |
03037b98 | 1544 | .compat_ioctl = lttng_channel_ioctl, |
baf20995 | 1545 | #endif |
11b5a3c2 | 1546 | }; |
baf20995 | 1547 | |
5dbbdb43 | 1548 | static const struct file_operations lttng_metadata_fops = { |
a33c9927 | 1549 | .owner = THIS_MODULE, |
d83004aa | 1550 | .release = lttng_metadata_channel_release, |
5dbbdb43 MD |
1551 | .unlocked_ioctl = lttng_metadata_ioctl, |
1552 | #ifdef CONFIG_COMPAT | |
1553 | .compat_ioctl = lttng_metadata_ioctl, | |
1554 | #endif | |
1555 | }; | |
1556 | ||
8070f5c0 MD |
1557 | /** |
1558 | * lttng_event_ioctl - lttng syscall through ioctl | |
1559 | * | |
1560 | * @file: the file | |
1561 | * @cmd: the command | |
1562 | * @arg: command arg | |
1563 | * | |
1564 | * This ioctl implements lttng commands: | |
8070f5c0 MD |
1565 | * LTTNG_KERNEL_CONTEXT |
1566 | * Prepend a context field to each record of this event | |
e64957da MD |
1567 | * LTTNG_KERNEL_ENABLE |
1568 | * Enable recording for this event (weak enable) | |
1569 | * LTTNG_KERNEL_DISABLE | |
1570 | * Disable recording for this event (strong disable) | |
8070f5c0 MD |
1571 | */ |
1572 | static | |
1573 | long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
1574 | { | |
3c997079 MD |
1575 | struct lttng_event *event; |
1576 | struct lttng_enabler *enabler; | |
1577 | enum lttng_event_type *evtype = file->private_data; | |
8070f5c0 MD |
1578 | |
1579 | switch (cmd) { | |
6dccd6c1 JD |
1580 | case LTTNG_KERNEL_OLD_CONTEXT: |
1581 | { | |
3c997079 MD |
1582 | /* Not implemented */ |
1583 | return -ENOSYS; | |
6dccd6c1 | 1584 | } |
8070f5c0 | 1585 | case LTTNG_KERNEL_CONTEXT: |
6dccd6c1 | 1586 | { |
3c997079 MD |
1587 | /* Not implemented */ |
1588 | return -ENOSYS; | |
6dccd6c1 JD |
1589 | } |
1590 | case LTTNG_KERNEL_OLD_ENABLE: | |
e64957da | 1591 | case LTTNG_KERNEL_ENABLE: |
3c997079 MD |
1592 | switch (*evtype) { |
1593 | case LTTNG_TYPE_EVENT: | |
1594 | event = file->private_data; | |
1595 | return lttng_event_enable(event); | |
1596 | case LTTNG_TYPE_ENABLER: | |
1597 | enabler = file->private_data; | |
1598 | return lttng_enabler_enable(enabler); | |
1599 | default: | |
1600 | WARN_ON_ONCE(1); | |
1601 | return -ENOSYS; | |
1602 | } | |
6dccd6c1 | 1603 | case LTTNG_KERNEL_OLD_DISABLE: |
e64957da | 1604 | case LTTNG_KERNEL_DISABLE: |
3c997079 MD |
1605 | switch (*evtype) { |
1606 | case LTTNG_TYPE_EVENT: | |
1607 | event = file->private_data; | |
1608 | return lttng_event_disable(event); | |
1609 | case LTTNG_TYPE_ENABLER: | |
1610 | enabler = file->private_data; | |
1611 | return lttng_enabler_disable(enabler); | |
1612 | default: | |
1613 | WARN_ON_ONCE(1); | |
1614 | return -ENOSYS; | |
1615 | } | |
07dfc1d0 MD |
1616 | case LTTNG_KERNEL_FILTER: |
1617 | switch (*evtype) { | |
1618 | case LTTNG_TYPE_EVENT: | |
1619 | return -EINVAL; | |
1620 | case LTTNG_TYPE_ENABLER: | |
1621 | { | |
1622 | enabler = file->private_data; | |
1623 | return lttng_enabler_attach_bytecode(enabler, | |
1624 | (struct lttng_kernel_filter_bytecode __user *) arg); | |
1625 | } | |
0586316f MD |
1626 | default: |
1627 | WARN_ON_ONCE(1); | |
1628 | return -ENOSYS; | |
07dfc1d0 | 1629 | } |
3aed4dca FD |
1630 | case LTTNG_KERNEL_ADD_CALLSITE: |
1631 | switch (*evtype) { | |
1632 | case LTTNG_TYPE_EVENT: | |
1633 | event = file->private_data; | |
1634 | return lttng_event_add_callsite(event, | |
1635 | (struct lttng_kernel_event_callsite __user *) arg); | |
1636 | case LTTNG_TYPE_ENABLER: | |
1637 | return -EINVAL; | |
64c147c0 MD |
1638 | default: |
1639 | WARN_ON_ONCE(1); | |
1640 | return -ENOSYS; | |
3aed4dca | 1641 | } |
8070f5c0 MD |
1642 | default: |
1643 | return -ENOIOCTLCMD; | |
1644 | } | |
1645 | } | |
1646 | ||
0a84a57f MD |
1647 | static |
1648 | int lttng_event_release(struct inode *inode, struct file *file) | |
1649 | { | |
3c997079 MD |
1650 | struct lttng_event *event; |
1651 | struct lttng_enabler *enabler; | |
1652 | enum lttng_event_type *evtype = file->private_data; | |
1653 | ||
1654 | if (!evtype) | |
1655 | return 0; | |
1656 | ||
1657 | switch (*evtype) { | |
1658 | case LTTNG_TYPE_EVENT: | |
1659 | event = file->private_data; | |
1660 | if (event) | |
1661 | fput(event->chan->file); | |
1662 | break; | |
1663 | case LTTNG_TYPE_ENABLER: | |
1664 | enabler = file->private_data; | |
1665 | if (enabler) | |
1666 | fput(enabler->chan->file); | |
1667 | break; | |
1668 | default: | |
1669 | WARN_ON_ONCE(1); | |
1670 | break; | |
1671 | } | |
c269fff4 | 1672 | |
0a84a57f MD |
1673 | return 0; |
1674 | } | |
1675 | ||
3b923e5b | 1676 | /* TODO: filter control ioctl */ |
0a84a57f | 1677 | static const struct file_operations lttng_event_fops = { |
a33c9927 | 1678 | .owner = THIS_MODULE, |
0a84a57f | 1679 | .release = lttng_event_release, |
8070f5c0 MD |
1680 | .unlocked_ioctl = lttng_event_ioctl, |
1681 | #ifdef CONFIG_COMPAT | |
1682 | .compat_ioctl = lttng_event_ioctl, | |
1683 | #endif | |
11b5a3c2 | 1684 | }; |
0a84a57f | 1685 | |
3b731ab1 JD |
1686 | static int put_u64(uint64_t val, unsigned long arg) |
1687 | { | |
1688 | return put_user(val, (uint64_t __user *) arg); | |
1689 | } | |
1690 | ||
ed8d02d6 JD |
1691 | static long lttng_stream_ring_buffer_ioctl(struct file *filp, |
1692 | unsigned int cmd, unsigned long arg) | |
1693 | { | |
3b731ab1 JD |
1694 | struct lib_ring_buffer *buf = filp->private_data; |
1695 | struct channel *chan = buf->backend.chan; | |
1696 | const struct lib_ring_buffer_config *config = &chan->backend.config; | |
dd5a0db3 | 1697 | const struct lttng_channel_ops *ops = chan->backend.priv_ops; |
3b731ab1 JD |
1698 | int ret; |
1699 | ||
1700 | if (atomic_read(&chan->record_disabled)) | |
1701 | return -EIO; | |
1702 | ||
ed8d02d6 | 1703 | switch (cmd) { |
3b731ab1 JD |
1704 | case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN: |
1705 | { | |
1706 | uint64_t ts; | |
1707 | ||
dd5a0db3 | 1708 | ret = ops->timestamp_begin(config, buf, &ts); |
3b731ab1 JD |
1709 | if (ret < 0) |
1710 | goto error; | |
1711 | return put_u64(ts, arg); | |
1712 | } | |
1713 | case LTTNG_RING_BUFFER_GET_TIMESTAMP_END: | |
1714 | { | |
1715 | uint64_t ts; | |
1716 | ||
dd5a0db3 | 1717 | ret = ops->timestamp_end(config, buf, &ts); |
3b731ab1 JD |
1718 | if (ret < 0) |
1719 | goto error; | |
1720 | return put_u64(ts, arg); | |
1721 | } | |
1722 | case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED: | |
1723 | { | |
1724 | uint64_t ed; | |
1725 | ||
dd5a0db3 | 1726 | ret = ops->events_discarded(config, buf, &ed); |
3b731ab1 JD |
1727 | if (ret < 0) |
1728 | goto error; | |
1729 | return put_u64(ed, arg); | |
1730 | } | |
1731 | case LTTNG_RING_BUFFER_GET_CONTENT_SIZE: | |
1732 | { | |
1733 | uint64_t cs; | |
1734 | ||
dd5a0db3 | 1735 | ret = ops->content_size(config, buf, &cs); |
3b731ab1 JD |
1736 | if (ret < 0) |
1737 | goto error; | |
1738 | return put_u64(cs, arg); | |
1739 | } | |
1740 | case LTTNG_RING_BUFFER_GET_PACKET_SIZE: | |
1741 | { | |
1742 | uint64_t ps; | |
1743 | ||
dd5a0db3 | 1744 | ret = ops->packet_size(config, buf, &ps); |
3b731ab1 JD |
1745 | if (ret < 0) |
1746 | goto error; | |
1747 | return put_u64(ps, arg); | |
1748 | } | |
1749 | case LTTNG_RING_BUFFER_GET_STREAM_ID: | |
1750 | { | |
1751 | uint64_t si; | |
1752 | ||
dd5a0db3 | 1753 | ret = ops->stream_id(config, buf, &si); |
3b731ab1 JD |
1754 | if (ret < 0) |
1755 | goto error; | |
1756 | return put_u64(si, arg); | |
1757 | } | |
2348ca17 JD |
1758 | case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP: |
1759 | { | |
1760 | uint64_t ts; | |
1761 | ||
dd5a0db3 | 1762 | ret = ops->current_timestamp(config, buf, &ts); |
2348ca17 JD |
1763 | if (ret < 0) |
1764 | goto error; | |
1765 | return put_u64(ts, arg); | |
1766 | } | |
5b3cf4f9 JD |
1767 | case LTTNG_RING_BUFFER_GET_SEQ_NUM: |
1768 | { | |
1769 | uint64_t seq; | |
1770 | ||
1771 | ret = ops->sequence_number(config, buf, &seq); | |
1772 | if (ret < 0) | |
1773 | goto error; | |
1774 | return put_u64(seq, arg); | |
1775 | } | |
5594698f JD |
1776 | case LTTNG_RING_BUFFER_INSTANCE_ID: |
1777 | { | |
1778 | uint64_t id; | |
1779 | ||
1780 | ret = ops->instance_id(config, buf, &id); | |
1781 | if (ret < 0) | |
1782 | goto error; | |
1783 | return put_u64(id, arg); | |
1784 | } | |
3b731ab1 JD |
1785 | default: |
1786 | return lib_ring_buffer_file_operations.unlocked_ioctl(filp, | |
1787 | cmd, arg); | |
ed8d02d6 | 1788 | } |
3b731ab1 JD |
1789 | |
1790 | error: | |
1791 | return -ENOSYS; | |
ed8d02d6 JD |
1792 | } |
1793 | ||
1794 | #ifdef CONFIG_COMPAT | |
1795 | static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp, | |
1796 | unsigned int cmd, unsigned long arg) | |
1797 | { | |
3b731ab1 JD |
1798 | struct lib_ring_buffer *buf = filp->private_data; |
1799 | struct channel *chan = buf->backend.chan; | |
1800 | const struct lib_ring_buffer_config *config = &chan->backend.config; | |
dd5a0db3 | 1801 | const struct lttng_channel_ops *ops = chan->backend.priv_ops; |
3b731ab1 JD |
1802 | int ret; |
1803 | ||
1804 | if (atomic_read(&chan->record_disabled)) | |
1805 | return -EIO; | |
1806 | ||
ed8d02d6 | 1807 | switch (cmd) { |
3b731ab1 JD |
1808 | case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN: |
1809 | { | |
1810 | uint64_t ts; | |
1811 | ||
dd5a0db3 | 1812 | ret = ops->timestamp_begin(config, buf, &ts); |
3b731ab1 JD |
1813 | if (ret < 0) |
1814 | goto error; | |
1815 | return put_u64(ts, arg); | |
1816 | } | |
1817 | case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_END: | |
1818 | { | |
1819 | uint64_t ts; | |
1820 | ||
dd5a0db3 | 1821 | ret = ops->timestamp_end(config, buf, &ts); |
3b731ab1 JD |
1822 | if (ret < 0) |
1823 | goto error; | |
1824 | return put_u64(ts, arg); | |
1825 | } | |
1826 | case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED: | |
1827 | { | |
1828 | uint64_t ed; | |
1829 | ||
dd5a0db3 | 1830 | ret = ops->events_discarded(config, buf, &ed); |
3b731ab1 JD |
1831 | if (ret < 0) |
1832 | goto error; | |
1833 | return put_u64(ed, arg); | |
ed8d02d6 | 1834 | } |
3b731ab1 JD |
1835 | case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE: |
1836 | { | |
1837 | uint64_t cs; | |
1838 | ||
dd5a0db3 | 1839 | ret = ops->content_size(config, buf, &cs); |
3b731ab1 JD |
1840 | if (ret < 0) |
1841 | goto error; | |
1842 | return put_u64(cs, arg); | |
1843 | } | |
1844 | case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE: | |
1845 | { | |
1846 | uint64_t ps; | |
1847 | ||
dd5a0db3 | 1848 | ret = ops->packet_size(config, buf, &ps); |
3b731ab1 JD |
1849 | if (ret < 0) |
1850 | goto error; | |
1851 | return put_u64(ps, arg); | |
1852 | } | |
1853 | case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID: | |
1854 | { | |
1855 | uint64_t si; | |
1856 | ||
dd5a0db3 | 1857 | ret = ops->stream_id(config, buf, &si); |
3b731ab1 JD |
1858 | if (ret < 0) |
1859 | goto error; | |
1860 | return put_u64(si, arg); | |
1861 | } | |
2348ca17 JD |
1862 | case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP: |
1863 | { | |
1864 | uint64_t ts; | |
1865 | ||
dd5a0db3 | 1866 | ret = ops->current_timestamp(config, buf, &ts); |
2348ca17 JD |
1867 | if (ret < 0) |
1868 | goto error; | |
1869 | return put_u64(ts, arg); | |
1870 | } | |
5b3cf4f9 JD |
1871 | case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM: |
1872 | { | |
1873 | uint64_t seq; | |
1874 | ||
1875 | ret = ops->sequence_number(config, buf, &seq); | |
1876 | if (ret < 0) | |
1877 | goto error; | |
1878 | return put_u64(seq, arg); | |
1879 | } | |
5594698f JD |
1880 | case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID: |
1881 | { | |
1882 | uint64_t id; | |
1883 | ||
1884 | ret = ops->instance_id(config, buf, &id); | |
1885 | if (ret < 0) | |
1886 | goto error; | |
1887 | return put_u64(id, arg); | |
1888 | } | |
3b731ab1 JD |
1889 | default: |
1890 | return lib_ring_buffer_file_operations.compat_ioctl(filp, | |
1891 | cmd, arg); | |
1892 | } | |
1893 | ||
1894 | error: | |
1895 | return -ENOSYS; | |
ed8d02d6 JD |
1896 | } |
1897 | #endif /* CONFIG_COMPAT */ | |
1898 | ||
1899 | static void lttng_stream_override_ring_buffer_fops(void) | |
1900 | { | |
1901 | lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE; | |
1902 | lttng_stream_ring_buffer_file_operations.open = | |
1903 | lib_ring_buffer_file_operations.open; | |
1904 | lttng_stream_ring_buffer_file_operations.release = | |
1905 | lib_ring_buffer_file_operations.release; | |
1906 | lttng_stream_ring_buffer_file_operations.poll = | |
1907 | lib_ring_buffer_file_operations.poll; | |
1908 | lttng_stream_ring_buffer_file_operations.splice_read = | |
1909 | lib_ring_buffer_file_operations.splice_read; | |
1910 | lttng_stream_ring_buffer_file_operations.mmap = | |
1911 | lib_ring_buffer_file_operations.mmap; | |
1912 | lttng_stream_ring_buffer_file_operations.unlocked_ioctl = | |
1913 | lttng_stream_ring_buffer_ioctl; | |
1914 | lttng_stream_ring_buffer_file_operations.llseek = | |
1915 | lib_ring_buffer_file_operations.llseek; | |
1916 | #ifdef CONFIG_COMPAT | |
1917 | lttng_stream_ring_buffer_file_operations.compat_ioctl = | |
1918 | lttng_stream_ring_buffer_compat_ioctl; | |
1919 | #endif | |
1920 | } | |
1921 | ||
80996790 | 1922 | int __init lttng_abi_init(void) |
baf20995 MD |
1923 | { |
1924 | int ret = 0; | |
1925 | ||
6d2a620c | 1926 | wrapper_vmalloc_sync_all(); |
2754583e | 1927 | lttng_clock_ref(); |
f771eda6 JD |
1928 | |
1929 | ret = lttng_tp_mempool_init(); | |
1930 | if (ret) { | |
1931 | goto error; | |
1932 | } | |
1933 | ||
d29348f7 | 1934 | lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL, |
e6a17f26 | 1935 | <tng_fops, NULL); |
2470a237 | 1936 | |
255e52a4 | 1937 | if (!lttng_proc_dentry) { |
baf20995 MD |
1938 | printk(KERN_ERR "Error creating LTTng control file\n"); |
1939 | ret = -ENOMEM; | |
1940 | goto error; | |
1941 | } | |
ed8d02d6 | 1942 | lttng_stream_override_ring_buffer_fops(); |
2754583e | 1943 | return 0; |
ed8d02d6 | 1944 | |
baf20995 | 1945 | error: |
f771eda6 | 1946 | lttng_tp_mempool_destroy(); |
2754583e | 1947 | lttng_clock_unref(); |
baf20995 MD |
1948 | return ret; |
1949 | } | |
1950 | ||
e6e65fcd MD |
1951 | /* No __exit annotation because used by init error path too. */ |
1952 | void lttng_abi_exit(void) | |
baf20995 | 1953 | { |
f771eda6 | 1954 | lttng_tp_mempool_destroy(); |
2754583e | 1955 | lttng_clock_unref(); |
e6a17f26 MD |
1956 | if (lttng_proc_dentry) |
1957 | remove_proc_entry("lttng", NULL); | |
baf20995 | 1958 | } |