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