Version 2.1.3
[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>
b13f3ebe 46#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
f3bc08c5 47#include "wrapper/ringbuffer/vfs.h"
24cedcfe 48#include "wrapper/poll.h"
e8951e63 49#include "lttng-abi.h"
6dccd6c1 50#include "lttng-abi-old.h"
a90917c3
MD
51#include "lttng-events.h"
52#include "lttng-tracer.h"
baf20995
MD
53
54/*
55 * This is LTTng's own personal way to create a system call as an external
80996790 56 * module. We use ioctl() on /proc/lttng.
baf20995
MD
57 */
58
e6a17f26 59static struct proc_dir_entry *lttng_proc_dentry;
ad1c05e1
MD
60static const struct file_operations lttng_fops;
61static const struct file_operations lttng_session_fops;
62static const struct file_operations lttng_channel_fops;
5dbbdb43 63static const struct file_operations lttng_metadata_fops;
305d3e42 64static const struct file_operations lttng_event_fops;
baf20995 65
a33c9927
MD
66/*
67 * Teardown management: opened file descriptors keep a refcount on the module,
68 * so it can only exit when all file descriptors are closed.
69 */
70
5dbbdb43
MD
71enum channel_type {
72 PER_CPU_CHANNEL,
5dbbdb43
MD
73 METADATA_CHANNEL,
74};
75
ad1c05e1 76static
baf20995
MD
77int lttng_abi_create_session(void)
78{
a90917c3 79 struct lttng_session *session;
c0e31d2e 80 struct file *session_file;
11b5a3c2 81 int session_fd, ret;
baf20995 82
a90917c3 83 session = lttng_session_create();
baf20995
MD
84 if (!session)
85 return -ENOMEM;
1c25284c 86 session_fd = get_unused_fd();
baf20995
MD
87 if (session_fd < 0) {
88 ret = session_fd;
89 goto fd_error;
90 }
c0e31d2e 91 session_file = anon_inode_getfile("[lttng_session]",
ad1c05e1 92 &lttng_session_fops,
baf20995 93 session, O_RDWR);
c0e31d2e
MD
94 if (IS_ERR(session_file)) {
95 ret = PTR_ERR(session_file);
baf20995
MD
96 goto file_error;
97 }
c0e31d2e
MD
98 session->file = session_file;
99 fd_install(session_fd, session_file);
baf20995
MD
100 return session_fd;
101
102file_error:
103 put_unused_fd(session_fd);
104fd_error:
a90917c3 105 lttng_session_destroy(session);
baf20995
MD
106 return ret;
107}
108
271b6681
MD
109static
110int lttng_abi_tracepoint_list(void)
111{
112 struct file *tracepoint_list_file;
113 int file_fd, ret;
114
115 file_fd = get_unused_fd();
116 if (file_fd < 0) {
117 ret = file_fd;
118 goto fd_error;
119 }
30f18bf0 120
271b6681
MD
121 tracepoint_list_file = anon_inode_getfile("[lttng_session]",
122 &lttng_tracepoint_list_fops,
123 NULL, O_RDWR);
124 if (IS_ERR(tracepoint_list_file)) {
125 ret = PTR_ERR(tracepoint_list_file);
126 goto file_error;
127 }
30f18bf0
MD
128 ret = lttng_tracepoint_list_fops.open(NULL, tracepoint_list_file);
129 if (ret < 0)
130 goto open_error;
271b6681 131 fd_install(file_fd, tracepoint_list_file);
30f18bf0
MD
132 if (file_fd < 0) {
133 ret = file_fd;
134 goto fd_error;
135 }
271b6681
MD
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
80c16bcf 146static
6dccd6c1 147void lttng_abi_tracer_version(struct lttng_kernel_tracer_version *v)
80c16bcf 148{
6dccd6c1
JD
149 v->major = LTTNG_MODULES_MAJOR_VERSION;
150 v->minor = LTTNG_MODULES_MINOR_VERSION;
151 v->patchlevel = LTTNG_MODULES_PATCHLEVEL_VERSION;
80c16bcf
MD
152}
153
8070f5c0
MD
154static
155long lttng_abi_add_context(struct file *file,
6dccd6c1 156 struct lttng_kernel_context *context_param,
a90917c3 157 struct lttng_ctx **ctx, struct lttng_session *session)
8070f5c0 158{
8070f5c0
MD
159
160 if (session->been_active)
161 return -EPERM;
162
6dccd6c1 163 switch (context_param->ctx) {
12a313a5 164 case LTTNG_KERNEL_CONTEXT_PID:
8070f5c0 165 return lttng_add_pid_to_ctx(ctx);
a8ad3613
MD
166 case LTTNG_KERNEL_CONTEXT_PRIO:
167 return lttng_add_prio_to_ctx(ctx);
53f1f0ca
MD
168 case LTTNG_KERNEL_CONTEXT_NICE:
169 return lttng_add_nice_to_ctx(ctx);
b64bc438
MD
170 case LTTNG_KERNEL_CONTEXT_VPID:
171 return lttng_add_vpid_to_ctx(ctx);
172 case LTTNG_KERNEL_CONTEXT_TID:
173 return lttng_add_tid_to_ctx(ctx);
174 case LTTNG_KERNEL_CONTEXT_VTID:
175 return lttng_add_vtid_to_ctx(ctx);
176 case LTTNG_KERNEL_CONTEXT_PPID:
177 return lttng_add_ppid_to_ctx(ctx);
178 case LTTNG_KERNEL_CONTEXT_VPPID:
179 return lttng_add_vppid_to_ctx(ctx);
12a313a5 180 case LTTNG_KERNEL_CONTEXT_PERF_COUNTER:
6dccd6c1
JD
181 context_param->u.perf_counter.name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
182 return lttng_add_perf_counter_to_ctx(context_param->u.perf_counter.type,
183 context_param->u.perf_counter.config,
184 context_param->u.perf_counter.name,
c24a0d71 185 ctx);
a2563e83
MD
186 case LTTNG_KERNEL_CONTEXT_PROCNAME:
187 return lttng_add_procname_to_ctx(ctx);
975da2c0
JD
188 case LTTNG_KERNEL_CONTEXT_HOSTNAME:
189 return lttng_add_hostname_to_ctx(ctx);
8070f5c0
MD
190 default:
191 return -EINVAL;
192 }
193}
194
ad1c05e1
MD
195/**
196 * lttng_ioctl - lttng syscall through ioctl
197 *
c0e31d2e 198 * @file: the file
ad1c05e1
MD
199 * @cmd: the command
200 * @arg: command arg
201 *
202 * This ioctl implements lttng commands:
38d024ae 203 * LTTNG_KERNEL_SESSION
ad1c05e1 204 * Returns a LTTng trace session file descriptor
271b6681
MD
205 * LTTNG_KERNEL_TRACER_VERSION
206 * Returns the LTTng kernel tracer version
207 * LTTNG_KERNEL_TRACEPOINT_LIST
208 * Returns a file descriptor listing available tracepoints
360f38ea
MD
209 * LTTNG_KERNEL_WAIT_QUIESCENT
210 * Returns after all previously running probes have completed
ad1c05e1
MD
211 *
212 * The returned session will be deleted when its file descriptor is closed.
213 */
214static
c0e31d2e 215long lttng_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1
MD
216{
217 switch (cmd) {
6dccd6c1 218 case LTTNG_KERNEL_OLD_SESSION:
38d024ae 219 case LTTNG_KERNEL_SESSION:
ad1c05e1 220 return lttng_abi_create_session();
6dccd6c1
JD
221 case LTTNG_KERNEL_OLD_TRACER_VERSION:
222 {
223 struct lttng_kernel_tracer_version v;
224 struct lttng_kernel_old_tracer_version oldv;
225 struct lttng_kernel_old_tracer_version *uversion =
226 (struct lttng_kernel_old_tracer_version __user *) arg;
227
228 lttng_abi_tracer_version(&v);
229 oldv.major = v.major;
230 oldv.minor = v.minor;
231 oldv.patchlevel = v.patchlevel;
232
233 if (copy_to_user(uversion, &oldv, sizeof(oldv)))
234 return -EFAULT;
235 return 0;
236 }
80c16bcf 237 case LTTNG_KERNEL_TRACER_VERSION:
6dccd6c1
JD
238 {
239 struct lttng_kernel_tracer_version version;
240 struct lttng_kernel_tracer_version *uversion =
241 (struct lttng_kernel_tracer_version __user *) arg;
242
243 lttng_abi_tracer_version(&version);
244
245 if (copy_to_user(uversion, &version, sizeof(version)))
246 return -EFAULT;
247 return 0;
248 }
249 case LTTNG_KERNEL_OLD_TRACEPOINT_LIST:
271b6681
MD
250 case LTTNG_KERNEL_TRACEPOINT_LIST:
251 return lttng_abi_tracepoint_list();
6dccd6c1 252 case LTTNG_KERNEL_OLD_WAIT_QUIESCENT:
5f7f9078
MD
253 case LTTNG_KERNEL_WAIT_QUIESCENT:
254 synchronize_trace();
255 return 0;
6dccd6c1
JD
256 case LTTNG_KERNEL_OLD_CALIBRATE:
257 {
258 struct lttng_kernel_old_calibrate __user *ucalibrate =
259 (struct lttng_kernel_old_calibrate __user *) arg;
260 struct lttng_kernel_old_calibrate old_calibrate;
261 struct lttng_kernel_calibrate calibrate;
262 int ret;
263
264 if (copy_from_user(&old_calibrate, ucalibrate, sizeof(old_calibrate)))
265 return -EFAULT;
266 calibrate.type = old_calibrate.type;
267 ret = lttng_calibrate(&calibrate);
268 if (copy_to_user(ucalibrate, &old_calibrate, sizeof(old_calibrate)))
269 return -EFAULT;
270 return ret;
271 }
57105fc2
MD
272 case LTTNG_KERNEL_CALIBRATE:
273 {
3db41b2c
MD
274 struct lttng_kernel_calibrate __user *ucalibrate =
275 (struct lttng_kernel_calibrate __user *) arg;
276 struct lttng_kernel_calibrate calibrate;
57105fc2
MD
277 int ret;
278
279 if (copy_from_user(&calibrate, ucalibrate, sizeof(calibrate)))
280 return -EFAULT;
281 ret = lttng_calibrate(&calibrate);
282 if (copy_to_user(ucalibrate, &calibrate, sizeof(calibrate)))
283 return -EFAULT;
284 return ret;
285 }
ad1c05e1
MD
286 default:
287 return -ENOIOCTLCMD;
288 }
289}
290
ad1c05e1 291static const struct file_operations lttng_fops = {
a33c9927 292 .owner = THIS_MODULE,
ad1c05e1
MD
293 .unlocked_ioctl = lttng_ioctl,
294#ifdef CONFIG_COMPAT
03037b98 295 .compat_ioctl = lttng_ioctl,
ad1c05e1 296#endif
11b5a3c2 297};
ad1c05e1 298
5dbbdb43
MD
299/*
300 * We tolerate no failure in this function (if one happens, we print a dmesg
301 * error, but cannot return any error, because the channel information is
302 * invariant.
303 */
304static
305void lttng_metadata_create_events(struct file *channel_file)
306{
a90917c3 307 struct lttng_channel *channel = channel_file->private_data;
e70a4758 308 static struct lttng_kernel_event metadata_params = {
ab2277d6 309 .instrumentation = LTTNG_KERNEL_TRACEPOINT,
e70a4758
MD
310 .name = "lttng_metadata",
311 };
a90917c3 312 struct lttng_event *event;
5dbbdb43 313
5dbbdb43
MD
314 /*
315 * We tolerate no failure path after event creation. It will stay
316 * invariant for the rest of the session.
317 */
a90917c3 318 event = lttng_event_create(channel, &metadata_params, NULL, NULL);
5dbbdb43 319 if (!event) {
271b6681 320 goto create_error;
5dbbdb43
MD
321 }
322 return;
323
85a9ca7f 324create_error:
5dbbdb43
MD
325 WARN_ON(1);
326 return; /* not allowed to return error */
327}
328
329static
c0e31d2e 330int lttng_abi_create_channel(struct file *session_file,
6dccd6c1 331 struct lttng_kernel_channel *chan_param,
5dbbdb43 332 enum channel_type channel_type)
baf20995 333{
a90917c3 334 struct lttng_session *session = session_file->private_data;
88dfd899 335 const struct file_operations *fops = NULL;
5dbbdb43 336 const char *transport_name;
a90917c3 337 struct lttng_channel *chan;
c0e31d2e 338 struct file *chan_file;
baf20995 339 int chan_fd;
ad1c05e1 340 int ret = 0;
baf20995 341
1c25284c 342 chan_fd = get_unused_fd();
baf20995
MD
343 if (chan_fd < 0) {
344 ret = chan_fd;
345 goto fd_error;
346 }
88dfd899
MD
347 switch (channel_type) {
348 case PER_CPU_CHANNEL:
349 fops = &lttng_channel_fops;
350 break;
351 case METADATA_CHANNEL:
352 fops = &lttng_metadata_fops;
353 break;
354 }
355
c0e31d2e 356 chan_file = anon_inode_getfile("[lttng_channel]",
88dfd899 357 fops,
03037b98 358 NULL, O_RDWR);
c0e31d2e
MD
359 if (IS_ERR(chan_file)) {
360 ret = PTR_ERR(chan_file);
baf20995
MD
361 goto file_error;
362 }
5dbbdb43
MD
363 switch (channel_type) {
364 case PER_CPU_CHANNEL:
6dccd6c1
JD
365 if (chan_param->output == LTTNG_KERNEL_SPLICE) {
366 transport_name = chan_param->overwrite ?
96ba7208 367 "relay-overwrite" : "relay-discard";
6dccd6c1
JD
368 } else if (chan_param->output == LTTNG_KERNEL_MMAP) {
369 transport_name = chan_param->overwrite ?
96ba7208
JD
370 "relay-overwrite-mmap" : "relay-discard-mmap";
371 } else {
372 return -EINVAL;
373 }
5dbbdb43 374 break;
5dbbdb43 375 case METADATA_CHANNEL:
6dccd6c1 376 if (chan_param->output == LTTNG_KERNEL_SPLICE)
96ba7208 377 transport_name = "relay-metadata";
6dccd6c1 378 else if (chan_param->output == LTTNG_KERNEL_MMAP)
96ba7208
JD
379 transport_name = "relay-metadata-mmap";
380 else
381 return -EINVAL;
5dbbdb43
MD
382 break;
383 default:
384 transport_name = "<unknown>";
385 break;
386 }
03037b98
MD
387 /*
388 * We tolerate no failure path after channel creation. It will stay
389 * invariant for the rest of the session.
390 */
a90917c3 391 chan = lttng_channel_create(session, transport_name, NULL,
6dccd6c1
JD
392 chan_param->subbuf_size,
393 chan_param->num_subbuf,
394 chan_param->switch_timer_interval,
395 chan_param->read_timer_interval);
03037b98 396 if (!chan) {
f3d01b96 397 ret = -EINVAL;
03037b98
MD
398 goto chan_error;
399 }
11b5a3c2 400 chan->file = chan_file;
c0e31d2e
MD
401 chan_file->private_data = chan;
402 fd_install(chan_fd, chan_file);
cd4bd11f 403 if (channel_type == METADATA_CHANNEL) {
cd4bd11f 404 session->metadata = chan;
4f1c3952 405 lttng_metadata_create_events(chan_file);
cd4bd11f 406 }
5dbbdb43 407
ad1c05e1 408 /* The channel created holds a reference on the session */
b0caa15a 409 atomic_long_inc(&session_file->f_count);
ad1c05e1 410
baf20995
MD
411 return chan_fd;
412
03037b98 413chan_error:
c0e31d2e 414 fput(chan_file);
baf20995
MD
415file_error:
416 put_unused_fd(chan_fd);
417fd_error:
baf20995
MD
418 return ret;
419}
420
421/**
ad1c05e1 422 * lttng_session_ioctl - lttng session fd ioctl
baf20995 423 *
c0e31d2e 424 * @file: the file
baf20995
MD
425 * @cmd: the command
426 * @arg: command arg
427 *
428 * This ioctl implements lttng commands:
38d024ae 429 * LTTNG_KERNEL_CHANNEL
baf20995 430 * Returns a LTTng channel file descriptor
e64957da
MD
431 * LTTNG_KERNEL_ENABLE
432 * Enables tracing for a session (weak enable)
433 * LTTNG_KERNEL_DISABLE
434 * Disables tracing for a session (strong disable)
8070f5c0
MD
435 * LTTNG_KERNEL_METADATA
436 * Returns a LTTng metadata file descriptor
ad1c05e1
MD
437 *
438 * The returned channel will be deleted when its file descriptor is closed.
439 */
440static
c0e31d2e 441long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 442{
a90917c3 443 struct lttng_session *session = file->private_data;
c0e31d2e 444
ad1c05e1 445 switch (cmd) {
6dccd6c1
JD
446 case LTTNG_KERNEL_OLD_CHANNEL:
447 {
448 struct lttng_kernel_channel chan_param;
449 struct lttng_kernel_old_channel old_chan_param;
450
451 if (copy_from_user(&old_chan_param,
452 (struct lttng_kernel_old_channel __user *) arg,
453 sizeof(struct lttng_kernel_old_channel)))
454 return -EFAULT;
455 chan_param.overwrite = old_chan_param.overwrite;
456 chan_param.subbuf_size = old_chan_param.subbuf_size;
457 chan_param.num_subbuf = old_chan_param.num_subbuf;
458 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
459 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
460 chan_param.output = old_chan_param.output;
461
462 return lttng_abi_create_channel(file, &chan_param,
463 PER_CPU_CHANNEL);
464 }
38d024ae 465 case LTTNG_KERNEL_CHANNEL:
6dccd6c1
JD
466 {
467 struct lttng_kernel_channel chan_param;
468
469 if (copy_from_user(&chan_param,
38d024ae 470 (struct lttng_kernel_channel __user *) arg,
6dccd6c1
JD
471 sizeof(struct lttng_kernel_channel)))
472 return -EFAULT;
473 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 474 PER_CPU_CHANNEL);
6dccd6c1
JD
475 }
476 case LTTNG_KERNEL_OLD_SESSION_START:
477 case LTTNG_KERNEL_OLD_ENABLE:
38d024ae 478 case LTTNG_KERNEL_SESSION_START:
e64957da 479 case LTTNG_KERNEL_ENABLE:
a90917c3 480 return lttng_session_enable(session);
6dccd6c1
JD
481 case LTTNG_KERNEL_OLD_SESSION_STOP:
482 case LTTNG_KERNEL_OLD_DISABLE:
38d024ae 483 case LTTNG_KERNEL_SESSION_STOP:
e64957da 484 case LTTNG_KERNEL_DISABLE:
a90917c3 485 return lttng_session_disable(session);
6dccd6c1
JD
486 case LTTNG_KERNEL_OLD_METADATA:
487 {
488 struct lttng_kernel_channel chan_param;
489 struct lttng_kernel_old_channel old_chan_param;
490
491 if (copy_from_user(&old_chan_param,
492 (struct lttng_kernel_old_channel __user *) arg,
493 sizeof(struct lttng_kernel_old_channel)))
494 return -EFAULT;
495 chan_param.overwrite = old_chan_param.overwrite;
496 chan_param.subbuf_size = old_chan_param.subbuf_size;
497 chan_param.num_subbuf = old_chan_param.num_subbuf;
498 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
499 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
500 chan_param.output = old_chan_param.output;
501
502 return lttng_abi_create_channel(file, &chan_param,
503 METADATA_CHANNEL);
504 }
38d024ae 505 case LTTNG_KERNEL_METADATA:
6dccd6c1
JD
506 {
507 struct lttng_kernel_channel chan_param;
508
509 if (copy_from_user(&chan_param,
510 (struct lttng_kernel_channel __user *) arg,
511 sizeof(struct lttng_kernel_channel)))
512 return -EFAULT;
513 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 514 METADATA_CHANNEL);
6dccd6c1 515 }
ad1c05e1
MD
516 default:
517 return -ENOIOCTLCMD;
518 }
519}
520
03037b98
MD
521/*
522 * Called when the last file reference is dropped.
523 *
524 * Big fat note: channels and events are invariant for the whole session after
525 * their creation. So this session destruction also destroys all channel and
526 * event structures specific to this session (they are not destroyed when their
527 * individual file is released).
528 */
ad1c05e1 529static
03037b98 530int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 531{
a90917c3 532 struct lttng_session *session = file->private_data;
c269fff4
MD
533
534 if (session)
a90917c3 535 lttng_session_destroy(session);
11b5a3c2 536 return 0;
ad1c05e1 537}
ad1c05e1
MD
538
539static const struct file_operations lttng_session_fops = {
a33c9927 540 .owner = THIS_MODULE,
03037b98 541 .release = lttng_session_release,
ad1c05e1
MD
542 .unlocked_ioctl = lttng_session_ioctl,
543#ifdef CONFIG_COMPAT
03037b98 544 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 545#endif
11b5a3c2 546};
ad1c05e1
MD
547
548static
c0e31d2e 549int lttng_abi_open_stream(struct file *channel_file)
ad1c05e1 550{
a90917c3 551 struct lttng_channel *channel = channel_file->private_data;
ad1c05e1
MD
552 struct lib_ring_buffer *buf;
553 int stream_fd, ret;
11b5a3c2 554 struct file *stream_file;
ad1c05e1 555
11b5a3c2 556 buf = channel->ops->buffer_read_open(channel->chan);
ad1c05e1
MD
557 if (!buf)
558 return -ENOENT;
559
1c25284c 560 stream_fd = get_unused_fd();
ad1c05e1
MD
561 if (stream_fd < 0) {
562 ret = stream_fd;
563 goto fd_error;
564 }
c0e31d2e 565 stream_file = anon_inode_getfile("[lttng_stream]",
7f57c73c 566 &lib_ring_buffer_file_operations,
ad1c05e1 567 buf, O_RDWR);
c0e31d2e
MD
568 if (IS_ERR(stream_file)) {
569 ret = PTR_ERR(stream_file);
ad1c05e1
MD
570 goto file_error;
571 }
409453cb
MD
572 /*
573 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
574 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
575 * file descriptor, so we set FMODE_PREAD here.
576 */
d7b6f197 577 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 578 fd_install(stream_fd, stream_file);
dda6a249
MD
579 /*
580 * The stream holds a reference to the channel within the generic ring
581 * buffer library, so no need to hold a refcount on the channel and
582 * session files here.
583 */
ad1c05e1
MD
584 return stream_fd;
585
586file_error:
587 put_unused_fd(stream_fd);
588fd_error:
11b5a3c2 589 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
590 return ret;
591}
592
653fe716 593static
c0e31d2e 594int lttng_abi_create_event(struct file *channel_file,
6dccd6c1 595 struct lttng_kernel_event *event_param)
653fe716 596{
a90917c3
MD
597 struct lttng_channel *channel = channel_file->private_data;
598 struct lttng_event *event;
653fe716 599 int event_fd, ret;
11b5a3c2 600 struct file *event_file;
653fe716 601
6dccd6c1
JD
602 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
603 switch (event_param->instrumentation) {
7371f44c 604 case LTTNG_KERNEL_KRETPROBE:
6dccd6c1 605 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
7371f44c 606 break;
ab2277d6 607 case LTTNG_KERNEL_KPROBE:
6dccd6c1 608 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4 609 break;
ab2277d6 610 case LTTNG_KERNEL_FUNCTION:
6dccd6c1 611 event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4
MD
612 break;
613 default:
614 break;
615 }
6dccd6c1 616 switch (event_param->instrumentation) {
1ec65de1
MD
617 default:
618 event_fd = get_unused_fd();
619 if (event_fd < 0) {
620 ret = event_fd;
621 goto fd_error;
622 }
623 event_file = anon_inode_getfile("[lttng_event]",
624 &lttng_event_fops,
625 NULL, O_RDWR);
626 if (IS_ERR(event_file)) {
627 ret = PTR_ERR(event_file);
628 goto file_error;
629 }
630 /*
631 * We tolerate no failure path after event creation. It
632 * will stay invariant for the rest of the session.
633 */
6dccd6c1 634 event = lttng_event_create(channel, event_param, NULL, NULL);
1ec65de1
MD
635 if (!event) {
636 ret = -EINVAL;
637 goto event_error;
638 }
639 event_file->private_data = event;
640 fd_install(event_fd, event_file);
641 /* The event holds a reference on the channel */
642 atomic_long_inc(&channel_file->f_count);
643 break;
43880ee8
MD
644 case LTTNG_KERNEL_SYSCALL:
645 /*
646 * Only all-syscall tracing supported for now.
647 */
6dccd6c1 648 if (event_param->name[0] != '\0')
43880ee8 649 return -EINVAL;
1ec65de1
MD
650 ret = lttng_syscalls_register(channel, NULL);
651 if (ret)
652 goto fd_error;
653 event_fd = 0;
654 break;
03037b98 655 }
653fe716
MD
656 return event_fd;
657
03037b98 658event_error:
c0e31d2e 659 fput(event_file);
653fe716
MD
660file_error:
661 put_unused_fd(event_fd);
662fd_error:
653fe716
MD
663 return ret;
664}
ad1c05e1
MD
665
666/**
667 * lttng_channel_ioctl - lttng syscall through ioctl
668 *
c0e31d2e 669 * @file: the file
ad1c05e1
MD
670 * @cmd: the command
671 * @arg: command arg
672 *
673 * This ioctl implements lttng commands:
38d024ae 674 * LTTNG_KERNEL_STREAM
ad1c05e1
MD
675 * Returns an event stream file descriptor or failure.
676 * (typically, one event stream records events from one CPU)
38d024ae 677 * LTTNG_KERNEL_EVENT
ad1c05e1 678 * Returns an event file descriptor or failure.
8070f5c0
MD
679 * LTTNG_KERNEL_CONTEXT
680 * Prepend a context field to each event in the channel
e64957da
MD
681 * LTTNG_KERNEL_ENABLE
682 * Enable recording for events in this channel (weak enable)
683 * LTTNG_KERNEL_DISABLE
684 * Disable recording for events in this channel (strong disable)
baf20995 685 *
baf20995
MD
686 * Channel and event file descriptors also hold a reference on the session.
687 */
ad1c05e1 688static
c0e31d2e 689long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 690{
a90917c3 691 struct lttng_channel *channel = file->private_data;
8070f5c0 692
baf20995 693 switch (cmd) {
6dccd6c1 694 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 695 case LTTNG_KERNEL_STREAM:
c0e31d2e 696 return lttng_abi_open_stream(file);
6dccd6c1
JD
697 case LTTNG_KERNEL_OLD_EVENT:
698 {
699 struct lttng_kernel_event *uevent_param;
700 struct lttng_kernel_old_event *old_uevent_param;
701 int ret;
702
703 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
704 GFP_KERNEL);
705 if (!uevent_param) {
706 ret = -ENOMEM;
707 goto old_event_end;
708 }
709 old_uevent_param = kmalloc(
710 sizeof(struct lttng_kernel_old_event),
711 GFP_KERNEL);
712 if (!old_uevent_param) {
713 ret = -ENOMEM;
714 goto old_event_error_free_param;
715 }
716 if (copy_from_user(old_uevent_param,
717 (struct lttng_kernel_old_event __user *) arg,
718 sizeof(struct lttng_kernel_old_event))) {
719 ret = -EFAULT;
720 goto old_event_error_free_old_param;
721 }
722
723 memcpy(uevent_param->name, old_uevent_param->name,
724 sizeof(uevent_param->name));
725 uevent_param->instrumentation =
726 old_uevent_param->instrumentation;
727
728 switch (old_uevent_param->instrumentation) {
729 case LTTNG_KERNEL_KPROBE:
730 uevent_param->u.kprobe.addr =
731 old_uevent_param->u.kprobe.addr;
732 uevent_param->u.kprobe.offset =
733 old_uevent_param->u.kprobe.offset;
734 memcpy(uevent_param->u.kprobe.symbol_name,
735 old_uevent_param->u.kprobe.symbol_name,
736 sizeof(uevent_param->u.kprobe.symbol_name));
737 break;
738 case LTTNG_KERNEL_KRETPROBE:
739 uevent_param->u.kretprobe.addr =
740 old_uevent_param->u.kretprobe.addr;
741 uevent_param->u.kretprobe.offset =
742 old_uevent_param->u.kretprobe.offset;
743 memcpy(uevent_param->u.kretprobe.symbol_name,
744 old_uevent_param->u.kretprobe.symbol_name,
745 sizeof(uevent_param->u.kretprobe.symbol_name));
746 break;
747 case LTTNG_KERNEL_FUNCTION:
748 memcpy(uevent_param->u.ftrace.symbol_name,
749 old_uevent_param->u.ftrace.symbol_name,
750 sizeof(uevent_param->u.ftrace.symbol_name));
751 break;
752 default:
753 break;
754 }
755 ret = lttng_abi_create_event(file, uevent_param);
756
757old_event_error_free_old_param:
758 kfree(old_uevent_param);
759old_event_error_free_param:
760 kfree(uevent_param);
761old_event_end:
762 return ret;
763 }
38d024ae 764 case LTTNG_KERNEL_EVENT:
6dccd6c1
JD
765 {
766 struct lttng_kernel_event uevent_param;
767
768 if (copy_from_user(&uevent_param,
769 (struct lttng_kernel_event __user *) arg,
770 sizeof(uevent_param)))
771 return -EFAULT;
772 return lttng_abi_create_event(file, &uevent_param);
773 }
774 case LTTNG_KERNEL_OLD_CONTEXT:
775 {
776 struct lttng_kernel_context *ucontext_param;
777 struct lttng_kernel_old_context *old_ucontext_param;
778 int ret;
779
780 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
781 GFP_KERNEL);
782 if (!ucontext_param) {
783 ret = -ENOMEM;
784 goto old_ctx_end;
785 }
786 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
787 GFP_KERNEL);
788 if (!old_ucontext_param) {
789 ret = -ENOMEM;
790 goto old_ctx_error_free_param;
791 }
792
793 if (copy_from_user(old_ucontext_param,
794 (struct lttng_kernel_old_context __user *) arg,
795 sizeof(struct lttng_kernel_old_context))) {
796 ret = -EFAULT;
797 goto old_ctx_error_free_old_param;
798 }
799 ucontext_param->ctx = old_ucontext_param->ctx;
800 memcpy(ucontext_param->padding, old_ucontext_param->padding,
801 sizeof(ucontext_param->padding));
802 /* only type that uses the union */
803 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
804 ucontext_param->u.perf_counter.type =
805 old_ucontext_param->u.perf_counter.type;
806 ucontext_param->u.perf_counter.config =
807 old_ucontext_param->u.perf_counter.config;
808 memcpy(ucontext_param->u.perf_counter.name,
809 old_ucontext_param->u.perf_counter.name,
810 sizeof(ucontext_param->u.perf_counter.name));
811 }
812
813 ret = lttng_abi_add_context(file,
814 ucontext_param,
815 &channel->ctx, channel->session);
816
817old_ctx_error_free_old_param:
818 kfree(old_ucontext_param);
819old_ctx_error_free_param:
820 kfree(ucontext_param);
821old_ctx_end:
822 return ret;
823 }
8070f5c0 824 case LTTNG_KERNEL_CONTEXT:
6dccd6c1
JD
825 {
826 struct lttng_kernel_context ucontext_param;
827
828 if (copy_from_user(&ucontext_param,
8070f5c0 829 (struct lttng_kernel_context __user *) arg,
6dccd6c1
JD
830 sizeof(ucontext_param)))
831 return -EFAULT;
832 return lttng_abi_add_context(file,
833 &ucontext_param,
8070f5c0 834 &channel->ctx, channel->session);
6dccd6c1
JD
835 }
836 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 837 case LTTNG_KERNEL_ENABLE:
a90917c3 838 return lttng_channel_enable(channel);
6dccd6c1 839 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 840 case LTTNG_KERNEL_DISABLE:
a90917c3 841 return lttng_channel_disable(channel);
baf20995
MD
842 default:
843 return -ENOIOCTLCMD;
844 }
6dccd6c1 845
baf20995
MD
846}
847
5dbbdb43
MD
848/**
849 * lttng_metadata_ioctl - lttng syscall through ioctl
850 *
851 * @file: the file
852 * @cmd: the command
853 * @arg: command arg
854 *
855 * This ioctl implements lttng commands:
38d024ae 856 * LTTNG_KERNEL_STREAM
5dbbdb43
MD
857 * Returns an event stream file descriptor or failure.
858 *
859 * Channel and event file descriptors also hold a reference on the session.
860 */
861static
862long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
863{
864 switch (cmd) {
6dccd6c1 865 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 866 case LTTNG_KERNEL_STREAM:
5dbbdb43
MD
867 return lttng_abi_open_stream(file);
868 default:
869 return -ENOIOCTLCMD;
870 }
871}
872
653fe716
MD
873/**
874 * lttng_channel_poll - lttng stream addition/removal monitoring
875 *
c0e31d2e 876 * @file: the file
653fe716
MD
877 * @wait: poll table
878 */
c0e31d2e 879unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 880{
a90917c3 881 struct lttng_channel *channel = file->private_data;
653fe716
MD
882 unsigned int mask = 0;
883
c0e31d2e 884 if (file->f_mode & FMODE_READ) {
a33e44a6 885 poll_wait_set_exclusive(wait);
24cedcfe
MD
886 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
887 wait);
653fe716 888
254ec7bc
MD
889 if (channel->ops->is_disabled(channel->chan))
890 return POLLERR;
24cedcfe 891 if (channel->ops->is_finalized(channel->chan))
653fe716 892 return POLLHUP;
f71ecafa 893 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
653fe716 894 return POLLIN | POLLRDNORM;
f71ecafa 895 return 0;
653fe716
MD
896 }
897 return mask;
898
899}
900
0a84a57f
MD
901static
902int lttng_channel_release(struct inode *inode, struct file *file)
903{
a90917c3 904 struct lttng_channel *channel = file->private_data;
c269fff4
MD
905
906 if (channel)
907 fput(channel->session->file);
0a84a57f
MD
908 return 0;
909}
910
ad1c05e1 911static const struct file_operations lttng_channel_fops = {
a33c9927 912 .owner = THIS_MODULE,
03037b98 913 .release = lttng_channel_release,
653fe716 914 .poll = lttng_channel_poll,
ad1c05e1 915 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 916#ifdef CONFIG_COMPAT
03037b98 917 .compat_ioctl = lttng_channel_ioctl,
baf20995 918#endif
11b5a3c2 919};
baf20995 920
5dbbdb43 921static const struct file_operations lttng_metadata_fops = {
a33c9927 922 .owner = THIS_MODULE,
5dbbdb43
MD
923 .release = lttng_channel_release,
924 .unlocked_ioctl = lttng_metadata_ioctl,
925#ifdef CONFIG_COMPAT
926 .compat_ioctl = lttng_metadata_ioctl,
927#endif
928};
929
8070f5c0
MD
930/**
931 * lttng_event_ioctl - lttng syscall through ioctl
932 *
933 * @file: the file
934 * @cmd: the command
935 * @arg: command arg
936 *
937 * This ioctl implements lttng commands:
8070f5c0
MD
938 * LTTNG_KERNEL_CONTEXT
939 * Prepend a context field to each record of this event
e64957da
MD
940 * LTTNG_KERNEL_ENABLE
941 * Enable recording for this event (weak enable)
942 * LTTNG_KERNEL_DISABLE
943 * Disable recording for this event (strong disable)
8070f5c0
MD
944 */
945static
946long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
947{
a90917c3 948 struct lttng_event *event = file->private_data;
8070f5c0
MD
949
950 switch (cmd) {
6dccd6c1
JD
951 case LTTNG_KERNEL_OLD_CONTEXT:
952 {
953 struct lttng_kernel_context *ucontext_param;
954 struct lttng_kernel_old_context *old_ucontext_param;
955 int ret;
956
957 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
958 GFP_KERNEL);
959 if (!ucontext_param) {
960 ret = -ENOMEM;
961 goto old_ctx_end;
962 }
963 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
964 GFP_KERNEL);
965 if (!old_ucontext_param) {
966 ret = -ENOMEM;
967 goto old_ctx_error_free_param;
968 }
969
970 if (copy_from_user(old_ucontext_param,
971 (struct lttng_kernel_old_context __user *) arg,
972 sizeof(struct lttng_kernel_old_context))) {
973 ret = -EFAULT;
974 goto old_ctx_error_free_old_param;
975 }
976 ucontext_param->ctx = old_ucontext_param->ctx;
977 memcpy(ucontext_param->padding, old_ucontext_param->padding,
978 sizeof(ucontext_param->padding));
979 /* only type that uses the union */
980 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
981 ucontext_param->u.perf_counter.type =
982 old_ucontext_param->u.perf_counter.type;
983 ucontext_param->u.perf_counter.config =
984 old_ucontext_param->u.perf_counter.config;
985 memcpy(ucontext_param->u.perf_counter.name,
986 old_ucontext_param->u.perf_counter.name,
987 sizeof(ucontext_param->u.perf_counter.name));
988 }
989
990 ret = lttng_abi_add_context(file,
991 ucontext_param,
992 &event->ctx, event->chan->session);
993
994old_ctx_error_free_old_param:
995 kfree(old_ucontext_param);
996old_ctx_error_free_param:
997 kfree(ucontext_param);
998old_ctx_end:
999 return ret;
1000 }
8070f5c0 1001 case LTTNG_KERNEL_CONTEXT:
6dccd6c1
JD
1002 {
1003 struct lttng_kernel_context ucontext_param;
1004
1005 if (copy_from_user(&ucontext_param,
1006 (struct lttng_kernel_context __user *) arg,
1007 sizeof(ucontext_param)))
1008 return -EFAULT;
8070f5c0 1009 return lttng_abi_add_context(file,
6dccd6c1 1010 &ucontext_param,
8070f5c0 1011 &event->ctx, event->chan->session);
6dccd6c1
JD
1012 }
1013 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 1014 case LTTNG_KERNEL_ENABLE:
a90917c3 1015 return lttng_event_enable(event);
6dccd6c1 1016 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 1017 case LTTNG_KERNEL_DISABLE:
a90917c3 1018 return lttng_event_disable(event);
8070f5c0
MD
1019 default:
1020 return -ENOIOCTLCMD;
1021 }
1022}
1023
0a84a57f
MD
1024static
1025int lttng_event_release(struct inode *inode, struct file *file)
1026{
a90917c3 1027 struct lttng_event *event = file->private_data;
c269fff4 1028
aa7c23a9 1029 if (event)
c269fff4 1030 fput(event->chan->file);
0a84a57f
MD
1031 return 0;
1032}
1033
3b923e5b 1034/* TODO: filter control ioctl */
0a84a57f 1035static const struct file_operations lttng_event_fops = {
a33c9927 1036 .owner = THIS_MODULE,
0a84a57f 1037 .release = lttng_event_release,
8070f5c0
MD
1038 .unlocked_ioctl = lttng_event_ioctl,
1039#ifdef CONFIG_COMPAT
1040 .compat_ioctl = lttng_event_ioctl,
1041#endif
11b5a3c2 1042};
0a84a57f 1043
80996790 1044int __init lttng_abi_init(void)
baf20995
MD
1045{
1046 int ret = 0;
1047
6d2a620c 1048 wrapper_vmalloc_sync_all();
d29348f7 1049 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
e6a17f26
MD
1050 &lttng_fops, NULL);
1051
255e52a4 1052 if (!lttng_proc_dentry) {
baf20995
MD
1053 printk(KERN_ERR "Error creating LTTng control file\n");
1054 ret = -ENOMEM;
1055 goto error;
1056 }
1057error:
1058 return ret;
1059}
1060
80996790 1061void __exit lttng_abi_exit(void)
baf20995 1062{
e6a17f26
MD
1063 if (lttng_proc_dentry)
1064 remove_proc_entry("lttng", NULL);
baf20995 1065}
This page took 0.078944 seconds and 4 git commands to generate.