Move definitions to per-file structure
[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 23#include <inttypes.h>
b4c19c1e 24#include <stdio.h>
0f980a35
MD
25#include <uuid/uuid.h>
26#include <sys/mman.h>
bbefb8dd 27#include <errno.h>
b4c19c1e 28#include <endian.h>
bbefb8dd 29#include <sys/types.h>
65102a8c 30#include <sys/stat.h>
bbefb8dd 31#include <fcntl.h>
65102a8c 32#include <dirent.h>
bbefb8dd 33#include <glib.h>
65102a8c
MD
34#include <unistd.h>
35#include <stdlib.h>
36
65102a8c
MD
37#include "metadata/ctf-scanner.h"
38#include "metadata/ctf-parser.h"
39#include "metadata/ctf-ast.h"
40
0f980a35
MD
41/*
42 * We currently simply map a page to read the packet header and packet
8c572eba 43 * context to get the packet length and content length. (in bits)
0f980a35 44 */
8c572eba
MD
45#define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
46#define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
0f980a35
MD
47#define UUID_LEN 16 /* uuid by value len */
48
a0fe7d97
MD
49#ifndef min
50#define min(a, b) (((a) < (b)) ? (a) : (b))
51#endif
52
65102a8c 53extern int yydebug;
bbefb8dd 54
e9378815 55static
bbefb8dd 56struct trace_descriptor *ctf_open_trace(const char *path, int flags);
e9378815 57static
bbefb8dd 58void ctf_close_trace(struct trace_descriptor *descriptor);
fc93b2bd 59
1ae19169
MD
60static
61rw_dispatch read_dispatch_table[] = {
d11e9c49
MD
62 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
63 [ CTF_TYPE_FLOAT ] = ctf_float_read,
64 [ CTF_TYPE_ENUM ] = ctf_enum_read,
65 [ CTF_TYPE_STRING ] = ctf_string_read,
66 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
67 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
68 [ CTF_TYPE_ARRAY ] = ctf_array_read,
69 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
d11e9c49
MD
70};
71
1ae19169
MD
72static
73rw_dispatch write_dispatch_table[] = {
d11e9c49
MD
74 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
75 [ CTF_TYPE_FLOAT ] = ctf_float_write,
76 [ CTF_TYPE_ENUM ] = ctf_enum_write,
77 [ CTF_TYPE_STRING ] = ctf_string_write,
78 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
79 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
80 [ CTF_TYPE_ARRAY ] = ctf_array_write,
81 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
d11e9c49
MD
82};
83
1ae19169 84static
d11e9c49 85struct format ctf_format = {
bbefb8dd
MD
86 .open_trace = ctf_open_trace,
87 .close_trace = ctf_close_trace,
fc93b2bd
MD
88};
89
2b9a764d
MD
90static
91void ctf_update_timestamp(struct ctf_stream *stream,
92 struct definition_integer *integer_definition)
93{
94 struct declaration_integer *integer_declaration =
95 integer_definition->declaration;
96 uint64_t oldval, newval, updateval;
97
98 if (integer_declaration->len == 64) {
99 stream->timestamp = integer_definition->value._unsigned;
100 return;
101 }
102 /* keep low bits */
103 oldval = stream->timestamp;
104 oldval &= (1ULL << integer_declaration->len) - 1;
105 newval = integer_definition->value._unsigned;
106 /* Test for overflow by comparing low bits */
107 if (newval < oldval)
108 newval += 1ULL << integer_declaration->len;
109 /* updateval contains old high bits, and new low bits (sum) */
110 updateval = stream->timestamp;
111 updateval &= ~((1ULL << integer_declaration->len) - 1);
112 updateval += newval;
113 stream->timestamp = updateval;
114}
115
31262354 116static
764af3f4 117int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream)
31262354
MD
118{
119 struct ctf_stream_pos *pos =
120 container_of(ppos, struct ctf_stream_pos, parent);
764af3f4 121 struct ctf_stream_class *stream_class = stream->stream_class;
e28d4618 122 struct ctf_file_event *event;
31262354 123 uint64_t id = 0;
31262354
MD
124 int ret;
125
126 if (pos->offset == EOF)
127 return EOF;
128
129 /* Read event header */
e28d4618 130 if (stream->stream_event_header) {
a35173fe 131 struct definition_integer *integer_definition;
ccdb988e 132 struct definition *variant;
a35173fe 133
e28d4618 134 ret = generic_rw(ppos, &stream->stream_event_header->p);
31262354
MD
135 if (ret)
136 goto error;
137 /* lookup event id */
e28d4618 138 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
139 if (integer_definition) {
140 id = integer_definition->value._unsigned;
141 } else {
142 struct definition_enum *enum_definition;
143
e28d4618 144 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
145 if (enum_definition) {
146 id = enum_definition->integer->value._unsigned;
147 }
31262354 148 }
764af3f4 149
e28d4618 150 variant = lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e
MD
151 if (variant) {
152 integer_definition = lookup_integer(variant, "id", FALSE);
153 if (integer_definition) {
154 id = integer_definition->value._unsigned;
155 }
156 }
157
764af3f4 158 /* lookup timestamp */
e28d4618 159 integer_definition = lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
a35173fe 160 if (integer_definition) {
2b9a764d 161 ctf_update_timestamp(stream, integer_definition);
a35173fe 162 } else {
ccdb988e
MD
163 if (variant) {
164 integer_definition = lookup_integer(variant, "timestamp", FALSE);
a35173fe 165 if (integer_definition) {
2b9a764d 166 ctf_update_timestamp(stream, integer_definition);
a35173fe
MD
167 }
168 }
764af3f4 169 }
31262354
MD
170 }
171
172 /* Read stream-declared event context */
e28d4618
MD
173 if (stream->stream_event_context) {
174 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
175 if (ret)
176 goto error;
177 }
178
179 if (id >= stream_class->events_by_id->len) {
180 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
181 return -EINVAL;
182 }
e28d4618
MD
183 event = g_ptr_array_index(stream->events_by_id, id);
184 if (!event) {
31262354
MD
185 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
186 return -EINVAL;
187 }
188
189 /* Read event-declared event context */
e28d4618
MD
190 if (event->event_context) {
191 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
192 if (ret)
193 goto error;
194 }
195
196 /* Read event payload */
e28d4618
MD
197 if (event->event_fields) {
198 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
199 if (ret)
200 goto error;
201 }
202
203 return 0;
204
205error:
206 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
207 return ret;
208}
209
210static
764af3f4 211int ctf_write_event(struct stream_pos *pos, struct ctf_stream *stream)
31262354 212{
764af3f4 213 struct ctf_stream_class *stream_class = stream->stream_class;
e28d4618 214 struct ctf_file_event *event;
31262354 215 uint64_t id = 0;
31262354
MD
216 int ret;
217
218 /* print event header */
e28d4618 219 if (stream->stream_event_header) {
ccdb988e
MD
220 struct definition_integer *integer_definition;
221 struct definition *variant;
222
31262354 223 /* lookup event id */
e28d4618 224 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
ccdb988e
MD
225 if (integer_definition) {
226 id = integer_definition->value._unsigned;
227 } else {
228 struct definition_enum *enum_definition;
229
e28d4618 230 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
ccdb988e
MD
231 if (enum_definition) {
232 id = enum_definition->integer->value._unsigned;
233 }
234 }
235
e28d4618 236 variant = lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e
MD
237 if (variant) {
238 integer_definition = lookup_integer(variant, "id", FALSE);
239 if (integer_definition) {
240 id = integer_definition->value._unsigned;
241 }
31262354
MD
242 }
243
e28d4618 244 ret = generic_rw(pos, &stream->stream_event_header->p);
31262354
MD
245 if (ret)
246 goto error;
247 }
248
249 /* print stream-declared event context */
e28d4618
MD
250 if (stream->stream_event_context) {
251 ret = generic_rw(pos, &stream->stream_event_context->p);
31262354
MD
252 if (ret)
253 goto error;
254 }
255
256 if (id >= stream_class->events_by_id->len) {
257 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
258 return -EINVAL;
259 }
e28d4618
MD
260 event = g_ptr_array_index(stream->events_by_id, id);
261 if (!event) {
31262354
MD
262 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
263 return -EINVAL;
264 }
265
266 /* print event-declared event context */
e28d4618
MD
267 if (event->event_context) {
268 ret = generic_rw(pos, &event->event_context->p);
31262354
MD
269 if (ret)
270 goto error;
271 }
272
273 /* Read and print event payload */
e28d4618
MD
274 if (event->event_fields) {
275 ret = generic_rw(pos, &event->event_fields->p);
31262354
MD
276 if (ret)
277 goto error;
278 }
279
280 return 0;
281
282error:
283 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
284 return ret;
285}
286
8563e754 287void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
8c572eba
MD
288{
289 pos->fd = fd;
290 pos->mmap_offset = 0;
291 pos->packet_size = 0;
292 pos->content_size = 0;
293 pos->content_size_loc = NULL;
294 pos->base = NULL;
295 pos->offset = 0;
296 pos->dummy = false;
8c572eba 297 pos->cur_index = 0;
8563e754
MD
298 if (fd >= 0)
299 pos->packet_index = g_array_new(FALSE, TRUE,
300 sizeof(struct packet_index));
301 else
302 pos->packet_index = NULL;
8563e754
MD
303 switch (open_flags & O_ACCMODE) {
304 case O_RDONLY:
305 pos->prot = PROT_READ;
306 pos->flags = MAP_PRIVATE;
307 pos->parent.rw_table = read_dispatch_table;
31262354 308 pos->parent.event_cb = ctf_read_event;
8563e754 309 break;
8563e754
MD
310 case O_RDWR:
311 pos->prot = PROT_WRITE; /* Write has priority */
312 pos->flags = MAP_SHARED;
313 pos->parent.rw_table = write_dispatch_table;
31262354 314 pos->parent.event_cb = ctf_write_event;
8563e754 315 if (fd >= 0)
847bf71a 316 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
8563e754
MD
317 break;
318 default:
319 assert(0);
8c572eba
MD
320 }
321}
322
46322b33 323void ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba
MD
324{
325 int ret;
326
327 if (pos->prot == PROT_WRITE && pos->content_size_loc)
328 *pos->content_size_loc = pos->offset;
329 if (pos->base) {
330 /* unmap old base */
331 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
332 if (ret) {
46322b33 333 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
8c572eba
MD
334 strerror(errno));
335 assert(0);
336 }
337 }
338 (void) g_array_free(pos->packet_index, TRUE);
339}
340
847bf71a 341void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
0f980a35
MD
342{
343 int ret;
8c572eba
MD
344 off_t off;
345 struct packet_index *index;
0f980a35 346
8c572eba
MD
347 if (pos->prot == PROT_WRITE && pos->content_size_loc)
348 *pos->content_size_loc = pos->offset;
0f980a35
MD
349
350 if (pos->base) {
351 /* unmap old base */
8c572eba 352 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 353 if (ret) {
46322b33 354 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
355 strerror(errno));
356 assert(0);
357 }
847bf71a 358 pos->base = NULL;
0f980a35
MD
359 }
360
8c572eba 361 /*
46322b33 362 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
363 * except to get exactly at the beginning of the next packet.
364 */
365 if (pos->prot == PROT_WRITE) {
989c73bc
MD
366 switch (whence) {
367 case SEEK_CUR:
368 /* The writer will add padding */
369 assert(pos->offset + offset == pos->packet_size);
8c572eba 370 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
989c73bc
MD
371 break;
372 case SEEK_SET:
373 assert(offset == 0); /* only seek supported for now */
374 pos->cur_index = 0;
375 break;
376 default:
377 assert(0);
378 }
8c572eba
MD
379 pos->content_size = -1U; /* Unknown at this point */
380 pos->packet_size = WRITE_PACKET_LEN;
989c73bc
MD
381 off = posix_fallocate(pos->fd, pos->mmap_offset,
382 pos->packet_size / CHAR_BIT);
8c572eba 383 assert(off >= 0);
847bf71a 384 pos->offset = 0;
8c572eba 385 } else {
847bf71a
MD
386 switch (whence) {
387 case SEEK_CUR:
388 /* The reader will expect us to skip padding */
389 assert(pos->offset + offset == pos->content_size);
8c572eba 390 ++pos->cur_index;
847bf71a
MD
391 break;
392 case SEEK_SET:
393 assert(offset == 0); /* only seek supported for now */
394 pos->cur_index = 0;
395 break;
396 default:
397 assert(0);
398 }
399 if (pos->cur_index >= pos->packet_index->len) {
670977d3 400 pos->offset = EOF;
847bf71a
MD
401 return;
402 }
8c572eba
MD
403 index = &g_array_index(pos->packet_index, struct packet_index,
404 pos->cur_index);
405 pos->mmap_offset = index->offset;
406
407 /* Lookup context/packet size in index */
408 pos->content_size = index->content_size;
409 pos->packet_size = index->packet_size;
2b9a764d
MD
410 if (index->data_offset < index->content_size)
411 pos->offset = index->data_offset;
412 else {
413 pos->offset = EOF;
414 return;
415 }
8c572eba 416 }
0f980a35 417 /* map new base. Need mapping length from header. */
8c572eba
MD
418 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
419 pos->flags, pos->fd, pos->mmap_offset);
847bf71a
MD
420 if (pos->base == MAP_FAILED) {
421 fprintf(stdout, "[error] mmap error %s.\n",
422 strerror(errno));
423 assert(0);
424 }
0f980a35
MD
425}
426
b4c19c1e
MD
427static
428int packet_metadata(struct ctf_trace *td, FILE *fp)
429{
430 uint32_t magic;
431 size_t len;
432 int ret = 0;
433
434 len = fread(&magic, sizeof(magic), 1, fp);
a0fe7d97 435 if (len != 1) {
b4c19c1e
MD
436 goto end;
437 }
438 if (magic == TSDL_MAGIC) {
439 ret = 1;
440 td->byte_order = BYTE_ORDER;
441 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
442 ret = 1;
443 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
444 LITTLE_ENDIAN : BIG_ENDIAN;
445 }
a0fe7d97 446 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
447end:
448 rewind(fp);
449 return ret;
450}
451
452static
453int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
454 FILE *out)
455{
456 struct metadata_packet_header header;
a0fe7d97 457 size_t readlen, writelen, toread;
b4c19c1e
MD
458 char buf[4096];
459 int ret = 0;
460
a91a962e 461 readlen = fread(&header, header_sizeof(header), 1, in);
a0fe7d97 462 if (readlen < 1)
b4c19c1e
MD
463 return -EINVAL;
464
465 if (td->byte_order != BYTE_ORDER) {
466 header.magic = GUINT32_SWAP_LE_BE(header.magic);
467 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
468 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
469 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
470 }
471 if (header.checksum)
472 fprintf(stdout, "[warning] checksum verification not supported yet.\n");
473 if (header.compression_scheme) {
474 fprintf(stdout, "[error] compression (%u) not supported yet.\n",
475 header.compression_scheme);
476 return -EINVAL;
477 }
478 if (header.encryption_scheme) {
479 fprintf(stdout, "[error] encryption (%u) not supported yet.\n",
480 header.encryption_scheme);
481 return -EINVAL;
482 }
483 if (header.checksum_scheme) {
484 fprintf(stdout, "[error] checksum (%u) not supported yet.\n",
485 header.checksum_scheme);
486 return -EINVAL;
487 }
488 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
489 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
490 CTF_TRACE_SET_FIELD(td, uuid);
491 } else {
492 if (uuid_compare(header.uuid, td->uuid))
493 return -EINVAL;
494 }
495
a0fe7d97
MD
496 toread = header.content_size / CHAR_BIT;
497
498 for (;;) {
499 readlen = fread(buf, sizeof(char), min(sizeof(buf), toread), in);
b4c19c1e
MD
500 if (ferror(in)) {
501 ret = -EINVAL;
502 break;
503 }
4152822b
MD
504 if (babeltrace_debug) {
505 fprintf(stdout, "[debug] metadata packet read: %s\n",
506 buf);
507 }
508
b4c19c1e
MD
509 writelen = fwrite(buf, sizeof(char), readlen, out);
510 if (writelen < readlen) {
511 ret = -EIO;
512 break;
513 }
514 if (ferror(out)) {
515 ret = -EINVAL;
516 break;
517 }
a0fe7d97
MD
518 toread -= readlen;
519 if (!toread) {
7f4b5c4d 520 ret = 0; /* continue reading next packet */
a0fe7d97
MD
521 break;
522 }
b4c19c1e
MD
523 }
524 return ret;
525}
526
527static
528int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
529 char **buf)
530{
531 FILE *in, *out;
532 size_t size;
533 int ret;
534
535 in = *fp;
c4f5487e
MD
536 /*
537 * Using strlen on *buf instead of size of open_memstream
538 * because its size includes garbage at the end (after final
539 * \0). This is the allocated size, not the actual string size.
540 */
b4c19c1e
MD
541 out = open_memstream(buf, &size);
542 if (out == NULL)
543 return -errno;
544
545 for (;;) {
546 ret = ctf_open_trace_metadata_packet_read(td, in, out);
7f4b5c4d 547 if (ret) {
b4c19c1e 548 break;
7f4b5c4d
MD
549 }
550 if (feof(in)) {
551 ret = 0;
b4c19c1e
MD
552 break;
553 }
554 }
555 fclose(out); /* flush the buffer */
556 fclose(in);
557 /* open for reading */
c4f5487e 558 *fp = fmemopen(*buf, strlen(*buf), "rb");
b4c19c1e
MD
559 return 0;
560}
561
65102a8c 562static
46322b33 563int ctf_open_trace_metadata_read(struct ctf_trace *td)
65102a8c
MD
564{
565 struct ctf_scanner *scanner;
566 FILE *fp;
b4c19c1e 567 char *buf = NULL;
65102a8c
MD
568 int ret = 0;
569
46322b33
MD
570 td->metadata.pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
571 if (td->metadata.pos.fd < 0) {
65102a8c 572 fprintf(stdout, "Unable to open metadata.\n");
46322b33 573 return td->metadata.pos.fd;
65102a8c
MD
574 }
575
576 if (babeltrace_debug)
577 yydebug = 1;
578
46322b33 579 fp = fdopen(td->metadata.pos.fd, "r");
65102a8c 580 if (!fp) {
46322b33 581 fprintf(stdout, "[error] Unable to open metadata stream.\n");
65102a8c
MD
582 ret = -errno;
583 goto end_stream;
584 }
585
b4c19c1e
MD
586 if (packet_metadata(td, fp)) {
587 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
588 if (ret)
589 goto end_packet_read;
590 }
591
65102a8c
MD
592 scanner = ctf_scanner_alloc(fp);
593 if (!scanner) {
46322b33 594 fprintf(stdout, "[error] Error allocating scanner\n");
65102a8c
MD
595 ret = -ENOMEM;
596 goto end_scanner_alloc;
597 }
598 ret = ctf_scanner_append_ast(scanner);
599 if (ret) {
46322b33 600 fprintf(stdout, "[error] Error creating AST\n");
65102a8c
MD
601 goto end;
602 }
603
604 if (babeltrace_debug) {
605 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
606 if (ret) {
46322b33 607 fprintf(stdout, "[error] Error visiting AST for XML output\n");
65102a8c
MD
608 goto end;
609 }
610 }
611
612 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
613 if (ret) {
46322b33 614 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
615 goto end;
616 }
617 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
46322b33 618 td, BYTE_ORDER);
65102a8c 619 if (ret) {
46322b33 620 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
621 goto end;
622 }
623end:
624 ctf_scanner_free(scanner);
625end_scanner_alloc:
b4c19c1e 626end_packet_read:
65102a8c 627 fclose(fp);
b4c19c1e 628 free(buf);
65102a8c 629end_stream:
46322b33 630 close(td->metadata.pos.fd);
0f980a35
MD
631 return ret;
632}
633
e28d4618
MD
634static
635struct ctf_file_event *create_event_definitions(struct ctf_trace *td,
636 struct ctf_stream *stream,
637 struct ctf_event *event)
638{
639 struct ctf_file_event *file_event = g_new0(struct ctf_file_event, 1);
640
641 if (event->context_decl) {
642 struct definition *definition =
643 event->context_decl->p.definition_new(&event->context_decl->p,
644 stream->parent_def_scope, 0, 0, "event.context");
645 if (!definition) {
646 goto error;
647 }
648 file_event->event_context = container_of(definition,
649 struct definition_struct, p);
650 stream->parent_def_scope = file_event->event_context->p.scope;
651 }
652 if (event->fields_decl) {
653 struct definition *definition =
654 event->fields_decl->p.definition_new(&event->fields_decl->p,
655 stream->parent_def_scope, 0, 0, "event.fields");
656 if (!definition) {
657 goto error;
658 }
659 file_event->event_fields = container_of(definition,
660 struct definition_struct, p);
661 stream->parent_def_scope = file_event->event_fields->p.scope;
662 }
663 return file_event;
664
665error:
666 if (file_event->event_fields)
667 definition_unref(&file_event->event_fields->p);
668 if (file_event->event_context)
669 definition_unref(&file_event->event_context->p);
670 return NULL;
671}
672
673static
674int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
675{
676 struct ctf_stream_class *stream_class;
677 int ret;
678 int i;
679
680 if (stream->stream_definitions_created)
681 return 0;
682
683 stream_class = stream->stream_class;
684
685 if (stream_class->packet_context_decl) {
686 struct definition *definition =
687 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
688 stream->parent_def_scope, 0, 0, "stream.packet.context");
689 if (!definition) {
690 ret = -EINVAL;
691 goto error;
692 }
693 stream->stream_packet_context = container_of(definition,
694 struct definition_struct, p);
695 stream->parent_def_scope = stream->stream_packet_context->p.scope;
696 }
697 if (stream_class->event_header_decl) {
698 struct definition *definition =
699 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
700 stream->parent_def_scope, 0, 0, "stream.event.header");
701 if (!definition) {
702 ret = -EINVAL;
703 goto error;
704 }
705 stream->stream_event_header =
706 container_of(definition, struct definition_struct, p);
707 stream->parent_def_scope = stream->stream_event_header->p.scope;
708 }
709 if (stream_class->event_context_decl) {
710 struct definition *definition =
711 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
712 stream->parent_def_scope, 0, 0, "stream.event.context");
713 if (!definition) {
714 ret = -EINVAL;
715 goto error;
716 }
717 stream->stream_event_context =
718 container_of(definition, struct definition_struct, p);
719 stream->parent_def_scope = stream->stream_event_context->p.scope;
720 }
721 stream->events_by_id = g_ptr_array_new();
722 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
723 for (i = 0; i < stream->events_by_id->len; i++) {
724 struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
725 struct ctf_file_event *file_event;
726
727 if (!event)
728 continue;
729 file_event = create_event_definitions(td, stream, event);
730 if (!file_event)
731 goto error_event;
732 g_ptr_array_index(stream->events_by_id, i) = file_event;
733 }
734 return 0;
735
736error_event:
737 for (i = 0; i < stream->events_by_id->len; i++) {
738 struct ctf_file_event *file_event = g_ptr_array_index(stream->events_by_id, i);
739 if (file_event)
740 g_free(file_event);
741 }
742 g_ptr_array_free(stream->events_by_id, TRUE);
743error:
744 if (stream->stream_event_context)
745 definition_unref(&stream->stream_event_context->p);
746 if (stream->stream_event_header)
747 definition_unref(&stream->stream_event_header->p);
748 if (stream->stream_packet_context)
749 definition_unref(&stream->stream_packet_context->p);
750 return ret;
751}
752
0f980a35
MD
753
754static
46322b33 755int create_stream_packet_index(struct ctf_trace *td,
0f980a35
MD
756 struct ctf_file_stream *file_stream)
757{
aa6bffae 758 struct ctf_stream_class *stream;
0f980a35 759 int len_index;
46322b33 760 struct ctf_stream_pos *pos;
0f980a35
MD
761 struct stat filestats;
762 struct packet_index packet_index;
763 int first_packet = 1;
764 int ret;
765
766 pos = &file_stream->pos;
767
768 ret = fstat(pos->fd, &filestats);
769 if (ret < 0)
770 return ret;
771
772 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
773 uint64_t stream_id = 0;
774
775 if (pos->base) {
776 /* unmap old base */
8c572eba 777 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 778 if (ret) {
46322b33 779 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
780 strerror(errno));
781 return ret;
782 }
8c572eba 783 pos->base = NULL;
0f980a35
MD
784 }
785 /* map new base. Need mapping length from header. */
8c572eba 786 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
0f980a35 787 MAP_PRIVATE, pos->fd, pos->mmap_offset);
dc48ecad
MD
788 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
789 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
0f980a35
MD
790 pos->offset = 0; /* Position of the packet header */
791
8c572eba
MD
792 packet_index.offset = pos->mmap_offset;
793 packet_index.content_size = 0;
794 packet_index.packet_size = 0;
795
0f980a35 796 /* read and check header, set stream id (and check) */
e28d4618 797 if (file_stream->stream.trace_packet_header) {
0f980a35 798 /* Read packet header */
e28d4618 799 ret = generic_rw(&pos->parent, &file_stream->stream.trace_packet_header->p);
c5e74408
MD
800 if (ret)
801 return ret;
e28d4618 802 len_index = struct_declaration_lookup_field_index(file_stream->stream.trace_packet_header->declaration, g_quark_from_static_string("magic"));
0f980a35
MD
803 if (len_index >= 0) {
804 struct definition_integer *defint;
b1a2f580 805 struct definition *field;
0f980a35 806
e28d4618 807 field = struct_definition_get_field_from_index(file_stream->stream.trace_packet_header, len_index);
b1a2f580
MD
808 assert(field->declaration->id == CTF_TYPE_INTEGER);
809 defint = container_of(field, struct definition_integer, p);
0f980a35
MD
810 assert(defint->declaration->signedness == FALSE);
811 if (defint->value._unsigned != CTF_MAGIC) {
d8ea2d29 812 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
8c572eba
MD
813 defint->value._unsigned,
814 file_stream->pos.packet_index->len,
815 (ssize_t) pos->mmap_offset);
0f980a35
MD
816 return -EINVAL;
817 }
818 }
819
820 /* check uuid */
e28d4618 821 len_index = struct_declaration_lookup_field_index(file_stream->stream.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
0f980a35
MD
822 if (len_index >= 0) {
823 struct definition_array *defarray;
b1a2f580 824 struct definition *field;
0f980a35
MD
825 uint64_t i;
826 uint8_t uuidval[UUID_LEN];
827
e28d4618 828 field = struct_definition_get_field_from_index(file_stream->stream.trace_packet_header, len_index);
b1a2f580
MD
829 assert(field->declaration->id == CTF_TYPE_ARRAY);
830 defarray = container_of(field, struct definition_array, p);
3838df27 831 assert(array_len(defarray) == UUID_LEN);
0f980a35
MD
832 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
833
834 for (i = 0; i < UUID_LEN; i++) {
835 struct definition *elem;
836 struct definition_integer *defint;
837
838 elem = array_index(defarray, i);
839 assert(elem);
840 defint = container_of(elem, struct definition_integer, p);
841 uuidval[i] = defint->value._unsigned;
842 }
46322b33 843 ret = uuid_compare(td->uuid, uuidval);
0f980a35
MD
844 if (ret) {
845 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
846 return -EINVAL;
847 }
848 }
849
850
e28d4618 851 len_index = struct_declaration_lookup_field_index(file_stream->stream.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
0f980a35
MD
852 if (len_index >= 0) {
853 struct definition_integer *defint;
b1a2f580 854 struct definition *field;
0f980a35 855
e28d4618 856 field = struct_definition_get_field_from_index(file_stream->stream.trace_packet_header, len_index);
b1a2f580
MD
857 assert(field->declaration->id == CTF_TYPE_INTEGER);
858 defint = container_of(field, struct definition_integer, p);
0f980a35
MD
859 assert(defint->declaration->signedness == FALSE);
860 stream_id = defint->value._unsigned;
861 }
862 }
863
864 if (!first_packet && file_stream->stream_id != stream_id) {
865 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
866 return -EINVAL;
867 }
868 if (first_packet) {
869 file_stream->stream_id = stream_id;
46322b33 870 if (stream_id >= td->streams->len) {
0f980a35
MD
871 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
872 return -EINVAL;
873 }
46322b33 874 stream = g_ptr_array_index(td->streams, stream_id);
0f980a35
MD
875 if (!stream) {
876 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
877 return -EINVAL;
878 }
764af3f4 879 file_stream->stream.stream_class = stream;
0f980a35
MD
880 }
881 first_packet = 0;
882
e28d4618
MD
883 ret = create_stream_definitions(td, &file_stream->stream);
884 if (ret)
885 return ret;
886
887 if (file_stream->stream.stream_packet_context) {
dc48ecad 888 /* Read packet context */
e28d4618 889 ret = generic_rw(&pos->parent, &file_stream->stream.stream_packet_context->p);
c5e74408
MD
890 if (ret)
891 return ret;
dc48ecad 892 /* read content size from header */
e28d4618 893 len_index = struct_declaration_lookup_field_index(file_stream->stream.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
dc48ecad
MD
894 if (len_index >= 0) {
895 struct definition_integer *defint;
b1a2f580 896 struct definition *field;
dc48ecad 897
e28d4618 898 field = struct_definition_get_field_from_index(file_stream->stream.stream_packet_context, len_index);
b1a2f580
MD
899 assert(field->declaration->id == CTF_TYPE_INTEGER);
900 defint = container_of(field, struct definition_integer, p);
dc48ecad 901 assert(defint->declaration->signedness == FALSE);
8c572eba 902 packet_index.content_size = defint->value._unsigned;
dc48ecad
MD
903 } else {
904 /* Use file size for packet size */
8c572eba 905 packet_index.content_size = filestats.st_size * CHAR_BIT;
dc48ecad
MD
906 }
907
908 /* read packet size from header */
e28d4618 909 len_index = struct_declaration_lookup_field_index(file_stream->stream.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
dc48ecad
MD
910 if (len_index >= 0) {
911 struct definition_integer *defint;
b1a2f580 912 struct definition *field;
dc48ecad 913
e28d4618 914 field = struct_definition_get_field_from_index(file_stream->stream.stream_packet_context, len_index);
b1a2f580
MD
915 assert(field->declaration->id == CTF_TYPE_INTEGER);
916 defint = container_of(field, struct definition_integer, p);
dc48ecad 917 assert(defint->declaration->signedness == FALSE);
8c572eba 918 packet_index.packet_size = defint->value._unsigned;
dc48ecad
MD
919 } else {
920 /* Use content size if non-zero, else file size */
8c572eba 921 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
dc48ecad 922 }
0f980a35
MD
923 } else {
924 /* Use file size for packet size */
8c572eba 925 packet_index.content_size = filestats.st_size * CHAR_BIT;
0f980a35 926 /* Use content size if non-zero, else file size */
8c572eba 927 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
0f980a35 928 }
546293fa
MD
929
930 /* Validate content size and packet size values */
931 if (packet_index.content_size > packet_index.packet_size) {
932 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
933 packet_index.content_size, packet_index.packet_size);
934 return -EINVAL;
935 }
936
58b0b883
MD
937 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
938 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
939 packet_index.content_size, (filestats.st_size - packet_index.offset) * CHAR_BIT);
546293fa
MD
940 return -EINVAL;
941 }
942
847bf71a
MD
943 /* Save position after header and context */
944 packet_index.data_offset = pos->offset;
0f980a35 945
0f980a35
MD
946 /* add index to packet array */
947 g_array_append_val(file_stream->pos.packet_index, packet_index);
948
8c572eba 949 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
0f980a35
MD
950 }
951
847bf71a
MD
952 /* Move pos back to beginning of file */
953 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
954
0f980a35
MD
955 return 0;
956}
957
e28d4618
MD
958static
959int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
960{
961 int ret;
962
963 if (td->packet_header_decl) {
964 struct definition *definition =
965 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
966 stream->parent_def_scope, 0, 0, "trace.packet.header");
967 if (!definition) {
968 ret = -EINVAL;
969 goto error;
970 }
971 stream->trace_packet_header =
972 container_of(definition, struct definition_struct, p);
973 stream->parent_def_scope = stream->trace_packet_header->p.scope;
974 }
975
976 return 0;
977
978error:
979 return ret;
980}
981
0f980a35
MD
982/*
983 * Note: many file streams can inherit from the same stream class
984 * description (metadata).
985 */
986static
46322b33 987int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
0f980a35
MD
988{
989 int ret;
990 struct ctf_file_stream *file_stream;
991
46322b33 992 ret = openat(td->dirfd, path, flags);
0f980a35
MD
993 if (ret < 0)
994 goto error;
995 file_stream = g_new0(struct ctf_file_stream, 1);
8563e754 996 ctf_init_pos(&file_stream->pos, ret, flags);
e28d4618
MD
997 ret = create_trace_definitions(td, &file_stream->stream);
998 if (ret)
999 goto error_def;
0f980a35
MD
1000 ret = create_stream_packet_index(td, file_stream);
1001 if (ret)
1002 goto error_index;
1003 /* Add stream file to stream class */
764af3f4 1004 g_ptr_array_add(file_stream->stream.stream_class->files, file_stream);
0f980a35
MD
1005 return 0;
1006
1007error_index:
e28d4618
MD
1008 if (file_stream->stream.trace_packet_header)
1009 definition_unref(&file_stream->stream.trace_packet_header->p);
1010error_def:
46322b33 1011 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
1012 close(file_stream->pos.fd);
1013 g_free(file_stream);
1014error:
65102a8c
MD
1015 return ret;
1016}
1017
bbefb8dd 1018static
46322b33 1019int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
bbefb8dd
MD
1020{
1021 int ret;
65102a8c
MD
1022 struct dirent *dirent;
1023 struct dirent *diriter;
1024 size_t dirent_len;
bbefb8dd 1025
46322b33 1026 td->flags = flags;
bbefb8dd
MD
1027
1028 /* Open trace directory */
46322b33
MD
1029 td->dir = opendir(path);
1030 if (!td->dir) {
dc48ecad 1031 fprintf(stdout, "[error] Unable to open trace directory.\n");
bbefb8dd
MD
1032 ret = -ENOENT;
1033 goto error;
1034 }
1035
46322b33
MD
1036 td->dirfd = open(path, 0);
1037 if (td->dirfd < 0) {
dc48ecad 1038 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
65102a8c
MD
1039 ret = -ENOENT;
1040 goto error_dirfd;
1041 }
0f980a35 1042
65102a8c
MD
1043 /*
1044 * Keep the metadata file separate.
1045 */
bbefb8dd 1046
65102a8c
MD
1047 ret = ctf_open_trace_metadata_read(td);
1048 if (ret) {
1049 goto error_metadata;
1050 }
bbefb8dd
MD
1051
1052 /*
1053 * Open each stream: for each file, try to open, check magic
1054 * number, and get the stream ID to add to the right location in
1055 * the stream array.
bbefb8dd
MD
1056 */
1057
65102a8c 1058 dirent_len = offsetof(struct dirent, d_name) +
46322b33 1059 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 1060
65102a8c 1061 dirent = malloc(dirent_len);
bbefb8dd 1062
65102a8c 1063 for (;;) {
46322b33 1064 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 1065 if (ret) {
dc48ecad 1066 fprintf(stdout, "[error] Readdir error.\n");
65102a8c 1067 goto readdir_error;
65102a8c
MD
1068 }
1069 if (!diriter)
1070 break;
d8ea2d29
MD
1071 /* Ignore hidden files, ., .. and metadata. */
1072 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
1073 || !strcmp(diriter->d_name, "..")
1074 || !strcmp(diriter->d_name, "metadata"))
1075 continue;
dc48ecad
MD
1076 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
1077 if (ret) {
1078 fprintf(stdout, "[error] Open file stream error.\n");
1079 goto readdir_error;
1080 }
65102a8c 1081 }
bbefb8dd 1082
65102a8c 1083 free(dirent);
bbefb8dd 1084 return 0;
65102a8c
MD
1085
1086readdir_error:
1087 free(dirent);
1088error_metadata:
46322b33 1089 close(td->dirfd);
65102a8c 1090error_dirfd:
46322b33 1091 closedir(td->dir);
bbefb8dd
MD
1092error:
1093 return ret;
1094}
1095
e9378815 1096static
bbefb8dd
MD
1097struct trace_descriptor *ctf_open_trace(const char *path, int flags)
1098{
46322b33 1099 struct ctf_trace *td;
bbefb8dd
MD
1100 int ret;
1101
46322b33 1102 td = g_new0(struct ctf_trace, 1);
bbefb8dd 1103
8c572eba 1104 switch (flags & O_ACCMODE) {
bbefb8dd 1105 case O_RDONLY:
b61922b5
MD
1106 if (!path) {
1107 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
1108 goto error;
1109 }
bbefb8dd
MD
1110 ret = ctf_open_trace_read(td, path, flags);
1111 if (ret)
1112 goto error;
1113 break;
989c73bc 1114 case O_RDWR:
46322b33
MD
1115 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
1116 goto error;
bbefb8dd 1117 default:
46322b33 1118 fprintf(stdout, "[error] Incorrect open flags.\n");
bbefb8dd
MD
1119 goto error;
1120 }
1121
46322b33 1122 return &td->parent;
bbefb8dd
MD
1123error:
1124 g_free(td);
1125 return NULL;
1126}
1127
0f980a35
MD
1128static
1129void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1130{
46322b33 1131 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
1132 close(file_stream->pos.fd);
1133}
1134
e9378815 1135static
46322b33 1136void ctf_close_trace(struct trace_descriptor *tdp)
bbefb8dd 1137{
46322b33 1138 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
0f980a35
MD
1139 int i;
1140
46322b33
MD
1141 if (td->streams) {
1142 for (i = 0; i < td->streams->len; i++) {
aa6bffae 1143 struct ctf_stream_class *stream;
0f980a35 1144 int j;
e9378815 1145
46322b33 1146 stream = g_ptr_array_index(td->streams, i);
e9378815
MD
1147 if (!stream)
1148 continue;
0f980a35
MD
1149 for (j = 0; j < stream->files->len; j++) {
1150 struct ctf_file_stream *file_stream;
2c117823 1151 file_stream = g_ptr_array_index(stream->files, j);
0f980a35
MD
1152 ctf_close_file_stream(file_stream);
1153 }
1154
1155 }
46322b33 1156 g_ptr_array_free(td->streams, TRUE);
0f980a35 1157 }
46322b33 1158 closedir(td->dir);
bbefb8dd
MD
1159 g_free(td);
1160}
1161
7fb21036 1162void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
1163{
1164 int ret;
1165
4c8bfb7e 1166 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
1167 ret = bt_register_format(&ctf_format);
1168 assert(!ret);
1169}
698f0fe4
MD
1170
1171/* TODO: finalize */
This page took 0.079073 seconds and 4 git commands to generate.