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