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