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