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>
16421f6e 22
10a8a223 23#include "kernel-ctl.h"
16421f6e 24#include "kernel-ioctl.h"
16421f6e 25
4dbc372b
JD
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 */
32static 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 */
43static 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
62end:
63 return ret;
64}
65
964ccb60 66int kernctl_create_session(int fd)
d65106b1 67{
4dbc372b
JD
68 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION,
69 LTTNG_KERNEL_SESSION);
d65106b1
DG
70}
71
964ccb60
MD
72/* open the metadata global channel */
73int kernctl_open_metadata(int fd, struct lttng_channel_attr *chops)
f3ed775e 74{
4dbc372b
JD
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;
f853c53a
DG
85
86 memset(old_channel.padding, 0, sizeof(old_channel.padding));
87 /*
88 * The new channel padding is smaller than the old ABI so we use the
89 * new ABI padding size for the memcpy.
90 */
91 memcpy(old_channel.padding, chops->padding, sizeof(chops->padding));
4dbc372b
JD
92
93 return ioctl(fd, LTTNG_KERNEL_OLD_METADATA, &old_channel);
94 }
95
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;
ea207e3b 102 memcpy(channel.padding, chops->padding, sizeof(chops->padding));
4dbc372b
JD
103
104 return ioctl(fd, LTTNG_KERNEL_METADATA, &channel);
f3ed775e
DG
105}
106
107int kernctl_create_channel(int fd, struct lttng_channel_attr *chops)
16421f6e 108{
4dbc372b
JD
109 struct lttng_kernel_channel channel;
110
388d4b5b 111 memset(&channel, 0, sizeof(channel));
4dbc372b
JD
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
964ccb60 143int kernctl_create_stream(int fd)
16421f6e 144{
4dbc372b
JD
145 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_STREAM,
146 LTTNG_KERNEL_STREAM);
16421f6e
DG
147}
148
964ccb60 149int kernctl_create_event(int fd, struct lttng_kernel_event *ev)
16421f6e 150{
4dbc372b
JD
151 if (lttng_kernel_use_old_abi) {
152 struct lttng_kernel_old_event old_event;
153
154 memcpy(old_event.name, ev->name, sizeof(old_event.name));
155 old_event.instrumentation = ev->instrumentation;
156 switch (ev->instrumentation) {
157 case LTTNG_KERNEL_KPROBE:
158 old_event.u.kprobe.addr = ev->u.kprobe.addr;
159 old_event.u.kprobe.offset = ev->u.kprobe.offset;
160 memcpy(old_event.u.kprobe.symbol_name,
161 ev->u.kprobe.symbol_name,
162 sizeof(old_event.u.kprobe.symbol_name));
163 break;
164 case LTTNG_KERNEL_KRETPROBE:
165 old_event.u.kretprobe.addr = ev->u.kretprobe.addr;
166 old_event.u.kretprobe.offset = ev->u.kretprobe.offset;
167 memcpy(old_event.u.kretprobe.symbol_name,
168 ev->u.kretprobe.symbol_name,
169 sizeof(old_event.u.kretprobe.symbol_name));
170 break;
171 case LTTNG_KERNEL_FUNCTION:
172 memcpy(old_event.u.ftrace.symbol_name,
173 ev->u.ftrace.symbol_name,
174 sizeof(old_event.u.ftrace.symbol_name));
175 break;
176 default:
177 break;
178 }
179
180 return ioctl(fd, LTTNG_KERNEL_OLD_EVENT, &old_event);
181 }
964ccb60 182 return ioctl(fd, LTTNG_KERNEL_EVENT, ev);
16421f6e
DG
183}
184
964ccb60 185int kernctl_add_context(int fd, struct lttng_kernel_context *ctx)
16421f6e 186{
4dbc372b
JD
187 if (lttng_kernel_use_old_abi) {
188 struct lttng_kernel_old_context old_ctx;
189
190 old_ctx.ctx = ctx->ctx;
191 /* only type that uses the union */
aa3514e9 192 if (ctx->ctx == LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER) {
4dbc372b
JD
193 old_ctx.u.perf_counter.type =
194 ctx->u.perf_counter.type;
195 old_ctx.u.perf_counter.config =
196 ctx->u.perf_counter.config;
197 memcpy(old_ctx.u.perf_counter.name,
198 ctx->u.perf_counter.name,
199 sizeof(old_ctx.u.perf_counter.name));
200 }
201 return ioctl(fd, LTTNG_KERNEL_OLD_CONTEXT, &old_ctx);
202 }
964ccb60 203 return ioctl(fd, LTTNG_KERNEL_CONTEXT, ctx);
16421f6e
DG
204}
205
964ccb60 206
f3ed775e
DG
207/* Enable event, channel and session ioctl */
208int kernctl_enable(int fd)
209{
4dbc372b
JD
210 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_ENABLE,
211 LTTNG_KERNEL_ENABLE);
f3ed775e
DG
212}
213
214/* Disable event, channel and session ioctl */
215int kernctl_disable(int fd)
216{
4dbc372b
JD
217 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_DISABLE,
218 LTTNG_KERNEL_DISABLE);
f3ed775e
DG
219}
220
964ccb60 221int kernctl_start_session(int fd)
16421f6e 222{
4dbc372b
JD
223 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_START,
224 LTTNG_KERNEL_SESSION_START);
964ccb60
MD
225}
226
227int kernctl_stop_session(int fd)
228{
4dbc372b
JD
229 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_STOP,
230 LTTNG_KERNEL_SESSION_STOP);
16421f6e
DG
231}
232
964ccb60
MD
233int kernctl_tracepoint_list(int fd)
234{
4dbc372b
JD
235 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_TRACEPOINT_LIST,
236 LTTNG_KERNEL_TRACEPOINT_LIST);
964ccb60
MD
237}
238
239int kernctl_tracer_version(int fd, struct lttng_kernel_tracer_version *v)
240{
4dbc372b
JD
241 int ret;
242
243 if (lttng_kernel_use_old_abi == -1) {
244 ret = ioctl(fd, LTTNG_KERNEL_TRACER_VERSION, v);
245 if (!ret) {
246 lttng_kernel_use_old_abi = 0;
247 goto end;
248 }
249 lttng_kernel_use_old_abi = 1;
250 }
251 if (lttng_kernel_use_old_abi) {
252 struct lttng_kernel_old_tracer_version old_v;
253
254 ret = ioctl(fd, LTTNG_KERNEL_OLD_TRACER_VERSION, &old_v);
255 if (ret) {
256 goto end;
257 }
258 v->major = old_v.major;
259 v->minor = old_v.minor;
260 v->patchlevel = old_v.patchlevel;
261 } else {
262 ret = ioctl(fd, LTTNG_KERNEL_TRACER_VERSION, v);
263 }
264
265end:
266 return ret;
964ccb60
MD
267}
268
269int kernctl_wait_quiescent(int fd)
270{
4dbc372b
JD
271 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_WAIT_QUIESCENT,
272 LTTNG_KERNEL_WAIT_QUIESCENT);
964ccb60
MD
273}
274
275int kernctl_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
276{
4dbc372b
JD
277 int ret;
278
279 if (lttng_kernel_use_old_abi == -1) {
280 ret = ioctl(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
281 if (!ret) {
282 lttng_kernel_use_old_abi = 0;
283 goto end;
284 }
285 lttng_kernel_use_old_abi = 1;
286 }
287 if (lttng_kernel_use_old_abi) {
288 struct lttng_kernel_old_calibrate old_calibrate;
289
290 old_calibrate.type = calibrate->type;
291 ret = ioctl(fd, LTTNG_KERNEL_OLD_CALIBRATE, &old_calibrate);
292 if (ret) {
293 goto end;
294 }
295 calibrate->type = old_calibrate.type;
296 } else {
297 ret = ioctl(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
298 }
299
300end:
301 return ret;
964ccb60
MD
302}
303
304
305int kernctl_buffer_flush(int fd)
306{
307 return ioctl(fd, RING_BUFFER_FLUSH);
308}
309
310
311/* Buffer operations */
312
313/* For mmap mode, readable without "get" operation */
314
16421f6e
DG
315/* returns the length to mmap. */
316int kernctl_get_mmap_len(int fd, unsigned long *len)
317{
318 return ioctl(fd, RING_BUFFER_GET_MMAP_LEN, len);
319}
320
964ccb60
MD
321/* returns the maximum size for sub-buffers. */
322int kernctl_get_max_subbuf_size(int fd, unsigned long *len)
323{
324 return ioctl(fd, RING_BUFFER_GET_MAX_SUBBUF_SIZE, len);
325}
326
327/*
328 * For mmap mode, operate on the current packet (between get/put or
329 * get_next/put_next).
330 */
331
16421f6e
DG
332/* returns the offset of the subbuffer belonging to the mmap reader. */
333int kernctl_get_mmap_read_offset(int fd, unsigned long *off)
334{
335 return ioctl(fd, RING_BUFFER_GET_MMAP_READ_OFFSET, off);
336}
337
964ccb60
MD
338/* returns the size of the current sub-buffer, without padding (for mmap). */
339int kernctl_get_subbuf_size(int fd, unsigned long *len)
16421f6e 340{
964ccb60 341 return ioctl(fd, RING_BUFFER_GET_SUBBUF_SIZE, len);
16421f6e
DG
342}
343
344/* returns the size of the current sub-buffer, without padding (for mmap). */
345int kernctl_get_padded_subbuf_size(int fd, unsigned long *len)
346{
347 return ioctl(fd, RING_BUFFER_GET_PADDED_SUBBUF_SIZE, len);
348}
349
964ccb60
MD
350/* Get exclusive read access to the next sub-buffer that can be read. */
351int kernctl_get_next_subbuf(int fd)
16421f6e 352{
964ccb60 353 return ioctl(fd, RING_BUFFER_GET_NEXT_SUBBUF);
16421f6e
DG
354}
355
d4a1283e 356
16421f6e
DG
357/* Release exclusive sub-buffer access, move consumer forward. */
358int kernctl_put_next_subbuf(int fd)
359{
360 return ioctl(fd, RING_BUFFER_PUT_NEXT_SUBBUF);
361}
362
964ccb60 363/* snapshot */
16421f6e
DG
364
365/* Get a snapshot of the current ring buffer producer and consumer positions */
366int kernctl_snapshot(int fd)
367{
368 return ioctl(fd, RING_BUFFER_SNAPSHOT);
369}
370
371/* Get the consumer position (iteration start) */
372int kernctl_snapshot_get_consumed(int fd, unsigned long *pos)
373{
374 return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_CONSUMED, pos);
375}
376
377/* Get the producer position (iteration end) */
378int kernctl_snapshot_get_produced(int fd, unsigned long *pos)
379{
380 return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_PRODUCED, pos);
381}
382
964ccb60
MD
383/* Get exclusive read access to the specified sub-buffer position */
384int kernctl_get_subbuf(int fd, unsigned long *len)
f3ed775e 385{
964ccb60 386 return ioctl(fd, RING_BUFFER_GET_SUBBUF, len);
f3ed775e 387}
d0254c7c 388
964ccb60
MD
389/* Release exclusive sub-buffer access */
390int kernctl_put_subbuf(int fd)
d0254c7c 391{
964ccb60 392 return ioctl(fd, RING_BUFFER_PUT_SUBBUF);
d0254c7c 393}
309167d2
JD
394
395/* Returns the timestamp begin of the current sub-buffer. */
396int kernctl_get_timestamp_begin(int fd, uint64_t *timestamp_begin)
397{
398 return ioctl(fd, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN, timestamp_begin);
399}
400
401/* Returns the timestamp end of the current sub-buffer. */
402int kernctl_get_timestamp_end(int fd, uint64_t *timestamp_end)
403{
404 return ioctl(fd, LTTNG_RING_BUFFER_GET_TIMESTAMP_END, timestamp_end);
405}
406
407/* Returns the number of discarded events in the current sub-buffer. */
408int kernctl_get_events_discarded(int fd, uint64_t *events_discarded)
409{
410 return ioctl(fd, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED, events_discarded);
411}
412
413/* Returns the content size in the current sub-buffer. */
414int kernctl_get_content_size(int fd, uint64_t *content_size)
415{
416 return ioctl(fd, LTTNG_RING_BUFFER_GET_CONTENT_SIZE, content_size);
417}
418
419/* Returns the packet size in the current sub-buffer. */
420int kernctl_get_packet_size(int fd, uint64_t *packet_size)
421{
422 return ioctl(fd, LTTNG_RING_BUFFER_GET_PACKET_SIZE, packet_size);
423}
424
425/* Returns the stream id of the current sub-buffer. */
426int kernctl_get_stream_id(int fd, uint64_t *stream_id)
427{
428 return ioctl(fd, LTTNG_RING_BUFFER_GET_STREAM_ID, stream_id);
429}
d3e2ba59
JD
430
431/* Returns the current timestamp. */
432int kernctl_get_current_timestamp(int fd, uint64_t *ts)
433{
434 return ioctl(fd, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP, ts);
435}
This page took 0.057059 seconds and 4 git commands to generate.