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