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