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