Standardise spelling of debug info
[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>
837b0013 30#include <babeltrace/format-internal.h>
fc93b2bd 31#include <babeltrace/ctf/types.h>
bbefb8dd 32#include <babeltrace/ctf/metadata.h>
70bd0a12 33#include <babeltrace/babeltrace-internal.h>
e4195791 34#include <babeltrace/ctf/events-internal.h>
98a04903
JD
35#include <babeltrace/trace-handle-internal.h>
36#include <babeltrace/context-internal.h>
4cb26dfb 37#include <babeltrace/compat/uuid.h>
43e34335 38#include <babeltrace/endian.h>
eb75a494 39#include <babeltrace/trace-debug-info.h>
0ace7505 40#include <babeltrace/ctf/ctf-index.h>
0f980a35 41#include <inttypes.h>
b4c19c1e 42#include <stdio.h>
0f980a35 43#include <sys/mman.h>
bbefb8dd 44#include <errno.h>
bbefb8dd 45#include <sys/types.h>
65102a8c 46#include <sys/stat.h>
bbefb8dd 47#include <fcntl.h>
65102a8c 48#include <dirent.h>
bbefb8dd 49#include <glib.h>
65102a8c
MD
50#include <unistd.h>
51#include <stdlib.h>
52
65102a8c
MD
53#include "metadata/ctf-scanner.h"
54#include "metadata/ctf-parser.h"
55#include "metadata/ctf-ast.h"
c34ea0fa 56#include "events-private.h"
68ef7952 57#include <babeltrace/compat/memstream.h>
edcad9c1 58#include <babeltrace/compat/fcntl.h>
65102a8c 59
ec323464
MD
60#define LOG2_CHAR_BIT 3
61
62/*
63 * Length of first attempt at mapping a packet header, in bits.
64 */
65#define DEFAULT_HEADER_LEN (getpagesize() * CHAR_BIT)
66
0f980a35 67/*
ec323464 68 * Lenght of packet to write, in bits.
0f980a35 69 */
8c572eba 70#define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
0f980a35 71
7d97fad9
MD
72#define NSEC_PER_SEC 1000000000ULL
73
0ace7505
JD
74#define INDEX_PATH "./index/%s.idx"
75
03798a93 76int opt_clock_cycles,
7d97fad9
MD
77 opt_clock_seconds,
78 opt_clock_date,
79 opt_clock_gmt;
80
81uint64_t opt_clock_offset;
65923160 82uint64_t opt_clock_offset_ns;
7d97fad9 83
65102a8c 84extern int yydebug;
3be1e3c9 85char *opt_debug_info_dir;
b7b61ced 86char *opt_debug_info_target_prefix;
bbefb8dd 87
2654fe9b
MD
88/*
89 * TODO: babeltrace_ctf_console_output ensures that we only print
90 * discarded events when ctf-text plugin is used. Should be cleaned up
91 * with the plugin system redesign.
92 */
93int babeltrace_ctf_console_output;
94
e9378815 95static
1b8455b7 96struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1cf393f6 97 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
20d0dcf9
MD
98 int whence),
99 FILE *metadata_fp);
e9378815 100static
1b8455b7 101struct bt_trace_descriptor *ctf_open_mmap_trace(
c150f912 102 struct bt_mmap_stream_list *mmap_list,
1cf393f6 103 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
20d0dcf9 104 int whence),
f571dfb1 105 FILE *metadata_fp);
98a04903 106static
1b8455b7 107void ctf_set_context(struct bt_trace_descriptor *descriptor,
98a04903
JD
108 struct bt_context *ctx);
109static
1b8455b7 110void ctf_set_handle(struct bt_trace_descriptor *descriptor,
98a04903 111 struct bt_trace_handle *handle);
f571dfb1
JD
112
113static
1b8455b7 114int ctf_close_trace(struct bt_trace_descriptor *descriptor);
30c276af 115static
1b8455b7 116uint64_t ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
03798a93 117 struct bt_trace_handle *handle, enum bt_clock_type type);
30c276af 118static
1b8455b7 119uint64_t ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
03798a93
JD
120 struct bt_trace_handle *handle, enum bt_clock_type type);
121static
1b8455b7 122int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp);
fc93b2bd 123
1ae19169
MD
124static
125rw_dispatch read_dispatch_table[] = {
d11e9c49
MD
126 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
127 [ CTF_TYPE_FLOAT ] = ctf_float_read,
128 [ CTF_TYPE_ENUM ] = ctf_enum_read,
129 [ CTF_TYPE_STRING ] = ctf_string_read,
130 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
131 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
132 [ CTF_TYPE_ARRAY ] = ctf_array_read,
133 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
d11e9c49
MD
134};
135
1ae19169
MD
136static
137rw_dispatch write_dispatch_table[] = {
d11e9c49
MD
138 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
139 [ CTF_TYPE_FLOAT ] = ctf_float_write,
140 [ CTF_TYPE_ENUM ] = ctf_enum_write,
141 [ CTF_TYPE_STRING ] = ctf_string_write,
142 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
143 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
144 [ CTF_TYPE_ARRAY ] = ctf_array_write,
145 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
d11e9c49
MD
146};
147
1ae19169 148static
37b99bdb 149struct bt_format ctf_format = {
bbefb8dd 150 .open_trace = ctf_open_trace,
f571dfb1 151 .open_mmap_trace = ctf_open_mmap_trace,
bbefb8dd 152 .close_trace = ctf_close_trace,
98a04903
JD
153 .set_context = ctf_set_context,
154 .set_handle = ctf_set_handle,
30c276af
JD
155 .timestamp_begin = ctf_timestamp_begin,
156 .timestamp_end = ctf_timestamp_end,
03798a93 157 .convert_index_timestamp = ctf_convert_index_timestamp,
fc93b2bd
MD
158};
159
30c276af 160static
1b8455b7 161uint64_t ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
03798a93 162 struct bt_trace_handle *handle, enum bt_clock_type type)
30c276af
JD
163{
164 struct ctf_trace *tin;
165 uint64_t begin = ULLONG_MAX;
166 int i, j;
167
168 tin = container_of(descriptor, struct ctf_trace, parent);
169
170 if (!tin)
171 goto error;
172
173 /* for each stream_class */
174 for (i = 0; i < tin->streams->len; i++) {
175 struct ctf_stream_declaration *stream_class;
176
177 stream_class = g_ptr_array_index(tin->streams, i);
1b01ffc2
MD
178 if (!stream_class)
179 continue;
30c276af
JD
180 /* for each file_stream */
181 for (j = 0; j < stream_class->streams->len; j++) {
182 struct ctf_stream_definition *stream;
183 struct ctf_file_stream *cfs;
184 struct ctf_stream_pos *stream_pos;
185 struct packet_index *index;
186
187 stream = g_ptr_array_index(stream_class->streams, j);
188 cfs = container_of(stream, struct ctf_file_stream,
189 parent);
190 stream_pos = &cfs->pos;
191
992e8cc0 192 if (!stream_pos->packet_index)
03798a93
JD
193 goto error;
194
992e8cc0 195 if (stream_pos->packet_index->len <= 0)
afe9cd4a
JD
196 continue;
197
992e8cc0
MD
198 index = &g_array_index(stream_pos->packet_index,
199 struct packet_index,
200 stream_pos->packet_index->len - 1);
03798a93 201 if (type == BT_CLOCK_REAL) {
992e8cc0
MD
202 if (index->ts_real.timestamp_begin < begin)
203 begin = index->ts_real.timestamp_begin;
03798a93 204 } else if (type == BT_CLOCK_CYCLES) {
992e8cc0
MD
205 if (index->ts_cycles.timestamp_begin < begin)
206 begin = index->ts_cycles.timestamp_begin;
03798a93
JD
207 } else {
208 goto error;
209 }
30c276af
JD
210 }
211 }
212
213 return begin;
214
215error:
216 return -1ULL;
217}
218
219static
1b8455b7 220uint64_t ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
03798a93 221 struct bt_trace_handle *handle, enum bt_clock_type type)
30c276af
JD
222{
223 struct ctf_trace *tin;
224 uint64_t end = 0;
225 int i, j;
226
227 tin = container_of(descriptor, struct ctf_trace, parent);
228
229 if (!tin)
230 goto error;
231
232 /* for each stream_class */
233 for (i = 0; i < tin->streams->len; i++) {
234 struct ctf_stream_declaration *stream_class;
235
236 stream_class = g_ptr_array_index(tin->streams, i);
1b01ffc2
MD
237 if (!stream_class)
238 continue;
30c276af
JD
239 /* for each file_stream */
240 for (j = 0; j < stream_class->streams->len; j++) {
241 struct ctf_stream_definition *stream;
242 struct ctf_file_stream *cfs;
243 struct ctf_stream_pos *stream_pos;
244 struct packet_index *index;
245
246 stream = g_ptr_array_index(stream_class->streams, j);
247 cfs = container_of(stream, struct ctf_file_stream,
248 parent);
249 stream_pos = &cfs->pos;
250
992e8cc0 251 if (!stream_pos->packet_index)
03798a93
JD
252 goto error;
253
992e8cc0 254 if (stream_pos->packet_index->len <= 0)
afe9cd4a
JD
255 continue;
256
992e8cc0
MD
257 index = &g_array_index(stream_pos->packet_index,
258 struct packet_index,
259 stream_pos->packet_index->len - 1);
03798a93 260 if (type == BT_CLOCK_REAL) {
992e8cc0
MD
261 if (index->ts_real.timestamp_end > end)
262 end = index->ts_real.timestamp_end;
03798a93 263 } else if (type == BT_CLOCK_CYCLES) {
992e8cc0
MD
264 if (index->ts_cycles.timestamp_end > end)
265 end = index->ts_cycles.timestamp_end;
03798a93
JD
266 } else {
267 goto error;
268 }
30c276af
JD
269 }
270 }
271
272 return end;
273
274error:
275 return -1ULL;
276}
277
25ccc85b 278/*
03798a93 279 * Update stream current timestamp
25ccc85b 280 */
2b9a764d 281static
9e88d150 282void ctf_update_timestamp(struct ctf_stream_definition *stream,
2b9a764d
MD
283 struct definition_integer *integer_definition)
284{
285 struct declaration_integer *integer_declaration =
286 integer_definition->declaration;
287 uint64_t oldval, newval, updateval;
288
7f3f572b 289 if (unlikely(integer_declaration->len == 64)) {
03798a93 290 stream->cycles_timestamp = integer_definition->value._unsigned;
03798a93
JD
291 stream->real_timestamp = ctf_get_real_timestamp(stream,
292 stream->cycles_timestamp);
2b9a764d
MD
293 return;
294 }
295 /* keep low bits */
03798a93 296 oldval = stream->cycles_timestamp;
2b9a764d
MD
297 oldval &= (1ULL << integer_declaration->len) - 1;
298 newval = integer_definition->value._unsigned;
299 /* Test for overflow by comparing low bits */
300 if (newval < oldval)
301 newval += 1ULL << integer_declaration->len;
302 /* updateval contains old high bits, and new low bits (sum) */
03798a93 303 updateval = stream->cycles_timestamp;
2b9a764d
MD
304 updateval &= ~((1ULL << integer_declaration->len) - 1);
305 updateval += newval;
03798a93
JD
306 stream->cycles_timestamp = updateval;
307
308 /* convert to real timestamp */
03798a93
JD
309 stream->real_timestamp = ctf_get_real_timestamp(stream,
310 stream->cycles_timestamp);
2b9a764d
MD
311}
312
25ccc85b
MD
313/*
314 * Print timestamp, rescaling clock frequency to nanoseconds and
315 * applying offsets as needed (unix time).
316 */
2e937fb4 317static
03798a93 318void ctf_print_timestamp_real(FILE *fp,
9e88d150 319 struct ctf_stream_definition *stream,
7d97fad9
MD
320 uint64_t timestamp)
321{
322 uint64_t ts_sec = 0, ts_nsec;
7d97fad9 323
03798a93 324 ts_nsec = timestamp;
7d97fad9 325
65923160
IJ
326 /* Add command-line offset in ns*/
327 ts_nsec += opt_clock_offset_ns;
328
c34ea0fa 329 /* Add command-line offset */
7d97fad9
MD
330 ts_sec += opt_clock_offset;
331
332 ts_sec += ts_nsec / NSEC_PER_SEC;
333 ts_nsec = ts_nsec % NSEC_PER_SEC;
334
335 if (!opt_clock_seconds) {
336 struct tm tm;
337 time_t time_s = (time_t) ts_sec;
338
339 if (!opt_clock_gmt) {
340 struct tm *res;
341
342 res = localtime_r(&time_s, &tm);
343 if (!res) {
344 fprintf(stderr, "[warning] Unable to get localtime.\n");
345 goto seconds;
346 }
347 } else {
348 struct tm *res;
349
350 res = gmtime_r(&time_s, &tm);
351 if (!res) {
352 fprintf(stderr, "[warning] Unable to get gmtime.\n");
353 goto seconds;
354 }
355 }
356 if (opt_clock_date) {
357 char timestr[26];
358 size_t res;
359
360 /* Print date and time */
361 res = strftime(timestr, sizeof(timestr),
362 "%F ", &tm);
363 if (!res) {
364 fprintf(stderr, "[warning] Unable to print ascii time.\n");
365 goto seconds;
366 }
367 fprintf(fp, "%s", timestr);
368 }
369 /* Print time in HH:MM:SS.ns */
370 fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
371 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
372 goto end;
373 }
374seconds:
375 fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
376 ts_sec, ts_nsec);
377
378end:
379 return;
380}
381
03798a93
JD
382/*
383 * Print timestamp, in cycles
384 */
2e937fb4 385static
03798a93
JD
386void ctf_print_timestamp_cycles(FILE *fp,
387 struct ctf_stream_definition *stream,
388 uint64_t timestamp)
389{
390 fprintf(fp, "%020" PRIu64, timestamp);
391}
392
393void ctf_print_timestamp(FILE *fp,
394 struct ctf_stream_definition *stream,
395 uint64_t timestamp)
396{
397 if (opt_clock_cycles) {
398 ctf_print_timestamp_cycles(fp, stream, timestamp);
399 } else {
400 ctf_print_timestamp_real(fp, stream, timestamp);
401 }
402}
403
87148dc7
MD
404static
405void print_uuid(FILE *fp, unsigned char *uuid)
406{
407 int i;
408
409 for (i = 0; i < BABELTRACE_UUID_LEN; i++)
410 fprintf(fp, "%x", (unsigned int) uuid[i]);
411}
412
2654fe9b
MD
413/*
414 * Discarded events can be either:
415 * - discarded after end of previous buffer due to buffer full:
416 * happened within range: [ prev_timestamp_end, timestamp_begin ]
417 * - discarded within current buffer due to either event too large or
418 * nested wrap-around:
419 * happened within range: [ timestamp_begin, timestamp_end ]
420 *
421 * Given we have discarded counters of those two types merged into the
422 * events_discarded counter, we need to use the union of those ranges:
423 * [ prev_timestamp_end, timestamp_end ]
2328c2cd
JD
424 *
425 * Lost packets occur if the tracer overwrote some subbuffer(s) before the
426 * consumer had time to extract them. We keep track of those gaps with the
427 * packet sequence number in each packet.
2654fe9b
MD
428 */
429static
2328c2cd 430void ctf_print_discarded_lost(FILE *fp, struct ctf_stream_definition *stream)
87148dc7 431{
2328c2cd
JD
432 if ((!stream->events_discarded && !stream->packets_lost) ||
433 !babeltrace_ctf_console_output) {
2654fe9b
MD
434 return;
435 }
436 fflush(stdout);
2328c2cd
JD
437 if (stream->events_discarded) {
438 fprintf(fp, "[warning] Tracer discarded %" PRIu64 " events between [",
439 stream->events_discarded);
440 } else if (stream->packets_lost) {
441 fprintf(fp, "[warning] Tracer lost %" PRIu64 " trace packets between [",
442 stream->packets_lost);
443 }
87148dc7
MD
444 if (opt_clock_cycles) {
445 ctf_print_timestamp(fp, stream,
2654fe9b 446 stream->prev.cycles.end);
87148dc7
MD
447 fprintf(fp, "] and [");
448 ctf_print_timestamp(fp, stream,
2654fe9b 449 stream->current.cycles.end);
87148dc7
MD
450 } else {
451 ctf_print_timestamp(fp, stream,
2654fe9b 452 stream->prev.real.end);
87148dc7
MD
453 fprintf(fp, "] and [");
454 ctf_print_timestamp(fp, stream,
2654fe9b 455 stream->current.real.end);
87148dc7
MD
456 }
457 fprintf(fp, "] in trace UUID ");
458 print_uuid(fp, stream->stream_class->trace->uuid);
caf929fa 459 if (stream->stream_class->trace->parent.path[0])
87148dc7 460 fprintf(fp, ", at path: \"%s\"",
caf929fa 461 stream->stream_class->trace->parent.path);
87148dc7
MD
462
463 fprintf(fp, ", within stream id %" PRIu64, stream->stream_id);
464 if (stream->path[0])
465 fprintf(fp, ", at relative path: \"%s\"", stream->path);
466 fprintf(fp, ". ");
467 fprintf(fp, "You should consider recording a new trace with larger "
468 "buffers or with fewer events enabled.\n");
469 fflush(fp);
470}
471
31262354 472static
1cf393f6 473int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream)
31262354
MD
474{
475 struct ctf_stream_pos *pos =
476 container_of(ppos, struct ctf_stream_pos, parent);
f380e105 477 struct ctf_stream_declaration *stream_class = stream->stream_class;
c716f83b 478 struct ctf_event_definition *event;
31262354 479 uint64_t id = 0;
31262354
MD
480 int ret;
481
3abe83c7
MD
482 /* We need to check for EOF here for empty files. */
483 if (unlikely(pos->offset == EOF))
484 return EOF;
485
5f643ed7
MD
486 ctf_pos_get_event(pos);
487
90fcbacc
JD
488 /* save the current position as a restore point */
489 pos->last_offset = pos->offset;
90fcbacc 490
3abe83c7
MD
491 /*
492 * This is the EOF check after we've advanced the position in
493 * ctf_pos_get_event.
494 */
7f3f572b 495 if (unlikely(pos->offset == EOF))
31262354 496 return EOF;
8793d9f8 497
500634be
JD
498 /* Stream is inactive for now (live reading). */
499 if (unlikely(pos->content_size == 0))
8793d9f8 500 return EAGAIN;
500634be
JD
501
502 /*
503 * Packet seeked to by ctf_pos_get_event() only contains
504 * headers, no event. Consider stream as inactive (live
505 * reading).
506 */
507 if (unlikely(pos->data_offset == pos->content_size))
a5260047
JD
508 return EAGAIN;
509
5f643ed7 510 assert(pos->offset < pos->content_size);
31262354
MD
511
512 /* Read event header */
7f3f572b 513 if (likely(stream->stream_event_header)) {
a35173fe 514 struct definition_integer *integer_definition;
0d69b916 515 struct bt_definition *variant;
a35173fe 516
e28d4618 517 ret = generic_rw(ppos, &stream->stream_event_header->p);
7f3f572b 518 if (unlikely(ret))
31262354
MD
519 goto error;
520 /* lookup event id */
bf78e2cf 521 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
522 if (integer_definition) {
523 id = integer_definition->value._unsigned;
524 } else {
525 struct definition_enum *enum_definition;
526
9e3274b0 527 enum_definition = bt_lookup_enum(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
528 if (enum_definition) {
529 id = enum_definition->integer->value._unsigned;
530 }
31262354 531 }
764af3f4 532
ebae302b 533 variant = bt_lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e 534 if (variant) {
bf78e2cf 535 integer_definition = bt_lookup_integer(variant, "id", FALSE);
ccdb988e
MD
536 if (integer_definition) {
537 id = integer_definition->value._unsigned;
538 }
539 }
c87a8eb2 540 stream->event_id = id;
ccdb988e 541
764af3f4 542 /* lookup timestamp */
5e2eb0ae 543 stream->has_timestamp = 0;
bf78e2cf 544 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
a35173fe 545 if (integer_definition) {
2b9a764d 546 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 547 stream->has_timestamp = 1;
a35173fe 548 } else {
ccdb988e 549 if (variant) {
bf78e2cf 550 integer_definition = bt_lookup_integer(variant, "timestamp", FALSE);
a35173fe 551 if (integer_definition) {
2b9a764d 552 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 553 stream->has_timestamp = 1;
a35173fe
MD
554 }
555 }
764af3f4 556 }
31262354
MD
557 }
558
559 /* Read stream-declared event context */
e28d4618
MD
560 if (stream->stream_event_context) {
561 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
562 if (ret)
563 goto error;
564 }
565
7f3f572b 566 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 567 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
568 return -EINVAL;
569 }
e28d4618 570 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 571 if (unlikely(!event)) {
3394d22e 572 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
573 return -EINVAL;
574 }
575
576 /* Read event-declared event context */
e28d4618
MD
577 if (event->event_context) {
578 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
579 if (ret)
580 goto error;
581 }
582
583 /* Read event payload */
7f3f572b 584 if (likely(event->event_fields)) {
e28d4618 585 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
586 if (ret)
587 goto error;
588 }
589
28f35f0a
MD
590 if (pos->last_offset == pos->offset) {
591 fprintf(stderr, "[error] Invalid 0 byte event encountered.\n");
592 return -EINVAL;
593 }
594
31262354
MD
595 return 0;
596
597error:
64f5abe1 598 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
599 return ret;
600}
601
602static
1cf393f6 603int ctf_write_event(struct bt_stream_pos *pos, struct ctf_stream_definition *stream)
31262354 604{
f380e105 605 struct ctf_stream_declaration *stream_class = stream->stream_class;
c716f83b 606 struct ctf_event_definition *event;
c87a8eb2 607 uint64_t id;
31262354
MD
608 int ret;
609
c87a8eb2
MD
610 id = stream->event_id;
611
31262354 612 /* print event header */
7f3f572b 613 if (likely(stream->stream_event_header)) {
e28d4618 614 ret = generic_rw(pos, &stream->stream_event_header->p);
31262354
MD
615 if (ret)
616 goto error;
617 }
618
619 /* print stream-declared event context */
e28d4618
MD
620 if (stream->stream_event_context) {
621 ret = generic_rw(pos, &stream->stream_event_context->p);
31262354
MD
622 if (ret)
623 goto error;
624 }
625
7f3f572b 626 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 627 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
628 return -EINVAL;
629 }
e28d4618 630 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 631 if (unlikely(!event)) {
3394d22e 632 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
633 return -EINVAL;
634 }
635
636 /* print event-declared event context */
e28d4618
MD
637 if (event->event_context) {
638 ret = generic_rw(pos, &event->event_context->p);
31262354
MD
639 if (ret)
640 goto error;
641 }
642
643 /* Read and print event payload */
7f3f572b 644 if (likely(event->event_fields)) {
e28d4618 645 ret = generic_rw(pos, &event->event_fields->p);
31262354
MD
646 if (ret)
647 goto error;
648 }
649
650 return 0;
651
652error:
3394d22e 653 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
654 return ret;
655}
656
e69dd258
JD
657/*
658 * One side-effect of this function is to unmap pos mmap base if one is
659 * mapped.
660 */
661static
662int find_data_offset(struct ctf_stream_pos *pos,
663 struct ctf_file_stream *file_stream,
664 struct packet_index *packet_index)
665{
666 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
667 struct stat filestats;
668 size_t filesize;
669 int ret;
670
671 pos = &file_stream->pos;
672
673 ret = fstat(pos->fd, &filestats);
674 if (ret < 0)
675 return ret;
676 filesize = filestats.st_size;
677
678 /* Deal with empty files */
679 if (!filesize) {
680 return 0;
681 }
682
683begin:
684 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
685 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
686 }
687
688 if (pos->base_mma) {
689 /* unmap old base */
690 ret = munmap_align(pos->base_mma);
691 if (ret) {
692 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
693 strerror(errno));
694 return ret;
695 }
696 pos->base_mma = NULL;
697 }
698 /* map new base. Need mapping length from header. */
699 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
700 MAP_PRIVATE, pos->fd, pos->mmap_offset);
701 assert(pos->base_mma != MAP_FAILED);
702
703 pos->content_size = packet_map_len;
704 pos->packet_size = packet_map_len;
705 pos->offset = 0; /* Position of the packet header */
706
707 /* update trace_packet_header and stream_packet_context */
708 if (pos->prot == PROT_READ && file_stream->parent.trace_packet_header) {
709 /* Read packet header */
710 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
711 if (ret) {
712 if (ret == -EFAULT)
713 goto retry;
714 }
715 }
716 if (pos->prot == PROT_READ && file_stream->parent.stream_packet_context) {
717 /* Read packet context */
718 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
719 if (ret) {
720 if (ret == -EFAULT)
721 goto retry;
722 }
723 }
724 packet_index->data_offset = pos->offset;
725
726 /* unmap old base */
727 ret = munmap_align(pos->base_mma);
728 if (ret) {
729 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
730 strerror(errno));
731 return ret;
732 }
733 pos->base_mma = NULL;
734
735 return 0;
736
737 /* Retry with larger mapping */
738retry:
739 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
740 /*
741 * Reached EOF, but still expecting header/context data.
742 */
743 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
744 return -EFAULT;
745 }
746 /* Double the mapping len, and retry */
747 tmp_map_len = packet_map_len << 1;
748 if (tmp_map_len >> 1 != packet_map_len) {
749 /* Overflow */
750 fprintf(stderr, "[error] Packet mapping length overflow\n");
751 return -EFAULT;
752 }
753 packet_map_len = tmp_map_len;
754 goto begin;
755}
756
757
ca334c72
MD
758int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
759 int fd, int open_flags)
8c572eba
MD
760{
761 pos->fd = fd;
37fcdd19 762 if (fd >= 0) {
992e8cc0 763 pos->packet_index = g_array_new(FALSE, TRUE,
37fcdd19
JD
764 sizeof(struct packet_index));
765 } else {
992e8cc0 766 pos->packet_index = NULL;
37fcdd19 767 }
8563e754
MD
768 switch (open_flags & O_ACCMODE) {
769 case O_RDONLY:
770 pos->prot = PROT_READ;
771 pos->flags = MAP_PRIVATE;
772 pos->parent.rw_table = read_dispatch_table;
31262354 773 pos->parent.event_cb = ctf_read_event;
ca334c72 774 pos->parent.trace = trace;
8563e754 775 break;
8563e754 776 case O_RDWR:
fcf10417 777 pos->prot = PROT_READ | PROT_WRITE;
8563e754
MD
778 pos->flags = MAP_SHARED;
779 pos->parent.rw_table = write_dispatch_table;
31262354 780 pos->parent.event_cb = ctf_write_event;
ca334c72 781 pos->parent.trace = trace;
8563e754 782 if (fd >= 0)
06789ffd 783 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
8563e754
MD
784 break;
785 default:
786 assert(0);
8c572eba 787 }
f824ae04 788 return 0;
8c572eba
MD
789}
790
f824ae04 791int ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba 792{
fcf10417 793 if ((pos->prot & PROT_WRITE) && pos->content_size_loc)
8c572eba 794 *pos->content_size_loc = pos->offset;
aee35fcc 795 if (pos->base_mma) {
08c82b90
MD
796 int ret;
797
8c572eba 798 /* unmap old base */
aee35fcc 799 ret = munmap_align(pos->base_mma);
8c572eba 800 if (ret) {
3394d22e 801 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
8c572eba 802 strerror(errno));
f824ae04 803 return -1;
8c572eba
MD
804 }
805 }
992e8cc0
MD
806 if (pos->packet_index)
807 (void) g_array_free(pos->packet_index, TRUE);
f824ae04 808 return 0;
8c572eba
MD
809}
810
f1f52630
MD
811void ctf_update_current_packet_index(struct ctf_stream_definition *stream,
812 struct packet_index *prev_index,
813 struct packet_index *cur_index)
814{
815 uint64_t events_discarded_diff;
2328c2cd 816 uint64_t packets_lost_diff = 0;
f1f52630
MD
817
818 /* Update packet index time information */
2654fe9b
MD
819
820 /* Current packet begin/end */
821 stream->current.real.begin =
822 cur_index->ts_real.timestamp_begin;
823 stream->current.cycles.begin =
f1f52630 824 cur_index->ts_cycles.timestamp_begin;
2654fe9b 825 stream->current.real.end =
f1f52630 826 cur_index->ts_real.timestamp_end;
2654fe9b
MD
827 stream->current.cycles.end =
828 cur_index->ts_cycles.timestamp_end;
f1f52630
MD
829
830 /* Update packet index discarded event information */
831 events_discarded_diff = cur_index->events_discarded;
832 if (prev_index) {
2654fe9b
MD
833 /* Previous packet begin/end */
834 stream->prev.cycles.begin =
835 prev_index->ts_cycles.timestamp_begin;
836 stream->prev.real.begin =
837 prev_index->ts_real.timestamp_begin;
838 stream->prev.cycles.end =
839 prev_index->ts_cycles.timestamp_end;
840 stream->prev.real.end =
841 prev_index->ts_real.timestamp_end;
842
f1f52630 843 events_discarded_diff -= prev_index->events_discarded;
2328c2cd
JD
844 /* packet_seq_num stays at 0 if not produced by the tracer */
845 if (cur_index->packet_seq_num) {
846 packets_lost_diff = cur_index->packet_seq_num -
847 prev_index->packet_seq_num - 1;
848 }
f1f52630
MD
849 /*
850 * Deal with 32-bit wrap-around if the tracer provided a
851 * 32-bit field.
852 */
853 if (prev_index->events_discarded_len == 32) {
854 events_discarded_diff = (uint32_t) events_discarded_diff;
855 }
2654fe9b
MD
856 } else {
857 /*
858 * First packet: use current packet info as limits for
859 * previous packet.
860 */
861 stream->prev.cycles.begin =
862 stream->prev.cycles.end =
863 stream->current.cycles.begin;
864 stream->prev.real.begin =
865 stream->prev.real.end =
866 stream->current.real.begin;
f1f52630
MD
867 }
868 stream->events_discarded = events_discarded_diff;
2328c2cd 869 stream->packets_lost = packets_lost_diff;
f1f52630
MD
870}
871
37ea109a
JD
872/*
873 * Find the timerange where all the streams in the trace are active
874 * simultaneously.
875 *
876 * Return 0 and update begin/end if necessary on success, return 1 for
877 * empty streams and return a negative value on error.
878 */
879static
837b0013
JG
880int ctf_find_stream_intersection(struct bt_trace_descriptor *td_read,
881 struct packet_index_time *_real,
882 struct packet_index_time *_cycles)
37ea109a 883{
37ea109a 884 int stream_id, ret = 0;
837b0013
JG
885 struct packet_index_time real = { 0, UINT64_MAX },
886 cycles = { 0, UINT64_MAX };
887 struct ctf_trace *tin = container_of(td_read, struct ctf_trace, parent);
37ea109a 888
837b0013
JG
889 /* At least one of the two return args must be provided. */
890 if (!_real && !_cycles) {
891 ret = -1;
892 goto end;
893 }
894
895 if (tin->streams->len == 0) {
896 ret = 1;
897 goto end;
898 }
37ea109a
JD
899
900 for (stream_id = 0; stream_id < tin->streams->len;
901 stream_id++) {
902 int filenr;
903 struct ctf_stream_declaration *stream_class;
904
905 stream_class = g_ptr_array_index(tin->streams, stream_id);
906 if (!stream_class) {
907 continue;
908 }
909 for (filenr = 0; filenr < stream_class->streams->len; filenr++) {
910 struct ctf_file_stream *file_stream;
911 struct ctf_stream_pos *stream_pos;
912 struct packet_index *index;
913
914 file_stream = g_ptr_array_index(stream_class->streams,
915 filenr);
916 if (!file_stream) {
917 continue;
918 }
919 stream_pos = &file_stream->pos;
920 if (!stream_pos->packet_index ||
921 stream_pos->packet_index->len <= 0) {
922 ret = 1;
923 goto end;
924 }
925 index = &g_array_index(stream_pos->packet_index,
926 struct packet_index, 0);
837b0013
JG
927 real.timestamp_begin = max(real.timestamp_begin,
928 index->ts_real.timestamp_begin);
929 cycles.timestamp_begin = max(cycles.timestamp_begin,
930 index->ts_cycles.timestamp_begin);
931
37ea109a
JD
932 index = &g_array_index(stream_pos->packet_index,
933 struct packet_index,
934 stream_pos->packet_index->len - 1);
837b0013
JG
935 real.timestamp_end = min(real.timestamp_end,
936 index->ts_real.timestamp_end);
937 cycles.timestamp_end = min(cycles.timestamp_end,
938 index->ts_cycles.timestamp_end);
37ea109a
JD
939 }
940 }
37ea109a 941end:
837b0013
JG
942 if (ret == 0) {
943 if (_real) {
944 *_real = real;
945 }
946 if (_cycles) {
947 *_cycles = cycles;
948 }
949 }
37ea109a
JD
950 return ret;
951}
952
953/*
837b0013
JG
954 * Find the union of all active regions in the trace collection's traces.
955 * Returns "real" timestamps.
37ea109a
JD
956 *
957 * Return 0 on success.
958 * Return 1 if no intersections are found.
959 * Return a negative value on error.
960 */
837b0013
JG
961int ctf_find_tc_stream_packet_intersection_union(struct bt_context *ctx,
962 uint64_t *_ts_begin, uint64_t *_ts_end)
37ea109a 963{
837b0013
JG
964 int ret = 0, i;
965 uint64_t ts_begin = UINT64_MAX, ts_end = 0;
37ea109a 966
837b0013 967 if (!ctx || !ctx->tc || !ctx->tc->array || !_ts_begin || !_ts_end) {
37ea109a
JD
968 ret = -EINVAL;
969 goto end;
970 }
971
972 for (i = 0; i < ctx->tc->array->len; i++) {
973 struct bt_trace_descriptor *td_read;
837b0013 974 struct packet_index_time intersection_real;
37ea109a
JD
975
976 td_read = g_ptr_array_index(ctx->tc->array, i);
977 if (!td_read) {
978 continue;
979 }
837b0013
JG
980 ret = ctf_find_stream_intersection(td_read, &intersection_real,
981 NULL);
982 if (ret == 1) {
983 /* Empty trace or no stream intersection. */
984 continue;
985 } else if (ret < 0) {
37ea109a
JD
986 goto end;
987 }
837b0013
JG
988
989 ts_begin = min(intersection_real.timestamp_begin, ts_begin);
990 ts_end = max(intersection_real.timestamp_end, ts_end);
37ea109a 991 }
837b0013
JG
992
993 if (ts_end < ts_begin) {
37ea109a 994 ret = 1;
837b0013
JG
995 goto end;
996 }
997 *_ts_begin = ts_begin;
998 *_ts_end = ts_end;
999end:
1000 return ret;
1001}
1002
1003int ctf_tc_set_stream_intersection_mode(struct bt_context *ctx)
1004{
1005 int ret = 0, i;
1006
1007 if (!ctx || !ctx->tc || !ctx->tc->array) {
1008 ret = -EINVAL;
1009 goto end;
1010 }
1011
1012 for (i = 0; i < ctx->tc->array->len; i++) {
1013 struct bt_trace_descriptor *td_read;
1014 struct packet_index_time intersection_real;
1015
1016 td_read = g_ptr_array_index(ctx->tc->array, i);
1017 if (!td_read) {
1018 continue;
1019 }
1020
1021 ret = ctf_find_stream_intersection(td_read, &intersection_real,
1022 NULL);
1023 if (ret == 1) {
1024 /* Empty trace or no stream intersection. */
1025 continue;
1026 } else if (ret < 0) {
1027 goto end;
1028 }
1029
1030 td_read->interval_real = intersection_real;
1031 td_read->interval_set = true;
37ea109a
JD
1032 }
1033end:
1034 return ret;
1035}
1036
20d0dcf9
MD
1037/*
1038 * for SEEK_CUR: go to next packet.
34b8fff6 1039 * for SEEK_SET: go to packet numer (index).
20d0dcf9 1040 */
1cf393f6 1041void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
0f980a35 1042{
d6425aaf
MD
1043 struct ctf_stream_pos *pos =
1044 container_of(stream_pos, struct ctf_stream_pos, parent);
75cc2c35
MD
1045 struct ctf_file_stream *file_stream =
1046 container_of(pos, struct ctf_file_stream, pos);
0f980a35 1047 int ret;
2654fe9b 1048 struct packet_index *packet_index, *prev_index;
0f980a35 1049
5bfcad93
MD
1050 switch (whence) {
1051 case SEEK_CUR:
1052 case SEEK_SET: /* Fall-through */
1053 break; /* OK */
1054 default:
1055 assert(0);
1056 }
1057
fcf10417 1058 if ((pos->prot & PROT_WRITE) && pos->content_size_loc)
8c572eba 1059 *pos->content_size_loc = pos->offset;
0f980a35 1060
aee35fcc 1061 if (pos->base_mma) {
0f980a35 1062 /* unmap old base */
aee35fcc 1063 ret = munmap_align(pos->base_mma);
0f980a35 1064 if (ret) {
3394d22e 1065 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
1066 strerror(errno));
1067 assert(0);
1068 }
aee35fcc 1069 pos->base_mma = NULL;
0f980a35
MD
1070 }
1071
8c572eba 1072 /*
46322b33 1073 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
1074 * except to get exactly at the beginning of the next packet.
1075 */
fcf10417 1076 if (pos->prot & PROT_WRITE) {
989c73bc
MD
1077 switch (whence) {
1078 case SEEK_CUR:
1079 /* The writer will add padding */
e0a5b455 1080 pos->mmap_offset += pos->packet_size / CHAR_BIT;
989c73bc
MD
1081 break;
1082 case SEEK_SET:
20d0dcf9 1083 assert(index == 0); /* only seek supported for now */
989c73bc
MD
1084 pos->cur_index = 0;
1085 break;
1086 default:
1087 assert(0);
1088 }
8c572eba
MD
1089 pos->content_size = -1U; /* Unknown at this point */
1090 pos->packet_size = WRITE_PACKET_LEN;
118f5776
MD
1091 do {
1092 ret = bt_posix_fallocate(pos->fd, pos->mmap_offset,
1093 pos->packet_size / CHAR_BIT);
1094 } while (ret == EINTR);
1095 assert(ret == 0);
847bf71a 1096 pos->offset = 0;
8c572eba 1097 } else {
5f643ed7 1098 read_next_packet:
847bf71a
MD
1099 switch (whence) {
1100 case SEEK_CUR:
41e82e00 1101 {
7d97fad9
MD
1102 if (pos->offset == EOF) {
1103 return;
1104 }
992e8cc0 1105 assert(pos->cur_index < pos->packet_index->len);
847bf71a 1106 /* The reader will expect us to skip padding */
8c572eba 1107 ++pos->cur_index;
847bf71a 1108 break;
41e82e00 1109 }
847bf71a 1110 case SEEK_SET:
992e8cc0 1111 if (index >= pos->packet_index->len) {
c309df1c
MD
1112 pos->offset = EOF;
1113 return;
1114 }
20d0dcf9 1115 pos->cur_index = index;
847bf71a
MD
1116 break;
1117 default:
1118 assert(0);
1119 }
2654fe9b 1120
0e1b84aa
MD
1121 if (pos->cur_index >= pos->packet_index->len) {
1122 pos->offset = EOF;
1123 return;
1124 }
1125
2654fe9b
MD
1126 packet_index = &g_array_index(pos->packet_index,
1127 struct packet_index, pos->cur_index);
1128 if (pos->cur_index > 0) {
1129 prev_index = &g_array_index(pos->packet_index,
1130 struct packet_index,
1131 pos->cur_index - 1);
1132 } else {
1133 prev_index = NULL;
1134 }
1135 ctf_update_current_packet_index(&file_stream->parent,
1136 prev_index, packet_index);
1137
2654fe9b
MD
1138 /*
1139 * We need to check if we are in trace read or called
1140 * from packet indexing. In this last case, the
1141 * collection is not there, so we cannot print the
1142 * timestamps.
1143 */
1144 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
2328c2cd 1145 ctf_print_discarded_lost(stderr, &file_stream->parent);
2654fe9b
MD
1146 }
1147
992e8cc0 1148 packet_index = &g_array_index(pos->packet_index,
03798a93
JD
1149 struct packet_index,
1150 pos->cur_index);
992e8cc0 1151 file_stream->parent.cycles_timestamp = packet_index->ts_cycles.timestamp_begin;
03798a93 1152
992e8cc0 1153 file_stream->parent.real_timestamp = packet_index->ts_real.timestamp_begin;
8c572eba
MD
1154
1155 /* Lookup context/packet size in index */
e69dd258
JD
1156 if (packet_index->data_offset == -1) {
1157 ret = find_data_offset(pos, file_stream, packet_index);
1158 if (ret < 0) {
1159 return;
1160 }
1161 }
20d0dcf9
MD
1162 pos->content_size = packet_index->content_size;
1163 pos->packet_size = packet_index->packet_size;
e69dd258 1164 pos->mmap_offset = packet_index->offset;
500634be
JD
1165 pos->data_offset = packet_index->data_offset;
1166 if (pos->data_offset < packet_index->content_size) {
75cc2c35 1167 pos->offset = 0; /* will read headers */
500634be 1168 } else if (pos->data_offset == packet_index->content_size) {
5f643ed7 1169 /* empty packet */
20d0dcf9 1170 pos->offset = packet_index->data_offset;
3abe83c7 1171 whence = SEEK_CUR;
5f643ed7 1172 goto read_next_packet;
7eda6fc7 1173 } else {
2b9a764d
MD
1174 pos->offset = EOF;
1175 return;
1176 }
8c572eba 1177 }
0f980a35 1178 /* map new base. Need mapping length from header. */
aee35fcc
MD
1179 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
1180 pos->flags, pos->fd, pos->mmap_offset);
1181 if (pos->base_mma == MAP_FAILED) {
3394d22e 1182 fprintf(stderr, "[error] mmap error %s.\n",
847bf71a
MD
1183 strerror(errno));
1184 assert(0);
1185 }
75cc2c35
MD
1186
1187 /* update trace_packet_header and stream_packet_context */
fcf10417
JG
1188 if (!(pos->prot & PROT_WRITE) &&
1189 file_stream->parent.trace_packet_header) {
75cc2c35 1190 /* Read packet header */
2d0bea29 1191 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
75cc2c35
MD
1192 assert(!ret);
1193 }
fcf10417
JG
1194 if (!(pos->prot & PROT_WRITE) &&
1195 file_stream->parent.stream_packet_context) {
75cc2c35 1196 /* Read packet context */
2d0bea29 1197 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
75cc2c35
MD
1198 assert(!ret);
1199 }
0f980a35
MD
1200}
1201
b4c19c1e
MD
1202static
1203int packet_metadata(struct ctf_trace *td, FILE *fp)
1204{
1205 uint32_t magic;
1206 size_t len;
1207 int ret = 0;
1208
1209 len = fread(&magic, sizeof(magic), 1, fp);
a0fe7d97 1210 if (len != 1) {
b4c19c1e
MD
1211 goto end;
1212 }
1213 if (magic == TSDL_MAGIC) {
1214 ret = 1;
1215 td->byte_order = BYTE_ORDER;
0d336fdf 1216 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
1217 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
1218 ret = 1;
1219 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
1220 LITTLE_ENDIAN : BIG_ENDIAN;
0d336fdf 1221 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
1222 }
1223end:
1224 rewind(fp);
1225 return ret;
1226}
1227
5c262147
MD
1228/*
1229 * Returns 0 on success, -1 on error.
1230 */
1231static
1232int check_version(unsigned int major, unsigned int minor)
1233{
1234 switch (major) {
1235 case 1:
1236 switch (minor) {
1237 case 8:
1238 return 0;
1239 default:
1240 goto warning;
1241 }
1242 default:
1243 goto warning;
1244
1245 }
1246
1247 /* eventually return an error instead of warning */
1248warning:
3394d22e 1249 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
5c262147
MD
1250 major, minor);
1251 return 0;
1252}
1253
b4c19c1e 1254static
0c880b0a 1255int ctf_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
b4c19c1e
MD
1256 FILE *out)
1257{
1258 struct metadata_packet_header header;
a0fe7d97 1259 size_t readlen, writelen, toread;
f8254867 1260 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
b4c19c1e
MD
1261 int ret = 0;
1262
a91a962e 1263 readlen = fread(&header, header_sizeof(header), 1, in);
a0fe7d97 1264 if (readlen < 1)
b4c19c1e
MD
1265 return -EINVAL;
1266
1267 if (td->byte_order != BYTE_ORDER) {
1268 header.magic = GUINT32_SWAP_LE_BE(header.magic);
1269 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
1270 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
1271 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
1272 }
1273 if (header.checksum)
3394d22e 1274 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
b4c19c1e 1275 if (header.compression_scheme) {
3394d22e 1276 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
b4c19c1e
MD
1277 header.compression_scheme);
1278 return -EINVAL;
1279 }
1280 if (header.encryption_scheme) {
3394d22e 1281 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
b4c19c1e
MD
1282 header.encryption_scheme);
1283 return -EINVAL;
1284 }
1285 if (header.checksum_scheme) {
3394d22e 1286 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
b4c19c1e
MD
1287 header.checksum_scheme);
1288 return -EINVAL;
1289 }
5c262147
MD
1290 if (check_version(header.major, header.minor) < 0)
1291 return -EINVAL;
b4c19c1e
MD
1292 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
1293 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
1294 CTF_TRACE_SET_FIELD(td, uuid);
1295 } else {
72a3bc16 1296 if (bt_uuid_compare(header.uuid, td->uuid))
b4c19c1e
MD
1297 return -EINVAL;
1298 }
1299
b1ccd079
MD
1300 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
1301 return -EINVAL;
1302
255b2138 1303 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
a0fe7d97
MD
1304
1305 for (;;) {
f8254867 1306 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
b4c19c1e
MD
1307 if (ferror(in)) {
1308 ret = -EINVAL;
1309 break;
1310 }
4152822b 1311 if (babeltrace_debug) {
f8254867 1312 buf[readlen] = '\0';
3394d22e 1313 fprintf(stderr, "[debug] metadata packet read: %s\n",
4152822b
MD
1314 buf);
1315 }
1316
b4c19c1e
MD
1317 writelen = fwrite(buf, sizeof(char), readlen, out);
1318 if (writelen < readlen) {
1319 ret = -EIO;
1320 break;
1321 }
1322 if (ferror(out)) {
1323 ret = -EINVAL;
1324 break;
1325 }
a0fe7d97
MD
1326 toread -= readlen;
1327 if (!toread) {
7f4b5c4d 1328 ret = 0; /* continue reading next packet */
ddbc52af 1329 goto read_padding;
a0fe7d97 1330 }
b4c19c1e
MD
1331 }
1332 return ret;
ddbc52af
MD
1333
1334read_padding:
1335 toread = (header.packet_size - header.content_size) / CHAR_BIT;
1336 ret = fseek(in, toread, SEEK_CUR);
1337 if (ret < 0) {
3394d22e 1338 fprintf(stderr, "[warning] Missing padding at end of file\n");
ddbc52af
MD
1339 ret = 0;
1340 }
1341 return ret;
b4c19c1e
MD
1342}
1343
1344static
0c880b0a 1345int ctf_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
b4c19c1e
MD
1346 char **buf)
1347{
1348 FILE *in, *out;
abc40d24 1349 size_t size, buflen;
b4c19c1e
MD
1350 int ret;
1351
1352 in = *fp;
c4f5487e
MD
1353 /*
1354 * Using strlen on *buf instead of size of open_memstream
1355 * because its size includes garbage at the end (after final
1356 * \0). This is the allocated size, not the actual string size.
1357 */
f8370579 1358 out = babeltrace_open_memstream(buf, &size);
a569a564
MD
1359 if (out == NULL) {
1360 perror("Metadata open_memstream");
b4c19c1e 1361 return -errno;
a569a564 1362 }
b4c19c1e 1363 for (;;) {
0c880b0a 1364 ret = ctf_trace_metadata_packet_read(td, in, out);
7f4b5c4d 1365 if (ret) {
b4c19c1e 1366 break;
7f4b5c4d
MD
1367 }
1368 if (feof(in)) {
1369 ret = 0;
b4c19c1e
MD
1370 break;
1371 }
1372 }
f8370579
MD
1373 /* close to flush the buffer */
1374 ret = babeltrace_close_memstream(buf, &size, out);
1375 if (ret < 0) {
f824ae04
MD
1376 int closeret;
1377
f8370579 1378 perror("babeltrace_flush_memstream");
f824ae04
MD
1379 ret = -errno;
1380 closeret = fclose(in);
1381 if (closeret) {
1382 perror("Error in fclose");
1383 }
1384 return ret;
1385 }
1386 ret = fclose(in);
1387 if (ret) {
1388 perror("Error in fclose");
f8370579 1389 }
b4c19c1e 1390 /* open for reading */
abc40d24
MD
1391 buflen = strlen(*buf);
1392 if (!buflen) {
1393 *fp = NULL;
493330cb 1394 return -ENOENT;
abc40d24
MD
1395 }
1396 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
a569a564
MD
1397 if (!*fp) {
1398 perror("Metadata fmemopen");
1399 return -errno;
1400 }
b4c19c1e
MD
1401 return 0;
1402}
1403
65102a8c 1404static
0c880b0a
MD
1405int ctf_trace_metadata_read(struct ctf_trace *td, FILE *metadata_fp,
1406 struct ctf_scanner *scanner, int append)
65102a8c 1407{
2d0bea29 1408 struct ctf_file_stream *metadata_stream;
65102a8c 1409 FILE *fp;
b4c19c1e 1410 char *buf = NULL;
f824ae04 1411 int ret = 0, closeret;
65102a8c 1412
2d0bea29 1413 metadata_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 1414 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
b086c01a 1415
ae23d232
JD
1416 if (metadata_fp) {
1417 fp = metadata_fp;
bcbfb8bf 1418 metadata_stream->pos.fd = -1;
ae23d232
JD
1419 } else {
1420 td->metadata = &metadata_stream->parent;
1421 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1422 if (metadata_stream->pos.fd < 0) {
3394d22e 1423 fprintf(stderr, "Unable to open metadata.\n");
f824ae04
MD
1424 ret = -1;
1425 goto end_free;
ae23d232 1426 }
65102a8c 1427
ae23d232
JD
1428 fp = fdopen(metadata_stream->pos.fd, "r");
1429 if (!fp) {
3394d22e 1430 fprintf(stderr, "[error] Unable to open metadata stream.\n");
a569a564 1431 perror("Metadata stream open");
ae23d232
JD
1432 ret = -errno;
1433 goto end_stream;
1434 }
f824ae04
MD
1435 /* fd now belongs to fp */
1436 metadata_stream->pos.fd = -1;
ae23d232 1437 }
65102a8c
MD
1438 if (babeltrace_debug)
1439 yydebug = 1;
1440
b4c19c1e 1441 if (packet_metadata(td, fp)) {
0c880b0a 1442 ret = ctf_trace_metadata_stream_read(td, &fp, &buf);
abc40d24 1443 if (ret) {
6514b4af 1444 goto end;
abc40d24 1445 }
7237592a
MD
1446 td->metadata_string = buf;
1447 td->metadata_packetized = 1;
da75b0f7 1448 } else {
0c880b0a
MD
1449 if (!append) {
1450 unsigned int major, minor;
1451 ssize_t nr_items;
1452
1453 td->byte_order = BYTE_ORDER;
1454
1455 /* Check text-only metadata header and version */
1456 nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor);
1457 if (nr_items < 2)
1458 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1459 if (check_version(major, minor) < 0) {
1460 ret = -EINVAL;
1461 goto end;
1462 }
1463 rewind(fp);
5c262147 1464 }
b4c19c1e
MD
1465 }
1466
cb2f43ee 1467 ret = ctf_scanner_append_ast(scanner, fp);
65102a8c 1468 if (ret) {
3394d22e 1469 fprintf(stderr, "[error] Error creating AST\n");
65102a8c
MD
1470 goto end;
1471 }
1472
1473 if (babeltrace_debug) {
3394d22e 1474 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
65102a8c 1475 if (ret) {
3394d22e 1476 fprintf(stderr, "[error] Error visiting AST for XML output\n");
65102a8c
MD
1477 goto end;
1478 }
1479 }
1480
3394d22e 1481 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
65102a8c 1482 if (ret) {
3394d22e 1483 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
1484 goto end;
1485 }
3394d22e 1486 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
ac5c6ca0 1487 td, td->byte_order);
65102a8c 1488 if (ret) {
3394d22e 1489 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
1490 goto end;
1491 }
1492end:
f824ae04
MD
1493 if (fp) {
1494 closeret = fclose(fp);
1495 if (closeret) {
1496 perror("Error on fclose");
1497 }
1498 }
65102a8c 1499end_stream:
f824ae04
MD
1500 if (metadata_stream->pos.fd >= 0) {
1501 closeret = close(metadata_stream->pos.fd);
1502 if (closeret) {
1503 perror("Error on metadata stream fd close");
1504 }
1505 }
1506end_free:
2d0bea29
MD
1507 if (ret)
1508 g_free(metadata_stream);
0f980a35
MD
1509 return ret;
1510}
1511
e28d4618 1512static
c716f83b 1513struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
9e88d150 1514 struct ctf_stream_definition *stream,
4716614a 1515 struct ctf_event_declaration *event)
e28d4618 1516{
c716f83b 1517 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
e28d4618
MD
1518
1519 if (event->context_decl) {
0d69b916 1520 struct bt_definition *definition =
e28d4618
MD
1521 event->context_decl->p.definition_new(&event->context_decl->p,
1522 stream->parent_def_scope, 0, 0, "event.context");
1523 if (!definition) {
1524 goto error;
1525 }
42dc00b7 1526 stream_event->event_context = container_of(definition,
e28d4618 1527 struct definition_struct, p);
42dc00b7 1528 stream->parent_def_scope = stream_event->event_context->p.scope;
e28d4618
MD
1529 }
1530 if (event->fields_decl) {
0d69b916 1531 struct bt_definition *definition =
e28d4618
MD
1532 event->fields_decl->p.definition_new(&event->fields_decl->p,
1533 stream->parent_def_scope, 0, 0, "event.fields");
1534 if (!definition) {
1535 goto error;
1536 }
42dc00b7 1537 stream_event->event_fields = container_of(definition,
e28d4618 1538 struct definition_struct, p);
42dc00b7 1539 stream->parent_def_scope = stream_event->event_fields->p.scope;
e28d4618 1540 }
d3ded99d 1541 stream_event->stream = stream;
42dc00b7 1542 return stream_event;
e28d4618
MD
1543
1544error:
42dc00b7 1545 if (stream_event->event_fields)
13fad8b6 1546 bt_definition_unref(&stream_event->event_fields->p);
42dc00b7 1547 if (stream_event->event_context)
13fad8b6 1548 bt_definition_unref(&stream_event->event_context->p);
888ec52a
MD
1549 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1550 g_quark_to_string(event->name));
e28d4618
MD
1551 return NULL;
1552}
1553
0c880b0a
MD
1554static
1555int copy_event_declarations_stream_class_to_stream(struct ctf_trace *td,
1556 struct ctf_stream_declaration *stream_class,
1557 struct ctf_stream_definition *stream)
1558{
1559 size_t def_size, class_size, i;
1560 int ret = 0;
1561
1562 def_size = stream->events_by_id->len;
1563 class_size = stream_class->events_by_id->len;
1564
1565 g_ptr_array_set_size(stream->events_by_id, class_size);
1566 for (i = def_size; i < class_size; i++) {
1567 struct ctf_event_declaration *event =
1568 g_ptr_array_index(stream_class->events_by_id, i);
1569 struct ctf_event_definition *stream_event;
1570
1571 if (!event)
1572 continue;
1573 stream_event = create_event_definitions(td, stream, event);
1574 if (!stream_event) {
1575 ret = -EINVAL;
1576 goto error;
1577 }
1578 g_ptr_array_index(stream->events_by_id, i) = stream_event;
1579 }
1580error:
1581 return ret;
1582}
1583
e28d4618 1584static
9e88d150 1585int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618 1586{
f380e105 1587 struct ctf_stream_declaration *stream_class;
e28d4618
MD
1588 int ret;
1589 int i;
1590
1591 if (stream->stream_definitions_created)
1592 return 0;
1593
1594 stream_class = stream->stream_class;
1595
1596 if (stream_class->packet_context_decl) {
0d69b916 1597 struct bt_definition *definition =
e28d4618
MD
1598 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1599 stream->parent_def_scope, 0, 0, "stream.packet.context");
1600 if (!definition) {
1601 ret = -EINVAL;
1602 goto error;
1603 }
1604 stream->stream_packet_context = container_of(definition,
1605 struct definition_struct, p);
1606 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1607 }
1608 if (stream_class->event_header_decl) {
0d69b916 1609 struct bt_definition *definition =
e28d4618
MD
1610 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1611 stream->parent_def_scope, 0, 0, "stream.event.header");
1612 if (!definition) {
1613 ret = -EINVAL;
1614 goto error;
1615 }
1616 stream->stream_event_header =
1617 container_of(definition, struct definition_struct, p);
1618 stream->parent_def_scope = stream->stream_event_header->p.scope;
1619 }
1620 if (stream_class->event_context_decl) {
0d69b916 1621 struct bt_definition *definition =
e28d4618
MD
1622 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1623 stream->parent_def_scope, 0, 0, "stream.event.context");
1624 if (!definition) {
1625 ret = -EINVAL;
1626 goto error;
1627 }
1628 stream->stream_event_context =
1629 container_of(definition, struct definition_struct, p);
1630 stream->parent_def_scope = stream->stream_event_context->p.scope;
1631 }
1632 stream->events_by_id = g_ptr_array_new();
0c880b0a
MD
1633 ret = copy_event_declarations_stream_class_to_stream(td,
1634 stream_class, stream);
1635 if (ret)
1636 goto error_event;
e28d4618
MD
1637 return 0;
1638
1639error_event:
1640 for (i = 0; i < stream->events_by_id->len; i++) {
c716f83b 1641 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
42dc00b7
MD
1642 if (stream_event)
1643 g_free(stream_event);
e28d4618
MD
1644 }
1645 g_ptr_array_free(stream->events_by_id, TRUE);
1646error:
1647 if (stream->stream_event_context)
13fad8b6 1648 bt_definition_unref(&stream->stream_event_context->p);
e28d4618 1649 if (stream->stream_event_header)
13fad8b6 1650 bt_definition_unref(&stream->stream_event_header->p);
e28d4618 1651 if (stream->stream_packet_context)
13fad8b6 1652 bt_definition_unref(&stream->stream_packet_context->p);
888ec52a
MD
1653 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1654 stream_class->stream_id, strerror(-ret));
e28d4618
MD
1655 return ret;
1656}
1657
5bfcad93
MD
1658static
1659int stream_assign_class(struct ctf_trace *td,
1660 struct ctf_file_stream *file_stream,
1661 uint64_t stream_id)
1662{
1663 struct ctf_stream_declaration *stream;
1664 int ret;
1665
1666 file_stream->parent.stream_id = stream_id;
1667 if (stream_id >= td->streams->len) {
1668 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1669 return -EINVAL;
1670 }
1671 stream = g_ptr_array_index(td->streams, stream_id);
1672 if (!stream) {
1673 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1674 return -EINVAL;
1675 }
1676 file_stream->parent.stream_class = stream;
1677 ret = create_stream_definitions(td, &file_stream->parent);
1678 if (ret)
1679 return ret;
1680 return 0;
1681}
1682
0f980a35 1683static
ec323464
MD
1684int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1685 struct ctf_trace *td,
1686 struct ctf_file_stream *file_stream,
1687 size_t filesize)
0f980a35 1688{
ec323464 1689 struct packet_index packet_index;
ec323464 1690 uint64_t stream_id = 0;
653906a4 1691 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
ec323464 1692 int first_packet = 0;
653906a4 1693 int len_index;
0f980a35
MD
1694 int ret;
1695
ec323464 1696begin:
5f75b32d 1697 memset(&packet_index, 0, sizeof(packet_index));
ec323464
MD
1698 if (!pos->mmap_offset) {
1699 first_packet = 1;
1700 }
8895362d 1701
ec323464
MD
1702 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1703 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1704 }
0f980a35 1705
ec323464
MD
1706 if (pos->base_mma) {
1707 /* unmap old base */
1708 ret = munmap_align(pos->base_mma);
1709 if (ret) {
1710 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1711 strerror(errno));
1712 return ret;
0f980a35 1713 }
ec323464
MD
1714 pos->base_mma = NULL;
1715 }
1716 /* map new base. Need mapping length from header. */
1717 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1718 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1719 assert(pos->base_mma != MAP_FAILED);
1720 /*
1721 * Use current mapping size as temporary content and packet
1722 * size.
1723 */
1724 pos->content_size = packet_map_len;
1725 pos->packet_size = packet_map_len;
1726 pos->offset = 0; /* Position of the packet header */
1727
1728 packet_index.offset = pos->mmap_offset;
ec323464
MD
1729
1730 /* read and check header, set stream id (and check) */
1731 if (file_stream->parent.trace_packet_header) {
1732 /* Read packet header */
1733 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1734 if (ret) {
1735 if (ret == -EFAULT)
1736 goto retry;
888ec52a 1737 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
ec323464
MD
1738 return ret;
1739 }
1740 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1741 if (len_index >= 0) {
1742 struct bt_definition *field;
1743 uint64_t magic;
1744
1745 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1746 magic = bt_get_unsigned_int(field);
1747 if (magic != CTF_MAGIC) {
1748 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1749 magic,
992e8cc0 1750 file_stream->pos.packet_index->len,
ec323464
MD
1751 (ssize_t) pos->mmap_offset);
1752 return -EINVAL;
0f980a35 1753 }
ec323464 1754 }
0f980a35 1755
ec323464
MD
1756 /* check uuid */
1757 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1758 if (len_index >= 0) {
1759 struct definition_array *defarray;
1760 struct bt_definition *field;
1761 uint64_t i;
1762 uint8_t uuidval[BABELTRACE_UUID_LEN];
0f980a35 1763
ec323464
MD
1764 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1765 assert(field->declaration->id == CTF_TYPE_ARRAY);
1766 defarray = container_of(field, struct definition_array, p);
1767 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
0f980a35 1768
ec323464
MD
1769 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1770 struct bt_definition *elem;
0f980a35 1771
ec323464
MD
1772 elem = bt_array_index(defarray, i);
1773 uuidval[i] = bt_get_unsigned_int(elem);
0f980a35 1774 }
72a3bc16 1775 ret = bt_uuid_compare(td->uuid, uuidval);
ec323464
MD
1776 if (ret) {
1777 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1778 return -EINVAL;
1779 }
1780 }
0f980a35 1781
ec323464
MD
1782 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1783 if (len_index >= 0) {
1784 struct bt_definition *field;
0f980a35 1785
ec323464
MD
1786 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1787 stream_id = bt_get_unsigned_int(field);
0f980a35 1788 }
ec323464 1789 }
0f980a35 1790
ec323464
MD
1791 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1792 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1793 stream_id,
1794 file_stream->parent.stream_id);
1795 return -EINVAL;
1796 }
1797 if (first_packet) {
5bfcad93 1798 ret = stream_assign_class(td, file_stream, stream_id);
ec323464
MD
1799 if (ret)
1800 return ret;
1801 }
dc48ecad 1802
ec323464
MD
1803 if (file_stream->parent.stream_packet_context) {
1804 /* Read packet context */
1805 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1806 if (ret) {
1807 if (ret == -EFAULT)
1808 goto retry;
888ec52a 1809 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
ec323464
MD
1810 return ret;
1811 }
95b34f38
MD
1812 /* read packet size from header */
1813 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
ec323464
MD
1814 if (len_index >= 0) {
1815 struct bt_definition *field;
dc48ecad 1816
ec323464 1817 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
95b34f38 1818 packet_index.packet_size = bt_get_unsigned_int(field);
ec323464
MD
1819 } else {
1820 /* Use file size for packet size */
95b34f38 1821 packet_index.packet_size = filesize * CHAR_BIT;
ec323464 1822 }
75cc2c35 1823
95b34f38
MD
1824 /* read content size from header */
1825 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
ec323464
MD
1826 if (len_index >= 0) {
1827 struct bt_definition *field;
75cc2c35 1828
ec323464 1829 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
95b34f38 1830 packet_index.content_size = bt_get_unsigned_int(field);
ec323464 1831 } else {
95b34f38
MD
1832 /* Use packet size if non-zero, else file size */
1833 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
ec323464 1834 }
41e82e00 1835
ec323464
MD
1836 /* read timestamp begin from header */
1837 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1838 if (len_index >= 0) {
1839 struct bt_definition *field;
41e82e00 1840
ec323464 1841 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
992e8cc0 1842 packet_index.ts_cycles.timestamp_begin = bt_get_unsigned_int(field);
4c62e2d8 1843 if (file_stream->parent.stream_class->trace->parent.collection) {
992e8cc0 1844 packet_index.ts_real.timestamp_begin =
ec323464
MD
1845 ctf_get_real_timestamp(
1846 &file_stream->parent,
992e8cc0 1847 packet_index.ts_cycles.timestamp_begin);
41e82e00 1848 }
0f980a35 1849 }
546293fa 1850
ec323464
MD
1851 /* read timestamp end from header */
1852 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1853 if (len_index >= 0) {
1854 struct bt_definition *field;
1855
1856 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
992e8cc0 1857 packet_index.ts_cycles.timestamp_end = bt_get_unsigned_int(field);
4c62e2d8 1858 if (file_stream->parent.stream_class->trace->parent.collection) {
992e8cc0 1859 packet_index.ts_real.timestamp_end =
ec323464
MD
1860 ctf_get_real_timestamp(
1861 &file_stream->parent,
992e8cc0 1862 packet_index.ts_cycles.timestamp_end);
ec323464 1863 }
546293fa
MD
1864 }
1865
ec323464
MD
1866 /* read events discarded from header */
1867 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1868 if (len_index >= 0) {
1869 struct bt_definition *field;
1870
1871 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1872 packet_index.events_discarded = bt_get_unsigned_int(field);
1873 packet_index.events_discarded_len = bt_get_int_len(field);
546293fa 1874 }
2328c2cd
JD
1875
1876 /* read packet_seq_num from header */
1877 len_index = bt_struct_declaration_lookup_field_index(
1878 file_stream->parent.stream_packet_context->declaration,
1879 g_quark_from_static_string("packet_seq_num"));
1880 if (len_index >= 0) {
1881 struct bt_definition *field;
1882
1883 field = bt_struct_definition_get_field_from_index(
1884 file_stream->parent.stream_packet_context,
1885 len_index);
1886 packet_index.packet_seq_num = bt_get_unsigned_int(field);
1887 }
ec323464
MD
1888 } else {
1889 /* Use file size for packet size */
2d686891
MD
1890 packet_index.packet_size = filesize * CHAR_BIT;
1891 /* Use packet size if non-zero, else file size */
1892 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
ec323464
MD
1893 }
1894
1895 /* Validate content size and packet size values */
1896 if (packet_index.content_size > packet_index.packet_size) {
1897 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1898 packet_index.content_size, packet_index.packet_size);
1899 return -EINVAL;
1900 }
1901
1902 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1903 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1904 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1905 return -EINVAL;
1906 }
546293fa 1907
a7ac9efd
MD
1908 if (packet_index.content_size < pos->offset) {
1909 fprintf(stderr, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1910 return -EINVAL;
1911 }
1912
2d686891
MD
1913 if ((packet_index.packet_size >> LOG2_CHAR_BIT) == 0) {
1914 fprintf(stderr, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1915 return -EINVAL;
1916 }
1917
ec323464
MD
1918 /* Save position after header and context */
1919 packet_index.data_offset = pos->offset;
0f980a35 1920
ec323464 1921 /* add index to packet array */
992e8cc0 1922 g_array_append_val(file_stream->pos.packet_index, packet_index);
0f980a35 1923
ec323464
MD
1924 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1925
1926 return 0;
1927
1928 /* Retry with larger mapping */
1929retry:
1930 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1931 /*
1932 * Reached EOF, but still expecting header/context data.
1933 */
1934 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1935 return -EFAULT;
1936 }
1937 /* Double the mapping len, and retry */
1938 tmp_map_len = packet_map_len << 1;
1939 if (tmp_map_len >> 1 != packet_map_len) {
1940 /* Overflow */
888ec52a 1941 fprintf(stderr, "[error] Packet mapping length overflow\n");
ec323464 1942 return -EFAULT;
0f980a35 1943 }
ec323464
MD
1944 packet_map_len = tmp_map_len;
1945 goto begin;
1946}
1947
1948static
1949int create_stream_packet_index(struct ctf_trace *td,
1950 struct ctf_file_stream *file_stream)
1951{
1952 struct ctf_stream_pos *pos;
1953 struct stat filestats;
1954 int ret;
1955
1956 pos = &file_stream->pos;
0f980a35 1957
ec323464
MD
1958 ret = fstat(pos->fd, &filestats);
1959 if (ret < 0)
1960 return ret;
1961
5bfcad93
MD
1962 /* Deal with empty files */
1963 if (!filestats.st_size) {
1964 if (file_stream->parent.trace_packet_header
1965 || file_stream->parent.stream_packet_context) {
1966 /*
1967 * We expect a trace packet header and/or stream packet
1968 * context. Since a trace needs to have at least one
1969 * packet, empty files are therefore not accepted.
1970 */
1971 fprintf(stderr, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1972 return -EINVAL;
1973 } else {
1974 /*
1975 * Without trace packet header nor stream packet
1976 * context, a one-packet trace can indeed be empty. This
1977 * is only valid if there is only one stream class: 0.
1978 */
1979 ret = stream_assign_class(td, file_stream, 0);
1980 if (ret)
1981 return ret;
1982 return 0;
1983 }
1984 }
1985
ec323464
MD
1986 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1987 ret = create_stream_one_packet_index(pos, td, file_stream,
1988 filestats.st_size);
1989 if (ret)
1990 return ret;
1991 }
0f980a35
MD
1992 return 0;
1993}
1994
e28d4618 1995static
9e88d150 1996int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618
MD
1997{
1998 int ret;
1999
2000 if (td->packet_header_decl) {
0d69b916 2001 struct bt_definition *definition =
e28d4618
MD
2002 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
2003 stream->parent_def_scope, 0, 0, "trace.packet.header");
2004 if (!definition) {
2005 ret = -EINVAL;
2006 goto error;
2007 }
2008 stream->trace_packet_header =
2009 container_of(definition, struct definition_struct, p);
2010 stream->parent_def_scope = stream->trace_packet_header->p.scope;
2011 }
2012
2013 return 0;
2014
2015error:
888ec52a 2016 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
e28d4618
MD
2017 return ret;
2018}
2019
0ace7505
JD
2020static
2021int import_stream_packet_index(struct ctf_trace *td,
2022 struct ctf_file_stream *file_stream)
2023{
0ace7505 2024 struct ctf_stream_pos *pos;
3ecd366e 2025 struct ctf_packet_index *ctf_index = NULL;
0ace7505
JD
2026 struct ctf_packet_index_file_hdr index_hdr;
2027 struct packet_index index;
138cb20b 2028 uint32_t packet_index_len, index_minor;
0ace7505
JD
2029 int ret = 0;
2030 int first_packet = 1;
2031 size_t len;
2032
2033 pos = &file_stream->pos;
2034
2035 len = fread(&index_hdr, sizeof(index_hdr), 1, pos->index_fp);
2036 if (len != 1) {
2037 perror("read index file header");
2038 goto error;
2039 }
2040
2041 /* Check the index header */
2042 if (be32toh(index_hdr.magic) != CTF_INDEX_MAGIC) {
2043 fprintf(stderr, "[error] wrong index magic\n");
2044 ret = -1;
2045 goto error;
2046 }
2047 if (be32toh(index_hdr.index_major) != CTF_INDEX_MAJOR) {
f84c13d9
MJ
2048 fprintf(stderr, "[error] Incompatible index file %" PRIu32
2049 ".%" PRIu32 ", supported %d.%d\n",
2050 be32toh(index_hdr.index_major),
2051 be32toh(index_hdr.index_minor), CTF_INDEX_MAJOR,
0ace7505
JD
2052 CTF_INDEX_MINOR);
2053 ret = -1;
2054 goto error;
2055 }
138cb20b
JD
2056 index_minor = be32toh(index_hdr.index_minor);
2057
e83ce12a
JD
2058 packet_index_len = be32toh(index_hdr.packet_index_len);
2059 if (packet_index_len == 0) {
a74d9cb2
MD
2060 fprintf(stderr, "[error] Packet index length cannot be 0.\n");
2061 ret = -1;
2062 goto error;
2063 }
3ecd366e
MD
2064 /*
2065 * Allocate the index length found in header, not internal
2066 * representation.
2067 */
e83ce12a
JD
2068 ctf_index = g_malloc0(packet_index_len);
2069 while (fread(ctf_index, packet_index_len, 1,
ad40ac1a 2070 pos->index_fp) == 1) {
0ace7505 2071 uint64_t stream_id;
51e0087f 2072 struct ctf_stream_declaration *stream = NULL;
0ace7505
JD
2073
2074 memset(&index, 0, sizeof(index));
3ecd366e
MD
2075 index.offset = be64toh(ctf_index->offset);
2076 index.packet_size = be64toh(ctf_index->packet_size);
2077 index.content_size = be64toh(ctf_index->content_size);
2078 index.ts_cycles.timestamp_begin = be64toh(ctf_index->timestamp_begin);
2079 index.ts_cycles.timestamp_end = be64toh(ctf_index->timestamp_end);
2080 index.events_discarded = be64toh(ctf_index->events_discarded);
0ace7505 2081 index.events_discarded_len = 64;
e69dd258 2082 index.data_offset = -1;
3ecd366e 2083 stream_id = be64toh(ctf_index->stream_id);
138cb20b
JD
2084 if (index_minor >= 1) {
2085 index.stream_instance_id = be64toh(ctf_index->stream_instance_id);
2086 index.packet_seq_num = be64toh(ctf_index->packet_seq_num);
2087 }
0ace7505
JD
2088
2089 if (!first_packet) {
2090 /* add index to packet array */
992e8cc0 2091 g_array_append_val(file_stream->pos.packet_index, index);
0ace7505
JD
2092 continue;
2093 }
2094
2095 file_stream->parent.stream_id = stream_id;
51e0087f
JG
2096 if (stream_id < td->streams->len) {
2097 stream = g_ptr_array_index(td->streams, stream_id);
2098 }
0ace7505
JD
2099 if (!stream) {
2100 fprintf(stderr, "[error] Stream %" PRIu64
2101 " is not declared in metadata.\n",
2102 stream_id);
2103 ret = -EINVAL;
2104 goto error;
2105 }
2106 file_stream->parent.stream_class = stream;
2107 ret = create_stream_definitions(td, &file_stream->parent);
2108 if (ret)
2109 goto error;
2110 first_packet = 0;
2111 /* add index to packet array */
992e8cc0 2112 g_array_append_val(file_stream->pos.packet_index, index);
0ace7505
JD
2113 }
2114
dae407df
JD
2115 /* Index containing only the header. */
2116 if (!file_stream->parent.stream_class) {
2117 ret = -1;
2118 goto error;
2119 }
2120
0ace7505
JD
2121 ret = 0;
2122
2123error:
3ecd366e 2124 g_free(ctf_index);
0ace7505
JD
2125 return ret;
2126}
2127
0f980a35
MD
2128/*
2129 * Note: many file streams can inherit from the same stream class
2130 * description (metadata).
2131 */
2132static
b086c01a 2133int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1cf393f6 2134 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
b086c01a 2135 int whence))
0f980a35 2136{
f824ae04 2137 int ret, fd, closeret;
0f980a35 2138 struct ctf_file_stream *file_stream;
ff075710 2139 struct stat statbuf;
0ace7505 2140 char *index_name;
0f980a35 2141
ff075710
MD
2142 fd = openat(td->dirfd, path, flags);
2143 if (fd < 0) {
a569a564 2144 perror("File stream openat()");
ff075710 2145 ret = fd;
0f980a35 2146 goto error;
a569a564 2147 }
ff075710
MD
2148
2149 /* Don't try to mmap subdirectories. Skip them, return success. */
2150 ret = fstat(fd, &statbuf);
2151 if (ret) {
2152 perror("File stream fstat()");
2153 goto fstat_error;
2154 }
2155 if (S_ISDIR(statbuf.st_mode)) {
0ace7505
JD
2156 if (strncmp(path, "index", 5) != 0) {
2157 fprintf(stderr, "[warning] Skipping directory '%s' "
2158 "found in trace\n", path);
2159 }
ff075710
MD
2160 ret = 0;
2161 goto fd_is_dir_ok;
2162 }
f4f0fb70
MD
2163 if (!statbuf.st_size) {
2164 /** Skip empty files. */
2165 ret = 0;
2166 goto fd_is_empty_file;
2167 }
ff075710 2168
0f980a35 2169 file_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 2170 file_stream->pos.last_offset = LAST_OFFSET_POISON;
0ace7505
JD
2171 file_stream->pos.fd = -1;
2172 file_stream->pos.index_fp = NULL;
b086c01a 2173
87148dc7
MD
2174 strncpy(file_stream->parent.path, path, PATH_MAX);
2175 file_stream->parent.path[PATH_MAX - 1] = '\0';
2176
06789ffd
MD
2177 if (packet_seek) {
2178 file_stream->pos.packet_seek = packet_seek;
b086c01a 2179 } else {
06789ffd 2180 fprintf(stderr, "[error] packet_seek function undefined.\n");
b086c01a
JD
2181 ret = -1;
2182 goto error_def;
2183 }
2184
ca334c72 2185 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
f824ae04
MD
2186 if (ret)
2187 goto error_def;
2d0bea29 2188 ret = create_trace_definitions(td, &file_stream->parent);
e28d4618
MD
2189 if (ret)
2190 goto error_def;
25ccc85b 2191 /*
50052405 2192 * For now, only a single clock per trace is supported.
25ccc85b 2193 */
7ec78969 2194 file_stream->parent.current_clock = td->parent.single_clock;
0ace7505
JD
2195
2196 /*
2197 * Allocate the index name for this stream and try to open it.
2198 */
2199 index_name = malloc((strlen(path) + sizeof(INDEX_PATH)) * sizeof(char));
2200 if (!index_name) {
2201 fprintf(stderr, "[error] Cannot allocate index filename\n");
f4f0fb70 2202 ret = -ENOMEM;
0ace7505 2203 goto error_def;
888ec52a 2204 }
0ace7505
JD
2205 snprintf(index_name, strlen(path) + sizeof(INDEX_PATH),
2206 INDEX_PATH, path);
2207
ff48c2b0 2208 if (bt_faccessat(td->dirfd, td->parent.path, index_name, O_RDONLY, 0) < 0) {
0ace7505
JD
2209 ret = create_stream_packet_index(td, file_stream);
2210 if (ret) {
2211 fprintf(stderr, "[error] Stream index creation error.\n");
2212 goto error_index;
2213 }
2214 } else {
2215 ret = openat(td->dirfd, index_name, flags);
2216 if (ret < 0) {
2217 perror("Index file openat()");
2218 ret = -1;
2219 goto error_free;
2220 }
2221 file_stream->pos.index_fp = fdopen(ret, "r");
2f0c6a52
MD
2222 if (!file_stream->pos.index_fp) {
2223 perror("fdopen() error");
2224 goto error_free;
2225 }
0ace7505
JD
2226 ret = import_stream_packet_index(td, file_stream);
2227 if (ret) {
2228 ret = -1;
2229 goto error_index;
2230 }
2231 ret = fclose(file_stream->pos.index_fp);
2232 if (ret < 0) {
2233 perror("close index");
2234 goto error_free;
2235 }
2236 }
2237 free(index_name);
2238
0f980a35 2239 /* Add stream file to stream class */
2d0bea29
MD
2240 g_ptr_array_add(file_stream->parent.stream_class->streams,
2241 &file_stream->parent);
0f980a35
MD
2242 return 0;
2243
2244error_index:
0ace7505
JD
2245 if (file_stream->pos.index_fp) {
2246 ret = fclose(file_stream->pos.index_fp);
2247 if (ret < 0) {
2248 perror("close index");
2249 }
2250 }
2d0bea29 2251 if (file_stream->parent.trace_packet_header)
13fad8b6 2252 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
0ace7505
JD
2253error_free:
2254 free(index_name);
e28d4618 2255error_def:
f824ae04
MD
2256 closeret = ctf_fini_pos(&file_stream->pos);
2257 if (closeret) {
2258 fprintf(stderr, "Error on ctf_fini_pos\n");
2259 }
0f980a35 2260 g_free(file_stream);
f4f0fb70 2261fd_is_empty_file:
ff075710
MD
2262fd_is_dir_ok:
2263fstat_error:
f824ae04
MD
2264 closeret = close(fd);
2265 if (closeret) {
2266 perror("Error on fd close");
2267 }
0f980a35 2268error:
65102a8c
MD
2269 return ret;
2270}
2271
bbefb8dd 2272static
5b80ddfb 2273int ctf_open_trace_read(struct ctf_trace *td,
8c250d87 2274 const char *path, int flags,
1cf393f6 2275 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 2276 int whence), FILE *metadata_fp)
bbefb8dd 2277{
6514b4af 2278 struct ctf_scanner *scanner;
f824ae04 2279 int ret, closeret;
65102a8c
MD
2280 struct dirent *dirent;
2281 struct dirent *diriter;
2282 size_t dirent_len;
0ace7505 2283 char *ext;
bbefb8dd 2284
46322b33 2285 td->flags = flags;
bbefb8dd
MD
2286
2287 /* Open trace directory */
46322b33
MD
2288 td->dir = opendir(path);
2289 if (!td->dir) {
6a6b384c 2290 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
bbefb8dd
MD
2291 ret = -ENOENT;
2292 goto error;
2293 }
2294
46322b33
MD
2295 td->dirfd = open(path, 0);
2296 if (td->dirfd < 0) {
6a6b384c 2297 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
a569a564
MD
2298 perror("Trace directory open");
2299 ret = -errno;
65102a8c
MD
2300 goto error_dirfd;
2301 }
caf929fa
MD
2302 strncpy(td->parent.path, path, sizeof(td->parent.path));
2303 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
0f980a35 2304
65102a8c
MD
2305 /*
2306 * Keep the metadata file separate.
0c880b0a
MD
2307 * Keep scanner object local to the open. We don't support
2308 * incremental metadata append for on-disk traces.
65102a8c 2309 */
6514b4af
MD
2310 scanner = ctf_scanner_alloc();
2311 if (!scanner) {
2312 fprintf(stderr, "[error] Error allocating scanner\n");
2313 ret = -ENOMEM;
2314 goto error_metadata;
2315 }
0c880b0a 2316 ret = ctf_trace_metadata_read(td, metadata_fp, scanner, 0);
6514b4af 2317 ctf_scanner_free(scanner);
65102a8c 2318 if (ret) {
251fc08c
MD
2319 if (ret == -ENOENT) {
2320 fprintf(stderr, "[warning] Empty metadata.\n");
2321 }
6a6b384c 2322 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
65102a8c
MD
2323 goto error_metadata;
2324 }
bbefb8dd
MD
2325
2326 /*
2327 * Open each stream: for each file, try to open, check magic
2328 * number, and get the stream ID to add to the right location in
2329 * the stream array.
bbefb8dd
MD
2330 */
2331
65102a8c 2332 dirent_len = offsetof(struct dirent, d_name) +
46322b33 2333 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 2334
65102a8c 2335 dirent = malloc(dirent_len);
bbefb8dd 2336
65102a8c 2337 for (;;) {
46322b33 2338 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 2339 if (ret) {
3394d22e 2340 fprintf(stderr, "[error] Readdir error.\n");
65102a8c 2341 goto readdir_error;
65102a8c
MD
2342 }
2343 if (!diriter)
2344 break;
d8ea2d29
MD
2345 /* Ignore hidden files, ., .. and metadata. */
2346 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
2347 || !strcmp(diriter->d_name, "..")
2348 || !strcmp(diriter->d_name, "metadata"))
2349 continue;
0ace7505
JD
2350
2351 /* Ignore index files : *.idx */
2352 ext = strrchr(diriter->d_name, '.');
2353 if (ext && (!strcmp(ext, ".idx"))) {
2354 continue;
2355 }
2356
06789ffd
MD
2357 ret = ctf_open_file_stream_read(td, diriter->d_name,
2358 flags, packet_seek);
dc48ecad 2359 if (ret) {
3394d22e 2360 fprintf(stderr, "[error] Open file stream error.\n");
dc48ecad
MD
2361 goto readdir_error;
2362 }
65102a8c 2363 }
bbefb8dd 2364
65102a8c 2365 free(dirent);
bbefb8dd 2366 return 0;
65102a8c
MD
2367
2368readdir_error:
2369 free(dirent);
2370error_metadata:
f824ae04
MD
2371 closeret = close(td->dirfd);
2372 if (closeret) {
2373 perror("Error on fd close");
2374 }
65102a8c 2375error_dirfd:
f824ae04
MD
2376 closeret = closedir(td->dir);
2377 if (closeret) {
2378 perror("Error on closedir");
2379 }
bbefb8dd
MD
2380error:
2381 return ret;
2382}
2383
03798a93
JD
2384/*
2385 * ctf_open_trace: Open a CTF trace and index it.
2386 * Note that the user must seek the trace after the open (using the iterator)
2387 * since the index creation read it entirely.
2388 */
e9378815 2389static
1b8455b7 2390struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1cf393f6 2391 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 2392 int whence), FILE *metadata_fp)
bbefb8dd 2393{
46322b33 2394 struct ctf_trace *td;
bbefb8dd
MD
2395 int ret;
2396
2715de36
MD
2397 /*
2398 * If packet_seek is NULL, we provide our default version.
2399 */
2400 if (!packet_seek)
2401 packet_seek = ctf_packet_seek;
2402
46322b33 2403 td = g_new0(struct ctf_trace, 1);
837b0013
JG
2404 if (!td) {
2405 goto error;
2406 }
2407 init_trace_descriptor(&td->parent);
bbefb8dd 2408
8c572eba 2409 switch (flags & O_ACCMODE) {
bbefb8dd 2410 case O_RDONLY:
b61922b5 2411 if (!path) {
3394d22e 2412 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
b61922b5
MD
2413 goto error;
2414 }
06789ffd 2415 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
bbefb8dd
MD
2416 if (ret)
2417 goto error;
2418 break;
989c73bc 2419 case O_RDWR:
3394d22e 2420 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
46322b33 2421 goto error;
bbefb8dd 2422 default:
3394d22e 2423 fprintf(stderr, "[error] Incorrect open flags.\n");
bbefb8dd
MD
2424 goto error;
2425 }
2426
b5a8598f
AB
2427 ret = trace_debug_info_create(td);
2428 if (ret) {
2429 goto error;
2430 }
2431
46322b33 2432 return &td->parent;
bbefb8dd 2433error:
b5a8598f 2434 trace_debug_info_destroy(td);
bbefb8dd
MD
2435 g_free(td);
2436 return NULL;
2437}
2438
2e937fb4 2439static
f571dfb1 2440void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
c150f912 2441 struct bt_mmap_stream *mmap_info)
f571dfb1
JD
2442{
2443 pos->mmap_offset = 0;
2444 pos->packet_size = 0;
2445 pos->content_size = 0;
2446 pos->content_size_loc = NULL;
2447 pos->fd = mmap_info->fd;
aee35fcc 2448 pos->base_mma = NULL;
f571dfb1
JD
2449 pos->offset = 0;
2450 pos->dummy = false;
2451 pos->cur_index = 0;
f571dfb1
JD
2452 pos->prot = PROT_READ;
2453 pos->flags = MAP_PRIVATE;
2454 pos->parent.rw_table = read_dispatch_table;
2455 pos->parent.event_cb = ctf_read_event;
731087d8 2456 pos->priv = mmap_info->priv;
f1f52630
MD
2457 pos->packet_index = g_array_new(FALSE, TRUE,
2458 sizeof(struct packet_index));
f571dfb1
JD
2459}
2460
2461static
2462int prepare_mmap_stream_definition(struct ctf_trace *td,
fb6b45aa
JD
2463 struct ctf_file_stream *file_stream,
2464 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2465 int whence))
f571dfb1 2466{
f380e105 2467 struct ctf_stream_declaration *stream;
fb6b45aa 2468 uint64_t stream_id;
f571dfb1
JD
2469 int ret;
2470
fb6b45aa
JD
2471 /* Ask for the first packet to get the stream_id. */
2472 packet_seek(&file_stream->pos.parent, 0, SEEK_SET);
2473 stream_id = file_stream->parent.stream_id;
f571dfb1 2474 if (stream_id >= td->streams->len) {
3394d22e 2475 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
2476 "in metadata.\n", stream_id);
2477 ret = -EINVAL;
2478 goto end;
2479 }
2480 stream = g_ptr_array_index(td->streams, stream_id);
2481 if (!stream) {
3394d22e 2482 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
2483 "in metadata.\n", stream_id);
2484 ret = -EINVAL;
2485 goto end;
2486 }
2487 file_stream->parent.stream_class = stream;
2488 ret = create_stream_definitions(td, &file_stream->parent);
2489end:
2490 return ret;
2491}
2492
2493static
2494int ctf_open_mmap_stream_read(struct ctf_trace *td,
c150f912 2495 struct bt_mmap_stream *mmap_info,
1cf393f6 2496 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
f571dfb1
JD
2497 int whence))
2498{
2499 int ret;
2500 struct ctf_file_stream *file_stream;
2501
2502 file_stream = g_new0(struct ctf_file_stream, 1);
fb6b45aa 2503 file_stream->parent.stream_id = -1ULL;
3a25e036 2504 file_stream->pos.last_offset = LAST_OFFSET_POISON;
f571dfb1
JD
2505 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
2506
06789ffd 2507 file_stream->pos.packet_seek = packet_seek;
f571dfb1
JD
2508
2509 ret = create_trace_definitions(td, &file_stream->parent);
2510 if (ret) {
2511 goto error_def;
2512 }
2513
fb6b45aa 2514 ret = prepare_mmap_stream_definition(td, file_stream, packet_seek);
f571dfb1
JD
2515 if (ret)
2516 goto error_index;
2517
f7bbd502 2518 /*
50052405 2519 * For now, only a single clock per trace is supported.
f7bbd502 2520 */
7ec78969 2521 file_stream->parent.current_clock = td->parent.single_clock;
f7bbd502 2522
f571dfb1
JD
2523 /* Add stream file to stream class */
2524 g_ptr_array_add(file_stream->parent.stream_class->streams,
2525 &file_stream->parent);
2526 return 0;
2527
2528error_index:
2529 if (file_stream->parent.trace_packet_header)
13fad8b6 2530 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
f571dfb1
JD
2531error_def:
2532 g_free(file_stream);
2533 return ret;
2534}
2535
2e937fb4 2536static
f571dfb1 2537int ctf_open_mmap_trace_read(struct ctf_trace *td,
c150f912 2538 struct bt_mmap_stream_list *mmap_list,
1cf393f6 2539 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
f571dfb1
JD
2540 int whence),
2541 FILE *metadata_fp)
2542{
2543 int ret;
c150f912 2544 struct bt_mmap_stream *mmap_info;
f571dfb1 2545
0c880b0a
MD
2546 td->scanner = ctf_scanner_alloc();
2547 if (!td->scanner) {
6514b4af
MD
2548 fprintf(stderr, "[error] Error allocating scanner\n");
2549 ret = -ENOMEM;
0c880b0a 2550 goto error;
6514b4af 2551 }
0c880b0a 2552 ret = ctf_trace_metadata_read(td, metadata_fp, td->scanner, 0);
f571dfb1 2553 if (ret) {
251fc08c
MD
2554 if (ret == -ENOENT) {
2555 fprintf(stderr, "[warning] Empty metadata.\n");
2556 }
f571dfb1
JD
2557 goto error;
2558 }
2559
2560 /*
2561 * for each stream, try to open, check magic number, and get the
2562 * stream ID to add to the right location in the stream array.
2563 */
3122e6f0 2564 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
06789ffd 2565 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
f571dfb1 2566 if (ret) {
3394d22e 2567 fprintf(stderr, "[error] Open file mmap stream error.\n");
f571dfb1
JD
2568 goto error;
2569 }
2570 }
f571dfb1
JD
2571 return 0;
2572
2573error:
0c880b0a 2574 ctf_scanner_free(td->scanner);
f571dfb1
JD
2575 return ret;
2576}
2577
2578static
1b8455b7 2579struct bt_trace_descriptor *ctf_open_mmap_trace(
c150f912 2580 struct bt_mmap_stream_list *mmap_list,
1cf393f6 2581 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
20d0dcf9 2582 int whence),
f571dfb1
JD
2583 FILE *metadata_fp)
2584{
2585 struct ctf_trace *td;
2586 int ret;
2587
2588 if (!metadata_fp) {
2589 fprintf(stderr, "[error] No metadata file pointer associated, "
2590 "required for mmap parsing\n");
2591 goto error;
2592 }
06789ffd
MD
2593 if (!packet_seek) {
2594 fprintf(stderr, "[error] packet_seek function undefined.\n");
f571dfb1
JD
2595 goto error;
2596 }
2597 td = g_new0(struct ctf_trace, 1);
b5a1fa45 2598 td->dirfd = -1;
06789ffd 2599 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
f571dfb1
JD
2600 if (ret)
2601 goto error_free;
2602
b5a8598f
AB
2603 ret = trace_debug_info_create(td);
2604 if (ret) {
2605 goto error_free;
2606 }
2607
f571dfb1
JD
2608 return &td->parent;
2609
2610error_free:
2611 g_free(td);
2612error:
2613 return NULL;
2614}
2615
0c880b0a
MD
2616int ctf_append_trace_metadata(struct bt_trace_descriptor *tdp,
2617 FILE *metadata_fp)
2618{
2619 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2620 int i, j;
2621 int ret;
2622
2623 if (!td->scanner)
2624 return -EINVAL;
2625 ret = ctf_trace_metadata_read(td, metadata_fp, td->scanner, 1);
2626 if (ret)
2627 return ret;
2628 /* for each stream_class */
2629 for (i = 0; i < td->streams->len; i++) {
2630 struct ctf_stream_declaration *stream_class;
2631
2632 stream_class = g_ptr_array_index(td->streams, i);
2633 if (!stream_class)
2634 continue;
2635 /* for each stream */
2636 for (j = 0; j < stream_class->streams->len; j++) {
2637 struct ctf_stream_definition *stream;
2638
2639 stream = g_ptr_array_index(stream_class->streams, j);
2640 if (!stream)
2641 continue;
2642 ret = copy_event_declarations_stream_class_to_stream(td,
2643 stream_class, stream);
2644 if (ret)
2645 return ret;
2646 }
2647 }
2648 return 0;
2649}
2650
03798a93 2651static
1b8455b7 2652int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
03798a93
JD
2653{
2654 int i, j, k;
2655 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2656
2657 /* for each stream_class */
2658 for (i = 0; i < td->streams->len; i++) {
2659 struct ctf_stream_declaration *stream_class;
2660
2661 stream_class = g_ptr_array_index(td->streams, i);
2662 if (!stream_class)
2663 continue;
2664 /* for each file_stream */
2665 for (j = 0; j < stream_class->streams->len; j++) {
2666 struct ctf_stream_definition *stream;
2667 struct ctf_stream_pos *stream_pos;
2668 struct ctf_file_stream *cfs;
2669
2670 stream = g_ptr_array_index(stream_class->streams, j);
2671 if (!stream)
2672 continue;
2673 cfs = container_of(stream, struct ctf_file_stream,
2674 parent);
2675 stream_pos = &cfs->pos;
992e8cc0 2676 if (!stream_pos->packet_index)
afe9cd4a
JD
2677 continue;
2678
992e8cc0 2679 for (k = 0; k < stream_pos->packet_index->len; k++) {
03798a93 2680 struct packet_index *index;
03798a93 2681
992e8cc0 2682 index = &g_array_index(stream_pos->packet_index,
03798a93 2683 struct packet_index, k);
992e8cc0 2684 index->ts_real.timestamp_begin =
03798a93 2685 ctf_get_real_timestamp(stream,
992e8cc0
MD
2686 index->ts_cycles.timestamp_begin);
2687 index->ts_real.timestamp_end =
03798a93 2688 ctf_get_real_timestamp(stream,
992e8cc0 2689 index->ts_cycles.timestamp_end);
03798a93
JD
2690 }
2691 }
2692 }
2693 return 0;
2694}
2695
0f980a35 2696static
f824ae04 2697int ctf_close_file_stream(struct ctf_file_stream *file_stream)
0f980a35 2698{
f824ae04
MD
2699 int ret;
2700
2701 ret = ctf_fini_pos(&file_stream->pos);
2702 if (ret) {
2703 fprintf(stderr, "Error on ctf_fini_pos\n");
2704 return -1;
2705 }
500634be
JD
2706 if (file_stream->pos.fd >= 0) {
2707 ret = close(file_stream->pos.fd);
2708 if (ret) {
2709 perror("Error closing file fd");
2710 return -1;
2711 }
f824ae04
MD
2712 }
2713 return 0;
0f980a35
MD
2714}
2715
e9378815 2716static
1b8455b7 2717int ctf_close_trace(struct bt_trace_descriptor *tdp)
bbefb8dd 2718{
46322b33 2719 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
08c82b90 2720 int ret;
0f980a35 2721
46322b33 2722 if (td->streams) {
08c82b90
MD
2723 int i;
2724
46322b33 2725 for (i = 0; i < td->streams->len; i++) {
f380e105 2726 struct ctf_stream_declaration *stream;
0f980a35 2727 int j;
e9378815 2728
46322b33 2729 stream = g_ptr_array_index(td->streams, i);
e9378815
MD
2730 if (!stream)
2731 continue;
2d0bea29 2732 for (j = 0; j < stream->streams->len; j++) {
0f980a35 2733 struct ctf_file_stream *file_stream;
15d4fe3c
JD
2734 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2735 struct ctf_file_stream, parent);
f824ae04
MD
2736 ret = ctf_close_file_stream(file_stream);
2737 if (ret)
2738 return ret;
0f980a35 2739 }
e003ab50 2740 }
e003ab50 2741 }
15d4fe3c 2742 ctf_destroy_metadata(td);
0c880b0a 2743 ctf_scanner_free(td->scanner);
500634be
JD
2744 if (td->dirfd >= 0) {
2745 ret = close(td->dirfd);
2746 if (ret) {
2747 perror("Error closing dirfd");
2748 return ret;
2749 }
f824ae04 2750 }
500634be
JD
2751 if (td->dir) {
2752 ret = closedir(td->dir);
2753 if (ret) {
2754 perror("Error closedir");
2755 return ret;
2756 }
f824ae04 2757 }
7237592a 2758 free(td->metadata_string);
b5a8598f 2759 trace_debug_info_destroy(td);
bbefb8dd 2760 g_free(td);
f824ae04 2761 return 0;
bbefb8dd
MD
2762}
2763
98a04903 2764static
1b8455b7 2765void ctf_set_context(struct bt_trace_descriptor *descriptor,
98a04903
JD
2766 struct bt_context *ctx)
2767{
2768 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2769 parent);
2770
45807148 2771 td->parent.ctx = ctx;
98a04903
JD
2772}
2773
2774static
1b8455b7 2775void ctf_set_handle(struct bt_trace_descriptor *descriptor,
98a04903
JD
2776 struct bt_trace_handle *handle)
2777{
2778 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2779 parent);
2780
4d086981 2781 td->parent.handle = handle;
98a04903
JD
2782}
2783
95febab3 2784static
7fb21036 2785void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
2786{
2787 int ret;
2788
4c8bfb7e 2789 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
2790 ret = bt_register_format(&ctf_format);
2791 assert(!ret);
2792}
698f0fe4 2793
95febab3
MD
2794static
2795void __attribute__((destructor)) ctf_exit(void)
2796{
2797 bt_unregister_format(&ctf_format);
2798}
This page took 0.194668 seconds and 4 git commands to generate.