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