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