Refuse 0 integer size
[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
JD
474
475 if (pos->content_size == 0) {
476 /* Stream is inactive for now (live reading). */
477 return EAGAIN;
478 }
a5260047
JD
479 /* Packet only contains headers */
480 if (pos->offset == pos->content_size)
481 return EAGAIN;
482
5f643ed7 483 assert(pos->offset < pos->content_size);
31262354
MD
484
485 /* Read event header */
7f3f572b 486 if (likely(stream->stream_event_header)) {
a35173fe 487 struct definition_integer *integer_definition;
0d69b916 488 struct bt_definition *variant;
a35173fe 489
e28d4618 490 ret = generic_rw(ppos, &stream->stream_event_header->p);
7f3f572b 491 if (unlikely(ret))
31262354
MD
492 goto error;
493 /* lookup event id */
bf78e2cf 494 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
495 if (integer_definition) {
496 id = integer_definition->value._unsigned;
497 } else {
498 struct definition_enum *enum_definition;
499
9e3274b0 500 enum_definition = bt_lookup_enum(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
501 if (enum_definition) {
502 id = enum_definition->integer->value._unsigned;
503 }
31262354 504 }
764af3f4 505
ebae302b 506 variant = bt_lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e 507 if (variant) {
bf78e2cf 508 integer_definition = bt_lookup_integer(variant, "id", FALSE);
ccdb988e
MD
509 if (integer_definition) {
510 id = integer_definition->value._unsigned;
511 }
512 }
c87a8eb2 513 stream->event_id = id;
ccdb988e 514
764af3f4 515 /* lookup timestamp */
5e2eb0ae 516 stream->has_timestamp = 0;
bf78e2cf 517 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
a35173fe 518 if (integer_definition) {
2b9a764d 519 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 520 stream->has_timestamp = 1;
a35173fe 521 } else {
ccdb988e 522 if (variant) {
bf78e2cf 523 integer_definition = bt_lookup_integer(variant, "timestamp", FALSE);
a35173fe 524 if (integer_definition) {
2b9a764d 525 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 526 stream->has_timestamp = 1;
a35173fe
MD
527 }
528 }
764af3f4 529 }
31262354
MD
530 }
531
532 /* Read stream-declared event context */
e28d4618
MD
533 if (stream->stream_event_context) {
534 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
535 if (ret)
536 goto error;
537 }
538
7f3f572b 539 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 540 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
541 return -EINVAL;
542 }
e28d4618 543 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 544 if (unlikely(!event)) {
3394d22e 545 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
546 return -EINVAL;
547 }
548
549 /* Read event-declared event context */
e28d4618
MD
550 if (event->event_context) {
551 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
552 if (ret)
553 goto error;
554 }
555
556 /* Read event payload */
7f3f572b 557 if (likely(event->event_fields)) {
e28d4618 558 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
559 if (ret)
560 goto error;
561 }
562
28f35f0a
MD
563 if (pos->last_offset == pos->offset) {
564 fprintf(stderr, "[error] Invalid 0 byte event encountered.\n");
565 return -EINVAL;
566 }
567
31262354
MD
568 return 0;
569
570error:
64f5abe1 571 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
572 return ret;
573}
574
575static
1cf393f6 576int ctf_write_event(struct bt_stream_pos *pos, struct ctf_stream_definition *stream)
31262354 577{
f380e105 578 struct ctf_stream_declaration *stream_class = stream->stream_class;
c716f83b 579 struct ctf_event_definition *event;
c87a8eb2 580 uint64_t id;
31262354
MD
581 int ret;
582
c87a8eb2
MD
583 id = stream->event_id;
584
31262354 585 /* print event header */
7f3f572b 586 if (likely(stream->stream_event_header)) {
e28d4618 587 ret = generic_rw(pos, &stream->stream_event_header->p);
31262354
MD
588 if (ret)
589 goto error;
590 }
591
592 /* print stream-declared event context */
e28d4618
MD
593 if (stream->stream_event_context) {
594 ret = generic_rw(pos, &stream->stream_event_context->p);
31262354
MD
595 if (ret)
596 goto error;
597 }
598
7f3f572b 599 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 600 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
601 return -EINVAL;
602 }
e28d4618 603 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 604 if (unlikely(!event)) {
3394d22e 605 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
606 return -EINVAL;
607 }
608
609 /* print event-declared event context */
e28d4618
MD
610 if (event->event_context) {
611 ret = generic_rw(pos, &event->event_context->p);
31262354
MD
612 if (ret)
613 goto error;
614 }
615
616 /* Read and print event payload */
7f3f572b 617 if (likely(event->event_fields)) {
e28d4618 618 ret = generic_rw(pos, &event->event_fields->p);
31262354
MD
619 if (ret)
620 goto error;
621 }
622
623 return 0;
624
625error:
3394d22e 626 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
627 return ret;
628}
629
e69dd258
JD
630/*
631 * One side-effect of this function is to unmap pos mmap base if one is
632 * mapped.
633 */
634static
635int find_data_offset(struct ctf_stream_pos *pos,
636 struct ctf_file_stream *file_stream,
637 struct packet_index *packet_index)
638{
639 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
640 struct stat filestats;
641 size_t filesize;
642 int ret;
643
644 pos = &file_stream->pos;
645
646 ret = fstat(pos->fd, &filestats);
647 if (ret < 0)
648 return ret;
649 filesize = filestats.st_size;
650
651 /* Deal with empty files */
652 if (!filesize) {
653 return 0;
654 }
655
656begin:
657 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
658 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
659 }
660
661 if (pos->base_mma) {
662 /* unmap old base */
663 ret = munmap_align(pos->base_mma);
664 if (ret) {
665 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
666 strerror(errno));
667 return ret;
668 }
669 pos->base_mma = NULL;
670 }
671 /* map new base. Need mapping length from header. */
672 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
673 MAP_PRIVATE, pos->fd, pos->mmap_offset);
674 assert(pos->base_mma != MAP_FAILED);
675
676 pos->content_size = packet_map_len;
677 pos->packet_size = packet_map_len;
678 pos->offset = 0; /* Position of the packet header */
679
680 /* update trace_packet_header and stream_packet_context */
681 if (pos->prot == PROT_READ && file_stream->parent.trace_packet_header) {
682 /* Read packet header */
683 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
684 if (ret) {
685 if (ret == -EFAULT)
686 goto retry;
687 }
688 }
689 if (pos->prot == PROT_READ && file_stream->parent.stream_packet_context) {
690 /* Read packet context */
691 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
692 if (ret) {
693 if (ret == -EFAULT)
694 goto retry;
695 }
696 }
697 packet_index->data_offset = pos->offset;
698
699 /* unmap old base */
700 ret = munmap_align(pos->base_mma);
701 if (ret) {
702 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
703 strerror(errno));
704 return ret;
705 }
706 pos->base_mma = NULL;
707
708 return 0;
709
710 /* Retry with larger mapping */
711retry:
712 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
713 /*
714 * Reached EOF, but still expecting header/context data.
715 */
716 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
717 return -EFAULT;
718 }
719 /* Double the mapping len, and retry */
720 tmp_map_len = packet_map_len << 1;
721 if (tmp_map_len >> 1 != packet_map_len) {
722 /* Overflow */
723 fprintf(stderr, "[error] Packet mapping length overflow\n");
724 return -EFAULT;
725 }
726 packet_map_len = tmp_map_len;
727 goto begin;
728}
729
730
ca334c72
MD
731int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
732 int fd, int open_flags)
8c572eba
MD
733{
734 pos->fd = fd;
37fcdd19 735 if (fd >= 0) {
03798a93 736 pos->packet_cycles_index = g_array_new(FALSE, TRUE,
8563e754 737 sizeof(struct packet_index));
37fcdd19
JD
738 pos->packet_real_index = g_array_new(FALSE, TRUE,
739 sizeof(struct packet_index));
740 } else {
03798a93 741 pos->packet_cycles_index = NULL;
37fcdd19
JD
742 pos->packet_real_index = NULL;
743 }
8563e754
MD
744 switch (open_flags & O_ACCMODE) {
745 case O_RDONLY:
746 pos->prot = PROT_READ;
747 pos->flags = MAP_PRIVATE;
748 pos->parent.rw_table = read_dispatch_table;
31262354 749 pos->parent.event_cb = ctf_read_event;
ca334c72 750 pos->parent.trace = trace;
8563e754 751 break;
8563e754
MD
752 case O_RDWR:
753 pos->prot = PROT_WRITE; /* Write has priority */
754 pos->flags = MAP_SHARED;
755 pos->parent.rw_table = write_dispatch_table;
31262354 756 pos->parent.event_cb = ctf_write_event;
ca334c72 757 pos->parent.trace = trace;
8563e754 758 if (fd >= 0)
06789ffd 759 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
8563e754
MD
760 break;
761 default:
762 assert(0);
8c572eba 763 }
f824ae04 764 return 0;
8c572eba
MD
765}
766
f824ae04 767int ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba 768{
8c572eba
MD
769 if (pos->prot == PROT_WRITE && pos->content_size_loc)
770 *pos->content_size_loc = pos->offset;
aee35fcc 771 if (pos->base_mma) {
08c82b90
MD
772 int ret;
773
8c572eba 774 /* unmap old base */
aee35fcc 775 ret = munmap_align(pos->base_mma);
8c572eba 776 if (ret) {
3394d22e 777 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
8c572eba 778 strerror(errno));
f824ae04 779 return -1;
8c572eba
MD
780 }
781 }
37fcdd19
JD
782 if (pos->packet_cycles_index)
783 (void) g_array_free(pos->packet_cycles_index, TRUE);
784 if (pos->packet_real_index)
785 (void) g_array_free(pos->packet_real_index, TRUE);
f824ae04 786 return 0;
8c572eba
MD
787}
788
20d0dcf9
MD
789/*
790 * for SEEK_CUR: go to next packet.
34b8fff6 791 * for SEEK_SET: go to packet numer (index).
20d0dcf9 792 */
1cf393f6 793void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
0f980a35 794{
d6425aaf
MD
795 struct ctf_stream_pos *pos =
796 container_of(stream_pos, struct ctf_stream_pos, parent);
75cc2c35
MD
797 struct ctf_file_stream *file_stream =
798 container_of(pos, struct ctf_file_stream, pos);
0f980a35 799 int ret;
8c572eba 800 off_t off;
20d0dcf9 801 struct packet_index *packet_index;
0f980a35 802
5bfcad93
MD
803 switch (whence) {
804 case SEEK_CUR:
805 case SEEK_SET: /* Fall-through */
806 break; /* OK */
807 default:
808 assert(0);
809 }
810
8c572eba
MD
811 if (pos->prot == PROT_WRITE && pos->content_size_loc)
812 *pos->content_size_loc = pos->offset;
0f980a35 813
aee35fcc 814 if (pos->base_mma) {
0f980a35 815 /* unmap old base */
aee35fcc 816 ret = munmap_align(pos->base_mma);
0f980a35 817 if (ret) {
3394d22e 818 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
819 strerror(errno));
820 assert(0);
821 }
aee35fcc 822 pos->base_mma = NULL;
0f980a35
MD
823 }
824
8c572eba 825 /*
46322b33 826 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
827 * except to get exactly at the beginning of the next packet.
828 */
829 if (pos->prot == PROT_WRITE) {
989c73bc
MD
830 switch (whence) {
831 case SEEK_CUR:
832 /* The writer will add padding */
e0a5b455 833 pos->mmap_offset += pos->packet_size / CHAR_BIT;
989c73bc
MD
834 break;
835 case SEEK_SET:
20d0dcf9 836 assert(index == 0); /* only seek supported for now */
989c73bc
MD
837 pos->cur_index = 0;
838 break;
839 default:
840 assert(0);
841 }
8c572eba
MD
842 pos->content_size = -1U; /* Unknown at this point */
843 pos->packet_size = WRITE_PACKET_LEN;
989c73bc
MD
844 off = posix_fallocate(pos->fd, pos->mmap_offset,
845 pos->packet_size / CHAR_BIT);
8c572eba 846 assert(off >= 0);
847bf71a 847 pos->offset = 0;
8c572eba 848 } else {
5f643ed7 849 read_next_packet:
847bf71a
MD
850 switch (whence) {
851 case SEEK_CUR:
41e82e00 852 {
4c4ba021 853 uint64_t events_discarded_diff;
41e82e00 854
7d97fad9
MD
855 if (pos->offset == EOF) {
856 return;
857 }
c309df1c
MD
858 assert(pos->cur_index < pos->packet_cycles_index->len);
859 assert(pos->cur_index < pos->packet_real_index->len);
860
3217ac37 861 /* For printing discarded event count */
03798a93 862 packet_index = &g_array_index(pos->packet_cycles_index,
41e82e00 863 struct packet_index, pos->cur_index);
03798a93
JD
864 file_stream->parent.prev_cycles_timestamp_end =
865 packet_index->timestamp_end;
866 file_stream->parent.prev_cycles_timestamp =
867 packet_index->timestamp_begin;
868
869 packet_index = &g_array_index(pos->packet_real_index,
870 struct packet_index, pos->cur_index);
871 file_stream->parent.prev_real_timestamp_end =
20d0dcf9 872 packet_index->timestamp_end;
03798a93
JD
873 file_stream->parent.prev_real_timestamp =
874 packet_index->timestamp_begin;
875
876 events_discarded_diff = packet_index->events_discarded;
41e82e00 877 if (pos->cur_index > 0) {
03798a93 878 packet_index = &g_array_index(pos->packet_real_index,
41e82e00
MD
879 struct packet_index,
880 pos->cur_index - 1);
20d0dcf9 881 events_discarded_diff -= packet_index->events_discarded;
4c4ba021
MD
882 /*
883 * Deal with 32-bit wrap-around if the
884 * tracer provided a 32-bit field.
885 */
886 if (packet_index->events_discarded_len == 32) {
887 events_discarded_diff = (uint32_t) events_discarded_diff;
888 }
41e82e00 889 }
fca04958 890 file_stream->parent.events_discarded = events_discarded_diff;
03798a93
JD
891 file_stream->parent.prev_real_timestamp = file_stream->parent.real_timestamp;
892 file_stream->parent.prev_cycles_timestamp = file_stream->parent.cycles_timestamp;
847bf71a 893 /* The reader will expect us to skip padding */
8c572eba 894 ++pos->cur_index;
847bf71a 895 break;
41e82e00 896 }
847bf71a 897 case SEEK_SET:
c309df1c
MD
898 if (index >= pos->packet_cycles_index->len) {
899 pos->offset = EOF;
900 return;
901 }
f60efc0e
JD
902 packet_index = &g_array_index(pos->packet_cycles_index,
903 struct packet_index, index);
904 pos->last_events_discarded = packet_index->events_discarded;
20d0dcf9 905 pos->cur_index = index;
03798a93
JD
906 file_stream->parent.prev_real_timestamp = 0;
907 file_stream->parent.prev_real_timestamp_end = 0;
908 file_stream->parent.prev_cycles_timestamp = 0;
909 file_stream->parent.prev_cycles_timestamp_end = 0;
847bf71a
MD
910 break;
911 default:
912 assert(0);
913 }
03798a93 914 if (pos->cur_index >= pos->packet_real_index->len) {
7d97fad9 915 /*
87148dc7
MD
916 * We need to check if we are in trace read or
917 * called from packet indexing. In this last
918 * case, the collection is not there, so we
919 * cannot print the timestamps.
7d97fad9 920 */
4c62e2d8 921 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
badea1a2 922 /*
87148dc7
MD
923 * When a stream reaches the end of the
924 * file, we need to show the number of
925 * events discarded ourselves, because
926 * there is no next event scheduled to
927 * be printed in the output.
badea1a2 928 */
87148dc7 929 if (file_stream->parent.events_discarded) {
badea1a2 930 fflush(stdout);
c829a65c
MD
931 ctf_print_discarded(stderr,
932 &file_stream->parent,
933 1);
87148dc7 934 file_stream->parent.events_discarded = 0;
badea1a2 935 }
7d97fad9 936 }
670977d3 937 pos->offset = EOF;
847bf71a
MD
938 return;
939 }
03798a93
JD
940 packet_index = &g_array_index(pos->packet_cycles_index,
941 struct packet_index,
942 pos->cur_index);
943 file_stream->parent.cycles_timestamp = packet_index->timestamp_begin;
944
945 packet_index = &g_array_index(pos->packet_real_index,
946 struct packet_index,
947 pos->cur_index);
948 file_stream->parent.real_timestamp = packet_index->timestamp_begin;
8c572eba
MD
949
950 /* Lookup context/packet size in index */
e69dd258
JD
951 if (packet_index->data_offset == -1) {
952 ret = find_data_offset(pos, file_stream, packet_index);
953 if (ret < 0) {
954 return;
955 }
956 }
20d0dcf9
MD
957 pos->content_size = packet_index->content_size;
958 pos->packet_size = packet_index->packet_size;
e69dd258 959 pos->mmap_offset = packet_index->offset;
20d0dcf9 960 if (packet_index->data_offset < packet_index->content_size) {
75cc2c35 961 pos->offset = 0; /* will read headers */
20d0dcf9 962 } else if (packet_index->data_offset == packet_index->content_size) {
5f643ed7 963 /* empty packet */
20d0dcf9 964 pos->offset = packet_index->data_offset;
3abe83c7 965 whence = SEEK_CUR;
5f643ed7 966 goto read_next_packet;
7eda6fc7 967 } else {
2b9a764d
MD
968 pos->offset = EOF;
969 return;
970 }
8c572eba 971 }
0f980a35 972 /* map new base. Need mapping length from header. */
aee35fcc
MD
973 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
974 pos->flags, pos->fd, pos->mmap_offset);
975 if (pos->base_mma == MAP_FAILED) {
3394d22e 976 fprintf(stderr, "[error] mmap error %s.\n",
847bf71a
MD
977 strerror(errno));
978 assert(0);
979 }
75cc2c35
MD
980
981 /* update trace_packet_header and stream_packet_context */
2d0bea29 982 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
75cc2c35 983 /* Read packet header */
2d0bea29 984 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
75cc2c35
MD
985 assert(!ret);
986 }
2d0bea29 987 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
75cc2c35 988 /* Read packet context */
2d0bea29 989 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
75cc2c35
MD
990 assert(!ret);
991 }
0f980a35
MD
992}
993
b4c19c1e
MD
994static
995int packet_metadata(struct ctf_trace *td, FILE *fp)
996{
997 uint32_t magic;
998 size_t len;
999 int ret = 0;
1000
1001 len = fread(&magic, sizeof(magic), 1, fp);
a0fe7d97 1002 if (len != 1) {
b4c19c1e
MD
1003 goto end;
1004 }
1005 if (magic == TSDL_MAGIC) {
1006 ret = 1;
1007 td->byte_order = BYTE_ORDER;
0d336fdf 1008 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
1009 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
1010 ret = 1;
1011 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
1012 LITTLE_ENDIAN : BIG_ENDIAN;
0d336fdf 1013 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
1014 }
1015end:
1016 rewind(fp);
1017 return ret;
1018}
1019
5c262147
MD
1020/*
1021 * Returns 0 on success, -1 on error.
1022 */
1023static
1024int check_version(unsigned int major, unsigned int minor)
1025{
1026 switch (major) {
1027 case 1:
1028 switch (minor) {
1029 case 8:
1030 return 0;
1031 default:
1032 goto warning;
1033 }
1034 default:
1035 goto warning;
1036
1037 }
1038
1039 /* eventually return an error instead of warning */
1040warning:
3394d22e 1041 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
5c262147
MD
1042 major, minor);
1043 return 0;
1044}
1045
b4c19c1e
MD
1046static
1047int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
1048 FILE *out)
1049{
1050 struct metadata_packet_header header;
a0fe7d97 1051 size_t readlen, writelen, toread;
f8254867 1052 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
b4c19c1e
MD
1053 int ret = 0;
1054
a91a962e 1055 readlen = fread(&header, header_sizeof(header), 1, in);
a0fe7d97 1056 if (readlen < 1)
b4c19c1e
MD
1057 return -EINVAL;
1058
1059 if (td->byte_order != BYTE_ORDER) {
1060 header.magic = GUINT32_SWAP_LE_BE(header.magic);
1061 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
1062 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
1063 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
1064 }
1065 if (header.checksum)
3394d22e 1066 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
b4c19c1e 1067 if (header.compression_scheme) {
3394d22e 1068 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
b4c19c1e
MD
1069 header.compression_scheme);
1070 return -EINVAL;
1071 }
1072 if (header.encryption_scheme) {
3394d22e 1073 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
b4c19c1e
MD
1074 header.encryption_scheme);
1075 return -EINVAL;
1076 }
1077 if (header.checksum_scheme) {
3394d22e 1078 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
b4c19c1e
MD
1079 header.checksum_scheme);
1080 return -EINVAL;
1081 }
5c262147
MD
1082 if (check_version(header.major, header.minor) < 0)
1083 return -EINVAL;
b4c19c1e
MD
1084 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
1085 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
1086 CTF_TRACE_SET_FIELD(td, uuid);
1087 } else {
43e34335 1088 if (babeltrace_uuid_compare(header.uuid, td->uuid))
b4c19c1e
MD
1089 return -EINVAL;
1090 }
1091
b1ccd079
MD
1092 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
1093 return -EINVAL;
1094
255b2138 1095 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
a0fe7d97
MD
1096
1097 for (;;) {
f8254867 1098 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
b4c19c1e
MD
1099 if (ferror(in)) {
1100 ret = -EINVAL;
1101 break;
1102 }
4152822b 1103 if (babeltrace_debug) {
f8254867 1104 buf[readlen] = '\0';
3394d22e 1105 fprintf(stderr, "[debug] metadata packet read: %s\n",
4152822b
MD
1106 buf);
1107 }
1108
b4c19c1e
MD
1109 writelen = fwrite(buf, sizeof(char), readlen, out);
1110 if (writelen < readlen) {
1111 ret = -EIO;
1112 break;
1113 }
1114 if (ferror(out)) {
1115 ret = -EINVAL;
1116 break;
1117 }
a0fe7d97
MD
1118 toread -= readlen;
1119 if (!toread) {
7f4b5c4d 1120 ret = 0; /* continue reading next packet */
ddbc52af 1121 goto read_padding;
a0fe7d97 1122 }
b4c19c1e
MD
1123 }
1124 return ret;
ddbc52af
MD
1125
1126read_padding:
1127 toread = (header.packet_size - header.content_size) / CHAR_BIT;
1128 ret = fseek(in, toread, SEEK_CUR);
1129 if (ret < 0) {
3394d22e 1130 fprintf(stderr, "[warning] Missing padding at end of file\n");
ddbc52af
MD
1131 ret = 0;
1132 }
1133 return ret;
b4c19c1e
MD
1134}
1135
1136static
1137int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
1138 char **buf)
1139{
1140 FILE *in, *out;
abc40d24 1141 size_t size, buflen;
b4c19c1e
MD
1142 int ret;
1143
1144 in = *fp;
c4f5487e
MD
1145 /*
1146 * Using strlen on *buf instead of size of open_memstream
1147 * because its size includes garbage at the end (after final
1148 * \0). This is the allocated size, not the actual string size.
1149 */
f8370579 1150 out = babeltrace_open_memstream(buf, &size);
a569a564
MD
1151 if (out == NULL) {
1152 perror("Metadata open_memstream");
b4c19c1e 1153 return -errno;
a569a564 1154 }
b4c19c1e
MD
1155 for (;;) {
1156 ret = ctf_open_trace_metadata_packet_read(td, in, out);
7f4b5c4d 1157 if (ret) {
b4c19c1e 1158 break;
7f4b5c4d
MD
1159 }
1160 if (feof(in)) {
1161 ret = 0;
b4c19c1e
MD
1162 break;
1163 }
1164 }
f8370579
MD
1165 /* close to flush the buffer */
1166 ret = babeltrace_close_memstream(buf, &size, out);
1167 if (ret < 0) {
f824ae04
MD
1168 int closeret;
1169
f8370579 1170 perror("babeltrace_flush_memstream");
f824ae04
MD
1171 ret = -errno;
1172 closeret = fclose(in);
1173 if (closeret) {
1174 perror("Error in fclose");
1175 }
1176 return ret;
1177 }
1178 ret = fclose(in);
1179 if (ret) {
1180 perror("Error in fclose");
f8370579 1181 }
b4c19c1e 1182 /* open for reading */
abc40d24
MD
1183 buflen = strlen(*buf);
1184 if (!buflen) {
1185 *fp = NULL;
493330cb 1186 return -ENOENT;
abc40d24
MD
1187 }
1188 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
a569a564
MD
1189 if (!*fp) {
1190 perror("Metadata fmemopen");
1191 return -errno;
1192 }
b4c19c1e
MD
1193 return 0;
1194}
1195
65102a8c 1196static
b086c01a 1197int ctf_open_trace_metadata_read(struct ctf_trace *td,
1cf393f6 1198 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 1199 int whence), FILE *metadata_fp)
65102a8c
MD
1200{
1201 struct ctf_scanner *scanner;
2d0bea29 1202 struct ctf_file_stream *metadata_stream;
65102a8c 1203 FILE *fp;
b4c19c1e 1204 char *buf = NULL;
f824ae04 1205 int ret = 0, closeret;
65102a8c 1206
2d0bea29 1207 metadata_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 1208 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
b086c01a 1209
06789ffd
MD
1210 if (packet_seek) {
1211 metadata_stream->pos.packet_seek = packet_seek;
b086c01a 1212 } else {
06789ffd 1213 fprintf(stderr, "[error] packet_seek function undefined.\n");
b086c01a 1214 ret = -1;
f824ae04 1215 goto end_free;
b086c01a
JD
1216 }
1217
ae23d232
JD
1218 if (metadata_fp) {
1219 fp = metadata_fp;
bcbfb8bf 1220 metadata_stream->pos.fd = -1;
ae23d232
JD
1221 } else {
1222 td->metadata = &metadata_stream->parent;
1223 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1224 if (metadata_stream->pos.fd < 0) {
3394d22e 1225 fprintf(stderr, "Unable to open metadata.\n");
f824ae04
MD
1226 ret = -1;
1227 goto end_free;
ae23d232 1228 }
65102a8c 1229
ae23d232
JD
1230 fp = fdopen(metadata_stream->pos.fd, "r");
1231 if (!fp) {
3394d22e 1232 fprintf(stderr, "[error] Unable to open metadata stream.\n");
a569a564 1233 perror("Metadata stream open");
ae23d232
JD
1234 ret = -errno;
1235 goto end_stream;
1236 }
f824ae04
MD
1237 /* fd now belongs to fp */
1238 metadata_stream->pos.fd = -1;
ae23d232 1239 }
65102a8c
MD
1240 if (babeltrace_debug)
1241 yydebug = 1;
1242
b4c19c1e
MD
1243 if (packet_metadata(td, fp)) {
1244 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
abc40d24
MD
1245 if (ret) {
1246 /* Warn about empty metadata */
1247 fprintf(stderr, "[warning] Empty metadata.\n");
b4c19c1e 1248 goto end_packet_read;
abc40d24 1249 }
7237592a
MD
1250 td->metadata_string = buf;
1251 td->metadata_packetized = 1;
da75b0f7 1252 } else {
5c262147
MD
1253 unsigned int major, minor;
1254 ssize_t nr_items;
8c2df3f5 1255
da75b0f7 1256 td->byte_order = BYTE_ORDER;
8c2df3f5 1257
5c262147
MD
1258 /* Check text-only metadata header and version */
1259 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
1260 if (nr_items < 2)
3394d22e 1261 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
5c262147
MD
1262 if (check_version(major, minor) < 0) {
1263 ret = -EINVAL;
1264 goto end_packet_read;
1265 }
8c2df3f5 1266 rewind(fp);
b4c19c1e
MD
1267 }
1268
65102a8c
MD
1269 scanner = ctf_scanner_alloc(fp);
1270 if (!scanner) {
3394d22e 1271 fprintf(stderr, "[error] Error allocating scanner\n");
65102a8c
MD
1272 ret = -ENOMEM;
1273 goto end_scanner_alloc;
1274 }
1275 ret = ctf_scanner_append_ast(scanner);
1276 if (ret) {
3394d22e 1277 fprintf(stderr, "[error] Error creating AST\n");
65102a8c
MD
1278 goto end;
1279 }
1280
1281 if (babeltrace_debug) {
3394d22e 1282 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
65102a8c 1283 if (ret) {
3394d22e 1284 fprintf(stderr, "[error] Error visiting AST for XML output\n");
65102a8c
MD
1285 goto end;
1286 }
1287 }
1288
3394d22e 1289 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
65102a8c 1290 if (ret) {
3394d22e 1291 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
1292 goto end;
1293 }
3394d22e 1294 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
ac5c6ca0 1295 td, td->byte_order);
65102a8c 1296 if (ret) {
3394d22e 1297 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
1298 goto end;
1299 }
1300end:
1301 ctf_scanner_free(scanner);
1302end_scanner_alloc:
b4c19c1e 1303end_packet_read:
f824ae04
MD
1304 if (fp) {
1305 closeret = fclose(fp);
1306 if (closeret) {
1307 perror("Error on fclose");
1308 }
1309 }
65102a8c 1310end_stream:
f824ae04
MD
1311 if (metadata_stream->pos.fd >= 0) {
1312 closeret = close(metadata_stream->pos.fd);
1313 if (closeret) {
1314 perror("Error on metadata stream fd close");
1315 }
1316 }
1317end_free:
2d0bea29
MD
1318 if (ret)
1319 g_free(metadata_stream);
0f980a35
MD
1320 return ret;
1321}
1322
e28d4618 1323static
c716f83b 1324struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
9e88d150 1325 struct ctf_stream_definition *stream,
4716614a 1326 struct ctf_event_declaration *event)
e28d4618 1327{
c716f83b 1328 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
e28d4618
MD
1329
1330 if (event->context_decl) {
0d69b916 1331 struct bt_definition *definition =
e28d4618
MD
1332 event->context_decl->p.definition_new(&event->context_decl->p,
1333 stream->parent_def_scope, 0, 0, "event.context");
1334 if (!definition) {
1335 goto error;
1336 }
42dc00b7 1337 stream_event->event_context = container_of(definition,
e28d4618 1338 struct definition_struct, p);
42dc00b7 1339 stream->parent_def_scope = stream_event->event_context->p.scope;
e28d4618
MD
1340 }
1341 if (event->fields_decl) {
0d69b916 1342 struct bt_definition *definition =
e28d4618
MD
1343 event->fields_decl->p.definition_new(&event->fields_decl->p,
1344 stream->parent_def_scope, 0, 0, "event.fields");
1345 if (!definition) {
1346 goto error;
1347 }
42dc00b7 1348 stream_event->event_fields = container_of(definition,
e28d4618 1349 struct definition_struct, p);
42dc00b7 1350 stream->parent_def_scope = stream_event->event_fields->p.scope;
e28d4618 1351 }
d3ded99d 1352 stream_event->stream = stream;
42dc00b7 1353 return stream_event;
e28d4618
MD
1354
1355error:
42dc00b7 1356 if (stream_event->event_fields)
13fad8b6 1357 bt_definition_unref(&stream_event->event_fields->p);
42dc00b7 1358 if (stream_event->event_context)
13fad8b6 1359 bt_definition_unref(&stream_event->event_context->p);
888ec52a
MD
1360 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1361 g_quark_to_string(event->name));
e28d4618
MD
1362 return NULL;
1363}
1364
1365static
9e88d150 1366int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618 1367{
f380e105 1368 struct ctf_stream_declaration *stream_class;
e28d4618
MD
1369 int ret;
1370 int i;
1371
1372 if (stream->stream_definitions_created)
1373 return 0;
1374
1375 stream_class = stream->stream_class;
1376
1377 if (stream_class->packet_context_decl) {
0d69b916 1378 struct bt_definition *definition =
e28d4618
MD
1379 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1380 stream->parent_def_scope, 0, 0, "stream.packet.context");
1381 if (!definition) {
1382 ret = -EINVAL;
1383 goto error;
1384 }
1385 stream->stream_packet_context = container_of(definition,
1386 struct definition_struct, p);
1387 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1388 }
1389 if (stream_class->event_header_decl) {
0d69b916 1390 struct bt_definition *definition =
e28d4618
MD
1391 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1392 stream->parent_def_scope, 0, 0, "stream.event.header");
1393 if (!definition) {
1394 ret = -EINVAL;
1395 goto error;
1396 }
1397 stream->stream_event_header =
1398 container_of(definition, struct definition_struct, p);
1399 stream->parent_def_scope = stream->stream_event_header->p.scope;
1400 }
1401 if (stream_class->event_context_decl) {
0d69b916 1402 struct bt_definition *definition =
e28d4618
MD
1403 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1404 stream->parent_def_scope, 0, 0, "stream.event.context");
1405 if (!definition) {
1406 ret = -EINVAL;
1407 goto error;
1408 }
1409 stream->stream_event_context =
1410 container_of(definition, struct definition_struct, p);
1411 stream->parent_def_scope = stream->stream_event_context->p.scope;
1412 }
1413 stream->events_by_id = g_ptr_array_new();
1414 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
1415 for (i = 0; i < stream->events_by_id->len; i++) {
4716614a 1416 struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i);
c716f83b 1417 struct ctf_event_definition *stream_event;
e28d4618
MD
1418
1419 if (!event)
1420 continue;
42dc00b7 1421 stream_event = create_event_definitions(td, stream, event);
888ec52a
MD
1422 if (!stream_event) {
1423 ret = -EINVAL;
e28d4618 1424 goto error_event;
888ec52a 1425 }
42dc00b7 1426 g_ptr_array_index(stream->events_by_id, i) = stream_event;
e28d4618
MD
1427 }
1428 return 0;
1429
1430error_event:
1431 for (i = 0; i < stream->events_by_id->len; i++) {
c716f83b 1432 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
42dc00b7
MD
1433 if (stream_event)
1434 g_free(stream_event);
e28d4618
MD
1435 }
1436 g_ptr_array_free(stream->events_by_id, TRUE);
1437error:
1438 if (stream->stream_event_context)
13fad8b6 1439 bt_definition_unref(&stream->stream_event_context->p);
e28d4618 1440 if (stream->stream_event_header)
13fad8b6 1441 bt_definition_unref(&stream->stream_event_header->p);
e28d4618 1442 if (stream->stream_packet_context)
13fad8b6 1443 bt_definition_unref(&stream->stream_packet_context->p);
888ec52a
MD
1444 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1445 stream_class->stream_id, strerror(-ret));
e28d4618
MD
1446 return ret;
1447}
1448
5bfcad93
MD
1449static
1450int stream_assign_class(struct ctf_trace *td,
1451 struct ctf_file_stream *file_stream,
1452 uint64_t stream_id)
1453{
1454 struct ctf_stream_declaration *stream;
1455 int ret;
1456
1457 file_stream->parent.stream_id = stream_id;
1458 if (stream_id >= td->streams->len) {
1459 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1460 return -EINVAL;
1461 }
1462 stream = g_ptr_array_index(td->streams, stream_id);
1463 if (!stream) {
1464 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1465 return -EINVAL;
1466 }
1467 file_stream->parent.stream_class = stream;
1468 ret = create_stream_definitions(td, &file_stream->parent);
1469 if (ret)
1470 return ret;
1471 return 0;
1472}
1473
0f980a35 1474static
ec323464
MD
1475int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1476 struct ctf_trace *td,
1477 struct ctf_file_stream *file_stream,
1478 size_t filesize)
0f980a35 1479{
ec323464 1480 struct packet_index packet_index;
ec323464 1481 uint64_t stream_id = 0;
653906a4 1482 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
ec323464 1483 int first_packet = 0;
653906a4 1484 int len_index;
0f980a35
MD
1485 int ret;
1486
ec323464
MD
1487begin:
1488 if (!pos->mmap_offset) {
1489 first_packet = 1;
1490 }
8895362d 1491
ec323464
MD
1492 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1493 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1494 }
0f980a35 1495
ec323464
MD
1496 if (pos->base_mma) {
1497 /* unmap old base */
1498 ret = munmap_align(pos->base_mma);
1499 if (ret) {
1500 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1501 strerror(errno));
1502 return ret;
0f980a35 1503 }
ec323464
MD
1504 pos->base_mma = NULL;
1505 }
1506 /* map new base. Need mapping length from header. */
1507 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1508 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1509 assert(pos->base_mma != MAP_FAILED);
1510 /*
1511 * Use current mapping size as temporary content and packet
1512 * size.
1513 */
1514 pos->content_size = packet_map_len;
1515 pos->packet_size = packet_map_len;
1516 pos->offset = 0; /* Position of the packet header */
1517
1518 packet_index.offset = pos->mmap_offset;
1519 packet_index.content_size = 0;
1520 packet_index.packet_size = 0;
1521 packet_index.timestamp_begin = 0;
1522 packet_index.timestamp_end = 0;
1523 packet_index.events_discarded = 0;
1524 packet_index.events_discarded_len = 0;
1525
1526 /* read and check header, set stream id (and check) */
1527 if (file_stream->parent.trace_packet_header) {
1528 /* Read packet header */
1529 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1530 if (ret) {
1531 if (ret == -EFAULT)
1532 goto retry;
888ec52a 1533 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
ec323464
MD
1534 return ret;
1535 }
1536 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1537 if (len_index >= 0) {
1538 struct bt_definition *field;
1539 uint64_t magic;
1540
1541 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1542 magic = bt_get_unsigned_int(field);
1543 if (magic != CTF_MAGIC) {
1544 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1545 magic,
1546 file_stream->pos.packet_cycles_index->len,
1547 (ssize_t) pos->mmap_offset);
1548 return -EINVAL;
0f980a35 1549 }
ec323464 1550 }
0f980a35 1551
ec323464
MD
1552 /* check uuid */
1553 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1554 if (len_index >= 0) {
1555 struct definition_array *defarray;
1556 struct bt_definition *field;
1557 uint64_t i;
1558 uint8_t uuidval[BABELTRACE_UUID_LEN];
0f980a35 1559
ec323464
MD
1560 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1561 assert(field->declaration->id == CTF_TYPE_ARRAY);
1562 defarray = container_of(field, struct definition_array, p);
1563 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
0f980a35 1564
ec323464
MD
1565 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1566 struct bt_definition *elem;
0f980a35 1567
ec323464
MD
1568 elem = bt_array_index(defarray, i);
1569 uuidval[i] = bt_get_unsigned_int(elem);
0f980a35 1570 }
ec323464
MD
1571 ret = babeltrace_uuid_compare(td->uuid, uuidval);
1572 if (ret) {
1573 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1574 return -EINVAL;
1575 }
1576 }
0f980a35 1577
ec323464
MD
1578 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1579 if (len_index >= 0) {
1580 struct bt_definition *field;
0f980a35 1581
ec323464
MD
1582 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1583 stream_id = bt_get_unsigned_int(field);
0f980a35 1584 }
ec323464 1585 }
0f980a35 1586
ec323464
MD
1587 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1588 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1589 stream_id,
1590 file_stream->parent.stream_id);
1591 return -EINVAL;
1592 }
1593 if (first_packet) {
5bfcad93 1594 ret = stream_assign_class(td, file_stream, stream_id);
ec323464
MD
1595 if (ret)
1596 return ret;
1597 }
dc48ecad 1598
ec323464
MD
1599 if (file_stream->parent.stream_packet_context) {
1600 /* Read packet context */
1601 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1602 if (ret) {
1603 if (ret == -EFAULT)
1604 goto retry;
888ec52a 1605 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
ec323464
MD
1606 return ret;
1607 }
95b34f38
MD
1608 /* read packet size from header */
1609 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
ec323464
MD
1610 if (len_index >= 0) {
1611 struct bt_definition *field;
dc48ecad 1612
ec323464 1613 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
95b34f38 1614 packet_index.packet_size = bt_get_unsigned_int(field);
ec323464
MD
1615 } else {
1616 /* Use file size for packet size */
95b34f38 1617 packet_index.packet_size = filesize * CHAR_BIT;
ec323464 1618 }
75cc2c35 1619
95b34f38
MD
1620 /* read content size from header */
1621 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
ec323464
MD
1622 if (len_index >= 0) {
1623 struct bt_definition *field;
75cc2c35 1624
ec323464 1625 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
95b34f38 1626 packet_index.content_size = bt_get_unsigned_int(field);
ec323464 1627 } else {
95b34f38
MD
1628 /* Use packet size if non-zero, else file size */
1629 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
ec323464 1630 }
41e82e00 1631
ec323464
MD
1632 /* read timestamp begin from header */
1633 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1634 if (len_index >= 0) {
1635 struct bt_definition *field;
41e82e00 1636
ec323464
MD
1637 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1638 packet_index.timestamp_begin = bt_get_unsigned_int(field);
4c62e2d8 1639 if (file_stream->parent.stream_class->trace->parent.collection) {
ec323464
MD
1640 packet_index.timestamp_begin =
1641 ctf_get_real_timestamp(
1642 &file_stream->parent,
1643 packet_index.timestamp_begin);
41e82e00 1644 }
0f980a35 1645 }
546293fa 1646
ec323464
MD
1647 /* read timestamp end from header */
1648 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1649 if (len_index >= 0) {
1650 struct bt_definition *field;
1651
1652 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1653 packet_index.timestamp_end = bt_get_unsigned_int(field);
4c62e2d8 1654 if (file_stream->parent.stream_class->trace->parent.collection) {
ec323464
MD
1655 packet_index.timestamp_end =
1656 ctf_get_real_timestamp(
1657 &file_stream->parent,
1658 packet_index.timestamp_end);
1659 }
546293fa
MD
1660 }
1661
ec323464
MD
1662 /* read events discarded from header */
1663 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1664 if (len_index >= 0) {
1665 struct bt_definition *field;
1666
1667 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1668 packet_index.events_discarded = bt_get_unsigned_int(field);
1669 packet_index.events_discarded_len = bt_get_int_len(field);
546293fa 1670 }
ec323464
MD
1671 } else {
1672 /* Use file size for packet size */
2d686891
MD
1673 packet_index.packet_size = filesize * CHAR_BIT;
1674 /* Use packet size if non-zero, else file size */
1675 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
ec323464
MD
1676 }
1677
1678 /* Validate content size and packet size values */
1679 if (packet_index.content_size > packet_index.packet_size) {
1680 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1681 packet_index.content_size, packet_index.packet_size);
1682 return -EINVAL;
1683 }
1684
1685 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1686 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1687 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1688 return -EINVAL;
1689 }
546293fa 1690
a7ac9efd
MD
1691 if (packet_index.content_size < pos->offset) {
1692 fprintf(stderr, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1693 return -EINVAL;
1694 }
1695
2d686891
MD
1696 if ((packet_index.packet_size >> LOG2_CHAR_BIT) == 0) {
1697 fprintf(stderr, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1698 return -EINVAL;
1699 }
1700
ec323464
MD
1701 /* Save position after header and context */
1702 packet_index.data_offset = pos->offset;
0f980a35 1703
ec323464
MD
1704 /* add index to packet array */
1705 g_array_append_val(file_stream->pos.packet_cycles_index, packet_index);
0f980a35 1706
ec323464
MD
1707 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1708
1709 return 0;
1710
1711 /* Retry with larger mapping */
1712retry:
1713 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1714 /*
1715 * Reached EOF, but still expecting header/context data.
1716 */
1717 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1718 return -EFAULT;
1719 }
1720 /* Double the mapping len, and retry */
1721 tmp_map_len = packet_map_len << 1;
1722 if (tmp_map_len >> 1 != packet_map_len) {
1723 /* Overflow */
888ec52a 1724 fprintf(stderr, "[error] Packet mapping length overflow\n");
ec323464 1725 return -EFAULT;
0f980a35 1726 }
ec323464
MD
1727 packet_map_len = tmp_map_len;
1728 goto begin;
1729}
1730
1731static
1732int create_stream_packet_index(struct ctf_trace *td,
1733 struct ctf_file_stream *file_stream)
1734{
1735 struct ctf_stream_pos *pos;
1736 struct stat filestats;
1737 int ret;
1738
1739 pos = &file_stream->pos;
0f980a35 1740
ec323464
MD
1741 ret = fstat(pos->fd, &filestats);
1742 if (ret < 0)
1743 return ret;
1744
5bfcad93
MD
1745 /* Deal with empty files */
1746 if (!filestats.st_size) {
1747 if (file_stream->parent.trace_packet_header
1748 || file_stream->parent.stream_packet_context) {
1749 /*
1750 * We expect a trace packet header and/or stream packet
1751 * context. Since a trace needs to have at least one
1752 * packet, empty files are therefore not accepted.
1753 */
1754 fprintf(stderr, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1755 return -EINVAL;
1756 } else {
1757 /*
1758 * Without trace packet header nor stream packet
1759 * context, a one-packet trace can indeed be empty. This
1760 * is only valid if there is only one stream class: 0.
1761 */
1762 ret = stream_assign_class(td, file_stream, 0);
1763 if (ret)
1764 return ret;
1765 return 0;
1766 }
1767 }
1768
ec323464
MD
1769 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1770 ret = create_stream_one_packet_index(pos, td, file_stream,
1771 filestats.st_size);
1772 if (ret)
1773 return ret;
1774 }
0f980a35
MD
1775 return 0;
1776}
1777
e28d4618 1778static
9e88d150 1779int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618
MD
1780{
1781 int ret;
1782
1783 if (td->packet_header_decl) {
0d69b916 1784 struct bt_definition *definition =
e28d4618
MD
1785 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1786 stream->parent_def_scope, 0, 0, "trace.packet.header");
1787 if (!definition) {
1788 ret = -EINVAL;
1789 goto error;
1790 }
1791 stream->trace_packet_header =
1792 container_of(definition, struct definition_struct, p);
1793 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1794 }
1795
1796 return 0;
1797
1798error:
888ec52a 1799 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
e28d4618
MD
1800 return ret;
1801}
1802
0ace7505
JD
1803static
1804int import_stream_packet_index(struct ctf_trace *td,
1805 struct ctf_file_stream *file_stream)
1806{
1807 struct ctf_stream_declaration *stream;
1808 struct ctf_stream_pos *pos;
1809 struct ctf_packet_index ctf_index;
1810 struct ctf_packet_index_file_hdr index_hdr;
1811 struct packet_index index;
1812 int index_read;
1813 int ret = 0;
1814 int first_packet = 1;
1815 size_t len;
1816
1817 pos = &file_stream->pos;
1818
1819 len = fread(&index_hdr, sizeof(index_hdr), 1, pos->index_fp);
1820 if (len != 1) {
1821 perror("read index file header");
1822 goto error;
1823 }
1824
1825 /* Check the index header */
1826 if (be32toh(index_hdr.magic) != CTF_INDEX_MAGIC) {
1827 fprintf(stderr, "[error] wrong index magic\n");
1828 ret = -1;
1829 goto error;
1830 }
1831 if (be32toh(index_hdr.index_major) != CTF_INDEX_MAJOR) {
1832 fprintf(stderr, "[error] Incompatible index file %" PRIu64
1833 ".%" PRIu64 ", supported %d.%d\n",
1834 be64toh(index_hdr.index_major),
1835 be64toh(index_hdr.index_minor), CTF_INDEX_MAJOR,
1836 CTF_INDEX_MINOR);
1837 ret = -1;
1838 goto error;
1839 }
a74d9cb2
MD
1840 if (index_hdr.packet_index_len == 0) {
1841 fprintf(stderr, "[error] Packet index length cannot be 0.\n");
1842 ret = -1;
1843 goto error;
1844 }
0ace7505
JD
1845
1846 while ((index_read = fread(&ctf_index, index_hdr.packet_index_len, 1,
1847 pos->index_fp)) == 1) {
1848 uint64_t stream_id;
1849
1850 memset(&index, 0, sizeof(index));
1851 index.offset = be64toh(ctf_index.offset);
1852 index.packet_size = be64toh(ctf_index.packet_size);
1853 index.content_size = be64toh(ctf_index.content_size);
1854 index.timestamp_begin = be64toh(ctf_index.timestamp_begin);
1855 index.timestamp_end = be64toh(ctf_index.timestamp_end);
1856 index.events_discarded = be64toh(ctf_index.events_discarded);
1857 index.events_discarded_len = 64;
e69dd258 1858 index.data_offset = -1;
0ace7505
JD
1859 stream_id = be64toh(ctf_index.stream_id);
1860
1861 if (!first_packet) {
1862 /* add index to packet array */
1863 g_array_append_val(file_stream->pos.packet_cycles_index, index);
1864 continue;
1865 }
1866
1867 file_stream->parent.stream_id = stream_id;
1868 stream = g_ptr_array_index(td->streams, stream_id);
1869 if (!stream) {
1870 fprintf(stderr, "[error] Stream %" PRIu64
1871 " is not declared in metadata.\n",
1872 stream_id);
1873 ret = -EINVAL;
1874 goto error;
1875 }
1876 file_stream->parent.stream_class = stream;
1877 ret = create_stream_definitions(td, &file_stream->parent);
1878 if (ret)
1879 goto error;
1880 first_packet = 0;
1881 /* add index to packet array */
1882 g_array_append_val(file_stream->pos.packet_cycles_index, index);
1883 }
1884
1885 ret = 0;
1886
1887error:
1888 return ret;
1889}
1890
0f980a35
MD
1891/*
1892 * Note: many file streams can inherit from the same stream class
1893 * description (metadata).
1894 */
1895static
b086c01a 1896int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1cf393f6 1897 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
b086c01a 1898 int whence))
0f980a35 1899{
f824ae04 1900 int ret, fd, closeret;
0f980a35 1901 struct ctf_file_stream *file_stream;
ff075710 1902 struct stat statbuf;
0ace7505 1903 char *index_name;
0f980a35 1904
ff075710
MD
1905 fd = openat(td->dirfd, path, flags);
1906 if (fd < 0) {
a569a564 1907 perror("File stream openat()");
ff075710 1908 ret = fd;
0f980a35 1909 goto error;
a569a564 1910 }
ff075710
MD
1911
1912 /* Don't try to mmap subdirectories. Skip them, return success. */
1913 ret = fstat(fd, &statbuf);
1914 if (ret) {
1915 perror("File stream fstat()");
1916 goto fstat_error;
1917 }
1918 if (S_ISDIR(statbuf.st_mode)) {
0ace7505
JD
1919 if (strncmp(path, "index", 5) != 0) {
1920 fprintf(stderr, "[warning] Skipping directory '%s' "
1921 "found in trace\n", path);
1922 }
ff075710
MD
1923 ret = 0;
1924 goto fd_is_dir_ok;
1925 }
1926
0f980a35 1927 file_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 1928 file_stream->pos.last_offset = LAST_OFFSET_POISON;
0ace7505
JD
1929 file_stream->pos.fd = -1;
1930 file_stream->pos.index_fp = NULL;
b086c01a 1931
87148dc7
MD
1932 strncpy(file_stream->parent.path, path, PATH_MAX);
1933 file_stream->parent.path[PATH_MAX - 1] = '\0';
1934
06789ffd
MD
1935 if (packet_seek) {
1936 file_stream->pos.packet_seek = packet_seek;
b086c01a 1937 } else {
06789ffd 1938 fprintf(stderr, "[error] packet_seek function undefined.\n");
b086c01a
JD
1939 ret = -1;
1940 goto error_def;
1941 }
1942
ca334c72 1943 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
f824ae04
MD
1944 if (ret)
1945 goto error_def;
2d0bea29 1946 ret = create_trace_definitions(td, &file_stream->parent);
e28d4618
MD
1947 if (ret)
1948 goto error_def;
25ccc85b 1949 /*
50052405 1950 * For now, only a single clock per trace is supported.
25ccc85b 1951 */
7ec78969 1952 file_stream->parent.current_clock = td->parent.single_clock;
0ace7505
JD
1953
1954 /*
1955 * Allocate the index name for this stream and try to open it.
1956 */
1957 index_name = malloc((strlen(path) + sizeof(INDEX_PATH)) * sizeof(char));
1958 if (!index_name) {
1959 fprintf(stderr, "[error] Cannot allocate index filename\n");
1960 goto error_def;
888ec52a 1961 }
0ace7505
JD
1962 snprintf(index_name, strlen(path) + sizeof(INDEX_PATH),
1963 INDEX_PATH, path);
1964
1965 if (faccessat(td->dirfd, index_name, O_RDONLY, flags) < 0) {
1966 ret = create_stream_packet_index(td, file_stream);
1967 if (ret) {
1968 fprintf(stderr, "[error] Stream index creation error.\n");
1969 goto error_index;
1970 }
1971 } else {
1972 ret = openat(td->dirfd, index_name, flags);
1973 if (ret < 0) {
1974 perror("Index file openat()");
1975 ret = -1;
1976 goto error_free;
1977 }
1978 file_stream->pos.index_fp = fdopen(ret, "r");
2f0c6a52
MD
1979 if (!file_stream->pos.index_fp) {
1980 perror("fdopen() error");
1981 goto error_free;
1982 }
0ace7505
JD
1983 ret = import_stream_packet_index(td, file_stream);
1984 if (ret) {
1985 ret = -1;
1986 goto error_index;
1987 }
1988 ret = fclose(file_stream->pos.index_fp);
1989 if (ret < 0) {
1990 perror("close index");
1991 goto error_free;
1992 }
1993 }
1994 free(index_name);
1995
0f980a35 1996 /* Add stream file to stream class */
2d0bea29
MD
1997 g_ptr_array_add(file_stream->parent.stream_class->streams,
1998 &file_stream->parent);
0f980a35
MD
1999 return 0;
2000
2001error_index:
0ace7505
JD
2002 if (file_stream->pos.index_fp) {
2003 ret = fclose(file_stream->pos.index_fp);
2004 if (ret < 0) {
2005 perror("close index");
2006 }
2007 }
2d0bea29 2008 if (file_stream->parent.trace_packet_header)
13fad8b6 2009 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
0ace7505
JD
2010error_free:
2011 free(index_name);
e28d4618 2012error_def:
f824ae04
MD
2013 closeret = ctf_fini_pos(&file_stream->pos);
2014 if (closeret) {
2015 fprintf(stderr, "Error on ctf_fini_pos\n");
2016 }
0f980a35 2017 g_free(file_stream);
ff075710
MD
2018fd_is_dir_ok:
2019fstat_error:
f824ae04
MD
2020 closeret = close(fd);
2021 if (closeret) {
2022 perror("Error on fd close");
2023 }
0f980a35 2024error:
65102a8c
MD
2025 return ret;
2026}
2027
bbefb8dd 2028static
5b80ddfb 2029int ctf_open_trace_read(struct ctf_trace *td,
8c250d87 2030 const char *path, int flags,
1cf393f6 2031 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 2032 int whence), FILE *metadata_fp)
bbefb8dd 2033{
f824ae04 2034 int ret, closeret;
65102a8c
MD
2035 struct dirent *dirent;
2036 struct dirent *diriter;
2037 size_t dirent_len;
0ace7505 2038 char *ext;
bbefb8dd 2039
46322b33 2040 td->flags = flags;
bbefb8dd
MD
2041
2042 /* Open trace directory */
46322b33
MD
2043 td->dir = opendir(path);
2044 if (!td->dir) {
6a6b384c 2045 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
bbefb8dd
MD
2046 ret = -ENOENT;
2047 goto error;
2048 }
2049
46322b33
MD
2050 td->dirfd = open(path, 0);
2051 if (td->dirfd < 0) {
6a6b384c 2052 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
a569a564
MD
2053 perror("Trace directory open");
2054 ret = -errno;
65102a8c
MD
2055 goto error_dirfd;
2056 }
caf929fa
MD
2057 strncpy(td->parent.path, path, sizeof(td->parent.path));
2058 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
0f980a35 2059
65102a8c
MD
2060 /*
2061 * Keep the metadata file separate.
2062 */
bbefb8dd 2063
06789ffd 2064 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
65102a8c 2065 if (ret) {
6a6b384c 2066 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
65102a8c
MD
2067 goto error_metadata;
2068 }
bbefb8dd
MD
2069
2070 /*
2071 * Open each stream: for each file, try to open, check magic
2072 * number, and get the stream ID to add to the right location in
2073 * the stream array.
bbefb8dd
MD
2074 */
2075
65102a8c 2076 dirent_len = offsetof(struct dirent, d_name) +
46322b33 2077 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 2078
65102a8c 2079 dirent = malloc(dirent_len);
bbefb8dd 2080
65102a8c 2081 for (;;) {
46322b33 2082 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 2083 if (ret) {
3394d22e 2084 fprintf(stderr, "[error] Readdir error.\n");
65102a8c 2085 goto readdir_error;
65102a8c
MD
2086 }
2087 if (!diriter)
2088 break;
d8ea2d29
MD
2089 /* Ignore hidden files, ., .. and metadata. */
2090 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
2091 || !strcmp(diriter->d_name, "..")
2092 || !strcmp(diriter->d_name, "metadata"))
2093 continue;
0ace7505
JD
2094
2095 /* Ignore index files : *.idx */
2096 ext = strrchr(diriter->d_name, '.');
2097 if (ext && (!strcmp(ext, ".idx"))) {
2098 continue;
2099 }
2100
06789ffd
MD
2101 ret = ctf_open_file_stream_read(td, diriter->d_name,
2102 flags, packet_seek);
dc48ecad 2103 if (ret) {
3394d22e 2104 fprintf(stderr, "[error] Open file stream error.\n");
dc48ecad
MD
2105 goto readdir_error;
2106 }
65102a8c 2107 }
bbefb8dd 2108
65102a8c 2109 free(dirent);
bbefb8dd 2110 return 0;
65102a8c
MD
2111
2112readdir_error:
2113 free(dirent);
2114error_metadata:
f824ae04
MD
2115 closeret = close(td->dirfd);
2116 if (closeret) {
2117 perror("Error on fd close");
2118 }
65102a8c 2119error_dirfd:
f824ae04
MD
2120 closeret = closedir(td->dir);
2121 if (closeret) {
2122 perror("Error on closedir");
2123 }
bbefb8dd
MD
2124error:
2125 return ret;
2126}
2127
03798a93
JD
2128/*
2129 * ctf_open_trace: Open a CTF trace and index it.
2130 * Note that the user must seek the trace after the open (using the iterator)
2131 * since the index creation read it entirely.
2132 */
e9378815 2133static
1b8455b7 2134struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1cf393f6 2135 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 2136 int whence), FILE *metadata_fp)
bbefb8dd 2137{
46322b33 2138 struct ctf_trace *td;
bbefb8dd
MD
2139 int ret;
2140
2715de36
MD
2141 /*
2142 * If packet_seek is NULL, we provide our default version.
2143 */
2144 if (!packet_seek)
2145 packet_seek = ctf_packet_seek;
2146
46322b33 2147 td = g_new0(struct ctf_trace, 1);
bbefb8dd 2148
8c572eba 2149 switch (flags & O_ACCMODE) {
bbefb8dd 2150 case O_RDONLY:
b61922b5 2151 if (!path) {
3394d22e 2152 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
b61922b5
MD
2153 goto error;
2154 }
06789ffd 2155 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
bbefb8dd
MD
2156 if (ret)
2157 goto error;
2158 break;
989c73bc 2159 case O_RDWR:
3394d22e 2160 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
46322b33 2161 goto error;
bbefb8dd 2162 default:
3394d22e 2163 fprintf(stderr, "[error] Incorrect open flags.\n");
bbefb8dd
MD
2164 goto error;
2165 }
2166
46322b33 2167 return &td->parent;
bbefb8dd
MD
2168error:
2169 g_free(td);
2170 return NULL;
2171}
2172
2e937fb4 2173static
f571dfb1 2174void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
c150f912 2175 struct bt_mmap_stream *mmap_info)
f571dfb1
JD
2176{
2177 pos->mmap_offset = 0;
2178 pos->packet_size = 0;
2179 pos->content_size = 0;
2180 pos->content_size_loc = NULL;
2181 pos->fd = mmap_info->fd;
aee35fcc 2182 pos->base_mma = NULL;
f571dfb1
JD
2183 pos->offset = 0;
2184 pos->dummy = false;
2185 pos->cur_index = 0;
03798a93
JD
2186 pos->packet_cycles_index = NULL;
2187 pos->packet_real_index = NULL;
f571dfb1
JD
2188 pos->prot = PROT_READ;
2189 pos->flags = MAP_PRIVATE;
2190 pos->parent.rw_table = read_dispatch_table;
2191 pos->parent.event_cb = ctf_read_event;
2192}
2193
2194static
2195int prepare_mmap_stream_definition(struct ctf_trace *td,
2196 struct ctf_file_stream *file_stream)
2197{
f380e105 2198 struct ctf_stream_declaration *stream;
f571dfb1
JD
2199 uint64_t stream_id = 0;
2200 int ret;
2201
2202 file_stream->parent.stream_id = stream_id;
2203 if (stream_id >= td->streams->len) {
3394d22e 2204 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
2205 "in metadata.\n", stream_id);
2206 ret = -EINVAL;
2207 goto end;
2208 }
2209 stream = g_ptr_array_index(td->streams, stream_id);
2210 if (!stream) {
3394d22e 2211 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
2212 "in metadata.\n", stream_id);
2213 ret = -EINVAL;
2214 goto end;
2215 }
2216 file_stream->parent.stream_class = stream;
2217 ret = create_stream_definitions(td, &file_stream->parent);
2218end:
2219 return ret;
2220}
2221
2222static
2223int ctf_open_mmap_stream_read(struct ctf_trace *td,
c150f912 2224 struct bt_mmap_stream *mmap_info,
1cf393f6 2225 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
f571dfb1
JD
2226 int whence))
2227{
2228 int ret;
2229 struct ctf_file_stream *file_stream;
2230
2231 file_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 2232 file_stream->pos.last_offset = LAST_OFFSET_POISON;
f571dfb1
JD
2233 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
2234
06789ffd 2235 file_stream->pos.packet_seek = packet_seek;
f571dfb1
JD
2236
2237 ret = create_trace_definitions(td, &file_stream->parent);
2238 if (ret) {
2239 goto error_def;
2240 }
2241
2242 ret = prepare_mmap_stream_definition(td, file_stream);
2243 if (ret)
2244 goto error_index;
2245
f7bbd502 2246 /*
50052405 2247 * For now, only a single clock per trace is supported.
f7bbd502 2248 */
7ec78969 2249 file_stream->parent.current_clock = td->parent.single_clock;
f7bbd502 2250
f571dfb1
JD
2251 /* Add stream file to stream class */
2252 g_ptr_array_add(file_stream->parent.stream_class->streams,
2253 &file_stream->parent);
2254 return 0;
2255
2256error_index:
2257 if (file_stream->parent.trace_packet_header)
13fad8b6 2258 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
f571dfb1
JD
2259error_def:
2260 g_free(file_stream);
2261 return ret;
2262}
2263
2e937fb4 2264static
f571dfb1 2265int ctf_open_mmap_trace_read(struct ctf_trace *td,
c150f912 2266 struct bt_mmap_stream_list *mmap_list,
1cf393f6 2267 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
f571dfb1
JD
2268 int whence),
2269 FILE *metadata_fp)
2270{
2271 int ret;
c150f912 2272 struct bt_mmap_stream *mmap_info;
f571dfb1 2273
06789ffd 2274 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
f571dfb1
JD
2275 if (ret) {
2276 goto error;
2277 }
2278
2279 /*
2280 * for each stream, try to open, check magic number, and get the
2281 * stream ID to add to the right location in the stream array.
2282 */
3122e6f0 2283 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
06789ffd 2284 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
f571dfb1 2285 if (ret) {
3394d22e 2286 fprintf(stderr, "[error] Open file mmap stream error.\n");
f571dfb1
JD
2287 goto error;
2288 }
2289 }
2290
2291 return 0;
2292
2293error:
2294 return ret;
2295}
2296
2297static
1b8455b7 2298struct bt_trace_descriptor *ctf_open_mmap_trace(
c150f912 2299 struct bt_mmap_stream_list *mmap_list,
1cf393f6 2300 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
20d0dcf9 2301 int whence),
f571dfb1
JD
2302 FILE *metadata_fp)
2303{
2304 struct ctf_trace *td;
2305 int ret;
2306
2307 if (!metadata_fp) {
2308 fprintf(stderr, "[error] No metadata file pointer associated, "
2309 "required for mmap parsing\n");
2310 goto error;
2311 }
06789ffd
MD
2312 if (!packet_seek) {
2313 fprintf(stderr, "[error] packet_seek function undefined.\n");
f571dfb1
JD
2314 goto error;
2315 }
2316 td = g_new0(struct ctf_trace, 1);
06789ffd 2317 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
f571dfb1
JD
2318 if (ret)
2319 goto error_free;
2320
2321 return &td->parent;
2322
2323error_free:
2324 g_free(td);
2325error:
2326 return NULL;
2327}
2328
03798a93 2329static
1b8455b7 2330int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
03798a93
JD
2331{
2332 int i, j, k;
2333 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2334
2335 /* for each stream_class */
2336 for (i = 0; i < td->streams->len; i++) {
2337 struct ctf_stream_declaration *stream_class;
2338
2339 stream_class = g_ptr_array_index(td->streams, i);
2340 if (!stream_class)
2341 continue;
2342 /* for each file_stream */
2343 for (j = 0; j < stream_class->streams->len; j++) {
2344 struct ctf_stream_definition *stream;
2345 struct ctf_stream_pos *stream_pos;
2346 struct ctf_file_stream *cfs;
2347
2348 stream = g_ptr_array_index(stream_class->streams, j);
2349 if (!stream)
2350 continue;
2351 cfs = container_of(stream, struct ctf_file_stream,
2352 parent);
2353 stream_pos = &cfs->pos;
afe9cd4a
JD
2354 if (!stream_pos->packet_cycles_index)
2355 continue;
2356
03798a93
JD
2357 for (k = 0; k < stream_pos->packet_cycles_index->len; k++) {
2358 struct packet_index *index;
2359 struct packet_index new_index;
2360
2361 index = &g_array_index(stream_pos->packet_cycles_index,
2362 struct packet_index, k);
2363 memcpy(&new_index, index,
2364 sizeof(struct packet_index));
2365 new_index.timestamp_begin =
2366 ctf_get_real_timestamp(stream,
2367 index->timestamp_begin);
2368 new_index.timestamp_end =
2369 ctf_get_real_timestamp(stream,
2370 index->timestamp_end);
2371 g_array_append_val(stream_pos->packet_real_index,
2372 new_index);
2373 }
2374 }
2375 }
2376 return 0;
2377}
2378
0f980a35 2379static
f824ae04 2380int ctf_close_file_stream(struct ctf_file_stream *file_stream)
0f980a35 2381{
f824ae04
MD
2382 int ret;
2383
2384 ret = ctf_fini_pos(&file_stream->pos);
2385 if (ret) {
2386 fprintf(stderr, "Error on ctf_fini_pos\n");
2387 return -1;
2388 }
2389 ret = close(file_stream->pos.fd);
2390 if (ret) {
2391 perror("Error closing file fd");
2392 return -1;
2393 }
2394 return 0;
0f980a35
MD
2395}
2396
e9378815 2397static
1b8455b7 2398int ctf_close_trace(struct bt_trace_descriptor *tdp)
bbefb8dd 2399{
46322b33 2400 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
08c82b90 2401 int ret;
0f980a35 2402
46322b33 2403 if (td->streams) {
08c82b90
MD
2404 int i;
2405
46322b33 2406 for (i = 0; i < td->streams->len; i++) {
f380e105 2407 struct ctf_stream_declaration *stream;
0f980a35 2408 int j;
e9378815 2409
46322b33 2410 stream = g_ptr_array_index(td->streams, i);
e9378815
MD
2411 if (!stream)
2412 continue;
2d0bea29 2413 for (j = 0; j < stream->streams->len; j++) {
0f980a35 2414 struct ctf_file_stream *file_stream;
15d4fe3c
JD
2415 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2416 struct ctf_file_stream, parent);
f824ae04
MD
2417 ret = ctf_close_file_stream(file_stream);
2418 if (ret)
2419 return ret;
0f980a35 2420 }
e003ab50 2421 }
e003ab50 2422 }
15d4fe3c 2423 ctf_destroy_metadata(td);
f824ae04
MD
2424 ret = close(td->dirfd);
2425 if (ret) {
2426 perror("Error closing dirfd");
2427 return ret;
2428 }
2429 ret = closedir(td->dir);
2430 if (ret) {
2431 perror("Error closedir");
2432 return ret;
2433 }
7237592a 2434 free(td->metadata_string);
bbefb8dd 2435 g_free(td);
f824ae04 2436 return 0;
bbefb8dd
MD
2437}
2438
98a04903 2439static
1b8455b7 2440void ctf_set_context(struct bt_trace_descriptor *descriptor,
98a04903
JD
2441 struct bt_context *ctx)
2442{
2443 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2444 parent);
2445
45807148 2446 td->parent.ctx = ctx;
98a04903
JD
2447}
2448
2449static
1b8455b7 2450void ctf_set_handle(struct bt_trace_descriptor *descriptor,
98a04903
JD
2451 struct bt_trace_handle *handle)
2452{
2453 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2454 parent);
2455
4d086981 2456 td->parent.handle = handle;
98a04903
JD
2457}
2458
95febab3 2459static
7fb21036 2460void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
2461{
2462 int ret;
2463
4c8bfb7e 2464 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
2465 ret = bt_register_format(&ctf_format);
2466 assert(!ret);
2467}
698f0fe4 2468
95febab3
MD
2469static
2470void __attribute__((destructor)) ctf_exit(void)
2471{
2472 bt_unregister_format(&ctf_format);
2473}
This page took 0.168604 seconds and 4 git commands to generate.