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