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