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