Add new option --clock-offset-ns
[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>
43e34335
MD
36#include <babeltrace/uuid.h>
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"
f8370579 54#include "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
551 return 0;
552
553error:
3394d22e 554 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
555 return ret;
556}
557
558static
1cf393f6 559int ctf_write_event(struct bt_stream_pos *pos, struct ctf_stream_definition *stream)
31262354 560{
f380e105 561 struct ctf_stream_declaration *stream_class = stream->stream_class;
c716f83b 562 struct ctf_event_definition *event;
c87a8eb2 563 uint64_t id;
31262354
MD
564 int ret;
565
c87a8eb2
MD
566 id = stream->event_id;
567
31262354 568 /* print event header */
7f3f572b 569 if (likely(stream->stream_event_header)) {
e28d4618 570 ret = generic_rw(pos, &stream->stream_event_header->p);
31262354
MD
571 if (ret)
572 goto error;
573 }
574
575 /* print stream-declared event context */
e28d4618
MD
576 if (stream->stream_event_context) {
577 ret = generic_rw(pos, &stream->stream_event_context->p);
31262354
MD
578 if (ret)
579 goto error;
580 }
581
7f3f572b 582 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 583 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
584 return -EINVAL;
585 }
e28d4618 586 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 587 if (unlikely(!event)) {
3394d22e 588 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
589 return -EINVAL;
590 }
591
592 /* print event-declared event context */
e28d4618
MD
593 if (event->event_context) {
594 ret = generic_rw(pos, &event->event_context->p);
31262354
MD
595 if (ret)
596 goto error;
597 }
598
599 /* Read and print event payload */
7f3f572b 600 if (likely(event->event_fields)) {
e28d4618 601 ret = generic_rw(pos, &event->event_fields->p);
31262354
MD
602 if (ret)
603 goto error;
604 }
605
606 return 0;
607
608error:
3394d22e 609 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
610 return ret;
611}
612
ca334c72
MD
613int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
614 int fd, int open_flags)
8c572eba
MD
615{
616 pos->fd = fd;
37fcdd19 617 if (fd >= 0) {
03798a93 618 pos->packet_cycles_index = g_array_new(FALSE, TRUE,
8563e754 619 sizeof(struct packet_index));
37fcdd19
JD
620 pos->packet_real_index = g_array_new(FALSE, TRUE,
621 sizeof(struct packet_index));
622 } else {
03798a93 623 pos->packet_cycles_index = NULL;
37fcdd19
JD
624 pos->packet_real_index = NULL;
625 }
8563e754
MD
626 switch (open_flags & O_ACCMODE) {
627 case O_RDONLY:
628 pos->prot = PROT_READ;
629 pos->flags = MAP_PRIVATE;
630 pos->parent.rw_table = read_dispatch_table;
31262354 631 pos->parent.event_cb = ctf_read_event;
ca334c72 632 pos->parent.trace = trace;
8563e754 633 break;
8563e754
MD
634 case O_RDWR:
635 pos->prot = PROT_WRITE; /* Write has priority */
636 pos->flags = MAP_SHARED;
637 pos->parent.rw_table = write_dispatch_table;
31262354 638 pos->parent.event_cb = ctf_write_event;
ca334c72 639 pos->parent.trace = trace;
8563e754 640 if (fd >= 0)
06789ffd 641 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
8563e754
MD
642 break;
643 default:
644 assert(0);
8c572eba 645 }
f824ae04 646 return 0;
8c572eba
MD
647}
648
f824ae04 649int ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba 650{
8c572eba
MD
651 if (pos->prot == PROT_WRITE && pos->content_size_loc)
652 *pos->content_size_loc = pos->offset;
aee35fcc 653 if (pos->base_mma) {
08c82b90
MD
654 int ret;
655
8c572eba 656 /* unmap old base */
aee35fcc 657 ret = munmap_align(pos->base_mma);
8c572eba 658 if (ret) {
3394d22e 659 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
8c572eba 660 strerror(errno));
f824ae04 661 return -1;
8c572eba
MD
662 }
663 }
37fcdd19
JD
664 if (pos->packet_cycles_index)
665 (void) g_array_free(pos->packet_cycles_index, TRUE);
666 if (pos->packet_real_index)
667 (void) g_array_free(pos->packet_real_index, TRUE);
f824ae04 668 return 0;
8c572eba
MD
669}
670
20d0dcf9
MD
671/*
672 * for SEEK_CUR: go to next packet.
673 * for SEEK_POS: go to packet numer (index).
674 */
1cf393f6 675void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
0f980a35 676{
d6425aaf
MD
677 struct ctf_stream_pos *pos =
678 container_of(stream_pos, struct ctf_stream_pos, parent);
75cc2c35
MD
679 struct ctf_file_stream *file_stream =
680 container_of(pos, struct ctf_file_stream, pos);
0f980a35 681 int ret;
8c572eba 682 off_t off;
20d0dcf9 683 struct packet_index *packet_index;
0f980a35 684
8c572eba
MD
685 if (pos->prot == PROT_WRITE && pos->content_size_loc)
686 *pos->content_size_loc = pos->offset;
0f980a35 687
aee35fcc 688 if (pos->base_mma) {
0f980a35 689 /* unmap old base */
aee35fcc 690 ret = munmap_align(pos->base_mma);
0f980a35 691 if (ret) {
3394d22e 692 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
693 strerror(errno));
694 assert(0);
695 }
aee35fcc 696 pos->base_mma = NULL;
0f980a35
MD
697 }
698
8c572eba 699 /*
46322b33 700 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
701 * except to get exactly at the beginning of the next packet.
702 */
703 if (pos->prot == PROT_WRITE) {
989c73bc
MD
704 switch (whence) {
705 case SEEK_CUR:
706 /* The writer will add padding */
8c572eba 707 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
989c73bc
MD
708 break;
709 case SEEK_SET:
20d0dcf9 710 assert(index == 0); /* only seek supported for now */
989c73bc
MD
711 pos->cur_index = 0;
712 break;
713 default:
714 assert(0);
715 }
8c572eba
MD
716 pos->content_size = -1U; /* Unknown at this point */
717 pos->packet_size = WRITE_PACKET_LEN;
989c73bc
MD
718 off = posix_fallocate(pos->fd, pos->mmap_offset,
719 pos->packet_size / CHAR_BIT);
8c572eba 720 assert(off >= 0);
847bf71a 721 pos->offset = 0;
8c572eba 722 } else {
5f643ed7 723 read_next_packet:
847bf71a
MD
724 switch (whence) {
725 case SEEK_CUR:
41e82e00 726 {
4c4ba021 727 uint64_t events_discarded_diff;
41e82e00 728
7d97fad9
MD
729 if (pos->offset == EOF) {
730 return;
731 }
3217ac37 732 /* For printing discarded event count */
03798a93 733 packet_index = &g_array_index(pos->packet_cycles_index,
41e82e00 734 struct packet_index, pos->cur_index);
03798a93
JD
735 file_stream->parent.prev_cycles_timestamp_end =
736 packet_index->timestamp_end;
737 file_stream->parent.prev_cycles_timestamp =
738 packet_index->timestamp_begin;
739
740 packet_index = &g_array_index(pos->packet_real_index,
741 struct packet_index, pos->cur_index);
742 file_stream->parent.prev_real_timestamp_end =
20d0dcf9 743 packet_index->timestamp_end;
03798a93
JD
744 file_stream->parent.prev_real_timestamp =
745 packet_index->timestamp_begin;
746
747 events_discarded_diff = packet_index->events_discarded;
41e82e00 748 if (pos->cur_index > 0) {
03798a93 749 packet_index = &g_array_index(pos->packet_real_index,
41e82e00
MD
750 struct packet_index,
751 pos->cur_index - 1);
20d0dcf9 752 events_discarded_diff -= packet_index->events_discarded;
4c4ba021
MD
753 /*
754 * Deal with 32-bit wrap-around if the
755 * tracer provided a 32-bit field.
756 */
757 if (packet_index->events_discarded_len == 32) {
758 events_discarded_diff = (uint32_t) events_discarded_diff;
759 }
41e82e00 760 }
fca04958 761 file_stream->parent.events_discarded = events_discarded_diff;
03798a93
JD
762 file_stream->parent.prev_real_timestamp = file_stream->parent.real_timestamp;
763 file_stream->parent.prev_cycles_timestamp = file_stream->parent.cycles_timestamp;
847bf71a 764 /* The reader will expect us to skip padding */
8c572eba 765 ++pos->cur_index;
847bf71a 766 break;
41e82e00 767 }
847bf71a 768 case SEEK_SET:
f60efc0e
JD
769 packet_index = &g_array_index(pos->packet_cycles_index,
770 struct packet_index, index);
771 pos->last_events_discarded = packet_index->events_discarded;
20d0dcf9 772 pos->cur_index = index;
03798a93
JD
773 file_stream->parent.prev_real_timestamp = 0;
774 file_stream->parent.prev_real_timestamp_end = 0;
775 file_stream->parent.prev_cycles_timestamp = 0;
776 file_stream->parent.prev_cycles_timestamp_end = 0;
847bf71a
MD
777 break;
778 default:
779 assert(0);
780 }
03798a93 781 if (pos->cur_index >= pos->packet_real_index->len) {
7d97fad9 782 /*
87148dc7
MD
783 * We need to check if we are in trace read or
784 * called from packet indexing. In this last
785 * case, the collection is not there, so we
786 * cannot print the timestamps.
7d97fad9 787 */
4c62e2d8 788 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
badea1a2 789 /*
87148dc7
MD
790 * When a stream reaches the end of the
791 * file, we need to show the number of
792 * events discarded ourselves, because
793 * there is no next event scheduled to
794 * be printed in the output.
badea1a2 795 */
87148dc7 796 if (file_stream->parent.events_discarded) {
badea1a2 797 fflush(stdout);
c829a65c
MD
798 ctf_print_discarded(stderr,
799 &file_stream->parent,
800 1);
87148dc7 801 file_stream->parent.events_discarded = 0;
badea1a2 802 }
7d97fad9 803 }
670977d3 804 pos->offset = EOF;
847bf71a
MD
805 return;
806 }
03798a93
JD
807 packet_index = &g_array_index(pos->packet_cycles_index,
808 struct packet_index,
809 pos->cur_index);
810 file_stream->parent.cycles_timestamp = packet_index->timestamp_begin;
811
812 packet_index = &g_array_index(pos->packet_real_index,
813 struct packet_index,
814 pos->cur_index);
815 file_stream->parent.real_timestamp = packet_index->timestamp_begin;
20d0dcf9 816 pos->mmap_offset = packet_index->offset;
8c572eba
MD
817
818 /* Lookup context/packet size in index */
20d0dcf9
MD
819 pos->content_size = packet_index->content_size;
820 pos->packet_size = packet_index->packet_size;
821 if (packet_index->data_offset < packet_index->content_size) {
75cc2c35 822 pos->offset = 0; /* will read headers */
20d0dcf9 823 } else if (packet_index->data_offset == packet_index->content_size) {
5f643ed7 824 /* empty packet */
20d0dcf9 825 pos->offset = packet_index->data_offset;
3abe83c7 826 whence = SEEK_CUR;
5f643ed7 827 goto read_next_packet;
7eda6fc7 828 } else {
2b9a764d
MD
829 pos->offset = EOF;
830 return;
831 }
8c572eba 832 }
0f980a35 833 /* map new base. Need mapping length from header. */
aee35fcc
MD
834 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
835 pos->flags, pos->fd, pos->mmap_offset);
836 if (pos->base_mma == MAP_FAILED) {
3394d22e 837 fprintf(stderr, "[error] mmap error %s.\n",
847bf71a
MD
838 strerror(errno));
839 assert(0);
840 }
75cc2c35
MD
841
842 /* update trace_packet_header and stream_packet_context */
2d0bea29 843 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
75cc2c35 844 /* Read packet header */
2d0bea29 845 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
75cc2c35
MD
846 assert(!ret);
847 }
2d0bea29 848 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
75cc2c35 849 /* Read packet context */
2d0bea29 850 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
75cc2c35
MD
851 assert(!ret);
852 }
0f980a35
MD
853}
854
b4c19c1e
MD
855static
856int packet_metadata(struct ctf_trace *td, FILE *fp)
857{
858 uint32_t magic;
859 size_t len;
860 int ret = 0;
861
862 len = fread(&magic, sizeof(magic), 1, fp);
a0fe7d97 863 if (len != 1) {
b4c19c1e
MD
864 goto end;
865 }
866 if (magic == TSDL_MAGIC) {
867 ret = 1;
868 td->byte_order = BYTE_ORDER;
0d336fdf 869 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
870 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
871 ret = 1;
872 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
873 LITTLE_ENDIAN : BIG_ENDIAN;
0d336fdf 874 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
875 }
876end:
877 rewind(fp);
878 return ret;
879}
880
5c262147
MD
881/*
882 * Returns 0 on success, -1 on error.
883 */
884static
885int check_version(unsigned int major, unsigned int minor)
886{
887 switch (major) {
888 case 1:
889 switch (minor) {
890 case 8:
891 return 0;
892 default:
893 goto warning;
894 }
895 default:
896 goto warning;
897
898 }
899
900 /* eventually return an error instead of warning */
901warning:
3394d22e 902 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
5c262147
MD
903 major, minor);
904 return 0;
905}
906
b4c19c1e
MD
907static
908int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
909 FILE *out)
910{
911 struct metadata_packet_header header;
a0fe7d97 912 size_t readlen, writelen, toread;
f8254867 913 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
b4c19c1e
MD
914 int ret = 0;
915
a91a962e 916 readlen = fread(&header, header_sizeof(header), 1, in);
a0fe7d97 917 if (readlen < 1)
b4c19c1e
MD
918 return -EINVAL;
919
920 if (td->byte_order != BYTE_ORDER) {
921 header.magic = GUINT32_SWAP_LE_BE(header.magic);
922 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
923 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
924 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
925 }
926 if (header.checksum)
3394d22e 927 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
b4c19c1e 928 if (header.compression_scheme) {
3394d22e 929 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
b4c19c1e
MD
930 header.compression_scheme);
931 return -EINVAL;
932 }
933 if (header.encryption_scheme) {
3394d22e 934 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
b4c19c1e
MD
935 header.encryption_scheme);
936 return -EINVAL;
937 }
938 if (header.checksum_scheme) {
3394d22e 939 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
b4c19c1e
MD
940 header.checksum_scheme);
941 return -EINVAL;
942 }
5c262147
MD
943 if (check_version(header.major, header.minor) < 0)
944 return -EINVAL;
b4c19c1e
MD
945 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
946 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
947 CTF_TRACE_SET_FIELD(td, uuid);
948 } else {
43e34335 949 if (babeltrace_uuid_compare(header.uuid, td->uuid))
b4c19c1e
MD
950 return -EINVAL;
951 }
952
b1ccd079
MD
953 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
954 return -EINVAL;
955
255b2138 956 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
a0fe7d97
MD
957
958 for (;;) {
f8254867 959 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
b4c19c1e
MD
960 if (ferror(in)) {
961 ret = -EINVAL;
962 break;
963 }
4152822b 964 if (babeltrace_debug) {
f8254867 965 buf[readlen] = '\0';
3394d22e 966 fprintf(stderr, "[debug] metadata packet read: %s\n",
4152822b
MD
967 buf);
968 }
969
b4c19c1e
MD
970 writelen = fwrite(buf, sizeof(char), readlen, out);
971 if (writelen < readlen) {
972 ret = -EIO;
973 break;
974 }
975 if (ferror(out)) {
976 ret = -EINVAL;
977 break;
978 }
a0fe7d97
MD
979 toread -= readlen;
980 if (!toread) {
7f4b5c4d 981 ret = 0; /* continue reading next packet */
ddbc52af 982 goto read_padding;
a0fe7d97 983 }
b4c19c1e
MD
984 }
985 return ret;
ddbc52af
MD
986
987read_padding:
988 toread = (header.packet_size - header.content_size) / CHAR_BIT;
989 ret = fseek(in, toread, SEEK_CUR);
990 if (ret < 0) {
3394d22e 991 fprintf(stderr, "[warning] Missing padding at end of file\n");
ddbc52af
MD
992 ret = 0;
993 }
994 return ret;
b4c19c1e
MD
995}
996
997static
998int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
999 char **buf)
1000{
1001 FILE *in, *out;
abc40d24 1002 size_t size, buflen;
b4c19c1e
MD
1003 int ret;
1004
1005 in = *fp;
c4f5487e
MD
1006 /*
1007 * Using strlen on *buf instead of size of open_memstream
1008 * because its size includes garbage at the end (after final
1009 * \0). This is the allocated size, not the actual string size.
1010 */
f8370579 1011 out = babeltrace_open_memstream(buf, &size);
a569a564
MD
1012 if (out == NULL) {
1013 perror("Metadata open_memstream");
b4c19c1e 1014 return -errno;
a569a564 1015 }
b4c19c1e
MD
1016 for (;;) {
1017 ret = ctf_open_trace_metadata_packet_read(td, in, out);
7f4b5c4d 1018 if (ret) {
b4c19c1e 1019 break;
7f4b5c4d
MD
1020 }
1021 if (feof(in)) {
1022 ret = 0;
b4c19c1e
MD
1023 break;
1024 }
1025 }
f8370579
MD
1026 /* close to flush the buffer */
1027 ret = babeltrace_close_memstream(buf, &size, out);
1028 if (ret < 0) {
f824ae04
MD
1029 int closeret;
1030
f8370579 1031 perror("babeltrace_flush_memstream");
f824ae04
MD
1032 ret = -errno;
1033 closeret = fclose(in);
1034 if (closeret) {
1035 perror("Error in fclose");
1036 }
1037 return ret;
1038 }
1039 ret = fclose(in);
1040 if (ret) {
1041 perror("Error in fclose");
f8370579 1042 }
b4c19c1e 1043 /* open for reading */
abc40d24
MD
1044 buflen = strlen(*buf);
1045 if (!buflen) {
1046 *fp = NULL;
493330cb 1047 return -ENOENT;
abc40d24
MD
1048 }
1049 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
a569a564
MD
1050 if (!*fp) {
1051 perror("Metadata fmemopen");
1052 return -errno;
1053 }
b4c19c1e
MD
1054 return 0;
1055}
1056
65102a8c 1057static
b086c01a 1058int ctf_open_trace_metadata_read(struct ctf_trace *td,
1cf393f6 1059 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 1060 int whence), FILE *metadata_fp)
65102a8c
MD
1061{
1062 struct ctf_scanner *scanner;
2d0bea29 1063 struct ctf_file_stream *metadata_stream;
65102a8c 1064 FILE *fp;
b4c19c1e 1065 char *buf = NULL;
f824ae04 1066 int ret = 0, closeret;
65102a8c 1067
2d0bea29 1068 metadata_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 1069 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
b086c01a 1070
06789ffd
MD
1071 if (packet_seek) {
1072 metadata_stream->pos.packet_seek = packet_seek;
b086c01a 1073 } else {
06789ffd 1074 fprintf(stderr, "[error] packet_seek function undefined.\n");
b086c01a 1075 ret = -1;
f824ae04 1076 goto end_free;
b086c01a
JD
1077 }
1078
ae23d232
JD
1079 if (metadata_fp) {
1080 fp = metadata_fp;
bcbfb8bf 1081 metadata_stream->pos.fd = -1;
ae23d232
JD
1082 } else {
1083 td->metadata = &metadata_stream->parent;
1084 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1085 if (metadata_stream->pos.fd < 0) {
3394d22e 1086 fprintf(stderr, "Unable to open metadata.\n");
f824ae04
MD
1087 ret = -1;
1088 goto end_free;
ae23d232 1089 }
65102a8c 1090
ae23d232
JD
1091 fp = fdopen(metadata_stream->pos.fd, "r");
1092 if (!fp) {
3394d22e 1093 fprintf(stderr, "[error] Unable to open metadata stream.\n");
a569a564 1094 perror("Metadata stream open");
ae23d232
JD
1095 ret = -errno;
1096 goto end_stream;
1097 }
f824ae04
MD
1098 /* fd now belongs to fp */
1099 metadata_stream->pos.fd = -1;
ae23d232 1100 }
65102a8c
MD
1101 if (babeltrace_debug)
1102 yydebug = 1;
1103
b4c19c1e
MD
1104 if (packet_metadata(td, fp)) {
1105 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
abc40d24
MD
1106 if (ret) {
1107 /* Warn about empty metadata */
1108 fprintf(stderr, "[warning] Empty metadata.\n");
b4c19c1e 1109 goto end_packet_read;
abc40d24 1110 }
7237592a
MD
1111 td->metadata_string = buf;
1112 td->metadata_packetized = 1;
da75b0f7 1113 } else {
5c262147
MD
1114 unsigned int major, minor;
1115 ssize_t nr_items;
8c2df3f5 1116
da75b0f7 1117 td->byte_order = BYTE_ORDER;
8c2df3f5 1118
5c262147
MD
1119 /* Check text-only metadata header and version */
1120 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
1121 if (nr_items < 2)
3394d22e 1122 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
5c262147
MD
1123 if (check_version(major, minor) < 0) {
1124 ret = -EINVAL;
1125 goto end_packet_read;
1126 }
8c2df3f5 1127 rewind(fp);
b4c19c1e
MD
1128 }
1129
65102a8c
MD
1130 scanner = ctf_scanner_alloc(fp);
1131 if (!scanner) {
3394d22e 1132 fprintf(stderr, "[error] Error allocating scanner\n");
65102a8c
MD
1133 ret = -ENOMEM;
1134 goto end_scanner_alloc;
1135 }
1136 ret = ctf_scanner_append_ast(scanner);
1137 if (ret) {
3394d22e 1138 fprintf(stderr, "[error] Error creating AST\n");
65102a8c
MD
1139 goto end;
1140 }
1141
1142 if (babeltrace_debug) {
3394d22e 1143 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
65102a8c 1144 if (ret) {
3394d22e 1145 fprintf(stderr, "[error] Error visiting AST for XML output\n");
65102a8c
MD
1146 goto end;
1147 }
1148 }
1149
3394d22e 1150 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
65102a8c 1151 if (ret) {
3394d22e 1152 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
1153 goto end;
1154 }
3394d22e 1155 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
ac5c6ca0 1156 td, td->byte_order);
65102a8c 1157 if (ret) {
3394d22e 1158 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
1159 goto end;
1160 }
1161end:
1162 ctf_scanner_free(scanner);
1163end_scanner_alloc:
b4c19c1e 1164end_packet_read:
f824ae04
MD
1165 if (fp) {
1166 closeret = fclose(fp);
1167 if (closeret) {
1168 perror("Error on fclose");
1169 }
1170 }
65102a8c 1171end_stream:
f824ae04
MD
1172 if (metadata_stream->pos.fd >= 0) {
1173 closeret = close(metadata_stream->pos.fd);
1174 if (closeret) {
1175 perror("Error on metadata stream fd close");
1176 }
1177 }
1178end_free:
2d0bea29
MD
1179 if (ret)
1180 g_free(metadata_stream);
0f980a35
MD
1181 return ret;
1182}
1183
e28d4618 1184static
c716f83b 1185struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
9e88d150 1186 struct ctf_stream_definition *stream,
4716614a 1187 struct ctf_event_declaration *event)
e28d4618 1188{
c716f83b 1189 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
e28d4618
MD
1190
1191 if (event->context_decl) {
0d69b916 1192 struct bt_definition *definition =
e28d4618
MD
1193 event->context_decl->p.definition_new(&event->context_decl->p,
1194 stream->parent_def_scope, 0, 0, "event.context");
1195 if (!definition) {
1196 goto error;
1197 }
42dc00b7 1198 stream_event->event_context = container_of(definition,
e28d4618 1199 struct definition_struct, p);
42dc00b7 1200 stream->parent_def_scope = stream_event->event_context->p.scope;
e28d4618
MD
1201 }
1202 if (event->fields_decl) {
0d69b916 1203 struct bt_definition *definition =
e28d4618
MD
1204 event->fields_decl->p.definition_new(&event->fields_decl->p,
1205 stream->parent_def_scope, 0, 0, "event.fields");
1206 if (!definition) {
1207 goto error;
1208 }
42dc00b7 1209 stream_event->event_fields = container_of(definition,
e28d4618 1210 struct definition_struct, p);
42dc00b7 1211 stream->parent_def_scope = stream_event->event_fields->p.scope;
e28d4618 1212 }
d3ded99d 1213 stream_event->stream = stream;
42dc00b7 1214 return stream_event;
e28d4618
MD
1215
1216error:
42dc00b7 1217 if (stream_event->event_fields)
13fad8b6 1218 bt_definition_unref(&stream_event->event_fields->p);
42dc00b7 1219 if (stream_event->event_context)
13fad8b6 1220 bt_definition_unref(&stream_event->event_context->p);
888ec52a
MD
1221 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1222 g_quark_to_string(event->name));
e28d4618
MD
1223 return NULL;
1224}
1225
1226static
9e88d150 1227int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618 1228{
f380e105 1229 struct ctf_stream_declaration *stream_class;
e28d4618
MD
1230 int ret;
1231 int i;
1232
1233 if (stream->stream_definitions_created)
1234 return 0;
1235
1236 stream_class = stream->stream_class;
1237
1238 if (stream_class->packet_context_decl) {
0d69b916 1239 struct bt_definition *definition =
e28d4618
MD
1240 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1241 stream->parent_def_scope, 0, 0, "stream.packet.context");
1242 if (!definition) {
1243 ret = -EINVAL;
1244 goto error;
1245 }
1246 stream->stream_packet_context = container_of(definition,
1247 struct definition_struct, p);
1248 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1249 }
1250 if (stream_class->event_header_decl) {
0d69b916 1251 struct bt_definition *definition =
e28d4618
MD
1252 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1253 stream->parent_def_scope, 0, 0, "stream.event.header");
1254 if (!definition) {
1255 ret = -EINVAL;
1256 goto error;
1257 }
1258 stream->stream_event_header =
1259 container_of(definition, struct definition_struct, p);
1260 stream->parent_def_scope = stream->stream_event_header->p.scope;
1261 }
1262 if (stream_class->event_context_decl) {
0d69b916 1263 struct bt_definition *definition =
e28d4618
MD
1264 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1265 stream->parent_def_scope, 0, 0, "stream.event.context");
1266 if (!definition) {
1267 ret = -EINVAL;
1268 goto error;
1269 }
1270 stream->stream_event_context =
1271 container_of(definition, struct definition_struct, p);
1272 stream->parent_def_scope = stream->stream_event_context->p.scope;
1273 }
1274 stream->events_by_id = g_ptr_array_new();
1275 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
1276 for (i = 0; i < stream->events_by_id->len; i++) {
4716614a 1277 struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i);
c716f83b 1278 struct ctf_event_definition *stream_event;
e28d4618
MD
1279
1280 if (!event)
1281 continue;
42dc00b7 1282 stream_event = create_event_definitions(td, stream, event);
888ec52a
MD
1283 if (!stream_event) {
1284 ret = -EINVAL;
e28d4618 1285 goto error_event;
888ec52a 1286 }
42dc00b7 1287 g_ptr_array_index(stream->events_by_id, i) = stream_event;
e28d4618
MD
1288 }
1289 return 0;
1290
1291error_event:
1292 for (i = 0; i < stream->events_by_id->len; i++) {
c716f83b 1293 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
42dc00b7
MD
1294 if (stream_event)
1295 g_free(stream_event);
e28d4618
MD
1296 }
1297 g_ptr_array_free(stream->events_by_id, TRUE);
1298error:
1299 if (stream->stream_event_context)
13fad8b6 1300 bt_definition_unref(&stream->stream_event_context->p);
e28d4618 1301 if (stream->stream_event_header)
13fad8b6 1302 bt_definition_unref(&stream->stream_event_header->p);
e28d4618 1303 if (stream->stream_packet_context)
13fad8b6 1304 bt_definition_unref(&stream->stream_packet_context->p);
888ec52a
MD
1305 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1306 stream_class->stream_id, strerror(-ret));
e28d4618
MD
1307 return ret;
1308}
1309
0f980a35 1310static
ec323464
MD
1311int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1312 struct ctf_trace *td,
1313 struct ctf_file_stream *file_stream,
1314 size_t filesize)
0f980a35 1315{
ec323464 1316 struct packet_index packet_index;
f380e105 1317 struct ctf_stream_declaration *stream;
ec323464 1318 uint64_t stream_id = 0;
653906a4 1319 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
ec323464 1320 int first_packet = 0;
653906a4 1321 int len_index;
0f980a35
MD
1322 int ret;
1323
ec323464
MD
1324begin:
1325 if (!pos->mmap_offset) {
1326 first_packet = 1;
1327 }
8895362d 1328
ec323464
MD
1329 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1330 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1331 }
0f980a35 1332
ec323464
MD
1333 if (pos->base_mma) {
1334 /* unmap old base */
1335 ret = munmap_align(pos->base_mma);
1336 if (ret) {
1337 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1338 strerror(errno));
1339 return ret;
0f980a35 1340 }
ec323464
MD
1341 pos->base_mma = NULL;
1342 }
1343 /* map new base. Need mapping length from header. */
1344 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1345 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1346 assert(pos->base_mma != MAP_FAILED);
1347 /*
1348 * Use current mapping size as temporary content and packet
1349 * size.
1350 */
1351 pos->content_size = packet_map_len;
1352 pos->packet_size = packet_map_len;
1353 pos->offset = 0; /* Position of the packet header */
1354
1355 packet_index.offset = pos->mmap_offset;
1356 packet_index.content_size = 0;
1357 packet_index.packet_size = 0;
1358 packet_index.timestamp_begin = 0;
1359 packet_index.timestamp_end = 0;
1360 packet_index.events_discarded = 0;
1361 packet_index.events_discarded_len = 0;
1362
1363 /* read and check header, set stream id (and check) */
1364 if (file_stream->parent.trace_packet_header) {
1365 /* Read packet header */
1366 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1367 if (ret) {
1368 if (ret == -EFAULT)
1369 goto retry;
888ec52a 1370 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
ec323464
MD
1371 return ret;
1372 }
1373 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1374 if (len_index >= 0) {
1375 struct bt_definition *field;
1376 uint64_t magic;
1377
1378 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1379 magic = bt_get_unsigned_int(field);
1380 if (magic != CTF_MAGIC) {
1381 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1382 magic,
1383 file_stream->pos.packet_cycles_index->len,
1384 (ssize_t) pos->mmap_offset);
1385 return -EINVAL;
0f980a35 1386 }
ec323464 1387 }
0f980a35 1388
ec323464
MD
1389 /* check uuid */
1390 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1391 if (len_index >= 0) {
1392 struct definition_array *defarray;
1393 struct bt_definition *field;
1394 uint64_t i;
1395 uint8_t uuidval[BABELTRACE_UUID_LEN];
0f980a35 1396
ec323464
MD
1397 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1398 assert(field->declaration->id == CTF_TYPE_ARRAY);
1399 defarray = container_of(field, struct definition_array, p);
1400 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
0f980a35 1401
ec323464
MD
1402 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1403 struct bt_definition *elem;
0f980a35 1404
ec323464
MD
1405 elem = bt_array_index(defarray, i);
1406 uuidval[i] = bt_get_unsigned_int(elem);
0f980a35 1407 }
ec323464
MD
1408 ret = babeltrace_uuid_compare(td->uuid, uuidval);
1409 if (ret) {
1410 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1411 return -EINVAL;
1412 }
1413 }
0f980a35 1414
ec323464
MD
1415 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1416 if (len_index >= 0) {
1417 struct bt_definition *field;
0f980a35 1418
ec323464
MD
1419 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1420 stream_id = bt_get_unsigned_int(field);
0f980a35 1421 }
ec323464 1422 }
0f980a35 1423
ec323464
MD
1424 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1425 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1426 stream_id,
1427 file_stream->parent.stream_id);
1428 return -EINVAL;
1429 }
1430 if (first_packet) {
1431 file_stream->parent.stream_id = stream_id;
1432 if (stream_id >= td->streams->len) {
1433 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
0f980a35
MD
1434 return -EINVAL;
1435 }
ec323464
MD
1436 stream = g_ptr_array_index(td->streams, stream_id);
1437 if (!stream) {
1438 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1439 return -EINVAL;
0f980a35 1440 }
ec323464
MD
1441 file_stream->parent.stream_class = stream;
1442 ret = create_stream_definitions(td, &file_stream->parent);
1443 if (ret)
1444 return ret;
1445 }
dc48ecad 1446
ec323464
MD
1447 if (file_stream->parent.stream_packet_context) {
1448 /* Read packet context */
1449 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1450 if (ret) {
1451 if (ret == -EFAULT)
1452 goto retry;
888ec52a 1453 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
ec323464
MD
1454 return ret;
1455 }
1456 /* read content size from header */
1457 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1458 if (len_index >= 0) {
1459 struct bt_definition *field;
dc48ecad 1460
ec323464
MD
1461 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1462 packet_index.content_size = bt_get_unsigned_int(field);
1463 } else {
1464 /* Use file size for packet size */
1465 packet_index.content_size = filesize * CHAR_BIT;
1466 }
75cc2c35 1467
ec323464
MD
1468 /* read packet size from header */
1469 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1470 if (len_index >= 0) {
1471 struct bt_definition *field;
75cc2c35 1472
ec323464
MD
1473 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1474 packet_index.packet_size = bt_get_unsigned_int(field);
1475 } else {
1476 /* Use content size if non-zero, else file size */
1477 packet_index.packet_size = packet_index.content_size ? : filesize * CHAR_BIT;
1478 }
41e82e00 1479
ec323464
MD
1480 /* read timestamp begin from header */
1481 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1482 if (len_index >= 0) {
1483 struct bt_definition *field;
41e82e00 1484
ec323464
MD
1485 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1486 packet_index.timestamp_begin = bt_get_unsigned_int(field);
4c62e2d8 1487 if (file_stream->parent.stream_class->trace->parent.collection) {
ec323464
MD
1488 packet_index.timestamp_begin =
1489 ctf_get_real_timestamp(
1490 &file_stream->parent,
1491 packet_index.timestamp_begin);
41e82e00 1492 }
0f980a35 1493 }
546293fa 1494
ec323464
MD
1495 /* read timestamp end from header */
1496 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1497 if (len_index >= 0) {
1498 struct bt_definition *field;
1499
1500 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1501 packet_index.timestamp_end = bt_get_unsigned_int(field);
4c62e2d8 1502 if (file_stream->parent.stream_class->trace->parent.collection) {
ec323464
MD
1503 packet_index.timestamp_end =
1504 ctf_get_real_timestamp(
1505 &file_stream->parent,
1506 packet_index.timestamp_end);
1507 }
546293fa
MD
1508 }
1509
ec323464
MD
1510 /* read events discarded from header */
1511 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1512 if (len_index >= 0) {
1513 struct bt_definition *field;
1514
1515 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1516 packet_index.events_discarded = bt_get_unsigned_int(field);
1517 packet_index.events_discarded_len = bt_get_int_len(field);
546293fa 1518 }
ec323464
MD
1519 } else {
1520 /* Use file size for packet size */
1521 packet_index.content_size = filesize * CHAR_BIT;
1522 /* Use content size if non-zero, else file size */
1523 packet_index.packet_size = packet_index.content_size ? : filesize * CHAR_BIT;
1524 }
1525
1526 /* Validate content size and packet size values */
1527 if (packet_index.content_size > packet_index.packet_size) {
1528 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1529 packet_index.content_size, packet_index.packet_size);
1530 return -EINVAL;
1531 }
1532
1533 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1534 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1535 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1536 return -EINVAL;
1537 }
546293fa 1538
ec323464
MD
1539 /* Save position after header and context */
1540 packet_index.data_offset = pos->offset;
0f980a35 1541
ec323464
MD
1542 /* add index to packet array */
1543 g_array_append_val(file_stream->pos.packet_cycles_index, packet_index);
0f980a35 1544
ec323464
MD
1545 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1546
1547 return 0;
1548
1549 /* Retry with larger mapping */
1550retry:
1551 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1552 /*
1553 * Reached EOF, but still expecting header/context data.
1554 */
1555 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1556 return -EFAULT;
1557 }
1558 /* Double the mapping len, and retry */
1559 tmp_map_len = packet_map_len << 1;
1560 if (tmp_map_len >> 1 != packet_map_len) {
1561 /* Overflow */
888ec52a 1562 fprintf(stderr, "[error] Packet mapping length overflow\n");
ec323464 1563 return -EFAULT;
0f980a35 1564 }
ec323464
MD
1565 packet_map_len = tmp_map_len;
1566 goto begin;
1567}
1568
1569static
1570int create_stream_packet_index(struct ctf_trace *td,
1571 struct ctf_file_stream *file_stream)
1572{
1573 struct ctf_stream_pos *pos;
1574 struct stat filestats;
1575 int ret;
1576
1577 pos = &file_stream->pos;
0f980a35 1578
ec323464
MD
1579 ret = fstat(pos->fd, &filestats);
1580 if (ret < 0)
1581 return ret;
1582
1583 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1584 ret = create_stream_one_packet_index(pos, td, file_stream,
1585 filestats.st_size);
1586 if (ret)
1587 return ret;
1588 }
0f980a35
MD
1589 return 0;
1590}
1591
e28d4618 1592static
9e88d150 1593int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618
MD
1594{
1595 int ret;
1596
1597 if (td->packet_header_decl) {
0d69b916 1598 struct bt_definition *definition =
e28d4618
MD
1599 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1600 stream->parent_def_scope, 0, 0, "trace.packet.header");
1601 if (!definition) {
1602 ret = -EINVAL;
1603 goto error;
1604 }
1605 stream->trace_packet_header =
1606 container_of(definition, struct definition_struct, p);
1607 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1608 }
1609
1610 return 0;
1611
1612error:
888ec52a 1613 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
e28d4618
MD
1614 return ret;
1615}
1616
0f980a35
MD
1617/*
1618 * Note: many file streams can inherit from the same stream class
1619 * description (metadata).
1620 */
1621static
b086c01a 1622int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1cf393f6 1623 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
b086c01a 1624 int whence))
0f980a35 1625{
f824ae04 1626 int ret, fd, closeret;
0f980a35 1627 struct ctf_file_stream *file_stream;
ff075710 1628 struct stat statbuf;
0f980a35 1629
ff075710
MD
1630 fd = openat(td->dirfd, path, flags);
1631 if (fd < 0) {
a569a564 1632 perror("File stream openat()");
ff075710 1633 ret = fd;
0f980a35 1634 goto error;
a569a564 1635 }
ff075710
MD
1636
1637 /* Don't try to mmap subdirectories. Skip them, return success. */
1638 ret = fstat(fd, &statbuf);
1639 if (ret) {
1640 perror("File stream fstat()");
1641 goto fstat_error;
1642 }
1643 if (S_ISDIR(statbuf.st_mode)) {
1644 fprintf(stderr, "[warning] Skipping directory '%s' found in trace\n", path);
1645 ret = 0;
1646 goto fd_is_dir_ok;
1647 }
1648
0f980a35 1649 file_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 1650 file_stream->pos.last_offset = LAST_OFFSET_POISON;
b086c01a 1651
87148dc7
MD
1652 strncpy(file_stream->parent.path, path, PATH_MAX);
1653 file_stream->parent.path[PATH_MAX - 1] = '\0';
1654
06789ffd
MD
1655 if (packet_seek) {
1656 file_stream->pos.packet_seek = packet_seek;
b086c01a 1657 } else {
06789ffd 1658 fprintf(stderr, "[error] packet_seek function undefined.\n");
b086c01a
JD
1659 ret = -1;
1660 goto error_def;
1661 }
1662
ca334c72 1663 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
f824ae04
MD
1664 if (ret)
1665 goto error_def;
2d0bea29 1666 ret = create_trace_definitions(td, &file_stream->parent);
e28d4618
MD
1667 if (ret)
1668 goto error_def;
25ccc85b 1669 /*
50052405 1670 * For now, only a single clock per trace is supported.
25ccc85b 1671 */
7ec78969 1672 file_stream->parent.current_clock = td->parent.single_clock;
0f980a35 1673 ret = create_stream_packet_index(td, file_stream);
888ec52a
MD
1674 if (ret) {
1675 fprintf(stderr, "[error] Stream index creation error.\n");
0f980a35 1676 goto error_index;
888ec52a 1677 }
0f980a35 1678 /* Add stream file to stream class */
2d0bea29
MD
1679 g_ptr_array_add(file_stream->parent.stream_class->streams,
1680 &file_stream->parent);
0f980a35
MD
1681 return 0;
1682
1683error_index:
2d0bea29 1684 if (file_stream->parent.trace_packet_header)
13fad8b6 1685 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
e28d4618 1686error_def:
f824ae04
MD
1687 closeret = ctf_fini_pos(&file_stream->pos);
1688 if (closeret) {
1689 fprintf(stderr, "Error on ctf_fini_pos\n");
1690 }
0f980a35 1691 g_free(file_stream);
ff075710
MD
1692fd_is_dir_ok:
1693fstat_error:
f824ae04
MD
1694 closeret = close(fd);
1695 if (closeret) {
1696 perror("Error on fd close");
1697 }
0f980a35 1698error:
65102a8c
MD
1699 return ret;
1700}
1701
bbefb8dd 1702static
5b80ddfb 1703int ctf_open_trace_read(struct ctf_trace *td,
8c250d87 1704 const char *path, int flags,
1cf393f6 1705 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 1706 int whence), FILE *metadata_fp)
bbefb8dd 1707{
f824ae04 1708 int ret, closeret;
65102a8c
MD
1709 struct dirent *dirent;
1710 struct dirent *diriter;
1711 size_t dirent_len;
bbefb8dd 1712
46322b33 1713 td->flags = flags;
bbefb8dd
MD
1714
1715 /* Open trace directory */
46322b33
MD
1716 td->dir = opendir(path);
1717 if (!td->dir) {
6a6b384c 1718 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
bbefb8dd
MD
1719 ret = -ENOENT;
1720 goto error;
1721 }
1722
46322b33
MD
1723 td->dirfd = open(path, 0);
1724 if (td->dirfd < 0) {
6a6b384c 1725 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
a569a564
MD
1726 perror("Trace directory open");
1727 ret = -errno;
65102a8c
MD
1728 goto error_dirfd;
1729 }
caf929fa
MD
1730 strncpy(td->parent.path, path, sizeof(td->parent.path));
1731 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
0f980a35 1732
65102a8c
MD
1733 /*
1734 * Keep the metadata file separate.
1735 */
bbefb8dd 1736
06789ffd 1737 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
65102a8c 1738 if (ret) {
6a6b384c 1739 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
65102a8c
MD
1740 goto error_metadata;
1741 }
bbefb8dd
MD
1742
1743 /*
1744 * Open each stream: for each file, try to open, check magic
1745 * number, and get the stream ID to add to the right location in
1746 * the stream array.
bbefb8dd
MD
1747 */
1748
65102a8c 1749 dirent_len = offsetof(struct dirent, d_name) +
46322b33 1750 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 1751
65102a8c 1752 dirent = malloc(dirent_len);
bbefb8dd 1753
65102a8c 1754 for (;;) {
46322b33 1755 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 1756 if (ret) {
3394d22e 1757 fprintf(stderr, "[error] Readdir error.\n");
65102a8c 1758 goto readdir_error;
65102a8c
MD
1759 }
1760 if (!diriter)
1761 break;
d8ea2d29
MD
1762 /* Ignore hidden files, ., .. and metadata. */
1763 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
1764 || !strcmp(diriter->d_name, "..")
1765 || !strcmp(diriter->d_name, "metadata"))
1766 continue;
06789ffd
MD
1767 ret = ctf_open_file_stream_read(td, diriter->d_name,
1768 flags, packet_seek);
dc48ecad 1769 if (ret) {
3394d22e 1770 fprintf(stderr, "[error] Open file stream error.\n");
dc48ecad
MD
1771 goto readdir_error;
1772 }
65102a8c 1773 }
bbefb8dd 1774
65102a8c 1775 free(dirent);
bbefb8dd 1776 return 0;
65102a8c
MD
1777
1778readdir_error:
1779 free(dirent);
1780error_metadata:
f824ae04
MD
1781 closeret = close(td->dirfd);
1782 if (closeret) {
1783 perror("Error on fd close");
1784 }
65102a8c 1785error_dirfd:
f824ae04
MD
1786 closeret = closedir(td->dir);
1787 if (closeret) {
1788 perror("Error on closedir");
1789 }
bbefb8dd
MD
1790error:
1791 return ret;
1792}
1793
03798a93
JD
1794/*
1795 * ctf_open_trace: Open a CTF trace and index it.
1796 * Note that the user must seek the trace after the open (using the iterator)
1797 * since the index creation read it entirely.
1798 */
e9378815 1799static
1b8455b7 1800struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1cf393f6 1801 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 1802 int whence), FILE *metadata_fp)
bbefb8dd 1803{
46322b33 1804 struct ctf_trace *td;
bbefb8dd
MD
1805 int ret;
1806
2715de36
MD
1807 /*
1808 * If packet_seek is NULL, we provide our default version.
1809 */
1810 if (!packet_seek)
1811 packet_seek = ctf_packet_seek;
1812
46322b33 1813 td = g_new0(struct ctf_trace, 1);
bbefb8dd 1814
8c572eba 1815 switch (flags & O_ACCMODE) {
bbefb8dd 1816 case O_RDONLY:
b61922b5 1817 if (!path) {
3394d22e 1818 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
b61922b5
MD
1819 goto error;
1820 }
06789ffd 1821 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
bbefb8dd
MD
1822 if (ret)
1823 goto error;
1824 break;
989c73bc 1825 case O_RDWR:
3394d22e 1826 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
46322b33 1827 goto error;
bbefb8dd 1828 default:
3394d22e 1829 fprintf(stderr, "[error] Incorrect open flags.\n");
bbefb8dd
MD
1830 goto error;
1831 }
1832
46322b33 1833 return &td->parent;
bbefb8dd
MD
1834error:
1835 g_free(td);
1836 return NULL;
1837}
1838
2e937fb4 1839static
f571dfb1 1840void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
c150f912 1841 struct bt_mmap_stream *mmap_info)
f571dfb1
JD
1842{
1843 pos->mmap_offset = 0;
1844 pos->packet_size = 0;
1845 pos->content_size = 0;
1846 pos->content_size_loc = NULL;
1847 pos->fd = mmap_info->fd;
aee35fcc 1848 pos->base_mma = NULL;
f571dfb1
JD
1849 pos->offset = 0;
1850 pos->dummy = false;
1851 pos->cur_index = 0;
03798a93
JD
1852 pos->packet_cycles_index = NULL;
1853 pos->packet_real_index = NULL;
f571dfb1
JD
1854 pos->prot = PROT_READ;
1855 pos->flags = MAP_PRIVATE;
1856 pos->parent.rw_table = read_dispatch_table;
1857 pos->parent.event_cb = ctf_read_event;
1858}
1859
1860static
1861int prepare_mmap_stream_definition(struct ctf_trace *td,
1862 struct ctf_file_stream *file_stream)
1863{
f380e105 1864 struct ctf_stream_declaration *stream;
f571dfb1
JD
1865 uint64_t stream_id = 0;
1866 int ret;
1867
1868 file_stream->parent.stream_id = stream_id;
1869 if (stream_id >= td->streams->len) {
3394d22e 1870 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
1871 "in metadata.\n", stream_id);
1872 ret = -EINVAL;
1873 goto end;
1874 }
1875 stream = g_ptr_array_index(td->streams, stream_id);
1876 if (!stream) {
3394d22e 1877 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
1878 "in metadata.\n", stream_id);
1879 ret = -EINVAL;
1880 goto end;
1881 }
1882 file_stream->parent.stream_class = stream;
1883 ret = create_stream_definitions(td, &file_stream->parent);
1884end:
1885 return ret;
1886}
1887
1888static
1889int ctf_open_mmap_stream_read(struct ctf_trace *td,
c150f912 1890 struct bt_mmap_stream *mmap_info,
1cf393f6 1891 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
f571dfb1
JD
1892 int whence))
1893{
1894 int ret;
1895 struct ctf_file_stream *file_stream;
1896
1897 file_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 1898 file_stream->pos.last_offset = LAST_OFFSET_POISON;
f571dfb1
JD
1899 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1900
06789ffd 1901 file_stream->pos.packet_seek = packet_seek;
f571dfb1
JD
1902
1903 ret = create_trace_definitions(td, &file_stream->parent);
1904 if (ret) {
1905 goto error_def;
1906 }
1907
1908 ret = prepare_mmap_stream_definition(td, file_stream);
1909 if (ret)
1910 goto error_index;
1911
f7bbd502 1912 /*
50052405 1913 * For now, only a single clock per trace is supported.
f7bbd502 1914 */
7ec78969 1915 file_stream->parent.current_clock = td->parent.single_clock;
f7bbd502 1916
f571dfb1
JD
1917 /* Add stream file to stream class */
1918 g_ptr_array_add(file_stream->parent.stream_class->streams,
1919 &file_stream->parent);
1920 return 0;
1921
1922error_index:
1923 if (file_stream->parent.trace_packet_header)
13fad8b6 1924 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
f571dfb1
JD
1925error_def:
1926 g_free(file_stream);
1927 return ret;
1928}
1929
2e937fb4 1930static
f571dfb1 1931int ctf_open_mmap_trace_read(struct ctf_trace *td,
c150f912 1932 struct bt_mmap_stream_list *mmap_list,
1cf393f6 1933 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
f571dfb1
JD
1934 int whence),
1935 FILE *metadata_fp)
1936{
1937 int ret;
c150f912 1938 struct bt_mmap_stream *mmap_info;
f571dfb1 1939
06789ffd 1940 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
f571dfb1
JD
1941 if (ret) {
1942 goto error;
1943 }
1944
1945 /*
1946 * for each stream, try to open, check magic number, and get the
1947 * stream ID to add to the right location in the stream array.
1948 */
3122e6f0 1949 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
06789ffd 1950 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
f571dfb1 1951 if (ret) {
3394d22e 1952 fprintf(stderr, "[error] Open file mmap stream error.\n");
f571dfb1
JD
1953 goto error;
1954 }
1955 }
1956
1957 return 0;
1958
1959error:
1960 return ret;
1961}
1962
1963static
1b8455b7 1964struct bt_trace_descriptor *ctf_open_mmap_trace(
c150f912 1965 struct bt_mmap_stream_list *mmap_list,
1cf393f6 1966 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
20d0dcf9 1967 int whence),
f571dfb1
JD
1968 FILE *metadata_fp)
1969{
1970 struct ctf_trace *td;
1971 int ret;
1972
1973 if (!metadata_fp) {
1974 fprintf(stderr, "[error] No metadata file pointer associated, "
1975 "required for mmap parsing\n");
1976 goto error;
1977 }
06789ffd
MD
1978 if (!packet_seek) {
1979 fprintf(stderr, "[error] packet_seek function undefined.\n");
f571dfb1
JD
1980 goto error;
1981 }
1982 td = g_new0(struct ctf_trace, 1);
06789ffd 1983 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
f571dfb1
JD
1984 if (ret)
1985 goto error_free;
1986
1987 return &td->parent;
1988
1989error_free:
1990 g_free(td);
1991error:
1992 return NULL;
1993}
1994
03798a93 1995static
1b8455b7 1996int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
03798a93
JD
1997{
1998 int i, j, k;
1999 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2000
2001 /* for each stream_class */
2002 for (i = 0; i < td->streams->len; i++) {
2003 struct ctf_stream_declaration *stream_class;
2004
2005 stream_class = g_ptr_array_index(td->streams, i);
2006 if (!stream_class)
2007 continue;
2008 /* for each file_stream */
2009 for (j = 0; j < stream_class->streams->len; j++) {
2010 struct ctf_stream_definition *stream;
2011 struct ctf_stream_pos *stream_pos;
2012 struct ctf_file_stream *cfs;
2013
2014 stream = g_ptr_array_index(stream_class->streams, j);
2015 if (!stream)
2016 continue;
2017 cfs = container_of(stream, struct ctf_file_stream,
2018 parent);
2019 stream_pos = &cfs->pos;
afe9cd4a
JD
2020 if (!stream_pos->packet_cycles_index)
2021 continue;
2022
03798a93
JD
2023 for (k = 0; k < stream_pos->packet_cycles_index->len; k++) {
2024 struct packet_index *index;
2025 struct packet_index new_index;
2026
2027 index = &g_array_index(stream_pos->packet_cycles_index,
2028 struct packet_index, k);
2029 memcpy(&new_index, index,
2030 sizeof(struct packet_index));
2031 new_index.timestamp_begin =
2032 ctf_get_real_timestamp(stream,
2033 index->timestamp_begin);
2034 new_index.timestamp_end =
2035 ctf_get_real_timestamp(stream,
2036 index->timestamp_end);
2037 g_array_append_val(stream_pos->packet_real_index,
2038 new_index);
2039 }
2040 }
2041 }
2042 return 0;
2043}
2044
0f980a35 2045static
f824ae04 2046int ctf_close_file_stream(struct ctf_file_stream *file_stream)
0f980a35 2047{
f824ae04
MD
2048 int ret;
2049
2050 ret = ctf_fini_pos(&file_stream->pos);
2051 if (ret) {
2052 fprintf(stderr, "Error on ctf_fini_pos\n");
2053 return -1;
2054 }
2055 ret = close(file_stream->pos.fd);
2056 if (ret) {
2057 perror("Error closing file fd");
2058 return -1;
2059 }
2060 return 0;
0f980a35
MD
2061}
2062
e9378815 2063static
1b8455b7 2064int ctf_close_trace(struct bt_trace_descriptor *tdp)
bbefb8dd 2065{
46322b33 2066 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
08c82b90 2067 int ret;
0f980a35 2068
46322b33 2069 if (td->streams) {
08c82b90
MD
2070 int i;
2071
46322b33 2072 for (i = 0; i < td->streams->len; i++) {
f380e105 2073 struct ctf_stream_declaration *stream;
0f980a35 2074 int j;
e9378815 2075
46322b33 2076 stream = g_ptr_array_index(td->streams, i);
e9378815
MD
2077 if (!stream)
2078 continue;
2d0bea29 2079 for (j = 0; j < stream->streams->len; j++) {
0f980a35 2080 struct ctf_file_stream *file_stream;
15d4fe3c
JD
2081 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2082 struct ctf_file_stream, parent);
f824ae04
MD
2083 ret = ctf_close_file_stream(file_stream);
2084 if (ret)
2085 return ret;
0f980a35 2086 }
e003ab50 2087 }
e003ab50 2088 }
15d4fe3c 2089 ctf_destroy_metadata(td);
f824ae04
MD
2090 ret = close(td->dirfd);
2091 if (ret) {
2092 perror("Error closing dirfd");
2093 return ret;
2094 }
2095 ret = closedir(td->dir);
2096 if (ret) {
2097 perror("Error closedir");
2098 return ret;
2099 }
7237592a 2100 free(td->metadata_string);
bbefb8dd 2101 g_free(td);
f824ae04 2102 return 0;
bbefb8dd
MD
2103}
2104
98a04903 2105static
1b8455b7 2106void ctf_set_context(struct bt_trace_descriptor *descriptor,
98a04903
JD
2107 struct bt_context *ctx)
2108{
2109 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2110 parent);
2111
45807148 2112 td->parent.ctx = ctx;
98a04903
JD
2113}
2114
2115static
1b8455b7 2116void ctf_set_handle(struct bt_trace_descriptor *descriptor,
98a04903
JD
2117 struct bt_trace_handle *handle)
2118{
2119 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2120 parent);
2121
4d086981 2122 td->parent.handle = handle;
98a04903
JD
2123}
2124
95febab3 2125static
7fb21036 2126void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
2127{
2128 int ret;
2129
4c8bfb7e 2130 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
2131 ret = bt_register_format(&ctf_format);
2132 assert(!ret);
2133}
698f0fe4 2134
95febab3
MD
2135static
2136void __attribute__((destructor)) ctf_exit(void)
2137{
2138 bt_unregister_format(&ctf_format);
2139}
This page took 0.149154 seconds and 4 git commands to generate.