ABI with support for compat 32/64 bits
[lttng-tools.git] / src / common / kernel-ctl / kernel-ctl.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
8 *
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.
13 *
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.
17 */
18
19 #define __USE_LINUX_IOCTL_DEFS
20 #include <sys/ioctl.h>
21 #include <string.h>
22
23 #include "kernel-ctl.h"
24 #include "kernel-ioctl.h"
25
26 /*
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.
31 */
32 static int lttng_kernel_use_old_abi = -1;
33
34 /*
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
38 * on the old one.
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.
42 */
43 static inline int compat_ioctl_no_arg(int fd, unsigned long oldname,
44 unsigned long newname)
45 {
46 int ret;
47
48 if (lttng_kernel_use_old_abi == -1) {
49 ret = ioctl(fd, newname);
50 if (!ret) {
51 lttng_kernel_use_old_abi = 0;
52 goto end;
53 }
54 lttng_kernel_use_old_abi = 1;
55 }
56 if (lttng_kernel_use_old_abi) {
57 ret = ioctl(fd, oldname);
58 } else {
59 ret = ioctl(fd, newname);
60 }
61
62 end:
63 return ret;
64 }
65
66 int kernctl_create_session(int fd)
67 {
68 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION,
69 LTTNG_KERNEL_SESSION);
70 }
71
72 /* open the metadata global channel */
73 int kernctl_open_metadata(int fd, struct lttng_channel_attr *chops)
74 {
75 struct lttng_kernel_old_channel old_channel;
76 struct lttng_kernel_channel channel;
77
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;
85 memcpy(old_channel.padding, chops->padding, sizeof(old_channel.padding));
86
87 return ioctl(fd, LTTNG_KERNEL_OLD_METADATA, &old_channel);
88 }
89
90 channel.overwrite = chops->overwrite;
91 channel.subbuf_size = chops->subbuf_size;
92 channel.num_subbuf = chops->num_subbuf;
93 channel.switch_timer_interval = chops->switch_timer_interval;
94 channel.read_timer_interval = chops->read_timer_interval;
95 channel.output = chops->output;
96 memcpy(channel.padding, chops->padding, sizeof(channel.padding));
97
98 return ioctl(fd, LTTNG_KERNEL_METADATA, &channel);
99 }
100
101 int kernctl_create_channel(int fd, struct lttng_channel_attr *chops)
102 {
103 struct lttng_kernel_channel channel;
104
105 if (lttng_kernel_use_old_abi) {
106 struct lttng_kernel_old_channel old_channel;
107
108 old_channel.overwrite = chops->overwrite;
109 old_channel.subbuf_size = chops->subbuf_size;
110 old_channel.num_subbuf = chops->num_subbuf;
111 old_channel.switch_timer_interval = chops->switch_timer_interval;
112 old_channel.read_timer_interval = chops->read_timer_interval;
113 old_channel.output = chops->output;
114 memcpy(old_channel.padding, chops->padding, sizeof(old_channel.padding));
115
116 return ioctl(fd, LTTNG_KERNEL_OLD_CHANNEL, &old_channel);
117 }
118
119 channel.overwrite = chops->overwrite;
120 channel.subbuf_size = chops->subbuf_size;
121 channel.num_subbuf = chops->num_subbuf;
122 channel.switch_timer_interval = chops->switch_timer_interval;
123 channel.read_timer_interval = chops->read_timer_interval;
124 channel.output = chops->output;
125 memcpy(channel.padding, chops->padding, sizeof(channel.padding));
126
127 return ioctl(fd, LTTNG_KERNEL_CHANNEL, &channel);
128 }
129
130 int kernctl_create_stream(int fd)
131 {
132 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_STREAM,
133 LTTNG_KERNEL_STREAM);
134 }
135
136 int kernctl_create_event(int fd, struct lttng_kernel_event *ev)
137 {
138 if (lttng_kernel_use_old_abi) {
139 struct lttng_kernel_old_event old_event;
140
141 memcpy(old_event.name, ev->name, sizeof(old_event.name));
142 old_event.instrumentation = ev->instrumentation;
143 switch (ev->instrumentation) {
144 case LTTNG_KERNEL_KPROBE:
145 old_event.u.kprobe.addr = ev->u.kprobe.addr;
146 old_event.u.kprobe.offset = ev->u.kprobe.offset;
147 memcpy(old_event.u.kprobe.symbol_name,
148 ev->u.kprobe.symbol_name,
149 sizeof(old_event.u.kprobe.symbol_name));
150 break;
151 case LTTNG_KERNEL_KRETPROBE:
152 old_event.u.kretprobe.addr = ev->u.kretprobe.addr;
153 old_event.u.kretprobe.offset = ev->u.kretprobe.offset;
154 memcpy(old_event.u.kretprobe.symbol_name,
155 ev->u.kretprobe.symbol_name,
156 sizeof(old_event.u.kretprobe.symbol_name));
157 break;
158 case LTTNG_KERNEL_FUNCTION:
159 memcpy(old_event.u.ftrace.symbol_name,
160 ev->u.ftrace.symbol_name,
161 sizeof(old_event.u.ftrace.symbol_name));
162 break;
163 default:
164 break;
165 }
166
167 return ioctl(fd, LTTNG_KERNEL_OLD_EVENT, &old_event);
168 }
169 return ioctl(fd, LTTNG_KERNEL_EVENT, ev);
170 }
171
172 int kernctl_add_context(int fd, struct lttng_kernel_context *ctx)
173 {
174 if (lttng_kernel_use_old_abi) {
175 struct lttng_kernel_old_context old_ctx;
176
177 old_ctx.ctx = ctx->ctx;
178 /* only type that uses the union */
179 if (ctx->ctx == LTTNG_KERNEL_CONTEXT_PERF_COUNTER) {
180 old_ctx.u.perf_counter.type =
181 ctx->u.perf_counter.type;
182 old_ctx.u.perf_counter.config =
183 ctx->u.perf_counter.config;
184 memcpy(old_ctx.u.perf_counter.name,
185 ctx->u.perf_counter.name,
186 sizeof(old_ctx.u.perf_counter.name));
187 }
188 return ioctl(fd, LTTNG_KERNEL_OLD_CONTEXT, &old_ctx);
189 }
190 return ioctl(fd, LTTNG_KERNEL_CONTEXT, ctx);
191 }
192
193
194 /* Enable event, channel and session ioctl */
195 int kernctl_enable(int fd)
196 {
197 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_ENABLE,
198 LTTNG_KERNEL_ENABLE);
199 }
200
201 /* Disable event, channel and session ioctl */
202 int kernctl_disable(int fd)
203 {
204 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_DISABLE,
205 LTTNG_KERNEL_DISABLE);
206 }
207
208 int kernctl_start_session(int fd)
209 {
210 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_START,
211 LTTNG_KERNEL_SESSION_START);
212 }
213
214 int kernctl_stop_session(int fd)
215 {
216 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_STOP,
217 LTTNG_KERNEL_SESSION_STOP);
218 }
219
220 int kernctl_tracepoint_list(int fd)
221 {
222 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_TRACEPOINT_LIST,
223 LTTNG_KERNEL_TRACEPOINT_LIST);
224 }
225
226 int kernctl_tracer_version(int fd, struct lttng_kernel_tracer_version *v)
227 {
228 int ret;
229
230 if (lttng_kernel_use_old_abi == -1) {
231 ret = ioctl(fd, LTTNG_KERNEL_TRACER_VERSION, v);
232 if (!ret) {
233 lttng_kernel_use_old_abi = 0;
234 goto end;
235 }
236 lttng_kernel_use_old_abi = 1;
237 }
238 if (lttng_kernel_use_old_abi) {
239 struct lttng_kernel_old_tracer_version old_v;
240
241 ret = ioctl(fd, LTTNG_KERNEL_OLD_TRACER_VERSION, &old_v);
242 if (ret) {
243 goto end;
244 }
245 v->major = old_v.major;
246 v->minor = old_v.minor;
247 v->patchlevel = old_v.patchlevel;
248 } else {
249 ret = ioctl(fd, LTTNG_KERNEL_TRACER_VERSION, v);
250 }
251
252 end:
253 return ret;
254 }
255
256 int kernctl_wait_quiescent(int fd)
257 {
258 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_WAIT_QUIESCENT,
259 LTTNG_KERNEL_WAIT_QUIESCENT);
260 }
261
262 int kernctl_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
263 {
264 int ret;
265
266 if (lttng_kernel_use_old_abi == -1) {
267 ret = ioctl(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
268 if (!ret) {
269 lttng_kernel_use_old_abi = 0;
270 goto end;
271 }
272 lttng_kernel_use_old_abi = 1;
273 }
274 if (lttng_kernel_use_old_abi) {
275 struct lttng_kernel_old_calibrate old_calibrate;
276
277 old_calibrate.type = calibrate->type;
278 ret = ioctl(fd, LTTNG_KERNEL_OLD_CALIBRATE, &old_calibrate);
279 if (ret) {
280 goto end;
281 }
282 calibrate->type = old_calibrate.type;
283 } else {
284 ret = ioctl(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
285 }
286
287 end:
288 return ret;
289 }
290
291
292 int kernctl_buffer_flush(int fd)
293 {
294 return ioctl(fd, RING_BUFFER_FLUSH);
295 }
296
297
298 /* Buffer operations */
299
300 /* For mmap mode, readable without "get" operation */
301
302 /* returns the length to mmap. */
303 int kernctl_get_mmap_len(int fd, unsigned long *len)
304 {
305 return ioctl(fd, RING_BUFFER_GET_MMAP_LEN, len);
306 }
307
308 /* returns the maximum size for sub-buffers. */
309 int kernctl_get_max_subbuf_size(int fd, unsigned long *len)
310 {
311 return ioctl(fd, RING_BUFFER_GET_MAX_SUBBUF_SIZE, len);
312 }
313
314 /*
315 * For mmap mode, operate on the current packet (between get/put or
316 * get_next/put_next).
317 */
318
319 /* returns the offset of the subbuffer belonging to the mmap reader. */
320 int kernctl_get_mmap_read_offset(int fd, unsigned long *off)
321 {
322 return ioctl(fd, RING_BUFFER_GET_MMAP_READ_OFFSET, off);
323 }
324
325 /* returns the size of the current sub-buffer, without padding (for mmap). */
326 int kernctl_get_subbuf_size(int fd, unsigned long *len)
327 {
328 return ioctl(fd, RING_BUFFER_GET_SUBBUF_SIZE, len);
329 }
330
331 /* returns the size of the current sub-buffer, without padding (for mmap). */
332 int kernctl_get_padded_subbuf_size(int fd, unsigned long *len)
333 {
334 return ioctl(fd, RING_BUFFER_GET_PADDED_SUBBUF_SIZE, len);
335 }
336
337 /* Get exclusive read access to the next sub-buffer that can be read. */
338 int kernctl_get_next_subbuf(int fd)
339 {
340 return ioctl(fd, RING_BUFFER_GET_NEXT_SUBBUF);
341 }
342
343
344 /* Release exclusive sub-buffer access, move consumer forward. */
345 int kernctl_put_next_subbuf(int fd)
346 {
347 return ioctl(fd, RING_BUFFER_PUT_NEXT_SUBBUF);
348 }
349
350 /* snapshot */
351
352 /* Get a snapshot of the current ring buffer producer and consumer positions */
353 int kernctl_snapshot(int fd)
354 {
355 return ioctl(fd, RING_BUFFER_SNAPSHOT);
356 }
357
358 /* Get the consumer position (iteration start) */
359 int kernctl_snapshot_get_consumed(int fd, unsigned long *pos)
360 {
361 return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_CONSUMED, pos);
362 }
363
364 /* Get the producer position (iteration end) */
365 int kernctl_snapshot_get_produced(int fd, unsigned long *pos)
366 {
367 return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_PRODUCED, pos);
368 }
369
370 /* Get exclusive read access to the specified sub-buffer position */
371 int kernctl_get_subbuf(int fd, unsigned long *len)
372 {
373 return ioctl(fd, RING_BUFFER_GET_SUBBUF, len);
374 }
375
376 /* Release exclusive sub-buffer access */
377 int kernctl_put_subbuf(int fd)
378 {
379 return ioctl(fd, RING_BUFFER_PUT_SUBBUF);
380 }
381
382 /* Set the stream_id */
383 int kernctl_set_stream_id(int fd, unsigned long *stream_id)
384 {
385 return ioctl(fd, RING_BUFFER_SET_STREAM_ID, stream_id);
386 }
This page took 0.039344 seconds and 6 git commands to generate.