move struct ctf_stream -> struct ctf_stream_class
[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
31262354 82static
aa6bffae 83int ctf_read_event(struct stream_pos *ppos, struct ctf_stream_class *stream_class)
31262354
MD
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
148error:
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
153static
aa6bffae 154int ctf_write_event(struct stream_pos *pos, struct ctf_stream_class *stream_class)
31262354
MD
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
215error:
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
8563e754 220void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
8c572eba
MD
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;
8c572eba 230 pos->cur_index = 0;
8563e754
MD
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;
8563e754
MD
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;
31262354 241 pos->parent.event_cb = ctf_read_event;
8563e754 242 break;
8563e754
MD
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;
31262354 247 pos->parent.event_cb = ctf_write_event;
8563e754 248 if (fd >= 0)
847bf71a 249 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
8563e754
MD
250 break;
251 default:
252 assert(0);
8c572eba
MD
253 }
254}
255
46322b33 256void ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba
MD
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) {
46322b33 266 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
8c572eba
MD
267 strerror(errno));
268 assert(0);
269 }
270 }
271 (void) g_array_free(pos->packet_index, TRUE);
272}
273
847bf71a 274void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
0f980a35
MD
275{
276 int ret;
8c572eba
MD
277 off_t off;
278 struct packet_index *index;
0f980a35 279
8c572eba
MD
280 if (pos->prot == PROT_WRITE && pos->content_size_loc)
281 *pos->content_size_loc = pos->offset;
0f980a35
MD
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 assert(0);
290 }
847bf71a 291 pos->base = NULL;
0f980a35
MD
292 }
293
8c572eba 294 /*
46322b33 295 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
296 * except to get exactly at the beginning of the next packet.
297 */
298 if (pos->prot == PROT_WRITE) {
989c73bc
MD
299 switch (whence) {
300 case SEEK_CUR:
301 /* The writer will add padding */
302 assert(pos->offset + offset == pos->packet_size);
8c572eba 303 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
989c73bc
MD
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 }
8c572eba
MD
312 pos->content_size = -1U; /* Unknown at this point */
313 pos->packet_size = WRITE_PACKET_LEN;
989c73bc
MD
314 off = posix_fallocate(pos->fd, pos->mmap_offset,
315 pos->packet_size / CHAR_BIT);
8c572eba 316 assert(off >= 0);
847bf71a 317 pos->offset = 0;
8c572eba 318 } else {
847bf71a
MD
319 switch (whence) {
320 case SEEK_CUR:
321 /* The reader will expect us to skip padding */
322 assert(pos->offset + offset == pos->content_size);
8c572eba 323 ++pos->cur_index;
847bf71a
MD
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) {
670977d3 333 pos->offset = EOF;
847bf71a
MD
334 return;
335 }
8c572eba
MD
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;
847bf71a 343 pos->offset = index->data_offset;
8c572eba 344 }
0f980a35 345 /* map new base. Need mapping length from header. */
8c572eba
MD
346 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
347 pos->flags, pos->fd, pos->mmap_offset);
847bf71a
MD
348 if (pos->base == MAP_FAILED) {
349 fprintf(stdout, "[error] mmap error %s.\n",
350 strerror(errno));
351 assert(0);
352 }
0f980a35
MD
353}
354
65102a8c
MD
355/*
356 * TODO: for now, we treat the metadata file as a simple text file
357 * (without any header nor packets nor padding).
358 */
359static
46322b33 360int ctf_open_trace_metadata_read(struct ctf_trace *td)
65102a8c
MD
361{
362 struct ctf_scanner *scanner;
363 FILE *fp;
364 int ret = 0;
365
46322b33
MD
366 td->metadata.pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
367 if (td->metadata.pos.fd < 0) {
65102a8c 368 fprintf(stdout, "Unable to open metadata.\n");
46322b33 369 return td->metadata.pos.fd;
65102a8c
MD
370 }
371
372 if (babeltrace_debug)
373 yydebug = 1;
374
46322b33 375 fp = fdopen(td->metadata.pos.fd, "r");
65102a8c 376 if (!fp) {
46322b33 377 fprintf(stdout, "[error] Unable to open metadata stream.\n");
65102a8c
MD
378 ret = -errno;
379 goto end_stream;
380 }
381
382 scanner = ctf_scanner_alloc(fp);
383 if (!scanner) {
46322b33 384 fprintf(stdout, "[error] Error allocating scanner\n");
65102a8c
MD
385 ret = -ENOMEM;
386 goto end_scanner_alloc;
387 }
388 ret = ctf_scanner_append_ast(scanner);
389 if (ret) {
46322b33 390 fprintf(stdout, "[error] Error creating AST\n");
65102a8c
MD
391 goto end;
392 }
393
394 if (babeltrace_debug) {
395 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
396 if (ret) {
46322b33 397 fprintf(stdout, "[error] Error visiting AST for XML output\n");
65102a8c
MD
398 goto end;
399 }
400 }
401
402 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
403 if (ret) {
46322b33 404 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
405 goto end;
406 }
407 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
46322b33 408 td, BYTE_ORDER);
65102a8c 409 if (ret) {
46322b33 410 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
411 goto end;
412 }
413end:
414 ctf_scanner_free(scanner);
415end_scanner_alloc:
416 fclose(fp);
417end_stream:
46322b33 418 close(td->metadata.pos.fd);
0f980a35
MD
419 return ret;
420}
421
422
423static
46322b33 424int create_stream_packet_index(struct ctf_trace *td,
0f980a35
MD
425 struct ctf_file_stream *file_stream)
426{
aa6bffae 427 struct ctf_stream_class *stream;
0f980a35 428 int len_index;
46322b33 429 struct ctf_stream_pos *pos;
0f980a35
MD
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 */
8c572eba 446 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 447 if (ret) {
46322b33 448 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
449 strerror(errno));
450 return ret;
451 }
8c572eba 452 pos->base = NULL;
0f980a35
MD
453 }
454 /* map new base. Need mapping length from header. */
8c572eba 455 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
0f980a35 456 MAP_PRIVATE, pos->fd, pos->mmap_offset);
dc48ecad
MD
457 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
458 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
0f980a35
MD
459 pos->offset = 0; /* Position of the packet header */
460
8c572eba
MD
461 packet_index.offset = pos->mmap_offset;
462 packet_index.content_size = 0;
463 packet_index.packet_size = 0;
464
0f980a35 465 /* read and check header, set stream id (and check) */
46322b33 466 if (td->packet_header) {
0f980a35 467 /* Read packet header */
c5e74408
MD
468 ret = generic_rw(&pos->parent, &td->packet_header->p);
469 if (ret)
470 return ret;
46322b33 471 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic"));
0f980a35
MD
472 if (len_index >= 0) {
473 struct definition_integer *defint;
b1a2f580 474 struct definition *field;
0f980a35 475
46322b33 476 field = struct_definition_get_field_from_index(td->packet_header, len_index);
b1a2f580
MD
477 assert(field->declaration->id == CTF_TYPE_INTEGER);
478 defint = container_of(field, struct definition_integer, p);
0f980a35
MD
479 assert(defint->declaration->signedness == FALSE);
480 if (defint->value._unsigned != CTF_MAGIC) {
d8ea2d29 481 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
8c572eba
MD
482 defint->value._unsigned,
483 file_stream->pos.packet_index->len,
484 (ssize_t) pos->mmap_offset);
0f980a35
MD
485 return -EINVAL;
486 }
487 }
488
489 /* check uuid */
46322b33 490 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("trace_uuid"));
0f980a35
MD
491 if (len_index >= 0) {
492 struct definition_array *defarray;
b1a2f580 493 struct definition *field;
0f980a35
MD
494 uint64_t i;
495 uint8_t uuidval[UUID_LEN];
496
46322b33 497 field = struct_definition_get_field_from_index(td->packet_header, len_index);
b1a2f580
MD
498 assert(field->declaration->id == CTF_TYPE_ARRAY);
499 defarray = container_of(field, struct definition_array, p);
3838df27 500 assert(array_len(defarray) == UUID_LEN);
0f980a35
MD
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 }
46322b33 512 ret = uuid_compare(td->uuid, uuidval);
0f980a35
MD
513 if (ret) {
514 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
515 return -EINVAL;
516 }
517 }
518
519
46322b33 520 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("stream_id"));
0f980a35
MD
521 if (len_index >= 0) {
522 struct definition_integer *defint;
b1a2f580 523 struct definition *field;
0f980a35 524
46322b33 525 field = struct_definition_get_field_from_index(td->packet_header, len_index);
b1a2f580
MD
526 assert(field->declaration->id == CTF_TYPE_INTEGER);
527 defint = container_of(field, struct definition_integer, p);
0f980a35
MD
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;
46322b33 539 if (stream_id >= td->streams->len) {
0f980a35
MD
540 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
541 return -EINVAL;
542 }
46322b33 543 stream = g_ptr_array_index(td->streams, stream_id);
0f980a35
MD
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
dc48ecad
MD
552 if (stream->packet_context) {
553 /* Read packet context */
c5e74408
MD
554 ret = generic_rw(&pos->parent, &stream->packet_context->p);
555 if (ret)
556 return ret;
dc48ecad
MD
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;
b1a2f580 561 struct definition *field;
dc48ecad
MD
562
563 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
b1a2f580
MD
564 assert(field->declaration->id == CTF_TYPE_INTEGER);
565 defint = container_of(field, struct definition_integer, p);
dc48ecad 566 assert(defint->declaration->signedness == FALSE);
8c572eba 567 packet_index.content_size = defint->value._unsigned;
dc48ecad
MD
568 } else {
569 /* Use file size for packet size */
8c572eba 570 packet_index.content_size = filestats.st_size * CHAR_BIT;
dc48ecad
MD
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;
b1a2f580 577 struct definition *field;
dc48ecad
MD
578
579 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
b1a2f580
MD
580 assert(field->declaration->id == CTF_TYPE_INTEGER);
581 defint = container_of(field, struct definition_integer, p);
dc48ecad 582 assert(defint->declaration->signedness == FALSE);
8c572eba 583 packet_index.packet_size = defint->value._unsigned;
dc48ecad
MD
584 } else {
585 /* Use content size if non-zero, else file size */
8c572eba 586 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
dc48ecad 587 }
0f980a35
MD
588 } else {
589 /* Use file size for packet size */
8c572eba 590 packet_index.content_size = filestats.st_size * CHAR_BIT;
0f980a35 591 /* Use content size if non-zero, else file size */
8c572eba 592 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
0f980a35 593 }
546293fa
MD
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
58b0b883
MD
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);
546293fa
MD
605 return -EINVAL;
606 }
607
847bf71a
MD
608 /* Save position after header and context */
609 packet_index.data_offset = pos->offset;
0f980a35 610
0f980a35
MD
611 /* add index to packet array */
612 g_array_append_val(file_stream->pos.packet_index, packet_index);
613
8c572eba 614 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
0f980a35
MD
615 }
616
847bf71a
MD
617 /* Move pos back to beginning of file */
618 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
619
0f980a35
MD
620 return 0;
621}
622
623/*
624 * Note: many file streams can inherit from the same stream class
625 * description (metadata).
626 */
627static
46322b33 628int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
0f980a35
MD
629{
630 int ret;
631 struct ctf_file_stream *file_stream;
632
46322b33 633 ret = openat(td->dirfd, path, flags);
0f980a35
MD
634 if (ret < 0)
635 goto error;
636 file_stream = g_new0(struct ctf_file_stream, 1);
8563e754 637 ctf_init_pos(&file_stream->pos, ret, flags);
0f980a35
MD
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
645error_index:
46322b33 646 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
647 close(file_stream->pos.fd);
648 g_free(file_stream);
649error:
65102a8c
MD
650 return ret;
651}
652
bbefb8dd 653static
46322b33 654int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
bbefb8dd
MD
655{
656 int ret;
65102a8c
MD
657 struct dirent *dirent;
658 struct dirent *diriter;
659 size_t dirent_len;
bbefb8dd 660
46322b33 661 td->flags = flags;
bbefb8dd
MD
662
663 /* Open trace directory */
46322b33
MD
664 td->dir = opendir(path);
665 if (!td->dir) {
dc48ecad 666 fprintf(stdout, "[error] Unable to open trace directory.\n");
bbefb8dd
MD
667 ret = -ENOENT;
668 goto error;
669 }
670
46322b33
MD
671 td->dirfd = open(path, 0);
672 if (td->dirfd < 0) {
dc48ecad 673 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
65102a8c
MD
674 ret = -ENOENT;
675 goto error_dirfd;
676 }
0f980a35 677
46322b33 678 td->streams = g_ptr_array_new();
0f980a35 679
65102a8c
MD
680 /*
681 * Keep the metadata file separate.
682 */
bbefb8dd 683
65102a8c
MD
684 ret = ctf_open_trace_metadata_read(td);
685 if (ret) {
686 goto error_metadata;
687 }
bbefb8dd
MD
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.
bbefb8dd
MD
693 */
694
65102a8c 695 dirent_len = offsetof(struct dirent, d_name) +
46322b33 696 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 697
65102a8c 698 dirent = malloc(dirent_len);
bbefb8dd 699
65102a8c 700 for (;;) {
46322b33 701 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 702 if (ret) {
dc48ecad 703 fprintf(stdout, "[error] Readdir error.\n");
65102a8c 704 goto readdir_error;
65102a8c
MD
705 }
706 if (!diriter)
707 break;
d8ea2d29
MD
708 /* Ignore hidden files, ., .. and metadata. */
709 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
710 || !strcmp(diriter->d_name, "..")
711 || !strcmp(diriter->d_name, "metadata"))
712 continue;
dc48ecad
MD
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 }
65102a8c 718 }
bbefb8dd 719
65102a8c 720 free(dirent);
bbefb8dd 721 return 0;
65102a8c
MD
722
723readdir_error:
724 free(dirent);
725error_metadata:
46322b33
MD
726 g_ptr_array_free(td->streams, TRUE);
727 close(td->dirfd);
65102a8c 728error_dirfd:
46322b33 729 closedir(td->dir);
bbefb8dd
MD
730error:
731 return ret;
732}
733
bbefb8dd
MD
734struct trace_descriptor *ctf_open_trace(const char *path, int flags)
735{
46322b33 736 struct ctf_trace *td;
bbefb8dd
MD
737 int ret;
738
46322b33 739 td = g_new0(struct ctf_trace, 1);
bbefb8dd 740
8c572eba 741 switch (flags & O_ACCMODE) {
bbefb8dd 742 case O_RDONLY:
b61922b5
MD
743 if (!path) {
744 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
745 goto error;
746 }
bbefb8dd
MD
747 ret = ctf_open_trace_read(td, path, flags);
748 if (ret)
749 goto error;
750 break;
989c73bc 751 case O_RDWR:
46322b33
MD
752 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
753 goto error;
bbefb8dd 754 default:
46322b33 755 fprintf(stdout, "[error] Incorrect open flags.\n");
bbefb8dd
MD
756 goto error;
757 }
758
46322b33 759 return &td->parent;
bbefb8dd
MD
760error:
761 g_free(td);
762 return NULL;
763}
764
0f980a35
MD
765static
766void ctf_close_file_stream(struct ctf_file_stream *file_stream)
767{
46322b33 768 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
769 close(file_stream->pos.fd);
770}
771
46322b33 772void ctf_close_trace(struct trace_descriptor *tdp)
bbefb8dd 773{
46322b33 774 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
0f980a35
MD
775 int i;
776
46322b33
MD
777 if (td->streams) {
778 for (i = 0; i < td->streams->len; i++) {
aa6bffae 779 struct ctf_stream_class *stream;
0f980a35 780 int j;
46322b33 781 stream = g_ptr_array_index(td->streams, i);
0f980a35
MD
782 for (j = 0; j < stream->files->len; j++) {
783 struct ctf_file_stream *file_stream;
2c117823 784 file_stream = g_ptr_array_index(stream->files, j);
0f980a35
MD
785 ctf_close_file_stream(file_stream);
786 }
787
788 }
46322b33 789 g_ptr_array_free(td->streams, TRUE);
0f980a35 790 }
46322b33 791 closedir(td->dir);
bbefb8dd
MD
792 g_free(td);
793}
794
7fb21036 795void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
796{
797 int ret;
798
4c8bfb7e 799 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
800 ret = bt_register_format(&ctf_format);
801 assert(!ret);
802}
698f0fe4
MD
803
804/* TODO: finalize */
This page took 0.099257 seconds and 4 git commands to generate.