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