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