Implement PID tracker content listing
[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_enable_syscall(int fd, const char *syscall_name)
146 {
147 struct lttng_kernel_event event;
148
149 memset(&event, 0, sizeof(event));
150 strncpy(event.name, syscall_name, sizeof(event.name));
151 event.name[sizeof(event.name) - 1] = '\0';
152 event.instrumentation = LTTNG_KERNEL_SYSCALL;
153 event.u.syscall.enable = 1;
154 return ioctl(fd, LTTNG_KERNEL_EVENT, &event);
155 }
156
157 int kernctl_disable_syscall(int fd, const char *syscall_name)
158 {
159 struct lttng_kernel_event event;
160
161 memset(&event, 0, sizeof(event));
162 strncpy(event.name, syscall_name, sizeof(event.name));
163 event.name[sizeof(event.name) - 1] = '\0';
164 event.instrumentation = LTTNG_KERNEL_SYSCALL;
165 event.u.syscall.enable = 0;
166 return ioctl(fd, LTTNG_KERNEL_EVENT, &event);
167 }
168
169 int kernctl_syscall_mask(int fd, char **syscall_mask, uint32_t *nr_bits)
170 {
171 struct lttng_kernel_syscall_mask kmask_len, *kmask = NULL;
172 size_t array_alloc_len;
173 char *new_mask;
174 int ret = 0;
175
176 if (!syscall_mask) {
177 ret = -1;
178 goto end;
179 }
180
181 if (!nr_bits) {
182 ret = -1;
183 goto end;
184 }
185
186 kmask_len.len = 0;
187 ret = ioctl(fd, LTTNG_KERNEL_SYSCALL_MASK, &kmask_len);
188 if (ret) {
189 goto end;
190 }
191
192 array_alloc_len = ALIGN(kmask_len.len, 8) >> 3;
193
194 kmask = zmalloc(sizeof(*kmask) + array_alloc_len);
195 if (!kmask) {
196 ret = -1;
197 goto end;
198 }
199
200 kmask->len = kmask_len.len;
201 ret = ioctl(fd, LTTNG_KERNEL_SYSCALL_MASK, kmask);
202 if (ret) {
203 goto end;
204 }
205
206 new_mask = realloc(*syscall_mask, array_alloc_len);
207 if (!new_mask) {
208 ret = -1;
209 goto end;
210 }
211 memcpy(new_mask, kmask->mask, array_alloc_len);
212 *syscall_mask = new_mask;
213 *nr_bits = kmask->len;
214
215 end:
216 free(kmask);
217 return ret;
218 }
219
220 int kernctl_track_pid(int fd, int pid)
221 {
222 return ioctl(fd, LTTNG_KERNEL_SESSION_TRACK_PID, pid);
223 }
224
225 int kernctl_untrack_pid(int fd, int pid)
226 {
227 return ioctl(fd, LTTNG_KERNEL_SESSION_UNTRACK_PID, pid);
228 }
229
230 int kernctl_list_tracker_pids(int fd)
231 {
232 return ioctl(fd, LTTNG_KERNEL_SESSION_LIST_TRACKER_PIDS);
233 }
234
235 int kernctl_create_stream(int fd)
236 {
237 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_STREAM,
238 LTTNG_KERNEL_STREAM);
239 }
240
241 int kernctl_create_event(int fd, struct lttng_kernel_event *ev)
242 {
243 if (lttng_kernel_use_old_abi) {
244 struct lttng_kernel_old_event old_event;
245
246 memcpy(old_event.name, ev->name, sizeof(old_event.name));
247 old_event.instrumentation = ev->instrumentation;
248 switch (ev->instrumentation) {
249 case LTTNG_KERNEL_KPROBE:
250 old_event.u.kprobe.addr = ev->u.kprobe.addr;
251 old_event.u.kprobe.offset = ev->u.kprobe.offset;
252 memcpy(old_event.u.kprobe.symbol_name,
253 ev->u.kprobe.symbol_name,
254 sizeof(old_event.u.kprobe.symbol_name));
255 break;
256 case LTTNG_KERNEL_KRETPROBE:
257 old_event.u.kretprobe.addr = ev->u.kretprobe.addr;
258 old_event.u.kretprobe.offset = ev->u.kretprobe.offset;
259 memcpy(old_event.u.kretprobe.symbol_name,
260 ev->u.kretprobe.symbol_name,
261 sizeof(old_event.u.kretprobe.symbol_name));
262 break;
263 case LTTNG_KERNEL_FUNCTION:
264 memcpy(old_event.u.ftrace.symbol_name,
265 ev->u.ftrace.symbol_name,
266 sizeof(old_event.u.ftrace.symbol_name));
267 break;
268 default:
269 break;
270 }
271
272 return ioctl(fd, LTTNG_KERNEL_OLD_EVENT, &old_event);
273 }
274 return ioctl(fd, LTTNG_KERNEL_EVENT, ev);
275 }
276
277 int kernctl_add_context(int fd, struct lttng_kernel_context *ctx)
278 {
279 if (lttng_kernel_use_old_abi) {
280 struct lttng_kernel_old_context old_ctx;
281
282 old_ctx.ctx = ctx->ctx;
283 /* only type that uses the union */
284 if (ctx->ctx == LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER) {
285 old_ctx.u.perf_counter.type =
286 ctx->u.perf_counter.type;
287 old_ctx.u.perf_counter.config =
288 ctx->u.perf_counter.config;
289 memcpy(old_ctx.u.perf_counter.name,
290 ctx->u.perf_counter.name,
291 sizeof(old_ctx.u.perf_counter.name));
292 }
293 return ioctl(fd, LTTNG_KERNEL_OLD_CONTEXT, &old_ctx);
294 }
295 return ioctl(fd, LTTNG_KERNEL_CONTEXT, ctx);
296 }
297
298
299 /* Enable event, channel and session ioctl */
300 int kernctl_enable(int fd)
301 {
302 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_ENABLE,
303 LTTNG_KERNEL_ENABLE);
304 }
305
306 /* Disable event, channel and session ioctl */
307 int kernctl_disable(int fd)
308 {
309 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_DISABLE,
310 LTTNG_KERNEL_DISABLE);
311 }
312
313 int kernctl_start_session(int fd)
314 {
315 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_START,
316 LTTNG_KERNEL_SESSION_START);
317 }
318
319 int kernctl_stop_session(int fd)
320 {
321 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_STOP,
322 LTTNG_KERNEL_SESSION_STOP);
323 }
324
325 int kernctl_tracepoint_list(int fd)
326 {
327 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_TRACEPOINT_LIST,
328 LTTNG_KERNEL_TRACEPOINT_LIST);
329 }
330
331 int kernctl_syscall_list(int fd)
332 {
333 return ioctl(fd, LTTNG_KERNEL_SYSCALL_LIST);
334 }
335
336 int kernctl_tracer_version(int fd, struct lttng_kernel_tracer_version *v)
337 {
338 int ret;
339
340 if (lttng_kernel_use_old_abi == -1) {
341 ret = ioctl(fd, LTTNG_KERNEL_TRACER_VERSION, v);
342 if (!ret) {
343 lttng_kernel_use_old_abi = 0;
344 goto end;
345 }
346 lttng_kernel_use_old_abi = 1;
347 }
348 if (lttng_kernel_use_old_abi) {
349 struct lttng_kernel_old_tracer_version old_v;
350
351 ret = ioctl(fd, LTTNG_KERNEL_OLD_TRACER_VERSION, &old_v);
352 if (ret) {
353 goto end;
354 }
355 v->major = old_v.major;
356 v->minor = old_v.minor;
357 v->patchlevel = old_v.patchlevel;
358 } else {
359 ret = ioctl(fd, LTTNG_KERNEL_TRACER_VERSION, v);
360 }
361
362 end:
363 return ret;
364 }
365
366 int kernctl_tracer_abi_version(int fd,
367 struct lttng_kernel_tracer_abi_version *v)
368 {
369 return ioctl(fd, LTTNG_KERNEL_TRACER_ABI_VERSION, v);
370 }
371
372 int kernctl_wait_quiescent(int fd)
373 {
374 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_WAIT_QUIESCENT,
375 LTTNG_KERNEL_WAIT_QUIESCENT);
376 }
377
378 int kernctl_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
379 {
380 int ret;
381
382 if (lttng_kernel_use_old_abi == -1) {
383 ret = ioctl(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
384 if (!ret) {
385 lttng_kernel_use_old_abi = 0;
386 goto end;
387 }
388 lttng_kernel_use_old_abi = 1;
389 }
390 if (lttng_kernel_use_old_abi) {
391 struct lttng_kernel_old_calibrate old_calibrate;
392
393 old_calibrate.type = calibrate->type;
394 ret = ioctl(fd, LTTNG_KERNEL_OLD_CALIBRATE, &old_calibrate);
395 if (ret) {
396 goto end;
397 }
398 calibrate->type = old_calibrate.type;
399 } else {
400 ret = ioctl(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
401 }
402
403 end:
404 return ret;
405 }
406
407
408 int kernctl_buffer_flush(int fd)
409 {
410 return ioctl(fd, RING_BUFFER_FLUSH);
411 }
412
413
414 /* Buffer operations */
415
416 /* For mmap mode, readable without "get" operation */
417
418 /* returns the length to mmap. */
419 int kernctl_get_mmap_len(int fd, unsigned long *len)
420 {
421 return ioctl(fd, RING_BUFFER_GET_MMAP_LEN, len);
422 }
423
424 /* returns the maximum size for sub-buffers. */
425 int kernctl_get_max_subbuf_size(int fd, unsigned long *len)
426 {
427 return ioctl(fd, RING_BUFFER_GET_MAX_SUBBUF_SIZE, len);
428 }
429
430 /*
431 * For mmap mode, operate on the current packet (between get/put or
432 * get_next/put_next).
433 */
434
435 /* returns the offset of the subbuffer belonging to the mmap reader. */
436 int kernctl_get_mmap_read_offset(int fd, unsigned long *off)
437 {
438 return ioctl(fd, RING_BUFFER_GET_MMAP_READ_OFFSET, off);
439 }
440
441 /* returns the size of the current sub-buffer, without padding (for mmap). */
442 int kernctl_get_subbuf_size(int fd, unsigned long *len)
443 {
444 return ioctl(fd, RING_BUFFER_GET_SUBBUF_SIZE, len);
445 }
446
447 /* returns the size of the current sub-buffer, without padding (for mmap). */
448 int kernctl_get_padded_subbuf_size(int fd, unsigned long *len)
449 {
450 return ioctl(fd, RING_BUFFER_GET_PADDED_SUBBUF_SIZE, len);
451 }
452
453 /* Get exclusive read access to the next sub-buffer that can be read. */
454 int kernctl_get_next_subbuf(int fd)
455 {
456 return ioctl(fd, RING_BUFFER_GET_NEXT_SUBBUF);
457 }
458
459
460 /* Release exclusive sub-buffer access, move consumer forward. */
461 int kernctl_put_next_subbuf(int fd)
462 {
463 return ioctl(fd, RING_BUFFER_PUT_NEXT_SUBBUF);
464 }
465
466 /* snapshot */
467
468 /* Get a snapshot of the current ring buffer producer and consumer positions */
469 int kernctl_snapshot(int fd)
470 {
471 return ioctl(fd, RING_BUFFER_SNAPSHOT);
472 }
473
474 /* Get the consumer position (iteration start) */
475 int kernctl_snapshot_get_consumed(int fd, unsigned long *pos)
476 {
477 return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_CONSUMED, pos);
478 }
479
480 /* Get the producer position (iteration end) */
481 int kernctl_snapshot_get_produced(int fd, unsigned long *pos)
482 {
483 return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_PRODUCED, pos);
484 }
485
486 /* Get exclusive read access to the specified sub-buffer position */
487 int kernctl_get_subbuf(int fd, unsigned long *len)
488 {
489 return ioctl(fd, RING_BUFFER_GET_SUBBUF, len);
490 }
491
492 /* Release exclusive sub-buffer access */
493 int kernctl_put_subbuf(int fd)
494 {
495 return ioctl(fd, RING_BUFFER_PUT_SUBBUF);
496 }
497
498 /* Returns the timestamp begin of the current sub-buffer. */
499 int kernctl_get_timestamp_begin(int fd, uint64_t *timestamp_begin)
500 {
501 return ioctl(fd, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN, timestamp_begin);
502 }
503
504 /* Returns the timestamp end of the current sub-buffer. */
505 int kernctl_get_timestamp_end(int fd, uint64_t *timestamp_end)
506 {
507 return ioctl(fd, LTTNG_RING_BUFFER_GET_TIMESTAMP_END, timestamp_end);
508 }
509
510 /* Returns the number of discarded events in the current sub-buffer. */
511 int kernctl_get_events_discarded(int fd, uint64_t *events_discarded)
512 {
513 return ioctl(fd, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED, events_discarded);
514 }
515
516 /* Returns the content size in the current sub-buffer. */
517 int kernctl_get_content_size(int fd, uint64_t *content_size)
518 {
519 return ioctl(fd, LTTNG_RING_BUFFER_GET_CONTENT_SIZE, content_size);
520 }
521
522 /* Returns the packet size in the current sub-buffer. */
523 int kernctl_get_packet_size(int fd, uint64_t *packet_size)
524 {
525 return ioctl(fd, LTTNG_RING_BUFFER_GET_PACKET_SIZE, packet_size);
526 }
527
528 /* Returns the stream id of the current sub-buffer. */
529 int kernctl_get_stream_id(int fd, uint64_t *stream_id)
530 {
531 return ioctl(fd, LTTNG_RING_BUFFER_GET_STREAM_ID, stream_id);
532 }
533
534 /* Returns the current timestamp. */
535 int kernctl_get_current_timestamp(int fd, uint64_t *ts)
536 {
537 return ioctl(fd, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP, ts);
538 }
This page took 0.04104 seconds and 5 git commands to generate.