Use inheritance for trace descriptor and positions
[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
4c8bfb7e 52static struct format ctf_format = {
fc93b2bd
MD
53 .uint_read = ctf_uint_read,
54 .int_read = ctf_int_read,
55 .uint_write = ctf_uint_write,
56 .int_write = ctf_int_write,
0a46062b
MD
57 .double_read = ctf_double_read,
58 .double_write = ctf_double_write,
dc48ecad
MD
59 .ldouble_read = ctf_ldouble_read,
60 .ldouble_write = ctf_ldouble_write,
fc93b2bd
MD
61 .float_copy = ctf_float_copy,
62 .string_copy = ctf_string_copy,
a52d7f6a
MD
63 .string_read = ctf_string_read,
64 .string_write = ctf_string_write,
65 .string_free_temp = ctf_string_free_temp,
448d3cc7
MD
66 .enum_read = ctf_enum_read,
67 .enum_write = ctf_enum_write,
11796b96
MD
68 .struct_begin = ctf_struct_begin,
69 .struct_end = ctf_struct_end,
c054553d
MD
70 .variant_begin = ctf_variant_begin,
71 .variant_end = ctf_variant_end,
d06d03db
MD
72 .array_begin = ctf_array_begin,
73 .array_end = ctf_array_end,
74 .sequence_begin = ctf_sequence_begin,
75 .sequence_end = ctf_sequence_end,
bbefb8dd
MD
76 .open_trace = ctf_open_trace,
77 .close_trace = ctf_close_trace,
fc93b2bd
MD
78};
79
46322b33 80void ctf_init_pos(struct ctf_stream_pos *pos, int fd)
8c572eba
MD
81{
82 pos->fd = fd;
83 pos->mmap_offset = 0;
84 pos->packet_size = 0;
85 pos->content_size = 0;
86 pos->content_size_loc = NULL;
87 pos->base = NULL;
88 pos->offset = 0;
89 pos->dummy = false;
90 pos->packet_index = g_array_new(FALSE, TRUE,
91 sizeof(struct packet_index));
92 pos->cur_index = 0;
93 if (fd >= 0) {
94 int flags = fcntl(fd, F_GETFL, 0);
95
96 switch (flags & O_ACCMODE) {
97 case O_RDONLY:
98 pos->prot = PROT_READ;
99 pos->flags = MAP_PRIVATE;
100 break;
101 case O_WRONLY:
102 case O_RDWR:
103 pos->prot = PROT_WRITE; /* Write has priority */
104 pos->flags = MAP_SHARED;
46322b33 105 ctf_move_pos_slow(pos, 0); /* position for write */
8c572eba
MD
106 break;
107 default:
108 assert(0);
109 }
110
111 }
112}
113
46322b33 114void ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba
MD
115{
116 int ret;
117
118 if (pos->prot == PROT_WRITE && pos->content_size_loc)
119 *pos->content_size_loc = pos->offset;
120 if (pos->base) {
121 /* unmap old base */
122 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
123 if (ret) {
46322b33 124 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
8c572eba
MD
125 strerror(errno));
126 assert(0);
127 }
128 }
129 (void) g_array_free(pos->packet_index, TRUE);
130}
131
46322b33 132void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset)
0f980a35
MD
133{
134 int ret;
8c572eba
MD
135 off_t off;
136 struct packet_index *index;
0f980a35 137
8c572eba
MD
138
139 if (pos->prot == PROT_WRITE && pos->content_size_loc)
140 *pos->content_size_loc = pos->offset;
0f980a35
MD
141
142 if (pos->base) {
143 /* unmap old base */
8c572eba 144 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 145 if (ret) {
46322b33 146 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
147 strerror(errno));
148 assert(0);
149 }
150 }
151
8c572eba 152 /*
46322b33 153 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
154 * except to get exactly at the beginning of the next packet.
155 */
156 if (pos->prot == PROT_WRITE) {
157 /* The writer will add padding */
158 assert(pos->offset + offset == pos->packet_size);
159
160 /*
161 * Don't increment for initial stream move (only condition where
162 * pos->offset can be 0.
163 */
164 if (pos->offset)
165 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
166 pos->content_size = -1U; /* Unknown at this point */
167 pos->packet_size = WRITE_PACKET_LEN;
168 off = posix_fallocate(pos->fd, pos->mmap_offset, pos->packet_size / CHAR_BIT);
169 assert(off >= 0);
170 } else {
171 /* The reader will expect us to skip padding */
172 assert(pos->offset + offset == pos->content_size);
173
174 /*
175 * Don't increment for initial stream move (only condition where
176 * pos->offset can be 0).
177 */
178 if (pos->offset)
179 ++pos->cur_index;
180 index = &g_array_index(pos->packet_index, struct packet_index,
181 pos->cur_index);
182 pos->mmap_offset = index->offset;
183
184 /* Lookup context/packet size in index */
185 pos->content_size = index->content_size;
186 pos->packet_size = index->packet_size;
187 }
0f980a35 188 /* map new base. Need mapping length from header. */
8c572eba
MD
189 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
190 pos->flags, pos->fd, pos->mmap_offset);
191 pos->offset = 0;
0f980a35
MD
192}
193
65102a8c
MD
194/*
195 * TODO: for now, we treat the metadata file as a simple text file
196 * (without any header nor packets nor padding).
197 */
198static
46322b33 199int ctf_open_trace_metadata_read(struct ctf_trace *td)
65102a8c
MD
200{
201 struct ctf_scanner *scanner;
202 FILE *fp;
203 int ret = 0;
204
46322b33
MD
205 td->metadata.pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
206 if (td->metadata.pos.fd < 0) {
65102a8c 207 fprintf(stdout, "Unable to open metadata.\n");
46322b33 208 return td->metadata.pos.fd;
65102a8c
MD
209 }
210
211 if (babeltrace_debug)
212 yydebug = 1;
213
46322b33 214 fp = fdopen(td->metadata.pos.fd, "r");
65102a8c 215 if (!fp) {
46322b33 216 fprintf(stdout, "[error] Unable to open metadata stream.\n");
65102a8c
MD
217 ret = -errno;
218 goto end_stream;
219 }
220
221 scanner = ctf_scanner_alloc(fp);
222 if (!scanner) {
46322b33 223 fprintf(stdout, "[error] Error allocating scanner\n");
65102a8c
MD
224 ret = -ENOMEM;
225 goto end_scanner_alloc;
226 }
227 ret = ctf_scanner_append_ast(scanner);
228 if (ret) {
46322b33 229 fprintf(stdout, "[error] Error creating AST\n");
65102a8c
MD
230 goto end;
231 }
232
233 if (babeltrace_debug) {
234 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
235 if (ret) {
46322b33 236 fprintf(stdout, "[error] Error visiting AST for XML output\n");
65102a8c
MD
237 goto end;
238 }
239 }
240
241 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
242 if (ret) {
46322b33 243 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
244 goto end;
245 }
246 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
46322b33 247 td, BYTE_ORDER);
65102a8c 248 if (ret) {
46322b33 249 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
250 goto end;
251 }
252end:
253 ctf_scanner_free(scanner);
254end_scanner_alloc:
255 fclose(fp);
256end_stream:
46322b33 257 close(td->metadata.pos.fd);
0f980a35
MD
258 return ret;
259}
260
261
262static
46322b33 263int create_stream_packet_index(struct ctf_trace *td,
0f980a35
MD
264 struct ctf_file_stream *file_stream)
265{
266 struct ctf_stream *stream;
267 int len_index;
46322b33 268 struct ctf_stream_pos *pos;
0f980a35
MD
269 struct stat filestats;
270 struct packet_index packet_index;
271 int first_packet = 1;
272 int ret;
273
274 pos = &file_stream->pos;
275
276 ret = fstat(pos->fd, &filestats);
277 if (ret < 0)
278 return ret;
279
280 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
281 uint64_t stream_id = 0;
282
283 if (pos->base) {
284 /* unmap old base */
8c572eba 285 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 286 if (ret) {
46322b33 287 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
288 strerror(errno));
289 return ret;
290 }
8c572eba 291 pos->base = NULL;
0f980a35
MD
292 }
293 /* map new base. Need mapping length from header. */
8c572eba 294 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
0f980a35 295 MAP_PRIVATE, pos->fd, pos->mmap_offset);
dc48ecad
MD
296 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
297 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
0f980a35
MD
298 pos->offset = 0; /* Position of the packet header */
299
8c572eba
MD
300 packet_index.offset = pos->mmap_offset;
301 packet_index.content_size = 0;
302 packet_index.packet_size = 0;
303
0f980a35 304 /* read and check header, set stream id (and check) */
46322b33 305 if (td->packet_header) {
0f980a35 306 /* Read packet header */
46322b33
MD
307 td->packet_header->p.declaration->copy(NULL, NULL,
308 &pos->parent, &ctf_format, &td->packet_header->p);
0f980a35 309
46322b33 310 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic"));
0f980a35
MD
311 if (len_index >= 0) {
312 struct definition_integer *defint;
313 struct field *field;
314
46322b33 315 field = struct_definition_get_field_from_index(td->packet_header, len_index);
0f980a35
MD
316 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
317 defint = container_of(field->definition, struct definition_integer, p);
318 assert(defint->declaration->signedness == FALSE);
319 if (defint->value._unsigned != CTF_MAGIC) {
8c572eba
MD
320 fprintf(stdout, "[error] Invalid magic number %" PRIX64 " at packet %u (file offset %zd).\n",
321 defint->value._unsigned,
322 file_stream->pos.packet_index->len,
323 (ssize_t) pos->mmap_offset);
0f980a35
MD
324 return -EINVAL;
325 }
326 }
327
328 /* check uuid */
46322b33 329 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("trace_uuid"));
0f980a35
MD
330 if (len_index >= 0) {
331 struct definition_array *defarray;
332 struct field *field;
333 uint64_t i;
334 uint8_t uuidval[UUID_LEN];
335
46322b33 336 field = struct_definition_get_field_from_index(td->packet_header, len_index);
0f980a35
MD
337 assert(field->definition->declaration->id == CTF_TYPE_ARRAY);
338 defarray = container_of(field->definition, struct definition_array, p);
3838df27 339 assert(array_len(defarray) == UUID_LEN);
0f980a35
MD
340 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
341
342 for (i = 0; i < UUID_LEN; i++) {
343 struct definition *elem;
344 struct definition_integer *defint;
345
346 elem = array_index(defarray, i);
347 assert(elem);
348 defint = container_of(elem, struct definition_integer, p);
349 uuidval[i] = defint->value._unsigned;
350 }
46322b33 351 ret = uuid_compare(td->uuid, uuidval);
0f980a35
MD
352 if (ret) {
353 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
354 return -EINVAL;
355 }
356 }
357
358
46322b33 359 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("stream_id"));
0f980a35
MD
360 if (len_index >= 0) {
361 struct definition_integer *defint;
362 struct field *field;
363
46322b33 364 field = struct_definition_get_field_from_index(td->packet_header, len_index);
0f980a35
MD
365 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
366 defint = container_of(field->definition, struct definition_integer, p);
367 assert(defint->declaration->signedness == FALSE);
368 stream_id = defint->value._unsigned;
369 }
370 }
371
372 if (!first_packet && file_stream->stream_id != stream_id) {
373 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
374 return -EINVAL;
375 }
376 if (first_packet) {
377 file_stream->stream_id = stream_id;
46322b33 378 if (stream_id >= td->streams->len) {
0f980a35
MD
379 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
380 return -EINVAL;
381 }
46322b33 382 stream = g_ptr_array_index(td->streams, stream_id);
0f980a35
MD
383 if (!stream) {
384 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
385 return -EINVAL;
386 }
387 file_stream->stream = stream;
388 }
389 first_packet = 0;
390
dc48ecad
MD
391 if (stream->packet_context) {
392 /* Read packet context */
393 stream->packet_context->p.declaration->copy(NULL, NULL,
46322b33 394 &pos->parent, &ctf_format, &stream->packet_context->p);
dc48ecad
MD
395
396 /* read content size from header */
397 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size"));
398 if (len_index >= 0) {
399 struct definition_integer *defint;
400 struct field *field;
401
402 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
403 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
404 defint = container_of(field->definition, struct definition_integer, p);
405 assert(defint->declaration->signedness == FALSE);
8c572eba 406 packet_index.content_size = defint->value._unsigned;
dc48ecad
MD
407 } else {
408 /* Use file size for packet size */
8c572eba 409 packet_index.content_size = filestats.st_size * CHAR_BIT;
dc48ecad
MD
410 }
411
412 /* read packet size from header */
413 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size"));
414 if (len_index >= 0) {
415 struct definition_integer *defint;
416 struct field *field;
417
418 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
419 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
420 defint = container_of(field->definition, struct definition_integer, p);
421 assert(defint->declaration->signedness == FALSE);
8c572eba 422 packet_index.packet_size = defint->value._unsigned;
dc48ecad
MD
423 } else {
424 /* Use content size if non-zero, else file size */
8c572eba 425 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
dc48ecad 426 }
0f980a35
MD
427 } else {
428 /* Use file size for packet size */
8c572eba 429 packet_index.content_size = filestats.st_size * CHAR_BIT;
0f980a35 430 /* Use content size if non-zero, else file size */
8c572eba 431 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
0f980a35
MD
432 }
433
0f980a35
MD
434 /* add index to packet array */
435 g_array_append_val(file_stream->pos.packet_index, packet_index);
436
8c572eba 437 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
0f980a35
MD
438 }
439
440 return 0;
441}
442
443/*
444 * Note: many file streams can inherit from the same stream class
445 * description (metadata).
446 */
447static
46322b33 448int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
0f980a35
MD
449{
450 int ret;
451 struct ctf_file_stream *file_stream;
452
46322b33 453 ret = openat(td->dirfd, path, flags);
0f980a35
MD
454 if (ret < 0)
455 goto error;
456 file_stream = g_new0(struct ctf_file_stream, 1);
46322b33 457 ctf_init_pos(&file_stream->pos, ret);
0f980a35
MD
458 ret = create_stream_packet_index(td, file_stream);
459 if (ret)
460 goto error_index;
461 /* Add stream file to stream class */
462 g_ptr_array_add(file_stream->stream->files, file_stream);
463 return 0;
464
465error_index:
46322b33 466 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
467 close(file_stream->pos.fd);
468 g_free(file_stream);
469error:
65102a8c
MD
470 return ret;
471}
472
bbefb8dd 473static
46322b33 474int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
bbefb8dd
MD
475{
476 int ret;
65102a8c
MD
477 struct dirent *dirent;
478 struct dirent *diriter;
479 size_t dirent_len;
bbefb8dd 480
46322b33 481 td->flags = flags;
bbefb8dd
MD
482
483 /* Open trace directory */
46322b33
MD
484 td->dir = opendir(path);
485 if (!td->dir) {
dc48ecad 486 fprintf(stdout, "[error] Unable to open trace directory.\n");
bbefb8dd
MD
487 ret = -ENOENT;
488 goto error;
489 }
490
46322b33
MD
491 td->dirfd = open(path, 0);
492 if (td->dirfd < 0) {
dc48ecad 493 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
65102a8c
MD
494 ret = -ENOENT;
495 goto error_dirfd;
496 }
0f980a35 497
46322b33 498 td->streams = g_ptr_array_new();
0f980a35 499
65102a8c
MD
500 /*
501 * Keep the metadata file separate.
502 */
bbefb8dd 503
65102a8c
MD
504 ret = ctf_open_trace_metadata_read(td);
505 if (ret) {
506 goto error_metadata;
507 }
bbefb8dd
MD
508
509 /*
510 * Open each stream: for each file, try to open, check magic
511 * number, and get the stream ID to add to the right location in
512 * the stream array.
bbefb8dd
MD
513 */
514
65102a8c 515 dirent_len = offsetof(struct dirent, d_name) +
46322b33 516 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 517
65102a8c 518 dirent = malloc(dirent_len);
bbefb8dd 519
65102a8c 520 for (;;) {
46322b33 521 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 522 if (ret) {
dc48ecad 523 fprintf(stdout, "[error] Readdir error.\n");
65102a8c 524 goto readdir_error;
65102a8c
MD
525 }
526 if (!diriter)
527 break;
528 if (!strcmp(diriter->d_name, ".")
529 || !strcmp(diriter->d_name, "..")
530 || !strcmp(diriter->d_name, "metadata"))
531 continue;
dc48ecad
MD
532 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
533 if (ret) {
534 fprintf(stdout, "[error] Open file stream error.\n");
535 goto readdir_error;
536 }
65102a8c 537 }
bbefb8dd 538
65102a8c 539 free(dirent);
bbefb8dd 540 return 0;
65102a8c
MD
541
542readdir_error:
543 free(dirent);
544error_metadata:
46322b33
MD
545 g_ptr_array_free(td->streams, TRUE);
546 close(td->dirfd);
65102a8c 547error_dirfd:
46322b33 548 closedir(td->dir);
bbefb8dd
MD
549error:
550 return ret;
551}
552
553static
46322b33 554int ctf_open_trace_write(struct ctf_trace *td, const char *path, int flags)
bbefb8dd
MD
555{
556 int ret;
557
558 ret = mkdir(path, S_IRWXU|S_IRWXG);
559 if (ret)
560 return ret;
561
65102a8c 562 /* Open trace directory */
46322b33
MD
563 td->dir = opendir(path);
564 if (!td->dir) {
565 fprintf(stdout, "[error] Unable to open trace directory.\n");
65102a8c
MD
566 ret = -ENOENT;
567 goto error;
568 }
569
570
bbefb8dd 571 return 0;
65102a8c
MD
572
573error:
574 return ret;
bbefb8dd
MD
575}
576
577struct trace_descriptor *ctf_open_trace(const char *path, int flags)
578{
46322b33 579 struct ctf_trace *td;
bbefb8dd
MD
580 int ret;
581
46322b33 582 td = g_new0(struct ctf_trace, 1);
bbefb8dd 583
8c572eba 584 switch (flags & O_ACCMODE) {
bbefb8dd
MD
585 case O_RDONLY:
586 ret = ctf_open_trace_read(td, path, flags);
587 if (ret)
588 goto error;
589 break;
590 case O_WRONLY:
46322b33
MD
591 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
592 goto error;
593#if 0
bbefb8dd
MD
594 ret = ctf_open_trace_write(td, path, flags);
595 if (ret)
596 goto error;
46322b33 597#endif //0
bbefb8dd
MD
598 break;
599 default:
46322b33 600 fprintf(stdout, "[error] Incorrect open flags.\n");
bbefb8dd
MD
601 goto error;
602 }
603
46322b33 604 return &td->parent;
bbefb8dd
MD
605error:
606 g_free(td);
607 return NULL;
608}
609
0f980a35
MD
610static
611void ctf_close_file_stream(struct ctf_file_stream *file_stream)
612{
46322b33 613 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
614 close(file_stream->pos.fd);
615}
616
46322b33 617void ctf_close_trace(struct trace_descriptor *tdp)
bbefb8dd 618{
46322b33 619 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
0f980a35
MD
620 int i;
621
46322b33
MD
622 if (td->streams) {
623 for (i = 0; i < td->streams->len; i++) {
0f980a35
MD
624 struct ctf_stream *stream;
625 int j;
626
46322b33 627 stream = g_ptr_array_index(td->streams, i);
0f980a35
MD
628 for (j = 0; j < stream->files->len; j++) {
629 struct ctf_file_stream *file_stream;
46322b33 630 file_stream = g_ptr_array_index(td->streams, j);
0f980a35
MD
631 ctf_close_file_stream(file_stream);
632 }
633
634 }
46322b33 635 g_ptr_array_free(td->streams, TRUE);
0f980a35 636 }
46322b33 637 closedir(td->dir);
bbefb8dd
MD
638 g_free(td);
639}
640
7fb21036 641void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
642{
643 int ret;
644
4c8bfb7e 645 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
646 ret = bt_register_format(&ctf_format);
647 assert(!ret);
648}
698f0fe4
MD
649
650/* TODO: finalize */
This page took 0.052677 seconds and 4 git commands to generate.