text conversion works
[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) {
192 pos->offset = -EOF;
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 */
d11e9c49 327 generic_rw(&pos->parent, &td->packet_header->p);
0f980a35 328
46322b33 329 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic"));
0f980a35
MD
330 if (len_index >= 0) {
331 struct definition_integer *defint;
332 struct field *field;
333
46322b33 334 field = struct_definition_get_field_from_index(td->packet_header, len_index);
0f980a35
MD
335 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
336 defint = container_of(field->definition, struct definition_integer, p);
337 assert(defint->declaration->signedness == FALSE);
338 if (defint->value._unsigned != CTF_MAGIC) {
8c572eba
MD
339 fprintf(stdout, "[error] Invalid magic number %" PRIX64 " at packet %u (file offset %zd).\n",
340 defint->value._unsigned,
341 file_stream->pos.packet_index->len,
342 (ssize_t) pos->mmap_offset);
0f980a35
MD
343 return -EINVAL;
344 }
345 }
346
347 /* check uuid */
46322b33 348 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("trace_uuid"));
0f980a35
MD
349 if (len_index >= 0) {
350 struct definition_array *defarray;
351 struct field *field;
352 uint64_t i;
353 uint8_t uuidval[UUID_LEN];
354
46322b33 355 field = struct_definition_get_field_from_index(td->packet_header, len_index);
0f980a35
MD
356 assert(field->definition->declaration->id == CTF_TYPE_ARRAY);
357 defarray = container_of(field->definition, struct definition_array, p);
3838df27 358 assert(array_len(defarray) == UUID_LEN);
0f980a35
MD
359 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
360
361 for (i = 0; i < UUID_LEN; i++) {
362 struct definition *elem;
363 struct definition_integer *defint;
364
365 elem = array_index(defarray, i);
366 assert(elem);
367 defint = container_of(elem, struct definition_integer, p);
368 uuidval[i] = defint->value._unsigned;
369 }
46322b33 370 ret = uuid_compare(td->uuid, uuidval);
0f980a35
MD
371 if (ret) {
372 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
373 return -EINVAL;
374 }
375 }
376
377
46322b33 378 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("stream_id"));
0f980a35
MD
379 if (len_index >= 0) {
380 struct definition_integer *defint;
381 struct field *field;
382
46322b33 383 field = struct_definition_get_field_from_index(td->packet_header, len_index);
0f980a35
MD
384 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
385 defint = container_of(field->definition, struct definition_integer, p);
386 assert(defint->declaration->signedness == FALSE);
387 stream_id = defint->value._unsigned;
388 }
389 }
390
391 if (!first_packet && file_stream->stream_id != stream_id) {
392 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
393 return -EINVAL;
394 }
395 if (first_packet) {
396 file_stream->stream_id = stream_id;
46322b33 397 if (stream_id >= td->streams->len) {
0f980a35
MD
398 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
399 return -EINVAL;
400 }
46322b33 401 stream = g_ptr_array_index(td->streams, stream_id);
0f980a35
MD
402 if (!stream) {
403 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
404 return -EINVAL;
405 }
406 file_stream->stream = stream;
407 }
408 first_packet = 0;
409
dc48ecad
MD
410 if (stream->packet_context) {
411 /* Read packet context */
d11e9c49 412 generic_rw(&pos->parent, &stream->packet_context->p);
dc48ecad
MD
413
414 /* read content size from header */
415 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size"));
416 if (len_index >= 0) {
417 struct definition_integer *defint;
418 struct field *field;
419
420 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
421 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
422 defint = container_of(field->definition, struct definition_integer, p);
423 assert(defint->declaration->signedness == FALSE);
8c572eba 424 packet_index.content_size = defint->value._unsigned;
dc48ecad
MD
425 } else {
426 /* Use file size for packet size */
8c572eba 427 packet_index.content_size = filestats.st_size * CHAR_BIT;
dc48ecad
MD
428 }
429
430 /* read packet size from header */
431 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size"));
432 if (len_index >= 0) {
433 struct definition_integer *defint;
434 struct field *field;
435
436 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
437 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
438 defint = container_of(field->definition, struct definition_integer, p);
439 assert(defint->declaration->signedness == FALSE);
8c572eba 440 packet_index.packet_size = defint->value._unsigned;
dc48ecad
MD
441 } else {
442 /* Use content size if non-zero, else file size */
8c572eba 443 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
dc48ecad 444 }
0f980a35
MD
445 } else {
446 /* Use file size for packet size */
8c572eba 447 packet_index.content_size = filestats.st_size * CHAR_BIT;
0f980a35 448 /* Use content size if non-zero, else file size */
8c572eba 449 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
0f980a35 450 }
847bf71a
MD
451 /* Save position after header and context */
452 packet_index.data_offset = pos->offset;
0f980a35 453
0f980a35
MD
454 /* add index to packet array */
455 g_array_append_val(file_stream->pos.packet_index, packet_index);
456
8c572eba 457 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
0f980a35
MD
458 }
459
847bf71a
MD
460 /* Move pos back to beginning of file */
461 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
462
0f980a35
MD
463 return 0;
464}
465
466/*
467 * Note: many file streams can inherit from the same stream class
468 * description (metadata).
469 */
470static
46322b33 471int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
0f980a35
MD
472{
473 int ret;
474 struct ctf_file_stream *file_stream;
475
46322b33 476 ret = openat(td->dirfd, path, flags);
0f980a35
MD
477 if (ret < 0)
478 goto error;
479 file_stream = g_new0(struct ctf_file_stream, 1);
8563e754 480 ctf_init_pos(&file_stream->pos, ret, flags);
0f980a35
MD
481 ret = create_stream_packet_index(td, file_stream);
482 if (ret)
483 goto error_index;
484 /* Add stream file to stream class */
485 g_ptr_array_add(file_stream->stream->files, file_stream);
486 return 0;
487
488error_index:
46322b33 489 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
490 close(file_stream->pos.fd);
491 g_free(file_stream);
492error:
65102a8c
MD
493 return ret;
494}
495
bbefb8dd 496static
46322b33 497int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
bbefb8dd
MD
498{
499 int ret;
65102a8c
MD
500 struct dirent *dirent;
501 struct dirent *diriter;
502 size_t dirent_len;
bbefb8dd 503
46322b33 504 td->flags = flags;
bbefb8dd
MD
505
506 /* Open trace directory */
46322b33
MD
507 td->dir = opendir(path);
508 if (!td->dir) {
dc48ecad 509 fprintf(stdout, "[error] Unable to open trace directory.\n");
bbefb8dd
MD
510 ret = -ENOENT;
511 goto error;
512 }
513
46322b33
MD
514 td->dirfd = open(path, 0);
515 if (td->dirfd < 0) {
dc48ecad 516 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
65102a8c
MD
517 ret = -ENOENT;
518 goto error_dirfd;
519 }
0f980a35 520
46322b33 521 td->streams = g_ptr_array_new();
0f980a35 522
65102a8c
MD
523 /*
524 * Keep the metadata file separate.
525 */
bbefb8dd 526
65102a8c
MD
527 ret = ctf_open_trace_metadata_read(td);
528 if (ret) {
529 goto error_metadata;
530 }
bbefb8dd
MD
531
532 /*
533 * Open each stream: for each file, try to open, check magic
534 * number, and get the stream ID to add to the right location in
535 * the stream array.
bbefb8dd
MD
536 */
537
65102a8c 538 dirent_len = offsetof(struct dirent, d_name) +
46322b33 539 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 540
65102a8c 541 dirent = malloc(dirent_len);
bbefb8dd 542
65102a8c 543 for (;;) {
46322b33 544 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 545 if (ret) {
dc48ecad 546 fprintf(stdout, "[error] Readdir error.\n");
65102a8c 547 goto readdir_error;
65102a8c
MD
548 }
549 if (!diriter)
550 break;
551 if (!strcmp(diriter->d_name, ".")
552 || !strcmp(diriter->d_name, "..")
553 || !strcmp(diriter->d_name, "metadata"))
554 continue;
dc48ecad
MD
555 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
556 if (ret) {
557 fprintf(stdout, "[error] Open file stream error.\n");
558 goto readdir_error;
559 }
65102a8c 560 }
bbefb8dd 561
65102a8c 562 free(dirent);
bbefb8dd 563 return 0;
65102a8c
MD
564
565readdir_error:
566 free(dirent);
567error_metadata:
46322b33
MD
568 g_ptr_array_free(td->streams, TRUE);
569 close(td->dirfd);
65102a8c 570error_dirfd:
46322b33 571 closedir(td->dir);
bbefb8dd
MD
572error:
573 return ret;
574}
575
576static
46322b33 577int ctf_open_trace_write(struct ctf_trace *td, const char *path, int flags)
bbefb8dd
MD
578{
579 int ret;
580
581 ret = mkdir(path, S_IRWXU|S_IRWXG);
582 if (ret)
583 return ret;
584
65102a8c 585 /* Open trace directory */
46322b33
MD
586 td->dir = opendir(path);
587 if (!td->dir) {
588 fprintf(stdout, "[error] Unable to open trace directory.\n");
65102a8c
MD
589 ret = -ENOENT;
590 goto error;
591 }
592
593
bbefb8dd 594 return 0;
65102a8c
MD
595
596error:
597 return ret;
bbefb8dd
MD
598}
599
600struct trace_descriptor *ctf_open_trace(const char *path, int flags)
601{
46322b33 602 struct ctf_trace *td;
bbefb8dd
MD
603 int ret;
604
46322b33 605 td = g_new0(struct ctf_trace, 1);
bbefb8dd 606
8c572eba 607 switch (flags & O_ACCMODE) {
bbefb8dd
MD
608 case O_RDONLY:
609 ret = ctf_open_trace_read(td, path, flags);
610 if (ret)
611 goto error;
612 break;
613 case O_WRONLY:
46322b33
MD
614 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
615 goto error;
616#if 0
bbefb8dd
MD
617 ret = ctf_open_trace_write(td, path, flags);
618 if (ret)
619 goto error;
46322b33 620#endif //0
bbefb8dd
MD
621 break;
622 default:
46322b33 623 fprintf(stdout, "[error] Incorrect open flags.\n");
bbefb8dd
MD
624 goto error;
625 }
626
46322b33 627 return &td->parent;
bbefb8dd
MD
628error:
629 g_free(td);
630 return NULL;
631}
632
0f980a35
MD
633static
634void ctf_close_file_stream(struct ctf_file_stream *file_stream)
635{
46322b33 636 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
637 close(file_stream->pos.fd);
638}
639
46322b33 640void ctf_close_trace(struct trace_descriptor *tdp)
bbefb8dd 641{
46322b33 642 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
0f980a35
MD
643 int i;
644
46322b33
MD
645 if (td->streams) {
646 for (i = 0; i < td->streams->len; i++) {
0f980a35
MD
647 struct ctf_stream *stream;
648 int j;
46322b33 649 stream = g_ptr_array_index(td->streams, i);
0f980a35
MD
650 for (j = 0; j < stream->files->len; j++) {
651 struct ctf_file_stream *file_stream;
2c117823 652 file_stream = g_ptr_array_index(stream->files, j);
0f980a35
MD
653 ctf_close_file_stream(file_stream);
654 }
655
656 }
46322b33 657 g_ptr_array_free(td->streams, TRUE);
0f980a35 658 }
46322b33 659 closedir(td->dir);
bbefb8dd
MD
660 g_free(td);
661}
662
7fb21036 663void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
664{
665 int ret;
666
4c8bfb7e 667 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
668 ret = bt_register_format(&ctf_format);
669 assert(!ret);
670}
698f0fe4
MD
671
672/* TODO: finalize */
This page took 0.054782 seconds and 4 git commands to generate.