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