Fix: enforce assumption that lttng-modules ioctl() return <= 0
[lttng-tools.git] / src / common / kernel-ctl / kernel-ctl.c
... / ...
CommitLineData
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 */
44static 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 */
55static 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
74end:
75 return ret;
76}
77
78int 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 */
85int kernctl_open_metadata(int fd, struct lttng_channel_attr *chops)
86{
87 struct lttng_kernel_old_channel old_channel;
88 struct lttng_kernel_channel channel;
89
90 if (lttng_kernel_use_old_abi) {
91 old_channel.overwrite = chops->overwrite;
92 old_channel.subbuf_size = chops->subbuf_size;
93 old_channel.num_subbuf = chops->num_subbuf;
94 old_channel.switch_timer_interval = chops->switch_timer_interval;
95 old_channel.read_timer_interval = chops->read_timer_interval;
96 old_channel.output = chops->output;
97
98 memset(old_channel.padding, 0, sizeof(old_channel.padding));
99 /*
100 * The new channel padding is smaller than the old ABI so we use the
101 * new ABI padding size for the memcpy.
102 */
103 memcpy(old_channel.padding, chops->padding, sizeof(chops->padding));
104
105 return ioctl(fd, LTTNG_KERNEL_OLD_METADATA, &old_channel);
106 }
107
108 channel.overwrite = chops->overwrite;
109 channel.subbuf_size = chops->subbuf_size;
110 channel.num_subbuf = chops->num_subbuf;
111 channel.switch_timer_interval = chops->switch_timer_interval;
112 channel.read_timer_interval = chops->read_timer_interval;
113 channel.output = chops->output;
114 memcpy(channel.padding, chops->padding, sizeof(chops->padding));
115
116 return ioctl(fd, LTTNG_KERNEL_METADATA, &channel);
117}
118
119int kernctl_create_channel(int fd, struct lttng_channel_attr *chops)
120{
121 struct lttng_kernel_channel channel;
122
123 memset(&channel, 0, sizeof(channel));
124 if (lttng_kernel_use_old_abi) {
125 struct lttng_kernel_old_channel old_channel;
126
127 old_channel.overwrite = chops->overwrite;
128 old_channel.subbuf_size = chops->subbuf_size;
129 old_channel.num_subbuf = chops->num_subbuf;
130 old_channel.switch_timer_interval = chops->switch_timer_interval;
131 old_channel.read_timer_interval = chops->read_timer_interval;
132 old_channel.output = chops->output;
133
134 memset(old_channel.padding, 0, sizeof(old_channel.padding));
135 /*
136 * The new channel padding is smaller than the old ABI so we use the
137 * new ABI padding size for the memcpy.
138 */
139 memcpy(old_channel.padding, chops->padding, sizeof(chops->padding));
140
141 return ioctl(fd, LTTNG_KERNEL_OLD_CHANNEL, &old_channel);
142 }
143
144 channel.overwrite = chops->overwrite;
145 channel.subbuf_size = chops->subbuf_size;
146 channel.num_subbuf = chops->num_subbuf;
147 channel.switch_timer_interval = chops->switch_timer_interval;
148 channel.read_timer_interval = chops->read_timer_interval;
149 channel.output = chops->output;
150 memcpy(channel.padding, chops->padding, sizeof(chops->padding));
151
152 return ioctl(fd, LTTNG_KERNEL_CHANNEL, &channel);
153}
154
155int kernctl_syscall_mask(int fd, char **syscall_mask, uint32_t *nr_bits)
156{
157 struct lttng_kernel_syscall_mask kmask_len, *kmask = NULL;
158 size_t array_alloc_len;
159 char *new_mask;
160 int ret = 0;
161
162 if (!syscall_mask) {
163 ret = -1;
164 goto end;
165 }
166
167 if (!nr_bits) {
168 ret = -1;
169 goto end;
170 }
171
172 kmask_len.len = 0;
173 ret = LTTNG_IOCTL(fd, LTTNG_KERNEL_SYSCALL_MASK, &kmask_len);
174 if (ret) {
175 goto end;
176 }
177
178 array_alloc_len = ALIGN(kmask_len.len, 8) >> 3;
179
180 kmask = zmalloc(sizeof(*kmask) + array_alloc_len);
181 if (!kmask) {
182 ret = -1;
183 goto end;
184 }
185
186 kmask->len = kmask_len.len;
187 ret = LTTNG_IOCTL(fd, LTTNG_KERNEL_SYSCALL_MASK, kmask);
188 if (ret) {
189 goto end;
190 }
191
192 new_mask = realloc(*syscall_mask, array_alloc_len);
193 if (!new_mask) {
194 ret = -1;
195 goto end;
196 }
197 memcpy(new_mask, kmask->mask, array_alloc_len);
198 *syscall_mask = new_mask;
199 *nr_bits = kmask->len;
200
201end:
202 free(kmask);
203 return ret;
204}
205
206int kernctl_track_pid(int fd, int pid)
207{
208 return LTTNG_IOCTL(fd, LTTNG_KERNEL_SESSION_TRACK_PID, pid);
209}
210
211int kernctl_untrack_pid(int fd, int pid)
212{
213 return LTTNG_IOCTL(fd, LTTNG_KERNEL_SESSION_UNTRACK_PID, pid);
214}
215
216int kernctl_list_tracker_pids(int fd)
217{
218 return ioctl(fd, LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS);
219}
220
221int kernctl_session_metadata_regenerate(int fd)
222{
223 return LTTNG_IOCTL(fd, LTTNG_KERNEL_SESSION_METADATA_REGEN);
224}
225
226int kernctl_create_stream(int fd)
227{
228 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_STREAM,
229 LTTNG_KERNEL_STREAM);
230}
231
232int kernctl_create_event(int fd, struct lttng_kernel_event *ev)
233{
234 if (lttng_kernel_use_old_abi) {
235 struct lttng_kernel_old_event old_event;
236
237 memcpy(old_event.name, ev->name, sizeof(old_event.name));
238 old_event.instrumentation = ev->instrumentation;
239 switch (ev->instrumentation) {
240 case LTTNG_KERNEL_KPROBE:
241 old_event.u.kprobe.addr = ev->u.kprobe.addr;
242 old_event.u.kprobe.offset = ev->u.kprobe.offset;
243 memcpy(old_event.u.kprobe.symbol_name,
244 ev->u.kprobe.symbol_name,
245 sizeof(old_event.u.kprobe.symbol_name));
246 break;
247 case LTTNG_KERNEL_KRETPROBE:
248 old_event.u.kretprobe.addr = ev->u.kretprobe.addr;
249 old_event.u.kretprobe.offset = ev->u.kretprobe.offset;
250 memcpy(old_event.u.kretprobe.symbol_name,
251 ev->u.kretprobe.symbol_name,
252 sizeof(old_event.u.kretprobe.symbol_name));
253 break;
254 case LTTNG_KERNEL_FUNCTION:
255 memcpy(old_event.u.ftrace.symbol_name,
256 ev->u.ftrace.symbol_name,
257 sizeof(old_event.u.ftrace.symbol_name));
258 break;
259 default:
260 break;
261 }
262
263 return ioctl(fd, LTTNG_KERNEL_OLD_EVENT, &old_event);
264 }
265 return ioctl(fd, LTTNG_KERNEL_EVENT, ev);
266}
267
268int kernctl_add_context(int fd, struct lttng_kernel_context *ctx)
269{
270 if (lttng_kernel_use_old_abi) {
271 struct lttng_kernel_old_context old_ctx;
272
273 old_ctx.ctx = ctx->ctx;
274 /* only type that uses the union */
275 if (ctx->ctx == LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER) {
276 old_ctx.u.perf_counter.type =
277 ctx->u.perf_counter.type;
278 old_ctx.u.perf_counter.config =
279 ctx->u.perf_counter.config;
280 memcpy(old_ctx.u.perf_counter.name,
281 ctx->u.perf_counter.name,
282 sizeof(old_ctx.u.perf_counter.name));
283 }
284 return LTTNG_IOCTL(fd, LTTNG_KERNEL_OLD_CONTEXT, &old_ctx);
285 }
286 return LTTNG_IOCTL(fd, LTTNG_KERNEL_CONTEXT, ctx);
287}
288
289
290/* Enable event, channel and session LTTNG_IOCTL */
291int kernctl_enable(int fd)
292{
293 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_ENABLE,
294 LTTNG_KERNEL_ENABLE);
295}
296
297/* Disable event, channel and session LTTNG_IOCTL */
298int kernctl_disable(int fd)
299{
300 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_DISABLE,
301 LTTNG_KERNEL_DISABLE);
302}
303
304int kernctl_start_session(int fd)
305{
306 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_START,
307 LTTNG_KERNEL_SESSION_START);
308}
309
310int kernctl_stop_session(int fd)
311{
312 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_STOP,
313 LTTNG_KERNEL_SESSION_STOP);
314}
315
316int kernctl_filter(int fd, struct lttng_filter_bytecode *filter)
317{
318 struct lttng_kernel_filter_bytecode *kb;
319 uint32_t len;
320 int ret;
321
322 /* Translate bytecode to kernel bytecode */
323 kb = zmalloc(sizeof(*kb) + filter->len);
324 if (!kb)
325 return -ENOMEM;
326 kb->len = len = filter->len;
327 kb->reloc_offset = filter->reloc_table_offset;
328 kb->seqnum = filter->seqnum;
329 memcpy(kb->data, filter->data, len);
330 ret = LTTNG_IOCTL(fd, LTTNG_KERNEL_FILTER, kb);
331 free(kb);
332 return ret;
333}
334
335int kernctl_tracepoint_list(int fd)
336{
337 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_TRACEPOINT_LIST,
338 LTTNG_KERNEL_TRACEPOINT_LIST);
339}
340
341int kernctl_syscall_list(int fd)
342{
343 return ioctl(fd, LTTNG_KERNEL_SYSCALL_LIST);
344}
345
346int kernctl_tracer_version(int fd, struct lttng_kernel_tracer_version *v)
347{
348 int ret;
349
350 if (lttng_kernel_use_old_abi == -1) {
351 ret = LTTNG_IOCTL(fd, LTTNG_KERNEL_TRACER_VERSION, v);
352 if (!ret) {
353 lttng_kernel_use_old_abi = 0;
354 goto end;
355 }
356 lttng_kernel_use_old_abi = 1;
357 }
358 if (lttng_kernel_use_old_abi) {
359 struct lttng_kernel_old_tracer_version old_v;
360
361 ret = LTTNG_IOCTL(fd, LTTNG_KERNEL_OLD_TRACER_VERSION, &old_v);
362 if (ret) {
363 goto end;
364 }
365 v->major = old_v.major;
366 v->minor = old_v.minor;
367 v->patchlevel = old_v.patchlevel;
368 } else {
369 ret = LTTNG_IOCTL(fd, LTTNG_KERNEL_TRACER_VERSION, v);
370 }
371
372end:
373 return ret;
374}
375
376int kernctl_tracer_abi_version(int fd,
377 struct lttng_kernel_tracer_abi_version *v)
378{
379 return LTTNG_IOCTL(fd, LTTNG_KERNEL_TRACER_ABI_VERSION, v);
380}
381
382int kernctl_wait_quiescent(int fd)
383{
384 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_WAIT_QUIESCENT,
385 LTTNG_KERNEL_WAIT_QUIESCENT);
386}
387
388int kernctl_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
389{
390 int ret;
391
392 if (lttng_kernel_use_old_abi == -1) {
393 ret = LTTNG_IOCTL(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
394 if (!ret) {
395 lttng_kernel_use_old_abi = 0;
396 goto end;
397 }
398 lttng_kernel_use_old_abi = 1;
399 }
400 if (lttng_kernel_use_old_abi) {
401 struct lttng_kernel_old_calibrate old_calibrate;
402
403 old_calibrate.type = calibrate->type;
404 ret = LTTNG_IOCTL(fd, LTTNG_KERNEL_OLD_CALIBRATE,
405 &old_calibrate);
406 if (ret) {
407 goto end;
408 }
409 calibrate->type = old_calibrate.type;
410 } else {
411 ret = LTTNG_IOCTL(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
412 }
413
414end:
415 return ret;
416}
417
418
419int kernctl_buffer_flush(int fd)
420{
421 return LTTNG_IOCTL(fd, RING_BUFFER_FLUSH);
422}
423
424/* returns the version of the metadata. */
425int kernctl_get_metadata_version(int fd, uint64_t *version)
426{
427 return LTTNG_IOCTL(fd, RING_BUFFER_GET_METADATA_VERSION, version);
428}
429
430
431/* Buffer operations */
432
433/* For mmap mode, readable without "get" operation */
434
435/* returns the length to mmap. */
436int kernctl_get_mmap_len(int fd, unsigned long *len)
437{
438 return LTTNG_IOCTL(fd, RING_BUFFER_GET_MMAP_LEN, len);
439}
440
441/* returns the maximum size for sub-buffers. */
442int kernctl_get_max_subbuf_size(int fd, unsigned long *len)
443{
444 return LTTNG_IOCTL(fd, RING_BUFFER_GET_MAX_SUBBUF_SIZE, len);
445}
446
447/*
448 * For mmap mode, operate on the current packet (between get/put or
449 * get_next/put_next).
450 */
451
452/* returns the offset of the subbuffer belonging to the mmap reader. */
453int kernctl_get_mmap_read_offset(int fd, unsigned long *off)
454{
455 return LTTNG_IOCTL(fd, RING_BUFFER_GET_MMAP_READ_OFFSET, off);
456}
457
458/* returns the size of the current sub-buffer, without padding (for mmap). */
459int kernctl_get_subbuf_size(int fd, unsigned long *len)
460{
461 return LTTNG_IOCTL(fd, RING_BUFFER_GET_SUBBUF_SIZE, len);
462}
463
464/* returns the size of the current sub-buffer, without padding (for mmap). */
465int kernctl_get_padded_subbuf_size(int fd, unsigned long *len)
466{
467 return LTTNG_IOCTL(fd, RING_BUFFER_GET_PADDED_SUBBUF_SIZE, len);
468}
469
470/* Get exclusive read access to the next sub-buffer that can be read. */
471int kernctl_get_next_subbuf(int fd)
472{
473 return LTTNG_IOCTL(fd, RING_BUFFER_GET_NEXT_SUBBUF);
474}
475
476
477/* Release exclusive sub-buffer access, move consumer forward. */
478int kernctl_put_next_subbuf(int fd)
479{
480 return LTTNG_IOCTL(fd, RING_BUFFER_PUT_NEXT_SUBBUF);
481}
482
483/* snapshot */
484
485/* Get a snapshot of the current ring buffer producer and consumer positions */
486int kernctl_snapshot(int fd)
487{
488 return LTTNG_IOCTL(fd, RING_BUFFER_SNAPSHOT);
489}
490
491/* Get the consumer position (iteration start) */
492int kernctl_snapshot_get_consumed(int fd, unsigned long *pos)
493{
494 return LTTNG_IOCTL(fd, RING_BUFFER_SNAPSHOT_GET_CONSUMED, pos);
495}
496
497/* Get the producer position (iteration end) */
498int kernctl_snapshot_get_produced(int fd, unsigned long *pos)
499{
500 return LTTNG_IOCTL(fd, RING_BUFFER_SNAPSHOT_GET_PRODUCED, pos);
501}
502
503/* Get exclusive read access to the specified sub-buffer position */
504int kernctl_get_subbuf(int fd, unsigned long *len)
505{
506 return LTTNG_IOCTL(fd, RING_BUFFER_GET_SUBBUF, len);
507}
508
509/* Release exclusive sub-buffer access */
510int kernctl_put_subbuf(int fd)
511{
512 return LTTNG_IOCTL(fd, RING_BUFFER_PUT_SUBBUF);
513}
514
515/* Returns the timestamp begin of the current sub-buffer. */
516int kernctl_get_timestamp_begin(int fd, uint64_t *timestamp_begin)
517{
518 return LTTNG_IOCTL(fd, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN,
519 timestamp_begin);
520}
521
522/* Returns the timestamp end of the current sub-buffer. */
523int kernctl_get_timestamp_end(int fd, uint64_t *timestamp_end)
524{
525 return LTTNG_IOCTL(fd, LTTNG_RING_BUFFER_GET_TIMESTAMP_END,
526 timestamp_end);
527}
528
529/* Returns the number of discarded events in the current sub-buffer. */
530int kernctl_get_events_discarded(int fd, uint64_t *events_discarded)
531{
532 return LTTNG_IOCTL(fd, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED,
533 events_discarded);
534}
535
536/* Returns the content size in the current sub-buffer. */
537int kernctl_get_content_size(int fd, uint64_t *content_size)
538{
539 return LTTNG_IOCTL(fd, LTTNG_RING_BUFFER_GET_CONTENT_SIZE,
540 content_size);
541}
542
543/* Returns the packet size in the current sub-buffer. */
544int kernctl_get_packet_size(int fd, uint64_t *packet_size)
545{
546 return LTTNG_IOCTL(fd, LTTNG_RING_BUFFER_GET_PACKET_SIZE, packet_size);
547}
548
549/* Returns the stream id of the current sub-buffer. */
550int kernctl_get_stream_id(int fd, uint64_t *stream_id)
551{
552 return LTTNG_IOCTL(fd, LTTNG_RING_BUFFER_GET_STREAM_ID, stream_id);
553}
554
555/* Returns the current timestamp. */
556int kernctl_get_current_timestamp(int fd, uint64_t *ts)
557{
558 return LTTNG_IOCTL(fd, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP, ts);
559}
560
561/* Returns the packet sequence number of the current sub-buffer. */
562int kernctl_get_sequence_number(int fd, uint64_t *seq)
563{
564 return LTTNG_IOCTL(fd, LTTNG_RING_BUFFER_GET_SEQ_NUM, seq);
565}
566
567/* Returns the stream instance id. */
568int kernctl_get_instance_id(int fd, uint64_t *id)
569{
570 return LTTNG_IOCTL(fd, LTTNG_RING_BUFFER_INSTANCE_ID, id);
571}
This page took 0.025408 seconds and 5 git commands to generate.