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