2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #define __USE_LINUX_IOCTL_DEFS
20 #include <sys/ioctl.h>
22 #include <common/align.h>
24 #include "kernel-ctl.h"
25 #include "kernel-ioctl.h"
28 * This flag indicates which version of the kernel ABI to use. The old
29 * ABI (namespace _old) does not support a 32-bit user-space when the
30 * kernel is 64-bit. The old ABI is kept here for compatibility but is
31 * deprecated and will be removed eventually.
33 static int lttng_kernel_use_old_abi
= -1;
36 * Execute the new or old ioctl depending on the ABI version.
37 * If the ABI version is not determined yet (lttng_kernel_use_old_abi = -1),
38 * this function tests if the new ABI is available and otherwise fallbacks
40 * This function takes the fd on which the ioctl must be executed and the old
41 * and new request codes.
42 * It returns the return value of the ioctl executed.
44 static inline int compat_ioctl_no_arg(int fd
, unsigned long oldname
,
45 unsigned long newname
)
49 if (lttng_kernel_use_old_abi
== -1) {
50 ret
= ioctl(fd
, newname
);
52 lttng_kernel_use_old_abi
= 0;
55 lttng_kernel_use_old_abi
= 1;
57 if (lttng_kernel_use_old_abi
) {
58 ret
= ioctl(fd
, oldname
);
60 ret
= ioctl(fd
, newname
);
67 int kernctl_create_session(int fd
)
69 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION
,
70 LTTNG_KERNEL_SESSION
);
73 /* open the metadata global channel */
74 int kernctl_open_metadata(int fd
, struct lttng_channel_attr
*chops
)
76 struct lttng_kernel_old_channel old_channel
;
77 struct lttng_kernel_channel channel
;
79 if (lttng_kernel_use_old_abi
) {
80 old_channel
.overwrite
= chops
->overwrite
;
81 old_channel
.subbuf_size
= chops
->subbuf_size
;
82 old_channel
.num_subbuf
= chops
->num_subbuf
;
83 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
84 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
85 old_channel
.output
= chops
->output
;
87 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
89 * The new channel padding is smaller than the old ABI so we use the
90 * new ABI padding size for the memcpy.
92 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
94 return ioctl(fd
, LTTNG_KERNEL_OLD_METADATA
, &old_channel
);
97 channel
.overwrite
= chops
->overwrite
;
98 channel
.subbuf_size
= chops
->subbuf_size
;
99 channel
.num_subbuf
= chops
->num_subbuf
;
100 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
101 channel
.read_timer_interval
= chops
->read_timer_interval
;
102 channel
.output
= chops
->output
;
103 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
105 return ioctl(fd
, LTTNG_KERNEL_METADATA
, &channel
);
108 int kernctl_create_channel(int fd
, struct lttng_channel_attr
*chops
)
110 struct lttng_kernel_channel channel
;
112 if (lttng_kernel_use_old_abi
) {
113 struct lttng_kernel_old_channel old_channel
;
115 old_channel
.overwrite
= chops
->overwrite
;
116 old_channel
.subbuf_size
= chops
->subbuf_size
;
117 old_channel
.num_subbuf
= chops
->num_subbuf
;
118 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
119 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
120 old_channel
.output
= chops
->output
;
122 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
124 * The new channel padding is smaller than the old ABI so we use the
125 * new ABI padding size for the memcpy.
127 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
129 return ioctl(fd
, LTTNG_KERNEL_OLD_CHANNEL
, &old_channel
);
132 channel
.overwrite
= chops
->overwrite
;
133 channel
.subbuf_size
= chops
->subbuf_size
;
134 channel
.num_subbuf
= chops
->num_subbuf
;
135 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
136 channel
.read_timer_interval
= chops
->read_timer_interval
;
137 channel
.output
= chops
->output
;
138 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
140 return ioctl(fd
, LTTNG_KERNEL_CHANNEL
, &channel
);
143 int kernctl_enable_syscall(int fd
, const char *syscall_name
)
145 struct lttng_kernel_event event
;
147 memset(&event
, 0, sizeof(event
));
148 strncpy(event
.name
, syscall_name
, sizeof(event
.name
));
149 event
.name
[sizeof(event
.name
) - 1] = '\0';
150 event
.instrumentation
= LTTNG_KERNEL_SYSCALL
;
151 event
.u
.syscall
.enable
= 1;
152 return ioctl(fd
, LTTNG_KERNEL_EVENT
, &event
);
155 int kernctl_disable_syscall(int fd
, const char *syscall_name
)
157 struct lttng_kernel_event event
;
159 memset(&event
, 0, sizeof(event
));
160 strncpy(event
.name
, syscall_name
, sizeof(event
.name
));
161 event
.name
[sizeof(event
.name
) - 1] = '\0';
162 event
.instrumentation
= LTTNG_KERNEL_SYSCALL
;
163 event
.u
.syscall
.enable
= 0;
164 return ioctl(fd
, LTTNG_KERNEL_EVENT
, &event
);
167 int kernctl_syscall_mask(int fd
, char **syscall_mask
, uint32_t *nr_bits
)
169 struct lttng_kernel_syscall_mask kmask_len
, *kmask
= NULL
;
170 size_t array_alloc_len
;
185 ret
= ioctl(fd
, LTTNG_KERNEL_SYSCALL_MASK
, &kmask_len
);
190 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
= ioctl(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_create_stream(int fd
)
219 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_STREAM
,
220 LTTNG_KERNEL_STREAM
);
223 int kernctl_create_event(int fd
, struct lttng_kernel_event
*ev
)
225 if (lttng_kernel_use_old_abi
) {
226 struct lttng_kernel_old_event old_event
;
228 memcpy(old_event
.name
, ev
->name
, sizeof(old_event
.name
));
229 old_event
.instrumentation
= ev
->instrumentation
;
230 switch (ev
->instrumentation
) {
231 case LTTNG_KERNEL_KPROBE
:
232 old_event
.u
.kprobe
.addr
= ev
->u
.kprobe
.addr
;
233 old_event
.u
.kprobe
.offset
= ev
->u
.kprobe
.offset
;
234 memcpy(old_event
.u
.kprobe
.symbol_name
,
235 ev
->u
.kprobe
.symbol_name
,
236 sizeof(old_event
.u
.kprobe
.symbol_name
));
238 case LTTNG_KERNEL_KRETPROBE
:
239 old_event
.u
.kretprobe
.addr
= ev
->u
.kretprobe
.addr
;
240 old_event
.u
.kretprobe
.offset
= ev
->u
.kretprobe
.offset
;
241 memcpy(old_event
.u
.kretprobe
.symbol_name
,
242 ev
->u
.kretprobe
.symbol_name
,
243 sizeof(old_event
.u
.kretprobe
.symbol_name
));
245 case LTTNG_KERNEL_FUNCTION
:
246 memcpy(old_event
.u
.ftrace
.symbol_name
,
247 ev
->u
.ftrace
.symbol_name
,
248 sizeof(old_event
.u
.ftrace
.symbol_name
));
254 return ioctl(fd
, LTTNG_KERNEL_OLD_EVENT
, &old_event
);
256 return ioctl(fd
, LTTNG_KERNEL_EVENT
, ev
);
259 int kernctl_add_context(int fd
, struct lttng_kernel_context
*ctx
)
261 if (lttng_kernel_use_old_abi
) {
262 struct lttng_kernel_old_context old_ctx
;
264 old_ctx
.ctx
= ctx
->ctx
;
265 /* only type that uses the union */
266 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
267 old_ctx
.u
.perf_counter
.type
=
268 ctx
->u
.perf_counter
.type
;
269 old_ctx
.u
.perf_counter
.config
=
270 ctx
->u
.perf_counter
.config
;
271 memcpy(old_ctx
.u
.perf_counter
.name
,
272 ctx
->u
.perf_counter
.name
,
273 sizeof(old_ctx
.u
.perf_counter
.name
));
275 return ioctl(fd
, LTTNG_KERNEL_OLD_CONTEXT
, &old_ctx
);
277 return ioctl(fd
, LTTNG_KERNEL_CONTEXT
, ctx
);
281 /* Enable event, channel and session ioctl */
282 int kernctl_enable(int fd
)
284 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_ENABLE
,
285 LTTNG_KERNEL_ENABLE
);
288 /* Disable event, channel and session ioctl */
289 int kernctl_disable(int fd
)
291 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_DISABLE
,
292 LTTNG_KERNEL_DISABLE
);
295 int kernctl_start_session(int fd
)
297 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_START
,
298 LTTNG_KERNEL_SESSION_START
);
301 int kernctl_stop_session(int fd
)
303 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_STOP
,
304 LTTNG_KERNEL_SESSION_STOP
);
307 int kernctl_tracepoint_list(int fd
)
309 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_TRACEPOINT_LIST
,
310 LTTNG_KERNEL_TRACEPOINT_LIST
);
313 int kernctl_syscall_list(int fd
)
315 return ioctl(fd
, LTTNG_KERNEL_SYSCALL_LIST
);
318 int kernctl_tracer_version(int fd
, struct lttng_kernel_tracer_version
*v
)
322 if (lttng_kernel_use_old_abi
== -1) {
323 ret
= ioctl(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
325 lttng_kernel_use_old_abi
= 0;
328 lttng_kernel_use_old_abi
= 1;
330 if (lttng_kernel_use_old_abi
) {
331 struct lttng_kernel_old_tracer_version old_v
;
333 ret
= ioctl(fd
, LTTNG_KERNEL_OLD_TRACER_VERSION
, &old_v
);
337 v
->major
= old_v
.major
;
338 v
->minor
= old_v
.minor
;
339 v
->patchlevel
= old_v
.patchlevel
;
341 ret
= ioctl(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
348 int kernctl_wait_quiescent(int fd
)
350 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_WAIT_QUIESCENT
,
351 LTTNG_KERNEL_WAIT_QUIESCENT
);
354 int kernctl_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
358 if (lttng_kernel_use_old_abi
== -1) {
359 ret
= ioctl(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
361 lttng_kernel_use_old_abi
= 0;
364 lttng_kernel_use_old_abi
= 1;
366 if (lttng_kernel_use_old_abi
) {
367 struct lttng_kernel_old_calibrate old_calibrate
;
369 old_calibrate
.type
= calibrate
->type
;
370 ret
= ioctl(fd
, LTTNG_KERNEL_OLD_CALIBRATE
, &old_calibrate
);
374 calibrate
->type
= old_calibrate
.type
;
376 ret
= ioctl(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
384 int kernctl_buffer_flush(int fd
)
386 return ioctl(fd
, RING_BUFFER_FLUSH
);
390 /* Buffer operations */
392 /* For mmap mode, readable without "get" operation */
394 /* returns the length to mmap. */
395 int kernctl_get_mmap_len(int fd
, unsigned long *len
)
397 return ioctl(fd
, RING_BUFFER_GET_MMAP_LEN
, len
);
400 /* returns the maximum size for sub-buffers. */
401 int kernctl_get_max_subbuf_size(int fd
, unsigned long *len
)
403 return ioctl(fd
, RING_BUFFER_GET_MAX_SUBBUF_SIZE
, len
);
407 * For mmap mode, operate on the current packet (between get/put or
408 * get_next/put_next).
411 /* returns the offset of the subbuffer belonging to the mmap reader. */
412 int kernctl_get_mmap_read_offset(int fd
, unsigned long *off
)
414 return ioctl(fd
, RING_BUFFER_GET_MMAP_READ_OFFSET
, off
);
417 /* returns the size of the current sub-buffer, without padding (for mmap). */
418 int kernctl_get_subbuf_size(int fd
, unsigned long *len
)
420 return ioctl(fd
, RING_BUFFER_GET_SUBBUF_SIZE
, len
);
423 /* returns the size of the current sub-buffer, without padding (for mmap). */
424 int kernctl_get_padded_subbuf_size(int fd
, unsigned long *len
)
426 return ioctl(fd
, RING_BUFFER_GET_PADDED_SUBBUF_SIZE
, len
);
429 /* Get exclusive read access to the next sub-buffer that can be read. */
430 int kernctl_get_next_subbuf(int fd
)
432 return ioctl(fd
, RING_BUFFER_GET_NEXT_SUBBUF
);
436 /* Release exclusive sub-buffer access, move consumer forward. */
437 int kernctl_put_next_subbuf(int fd
)
439 return ioctl(fd
, RING_BUFFER_PUT_NEXT_SUBBUF
);
444 /* Get a snapshot of the current ring buffer producer and consumer positions */
445 int kernctl_snapshot(int fd
)
447 return ioctl(fd
, RING_BUFFER_SNAPSHOT
);
450 /* Get the consumer position (iteration start) */
451 int kernctl_snapshot_get_consumed(int fd
, unsigned long *pos
)
453 return ioctl(fd
, RING_BUFFER_SNAPSHOT_GET_CONSUMED
, pos
);
456 /* Get the producer position (iteration end) */
457 int kernctl_snapshot_get_produced(int fd
, unsigned long *pos
)
459 return ioctl(fd
, RING_BUFFER_SNAPSHOT_GET_PRODUCED
, pos
);
462 /* Get exclusive read access to the specified sub-buffer position */
463 int kernctl_get_subbuf(int fd
, unsigned long *len
)
465 return ioctl(fd
, RING_BUFFER_GET_SUBBUF
, len
);
468 /* Release exclusive sub-buffer access */
469 int kernctl_put_subbuf(int fd
)
471 return ioctl(fd
, RING_BUFFER_PUT_SUBBUF
);
474 /* Returns the timestamp begin of the current sub-buffer. */
475 int kernctl_get_timestamp_begin(int fd
, uint64_t *timestamp_begin
)
477 return ioctl(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
, timestamp_begin
);
480 /* Returns the timestamp end of the current sub-buffer. */
481 int kernctl_get_timestamp_end(int fd
, uint64_t *timestamp_end
)
483 return ioctl(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_END
, timestamp_end
);
486 /* Returns the number of discarded events in the current sub-buffer. */
487 int kernctl_get_events_discarded(int fd
, uint64_t *events_discarded
)
489 return ioctl(fd
, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
, events_discarded
);
492 /* Returns the content size in the current sub-buffer. */
493 int kernctl_get_content_size(int fd
, uint64_t *content_size
)
495 return ioctl(fd
, LTTNG_RING_BUFFER_GET_CONTENT_SIZE
, content_size
);
498 /* Returns the packet size in the current sub-buffer. */
499 int kernctl_get_packet_size(int fd
, uint64_t *packet_size
)
501 return ioctl(fd
, LTTNG_RING_BUFFER_GET_PACKET_SIZE
, packet_size
);
504 /* Returns the stream id of the current sub-buffer. */
505 int kernctl_get_stream_id(int fd
, uint64_t *stream_id
)
507 return ioctl(fd
, LTTNG_RING_BUFFER_GET_STREAM_ID
, stream_id
);
510 /* Returns the current timestamp. */
511 int kernctl_get_current_timestamp(int fd
, uint64_t *ts
)
513 return ioctl(fd
, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
, ts
);