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