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