Fix: ctf.c fscanf missing integer length check
[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.
c462e188
MD
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
fc93b2bd
MD
27 */
28
29#include <babeltrace/format.h>
30#include <babeltrace/ctf/types.h>
bbefb8dd 31#include <babeltrace/ctf/metadata.h>
70bd0a12 32#include <babeltrace/babeltrace-internal.h>
e4195791 33#include <babeltrace/ctf/events-internal.h>
98a04903
JD
34#include <babeltrace/trace-handle-internal.h>
35#include <babeltrace/context-internal.h>
4cb26dfb 36#include <babeltrace/compat/uuid.h>
43e34335 37#include <babeltrace/endian.h>
0f980a35 38#include <inttypes.h>
b4c19c1e 39#include <stdio.h>
0f980a35 40#include <sys/mman.h>
bbefb8dd 41#include <errno.h>
bbefb8dd 42#include <sys/types.h>
65102a8c 43#include <sys/stat.h>
bbefb8dd 44#include <fcntl.h>
65102a8c 45#include <dirent.h>
bbefb8dd 46#include <glib.h>
65102a8c
MD
47#include <unistd.h>
48#include <stdlib.h>
49
65102a8c
MD
50#include "metadata/ctf-scanner.h"
51#include "metadata/ctf-parser.h"
52#include "metadata/ctf-ast.h"
c34ea0fa 53#include "events-private.h"
68ef7952 54#include <babeltrace/compat/memstream.h>
65102a8c 55
ec323464
MD
56#define LOG2_CHAR_BIT 3
57
58/*
59 * Length of first attempt at mapping a packet header, in bits.
60 */
61#define DEFAULT_HEADER_LEN (getpagesize() * CHAR_BIT)
62
0f980a35 63/*
ec323464 64 * Lenght of packet to write, in bits.
0f980a35 65 */
8c572eba 66#define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
0f980a35 67
a0fe7d97
MD
68#ifndef min
69#define min(a, b) (((a) < (b)) ? (a) : (b))
70#endif
71
7d97fad9
MD
72#define NSEC_PER_SEC 1000000000ULL
73
03798a93 74int opt_clock_cycles,
7d97fad9
MD
75 opt_clock_seconds,
76 opt_clock_date,
77 opt_clock_gmt;
78
79uint64_t opt_clock_offset;
65923160 80uint64_t opt_clock_offset_ns;
7d97fad9 81
65102a8c 82extern int yydebug;
bbefb8dd 83
e9378815 84static
1b8455b7 85struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1cf393f6 86 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
20d0dcf9
MD
87 int whence),
88 FILE *metadata_fp);
e9378815 89static
1b8455b7 90struct bt_trace_descriptor *ctf_open_mmap_trace(
c150f912 91 struct bt_mmap_stream_list *mmap_list,
1cf393f6 92 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
20d0dcf9 93 int whence),
f571dfb1 94 FILE *metadata_fp);
98a04903 95static
1b8455b7 96void ctf_set_context(struct bt_trace_descriptor *descriptor,
98a04903
JD
97 struct bt_context *ctx);
98static
1b8455b7 99void ctf_set_handle(struct bt_trace_descriptor *descriptor,
98a04903 100 struct bt_trace_handle *handle);
f571dfb1
JD
101
102static
1b8455b7 103int ctf_close_trace(struct bt_trace_descriptor *descriptor);
30c276af 104static
1b8455b7 105uint64_t ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
03798a93 106 struct bt_trace_handle *handle, enum bt_clock_type type);
30c276af 107static
1b8455b7 108uint64_t ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
03798a93
JD
109 struct bt_trace_handle *handle, enum bt_clock_type type);
110static
1b8455b7 111int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp);
fc93b2bd 112
1ae19169
MD
113static
114rw_dispatch read_dispatch_table[] = {
d11e9c49
MD
115 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
116 [ CTF_TYPE_FLOAT ] = ctf_float_read,
117 [ CTF_TYPE_ENUM ] = ctf_enum_read,
118 [ CTF_TYPE_STRING ] = ctf_string_read,
119 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
120 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
121 [ CTF_TYPE_ARRAY ] = ctf_array_read,
122 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
d11e9c49
MD
123};
124
1ae19169
MD
125static
126rw_dispatch write_dispatch_table[] = {
d11e9c49
MD
127 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
128 [ CTF_TYPE_FLOAT ] = ctf_float_write,
129 [ CTF_TYPE_ENUM ] = ctf_enum_write,
130 [ CTF_TYPE_STRING ] = ctf_string_write,
131 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
132 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
133 [ CTF_TYPE_ARRAY ] = ctf_array_write,
134 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
d11e9c49
MD
135};
136
1ae19169 137static
37b99bdb 138struct bt_format ctf_format = {
bbefb8dd 139 .open_trace = ctf_open_trace,
f571dfb1 140 .open_mmap_trace = ctf_open_mmap_trace,
bbefb8dd 141 .close_trace = ctf_close_trace,
98a04903
JD
142 .set_context = ctf_set_context,
143 .set_handle = ctf_set_handle,
30c276af
JD
144 .timestamp_begin = ctf_timestamp_begin,
145 .timestamp_end = ctf_timestamp_end,
03798a93 146 .convert_index_timestamp = ctf_convert_index_timestamp,
fc93b2bd
MD
147};
148
30c276af 149static
1b8455b7 150uint64_t ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
03798a93 151 struct bt_trace_handle *handle, enum bt_clock_type type)
30c276af
JD
152{
153 struct ctf_trace *tin;
154 uint64_t begin = ULLONG_MAX;
155 int i, j;
156
157 tin = container_of(descriptor, struct ctf_trace, parent);
158
159 if (!tin)
160 goto error;
161
162 /* for each stream_class */
163 for (i = 0; i < tin->streams->len; i++) {
164 struct ctf_stream_declaration *stream_class;
165
166 stream_class = g_ptr_array_index(tin->streams, i);
1b01ffc2
MD
167 if (!stream_class)
168 continue;
30c276af
JD
169 /* for each file_stream */
170 for (j = 0; j < stream_class->streams->len; j++) {
171 struct ctf_stream_definition *stream;
172 struct ctf_file_stream *cfs;
173 struct ctf_stream_pos *stream_pos;
174 struct packet_index *index;
175
176 stream = g_ptr_array_index(stream_class->streams, j);
177 cfs = container_of(stream, struct ctf_file_stream,
178 parent);
179 stream_pos = &cfs->pos;
180
03798a93
JD
181 if (!stream_pos->packet_real_index)
182 goto error;
183
afe9cd4a
JD
184 if (stream_pos->packet_real_index->len <= 0)
185 continue;
186
03798a93
JD
187 if (type == BT_CLOCK_REAL) {
188 index = &g_array_index(stream_pos->packet_real_index,
189 struct packet_index,
190 stream_pos->packet_real_index->len - 1);
191 } else if (type == BT_CLOCK_CYCLES) {
192 index = &g_array_index(stream_pos->packet_cycles_index,
193 struct packet_index,
194 stream_pos->packet_real_index->len - 1);
195
196 } else {
197 goto error;
198 }
30c276af
JD
199 if (index->timestamp_begin < begin)
200 begin = index->timestamp_begin;
201 }
202 }
203
204 return begin;
205
206error:
207 return -1ULL;
208}
209
210static
1b8455b7 211uint64_t ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
03798a93 212 struct bt_trace_handle *handle, enum bt_clock_type type)
30c276af
JD
213{
214 struct ctf_trace *tin;
215 uint64_t end = 0;
216 int i, j;
217
218 tin = container_of(descriptor, struct ctf_trace, parent);
219
220 if (!tin)
221 goto error;
222
223 /* for each stream_class */
224 for (i = 0; i < tin->streams->len; i++) {
225 struct ctf_stream_declaration *stream_class;
226
227 stream_class = g_ptr_array_index(tin->streams, i);
1b01ffc2
MD
228 if (!stream_class)
229 continue;
30c276af
JD
230 /* for each file_stream */
231 for (j = 0; j < stream_class->streams->len; j++) {
232 struct ctf_stream_definition *stream;
233 struct ctf_file_stream *cfs;
234 struct ctf_stream_pos *stream_pos;
235 struct packet_index *index;
236
237 stream = g_ptr_array_index(stream_class->streams, j);
238 cfs = container_of(stream, struct ctf_file_stream,
239 parent);
240 stream_pos = &cfs->pos;
241
03798a93
JD
242 if (!stream_pos->packet_real_index)
243 goto error;
244
afe9cd4a
JD
245 if (stream_pos->packet_real_index->len <= 0)
246 continue;
247
03798a93
JD
248 if (type == BT_CLOCK_REAL) {
249 index = &g_array_index(stream_pos->packet_real_index,
250 struct packet_index,
251 stream_pos->packet_real_index->len - 1);
252 } else if (type == BT_CLOCK_CYCLES) {
253 index = &g_array_index(stream_pos->packet_cycles_index,
254 struct packet_index,
255 stream_pos->packet_real_index->len - 1);
256
257 } else {
258 goto error;
259 }
30c276af
JD
260 if (index->timestamp_end > end)
261 end = index->timestamp_end;
262 }
263 }
264
265 return end;
266
267error:
268 return -1ULL;
269}
270
25ccc85b 271/*
03798a93 272 * Update stream current timestamp
25ccc85b 273 */
2b9a764d 274static
9e88d150 275void ctf_update_timestamp(struct ctf_stream_definition *stream,
2b9a764d
MD
276 struct definition_integer *integer_definition)
277{
278 struct declaration_integer *integer_declaration =
279 integer_definition->declaration;
280 uint64_t oldval, newval, updateval;
281
7f3f572b 282 if (unlikely(integer_declaration->len == 64)) {
03798a93
JD
283 stream->prev_cycles_timestamp = stream->cycles_timestamp;
284 stream->cycles_timestamp = integer_definition->value._unsigned;
285 stream->prev_real_timestamp = ctf_get_real_timestamp(stream,
286 stream->prev_cycles_timestamp);
287 stream->real_timestamp = ctf_get_real_timestamp(stream,
288 stream->cycles_timestamp);
2b9a764d
MD
289 return;
290 }
291 /* keep low bits */
03798a93 292 oldval = stream->cycles_timestamp;
2b9a764d
MD
293 oldval &= (1ULL << integer_declaration->len) - 1;
294 newval = integer_definition->value._unsigned;
295 /* Test for overflow by comparing low bits */
296 if (newval < oldval)
297 newval += 1ULL << integer_declaration->len;
298 /* updateval contains old high bits, and new low bits (sum) */
03798a93 299 updateval = stream->cycles_timestamp;
2b9a764d
MD
300 updateval &= ~((1ULL << integer_declaration->len) - 1);
301 updateval += newval;
03798a93
JD
302 stream->prev_cycles_timestamp = stream->cycles_timestamp;
303 stream->cycles_timestamp = updateval;
304
305 /* convert to real timestamp */
306 stream->prev_real_timestamp = ctf_get_real_timestamp(stream,
307 stream->prev_cycles_timestamp);
308 stream->real_timestamp = ctf_get_real_timestamp(stream,
309 stream->cycles_timestamp);
2b9a764d
MD
310}
311
25ccc85b
MD
312/*
313 * Print timestamp, rescaling clock frequency to nanoseconds and
314 * applying offsets as needed (unix time).
315 */
2e937fb4 316static
03798a93 317void ctf_print_timestamp_real(FILE *fp,
9e88d150 318 struct ctf_stream_definition *stream,
7d97fad9
MD
319 uint64_t timestamp)
320{
321 uint64_t ts_sec = 0, ts_nsec;
7d97fad9 322
03798a93 323 ts_nsec = timestamp;
7d97fad9 324
65923160
IJ
325 /* Add command-line offset in ns*/
326 ts_nsec += opt_clock_offset_ns;
327
c34ea0fa 328 /* Add command-line offset */
7d97fad9
MD
329 ts_sec += opt_clock_offset;
330
331 ts_sec += ts_nsec / NSEC_PER_SEC;
332 ts_nsec = ts_nsec % NSEC_PER_SEC;
333
334 if (!opt_clock_seconds) {
335 struct tm tm;
336 time_t time_s = (time_t) ts_sec;
337
338 if (!opt_clock_gmt) {
339 struct tm *res;
340
341 res = localtime_r(&time_s, &tm);
342 if (!res) {
343 fprintf(stderr, "[warning] Unable to get localtime.\n");
344 goto seconds;
345 }
346 } else {
347 struct tm *res;
348
349 res = gmtime_r(&time_s, &tm);
350 if (!res) {
351 fprintf(stderr, "[warning] Unable to get gmtime.\n");
352 goto seconds;
353 }
354 }
355 if (opt_clock_date) {
356 char timestr[26];
357 size_t res;
358
359 /* Print date and time */
360 res = strftime(timestr, sizeof(timestr),
361 "%F ", &tm);
362 if (!res) {
363 fprintf(stderr, "[warning] Unable to print ascii time.\n");
364 goto seconds;
365 }
366 fprintf(fp, "%s", timestr);
367 }
368 /* Print time in HH:MM:SS.ns */
369 fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
370 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
371 goto end;
372 }
373seconds:
374 fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
375 ts_sec, ts_nsec);
376
377end:
378 return;
379}
380
03798a93
JD
381/*
382 * Print timestamp, in cycles
383 */
2e937fb4 384static
03798a93
JD
385void ctf_print_timestamp_cycles(FILE *fp,
386 struct ctf_stream_definition *stream,
387 uint64_t timestamp)
388{
389 fprintf(fp, "%020" PRIu64, timestamp);
390}
391
392void ctf_print_timestamp(FILE *fp,
393 struct ctf_stream_definition *stream,
394 uint64_t timestamp)
395{
396 if (opt_clock_cycles) {
397 ctf_print_timestamp_cycles(fp, stream, timestamp);
398 } else {
399 ctf_print_timestamp_real(fp, stream, timestamp);
400 }
401}
402
87148dc7
MD
403static
404void print_uuid(FILE *fp, unsigned char *uuid)
405{
406 int i;
407
408 for (i = 0; i < BABELTRACE_UUID_LEN; i++)
409 fprintf(fp, "%x", (unsigned int) uuid[i]);
410}
411
c829a65c
MD
412void ctf_print_discarded(FILE *fp, struct ctf_stream_definition *stream,
413 int end_stream)
87148dc7 414{
c829a65c
MD
415 fprintf(fp, "[warning] Tracer discarded %" PRIu64 " events %sbetween [",
416 stream->events_discarded,
417 end_stream ? "at end of stream " : "");
87148dc7
MD
418 if (opt_clock_cycles) {
419 ctf_print_timestamp(fp, stream,
420 stream->prev_cycles_timestamp);
421 fprintf(fp, "] and [");
422 ctf_print_timestamp(fp, stream,
423 stream->prev_cycles_timestamp_end);
424 } else {
425 ctf_print_timestamp(fp, stream,
426 stream->prev_real_timestamp);
427 fprintf(fp, "] and [");
428 ctf_print_timestamp(fp, stream,
429 stream->prev_real_timestamp_end);
430 }
431 fprintf(fp, "] in trace UUID ");
432 print_uuid(fp, stream->stream_class->trace->uuid);
caf929fa 433 if (stream->stream_class->trace->parent.path[0])
87148dc7 434 fprintf(fp, ", at path: \"%s\"",
caf929fa 435 stream->stream_class->trace->parent.path);
87148dc7
MD
436
437 fprintf(fp, ", within stream id %" PRIu64, stream->stream_id);
438 if (stream->path[0])
439 fprintf(fp, ", at relative path: \"%s\"", stream->path);
440 fprintf(fp, ". ");
441 fprintf(fp, "You should consider recording a new trace with larger "
442 "buffers or with fewer events enabled.\n");
443 fflush(fp);
444}
445
31262354 446static
1cf393f6 447int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream)
31262354
MD
448{
449 struct ctf_stream_pos *pos =
450 container_of(ppos, struct ctf_stream_pos, parent);
f380e105 451 struct ctf_stream_declaration *stream_class = stream->stream_class;
c716f83b 452 struct ctf_event_definition *event;
31262354 453 uint64_t id = 0;
31262354
MD
454 int ret;
455
3abe83c7
MD
456 /* We need to check for EOF here for empty files. */
457 if (unlikely(pos->offset == EOF))
458 return EOF;
459
5f643ed7
MD
460 ctf_pos_get_event(pos);
461
90fcbacc
JD
462 /* save the current position as a restore point */
463 pos->last_offset = pos->offset;
90fcbacc 464
3abe83c7
MD
465 /*
466 * This is the EOF check after we've advanced the position in
467 * ctf_pos_get_event.
468 */
7f3f572b 469 if (unlikely(pos->offset == EOF))
31262354 470 return EOF;
5f643ed7 471 assert(pos->offset < pos->content_size);
31262354
MD
472
473 /* Read event header */
7f3f572b 474 if (likely(stream->stream_event_header)) {
a35173fe 475 struct definition_integer *integer_definition;
0d69b916 476 struct bt_definition *variant;
a35173fe 477
e28d4618 478 ret = generic_rw(ppos, &stream->stream_event_header->p);
7f3f572b 479 if (unlikely(ret))
31262354
MD
480 goto error;
481 /* lookup event id */
bf78e2cf 482 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
483 if (integer_definition) {
484 id = integer_definition->value._unsigned;
485 } else {
486 struct definition_enum *enum_definition;
487
9e3274b0 488 enum_definition = bt_lookup_enum(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
489 if (enum_definition) {
490 id = enum_definition->integer->value._unsigned;
491 }
31262354 492 }
764af3f4 493
ebae302b 494 variant = bt_lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e 495 if (variant) {
bf78e2cf 496 integer_definition = bt_lookup_integer(variant, "id", FALSE);
ccdb988e
MD
497 if (integer_definition) {
498 id = integer_definition->value._unsigned;
499 }
500 }
c87a8eb2 501 stream->event_id = id;
ccdb988e 502
764af3f4 503 /* lookup timestamp */
5e2eb0ae 504 stream->has_timestamp = 0;
bf78e2cf 505 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
a35173fe 506 if (integer_definition) {
2b9a764d 507 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 508 stream->has_timestamp = 1;
a35173fe 509 } else {
ccdb988e 510 if (variant) {
bf78e2cf 511 integer_definition = bt_lookup_integer(variant, "timestamp", FALSE);
a35173fe 512 if (integer_definition) {
2b9a764d 513 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 514 stream->has_timestamp = 1;
a35173fe
MD
515 }
516 }
764af3f4 517 }
31262354
MD
518 }
519
520 /* Read stream-declared event context */
e28d4618
MD
521 if (stream->stream_event_context) {
522 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
523 if (ret)
524 goto error;
525 }
526
7f3f572b 527 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 528 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
529 return -EINVAL;
530 }
e28d4618 531 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 532 if (unlikely(!event)) {
3394d22e 533 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
534 return -EINVAL;
535 }
536
537 /* Read event-declared event context */
e28d4618
MD
538 if (event->event_context) {
539 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
540 if (ret)
541 goto error;
542 }
543
544 /* Read event payload */
7f3f572b 545 if (likely(event->event_fields)) {
e28d4618 546 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
547 if (ret)
548 goto error;
549 }
550
1c7ddc31
MD
551 if (pos->last_offset == pos->offset) {
552 fprintf(stderr, "[error] Invalid 0 byte event encountered.\n");
553 return -EINVAL;
554 }
555
31262354
MD
556 return 0;
557
558error:
4d251e22 559 fprintf(stderr, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
31262354
MD
560 return ret;
561}
562
563static
1cf393f6 564int ctf_write_event(struct bt_stream_pos *pos, struct ctf_stream_definition *stream)
31262354 565{
f380e105 566 struct ctf_stream_declaration *stream_class = stream->stream_class;
c716f83b 567 struct ctf_event_definition *event;
c87a8eb2 568 uint64_t id;
31262354
MD
569 int ret;
570
c87a8eb2
MD
571 id = stream->event_id;
572
31262354 573 /* print event header */
7f3f572b 574 if (likely(stream->stream_event_header)) {
e28d4618 575 ret = generic_rw(pos, &stream->stream_event_header->p);
31262354
MD
576 if (ret)
577 goto error;
578 }
579
580 /* print stream-declared event context */
e28d4618
MD
581 if (stream->stream_event_context) {
582 ret = generic_rw(pos, &stream->stream_event_context->p);
31262354
MD
583 if (ret)
584 goto error;
585 }
586
7f3f572b 587 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 588 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
589 return -EINVAL;
590 }
e28d4618 591 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 592 if (unlikely(!event)) {
3394d22e 593 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
594 return -EINVAL;
595 }
596
597 /* print event-declared event context */
e28d4618
MD
598 if (event->event_context) {
599 ret = generic_rw(pos, &event->event_context->p);
31262354
MD
600 if (ret)
601 goto error;
602 }
603
604 /* Read and print event payload */
7f3f572b 605 if (likely(event->event_fields)) {
e28d4618 606 ret = generic_rw(pos, &event->event_fields->p);
31262354
MD
607 if (ret)
608 goto error;
609 }
610
611 return 0;
612
613error:
3394d22e 614 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
615 return ret;
616}
617
ca334c72
MD
618int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
619 int fd, int open_flags)
8c572eba
MD
620{
621 pos->fd = fd;
37fcdd19 622 if (fd >= 0) {
03798a93 623 pos->packet_cycles_index = g_array_new(FALSE, TRUE,
8563e754 624 sizeof(struct packet_index));
37fcdd19
JD
625 pos->packet_real_index = g_array_new(FALSE, TRUE,
626 sizeof(struct packet_index));
627 } else {
03798a93 628 pos->packet_cycles_index = NULL;
37fcdd19
JD
629 pos->packet_real_index = NULL;
630 }
8563e754
MD
631 switch (open_flags & O_ACCMODE) {
632 case O_RDONLY:
633 pos->prot = PROT_READ;
634 pos->flags = MAP_PRIVATE;
635 pos->parent.rw_table = read_dispatch_table;
31262354 636 pos->parent.event_cb = ctf_read_event;
ca334c72 637 pos->parent.trace = trace;
8563e754 638 break;
8563e754
MD
639 case O_RDWR:
640 pos->prot = PROT_WRITE; /* Write has priority */
641 pos->flags = MAP_SHARED;
642 pos->parent.rw_table = write_dispatch_table;
31262354 643 pos->parent.event_cb = ctf_write_event;
ca334c72 644 pos->parent.trace = trace;
8563e754 645 if (fd >= 0)
06789ffd 646 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
8563e754
MD
647 break;
648 default:
649 assert(0);
8c572eba 650 }
f824ae04 651 return 0;
8c572eba
MD
652}
653
f824ae04 654int ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba 655{
8c572eba
MD
656 if (pos->prot == PROT_WRITE && pos->content_size_loc)
657 *pos->content_size_loc = pos->offset;
aee35fcc 658 if (pos->base_mma) {
08c82b90
MD
659 int ret;
660
8c572eba 661 /* unmap old base */
aee35fcc 662 ret = munmap_align(pos->base_mma);
8c572eba 663 if (ret) {
3394d22e 664 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
8c572eba 665 strerror(errno));
f824ae04 666 return -1;
8c572eba
MD
667 }
668 }
37fcdd19
JD
669 if (pos->packet_cycles_index)
670 (void) g_array_free(pos->packet_cycles_index, TRUE);
671 if (pos->packet_real_index)
672 (void) g_array_free(pos->packet_real_index, TRUE);
f824ae04 673 return 0;
8c572eba
MD
674}
675
20d0dcf9
MD
676/*
677 * for SEEK_CUR: go to next packet.
f5a60aad 678 * for SEEK_SET: go to packet numer (index).
20d0dcf9 679 */
1cf393f6 680void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
0f980a35 681{
d6425aaf
MD
682 struct ctf_stream_pos *pos =
683 container_of(stream_pos, struct ctf_stream_pos, parent);
75cc2c35
MD
684 struct ctf_file_stream *file_stream =
685 container_of(pos, struct ctf_file_stream, pos);
0f980a35 686 int ret;
8c572eba 687 off_t off;
20d0dcf9 688 struct packet_index *packet_index;
0f980a35 689
5bfcad93
MD
690 switch (whence) {
691 case SEEK_CUR:
692 case SEEK_SET: /* Fall-through */
693 break; /* OK */
694 default:
695 assert(0);
696 }
697
8c572eba
MD
698 if (pos->prot == PROT_WRITE && pos->content_size_loc)
699 *pos->content_size_loc = pos->offset;
0f980a35 700
aee35fcc 701 if (pos->base_mma) {
0f980a35 702 /* unmap old base */
aee35fcc 703 ret = munmap_align(pos->base_mma);
0f980a35 704 if (ret) {
3394d22e 705 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
706 strerror(errno));
707 assert(0);
708 }
aee35fcc 709 pos->base_mma = NULL;
0f980a35
MD
710 }
711
8c572eba 712 /*
46322b33 713 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
714 * except to get exactly at the beginning of the next packet.
715 */
716 if (pos->prot == PROT_WRITE) {
989c73bc
MD
717 switch (whence) {
718 case SEEK_CUR:
719 /* The writer will add padding */
61bd6060 720 pos->mmap_offset += pos->packet_size / CHAR_BIT;
989c73bc
MD
721 break;
722 case SEEK_SET:
20d0dcf9 723 assert(index == 0); /* only seek supported for now */
989c73bc
MD
724 pos->cur_index = 0;
725 break;
726 default:
727 assert(0);
728 }
8c572eba
MD
729 pos->content_size = -1U; /* Unknown at this point */
730 pos->packet_size = WRITE_PACKET_LEN;
989c73bc
MD
731 off = posix_fallocate(pos->fd, pos->mmap_offset,
732 pos->packet_size / CHAR_BIT);
8c572eba 733 assert(off >= 0);
847bf71a 734 pos->offset = 0;
8c572eba 735 } else {
5f643ed7 736 read_next_packet:
847bf71a
MD
737 switch (whence) {
738 case SEEK_CUR:
41e82e00 739 {
4c4ba021 740 uint64_t events_discarded_diff;
41e82e00 741
7d97fad9
MD
742 if (pos->offset == EOF) {
743 return;
744 }
c309df1c
MD
745 assert(pos->cur_index < pos->packet_cycles_index->len);
746 assert(pos->cur_index < pos->packet_real_index->len);
747
3217ac37 748 /* For printing discarded event count */
03798a93 749 packet_index = &g_array_index(pos->packet_cycles_index,
41e82e00 750 struct packet_index, pos->cur_index);
03798a93
JD
751 file_stream->parent.prev_cycles_timestamp_end =
752 packet_index->timestamp_end;
753 file_stream->parent.prev_cycles_timestamp =
754 packet_index->timestamp_begin;
755
756 packet_index = &g_array_index(pos->packet_real_index,
757 struct packet_index, pos->cur_index);
758 file_stream->parent.prev_real_timestamp_end =
20d0dcf9 759 packet_index->timestamp_end;
03798a93
JD
760 file_stream->parent.prev_real_timestamp =
761 packet_index->timestamp_begin;
762
763 events_discarded_diff = packet_index->events_discarded;
41e82e00 764 if (pos->cur_index > 0) {
03798a93 765 packet_index = &g_array_index(pos->packet_real_index,
41e82e00
MD
766 struct packet_index,
767 pos->cur_index - 1);
20d0dcf9 768 events_discarded_diff -= packet_index->events_discarded;
4c4ba021
MD
769 /*
770 * Deal with 32-bit wrap-around if the
771 * tracer provided a 32-bit field.
772 */
773 if (packet_index->events_discarded_len == 32) {
774 events_discarded_diff = (uint32_t) events_discarded_diff;
775 }
41e82e00 776 }
fca04958 777 file_stream->parent.events_discarded = events_discarded_diff;
03798a93
JD
778 file_stream->parent.prev_real_timestamp = file_stream->parent.real_timestamp;
779 file_stream->parent.prev_cycles_timestamp = file_stream->parent.cycles_timestamp;
847bf71a 780 /* The reader will expect us to skip padding */
8c572eba 781 ++pos->cur_index;
847bf71a 782 break;
41e82e00 783 }
847bf71a 784 case SEEK_SET:
c309df1c
MD
785 if (index >= pos->packet_cycles_index->len) {
786 pos->offset = EOF;
787 return;
788 }
f60efc0e
JD
789 packet_index = &g_array_index(pos->packet_cycles_index,
790 struct packet_index, index);
791 pos->last_events_discarded = packet_index->events_discarded;
20d0dcf9 792 pos->cur_index = index;
03798a93
JD
793 file_stream->parent.prev_real_timestamp = 0;
794 file_stream->parent.prev_real_timestamp_end = 0;
795 file_stream->parent.prev_cycles_timestamp = 0;
796 file_stream->parent.prev_cycles_timestamp_end = 0;
847bf71a
MD
797 break;
798 default:
799 assert(0);
800 }
03798a93 801 if (pos->cur_index >= pos->packet_real_index->len) {
7d97fad9 802 /*
87148dc7
MD
803 * We need to check if we are in trace read or
804 * called from packet indexing. In this last
805 * case, the collection is not there, so we
806 * cannot print the timestamps.
7d97fad9 807 */
4c62e2d8 808 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
badea1a2 809 /*
87148dc7
MD
810 * When a stream reaches the end of the
811 * file, we need to show the number of
812 * events discarded ourselves, because
813 * there is no next event scheduled to
814 * be printed in the output.
badea1a2 815 */
87148dc7 816 if (file_stream->parent.events_discarded) {
badea1a2 817 fflush(stdout);
c829a65c
MD
818 ctf_print_discarded(stderr,
819 &file_stream->parent,
820 1);
87148dc7 821 file_stream->parent.events_discarded = 0;
badea1a2 822 }
7d97fad9 823 }
670977d3 824 pos->offset = EOF;
847bf71a
MD
825 return;
826 }
03798a93
JD
827 packet_index = &g_array_index(pos->packet_cycles_index,
828 struct packet_index,
829 pos->cur_index);
830 file_stream->parent.cycles_timestamp = packet_index->timestamp_begin;
831
832 packet_index = &g_array_index(pos->packet_real_index,
833 struct packet_index,
834 pos->cur_index);
835 file_stream->parent.real_timestamp = packet_index->timestamp_begin;
20d0dcf9 836 pos->mmap_offset = packet_index->offset;
8c572eba
MD
837
838 /* Lookup context/packet size in index */
20d0dcf9
MD
839 pos->content_size = packet_index->content_size;
840 pos->packet_size = packet_index->packet_size;
841 if (packet_index->data_offset < packet_index->content_size) {
75cc2c35 842 pos->offset = 0; /* will read headers */
20d0dcf9 843 } else if (packet_index->data_offset == packet_index->content_size) {
5f643ed7 844 /* empty packet */
20d0dcf9 845 pos->offset = packet_index->data_offset;
3abe83c7 846 whence = SEEK_CUR;
5f643ed7 847 goto read_next_packet;
7eda6fc7 848 } else {
2b9a764d
MD
849 pos->offset = EOF;
850 return;
851 }
8c572eba 852 }
0f980a35 853 /* map new base. Need mapping length from header. */
aee35fcc
MD
854 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
855 pos->flags, pos->fd, pos->mmap_offset);
856 if (pos->base_mma == MAP_FAILED) {
3394d22e 857 fprintf(stderr, "[error] mmap error %s.\n",
847bf71a
MD
858 strerror(errno));
859 assert(0);
860 }
75cc2c35
MD
861
862 /* update trace_packet_header and stream_packet_context */
2d0bea29 863 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
75cc2c35 864 /* Read packet header */
2d0bea29 865 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
75cc2c35
MD
866 assert(!ret);
867 }
2d0bea29 868 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
75cc2c35 869 /* Read packet context */
2d0bea29 870 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
75cc2c35
MD
871 assert(!ret);
872 }
0f980a35
MD
873}
874
b4c19c1e
MD
875static
876int packet_metadata(struct ctf_trace *td, FILE *fp)
877{
878 uint32_t magic;
879 size_t len;
880 int ret = 0;
881
882 len = fread(&magic, sizeof(magic), 1, fp);
a0fe7d97 883 if (len != 1) {
b4c19c1e
MD
884 goto end;
885 }
886 if (magic == TSDL_MAGIC) {
887 ret = 1;
888 td->byte_order = BYTE_ORDER;
0d336fdf 889 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
890 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
891 ret = 1;
892 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
893 LITTLE_ENDIAN : BIG_ENDIAN;
0d336fdf 894 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
895 }
896end:
897 rewind(fp);
898 return ret;
899}
900
5c262147
MD
901/*
902 * Returns 0 on success, -1 on error.
903 */
904static
905int check_version(unsigned int major, unsigned int minor)
906{
907 switch (major) {
908 case 1:
909 switch (minor) {
910 case 8:
911 return 0;
912 default:
913 goto warning;
914 }
915 default:
916 goto warning;
917
918 }
919
920 /* eventually return an error instead of warning */
921warning:
3394d22e 922 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
5c262147
MD
923 major, minor);
924 return 0;
925}
926
b4c19c1e
MD
927static
928int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
929 FILE *out)
930{
931 struct metadata_packet_header header;
a0fe7d97 932 size_t readlen, writelen, toread;
f8254867 933 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
b4c19c1e
MD
934 int ret = 0;
935
a91a962e 936 readlen = fread(&header, header_sizeof(header), 1, in);
a0fe7d97 937 if (readlen < 1)
b4c19c1e
MD
938 return -EINVAL;
939
940 if (td->byte_order != BYTE_ORDER) {
941 header.magic = GUINT32_SWAP_LE_BE(header.magic);
942 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
943 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
944 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
945 }
946 if (header.checksum)
3394d22e 947 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
b4c19c1e 948 if (header.compression_scheme) {
3394d22e 949 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
b4c19c1e
MD
950 header.compression_scheme);
951 return -EINVAL;
952 }
953 if (header.encryption_scheme) {
3394d22e 954 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
b4c19c1e
MD
955 header.encryption_scheme);
956 return -EINVAL;
957 }
958 if (header.checksum_scheme) {
3394d22e 959 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
b4c19c1e
MD
960 header.checksum_scheme);
961 return -EINVAL;
962 }
5c262147
MD
963 if (check_version(header.major, header.minor) < 0)
964 return -EINVAL;
b4c19c1e
MD
965 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
966 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
967 CTF_TRACE_SET_FIELD(td, uuid);
968 } else {
43e34335 969 if (babeltrace_uuid_compare(header.uuid, td->uuid))
b4c19c1e
MD
970 return -EINVAL;
971 }
972
b1ccd079
MD
973 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
974 return -EINVAL;
975
255b2138 976 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
a0fe7d97
MD
977
978 for (;;) {
f8254867 979 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
b4c19c1e
MD
980 if (ferror(in)) {
981 ret = -EINVAL;
982 break;
983 }
4152822b 984 if (babeltrace_debug) {
f8254867 985 buf[readlen] = '\0';
3394d22e 986 fprintf(stderr, "[debug] metadata packet read: %s\n",
4152822b
MD
987 buf);
988 }
989
b4c19c1e
MD
990 writelen = fwrite(buf, sizeof(char), readlen, out);
991 if (writelen < readlen) {
992 ret = -EIO;
993 break;
994 }
995 if (ferror(out)) {
996 ret = -EINVAL;
997 break;
998 }
a0fe7d97
MD
999 toread -= readlen;
1000 if (!toread) {
7f4b5c4d 1001 ret = 0; /* continue reading next packet */
ddbc52af 1002 goto read_padding;
a0fe7d97 1003 }
b4c19c1e
MD
1004 }
1005 return ret;
ddbc52af
MD
1006
1007read_padding:
1008 toread = (header.packet_size - header.content_size) / CHAR_BIT;
1009 ret = fseek(in, toread, SEEK_CUR);
1010 if (ret < 0) {
3394d22e 1011 fprintf(stderr, "[warning] Missing padding at end of file\n");
ddbc52af
MD
1012 ret = 0;
1013 }
1014 return ret;
b4c19c1e
MD
1015}
1016
1017static
1018int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
1019 char **buf)
1020{
1021 FILE *in, *out;
abc40d24 1022 size_t size, buflen;
b4c19c1e
MD
1023 int ret;
1024
1025 in = *fp;
c4f5487e
MD
1026 /*
1027 * Using strlen on *buf instead of size of open_memstream
1028 * because its size includes garbage at the end (after final
1029 * \0). This is the allocated size, not the actual string size.
1030 */
f8370579 1031 out = babeltrace_open_memstream(buf, &size);
a569a564
MD
1032 if (out == NULL) {
1033 perror("Metadata open_memstream");
b4c19c1e 1034 return -errno;
a569a564 1035 }
b4c19c1e
MD
1036 for (;;) {
1037 ret = ctf_open_trace_metadata_packet_read(td, in, out);
7f4b5c4d 1038 if (ret) {
b4c19c1e 1039 break;
7f4b5c4d
MD
1040 }
1041 if (feof(in)) {
1042 ret = 0;
b4c19c1e
MD
1043 break;
1044 }
1045 }
f8370579
MD
1046 /* close to flush the buffer */
1047 ret = babeltrace_close_memstream(buf, &size, out);
1048 if (ret < 0) {
f824ae04
MD
1049 int closeret;
1050
f8370579 1051 perror("babeltrace_flush_memstream");
f824ae04
MD
1052 ret = -errno;
1053 closeret = fclose(in);
1054 if (closeret) {
1055 perror("Error in fclose");
1056 }
1057 return ret;
1058 }
1059 ret = fclose(in);
1060 if (ret) {
1061 perror("Error in fclose");
f8370579 1062 }
b4c19c1e 1063 /* open for reading */
abc40d24
MD
1064 buflen = strlen(*buf);
1065 if (!buflen) {
1066 *fp = NULL;
493330cb 1067 return -ENOENT;
abc40d24
MD
1068 }
1069 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
a569a564
MD
1070 if (!*fp) {
1071 perror("Metadata fmemopen");
1072 return -errno;
1073 }
b4c19c1e
MD
1074 return 0;
1075}
1076
65102a8c 1077static
b086c01a 1078int ctf_open_trace_metadata_read(struct ctf_trace *td,
1cf393f6 1079 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 1080 int whence), FILE *metadata_fp)
65102a8c
MD
1081{
1082 struct ctf_scanner *scanner;
2d0bea29 1083 struct ctf_file_stream *metadata_stream;
65102a8c 1084 FILE *fp;
b4c19c1e 1085 char *buf = NULL;
f824ae04 1086 int ret = 0, closeret;
65102a8c 1087
2d0bea29 1088 metadata_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 1089 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
b086c01a 1090
06789ffd
MD
1091 if (packet_seek) {
1092 metadata_stream->pos.packet_seek = packet_seek;
b086c01a 1093 } else {
06789ffd 1094 fprintf(stderr, "[error] packet_seek function undefined.\n");
b086c01a 1095 ret = -1;
f824ae04 1096 goto end_free;
b086c01a
JD
1097 }
1098
ae23d232
JD
1099 if (metadata_fp) {
1100 fp = metadata_fp;
bcbfb8bf 1101 metadata_stream->pos.fd = -1;
ae23d232
JD
1102 } else {
1103 td->metadata = &metadata_stream->parent;
1104 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1105 if (metadata_stream->pos.fd < 0) {
3394d22e 1106 fprintf(stderr, "Unable to open metadata.\n");
f824ae04
MD
1107 ret = -1;
1108 goto end_free;
ae23d232 1109 }
65102a8c 1110
ae23d232
JD
1111 fp = fdopen(metadata_stream->pos.fd, "r");
1112 if (!fp) {
3394d22e 1113 fprintf(stderr, "[error] Unable to open metadata stream.\n");
a569a564 1114 perror("Metadata stream open");
ae23d232
JD
1115 ret = -errno;
1116 goto end_stream;
1117 }
f824ae04
MD
1118 /* fd now belongs to fp */
1119 metadata_stream->pos.fd = -1;
ae23d232 1120 }
65102a8c
MD
1121 if (babeltrace_debug)
1122 yydebug = 1;
1123
b4c19c1e
MD
1124 if (packet_metadata(td, fp)) {
1125 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
abc40d24
MD
1126 if (ret) {
1127 /* Warn about empty metadata */
1128 fprintf(stderr, "[warning] Empty metadata.\n");
b4c19c1e 1129 goto end_packet_read;
abc40d24 1130 }
7237592a
MD
1131 td->metadata_string = buf;
1132 td->metadata_packetized = 1;
da75b0f7 1133 } else {
5c262147
MD
1134 unsigned int major, minor;
1135 ssize_t nr_items;
8c2df3f5 1136
da75b0f7 1137 td->byte_order = BYTE_ORDER;
8c2df3f5 1138
5c262147 1139 /* Check text-only metadata header and version */
1b159566 1140 nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor);
5c262147 1141 if (nr_items < 2)
3394d22e 1142 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
5c262147
MD
1143 if (check_version(major, minor) < 0) {
1144 ret = -EINVAL;
1145 goto end_packet_read;
1146 }
8c2df3f5 1147 rewind(fp);
b4c19c1e
MD
1148 }
1149
65102a8c
MD
1150 scanner = ctf_scanner_alloc(fp);
1151 if (!scanner) {
3394d22e 1152 fprintf(stderr, "[error] Error allocating scanner\n");
65102a8c
MD
1153 ret = -ENOMEM;
1154 goto end_scanner_alloc;
1155 }
1156 ret = ctf_scanner_append_ast(scanner);
1157 if (ret) {
3394d22e 1158 fprintf(stderr, "[error] Error creating AST\n");
65102a8c
MD
1159 goto end;
1160 }
1161
1162 if (babeltrace_debug) {
3394d22e 1163 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
65102a8c 1164 if (ret) {
3394d22e 1165 fprintf(stderr, "[error] Error visiting AST for XML output\n");
65102a8c
MD
1166 goto end;
1167 }
1168 }
1169
3394d22e 1170 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
65102a8c 1171 if (ret) {
3394d22e 1172 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
1173 goto end;
1174 }
3394d22e 1175 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
ac5c6ca0 1176 td, td->byte_order);
65102a8c 1177 if (ret) {
3394d22e 1178 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
1179 goto end;
1180 }
1181end:
1182 ctf_scanner_free(scanner);
1183end_scanner_alloc:
b4c19c1e 1184end_packet_read:
f824ae04
MD
1185 if (fp) {
1186 closeret = fclose(fp);
1187 if (closeret) {
1188 perror("Error on fclose");
1189 }
1190 }
65102a8c 1191end_stream:
f824ae04
MD
1192 if (metadata_stream->pos.fd >= 0) {
1193 closeret = close(metadata_stream->pos.fd);
1194 if (closeret) {
1195 perror("Error on metadata stream fd close");
1196 }
1197 }
1198end_free:
2d0bea29
MD
1199 if (ret)
1200 g_free(metadata_stream);
0f980a35
MD
1201 return ret;
1202}
1203
e28d4618 1204static
c716f83b 1205struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
9e88d150 1206 struct ctf_stream_definition *stream,
4716614a 1207 struct ctf_event_declaration *event)
e28d4618 1208{
c716f83b 1209 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
e28d4618
MD
1210
1211 if (event->context_decl) {
0d69b916 1212 struct bt_definition *definition =
e28d4618
MD
1213 event->context_decl->p.definition_new(&event->context_decl->p,
1214 stream->parent_def_scope, 0, 0, "event.context");
1215 if (!definition) {
1216 goto error;
1217 }
42dc00b7 1218 stream_event->event_context = container_of(definition,
e28d4618 1219 struct definition_struct, p);
42dc00b7 1220 stream->parent_def_scope = stream_event->event_context->p.scope;
e28d4618
MD
1221 }
1222 if (event->fields_decl) {
0d69b916 1223 struct bt_definition *definition =
e28d4618
MD
1224 event->fields_decl->p.definition_new(&event->fields_decl->p,
1225 stream->parent_def_scope, 0, 0, "event.fields");
1226 if (!definition) {
1227 goto error;
1228 }
42dc00b7 1229 stream_event->event_fields = container_of(definition,
e28d4618 1230 struct definition_struct, p);
42dc00b7 1231 stream->parent_def_scope = stream_event->event_fields->p.scope;
e28d4618 1232 }
d3ded99d 1233 stream_event->stream = stream;
42dc00b7 1234 return stream_event;
e28d4618
MD
1235
1236error:
42dc00b7 1237 if (stream_event->event_fields)
13fad8b6 1238 bt_definition_unref(&stream_event->event_fields->p);
42dc00b7 1239 if (stream_event->event_context)
13fad8b6 1240 bt_definition_unref(&stream_event->event_context->p);
888ec52a
MD
1241 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1242 g_quark_to_string(event->name));
e28d4618
MD
1243 return NULL;
1244}
1245
1246static
9e88d150 1247int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618 1248{
f380e105 1249 struct ctf_stream_declaration *stream_class;
e28d4618
MD
1250 int ret;
1251 int i;
1252
1253 if (stream->stream_definitions_created)
1254 return 0;
1255
1256 stream_class = stream->stream_class;
1257
1258 if (stream_class->packet_context_decl) {
0d69b916 1259 struct bt_definition *definition =
e28d4618
MD
1260 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1261 stream->parent_def_scope, 0, 0, "stream.packet.context");
1262 if (!definition) {
1263 ret = -EINVAL;
1264 goto error;
1265 }
1266 stream->stream_packet_context = container_of(definition,
1267 struct definition_struct, p);
1268 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1269 }
1270 if (stream_class->event_header_decl) {
0d69b916 1271 struct bt_definition *definition =
e28d4618
MD
1272 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1273 stream->parent_def_scope, 0, 0, "stream.event.header");
1274 if (!definition) {
1275 ret = -EINVAL;
1276 goto error;
1277 }
1278 stream->stream_event_header =
1279 container_of(definition, struct definition_struct, p);
1280 stream->parent_def_scope = stream->stream_event_header->p.scope;
1281 }
1282 if (stream_class->event_context_decl) {
0d69b916 1283 struct bt_definition *definition =
e28d4618
MD
1284 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1285 stream->parent_def_scope, 0, 0, "stream.event.context");
1286 if (!definition) {
1287 ret = -EINVAL;
1288 goto error;
1289 }
1290 stream->stream_event_context =
1291 container_of(definition, struct definition_struct, p);
1292 stream->parent_def_scope = stream->stream_event_context->p.scope;
1293 }
1294 stream->events_by_id = g_ptr_array_new();
1295 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
1296 for (i = 0; i < stream->events_by_id->len; i++) {
4716614a 1297 struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i);
c716f83b 1298 struct ctf_event_definition *stream_event;
e28d4618
MD
1299
1300 if (!event)
1301 continue;
42dc00b7 1302 stream_event = create_event_definitions(td, stream, event);
888ec52a
MD
1303 if (!stream_event) {
1304 ret = -EINVAL;
e28d4618 1305 goto error_event;
888ec52a 1306 }
42dc00b7 1307 g_ptr_array_index(stream->events_by_id, i) = stream_event;
e28d4618
MD
1308 }
1309 return 0;
1310
1311error_event:
1312 for (i = 0; i < stream->events_by_id->len; i++) {
c716f83b 1313 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
42dc00b7
MD
1314 if (stream_event)
1315 g_free(stream_event);
e28d4618
MD
1316 }
1317 g_ptr_array_free(stream->events_by_id, TRUE);
1318error:
1319 if (stream->stream_event_context)
13fad8b6 1320 bt_definition_unref(&stream->stream_event_context->p);
e28d4618 1321 if (stream->stream_event_header)
13fad8b6 1322 bt_definition_unref(&stream->stream_event_header->p);
e28d4618 1323 if (stream->stream_packet_context)
13fad8b6 1324 bt_definition_unref(&stream->stream_packet_context->p);
888ec52a
MD
1325 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1326 stream_class->stream_id, strerror(-ret));
e28d4618
MD
1327 return ret;
1328}
1329
5bfcad93
MD
1330static
1331int stream_assign_class(struct ctf_trace *td,
1332 struct ctf_file_stream *file_stream,
1333 uint64_t stream_id)
1334{
1335 struct ctf_stream_declaration *stream;
1336 int ret;
1337
1338 file_stream->parent.stream_id = stream_id;
1339 if (stream_id >= td->streams->len) {
1340 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1341 return -EINVAL;
1342 }
1343 stream = g_ptr_array_index(td->streams, stream_id);
1344 if (!stream) {
1345 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1346 return -EINVAL;
1347 }
1348 file_stream->parent.stream_class = stream;
1349 ret = create_stream_definitions(td, &file_stream->parent);
1350 if (ret)
1351 return ret;
1352 return 0;
1353}
1354
0f980a35 1355static
ec323464
MD
1356int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1357 struct ctf_trace *td,
1358 struct ctf_file_stream *file_stream,
1359 size_t filesize)
0f980a35 1360{
ec323464 1361 struct packet_index packet_index;
ec323464 1362 uint64_t stream_id = 0;
653906a4 1363 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
ec323464 1364 int first_packet = 0;
653906a4 1365 int len_index;
0f980a35
MD
1366 int ret;
1367
ec323464
MD
1368begin:
1369 if (!pos->mmap_offset) {
1370 first_packet = 1;
1371 }
8895362d 1372
ec323464
MD
1373 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1374 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1375 }
0f980a35 1376
ec323464
MD
1377 if (pos->base_mma) {
1378 /* unmap old base */
1379 ret = munmap_align(pos->base_mma);
1380 if (ret) {
1381 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1382 strerror(errno));
1383 return ret;
0f980a35 1384 }
ec323464
MD
1385 pos->base_mma = NULL;
1386 }
1387 /* map new base. Need mapping length from header. */
1388 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1389 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1390 assert(pos->base_mma != MAP_FAILED);
1391 /*
1392 * Use current mapping size as temporary content and packet
1393 * size.
1394 */
1395 pos->content_size = packet_map_len;
1396 pos->packet_size = packet_map_len;
1397 pos->offset = 0; /* Position of the packet header */
1398
1399 packet_index.offset = pos->mmap_offset;
1400 packet_index.content_size = 0;
1401 packet_index.packet_size = 0;
1402 packet_index.timestamp_begin = 0;
1403 packet_index.timestamp_end = 0;
1404 packet_index.events_discarded = 0;
1405 packet_index.events_discarded_len = 0;
1406
1407 /* read and check header, set stream id (and check) */
1408 if (file_stream->parent.trace_packet_header) {
1409 /* Read packet header */
1410 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1411 if (ret) {
1412 if (ret == -EFAULT)
1413 goto retry;
888ec52a 1414 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
ec323464
MD
1415 return ret;
1416 }
1417 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1418 if (len_index >= 0) {
1419 struct bt_definition *field;
1420 uint64_t magic;
1421
1422 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1423 magic = bt_get_unsigned_int(field);
1424 if (magic != CTF_MAGIC) {
1425 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1426 magic,
1427 file_stream->pos.packet_cycles_index->len,
1428 (ssize_t) pos->mmap_offset);
1429 return -EINVAL;
0f980a35 1430 }
ec323464 1431 }
0f980a35 1432
ec323464
MD
1433 /* check uuid */
1434 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1435 if (len_index >= 0) {
1436 struct definition_array *defarray;
1437 struct bt_definition *field;
1438 uint64_t i;
1439 uint8_t uuidval[BABELTRACE_UUID_LEN];
0f980a35 1440
ec323464
MD
1441 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1442 assert(field->declaration->id == CTF_TYPE_ARRAY);
1443 defarray = container_of(field, struct definition_array, p);
1444 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
0f980a35 1445
ec323464
MD
1446 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1447 struct bt_definition *elem;
0f980a35 1448
ec323464
MD
1449 elem = bt_array_index(defarray, i);
1450 uuidval[i] = bt_get_unsigned_int(elem);
0f980a35 1451 }
ec323464
MD
1452 ret = babeltrace_uuid_compare(td->uuid, uuidval);
1453 if (ret) {
1454 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1455 return -EINVAL;
1456 }
1457 }
0f980a35 1458
ec323464
MD
1459 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1460 if (len_index >= 0) {
1461 struct bt_definition *field;
0f980a35 1462
ec323464
MD
1463 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1464 stream_id = bt_get_unsigned_int(field);
0f980a35 1465 }
ec323464 1466 }
0f980a35 1467
ec323464
MD
1468 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1469 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1470 stream_id,
1471 file_stream->parent.stream_id);
1472 return -EINVAL;
1473 }
1474 if (first_packet) {
5bfcad93 1475 ret = stream_assign_class(td, file_stream, stream_id);
ec323464
MD
1476 if (ret)
1477 return ret;
1478 }
dc48ecad 1479
ec323464
MD
1480 if (file_stream->parent.stream_packet_context) {
1481 /* Read packet context */
1482 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1483 if (ret) {
1484 if (ret == -EFAULT)
1485 goto retry;
888ec52a 1486 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
ec323464
MD
1487 return ret;
1488 }
3dd513bb
MD
1489 /* read packet size from header */
1490 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
ec323464
MD
1491 if (len_index >= 0) {
1492 struct bt_definition *field;
dc48ecad 1493
ec323464 1494 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
3dd513bb 1495 packet_index.packet_size = bt_get_unsigned_int(field);
ec323464
MD
1496 } else {
1497 /* Use file size for packet size */
3dd513bb 1498 packet_index.packet_size = filesize * CHAR_BIT;
ec323464 1499 }
75cc2c35 1500
3dd513bb
MD
1501 /* read content size from header */
1502 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
ec323464
MD
1503 if (len_index >= 0) {
1504 struct bt_definition *field;
75cc2c35 1505
ec323464 1506 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
3dd513bb 1507 packet_index.content_size = bt_get_unsigned_int(field);
ec323464 1508 } else {
3dd513bb
MD
1509 /* Use packet size if non-zero, else file size */
1510 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
ec323464 1511 }
41e82e00 1512
ec323464
MD
1513 /* read timestamp begin from header */
1514 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1515 if (len_index >= 0) {
1516 struct bt_definition *field;
41e82e00 1517
ec323464
MD
1518 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1519 packet_index.timestamp_begin = bt_get_unsigned_int(field);
4c62e2d8 1520 if (file_stream->parent.stream_class->trace->parent.collection) {
ec323464
MD
1521 packet_index.timestamp_begin =
1522 ctf_get_real_timestamp(
1523 &file_stream->parent,
1524 packet_index.timestamp_begin);
41e82e00 1525 }
0f980a35 1526 }
546293fa 1527
ec323464
MD
1528 /* read timestamp end from header */
1529 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1530 if (len_index >= 0) {
1531 struct bt_definition *field;
1532
1533 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1534 packet_index.timestamp_end = bt_get_unsigned_int(field);
4c62e2d8 1535 if (file_stream->parent.stream_class->trace->parent.collection) {
ec323464
MD
1536 packet_index.timestamp_end =
1537 ctf_get_real_timestamp(
1538 &file_stream->parent,
1539 packet_index.timestamp_end);
1540 }
546293fa
MD
1541 }
1542
ec323464
MD
1543 /* read events discarded from header */
1544 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1545 if (len_index >= 0) {
1546 struct bt_definition *field;
1547
1548 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1549 packet_index.events_discarded = bt_get_unsigned_int(field);
1550 packet_index.events_discarded_len = bt_get_int_len(field);
546293fa 1551 }
ec323464
MD
1552 } else {
1553 /* Use file size for packet size */
9cb99902
MD
1554 packet_index.packet_size = filesize * CHAR_BIT;
1555 /* Use packet size if non-zero, else file size */
1556 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
ec323464
MD
1557 }
1558
1559 /* Validate content size and packet size values */
1560 if (packet_index.content_size > packet_index.packet_size) {
1561 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1562 packet_index.content_size, packet_index.packet_size);
1563 return -EINVAL;
1564 }
1565
1566 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1567 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1568 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1569 return -EINVAL;
1570 }
546293fa 1571
bf2c7f07
MD
1572 if (packet_index.content_size < pos->offset) {
1573 fprintf(stderr, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1574 return -EINVAL;
1575 }
1576
9cb99902
MD
1577 if ((packet_index.packet_size >> LOG2_CHAR_BIT) == 0) {
1578 fprintf(stderr, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1579 return -EINVAL;
1580 }
1581
ec323464
MD
1582 /* Save position after header and context */
1583 packet_index.data_offset = pos->offset;
0f980a35 1584
ec323464
MD
1585 /* add index to packet array */
1586 g_array_append_val(file_stream->pos.packet_cycles_index, packet_index);
0f980a35 1587
ec323464
MD
1588 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1589
1590 return 0;
1591
1592 /* Retry with larger mapping */
1593retry:
1594 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1595 /*
1596 * Reached EOF, but still expecting header/context data.
1597 */
1598 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1599 return -EFAULT;
1600 }
1601 /* Double the mapping len, and retry */
1602 tmp_map_len = packet_map_len << 1;
1603 if (tmp_map_len >> 1 != packet_map_len) {
1604 /* Overflow */
888ec52a 1605 fprintf(stderr, "[error] Packet mapping length overflow\n");
ec323464 1606 return -EFAULT;
0f980a35 1607 }
ec323464
MD
1608 packet_map_len = tmp_map_len;
1609 goto begin;
1610}
1611
1612static
1613int create_stream_packet_index(struct ctf_trace *td,
1614 struct ctf_file_stream *file_stream)
1615{
1616 struct ctf_stream_pos *pos;
1617 struct stat filestats;
1618 int ret;
1619
1620 pos = &file_stream->pos;
0f980a35 1621
ec323464
MD
1622 ret = fstat(pos->fd, &filestats);
1623 if (ret < 0)
1624 return ret;
1625
5bfcad93
MD
1626 /* Deal with empty files */
1627 if (!filestats.st_size) {
1628 if (file_stream->parent.trace_packet_header
1629 || file_stream->parent.stream_packet_context) {
1630 /*
1631 * We expect a trace packet header and/or stream packet
1632 * context. Since a trace needs to have at least one
1633 * packet, empty files are therefore not accepted.
1634 */
1635 fprintf(stderr, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1636 return -EINVAL;
1637 } else {
1638 /*
1639 * Without trace packet header nor stream packet
1640 * context, a one-packet trace can indeed be empty. This
1641 * is only valid if there is only one stream class: 0.
1642 */
1643 ret = stream_assign_class(td, file_stream, 0);
1644 if (ret)
1645 return ret;
1646 return 0;
1647 }
1648 }
1649
ec323464
MD
1650 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1651 ret = create_stream_one_packet_index(pos, td, file_stream,
1652 filestats.st_size);
1653 if (ret)
1654 return ret;
1655 }
0f980a35
MD
1656 return 0;
1657}
1658
e28d4618 1659static
9e88d150 1660int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618
MD
1661{
1662 int ret;
1663
1664 if (td->packet_header_decl) {
0d69b916 1665 struct bt_definition *definition =
e28d4618
MD
1666 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1667 stream->parent_def_scope, 0, 0, "trace.packet.header");
1668 if (!definition) {
1669 ret = -EINVAL;
1670 goto error;
1671 }
1672 stream->trace_packet_header =
1673 container_of(definition, struct definition_struct, p);
1674 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1675 }
1676
1677 return 0;
1678
1679error:
888ec52a 1680 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
e28d4618
MD
1681 return ret;
1682}
1683
0f980a35
MD
1684/*
1685 * Note: many file streams can inherit from the same stream class
1686 * description (metadata).
1687 */
1688static
b086c01a 1689int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1cf393f6 1690 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
b086c01a 1691 int whence))
0f980a35 1692{
f824ae04 1693 int ret, fd, closeret;
0f980a35 1694 struct ctf_file_stream *file_stream;
ff075710 1695 struct stat statbuf;
0f980a35 1696
ff075710
MD
1697 fd = openat(td->dirfd, path, flags);
1698 if (fd < 0) {
a569a564 1699 perror("File stream openat()");
ff075710 1700 ret = fd;
0f980a35 1701 goto error;
a569a564 1702 }
ff075710
MD
1703
1704 /* Don't try to mmap subdirectories. Skip them, return success. */
1705 ret = fstat(fd, &statbuf);
1706 if (ret) {
1707 perror("File stream fstat()");
1708 goto fstat_error;
1709 }
1710 if (S_ISDIR(statbuf.st_mode)) {
1711 fprintf(stderr, "[warning] Skipping directory '%s' found in trace\n", path);
1712 ret = 0;
1713 goto fd_is_dir_ok;
1714 }
1715
0f980a35 1716 file_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 1717 file_stream->pos.last_offset = LAST_OFFSET_POISON;
b086c01a 1718
87148dc7
MD
1719 strncpy(file_stream->parent.path, path, PATH_MAX);
1720 file_stream->parent.path[PATH_MAX - 1] = '\0';
1721
06789ffd
MD
1722 if (packet_seek) {
1723 file_stream->pos.packet_seek = packet_seek;
b086c01a 1724 } else {
06789ffd 1725 fprintf(stderr, "[error] packet_seek function undefined.\n");
b086c01a
JD
1726 ret = -1;
1727 goto error_def;
1728 }
1729
ca334c72 1730 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
f824ae04
MD
1731 if (ret)
1732 goto error_def;
2d0bea29 1733 ret = create_trace_definitions(td, &file_stream->parent);
e28d4618
MD
1734 if (ret)
1735 goto error_def;
25ccc85b 1736 /*
50052405 1737 * For now, only a single clock per trace is supported.
25ccc85b 1738 */
7ec78969 1739 file_stream->parent.current_clock = td->parent.single_clock;
0f980a35 1740 ret = create_stream_packet_index(td, file_stream);
888ec52a
MD
1741 if (ret) {
1742 fprintf(stderr, "[error] Stream index creation error.\n");
0f980a35 1743 goto error_index;
888ec52a 1744 }
0f980a35 1745 /* Add stream file to stream class */
2d0bea29
MD
1746 g_ptr_array_add(file_stream->parent.stream_class->streams,
1747 &file_stream->parent);
0f980a35
MD
1748 return 0;
1749
1750error_index:
2d0bea29 1751 if (file_stream->parent.trace_packet_header)
13fad8b6 1752 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
e28d4618 1753error_def:
f824ae04
MD
1754 closeret = ctf_fini_pos(&file_stream->pos);
1755 if (closeret) {
1756 fprintf(stderr, "Error on ctf_fini_pos\n");
1757 }
0f980a35 1758 g_free(file_stream);
ff075710
MD
1759fd_is_dir_ok:
1760fstat_error:
f824ae04
MD
1761 closeret = close(fd);
1762 if (closeret) {
1763 perror("Error on fd close");
1764 }
0f980a35 1765error:
65102a8c
MD
1766 return ret;
1767}
1768
bbefb8dd 1769static
5b80ddfb 1770int ctf_open_trace_read(struct ctf_trace *td,
8c250d87 1771 const char *path, int flags,
1cf393f6 1772 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 1773 int whence), FILE *metadata_fp)
bbefb8dd 1774{
f824ae04 1775 int ret, closeret;
65102a8c
MD
1776 struct dirent *dirent;
1777 struct dirent *diriter;
1778 size_t dirent_len;
bbefb8dd 1779
46322b33 1780 td->flags = flags;
bbefb8dd
MD
1781
1782 /* Open trace directory */
46322b33
MD
1783 td->dir = opendir(path);
1784 if (!td->dir) {
6a6b384c 1785 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
bbefb8dd
MD
1786 ret = -ENOENT;
1787 goto error;
1788 }
1789
46322b33
MD
1790 td->dirfd = open(path, 0);
1791 if (td->dirfd < 0) {
6a6b384c 1792 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
a569a564
MD
1793 perror("Trace directory open");
1794 ret = -errno;
65102a8c
MD
1795 goto error_dirfd;
1796 }
caf929fa
MD
1797 strncpy(td->parent.path, path, sizeof(td->parent.path));
1798 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
0f980a35 1799
65102a8c
MD
1800 /*
1801 * Keep the metadata file separate.
1802 */
bbefb8dd 1803
06789ffd 1804 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
65102a8c 1805 if (ret) {
6a6b384c 1806 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
65102a8c
MD
1807 goto error_metadata;
1808 }
bbefb8dd
MD
1809
1810 /*
1811 * Open each stream: for each file, try to open, check magic
1812 * number, and get the stream ID to add to the right location in
1813 * the stream array.
bbefb8dd
MD
1814 */
1815
65102a8c 1816 dirent_len = offsetof(struct dirent, d_name) +
46322b33 1817 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 1818
65102a8c 1819 dirent = malloc(dirent_len);
bbefb8dd 1820
65102a8c 1821 for (;;) {
46322b33 1822 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 1823 if (ret) {
3394d22e 1824 fprintf(stderr, "[error] Readdir error.\n");
65102a8c 1825 goto readdir_error;
65102a8c
MD
1826 }
1827 if (!diriter)
1828 break;
d8ea2d29
MD
1829 /* Ignore hidden files, ., .. and metadata. */
1830 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
1831 || !strcmp(diriter->d_name, "..")
1832 || !strcmp(diriter->d_name, "metadata"))
1833 continue;
06789ffd
MD
1834 ret = ctf_open_file_stream_read(td, diriter->d_name,
1835 flags, packet_seek);
dc48ecad 1836 if (ret) {
3394d22e 1837 fprintf(stderr, "[error] Open file stream error.\n");
dc48ecad
MD
1838 goto readdir_error;
1839 }
65102a8c 1840 }
bbefb8dd 1841
65102a8c 1842 free(dirent);
bbefb8dd 1843 return 0;
65102a8c
MD
1844
1845readdir_error:
1846 free(dirent);
1847error_metadata:
f824ae04
MD
1848 closeret = close(td->dirfd);
1849 if (closeret) {
1850 perror("Error on fd close");
1851 }
65102a8c 1852error_dirfd:
f824ae04
MD
1853 closeret = closedir(td->dir);
1854 if (closeret) {
1855 perror("Error on closedir");
1856 }
bbefb8dd
MD
1857error:
1858 return ret;
1859}
1860
03798a93
JD
1861/*
1862 * ctf_open_trace: Open a CTF trace and index it.
1863 * Note that the user must seek the trace after the open (using the iterator)
1864 * since the index creation read it entirely.
1865 */
e9378815 1866static
1b8455b7 1867struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1cf393f6 1868 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 1869 int whence), FILE *metadata_fp)
bbefb8dd 1870{
46322b33 1871 struct ctf_trace *td;
bbefb8dd
MD
1872 int ret;
1873
2715de36
MD
1874 /*
1875 * If packet_seek is NULL, we provide our default version.
1876 */
1877 if (!packet_seek)
1878 packet_seek = ctf_packet_seek;
1879
46322b33 1880 td = g_new0(struct ctf_trace, 1);
bbefb8dd 1881
8c572eba 1882 switch (flags & O_ACCMODE) {
bbefb8dd 1883 case O_RDONLY:
b61922b5 1884 if (!path) {
3394d22e 1885 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
b61922b5
MD
1886 goto error;
1887 }
06789ffd 1888 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
bbefb8dd
MD
1889 if (ret)
1890 goto error;
1891 break;
989c73bc 1892 case O_RDWR:
3394d22e 1893 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
46322b33 1894 goto error;
bbefb8dd 1895 default:
3394d22e 1896 fprintf(stderr, "[error] Incorrect open flags.\n");
bbefb8dd
MD
1897 goto error;
1898 }
1899
46322b33 1900 return &td->parent;
bbefb8dd
MD
1901error:
1902 g_free(td);
1903 return NULL;
1904}
1905
2e937fb4 1906static
f571dfb1 1907void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
c150f912 1908 struct bt_mmap_stream *mmap_info)
f571dfb1
JD
1909{
1910 pos->mmap_offset = 0;
1911 pos->packet_size = 0;
1912 pos->content_size = 0;
1913 pos->content_size_loc = NULL;
1914 pos->fd = mmap_info->fd;
aee35fcc 1915 pos->base_mma = NULL;
f571dfb1
JD
1916 pos->offset = 0;
1917 pos->dummy = false;
1918 pos->cur_index = 0;
03798a93
JD
1919 pos->packet_cycles_index = NULL;
1920 pos->packet_real_index = NULL;
f571dfb1
JD
1921 pos->prot = PROT_READ;
1922 pos->flags = MAP_PRIVATE;
1923 pos->parent.rw_table = read_dispatch_table;
1924 pos->parent.event_cb = ctf_read_event;
1925}
1926
1927static
1928int prepare_mmap_stream_definition(struct ctf_trace *td,
1929 struct ctf_file_stream *file_stream)
1930{
f380e105 1931 struct ctf_stream_declaration *stream;
f571dfb1
JD
1932 uint64_t stream_id = 0;
1933 int ret;
1934
1935 file_stream->parent.stream_id = stream_id;
1936 if (stream_id >= td->streams->len) {
3394d22e 1937 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
1938 "in metadata.\n", stream_id);
1939 ret = -EINVAL;
1940 goto end;
1941 }
1942 stream = g_ptr_array_index(td->streams, stream_id);
1943 if (!stream) {
3394d22e 1944 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
1945 "in metadata.\n", stream_id);
1946 ret = -EINVAL;
1947 goto end;
1948 }
1949 file_stream->parent.stream_class = stream;
1950 ret = create_stream_definitions(td, &file_stream->parent);
1951end:
1952 return ret;
1953}
1954
1955static
1956int ctf_open_mmap_stream_read(struct ctf_trace *td,
c150f912 1957 struct bt_mmap_stream *mmap_info,
1cf393f6 1958 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
f571dfb1
JD
1959 int whence))
1960{
1961 int ret;
1962 struct ctf_file_stream *file_stream;
1963
1964 file_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 1965 file_stream->pos.last_offset = LAST_OFFSET_POISON;
f571dfb1
JD
1966 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1967
06789ffd 1968 file_stream->pos.packet_seek = packet_seek;
f571dfb1
JD
1969
1970 ret = create_trace_definitions(td, &file_stream->parent);
1971 if (ret) {
1972 goto error_def;
1973 }
1974
1975 ret = prepare_mmap_stream_definition(td, file_stream);
1976 if (ret)
1977 goto error_index;
1978
f7bbd502 1979 /*
50052405 1980 * For now, only a single clock per trace is supported.
f7bbd502 1981 */
7ec78969 1982 file_stream->parent.current_clock = td->parent.single_clock;
f7bbd502 1983
f571dfb1
JD
1984 /* Add stream file to stream class */
1985 g_ptr_array_add(file_stream->parent.stream_class->streams,
1986 &file_stream->parent);
1987 return 0;
1988
1989error_index:
1990 if (file_stream->parent.trace_packet_header)
13fad8b6 1991 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
f571dfb1
JD
1992error_def:
1993 g_free(file_stream);
1994 return ret;
1995}
1996
2e937fb4 1997static
f571dfb1 1998int ctf_open_mmap_trace_read(struct ctf_trace *td,
c150f912 1999 struct bt_mmap_stream_list *mmap_list,
1cf393f6 2000 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
f571dfb1
JD
2001 int whence),
2002 FILE *metadata_fp)
2003{
2004 int ret;
c150f912 2005 struct bt_mmap_stream *mmap_info;
f571dfb1 2006
06789ffd 2007 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
f571dfb1
JD
2008 if (ret) {
2009 goto error;
2010 }
2011
2012 /*
2013 * for each stream, try to open, check magic number, and get the
2014 * stream ID to add to the right location in the stream array.
2015 */
3122e6f0 2016 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
06789ffd 2017 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
f571dfb1 2018 if (ret) {
3394d22e 2019 fprintf(stderr, "[error] Open file mmap stream error.\n");
f571dfb1
JD
2020 goto error;
2021 }
2022 }
2023
2024 return 0;
2025
2026error:
2027 return ret;
2028}
2029
2030static
1b8455b7 2031struct bt_trace_descriptor *ctf_open_mmap_trace(
c150f912 2032 struct bt_mmap_stream_list *mmap_list,
1cf393f6 2033 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
20d0dcf9 2034 int whence),
f571dfb1
JD
2035 FILE *metadata_fp)
2036{
2037 struct ctf_trace *td;
2038 int ret;
2039
2040 if (!metadata_fp) {
2041 fprintf(stderr, "[error] No metadata file pointer associated, "
2042 "required for mmap parsing\n");
2043 goto error;
2044 }
06789ffd
MD
2045 if (!packet_seek) {
2046 fprintf(stderr, "[error] packet_seek function undefined.\n");
f571dfb1
JD
2047 goto error;
2048 }
2049 td = g_new0(struct ctf_trace, 1);
06789ffd 2050 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
f571dfb1
JD
2051 if (ret)
2052 goto error_free;
2053
2054 return &td->parent;
2055
2056error_free:
2057 g_free(td);
2058error:
2059 return NULL;
2060}
2061
03798a93 2062static
1b8455b7 2063int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
03798a93
JD
2064{
2065 int i, j, k;
2066 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2067
2068 /* for each stream_class */
2069 for (i = 0; i < td->streams->len; i++) {
2070 struct ctf_stream_declaration *stream_class;
2071
2072 stream_class = g_ptr_array_index(td->streams, i);
2073 if (!stream_class)
2074 continue;
2075 /* for each file_stream */
2076 for (j = 0; j < stream_class->streams->len; j++) {
2077 struct ctf_stream_definition *stream;
2078 struct ctf_stream_pos *stream_pos;
2079 struct ctf_file_stream *cfs;
2080
2081 stream = g_ptr_array_index(stream_class->streams, j);
2082 if (!stream)
2083 continue;
2084 cfs = container_of(stream, struct ctf_file_stream,
2085 parent);
2086 stream_pos = &cfs->pos;
afe9cd4a
JD
2087 if (!stream_pos->packet_cycles_index)
2088 continue;
2089
03798a93
JD
2090 for (k = 0; k < stream_pos->packet_cycles_index->len; k++) {
2091 struct packet_index *index;
2092 struct packet_index new_index;
2093
2094 index = &g_array_index(stream_pos->packet_cycles_index,
2095 struct packet_index, k);
2096 memcpy(&new_index, index,
2097 sizeof(struct packet_index));
2098 new_index.timestamp_begin =
2099 ctf_get_real_timestamp(stream,
2100 index->timestamp_begin);
2101 new_index.timestamp_end =
2102 ctf_get_real_timestamp(stream,
2103 index->timestamp_end);
2104 g_array_append_val(stream_pos->packet_real_index,
2105 new_index);
2106 }
2107 }
2108 }
2109 return 0;
2110}
2111
0f980a35 2112static
f824ae04 2113int ctf_close_file_stream(struct ctf_file_stream *file_stream)
0f980a35 2114{
f824ae04
MD
2115 int ret;
2116
2117 ret = ctf_fini_pos(&file_stream->pos);
2118 if (ret) {
2119 fprintf(stderr, "Error on ctf_fini_pos\n");
2120 return -1;
2121 }
2122 ret = close(file_stream->pos.fd);
2123 if (ret) {
2124 perror("Error closing file fd");
2125 return -1;
2126 }
2127 return 0;
0f980a35
MD
2128}
2129
e9378815 2130static
1b8455b7 2131int ctf_close_trace(struct bt_trace_descriptor *tdp)
bbefb8dd 2132{
46322b33 2133 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
08c82b90 2134 int ret;
0f980a35 2135
46322b33 2136 if (td->streams) {
08c82b90
MD
2137 int i;
2138
46322b33 2139 for (i = 0; i < td->streams->len; i++) {
f380e105 2140 struct ctf_stream_declaration *stream;
0f980a35 2141 int j;
e9378815 2142
46322b33 2143 stream = g_ptr_array_index(td->streams, i);
e9378815
MD
2144 if (!stream)
2145 continue;
2d0bea29 2146 for (j = 0; j < stream->streams->len; j++) {
0f980a35 2147 struct ctf_file_stream *file_stream;
15d4fe3c
JD
2148 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2149 struct ctf_file_stream, parent);
f824ae04
MD
2150 ret = ctf_close_file_stream(file_stream);
2151 if (ret)
2152 return ret;
0f980a35 2153 }
e003ab50 2154 }
e003ab50 2155 }
15d4fe3c 2156 ctf_destroy_metadata(td);
f824ae04
MD
2157 ret = close(td->dirfd);
2158 if (ret) {
2159 perror("Error closing dirfd");
2160 return ret;
2161 }
2162 ret = closedir(td->dir);
2163 if (ret) {
2164 perror("Error closedir");
2165 return ret;
2166 }
7237592a 2167 free(td->metadata_string);
bbefb8dd 2168 g_free(td);
f824ae04 2169 return 0;
bbefb8dd
MD
2170}
2171
98a04903 2172static
1b8455b7 2173void ctf_set_context(struct bt_trace_descriptor *descriptor,
98a04903
JD
2174 struct bt_context *ctx)
2175{
2176 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2177 parent);
2178
45807148 2179 td->parent.ctx = ctx;
98a04903
JD
2180}
2181
2182static
1b8455b7 2183void ctf_set_handle(struct bt_trace_descriptor *descriptor,
98a04903
JD
2184 struct bt_trace_handle *handle)
2185{
2186 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2187 parent);
2188
4d086981 2189 td->parent.handle = handle;
98a04903
JD
2190}
2191
95febab3 2192static
7fb21036 2193void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
2194{
2195 int ret;
2196
4c8bfb7e 2197 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
2198 ret = bt_register_format(&ctf_format);
2199 assert(!ret);
2200}
698f0fe4 2201
95febab3
MD
2202static
2203void __attribute__((destructor)) ctf_exit(void)
2204{
2205 bt_unregister_format(&ctf_format);
2206}
This page took 0.180446 seconds and 4 git commands to generate.