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