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