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