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