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