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