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