Implement UST PID tracker
[lttng-tools.git] / src / common / kernel-ctl / kernel-ctl.c
CommitLineData
ee0326c0 1/*
16421f6e
DG
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
ee0326c0 4 *
d14d33bf
AM
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.
ee0326c0
DG
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 *
d14d33bf
AM
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.
ee0326c0 17 */
16421f6e 18
6c1c0768
MD
19#define _GNU_SOURCE
20#define _LGPL_SOURCE
95ba0f2f 21#define __USE_LINUX_IOCTL_DEFS
16421f6e 22#include <sys/ioctl.h>
4dbc372b 23#include <string.h>
46820c8b 24#include <common/align.h>
16421f6e 25
10a8a223 26#include "kernel-ctl.h"
16421f6e 27#include "kernel-ioctl.h"
16421f6e 28
4dbc372b
JD
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 */
35static 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 */
46static 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
65end:
66 return ret;
67}
68
964ccb60 69int kernctl_create_session(int fd)
d65106b1 70{
4dbc372b
JD
71 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION,
72 LTTNG_KERNEL_SESSION);
d65106b1
DG
73}
74
964ccb60
MD
75/* open the metadata global channel */
76int kernctl_open_metadata(int fd, struct lttng_channel_attr *chops)
f3ed775e 77{
4dbc372b
JD
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;
f853c53a
DG
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));
4dbc372b
JD
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;
ea207e3b 105 memcpy(channel.padding, chops->padding, sizeof(chops->padding));
4dbc372b
JD
106
107 return ioctl(fd, LTTNG_KERNEL_METADATA, &channel);
f3ed775e
DG
108}
109
110int kernctl_create_channel(int fd, struct lttng_channel_attr *chops)
16421f6e 111{
4dbc372b
JD
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;
f853c53a
DG
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));
4dbc372b
JD
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;
ea207e3b 140 memcpy(channel.padding, chops->padding, sizeof(chops->padding));
4dbc372b
JD
141
142 return ioctl(fd, LTTNG_KERNEL_CHANNEL, &channel);
16421f6e
DG
143}
144
6e911cad
MD
145int 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;
562ab9a8 153 event.u.syscall.enable = 1;
6e911cad
MD
154 return ioctl(fd, LTTNG_KERNEL_EVENT, &event);
155}
156
157int 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;
562ab9a8 165 event.u.syscall.enable = 0;
6e911cad
MD
166 return ioctl(fd, LTTNG_KERNEL_EVENT, &event);
167}
168
46820c8b
MD
169int 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;
834978fd 193
46820c8b
MD
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
834978fd 206 new_mask = realloc(*syscall_mask, array_alloc_len);
46820c8b
MD
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
215end:
216 free(kmask);
217 return ret;
218}
219
ccf10263
MD
220int kernctl_track_pid(int fd, int pid)
221{
222 return ioctl(fd, LTTNG_KERNEL_SESSION_TRACK_PID, pid);
223}
224
225int kernctl_untrack_pid(int fd, int pid)
226{
227 return ioctl(fd, LTTNG_KERNEL_SESSION_UNTRACK_PID, pid);
228}
229
964ccb60 230int kernctl_create_stream(int fd)
16421f6e 231{
4dbc372b
JD
232 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_STREAM,
233 LTTNG_KERNEL_STREAM);
16421f6e
DG
234}
235
964ccb60 236int kernctl_create_event(int fd, struct lttng_kernel_event *ev)
16421f6e 237{
4dbc372b
JD
238 if (lttng_kernel_use_old_abi) {
239 struct lttng_kernel_old_event old_event;
240
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 }
964ccb60 269 return ioctl(fd, LTTNG_KERNEL_EVENT, ev);
16421f6e
DG
270}
271
964ccb60 272int kernctl_add_context(int fd, struct lttng_kernel_context *ctx)
16421f6e 273{
4dbc372b
JD
274 if (lttng_kernel_use_old_abi) {
275 struct lttng_kernel_old_context old_ctx;
276
277 old_ctx.ctx = ctx->ctx;
278 /* only type that uses the union */
aa3514e9 279 if (ctx->ctx == LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER) {
4dbc372b
JD
280 old_ctx.u.perf_counter.type =
281 ctx->u.perf_counter.type;
282 old_ctx.u.perf_counter.config =
283 ctx->u.perf_counter.config;
284 memcpy(old_ctx.u.perf_counter.name,
285 ctx->u.perf_counter.name,
286 sizeof(old_ctx.u.perf_counter.name));
287 }
288 return ioctl(fd, LTTNG_KERNEL_OLD_CONTEXT, &old_ctx);
289 }
964ccb60 290 return ioctl(fd, LTTNG_KERNEL_CONTEXT, ctx);
16421f6e
DG
291}
292
964ccb60 293
f3ed775e
DG
294/* Enable event, channel and session ioctl */
295int kernctl_enable(int fd)
296{
4dbc372b
JD
297 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_ENABLE,
298 LTTNG_KERNEL_ENABLE);
f3ed775e
DG
299}
300
301/* Disable event, channel and session ioctl */
302int kernctl_disable(int fd)
303{
4dbc372b
JD
304 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_DISABLE,
305 LTTNG_KERNEL_DISABLE);
f3ed775e
DG
306}
307
964ccb60 308int kernctl_start_session(int fd)
16421f6e 309{
4dbc372b
JD
310 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_START,
311 LTTNG_KERNEL_SESSION_START);
964ccb60
MD
312}
313
314int kernctl_stop_session(int fd)
315{
4dbc372b
JD
316 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_SESSION_STOP,
317 LTTNG_KERNEL_SESSION_STOP);
16421f6e
DG
318}
319
964ccb60
MD
320int kernctl_tracepoint_list(int fd)
321{
4dbc372b
JD
322 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_TRACEPOINT_LIST,
323 LTTNG_KERNEL_TRACEPOINT_LIST);
964ccb60
MD
324}
325
d02666a9
MD
326int kernctl_syscall_list(int fd)
327{
328 return ioctl(fd, LTTNG_KERNEL_SYSCALL_LIST);
329}
330
964ccb60
MD
331int kernctl_tracer_version(int fd, struct lttng_kernel_tracer_version *v)
332{
4dbc372b
JD
333 int ret;
334
335 if (lttng_kernel_use_old_abi == -1) {
336 ret = ioctl(fd, LTTNG_KERNEL_TRACER_VERSION, v);
337 if (!ret) {
338 lttng_kernel_use_old_abi = 0;
339 goto end;
340 }
341 lttng_kernel_use_old_abi = 1;
342 }
343 if (lttng_kernel_use_old_abi) {
344 struct lttng_kernel_old_tracer_version old_v;
345
346 ret = ioctl(fd, LTTNG_KERNEL_OLD_TRACER_VERSION, &old_v);
347 if (ret) {
348 goto end;
349 }
350 v->major = old_v.major;
351 v->minor = old_v.minor;
352 v->patchlevel = old_v.patchlevel;
353 } else {
354 ret = ioctl(fd, LTTNG_KERNEL_TRACER_VERSION, v);
355 }
356
357end:
358 return ret;
964ccb60
MD
359}
360
c052142c
MD
361int kernctl_tracer_abi_version(int fd,
362 struct lttng_kernel_tracer_abi_version *v)
363{
364 return ioctl(fd, LTTNG_KERNEL_TRACER_ABI_VERSION, v);
365}
366
964ccb60
MD
367int kernctl_wait_quiescent(int fd)
368{
4dbc372b
JD
369 return compat_ioctl_no_arg(fd, LTTNG_KERNEL_OLD_WAIT_QUIESCENT,
370 LTTNG_KERNEL_WAIT_QUIESCENT);
964ccb60
MD
371}
372
373int kernctl_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
374{
4dbc372b
JD
375 int ret;
376
377 if (lttng_kernel_use_old_abi == -1) {
378 ret = ioctl(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
379 if (!ret) {
380 lttng_kernel_use_old_abi = 0;
381 goto end;
382 }
383 lttng_kernel_use_old_abi = 1;
384 }
385 if (lttng_kernel_use_old_abi) {
386 struct lttng_kernel_old_calibrate old_calibrate;
387
388 old_calibrate.type = calibrate->type;
389 ret = ioctl(fd, LTTNG_KERNEL_OLD_CALIBRATE, &old_calibrate);
390 if (ret) {
391 goto end;
392 }
393 calibrate->type = old_calibrate.type;
394 } else {
395 ret = ioctl(fd, LTTNG_KERNEL_CALIBRATE, calibrate);
396 }
397
398end:
399 return ret;
964ccb60
MD
400}
401
402
403int kernctl_buffer_flush(int fd)
404{
405 return ioctl(fd, RING_BUFFER_FLUSH);
406}
407
408
409/* Buffer operations */
410
411/* For mmap mode, readable without "get" operation */
412
16421f6e
DG
413/* returns the length to mmap. */
414int kernctl_get_mmap_len(int fd, unsigned long *len)
415{
416 return ioctl(fd, RING_BUFFER_GET_MMAP_LEN, len);
417}
418
964ccb60
MD
419/* returns the maximum size for sub-buffers. */
420int kernctl_get_max_subbuf_size(int fd, unsigned long *len)
421{
422 return ioctl(fd, RING_BUFFER_GET_MAX_SUBBUF_SIZE, len);
423}
424
425/*
426 * For mmap mode, operate on the current packet (between get/put or
427 * get_next/put_next).
428 */
429
16421f6e
DG
430/* returns the offset of the subbuffer belonging to the mmap reader. */
431int kernctl_get_mmap_read_offset(int fd, unsigned long *off)
432{
433 return ioctl(fd, RING_BUFFER_GET_MMAP_READ_OFFSET, off);
434}
435
964ccb60
MD
436/* returns the size of the current sub-buffer, without padding (for mmap). */
437int kernctl_get_subbuf_size(int fd, unsigned long *len)
16421f6e 438{
964ccb60 439 return ioctl(fd, RING_BUFFER_GET_SUBBUF_SIZE, len);
16421f6e
DG
440}
441
442/* returns the size of the current sub-buffer, without padding (for mmap). */
443int kernctl_get_padded_subbuf_size(int fd, unsigned long *len)
444{
445 return ioctl(fd, RING_BUFFER_GET_PADDED_SUBBUF_SIZE, len);
446}
447
964ccb60
MD
448/* Get exclusive read access to the next sub-buffer that can be read. */
449int kernctl_get_next_subbuf(int fd)
16421f6e 450{
964ccb60 451 return ioctl(fd, RING_BUFFER_GET_NEXT_SUBBUF);
16421f6e
DG
452}
453
d4a1283e 454
16421f6e
DG
455/* Release exclusive sub-buffer access, move consumer forward. */
456int kernctl_put_next_subbuf(int fd)
457{
458 return ioctl(fd, RING_BUFFER_PUT_NEXT_SUBBUF);
459}
460
964ccb60 461/* snapshot */
16421f6e
DG
462
463/* Get a snapshot of the current ring buffer producer and consumer positions */
464int kernctl_snapshot(int fd)
465{
466 return ioctl(fd, RING_BUFFER_SNAPSHOT);
467}
468
469/* Get the consumer position (iteration start) */
470int kernctl_snapshot_get_consumed(int fd, unsigned long *pos)
471{
472 return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_CONSUMED, pos);
473}
474
475/* Get the producer position (iteration end) */
476int kernctl_snapshot_get_produced(int fd, unsigned long *pos)
477{
478 return ioctl(fd, RING_BUFFER_SNAPSHOT_GET_PRODUCED, pos);
479}
480
964ccb60
MD
481/* Get exclusive read access to the specified sub-buffer position */
482int kernctl_get_subbuf(int fd, unsigned long *len)
f3ed775e 483{
964ccb60 484 return ioctl(fd, RING_BUFFER_GET_SUBBUF, len);
f3ed775e 485}
d0254c7c 486
964ccb60
MD
487/* Release exclusive sub-buffer access */
488int kernctl_put_subbuf(int fd)
d0254c7c 489{
964ccb60 490 return ioctl(fd, RING_BUFFER_PUT_SUBBUF);
d0254c7c 491}
309167d2
JD
492
493/* Returns the timestamp begin of the current sub-buffer. */
494int kernctl_get_timestamp_begin(int fd, uint64_t *timestamp_begin)
495{
496 return ioctl(fd, LTTNG_RING_BUFFER_GET_TIMESTAMP_BEGIN, timestamp_begin);
497}
498
499/* Returns the timestamp end of the current sub-buffer. */
500int kernctl_get_timestamp_end(int fd, uint64_t *timestamp_end)
501{
502 return ioctl(fd, LTTNG_RING_BUFFER_GET_TIMESTAMP_END, timestamp_end);
503}
504
505/* Returns the number of discarded events in the current sub-buffer. */
506int kernctl_get_events_discarded(int fd, uint64_t *events_discarded)
507{
508 return ioctl(fd, LTTNG_RING_BUFFER_GET_EVENTS_DISCARDED, events_discarded);
509}
510
511/* Returns the content size in the current sub-buffer. */
512int kernctl_get_content_size(int fd, uint64_t *content_size)
513{
514 return ioctl(fd, LTTNG_RING_BUFFER_GET_CONTENT_SIZE, content_size);
515}
516
517/* Returns the packet size in the current sub-buffer. */
518int kernctl_get_packet_size(int fd, uint64_t *packet_size)
519{
520 return ioctl(fd, LTTNG_RING_BUFFER_GET_PACKET_SIZE, packet_size);
521}
522
523/* Returns the stream id of the current sub-buffer. */
524int kernctl_get_stream_id(int fd, uint64_t *stream_id)
525{
526 return ioctl(fd, LTTNG_RING_BUFFER_GET_STREAM_ID, stream_id);
527}
d3e2ba59
JD
528
529/* Returns the current timestamp. */
530int kernctl_get_current_timestamp(int fd, uint64_t *ts)
531{
532 return ioctl(fd, LTTNG_RING_BUFFER_GET_CURRENT_TIMESTAMP, ts);
533}
This page took 0.098303 seconds and 4 git commands to generate.