Backport the CTF-IR interface
[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
MD
782 break;
783 default:
784 assert(0);
8c572eba 785 }
f824ae04 786 return 0;
8c572eba
MD
787}
788
f824ae04 789int ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba 790{
fcf10417 791 if ((pos->prot & PROT_WRITE) && pos->content_size_loc)
8c572eba 792 *pos->content_size_loc = pos->offset;
aee35fcc 793 if (pos->base_mma) {
08c82b90
MD
794 int ret;
795
8c572eba 796 /* unmap old base */
aee35fcc 797 ret = munmap_align(pos->base_mma);
8c572eba 798 if (ret) {
3394d22e 799 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
8c572eba 800 strerror(errno));
f824ae04 801 return -1;
8c572eba
MD
802 }
803 }
992e8cc0
MD
804 if (pos->packet_index)
805 (void) g_array_free(pos->packet_index, TRUE);
f824ae04 806 return 0;
8c572eba
MD
807}
808
f1f52630
MD
809void ctf_update_current_packet_index(struct ctf_stream_definition *stream,
810 struct packet_index *prev_index,
811 struct packet_index *cur_index)
812{
813 uint64_t events_discarded_diff;
2328c2cd 814 uint64_t packets_lost_diff = 0;
f1f52630
MD
815
816 /* Update packet index time information */
2654fe9b
MD
817
818 /* Current packet begin/end */
819 stream->current.real.begin =
820 cur_index->ts_real.timestamp_begin;
821 stream->current.cycles.begin =
f1f52630 822 cur_index->ts_cycles.timestamp_begin;
2654fe9b 823 stream->current.real.end =
f1f52630 824 cur_index->ts_real.timestamp_end;
2654fe9b
MD
825 stream->current.cycles.end =
826 cur_index->ts_cycles.timestamp_end;
f1f52630
MD
827
828 /* Update packet index discarded event information */
829 events_discarded_diff = cur_index->events_discarded;
830 if (prev_index) {
2654fe9b
MD
831 /* Previous packet begin/end */
832 stream->prev.cycles.begin =
833 prev_index->ts_cycles.timestamp_begin;
834 stream->prev.real.begin =
835 prev_index->ts_real.timestamp_begin;
836 stream->prev.cycles.end =
837 prev_index->ts_cycles.timestamp_end;
838 stream->prev.real.end =
839 prev_index->ts_real.timestamp_end;
840
f1f52630 841 events_discarded_diff -= prev_index->events_discarded;
2328c2cd
JD
842 /* packet_seq_num stays at 0 if not produced by the tracer */
843 if (cur_index->packet_seq_num) {
844 packets_lost_diff = cur_index->packet_seq_num -
845 prev_index->packet_seq_num - 1;
846 }
f1f52630
MD
847 /*
848 * Deal with 32-bit wrap-around if the tracer provided a
849 * 32-bit field.
850 */
851 if (prev_index->events_discarded_len == 32) {
852 events_discarded_diff = (uint32_t) events_discarded_diff;
853 }
2654fe9b
MD
854 } else {
855 /*
856 * First packet: use current packet info as limits for
857 * previous packet.
858 */
859 stream->prev.cycles.begin =
860 stream->prev.cycles.end =
861 stream->current.cycles.begin;
862 stream->prev.real.begin =
863 stream->prev.real.end =
864 stream->current.real.begin;
f1f52630
MD
865 }
866 stream->events_discarded = events_discarded_diff;
2328c2cd 867 stream->packets_lost = packets_lost_diff;
f1f52630
MD
868}
869
37ea109a
JD
870/*
871 * Find the timerange where all the streams in the trace are active
872 * simultaneously.
873 *
874 * Return 0 and update begin/end if necessary on success, return 1 for
875 * empty streams and return a negative value on error.
876 */
877static
837b0013
JG
878int ctf_find_stream_intersection(struct bt_trace_descriptor *td_read,
879 struct packet_index_time *_real,
880 struct packet_index_time *_cycles)
37ea109a 881{
37ea109a 882 int stream_id, ret = 0;
837b0013
JG
883 struct packet_index_time real = { 0, UINT64_MAX },
884 cycles = { 0, UINT64_MAX };
885 struct ctf_trace *tin = container_of(td_read, struct ctf_trace, parent);
37ea109a 886
837b0013
JG
887 /* At least one of the two return args must be provided. */
888 if (!_real && !_cycles) {
889 ret = -1;
890 goto end;
891 }
892
893 if (tin->streams->len == 0) {
894 ret = 1;
895 goto end;
896 }
37ea109a
JD
897
898 for (stream_id = 0; stream_id < tin->streams->len;
899 stream_id++) {
900 int filenr;
901 struct ctf_stream_declaration *stream_class;
902
903 stream_class = g_ptr_array_index(tin->streams, stream_id);
904 if (!stream_class) {
905 continue;
906 }
907 for (filenr = 0; filenr < stream_class->streams->len; filenr++) {
908 struct ctf_file_stream *file_stream;
909 struct ctf_stream_pos *stream_pos;
910 struct packet_index *index;
911
912 file_stream = g_ptr_array_index(stream_class->streams,
913 filenr);
914 if (!file_stream) {
915 continue;
916 }
917 stream_pos = &file_stream->pos;
918 if (!stream_pos->packet_index ||
919 stream_pos->packet_index->len <= 0) {
920 ret = 1;
921 goto end;
922 }
923 index = &g_array_index(stream_pos->packet_index,
924 struct packet_index, 0);
837b0013
JG
925 real.timestamp_begin = max(real.timestamp_begin,
926 index->ts_real.timestamp_begin);
927 cycles.timestamp_begin = max(cycles.timestamp_begin,
928 index->ts_cycles.timestamp_begin);
929
37ea109a
JD
930 index = &g_array_index(stream_pos->packet_index,
931 struct packet_index,
932 stream_pos->packet_index->len - 1);
837b0013
JG
933 real.timestamp_end = min(real.timestamp_end,
934 index->ts_real.timestamp_end);
935 cycles.timestamp_end = min(cycles.timestamp_end,
936 index->ts_cycles.timestamp_end);
37ea109a
JD
937 }
938 }
37ea109a 939end:
837b0013
JG
940 if (ret == 0) {
941 if (_real) {
942 *_real = real;
943 }
944 if (_cycles) {
945 *_cycles = cycles;
946 }
947 }
37ea109a
JD
948 return ret;
949}
950
951/*
837b0013
JG
952 * Find the union of all active regions in the trace collection's traces.
953 * Returns "real" timestamps.
37ea109a
JD
954 *
955 * Return 0 on success.
956 * Return 1 if no intersections are found.
957 * Return a negative value on error.
958 */
837b0013
JG
959int ctf_find_tc_stream_packet_intersection_union(struct bt_context *ctx,
960 uint64_t *_ts_begin, uint64_t *_ts_end)
37ea109a 961{
837b0013
JG
962 int ret = 0, i;
963 uint64_t ts_begin = UINT64_MAX, ts_end = 0;
37ea109a 964
837b0013 965 if (!ctx || !ctx->tc || !ctx->tc->array || !_ts_begin || !_ts_end) {
37ea109a
JD
966 ret = -EINVAL;
967 goto end;
968 }
969
970 for (i = 0; i < ctx->tc->array->len; i++) {
971 struct bt_trace_descriptor *td_read;
837b0013 972 struct packet_index_time intersection_real;
37ea109a
JD
973
974 td_read = g_ptr_array_index(ctx->tc->array, i);
975 if (!td_read) {
976 continue;
977 }
837b0013
JG
978 ret = ctf_find_stream_intersection(td_read, &intersection_real,
979 NULL);
980 if (ret == 1) {
981 /* Empty trace or no stream intersection. */
982 continue;
983 } else if (ret < 0) {
37ea109a
JD
984 goto end;
985 }
837b0013
JG
986
987 ts_begin = min(intersection_real.timestamp_begin, ts_begin);
988 ts_end = max(intersection_real.timestamp_end, ts_end);
37ea109a 989 }
837b0013
JG
990
991 if (ts_end < ts_begin) {
37ea109a 992 ret = 1;
837b0013
JG
993 goto end;
994 }
995 *_ts_begin = ts_begin;
996 *_ts_end = ts_end;
997end:
998 return ret;
999}
1000
1001int ctf_tc_set_stream_intersection_mode(struct bt_context *ctx)
1002{
1003 int ret = 0, i;
1004
1005 if (!ctx || !ctx->tc || !ctx->tc->array) {
1006 ret = -EINVAL;
1007 goto end;
1008 }
1009
1010 for (i = 0; i < ctx->tc->array->len; i++) {
1011 struct bt_trace_descriptor *td_read;
1012 struct packet_index_time intersection_real;
1013
1014 td_read = g_ptr_array_index(ctx->tc->array, i);
1015 if (!td_read) {
1016 continue;
1017 }
1018
1019 ret = ctf_find_stream_intersection(td_read, &intersection_real,
1020 NULL);
1021 if (ret == 1) {
1022 /* Empty trace or no stream intersection. */
1023 continue;
1024 } else if (ret < 0) {
1025 goto end;
1026 }
1027
1028 td_read->interval_real = intersection_real;
1029 td_read->interval_set = true;
37ea109a
JD
1030 }
1031end:
1032 return ret;
1033}
1034
20d0dcf9
MD
1035/*
1036 * for SEEK_CUR: go to next packet.
34b8fff6 1037 * for SEEK_SET: go to packet numer (index).
20d0dcf9 1038 */
1cf393f6 1039void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
0f980a35 1040{
d6425aaf
MD
1041 struct ctf_stream_pos *pos =
1042 container_of(stream_pos, struct ctf_stream_pos, parent);
75cc2c35
MD
1043 struct ctf_file_stream *file_stream =
1044 container_of(pos, struct ctf_file_stream, pos);
0f980a35 1045 int ret;
2654fe9b 1046 struct packet_index *packet_index, *prev_index;
0f980a35 1047
5bfcad93
MD
1048 switch (whence) {
1049 case SEEK_CUR:
1050 case SEEK_SET: /* Fall-through */
1051 break; /* OK */
1052 default:
1053 assert(0);
1054 }
1055
fcf10417 1056 if ((pos->prot & PROT_WRITE) && pos->content_size_loc)
8c572eba 1057 *pos->content_size_loc = pos->offset;
0f980a35 1058
aee35fcc 1059 if (pos->base_mma) {
0f980a35 1060 /* unmap old base */
aee35fcc 1061 ret = munmap_align(pos->base_mma);
0f980a35 1062 if (ret) {
3394d22e 1063 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
1064 strerror(errno));
1065 assert(0);
1066 }
aee35fcc 1067 pos->base_mma = NULL;
0f980a35
MD
1068 }
1069
8c572eba 1070 /*
46322b33 1071 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
1072 * except to get exactly at the beginning of the next packet.
1073 */
fcf10417 1074 if (pos->prot & PROT_WRITE) {
989c73bc
MD
1075 switch (whence) {
1076 case SEEK_CUR:
1077 /* The writer will add padding */
e0a5b455 1078 pos->mmap_offset += pos->packet_size / CHAR_BIT;
989c73bc
MD
1079 break;
1080 case SEEK_SET:
20d0dcf9 1081 assert(index == 0); /* only seek supported for now */
989c73bc
MD
1082 pos->cur_index = 0;
1083 break;
1084 default:
1085 assert(0);
1086 }
8c572eba
MD
1087 pos->content_size = -1U; /* Unknown at this point */
1088 pos->packet_size = WRITE_PACKET_LEN;
118f5776
MD
1089 do {
1090 ret = bt_posix_fallocate(pos->fd, pos->mmap_offset,
1091 pos->packet_size / CHAR_BIT);
1092 } while (ret == EINTR);
1093 assert(ret == 0);
847bf71a 1094 pos->offset = 0;
8c572eba 1095 } else {
5f643ed7 1096 read_next_packet:
847bf71a
MD
1097 switch (whence) {
1098 case SEEK_CUR:
41e82e00 1099 {
7d97fad9
MD
1100 if (pos->offset == EOF) {
1101 return;
1102 }
992e8cc0 1103 assert(pos->cur_index < pos->packet_index->len);
847bf71a 1104 /* The reader will expect us to skip padding */
8c572eba 1105 ++pos->cur_index;
847bf71a 1106 break;
41e82e00 1107 }
847bf71a 1108 case SEEK_SET:
992e8cc0 1109 if (index >= pos->packet_index->len) {
c309df1c
MD
1110 pos->offset = EOF;
1111 return;
1112 }
20d0dcf9 1113 pos->cur_index = index;
847bf71a
MD
1114 break;
1115 default:
1116 assert(0);
1117 }
2654fe9b 1118
0e1b84aa
MD
1119 if (pos->cur_index >= pos->packet_index->len) {
1120 pos->offset = EOF;
1121 return;
1122 }
1123
2654fe9b
MD
1124 packet_index = &g_array_index(pos->packet_index,
1125 struct packet_index, pos->cur_index);
1126 if (pos->cur_index > 0) {
1127 prev_index = &g_array_index(pos->packet_index,
1128 struct packet_index,
1129 pos->cur_index - 1);
1130 } else {
1131 prev_index = NULL;
1132 }
1133 ctf_update_current_packet_index(&file_stream->parent,
1134 prev_index, packet_index);
1135
2654fe9b
MD
1136 /*
1137 * We need to check if we are in trace read or called
1138 * from packet indexing. In this last case, the
1139 * collection is not there, so we cannot print the
1140 * timestamps.
1141 */
1142 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
2328c2cd 1143 ctf_print_discarded_lost(stderr, &file_stream->parent);
2654fe9b
MD
1144 }
1145
992e8cc0 1146 packet_index = &g_array_index(pos->packet_index,
03798a93
JD
1147 struct packet_index,
1148 pos->cur_index);
992e8cc0 1149 file_stream->parent.cycles_timestamp = packet_index->ts_cycles.timestamp_begin;
03798a93 1150
992e8cc0 1151 file_stream->parent.real_timestamp = packet_index->ts_real.timestamp_begin;
8c572eba
MD
1152
1153 /* Lookup context/packet size in index */
e69dd258
JD
1154 if (packet_index->data_offset == -1) {
1155 ret = find_data_offset(pos, file_stream, packet_index);
1156 if (ret < 0) {
1157 return;
1158 }
1159 }
20d0dcf9
MD
1160 pos->content_size = packet_index->content_size;
1161 pos->packet_size = packet_index->packet_size;
e69dd258 1162 pos->mmap_offset = packet_index->offset;
500634be
JD
1163 pos->data_offset = packet_index->data_offset;
1164 if (pos->data_offset < packet_index->content_size) {
75cc2c35 1165 pos->offset = 0; /* will read headers */
500634be 1166 } else if (pos->data_offset == packet_index->content_size) {
5f643ed7 1167 /* empty packet */
20d0dcf9 1168 pos->offset = packet_index->data_offset;
3abe83c7 1169 whence = SEEK_CUR;
5f643ed7 1170 goto read_next_packet;
7eda6fc7 1171 } else {
2b9a764d
MD
1172 pos->offset = EOF;
1173 return;
1174 }
8c572eba 1175 }
0f980a35 1176 /* map new base. Need mapping length from header. */
aee35fcc
MD
1177 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
1178 pos->flags, pos->fd, pos->mmap_offset);
1179 if (pos->base_mma == MAP_FAILED) {
3394d22e 1180 fprintf(stderr, "[error] mmap error %s.\n",
847bf71a
MD
1181 strerror(errno));
1182 assert(0);
1183 }
75cc2c35
MD
1184
1185 /* update trace_packet_header and stream_packet_context */
fcf10417
JG
1186 if (!(pos->prot & PROT_WRITE) &&
1187 file_stream->parent.trace_packet_header) {
75cc2c35 1188 /* Read packet header */
2d0bea29 1189 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
75cc2c35
MD
1190 assert(!ret);
1191 }
fcf10417
JG
1192 if (!(pos->prot & PROT_WRITE) &&
1193 file_stream->parent.stream_packet_context) {
75cc2c35 1194 /* Read packet context */
2d0bea29 1195 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
75cc2c35
MD
1196 assert(!ret);
1197 }
0f980a35
MD
1198}
1199
b4c19c1e
MD
1200static
1201int packet_metadata(struct ctf_trace *td, FILE *fp)
1202{
1203 uint32_t magic;
1204 size_t len;
1205 int ret = 0;
1206
1207 len = fread(&magic, sizeof(magic), 1, fp);
a0fe7d97 1208 if (len != 1) {
b4c19c1e
MD
1209 goto end;
1210 }
1211 if (magic == TSDL_MAGIC) {
1212 ret = 1;
1213 td->byte_order = BYTE_ORDER;
0d336fdf 1214 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
1215 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
1216 ret = 1;
1217 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
1218 LITTLE_ENDIAN : BIG_ENDIAN;
0d336fdf 1219 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
1220 }
1221end:
1222 rewind(fp);
1223 return ret;
1224}
1225
5c262147
MD
1226/*
1227 * Returns 0 on success, -1 on error.
1228 */
1229static
1230int check_version(unsigned int major, unsigned int minor)
1231{
1232 switch (major) {
1233 case 1:
1234 switch (minor) {
1235 case 8:
1236 return 0;
1237 default:
1238 goto warning;
1239 }
1240 default:
1241 goto warning;
1242
1243 }
1244
1245 /* eventually return an error instead of warning */
1246warning:
3394d22e 1247 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
5c262147
MD
1248 major, minor);
1249 return 0;
1250}
1251
b4c19c1e 1252static
0c880b0a 1253int ctf_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
b4c19c1e
MD
1254 FILE *out)
1255{
1256 struct metadata_packet_header header;
a0fe7d97 1257 size_t readlen, writelen, toread;
f8254867 1258 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
b4c19c1e
MD
1259 int ret = 0;
1260
a91a962e 1261 readlen = fread(&header, header_sizeof(header), 1, in);
a0fe7d97 1262 if (readlen < 1)
b4c19c1e
MD
1263 return -EINVAL;
1264
1265 if (td->byte_order != BYTE_ORDER) {
1266 header.magic = GUINT32_SWAP_LE_BE(header.magic);
1267 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
1268 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
1269 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
1270 }
1271 if (header.checksum)
3394d22e 1272 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
b4c19c1e 1273 if (header.compression_scheme) {
3394d22e 1274 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
b4c19c1e
MD
1275 header.compression_scheme);
1276 return -EINVAL;
1277 }
1278 if (header.encryption_scheme) {
3394d22e 1279 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
b4c19c1e
MD
1280 header.encryption_scheme);
1281 return -EINVAL;
1282 }
1283 if (header.checksum_scheme) {
3394d22e 1284 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
b4c19c1e
MD
1285 header.checksum_scheme);
1286 return -EINVAL;
1287 }
5c262147
MD
1288 if (check_version(header.major, header.minor) < 0)
1289 return -EINVAL;
b4c19c1e
MD
1290 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
1291 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
1292 CTF_TRACE_SET_FIELD(td, uuid);
1293 } else {
72a3bc16 1294 if (bt_uuid_compare(header.uuid, td->uuid))
b4c19c1e
MD
1295 return -EINVAL;
1296 }
1297
b1ccd079
MD
1298 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
1299 return -EINVAL;
1300
255b2138 1301 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
a0fe7d97
MD
1302
1303 for (;;) {
f8254867 1304 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
b4c19c1e
MD
1305 if (ferror(in)) {
1306 ret = -EINVAL;
1307 break;
1308 }
4152822b 1309 if (babeltrace_debug) {
f8254867 1310 buf[readlen] = '\0';
3394d22e 1311 fprintf(stderr, "[debug] metadata packet read: %s\n",
4152822b
MD
1312 buf);
1313 }
1314
b4c19c1e
MD
1315 writelen = fwrite(buf, sizeof(char), readlen, out);
1316 if (writelen < readlen) {
1317 ret = -EIO;
1318 break;
1319 }
1320 if (ferror(out)) {
1321 ret = -EINVAL;
1322 break;
1323 }
a0fe7d97
MD
1324 toread -= readlen;
1325 if (!toread) {
7f4b5c4d 1326 ret = 0; /* continue reading next packet */
ddbc52af 1327 goto read_padding;
a0fe7d97 1328 }
b4c19c1e
MD
1329 }
1330 return ret;
ddbc52af
MD
1331
1332read_padding:
1333 toread = (header.packet_size - header.content_size) / CHAR_BIT;
1334 ret = fseek(in, toread, SEEK_CUR);
1335 if (ret < 0) {
3394d22e 1336 fprintf(stderr, "[warning] Missing padding at end of file\n");
ddbc52af
MD
1337 ret = 0;
1338 }
1339 return ret;
b4c19c1e
MD
1340}
1341
1342static
0c880b0a 1343int ctf_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
b4c19c1e
MD
1344 char **buf)
1345{
1346 FILE *in, *out;
abc40d24 1347 size_t size, buflen;
b4c19c1e
MD
1348 int ret;
1349
1350 in = *fp;
c4f5487e
MD
1351 /*
1352 * Using strlen on *buf instead of size of open_memstream
1353 * because its size includes garbage at the end (after final
1354 * \0). This is the allocated size, not the actual string size.
1355 */
f8370579 1356 out = babeltrace_open_memstream(buf, &size);
a569a564
MD
1357 if (out == NULL) {
1358 perror("Metadata open_memstream");
b4c19c1e 1359 return -errno;
a569a564 1360 }
b4c19c1e 1361 for (;;) {
0c880b0a 1362 ret = ctf_trace_metadata_packet_read(td, in, out);
7f4b5c4d 1363 if (ret) {
b4c19c1e 1364 break;
7f4b5c4d
MD
1365 }
1366 if (feof(in)) {
1367 ret = 0;
b4c19c1e
MD
1368 break;
1369 }
1370 }
f8370579
MD
1371 /* close to flush the buffer */
1372 ret = babeltrace_close_memstream(buf, &size, out);
1373 if (ret < 0) {
f824ae04
MD
1374 int closeret;
1375
f8370579 1376 perror("babeltrace_flush_memstream");
f824ae04
MD
1377 ret = -errno;
1378 closeret = fclose(in);
1379 if (closeret) {
1380 perror("Error in fclose");
1381 }
1382 return ret;
1383 }
1384 ret = fclose(in);
1385 if (ret) {
1386 perror("Error in fclose");
f8370579 1387 }
b4c19c1e 1388 /* open for reading */
abc40d24
MD
1389 buflen = strlen(*buf);
1390 if (!buflen) {
1391 *fp = NULL;
493330cb 1392 return -ENOENT;
abc40d24
MD
1393 }
1394 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
a569a564
MD
1395 if (!*fp) {
1396 perror("Metadata fmemopen");
1397 return -errno;
1398 }
b4c19c1e
MD
1399 return 0;
1400}
1401
65102a8c 1402static
0c880b0a
MD
1403int ctf_trace_metadata_read(struct ctf_trace *td, FILE *metadata_fp,
1404 struct ctf_scanner *scanner, int append)
65102a8c 1405{
2d0bea29 1406 struct ctf_file_stream *metadata_stream;
65102a8c 1407 FILE *fp;
b4c19c1e 1408 char *buf = NULL;
f824ae04 1409 int ret = 0, closeret;
65102a8c 1410
2d0bea29 1411 metadata_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 1412 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
b086c01a 1413
ae23d232
JD
1414 if (metadata_fp) {
1415 fp = metadata_fp;
bcbfb8bf 1416 metadata_stream->pos.fd = -1;
ae23d232
JD
1417 } else {
1418 td->metadata = &metadata_stream->parent;
1419 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1420 if (metadata_stream->pos.fd < 0) {
3394d22e 1421 fprintf(stderr, "Unable to open metadata.\n");
f824ae04
MD
1422 ret = -1;
1423 goto end_free;
ae23d232 1424 }
65102a8c 1425
ae23d232
JD
1426 fp = fdopen(metadata_stream->pos.fd, "r");
1427 if (!fp) {
3394d22e 1428 fprintf(stderr, "[error] Unable to open metadata stream.\n");
a569a564 1429 perror("Metadata stream open");
ae23d232
JD
1430 ret = -errno;
1431 goto end_stream;
1432 }
f824ae04
MD
1433 /* fd now belongs to fp */
1434 metadata_stream->pos.fd = -1;
ae23d232 1435 }
65102a8c
MD
1436 if (babeltrace_debug)
1437 yydebug = 1;
1438
b4c19c1e 1439 if (packet_metadata(td, fp)) {
0c880b0a 1440 ret = ctf_trace_metadata_stream_read(td, &fp, &buf);
abc40d24 1441 if (ret) {
6514b4af 1442 goto end;
abc40d24 1443 }
7237592a
MD
1444 td->metadata_string = buf;
1445 td->metadata_packetized = 1;
da75b0f7 1446 } else {
0c880b0a
MD
1447 if (!append) {
1448 unsigned int major, minor;
1449 ssize_t nr_items;
1450
1451 td->byte_order = BYTE_ORDER;
1452
1453 /* Check text-only metadata header and version */
1454 nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor);
1455 if (nr_items < 2)
1456 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1457 if (check_version(major, minor) < 0) {
1458 ret = -EINVAL;
1459 goto end;
1460 }
1461 rewind(fp);
5c262147 1462 }
b4c19c1e
MD
1463 }
1464
cb2f43ee 1465 ret = ctf_scanner_append_ast(scanner, fp);
65102a8c 1466 if (ret) {
3394d22e 1467 fprintf(stderr, "[error] Error creating AST\n");
65102a8c
MD
1468 goto end;
1469 }
1470
1471 if (babeltrace_debug) {
3394d22e 1472 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
65102a8c 1473 if (ret) {
3394d22e 1474 fprintf(stderr, "[error] Error visiting AST for XML output\n");
65102a8c
MD
1475 goto end;
1476 }
1477 }
1478
3394d22e 1479 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
65102a8c 1480 if (ret) {
3394d22e 1481 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
1482 goto end;
1483 }
3394d22e 1484 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
ac5c6ca0 1485 td, td->byte_order);
65102a8c 1486 if (ret) {
3394d22e 1487 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
1488 goto end;
1489 }
1490end:
f824ae04
MD
1491 if (fp) {
1492 closeret = fclose(fp);
1493 if (closeret) {
1494 perror("Error on fclose");
1495 }
1496 }
65102a8c 1497end_stream:
f824ae04
MD
1498 if (metadata_stream->pos.fd >= 0) {
1499 closeret = close(metadata_stream->pos.fd);
1500 if (closeret) {
1501 perror("Error on metadata stream fd close");
1502 }
1503 }
1504end_free:
2d0bea29
MD
1505 if (ret)
1506 g_free(metadata_stream);
0f980a35
MD
1507 return ret;
1508}
1509
e28d4618 1510static
c716f83b 1511struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
9e88d150 1512 struct ctf_stream_definition *stream,
4716614a 1513 struct ctf_event_declaration *event)
e28d4618 1514{
c716f83b 1515 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
e28d4618
MD
1516
1517 if (event->context_decl) {
0d69b916 1518 struct bt_definition *definition =
e28d4618
MD
1519 event->context_decl->p.definition_new(&event->context_decl->p,
1520 stream->parent_def_scope, 0, 0, "event.context");
1521 if (!definition) {
1522 goto error;
1523 }
42dc00b7 1524 stream_event->event_context = container_of(definition,
e28d4618 1525 struct definition_struct, p);
42dc00b7 1526 stream->parent_def_scope = stream_event->event_context->p.scope;
e28d4618
MD
1527 }
1528 if (event->fields_decl) {
0d69b916 1529 struct bt_definition *definition =
e28d4618
MD
1530 event->fields_decl->p.definition_new(&event->fields_decl->p,
1531 stream->parent_def_scope, 0, 0, "event.fields");
1532 if (!definition) {
1533 goto error;
1534 }
42dc00b7 1535 stream_event->event_fields = container_of(definition,
e28d4618 1536 struct definition_struct, p);
42dc00b7 1537 stream->parent_def_scope = stream_event->event_fields->p.scope;
e28d4618 1538 }
d3ded99d 1539 stream_event->stream = stream;
42dc00b7 1540 return stream_event;
e28d4618
MD
1541
1542error:
42dc00b7 1543 if (stream_event->event_fields)
13fad8b6 1544 bt_definition_unref(&stream_event->event_fields->p);
42dc00b7 1545 if (stream_event->event_context)
13fad8b6 1546 bt_definition_unref(&stream_event->event_context->p);
888ec52a
MD
1547 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1548 g_quark_to_string(event->name));
e28d4618
MD
1549 return NULL;
1550}
1551
0c880b0a
MD
1552static
1553int copy_event_declarations_stream_class_to_stream(struct ctf_trace *td,
1554 struct ctf_stream_declaration *stream_class,
1555 struct ctf_stream_definition *stream)
1556{
1557 size_t def_size, class_size, i;
1558 int ret = 0;
1559
1560 def_size = stream->events_by_id->len;
1561 class_size = stream_class->events_by_id->len;
1562
1563 g_ptr_array_set_size(stream->events_by_id, class_size);
1564 for (i = def_size; i < class_size; i++) {
1565 struct ctf_event_declaration *event =
1566 g_ptr_array_index(stream_class->events_by_id, i);
1567 struct ctf_event_definition *stream_event;
1568
1569 if (!event)
1570 continue;
1571 stream_event = create_event_definitions(td, stream, event);
1572 if (!stream_event) {
1573 ret = -EINVAL;
1574 goto error;
1575 }
1576 g_ptr_array_index(stream->events_by_id, i) = stream_event;
1577 }
1578error:
1579 return ret;
1580}
1581
e28d4618 1582static
9e88d150 1583int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618 1584{
f380e105 1585 struct ctf_stream_declaration *stream_class;
e28d4618
MD
1586 int ret;
1587 int i;
1588
1589 if (stream->stream_definitions_created)
1590 return 0;
1591
1592 stream_class = stream->stream_class;
1593
1594 if (stream_class->packet_context_decl) {
0d69b916 1595 struct bt_definition *definition =
e28d4618
MD
1596 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1597 stream->parent_def_scope, 0, 0, "stream.packet.context");
1598 if (!definition) {
1599 ret = -EINVAL;
1600 goto error;
1601 }
1602 stream->stream_packet_context = container_of(definition,
1603 struct definition_struct, p);
1604 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1605 }
1606 if (stream_class->event_header_decl) {
0d69b916 1607 struct bt_definition *definition =
e28d4618
MD
1608 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1609 stream->parent_def_scope, 0, 0, "stream.event.header");
1610 if (!definition) {
1611 ret = -EINVAL;
1612 goto error;
1613 }
1614 stream->stream_event_header =
1615 container_of(definition, struct definition_struct, p);
1616 stream->parent_def_scope = stream->stream_event_header->p.scope;
1617 }
1618 if (stream_class->event_context_decl) {
0d69b916 1619 struct bt_definition *definition =
e28d4618
MD
1620 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1621 stream->parent_def_scope, 0, 0, "stream.event.context");
1622 if (!definition) {
1623 ret = -EINVAL;
1624 goto error;
1625 }
1626 stream->stream_event_context =
1627 container_of(definition, struct definition_struct, p);
1628 stream->parent_def_scope = stream->stream_event_context->p.scope;
1629 }
1630 stream->events_by_id = g_ptr_array_new();
0c880b0a
MD
1631 ret = copy_event_declarations_stream_class_to_stream(td,
1632 stream_class, stream);
1633 if (ret)
1634 goto error_event;
e28d4618
MD
1635 return 0;
1636
1637error_event:
1638 for (i = 0; i < stream->events_by_id->len; i++) {
c716f83b 1639 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
42dc00b7
MD
1640 if (stream_event)
1641 g_free(stream_event);
e28d4618
MD
1642 }
1643 g_ptr_array_free(stream->events_by_id, TRUE);
1644error:
1645 if (stream->stream_event_context)
13fad8b6 1646 bt_definition_unref(&stream->stream_event_context->p);
e28d4618 1647 if (stream->stream_event_header)
13fad8b6 1648 bt_definition_unref(&stream->stream_event_header->p);
e28d4618 1649 if (stream->stream_packet_context)
13fad8b6 1650 bt_definition_unref(&stream->stream_packet_context->p);
888ec52a
MD
1651 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1652 stream_class->stream_id, strerror(-ret));
e28d4618
MD
1653 return ret;
1654}
1655
5bfcad93
MD
1656static
1657int stream_assign_class(struct ctf_trace *td,
1658 struct ctf_file_stream *file_stream,
1659 uint64_t stream_id)
1660{
1661 struct ctf_stream_declaration *stream;
1662 int ret;
1663
1664 file_stream->parent.stream_id = stream_id;
1665 if (stream_id >= td->streams->len) {
1666 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1667 return -EINVAL;
1668 }
1669 stream = g_ptr_array_index(td->streams, stream_id);
1670 if (!stream) {
1671 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1672 return -EINVAL;
1673 }
1674 file_stream->parent.stream_class = stream;
1675 ret = create_stream_definitions(td, &file_stream->parent);
1676 if (ret)
1677 return ret;
1678 return 0;
1679}
1680
0f980a35 1681static
ec323464
MD
1682int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1683 struct ctf_trace *td,
1684 struct ctf_file_stream *file_stream,
1685 size_t filesize)
0f980a35 1686{
ec323464 1687 struct packet_index packet_index;
ec323464 1688 uint64_t stream_id = 0;
653906a4 1689 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
ec323464 1690 int first_packet = 0;
653906a4 1691 int len_index;
0f980a35
MD
1692 int ret;
1693
ec323464 1694begin:
5f75b32d 1695 memset(&packet_index, 0, sizeof(packet_index));
ec323464
MD
1696 if (!pos->mmap_offset) {
1697 first_packet = 1;
1698 }
8895362d 1699
ec323464
MD
1700 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1701 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1702 }
0f980a35 1703
ec323464
MD
1704 if (pos->base_mma) {
1705 /* unmap old base */
1706 ret = munmap_align(pos->base_mma);
1707 if (ret) {
1708 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1709 strerror(errno));
1710 return ret;
0f980a35 1711 }
ec323464
MD
1712 pos->base_mma = NULL;
1713 }
1714 /* map new base. Need mapping length from header. */
1715 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1716 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1717 assert(pos->base_mma != MAP_FAILED);
1718 /*
1719 * Use current mapping size as temporary content and packet
1720 * size.
1721 */
1722 pos->content_size = packet_map_len;
1723 pos->packet_size = packet_map_len;
1724 pos->offset = 0; /* Position of the packet header */
1725
1726 packet_index.offset = pos->mmap_offset;
ec323464
MD
1727
1728 /* read and check header, set stream id (and check) */
1729 if (file_stream->parent.trace_packet_header) {
1730 /* Read packet header */
1731 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1732 if (ret) {
1733 if (ret == -EFAULT)
1734 goto retry;
888ec52a 1735 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
ec323464
MD
1736 return ret;
1737 }
1738 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1739 if (len_index >= 0) {
1740 struct bt_definition *field;
1741 uint64_t magic;
1742
1743 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1744 magic = bt_get_unsigned_int(field);
1745 if (magic != CTF_MAGIC) {
1746 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1747 magic,
992e8cc0 1748 file_stream->pos.packet_index->len,
ec323464
MD
1749 (ssize_t) pos->mmap_offset);
1750 return -EINVAL;
0f980a35 1751 }
ec323464 1752 }
0f980a35 1753
ec323464
MD
1754 /* check uuid */
1755 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1756 if (len_index >= 0) {
1757 struct definition_array *defarray;
1758 struct bt_definition *field;
1759 uint64_t i;
1760 uint8_t uuidval[BABELTRACE_UUID_LEN];
0f980a35 1761
ec323464
MD
1762 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1763 assert(field->declaration->id == CTF_TYPE_ARRAY);
1764 defarray = container_of(field, struct definition_array, p);
1765 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
0f980a35 1766
ec323464
MD
1767 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1768 struct bt_definition *elem;
0f980a35 1769
ec323464
MD
1770 elem = bt_array_index(defarray, i);
1771 uuidval[i] = bt_get_unsigned_int(elem);
0f980a35 1772 }
72a3bc16 1773 ret = bt_uuid_compare(td->uuid, uuidval);
ec323464
MD
1774 if (ret) {
1775 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1776 return -EINVAL;
1777 }
1778 }
0f980a35 1779
ec323464
MD
1780 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1781 if (len_index >= 0) {
1782 struct bt_definition *field;
0f980a35 1783
ec323464
MD
1784 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1785 stream_id = bt_get_unsigned_int(field);
0f980a35 1786 }
ec323464 1787 }
0f980a35 1788
ec323464
MD
1789 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1790 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1791 stream_id,
1792 file_stream->parent.stream_id);
1793 return -EINVAL;
1794 }
1795 if (first_packet) {
5bfcad93 1796 ret = stream_assign_class(td, file_stream, stream_id);
ec323464
MD
1797 if (ret)
1798 return ret;
1799 }
dc48ecad 1800
ec323464
MD
1801 if (file_stream->parent.stream_packet_context) {
1802 /* Read packet context */
1803 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1804 if (ret) {
1805 if (ret == -EFAULT)
1806 goto retry;
888ec52a 1807 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
ec323464
MD
1808 return ret;
1809 }
95b34f38
MD
1810 /* read packet size from header */
1811 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
ec323464
MD
1812 if (len_index >= 0) {
1813 struct bt_definition *field;
dc48ecad 1814
ec323464 1815 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
95b34f38 1816 packet_index.packet_size = bt_get_unsigned_int(field);
ec323464
MD
1817 } else {
1818 /* Use file size for packet size */
95b34f38 1819 packet_index.packet_size = filesize * CHAR_BIT;
ec323464 1820 }
75cc2c35 1821
95b34f38
MD
1822 /* read content size from header */
1823 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
ec323464
MD
1824 if (len_index >= 0) {
1825 struct bt_definition *field;
75cc2c35 1826
ec323464 1827 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
95b34f38 1828 packet_index.content_size = bt_get_unsigned_int(field);
ec323464 1829 } else {
95b34f38
MD
1830 /* Use packet size if non-zero, else file size */
1831 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
ec323464 1832 }
41e82e00 1833
ec323464
MD
1834 /* read timestamp begin from header */
1835 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1836 if (len_index >= 0) {
1837 struct bt_definition *field;
41e82e00 1838
ec323464 1839 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
992e8cc0 1840 packet_index.ts_cycles.timestamp_begin = bt_get_unsigned_int(field);
4c62e2d8 1841 if (file_stream->parent.stream_class->trace->parent.collection) {
992e8cc0 1842 packet_index.ts_real.timestamp_begin =
ec323464
MD
1843 ctf_get_real_timestamp(
1844 &file_stream->parent,
992e8cc0 1845 packet_index.ts_cycles.timestamp_begin);
41e82e00 1846 }
0f980a35 1847 }
546293fa 1848
ec323464
MD
1849 /* read timestamp end from header */
1850 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1851 if (len_index >= 0) {
1852 struct bt_definition *field;
1853
1854 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
992e8cc0 1855 packet_index.ts_cycles.timestamp_end = bt_get_unsigned_int(field);
4c62e2d8 1856 if (file_stream->parent.stream_class->trace->parent.collection) {
992e8cc0 1857 packet_index.ts_real.timestamp_end =
ec323464
MD
1858 ctf_get_real_timestamp(
1859 &file_stream->parent,
992e8cc0 1860 packet_index.ts_cycles.timestamp_end);
ec323464 1861 }
546293fa
MD
1862 }
1863
ec323464
MD
1864 /* read events discarded from header */
1865 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1866 if (len_index >= 0) {
1867 struct bt_definition *field;
1868
1869 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1870 packet_index.events_discarded = bt_get_unsigned_int(field);
1871 packet_index.events_discarded_len = bt_get_int_len(field);
546293fa 1872 }
2328c2cd
JD
1873
1874 /* read packet_seq_num from header */
1875 len_index = bt_struct_declaration_lookup_field_index(
1876 file_stream->parent.stream_packet_context->declaration,
1877 g_quark_from_static_string("packet_seq_num"));
1878 if (len_index >= 0) {
1879 struct bt_definition *field;
1880
1881 field = bt_struct_definition_get_field_from_index(
1882 file_stream->parent.stream_packet_context,
1883 len_index);
1884 packet_index.packet_seq_num = bt_get_unsigned_int(field);
1885 }
ec323464
MD
1886 } else {
1887 /* Use file size for packet size */
2d686891
MD
1888 packet_index.packet_size = filesize * CHAR_BIT;
1889 /* Use packet size if non-zero, else file size */
1890 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
ec323464
MD
1891 }
1892
1893 /* Validate content size and packet size values */
1894 if (packet_index.content_size > packet_index.packet_size) {
1895 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1896 packet_index.content_size, packet_index.packet_size);
1897 return -EINVAL;
1898 }
1899
1900 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1901 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1902 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1903 return -EINVAL;
1904 }
546293fa 1905
a7ac9efd
MD
1906 if (packet_index.content_size < pos->offset) {
1907 fprintf(stderr, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1908 return -EINVAL;
1909 }
1910
2d686891
MD
1911 if ((packet_index.packet_size >> LOG2_CHAR_BIT) == 0) {
1912 fprintf(stderr, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1913 return -EINVAL;
1914 }
1915
ec323464
MD
1916 /* Save position after header and context */
1917 packet_index.data_offset = pos->offset;
0f980a35 1918
ec323464 1919 /* add index to packet array */
992e8cc0 1920 g_array_append_val(file_stream->pos.packet_index, packet_index);
0f980a35 1921
ec323464
MD
1922 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1923
1924 return 0;
1925
1926 /* Retry with larger mapping */
1927retry:
1928 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1929 /*
1930 * Reached EOF, but still expecting header/context data.
1931 */
1932 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1933 return -EFAULT;
1934 }
1935 /* Double the mapping len, and retry */
1936 tmp_map_len = packet_map_len << 1;
1937 if (tmp_map_len >> 1 != packet_map_len) {
1938 /* Overflow */
888ec52a 1939 fprintf(stderr, "[error] Packet mapping length overflow\n");
ec323464 1940 return -EFAULT;
0f980a35 1941 }
ec323464
MD
1942 packet_map_len = tmp_map_len;
1943 goto begin;
1944}
1945
1946static
1947int create_stream_packet_index(struct ctf_trace *td,
1948 struct ctf_file_stream *file_stream)
1949{
1950 struct ctf_stream_pos *pos;
1951 struct stat filestats;
1952 int ret;
1953
1954 pos = &file_stream->pos;
0f980a35 1955
ec323464
MD
1956 ret = fstat(pos->fd, &filestats);
1957 if (ret < 0)
1958 return ret;
1959
5bfcad93
MD
1960 /* Deal with empty files */
1961 if (!filestats.st_size) {
1962 if (file_stream->parent.trace_packet_header
1963 || file_stream->parent.stream_packet_context) {
1964 /*
1965 * We expect a trace packet header and/or stream packet
1966 * context. Since a trace needs to have at least one
1967 * packet, empty files are therefore not accepted.
1968 */
1969 fprintf(stderr, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1970 return -EINVAL;
1971 } else {
1972 /*
1973 * Without trace packet header nor stream packet
1974 * context, a one-packet trace can indeed be empty. This
1975 * is only valid if there is only one stream class: 0.
1976 */
1977 ret = stream_assign_class(td, file_stream, 0);
1978 if (ret)
1979 return ret;
1980 return 0;
1981 }
1982 }
1983
ec323464
MD
1984 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1985 ret = create_stream_one_packet_index(pos, td, file_stream,
1986 filestats.st_size);
1987 if (ret)
1988 return ret;
1989 }
0f980a35
MD
1990 return 0;
1991}
1992
e28d4618 1993static
9e88d150 1994int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618
MD
1995{
1996 int ret;
1997
1998 if (td->packet_header_decl) {
0d69b916 1999 struct bt_definition *definition =
e28d4618
MD
2000 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
2001 stream->parent_def_scope, 0, 0, "trace.packet.header");
2002 if (!definition) {
2003 ret = -EINVAL;
2004 goto error;
2005 }
2006 stream->trace_packet_header =
2007 container_of(definition, struct definition_struct, p);
2008 stream->parent_def_scope = stream->trace_packet_header->p.scope;
2009 }
2010
2011 return 0;
2012
2013error:
888ec52a 2014 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
e28d4618
MD
2015 return ret;
2016}
2017
0ace7505
JD
2018static
2019int import_stream_packet_index(struct ctf_trace *td,
2020 struct ctf_file_stream *file_stream)
2021{
0ace7505 2022 struct ctf_stream_pos *pos;
3ecd366e 2023 struct ctf_packet_index *ctf_index = NULL;
0ace7505
JD
2024 struct ctf_packet_index_file_hdr index_hdr;
2025 struct packet_index index;
138cb20b 2026 uint32_t packet_index_len, index_minor;
0ace7505
JD
2027 int ret = 0;
2028 int first_packet = 1;
2029 size_t len;
2030
2031 pos = &file_stream->pos;
2032
2033 len = fread(&index_hdr, sizeof(index_hdr), 1, pos->index_fp);
2034 if (len != 1) {
2035 perror("read index file header");
2036 goto error;
2037 }
2038
2039 /* Check the index header */
2040 if (be32toh(index_hdr.magic) != CTF_INDEX_MAGIC) {
2041 fprintf(stderr, "[error] wrong index magic\n");
2042 ret = -1;
2043 goto error;
2044 }
2045 if (be32toh(index_hdr.index_major) != CTF_INDEX_MAJOR) {
f84c13d9
MJ
2046 fprintf(stderr, "[error] Incompatible index file %" PRIu32
2047 ".%" PRIu32 ", supported %d.%d\n",
2048 be32toh(index_hdr.index_major),
2049 be32toh(index_hdr.index_minor), CTF_INDEX_MAJOR,
0ace7505
JD
2050 CTF_INDEX_MINOR);
2051 ret = -1;
2052 goto error;
2053 }
138cb20b
JD
2054 index_minor = be32toh(index_hdr.index_minor);
2055
e83ce12a
JD
2056 packet_index_len = be32toh(index_hdr.packet_index_len);
2057 if (packet_index_len == 0) {
a74d9cb2
MD
2058 fprintf(stderr, "[error] Packet index length cannot be 0.\n");
2059 ret = -1;
2060 goto error;
2061 }
3ecd366e
MD
2062 /*
2063 * Allocate the index length found in header, not internal
2064 * representation.
2065 */
e83ce12a
JD
2066 ctf_index = g_malloc0(packet_index_len);
2067 while (fread(ctf_index, packet_index_len, 1,
ad40ac1a 2068 pos->index_fp) == 1) {
0ace7505 2069 uint64_t stream_id;
51e0087f 2070 struct ctf_stream_declaration *stream = NULL;
0ace7505
JD
2071
2072 memset(&index, 0, sizeof(index));
3ecd366e
MD
2073 index.offset = be64toh(ctf_index->offset);
2074 index.packet_size = be64toh(ctf_index->packet_size);
2075 index.content_size = be64toh(ctf_index->content_size);
2076 index.ts_cycles.timestamp_begin = be64toh(ctf_index->timestamp_begin);
2077 index.ts_cycles.timestamp_end = be64toh(ctf_index->timestamp_end);
2078 index.events_discarded = be64toh(ctf_index->events_discarded);
0ace7505 2079 index.events_discarded_len = 64;
e69dd258 2080 index.data_offset = -1;
3ecd366e 2081 stream_id = be64toh(ctf_index->stream_id);
138cb20b
JD
2082 if (index_minor >= 1) {
2083 index.stream_instance_id = be64toh(ctf_index->stream_instance_id);
2084 index.packet_seq_num = be64toh(ctf_index->packet_seq_num);
2085 }
0ace7505
JD
2086
2087 if (!first_packet) {
2088 /* add index to packet array */
992e8cc0 2089 g_array_append_val(file_stream->pos.packet_index, index);
0ace7505
JD
2090 continue;
2091 }
2092
2093 file_stream->parent.stream_id = stream_id;
51e0087f
JG
2094 if (stream_id < td->streams->len) {
2095 stream = g_ptr_array_index(td->streams, stream_id);
2096 }
0ace7505
JD
2097 if (!stream) {
2098 fprintf(stderr, "[error] Stream %" PRIu64
2099 " is not declared in metadata.\n",
2100 stream_id);
2101 ret = -EINVAL;
2102 goto error;
2103 }
2104 file_stream->parent.stream_class = stream;
2105 ret = create_stream_definitions(td, &file_stream->parent);
2106 if (ret)
2107 goto error;
2108 first_packet = 0;
2109 /* add index to packet array */
992e8cc0 2110 g_array_append_val(file_stream->pos.packet_index, index);
0ace7505
JD
2111 }
2112
dae407df
JD
2113 /* Index containing only the header. */
2114 if (!file_stream->parent.stream_class) {
2115 ret = -1;
2116 goto error;
2117 }
2118
0ace7505
JD
2119 ret = 0;
2120
2121error:
3ecd366e 2122 g_free(ctf_index);
0ace7505
JD
2123 return ret;
2124}
2125
0f980a35
MD
2126/*
2127 * Note: many file streams can inherit from the same stream class
2128 * description (metadata).
2129 */
2130static
b086c01a 2131int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1cf393f6 2132 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
b086c01a 2133 int whence))
0f980a35 2134{
f824ae04 2135 int ret, fd, closeret;
0f980a35 2136 struct ctf_file_stream *file_stream;
ff075710 2137 struct stat statbuf;
0ace7505 2138 char *index_name;
0f980a35 2139
ff075710
MD
2140 fd = openat(td->dirfd, path, flags);
2141 if (fd < 0) {
a569a564 2142 perror("File stream openat()");
ff075710 2143 ret = fd;
0f980a35 2144 goto error;
a569a564 2145 }
ff075710
MD
2146
2147 /* Don't try to mmap subdirectories. Skip them, return success. */
2148 ret = fstat(fd, &statbuf);
2149 if (ret) {
2150 perror("File stream fstat()");
2151 goto fstat_error;
2152 }
2153 if (S_ISDIR(statbuf.st_mode)) {
0ace7505
JD
2154 if (strncmp(path, "index", 5) != 0) {
2155 fprintf(stderr, "[warning] Skipping directory '%s' "
2156 "found in trace\n", path);
2157 }
ff075710
MD
2158 ret = 0;
2159 goto fd_is_dir_ok;
2160 }
f4f0fb70
MD
2161 if (!statbuf.st_size) {
2162 /** Skip empty files. */
2163 ret = 0;
2164 goto fd_is_empty_file;
2165 }
ff075710 2166
0f980a35 2167 file_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 2168 file_stream->pos.last_offset = LAST_OFFSET_POISON;
0ace7505
JD
2169 file_stream->pos.fd = -1;
2170 file_stream->pos.index_fp = NULL;
b086c01a 2171
87148dc7
MD
2172 strncpy(file_stream->parent.path, path, PATH_MAX);
2173 file_stream->parent.path[PATH_MAX - 1] = '\0';
2174
06789ffd
MD
2175 if (packet_seek) {
2176 file_stream->pos.packet_seek = packet_seek;
b086c01a 2177 } else {
06789ffd 2178 fprintf(stderr, "[error] packet_seek function undefined.\n");
b086c01a
JD
2179 ret = -1;
2180 goto error_def;
2181 }
2182
ca334c72 2183 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
f824ae04
MD
2184 if (ret)
2185 goto error_def;
2d0bea29 2186 ret = create_trace_definitions(td, &file_stream->parent);
e28d4618
MD
2187 if (ret)
2188 goto error_def;
25ccc85b 2189 /*
50052405 2190 * For now, only a single clock per trace is supported.
25ccc85b 2191 */
7ec78969 2192 file_stream->parent.current_clock = td->parent.single_clock;
0ace7505
JD
2193
2194 /*
2195 * Allocate the index name for this stream and try to open it.
2196 */
2197 index_name = malloc((strlen(path) + sizeof(INDEX_PATH)) * sizeof(char));
2198 if (!index_name) {
2199 fprintf(stderr, "[error] Cannot allocate index filename\n");
f4f0fb70 2200 ret = -ENOMEM;
0ace7505 2201 goto error_def;
888ec52a 2202 }
0ace7505
JD
2203 snprintf(index_name, strlen(path) + sizeof(INDEX_PATH),
2204 INDEX_PATH, path);
2205
ff48c2b0 2206 if (bt_faccessat(td->dirfd, td->parent.path, index_name, O_RDONLY, 0) < 0) {
0ace7505
JD
2207 ret = create_stream_packet_index(td, file_stream);
2208 if (ret) {
2209 fprintf(stderr, "[error] Stream index creation error.\n");
2210 goto error_index;
2211 }
2212 } else {
2213 ret = openat(td->dirfd, index_name, flags);
2214 if (ret < 0) {
2215 perror("Index file openat()");
2216 ret = -1;
2217 goto error_free;
2218 }
2219 file_stream->pos.index_fp = fdopen(ret, "r");
2f0c6a52
MD
2220 if (!file_stream->pos.index_fp) {
2221 perror("fdopen() error");
2222 goto error_free;
2223 }
0ace7505
JD
2224 ret = import_stream_packet_index(td, file_stream);
2225 if (ret) {
2226 ret = -1;
2227 goto error_index;
2228 }
2229 ret = fclose(file_stream->pos.index_fp);
2230 if (ret < 0) {
2231 perror("close index");
2232 goto error_free;
2233 }
2234 }
2235 free(index_name);
2236
0f980a35 2237 /* Add stream file to stream class */
2d0bea29
MD
2238 g_ptr_array_add(file_stream->parent.stream_class->streams,
2239 &file_stream->parent);
0f980a35
MD
2240 return 0;
2241
2242error_index:
0ace7505
JD
2243 if (file_stream->pos.index_fp) {
2244 ret = fclose(file_stream->pos.index_fp);
2245 if (ret < 0) {
2246 perror("close index");
2247 }
2248 }
2d0bea29 2249 if (file_stream->parent.trace_packet_header)
13fad8b6 2250 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
0ace7505
JD
2251error_free:
2252 free(index_name);
e28d4618 2253error_def:
f824ae04
MD
2254 closeret = ctf_fini_pos(&file_stream->pos);
2255 if (closeret) {
2256 fprintf(stderr, "Error on ctf_fini_pos\n");
2257 }
0f980a35 2258 g_free(file_stream);
f4f0fb70 2259fd_is_empty_file:
ff075710
MD
2260fd_is_dir_ok:
2261fstat_error:
f824ae04
MD
2262 closeret = close(fd);
2263 if (closeret) {
2264 perror("Error on fd close");
2265 }
0f980a35 2266error:
65102a8c
MD
2267 return ret;
2268}
2269
bbefb8dd 2270static
5b80ddfb 2271int ctf_open_trace_read(struct ctf_trace *td,
8c250d87 2272 const char *path, int flags,
1cf393f6 2273 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 2274 int whence), FILE *metadata_fp)
bbefb8dd 2275{
6514b4af 2276 struct ctf_scanner *scanner;
f824ae04 2277 int ret, closeret;
65102a8c
MD
2278 struct dirent *dirent;
2279 struct dirent *diriter;
2280 size_t dirent_len;
0ace7505 2281 char *ext;
bbefb8dd 2282
46322b33 2283 td->flags = flags;
bbefb8dd
MD
2284
2285 /* Open trace directory */
46322b33
MD
2286 td->dir = opendir(path);
2287 if (!td->dir) {
6a6b384c 2288 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
bbefb8dd
MD
2289 ret = -ENOENT;
2290 goto error;
2291 }
2292
46322b33
MD
2293 td->dirfd = open(path, 0);
2294 if (td->dirfd < 0) {
6a6b384c 2295 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
a569a564
MD
2296 perror("Trace directory open");
2297 ret = -errno;
65102a8c
MD
2298 goto error_dirfd;
2299 }
caf929fa
MD
2300 strncpy(td->parent.path, path, sizeof(td->parent.path));
2301 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
0f980a35 2302
65102a8c
MD
2303 /*
2304 * Keep the metadata file separate.
0c880b0a
MD
2305 * Keep scanner object local to the open. We don't support
2306 * incremental metadata append for on-disk traces.
65102a8c 2307 */
6514b4af
MD
2308 scanner = ctf_scanner_alloc();
2309 if (!scanner) {
2310 fprintf(stderr, "[error] Error allocating scanner\n");
2311 ret = -ENOMEM;
2312 goto error_metadata;
2313 }
0c880b0a 2314 ret = ctf_trace_metadata_read(td, metadata_fp, scanner, 0);
6514b4af 2315 ctf_scanner_free(scanner);
65102a8c 2316 if (ret) {
251fc08c
MD
2317 if (ret == -ENOENT) {
2318 fprintf(stderr, "[warning] Empty metadata.\n");
2319 }
6a6b384c 2320 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
65102a8c
MD
2321 goto error_metadata;
2322 }
bbefb8dd
MD
2323
2324 /*
2325 * Open each stream: for each file, try to open, check magic
2326 * number, and get the stream ID to add to the right location in
2327 * the stream array.
bbefb8dd
MD
2328 */
2329
65102a8c 2330 dirent_len = offsetof(struct dirent, d_name) +
46322b33 2331 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 2332
65102a8c 2333 dirent = malloc(dirent_len);
bbefb8dd 2334
65102a8c 2335 for (;;) {
46322b33 2336 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 2337 if (ret) {
3394d22e 2338 fprintf(stderr, "[error] Readdir error.\n");
65102a8c 2339 goto readdir_error;
65102a8c
MD
2340 }
2341 if (!diriter)
2342 break;
d8ea2d29
MD
2343 /* Ignore hidden files, ., .. and metadata. */
2344 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
2345 || !strcmp(diriter->d_name, "..")
2346 || !strcmp(diriter->d_name, "metadata"))
2347 continue;
0ace7505
JD
2348
2349 /* Ignore index files : *.idx */
2350 ext = strrchr(diriter->d_name, '.');
2351 if (ext && (!strcmp(ext, ".idx"))) {
2352 continue;
2353 }
2354
06789ffd
MD
2355 ret = ctf_open_file_stream_read(td, diriter->d_name,
2356 flags, packet_seek);
dc48ecad 2357 if (ret) {
3394d22e 2358 fprintf(stderr, "[error] Open file stream error.\n");
dc48ecad
MD
2359 goto readdir_error;
2360 }
65102a8c 2361 }
bbefb8dd 2362
65102a8c 2363 free(dirent);
bbefb8dd 2364 return 0;
65102a8c
MD
2365
2366readdir_error:
2367 free(dirent);
2368error_metadata:
f824ae04
MD
2369 closeret = close(td->dirfd);
2370 if (closeret) {
2371 perror("Error on fd close");
2372 }
65102a8c 2373error_dirfd:
f824ae04
MD
2374 closeret = closedir(td->dir);
2375 if (closeret) {
2376 perror("Error on closedir");
2377 }
bbefb8dd
MD
2378error:
2379 return ret;
2380}
2381
03798a93
JD
2382/*
2383 * ctf_open_trace: Open a CTF trace and index it.
2384 * Note that the user must seek the trace after the open (using the iterator)
2385 * since the index creation read it entirely.
2386 */
e9378815 2387static
1b8455b7 2388struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1cf393f6 2389 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 2390 int whence), FILE *metadata_fp)
bbefb8dd 2391{
46322b33 2392 struct ctf_trace *td;
bbefb8dd
MD
2393 int ret;
2394
2715de36
MD
2395 /*
2396 * If packet_seek is NULL, we provide our default version.
2397 */
2398 if (!packet_seek)
2399 packet_seek = ctf_packet_seek;
2400
46322b33 2401 td = g_new0(struct ctf_trace, 1);
837b0013
JG
2402 if (!td) {
2403 goto error;
2404 }
2405 init_trace_descriptor(&td->parent);
bbefb8dd 2406
8c572eba 2407 switch (flags & O_ACCMODE) {
bbefb8dd 2408 case O_RDONLY:
b61922b5 2409 if (!path) {
3394d22e 2410 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
b61922b5
MD
2411 goto error;
2412 }
06789ffd 2413 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
bbefb8dd
MD
2414 if (ret)
2415 goto error;
2416 break;
989c73bc 2417 case O_RDWR:
3394d22e 2418 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
46322b33 2419 goto error;
bbefb8dd 2420 default:
3394d22e 2421 fprintf(stderr, "[error] Incorrect open flags.\n");
bbefb8dd
MD
2422 goto error;
2423 }
2424
b5a8598f
AB
2425 ret = trace_debug_info_create(td);
2426 if (ret) {
2427 goto error;
2428 }
2429
46322b33 2430 return &td->parent;
bbefb8dd 2431error:
80715793
MJ
2432 if (td) {
2433 trace_debug_info_destroy(td);
2434 g_free(td);
2435 }
bbefb8dd
MD
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.218217 seconds and 4 git commands to generate.