Remove useless lttng_metadata probe
[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 299static
c0e31d2e 300int lttng_abi_create_channel(struct file *session_file,
6dccd6c1 301 struct lttng_kernel_channel *chan_param,
5dbbdb43 302 enum channel_type channel_type)
baf20995 303{
a90917c3 304 struct lttng_session *session = session_file->private_data;
88dfd899 305 const struct file_operations *fops = NULL;
5dbbdb43 306 const char *transport_name;
a90917c3 307 struct lttng_channel *chan;
c0e31d2e 308 struct file *chan_file;
baf20995 309 int chan_fd;
ad1c05e1 310 int ret = 0;
baf20995 311
1c25284c 312 chan_fd = get_unused_fd();
baf20995
MD
313 if (chan_fd < 0) {
314 ret = chan_fd;
315 goto fd_error;
316 }
88dfd899
MD
317 switch (channel_type) {
318 case PER_CPU_CHANNEL:
319 fops = &lttng_channel_fops;
320 break;
321 case METADATA_CHANNEL:
322 fops = &lttng_metadata_fops;
323 break;
324 }
325
c0e31d2e 326 chan_file = anon_inode_getfile("[lttng_channel]",
88dfd899 327 fops,
03037b98 328 NULL, O_RDWR);
c0e31d2e
MD
329 if (IS_ERR(chan_file)) {
330 ret = PTR_ERR(chan_file);
baf20995
MD
331 goto file_error;
332 }
5dbbdb43
MD
333 switch (channel_type) {
334 case PER_CPU_CHANNEL:
6dccd6c1
JD
335 if (chan_param->output == LTTNG_KERNEL_SPLICE) {
336 transport_name = chan_param->overwrite ?
96ba7208 337 "relay-overwrite" : "relay-discard";
6dccd6c1
JD
338 } else if (chan_param->output == LTTNG_KERNEL_MMAP) {
339 transport_name = chan_param->overwrite ?
96ba7208
JD
340 "relay-overwrite-mmap" : "relay-discard-mmap";
341 } else {
342 return -EINVAL;
343 }
5dbbdb43 344 break;
5dbbdb43 345 case METADATA_CHANNEL:
6dccd6c1 346 if (chan_param->output == LTTNG_KERNEL_SPLICE)
96ba7208 347 transport_name = "relay-metadata";
6dccd6c1 348 else if (chan_param->output == LTTNG_KERNEL_MMAP)
96ba7208
JD
349 transport_name = "relay-metadata-mmap";
350 else
351 return -EINVAL;
5dbbdb43
MD
352 break;
353 default:
354 transport_name = "<unknown>";
355 break;
356 }
03037b98
MD
357 /*
358 * We tolerate no failure path after channel creation. It will stay
359 * invariant for the rest of the session.
360 */
a90917c3 361 chan = lttng_channel_create(session, transport_name, NULL,
6dccd6c1
JD
362 chan_param->subbuf_size,
363 chan_param->num_subbuf,
364 chan_param->switch_timer_interval,
365 chan_param->read_timer_interval);
03037b98 366 if (!chan) {
f3d01b96 367 ret = -EINVAL;
03037b98
MD
368 goto chan_error;
369 }
11b5a3c2 370 chan->file = chan_file;
c0e31d2e
MD
371 chan_file->private_data = chan;
372 fd_install(chan_fd, chan_file);
cd4bd11f 373 if (channel_type == METADATA_CHANNEL) {
cd4bd11f
MD
374 session->metadata = chan;
375 }
5dbbdb43 376
ad1c05e1 377 /* The channel created holds a reference on the session */
b0caa15a 378 atomic_long_inc(&session_file->f_count);
ad1c05e1 379
baf20995
MD
380 return chan_fd;
381
03037b98 382chan_error:
c0e31d2e 383 fput(chan_file);
baf20995
MD
384file_error:
385 put_unused_fd(chan_fd);
386fd_error:
baf20995
MD
387 return ret;
388}
389
390/**
ad1c05e1 391 * lttng_session_ioctl - lttng session fd ioctl
baf20995 392 *
c0e31d2e 393 * @file: the file
baf20995
MD
394 * @cmd: the command
395 * @arg: command arg
396 *
397 * This ioctl implements lttng commands:
38d024ae 398 * LTTNG_KERNEL_CHANNEL
baf20995 399 * Returns a LTTng channel file descriptor
e64957da
MD
400 * LTTNG_KERNEL_ENABLE
401 * Enables tracing for a session (weak enable)
402 * LTTNG_KERNEL_DISABLE
403 * Disables tracing for a session (strong disable)
8070f5c0
MD
404 * LTTNG_KERNEL_METADATA
405 * Returns a LTTng metadata file descriptor
ad1c05e1
MD
406 *
407 * The returned channel will be deleted when its file descriptor is closed.
408 */
409static
c0e31d2e 410long lttng_session_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
ad1c05e1 411{
a90917c3 412 struct lttng_session *session = file->private_data;
c0e31d2e 413
ad1c05e1 414 switch (cmd) {
6dccd6c1
JD
415 case LTTNG_KERNEL_OLD_CHANNEL:
416 {
417 struct lttng_kernel_channel chan_param;
418 struct lttng_kernel_old_channel old_chan_param;
419
420 if (copy_from_user(&old_chan_param,
421 (struct lttng_kernel_old_channel __user *) arg,
422 sizeof(struct lttng_kernel_old_channel)))
423 return -EFAULT;
424 chan_param.overwrite = old_chan_param.overwrite;
425 chan_param.subbuf_size = old_chan_param.subbuf_size;
426 chan_param.num_subbuf = old_chan_param.num_subbuf;
427 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
428 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
429 chan_param.output = old_chan_param.output;
430
431 return lttng_abi_create_channel(file, &chan_param,
432 PER_CPU_CHANNEL);
433 }
38d024ae 434 case LTTNG_KERNEL_CHANNEL:
6dccd6c1
JD
435 {
436 struct lttng_kernel_channel chan_param;
437
438 if (copy_from_user(&chan_param,
38d024ae 439 (struct lttng_kernel_channel __user *) arg,
6dccd6c1
JD
440 sizeof(struct lttng_kernel_channel)))
441 return -EFAULT;
442 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 443 PER_CPU_CHANNEL);
6dccd6c1
JD
444 }
445 case LTTNG_KERNEL_OLD_SESSION_START:
446 case LTTNG_KERNEL_OLD_ENABLE:
38d024ae 447 case LTTNG_KERNEL_SESSION_START:
e64957da 448 case LTTNG_KERNEL_ENABLE:
a90917c3 449 return lttng_session_enable(session);
6dccd6c1
JD
450 case LTTNG_KERNEL_OLD_SESSION_STOP:
451 case LTTNG_KERNEL_OLD_DISABLE:
38d024ae 452 case LTTNG_KERNEL_SESSION_STOP:
e64957da 453 case LTTNG_KERNEL_DISABLE:
a90917c3 454 return lttng_session_disable(session);
6dccd6c1
JD
455 case LTTNG_KERNEL_OLD_METADATA:
456 {
457 struct lttng_kernel_channel chan_param;
458 struct lttng_kernel_old_channel old_chan_param;
459
460 if (copy_from_user(&old_chan_param,
461 (struct lttng_kernel_old_channel __user *) arg,
462 sizeof(struct lttng_kernel_old_channel)))
463 return -EFAULT;
464 chan_param.overwrite = old_chan_param.overwrite;
465 chan_param.subbuf_size = old_chan_param.subbuf_size;
466 chan_param.num_subbuf = old_chan_param.num_subbuf;
467 chan_param.switch_timer_interval = old_chan_param.switch_timer_interval;
468 chan_param.read_timer_interval = old_chan_param.read_timer_interval;
469 chan_param.output = old_chan_param.output;
470
471 return lttng_abi_create_channel(file, &chan_param,
472 METADATA_CHANNEL);
473 }
38d024ae 474 case LTTNG_KERNEL_METADATA:
6dccd6c1
JD
475 {
476 struct lttng_kernel_channel chan_param;
477
478 if (copy_from_user(&chan_param,
479 (struct lttng_kernel_channel __user *) arg,
480 sizeof(struct lttng_kernel_channel)))
481 return -EFAULT;
482 return lttng_abi_create_channel(file, &chan_param,
5dbbdb43 483 METADATA_CHANNEL);
6dccd6c1 484 }
ad1c05e1
MD
485 default:
486 return -ENOIOCTLCMD;
487 }
488}
489
03037b98
MD
490/*
491 * Called when the last file reference is dropped.
492 *
493 * Big fat note: channels and events are invariant for the whole session after
494 * their creation. So this session destruction also destroys all channel and
495 * event structures specific to this session (they are not destroyed when their
496 * individual file is released).
497 */
ad1c05e1 498static
03037b98 499int lttng_session_release(struct inode *inode, struct file *file)
ad1c05e1 500{
a90917c3 501 struct lttng_session *session = file->private_data;
c269fff4
MD
502
503 if (session)
a90917c3 504 lttng_session_destroy(session);
11b5a3c2 505 return 0;
ad1c05e1 506}
ad1c05e1
MD
507
508static const struct file_operations lttng_session_fops = {
a33c9927 509 .owner = THIS_MODULE,
03037b98 510 .release = lttng_session_release,
ad1c05e1
MD
511 .unlocked_ioctl = lttng_session_ioctl,
512#ifdef CONFIG_COMPAT
03037b98 513 .compat_ioctl = lttng_session_ioctl,
ad1c05e1 514#endif
11b5a3c2 515};
ad1c05e1
MD
516
517static
c0e31d2e 518int lttng_abi_open_stream(struct file *channel_file)
ad1c05e1 519{
a90917c3 520 struct lttng_channel *channel = channel_file->private_data;
ad1c05e1
MD
521 struct lib_ring_buffer *buf;
522 int stream_fd, ret;
11b5a3c2 523 struct file *stream_file;
ad1c05e1 524
11b5a3c2 525 buf = channel->ops->buffer_read_open(channel->chan);
ad1c05e1
MD
526 if (!buf)
527 return -ENOENT;
528
1c25284c 529 stream_fd = get_unused_fd();
ad1c05e1
MD
530 if (stream_fd < 0) {
531 ret = stream_fd;
532 goto fd_error;
533 }
c0e31d2e 534 stream_file = anon_inode_getfile("[lttng_stream]",
7f57c73c 535 &lib_ring_buffer_file_operations,
ad1c05e1 536 buf, O_RDWR);
c0e31d2e
MD
537 if (IS_ERR(stream_file)) {
538 ret = PTR_ERR(stream_file);
ad1c05e1
MD
539 goto file_error;
540 }
409453cb
MD
541 /*
542 * OPEN_FMODE, called within anon_inode_getfile/alloc_file, don't honor
543 * FMODE_LSEEK, FMODE_PREAD nor FMODE_PWRITE. We need to read from this
544 * file descriptor, so we set FMODE_PREAD here.
545 */
d7b6f197 546 stream_file->f_mode |= FMODE_PREAD;
c0e31d2e 547 fd_install(stream_fd, stream_file);
dda6a249
MD
548 /*
549 * The stream holds a reference to the channel within the generic ring
550 * buffer library, so no need to hold a refcount on the channel and
551 * session files here.
552 */
ad1c05e1
MD
553 return stream_fd;
554
555file_error:
556 put_unused_fd(stream_fd);
557fd_error:
11b5a3c2 558 channel->ops->buffer_read_close(buf);
ad1c05e1
MD
559 return ret;
560}
561
653fe716 562static
c0e31d2e 563int lttng_abi_create_event(struct file *channel_file,
6dccd6c1 564 struct lttng_kernel_event *event_param)
653fe716 565{
a90917c3
MD
566 struct lttng_channel *channel = channel_file->private_data;
567 struct lttng_event *event;
653fe716 568 int event_fd, ret;
11b5a3c2 569 struct file *event_file;
653fe716 570
6dccd6c1
JD
571 event_param->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
572 switch (event_param->instrumentation) {
7371f44c 573 case LTTNG_KERNEL_KRETPROBE:
6dccd6c1 574 event_param->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
7371f44c 575 break;
ab2277d6 576 case LTTNG_KERNEL_KPROBE:
6dccd6c1 577 event_param->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4 578 break;
ab2277d6 579 case LTTNG_KERNEL_FUNCTION:
6dccd6c1 580 event_param->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
e0a7a7c4
MD
581 break;
582 default:
583 break;
584 }
6dccd6c1 585 switch (event_param->instrumentation) {
1ec65de1
MD
586 default:
587 event_fd = get_unused_fd();
588 if (event_fd < 0) {
589 ret = event_fd;
590 goto fd_error;
591 }
592 event_file = anon_inode_getfile("[lttng_event]",
593 &lttng_event_fops,
594 NULL, O_RDWR);
595 if (IS_ERR(event_file)) {
596 ret = PTR_ERR(event_file);
597 goto file_error;
598 }
599 /*
600 * We tolerate no failure path after event creation. It
601 * will stay invariant for the rest of the session.
602 */
6dccd6c1 603 event = lttng_event_create(channel, event_param, NULL, NULL);
1ec65de1
MD
604 if (!event) {
605 ret = -EINVAL;
606 goto event_error;
607 }
608 event_file->private_data = event;
609 fd_install(event_fd, event_file);
610 /* The event holds a reference on the channel */
611 atomic_long_inc(&channel_file->f_count);
612 break;
43880ee8
MD
613 case LTTNG_KERNEL_SYSCALL:
614 /*
615 * Only all-syscall tracing supported for now.
616 */
6dccd6c1 617 if (event_param->name[0] != '\0')
43880ee8 618 return -EINVAL;
1ec65de1
MD
619 ret = lttng_syscalls_register(channel, NULL);
620 if (ret)
621 goto fd_error;
622 event_fd = 0;
623 break;
03037b98 624 }
653fe716
MD
625 return event_fd;
626
03037b98 627event_error:
c0e31d2e 628 fput(event_file);
653fe716
MD
629file_error:
630 put_unused_fd(event_fd);
631fd_error:
653fe716
MD
632 return ret;
633}
ad1c05e1
MD
634
635/**
636 * lttng_channel_ioctl - lttng syscall through ioctl
637 *
c0e31d2e 638 * @file: the file
ad1c05e1
MD
639 * @cmd: the command
640 * @arg: command arg
641 *
642 * This ioctl implements lttng commands:
38d024ae 643 * LTTNG_KERNEL_STREAM
ad1c05e1
MD
644 * Returns an event stream file descriptor or failure.
645 * (typically, one event stream records events from one CPU)
38d024ae 646 * LTTNG_KERNEL_EVENT
ad1c05e1 647 * Returns an event file descriptor or failure.
8070f5c0
MD
648 * LTTNG_KERNEL_CONTEXT
649 * Prepend a context field to each event in the channel
e64957da
MD
650 * LTTNG_KERNEL_ENABLE
651 * Enable recording for events in this channel (weak enable)
652 * LTTNG_KERNEL_DISABLE
653 * Disable recording for events in this channel (strong disable)
baf20995 654 *
baf20995
MD
655 * Channel and event file descriptors also hold a reference on the session.
656 */
ad1c05e1 657static
c0e31d2e 658long lttng_channel_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
baf20995 659{
a90917c3 660 struct lttng_channel *channel = file->private_data;
8070f5c0 661
baf20995 662 switch (cmd) {
6dccd6c1 663 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 664 case LTTNG_KERNEL_STREAM:
c0e31d2e 665 return lttng_abi_open_stream(file);
6dccd6c1
JD
666 case LTTNG_KERNEL_OLD_EVENT:
667 {
668 struct lttng_kernel_event *uevent_param;
669 struct lttng_kernel_old_event *old_uevent_param;
670 int ret;
671
672 uevent_param = kmalloc(sizeof(struct lttng_kernel_event),
673 GFP_KERNEL);
674 if (!uevent_param) {
675 ret = -ENOMEM;
676 goto old_event_end;
677 }
678 old_uevent_param = kmalloc(
679 sizeof(struct lttng_kernel_old_event),
680 GFP_KERNEL);
681 if (!old_uevent_param) {
682 ret = -ENOMEM;
683 goto old_event_error_free_param;
684 }
685 if (copy_from_user(old_uevent_param,
686 (struct lttng_kernel_old_event __user *) arg,
687 sizeof(struct lttng_kernel_old_event))) {
688 ret = -EFAULT;
689 goto old_event_error_free_old_param;
690 }
691
692 memcpy(uevent_param->name, old_uevent_param->name,
693 sizeof(uevent_param->name));
694 uevent_param->instrumentation =
695 old_uevent_param->instrumentation;
696
697 switch (old_uevent_param->instrumentation) {
698 case LTTNG_KERNEL_KPROBE:
699 uevent_param->u.kprobe.addr =
700 old_uevent_param->u.kprobe.addr;
701 uevent_param->u.kprobe.offset =
702 old_uevent_param->u.kprobe.offset;
703 memcpy(uevent_param->u.kprobe.symbol_name,
704 old_uevent_param->u.kprobe.symbol_name,
705 sizeof(uevent_param->u.kprobe.symbol_name));
706 break;
707 case LTTNG_KERNEL_KRETPROBE:
708 uevent_param->u.kretprobe.addr =
709 old_uevent_param->u.kretprobe.addr;
710 uevent_param->u.kretprobe.offset =
711 old_uevent_param->u.kretprobe.offset;
712 memcpy(uevent_param->u.kretprobe.symbol_name,
713 old_uevent_param->u.kretprobe.symbol_name,
714 sizeof(uevent_param->u.kretprobe.symbol_name));
715 break;
716 case LTTNG_KERNEL_FUNCTION:
717 memcpy(uevent_param->u.ftrace.symbol_name,
718 old_uevent_param->u.ftrace.symbol_name,
719 sizeof(uevent_param->u.ftrace.symbol_name));
720 break;
721 default:
722 break;
723 }
724 ret = lttng_abi_create_event(file, uevent_param);
725
726old_event_error_free_old_param:
727 kfree(old_uevent_param);
728old_event_error_free_param:
729 kfree(uevent_param);
730old_event_end:
731 return ret;
732 }
38d024ae 733 case LTTNG_KERNEL_EVENT:
6dccd6c1
JD
734 {
735 struct lttng_kernel_event uevent_param;
736
737 if (copy_from_user(&uevent_param,
738 (struct lttng_kernel_event __user *) arg,
739 sizeof(uevent_param)))
740 return -EFAULT;
741 return lttng_abi_create_event(file, &uevent_param);
742 }
743 case LTTNG_KERNEL_OLD_CONTEXT:
744 {
745 struct lttng_kernel_context *ucontext_param;
746 struct lttng_kernel_old_context *old_ucontext_param;
747 int ret;
748
749 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
750 GFP_KERNEL);
751 if (!ucontext_param) {
752 ret = -ENOMEM;
753 goto old_ctx_end;
754 }
755 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
756 GFP_KERNEL);
757 if (!old_ucontext_param) {
758 ret = -ENOMEM;
759 goto old_ctx_error_free_param;
760 }
761
762 if (copy_from_user(old_ucontext_param,
763 (struct lttng_kernel_old_context __user *) arg,
764 sizeof(struct lttng_kernel_old_context))) {
765 ret = -EFAULT;
766 goto old_ctx_error_free_old_param;
767 }
768 ucontext_param->ctx = old_ucontext_param->ctx;
769 memcpy(ucontext_param->padding, old_ucontext_param->padding,
770 sizeof(ucontext_param->padding));
771 /* only type that uses the union */
772 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
773 ucontext_param->u.perf_counter.type =
774 old_ucontext_param->u.perf_counter.type;
775 ucontext_param->u.perf_counter.config =
776 old_ucontext_param->u.perf_counter.config;
777 memcpy(ucontext_param->u.perf_counter.name,
778 old_ucontext_param->u.perf_counter.name,
779 sizeof(ucontext_param->u.perf_counter.name));
780 }
781
782 ret = lttng_abi_add_context(file,
783 ucontext_param,
784 &channel->ctx, channel->session);
785
786old_ctx_error_free_old_param:
787 kfree(old_ucontext_param);
788old_ctx_error_free_param:
789 kfree(ucontext_param);
790old_ctx_end:
791 return ret;
792 }
8070f5c0 793 case LTTNG_KERNEL_CONTEXT:
6dccd6c1
JD
794 {
795 struct lttng_kernel_context ucontext_param;
796
797 if (copy_from_user(&ucontext_param,
8070f5c0 798 (struct lttng_kernel_context __user *) arg,
6dccd6c1
JD
799 sizeof(ucontext_param)))
800 return -EFAULT;
801 return lttng_abi_add_context(file,
802 &ucontext_param,
8070f5c0 803 &channel->ctx, channel->session);
6dccd6c1
JD
804 }
805 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 806 case LTTNG_KERNEL_ENABLE:
a90917c3 807 return lttng_channel_enable(channel);
6dccd6c1 808 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 809 case LTTNG_KERNEL_DISABLE:
a90917c3 810 return lttng_channel_disable(channel);
baf20995
MD
811 default:
812 return -ENOIOCTLCMD;
813 }
6dccd6c1 814
baf20995
MD
815}
816
5dbbdb43
MD
817/**
818 * lttng_metadata_ioctl - lttng syscall through ioctl
819 *
820 * @file: the file
821 * @cmd: the command
822 * @arg: command arg
823 *
824 * This ioctl implements lttng commands:
38d024ae 825 * LTTNG_KERNEL_STREAM
5dbbdb43
MD
826 * Returns an event stream file descriptor or failure.
827 *
828 * Channel and event file descriptors also hold a reference on the session.
829 */
830static
831long lttng_metadata_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
832{
833 switch (cmd) {
6dccd6c1 834 case LTTNG_KERNEL_OLD_STREAM:
38d024ae 835 case LTTNG_KERNEL_STREAM:
5dbbdb43
MD
836 return lttng_abi_open_stream(file);
837 default:
838 return -ENOIOCTLCMD;
839 }
840}
841
653fe716
MD
842/**
843 * lttng_channel_poll - lttng stream addition/removal monitoring
844 *
c0e31d2e 845 * @file: the file
653fe716
MD
846 * @wait: poll table
847 */
c0e31d2e 848unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
653fe716 849{
a90917c3 850 struct lttng_channel *channel = file->private_data;
653fe716
MD
851 unsigned int mask = 0;
852
c0e31d2e 853 if (file->f_mode & FMODE_READ) {
a33e44a6 854 poll_wait_set_exclusive(wait);
24cedcfe
MD
855 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
856 wait);
653fe716 857
254ec7bc
MD
858 if (channel->ops->is_disabled(channel->chan))
859 return POLLERR;
24cedcfe 860 if (channel->ops->is_finalized(channel->chan))
653fe716 861 return POLLHUP;
f71ecafa 862 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
653fe716 863 return POLLIN | POLLRDNORM;
f71ecafa 864 return 0;
653fe716
MD
865 }
866 return mask;
867
868}
869
0a84a57f
MD
870static
871int lttng_channel_release(struct inode *inode, struct file *file)
872{
a90917c3 873 struct lttng_channel *channel = file->private_data;
c269fff4
MD
874
875 if (channel)
876 fput(channel->session->file);
0a84a57f
MD
877 return 0;
878}
879
ad1c05e1 880static const struct file_operations lttng_channel_fops = {
a33c9927 881 .owner = THIS_MODULE,
03037b98 882 .release = lttng_channel_release,
653fe716 883 .poll = lttng_channel_poll,
ad1c05e1 884 .unlocked_ioctl = lttng_channel_ioctl,
baf20995 885#ifdef CONFIG_COMPAT
03037b98 886 .compat_ioctl = lttng_channel_ioctl,
baf20995 887#endif
11b5a3c2 888};
baf20995 889
5dbbdb43 890static const struct file_operations lttng_metadata_fops = {
a33c9927 891 .owner = THIS_MODULE,
5dbbdb43
MD
892 .release = lttng_channel_release,
893 .unlocked_ioctl = lttng_metadata_ioctl,
894#ifdef CONFIG_COMPAT
895 .compat_ioctl = lttng_metadata_ioctl,
896#endif
897};
898
8070f5c0
MD
899/**
900 * lttng_event_ioctl - lttng syscall through ioctl
901 *
902 * @file: the file
903 * @cmd: the command
904 * @arg: command arg
905 *
906 * This ioctl implements lttng commands:
8070f5c0
MD
907 * LTTNG_KERNEL_CONTEXT
908 * Prepend a context field to each record of this event
e64957da
MD
909 * LTTNG_KERNEL_ENABLE
910 * Enable recording for this event (weak enable)
911 * LTTNG_KERNEL_DISABLE
912 * Disable recording for this event (strong disable)
8070f5c0
MD
913 */
914static
915long lttng_event_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
916{
a90917c3 917 struct lttng_event *event = file->private_data;
8070f5c0
MD
918
919 switch (cmd) {
6dccd6c1
JD
920 case LTTNG_KERNEL_OLD_CONTEXT:
921 {
922 struct lttng_kernel_context *ucontext_param;
923 struct lttng_kernel_old_context *old_ucontext_param;
924 int ret;
925
926 ucontext_param = kmalloc(sizeof(struct lttng_kernel_context),
927 GFP_KERNEL);
928 if (!ucontext_param) {
929 ret = -ENOMEM;
930 goto old_ctx_end;
931 }
932 old_ucontext_param = kmalloc(sizeof(struct lttng_kernel_old_context),
933 GFP_KERNEL);
934 if (!old_ucontext_param) {
935 ret = -ENOMEM;
936 goto old_ctx_error_free_param;
937 }
938
939 if (copy_from_user(old_ucontext_param,
940 (struct lttng_kernel_old_context __user *) arg,
941 sizeof(struct lttng_kernel_old_context))) {
942 ret = -EFAULT;
943 goto old_ctx_error_free_old_param;
944 }
945 ucontext_param->ctx = old_ucontext_param->ctx;
946 memcpy(ucontext_param->padding, old_ucontext_param->padding,
947 sizeof(ucontext_param->padding));
948 /* only type that uses the union */
949 if (old_ucontext_param->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
950 ucontext_param->u.perf_counter.type =
951 old_ucontext_param->u.perf_counter.type;
952 ucontext_param->u.perf_counter.config =
953 old_ucontext_param->u.perf_counter.config;
954 memcpy(ucontext_param->u.perf_counter.name,
955 old_ucontext_param->u.perf_counter.name,
956 sizeof(ucontext_param->u.perf_counter.name));
957 }
958
959 ret = lttng_abi_add_context(file,
960 ucontext_param,
961 &event->ctx, event->chan->session);
962
963old_ctx_error_free_old_param:
964 kfree(old_ucontext_param);
965old_ctx_error_free_param:
966 kfree(ucontext_param);
967old_ctx_end:
968 return ret;
969 }
8070f5c0 970 case LTTNG_KERNEL_CONTEXT:
6dccd6c1
JD
971 {
972 struct lttng_kernel_context ucontext_param;
973
974 if (copy_from_user(&ucontext_param,
975 (struct lttng_kernel_context __user *) arg,
976 sizeof(ucontext_param)))
977 return -EFAULT;
8070f5c0 978 return lttng_abi_add_context(file,
6dccd6c1 979 &ucontext_param,
8070f5c0 980 &event->ctx, event->chan->session);
6dccd6c1
JD
981 }
982 case LTTNG_KERNEL_OLD_ENABLE:
e64957da 983 case LTTNG_KERNEL_ENABLE:
a90917c3 984 return lttng_event_enable(event);
6dccd6c1 985 case LTTNG_KERNEL_OLD_DISABLE:
e64957da 986 case LTTNG_KERNEL_DISABLE:
a90917c3 987 return lttng_event_disable(event);
8070f5c0
MD
988 default:
989 return -ENOIOCTLCMD;
990 }
991}
992
0a84a57f
MD
993static
994int lttng_event_release(struct inode *inode, struct file *file)
995{
a90917c3 996 struct lttng_event *event = file->private_data;
c269fff4 997
aa7c23a9 998 if (event)
c269fff4 999 fput(event->chan->file);
0a84a57f
MD
1000 return 0;
1001}
1002
3b923e5b 1003/* TODO: filter control ioctl */
0a84a57f 1004static const struct file_operations lttng_event_fops = {
a33c9927 1005 .owner = THIS_MODULE,
0a84a57f 1006 .release = lttng_event_release,
8070f5c0
MD
1007 .unlocked_ioctl = lttng_event_ioctl,
1008#ifdef CONFIG_COMPAT
1009 .compat_ioctl = lttng_event_ioctl,
1010#endif
11b5a3c2 1011};
0a84a57f 1012
80996790 1013int __init lttng_abi_init(void)
baf20995
MD
1014{
1015 int ret = 0;
1016
6d2a620c 1017 wrapper_vmalloc_sync_all();
d29348f7 1018 lttng_proc_dentry = proc_create_data("lttng", S_IRUSR | S_IWUSR, NULL,
e6a17f26
MD
1019 &lttng_fops, NULL);
1020
255e52a4 1021 if (!lttng_proc_dentry) {
baf20995
MD
1022 printk(KERN_ERR "Error creating LTTng control file\n");
1023 ret = -ENOMEM;
1024 goto error;
1025 }
1026error:
1027 return ret;
1028}
1029
80996790 1030void __exit lttng_abi_exit(void)
baf20995 1031{
e6a17f26
MD
1032 if (lttng_proc_dentry)
1033 remove_proc_entry("lttng", NULL);
baf20995 1034}
This page took 0.076441 seconds and 4 git commands to generate.