2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #define __USE_LINUX_IOCTL_DEFS
22 #include <sys/ioctl.h>
24 #include <common/align.h>
29 #include "kernel-ctl.h"
30 #include "kernel-ioctl.h"
32 #define LTTNG_IOCTL(fildes, request, ...) ({ \
33 int ret = ioctl(fildes, request, ##__VA_ARGS__);\
39 * This flag indicates which version of the kernel ABI to use. The old
40 * ABI (namespace _old) does not support a 32-bit user-space when the
41 * kernel is 64-bit. The old ABI is kept here for compatibility but is
42 * deprecated and will be removed eventually.
44 static int lttng_kernel_use_old_abi
= -1;
47 * Execute the new or old ioctl depending on the ABI version.
48 * If the ABI version is not determined yet (lttng_kernel_use_old_abi = -1),
49 * this function tests if the new ABI is available and otherwise fallbacks
51 * This function takes the fd on which the ioctl must be executed and the old
52 * and new request codes.
53 * It returns the return value of the ioctl executed.
55 static inline int compat_ioctl_no_arg(int fd
, unsigned long oldname
,
56 unsigned long newname
)
60 if (lttng_kernel_use_old_abi
== -1) {
61 ret
= ioctl(fd
, newname
);
63 lttng_kernel_use_old_abi
= 0;
66 lttng_kernel_use_old_abi
= 1;
68 if (lttng_kernel_use_old_abi
) {
69 ret
= ioctl(fd
, oldname
);
71 ret
= ioctl(fd
, newname
);
78 int kernctl_create_session(int fd
)
80 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION
,
81 LTTNG_KERNEL_SESSION
);
84 /* open the metadata global channel */
85 int kernctl_open_metadata(int fd
, struct lttng_channel_attr
*chops
)
87 struct lttng_kernel_old_channel old_channel
;
88 struct lttng_kernel_channel channel
;
90 if (lttng_kernel_use_old_abi
) {
91 old_channel
.overwrite
= chops
->overwrite
;
92 old_channel
.subbuf_size
= chops
->subbuf_size
;
93 old_channel
.num_subbuf
= chops
->num_subbuf
;
94 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
95 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
96 old_channel
.output
= chops
->output
;
98 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
100 * The new channel padding is smaller than the old ABI so we use the
101 * new ABI padding size for the memcpy.
103 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
105 return ioctl(fd
, LTTNG_KERNEL_OLD_METADATA
, &old_channel
);
108 channel
.overwrite
= chops
->overwrite
;
109 channel
.subbuf_size
= chops
->subbuf_size
;
110 channel
.num_subbuf
= chops
->num_subbuf
;
111 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
112 channel
.read_timer_interval
= chops
->read_timer_interval
;
113 channel
.output
= chops
->output
;
114 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
116 return ioctl(fd
, LTTNG_KERNEL_METADATA
, &channel
);
119 int kernctl_create_channel(int fd
, struct lttng_channel_attr
*chops
)
121 struct lttng_kernel_channel channel
;
123 memset(&channel
, 0, sizeof(channel
));
124 if (lttng_kernel_use_old_abi
) {
125 struct lttng_kernel_old_channel old_channel
;
127 old_channel
.overwrite
= chops
->overwrite
;
128 old_channel
.subbuf_size
= chops
->subbuf_size
;
129 old_channel
.num_subbuf
= chops
->num_subbuf
;
130 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
131 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
132 old_channel
.output
= chops
->output
;
134 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
136 * The new channel padding is smaller than the old ABI so we use the
137 * new ABI padding size for the memcpy.
139 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
141 return ioctl(fd
, LTTNG_KERNEL_OLD_CHANNEL
, &old_channel
);
144 channel
.overwrite
= chops
->overwrite
;
145 channel
.subbuf_size
= chops
->subbuf_size
;
146 channel
.num_subbuf
= chops
->num_subbuf
;
147 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
148 channel
.read_timer_interval
= chops
->read_timer_interval
;
149 channel
.output
= chops
->output
;
150 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
152 return ioctl(fd
, LTTNG_KERNEL_CHANNEL
, &channel
);
155 int kernctl_syscall_mask(int fd
, char **syscall_mask
, uint32_t *nr_bits
)
157 struct lttng_kernel_syscall_mask kmask_len
, *kmask
= NULL
;
158 size_t array_alloc_len
;
173 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_SYSCALL_MASK
, &kmask_len
);
178 array_alloc_len
= ALIGN(kmask_len
.len
, 8) >> 3;
180 kmask
= zmalloc(sizeof(*kmask
) + array_alloc_len
);
186 kmask
->len
= kmask_len
.len
;
187 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_SYSCALL_MASK
, kmask
);
192 new_mask
= realloc(*syscall_mask
, array_alloc_len
);
197 memcpy(new_mask
, kmask
->mask
, array_alloc_len
);
198 *syscall_mask
= new_mask
;
199 *nr_bits
= kmask
->len
;
206 int kernctl_track_pid(int fd
, int pid
)
208 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_SESSION_TRACK_PID
, pid
);
211 int kernctl_untrack_pid(int fd
, int pid
)
213 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_SESSION_UNTRACK_PID
, pid
);
216 int kernctl_list_tracker_pids(int fd
)
218 return ioctl(fd
, LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
);
221 int kernctl_session_metadata_regenerate(int fd
)
223 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_SESSION_METADATA_REGEN
);
226 int kernctl_create_stream(int fd
)
228 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_STREAM
,
229 LTTNG_KERNEL_STREAM
);
232 int kernctl_create_event(int fd
, struct lttng_kernel_event
*ev
)
234 if (lttng_kernel_use_old_abi
) {
235 struct lttng_kernel_old_event old_event
;
237 memcpy(old_event
.name
, ev
->name
, sizeof(old_event
.name
));
238 old_event
.instrumentation
= ev
->instrumentation
;
239 switch (ev
->instrumentation
) {
240 case LTTNG_KERNEL_KPROBE
:
241 old_event
.u
.kprobe
.addr
= ev
->u
.kprobe
.addr
;
242 old_event
.u
.kprobe
.offset
= ev
->u
.kprobe
.offset
;
243 memcpy(old_event
.u
.kprobe
.symbol_name
,
244 ev
->u
.kprobe
.symbol_name
,
245 sizeof(old_event
.u
.kprobe
.symbol_name
));
247 case LTTNG_KERNEL_KRETPROBE
:
248 old_event
.u
.kretprobe
.addr
= ev
->u
.kretprobe
.addr
;
249 old_event
.u
.kretprobe
.offset
= ev
->u
.kretprobe
.offset
;
250 memcpy(old_event
.u
.kretprobe
.symbol_name
,
251 ev
->u
.kretprobe
.symbol_name
,
252 sizeof(old_event
.u
.kretprobe
.symbol_name
));
254 case LTTNG_KERNEL_FUNCTION
:
255 memcpy(old_event
.u
.ftrace
.symbol_name
,
256 ev
->u
.ftrace
.symbol_name
,
257 sizeof(old_event
.u
.ftrace
.symbol_name
));
263 return ioctl(fd
, LTTNG_KERNEL_OLD_EVENT
, &old_event
);
265 return ioctl(fd
, LTTNG_KERNEL_EVENT
, ev
);
268 int kernctl_add_context(int fd
, struct lttng_kernel_context
*ctx
)
270 if (lttng_kernel_use_old_abi
) {
271 struct lttng_kernel_old_context old_ctx
;
273 old_ctx
.ctx
= ctx
->ctx
;
274 /* only type that uses the union */
275 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
276 old_ctx
.u
.perf_counter
.type
=
277 ctx
->u
.perf_counter
.type
;
278 old_ctx
.u
.perf_counter
.config
=
279 ctx
->u
.perf_counter
.config
;
280 memcpy(old_ctx
.u
.perf_counter
.name
,
281 ctx
->u
.perf_counter
.name
,
282 sizeof(old_ctx
.u
.perf_counter
.name
));
284 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_OLD_CONTEXT
, &old_ctx
);
286 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_CONTEXT
, ctx
);
290 /* Enable event, channel and session LTTNG_IOCTL */
291 int kernctl_enable(int fd
)
293 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_ENABLE
,
294 LTTNG_KERNEL_ENABLE
);
297 /* Disable event, channel and session LTTNG_IOCTL */
298 int kernctl_disable(int fd
)
300 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_DISABLE
,
301 LTTNG_KERNEL_DISABLE
);
304 int kernctl_start_session(int fd
)
306 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_START
,
307 LTTNG_KERNEL_SESSION_START
);
310 int kernctl_stop_session(int fd
)
312 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_STOP
,
313 LTTNG_KERNEL_SESSION_STOP
);
316 int kernctl_filter(int fd
, struct lttng_filter_bytecode
*filter
)
318 struct lttng_kernel_filter_bytecode
*kb
;
322 /* Translate bytecode to kernel bytecode */
323 kb
= zmalloc(sizeof(*kb
) + filter
->len
);
326 kb
->len
= len
= filter
->len
;
327 kb
->reloc_offset
= filter
->reloc_table_offset
;
328 kb
->seqnum
= filter
->seqnum
;
329 memcpy(kb
->data
, filter
->data
, len
);
330 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_FILTER
, kb
);
335 int kernctl_tracepoint_list(int fd
)
337 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_TRACEPOINT_LIST
,
338 LTTNG_KERNEL_TRACEPOINT_LIST
);
341 int kernctl_syscall_list(int fd
)
343 return ioctl(fd
, LTTNG_KERNEL_SYSCALL_LIST
);
346 int kernctl_tracer_version(int fd
, struct lttng_kernel_tracer_version
*v
)
350 if (lttng_kernel_use_old_abi
== -1) {
351 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
353 lttng_kernel_use_old_abi
= 0;
356 lttng_kernel_use_old_abi
= 1;
358 if (lttng_kernel_use_old_abi
) {
359 struct lttng_kernel_old_tracer_version old_v
;
361 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_OLD_TRACER_VERSION
, &old_v
);
365 v
->major
= old_v
.major
;
366 v
->minor
= old_v
.minor
;
367 v
->patchlevel
= old_v
.patchlevel
;
369 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
376 int kernctl_tracer_abi_version(int fd
,
377 struct lttng_kernel_tracer_abi_version
*v
)
379 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_TRACER_ABI_VERSION
, v
);
382 int kernctl_wait_quiescent(int fd
)
384 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_WAIT_QUIESCENT
,
385 LTTNG_KERNEL_WAIT_QUIESCENT
);
388 int kernctl_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
392 if (lttng_kernel_use_old_abi
== -1) {
393 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
395 lttng_kernel_use_old_abi
= 0;
398 lttng_kernel_use_old_abi
= 1;
400 if (lttng_kernel_use_old_abi
) {
401 struct lttng_kernel_old_calibrate old_calibrate
;
403 old_calibrate
.type
= calibrate
->type
;
404 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_OLD_CALIBRATE
,
409 calibrate
->type
= old_calibrate
.type
;
411 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
419 int kernctl_buffer_flush(int fd
)
421 return LTTNG_IOCTL(fd
, RING_BUFFER_FLUSH
);
424 /* returns the version of the metadata. */
425 int kernctl_get_metadata_version(int fd
, uint64_t *version
)
427 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_METADATA_VERSION
, version
);
431 /* Buffer operations */
433 /* For mmap mode, readable without "get" operation */
435 /* returns the length to mmap. */
436 int kernctl_get_mmap_len(int fd
, unsigned long *len
)
438 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_MMAP_LEN
, len
);
441 /* returns the maximum size for sub-buffers. */
442 int kernctl_get_max_subbuf_size(int fd
, unsigned long *len
)
444 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_MAX_SUBBUF_SIZE
, len
);
448 * For mmap mode, operate on the current packet (between get/put or
449 * get_next/put_next).
452 /* returns the offset of the subbuffer belonging to the mmap reader. */
453 int kernctl_get_mmap_read_offset(int fd
, unsigned long *off
)
455 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_MMAP_READ_OFFSET
, off
);
458 /* returns the size of the current sub-buffer, without padding (for mmap). */
459 int kernctl_get_subbuf_size(int fd
, unsigned long *len
)
461 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_SUBBUF_SIZE
, len
);
464 /* returns the size of the current sub-buffer, without padding (for mmap). */
465 int kernctl_get_padded_subbuf_size(int fd
, unsigned long *len
)
467 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_PADDED_SUBBUF_SIZE
, len
);
470 /* Get exclusive read access to the next sub-buffer that can be read. */
471 int kernctl_get_next_subbuf(int fd
)
473 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_NEXT_SUBBUF
);
477 /* Release exclusive sub-buffer access, move consumer forward. */
478 int kernctl_put_next_subbuf(int fd
)
480 return LTTNG_IOCTL(fd
, RING_BUFFER_PUT_NEXT_SUBBUF
);
485 /* Get a snapshot of the current ring buffer producer and consumer positions */
486 int kernctl_snapshot(int fd
)
488 return LTTNG_IOCTL(fd
, RING_BUFFER_SNAPSHOT
);
491 /* Get the consumer position (iteration start) */
492 int kernctl_snapshot_get_consumed(int fd
, unsigned long *pos
)
494 return LTTNG_IOCTL(fd
, RING_BUFFER_SNAPSHOT_GET_CONSUMED
, pos
);
497 /* Get the producer position (iteration end) */
498 int kernctl_snapshot_get_produced(int fd
, unsigned long *pos
)
500 return LTTNG_IOCTL(fd
, RING_BUFFER_SNAPSHOT_GET_PRODUCED
, pos
);
503 /* Get exclusive read access to the specified sub-buffer position */
504 int kernctl_get_subbuf(int fd
, unsigned long *len
)
506 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_SUBBUF
, len
);
509 /* Release exclusive sub-buffer access */
510 int kernctl_put_subbuf(int fd
)
512 return LTTNG_IOCTL(fd
, RING_BUFFER_PUT_SUBBUF
);
515 /* Returns the timestamp begin of the current sub-buffer. */
516 int kernctl_get_timestamp_begin(int fd
, uint64_t *timestamp_begin
)
518 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
,
522 /* Returns the timestamp end of the current sub-buffer. */
523 int kernctl_get_timestamp_end(int fd
, uint64_t *timestamp_end
)
525 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_END
,
529 /* Returns the number of discarded events in the current sub-buffer. */
530 int kernctl_get_events_discarded(int fd
, uint64_t *events_discarded
)
532 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
,
536 /* Returns the content size in the current sub-buffer. */
537 int kernctl_get_content_size(int fd
, uint64_t *content_size
)
539 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_CONTENT_SIZE
,
543 /* Returns the packet size in the current sub-buffer. */
544 int kernctl_get_packet_size(int fd
, uint64_t *packet_size
)
546 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_PACKET_SIZE
, packet_size
);
549 /* Returns the stream id of the current sub-buffer. */
550 int kernctl_get_stream_id(int fd
, uint64_t *stream_id
)
552 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_STREAM_ID
, stream_id
);
555 /* Returns the current timestamp. */
556 int kernctl_get_current_timestamp(int fd
, uint64_t *ts
)
558 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
, ts
);
561 /* Returns the packet sequence number of the current sub-buffer. */
562 int kernctl_get_sequence_number(int fd
, uint64_t *seq
)
564 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_SEQ_NUM
, seq
);
567 /* Returns the stream instance id. */
568 int kernctl_get_instance_id(int fd
, uint64_t *id
)
570 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_INSTANCE_ID
, id
);