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