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>
23 #include "kernel-ctl.h"
24 #include "kernel-ioctl.h"
27 * This flag indicates which version of the kernel ABI to use. The old
28 * ABI (namespace _old) does not support a 32-bit user-space when the
29 * kernel is 64-bit. The old ABI is kept here for compatibility but is
30 * deprecated and will be removed eventually.
32 static int lttng_kernel_use_old_abi
= -1;
35 * Execute the new or old ioctl depending on the ABI version.
36 * If the ABI version is not determined yet (lttng_kernel_use_old_abi = -1),
37 * this function tests if the new ABI is available and otherwise fallbacks
39 * This function takes the fd on which the ioctl must be executed and the old
40 * and new request codes.
41 * It returns the return value of the ioctl executed.
43 static inline int compat_ioctl_no_arg(int fd
, unsigned long oldname
,
44 unsigned long newname
)
48 if (lttng_kernel_use_old_abi
== -1) {
49 ret
= ioctl(fd
, newname
);
51 lttng_kernel_use_old_abi
= 0;
54 lttng_kernel_use_old_abi
= 1;
56 if (lttng_kernel_use_old_abi
) {
57 ret
= ioctl(fd
, oldname
);
59 ret
= ioctl(fd
, newname
);
66 int kernctl_create_session(int fd
)
68 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION
,
69 LTTNG_KERNEL_SESSION
);
72 /* open the metadata global channel */
73 int kernctl_open_metadata(int fd
, struct lttng_channel_attr
*chops
)
75 struct lttng_kernel_old_channel old_channel
;
76 struct lttng_kernel_channel channel
;
78 if (lttng_kernel_use_old_abi
) {
79 old_channel
.overwrite
= chops
->overwrite
;
80 old_channel
.subbuf_size
= chops
->subbuf_size
;
81 old_channel
.num_subbuf
= chops
->num_subbuf
;
82 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
83 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
84 old_channel
.output
= chops
->output
;
86 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
88 * The new channel padding is smaller than the old ABI so we use the
89 * new ABI padding size for the memcpy.
91 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
93 return ioctl(fd
, LTTNG_KERNEL_OLD_METADATA
, &old_channel
);
96 channel
.overwrite
= chops
->overwrite
;
97 channel
.subbuf_size
= chops
->subbuf_size
;
98 channel
.num_subbuf
= chops
->num_subbuf
;
99 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
100 channel
.read_timer_interval
= chops
->read_timer_interval
;
101 channel
.output
= chops
->output
;
102 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
104 return ioctl(fd
, LTTNG_KERNEL_METADATA
, &channel
);
107 int kernctl_create_channel(int fd
, struct lttng_channel_attr
*chops
)
109 struct lttng_kernel_channel channel
;
111 if (lttng_kernel_use_old_abi
) {
112 struct lttng_kernel_old_channel old_channel
;
114 old_channel
.overwrite
= chops
->overwrite
;
115 old_channel
.subbuf_size
= chops
->subbuf_size
;
116 old_channel
.num_subbuf
= chops
->num_subbuf
;
117 old_channel
.switch_timer_interval
= chops
->switch_timer_interval
;
118 old_channel
.read_timer_interval
= chops
->read_timer_interval
;
119 old_channel
.output
= chops
->output
;
121 memset(old_channel
.padding
, 0, sizeof(old_channel
.padding
));
123 * The new channel padding is smaller than the old ABI so we use the
124 * new ABI padding size for the memcpy.
126 memcpy(old_channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
128 return ioctl(fd
, LTTNG_KERNEL_OLD_CHANNEL
, &old_channel
);
131 channel
.overwrite
= chops
->overwrite
;
132 channel
.subbuf_size
= chops
->subbuf_size
;
133 channel
.num_subbuf
= chops
->num_subbuf
;
134 channel
.switch_timer_interval
= chops
->switch_timer_interval
;
135 channel
.read_timer_interval
= chops
->read_timer_interval
;
136 channel
.output
= chops
->output
;
137 memcpy(channel
.padding
, chops
->padding
, sizeof(chops
->padding
));
139 return ioctl(fd
, LTTNG_KERNEL_CHANNEL
, &channel
);
142 int kernctl_create_stream(int fd
)
144 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_STREAM
,
145 LTTNG_KERNEL_STREAM
);
148 int kernctl_create_event(int fd
, struct lttng_kernel_event
*ev
)
150 if (lttng_kernel_use_old_abi
) {
151 struct lttng_kernel_old_event old_event
;
153 memcpy(old_event
.name
, ev
->name
, sizeof(old_event
.name
));
154 old_event
.instrumentation
= ev
->instrumentation
;
155 switch (ev
->instrumentation
) {
156 case LTTNG_KERNEL_KPROBE
:
157 old_event
.u
.kprobe
.addr
= ev
->u
.kprobe
.addr
;
158 old_event
.u
.kprobe
.offset
= ev
->u
.kprobe
.offset
;
159 memcpy(old_event
.u
.kprobe
.symbol_name
,
160 ev
->u
.kprobe
.symbol_name
,
161 sizeof(old_event
.u
.kprobe
.symbol_name
));
163 case LTTNG_KERNEL_KRETPROBE
:
164 old_event
.u
.kretprobe
.addr
= ev
->u
.kretprobe
.addr
;
165 old_event
.u
.kretprobe
.offset
= ev
->u
.kretprobe
.offset
;
166 memcpy(old_event
.u
.kretprobe
.symbol_name
,
167 ev
->u
.kretprobe
.symbol_name
,
168 sizeof(old_event
.u
.kretprobe
.symbol_name
));
170 case LTTNG_KERNEL_FUNCTION
:
171 memcpy(old_event
.u
.ftrace
.symbol_name
,
172 ev
->u
.ftrace
.symbol_name
,
173 sizeof(old_event
.u
.ftrace
.symbol_name
));
179 return ioctl(fd
, LTTNG_KERNEL_OLD_EVENT
, &old_event
);
181 return ioctl(fd
, LTTNG_KERNEL_EVENT
, ev
);
184 int kernctl_add_context(int fd
, struct lttng_kernel_context
*ctx
)
186 if (lttng_kernel_use_old_abi
) {
187 struct lttng_kernel_old_context old_ctx
;
189 old_ctx
.ctx
= ctx
->ctx
;
190 /* only type that uses the union */
191 if (ctx
->ctx
== LTTNG_KERNEL_CONTEXT_PERF_COUNTER
) {
192 old_ctx
.u
.perf_counter
.type
=
193 ctx
->u
.perf_counter
.type
;
194 old_ctx
.u
.perf_counter
.config
=
195 ctx
->u
.perf_counter
.config
;
196 memcpy(old_ctx
.u
.perf_counter
.name
,
197 ctx
->u
.perf_counter
.name
,
198 sizeof(old_ctx
.u
.perf_counter
.name
));
200 return ioctl(fd
, LTTNG_KERNEL_OLD_CONTEXT
, &old_ctx
);
202 return ioctl(fd
, LTTNG_KERNEL_CONTEXT
, ctx
);
206 /* Enable event, channel and session ioctl */
207 int kernctl_enable(int fd
)
209 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_ENABLE
,
210 LTTNG_KERNEL_ENABLE
);
213 /* Disable event, channel and session ioctl */
214 int kernctl_disable(int fd
)
216 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_DISABLE
,
217 LTTNG_KERNEL_DISABLE
);
220 int kernctl_start_session(int fd
)
222 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_START
,
223 LTTNG_KERNEL_SESSION_START
);
226 int kernctl_stop_session(int fd
)
228 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_SESSION_STOP
,
229 LTTNG_KERNEL_SESSION_STOP
);
232 int kernctl_tracepoint_list(int fd
)
234 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_TRACEPOINT_LIST
,
235 LTTNG_KERNEL_TRACEPOINT_LIST
);
238 int kernctl_tracer_version(int fd
, struct lttng_kernel_tracer_version
*v
)
242 if (lttng_kernel_use_old_abi
== -1) {
243 ret
= ioctl(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
245 lttng_kernel_use_old_abi
= 0;
248 lttng_kernel_use_old_abi
= 1;
250 if (lttng_kernel_use_old_abi
) {
251 struct lttng_kernel_old_tracer_version old_v
;
253 ret
= ioctl(fd
, LTTNG_KERNEL_OLD_TRACER_VERSION
, &old_v
);
257 v
->major
= old_v
.major
;
258 v
->minor
= old_v
.minor
;
259 v
->patchlevel
= old_v
.patchlevel
;
261 ret
= ioctl(fd
, LTTNG_KERNEL_TRACER_VERSION
, v
);
268 int kernctl_wait_quiescent(int fd
)
270 return compat_ioctl_no_arg(fd
, LTTNG_KERNEL_OLD_WAIT_QUIESCENT
,
271 LTTNG_KERNEL_WAIT_QUIESCENT
);
274 int kernctl_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
278 if (lttng_kernel_use_old_abi
== -1) {
279 ret
= ioctl(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
281 lttng_kernel_use_old_abi
= 0;
284 lttng_kernel_use_old_abi
= 1;
286 if (lttng_kernel_use_old_abi
) {
287 struct lttng_kernel_old_calibrate old_calibrate
;
289 old_calibrate
.type
= calibrate
->type
;
290 ret
= ioctl(fd
, LTTNG_KERNEL_OLD_CALIBRATE
, &old_calibrate
);
294 calibrate
->type
= old_calibrate
.type
;
296 ret
= ioctl(fd
, LTTNG_KERNEL_CALIBRATE
, calibrate
);
304 int kernctl_buffer_flush(int fd
)
306 return ioctl(fd
, RING_BUFFER_FLUSH
);
310 /* Buffer operations */
312 /* For mmap mode, readable without "get" operation */
314 /* returns the length to mmap. */
315 int kernctl_get_mmap_len(int fd
, unsigned long *len
)
317 return ioctl(fd
, RING_BUFFER_GET_MMAP_LEN
, len
);
320 /* returns the maximum size for sub-buffers. */
321 int kernctl_get_max_subbuf_size(int fd
, unsigned long *len
)
323 return ioctl(fd
, RING_BUFFER_GET_MAX_SUBBUF_SIZE
, len
);
327 * For mmap mode, operate on the current packet (between get/put or
328 * get_next/put_next).
331 /* returns the offset of the subbuffer belonging to the mmap reader. */
332 int kernctl_get_mmap_read_offset(int fd
, unsigned long *off
)
334 return ioctl(fd
, RING_BUFFER_GET_MMAP_READ_OFFSET
, off
);
337 /* returns the size of the current sub-buffer, without padding (for mmap). */
338 int kernctl_get_subbuf_size(int fd
, unsigned long *len
)
340 return ioctl(fd
, RING_BUFFER_GET_SUBBUF_SIZE
, len
);
343 /* returns the size of the current sub-buffer, without padding (for mmap). */
344 int kernctl_get_padded_subbuf_size(int fd
, unsigned long *len
)
346 return ioctl(fd
, RING_BUFFER_GET_PADDED_SUBBUF_SIZE
, len
);
349 /* Get exclusive read access to the next sub-buffer that can be read. */
350 int kernctl_get_next_subbuf(int fd
)
352 return ioctl(fd
, RING_BUFFER_GET_NEXT_SUBBUF
);
356 /* Release exclusive sub-buffer access, move consumer forward. */
357 int kernctl_put_next_subbuf(int fd
)
359 return ioctl(fd
, RING_BUFFER_PUT_NEXT_SUBBUF
);
364 /* Get a snapshot of the current ring buffer producer and consumer positions */
365 int kernctl_snapshot(int fd
)
367 return ioctl(fd
, RING_BUFFER_SNAPSHOT
);
370 /* Get the consumer position (iteration start) */
371 int kernctl_snapshot_get_consumed(int fd
, unsigned long *pos
)
373 return ioctl(fd
, RING_BUFFER_SNAPSHOT_GET_CONSUMED
, pos
);
376 /* Get the producer position (iteration end) */
377 int kernctl_snapshot_get_produced(int fd
, unsigned long *pos
)
379 return ioctl(fd
, RING_BUFFER_SNAPSHOT_GET_PRODUCED
, pos
);
382 /* Get exclusive read access to the specified sub-buffer position */
383 int kernctl_get_subbuf(int fd
, unsigned long *len
)
385 return ioctl(fd
, RING_BUFFER_GET_SUBBUF
, len
);
388 /* Release exclusive sub-buffer access */
389 int kernctl_put_subbuf(int fd
)
391 return ioctl(fd
, RING_BUFFER_PUT_SUBBUF
);