move struct ctf_stream -> struct ctf_stream_class
[babeltrace.git] / formats / ctf / ctf.c
1 /*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * Format registration.
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/format.h>
20 #include <babeltrace/ctf/types.h>
21 #include <babeltrace/ctf/metadata.h>
22 #include <babeltrace/babeltrace.h>
23 #include <inttypes.h>
24 #include <uuid/uuid.h>
25 #include <sys/mman.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <dirent.h>
31 #include <glib.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34
35 #include "metadata/ctf-scanner.h"
36 #include "metadata/ctf-parser.h"
37 #include "metadata/ctf-ast.h"
38
39 /*
40 * We currently simply map a page to read the packet header and packet
41 * context to get the packet length and content length. (in bits)
42 */
43 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
44 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
45 #define UUID_LEN 16 /* uuid by value len */
46
47 extern int yydebug;
48
49 struct trace_descriptor *ctf_open_trace(const char *path, int flags);
50 void ctf_close_trace(struct trace_descriptor *descriptor);
51
52 static
53 rw_dispatch read_dispatch_table[] = {
54 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
55 [ CTF_TYPE_FLOAT ] = ctf_float_read,
56 [ CTF_TYPE_ENUM ] = ctf_enum_read,
57 [ CTF_TYPE_STRING ] = ctf_string_read,
58 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
59 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
60 [ CTF_TYPE_ARRAY ] = ctf_array_rw,
61 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_rw,
62 };
63
64 static
65 rw_dispatch write_dispatch_table[] = {
66 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
67 [ CTF_TYPE_FLOAT ] = ctf_float_write,
68 [ CTF_TYPE_ENUM ] = ctf_enum_write,
69 [ CTF_TYPE_STRING ] = ctf_string_write,
70 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
71 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
72 [ CTF_TYPE_ARRAY ] = ctf_array_rw,
73 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_rw,
74 };
75
76 static
77 struct format ctf_format = {
78 .open_trace = ctf_open_trace,
79 .close_trace = ctf_close_trace,
80 };
81
82 static
83 int ctf_read_event(struct stream_pos *ppos, struct ctf_stream_class *stream_class)
84 {
85 struct ctf_stream_pos *pos =
86 container_of(ppos, struct ctf_stream_pos, parent);
87 struct ctf_event *event_class;
88 uint64_t id = 0;
89 int len_index;
90 int ret;
91
92 if (pos->offset == EOF)
93 return EOF;
94
95 /* Read event header */
96 if (stream_class->event_header) {
97 ret = generic_rw(ppos, &stream_class->event_header->p);
98 if (ret)
99 goto error;
100 /* lookup event id */
101 len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl,
102 g_quark_from_static_string("id"));
103 if (len_index >= 0) {
104 struct definition_integer *defint;
105 struct definition *field;
106
107 field = struct_definition_get_field_from_index(stream_class->event_header, len_index);
108 assert(field->declaration->id == CTF_TYPE_INTEGER);
109 defint = container_of(field, struct definition_integer, p);
110 assert(defint->declaration->signedness == FALSE);
111 id = defint->value._unsigned; /* set id */
112 }
113 }
114
115 /* Read stream-declared event context */
116 if (stream_class->event_context) {
117 ret = generic_rw(ppos, &stream_class->event_context->p);
118 if (ret)
119 goto error;
120 }
121
122 if (id >= stream_class->events_by_id->len) {
123 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
124 return -EINVAL;
125 }
126 event_class = g_ptr_array_index(stream_class->events_by_id, id);
127 if (!event_class) {
128 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
129 return -EINVAL;
130 }
131
132 /* Read event-declared event context */
133 if (event_class->context) {
134 ret = generic_rw(ppos, &event_class->context->p);
135 if (ret)
136 goto error;
137 }
138
139 /* Read event payload */
140 if (event_class->fields) {
141 ret = generic_rw(ppos, &event_class->fields->p);
142 if (ret)
143 goto error;
144 }
145
146 return 0;
147
148 error:
149 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
150 return ret;
151 }
152
153 static
154 int ctf_write_event(struct stream_pos *pos, struct ctf_stream_class *stream_class)
155 {
156 struct ctf_event *event_class;
157 uint64_t id = 0;
158 int len_index;
159 int ret;
160
161 /* print event header */
162 if (stream_class->event_header) {
163 /* lookup event id */
164 len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl,
165 g_quark_from_static_string("id"));
166 if (len_index >= 0) {
167 struct definition_integer *defint;
168 struct definition *field;
169
170 field = struct_definition_get_field_from_index(stream_class->event_header, len_index);
171 assert(field->declaration->id == CTF_TYPE_INTEGER);
172 defint = container_of(field, struct definition_integer, p);
173 assert(defint->declaration->signedness == FALSE);
174 id = defint->value._unsigned; /* set id */
175 }
176
177 ret = generic_rw(pos, &stream_class->event_header->p);
178 if (ret)
179 goto error;
180 }
181
182 /* print stream-declared event context */
183 if (stream_class->event_context) {
184 ret = generic_rw(pos, &stream_class->event_context->p);
185 if (ret)
186 goto error;
187 }
188
189 if (id >= stream_class->events_by_id->len) {
190 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
191 return -EINVAL;
192 }
193 event_class = g_ptr_array_index(stream_class->events_by_id, id);
194 if (!event_class) {
195 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
196 return -EINVAL;
197 }
198
199 /* print event-declared event context */
200 if (event_class->context) {
201 ret = generic_rw(pos, &event_class->context->p);
202 if (ret)
203 goto error;
204 }
205
206 /* Read and print event payload */
207 if (event_class->fields) {
208 ret = generic_rw(pos, &event_class->fields->p);
209 if (ret)
210 goto error;
211 }
212
213 return 0;
214
215 error:
216 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
217 return ret;
218 }
219
220 void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
221 {
222 pos->fd = fd;
223 pos->mmap_offset = 0;
224 pos->packet_size = 0;
225 pos->content_size = 0;
226 pos->content_size_loc = NULL;
227 pos->base = NULL;
228 pos->offset = 0;
229 pos->dummy = false;
230 pos->cur_index = 0;
231 if (fd >= 0)
232 pos->packet_index = g_array_new(FALSE, TRUE,
233 sizeof(struct packet_index));
234 else
235 pos->packet_index = NULL;
236 switch (open_flags & O_ACCMODE) {
237 case O_RDONLY:
238 pos->prot = PROT_READ;
239 pos->flags = MAP_PRIVATE;
240 pos->parent.rw_table = read_dispatch_table;
241 pos->parent.event_cb = ctf_read_event;
242 break;
243 case O_RDWR:
244 pos->prot = PROT_WRITE; /* Write has priority */
245 pos->flags = MAP_SHARED;
246 pos->parent.rw_table = write_dispatch_table;
247 pos->parent.event_cb = ctf_write_event;
248 if (fd >= 0)
249 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
250 break;
251 default:
252 assert(0);
253 }
254 }
255
256 void ctf_fini_pos(struct ctf_stream_pos *pos)
257 {
258 int ret;
259
260 if (pos->prot == PROT_WRITE && pos->content_size_loc)
261 *pos->content_size_loc = pos->offset;
262 if (pos->base) {
263 /* unmap old base */
264 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
265 if (ret) {
266 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
267 strerror(errno));
268 assert(0);
269 }
270 }
271 (void) g_array_free(pos->packet_index, TRUE);
272 }
273
274 void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
275 {
276 int ret;
277 off_t off;
278 struct packet_index *index;
279
280 if (pos->prot == PROT_WRITE && pos->content_size_loc)
281 *pos->content_size_loc = pos->offset;
282
283 if (pos->base) {
284 /* unmap old base */
285 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
286 if (ret) {
287 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
288 strerror(errno));
289 assert(0);
290 }
291 pos->base = NULL;
292 }
293
294 /*
295 * The caller should never ask for ctf_move_pos across packets,
296 * except to get exactly at the beginning of the next packet.
297 */
298 if (pos->prot == PROT_WRITE) {
299 switch (whence) {
300 case SEEK_CUR:
301 /* The writer will add padding */
302 assert(pos->offset + offset == pos->packet_size);
303 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
304 break;
305 case SEEK_SET:
306 assert(offset == 0); /* only seek supported for now */
307 pos->cur_index = 0;
308 break;
309 default:
310 assert(0);
311 }
312 pos->content_size = -1U; /* Unknown at this point */
313 pos->packet_size = WRITE_PACKET_LEN;
314 off = posix_fallocate(pos->fd, pos->mmap_offset,
315 pos->packet_size / CHAR_BIT);
316 assert(off >= 0);
317 pos->offset = 0;
318 } else {
319 switch (whence) {
320 case SEEK_CUR:
321 /* The reader will expect us to skip padding */
322 assert(pos->offset + offset == pos->content_size);
323 ++pos->cur_index;
324 break;
325 case SEEK_SET:
326 assert(offset == 0); /* only seek supported for now */
327 pos->cur_index = 0;
328 break;
329 default:
330 assert(0);
331 }
332 if (pos->cur_index >= pos->packet_index->len) {
333 pos->offset = EOF;
334 return;
335 }
336 index = &g_array_index(pos->packet_index, struct packet_index,
337 pos->cur_index);
338 pos->mmap_offset = index->offset;
339
340 /* Lookup context/packet size in index */
341 pos->content_size = index->content_size;
342 pos->packet_size = index->packet_size;
343 pos->offset = index->data_offset;
344 }
345 /* map new base. Need mapping length from header. */
346 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
347 pos->flags, pos->fd, pos->mmap_offset);
348 if (pos->base == MAP_FAILED) {
349 fprintf(stdout, "[error] mmap error %s.\n",
350 strerror(errno));
351 assert(0);
352 }
353 }
354
355 /*
356 * TODO: for now, we treat the metadata file as a simple text file
357 * (without any header nor packets nor padding).
358 */
359 static
360 int ctf_open_trace_metadata_read(struct ctf_trace *td)
361 {
362 struct ctf_scanner *scanner;
363 FILE *fp;
364 int ret = 0;
365
366 td->metadata.pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
367 if (td->metadata.pos.fd < 0) {
368 fprintf(stdout, "Unable to open metadata.\n");
369 return td->metadata.pos.fd;
370 }
371
372 if (babeltrace_debug)
373 yydebug = 1;
374
375 fp = fdopen(td->metadata.pos.fd, "r");
376 if (!fp) {
377 fprintf(stdout, "[error] Unable to open metadata stream.\n");
378 ret = -errno;
379 goto end_stream;
380 }
381
382 scanner = ctf_scanner_alloc(fp);
383 if (!scanner) {
384 fprintf(stdout, "[error] Error allocating scanner\n");
385 ret = -ENOMEM;
386 goto end_scanner_alloc;
387 }
388 ret = ctf_scanner_append_ast(scanner);
389 if (ret) {
390 fprintf(stdout, "[error] Error creating AST\n");
391 goto end;
392 }
393
394 if (babeltrace_debug) {
395 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
396 if (ret) {
397 fprintf(stdout, "[error] Error visiting AST for XML output\n");
398 goto end;
399 }
400 }
401
402 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
403 if (ret) {
404 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
405 goto end;
406 }
407 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
408 td, BYTE_ORDER);
409 if (ret) {
410 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
411 goto end;
412 }
413 end:
414 ctf_scanner_free(scanner);
415 end_scanner_alloc:
416 fclose(fp);
417 end_stream:
418 close(td->metadata.pos.fd);
419 return ret;
420 }
421
422
423 static
424 int create_stream_packet_index(struct ctf_trace *td,
425 struct ctf_file_stream *file_stream)
426 {
427 struct ctf_stream_class *stream;
428 int len_index;
429 struct ctf_stream_pos *pos;
430 struct stat filestats;
431 struct packet_index packet_index;
432 int first_packet = 1;
433 int ret;
434
435 pos = &file_stream->pos;
436
437 ret = fstat(pos->fd, &filestats);
438 if (ret < 0)
439 return ret;
440
441 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
442 uint64_t stream_id = 0;
443
444 if (pos->base) {
445 /* unmap old base */
446 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
447 if (ret) {
448 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
449 strerror(errno));
450 return ret;
451 }
452 pos->base = NULL;
453 }
454 /* map new base. Need mapping length from header. */
455 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
456 MAP_PRIVATE, pos->fd, pos->mmap_offset);
457 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
458 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
459 pos->offset = 0; /* Position of the packet header */
460
461 packet_index.offset = pos->mmap_offset;
462 packet_index.content_size = 0;
463 packet_index.packet_size = 0;
464
465 /* read and check header, set stream id (and check) */
466 if (td->packet_header) {
467 /* Read packet header */
468 ret = generic_rw(&pos->parent, &td->packet_header->p);
469 if (ret)
470 return ret;
471 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic"));
472 if (len_index >= 0) {
473 struct definition_integer *defint;
474 struct definition *field;
475
476 field = struct_definition_get_field_from_index(td->packet_header, len_index);
477 assert(field->declaration->id == CTF_TYPE_INTEGER);
478 defint = container_of(field, struct definition_integer, p);
479 assert(defint->declaration->signedness == FALSE);
480 if (defint->value._unsigned != CTF_MAGIC) {
481 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
482 defint->value._unsigned,
483 file_stream->pos.packet_index->len,
484 (ssize_t) pos->mmap_offset);
485 return -EINVAL;
486 }
487 }
488
489 /* check uuid */
490 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("trace_uuid"));
491 if (len_index >= 0) {
492 struct definition_array *defarray;
493 struct definition *field;
494 uint64_t i;
495 uint8_t uuidval[UUID_LEN];
496
497 field = struct_definition_get_field_from_index(td->packet_header, len_index);
498 assert(field->declaration->id == CTF_TYPE_ARRAY);
499 defarray = container_of(field, struct definition_array, p);
500 assert(array_len(defarray) == UUID_LEN);
501 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
502
503 for (i = 0; i < UUID_LEN; i++) {
504 struct definition *elem;
505 struct definition_integer *defint;
506
507 elem = array_index(defarray, i);
508 assert(elem);
509 defint = container_of(elem, struct definition_integer, p);
510 uuidval[i] = defint->value._unsigned;
511 }
512 ret = uuid_compare(td->uuid, uuidval);
513 if (ret) {
514 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
515 return -EINVAL;
516 }
517 }
518
519
520 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("stream_id"));
521 if (len_index >= 0) {
522 struct definition_integer *defint;
523 struct definition *field;
524
525 field = struct_definition_get_field_from_index(td->packet_header, len_index);
526 assert(field->declaration->id == CTF_TYPE_INTEGER);
527 defint = container_of(field, struct definition_integer, p);
528 assert(defint->declaration->signedness == FALSE);
529 stream_id = defint->value._unsigned;
530 }
531 }
532
533 if (!first_packet && file_stream->stream_id != stream_id) {
534 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
535 return -EINVAL;
536 }
537 if (first_packet) {
538 file_stream->stream_id = stream_id;
539 if (stream_id >= td->streams->len) {
540 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
541 return -EINVAL;
542 }
543 stream = g_ptr_array_index(td->streams, stream_id);
544 if (!stream) {
545 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
546 return -EINVAL;
547 }
548 file_stream->stream = stream;
549 }
550 first_packet = 0;
551
552 if (stream->packet_context) {
553 /* Read packet context */
554 ret = generic_rw(&pos->parent, &stream->packet_context->p);
555 if (ret)
556 return ret;
557 /* read content size from header */
558 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size"));
559 if (len_index >= 0) {
560 struct definition_integer *defint;
561 struct definition *field;
562
563 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
564 assert(field->declaration->id == CTF_TYPE_INTEGER);
565 defint = container_of(field, struct definition_integer, p);
566 assert(defint->declaration->signedness == FALSE);
567 packet_index.content_size = defint->value._unsigned;
568 } else {
569 /* Use file size for packet size */
570 packet_index.content_size = filestats.st_size * CHAR_BIT;
571 }
572
573 /* read packet size from header */
574 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size"));
575 if (len_index >= 0) {
576 struct definition_integer *defint;
577 struct definition *field;
578
579 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
580 assert(field->declaration->id == CTF_TYPE_INTEGER);
581 defint = container_of(field, struct definition_integer, p);
582 assert(defint->declaration->signedness == FALSE);
583 packet_index.packet_size = defint->value._unsigned;
584 } else {
585 /* Use content size if non-zero, else file size */
586 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
587 }
588 } else {
589 /* Use file size for packet size */
590 packet_index.content_size = filestats.st_size * CHAR_BIT;
591 /* Use content size if non-zero, else file size */
592 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
593 }
594
595 /* Validate content size and packet size values */
596 if (packet_index.content_size > packet_index.packet_size) {
597 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
598 packet_index.content_size, packet_index.packet_size);
599 return -EINVAL;
600 }
601
602 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
603 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
604 packet_index.content_size, (filestats.st_size - packet_index.offset) * CHAR_BIT);
605 return -EINVAL;
606 }
607
608 /* Save position after header and context */
609 packet_index.data_offset = pos->offset;
610
611 /* add index to packet array */
612 g_array_append_val(file_stream->pos.packet_index, packet_index);
613
614 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
615 }
616
617 /* Move pos back to beginning of file */
618 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
619
620 return 0;
621 }
622
623 /*
624 * Note: many file streams can inherit from the same stream class
625 * description (metadata).
626 */
627 static
628 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
629 {
630 int ret;
631 struct ctf_file_stream *file_stream;
632
633 ret = openat(td->dirfd, path, flags);
634 if (ret < 0)
635 goto error;
636 file_stream = g_new0(struct ctf_file_stream, 1);
637 ctf_init_pos(&file_stream->pos, ret, flags);
638 ret = create_stream_packet_index(td, file_stream);
639 if (ret)
640 goto error_index;
641 /* Add stream file to stream class */
642 g_ptr_array_add(file_stream->stream->files, file_stream);
643 return 0;
644
645 error_index:
646 ctf_fini_pos(&file_stream->pos);
647 close(file_stream->pos.fd);
648 g_free(file_stream);
649 error:
650 return ret;
651 }
652
653 static
654 int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
655 {
656 int ret;
657 struct dirent *dirent;
658 struct dirent *diriter;
659 size_t dirent_len;
660
661 td->flags = flags;
662
663 /* Open trace directory */
664 td->dir = opendir(path);
665 if (!td->dir) {
666 fprintf(stdout, "[error] Unable to open trace directory.\n");
667 ret = -ENOENT;
668 goto error;
669 }
670
671 td->dirfd = open(path, 0);
672 if (td->dirfd < 0) {
673 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
674 ret = -ENOENT;
675 goto error_dirfd;
676 }
677
678 td->streams = g_ptr_array_new();
679
680 /*
681 * Keep the metadata file separate.
682 */
683
684 ret = ctf_open_trace_metadata_read(td);
685 if (ret) {
686 goto error_metadata;
687 }
688
689 /*
690 * Open each stream: for each file, try to open, check magic
691 * number, and get the stream ID to add to the right location in
692 * the stream array.
693 */
694
695 dirent_len = offsetof(struct dirent, d_name) +
696 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
697
698 dirent = malloc(dirent_len);
699
700 for (;;) {
701 ret = readdir_r(td->dir, dirent, &diriter);
702 if (ret) {
703 fprintf(stdout, "[error] Readdir error.\n");
704 goto readdir_error;
705 }
706 if (!diriter)
707 break;
708 /* Ignore hidden files, ., .. and metadata. */
709 if (!strncmp(diriter->d_name, ".", 1)
710 || !strcmp(diriter->d_name, "..")
711 || !strcmp(diriter->d_name, "metadata"))
712 continue;
713 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
714 if (ret) {
715 fprintf(stdout, "[error] Open file stream error.\n");
716 goto readdir_error;
717 }
718 }
719
720 free(dirent);
721 return 0;
722
723 readdir_error:
724 free(dirent);
725 error_metadata:
726 g_ptr_array_free(td->streams, TRUE);
727 close(td->dirfd);
728 error_dirfd:
729 closedir(td->dir);
730 error:
731 return ret;
732 }
733
734 struct trace_descriptor *ctf_open_trace(const char *path, int flags)
735 {
736 struct ctf_trace *td;
737 int ret;
738
739 td = g_new0(struct ctf_trace, 1);
740
741 switch (flags & O_ACCMODE) {
742 case O_RDONLY:
743 if (!path) {
744 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
745 goto error;
746 }
747 ret = ctf_open_trace_read(td, path, flags);
748 if (ret)
749 goto error;
750 break;
751 case O_RDWR:
752 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
753 goto error;
754 default:
755 fprintf(stdout, "[error] Incorrect open flags.\n");
756 goto error;
757 }
758
759 return &td->parent;
760 error:
761 g_free(td);
762 return NULL;
763 }
764
765 static
766 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
767 {
768 ctf_fini_pos(&file_stream->pos);
769 close(file_stream->pos.fd);
770 }
771
772 void ctf_close_trace(struct trace_descriptor *tdp)
773 {
774 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
775 int i;
776
777 if (td->streams) {
778 for (i = 0; i < td->streams->len; i++) {
779 struct ctf_stream_class *stream;
780 int j;
781 stream = g_ptr_array_index(td->streams, i);
782 for (j = 0; j < stream->files->len; j++) {
783 struct ctf_file_stream *file_stream;
784 file_stream = g_ptr_array_index(stream->files, j);
785 ctf_close_file_stream(file_stream);
786 }
787
788 }
789 g_ptr_array_free(td->streams, TRUE);
790 }
791 closedir(td->dir);
792 g_free(td);
793 }
794
795 void __attribute__((constructor)) ctf_init(void)
796 {
797 int ret;
798
799 ctf_format.name = g_quark_from_static_string("ctf");
800 ret = bt_register_format(&ctf_format);
801 assert(!ret);
802 }
803
804 /* TODO: finalize */
This page took 0.045656 seconds and 4 git commands to generate.