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