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