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