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