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