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