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>
28 #include <common/time.h>
30 #include "kernel-ctl.h"
31 #include "kernel-ioctl.h"
33 #define LTTNG_IOCTL_CHECK(fildes, request, ...) ({ \
34 int ret = ioctl(fildes, request, ##__VA_ARGS__);\
39 #define LTTNG_IOCTL_NO_CHECK(fildes, request, ...) ({ \
40 int ret = ioctl(fildes, request, ##__VA_ARGS__);\
41 ret >= 0 ? ret : -errno; \
45 * This flag indicates which version of the kernel ABI to use. The old
46 * ABI (namespace _old) does not support a 32-bit user-space when the
47 * kernel is 64-bit. The old ABI is kept here for compatibility but is
48 * deprecated and will be removed eventually.
50 static int lttng_kernel_use_old_abi
= -1;
53 * Execute the new or old ioctl depending on the ABI version.
54 * If the ABI version is not determined yet (lttng_kernel_use_old_abi = -1),
55 * this function tests if the new ABI is available and otherwise fallbacks
57 * This function takes the fd on which the ioctl must be executed and the old
58 * and new request codes.
59 * It returns the return value of the ioctl executed.
61 static inline int compat_ioctl_no_arg(int fd
, unsigned long oldname
,
62 unsigned long newname
)
66 if (lttng_kernel_use_old_abi
== -1) {
67 ret
= LTTNG_IOCTL_NO_CHECK(fd
, newname
);
69 lttng_kernel_use_old_abi
= 0;
72 lttng_kernel_use_old_abi
= 1;
74 if (lttng_kernel_use_old_abi
) {
75 ret
= LTTNG_IOCTL_NO_CHECK(fd
, oldname
);
77 ret
= LTTNG_IOCTL_NO_CHECK(fd
, newname
);
84 int kernctl_create_session(int fd
)
86 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION
,
87 LTTNG_KERNEL_SESSION
);
90 /* open the metadata global channel */
91 int kernctl_open_metadata(int fd
, struct lttng_channel_attr
*chops
)
93 struct lttng_kernel_channel channel
;
95 if (lttng_kernel_use_old_abi
) {
96 struct lttng_kernel_old_channel old_channel
;
98 memset(&old_channel
, 0, sizeof(old_channel
));
99 old_channel
.overwrite
= chops
->overwrite
;
100 old_channel
.subbuf_size
= chops
->subbuf_size
;
101 old_channel
.num_subbuf
= chops
->num_subbuf
;
102 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
103 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
104 old_channel
.output
= chops
->output
;
106 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
108 * The new channel padding is smaller than the old ABI so we use the
109 * new ABI padding size for the memcpy.
111 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
113 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_OLD_METADATA
,
117 memset(&channel
, 0, sizeof(channel
));
118 channel
.overwrite
= chops
->overwrite
;
119 channel
.subbuf_size
= chops
->subbuf_size
;
120 channel
.num_subbuf
= chops
->num_subbuf
;
121 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
122 channel
.read_timer_interval
= chops
->read_timer_interval
;
123 channel
.output
= chops
->output
;
124 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
126 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_METADATA
, &channel
);
129 int kernctl_create_channel(int fd
, struct lttng_channel_attr
*chops
)
131 struct lttng_kernel_channel channel
;
133 memset(&channel
, 0, sizeof(channel
));
134 if (lttng_kernel_use_old_abi
) {
135 struct lttng_kernel_old_channel old_channel
;
137 old_channel
.overwrite
= chops
->overwrite
;
138 old_channel
.subbuf_size
= chops
->subbuf_size
;
139 old_channel
.num_subbuf
= chops
->num_subbuf
;
140 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
141 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
142 old_channel
.output
= chops
->output
;
144 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
146 * The new channel padding is smaller than the old ABI so we use the
147 * new ABI padding size for the memcpy.
149 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
151 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_OLD_CHANNEL
,
155 channel
.overwrite
= chops
->overwrite
;
156 channel
.subbuf_size
= chops
->subbuf_size
;
157 channel
.num_subbuf
= chops
->num_subbuf
;
158 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
159 channel
.read_timer_interval
= chops
->read_timer_interval
;
160 channel
.output
= chops
->output
;
161 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
163 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_CHANNEL
, &channel
);
166 int kernctl_syscall_mask(int fd
, char **syscall_mask
, uint32_t *nr_bits
)
168 struct lttng_kernel_syscall_mask kmask_len
, *kmask
= NULL
;
169 size_t array_alloc_len
;
184 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SYSCALL_MASK
, &kmask_len
);
189 array_alloc_len
= ALIGN(kmask_len
.len
, 8) >> 3;
191 kmask
= zmalloc(sizeof(*kmask
) + array_alloc_len
);
197 kmask
->len
= kmask_len
.len
;
198 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SYSCALL_MASK
, kmask
);
203 new_mask
= realloc(*syscall_mask
, array_alloc_len
);
208 memcpy(new_mask
, kmask
->mask
, array_alloc_len
);
209 *syscall_mask
= new_mask
;
210 *nr_bits
= kmask
->len
;
217 int kernctl_track_pid(int fd
, int pid
)
219 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_TRACK_PID
, pid
);
222 int kernctl_untrack_pid(int fd
, int pid
)
224 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_UNTRACK_PID
, pid
);
227 int kernctl_list_tracker_pids(int fd
)
229 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS
);
232 int kernctl_session_regenerate_metadata(int fd
)
234 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_METADATA_REGEN
);
237 int kernctl_session_regenerate_statedump(int fd
)
239 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_STATEDUMP
);
242 int kernctl_session_set_name(int fd
, const char *name
)
244 struct lttng_kernel_session_name session_name
;
246 strncpy(session_name
.name
, name
, LTTNG_KERNEL_SESSION_NAME_LEN
);
248 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_SET_NAME
,
252 int kernctl_session_set_creation_time(int fd
, time_t time
)
255 struct lttng_kernel_session_creation_time creation_time
;
257 ret
= time_t_to_ISO8601(creation_time
.iso8601
, sizeof(creation_time
.iso8601
), time
);
262 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_SESSION_SET_CREATION_TIME
,
266 int kernctl_create_stream(int fd
)
268 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_STREAM
,
269 LTTNG_KERNEL_STREAM
);
272 int kernctl_create_event(int fd
, struct lttng_kernel_event
*ev
)
274 if (lttng_kernel_use_old_abi
) {
275 struct lttng_kernel_old_event old_event
;
277 memset(&old_event
, 0, sizeof(old_event
));
278 memcpy(old_event
.name
, ev
->name
, sizeof(old_event
.name
));
279 old_event
.instrumentation
= ev
->instrumentation
;
280 switch (ev
->instrumentation
) {
281 case LTTNG_KERNEL_KPROBE
:
282 old_event
.u
.kprobe
.addr
= ev
->u
.kprobe
.addr
;
283 old_event
.u
.kprobe
.offset
= ev
->u
.kprobe
.offset
;
284 memcpy(old_event
.u
.kprobe
.symbol_name
,
285 ev
->u
.kprobe
.symbol_name
,
286 sizeof(old_event
.u
.kprobe
.symbol_name
));
288 case LTTNG_KERNEL_KRETPROBE
:
289 old_event
.u
.kretprobe
.addr
= ev
->u
.kretprobe
.addr
;
290 old_event
.u
.kretprobe
.offset
= ev
->u
.kretprobe
.offset
;
291 memcpy(old_event
.u
.kretprobe
.symbol_name
,
292 ev
->u
.kretprobe
.symbol_name
,
293 sizeof(old_event
.u
.kretprobe
.symbol_name
));
295 case LTTNG_KERNEL_FUNCTION
:
296 memcpy(old_event
.u
.ftrace
.symbol_name
,
297 ev
->u
.ftrace
.symbol_name
,
298 sizeof(old_event
.u
.ftrace
.symbol_name
));
304 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_OLD_EVENT
,
307 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_EVENT
, ev
);
310 int kernctl_add_context(int fd
, struct lttng_kernel_context
*ctx
)
312 if (lttng_kernel_use_old_abi
) {
313 struct lttng_kernel_old_context old_ctx
;
315 memset(&old_ctx
, 0, sizeof(old_ctx
));
316 old_ctx
.ctx
= ctx
->ctx
;
317 /* only type that uses the union */
318 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
319 old_ctx
.u
.perf_counter
.type
=
320 ctx
->u
.perf_counter
.type
;
321 old_ctx
.u
.perf_counter
.config
=
322 ctx
->u
.perf_counter
.config
;
323 memcpy(old_ctx
.u
.perf_counter
.name
,
324 ctx
->u
.perf_counter
.name
,
325 sizeof(old_ctx
.u
.perf_counter
.name
));
327 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_OLD_CONTEXT
, &old_ctx
);
329 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_CONTEXT
, ctx
);
333 /* Enable event, channel and session LTTNG_IOCTL_CHECK */
334 int kernctl_enable(int fd
)
336 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_ENABLE
,
337 LTTNG_KERNEL_ENABLE
);
340 /* Disable event, channel and session LTTNG_IOCTL_CHECK */
341 int kernctl_disable(int fd
)
343 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_DISABLE
,
344 LTTNG_KERNEL_DISABLE
);
347 int kernctl_start_session(int fd
)
349 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_START
,
350 LTTNG_KERNEL_SESSION_START
);
353 int kernctl_stop_session(int fd
)
355 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_STOP
,
356 LTTNG_KERNEL_SESSION_STOP
);
359 int kernctl_filter(int fd
, struct lttng_filter_bytecode
*filter
)
361 struct lttng_kernel_filter_bytecode
*kb
;
365 /* Translate bytecode to kernel bytecode */
366 kb
= zmalloc(sizeof(*kb
) + filter
->len
);
369 kb
->len
= len
= filter
->len
;
370 kb
->reloc_offset
= filter
->reloc_table_offset
;
371 kb
->seqnum
= filter
->seqnum
;
372 memcpy(kb
->data
, filter
->data
, len
);
373 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_FILTER
, kb
);
378 int kernctl_add_callsite(int fd
, struct lttng_kernel_event_callsite
*callsite
)
380 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_ADD_CALLSITE
, callsite
);
383 int kernctl_tracepoint_list(int fd
)
385 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_TRACEPOINT_LIST
,
386 LTTNG_KERNEL_TRACEPOINT_LIST
);
389 int kernctl_syscall_list(int fd
)
391 return LTTNG_IOCTL_NO_CHECK(fd
, LTTNG_KERNEL_SYSCALL_LIST
);
394 int kernctl_tracer_version(int fd
, struct lttng_kernel_tracer_version
*v
)
398 if (lttng_kernel_use_old_abi
== -1) {
399 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
401 lttng_kernel_use_old_abi
= 0;
404 lttng_kernel_use_old_abi
= 1;
406 if (lttng_kernel_use_old_abi
) {
407 struct lttng_kernel_old_tracer_version old_v
;
409 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_OLD_TRACER_VERSION
, &old_v
);
413 v
->major
= old_v
.major
;
414 v
->minor
= old_v
.minor
;
415 v
->patchlevel
= old_v
.patchlevel
;
417 ret
= LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
424 int kernctl_tracer_abi_version(int fd
,
425 struct lttng_kernel_tracer_abi_version
*v
)
427 return LTTNG_IOCTL_CHECK(fd
, LTTNG_KERNEL_TRACER_ABI_VERSION
, v
);
430 int kernctl_wait_quiescent(int fd
)
432 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_WAIT_QUIESCENT
,
433 LTTNG_KERNEL_WAIT_QUIESCENT
);
436 int kernctl_buffer_flush(int fd
)
438 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_FLUSH
);
441 int kernctl_buffer_flush_empty(int fd
)
443 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_FLUSH_EMPTY
);
446 /* returns the version of the metadata. */
447 int kernctl_get_metadata_version(int fd
, uint64_t *version
)
449 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_METADATA_VERSION
, version
);
452 int kernctl_metadata_cache_dump(int fd
)
454 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_METADATA_CACHE_DUMP
);
457 /* Buffer operations */
459 /* For mmap mode, readable without "get" operation */
461 /* returns the length to mmap. */
462 int kernctl_get_mmap_len(int fd
, unsigned long *len
)
464 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_MMAP_LEN
, len
);
467 /* returns the maximum size for sub-buffers. */
468 int kernctl_get_max_subbuf_size(int fd
, unsigned long *len
)
470 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_MAX_SUBBUF_SIZE
, len
);
474 * For mmap mode, operate on the current packet (between get/put or
475 * get_next/put_next).
478 /* returns the offset of the subbuffer belonging to the mmap reader. */
479 int kernctl_get_mmap_read_offset(int fd
, unsigned long *off
)
481 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_MMAP_READ_OFFSET
, off
);
484 /* returns the size of the current sub-buffer, without padding (for mmap). */
485 int kernctl_get_subbuf_size(int fd
, unsigned long *len
)
487 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_SUBBUF_SIZE
, len
);
490 /* returns the size of the current sub-buffer, without padding (for mmap). */
491 int kernctl_get_padded_subbuf_size(int fd
, unsigned long *len
)
493 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_PADDED_SUBBUF_SIZE
, len
);
496 /* Get exclusive read access to the next sub-buffer that can be read. */
497 int kernctl_get_next_subbuf(int fd
)
499 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_NEXT_SUBBUF
);
503 /* Release exclusive sub-buffer access, move consumer forward. */
504 int kernctl_put_next_subbuf(int fd
)
506 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_PUT_NEXT_SUBBUF
);
511 /* Get a snapshot of the current ring buffer producer and consumer positions */
512 int kernctl_snapshot(int fd
)
514 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_SNAPSHOT
);
518 * Get a snapshot of the current ring buffer producer and consumer positions,
519 * regardless of whether or not the two positions are contained within the
522 int kernctl_snapshot_sample_positions(int fd
)
524 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS
);
527 /* Get the consumer position (iteration start) */
528 int kernctl_snapshot_get_consumed(int fd
, unsigned long *pos
)
530 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_SNAPSHOT_GET_CONSUMED
, pos
);
533 /* Get the producer position (iteration end) */
534 int kernctl_snapshot_get_produced(int fd
, unsigned long *pos
)
536 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_SNAPSHOT_GET_PRODUCED
, pos
);
539 /* Get exclusive read access to the specified sub-buffer position */
540 int kernctl_get_subbuf(int fd
, unsigned long *len
)
542 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_GET_SUBBUF
, len
);
545 /* Release exclusive sub-buffer access */
546 int kernctl_put_subbuf(int fd
)
548 return LTTNG_IOCTL_CHECK(fd
, RING_BUFFER_PUT_SUBBUF
);
551 /* Returns the timestamp begin of the current sub-buffer. */
552 int kernctl_get_timestamp_begin(int fd
, uint64_t *timestamp_begin
)
554 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
,
558 /* Returns the timestamp end of the current sub-buffer. */
559 int kernctl_get_timestamp_end(int fd
, uint64_t *timestamp_end
)
561 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_END
,
565 /* Returns the number of discarded events in the current sub-buffer. */
566 int kernctl_get_events_discarded(int fd
, uint64_t *events_discarded
)
568 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
,
572 /* Returns the content size in the current sub-buffer. */
573 int kernctl_get_content_size(int fd
, uint64_t *content_size
)
575 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_CONTENT_SIZE
,
579 /* Returns the packet size in the current sub-buffer. */
580 int kernctl_get_packet_size(int fd
, uint64_t *packet_size
)
582 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_PACKET_SIZE
,
586 /* Returns the stream id of the current sub-buffer. */
587 int kernctl_get_stream_id(int fd
, uint64_t *stream_id
)
589 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_STREAM_ID
,
593 /* Returns the current timestamp. */
594 int kernctl_get_current_timestamp(int fd
, uint64_t *ts
)
596 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
,
600 /* Returns the packet sequence number of the current sub-buffer. */
601 int kernctl_get_sequence_number(int fd
, uint64_t *seq
)
603 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_GET_SEQ_NUM
, seq
);
606 /* Returns the stream instance id. */
607 int kernctl_get_instance_id(int fd
, uint64_t *id
)
609 return LTTNG_IOCTL_CHECK(fd
, LTTNG_RING_BUFFER_INSTANCE_ID
, id
);