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