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