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_channel channel
;
89 if (lttng_kernel_use_old_abi
) {
90 struct lttng_kernel_old_channel old_channel
;
92 memset(&old_channel
, 0, sizeof(old_channel
));
93 old_channel
.overwrite
= chops
->overwrite
;
94 old_channel
.subbuf_size
= chops
->subbuf_size
;
95 old_channel
.num_subbuf
= chops
->num_subbuf
;
96 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
97 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
98 old_channel
.output
= chops
->output
;
100 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
102 * The new channel padding is smaller than the old ABI so we use the
103 * new ABI padding size for the memcpy.
105 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
107 return ioctl(fd
, LTTNG_KERNEL_OLD_METADATA
, &old_channel
);
110 memset(&channel
, 0, sizeof(channel
));
111 channel
.overwrite
= chops
->overwrite
;
112 channel
.subbuf_size
= chops
->subbuf_size
;
113 channel
.num_subbuf
= chops
->num_subbuf
;
114 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
115 channel
.read_timer_interval
= chops
->read_timer_interval
;
116 channel
.output
= chops
->output
;
117 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
119 return ioctl(fd
, LTTNG_KERNEL_METADATA
, &channel
);
122 int kernctl_create_channel(int fd
, struct lttng_channel_attr
*chops
)
124 struct lttng_kernel_channel channel
;
126 memset(&channel
, 0, sizeof(channel
));
127 if (lttng_kernel_use_old_abi
) {
128 struct lttng_kernel_old_channel old_channel
;
130 old_channel
.overwrite
= chops
->overwrite
;
131 old_channel
.subbuf_size
= chops
->subbuf_size
;
132 old_channel
.num_subbuf
= chops
->num_subbuf
;
133 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
134 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
135 old_channel
.output
= chops
->output
;
137 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
139 * The new channel padding is smaller than the old ABI so we use the
140 * new ABI padding size for the memcpy.
142 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
144 return ioctl(fd
, LTTNG_KERNEL_OLD_CHANNEL
, &old_channel
);
147 channel
.overwrite
= chops
->overwrite
;
148 channel
.subbuf_size
= chops
->subbuf_size
;
149 channel
.num_subbuf
= chops
->num_subbuf
;
150 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
151 channel
.read_timer_interval
= chops
->read_timer_interval
;
152 channel
.output
= chops
->output
;
153 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
155 return ioctl(fd
, LTTNG_KERNEL_CHANNEL
, &channel
);
158 int kernctl_syscall_mask(int fd
, char **syscall_mask
, uint32_t *nr_bits
)
160 struct lttng_kernel_syscall_mask kmask_len
, *kmask
= NULL
;
161 size_t array_alloc_len
;
176 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_SYSCALL_MASK
, &kmask_len
);
181 array_alloc_len
= ALIGN(kmask_len
.len
, 8) >> 3;
183 kmask
= zmalloc(sizeof(*kmask
) + array_alloc_len
);
189 kmask
->len
= kmask_len
.len
;
190 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_SYSCALL_MASK
, kmask
);
195 new_mask
= realloc(*syscall_mask
, array_alloc_len
);
200 memcpy(new_mask
, kmask
->mask
, array_alloc_len
);
201 *syscall_mask
= new_mask
;
202 *nr_bits
= kmask
->len
;
209 int kernctl_track_pid(int fd
, int pid
)
211 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_SESSION_TRACK_PID
, pid
);
214 int kernctl_untrack_pid(int fd
, int pid
)
216 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_SESSION_UNTRACK_PID
, pid
);
219 int kernctl_list_tracker_pids(int fd
)
221 return ioctl(fd
, LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
);
224 int kernctl_session_metadata_regenerate(int fd
)
226 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_SESSION_METADATA_REGEN
);
229 int kernctl_create_stream(int fd
)
231 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_STREAM
,
232 LTTNG_KERNEL_STREAM
);
235 int kernctl_create_event(int fd
, struct lttng_kernel_event
*ev
)
237 if (lttng_kernel_use_old_abi
) {
238 struct lttng_kernel_old_event old_event
;
240 memset(&old_event
, 0, sizeof(old_event
));
241 memcpy(old_event
.name
, ev
->name
, sizeof(old_event
.name
));
242 old_event
.instrumentation
= ev
->instrumentation
;
243 switch (ev
->instrumentation
) {
244 case LTTNG_KERNEL_KPROBE
:
245 old_event
.u
.kprobe
.addr
= ev
->u
.kprobe
.addr
;
246 old_event
.u
.kprobe
.offset
= ev
->u
.kprobe
.offset
;
247 memcpy(old_event
.u
.kprobe
.symbol_name
,
248 ev
->u
.kprobe
.symbol_name
,
249 sizeof(old_event
.u
.kprobe
.symbol_name
));
251 case LTTNG_KERNEL_KRETPROBE
:
252 old_event
.u
.kretprobe
.addr
= ev
->u
.kretprobe
.addr
;
253 old_event
.u
.kretprobe
.offset
= ev
->u
.kretprobe
.offset
;
254 memcpy(old_event
.u
.kretprobe
.symbol_name
,
255 ev
->u
.kretprobe
.symbol_name
,
256 sizeof(old_event
.u
.kretprobe
.symbol_name
));
258 case LTTNG_KERNEL_FUNCTION
:
259 memcpy(old_event
.u
.ftrace
.symbol_name
,
260 ev
->u
.ftrace
.symbol_name
,
261 sizeof(old_event
.u
.ftrace
.symbol_name
));
267 return ioctl(fd
, LTTNG_KERNEL_OLD_EVENT
, &old_event
);
269 return ioctl(fd
, LTTNG_KERNEL_EVENT
, ev
);
272 int kernctl_add_context(int fd
, struct lttng_kernel_context
*ctx
)
274 if (lttng_kernel_use_old_abi
) {
275 struct lttng_kernel_old_context old_ctx
;
277 memset(&old_ctx
, 0, sizeof(old_ctx
));
278 old_ctx
.ctx
= ctx
->ctx
;
279 /* only type that uses the union */
280 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
281 old_ctx
.u
.perf_counter
.type
=
282 ctx
->u
.perf_counter
.type
;
283 old_ctx
.u
.perf_counter
.config
=
284 ctx
->u
.perf_counter
.config
;
285 memcpy(old_ctx
.u
.perf_counter
.name
,
286 ctx
->u
.perf_counter
.name
,
287 sizeof(old_ctx
.u
.perf_counter
.name
));
289 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_OLD_CONTEXT
, &old_ctx
);
291 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_CONTEXT
, ctx
);
295 /* Enable event, channel and session LTTNG_IOCTL */
296 int kernctl_enable(int fd
)
298 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_ENABLE
,
299 LTTNG_KERNEL_ENABLE
);
302 /* Disable event, channel and session LTTNG_IOCTL */
303 int kernctl_disable(int fd
)
305 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_DISABLE
,
306 LTTNG_KERNEL_DISABLE
);
309 int kernctl_start_session(int fd
)
311 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_START
,
312 LTTNG_KERNEL_SESSION_START
);
315 int kernctl_stop_session(int fd
)
317 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_STOP
,
318 LTTNG_KERNEL_SESSION_STOP
);
321 int kernctl_filter(int fd
, struct lttng_filter_bytecode
*filter
)
323 struct lttng_kernel_filter_bytecode
*kb
;
327 /* Translate bytecode to kernel bytecode */
328 kb
= zmalloc(sizeof(*kb
) + filter
->len
);
331 kb
->len
= len
= filter
->len
;
332 kb
->reloc_offset
= filter
->reloc_table_offset
;
333 kb
->seqnum
= filter
->seqnum
;
334 memcpy(kb
->data
, filter
->data
, len
);
335 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_FILTER
, kb
);
340 int kernctl_tracepoint_list(int fd
)
342 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_TRACEPOINT_LIST
,
343 LTTNG_KERNEL_TRACEPOINT_LIST
);
346 int kernctl_syscall_list(int fd
)
348 return ioctl(fd
, LTTNG_KERNEL_SYSCALL_LIST
);
351 int kernctl_tracer_version(int fd
, struct lttng_kernel_tracer_version
*v
)
355 if (lttng_kernel_use_old_abi
== -1) {
356 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
358 lttng_kernel_use_old_abi
= 0;
361 lttng_kernel_use_old_abi
= 1;
363 if (lttng_kernel_use_old_abi
) {
364 struct lttng_kernel_old_tracer_version old_v
;
366 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_OLD_TRACER_VERSION
, &old_v
);
370 v
->major
= old_v
.major
;
371 v
->minor
= old_v
.minor
;
372 v
->patchlevel
= old_v
.patchlevel
;
374 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
381 int kernctl_tracer_abi_version(int fd
,
382 struct lttng_kernel_tracer_abi_version
*v
)
384 return LTTNG_IOCTL(fd
, LTTNG_KERNEL_TRACER_ABI_VERSION
, v
);
387 int kernctl_wait_quiescent(int fd
)
389 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_WAIT_QUIESCENT
,
390 LTTNG_KERNEL_WAIT_QUIESCENT
);
393 int kernctl_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
397 if (lttng_kernel_use_old_abi
== -1) {
398 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
400 lttng_kernel_use_old_abi
= 0;
403 lttng_kernel_use_old_abi
= 1;
405 if (lttng_kernel_use_old_abi
) {
406 struct lttng_kernel_old_calibrate old_calibrate
;
408 old_calibrate
.type
= calibrate
->type
;
409 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_OLD_CALIBRATE
,
414 calibrate
->type
= old_calibrate
.type
;
416 ret
= LTTNG_IOCTL(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
424 int kernctl_buffer_flush(int fd
)
426 return LTTNG_IOCTL(fd
, RING_BUFFER_FLUSH
);
429 int kernctl_buffer_flush_empty(int fd
)
431 return LTTNG_IOCTL(fd
, RING_BUFFER_FLUSH_EMPTY
);
434 /* returns the version of the metadata. */
435 int kernctl_get_metadata_version(int fd
, uint64_t *version
)
437 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_METADATA_VERSION
, version
);
441 /* Buffer operations */
443 /* For mmap mode, readable without "get" operation */
445 /* returns the length to mmap. */
446 int kernctl_get_mmap_len(int fd
, unsigned long *len
)
448 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_MMAP_LEN
, len
);
451 /* returns the maximum size for sub-buffers. */
452 int kernctl_get_max_subbuf_size(int fd
, unsigned long *len
)
454 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_MAX_SUBBUF_SIZE
, len
);
458 * For mmap mode, operate on the current packet (between get/put or
459 * get_next/put_next).
462 /* returns the offset of the subbuffer belonging to the mmap reader. */
463 int kernctl_get_mmap_read_offset(int fd
, unsigned long *off
)
465 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_MMAP_READ_OFFSET
, off
);
468 /* returns the size of the current sub-buffer, without padding (for mmap). */
469 int kernctl_get_subbuf_size(int fd
, unsigned long *len
)
471 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_SUBBUF_SIZE
, len
);
474 /* returns the size of the current sub-buffer, without padding (for mmap). */
475 int kernctl_get_padded_subbuf_size(int fd
, unsigned long *len
)
477 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_PADDED_SUBBUF_SIZE
, len
);
480 /* Get exclusive read access to the next sub-buffer that can be read. */
481 int kernctl_get_next_subbuf(int fd
)
483 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_NEXT_SUBBUF
);
487 /* Release exclusive sub-buffer access, move consumer forward. */
488 int kernctl_put_next_subbuf(int fd
)
490 return LTTNG_IOCTL(fd
, RING_BUFFER_PUT_NEXT_SUBBUF
);
495 /* Get a snapshot of the current ring buffer producer and consumer positions */
496 int kernctl_snapshot(int fd
)
498 return LTTNG_IOCTL(fd
, RING_BUFFER_SNAPSHOT
);
501 /* Get the consumer position (iteration start) */
502 int kernctl_snapshot_get_consumed(int fd
, unsigned long *pos
)
504 return LTTNG_IOCTL(fd
, RING_BUFFER_SNAPSHOT_GET_CONSUMED
, pos
);
507 /* Get the producer position (iteration end) */
508 int kernctl_snapshot_get_produced(int fd
, unsigned long *pos
)
510 return LTTNG_IOCTL(fd
, RING_BUFFER_SNAPSHOT_GET_PRODUCED
, pos
);
513 /* Get exclusive read access to the specified sub-buffer position */
514 int kernctl_get_subbuf(int fd
, unsigned long *len
)
516 return LTTNG_IOCTL(fd
, RING_BUFFER_GET_SUBBUF
, len
);
519 /* Release exclusive sub-buffer access */
520 int kernctl_put_subbuf(int fd
)
522 return LTTNG_IOCTL(fd
, RING_BUFFER_PUT_SUBBUF
);
525 /* Returns the timestamp begin of the current sub-buffer. */
526 int kernctl_get_timestamp_begin(int fd
, uint64_t *timestamp_begin
)
528 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
,
532 /* Returns the timestamp end of the current sub-buffer. */
533 int kernctl_get_timestamp_end(int fd
, uint64_t *timestamp_end
)
535 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_END
,
539 /* Returns the number of discarded events in the current sub-buffer. */
540 int kernctl_get_events_discarded(int fd
, uint64_t *events_discarded
)
542 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
,
546 /* Returns the content size in the current sub-buffer. */
547 int kernctl_get_content_size(int fd
, uint64_t *content_size
)
549 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_CONTENT_SIZE
,
553 /* Returns the packet size in the current sub-buffer. */
554 int kernctl_get_packet_size(int fd
, uint64_t *packet_size
)
556 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_PACKET_SIZE
, packet_size
);
559 /* Returns the stream id of the current sub-buffer. */
560 int kernctl_get_stream_id(int fd
, uint64_t *stream_id
)
562 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_STREAM_ID
, stream_id
);
565 /* Returns the current timestamp. */
566 int kernctl_get_current_timestamp(int fd
, uint64_t *ts
)
568 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
, ts
);
571 /* Returns the packet sequence number of the current sub-buffer. */
572 int kernctl_get_sequence_number(int fd
, uint64_t *seq
)
574 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_GET_SEQ_NUM
, seq
);
577 /* Returns the stream instance id. */
578 int kernctl_get_instance_id(int fd
, uint64_t *id
)
580 return LTTNG_IOCTL(fd
, LTTNG_RING_BUFFER_INSTANCE_ID
, id
);