Create a memory pool for temporary tracepoint probes storage
[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 }
9c1f4643
MD
433 if (atomic_long_add_unless(&session_file->f_count,
434 1, INT_MAX) == INT_MAX) {
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;
c0e31d2e 494
ad1c05e1 495 switch (cmd) {
6dccd6c1
JD
496 case LTTNG_KERNEL_OLD_CHANNEL:
497 {
498 struct lttng_kernel_channel chan_param;
499 struct lttng_kernel_old_channel old_chan_param;
500
501 if (copy_from_user(&old_chan_param,
502 (struct lttng_kernel_old_channel __user *) arg,
503 sizeof(struct lttng_kernel_old_channel)))
504 return -EFAULT;
505 chan_param.overwrite = old_chan_param.overwrite;
506 chan_param.subbuf_size = old_chan_param.subbuf_size;
507 chan_param.num_subbuf = old_chan_param.num_subbuf;
508 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
509 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
510 chan_param.output = old_chan_param.output;
511
512 return lttng_abi_create_channel(file, &chan_param,
513 PER_CPU_CHANNEL);
514 }
38d024ae 515 case LTTNG_KERNEL_CHANNEL:
6dccd6c1
JD
516 {
517 struct lttng_kernel_channel chan_param;
518
519 if (copy_from_user(&chan_param,
38d024ae 520 (struct lttng_kernel_channel __user *) arg,
6dccd6c1
JD
521 sizeof(struct lttng_kernel_channel)))
522 return -EFAULT;
523 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 524 PER_CPU_CHANNEL);
6dccd6c1
JD
525 }
526 case LTTNG_KERNEL_OLD_SESSION_START:
527 case LTTNG_KERNEL_OLD_ENABLE:
38d024ae 528 case LTTNG_KERNEL_SESSION_START:
e64957da 529 case LTTNG_KERNEL_ENABLE:
a90917c3 530 return lttng_session_enable(session);
6dccd6c1
JD
531 case LTTNG_KERNEL_OLD_SESSION_STOP:
532 case LTTNG_KERNEL_OLD_DISABLE:
38d024ae 533 case LTTNG_KERNEL_SESSION_STOP:
e64957da 534 case LTTNG_KERNEL_DISABLE:
a90917c3 535 return lttng_session_disable(session);
6dccd6c1
JD
536 case LTTNG_KERNEL_OLD_METADATA:
537 {
538 struct lttng_kernel_channel chan_param;
539 struct lttng_kernel_old_channel old_chan_param;
540
541 if (copy_from_user(&old_chan_param,
542 (struct lttng_kernel_old_channel __user *) arg,
543 sizeof(struct lttng_kernel_old_channel)))
544 return -EFAULT;
545 chan_param.overwrite = old_chan_param.overwrite;
546 chan_param.subbuf_size = old_chan_param.subbuf_size;
547 chan_param.num_subbuf = old_chan_param.num_subbuf;
548 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
549 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
550 chan_param.output = old_chan_param.output;
551
552 return lttng_abi_create_channel(file, &chan_param,
553 METADATA_CHANNEL);
554 }
38d024ae 555 case LTTNG_KERNEL_METADATA:
6dccd6c1
JD
556 {
557 struct lttng_kernel_channel chan_param;
558
559 if (copy_from_user(&chan_param,
560 (struct lttng_kernel_channel __user *) arg,
561 sizeof(struct lttng_kernel_channel)))
562 return -EFAULT;
563 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 564 METADATA_CHANNEL);
6dccd6c1 565 }
e0130fab
MD
566 case LTTNG_KERNEL_SESSION_TRACK_PID:
567 return lttng_session_track_pid(session, (int) arg);
568 case LTTNG_KERNEL_SESSION_UNTRACK_PID:
569 return lttng_session_untrack_pid(session, (int) arg);
7e6f9ef6
MD
570 case LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS:
571 return lttng_session_list_tracker_pids(session);
9616f0bf
JD
572 case LTTNG_KERNEL_SESSION_METADATA_REGEN:
573 return lttng_session_metadata_regenerate(session);
601252cf
MD
574 case LTTNG_KERNEL_SESSION_STATEDUMP:
575 return lttng_session_statedump(session);
ad1c05e1
MD
576 default:
577 return -ENOIOCTLCMD;
578 }
579}
580
03037b98
MD
581/*
582 * Called when the last file reference is dropped.
583 *
584 * Big fat note: channels and events are invariant for the whole session after
585 * their creation. So this session destruction also destroys all channel and
586 * event structures specific to this session (they are not destroyed when their
587 * individual file is released).
588 */
ad1c05e1 589static
03037b98 590int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 591{
a90917c3 592 struct lttng_session *session = file->private_data;
c269fff4
MD
593
594 if (session)
a90917c3 595 lttng_session_destroy(session);
11b5a3c2 596 return 0;
ad1c05e1 597}
ad1c05e1
MD
598
599static const struct file_operations lttng_session_fops = {
a33c9927 600 .owner = THIS_MODULE,
03037b98 601 .release = lttng_session_release,
ad1c05e1
MD
602 .unlocked_ioctl = lttng_session_ioctl,
603#ifdef CONFIG_COMPAT
03037b98 604 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 605#endif
11b5a3c2 606};
ad1c05e1 607
d83004aa
JD
608/**
609 * lttng_metadata_ring_buffer_poll - LTTng ring buffer poll file operation
610 * @filp: the file
611 * @wait: poll table
612 *
613 * Handles the poll operations for the metadata channels.
614 */
ad1c05e1 615static
d83004aa
JD
616unsigned int lttng_metadata_ring_buffer_poll(struct file *filp,
617 poll_table *wait)
618{
619 struct lttng_metadata_stream *stream = filp->private_data;
620 struct lib_ring_buffer *buf = stream->priv;
621 int finalized;
622 unsigned int mask = 0;
623
624 if (filp->f_mode & FMODE_READ) {
625 poll_wait_set_exclusive(wait);
626 poll_wait(filp, &stream->read_wait, wait);
627
628 finalized = stream->finalized;
629
630 /*
631 * lib_ring_buffer_is_finalized() contains a smp_rmb()
632 * ordering finalized load before offsets loads.
633 */
634 WARN_ON(atomic_long_read(&buf->active_readers) != 1);
635
636 if (finalized)
637 mask |= POLLHUP;
638
92d9f5e6 639 mutex_lock(&stream->metadata_cache->lock);
d83004aa 640 if (stream->metadata_cache->metadata_written >
f613e3e6 641 stream->metadata_out)
d83004aa 642 mask |= POLLIN;
92d9f5e6 643 mutex_unlock(&stream->metadata_cache->lock);
d83004aa
JD
644 }
645
646 return mask;
647}
648
f613e3e6
MD
649static
650void lttng_metadata_ring_buffer_ioctl_put_next_subbuf(struct file *filp,
651 unsigned int cmd, unsigned long arg)
652{
653 struct lttng_metadata_stream *stream = filp->private_data;
654
655 stream->metadata_out = stream->metadata_in;
656}
657
d83004aa
JD
658static
659long lttng_metadata_ring_buffer_ioctl(struct file *filp,
660 unsigned int cmd, unsigned long arg)
661{
662 int ret;
663 struct lttng_metadata_stream *stream = filp->private_data;
664 struct lib_ring_buffer *buf = stream->priv;
665
666 switch (cmd) {
d83004aa
JD
667 case RING_BUFFER_GET_NEXT_SUBBUF:
668 {
35097f36
JD
669 struct lttng_metadata_stream *stream = filp->private_data;
670 struct lib_ring_buffer *buf = stream->priv;
671 struct channel *chan = buf->backend.chan;
672
673 ret = lttng_metadata_output_channel(stream, chan);
674 if (ret > 0) {
675 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
676 ret = 0;
677 } else if (ret < 0)
d83004aa
JD
678 goto err;
679 break;
680 }
f613e3e6
MD
681 case RING_BUFFER_GET_SUBBUF:
682 {
683 /*
684 * Random access is not allowed for metadata channel.
685 */
686 return -ENOSYS;
687 }
e1cdd6e9 688 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
35097f36
JD
689 case RING_BUFFER_FLUSH:
690 {
691 struct lttng_metadata_stream *stream = filp->private_data;
692 struct lib_ring_buffer *buf = stream->priv;
693 struct channel *chan = buf->backend.chan;
694
695 /*
696 * Before doing the actual ring buffer flush, write up to one
697 * packet of metadata in the ring buffer.
698 */
699 ret = lttng_metadata_output_channel(stream, chan);
700 if (ret < 0)
701 goto err;
702 break;
703 }
9616f0bf
JD
704 case RING_BUFFER_GET_METADATA_VERSION:
705 {
706 struct lttng_metadata_stream *stream = filp->private_data;
707
708 return put_u64(stream->version, arg);
709 }
d83004aa
JD
710 default:
711 break;
712 }
f613e3e6
MD
713 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
714
d83004aa 715 /* Performing lib ring buffer ioctl after our own. */
f613e3e6
MD
716 ret = lib_ring_buffer_ioctl(filp, cmd, arg, buf);
717 if (ret < 0)
718 goto err;
d83004aa 719
f613e3e6
MD
720 switch (cmd) {
721 case RING_BUFFER_PUT_NEXT_SUBBUF:
722 {
723 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
724 cmd, arg);
725 break;
726 }
727 default:
728 break;
729 }
d83004aa
JD
730err:
731 return ret;
732}
733
aeb9064d 734#ifdef CONFIG_COMPAT
d83004aa
JD
735static
736long lttng_metadata_ring_buffer_compat_ioctl(struct file *filp,
737 unsigned int cmd, unsigned long arg)
738{
739 int ret;
740 struct lttng_metadata_stream *stream = filp->private_data;
741 struct lib_ring_buffer *buf = stream->priv;
742
743 switch (cmd) {
d83004aa
JD
744 case RING_BUFFER_GET_NEXT_SUBBUF:
745 {
35097f36
JD
746 struct lttng_metadata_stream *stream = filp->private_data;
747 struct lib_ring_buffer *buf = stream->priv;
748 struct channel *chan = buf->backend.chan;
749
750 ret = lttng_metadata_output_channel(stream, chan);
751 if (ret > 0) {
752 lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE);
753 ret = 0;
754 } else if (ret < 0)
d83004aa
JD
755 goto err;
756 break;
757 }
f613e3e6
MD
758 case RING_BUFFER_GET_SUBBUF:
759 {
760 /*
761 * Random access is not allowed for metadata channel.
762 */
763 return -ENOSYS;
764 }
e1cdd6e9 765 case RING_BUFFER_FLUSH_EMPTY: /* Fall-through. */
96c55c2f
MD
766 case RING_BUFFER_FLUSH:
767 {
768 struct lttng_metadata_stream *stream = filp->private_data;
769 struct lib_ring_buffer *buf = stream->priv;
770 struct channel *chan = buf->backend.chan;
771
772 /*
773 * Before doing the actual ring buffer flush, write up to one
774 * packet of metadata in the ring buffer.
775 */
776 ret = lttng_metadata_output_channel(stream, chan);
777 if (ret < 0)
778 goto err;
779 break;
780 }
781 case RING_BUFFER_GET_METADATA_VERSION:
782 {
783 struct lttng_metadata_stream *stream = filp->private_data;
784
785 return put_u64(stream->version, arg);
786 }
d83004aa
JD
787 default:
788 break;
789 }
f613e3e6
MD
790 /* PUT_SUBBUF is the one from lib ring buffer, unmodified. */
791
d83004aa 792 /* Performing lib ring buffer ioctl after our own. */
f613e3e6
MD
793 ret = lib_ring_buffer_compat_ioctl(filp, cmd, arg, buf);
794 if (ret < 0)
795 goto err;
d83004aa 796
f613e3e6
MD
797 switch (cmd) {
798 case RING_BUFFER_PUT_NEXT_SUBBUF:
799 {
800 lttng_metadata_ring_buffer_ioctl_put_next_subbuf(filp,
801 cmd, arg);
802 break;
803 }
804 default:
805 break;
806 }
d83004aa
JD
807err:
808 return ret;
809}
aeb9064d 810#endif
d83004aa 811
b3b8072b
MD
812/*
813 * This is not used by anonymous file descriptors. This code is left
814 * there if we ever want to implement an inode with open() operation.
815 */
d83004aa
JD
816static
817int lttng_metadata_ring_buffer_open(struct inode *inode, struct file *file)
818{
819 struct lttng_metadata_stream *stream = inode->i_private;
820 struct lib_ring_buffer *buf = stream->priv;
821
822 file->private_data = buf;
b3b8072b
MD
823 /*
824 * Since life-time of metadata cache differs from that of
825 * session, we need to keep our own reference on the transport.
826 */
827 if (!try_module_get(stream->transport->owner)) {
828 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
829 return -EBUSY;
830 }
d83004aa
JD
831 return lib_ring_buffer_open(inode, file, buf);
832}
833
834static
835int lttng_metadata_ring_buffer_release(struct inode *inode, struct file *file)
836{
837 struct lttng_metadata_stream *stream = file->private_data;
838 struct lib_ring_buffer *buf = stream->priv;
839
840 kref_put(&stream->metadata_cache->refcount, metadata_cache_destroy);
b3b8072b 841 module_put(stream->transport->owner);
d83004aa
JD
842 return lib_ring_buffer_release(inode, file, buf);
843}
844
845static
846ssize_t lttng_metadata_ring_buffer_splice_read(struct file *in, loff_t *ppos,
847 struct pipe_inode_info *pipe, size_t len,
848 unsigned int flags)
849{
850 struct lttng_metadata_stream *stream = in->private_data;
851 struct lib_ring_buffer *buf = stream->priv;
852
853 return lib_ring_buffer_splice_read(in, ppos, pipe, len,
854 flags, buf);
855}
856
857static
858int lttng_metadata_ring_buffer_mmap(struct file *filp,
859 struct vm_area_struct *vma)
860{
861 struct lttng_metadata_stream *stream = filp->private_data;
862 struct lib_ring_buffer *buf = stream->priv;
863
864 return lib_ring_buffer_mmap(filp, vma, buf);
865}
866
867static
868const struct file_operations lttng_metadata_ring_buffer_file_operations = {
869 .owner = THIS_MODULE,
870 .open = lttng_metadata_ring_buffer_open,
871 .release = lttng_metadata_ring_buffer_release,
872 .poll = lttng_metadata_ring_buffer_poll,
873 .splice_read = lttng_metadata_ring_buffer_splice_read,
874 .mmap = lttng_metadata_ring_buffer_mmap,
875 .unlocked_ioctl = lttng_metadata_ring_buffer_ioctl,
876 .llseek = vfs_lib_ring_buffer_no_llseek,
877#ifdef CONFIG_COMPAT
878 .compat_ioctl = lttng_metadata_ring_buffer_compat_ioctl,
879#endif
880};
881
882static
883int lttng_abi_create_stream_fd(struct file *channel_file, void *stream_priv,
884 const struct file_operations *fops)
ad1c05e1 885{
ad1c05e1 886 int stream_fd, ret;
11b5a3c2 887 struct file *stream_file;
ad1c05e1 888
4ac10b76 889 stream_fd = lttng_get_unused_fd();
ad1c05e1
MD
890 if (stream_fd < 0) {
891 ret = stream_fd;
892 goto fd_error;
893 }
d83004aa
JD
894 stream_file = anon_inode_getfile("[lttng_stream]", fops,
895 stream_priv, O_RDWR);
c0e31d2e
MD
896 if (IS_ERR(stream_file)) {
897 ret = PTR_ERR(stream_file);
ad1c05e1
MD
898 goto file_error;
899 }
409453cb
MD
900 /*
901 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
902 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
903 * file descriptor, so we set FMODE_PREAD here.
904 */
d7b6f197 905 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 906 fd_install(stream_fd, stream_file);
dda6a249
MD
907 /*
908 * The stream holds a reference to the channel within the generic ring
909 * buffer library, so no need to hold a refcount on the channel and
910 * session files here.
911 */
ad1c05e1
MD
912 return stream_fd;
913
914file_error:
915 put_unused_fd(stream_fd);
d83004aa
JD
916fd_error:
917 return ret;
918}
919
920static
921int lttng_abi_open_stream(struct file *channel_file)
922{
923 struct lttng_channel *channel = channel_file->private_data;
924 struct lib_ring_buffer *buf;
925 int ret;
926 void *stream_priv;
927
928 buf = channel->ops->buffer_read_open(channel->chan);
929 if (!buf)
930 return -ENOENT;
931
932 stream_priv = buf;
933 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
ed8d02d6 934 &lttng_stream_ring_buffer_file_operations);
d83004aa
JD
935 if (ret < 0)
936 goto fd_error;
937
938 return ret;
939
940fd_error:
941 channel->ops->buffer_read_close(buf);
942 return ret;
943}
944
945static
946int lttng_abi_open_metadata_stream(struct file *channel_file)
947{
948 struct lttng_channel *channel = channel_file->private_data;
949 struct lttng_session *session = channel->session;
950 struct lib_ring_buffer *buf;
951 int ret;
952 struct lttng_metadata_stream *metadata_stream;
953 void *stream_priv;
954
955 buf = channel->ops->buffer_read_open(channel->chan);
956 if (!buf)
957 return -ENOENT;
958
959 metadata_stream = kzalloc(sizeof(struct lttng_metadata_stream),
960 GFP_KERNEL);
b3b8072b
MD
961 if (!metadata_stream) {
962 ret = -ENOMEM;
963 goto nomem;
964 }
d83004aa
JD
965 metadata_stream->metadata_cache = session->metadata_cache;
966 init_waitqueue_head(&metadata_stream->read_wait);
967 metadata_stream->priv = buf;
968 stream_priv = metadata_stream;
b3b8072b
MD
969 metadata_stream->transport = channel->transport;
970
971 /*
972 * Since life-time of metadata cache differs from that of
973 * session, we need to keep our own reference on the transport.
974 */
975 if (!try_module_get(metadata_stream->transport->owner)) {
976 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
977 ret = -EINVAL;
978 goto notransport;
979 }
980
901aaa5f
FD
981 if (!lttng_kref_get(&session->metadata_cache->refcount)) {
982 ret = -EOVERFLOW;
9c1f4643 983 goto kref_error;
901aaa5f
FD
984 }
985
d83004aa
JD
986 ret = lttng_abi_create_stream_fd(channel_file, stream_priv,
987 &lttng_metadata_ring_buffer_file_operations);
988 if (ret < 0)
989 goto fd_error;
990
d83004aa
JD
991 list_add(&metadata_stream->list,
992 &session->metadata_cache->metadata_stream);
993 return ret;
994
ad1c05e1 995fd_error:
9c1f4643
MD
996 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
997kref_error:
b3b8072b
MD
998 module_put(metadata_stream->transport->owner);
999notransport:
1000 kfree(metadata_stream);
1001nomem:
11b5a3c2 1002 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
1003 return ret;
1004}
1005
653fe716 1006static
c0e31d2e 1007int lttng_abi_create_event(struct file *channel_file,
6dccd6c1 1008 struct lttng_kernel_event *event_param)
653fe716 1009{
a90917c3 1010 struct lttng_channel *channel = channel_file->private_data;
653fe716 1011 int event_fd, ret;
11b5a3c2 1012 struct file *event_file;
3c997079 1013 void *priv;
653fe716 1014
6dccd6c1
JD
1015 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
1016 switch (event_param->instrumentation) {
7371f44c 1017 case LTTNG_KERNEL_KRETPROBE:
6dccd6c1 1018 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
7371f44c 1019 break;
ab2277d6 1020 case LTTNG_KERNEL_KPROBE:
6dccd6c1 1021 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4 1022 break;
ab2277d6 1023 case LTTNG_KERNEL_FUNCTION:
6dccd6c1 1024 event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4
MD
1025 break;
1026 default:
1027 break;
1028 }
33a39a3c
MD
1029 event_fd = lttng_get_unused_fd();
1030 if (event_fd < 0) {
1031 ret = event_fd;
1032 goto fd_error;
1033 }
1034 event_file = anon_inode_getfile("[lttng_event]",
1035 &lttng_event_fops,
1036 NULL, O_RDWR);
1037 if (IS_ERR(event_file)) {
1038 ret = PTR_ERR(event_file);
1039 goto file_error;
1040 }
9c1f4643
MD
1041 /* The event holds a reference on the channel */
1042 if (atomic_long_add_unless(&channel_file->f_count,
1043 1, INT_MAX) == INT_MAX) {
0d2c717f 1044 ret = -EOVERFLOW;
9c1f4643
MD
1045 goto refcount_error;
1046 }
33a39a3c
MD
1047 if (event_param->instrumentation == LTTNG_KERNEL_TRACEPOINT
1048 || event_param->instrumentation == LTTNG_KERNEL_SYSCALL) {
1049 struct lttng_enabler *enabler;
1050
4993071a
PP
1051 if (strutils_is_star_glob_pattern(event_param->name)) {
1052 /*
1053 * If the event name is a star globbing pattern,
1054 * we create the special star globbing enabler.
1055 */
1056 enabler = lttng_enabler_create(LTTNG_ENABLER_STAR_GLOB,
33a39a3c 1057 event_param, channel);
3c997079 1058 } else {
33a39a3c
MD
1059 enabler = lttng_enabler_create(LTTNG_ENABLER_NAME,
1060 event_param, channel);
1ec65de1 1061 }
33a39a3c
MD
1062 priv = enabler;
1063 } else {
1064 struct lttng_event *event;
4ee2453d 1065
33a39a3c
MD
1066 /*
1067 * We tolerate no failure path after event creation. It
1068 * will stay invariant for the rest of the session.
1069 */
1070 event = lttng_event_create(channel, event_param,
1071 NULL, NULL,
1072 event_param->instrumentation);
1073 WARN_ON_ONCE(!event);
1074 if (IS_ERR(event)) {
1075 ret = PTR_ERR(event);
1076 goto event_error;
80f87dd2 1077 }
33a39a3c 1078 priv = event;
03037b98 1079 }
33a39a3c
MD
1080 event_file->private_data = priv;
1081 fd_install(event_fd, event_file);
653fe716
MD
1082 return event_fd;
1083
03037b98 1084event_error:
9c1f4643
MD
1085 atomic_long_dec(&channel_file->f_count);
1086refcount_error:
c0e31d2e 1087 fput(event_file);
653fe716
MD
1088file_error:
1089 put_unused_fd(event_fd);
1090fd_error:
653fe716
MD
1091 return ret;
1092}
ad1c05e1
MD
1093
1094/**
1095 * lttng_channel_ioctl - lttng syscall through ioctl
1096 *
c0e31d2e 1097 * @file: the file
ad1c05e1
MD
1098 * @cmd: the command
1099 * @arg: command arg
1100 *
1101 * This ioctl implements lttng commands:
38d024ae 1102 * LTTNG_KERNEL_STREAM
ad1c05e1
MD
1103 * Returns an event stream file descriptor or failure.
1104 * (typically, one event stream records events from one CPU)
38d024ae 1105 * LTTNG_KERNEL_EVENT
ad1c05e1 1106 * Returns an event file descriptor or failure.
8070f5c0
MD
1107 * LTTNG_KERNEL_CONTEXT
1108 * Prepend a context field to each event in the channel
e64957da
MD
1109 * LTTNG_KERNEL_ENABLE
1110 * Enable recording for events in this channel (weak enable)
1111 * LTTNG_KERNEL_DISABLE
1112 * Disable recording for events in this channel (strong disable)
baf20995 1113 *
baf20995
MD
1114 * Channel and event file descriptors also hold a reference on the session.
1115 */
ad1c05e1 1116static
c0e31d2e 1117long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 1118{
a90917c3 1119 struct lttng_channel *channel = file->private_data;
8070f5c0 1120
baf20995 1121 switch (cmd) {
6dccd6c1 1122 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 1123 case LTTNG_KERNEL_STREAM:
c0e31d2e 1124 return lttng_abi_open_stream(file);
6dccd6c1
JD
1125 case LTTNG_KERNEL_OLD_EVENT:
1126 {
1127 struct lttng_kernel_event *uevent_param;
1128 struct lttng_kernel_old_event *old_uevent_param;
1129 int ret;
1130
1131 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
1132 GFP_KERNEL);
1133 if (!uevent_param) {
1134 ret = -ENOMEM;
1135 goto old_event_end;
1136 }
1137 old_uevent_param = kmalloc(
1138 sizeof(struct lttng_kernel_old_event),
1139 GFP_KERNEL);
1140 if (!old_uevent_param) {
1141 ret = -ENOMEM;
1142 goto old_event_error_free_param;
1143 }
1144 if (copy_from_user(old_uevent_param,
1145 (struct lttng_kernel_old_event __user *) arg,
1146 sizeof(struct lttng_kernel_old_event))) {
1147 ret = -EFAULT;
1148 goto old_event_error_free_old_param;
1149 }
1150
1151 memcpy(uevent_param->name, old_uevent_param->name,
1152 sizeof(uevent_param->name));
1153 uevent_param->instrumentation =
1154 old_uevent_param->instrumentation;
1155
1156 switch (old_uevent_param->instrumentation) {
1157 case LTTNG_KERNEL_KPROBE:
1158 uevent_param->u.kprobe.addr =
1159 old_uevent_param->u.kprobe.addr;
1160 uevent_param->u.kprobe.offset =
1161 old_uevent_param->u.kprobe.offset;
1162 memcpy(uevent_param->u.kprobe.symbol_name,
1163 old_uevent_param->u.kprobe.symbol_name,
1164 sizeof(uevent_param->u.kprobe.symbol_name));
1165 break;
1166 case LTTNG_KERNEL_KRETPROBE:
1167 uevent_param->u.kretprobe.addr =
1168 old_uevent_param->u.kretprobe.addr;
1169 uevent_param->u.kretprobe.offset =
1170 old_uevent_param->u.kretprobe.offset;
1171 memcpy(uevent_param->u.kretprobe.symbol_name,
1172 old_uevent_param->u.kretprobe.symbol_name,
1173 sizeof(uevent_param->u.kretprobe.symbol_name));
1174 break;
1175 case LTTNG_KERNEL_FUNCTION:
1176 memcpy(uevent_param->u.ftrace.symbol_name,
1177 old_uevent_param->u.ftrace.symbol_name,
1178 sizeof(uevent_param->u.ftrace.symbol_name));
1179 break;
1180 default:
1181 break;
1182 }
1183 ret = lttng_abi_create_event(file, uevent_param);
1184
1185old_event_error_free_old_param:
1186 kfree(old_uevent_param);
1187old_event_error_free_param:
1188 kfree(uevent_param);
1189old_event_end:
1190 return ret;
1191 }
38d024ae 1192 case LTTNG_KERNEL_EVENT:
6dccd6c1
JD
1193 {
1194 struct lttng_kernel_event uevent_param;
1195
1196 if (copy_from_user(&uevent_param,
1197 (struct lttng_kernel_event __user *) arg,
1198 sizeof(uevent_param)))
1199 return -EFAULT;
1200 return lttng_abi_create_event(file, &uevent_param);
1201 }
1202 case LTTNG_KERNEL_OLD_CONTEXT:
1203 {
1204 struct lttng_kernel_context *ucontext_param;
1205 struct lttng_kernel_old_context *old_ucontext_param;
1206 int ret;
1207
1208 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
1209 GFP_KERNEL);
1210 if (!ucontext_param) {
1211 ret = -ENOMEM;
1212 goto old_ctx_end;
1213 }
1214 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
1215 GFP_KERNEL);
1216 if (!old_ucontext_param) {
1217 ret = -ENOMEM;
1218 goto old_ctx_error_free_param;
1219 }
1220
1221 if (copy_from_user(old_ucontext_param,
1222 (struct lttng_kernel_old_context __user *) arg,
1223 sizeof(struct lttng_kernel_old_context))) {
1224 ret = -EFAULT;
1225 goto old_ctx_error_free_old_param;
1226 }
1227 ucontext_param->ctx = old_ucontext_param->ctx;
1228 memcpy(ucontext_param->padding, old_ucontext_param->padding,
1229 sizeof(ucontext_param->padding));
1230 /* only type that uses the union */
1231 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
1232 ucontext_param->u.perf_counter.type =
1233 old_ucontext_param->u.perf_counter.type;
1234 ucontext_param->u.perf_counter.config =
1235 old_ucontext_param->u.perf_counter.config;
1236 memcpy(ucontext_param->u.perf_counter.name,
1237 old_ucontext_param->u.perf_counter.name,
1238 sizeof(ucontext_param->u.perf_counter.name));
1239 }
1240
1241 ret = lttng_abi_add_context(file,
1242 ucontext_param,
1243 &channel->ctx, channel->session);
1244
1245old_ctx_error_free_old_param:
1246 kfree(old_ucontext_param);
1247old_ctx_error_free_param:
1248 kfree(ucontext_param);
1249old_ctx_end:
1250 return ret;
1251 }
8070f5c0 1252 case LTTNG_KERNEL_CONTEXT:
6dccd6c1
JD
1253 {
1254 struct lttng_kernel_context ucontext_param;
1255
1256 if (copy_from_user(&ucontext_param,
8070f5c0 1257 (struct lttng_kernel_context __user *) arg,
6dccd6c1
JD
1258 sizeof(ucontext_param)))
1259 return -EFAULT;
1260 return lttng_abi_add_context(file,
1261 &ucontext_param,
8070f5c0 1262 &channel->ctx, channel->session);
6dccd6c1
JD
1263 }
1264 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 1265 case LTTNG_KERNEL_ENABLE:
a90917c3 1266 return lttng_channel_enable(channel);
6dccd6c1 1267 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 1268 case LTTNG_KERNEL_DISABLE:
a90917c3 1269 return lttng_channel_disable(channel);
12e579db
MD
1270 case LTTNG_KERNEL_SYSCALL_MASK:
1271 return lttng_channel_syscall_mask(channel,
1272 (struct lttng_kernel_syscall_mask __user *) arg);
baf20995
MD
1273 default:
1274 return -ENOIOCTLCMD;
1275 }
6dccd6c1 1276
baf20995
MD
1277}
1278
5dbbdb43
MD
1279/**
1280 * lttng_metadata_ioctl - lttng syscall through ioctl
1281 *
1282 * @file: the file
1283 * @cmd: the command
1284 * @arg: command arg
1285 *
1286 * This ioctl implements lttng commands:
38d024ae 1287 * LTTNG_KERNEL_STREAM
5dbbdb43
MD
1288 * Returns an event stream file descriptor or failure.
1289 *
1290 * Channel and event file descriptors also hold a reference on the session.
1291 */
1292static
1293long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1294{
1295 switch (cmd) {
6dccd6c1 1296 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 1297 case LTTNG_KERNEL_STREAM:
d83004aa 1298 return lttng_abi_open_metadata_stream(file);
5dbbdb43
MD
1299 default:
1300 return -ENOIOCTLCMD;
1301 }
1302}
1303
653fe716
MD
1304/**
1305 * lttng_channel_poll - lttng stream addition/removal monitoring
1306 *
c0e31d2e 1307 * @file: the file
653fe716
MD
1308 * @wait: poll table
1309 */
c0e31d2e 1310unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 1311{
a90917c3 1312 struct lttng_channel *channel = file->private_data;
653fe716
MD
1313 unsigned int mask = 0;
1314
c0e31d2e 1315 if (file->f_mode & FMODE_READ) {
a33e44a6 1316 poll_wait_set_exclusive(wait);
24cedcfe
MD
1317 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
1318 wait);
653fe716 1319
254ec7bc
MD
1320 if (channel->ops->is_disabled(channel->chan))
1321 return POLLERR;
24cedcfe 1322 if (channel->ops->is_finalized(channel->chan))
653fe716 1323 return POLLHUP;
f71ecafa 1324 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
653fe716 1325 return POLLIN | POLLRDNORM;
f71ecafa 1326 return 0;
653fe716
MD
1327 }
1328 return mask;
1329
1330}
1331
0a84a57f
MD
1332static
1333int lttng_channel_release(struct inode *inode, struct file *file)
1334{
a90917c3 1335 struct lttng_channel *channel = file->private_data;
c269fff4
MD
1336
1337 if (channel)
1338 fput(channel->session->file);
0a84a57f
MD
1339 return 0;
1340}
1341
d83004aa
JD
1342static
1343int lttng_metadata_channel_release(struct inode *inode, struct file *file)
1344{
1345 struct lttng_channel *channel = file->private_data;
1346
1347 if (channel) {
d83004aa 1348 fput(channel->session->file);
a3381417 1349 lttng_metadata_channel_destroy(channel);
d83004aa
JD
1350 }
1351
1352 return 0;
1353}
1354
ad1c05e1 1355static const struct file_operations lttng_channel_fops = {
a33c9927 1356 .owner = THIS_MODULE,
03037b98 1357 .release = lttng_channel_release,
653fe716 1358 .poll = lttng_channel_poll,
ad1c05e1 1359 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 1360#ifdef CONFIG_COMPAT
03037b98 1361 .compat_ioctl = lttng_channel_ioctl,
baf20995 1362#endif
11b5a3c2 1363};
baf20995 1364
5dbbdb43 1365static const struct file_operations lttng_metadata_fops = {
a33c9927 1366 .owner = THIS_MODULE,
d83004aa 1367 .release = lttng_metadata_channel_release,
5dbbdb43
MD
1368 .unlocked_ioctl = lttng_metadata_ioctl,
1369#ifdef CONFIG_COMPAT
1370 .compat_ioctl = lttng_metadata_ioctl,
1371#endif
1372};
1373
8070f5c0
MD
1374/**
1375 * lttng_event_ioctl - lttng syscall through ioctl
1376 *
1377 * @file: the file
1378 * @cmd: the command
1379 * @arg: command arg
1380 *
1381 * This ioctl implements lttng commands:
8070f5c0
MD
1382 * LTTNG_KERNEL_CONTEXT
1383 * Prepend a context field to each record of this event
e64957da
MD
1384 * LTTNG_KERNEL_ENABLE
1385 * Enable recording for this event (weak enable)
1386 * LTTNG_KERNEL_DISABLE
1387 * Disable recording for this event (strong disable)
8070f5c0
MD
1388 */
1389static
1390long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1391{
3c997079
MD
1392 struct lttng_event *event;
1393 struct lttng_enabler *enabler;
1394 enum lttng_event_type *evtype = file->private_data;
8070f5c0
MD
1395
1396 switch (cmd) {
6dccd6c1
JD
1397 case LTTNG_KERNEL_OLD_CONTEXT:
1398 {
3c997079
MD
1399 /* Not implemented */
1400 return -ENOSYS;
6dccd6c1 1401 }
8070f5c0 1402 case LTTNG_KERNEL_CONTEXT:
6dccd6c1 1403 {
3c997079
MD
1404 /* Not implemented */
1405 return -ENOSYS;
6dccd6c1
JD
1406 }
1407 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 1408 case LTTNG_KERNEL_ENABLE:
3c997079
MD
1409 switch (*evtype) {
1410 case LTTNG_TYPE_EVENT:
1411 event = file->private_data;
1412 return lttng_event_enable(event);
1413 case LTTNG_TYPE_ENABLER:
1414 enabler = file->private_data;
1415 return lttng_enabler_enable(enabler);
1416 default:
1417 WARN_ON_ONCE(1);
1418 return -ENOSYS;
1419 }
6dccd6c1 1420 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 1421 case LTTNG_KERNEL_DISABLE:
3c997079
MD
1422 switch (*evtype) {
1423 case LTTNG_TYPE_EVENT:
1424 event = file->private_data;
1425 return lttng_event_disable(event);
1426 case LTTNG_TYPE_ENABLER:
1427 enabler = file->private_data;
1428 return lttng_enabler_disable(enabler);
1429 default:
1430 WARN_ON_ONCE(1);
1431 return -ENOSYS;
1432 }
07dfc1d0
MD
1433 case LTTNG_KERNEL_FILTER:
1434 switch (*evtype) {
1435 case LTTNG_TYPE_EVENT:
1436 return -EINVAL;
1437 case LTTNG_TYPE_ENABLER:
1438 {
1439 enabler = file->private_data;
1440 return lttng_enabler_attach_bytecode(enabler,
1441 (struct lttng_kernel_filter_bytecode __user *) arg);
1442 }
1443
1444 }
8070f5c0
MD
1445 default:
1446 return -ENOIOCTLCMD;
1447 }
1448}
1449
0a84a57f
MD
1450static
1451int lttng_event_release(struct inode *inode, struct file *file)
1452{
3c997079
MD
1453 struct lttng_event *event;
1454 struct lttng_enabler *enabler;
1455 enum lttng_event_type *evtype = file->private_data;
1456
1457 if (!evtype)
1458 return 0;
1459
1460 switch (*evtype) {
1461 case LTTNG_TYPE_EVENT:
1462 event = file->private_data;
1463 if (event)
1464 fput(event->chan->file);
1465 break;
1466 case LTTNG_TYPE_ENABLER:
1467 enabler = file->private_data;
1468 if (enabler)
1469 fput(enabler->chan->file);
1470 break;
1471 default:
1472 WARN_ON_ONCE(1);
1473 break;
1474 }
c269fff4 1475
0a84a57f
MD
1476 return 0;
1477}
1478
3b923e5b 1479/* TODO: filter control ioctl */
0a84a57f 1480static const struct file_operations lttng_event_fops = {
a33c9927 1481 .owner = THIS_MODULE,
0a84a57f 1482 .release = lttng_event_release,
8070f5c0
MD
1483 .unlocked_ioctl = lttng_event_ioctl,
1484#ifdef CONFIG_COMPAT
1485 .compat_ioctl = lttng_event_ioctl,
1486#endif
11b5a3c2 1487};
0a84a57f 1488
3b731ab1
JD
1489static int put_u64(uint64_t val, unsigned long arg)
1490{
1491 return put_user(val, (uint64_t __user *) arg);
1492}
1493
ed8d02d6
JD
1494static long lttng_stream_ring_buffer_ioctl(struct file *filp,
1495 unsigned int cmd, unsigned long arg)
1496{
3b731ab1
JD
1497 struct lib_ring_buffer *buf = filp->private_data;
1498 struct channel *chan = buf->backend.chan;
1499 const struct lib_ring_buffer_config *config = &chan->backend.config;
dd5a0db3 1500 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
1501 int ret;
1502
1503 if (atomic_read(&chan->record_disabled))
1504 return -EIO;
1505
ed8d02d6 1506 switch (cmd) {
3b731ab1
JD
1507 case LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN:
1508 {
1509 uint64_t ts;
1510
dd5a0db3 1511 ret = ops->timestamp_begin(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_TIMESTAMP_END:
1517 {
1518 uint64_t ts;
1519
dd5a0db3 1520 ret = ops->timestamp_end(config, buf, &ts);
3b731ab1
JD
1521 if (ret < 0)
1522 goto error;
1523 return put_u64(ts, arg);
1524 }
1525 case LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED:
1526 {
1527 uint64_t ed;
1528
dd5a0db3 1529 ret = ops->events_discarded(config, buf, &ed);
3b731ab1
JD
1530 if (ret < 0)
1531 goto error;
1532 return put_u64(ed, arg);
1533 }
1534 case LTTNG_RING_BUFFER_GET_CONTENT_SIZE:
1535 {
1536 uint64_t cs;
1537
dd5a0db3 1538 ret = ops->content_size(config, buf, &cs);
3b731ab1
JD
1539 if (ret < 0)
1540 goto error;
1541 return put_u64(cs, arg);
1542 }
1543 case LTTNG_RING_BUFFER_GET_PACKET_SIZE:
1544 {
1545 uint64_t ps;
1546
dd5a0db3 1547 ret = ops->packet_size(config, buf, &ps);
3b731ab1
JD
1548 if (ret < 0)
1549 goto error;
1550 return put_u64(ps, arg);
1551 }
1552 case LTTNG_RING_BUFFER_GET_STREAM_ID:
1553 {
1554 uint64_t si;
1555
dd5a0db3 1556 ret = ops->stream_id(config, buf, &si);
3b731ab1
JD
1557 if (ret < 0)
1558 goto error;
1559 return put_u64(si, arg);
1560 }
2348ca17
JD
1561 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1562 {
1563 uint64_t ts;
1564
dd5a0db3 1565 ret = ops->current_timestamp(config, buf, &ts);
2348ca17
JD
1566 if (ret < 0)
1567 goto error;
1568 return put_u64(ts, arg);
1569 }
5b3cf4f9
JD
1570 case LTTNG_RING_BUFFER_GET_SEQ_NUM:
1571 {
1572 uint64_t seq;
1573
1574 ret = ops->sequence_number(config, buf, &seq);
1575 if (ret < 0)
1576 goto error;
1577 return put_u64(seq, arg);
1578 }
5594698f
JD
1579 case LTTNG_RING_BUFFER_INSTANCE_ID:
1580 {
1581 uint64_t id;
1582
1583 ret = ops->instance_id(config, buf, &id);
1584 if (ret < 0)
1585 goto error;
1586 return put_u64(id, arg);
1587 }
3b731ab1
JD
1588 default:
1589 return lib_ring_buffer_file_operations.unlocked_ioctl(filp,
1590 cmd, arg);
ed8d02d6 1591 }
3b731ab1
JD
1592
1593error:
1594 return -ENOSYS;
ed8d02d6
JD
1595}
1596
1597#ifdef CONFIG_COMPAT
1598static long lttng_stream_ring_buffer_compat_ioctl(struct file *filp,
1599 unsigned int cmd, unsigned long arg)
1600{
3b731ab1
JD
1601 struct lib_ring_buffer *buf = filp->private_data;
1602 struct channel *chan = buf->backend.chan;
1603 const struct lib_ring_buffer_config *config = &chan->backend.config;
dd5a0db3 1604 const struct lttng_channel_ops *ops = chan->backend.priv_ops;
3b731ab1
JD
1605 int ret;
1606
1607 if (atomic_read(&chan->record_disabled))
1608 return -EIO;
1609
ed8d02d6 1610 switch (cmd) {
3b731ab1
JD
1611 case LTTNG_RING_BUFFER_COMPAT_GET_TIMESTAMP_BEGIN:
1612 {
1613 uint64_t ts;
1614
dd5a0db3 1615 ret = ops->timestamp_begin(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_TIMESTAMP_END:
1621 {
1622 uint64_t ts;
1623
dd5a0db3 1624 ret = ops->timestamp_end(config, buf, &ts);
3b731ab1
JD
1625 if (ret < 0)
1626 goto error;
1627 return put_u64(ts, arg);
1628 }
1629 case LTTNG_RING_BUFFER_COMPAT_GET_EVENTS_DISCARDED:
1630 {
1631 uint64_t ed;
1632
dd5a0db3 1633 ret = ops->events_discarded(config, buf, &ed);
3b731ab1
JD
1634 if (ret < 0)
1635 goto error;
1636 return put_u64(ed, arg);
ed8d02d6 1637 }
3b731ab1
JD
1638 case LTTNG_RING_BUFFER_COMPAT_GET_CONTENT_SIZE:
1639 {
1640 uint64_t cs;
1641
dd5a0db3 1642 ret = ops->content_size(config, buf, &cs);
3b731ab1
JD
1643 if (ret < 0)
1644 goto error;
1645 return put_u64(cs, arg);
1646 }
1647 case LTTNG_RING_BUFFER_COMPAT_GET_PACKET_SIZE:
1648 {
1649 uint64_t ps;
1650
dd5a0db3 1651 ret = ops->packet_size(config, buf, &ps);
3b731ab1
JD
1652 if (ret < 0)
1653 goto error;
1654 return put_u64(ps, arg);
1655 }
1656 case LTTNG_RING_BUFFER_COMPAT_GET_STREAM_ID:
1657 {
1658 uint64_t si;
1659
dd5a0db3 1660 ret = ops->stream_id(config, buf, &si);
3b731ab1
JD
1661 if (ret < 0)
1662 goto error;
1663 return put_u64(si, arg);
1664 }
2348ca17
JD
1665 case LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP:
1666 {
1667 uint64_t ts;
1668
dd5a0db3 1669 ret = ops->current_timestamp(config, buf, &ts);
2348ca17
JD
1670 if (ret < 0)
1671 goto error;
1672 return put_u64(ts, arg);
1673 }
5b3cf4f9
JD
1674 case LTTNG_RING_BUFFER_COMPAT_GET_SEQ_NUM:
1675 {
1676 uint64_t seq;
1677
1678 ret = ops->sequence_number(config, buf, &seq);
1679 if (ret < 0)
1680 goto error;
1681 return put_u64(seq, arg);
1682 }
5594698f
JD
1683 case LTTNG_RING_BUFFER_COMPAT_INSTANCE_ID:
1684 {
1685 uint64_t id;
1686
1687 ret = ops->instance_id(config, buf, &id);
1688 if (ret < 0)
1689 goto error;
1690 return put_u64(id, arg);
1691 }
3b731ab1
JD
1692 default:
1693 return lib_ring_buffer_file_operations.compat_ioctl(filp,
1694 cmd, arg);
1695 }
1696
1697error:
1698 return -ENOSYS;
ed8d02d6
JD
1699}
1700#endif /* CONFIG_COMPAT */
1701
1702static void lttng_stream_override_ring_buffer_fops(void)
1703{
1704 lttng_stream_ring_buffer_file_operations.owner = THIS_MODULE;
1705 lttng_stream_ring_buffer_file_operations.open =
1706 lib_ring_buffer_file_operations.open;
1707 lttng_stream_ring_buffer_file_operations.release =
1708 lib_ring_buffer_file_operations.release;
1709 lttng_stream_ring_buffer_file_operations.poll =
1710 lib_ring_buffer_file_operations.poll;
1711 lttng_stream_ring_buffer_file_operations.splice_read =
1712 lib_ring_buffer_file_operations.splice_read;
1713 lttng_stream_ring_buffer_file_operations.mmap =
1714 lib_ring_buffer_file_operations.mmap;
1715 lttng_stream_ring_buffer_file_operations.unlocked_ioctl =
1716 lttng_stream_ring_buffer_ioctl;
1717 lttng_stream_ring_buffer_file_operations.llseek =
1718 lib_ring_buffer_file_operations.llseek;
1719#ifdef CONFIG_COMPAT
1720 lttng_stream_ring_buffer_file_operations.compat_ioctl =
1721 lttng_stream_ring_buffer_compat_ioctl;
1722#endif
1723}
1724
80996790 1725int __init lttng_abi_init(void)
baf20995
MD
1726{
1727 int ret = 0;
1728
6d2a620c 1729 wrapper_vmalloc_sync_all();
2754583e 1730 lttng_clock_ref();
aa5d4c0d
JD
1731
1732 ret = lttng_tp_mempool_init();
1733 if (ret) {
1734 goto error;
1735 }
1736
d29348f7 1737 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
e6a17f26 1738 &lttng_fops, NULL);
2470a237 1739
255e52a4 1740 if (!lttng_proc_dentry) {
baf20995
MD
1741 printk(KERN_ERR "Error creating LTTng control file\n");
1742 ret = -ENOMEM;
1743 goto error;
1744 }
ed8d02d6 1745 lttng_stream_override_ring_buffer_fops();
2754583e 1746 return 0;
ed8d02d6 1747
baf20995 1748error:
aa5d4c0d 1749 lttng_tp_mempool_destroy();
2754583e 1750 lttng_clock_unref();
baf20995
MD
1751 return ret;
1752}
1753
e6e65fcd
MD
1754/* No __exit annotation because used by init error path too. */
1755void lttng_abi_exit(void)
baf20995 1756{
aa5d4c0d 1757 lttng_tp_mempool_destroy();
2754583e 1758 lttng_clock_unref();
e6a17f26
MD
1759 if (lttng_proc_dentry)
1760 remove_proc_entry("lttng", NULL);
baf20995 1761}
This page took 0.124447 seconds and 4 git commands to generate.