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