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