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_old_channel old_channel
;
93 struct lttng_kernel_channel channel
;
95 if (lttng_kernel_use_old_abi
) {
96 old_channel
.overwrite
= chops
->overwrite
;
97 old_channel
.subbuf_size
= chops
->subbuf_size
;
98 old_channel
.num_subbuf
= chops
->num_subbuf
;
99 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
100 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
101 old_channel
.output
= chops
->output
;
103 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
105 * The new channel padding is smaller than the old ABI so we use the
106 * new ABI padding size for the memcpy.
108 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
110 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_OLD_METADATA
,
114 channel
.overwrite
= chops
->overwrite
;
115 channel
.subbuf_size
= chops
->subbuf_size
;
116 channel
.num_subbuf
= chops
->num_subbuf
;
117 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
118 channel
.read_timer_interval
= chops
->read_timer_interval
;
119 channel
.output
= chops
->output
;
120 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
122 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_METADATA
, &channel
);
125 int kernctl_create_channel(int fd
, struct lttng_channel_attr
*chops
)
127 struct lttng_kernel_channel channel
;
129 memset(&channel
, 0, sizeof(channel
));
130 if (lttng_kernel_use_old_abi
) {
131 struct lttng_kernel_old_channel old_channel
;
133 old_channel
.overwrite
= chops
->overwrite
;
134 old_channel
.subbuf_size
= chops
->subbuf_size
;
135 old_channel
.num_subbuf
= chops
->num_subbuf
;
136 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
137 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
138 old_channel
.output
= chops
->output
;
140 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
142 * The new channel padding is smaller than the old ABI so we use the
143 * new ABI padding size for the memcpy.
145 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
147 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_OLD_CHANNEL
,
151 channel
.overwrite
= chops
->overwrite
;
152 channel
.subbuf_size
= chops
->subbuf_size
;
153 channel
.num_subbuf
= chops
->num_subbuf
;
154 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
155 channel
.read_timer_interval
= chops
->read_timer_interval
;
156 channel
.output
= chops
->output
;
157 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
159 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_CHANNEL
, &channel
);
162 int kernctl_syscall_mask(int fd
, char **syscall_mask
, uint32_t *nr_bits
)
164 struct lttng_kernel_syscall_mask kmask_len
, *kmask
= NULL
;
165 size_t array_alloc_len
;
180 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SYSCALL_MASK
, &kmask_len
);
185 array_alloc_len
= ALIGN(kmask_len
.len
, 8) >> 3;
187 kmask
= zmalloc(sizeof(*kmask
) + array_alloc_len
);
193 kmask
->len
= kmask_len
.len
;
194 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SYSCALL_MASK
, kmask
);
199 new_mask
= realloc(*syscall_mask
, array_alloc_len
);
204 memcpy(new_mask
, kmask
->mask
, array_alloc_len
);
205 *syscall_mask
= new_mask
;
206 *nr_bits
= kmask
->len
;
213 int kernctl_track_pid(int fd
, int pid
)
215 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_TRACK_PID
, pid
);
218 int kernctl_untrack_pid(int fd
, int pid
)
220 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_UNTRACK_PID
, pid
);
223 int kernctl_list_tracker_pids(int fd
)
225 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
);
228 int kernctl_session_regenerate_metadata(int fd
)
230 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_METADATA_REGEN
);
233 int kernctl_session_regenerate_statedump(int fd
)
235 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_STATEDUMP
);
238 int kernctl_create_stream(int fd
)
240 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_STREAM
,
241 LTTNG_KERNEL_STREAM
);
244 int kernctl_create_event(int fd
, struct lttng_kernel_event
*ev
)
246 if (lttng_kernel_use_old_abi
) {
247 struct lttng_kernel_old_event old_event
;
249 memcpy(old_event
.name
, ev
->name
, sizeof(old_event
.name
));
250 old_event
.instrumentation
= ev
->instrumentation
;
251 switch (ev
->instrumentation
) {
252 case LTTNG_KERNEL_KPROBE
:
253 old_event
.u
.kprobe
.addr
= ev
->u
.kprobe
.addr
;
254 old_event
.u
.kprobe
.offset
= ev
->u
.kprobe
.offset
;
255 memcpy(old_event
.u
.kprobe
.symbol_name
,
256 ev
->u
.kprobe
.symbol_name
,
257 sizeof(old_event
.u
.kprobe
.symbol_name
));
259 case LTTNG_KERNEL_KRETPROBE
:
260 old_event
.u
.kretprobe
.addr
= ev
->u
.kretprobe
.addr
;
261 old_event
.u
.kretprobe
.offset
= ev
->u
.kretprobe
.offset
;
262 memcpy(old_event
.u
.kretprobe
.symbol_name
,
263 ev
->u
.kretprobe
.symbol_name
,
264 sizeof(old_event
.u
.kretprobe
.symbol_name
));
266 case LTTNG_KERNEL_FUNCTION
:
267 memcpy(old_event
.u
.ftrace
.symbol_name
,
268 ev
->u
.ftrace
.symbol_name
,
269 sizeof(old_event
.u
.ftrace
.symbol_name
));
275 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_OLD_EVENT
,
278 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_EVENT
, ev
);
281 int kernctl_add_context(int fd
, struct lttng_kernel_context
*ctx
)
283 if (lttng_kernel_use_old_abi
) {
284 struct lttng_kernel_old_context old_ctx
;
286 old_ctx
.ctx
= ctx
->ctx
;
287 /* only type that uses the union */
288 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
289 old_ctx
.u
.perf_counter
.type
=
290 ctx
->u
.perf_counter
.type
;
291 old_ctx
.u
.perf_counter
.config
=
292 ctx
->u
.perf_counter
.config
;
293 memcpy(old_ctx
.u
.perf_counter
.name
,
294 ctx
->u
.perf_counter
.name
,
295 sizeof(old_ctx
.u
.perf_counter
.name
));
297 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_OLD_CONTEXT
, &old_ctx
);
299 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_CONTEXT
, ctx
);
303 /* Enable event, channel and session LTTNG_IOCTL_CHECK */
304 int kernctl_enable(int fd
)
306 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_ENABLE
,
307 LTTNG_KERNEL_ENABLE
);
310 /* Disable event, channel and session LTTNG_IOCTL_CHECK */
311 int kernctl_disable(int fd
)
313 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_DISABLE
,
314 LTTNG_KERNEL_DISABLE
);
317 int kernctl_start_session(int fd
)
319 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_START
,
320 LTTNG_KERNEL_SESSION_START
);
323 int kernctl_stop_session(int fd
)
325 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_STOP
,
326 LTTNG_KERNEL_SESSION_STOP
);
329 int kernctl_filter(int fd
, struct lttng_filter_bytecode
*filter
)
331 struct lttng_kernel_filter_bytecode
*kb
;
335 /* Translate bytecode to kernel bytecode */
336 kb
= zmalloc(sizeof(*kb
) + filter
->len
);
339 kb
->len
= len
= filter
->len
;
340 kb
->reloc_offset
= filter
->reloc_table_offset
;
341 kb
->seqnum
= filter
->seqnum
;
342 memcpy(kb
->data
, filter
->data
, len
);
343 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_FILTER
, kb
);
348 int kernctl_tracepoint_list(int fd
)
350 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_TRACEPOINT_LIST
,
351 LTTNG_KERNEL_TRACEPOINT_LIST
);
354 int kernctl_syscall_list(int fd
)
356 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_SYSCALL_LIST
);
359 int kernctl_tracer_version(int fd
, struct lttng_kernel_tracer_version
*v
)
363 if (lttng_kernel_use_old_abi
== -1) {
364 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
366 lttng_kernel_use_old_abi
= 0;
369 lttng_kernel_use_old_abi
= 1;
371 if (lttng_kernel_use_old_abi
) {
372 struct lttng_kernel_old_tracer_version old_v
;
374 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_OLD_TRACER_VERSION
, &old_v
);
378 v
->major
= old_v
.major
;
379 v
->minor
= old_v
.minor
;
380 v
->patchlevel
= old_v
.patchlevel
;
382 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
389 int kernctl_tracer_abi_version(int fd
,
390 struct lttng_kernel_tracer_abi_version
*v
)
392 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_TRACER_ABI_VERSION
, v
);
395 int kernctl_wait_quiescent(int fd
)
397 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_WAIT_QUIESCENT
,
398 LTTNG_KERNEL_WAIT_QUIESCENT
);
401 int kernctl_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
405 if (lttng_kernel_use_old_abi
== -1) {
406 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
408 lttng_kernel_use_old_abi
= 0;
411 lttng_kernel_use_old_abi
= 1;
413 if (lttng_kernel_use_old_abi
) {
414 struct lttng_kernel_old_calibrate old_calibrate
;
416 old_calibrate
.type
= calibrate
->type
;
417 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_OLD_CALIBRATE
,
422 calibrate
->type
= old_calibrate
.type
;
424 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
432 int kernctl_buffer_flush(int fd
)
434 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_FLUSH
);
437 /* returns the version of the metadata. */
438 int kernctl_get_metadata_version(int fd
, uint64_t *version
)
440 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_METADATA_VERSION
, version
);
444 /* Buffer operations */
446 /* For mmap mode, readable without "get" operation */
448 /* returns the length to mmap. */
449 int kernctl_get_mmap_len(int fd
, unsigned long *len
)
451 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_MMAP_LEN
, len
);
454 /* returns the maximum size for sub-buffers. */
455 int kernctl_get_max_subbuf_size(int fd
, unsigned long *len
)
457 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_MAX_SUBBUF_SIZE
, len
);
461 * For mmap mode, operate on the current packet (between get/put or
462 * get_next/put_next).
465 /* returns the offset of the subbuffer belonging to the mmap reader. */
466 int kernctl_get_mmap_read_offset(int fd
, unsigned long *off
)
468 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_MMAP_READ_OFFSET
, off
);
471 /* returns the size of the current sub-buffer, without padding (for mmap). */
472 int kernctl_get_subbuf_size(int fd
, unsigned long *len
)
474 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_SUBBUF_SIZE
, len
);
477 /* returns the size of the current sub-buffer, without padding (for mmap). */
478 int kernctl_get_padded_subbuf_size(int fd
, unsigned long *len
)
480 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_PADDED_SUBBUF_SIZE
, len
);
483 /* Get exclusive read access to the next sub-buffer that can be read. */
484 int kernctl_get_next_subbuf(int fd
)
486 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_NEXT_SUBBUF
);
490 /* Release exclusive sub-buffer access, move consumer forward. */
491 int kernctl_put_next_subbuf(int fd
)
493 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_PUT_NEXT_SUBBUF
);
498 /* Get a snapshot of the current ring buffer producer and consumer positions */
499 int kernctl_snapshot(int fd
)
501 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_SNAPSHOT
);
504 /* Get the consumer position (iteration start) */
505 int kernctl_snapshot_get_consumed(int fd
, unsigned long *pos
)
507 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_SNAPSHOT_GET_CONSUMED
, pos
);
510 /* Get the producer position (iteration end) */
511 int kernctl_snapshot_get_produced(int fd
, unsigned long *pos
)
513 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_SNAPSHOT_GET_PRODUCED
, pos
);
516 /* Get exclusive read access to the specified sub-buffer position */
517 int kernctl_get_subbuf(int fd
, unsigned long *len
)
519 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_SUBBUF
, len
);
522 /* Release exclusive sub-buffer access */
523 int kernctl_put_subbuf(int fd
)
525 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_PUT_SUBBUF
);
528 /* Returns the timestamp begin of the current sub-buffer. */
529 int kernctl_get_timestamp_begin(int fd
, uint64_t *timestamp_begin
)
531 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
,
535 /* Returns the timestamp end of the current sub-buffer. */
536 int kernctl_get_timestamp_end(int fd
, uint64_t *timestamp_end
)
538 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_END
,
542 /* Returns the number of discarded events in the current sub-buffer. */
543 int kernctl_get_events_discarded(int fd
, uint64_t *events_discarded
)
545 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
,
549 /* Returns the content size in the current sub-buffer. */
550 int kernctl_get_content_size(int fd
, uint64_t *content_size
)
552 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_CONTENT_SIZE
,
556 /* Returns the packet size in the current sub-buffer. */
557 int kernctl_get_packet_size(int fd
, uint64_t *packet_size
)
559 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_PACKET_SIZE
,
563 /* Returns the stream id of the current sub-buffer. */
564 int kernctl_get_stream_id(int fd
, uint64_t *stream_id
)
566 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_STREAM_ID
,
570 /* Returns the current timestamp. */
571 int kernctl_get_current_timestamp(int fd
, uint64_t *ts
)
573 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
,
577 /* Returns the packet sequence number of the current sub-buffer. */
578 int kernctl_get_sequence_number(int fd
, uint64_t *seq
)
580 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_SEQ_NUM
, seq
);
583 /* Returns the stream instance id. */
584 int kernctl_get_instance_id(int fd
, uint64_t *id
)
586 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_INSTANCE_ID
, id
);