allow printing domains when no procname are present, handle various / patterns
[babeltrace.git] / formats / ctf / ctf.c
1 /*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * Format registration.
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/format.h>
22 #include <babeltrace/ctf/types.h>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <inttypes.h>
26 #include <stdio.h>
27 #include <uuid/uuid.h>
28 #include <sys/mman.h>
29 #include <errno.h>
30 #include <endian.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <dirent.h>
35 #include <glib.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38
39 #include "metadata/ctf-scanner.h"
40 #include "metadata/ctf-parser.h"
41 #include "metadata/ctf-ast.h"
42
43 /*
44 * We currently simply map a page to read the packet header and packet
45 * context to get the packet length and content length. (in bits)
46 */
47 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
48 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
49 #define UUID_LEN 16 /* uuid by value len */
50
51 #ifndef min
52 #define min(a, b) (((a) < (b)) ? (a) : (b))
53 #endif
54
55 extern int yydebug;
56
57 static
58 struct trace_descriptor *ctf_open_trace(const char *collection_path, const char *path, int flags,
59 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
60 int whence), FILE *metadata_fp);
61 static
62 struct trace_descriptor *ctf_open_mmap_trace(
63 struct mmap_stream_list *mmap_list,
64 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, int whence),
65 FILE *metadata_fp);
66
67 static
68 void ctf_close_trace(struct trace_descriptor *descriptor);
69
70 static
71 rw_dispatch read_dispatch_table[] = {
72 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
73 [ CTF_TYPE_FLOAT ] = ctf_float_read,
74 [ CTF_TYPE_ENUM ] = ctf_enum_read,
75 [ CTF_TYPE_STRING ] = ctf_string_read,
76 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
77 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
78 [ CTF_TYPE_ARRAY ] = ctf_array_read,
79 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
80 };
81
82 static
83 rw_dispatch write_dispatch_table[] = {
84 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
85 [ CTF_TYPE_FLOAT ] = ctf_float_write,
86 [ CTF_TYPE_ENUM ] = ctf_enum_write,
87 [ CTF_TYPE_STRING ] = ctf_string_write,
88 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
89 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
90 [ CTF_TYPE_ARRAY ] = ctf_array_write,
91 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
92 };
93
94 static
95 struct format ctf_format = {
96 .open_trace = ctf_open_trace,
97 .open_mmap_trace = ctf_open_mmap_trace,
98 .close_trace = ctf_close_trace,
99 };
100
101 static
102 void ctf_update_timestamp(struct ctf_stream *stream,
103 struct definition_integer *integer_definition)
104 {
105 struct declaration_integer *integer_declaration =
106 integer_definition->declaration;
107 uint64_t oldval, newval, updateval;
108
109 if (unlikely(integer_declaration->len == 64)) {
110 stream->timestamp = integer_definition->value._unsigned;
111 return;
112 }
113 /* keep low bits */
114 oldval = stream->timestamp;
115 oldval &= (1ULL << integer_declaration->len) - 1;
116 newval = integer_definition->value._unsigned;
117 /* Test for overflow by comparing low bits */
118 if (newval < oldval)
119 newval += 1ULL << integer_declaration->len;
120 /* updateval contains old high bits, and new low bits (sum) */
121 updateval = stream->timestamp;
122 updateval &= ~((1ULL << integer_declaration->len) - 1);
123 updateval += newval;
124 stream->timestamp = updateval;
125 }
126
127 static
128 int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream)
129 {
130 struct ctf_stream_pos *pos =
131 container_of(ppos, struct ctf_stream_pos, parent);
132 struct ctf_stream_class *stream_class = stream->stream_class;
133 struct ctf_stream_event *event;
134 uint64_t id = 0;
135 int ret;
136
137 /* We need to check for EOF here for empty files. */
138 if (unlikely(pos->offset == EOF))
139 return EOF;
140
141 ctf_pos_get_event(pos);
142
143 /*
144 * This is the EOF check after we've advanced the position in
145 * ctf_pos_get_event.
146 */
147 if (unlikely(pos->offset == EOF))
148 return EOF;
149 assert(pos->offset < pos->content_size);
150
151 /* Read event header */
152 if (likely(stream->stream_event_header)) {
153 struct definition_integer *integer_definition;
154 struct definition *variant;
155
156 ret = generic_rw(ppos, &stream->stream_event_header->p);
157 if (unlikely(ret))
158 goto error;
159 /* lookup event id */
160 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
161 if (integer_definition) {
162 id = integer_definition->value._unsigned;
163 } else {
164 struct definition_enum *enum_definition;
165
166 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
167 if (enum_definition) {
168 id = enum_definition->integer->value._unsigned;
169 }
170 }
171
172 variant = lookup_variant(&stream->stream_event_header->p, "v");
173 if (variant) {
174 integer_definition = lookup_integer(variant, "id", FALSE);
175 if (integer_definition) {
176 id = integer_definition->value._unsigned;
177 }
178 }
179 stream->event_id = id;
180
181 /* lookup timestamp */
182 stream->has_timestamp = 0;
183 integer_definition = lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
184 if (integer_definition) {
185 ctf_update_timestamp(stream, integer_definition);
186 stream->has_timestamp = 1;
187 } else {
188 if (variant) {
189 integer_definition = lookup_integer(variant, "timestamp", FALSE);
190 if (integer_definition) {
191 ctf_update_timestamp(stream, integer_definition);
192 stream->has_timestamp = 1;
193 }
194 }
195 }
196 }
197
198 /* Read stream-declared event context */
199 if (stream->stream_event_context) {
200 ret = generic_rw(ppos, &stream->stream_event_context->p);
201 if (ret)
202 goto error;
203 }
204
205 if (unlikely(id >= stream_class->events_by_id->len)) {
206 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
207 return -EINVAL;
208 }
209 event = g_ptr_array_index(stream->events_by_id, id);
210 if (unlikely(!event)) {
211 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
212 return -EINVAL;
213 }
214
215 /* Read event-declared event context */
216 if (event->event_context) {
217 ret = generic_rw(ppos, &event->event_context->p);
218 if (ret)
219 goto error;
220 }
221
222 /* Read event payload */
223 if (likely(event->event_fields)) {
224 ret = generic_rw(ppos, &event->event_fields->p);
225 if (ret)
226 goto error;
227 }
228
229 return 0;
230
231 error:
232 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
233 return ret;
234 }
235
236 static
237 int ctf_write_event(struct stream_pos *pos, struct ctf_stream *stream)
238 {
239 struct ctf_stream_class *stream_class = stream->stream_class;
240 struct ctf_stream_event *event;
241 uint64_t id;
242 int ret;
243
244 id = stream->event_id;
245
246 /* print event header */
247 if (likely(stream->stream_event_header)) {
248 ret = generic_rw(pos, &stream->stream_event_header->p);
249 if (ret)
250 goto error;
251 }
252
253 /* print stream-declared event context */
254 if (stream->stream_event_context) {
255 ret = generic_rw(pos, &stream->stream_event_context->p);
256 if (ret)
257 goto error;
258 }
259
260 if (unlikely(id >= stream_class->events_by_id->len)) {
261 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
262 return -EINVAL;
263 }
264 event = g_ptr_array_index(stream->events_by_id, id);
265 if (unlikely(!event)) {
266 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
267 return -EINVAL;
268 }
269
270 /* print event-declared event context */
271 if (event->event_context) {
272 ret = generic_rw(pos, &event->event_context->p);
273 if (ret)
274 goto error;
275 }
276
277 /* Read and print event payload */
278 if (likely(event->event_fields)) {
279 ret = generic_rw(pos, &event->event_fields->p);
280 if (ret)
281 goto error;
282 }
283
284 return 0;
285
286 error:
287 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
288 return ret;
289 }
290
291 void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
292 {
293 pos->fd = fd;
294 pos->mmap_offset = 0;
295 pos->packet_size = 0;
296 pos->content_size = 0;
297 pos->content_size_loc = NULL;
298 pos->base = NULL;
299 pos->offset = 0;
300 pos->dummy = false;
301 pos->cur_index = 0;
302 if (fd >= 0)
303 pos->packet_index = g_array_new(FALSE, TRUE,
304 sizeof(struct packet_index));
305 else
306 pos->packet_index = NULL;
307 switch (open_flags & O_ACCMODE) {
308 case O_RDONLY:
309 pos->prot = PROT_READ;
310 pos->flags = MAP_PRIVATE;
311 pos->parent.rw_table = read_dispatch_table;
312 pos->parent.event_cb = ctf_read_event;
313 break;
314 case O_RDWR:
315 pos->prot = PROT_WRITE; /* Write has priority */
316 pos->flags = MAP_SHARED;
317 pos->parent.rw_table = write_dispatch_table;
318 pos->parent.event_cb = ctf_write_event;
319 if (fd >= 0)
320 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
321 break;
322 default:
323 assert(0);
324 }
325 }
326
327 void ctf_fini_pos(struct ctf_stream_pos *pos)
328 {
329 int ret;
330
331 if (pos->prot == PROT_WRITE && pos->content_size_loc)
332 *pos->content_size_loc = pos->offset;
333 if (pos->base) {
334 /* unmap old base */
335 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
336 if (ret) {
337 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
338 strerror(errno));
339 assert(0);
340 }
341 }
342 (void) g_array_free(pos->packet_index, TRUE);
343 }
344
345 void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
346 {
347 struct ctf_file_stream *file_stream =
348 container_of(pos, struct ctf_file_stream, pos);
349 int ret;
350 off_t off;
351 struct packet_index *index;
352
353 if (pos->prot == PROT_WRITE && pos->content_size_loc)
354 *pos->content_size_loc = pos->offset;
355
356 if (pos->base) {
357 /* unmap old base */
358 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
359 if (ret) {
360 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
361 strerror(errno));
362 assert(0);
363 }
364 pos->base = NULL;
365 }
366
367 /*
368 * The caller should never ask for ctf_move_pos across packets,
369 * except to get exactly at the beginning of the next packet.
370 */
371 if (pos->prot == PROT_WRITE) {
372 switch (whence) {
373 case SEEK_CUR:
374 /* The writer will add padding */
375 assert(pos->offset + offset == pos->packet_size);
376 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
377 break;
378 case SEEK_SET:
379 assert(offset == 0); /* only seek supported for now */
380 pos->cur_index = 0;
381 break;
382 default:
383 assert(0);
384 }
385 pos->content_size = -1U; /* Unknown at this point */
386 pos->packet_size = WRITE_PACKET_LEN;
387 off = posix_fallocate(pos->fd, pos->mmap_offset,
388 pos->packet_size / CHAR_BIT);
389 assert(off >= 0);
390 pos->offset = 0;
391 } else {
392 read_next_packet:
393 switch (whence) {
394 case SEEK_CUR:
395 if (pos->offset == EOF)
396 return;
397 /* The reader will expect us to skip padding */
398 assert(pos->offset + offset == pos->content_size);
399 ++pos->cur_index;
400 break;
401 case SEEK_SET:
402 assert(offset == 0); /* only seek supported for now */
403 pos->cur_index = 0;
404 break;
405 default:
406 assert(0);
407 }
408 if (pos->cur_index >= pos->packet_index->len) {
409 pos->offset = EOF;
410 return;
411 }
412 index = &g_array_index(pos->packet_index, struct packet_index,
413 pos->cur_index);
414 pos->mmap_offset = index->offset;
415
416 /* Lookup context/packet size in index */
417 file_stream->parent.timestamp = index->timestamp_begin;
418 pos->content_size = index->content_size;
419 pos->packet_size = index->packet_size;
420 if (index->data_offset < index->content_size) {
421 pos->offset = 0; /* will read headers */
422 } else if (index->data_offset == index->content_size) {
423 /* empty packet */
424 pos->offset = index->data_offset;
425 offset = 0;
426 whence = SEEK_CUR;
427 goto read_next_packet;
428 } else {
429 pos->offset = EOF;
430 return;
431 }
432 }
433 /* map new base. Need mapping length from header. */
434 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
435 pos->flags, pos->fd, pos->mmap_offset);
436 if (pos->base == MAP_FAILED) {
437 fprintf(stdout, "[error] mmap error %s.\n",
438 strerror(errno));
439 assert(0);
440 }
441
442 /* update trace_packet_header and stream_packet_context */
443 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
444 /* Read packet header */
445 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
446 assert(!ret);
447 }
448 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
449 /* Read packet context */
450 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
451 assert(!ret);
452 }
453 }
454
455 static
456 int packet_metadata(struct ctf_trace *td, FILE *fp)
457 {
458 uint32_t magic;
459 size_t len;
460 int ret = 0;
461
462 len = fread(&magic, sizeof(magic), 1, fp);
463 if (len != 1) {
464 goto end;
465 }
466 if (magic == TSDL_MAGIC) {
467 ret = 1;
468 td->byte_order = BYTE_ORDER;
469 CTF_TRACE_SET_FIELD(td, byte_order);
470 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
471 ret = 1;
472 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
473 LITTLE_ENDIAN : BIG_ENDIAN;
474 CTF_TRACE_SET_FIELD(td, byte_order);
475 }
476 end:
477 rewind(fp);
478 return ret;
479 }
480
481 /*
482 * Returns 0 on success, -1 on error.
483 */
484 static
485 int check_version(unsigned int major, unsigned int minor)
486 {
487 switch (major) {
488 case 1:
489 switch (minor) {
490 case 8:
491 return 0;
492 default:
493 goto warning;
494 }
495 default:
496 goto warning;
497
498 }
499
500 /* eventually return an error instead of warning */
501 warning:
502 fprintf(stdout, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
503 major, minor);
504 return 0;
505 }
506
507 static
508 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
509 FILE *out)
510 {
511 struct metadata_packet_header header;
512 size_t readlen, writelen, toread;
513 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
514 int ret = 0;
515
516 readlen = fread(&header, header_sizeof(header), 1, in);
517 if (readlen < 1)
518 return -EINVAL;
519
520 if (td->byte_order != BYTE_ORDER) {
521 header.magic = GUINT32_SWAP_LE_BE(header.magic);
522 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
523 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
524 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
525 }
526 if (header.checksum)
527 fprintf(stdout, "[warning] checksum verification not supported yet.\n");
528 if (header.compression_scheme) {
529 fprintf(stdout, "[error] compression (%u) not supported yet.\n",
530 header.compression_scheme);
531 return -EINVAL;
532 }
533 if (header.encryption_scheme) {
534 fprintf(stdout, "[error] encryption (%u) not supported yet.\n",
535 header.encryption_scheme);
536 return -EINVAL;
537 }
538 if (header.checksum_scheme) {
539 fprintf(stdout, "[error] checksum (%u) not supported yet.\n",
540 header.checksum_scheme);
541 return -EINVAL;
542 }
543 if (check_version(header.major, header.minor) < 0)
544 return -EINVAL;
545 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
546 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
547 CTF_TRACE_SET_FIELD(td, uuid);
548 } else {
549 if (uuid_compare(header.uuid, td->uuid))
550 return -EINVAL;
551 }
552
553 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
554
555 for (;;) {
556 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
557 if (ferror(in)) {
558 ret = -EINVAL;
559 break;
560 }
561 if (babeltrace_debug) {
562 buf[readlen] = '\0';
563 fprintf(stdout, "[debug] metadata packet read: %s\n",
564 buf);
565 }
566
567 writelen = fwrite(buf, sizeof(char), readlen, out);
568 if (writelen < readlen) {
569 ret = -EIO;
570 break;
571 }
572 if (ferror(out)) {
573 ret = -EINVAL;
574 break;
575 }
576 toread -= readlen;
577 if (!toread) {
578 ret = 0; /* continue reading next packet */
579 goto read_padding;
580 }
581 }
582 return ret;
583
584 read_padding:
585 toread = (header.packet_size - header.content_size) / CHAR_BIT;
586 ret = fseek(in, toread, SEEK_CUR);
587 if (ret < 0) {
588 fprintf(stdout, "[warning] Missing padding at end of file\n");
589 ret = 0;
590 }
591 return ret;
592 }
593
594 static
595 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
596 char **buf)
597 {
598 FILE *in, *out;
599 size_t size;
600 int ret;
601
602 in = *fp;
603 /*
604 * Using strlen on *buf instead of size of open_memstream
605 * because its size includes garbage at the end (after final
606 * \0). This is the allocated size, not the actual string size.
607 */
608 out = open_memstream(buf, &size);
609 if (out == NULL) {
610 perror("Metadata open_memstream");
611 return -errno;
612 }
613 for (;;) {
614 ret = ctf_open_trace_metadata_packet_read(td, in, out);
615 if (ret) {
616 break;
617 }
618 if (feof(in)) {
619 ret = 0;
620 break;
621 }
622 }
623 fclose(out); /* flush the buffer */
624 fclose(in);
625 /* open for reading */
626 *fp = fmemopen(*buf, strlen(*buf), "rb");
627 if (!*fp) {
628 perror("Metadata fmemopen");
629 return -errno;
630 }
631 return 0;
632 }
633
634 static
635 int ctf_open_trace_metadata_read(struct ctf_trace *td,
636 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
637 int whence), FILE *metadata_fp)
638 {
639 struct ctf_scanner *scanner;
640 struct ctf_file_stream *metadata_stream;
641 FILE *fp;
642 char *buf = NULL;
643 int ret = 0;
644
645 metadata_stream = g_new0(struct ctf_file_stream, 1);
646
647 if (move_pos_slow) {
648 metadata_stream->pos.move_pos_slow = move_pos_slow;
649 } else {
650 fprintf(stderr, "[error] move_pos_slow function undefined.\n");
651 ret = -1;
652 goto end_stream;
653 }
654
655 if (metadata_fp) {
656 fp = metadata_fp;
657 } else {
658 td->metadata = &metadata_stream->parent;
659 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
660 if (metadata_stream->pos.fd < 0) {
661 fprintf(stdout, "Unable to open metadata.\n");
662 return metadata_stream->pos.fd;
663 }
664
665 fp = fdopen(metadata_stream->pos.fd, "r");
666 if (!fp) {
667 fprintf(stdout, "[error] Unable to open metadata stream.\n");
668 perror("Metadata stream open");
669 ret = -errno;
670 goto end_stream;
671 }
672 }
673 if (babeltrace_debug)
674 yydebug = 1;
675
676 if (packet_metadata(td, fp)) {
677 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
678 if (ret)
679 goto end_packet_read;
680 } else {
681 unsigned int major, minor;
682 ssize_t nr_items;
683
684 td->byte_order = BYTE_ORDER;
685
686 /* Check text-only metadata header and version */
687 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
688 if (nr_items < 2)
689 fprintf(stdout, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
690 if (check_version(major, minor) < 0) {
691 ret = -EINVAL;
692 goto end_packet_read;
693 }
694 rewind(fp);
695 }
696
697 scanner = ctf_scanner_alloc(fp);
698 if (!scanner) {
699 fprintf(stdout, "[error] Error allocating scanner\n");
700 ret = -ENOMEM;
701 goto end_scanner_alloc;
702 }
703 ret = ctf_scanner_append_ast(scanner);
704 if (ret) {
705 fprintf(stdout, "[error] Error creating AST\n");
706 goto end;
707 }
708
709 if (babeltrace_debug) {
710 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
711 if (ret) {
712 fprintf(stdout, "[error] Error visiting AST for XML output\n");
713 goto end;
714 }
715 }
716
717 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
718 if (ret) {
719 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
720 goto end;
721 }
722 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
723 td, td->byte_order);
724 if (ret) {
725 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
726 goto end;
727 }
728 end:
729 ctf_scanner_free(scanner);
730 end_scanner_alloc:
731 end_packet_read:
732 fclose(fp);
733 free(buf);
734 end_stream:
735 close(metadata_stream->pos.fd);
736 if (ret)
737 g_free(metadata_stream);
738 return ret;
739 }
740
741 static
742 struct ctf_stream_event *create_event_definitions(struct ctf_trace *td,
743 struct ctf_stream *stream,
744 struct ctf_event *event)
745 {
746 struct ctf_stream_event *stream_event = g_new0(struct ctf_stream_event, 1);
747
748 if (event->context_decl) {
749 struct definition *definition =
750 event->context_decl->p.definition_new(&event->context_decl->p,
751 stream->parent_def_scope, 0, 0, "event.context");
752 if (!definition) {
753 goto error;
754 }
755 stream_event->event_context = container_of(definition,
756 struct definition_struct, p);
757 stream->parent_def_scope = stream_event->event_context->p.scope;
758 }
759 if (event->fields_decl) {
760 struct definition *definition =
761 event->fields_decl->p.definition_new(&event->fields_decl->p,
762 stream->parent_def_scope, 0, 0, "event.fields");
763 if (!definition) {
764 goto error;
765 }
766 stream_event->event_fields = container_of(definition,
767 struct definition_struct, p);
768 stream->parent_def_scope = stream_event->event_fields->p.scope;
769 }
770 return stream_event;
771
772 error:
773 if (stream_event->event_fields)
774 definition_unref(&stream_event->event_fields->p);
775 if (stream_event->event_context)
776 definition_unref(&stream_event->event_context->p);
777 return NULL;
778 }
779
780 static
781 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
782 {
783 struct ctf_stream_class *stream_class;
784 int ret;
785 int i;
786
787 if (stream->stream_definitions_created)
788 return 0;
789
790 stream_class = stream->stream_class;
791
792 if (stream_class->packet_context_decl) {
793 struct definition *definition =
794 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
795 stream->parent_def_scope, 0, 0, "stream.packet.context");
796 if (!definition) {
797 ret = -EINVAL;
798 goto error;
799 }
800 stream->stream_packet_context = container_of(definition,
801 struct definition_struct, p);
802 stream->parent_def_scope = stream->stream_packet_context->p.scope;
803 }
804 if (stream_class->event_header_decl) {
805 struct definition *definition =
806 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
807 stream->parent_def_scope, 0, 0, "stream.event.header");
808 if (!definition) {
809 ret = -EINVAL;
810 goto error;
811 }
812 stream->stream_event_header =
813 container_of(definition, struct definition_struct, p);
814 stream->parent_def_scope = stream->stream_event_header->p.scope;
815 }
816 if (stream_class->event_context_decl) {
817 struct definition *definition =
818 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
819 stream->parent_def_scope, 0, 0, "stream.event.context");
820 if (!definition) {
821 ret = -EINVAL;
822 goto error;
823 }
824 stream->stream_event_context =
825 container_of(definition, struct definition_struct, p);
826 stream->parent_def_scope = stream->stream_event_context->p.scope;
827 }
828 stream->events_by_id = g_ptr_array_new();
829 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
830 for (i = 0; i < stream->events_by_id->len; i++) {
831 struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
832 struct ctf_stream_event *stream_event;
833
834 if (!event)
835 continue;
836 stream_event = create_event_definitions(td, stream, event);
837 if (!stream_event)
838 goto error_event;
839 g_ptr_array_index(stream->events_by_id, i) = stream_event;
840 }
841 return 0;
842
843 error_event:
844 for (i = 0; i < stream->events_by_id->len; i++) {
845 struct ctf_stream_event *stream_event = g_ptr_array_index(stream->events_by_id, i);
846 if (stream_event)
847 g_free(stream_event);
848 }
849 g_ptr_array_free(stream->events_by_id, TRUE);
850 error:
851 if (stream->stream_event_context)
852 definition_unref(&stream->stream_event_context->p);
853 if (stream->stream_event_header)
854 definition_unref(&stream->stream_event_header->p);
855 if (stream->stream_packet_context)
856 definition_unref(&stream->stream_packet_context->p);
857 return ret;
858 }
859
860
861 static
862 int create_stream_packet_index(struct ctf_trace *td,
863 struct ctf_file_stream *file_stream)
864 {
865 struct ctf_stream_class *stream;
866 int len_index;
867 struct ctf_stream_pos *pos;
868 struct stat filestats;
869 struct packet_index packet_index;
870 int first_packet = 1;
871 int ret;
872
873 pos = &file_stream->pos;
874
875 ret = fstat(pos->fd, &filestats);
876 if (ret < 0)
877 return ret;
878
879 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
880 return -EINVAL;
881
882 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
883 uint64_t stream_id = 0;
884
885 if (pos->base) {
886 /* unmap old base */
887 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
888 if (ret) {
889 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
890 strerror(errno));
891 return ret;
892 }
893 pos->base = NULL;
894 }
895 /* map new base. Need mapping length from header. */
896 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
897 MAP_PRIVATE, pos->fd, pos->mmap_offset);
898 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
899 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
900 pos->offset = 0; /* Position of the packet header */
901
902 packet_index.offset = pos->mmap_offset;
903 packet_index.content_size = 0;
904 packet_index.packet_size = 0;
905 packet_index.timestamp_begin = 0;
906 packet_index.timestamp_end = 0;
907
908 /* read and check header, set stream id (and check) */
909 if (file_stream->parent.trace_packet_header) {
910 /* Read packet header */
911 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
912 if (ret)
913 return ret;
914 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
915 if (len_index >= 0) {
916 struct definition *field;
917 uint64_t magic;
918
919 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
920 magic = get_unsigned_int(field);
921 if (magic != CTF_MAGIC) {
922 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
923 magic,
924 file_stream->pos.packet_index->len,
925 (ssize_t) pos->mmap_offset);
926 return -EINVAL;
927 }
928 }
929
930 /* check uuid */
931 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
932 if (len_index >= 0) {
933 struct definition_array *defarray;
934 struct definition *field;
935 uint64_t i;
936 uint8_t uuidval[UUID_LEN];
937
938 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
939 assert(field->declaration->id == CTF_TYPE_ARRAY);
940 defarray = container_of(field, struct definition_array, p);
941 assert(array_len(defarray) == UUID_LEN);
942
943 for (i = 0; i < UUID_LEN; i++) {
944 struct definition *elem;
945
946 elem = array_index(defarray, i);
947 uuidval[i] = get_unsigned_int(elem);
948 }
949 ret = uuid_compare(td->uuid, uuidval);
950 if (ret) {
951 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
952 return -EINVAL;
953 }
954 }
955
956
957 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
958 if (len_index >= 0) {
959 struct definition *field;
960
961 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
962 stream_id = get_unsigned_int(field);
963 }
964 }
965
966 if (!first_packet && file_stream->parent.stream_id != stream_id) {
967 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
968 return -EINVAL;
969 }
970 if (first_packet) {
971 file_stream->parent.stream_id = stream_id;
972 if (stream_id >= td->streams->len) {
973 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
974 return -EINVAL;
975 }
976 stream = g_ptr_array_index(td->streams, stream_id);
977 if (!stream) {
978 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
979 return -EINVAL;
980 }
981 file_stream->parent.stream_class = stream;
982 ret = create_stream_definitions(td, &file_stream->parent);
983 if (ret)
984 return ret;
985 }
986 first_packet = 0;
987
988 if (file_stream->parent.stream_packet_context) {
989 /* Read packet context */
990 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
991 if (ret)
992 return ret;
993 /* read content size from header */
994 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
995 if (len_index >= 0) {
996 struct definition *field;
997
998 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
999 packet_index.content_size = get_unsigned_int(field);
1000 } else {
1001 /* Use file size for packet size */
1002 packet_index.content_size = filestats.st_size * CHAR_BIT;
1003 }
1004
1005 /* read packet size from header */
1006 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1007 if (len_index >= 0) {
1008 struct definition *field;
1009
1010 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1011 packet_index.packet_size = get_unsigned_int(field);
1012 } else {
1013 /* Use content size if non-zero, else file size */
1014 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1015 }
1016
1017 /* read timestamp begin from header */
1018 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1019 if (len_index >= 0) {
1020 struct definition *field;
1021
1022 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1023 packet_index.timestamp_begin = get_unsigned_int(field);
1024 }
1025
1026 /* read timestamp end from header */
1027 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1028 if (len_index >= 0) {
1029 struct definition *field;
1030
1031 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1032 packet_index.timestamp_end = get_unsigned_int(field);
1033 }
1034 } else {
1035 /* Use file size for packet size */
1036 packet_index.content_size = filestats.st_size * CHAR_BIT;
1037 /* Use content size if non-zero, else file size */
1038 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1039 }
1040
1041 /* Validate content size and packet size values */
1042 if (packet_index.content_size > packet_index.packet_size) {
1043 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1044 packet_index.content_size, packet_index.packet_size);
1045 return -EINVAL;
1046 }
1047
1048 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
1049 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1050 packet_index.content_size, (size_t) (filestats.st_size - packet_index.offset) * CHAR_BIT);
1051 return -EINVAL;
1052 }
1053
1054 /* Save position after header and context */
1055 packet_index.data_offset = pos->offset;
1056
1057 /* add index to packet array */
1058 g_array_append_val(file_stream->pos.packet_index, packet_index);
1059
1060 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
1061 }
1062
1063 /* Move pos back to beginning of file */
1064 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
1065
1066 return 0;
1067 }
1068
1069 static
1070 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
1071 {
1072 int ret;
1073
1074 if (td->packet_header_decl) {
1075 struct definition *definition =
1076 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1077 stream->parent_def_scope, 0, 0, "trace.packet.header");
1078 if (!definition) {
1079 ret = -EINVAL;
1080 goto error;
1081 }
1082 stream->trace_packet_header =
1083 container_of(definition, struct definition_struct, p);
1084 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1085 }
1086
1087 return 0;
1088
1089 error:
1090 return ret;
1091 }
1092
1093 /*
1094 * Note: many file streams can inherit from the same stream class
1095 * description (metadata).
1096 */
1097 static
1098 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1099 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1100 int whence))
1101 {
1102 int ret;
1103 struct ctf_file_stream *file_stream;
1104
1105 ret = openat(td->dirfd, path, flags);
1106 if (ret < 0) {
1107 perror("File stream openat()");
1108 goto error;
1109 }
1110 file_stream = g_new0(struct ctf_file_stream, 1);
1111
1112 if (move_pos_slow) {
1113 file_stream->pos.move_pos_slow = move_pos_slow;
1114 } else {
1115 fprintf(stderr, "[error] move_pos_slow function undefined.\n");
1116 ret = -1;
1117 goto error_def;
1118 }
1119
1120 ctf_init_pos(&file_stream->pos, ret, flags);
1121 ret = create_trace_definitions(td, &file_stream->parent);
1122 if (ret)
1123 goto error_def;
1124 ret = create_stream_packet_index(td, file_stream);
1125 if (ret)
1126 goto error_index;
1127 /* Add stream file to stream class */
1128 g_ptr_array_add(file_stream->parent.stream_class->streams,
1129 &file_stream->parent);
1130 return 0;
1131
1132 error_index:
1133 if (file_stream->parent.trace_packet_header)
1134 definition_unref(&file_stream->parent.trace_packet_header->p);
1135 error_def:
1136 ctf_fini_pos(&file_stream->pos);
1137 close(file_stream->pos.fd);
1138 g_free(file_stream);
1139 error:
1140 return ret;
1141 }
1142
1143 static void
1144 init_domain_name(struct ctf_trace *td)
1145 {
1146 char *start, *end;
1147
1148 start = td->path + strlen(td->collection_path);
1149 while (start[0] == '/')
1150 start++; /* skip / */
1151 end = strchr(start, '/');
1152 if (!end)
1153 end = start + strlen(start);
1154 memcpy(td->domain, start, end - start);
1155 td->domain[end - start] = '\0';
1156 }
1157
1158 static void
1159 init_proc_name(struct ctf_trace *td)
1160 {
1161 char buf[PATH_MAX];
1162 char *start, *end;
1163
1164 if (td->domain[0] == '\0')
1165 return;
1166 memcpy(buf, td->path, PATH_MAX);
1167 start = buf + strlen(td->collection_path);
1168 while (start[0] == '/')
1169 start++; /* skip / */
1170 start = strchr(start, '/'); /* get begin of domain content */
1171 if (!start)
1172 return;
1173 while (start[0] == '/')
1174 start++; /* skip / */
1175 /* find last -, skips time */
1176 end = strrchr(start, '-');
1177 if (!end)
1178 return;
1179 *end = '\0';
1180 /* find previous -, skips date */
1181 end = strrchr(start, '-');
1182 if (!end)
1183 return;
1184 *end = '\0';
1185 /* find previous -, skips pid */
1186 end = strrchr(start, '-');
1187 if (!end)
1188 return;
1189 *end = '\0';
1190
1191 memcpy(td->procname, start, end - start);
1192 td->procname[end - start] = '\0';
1193 }
1194
1195 static void
1196 init_vpid(struct ctf_trace *td)
1197 {
1198 char buf[PATH_MAX];
1199 char *start, *end;
1200
1201 if (td->domain[0] == '\0')
1202 return;
1203 memcpy(buf, td->path, PATH_MAX);
1204 start = buf + strlen(td->collection_path);
1205 while (start[0] == '/')
1206 start++; /* skip / */
1207 start = strchr(start, '/'); /* get begin of domain content */
1208 if (!start)
1209 return;
1210 while (start[0] == '/')
1211 start++; /* skip / */
1212 /* find last -, skips time */
1213 end = strrchr(start, '-');
1214 if (!end)
1215 return;
1216 *end = '\0';
1217 /* find previous -, skips date */
1218 end = strrchr(start, '-');
1219 if (!end)
1220 return;
1221 *end = '\0';
1222 /* find previous -, skips pid */
1223 start = strrchr(start, '-');
1224 if (!start)
1225 return;
1226 start++; /* skip - */
1227
1228 memcpy(td->vpid, start, end - start);
1229 td->vpid[end - start] = '\0';
1230 }
1231
1232 static
1233 int ctf_open_trace_read(struct ctf_trace *td, const char *collection_path,
1234 const char *path, int flags,
1235 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1236 int whence), FILE *metadata_fp)
1237 {
1238 int ret;
1239 struct dirent *dirent;
1240 struct dirent *diriter;
1241 size_t dirent_len;
1242
1243 td->flags = flags;
1244
1245 /* Open trace directory */
1246 td->dir = opendir(path);
1247 if (!td->dir) {
1248 fprintf(stdout, "[error] Unable to open trace directory.\n");
1249 ret = -ENOENT;
1250 goto error;
1251 }
1252
1253 td->dirfd = open(path, 0);
1254 if (td->dirfd < 0) {
1255 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
1256 perror("Trace directory open");
1257 ret = -errno;
1258 goto error_dirfd;
1259 }
1260 strncpy(td->collection_path, collection_path, PATH_MAX);
1261 td->collection_path[PATH_MAX - 1] = '\0';
1262 strncpy(td->path, path, PATH_MAX);
1263 td->path[PATH_MAX - 1] = '\0';
1264 init_domain_name(td);
1265 init_proc_name(td);
1266 init_vpid(td);
1267
1268 /*
1269 * Keep the metadata file separate.
1270 */
1271
1272 ret = ctf_open_trace_metadata_read(td, move_pos_slow, metadata_fp);
1273 if (ret) {
1274 goto error_metadata;
1275 }
1276
1277 /*
1278 * Open each stream: for each file, try to open, check magic
1279 * number, and get the stream ID to add to the right location in
1280 * the stream array.
1281 */
1282
1283 dirent_len = offsetof(struct dirent, d_name) +
1284 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1285
1286 dirent = malloc(dirent_len);
1287
1288 for (;;) {
1289 ret = readdir_r(td->dir, dirent, &diriter);
1290 if (ret) {
1291 fprintf(stdout, "[error] Readdir error.\n");
1292 goto readdir_error;
1293 }
1294 if (!diriter)
1295 break;
1296 /* Ignore hidden files, ., .. and metadata. */
1297 if (!strncmp(diriter->d_name, ".", 1)
1298 || !strcmp(diriter->d_name, "..")
1299 || !strcmp(diriter->d_name, "metadata"))
1300 continue;
1301 ret = ctf_open_file_stream_read(td, diriter->d_name, flags, move_pos_slow);
1302 if (ret) {
1303 fprintf(stdout, "[error] Open file stream error.\n");
1304 goto readdir_error;
1305 }
1306 }
1307
1308 free(dirent);
1309 return 0;
1310
1311 readdir_error:
1312 free(dirent);
1313 error_metadata:
1314 close(td->dirfd);
1315 error_dirfd:
1316 closedir(td->dir);
1317 error:
1318 return ret;
1319 }
1320
1321 static
1322 struct trace_descriptor *ctf_open_trace(const char *collection_path, const char *path, int flags,
1323 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1324 int whence), FILE *metadata_fp)
1325 {
1326 struct ctf_trace *td;
1327 int ret;
1328
1329 td = g_new0(struct ctf_trace, 1);
1330
1331 switch (flags & O_ACCMODE) {
1332 case O_RDONLY:
1333 if (!path) {
1334 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
1335 goto error;
1336 }
1337 ret = ctf_open_trace_read(td, collection_path, path, flags, move_pos_slow, metadata_fp);
1338 if (ret)
1339 goto error;
1340 break;
1341 case O_RDWR:
1342 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
1343 goto error;
1344 default:
1345 fprintf(stdout, "[error] Incorrect open flags.\n");
1346 goto error;
1347 }
1348
1349 return &td->parent;
1350 error:
1351 g_free(td);
1352 return NULL;
1353 }
1354
1355
1356 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
1357 struct mmap_stream *mmap_info)
1358 {
1359 pos->mmap_offset = 0;
1360 pos->packet_size = 0;
1361 pos->content_size = 0;
1362 pos->content_size_loc = NULL;
1363 pos->fd = mmap_info->fd;
1364 pos->base = 0;
1365 pos->offset = 0;
1366 pos->dummy = false;
1367 pos->cur_index = 0;
1368 pos->packet_index = NULL;
1369 pos->prot = PROT_READ;
1370 pos->flags = MAP_PRIVATE;
1371 pos->parent.rw_table = read_dispatch_table;
1372 pos->parent.event_cb = ctf_read_event;
1373 }
1374
1375 static
1376 int prepare_mmap_stream_definition(struct ctf_trace *td,
1377 struct ctf_file_stream *file_stream)
1378 {
1379 struct ctf_stream_class *stream;
1380 uint64_t stream_id = 0;
1381 int ret;
1382
1383 file_stream->parent.stream_id = stream_id;
1384 if (stream_id >= td->streams->len) {
1385 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared "
1386 "in metadata.\n", stream_id);
1387 ret = -EINVAL;
1388 goto end;
1389 }
1390 stream = g_ptr_array_index(td->streams, stream_id);
1391 if (!stream) {
1392 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared "
1393 "in metadata.\n", stream_id);
1394 ret = -EINVAL;
1395 goto end;
1396 }
1397 file_stream->parent.stream_class = stream;
1398 ret = create_stream_definitions(td, &file_stream->parent);
1399 end:
1400 return ret;
1401 }
1402
1403 static
1404 int ctf_open_mmap_stream_read(struct ctf_trace *td,
1405 struct mmap_stream *mmap_info,
1406 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1407 int whence))
1408 {
1409 int ret;
1410 struct ctf_file_stream *file_stream;
1411
1412 file_stream = g_new0(struct ctf_file_stream, 1);
1413 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1414
1415 file_stream->pos.move_pos_slow = move_pos_slow;
1416
1417 ret = create_trace_definitions(td, &file_stream->parent);
1418 if (ret) {
1419 goto error_def;
1420 }
1421
1422 ret = prepare_mmap_stream_definition(td, file_stream);
1423 if (ret)
1424 goto error_index;
1425
1426 /* Add stream file to stream class */
1427 g_ptr_array_add(file_stream->parent.stream_class->streams,
1428 &file_stream->parent);
1429 return 0;
1430
1431 error_index:
1432 if (file_stream->parent.trace_packet_header)
1433 definition_unref(&file_stream->parent.trace_packet_header->p);
1434 error_def:
1435 g_free(file_stream);
1436 return ret;
1437 }
1438
1439 int ctf_open_mmap_trace_read(struct ctf_trace *td,
1440 struct mmap_stream_list *mmap_list,
1441 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1442 int whence),
1443 FILE *metadata_fp)
1444 {
1445 int ret;
1446 struct mmap_stream *mmap_info;
1447
1448 ret = ctf_open_trace_metadata_read(td, ctf_move_pos_slow, metadata_fp);
1449 if (ret) {
1450 goto error;
1451 }
1452
1453 /*
1454 * for each stream, try to open, check magic number, and get the
1455 * stream ID to add to the right location in the stream array.
1456 */
1457 cds_list_for_each_entry(mmap_info, &mmap_list->head, list) {
1458 ret = ctf_open_mmap_stream_read(td, mmap_info, move_pos_slow);
1459 if (ret) {
1460 fprintf(stdout, "[error] Open file mmap stream error.\n");
1461 goto error;
1462 }
1463 }
1464
1465 return 0;
1466
1467 error:
1468 return ret;
1469 }
1470
1471 static
1472 struct trace_descriptor *ctf_open_mmap_trace(
1473 struct mmap_stream_list *mmap_list,
1474 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, int whence),
1475 FILE *metadata_fp)
1476 {
1477 struct ctf_trace *td;
1478 int ret;
1479
1480 if (!metadata_fp) {
1481 fprintf(stderr, "[error] No metadata file pointer associated, "
1482 "required for mmap parsing\n");
1483 goto error;
1484 }
1485 if (!move_pos_slow) {
1486 fprintf(stderr, "[error] move_pos_slow function undefined.\n");
1487 goto error;
1488 }
1489 td = g_new0(struct ctf_trace, 1);
1490 ret = ctf_open_mmap_trace_read(td, mmap_list, move_pos_slow, metadata_fp);
1491 if (ret)
1492 goto error_free;
1493
1494 return &td->parent;
1495
1496 error_free:
1497 g_free(td);
1498 error:
1499 return NULL;
1500 }
1501
1502 static
1503 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1504 {
1505 ctf_fini_pos(&file_stream->pos);
1506 close(file_stream->pos.fd);
1507 }
1508
1509 static
1510 void ctf_close_trace(struct trace_descriptor *tdp)
1511 {
1512 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
1513 int i;
1514
1515 if (td->streams) {
1516 for (i = 0; i < td->streams->len; i++) {
1517 struct ctf_stream_class *stream;
1518 int j;
1519
1520 stream = g_ptr_array_index(td->streams, i);
1521 if (!stream)
1522 continue;
1523 for (j = 0; j < stream->streams->len; j++) {
1524 struct ctf_file_stream *file_stream;
1525 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
1526 ctf_close_file_stream(file_stream);
1527 }
1528
1529 }
1530 g_ptr_array_free(td->streams, TRUE);
1531 }
1532 closedir(td->dir);
1533 g_free(td);
1534 }
1535
1536 void __attribute__((constructor)) ctf_init(void)
1537 {
1538 int ret;
1539
1540 ctf_format.name = g_quark_from_static_string("ctf");
1541 ret = bt_register_format(&ctf_format);
1542 assert(!ret);
1543 }
1544
1545 /* TODO: finalize */
This page took 0.070118 seconds and 5 git commands to generate.