Change -EOF for EOF (it is worth -1), fix assertion
[babeltrace.git] / formats / ctf / ctf.c
CommitLineData
fc93b2bd
MD
1/*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * Format registration.
5 *
c054553d 6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fc93b2bd 7 *
ccd7e1c8
MD
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:
fc93b2bd 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
fc93b2bd
MD
17 */
18
19#include <babeltrace/format.h>
20#include <babeltrace/ctf/types.h>
bbefb8dd 21#include <babeltrace/ctf/metadata.h>
65102a8c 22#include <babeltrace/babeltrace.h>
0f980a35
MD
23#include <inttypes.h>
24#include <uuid/uuid.h>
25#include <sys/mman.h>
bbefb8dd 26#include <errno.h>
bbefb8dd 27#include <sys/types.h>
65102a8c 28#include <sys/stat.h>
bbefb8dd 29#include <fcntl.h>
65102a8c 30#include <dirent.h>
bbefb8dd 31#include <glib.h>
65102a8c
MD
32#include <unistd.h>
33#include <stdlib.h>
34
65102a8c
MD
35#include "metadata/ctf-scanner.h"
36#include "metadata/ctf-parser.h"
37#include "metadata/ctf-ast.h"
38
0f980a35
MD
39/*
40 * We currently simply map a page to read the packet header and packet
8c572eba 41 * context to get the packet length and content length. (in bits)
0f980a35 42 */
8c572eba
MD
43#define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
44#define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
0f980a35
MD
45#define UUID_LEN 16 /* uuid by value len */
46
65102a8c 47extern int yydebug;
bbefb8dd 48
bbefb8dd
MD
49struct trace_descriptor *ctf_open_trace(const char *path, int flags);
50void ctf_close_trace(struct trace_descriptor *descriptor);
fc93b2bd 51
1ae19169
MD
52static
53rw_dispatch read_dispatch_table[] = {
d11e9c49
MD
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
1ae19169
MD
64static
65rw_dispatch write_dispatch_table[] = {
d11e9c49
MD
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
1ae19169 76static
d11e9c49 77struct format ctf_format = {
bbefb8dd
MD
78 .open_trace = ctf_open_trace,
79 .close_trace = ctf_close_trace,
fc93b2bd
MD
80};
81
8563e754 82void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
8c572eba
MD
83{
84 pos->fd = fd;
85 pos->mmap_offset = 0;
86 pos->packet_size = 0;
87 pos->content_size = 0;
88 pos->content_size_loc = NULL;
89 pos->base = NULL;
90 pos->offset = 0;
91 pos->dummy = false;
8c572eba 92 pos->cur_index = 0;
8563e754
MD
93 if (fd >= 0)
94 pos->packet_index = g_array_new(FALSE, TRUE,
95 sizeof(struct packet_index));
96 else
97 pos->packet_index = NULL;
8563e754
MD
98 switch (open_flags & O_ACCMODE) {
99 case O_RDONLY:
100 pos->prot = PROT_READ;
101 pos->flags = MAP_PRIVATE;
102 pos->parent.rw_table = read_dispatch_table;
103 break;
104 case O_WRONLY:
105 case O_RDWR:
106 pos->prot = PROT_WRITE; /* Write has priority */
107 pos->flags = MAP_SHARED;
108 pos->parent.rw_table = write_dispatch_table;
109 if (fd >= 0)
847bf71a 110 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
8563e754
MD
111 break;
112 default:
113 assert(0);
8c572eba
MD
114 }
115}
116
46322b33 117void ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba
MD
118{
119 int ret;
120
121 if (pos->prot == PROT_WRITE && pos->content_size_loc)
122 *pos->content_size_loc = pos->offset;
123 if (pos->base) {
124 /* unmap old base */
125 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
126 if (ret) {
46322b33 127 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
8c572eba
MD
128 strerror(errno));
129 assert(0);
130 }
131 }
132 (void) g_array_free(pos->packet_index, TRUE);
133}
134
847bf71a 135void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
0f980a35
MD
136{
137 int ret;
8c572eba
MD
138 off_t off;
139 struct packet_index *index;
0f980a35 140
847bf71a
MD
141 /* Only allow random seek in read mode */
142 assert(pos->prot != PROT_WRITE || whence == SEEK_CUR);
8c572eba
MD
143
144 if (pos->prot == PROT_WRITE && pos->content_size_loc)
145 *pos->content_size_loc = pos->offset;
0f980a35
MD
146
147 if (pos->base) {
148 /* unmap old base */
8c572eba 149 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 150 if (ret) {
46322b33 151 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
152 strerror(errno));
153 assert(0);
154 }
847bf71a 155 pos->base = NULL;
0f980a35
MD
156 }
157
8c572eba 158 /*
46322b33 159 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
160 * except to get exactly at the beginning of the next packet.
161 */
162 if (pos->prot == PROT_WRITE) {
163 /* The writer will add padding */
164 assert(pos->offset + offset == pos->packet_size);
165
166 /*
167 * Don't increment for initial stream move (only condition where
168 * pos->offset can be 0.
169 */
170 if (pos->offset)
171 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
172 pos->content_size = -1U; /* Unknown at this point */
173 pos->packet_size = WRITE_PACKET_LEN;
174 off = posix_fallocate(pos->fd, pos->mmap_offset, pos->packet_size / CHAR_BIT);
175 assert(off >= 0);
847bf71a 176 pos->offset = 0;
8c572eba 177 } else {
847bf71a
MD
178 switch (whence) {
179 case SEEK_CUR:
180 /* The reader will expect us to skip padding */
181 assert(pos->offset + offset == pos->content_size);
8c572eba 182 ++pos->cur_index;
847bf71a
MD
183 break;
184 case SEEK_SET:
185 assert(offset == 0); /* only seek supported for now */
186 pos->cur_index = 0;
187 break;
188 default:
189 assert(0);
190 }
191 if (pos->cur_index >= pos->packet_index->len) {
670977d3 192 pos->offset = EOF;
847bf71a
MD
193 return;
194 }
8c572eba
MD
195 index = &g_array_index(pos->packet_index, struct packet_index,
196 pos->cur_index);
197 pos->mmap_offset = index->offset;
198
199 /* Lookup context/packet size in index */
200 pos->content_size = index->content_size;
201 pos->packet_size = index->packet_size;
847bf71a 202 pos->offset = index->data_offset;
8c572eba 203 }
0f980a35 204 /* map new base. Need mapping length from header. */
8c572eba
MD
205 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
206 pos->flags, pos->fd, pos->mmap_offset);
847bf71a
MD
207 if (pos->base == MAP_FAILED) {
208 fprintf(stdout, "[error] mmap error %s.\n",
209 strerror(errno));
210 assert(0);
211 }
0f980a35
MD
212}
213
65102a8c
MD
214/*
215 * TODO: for now, we treat the metadata file as a simple text file
216 * (without any header nor packets nor padding).
217 */
218static
46322b33 219int ctf_open_trace_metadata_read(struct ctf_trace *td)
65102a8c
MD
220{
221 struct ctf_scanner *scanner;
222 FILE *fp;
223 int ret = 0;
224
46322b33
MD
225 td->metadata.pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
226 if (td->metadata.pos.fd < 0) {
65102a8c 227 fprintf(stdout, "Unable to open metadata.\n");
46322b33 228 return td->metadata.pos.fd;
65102a8c
MD
229 }
230
231 if (babeltrace_debug)
232 yydebug = 1;
233
46322b33 234 fp = fdopen(td->metadata.pos.fd, "r");
65102a8c 235 if (!fp) {
46322b33 236 fprintf(stdout, "[error] Unable to open metadata stream.\n");
65102a8c
MD
237 ret = -errno;
238 goto end_stream;
239 }
240
241 scanner = ctf_scanner_alloc(fp);
242 if (!scanner) {
46322b33 243 fprintf(stdout, "[error] Error allocating scanner\n");
65102a8c
MD
244 ret = -ENOMEM;
245 goto end_scanner_alloc;
246 }
247 ret = ctf_scanner_append_ast(scanner);
248 if (ret) {
46322b33 249 fprintf(stdout, "[error] Error creating AST\n");
65102a8c
MD
250 goto end;
251 }
252
253 if (babeltrace_debug) {
254 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
255 if (ret) {
46322b33 256 fprintf(stdout, "[error] Error visiting AST for XML output\n");
65102a8c
MD
257 goto end;
258 }
259 }
260
261 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
262 if (ret) {
46322b33 263 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
264 goto end;
265 }
266 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
46322b33 267 td, BYTE_ORDER);
65102a8c 268 if (ret) {
46322b33 269 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
270 goto end;
271 }
272end:
273 ctf_scanner_free(scanner);
274end_scanner_alloc:
275 fclose(fp);
276end_stream:
46322b33 277 close(td->metadata.pos.fd);
0f980a35
MD
278 return ret;
279}
280
281
282static
46322b33 283int create_stream_packet_index(struct ctf_trace *td,
0f980a35
MD
284 struct ctf_file_stream *file_stream)
285{
286 struct ctf_stream *stream;
287 int len_index;
46322b33 288 struct ctf_stream_pos *pos;
0f980a35
MD
289 struct stat filestats;
290 struct packet_index packet_index;
291 int first_packet = 1;
292 int ret;
293
294 pos = &file_stream->pos;
295
296 ret = fstat(pos->fd, &filestats);
297 if (ret < 0)
298 return ret;
299
300 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
301 uint64_t stream_id = 0;
302
303 if (pos->base) {
304 /* unmap old base */
8c572eba 305 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 306 if (ret) {
46322b33 307 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
308 strerror(errno));
309 return ret;
310 }
8c572eba 311 pos->base = NULL;
0f980a35
MD
312 }
313 /* map new base. Need mapping length from header. */
8c572eba 314 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
0f980a35 315 MAP_PRIVATE, pos->fd, pos->mmap_offset);
dc48ecad
MD
316 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
317 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
0f980a35
MD
318 pos->offset = 0; /* Position of the packet header */
319
8c572eba
MD
320 packet_index.offset = pos->mmap_offset;
321 packet_index.content_size = 0;
322 packet_index.packet_size = 0;
323
0f980a35 324 /* read and check header, set stream id (and check) */
46322b33 325 if (td->packet_header) {
0f980a35 326 /* Read packet header */
c5e74408
MD
327 ret = generic_rw(&pos->parent, &td->packet_header->p);
328 if (ret)
329 return ret;
46322b33 330 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic"));
0f980a35
MD
331 if (len_index >= 0) {
332 struct definition_integer *defint;
333 struct field *field;
334
46322b33 335 field = struct_definition_get_field_from_index(td->packet_header, len_index);
0f980a35
MD
336 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
337 defint = container_of(field->definition, struct definition_integer, p);
338 assert(defint->declaration->signedness == FALSE);
339 if (defint->value._unsigned != CTF_MAGIC) {
d8ea2d29 340 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
8c572eba
MD
341 defint->value._unsigned,
342 file_stream->pos.packet_index->len,
343 (ssize_t) pos->mmap_offset);
0f980a35
MD
344 return -EINVAL;
345 }
346 }
347
348 /* check uuid */
46322b33 349 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("trace_uuid"));
0f980a35
MD
350 if (len_index >= 0) {
351 struct definition_array *defarray;
352 struct field *field;
353 uint64_t i;
354 uint8_t uuidval[UUID_LEN];
355
46322b33 356 field = struct_definition_get_field_from_index(td->packet_header, len_index);
0f980a35
MD
357 assert(field->definition->declaration->id == CTF_TYPE_ARRAY);
358 defarray = container_of(field->definition, struct definition_array, p);
3838df27 359 assert(array_len(defarray) == UUID_LEN);
0f980a35
MD
360 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
361
362 for (i = 0; i < UUID_LEN; i++) {
363 struct definition *elem;
364 struct definition_integer *defint;
365
366 elem = array_index(defarray, i);
367 assert(elem);
368 defint = container_of(elem, struct definition_integer, p);
369 uuidval[i] = defint->value._unsigned;
370 }
46322b33 371 ret = uuid_compare(td->uuid, uuidval);
0f980a35
MD
372 if (ret) {
373 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
374 return -EINVAL;
375 }
376 }
377
378
46322b33 379 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("stream_id"));
0f980a35
MD
380 if (len_index >= 0) {
381 struct definition_integer *defint;
382 struct field *field;
383
46322b33 384 field = struct_definition_get_field_from_index(td->packet_header, len_index);
0f980a35
MD
385 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
386 defint = container_of(field->definition, struct definition_integer, p);
387 assert(defint->declaration->signedness == FALSE);
388 stream_id = defint->value._unsigned;
389 }
390 }
391
392 if (!first_packet && file_stream->stream_id != stream_id) {
393 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
394 return -EINVAL;
395 }
396 if (first_packet) {
397 file_stream->stream_id = stream_id;
46322b33 398 if (stream_id >= td->streams->len) {
0f980a35
MD
399 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
400 return -EINVAL;
401 }
46322b33 402 stream = g_ptr_array_index(td->streams, stream_id);
0f980a35
MD
403 if (!stream) {
404 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
405 return -EINVAL;
406 }
407 file_stream->stream = stream;
408 }
409 first_packet = 0;
410
dc48ecad
MD
411 if (stream->packet_context) {
412 /* Read packet context */
c5e74408
MD
413 ret = generic_rw(&pos->parent, &stream->packet_context->p);
414 if (ret)
415 return ret;
dc48ecad
MD
416 /* read content size from header */
417 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size"));
418 if (len_index >= 0) {
419 struct definition_integer *defint;
420 struct field *field;
421
422 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
423 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
424 defint = container_of(field->definition, struct definition_integer, p);
425 assert(defint->declaration->signedness == FALSE);
8c572eba 426 packet_index.content_size = defint->value._unsigned;
dc48ecad
MD
427 } else {
428 /* Use file size for packet size */
8c572eba 429 packet_index.content_size = filestats.st_size * CHAR_BIT;
dc48ecad
MD
430 }
431
432 /* read packet size from header */
433 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size"));
434 if (len_index >= 0) {
435 struct definition_integer *defint;
436 struct field *field;
437
438 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
439 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
440 defint = container_of(field->definition, struct definition_integer, p);
441 assert(defint->declaration->signedness == FALSE);
8c572eba 442 packet_index.packet_size = defint->value._unsigned;
dc48ecad
MD
443 } else {
444 /* Use content size if non-zero, else file size */
8c572eba 445 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
dc48ecad 446 }
0f980a35
MD
447 } else {
448 /* Use file size for packet size */
8c572eba 449 packet_index.content_size = filestats.st_size * CHAR_BIT;
0f980a35 450 /* Use content size if non-zero, else file size */
8c572eba 451 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
0f980a35 452 }
546293fa
MD
453
454 /* Validate content size and packet size values */
455 if (packet_index.content_size > packet_index.packet_size) {
456 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
457 packet_index.content_size, packet_index.packet_size);
458 return -EINVAL;
459 }
460
58b0b883
MD
461 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
462 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
463 packet_index.content_size, (filestats.st_size - packet_index.offset) * CHAR_BIT);
546293fa
MD
464 return -EINVAL;
465 }
466
847bf71a
MD
467 /* Save position after header and context */
468 packet_index.data_offset = pos->offset;
0f980a35 469
0f980a35
MD
470 /* add index to packet array */
471 g_array_append_val(file_stream->pos.packet_index, packet_index);
472
8c572eba 473 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
0f980a35
MD
474 }
475
847bf71a
MD
476 /* Move pos back to beginning of file */
477 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
478
0f980a35
MD
479 return 0;
480}
481
482/*
483 * Note: many file streams can inherit from the same stream class
484 * description (metadata).
485 */
486static
46322b33 487int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
0f980a35
MD
488{
489 int ret;
490 struct ctf_file_stream *file_stream;
491
46322b33 492 ret = openat(td->dirfd, path, flags);
0f980a35
MD
493 if (ret < 0)
494 goto error;
495 file_stream = g_new0(struct ctf_file_stream, 1);
8563e754 496 ctf_init_pos(&file_stream->pos, ret, flags);
0f980a35
MD
497 ret = create_stream_packet_index(td, file_stream);
498 if (ret)
499 goto error_index;
500 /* Add stream file to stream class */
501 g_ptr_array_add(file_stream->stream->files, file_stream);
502 return 0;
503
504error_index:
46322b33 505 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
506 close(file_stream->pos.fd);
507 g_free(file_stream);
508error:
65102a8c
MD
509 return ret;
510}
511
bbefb8dd 512static
46322b33 513int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
bbefb8dd
MD
514{
515 int ret;
65102a8c
MD
516 struct dirent *dirent;
517 struct dirent *diriter;
518 size_t dirent_len;
bbefb8dd 519
46322b33 520 td->flags = flags;
bbefb8dd
MD
521
522 /* Open trace directory */
46322b33
MD
523 td->dir = opendir(path);
524 if (!td->dir) {
dc48ecad 525 fprintf(stdout, "[error] Unable to open trace directory.\n");
bbefb8dd
MD
526 ret = -ENOENT;
527 goto error;
528 }
529
46322b33
MD
530 td->dirfd = open(path, 0);
531 if (td->dirfd < 0) {
dc48ecad 532 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
65102a8c
MD
533 ret = -ENOENT;
534 goto error_dirfd;
535 }
0f980a35 536
46322b33 537 td->streams = g_ptr_array_new();
0f980a35 538
65102a8c
MD
539 /*
540 * Keep the metadata file separate.
541 */
bbefb8dd 542
65102a8c
MD
543 ret = ctf_open_trace_metadata_read(td);
544 if (ret) {
545 goto error_metadata;
546 }
bbefb8dd
MD
547
548 /*
549 * Open each stream: for each file, try to open, check magic
550 * number, and get the stream ID to add to the right location in
551 * the stream array.
bbefb8dd
MD
552 */
553
65102a8c 554 dirent_len = offsetof(struct dirent, d_name) +
46322b33 555 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 556
65102a8c 557 dirent = malloc(dirent_len);
bbefb8dd 558
65102a8c 559 for (;;) {
46322b33 560 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 561 if (ret) {
dc48ecad 562 fprintf(stdout, "[error] Readdir error.\n");
65102a8c 563 goto readdir_error;
65102a8c
MD
564 }
565 if (!diriter)
566 break;
d8ea2d29
MD
567 /* Ignore hidden files, ., .. and metadata. */
568 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
569 || !strcmp(diriter->d_name, "..")
570 || !strcmp(diriter->d_name, "metadata"))
571 continue;
dc48ecad
MD
572 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
573 if (ret) {
574 fprintf(stdout, "[error] Open file stream error.\n");
575 goto readdir_error;
576 }
65102a8c 577 }
bbefb8dd 578
65102a8c 579 free(dirent);
bbefb8dd 580 return 0;
65102a8c
MD
581
582readdir_error:
583 free(dirent);
584error_metadata:
46322b33
MD
585 g_ptr_array_free(td->streams, TRUE);
586 close(td->dirfd);
65102a8c 587error_dirfd:
46322b33 588 closedir(td->dir);
bbefb8dd
MD
589error:
590 return ret;
591}
592
bbefb8dd
MD
593struct trace_descriptor *ctf_open_trace(const char *path, int flags)
594{
46322b33 595 struct ctf_trace *td;
bbefb8dd
MD
596 int ret;
597
46322b33 598 td = g_new0(struct ctf_trace, 1);
bbefb8dd 599
8c572eba 600 switch (flags & O_ACCMODE) {
bbefb8dd 601 case O_RDONLY:
b61922b5
MD
602 if (!path) {
603 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
604 goto error;
605 }
bbefb8dd
MD
606 ret = ctf_open_trace_read(td, path, flags);
607 if (ret)
608 goto error;
609 break;
610 case O_WRONLY:
46322b33
MD
611 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
612 goto error;
bbefb8dd 613 default:
46322b33 614 fprintf(stdout, "[error] Incorrect open flags.\n");
bbefb8dd
MD
615 goto error;
616 }
617
46322b33 618 return &td->parent;
bbefb8dd
MD
619error:
620 g_free(td);
621 return NULL;
622}
623
0f980a35
MD
624static
625void ctf_close_file_stream(struct ctf_file_stream *file_stream)
626{
46322b33 627 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
628 close(file_stream->pos.fd);
629}
630
46322b33 631void ctf_close_trace(struct trace_descriptor *tdp)
bbefb8dd 632{
46322b33 633 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
0f980a35
MD
634 int i;
635
46322b33
MD
636 if (td->streams) {
637 for (i = 0; i < td->streams->len; i++) {
0f980a35
MD
638 struct ctf_stream *stream;
639 int j;
46322b33 640 stream = g_ptr_array_index(td->streams, i);
0f980a35
MD
641 for (j = 0; j < stream->files->len; j++) {
642 struct ctf_file_stream *file_stream;
2c117823 643 file_stream = g_ptr_array_index(stream->files, j);
0f980a35
MD
644 ctf_close_file_stream(file_stream);
645 }
646
647 }
46322b33 648 g_ptr_array_free(td->streams, TRUE);
0f980a35 649 }
46322b33 650 closedir(td->dir);
bbefb8dd
MD
651 g_free(td);
652}
653
7fb21036 654void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
655{
656 int ret;
657
4c8bfb7e 658 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
659 ret = bt_register_format(&ctf_format);
660 assert(!ret);
661}
698f0fe4
MD
662
663/* TODO: finalize */
This page took 0.055681 seconds and 4 git commands to generate.