Use lttng-modules ABI version ioctl
[lttng-tools.git] / src / common / kernel-ctl / kernel-ctl.c
CommitLineData
ee0326c0 1/*
16421f6e
DG
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
ee0326c0 4 *
d14d33bf
AM
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.
ee0326c0
DG
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 *
d14d33bf
AM
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.
ee0326c0 17 */
16421f6e 18
95ba0f2f 19#define __USE_LINUX_IOCTL_DEFS
16421f6e 20#include <sys/ioctl.h>
4dbc372b 21#include <string.h>
46820c8b 22#include <common/align.h>
16421f6e 23
10a8a223 24#include "kernel-ctl.h"
16421f6e 25#include "kernel-ioctl.h"
16421f6e 26
4dbc372b
JD
27/*
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.
32 */
33static int lttng_kernel_use_old_abi = -1;
34
35/*
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
39 * on the old one.
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.
43 */
44static inline int compat_ioctl_no_arg(int fd, unsigned long oldname,
45 unsigned long newname)
46{
47 int ret;
48
49 if (lttng_kernel_use_old_abi == -1) {
50 ret = ioctl(fd, newname);
51 if (!ret) {
52 lttng_kernel_use_old_abi = 0;
53 goto end;
54 }
55 lttng_kernel_use_old_abi = 1;
56 }
57 if (lttng_kernel_use_old_abi) {
58 ret = ioctl(fd, oldname);
59 } else {
60 ret = ioctl(fd, newname);
61 }
62
63end:
64 return ret;
65}
66
964ccb60 67int kernctl_create_session(int fd)
d65106b1 68{
4dbc372b
JD
69 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION,
70 LTTNG_KERNEL_SESSION);
d65106b1
DG
71}
72
964ccb60
MD
73/* open the metadata global channel */
74int kernctl_open_metadata(int fd, struct lttng_channel_attr *chops)
f3ed775e 75{
4dbc372b
JD
76 struct lttng_kernel_old_channel old_channel;
77 struct lttng_kernel_channel channel;
78
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;
f853c53a
DG
86
87 memset(old_channel.padding, 0, sizeof(old_channel.padding));
88 /*
89 * The new channel padding is smaller than the old ABI so we use the
90 * new ABI padding size for the memcpy.
91 */
92 memcpy(old_channel.padding, chops->padding, sizeof(chops->padding));
4dbc372b
JD
93
94 return ioctl(fd, LTTNG_KERNEL_OLD_METADATA, &old_channel);
95 }
96
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;
ea207e3b 103 memcpy(channel.padding, chops->padding, sizeof(chops->padding));
4dbc372b
JD
104
105 return ioctl(fd, LTTNG_KERNEL_METADATA, &channel);
f3ed775e
DG
106}
107
108int kernctl_create_channel(int fd, struct lttng_channel_attr *chops)
16421f6e 109{
4dbc372b
JD
110 struct lttng_kernel_channel channel;
111
112 if (lttng_kernel_use_old_abi) {
113 struct lttng_kernel_old_channel old_channel;
114
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;
f853c53a
DG
121
122 memset(old_channel.padding, 0, sizeof(old_channel.padding));
123 /*
124 * The new channel padding is smaller than the old ABI so we use the
125 * new ABI padding size for the memcpy.
126 */
127 memcpy(old_channel.padding, chops->padding, sizeof(chops->padding));
4dbc372b
JD
128
129 return ioctl(fd, LTTNG_KERNEL_OLD_CHANNEL, &old_channel);
130 }
131
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;
ea207e3b 138 memcpy(channel.padding, chops->padding, sizeof(chops->padding));
4dbc372b
JD
139
140 return ioctl(fd, LTTNG_KERNEL_CHANNEL, &channel);
16421f6e
DG
141}
142
6e911cad
MD
143int kernctl_enable_syscall(int fd, const char *syscall_name)
144{
145 struct lttng_kernel_event event;
146
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;
562ab9a8 151 event.u.syscall.enable = 1;
6e911cad
MD
152 return ioctl(fd, LTTNG_KERNEL_EVENT, &event);
153}
154
155int kernctl_disable_syscall(int fd, const char *syscall_name)
156{
157 struct lttng_kernel_event event;
158
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;
562ab9a8 163 event.u.syscall.enable = 0;
6e911cad
MD
164 return ioctl(fd, LTTNG_KERNEL_EVENT, &event);
165}
166
46820c8b
MD
167int kernctl_syscall_mask(int fd, char **syscall_mask, uint32_t *nr_bits)
168{
169 struct lttng_kernel_syscall_mask kmask_len, *kmask = NULL;
170 size_t array_alloc_len;
171 char *new_mask;
172 int ret = 0;
173
174 if (!syscall_mask) {
175 ret = -1;
176 goto end;
177 }
178
179 if (!nr_bits) {
180 ret = -1;
181 goto end;
182 }
183
184 kmask_len.len = 0;
185 ret = ioctl(fd, LTTNG_KERNEL_SYSCALL_MASK, &kmask_len);
186 if (ret) {
187 goto end;
188 }
189
190 array_alloc_len = ALIGN(kmask_len.len, 8) >> 3;
834978fd 191
46820c8b
MD
192 kmask = zmalloc(sizeof(*kmask) + array_alloc_len);
193 if (!kmask) {
194 ret = -1;
195 goto end;
196 }
197
198 kmask->len = kmask_len.len;
199 ret = ioctl(fd, LTTNG_KERNEL_SYSCALL_MASK, kmask);
200 if (ret) {
201 goto end;
202 }
203
834978fd 204 new_mask = realloc(*syscall_mask, array_alloc_len);
46820c8b
MD
205 if (!new_mask) {
206 ret = -1;
207 goto end;
208 }
209 memcpy(new_mask, kmask->mask, array_alloc_len);
210 *syscall_mask = new_mask;
211 *nr_bits = kmask->len;
212
213end:
214 free(kmask);
215 return ret;
216}
217
964ccb60 218int kernctl_create_stream(int fd)
16421f6e 219{
4dbc372b
JD
220 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_STREAM,
221 LTTNG_KERNEL_STREAM);
16421f6e
DG
222}
223
964ccb60 224int kernctl_create_event(int fd, struct lttng_kernel_event *ev)
16421f6e 225{
4dbc372b
JD
226 if (lttng_kernel_use_old_abi) {
227 struct lttng_kernel_old_event old_event;
228
229 memcpy(old_event.name, ev->name, sizeof(old_event.name));
230 old_event.instrumentation = ev->instrumentation;
231 switch (ev->instrumentation) {
232 case LTTNG_KERNEL_KPROBE:
233 old_event.u.kprobe.addr = ev->u.kprobe.addr;
234 old_event.u.kprobe.offset = ev->u.kprobe.offset;
235 memcpy(old_event.u.kprobe.symbol_name,
236 ev->u.kprobe.symbol_name,
237 sizeof(old_event.u.kprobe.symbol_name));
238 break;
239 case LTTNG_KERNEL_KRETPROBE:
240 old_event.u.kretprobe.addr = ev->u.kretprobe.addr;
241 old_event.u.kretprobe.offset = ev->u.kretprobe.offset;
242 memcpy(old_event.u.kretprobe.symbol_name,
243 ev->u.kretprobe.symbol_name,
244 sizeof(old_event.u.kretprobe.symbol_name));
245 break;
246 case LTTNG_KERNEL_FUNCTION:
247 memcpy(old_event.u.ftrace.symbol_name,
248 ev->u.ftrace.symbol_name,
249 sizeof(old_event.u.ftrace.symbol_name));
250 break;
251 default:
252 break;
253 }
254
255 return ioctl(fd, LTTNG_KERNEL_OLD_EVENT, &old_event);
256 }
964ccb60 257 return ioctl(fd, LTTNG_KERNEL_EVENT, ev);
16421f6e
DG
258}
259
964ccb60 260int kernctl_add_context(int fd, struct lttng_kernel_context *ctx)
16421f6e 261{
4dbc372b
JD
262 if (lttng_kernel_use_old_abi) {
263 struct lttng_kernel_old_context old_ctx;
264
265 old_ctx.ctx = ctx->ctx;
266 /* only type that uses the union */
aa3514e9 267 if (ctx->ctx == LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER) {
4dbc372b
JD
268 old_ctx.u.perf_counter.type =
269 ctx->u.perf_counter.type;
270 old_ctx.u.perf_counter.config =
271 ctx->u.perf_counter.config;
272 memcpy(old_ctx.u.perf_counter.name,
273 ctx->u.perf_counter.name,
274 sizeof(old_ctx.u.perf_counter.name));
275 }
276 return ioctl(fd, LTTNG_KERNEL_OLD_CONTEXT, &old_ctx);
277 }
964ccb60 278 return ioctl(fd, LTTNG_KERNEL_CONTEXT, ctx);
16421f6e
DG
279}
280
964ccb60 281
f3ed775e
DG
282/* Enable event, channel and session ioctl */
283int kernctl_enable(int fd)
284{
4dbc372b
JD
285 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_ENABLE,
286 LTTNG_KERNEL_ENABLE);
f3ed775e
DG
287}
288
289/* Disable event, channel and session ioctl */
290int kernctl_disable(int fd)
291{
4dbc372b
JD
292 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_DISABLE,
293 LTTNG_KERNEL_DISABLE);
f3ed775e
DG
294}
295
964ccb60 296int kernctl_start_session(int fd)
16421f6e 297{
4dbc372b
JD
298 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_START,
299 LTTNG_KERNEL_SESSION_START);
964ccb60
MD
300}
301
302int kernctl_stop_session(int fd)
303{
4dbc372b
JD
304 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_STOP,
305 LTTNG_KERNEL_SESSION_STOP);
16421f6e
DG
306}
307
964ccb60
MD
308int kernctl_tracepoint_list(int fd)
309{
4dbc372b
JD
310 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_TRACEPOINT_LIST,
311 LTTNG_KERNEL_TRACEPOINT_LIST);
964ccb60
MD
312}
313
d02666a9
MD
314int kernctl_syscall_list(int fd)
315{
316 return ioctl(fd, LTTNG_KERNEL_SYSCALL_LIST);
317}
318
964ccb60
MD
319int kernctl_tracer_version(int fd, struct lttng_kernel_tracer_version *v)
320{
4dbc372b
JD
321 int ret;
322
323 if (lttng_kernel_use_old_abi == -1) {
324 ret = ioctl(fd, LTTNG_KERNEL_TRACER_VERSION, v);
325 if (!ret) {
326 lttng_kernel_use_old_abi = 0;
327 goto end;
328 }
329 lttng_kernel_use_old_abi = 1;
330 }
331 if (lttng_kernel_use_old_abi) {
332 struct lttng_kernel_old_tracer_version old_v;
333
334 ret = ioctl(fd, LTTNG_KERNEL_OLD_TRACER_VERSION, &old_v);
335 if (ret) {
336 goto end;
337 }
338 v->major = old_v.major;
339 v->minor = old_v.minor;
340 v->patchlevel = old_v.patchlevel;
341 } else {
342 ret = ioctl(fd, LTTNG_KERNEL_TRACER_VERSION, v);
343 }
344
345end:
346 return ret;
964ccb60
MD
347}
348
c052142c
MD
349int kernctl_tracer_abi_version(int fd,
350 struct lttng_kernel_tracer_abi_version *v)
351{
352 return ioctl(fd, LTTNG_KERNEL_TRACER_ABI_VERSION, v);
353}
354
964ccb60
MD
355int kernctl_wait_quiescent(int fd)
356{
4dbc372b
JD
357 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_WAIT_QUIESCENT,
358 LTTNG_KERNEL_WAIT_QUIESCENT);
964ccb60
MD
359}
360
361int kernctl_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
362{
4dbc372b
JD
363 int ret;
364
365 if (lttng_kernel_use_old_abi == -1) {
366 ret = ioctl(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
367 if (!ret) {
368 lttng_kernel_use_old_abi = 0;
369 goto end;
370 }
371 lttng_kernel_use_old_abi = 1;
372 }
373 if (lttng_kernel_use_old_abi) {
374 struct lttng_kernel_old_calibrate old_calibrate;
375
376 old_calibrate.type = calibrate->type;
377 ret = ioctl(fd, LTTNG_KERNEL_OLD_CALIBRATE, &old_calibrate);
378 if (ret) {
379 goto end;
380 }
381 calibrate->type = old_calibrate.type;
382 } else {
383 ret = ioctl(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
384 }
385
386end:
387 return ret;
964ccb60
MD
388}
389
390
391int kernctl_buffer_flush(int fd)
392{
393 return ioctl(fd, RING_BUFFER_FLUSH);
394}
395
396
397/* Buffer operations */
398
399/* For mmap mode, readable without "get" operation */
400
16421f6e
DG
401/* returns the length to mmap. */
402int kernctl_get_mmap_len(int fd, unsigned long *len)
403{
404 return ioctl(fd, RING_BUFFER_GET_MMAP_LEN, len);
405}
406
964ccb60
MD
407/* returns the maximum size for sub-buffers. */
408int kernctl_get_max_subbuf_size(int fd, unsigned long *len)
409{
410 return ioctl(fd, RING_BUFFER_GET_MAX_SUBBUF_SIZE, len);
411}
412
413/*
414 * For mmap mode, operate on the current packet (between get/put or
415 * get_next/put_next).
416 */
417
16421f6e
DG
418/* returns the offset of the subbuffer belonging to the mmap reader. */
419int kernctl_get_mmap_read_offset(int fd, unsigned long *off)
420{
421 return ioctl(fd, RING_BUFFER_GET_MMAP_READ_OFFSET, off);
422}
423
964ccb60
MD
424/* returns the size of the current sub-buffer, without padding (for mmap). */
425int kernctl_get_subbuf_size(int fd, unsigned long *len)
16421f6e 426{
964ccb60 427 return ioctl(fd, RING_BUFFER_GET_SUBBUF_SIZE, len);
16421f6e
DG
428}
429
430/* returns the size of the current sub-buffer, without padding (for mmap). */
431int kernctl_get_padded_subbuf_size(int fd, unsigned long *len)
432{
433 return ioctl(fd, RING_BUFFER_GET_PADDED_SUBBUF_SIZE, len);
434}
435
964ccb60
MD
436/* Get exclusive read access to the next sub-buffer that can be read. */
437int kernctl_get_next_subbuf(int fd)
16421f6e 438{
964ccb60 439 return ioctl(fd, RING_BUFFER_GET_NEXT_SUBBUF);
16421f6e
DG
440}
441
d4a1283e 442
16421f6e
DG
443/* Release exclusive sub-buffer access, move consumer forward. */
444int kernctl_put_next_subbuf(int fd)
445{
446 return ioctl(fd, RING_BUFFER_PUT_NEXT_SUBBUF);
447}
448
964ccb60 449/* snapshot */
16421f6e
DG
450
451/* Get a snapshot of the current ring buffer producer and consumer positions */
452int kernctl_snapshot(int fd)
453{
454 return ioctl(fd, RING_BUFFER_SNAPSHOT);
455}
456
457/* Get the consumer position (iteration start) */
458int kernctl_snapshot_get_consumed(int fd, unsigned long *pos)
459{
460 return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_CONSUMED, pos);
461}
462
463/* Get the producer position (iteration end) */
464int kernctl_snapshot_get_produced(int fd, unsigned long *pos)
465{
466 return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_PRODUCED, pos);
467}
468
964ccb60
MD
469/* Get exclusive read access to the specified sub-buffer position */
470int kernctl_get_subbuf(int fd, unsigned long *len)
f3ed775e 471{
964ccb60 472 return ioctl(fd, RING_BUFFER_GET_SUBBUF, len);
f3ed775e 473}
d0254c7c 474
964ccb60
MD
475/* Release exclusive sub-buffer access */
476int kernctl_put_subbuf(int fd)
d0254c7c 477{
964ccb60 478 return ioctl(fd, RING_BUFFER_PUT_SUBBUF);
d0254c7c 479}
309167d2
JD
480
481/* Returns the timestamp begin of the current sub-buffer. */
482int kernctl_get_timestamp_begin(int fd, uint64_t *timestamp_begin)
483{
484 return ioctl(fd, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN, timestamp_begin);
485}
486
487/* Returns the timestamp end of the current sub-buffer. */
488int kernctl_get_timestamp_end(int fd, uint64_t *timestamp_end)
489{
490 return ioctl(fd, LTTNG_RING_BUFFER_GET_TIMESTAMP_END, timestamp_end);
491}
492
493/* Returns the number of discarded events in the current sub-buffer. */
494int kernctl_get_events_discarded(int fd, uint64_t *events_discarded)
495{
496 return ioctl(fd, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED, events_discarded);
497}
498
499/* Returns the content size in the current sub-buffer. */
500int kernctl_get_content_size(int fd, uint64_t *content_size)
501{
502 return ioctl(fd, LTTNG_RING_BUFFER_GET_CONTENT_SIZE, content_size);
503}
504
505/* Returns the packet size in the current sub-buffer. */
506int kernctl_get_packet_size(int fd, uint64_t *packet_size)
507{
508 return ioctl(fd, LTTNG_RING_BUFFER_GET_PACKET_SIZE, packet_size);
509}
510
511/* Returns the stream id of the current sub-buffer. */
512int kernctl_get_stream_id(int fd, uint64_t *stream_id)
513{
514 return ioctl(fd, LTTNG_RING_BUFFER_GET_STREAM_ID, stream_id);
515}
d3e2ba59
JD
516
517/* Returns the current timestamp. */
518int kernctl_get_current_timestamp(int fd, uint64_t *ts)
519{
520 return ioctl(fd, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP, ts);
521}
This page took 0.060943 seconds and 4 git commands to generate.