Update version to 0.10
[babeltrace.git] / formats / ctf / ctf.c
CommitLineData
fc93b2bd
MD
1/*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * Format registration.
5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fc93b2bd 9 *
ccd7e1c8
MD
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
fc93b2bd 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
fc93b2bd
MD
19 */
20
21#include <babeltrace/format.h>
22#include <babeltrace/ctf/types.h>
bbefb8dd 23#include <babeltrace/ctf/metadata.h>
70bd0a12 24#include <babeltrace/babeltrace-internal.h>
0f980a35 25#include <inttypes.h>
b4c19c1e 26#include <stdio.h>
0f980a35
MD
27#include <uuid/uuid.h>
28#include <sys/mman.h>
bbefb8dd 29#include <errno.h>
b4c19c1e 30#include <endian.h>
bbefb8dd 31#include <sys/types.h>
65102a8c 32#include <sys/stat.h>
bbefb8dd 33#include <fcntl.h>
65102a8c 34#include <dirent.h>
bbefb8dd 35#include <glib.h>
65102a8c
MD
36#include <unistd.h>
37#include <stdlib.h>
38
65102a8c
MD
39#include "metadata/ctf-scanner.h"
40#include "metadata/ctf-parser.h"
41#include "metadata/ctf-ast.h"
42
0f980a35
MD
43/*
44 * We currently simply map a page to read the packet header and packet
8c572eba 45 * context to get the packet length and content length. (in bits)
0f980a35 46 */
8c572eba
MD
47#define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
48#define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
0f980a35
MD
49#define UUID_LEN 16 /* uuid by value len */
50
a0fe7d97
MD
51#ifndef min
52#define min(a, b) (((a) < (b)) ? (a) : (b))
53#endif
54
7d97fad9
MD
55#define NSEC_PER_SEC 1000000000ULL
56
57int opt_clock_raw,
58 opt_clock_seconds,
59 opt_clock_date,
60 opt_clock_gmt;
61
62uint64_t opt_clock_offset;
63
65102a8c 64extern int yydebug;
bbefb8dd 65
e9378815 66static
5b80ddfb 67struct trace_descriptor *ctf_open_trace(const char *path, int flags,
b086c01a 68 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
ae23d232 69 int whence), FILE *metadata_fp);
e9378815 70static
f571dfb1
JD
71struct trace_descriptor *ctf_open_mmap_trace(
72 struct mmap_stream_list *mmap_list,
73 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, int whence),
74 FILE *metadata_fp);
75
76static
bbefb8dd 77void ctf_close_trace(struct trace_descriptor *descriptor);
fc93b2bd 78
1ae19169
MD
79static
80rw_dispatch read_dispatch_table[] = {
d11e9c49
MD
81 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
82 [ CTF_TYPE_FLOAT ] = ctf_float_read,
83 [ CTF_TYPE_ENUM ] = ctf_enum_read,
84 [ CTF_TYPE_STRING ] = ctf_string_read,
85 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
86 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
87 [ CTF_TYPE_ARRAY ] = ctf_array_read,
88 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
d11e9c49
MD
89};
90
1ae19169
MD
91static
92rw_dispatch write_dispatch_table[] = {
d11e9c49
MD
93 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
94 [ CTF_TYPE_FLOAT ] = ctf_float_write,
95 [ CTF_TYPE_ENUM ] = ctf_enum_write,
96 [ CTF_TYPE_STRING ] = ctf_string_write,
97 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
98 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
99 [ CTF_TYPE_ARRAY ] = ctf_array_write,
100 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
d11e9c49
MD
101};
102
1ae19169 103static
d11e9c49 104struct format ctf_format = {
bbefb8dd 105 .open_trace = ctf_open_trace,
f571dfb1 106 .open_mmap_trace = ctf_open_mmap_trace,
bbefb8dd 107 .close_trace = ctf_close_trace,
fc93b2bd
MD
108};
109
25ccc85b
MD
110/*
111 * Update stream current timestamp, keep at clock frequency.
112 */
2b9a764d
MD
113static
114void ctf_update_timestamp(struct ctf_stream *stream,
115 struct definition_integer *integer_definition)
116{
117 struct declaration_integer *integer_declaration =
118 integer_definition->declaration;
119 uint64_t oldval, newval, updateval;
120
7f3f572b 121 if (unlikely(integer_declaration->len == 64)) {
2b9a764d
MD
122 stream->timestamp = integer_definition->value._unsigned;
123 return;
124 }
125 /* keep low bits */
126 oldval = stream->timestamp;
127 oldval &= (1ULL << integer_declaration->len) - 1;
128 newval = integer_definition->value._unsigned;
129 /* Test for overflow by comparing low bits */
130 if (newval < oldval)
131 newval += 1ULL << integer_declaration->len;
132 /* updateval contains old high bits, and new low bits (sum) */
133 updateval = stream->timestamp;
134 updateval &= ~((1ULL << integer_declaration->len) - 1);
135 updateval += newval;
fca04958 136 stream->prev_timestamp = stream->timestamp;
2b9a764d
MD
137 stream->timestamp = updateval;
138}
139
25ccc85b
MD
140/*
141 * Print timestamp, rescaling clock frequency to nanoseconds and
142 * applying offsets as needed (unix time).
143 */
7d97fad9
MD
144void ctf_print_timestamp(FILE *fp,
145 struct ctf_stream *stream,
146 uint64_t timestamp)
147{
148 uint64_t ts_sec = 0, ts_nsec;
149 struct ctf_trace *trace = stream->stream_class->trace;
150 struct trace_collection *tc = trace->collection;
0716bb06 151 uint64_t tc_offset = tc->single_clock_offset_avg;
7d97fad9 152
25ccc85b
MD
153 if (stream->current_clock->freq == 1000000000ULL) {
154 ts_nsec = timestamp;
155 } else {
156 ts_nsec = (uint64_t) ((double) timestamp * 1000000000.0
157 / (double) stream->current_clock->freq);
158 }
7d97fad9
MD
159
160 /* Add offsets */
0716bb06
MD
161 if (!opt_clock_raw) {
162 ts_nsec += tc_offset;
7d97fad9
MD
163 }
164 ts_sec += opt_clock_offset;
165
166 ts_sec += ts_nsec / NSEC_PER_SEC;
167 ts_nsec = ts_nsec % NSEC_PER_SEC;
168
169 if (!opt_clock_seconds) {
170 struct tm tm;
171 time_t time_s = (time_t) ts_sec;
172
173 if (!opt_clock_gmt) {
174 struct tm *res;
175
176 res = localtime_r(&time_s, &tm);
177 if (!res) {
178 fprintf(stderr, "[warning] Unable to get localtime.\n");
179 goto seconds;
180 }
181 } else {
182 struct tm *res;
183
184 res = gmtime_r(&time_s, &tm);
185 if (!res) {
186 fprintf(stderr, "[warning] Unable to get gmtime.\n");
187 goto seconds;
188 }
189 }
190 if (opt_clock_date) {
191 char timestr[26];
192 size_t res;
193
194 /* Print date and time */
195 res = strftime(timestr, sizeof(timestr),
196 "%F ", &tm);
197 if (!res) {
198 fprintf(stderr, "[warning] Unable to print ascii time.\n");
199 goto seconds;
200 }
201 fprintf(fp, "%s", timestr);
202 }
203 /* Print time in HH:MM:SS.ns */
204 fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
205 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
206 goto end;
207 }
208seconds:
209 fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
210 ts_sec, ts_nsec);
211
212end:
213 return;
214}
215
31262354 216static
764af3f4 217int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream)
31262354
MD
218{
219 struct ctf_stream_pos *pos =
220 container_of(ppos, struct ctf_stream_pos, parent);
764af3f4 221 struct ctf_stream_class *stream_class = stream->stream_class;
42dc00b7 222 struct ctf_stream_event *event;
31262354 223 uint64_t id = 0;
31262354
MD
224 int ret;
225
3abe83c7
MD
226 /* We need to check for EOF here for empty files. */
227 if (unlikely(pos->offset == EOF))
228 return EOF;
229
5f643ed7
MD
230 ctf_pos_get_event(pos);
231
3abe83c7
MD
232 /*
233 * This is the EOF check after we've advanced the position in
234 * ctf_pos_get_event.
235 */
7f3f572b 236 if (unlikely(pos->offset == EOF))
31262354 237 return EOF;
5f643ed7 238 assert(pos->offset < pos->content_size);
31262354
MD
239
240 /* Read event header */
7f3f572b 241 if (likely(stream->stream_event_header)) {
a35173fe 242 struct definition_integer *integer_definition;
ccdb988e 243 struct definition *variant;
a35173fe 244
e28d4618 245 ret = generic_rw(ppos, &stream->stream_event_header->p);
7f3f572b 246 if (unlikely(ret))
31262354
MD
247 goto error;
248 /* lookup event id */
e28d4618 249 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
250 if (integer_definition) {
251 id = integer_definition->value._unsigned;
252 } else {
253 struct definition_enum *enum_definition;
254
e28d4618 255 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
256 if (enum_definition) {
257 id = enum_definition->integer->value._unsigned;
258 }
31262354 259 }
764af3f4 260
e28d4618 261 variant = lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e
MD
262 if (variant) {
263 integer_definition = lookup_integer(variant, "id", FALSE);
264 if (integer_definition) {
265 id = integer_definition->value._unsigned;
266 }
267 }
c87a8eb2 268 stream->event_id = id;
ccdb988e 269
764af3f4 270 /* lookup timestamp */
5e2eb0ae 271 stream->has_timestamp = 0;
e28d4618 272 integer_definition = lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
a35173fe 273 if (integer_definition) {
2b9a764d 274 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 275 stream->has_timestamp = 1;
a35173fe 276 } else {
ccdb988e
MD
277 if (variant) {
278 integer_definition = lookup_integer(variant, "timestamp", FALSE);
a35173fe 279 if (integer_definition) {
2b9a764d 280 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 281 stream->has_timestamp = 1;
a35173fe
MD
282 }
283 }
764af3f4 284 }
31262354
MD
285 }
286
287 /* Read stream-declared event context */
e28d4618
MD
288 if (stream->stream_event_context) {
289 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
290 if (ret)
291 goto error;
292 }
293
7f3f572b 294 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 295 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
296 return -EINVAL;
297 }
e28d4618 298 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 299 if (unlikely(!event)) {
3394d22e 300 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
301 return -EINVAL;
302 }
303
304 /* Read event-declared event context */
e28d4618
MD
305 if (event->event_context) {
306 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
307 if (ret)
308 goto error;
309 }
310
311 /* Read event payload */
7f3f572b 312 if (likely(event->event_fields)) {
e28d4618 313 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
314 if (ret)
315 goto error;
316 }
317
318 return 0;
319
320error:
3394d22e 321 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
31262354
MD
322 return ret;
323}
324
325static
764af3f4 326int ctf_write_event(struct stream_pos *pos, struct ctf_stream *stream)
31262354 327{
764af3f4 328 struct ctf_stream_class *stream_class = stream->stream_class;
42dc00b7 329 struct ctf_stream_event *event;
c87a8eb2 330 uint64_t id;
31262354
MD
331 int ret;
332
c87a8eb2
MD
333 id = stream->event_id;
334
31262354 335 /* print event header */
7f3f572b 336 if (likely(stream->stream_event_header)) {
e28d4618 337 ret = generic_rw(pos, &stream->stream_event_header->p);
31262354
MD
338 if (ret)
339 goto error;
340 }
341
342 /* print stream-declared event context */
e28d4618
MD
343 if (stream->stream_event_context) {
344 ret = generic_rw(pos, &stream->stream_event_context->p);
31262354
MD
345 if (ret)
346 goto error;
347 }
348
7f3f572b 349 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 350 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
351 return -EINVAL;
352 }
e28d4618 353 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 354 if (unlikely(!event)) {
3394d22e 355 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
356 return -EINVAL;
357 }
358
359 /* print event-declared event context */
e28d4618
MD
360 if (event->event_context) {
361 ret = generic_rw(pos, &event->event_context->p);
31262354
MD
362 if (ret)
363 goto error;
364 }
365
366 /* Read and print event payload */
7f3f572b 367 if (likely(event->event_fields)) {
e28d4618 368 ret = generic_rw(pos, &event->event_fields->p);
31262354
MD
369 if (ret)
370 goto error;
371 }
372
373 return 0;
374
375error:
3394d22e 376 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
31262354
MD
377 return ret;
378}
379
8563e754 380void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
8c572eba
MD
381{
382 pos->fd = fd;
383 pos->mmap_offset = 0;
384 pos->packet_size = 0;
385 pos->content_size = 0;
386 pos->content_size_loc = NULL;
387 pos->base = NULL;
388 pos->offset = 0;
389 pos->dummy = false;
8c572eba 390 pos->cur_index = 0;
8563e754
MD
391 if (fd >= 0)
392 pos->packet_index = g_array_new(FALSE, TRUE,
393 sizeof(struct packet_index));
394 else
395 pos->packet_index = NULL;
8563e754
MD
396 switch (open_flags & O_ACCMODE) {
397 case O_RDONLY:
398 pos->prot = PROT_READ;
399 pos->flags = MAP_PRIVATE;
400 pos->parent.rw_table = read_dispatch_table;
31262354 401 pos->parent.event_cb = ctf_read_event;
8563e754 402 break;
8563e754
MD
403 case O_RDWR:
404 pos->prot = PROT_WRITE; /* Write has priority */
405 pos->flags = MAP_SHARED;
406 pos->parent.rw_table = write_dispatch_table;
31262354 407 pos->parent.event_cb = ctf_write_event;
8563e754 408 if (fd >= 0)
847bf71a 409 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
8563e754
MD
410 break;
411 default:
412 assert(0);
8c572eba
MD
413 }
414}
415
46322b33 416void ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba
MD
417{
418 int ret;
419
420 if (pos->prot == PROT_WRITE && pos->content_size_loc)
421 *pos->content_size_loc = pos->offset;
422 if (pos->base) {
423 /* unmap old base */
424 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
425 if (ret) {
3394d22e 426 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
8c572eba
MD
427 strerror(errno));
428 assert(0);
429 }
430 }
431 (void) g_array_free(pos->packet_index, TRUE);
432}
433
847bf71a 434void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
0f980a35 435{
75cc2c35
MD
436 struct ctf_file_stream *file_stream =
437 container_of(pos, struct ctf_file_stream, pos);
0f980a35 438 int ret;
8c572eba
MD
439 off_t off;
440 struct packet_index *index;
0f980a35 441
8c572eba
MD
442 if (pos->prot == PROT_WRITE && pos->content_size_loc)
443 *pos->content_size_loc = pos->offset;
0f980a35
MD
444
445 if (pos->base) {
446 /* unmap old base */
8c572eba 447 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 448 if (ret) {
3394d22e 449 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
450 strerror(errno));
451 assert(0);
452 }
847bf71a 453 pos->base = NULL;
0f980a35
MD
454 }
455
8c572eba 456 /*
46322b33 457 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
458 * except to get exactly at the beginning of the next packet.
459 */
460 if (pos->prot == PROT_WRITE) {
989c73bc
MD
461 switch (whence) {
462 case SEEK_CUR:
463 /* The writer will add padding */
464 assert(pos->offset + offset == pos->packet_size);
8c572eba 465 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
989c73bc
MD
466 break;
467 case SEEK_SET:
468 assert(offset == 0); /* only seek supported for now */
469 pos->cur_index = 0;
470 break;
471 default:
472 assert(0);
473 }
8c572eba
MD
474 pos->content_size = -1U; /* Unknown at this point */
475 pos->packet_size = WRITE_PACKET_LEN;
989c73bc
MD
476 off = posix_fallocate(pos->fd, pos->mmap_offset,
477 pos->packet_size / CHAR_BIT);
8c572eba 478 assert(off >= 0);
847bf71a 479 pos->offset = 0;
8c572eba 480 } else {
5f643ed7 481 read_next_packet:
847bf71a
MD
482 switch (whence) {
483 case SEEK_CUR:
41e82e00
MD
484 {
485 uint32_t events_discarded_diff;
486
7d97fad9
MD
487 if (pos->offset == EOF) {
488 return;
489 }
3217ac37 490 /* For printing discarded event count */
41e82e00
MD
491 index = &g_array_index(pos->packet_index,
492 struct packet_index, pos->cur_index);
493 events_discarded_diff = index->events_discarded;
3217ac37
MD
494 file_stream->parent.prev_timestamp_end =
495 index->timestamp_end;
41e82e00
MD
496 if (pos->cur_index > 0) {
497 index = &g_array_index(pos->packet_index,
498 struct packet_index,
499 pos->cur_index - 1);
500 events_discarded_diff -= index->events_discarded;
501 }
fca04958 502 file_stream->parent.events_discarded = events_discarded_diff;
7d97fad9 503 file_stream->parent.prev_timestamp = file_stream->parent.timestamp;
847bf71a
MD
504 /* The reader will expect us to skip padding */
505 assert(pos->offset + offset == pos->content_size);
8c572eba 506 ++pos->cur_index;
847bf71a 507 break;
41e82e00 508 }
847bf71a
MD
509 case SEEK_SET:
510 assert(offset == 0); /* only seek supported for now */
511 pos->cur_index = 0;
fca04958 512 file_stream->parent.prev_timestamp = 0;
3217ac37 513 file_stream->parent.prev_timestamp_end = 0;
847bf71a
MD
514 break;
515 default:
516 assert(0);
517 }
518 if (pos->cur_index >= pos->packet_index->len) {
7d97fad9
MD
519 /*
520 * When a stream reaches the end of the
521 * file, we need to show the number of
522 * events discarded ourselves, because
523 * there is no next event scheduled to
524 * be printed in the output.
525 */
526 if (file_stream->parent.events_discarded) {
527 fflush(stdout);
528 fprintf(stderr, "[warning] Tracer discarded %d events at end of stream between [",
529 file_stream->parent.events_discarded);
530 ctf_print_timestamp(stderr, &file_stream->parent,
531 file_stream->parent.prev_timestamp);
532 fprintf(stderr, "] and [");
533 ctf_print_timestamp(stderr, &file_stream->parent,
534 file_stream->parent.prev_timestamp_end);
535 fprintf(stderr, "]. You should consider increasing the buffer size.\n");
536 fflush(stderr);
537 file_stream->parent.events_discarded = 0;
538 }
670977d3 539 pos->offset = EOF;
847bf71a
MD
540 return;
541 }
8c572eba
MD
542 index = &g_array_index(pos->packet_index, struct packet_index,
543 pos->cur_index);
544 pos->mmap_offset = index->offset;
545
546 /* Lookup context/packet size in index */
2d0bea29 547 file_stream->parent.timestamp = index->timestamp_begin;
8c572eba
MD
548 pos->content_size = index->content_size;
549 pos->packet_size = index->packet_size;
5f643ed7 550 if (index->data_offset < index->content_size) {
75cc2c35 551 pos->offset = 0; /* will read headers */
5f643ed7
MD
552 } else if (index->data_offset == index->content_size) {
553 /* empty packet */
554 pos->offset = index->data_offset;
555 offset = 0;
3abe83c7 556 whence = SEEK_CUR;
5f643ed7 557 goto read_next_packet;
7eda6fc7 558 } else {
2b9a764d
MD
559 pos->offset = EOF;
560 return;
561 }
8c572eba 562 }
0f980a35 563 /* map new base. Need mapping length from header. */
8c572eba
MD
564 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
565 pos->flags, pos->fd, pos->mmap_offset);
847bf71a 566 if (pos->base == MAP_FAILED) {
3394d22e 567 fprintf(stderr, "[error] mmap error %s.\n",
847bf71a
MD
568 strerror(errno));
569 assert(0);
570 }
75cc2c35
MD
571
572 /* update trace_packet_header and stream_packet_context */
2d0bea29 573 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
75cc2c35 574 /* Read packet header */
2d0bea29 575 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
75cc2c35
MD
576 assert(!ret);
577 }
2d0bea29 578 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
75cc2c35 579 /* Read packet context */
2d0bea29 580 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
75cc2c35
MD
581 assert(!ret);
582 }
0f980a35
MD
583}
584
b4c19c1e
MD
585static
586int packet_metadata(struct ctf_trace *td, FILE *fp)
587{
588 uint32_t magic;
589 size_t len;
590 int ret = 0;
591
592 len = fread(&magic, sizeof(magic), 1, fp);
a0fe7d97 593 if (len != 1) {
b4c19c1e
MD
594 goto end;
595 }
596 if (magic == TSDL_MAGIC) {
597 ret = 1;
598 td->byte_order = BYTE_ORDER;
0d336fdf 599 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
600 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
601 ret = 1;
602 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
603 LITTLE_ENDIAN : BIG_ENDIAN;
0d336fdf 604 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
605 }
606end:
607 rewind(fp);
608 return ret;
609}
610
5c262147
MD
611/*
612 * Returns 0 on success, -1 on error.
613 */
614static
615int check_version(unsigned int major, unsigned int minor)
616{
617 switch (major) {
618 case 1:
619 switch (minor) {
620 case 8:
621 return 0;
622 default:
623 goto warning;
624 }
625 default:
626 goto warning;
627
628 }
629
630 /* eventually return an error instead of warning */
631warning:
3394d22e 632 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
5c262147
MD
633 major, minor);
634 return 0;
635}
636
b4c19c1e
MD
637static
638int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
639 FILE *out)
640{
641 struct metadata_packet_header header;
a0fe7d97 642 size_t readlen, writelen, toread;
f8254867 643 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
b4c19c1e
MD
644 int ret = 0;
645
a91a962e 646 readlen = fread(&header, header_sizeof(header), 1, in);
a0fe7d97 647 if (readlen < 1)
b4c19c1e
MD
648 return -EINVAL;
649
650 if (td->byte_order != BYTE_ORDER) {
651 header.magic = GUINT32_SWAP_LE_BE(header.magic);
652 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
653 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
654 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
655 }
656 if (header.checksum)
3394d22e 657 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
b4c19c1e 658 if (header.compression_scheme) {
3394d22e 659 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
b4c19c1e
MD
660 header.compression_scheme);
661 return -EINVAL;
662 }
663 if (header.encryption_scheme) {
3394d22e 664 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
b4c19c1e
MD
665 header.encryption_scheme);
666 return -EINVAL;
667 }
668 if (header.checksum_scheme) {
3394d22e 669 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
b4c19c1e
MD
670 header.checksum_scheme);
671 return -EINVAL;
672 }
5c262147
MD
673 if (check_version(header.major, header.minor) < 0)
674 return -EINVAL;
b4c19c1e
MD
675 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
676 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
677 CTF_TRACE_SET_FIELD(td, uuid);
678 } else {
679 if (uuid_compare(header.uuid, td->uuid))
680 return -EINVAL;
681 }
682
255b2138 683 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
a0fe7d97
MD
684
685 for (;;) {
f8254867 686 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
b4c19c1e
MD
687 if (ferror(in)) {
688 ret = -EINVAL;
689 break;
690 }
4152822b 691 if (babeltrace_debug) {
f8254867 692 buf[readlen] = '\0';
3394d22e 693 fprintf(stderr, "[debug] metadata packet read: %s\n",
4152822b
MD
694 buf);
695 }
696
b4c19c1e
MD
697 writelen = fwrite(buf, sizeof(char), readlen, out);
698 if (writelen < readlen) {
699 ret = -EIO;
700 break;
701 }
702 if (ferror(out)) {
703 ret = -EINVAL;
704 break;
705 }
a0fe7d97
MD
706 toread -= readlen;
707 if (!toread) {
7f4b5c4d 708 ret = 0; /* continue reading next packet */
ddbc52af 709 goto read_padding;
a0fe7d97 710 }
b4c19c1e
MD
711 }
712 return ret;
ddbc52af
MD
713
714read_padding:
715 toread = (header.packet_size - header.content_size) / CHAR_BIT;
716 ret = fseek(in, toread, SEEK_CUR);
717 if (ret < 0) {
3394d22e 718 fprintf(stderr, "[warning] Missing padding at end of file\n");
ddbc52af
MD
719 ret = 0;
720 }
721 return ret;
b4c19c1e
MD
722}
723
724static
725int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
726 char **buf)
727{
728 FILE *in, *out;
729 size_t size;
730 int ret;
731
732 in = *fp;
c4f5487e
MD
733 /*
734 * Using strlen on *buf instead of size of open_memstream
735 * because its size includes garbage at the end (after final
736 * \0). This is the allocated size, not the actual string size.
737 */
b4c19c1e 738 out = open_memstream(buf, &size);
a569a564
MD
739 if (out == NULL) {
740 perror("Metadata open_memstream");
b4c19c1e 741 return -errno;
a569a564 742 }
b4c19c1e
MD
743 for (;;) {
744 ret = ctf_open_trace_metadata_packet_read(td, in, out);
7f4b5c4d 745 if (ret) {
b4c19c1e 746 break;
7f4b5c4d
MD
747 }
748 if (feof(in)) {
749 ret = 0;
b4c19c1e
MD
750 break;
751 }
752 }
753 fclose(out); /* flush the buffer */
754 fclose(in);
755 /* open for reading */
c4f5487e 756 *fp = fmemopen(*buf, strlen(*buf), "rb");
a569a564
MD
757 if (!*fp) {
758 perror("Metadata fmemopen");
759 return -errno;
760 }
b4c19c1e
MD
761 return 0;
762}
763
65102a8c 764static
b086c01a
JD
765int ctf_open_trace_metadata_read(struct ctf_trace *td,
766 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
ae23d232 767 int whence), FILE *metadata_fp)
65102a8c
MD
768{
769 struct ctf_scanner *scanner;
2d0bea29 770 struct ctf_file_stream *metadata_stream;
65102a8c 771 FILE *fp;
b4c19c1e 772 char *buf = NULL;
65102a8c
MD
773 int ret = 0;
774
2d0bea29 775 metadata_stream = g_new0(struct ctf_file_stream, 1);
b086c01a
JD
776
777 if (move_pos_slow) {
778 metadata_stream->pos.move_pos_slow = move_pos_slow;
779 } else {
780 fprintf(stderr, "[error] move_pos_slow function undefined.\n");
781 ret = -1;
782 goto end_stream;
783 }
784
ae23d232
JD
785 if (metadata_fp) {
786 fp = metadata_fp;
787 } else {
788 td->metadata = &metadata_stream->parent;
789 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
790 if (metadata_stream->pos.fd < 0) {
3394d22e 791 fprintf(stderr, "Unable to open metadata.\n");
ae23d232
JD
792 return metadata_stream->pos.fd;
793 }
65102a8c 794
ae23d232
JD
795 fp = fdopen(metadata_stream->pos.fd, "r");
796 if (!fp) {
3394d22e 797 fprintf(stderr, "[error] Unable to open metadata stream.\n");
a569a564 798 perror("Metadata stream open");
ae23d232
JD
799 ret = -errno;
800 goto end_stream;
801 }
802 }
65102a8c
MD
803 if (babeltrace_debug)
804 yydebug = 1;
805
b4c19c1e
MD
806 if (packet_metadata(td, fp)) {
807 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
808 if (ret)
809 goto end_packet_read;
da75b0f7 810 } else {
5c262147
MD
811 unsigned int major, minor;
812 ssize_t nr_items;
8c2df3f5 813
da75b0f7 814 td->byte_order = BYTE_ORDER;
8c2df3f5 815
5c262147
MD
816 /* Check text-only metadata header and version */
817 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
818 if (nr_items < 2)
3394d22e 819 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
5c262147
MD
820 if (check_version(major, minor) < 0) {
821 ret = -EINVAL;
822 goto end_packet_read;
823 }
8c2df3f5 824 rewind(fp);
b4c19c1e
MD
825 }
826
65102a8c
MD
827 scanner = ctf_scanner_alloc(fp);
828 if (!scanner) {
3394d22e 829 fprintf(stderr, "[error] Error allocating scanner\n");
65102a8c
MD
830 ret = -ENOMEM;
831 goto end_scanner_alloc;
832 }
833 ret = ctf_scanner_append_ast(scanner);
834 if (ret) {
3394d22e 835 fprintf(stderr, "[error] Error creating AST\n");
65102a8c
MD
836 goto end;
837 }
838
839 if (babeltrace_debug) {
3394d22e 840 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
65102a8c 841 if (ret) {
3394d22e 842 fprintf(stderr, "[error] Error visiting AST for XML output\n");
65102a8c
MD
843 goto end;
844 }
845 }
846
3394d22e 847 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
65102a8c 848 if (ret) {
3394d22e 849 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
850 goto end;
851 }
3394d22e 852 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
ac5c6ca0 853 td, td->byte_order);
65102a8c 854 if (ret) {
3394d22e 855 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
856 goto end;
857 }
858end:
859 ctf_scanner_free(scanner);
860end_scanner_alloc:
b4c19c1e 861end_packet_read:
65102a8c 862 fclose(fp);
b4c19c1e 863 free(buf);
65102a8c 864end_stream:
2d0bea29
MD
865 close(metadata_stream->pos.fd);
866 if (ret)
867 g_free(metadata_stream);
0f980a35
MD
868 return ret;
869}
870
e28d4618 871static
42dc00b7
MD
872struct ctf_stream_event *create_event_definitions(struct ctf_trace *td,
873 struct ctf_stream *stream,
874 struct ctf_event *event)
e28d4618 875{
42dc00b7 876 struct ctf_stream_event *stream_event = g_new0(struct ctf_stream_event, 1);
e28d4618
MD
877
878 if (event->context_decl) {
879 struct definition *definition =
880 event->context_decl->p.definition_new(&event->context_decl->p,
881 stream->parent_def_scope, 0, 0, "event.context");
882 if (!definition) {
883 goto error;
884 }
42dc00b7 885 stream_event->event_context = container_of(definition,
e28d4618 886 struct definition_struct, p);
42dc00b7 887 stream->parent_def_scope = stream_event->event_context->p.scope;
e28d4618
MD
888 }
889 if (event->fields_decl) {
890 struct definition *definition =
891 event->fields_decl->p.definition_new(&event->fields_decl->p,
892 stream->parent_def_scope, 0, 0, "event.fields");
893 if (!definition) {
894 goto error;
895 }
42dc00b7 896 stream_event->event_fields = container_of(definition,
e28d4618 897 struct definition_struct, p);
42dc00b7 898 stream->parent_def_scope = stream_event->event_fields->p.scope;
e28d4618 899 }
42dc00b7 900 return stream_event;
e28d4618
MD
901
902error:
42dc00b7
MD
903 if (stream_event->event_fields)
904 definition_unref(&stream_event->event_fields->p);
905 if (stream_event->event_context)
906 definition_unref(&stream_event->event_context->p);
e28d4618
MD
907 return NULL;
908}
909
910static
911int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
912{
913 struct ctf_stream_class *stream_class;
914 int ret;
915 int i;
916
917 if (stream->stream_definitions_created)
918 return 0;
919
920 stream_class = stream->stream_class;
921
922 if (stream_class->packet_context_decl) {
923 struct definition *definition =
924 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
925 stream->parent_def_scope, 0, 0, "stream.packet.context");
926 if (!definition) {
927 ret = -EINVAL;
928 goto error;
929 }
930 stream->stream_packet_context = container_of(definition,
931 struct definition_struct, p);
932 stream->parent_def_scope = stream->stream_packet_context->p.scope;
933 }
934 if (stream_class->event_header_decl) {
935 struct definition *definition =
936 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
937 stream->parent_def_scope, 0, 0, "stream.event.header");
938 if (!definition) {
939 ret = -EINVAL;
940 goto error;
941 }
942 stream->stream_event_header =
943 container_of(definition, struct definition_struct, p);
944 stream->parent_def_scope = stream->stream_event_header->p.scope;
945 }
946 if (stream_class->event_context_decl) {
947 struct definition *definition =
948 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
949 stream->parent_def_scope, 0, 0, "stream.event.context");
950 if (!definition) {
951 ret = -EINVAL;
952 goto error;
953 }
954 stream->stream_event_context =
955 container_of(definition, struct definition_struct, p);
956 stream->parent_def_scope = stream->stream_event_context->p.scope;
957 }
958 stream->events_by_id = g_ptr_array_new();
959 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
960 for (i = 0; i < stream->events_by_id->len; i++) {
961 struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
42dc00b7 962 struct ctf_stream_event *stream_event;
e28d4618
MD
963
964 if (!event)
965 continue;
42dc00b7
MD
966 stream_event = create_event_definitions(td, stream, event);
967 if (!stream_event)
e28d4618 968 goto error_event;
42dc00b7 969 g_ptr_array_index(stream->events_by_id, i) = stream_event;
e28d4618
MD
970 }
971 return 0;
972
973error_event:
974 for (i = 0; i < stream->events_by_id->len; i++) {
42dc00b7
MD
975 struct ctf_stream_event *stream_event = g_ptr_array_index(stream->events_by_id, i);
976 if (stream_event)
977 g_free(stream_event);
e28d4618
MD
978 }
979 g_ptr_array_free(stream->events_by_id, TRUE);
980error:
981 if (stream->stream_event_context)
982 definition_unref(&stream->stream_event_context->p);
983 if (stream->stream_event_header)
984 definition_unref(&stream->stream_event_header->p);
985 if (stream->stream_packet_context)
986 definition_unref(&stream->stream_packet_context->p);
987 return ret;
988}
989
0f980a35
MD
990
991static
46322b33 992int create_stream_packet_index(struct ctf_trace *td,
0f980a35
MD
993 struct ctf_file_stream *file_stream)
994{
aa6bffae 995 struct ctf_stream_class *stream;
0f980a35 996 int len_index;
46322b33 997 struct ctf_stream_pos *pos;
0f980a35
MD
998 struct stat filestats;
999 struct packet_index packet_index;
1000 int first_packet = 1;
1001 int ret;
1002
1003 pos = &file_stream->pos;
1004
1005 ret = fstat(pos->fd, &filestats);
1006 if (ret < 0)
1007 return ret;
1008
8895362d
MD
1009 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
1010 return -EINVAL;
1011
0f980a35
MD
1012 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1013 uint64_t stream_id = 0;
1014
1015 if (pos->base) {
1016 /* unmap old base */
8c572eba 1017 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 1018 if (ret) {
3394d22e 1019 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
1020 strerror(errno));
1021 return ret;
1022 }
8c572eba 1023 pos->base = NULL;
0f980a35
MD
1024 }
1025 /* map new base. Need mapping length from header. */
8c572eba 1026 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
0f980a35 1027 MAP_PRIVATE, pos->fd, pos->mmap_offset);
dc48ecad
MD
1028 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
1029 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
0f980a35
MD
1030 pos->offset = 0; /* Position of the packet header */
1031
8c572eba
MD
1032 packet_index.offset = pos->mmap_offset;
1033 packet_index.content_size = 0;
1034 packet_index.packet_size = 0;
75cc2c35
MD
1035 packet_index.timestamp_begin = 0;
1036 packet_index.timestamp_end = 0;
41e82e00 1037 packet_index.events_discarded = 0;
8c572eba 1038
0f980a35 1039 /* read and check header, set stream id (and check) */
2d0bea29 1040 if (file_stream->parent.trace_packet_header) {
0f980a35 1041 /* Read packet header */
2d0bea29 1042 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
c5e74408
MD
1043 if (ret)
1044 return ret;
2d0bea29 1045 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
0f980a35 1046 if (len_index >= 0) {
b1a2f580 1047 struct definition *field;
f4c56ac2 1048 uint64_t magic;
0f980a35 1049
2d0bea29 1050 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
f4c56ac2
MD
1051 magic = get_unsigned_int(field);
1052 if (magic != CTF_MAGIC) {
3394d22e 1053 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
f4c56ac2 1054 magic,
8c572eba
MD
1055 file_stream->pos.packet_index->len,
1056 (ssize_t) pos->mmap_offset);
0f980a35
MD
1057 return -EINVAL;
1058 }
1059 }
1060
1061 /* check uuid */
2d0bea29 1062 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
0f980a35
MD
1063 if (len_index >= 0) {
1064 struct definition_array *defarray;
b1a2f580 1065 struct definition *field;
0f980a35
MD
1066 uint64_t i;
1067 uint8_t uuidval[UUID_LEN];
1068
2d0bea29 1069 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
b1a2f580
MD
1070 assert(field->declaration->id == CTF_TYPE_ARRAY);
1071 defarray = container_of(field, struct definition_array, p);
3838df27 1072 assert(array_len(defarray) == UUID_LEN);
0f980a35
MD
1073
1074 for (i = 0; i < UUID_LEN; i++) {
1075 struct definition *elem;
0f980a35
MD
1076
1077 elem = array_index(defarray, i);
f4c56ac2 1078 uuidval[i] = get_unsigned_int(elem);
0f980a35 1079 }
46322b33 1080 ret = uuid_compare(td->uuid, uuidval);
0f980a35 1081 if (ret) {
3394d22e 1082 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
0f980a35
MD
1083 return -EINVAL;
1084 }
1085 }
1086
1087
2d0bea29 1088 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
0f980a35 1089 if (len_index >= 0) {
b1a2f580 1090 struct definition *field;
0f980a35 1091
2d0bea29 1092 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
f4c56ac2 1093 stream_id = get_unsigned_int(field);
0f980a35
MD
1094 }
1095 }
1096
2d0bea29 1097 if (!first_packet && file_stream->parent.stream_id != stream_id) {
3394d22e 1098 fprintf(stderr, "[error] Stream ID is changing within a stream.\n");
0f980a35
MD
1099 return -EINVAL;
1100 }
1101 if (first_packet) {
2d0bea29 1102 file_stream->parent.stream_id = stream_id;
46322b33 1103 if (stream_id >= td->streams->len) {
3394d22e 1104 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
0f980a35
MD
1105 return -EINVAL;
1106 }
46322b33 1107 stream = g_ptr_array_index(td->streams, stream_id);
0f980a35 1108 if (!stream) {
3394d22e 1109 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
0f980a35
MD
1110 return -EINVAL;
1111 }
2d0bea29 1112 file_stream->parent.stream_class = stream;
01c76b24 1113 ret = create_stream_definitions(td, &file_stream->parent);
862434ec
MD
1114 if (ret)
1115 return ret;
0f980a35
MD
1116 }
1117 first_packet = 0;
1118
2d0bea29 1119 if (file_stream->parent.stream_packet_context) {
dc48ecad 1120 /* Read packet context */
2d0bea29 1121 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
c5e74408
MD
1122 if (ret)
1123 return ret;
dc48ecad 1124 /* read content size from header */
2d0bea29 1125 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
dc48ecad 1126 if (len_index >= 0) {
b1a2f580 1127 struct definition *field;
dc48ecad 1128
2d0bea29 1129 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
f4c56ac2 1130 packet_index.content_size = get_unsigned_int(field);
dc48ecad
MD
1131 } else {
1132 /* Use file size for packet size */
8c572eba 1133 packet_index.content_size = filestats.st_size * CHAR_BIT;
dc48ecad
MD
1134 }
1135
1136 /* read packet size from header */
2d0bea29 1137 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
dc48ecad 1138 if (len_index >= 0) {
b1a2f580 1139 struct definition *field;
dc48ecad 1140
2d0bea29 1141 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
f4c56ac2 1142 packet_index.packet_size = get_unsigned_int(field);
dc48ecad
MD
1143 } else {
1144 /* Use content size if non-zero, else file size */
8c572eba 1145 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
dc48ecad 1146 }
75cc2c35
MD
1147
1148 /* read timestamp begin from header */
2d0bea29 1149 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
75cc2c35 1150 if (len_index >= 0) {
75cc2c35
MD
1151 struct definition *field;
1152
2d0bea29 1153 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
f4c56ac2 1154 packet_index.timestamp_begin = get_unsigned_int(field);
75cc2c35
MD
1155 }
1156
1157 /* read timestamp end from header */
2d0bea29 1158 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
75cc2c35 1159 if (len_index >= 0) {
75cc2c35
MD
1160 struct definition *field;
1161
2d0bea29 1162 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
f4c56ac2 1163 packet_index.timestamp_end = get_unsigned_int(field);
75cc2c35 1164 }
41e82e00
MD
1165
1166 /* read events discarded from header */
1167 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1168 if (len_index >= 0) {
1169 struct definition *field;
1170
1171 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1172 packet_index.events_discarded = get_unsigned_int(field);
1173 }
0f980a35
MD
1174 } else {
1175 /* Use file size for packet size */
8c572eba 1176 packet_index.content_size = filestats.st_size * CHAR_BIT;
0f980a35 1177 /* Use content size if non-zero, else file size */
8c572eba 1178 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
0f980a35 1179 }
546293fa
MD
1180
1181 /* Validate content size and packet size values */
1182 if (packet_index.content_size > packet_index.packet_size) {
3394d22e 1183 fprintf(stderr, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
546293fa
MD
1184 packet_index.content_size, packet_index.packet_size);
1185 return -EINVAL;
1186 }
1187
58b0b883 1188 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
3394d22e 1189 fprintf(stderr, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
5aebb2ee 1190 packet_index.content_size, (size_t) (filestats.st_size - packet_index.offset) * CHAR_BIT);
546293fa
MD
1191 return -EINVAL;
1192 }
1193
847bf71a
MD
1194 /* Save position after header and context */
1195 packet_index.data_offset = pos->offset;
0f980a35 1196
0f980a35
MD
1197 /* add index to packet array */
1198 g_array_append_val(file_stream->pos.packet_index, packet_index);
1199
8c572eba 1200 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
0f980a35
MD
1201 }
1202
847bf71a
MD
1203 /* Move pos back to beginning of file */
1204 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
1205
0f980a35
MD
1206 return 0;
1207}
1208
e28d4618
MD
1209static
1210int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
1211{
1212 int ret;
1213
1214 if (td->packet_header_decl) {
1215 struct definition *definition =
1216 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1217 stream->parent_def_scope, 0, 0, "trace.packet.header");
1218 if (!definition) {
1219 ret = -EINVAL;
1220 goto error;
1221 }
1222 stream->trace_packet_header =
1223 container_of(definition, struct definition_struct, p);
1224 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1225 }
1226
1227 return 0;
1228
1229error:
1230 return ret;
1231}
1232
0f980a35
MD
1233/*
1234 * Note: many file streams can inherit from the same stream class
1235 * description (metadata).
1236 */
1237static
b086c01a
JD
1238int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1239 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1240 int whence))
0f980a35
MD
1241{
1242 int ret;
1243 struct ctf_file_stream *file_stream;
1244
46322b33 1245 ret = openat(td->dirfd, path, flags);
a569a564
MD
1246 if (ret < 0) {
1247 perror("File stream openat()");
0f980a35 1248 goto error;
a569a564 1249 }
0f980a35 1250 file_stream = g_new0(struct ctf_file_stream, 1);
b086c01a
JD
1251
1252 if (move_pos_slow) {
1253 file_stream->pos.move_pos_slow = move_pos_slow;
1254 } else {
1255 fprintf(stderr, "[error] move_pos_slow function undefined.\n");
1256 ret = -1;
1257 goto error_def;
1258 }
1259
8563e754 1260 ctf_init_pos(&file_stream->pos, ret, flags);
2d0bea29 1261 ret = create_trace_definitions(td, &file_stream->parent);
e28d4618
MD
1262 if (ret)
1263 goto error_def;
25ccc85b
MD
1264 /*
1265 * For now, only a single slock is supported.
1266 */
1267 file_stream->parent.current_clock = td->single_clock;
0f980a35
MD
1268 ret = create_stream_packet_index(td, file_stream);
1269 if (ret)
1270 goto error_index;
1271 /* Add stream file to stream class */
2d0bea29
MD
1272 g_ptr_array_add(file_stream->parent.stream_class->streams,
1273 &file_stream->parent);
0f980a35
MD
1274 return 0;
1275
1276error_index:
2d0bea29
MD
1277 if (file_stream->parent.trace_packet_header)
1278 definition_unref(&file_stream->parent.trace_packet_header->p);
e28d4618 1279error_def:
46322b33 1280 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
1281 close(file_stream->pos.fd);
1282 g_free(file_stream);
1283error:
65102a8c
MD
1284 return ret;
1285}
1286
bbefb8dd 1287static
5b80ddfb 1288int ctf_open_trace_read(struct ctf_trace *td,
8c250d87 1289 const char *path, int flags,
b086c01a 1290 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
ae23d232 1291 int whence), FILE *metadata_fp)
bbefb8dd
MD
1292{
1293 int ret;
65102a8c
MD
1294 struct dirent *dirent;
1295 struct dirent *diriter;
1296 size_t dirent_len;
bbefb8dd 1297
46322b33 1298 td->flags = flags;
bbefb8dd
MD
1299
1300 /* Open trace directory */
46322b33
MD
1301 td->dir = opendir(path);
1302 if (!td->dir) {
3394d22e 1303 fprintf(stderr, "[error] Unable to open trace directory.\n");
bbefb8dd
MD
1304 ret = -ENOENT;
1305 goto error;
1306 }
1307
46322b33
MD
1308 td->dirfd = open(path, 0);
1309 if (td->dirfd < 0) {
3394d22e 1310 fprintf(stderr, "[error] Unable to open trace directory file descriptor.\n");
a569a564
MD
1311 perror("Trace directory open");
1312 ret = -errno;
65102a8c
MD
1313 goto error_dirfd;
1314 }
5b80ddfb
MD
1315 strncpy(td->path, path, sizeof(td->path));
1316 td->path[sizeof(td->path) - 1] = '\0';
0f980a35 1317
65102a8c
MD
1318 /*
1319 * Keep the metadata file separate.
1320 */
bbefb8dd 1321
ae23d232 1322 ret = ctf_open_trace_metadata_read(td, move_pos_slow, metadata_fp);
65102a8c
MD
1323 if (ret) {
1324 goto error_metadata;
1325 }
bbefb8dd
MD
1326
1327 /*
1328 * Open each stream: for each file, try to open, check magic
1329 * number, and get the stream ID to add to the right location in
1330 * the stream array.
bbefb8dd
MD
1331 */
1332
65102a8c 1333 dirent_len = offsetof(struct dirent, d_name) +
46322b33 1334 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 1335
65102a8c 1336 dirent = malloc(dirent_len);
bbefb8dd 1337
65102a8c 1338 for (;;) {
46322b33 1339 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 1340 if (ret) {
3394d22e 1341 fprintf(stderr, "[error] Readdir error.\n");
65102a8c 1342 goto readdir_error;
65102a8c
MD
1343 }
1344 if (!diriter)
1345 break;
d8ea2d29
MD
1346 /* Ignore hidden files, ., .. and metadata. */
1347 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
1348 || !strcmp(diriter->d_name, "..")
1349 || !strcmp(diriter->d_name, "metadata"))
1350 continue;
b086c01a 1351 ret = ctf_open_file_stream_read(td, diriter->d_name, flags, move_pos_slow);
dc48ecad 1352 if (ret) {
3394d22e 1353 fprintf(stderr, "[error] Open file stream error.\n");
dc48ecad
MD
1354 goto readdir_error;
1355 }
65102a8c 1356 }
bbefb8dd 1357
65102a8c 1358 free(dirent);
bbefb8dd 1359 return 0;
65102a8c
MD
1360
1361readdir_error:
1362 free(dirent);
1363error_metadata:
46322b33 1364 close(td->dirfd);
65102a8c 1365error_dirfd:
46322b33 1366 closedir(td->dir);
bbefb8dd
MD
1367error:
1368 return ret;
1369}
1370
e9378815 1371static
5b80ddfb 1372struct trace_descriptor *ctf_open_trace(const char *path, int flags,
b086c01a 1373 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
ae23d232 1374 int whence), FILE *metadata_fp)
bbefb8dd 1375{
46322b33 1376 struct ctf_trace *td;
bbefb8dd
MD
1377 int ret;
1378
46322b33 1379 td = g_new0(struct ctf_trace, 1);
bbefb8dd 1380
8c572eba 1381 switch (flags & O_ACCMODE) {
bbefb8dd 1382 case O_RDONLY:
b61922b5 1383 if (!path) {
3394d22e 1384 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
b61922b5
MD
1385 goto error;
1386 }
5b80ddfb 1387 ret = ctf_open_trace_read(td, path, flags, move_pos_slow, metadata_fp);
bbefb8dd
MD
1388 if (ret)
1389 goto error;
1390 break;
989c73bc 1391 case O_RDWR:
3394d22e 1392 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
46322b33 1393 goto error;
bbefb8dd 1394 default:
3394d22e 1395 fprintf(stderr, "[error] Incorrect open flags.\n");
bbefb8dd
MD
1396 goto error;
1397 }
1398
46322b33 1399 return &td->parent;
bbefb8dd
MD
1400error:
1401 g_free(td);
1402 return NULL;
1403}
1404
f571dfb1
JD
1405
1406void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
1407 struct mmap_stream *mmap_info)
1408{
1409 pos->mmap_offset = 0;
1410 pos->packet_size = 0;
1411 pos->content_size = 0;
1412 pos->content_size_loc = NULL;
1413 pos->fd = mmap_info->fd;
1414 pos->base = 0;
1415 pos->offset = 0;
1416 pos->dummy = false;
1417 pos->cur_index = 0;
1418 pos->packet_index = NULL;
1419 pos->prot = PROT_READ;
1420 pos->flags = MAP_PRIVATE;
1421 pos->parent.rw_table = read_dispatch_table;
1422 pos->parent.event_cb = ctf_read_event;
1423}
1424
1425static
1426int prepare_mmap_stream_definition(struct ctf_trace *td,
1427 struct ctf_file_stream *file_stream)
1428{
1429 struct ctf_stream_class *stream;
1430 uint64_t stream_id = 0;
1431 int ret;
1432
1433 file_stream->parent.stream_id = stream_id;
1434 if (stream_id >= td->streams->len) {
3394d22e 1435 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
1436 "in metadata.\n", stream_id);
1437 ret = -EINVAL;
1438 goto end;
1439 }
1440 stream = g_ptr_array_index(td->streams, stream_id);
1441 if (!stream) {
3394d22e 1442 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
1443 "in metadata.\n", stream_id);
1444 ret = -EINVAL;
1445 goto end;
1446 }
1447 file_stream->parent.stream_class = stream;
1448 ret = create_stream_definitions(td, &file_stream->parent);
1449end:
1450 return ret;
1451}
1452
1453static
1454int ctf_open_mmap_stream_read(struct ctf_trace *td,
1455 struct mmap_stream *mmap_info,
1456 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1457 int whence))
1458{
1459 int ret;
1460 struct ctf_file_stream *file_stream;
1461
1462 file_stream = g_new0(struct ctf_file_stream, 1);
1463 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1464
1465 file_stream->pos.move_pos_slow = move_pos_slow;
1466
1467 ret = create_trace_definitions(td, &file_stream->parent);
1468 if (ret) {
1469 goto error_def;
1470 }
1471
1472 ret = prepare_mmap_stream_definition(td, file_stream);
1473 if (ret)
1474 goto error_index;
1475
1476 /* Add stream file to stream class */
1477 g_ptr_array_add(file_stream->parent.stream_class->streams,
1478 &file_stream->parent);
1479 return 0;
1480
1481error_index:
1482 if (file_stream->parent.trace_packet_header)
1483 definition_unref(&file_stream->parent.trace_packet_header->p);
1484error_def:
1485 g_free(file_stream);
1486 return ret;
1487}
1488
1489int ctf_open_mmap_trace_read(struct ctf_trace *td,
1490 struct mmap_stream_list *mmap_list,
1491 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1492 int whence),
1493 FILE *metadata_fp)
1494{
1495 int ret;
1496 struct mmap_stream *mmap_info;
1497
1498 ret = ctf_open_trace_metadata_read(td, ctf_move_pos_slow, metadata_fp);
1499 if (ret) {
1500 goto error;
1501 }
1502
1503 /*
1504 * for each stream, try to open, check magic number, and get the
1505 * stream ID to add to the right location in the stream array.
1506 */
1507 cds_list_for_each_entry(mmap_info, &mmap_list->head, list) {
1508 ret = ctf_open_mmap_stream_read(td, mmap_info, move_pos_slow);
1509 if (ret) {
3394d22e 1510 fprintf(stderr, "[error] Open file mmap stream error.\n");
f571dfb1
JD
1511 goto error;
1512 }
1513 }
1514
1515 return 0;
1516
1517error:
1518 return ret;
1519}
1520
1521static
1522struct trace_descriptor *ctf_open_mmap_trace(
1523 struct mmap_stream_list *mmap_list,
1524 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, int whence),
1525 FILE *metadata_fp)
1526{
1527 struct ctf_trace *td;
1528 int ret;
1529
1530 if (!metadata_fp) {
1531 fprintf(stderr, "[error] No metadata file pointer associated, "
1532 "required for mmap parsing\n");
1533 goto error;
1534 }
1535 if (!move_pos_slow) {
1536 fprintf(stderr, "[error] move_pos_slow function undefined.\n");
1537 goto error;
1538 }
1539 td = g_new0(struct ctf_trace, 1);
1540 ret = ctf_open_mmap_trace_read(td, mmap_list, move_pos_slow, metadata_fp);
1541 if (ret)
1542 goto error_free;
1543
1544 return &td->parent;
1545
1546error_free:
1547 g_free(td);
1548error:
1549 return NULL;
1550}
1551
0f980a35
MD
1552static
1553void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1554{
46322b33 1555 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
1556 close(file_stream->pos.fd);
1557}
1558
e9378815 1559static
46322b33 1560void ctf_close_trace(struct trace_descriptor *tdp)
bbefb8dd 1561{
46322b33 1562 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
0f980a35
MD
1563 int i;
1564
46322b33
MD
1565 if (td->streams) {
1566 for (i = 0; i < td->streams->len; i++) {
aa6bffae 1567 struct ctf_stream_class *stream;
0f980a35 1568 int j;
e9378815 1569
46322b33 1570 stream = g_ptr_array_index(td->streams, i);
e9378815
MD
1571 if (!stream)
1572 continue;
2d0bea29 1573 for (j = 0; j < stream->streams->len; j++) {
0f980a35 1574 struct ctf_file_stream *file_stream;
2d0bea29 1575 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
0f980a35
MD
1576 ctf_close_file_stream(file_stream);
1577 }
1578
1579 }
46322b33 1580 g_ptr_array_free(td->streams, TRUE);
0f980a35 1581 }
46322b33 1582 closedir(td->dir);
bbefb8dd
MD
1583 g_free(td);
1584}
1585
7fb21036 1586void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
1587{
1588 int ret;
1589
4c8bfb7e 1590 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
1591 ret = bt_register_format(&ctf_format);
1592 assert(!ret);
1593}
698f0fe4
MD
1594
1595/* TODO: finalize */
This page took 0.107863 seconds and 4 git commands to generate.