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