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