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.
21 #define __USE_LINUX_IOCTL_DEFS
22 #include <sys/ioctl.h>
24 #include <common/align.h>
26 #include "kernel-ctl.h"
27 #include "kernel-ioctl.h"
30 * This flag indicates which version of the kernel ABI to use. The old
31 * ABI (namespace _old) does not support a 32-bit user-space when the
32 * kernel is 64-bit. The old ABI is kept here for compatibility but is
33 * deprecated and will be removed eventually.
35 static int lttng_kernel_use_old_abi
= -1;
38 * Execute the new or old ioctl depending on the ABI version.
39 * If the ABI version is not determined yet (lttng_kernel_use_old_abi = -1),
40 * this function tests if the new ABI is available and otherwise fallbacks
42 * This function takes the fd on which the ioctl must be executed and the old
43 * and new request codes.
44 * It returns the return value of the ioctl executed.
46 static inline int compat_ioctl_no_arg(int fd
, unsigned long oldname
,
47 unsigned long newname
)
51 if (lttng_kernel_use_old_abi
== -1) {
52 ret
= ioctl(fd
, newname
);
54 lttng_kernel_use_old_abi
= 0;
57 lttng_kernel_use_old_abi
= 1;
59 if (lttng_kernel_use_old_abi
) {
60 ret
= ioctl(fd
, oldname
);
62 ret
= ioctl(fd
, newname
);
69 int kernctl_create_session(int fd
)
71 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION
,
72 LTTNG_KERNEL_SESSION
);
75 /* open the metadata global channel */
76 int kernctl_open_metadata(int fd
, struct lttng_channel_attr
*chops
)
78 struct lttng_kernel_old_channel old_channel
;
79 struct lttng_kernel_channel channel
;
81 if (lttng_kernel_use_old_abi
) {
82 old_channel
.overwrite
= chops
->overwrite
;
83 old_channel
.subbuf_size
= chops
->subbuf_size
;
84 old_channel
.num_subbuf
= chops
->num_subbuf
;
85 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
86 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
87 old_channel
.output
= chops
->output
;
89 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
91 * The new channel padding is smaller than the old ABI so we use the
92 * new ABI padding size for the memcpy.
94 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
96 return ioctl(fd
, LTTNG_KERNEL_OLD_METADATA
, &old_channel
);
99 channel
.overwrite
= chops
->overwrite
;
100 channel
.subbuf_size
= chops
->subbuf_size
;
101 channel
.num_subbuf
= chops
->num_subbuf
;
102 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
103 channel
.read_timer_interval
= chops
->read_timer_interval
;
104 channel
.output
= chops
->output
;
105 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
107 return ioctl(fd
, LTTNG_KERNEL_METADATA
, &channel
);
110 int kernctl_create_channel(int fd
, struct lttng_channel_attr
*chops
)
112 struct lttng_kernel_channel channel
;
114 if (lttng_kernel_use_old_abi
) {
115 struct lttng_kernel_old_channel old_channel
;
117 old_channel
.overwrite
= chops
->overwrite
;
118 old_channel
.subbuf_size
= chops
->subbuf_size
;
119 old_channel
.num_subbuf
= chops
->num_subbuf
;
120 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
121 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
122 old_channel
.output
= chops
->output
;
124 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
126 * The new channel padding is smaller than the old ABI so we use the
127 * new ABI padding size for the memcpy.
129 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
131 return ioctl(fd
, LTTNG_KERNEL_OLD_CHANNEL
, &old_channel
);
134 channel
.overwrite
= chops
->overwrite
;
135 channel
.subbuf_size
= chops
->subbuf_size
;
136 channel
.num_subbuf
= chops
->num_subbuf
;
137 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
138 channel
.read_timer_interval
= chops
->read_timer_interval
;
139 channel
.output
= chops
->output
;
140 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
142 return ioctl(fd
, LTTNG_KERNEL_CHANNEL
, &channel
);
145 int kernctl_enable_syscall(int fd
, const char *syscall_name
)
147 struct lttng_kernel_event event
;
149 memset(&event
, 0, sizeof(event
));
150 strncpy(event
.name
, syscall_name
, sizeof(event
.name
));
151 event
.name
[sizeof(event
.name
) - 1] = '\0';
152 event
.instrumentation
= LTTNG_KERNEL_SYSCALL
;
153 event
.u
.syscall
.enable
= 1;
154 return ioctl(fd
, LTTNG_KERNEL_EVENT
, &event
);
157 int kernctl_disable_syscall(int fd
, const char *syscall_name
)
159 struct lttng_kernel_event event
;
161 memset(&event
, 0, sizeof(event
));
162 strncpy(event
.name
, syscall_name
, sizeof(event
.name
));
163 event
.name
[sizeof(event
.name
) - 1] = '\0';
164 event
.instrumentation
= LTTNG_KERNEL_SYSCALL
;
165 event
.u
.syscall
.enable
= 0;
166 return ioctl(fd
, LTTNG_KERNEL_EVENT
, &event
);
169 int kernctl_syscall_mask(int fd
, char **syscall_mask
, uint32_t *nr_bits
)
171 struct lttng_kernel_syscall_mask kmask_len
, *kmask
= NULL
;
172 size_t array_alloc_len
;
187 ret
= ioctl(fd
, LTTNG_KERNEL_SYSCALL_MASK
, &kmask_len
);
192 array_alloc_len
= ALIGN(kmask_len
.len
, 8) >> 3;
194 kmask
= zmalloc(sizeof(*kmask
) + array_alloc_len
);
200 kmask
->len
= kmask_len
.len
;
201 ret
= ioctl(fd
, LTTNG_KERNEL_SYSCALL_MASK
, kmask
);
206 new_mask
= realloc(*syscall_mask
, array_alloc_len
);
211 memcpy(new_mask
, kmask
->mask
, array_alloc_len
);
212 *syscall_mask
= new_mask
;
213 *nr_bits
= kmask
->len
;
220 int kernctl_create_stream(int fd
)
222 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_STREAM
,
223 LTTNG_KERNEL_STREAM
);
226 int kernctl_create_event(int fd
, struct lttng_kernel_event
*ev
)
228 if (lttng_kernel_use_old_abi
) {
229 struct lttng_kernel_old_event old_event
;
231 memcpy(old_event
.name
, ev
->name
, sizeof(old_event
.name
));
232 old_event
.instrumentation
= ev
->instrumentation
;
233 switch (ev
->instrumentation
) {
234 case LTTNG_KERNEL_KPROBE
:
235 old_event
.u
.kprobe
.addr
= ev
->u
.kprobe
.addr
;
236 old_event
.u
.kprobe
.offset
= ev
->u
.kprobe
.offset
;
237 memcpy(old_event
.u
.kprobe
.symbol_name
,
238 ev
->u
.kprobe
.symbol_name
,
239 sizeof(old_event
.u
.kprobe
.symbol_name
));
241 case LTTNG_KERNEL_KRETPROBE
:
242 old_event
.u
.kretprobe
.addr
= ev
->u
.kretprobe
.addr
;
243 old_event
.u
.kretprobe
.offset
= ev
->u
.kretprobe
.offset
;
244 memcpy(old_event
.u
.kretprobe
.symbol_name
,
245 ev
->u
.kretprobe
.symbol_name
,
246 sizeof(old_event
.u
.kretprobe
.symbol_name
));
248 case LTTNG_KERNEL_FUNCTION
:
249 memcpy(old_event
.u
.ftrace
.symbol_name
,
250 ev
->u
.ftrace
.symbol_name
,
251 sizeof(old_event
.u
.ftrace
.symbol_name
));
257 return ioctl(fd
, LTTNG_KERNEL_OLD_EVENT
, &old_event
);
259 return ioctl(fd
, LTTNG_KERNEL_EVENT
, ev
);
262 int kernctl_add_context(int fd
, struct lttng_kernel_context
*ctx
)
264 if (lttng_kernel_use_old_abi
) {
265 struct lttng_kernel_old_context old_ctx
;
267 old_ctx
.ctx
= ctx
->ctx
;
268 /* only type that uses the union */
269 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
) {
270 old_ctx
.u
.perf_counter
.type
=
271 ctx
->u
.perf_counter
.type
;
272 old_ctx
.u
.perf_counter
.config
=
273 ctx
->u
.perf_counter
.config
;
274 memcpy(old_ctx
.u
.perf_counter
.name
,
275 ctx
->u
.perf_counter
.name
,
276 sizeof(old_ctx
.u
.perf_counter
.name
));
278 return ioctl(fd
, LTTNG_KERNEL_OLD_CONTEXT
, &old_ctx
);
280 return ioctl(fd
, LTTNG_KERNEL_CONTEXT
, ctx
);
284 /* Enable event, channel and session ioctl */
285 int kernctl_enable(int fd
)
287 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_ENABLE
,
288 LTTNG_KERNEL_ENABLE
);
291 /* Disable event, channel and session ioctl */
292 int kernctl_disable(int fd
)
294 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_DISABLE
,
295 LTTNG_KERNEL_DISABLE
);
298 int kernctl_start_session(int fd
)
300 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_START
,
301 LTTNG_KERNEL_SESSION_START
);
304 int kernctl_stop_session(int fd
)
306 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_STOP
,
307 LTTNG_KERNEL_SESSION_STOP
);
310 int kernctl_tracepoint_list(int fd
)
312 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_TRACEPOINT_LIST
,
313 LTTNG_KERNEL_TRACEPOINT_LIST
);
316 int kernctl_syscall_list(int fd
)
318 return ioctl(fd
, LTTNG_KERNEL_SYSCALL_LIST
);
321 int kernctl_tracer_version(int fd
, struct lttng_kernel_tracer_version
*v
)
325 if (lttng_kernel_use_old_abi
== -1) {
326 ret
= ioctl(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
328 lttng_kernel_use_old_abi
= 0;
331 lttng_kernel_use_old_abi
= 1;
333 if (lttng_kernel_use_old_abi
) {
334 struct lttng_kernel_old_tracer_version old_v
;
336 ret
= ioctl(fd
, LTTNG_KERNEL_OLD_TRACER_VERSION
, &old_v
);
340 v
->major
= old_v
.major
;
341 v
->minor
= old_v
.minor
;
342 v
->patchlevel
= old_v
.patchlevel
;
344 ret
= ioctl(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
351 int kernctl_tracer_abi_version(int fd
,
352 struct lttng_kernel_tracer_abi_version
*v
)
354 return ioctl(fd
, LTTNG_KERNEL_TRACER_ABI_VERSION
, v
);
357 int kernctl_wait_quiescent(int fd
)
359 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_WAIT_QUIESCENT
,
360 LTTNG_KERNEL_WAIT_QUIESCENT
);
363 int kernctl_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
367 if (lttng_kernel_use_old_abi
== -1) {
368 ret
= ioctl(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
370 lttng_kernel_use_old_abi
= 0;
373 lttng_kernel_use_old_abi
= 1;
375 if (lttng_kernel_use_old_abi
) {
376 struct lttng_kernel_old_calibrate old_calibrate
;
378 old_calibrate
.type
= calibrate
->type
;
379 ret
= ioctl(fd
, LTTNG_KERNEL_OLD_CALIBRATE
, &old_calibrate
);
383 calibrate
->type
= old_calibrate
.type
;
385 ret
= ioctl(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
393 int kernctl_buffer_flush(int fd
)
395 return ioctl(fd
, RING_BUFFER_FLUSH
);
399 /* Buffer operations */
401 /* For mmap mode, readable without "get" operation */
403 /* returns the length to mmap. */
404 int kernctl_get_mmap_len(int fd
, unsigned long *len
)
406 return ioctl(fd
, RING_BUFFER_GET_MMAP_LEN
, len
);
409 /* returns the maximum size for sub-buffers. */
410 int kernctl_get_max_subbuf_size(int fd
, unsigned long *len
)
412 return ioctl(fd
, RING_BUFFER_GET_MAX_SUBBUF_SIZE
, len
);
416 * For mmap mode, operate on the current packet (between get/put or
417 * get_next/put_next).
420 /* returns the offset of the subbuffer belonging to the mmap reader. */
421 int kernctl_get_mmap_read_offset(int fd
, unsigned long *off
)
423 return ioctl(fd
, RING_BUFFER_GET_MMAP_READ_OFFSET
, off
);
426 /* returns the size of the current sub-buffer, without padding (for mmap). */
427 int kernctl_get_subbuf_size(int fd
, unsigned long *len
)
429 return ioctl(fd
, RING_BUFFER_GET_SUBBUF_SIZE
, len
);
432 /* returns the size of the current sub-buffer, without padding (for mmap). */
433 int kernctl_get_padded_subbuf_size(int fd
, unsigned long *len
)
435 return ioctl(fd
, RING_BUFFER_GET_PADDED_SUBBUF_SIZE
, len
);
438 /* Get exclusive read access to the next sub-buffer that can be read. */
439 int kernctl_get_next_subbuf(int fd
)
441 return ioctl(fd
, RING_BUFFER_GET_NEXT_SUBBUF
);
445 /* Release exclusive sub-buffer access, move consumer forward. */
446 int kernctl_put_next_subbuf(int fd
)
448 return ioctl(fd
, RING_BUFFER_PUT_NEXT_SUBBUF
);
453 /* Get a snapshot of the current ring buffer producer and consumer positions */
454 int kernctl_snapshot(int fd
)
456 return ioctl(fd
, RING_BUFFER_SNAPSHOT
);
459 /* Get the consumer position (iteration start) */
460 int kernctl_snapshot_get_consumed(int fd
, unsigned long *pos
)
462 return ioctl(fd
, RING_BUFFER_SNAPSHOT_GET_CONSUMED
, pos
);
465 /* Get the producer position (iteration end) */
466 int kernctl_snapshot_get_produced(int fd
, unsigned long *pos
)
468 return ioctl(fd
, RING_BUFFER_SNAPSHOT_GET_PRODUCED
, pos
);
471 /* Get exclusive read access to the specified sub-buffer position */
472 int kernctl_get_subbuf(int fd
, unsigned long *len
)
474 return ioctl(fd
, RING_BUFFER_GET_SUBBUF
, len
);
477 /* Release exclusive sub-buffer access */
478 int kernctl_put_subbuf(int fd
)
480 return ioctl(fd
, RING_BUFFER_PUT_SUBBUF
);
483 /* Returns the timestamp begin of the current sub-buffer. */
484 int kernctl_get_timestamp_begin(int fd
, uint64_t *timestamp_begin
)
486 return ioctl(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN
, timestamp_begin
);
489 /* Returns the timestamp end of the current sub-buffer. */
490 int kernctl_get_timestamp_end(int fd
, uint64_t *timestamp_end
)
492 return ioctl(fd
, LTTNG_RING_BUFFER_GET_TIMESTAMP_END
, timestamp_end
);
495 /* Returns the number of discarded events in the current sub-buffer. */
496 int kernctl_get_events_discarded(int fd
, uint64_t *events_discarded
)
498 return ioctl(fd
, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED
, events_discarded
);
501 /* Returns the content size in the current sub-buffer. */
502 int kernctl_get_content_size(int fd
, uint64_t *content_size
)
504 return ioctl(fd
, LTTNG_RING_BUFFER_GET_CONTENT_SIZE
, content_size
);
507 /* Returns the packet size in the current sub-buffer. */
508 int kernctl_get_packet_size(int fd
, uint64_t *packet_size
)
510 return ioctl(fd
, LTTNG_RING_BUFFER_GET_PACKET_SIZE
, packet_size
);
513 /* Returns the stream id of the current sub-buffer. */
514 int kernctl_get_stream_id(int fd
, uint64_t *stream_id
)
516 return ioctl(fd
, LTTNG_RING_BUFFER_GET_STREAM_ID
, stream_id
);
519 /* Returns the current timestamp. */
520 int kernctl_get_current_timestamp(int fd
, uint64_t *ts
)
522 return ioctl(fd
, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP
, ts
);