Commit | Line | Data |
---|---|---|
20fe2104 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
82a3637f DG |
6 | * as published by the Free Software Foundation; only version 2 |
7 | * of the License. | |
20fe2104 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 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | */ | |
18 | ||
8c0faa1d | 19 | #define _GNU_SOURCE |
20fe2104 | 20 | #include <errno.h> |
7b395890 | 21 | #include <fcntl.h> |
20fe2104 DG |
22 | #include <stdlib.h> |
23 | #include <stdio.h> | |
f34daff7 | 24 | #include <string.h> |
8c0faa1d | 25 | #include <unistd.h> |
20fe2104 | 26 | |
1e307fab DG |
27 | #include <lttng-kernel-ctl.h> |
28 | #include <lttngerr.h> | |
29 | ||
20fe2104 | 30 | #include "kernel-ctl.h" |
20fe2104 | 31 | |
d65106b1 | 32 | /* |
050349bb | 33 | * Add context on a kernel channel. |
d65106b1 DG |
34 | */ |
35 | int kernel_add_channel_context(struct ltt_kernel_channel *chan, | |
36 | struct lttng_kernel_context *ctx) | |
37 | { | |
38 | int ret; | |
39 | ||
40 | DBG("Adding context to channel %s", chan->channel->name); | |
41 | ret = kernctl_add_context(chan->fd, ctx); | |
42 | if (ret < 0) { | |
b579acd9 DG |
43 | if (errno != EEXIST) { |
44 | perror("add context ioctl"); | |
45 | } else { | |
46 | /* If EEXIST, we just ignore the error */ | |
47 | ret = 0; | |
48 | } | |
d65106b1 DG |
49 | goto error; |
50 | } | |
51 | ||
52 | chan->ctx = malloc(sizeof(struct lttng_kernel_context)); | |
53 | if (chan->ctx == NULL) { | |
54 | perror("malloc event context"); | |
55 | goto error; | |
56 | } | |
57 | ||
58 | memcpy(chan->ctx, ctx, sizeof(struct lttng_kernel_context)); | |
59 | ||
60 | return 0; | |
61 | ||
62 | error: | |
63 | return ret; | |
64 | } | |
65 | ||
66 | /* | |
050349bb | 67 | * Add context on a kernel event. |
d65106b1 DG |
68 | */ |
69 | int kernel_add_event_context(struct ltt_kernel_event *event, | |
70 | struct lttng_kernel_context *ctx) | |
71 | { | |
72 | int ret; | |
73 | ||
74 | DBG("Adding context to event %s", event->event->name); | |
75 | ret = kernctl_add_context(event->fd, ctx); | |
76 | if (ret < 0) { | |
77 | perror("add context ioctl"); | |
78 | goto error; | |
79 | } | |
80 | ||
81 | event->ctx = malloc(sizeof(struct lttng_kernel_context)); | |
82 | if (event->ctx == NULL) { | |
83 | perror("malloc event context"); | |
84 | goto error; | |
85 | } | |
86 | ||
87 | memcpy(event->ctx, ctx, sizeof(struct lttng_kernel_context)); | |
88 | ||
89 | return 0; | |
90 | ||
91 | error: | |
92 | return ret; | |
93 | } | |
94 | ||
20fe2104 | 95 | /* |
050349bb DG |
96 | * Create a new kernel session, register it to the kernel tracer and add it to |
97 | * the session daemon session. | |
20fe2104 | 98 | */ |
8c0faa1d | 99 | int kernel_create_session(struct ltt_session *session, int tracer_fd) |
20fe2104 DG |
100 | { |
101 | int ret; | |
102 | struct ltt_kernel_session *lks; | |
103 | ||
54012638 | 104 | /* Allocate data structure */ |
f9815039 | 105 | lks = trace_kernel_create_session(session->path); |
20fe2104 | 106 | if (lks == NULL) { |
54012638 | 107 | ret = -1; |
20fe2104 DG |
108 | goto error; |
109 | } | |
110 | ||
54012638 | 111 | /* Kernel tracer session creation */ |
20fe2104 DG |
112 | ret = kernctl_create_session(tracer_fd); |
113 | if (ret < 0) { | |
54012638 | 114 | perror("ioctl kernel create session"); |
20fe2104 DG |
115 | goto error; |
116 | } | |
117 | ||
20fe2104 | 118 | lks->fd = ret; |
7b395890 DG |
119 | /* Prevent fd duplication after execlp() */ |
120 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
121 | if (ret < 0) { | |
122 | perror("fcntl session fd"); | |
123 | } | |
124 | ||
f3ed775e | 125 | lks->kconsumer_fds_sent = 0; |
8c0faa1d | 126 | session->kernel_session = lks; |
8c0faa1d DG |
127 | |
128 | DBG("Kernel session created (fd: %d)", lks->fd); | |
20fe2104 DG |
129 | |
130 | return 0; | |
131 | ||
132 | error: | |
133 | return ret; | |
134 | } | |
135 | ||
136 | /* | |
050349bb DG |
137 | * Create a kernel channel, register it to the kernel tracer and add it to the |
138 | * kernel session. | |
20fe2104 | 139 | */ |
050349bb DG |
140 | int kernel_create_channel(struct ltt_kernel_session *session, |
141 | struct lttng_channel *chan, char *path) | |
20fe2104 DG |
142 | { |
143 | int ret; | |
144 | struct ltt_kernel_channel *lkc; | |
20fe2104 | 145 | |
54012638 | 146 | /* Allocate kernel channel */ |
62499ad6 | 147 | lkc = trace_kernel_create_channel(chan, path); |
54012638 | 148 | if (lkc == NULL) { |
20fe2104 DG |
149 | goto error; |
150 | } | |
151 | ||
54012638 | 152 | /* Kernel tracer channel creation */ |
f3ed775e | 153 | ret = kernctl_create_channel(session->fd, &lkc->channel->attr); |
20fe2104 | 154 | if (ret < 0) { |
54012638 | 155 | perror("ioctl kernel create channel"); |
20fe2104 DG |
156 | goto error; |
157 | } | |
158 | ||
54012638 | 159 | /* Setup the channel fd */ |
20fe2104 | 160 | lkc->fd = ret; |
7b395890 DG |
161 | /* Prevent fd duplication after execlp() */ |
162 | ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC); | |
163 | if (ret < 0) { | |
164 | perror("fcntl session fd"); | |
165 | } | |
166 | ||
54012638 | 167 | /* Add channel to session */ |
8c0faa1d DG |
168 | cds_list_add(&lkc->list, &session->channel_list.head); |
169 | session->channel_count++; | |
20fe2104 | 170 | |
f3ed775e DG |
171 | DBG("Kernel channel %s created (fd: %d and path: %s)", |
172 | lkc->channel->name, lkc->fd, lkc->pathname); | |
20fe2104 DG |
173 | |
174 | return 0; | |
175 | ||
176 | error: | |
54012638 | 177 | return -1; |
20fe2104 | 178 | } |
f34daff7 DG |
179 | |
180 | /* | |
050349bb DG |
181 | * Create a kernel event, enable it to the kernel tracer and add it to the |
182 | * channel event list of the kernel session. | |
f34daff7 | 183 | */ |
050349bb DG |
184 | int kernel_create_event(struct lttng_event *ev, |
185 | struct ltt_kernel_channel *channel) | |
f34daff7 DG |
186 | { |
187 | int ret; | |
188 | struct ltt_kernel_event *event; | |
f34daff7 | 189 | |
62499ad6 | 190 | event = trace_kernel_create_event(ev); |
54012638 | 191 | if (event == NULL) { |
f34daff7 DG |
192 | goto error; |
193 | } | |
194 | ||
f3ed775e DG |
195 | ret = kernctl_create_event(channel->fd, event->event); |
196 | if (ret < 0) { | |
e953ef25 DG |
197 | perror("create event ioctl"); |
198 | goto free_event; | |
8c0faa1d | 199 | } |
f34daff7 | 200 | |
f3ed775e | 201 | event->fd = ret; |
7b395890 DG |
202 | /* Prevent fd duplication after execlp() */ |
203 | ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC); | |
204 | if (ret < 0) { | |
205 | perror("fcntl session fd"); | |
206 | } | |
207 | ||
f3ed775e DG |
208 | /* Add event to event list */ |
209 | cds_list_add(&event->list, &channel->events_list.head); | |
cbbbb275 DG |
210 | channel->event_count++; |
211 | ||
e953ef25 DG |
212 | DBG("Event %s created (fd: %d)", ev->name, event->fd); |
213 | ||
214 | return 0; | |
215 | ||
216 | free_event: | |
217 | free(event); | |
218 | error: | |
219 | return -1; | |
220 | } | |
221 | ||
26cc6b4e | 222 | /* |
050349bb | 223 | * Disable a kernel channel. |
26cc6b4e DG |
224 | */ |
225 | int kernel_disable_channel(struct ltt_kernel_channel *chan) | |
226 | { | |
227 | int ret; | |
228 | ||
229 | ret = kernctl_disable(chan->fd); | |
230 | if (ret < 0) { | |
231 | perror("disable chan ioctl"); | |
232 | ret = errno; | |
233 | goto error; | |
234 | } | |
235 | ||
236 | chan->enabled = 0; | |
237 | DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd); | |
238 | ||
239 | return 0; | |
240 | ||
241 | error: | |
242 | return ret; | |
243 | } | |
244 | ||
d36b8583 | 245 | /* |
050349bb | 246 | * Enable a kernel channel. |
d36b8583 DG |
247 | */ |
248 | int kernel_enable_channel(struct ltt_kernel_channel *chan) | |
249 | { | |
250 | int ret; | |
251 | ||
252 | ret = kernctl_enable(chan->fd); | |
253 | if (ret < 0) { | |
254 | perror("enable chan ioctl"); | |
255 | ret = errno; | |
256 | goto error; | |
257 | } | |
258 | ||
259 | chan->enabled = 1; | |
260 | DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd); | |
261 | ||
262 | return 0; | |
263 | ||
264 | error: | |
265 | return ret; | |
266 | } | |
267 | ||
19e70852 | 268 | /* |
050349bb | 269 | * Enable a kernel event. |
19e70852 DG |
270 | */ |
271 | int kernel_enable_event(struct ltt_kernel_event *event) | |
272 | { | |
273 | int ret; | |
274 | ||
275 | ret = kernctl_enable(event->fd); | |
276 | if (ret < 0) { | |
277 | perror("enable event ioctl"); | |
7d29a247 DG |
278 | if (errno == EEXIST) { |
279 | ret = -EEXIST; | |
280 | } | |
19e70852 DG |
281 | goto error; |
282 | } | |
283 | ||
284 | event->enabled = 1; | |
285 | DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd); | |
286 | ||
287 | return 0; | |
288 | ||
289 | error: | |
d36b8583 | 290 | return ret; |
19e70852 DG |
291 | } |
292 | ||
e953ef25 | 293 | /* |
050349bb | 294 | * Disable a kernel event. |
e953ef25 | 295 | */ |
19e70852 | 296 | int kernel_disable_event(struct ltt_kernel_event *event) |
e953ef25 DG |
297 | { |
298 | int ret; | |
19e70852 DG |
299 | |
300 | ret = kernctl_disable(event->fd); | |
301 | if (ret < 0) { | |
302 | perror("disable event ioctl"); | |
303 | goto error; | |
e953ef25 | 304 | } |
f3ed775e | 305 | |
19e70852 DG |
306 | event->enabled = 0; |
307 | DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd); | |
308 | ||
f34daff7 DG |
309 | return 0; |
310 | ||
311 | error: | |
d36b8583 | 312 | return ret; |
f34daff7 | 313 | } |
aaf26714 DG |
314 | |
315 | /* | |
050349bb DG |
316 | * Create kernel metadata, open from the kernel tracer and add it to the |
317 | * kernel session. | |
aaf26714 | 318 | */ |
58a97671 | 319 | int kernel_open_metadata(struct ltt_kernel_session *session, char *path) |
aaf26714 DG |
320 | { |
321 | int ret; | |
322 | struct ltt_kernel_metadata *lkm; | |
aaf26714 | 323 | |
54012638 | 324 | /* Allocate kernel metadata */ |
62499ad6 | 325 | lkm = trace_kernel_create_metadata(path); |
54012638 | 326 | if (lkm == NULL) { |
aaf26714 DG |
327 | goto error; |
328 | } | |
329 | ||
54012638 | 330 | /* Kernel tracer metadata creation */ |
f3ed775e | 331 | ret = kernctl_open_metadata(session->fd, &lkm->conf->attr); |
aaf26714 DG |
332 | if (ret < 0) { |
333 | goto error; | |
334 | } | |
335 | ||
8c0faa1d | 336 | lkm->fd = ret; |
7b395890 DG |
337 | /* Prevent fd duplication after execlp() */ |
338 | ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC); | |
339 | if (ret < 0) { | |
340 | perror("fcntl session fd"); | |
341 | } | |
342 | ||
aaf26714 | 343 | session->metadata = lkm; |
8c0faa1d | 344 | |
54012638 | 345 | DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname); |
8c0faa1d DG |
346 | |
347 | return 0; | |
348 | ||
349 | error: | |
54012638 | 350 | return -1; |
8c0faa1d DG |
351 | } |
352 | ||
353 | /* | |
050349bb | 354 | * Start tracing session. |
8c0faa1d DG |
355 | */ |
356 | int kernel_start_session(struct ltt_kernel_session *session) | |
357 | { | |
358 | int ret; | |
359 | ||
360 | ret = kernctl_start_session(session->fd); | |
361 | if (ret < 0) { | |
f3ed775e | 362 | perror("ioctl start session"); |
8c0faa1d DG |
363 | goto error; |
364 | } | |
365 | ||
366 | DBG("Kernel session started"); | |
367 | ||
368 | return 0; | |
369 | ||
370 | error: | |
371 | return ret; | |
372 | } | |
373 | ||
f3ed775e | 374 | /* |
050349bb | 375 | * Make a kernel wait to make sure in-flight probe have completed. |
f3ed775e DG |
376 | */ |
377 | void kernel_wait_quiescent(int fd) | |
378 | { | |
379 | int ret; | |
380 | ||
381 | DBG("Kernel quiescent wait on %d", fd); | |
382 | ||
383 | ret = kernctl_wait_quiescent(fd); | |
384 | if (ret < 0) { | |
385 | perror("wait quiescent ioctl"); | |
386 | ERR("Kernel quiescent wait failed"); | |
387 | } | |
388 | } | |
389 | ||
d0254c7c | 390 | /* |
050349bb | 391 | * Kernel calibrate |
d0254c7c MD |
392 | */ |
393 | int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate) | |
394 | { | |
395 | int ret; | |
396 | ||
397 | ret = kernctl_calibrate(fd, calibrate); | |
398 | if (ret < 0) { | |
399 | perror("calibrate ioctl"); | |
400 | return -1; | |
401 | } | |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
406 | ||
f3ed775e | 407 | /* |
f3ed775e DG |
408 | * Force flush buffer of metadata. |
409 | */ | |
410 | int kernel_metadata_flush_buffer(int fd) | |
411 | { | |
412 | int ret; | |
413 | ||
414 | ret = kernctl_buffer_flush(fd); | |
415 | if (ret < 0) { | |
416 | ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret); | |
417 | } | |
418 | ||
419 | return 0; | |
420 | } | |
421 | ||
422 | /* | |
050349bb | 423 | * Force flush buffer for channel. |
f3ed775e DG |
424 | */ |
425 | int kernel_flush_buffer(struct ltt_kernel_channel *channel) | |
426 | { | |
427 | int ret; | |
428 | struct ltt_kernel_stream *stream; | |
429 | ||
430 | DBG("Flush buffer for channel %s", channel->channel->name); | |
431 | ||
432 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { | |
433 | DBG("Flushing channel stream %d", stream->fd); | |
434 | ret = kernctl_buffer_flush(stream->fd); | |
435 | if (ret < 0) { | |
436 | perror("ioctl"); | |
437 | ERR("Fail to flush buffer for stream %d (ret: %d)", | |
438 | stream->fd, ret); | |
439 | } | |
440 | } | |
441 | ||
442 | return 0; | |
443 | } | |
444 | ||
8c0faa1d | 445 | /* |
050349bb | 446 | * Stop tracing session. |
8c0faa1d DG |
447 | */ |
448 | int kernel_stop_session(struct ltt_kernel_session *session) | |
449 | { | |
450 | int ret; | |
451 | ||
452 | ret = kernctl_stop_session(session->fd); | |
453 | if (ret < 0) { | |
454 | goto error; | |
455 | } | |
456 | ||
457 | DBG("Kernel session stopped"); | |
458 | ||
459 | return 0; | |
460 | ||
461 | error: | |
462 | return ret; | |
463 | } | |
464 | ||
465 | /* | |
050349bb DG |
466 | * Open stream of channel, register it to the kernel tracer and add it |
467 | * to the stream list of the channel. | |
8c0faa1d | 468 | * |
050349bb | 469 | * Return the number of created stream. Else, a negative value. |
8c0faa1d | 470 | */ |
f3ed775e | 471 | int kernel_open_channel_stream(struct ltt_kernel_channel *channel) |
8c0faa1d DG |
472 | { |
473 | int ret; | |
474 | struct ltt_kernel_stream *lks; | |
475 | ||
476 | while ((ret = kernctl_create_stream(channel->fd)) > 0) { | |
62499ad6 | 477 | lks = trace_kernel_create_stream(); |
8c0faa1d | 478 | if (lks == NULL) { |
54012638 | 479 | close(ret); |
8c0faa1d DG |
480 | goto error; |
481 | } | |
482 | ||
483 | lks->fd = ret; | |
7b395890 DG |
484 | /* Prevent fd duplication after execlp() */ |
485 | ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC); | |
486 | if (ret < 0) { | |
487 | perror("fcntl session fd"); | |
488 | } | |
489 | ||
8e68d1c8 DG |
490 | ret = asprintf(&lks->pathname, "%s/%s_%d", |
491 | channel->pathname, channel->channel->name, channel->stream_count); | |
8c0faa1d DG |
492 | if (ret < 0) { |
493 | perror("asprintf kernel create stream"); | |
494 | goto error; | |
495 | } | |
8c0faa1d | 496 | |
54012638 | 497 | /* Add stream to channe stream list */ |
8c0faa1d DG |
498 | cds_list_add(&lks->list, &channel->stream_list.head); |
499 | channel->stream_count++; | |
8c0faa1d | 500 | |
54012638 DG |
501 | DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)", |
502 | channel->stream_count, lks->fd, lks->state, lks->pathname); | |
503 | } | |
8c0faa1d DG |
504 | |
505 | return channel->stream_count; | |
506 | ||
507 | error: | |
54012638 | 508 | return -1; |
8c0faa1d DG |
509 | } |
510 | ||
511 | /* | |
050349bb | 512 | * Open the metadata stream and set it to the kernel session. |
8c0faa1d | 513 | */ |
f3ed775e | 514 | int kernel_open_metadata_stream(struct ltt_kernel_session *session) |
8c0faa1d DG |
515 | { |
516 | int ret; | |
517 | ||
518 | ret = kernctl_create_stream(session->metadata->fd); | |
519 | if (ret < 0) { | |
520 | perror("kernel create metadata stream"); | |
8c0faa1d DG |
521 | goto error; |
522 | } | |
523 | ||
524 | DBG("Kernel metadata stream created (fd: %d)", ret); | |
525 | session->metadata_stream_fd = ret; | |
7b395890 DG |
526 | /* Prevent fd duplication after execlp() */ |
527 | ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC); | |
528 | if (ret < 0) { | |
529 | perror("fcntl session fd"); | |
530 | } | |
aaf26714 DG |
531 | |
532 | return 0; | |
533 | ||
534 | error: | |
54012638 | 535 | return -1; |
aaf26714 | 536 | } |
2ef84c95 DG |
537 | |
538 | /* | |
9f19cc17 | 539 | * Get the event list from the kernel tracer and return the number of elements. |
2ef84c95 | 540 | */ |
9f19cc17 | 541 | ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events) |
2ef84c95 | 542 | { |
9f19cc17 DG |
543 | int fd, pos; |
544 | char *event; | |
545 | size_t nbmem, count = 0; | |
2ef84c95 DG |
546 | ssize_t size; |
547 | FILE *fp; | |
9f19cc17 | 548 | struct lttng_event *elist; |
2ef84c95 DG |
549 | |
550 | fd = kernctl_tracepoint_list(tracer_fd); | |
551 | if (fd < 0) { | |
552 | perror("kernel tracepoint list"); | |
553 | goto error; | |
554 | } | |
555 | ||
556 | fp = fdopen(fd, "r"); | |
557 | if (fp == NULL) { | |
558 | perror("kernel tracepoint list fdopen"); | |
61b73b12 | 559 | goto error_fp; |
2ef84c95 DG |
560 | } |
561 | ||
562 | /* | |
563 | * Init memory size counter | |
564 | * See kernel-ctl.h for explanation of this value | |
565 | */ | |
566 | nbmem = KERNEL_EVENT_LIST_SIZE; | |
9f19cc17 | 567 | elist = malloc(sizeof(struct lttng_event) * nbmem); |
2ef84c95 | 568 | |
9f19cc17 DG |
569 | while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) { |
570 | if (count > nbmem) { | |
ced2f820 | 571 | DBG("Reallocating event list from %zu to %zu bytes", nbmem, |
9f19cc17 | 572 | nbmem + KERNEL_EVENT_LIST_SIZE); |
2ef84c95 | 573 | /* Adding the default size again */ |
9f19cc17 DG |
574 | nbmem += KERNEL_EVENT_LIST_SIZE; |
575 | elist = realloc(elist, nbmem); | |
576 | if (elist == NULL) { | |
2ef84c95 | 577 | perror("realloc list events"); |
61b73b12 MD |
578 | count = -ENOMEM; |
579 | goto end; | |
2ef84c95 DG |
580 | } |
581 | } | |
99497cd0 MD |
582 | strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN); |
583 | elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
9f19cc17 | 584 | count++; |
2ef84c95 DG |
585 | } |
586 | ||
9f19cc17 | 587 | *events = elist; |
ced2f820 | 588 | DBG("Kernel list events done (%zu events)", count); |
61b73b12 MD |
589 | end: |
590 | fclose(fp); /* closes both fp and fd */ | |
9f19cc17 | 591 | return count; |
2ef84c95 | 592 | |
61b73b12 MD |
593 | error_fp: |
594 | close(fd); | |
2ef84c95 DG |
595 | error: |
596 | return -1; | |
597 | } |