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_CHECK(fildes, request, ...) ({ \
33 int ret = ioctl(fildes, request, ##__VA_ARGS__);\
38 #define LTTNG_IOCTL_NO_CHECK(fildes, request, ...) ({ \
39 int ret = ioctl(fildes, request, ##__VA_ARGS__);\
40 ret >= 0 ? ret : -errno; \
44 * This flag indicates which version of the kernel ABI to use. The old
45 * ABI (namespace _old) does not support a 32-bit user-space when the
46 * kernel is 64-bit. The old ABI is kept here for compatibility but is
47 * deprecated and will be removed eventually.
49 static int lttng_kernel_use_old_abi
= -1;
52 * Execute the new or old ioctl depending on the ABI version.
53 * If the ABI version is not determined yet (lttng_kernel_use_old_abi = -1),
54 * this function tests if the new ABI is available and otherwise fallbacks
56 * This function takes the fd on which the ioctl must be executed and the old
57 * and new request codes.
58 * It returns the return value of the ioctl executed.
60 static inline int compat_ioctl_no_arg(int fd
, unsigned long oldname
,
61 unsigned long newname
)
65 if (lttng_kernel_use_old_abi
== -1) {
66 ret
= LTTNG_IOCTL_NO_CHECK(fd
, newname
);
68 lttng_kernel_use_old_abi
= 0;
71 lttng_kernel_use_old_abi
= 1;
73 if (lttng_kernel_use_old_abi
) {
74 ret
= LTTNG_IOCTL_NO_CHECK(fd
, oldname
);
76 ret
= LTTNG_IOCTL_NO_CHECK(fd
, newname
);
83 int kernctl_create_session(int fd
)
85 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION
,
86 LTTNG_KERNEL_SESSION
);
89 /* open the metadata global channel */
90 int kernctl_open_metadata(int fd
, struct lttng_channel_attr
*chops
)
92 struct lttng_kernel_channel channel
;
94 if (lttng_kernel_use_old_abi
) {
95 struct lttng_kernel_old_channel old_channel
;
97 memset(&old_channel
, 0, sizeof(old_channel
));
98 old_channel
.overwrite
= chops
->overwrite
;
99 old_channel
.subbuf_size
= chops
->subbuf_size
;
100 old_channel
.num_subbuf
= chops
->num_subbuf
;
101 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
102 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
103 old_channel
.output
= chops
->output
;
105 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
107 * The new channel padding is smaller than the old ABI so we use the
108 * new ABI padding size for the memcpy.
110 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
112 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_OLD_METADATA
,
116 memset(&channel
, 0, sizeof(channel
));
117 channel
.overwrite
= chops
->overwrite
;
118 channel
.subbuf_size
= chops
->subbuf_size
;
119 channel
.num_subbuf
= chops
->num_subbuf
;
120 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
121 channel
.read_timer_interval
= chops
->read_timer_interval
;
122 channel
.output
= chops
->output
;
123 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
125 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_METADATA
, &channel
);
128 int kernctl_create_channel(int fd
, struct lttng_channel_attr
*chops
)
130 struct lttng_kernel_channel channel
;
132 memset(&channel
, 0, sizeof(channel
));
133 if (lttng_kernel_use_old_abi
) {
134 struct lttng_kernel_old_channel old_channel
;
136 old_channel
.overwrite
= chops
->overwrite
;
137 old_channel
.subbuf_size
= chops
->subbuf_size
;
138 old_channel
.num_subbuf
= chops
->num_subbuf
;
139 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
140 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
141 old_channel
.output
= chops
->output
;
143 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
145 * The new channel padding is smaller than the old ABI so we use the
146 * new ABI padding size for the memcpy.
148 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
150 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_OLD_CHANNEL
,
154 channel
.overwrite
= chops
->overwrite
;
155 channel
.subbuf_size
= chops
->subbuf_size
;
156 channel
.num_subbuf
= chops
->num_subbuf
;
157 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
158 channel
.read_timer_interval
= chops
->read_timer_interval
;
159 channel
.output
= chops
->output
;
160 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
162 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_CHANNEL
, &channel
);
165 int kernctl_syscall_mask(int fd
, char **syscall_mask
, uint32_t *nr_bits
)
167 struct lttng_kernel_syscall_mask kmask_len
, *kmask
= NULL
;
168 size_t array_alloc_len
;
183 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SYSCALL_MASK
, &kmask_len
);
188 array_alloc_len
= ALIGN(kmask_len
.len
, 8) >> 3;
190 kmask
= zmalloc(sizeof(*kmask
) + array_alloc_len
);
196 kmask
->len
= kmask_len
.len
;
197 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SYSCALL_MASK
, kmask
);
202 new_mask
= realloc(*syscall_mask
, array_alloc_len
);
207 memcpy(new_mask
, kmask
->mask
, array_alloc_len
);
208 *syscall_mask
= new_mask
;
209 *nr_bits
= kmask
->len
;
216 int kernctl_track_pid(int fd
, int pid
)
218 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_TRACK_PID
, pid
);
221 int kernctl_untrack_pid(int fd
, int pid
)
223 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_UNTRACK_PID
, pid
);
226 int kernctl_list_tracker_pids(int fd
)
228 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
);
231 int kernctl_session_regenerate_metadata(int fd
)
233 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_METADATA_REGEN
);
236 int kernctl_session_regenerate_statedump(int fd
)
238 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_STATEDUMP
);
241 int kernctl_create_stream(int fd
)
243 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_STREAM
,
244 LTTNG_KERNEL_STREAM
);
247 int kernctl_create_event(int fd
, struct lttng_kernel_event
*ev
)
249 if (lttng_kernel_use_old_abi
) {
250 struct lttng_kernel_old_event old_event
;
252 memset(&old_event
, 0, sizeof(old_event
));
253 memcpy(old_event
.name
, ev
->name
, sizeof(old_event
.name
));
254 old_event
.instrumentation
= ev
->instrumentation
;
255 switch (ev
->instrumentation
) {
256 case LTTNG_KERNEL_KPROBE
:
257 old_event
.u
.kprobe
.addr
= ev
->u
.kprobe
.addr
;
258 old_event
.u
.kprobe
.offset
= ev
->u
.kprobe
.offset
;
259 memcpy(old_event
.u
.kprobe
.symbol_name
,
260 ev
->u
.kprobe
.symbol_name
,
261 sizeof(old_event
.u
.kprobe
.symbol_name
));
263 case LTTNG_KERNEL_KRETPROBE
:
264 old_event
.u
.kretprobe
.addr
= ev
->u
.kretprobe
.addr
;
265 old_event
.u
.kretprobe
.offset
= ev
->u
.kretprobe
.offset
;
266 memcpy(old_event
.u
.kretprobe
.symbol_name
,
267 ev
->u
.kretprobe
.symbol_name
,
268 sizeof(old_event
.u
.kretprobe
.symbol_name
));
270 case LTTNG_KERNEL_FUNCTION
:
271 memcpy(old_event
.u
.ftrace
.symbol_name
,
272 ev
->u
.ftrace
.symbol_name
,
273 sizeof(old_event
.u
.ftrace
.symbol_name
));
279 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_OLD_EVENT
,
282 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_EVENT
, ev
);
285 int kernctl_add_context(int fd
, struct lttng_kernel_context
*ctx
)
287 if (lttng_kernel_use_old_abi
) {
288 struct lttng_kernel_old_context old_ctx
;
290 memset(&old_ctx
, 0, sizeof(old_ctx
));
291 old_ctx
.ctx
= ctx
->ctx
;
292 /* only type that uses the union */
293 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
294 old_ctx
.u
.perf_counter
.type
=
295 ctx
->u
.perf_counter
.type
;
296 old_ctx
.u
.perf_counter
.config
=
297 ctx
->u
.perf_counter
.config
;
298 memcpy(old_ctx
.u
.perf_counter
.name
,
299 ctx
->u
.perf_counter
.name
,
300 sizeof(old_ctx
.u
.perf_counter
.name
));
302 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_OLD_CONTEXT
, &old_ctx
);
304 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_CONTEXT
, ctx
);
308 /* Enable event, channel and session LTTNG_IOCTL_CHECK */
309 int kernctl_enable(int fd
)
311 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_ENABLE
,
312 LTTNG_KERNEL_ENABLE
);
315 /* Disable event, channel and session LTTNG_IOCTL_CHECK */
316 int kernctl_disable(int fd
)
318 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_DISABLE
,
319 LTTNG_KERNEL_DISABLE
);
322 int kernctl_start_session(int fd
)
324 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_START
,
325 LTTNG_KERNEL_SESSION_START
);
328 int kernctl_stop_session(int fd
)
330 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_STOP
,
331 LTTNG_KERNEL_SESSION_STOP
);
334 int kernctl_filter(int fd
, struct lttng_filter_bytecode
*filter
)
336 struct lttng_kernel_filter_bytecode
*kb
;
340 /* Translate bytecode to kernel bytecode */
341 kb
= zmalloc(sizeof(*kb
) + filter
->len
);
344 kb
->len
= len
= filter
->len
;
345 kb
->reloc_offset
= filter
->reloc_table_offset
;
346 kb
->seqnum
= filter
->seqnum
;
347 memcpy(kb
->data
, filter
->data
, len
);
348 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_FILTER
, kb
);
353 int kernctl_tracepoint_list(int fd
)
355 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_TRACEPOINT_LIST
,
356 LTTNG_KERNEL_TRACEPOINT_LIST
);
359 int kernctl_syscall_list(int fd
)
361 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_SYSCALL_LIST
);
364 int kernctl_tracer_version(int fd
, struct lttng_kernel_tracer_version
*v
)
368 if (lttng_kernel_use_old_abi
== -1) {
369 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
371 lttng_kernel_use_old_abi
= 0;
374 lttng_kernel_use_old_abi
= 1;
376 if (lttng_kernel_use_old_abi
) {
377 struct lttng_kernel_old_tracer_version old_v
;
379 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_OLD_TRACER_VERSION
, &old_v
);
383 v
->major
= old_v
.major
;
384 v
->minor
= old_v
.minor
;
385 v
->patchlevel
= old_v
.patchlevel
;
387 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
394 int kernctl_tracer_abi_version(int fd
,
395 struct lttng_kernel_tracer_abi_version
*v
)
397 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_TRACER_ABI_VERSION
, v
);
400 int kernctl_wait_quiescent(int fd
)
402 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_WAIT_QUIESCENT
,
403 LTTNG_KERNEL_WAIT_QUIESCENT
);
406 int kernctl_buffer_flush(int fd
)
408 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_FLUSH
);
411 int kernctl_buffer_flush_empty(int fd
)
413 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_FLUSH_EMPTY
);
416 /* returns the version of the metadata. */
417 int kernctl_get_metadata_version(int fd
, uint64_t *version
)
419 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_METADATA_VERSION
, version
);
423 /* Buffer operations */
425 /* For mmap mode, readable without "get" operation */
427 /* returns the length to mmap. */
428 int kernctl_get_mmap_len(int fd
, unsigned long *len
)
430 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_MMAP_LEN
, len
);
433 /* returns the maximum size for sub-buffers. */
434 int kernctl_get_max_subbuf_size(int fd
, unsigned long *len
)
436 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_MAX_SUBBUF_SIZE
, len
);
440 * For mmap mode, operate on the current packet (between get/put or
441 * get_next/put_next).
444 /* returns the offset of the subbuffer belonging to the mmap reader. */
445 int kernctl_get_mmap_read_offset(int fd
, unsigned long *off
)
447 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_MMAP_READ_OFFSET
, off
);
450 /* returns the size of the current sub-buffer, without padding (for mmap). */
451 int kernctl_get_subbuf_size(int fd
, unsigned long *len
)
453 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_SUBBUF_SIZE
, len
);
456 /* returns the size of the current sub-buffer, without padding (for mmap). */
457 int kernctl_get_padded_subbuf_size(int fd
, unsigned long *len
)
459 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_PADDED_SUBBUF_SIZE
, len
);
462 /* Get exclusive read access to the next sub-buffer that can be read. */
463 int kernctl_get_next_subbuf(int fd
)
465 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_NEXT_SUBBUF
);
469 /* Release exclusive sub-buffer access, move consumer forward. */
470 int kernctl_put_next_subbuf(int fd
)
472 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_PUT_NEXT_SUBBUF
);
477 /* Get a snapshot of the current ring buffer producer and consumer positions */
478 int kernctl_snapshot(int fd
)
480 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_SNAPSHOT
);
484 * Get a snapshot of the current ring buffer producer and consumer positions,
485 * regardless of whether or not the two positions are contained within the
488 int kernctl_snapshot_sample_positions(int fd
)
490 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS
);
493 /* Get the consumer position (iteration start) */
494 int kernctl_snapshot_get_consumed(int fd
, unsigned long *pos
)
496 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_SNAPSHOT_GET_CONSUMED
, pos
);
499 /* Get the producer position (iteration end) */
500 int kernctl_snapshot_get_produced(int fd
, unsigned long *pos
)
502 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_SNAPSHOT_GET_PRODUCED
, pos
);
505 /* Get exclusive read access to the specified sub-buffer position */
506 int kernctl_get_subbuf(int fd
, unsigned long *len
)
508 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_SUBBUF
, len
);
511 /* Release exclusive sub-buffer access */
512 int kernctl_put_subbuf(int fd
)
514 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_PUT_SUBBUF
);
517 /* Returns the timestamp begin of the current sub-buffer. */
518 int kernctl_get_timestamp_begin(int fd
, uint64_t *timestamp_begin
)
520 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
,
524 /* Returns the timestamp end of the current sub-buffer. */
525 int kernctl_get_timestamp_end(int fd
, uint64_t *timestamp_end
)
527 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_END
,
531 /* Returns the number of discarded events in the current sub-buffer. */
532 int kernctl_get_events_discarded(int fd
, uint64_t *events_discarded
)
534 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
,
538 /* Returns the content size in the current sub-buffer. */
539 int kernctl_get_content_size(int fd
, uint64_t *content_size
)
541 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_CONTENT_SIZE
,
545 /* Returns the packet size in the current sub-buffer. */
546 int kernctl_get_packet_size(int fd
, uint64_t *packet_size
)
548 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_PACKET_SIZE
,
552 /* Returns the stream id of the current sub-buffer. */
553 int kernctl_get_stream_id(int fd
, uint64_t *stream_id
)
555 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_STREAM_ID
,
559 /* Returns the current timestamp. */
560 int kernctl_get_current_timestamp(int fd
, uint64_t *ts
)
562 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
,
566 /* Returns the packet sequence number of the current sub-buffer. */
567 int kernctl_get_sequence_number(int fd
, uint64_t *seq
)
569 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_SEQ_NUM
, seq
);
572 /* Returns the stream instance id. */
573 int kernctl_get_instance_id(int fd
, uint64_t *id
)
575 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_INSTANCE_ID
, id
);