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