Python bindings: PEP8 mandates two blank lines before class
[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>
c40a57e5 38#include <babeltrace/trace-debuginfo.h>
0ace7505 39#include <babeltrace/ctf/ctf-index.h>
0f980a35 40#include <inttypes.h>
b4c19c1e 41#include <stdio.h>
0f980a35 42#include <sys/mman.h>
bbefb8dd 43#include <errno.h>
bbefb8dd 44#include <sys/types.h>
65102a8c 45#include <sys/stat.h>
bbefb8dd 46#include <fcntl.h>
65102a8c 47#include <dirent.h>
bbefb8dd 48#include <glib.h>
65102a8c
MD
49#include <unistd.h>
50#include <stdlib.h>
51
65102a8c
MD
52#include "metadata/ctf-scanner.h"
53#include "metadata/ctf-parser.h"
54#include "metadata/ctf-ast.h"
c34ea0fa 55#include "events-private.h"
68ef7952 56#include <babeltrace/compat/memstream.h>
a323afb2 57#include <babeltrace/compat/fcntl.h>
65102a8c 58
ec323464
MD
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
0f980a35 66/*
ec323464 67 * Lenght of packet to write, in bits.
0f980a35 68 */
8c572eba 69#define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
0f980a35 70
a0fe7d97
MD
71#ifndef min
72#define min(a, b) (((a) < (b)) ? (a) : (b))
73#endif
74
61cf588b 75#define NSEC_PER_SEC 1000000000LL
7d97fad9 76
0ace7505
JD
77#define INDEX_PATH "./index/%s.idx"
78
03798a93 79int opt_clock_cycles,
7d97fad9
MD
80 opt_clock_seconds,
81 opt_clock_date,
82 opt_clock_gmt;
83
61cf588b
MD
84int64_t opt_clock_offset;
85int64_t opt_clock_offset_ns;
7d97fad9 86
65102a8c 87extern int yydebug;
05984e0c 88char *opt_debug_info_dir;
5cde0dc1 89char *opt_debug_info_target_prefix;
bbefb8dd 90
2654fe9b
MD
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
e9378815 98static
1b8455b7 99struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1cf393f6 100 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
20d0dcf9
MD
101 int whence),
102 FILE *metadata_fp);
e9378815 103static
1b8455b7 104struct bt_trace_descriptor *ctf_open_mmap_trace(
c150f912 105 struct bt_mmap_stream_list *mmap_list,
1cf393f6 106 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
20d0dcf9 107 int whence),
f571dfb1 108 FILE *metadata_fp);
98a04903 109static
1b8455b7 110void ctf_set_context(struct bt_trace_descriptor *descriptor,
98a04903
JD
111 struct bt_context *ctx);
112static
1b8455b7 113void ctf_set_handle(struct bt_trace_descriptor *descriptor,
98a04903 114 struct bt_trace_handle *handle);
f571dfb1
JD
115
116static
1b8455b7 117int ctf_close_trace(struct bt_trace_descriptor *descriptor);
30c276af 118static
61cf588b
MD
119int ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
120 struct bt_trace_handle *handle, enum bt_clock_type type,
121 int64_t *timestamp);
30c276af 122static
61cf588b
MD
123int ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
124 struct bt_trace_handle *handle, enum bt_clock_type type,
125 int64_t *timestamp);
03798a93 126static
1b8455b7 127int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp);
fc93b2bd 128
1ae19169
MD
129static
130rw_dispatch read_dispatch_table[] = {
9a19a512
PP
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,
d11e9c49
MD
139};
140
1ae19169
MD
141static
142rw_dispatch write_dispatch_table[] = {
9a19a512
PP
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,
d11e9c49
MD
151};
152
1ae19169 153static
37b99bdb 154struct bt_format ctf_format = {
bbefb8dd 155 .open_trace = ctf_open_trace,
f571dfb1 156 .open_mmap_trace = ctf_open_mmap_trace,
bbefb8dd 157 .close_trace = ctf_close_trace,
98a04903
JD
158 .set_context = ctf_set_context,
159 .set_handle = ctf_set_handle,
30c276af
JD
160 .timestamp_begin = ctf_timestamp_begin,
161 .timestamp_end = ctf_timestamp_end,
03798a93 162 .convert_index_timestamp = ctf_convert_index_timestamp,
fc93b2bd
MD
163};
164
30c276af 165static
61cf588b
MD
166int ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
167 struct bt_trace_handle *handle, enum bt_clock_type type,
168 int64_t *timestamp)
30c276af
JD
169{
170 struct ctf_trace *tin;
61cf588b
MD
171 int64_t begin = LLONG_MAX;
172 int i, j, ret;
30c276af
JD
173
174 tin = container_of(descriptor, struct ctf_trace, parent);
175
61cf588b
MD
176 if (!tin || !timestamp) {
177 ret = -EINVAL;
30c276af 178 goto error;
61cf588b 179 }
30c276af
JD
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);
1b01ffc2
MD
186 if (!stream_class)
187 continue;
30c276af
JD
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
61cf588b
MD
200 if (!stream_pos->packet_index) {
201 ret = -EINVAL;
03798a93 202 goto error;
61cf588b 203 }
03798a93 204
992e8cc0 205 if (stream_pos->packet_index->len <= 0)
afe9cd4a
JD
206 continue;
207
992e8cc0
MD
208 index = &g_array_index(stream_pos->packet_index,
209 struct packet_index,
210 stream_pos->packet_index->len - 1);
03798a93 211 if (type == BT_CLOCK_REAL) {
992e8cc0
MD
212 if (index->ts_real.timestamp_begin < begin)
213 begin = index->ts_real.timestamp_begin;
03798a93 214 } else if (type == BT_CLOCK_CYCLES) {
992e8cc0
MD
215 if (index->ts_cycles.timestamp_begin < begin)
216 begin = index->ts_cycles.timestamp_begin;
03798a93 217 } else {
61cf588b 218 ret = -EINVAL;
03798a93
JD
219 goto error;
220 }
30c276af
JD
221 }
222 }
61cf588b
MD
223 if (begin == LLONG_MAX) {
224 ret = -ENOENT;
225 goto error;
226 }
227 *timestamp = begin;
228 return 0;
30c276af
JD
229
230error:
61cf588b 231 return ret;
30c276af
JD
232}
233
234static
61cf588b
MD
235int ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
236 struct bt_trace_handle *handle, enum bt_clock_type type,
237 int64_t *timestamp)
30c276af
JD
238{
239 struct ctf_trace *tin;
61cf588b
MD
240 int64_t end = LLONG_MIN;
241 int i, j, ret;
30c276af
JD
242
243 tin = container_of(descriptor, struct ctf_trace, parent);
244
61cf588b
MD
245 if (!tin || !timestamp) {
246 ret = -EINVAL;
30c276af 247 goto error;
61cf588b 248 }
30c276af
JD
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);
1b01ffc2
MD
255 if (!stream_class)
256 continue;
30c276af
JD
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
61cf588b
MD
269 if (!stream_pos->packet_index) {
270 ret = -EINVAL;
03798a93 271 goto error;
61cf588b 272 }
03798a93 273
992e8cc0 274 if (stream_pos->packet_index->len <= 0)
afe9cd4a
JD
275 continue;
276
992e8cc0
MD
277 index = &g_array_index(stream_pos->packet_index,
278 struct packet_index,
279 stream_pos->packet_index->len - 1);
03798a93 280 if (type == BT_CLOCK_REAL) {
992e8cc0
MD
281 if (index->ts_real.timestamp_end > end)
282 end = index->ts_real.timestamp_end;
03798a93 283 } else if (type == BT_CLOCK_CYCLES) {
992e8cc0
MD
284 if (index->ts_cycles.timestamp_end > end)
285 end = index->ts_cycles.timestamp_end;
03798a93 286 } else {
61cf588b 287 ret = -EINVAL;
03798a93
JD
288 goto error;
289 }
30c276af
JD
290 }
291 }
61cf588b
MD
292 if (end == LLONG_MIN) {
293 ret = -ENOENT;
294 goto error;
295 }
296 *timestamp = end;
297 return 0;
30c276af
JD
298
299error:
61cf588b 300 return ret;
30c276af
JD
301}
302
25ccc85b 303/*
03798a93 304 * Update stream current timestamp
25ccc85b 305 */
2b9a764d 306static
9e88d150 307void ctf_update_timestamp(struct ctf_stream_definition *stream,
2b9a764d
MD
308 struct definition_integer *integer_definition)
309{
310 struct declaration_integer *integer_declaration =
311 integer_definition->declaration;
312 uint64_t oldval, newval, updateval;
313
7f3f572b 314 if (unlikely(integer_declaration->len == 64)) {
03798a93 315 stream->cycles_timestamp = integer_definition->value._unsigned;
03798a93
JD
316 stream->real_timestamp = ctf_get_real_timestamp(stream,
317 stream->cycles_timestamp);
2b9a764d
MD
318 return;
319 }
320 /* keep low bits */
03798a93 321 oldval = stream->cycles_timestamp;
2b9a764d
MD
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) */
03798a93 328 updateval = stream->cycles_timestamp;
2b9a764d
MD
329 updateval &= ~((1ULL << integer_declaration->len) - 1);
330 updateval += newval;
03798a93
JD
331 stream->cycles_timestamp = updateval;
332
333 /* convert to real timestamp */
03798a93
JD
334 stream->real_timestamp = ctf_get_real_timestamp(stream,
335 stream->cycles_timestamp);
2b9a764d
MD
336}
337
25ccc85b
MD
338/*
339 * Print timestamp, rescaling clock frequency to nanoseconds and
340 * applying offsets as needed (unix time).
341 */
2e937fb4 342static
03798a93 343void ctf_print_timestamp_real(FILE *fp,
9e88d150 344 struct ctf_stream_definition *stream,
61cf588b 345 int64_t timestamp)
7d97fad9 346{
61cf588b
MD
347 int64_t ts_sec = 0, ts_nsec;
348 uint64_t ts_sec_abs, ts_nsec_abs;
349 bool is_negative;
7d97fad9 350
03798a93 351 ts_nsec = timestamp;
7d97fad9 352
61cf588b 353 /* Add command-line offset in ns */
65923160
IJ
354 ts_nsec += opt_clock_offset_ns;
355
c34ea0fa 356 /* Add command-line offset */
7d97fad9
MD
357 ts_sec += opt_clock_offset;
358
359 ts_sec += ts_nsec / NSEC_PER_SEC;
360 ts_nsec = ts_nsec % NSEC_PER_SEC;
61cf588b
MD
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 }
7d97fad9
MD
386
387 if (!opt_clock_seconds) {
388 struct tm tm;
61cf588b
MD
389 time_t time_s = (time_t) ts_sec_abs;
390
391 if (is_negative) {
a0960c1c 392 fprintf(stderr, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
61cf588b
MD
393 goto seconds;
394 }
7d97fad9
MD
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,
61cf588b 428 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec_abs);
7d97fad9
MD
429 goto end;
430 }
431seconds:
61cf588b
MD
432 fprintf(fp, "%s%" PRId64 ".%09" PRIu64,
433 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
7d97fad9
MD
434
435end:
436 return;
437}
438
03798a93
JD
439/*
440 * Print timestamp, in cycles
441 */
2e937fb4 442static
03798a93
JD
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,
61cf588b 452 int64_t timestamp)
03798a93
JD
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
87148dc7
MD
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
2654fe9b
MD
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 ]
6246fd54
JD
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.
2654fe9b
MD
485 */
486static
6246fd54 487void ctf_print_discarded_lost(FILE *fp, struct ctf_stream_definition *stream)
87148dc7 488{
6246fd54
JD
489 if ((!stream->events_discarded && !stream->packets_lost) ||
490 !babeltrace_ctf_console_output) {
2654fe9b
MD
491 return;
492 }
493 fflush(stdout);
6246fd54
JD
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 }
87148dc7
MD
501 if (opt_clock_cycles) {
502 ctf_print_timestamp(fp, stream,
2654fe9b 503 stream->prev.cycles.end);
87148dc7
MD
504 fprintf(fp, "] and [");
505 ctf_print_timestamp(fp, stream,
2654fe9b 506 stream->current.cycles.end);
87148dc7
MD
507 } else {
508 ctf_print_timestamp(fp, stream,
2654fe9b 509 stream->prev.real.end);
87148dc7
MD
510 fprintf(fp, "] and [");
511 ctf_print_timestamp(fp, stream,
2654fe9b 512 stream->current.real.end);
87148dc7
MD
513 }
514 fprintf(fp, "] in trace UUID ");
515 print_uuid(fp, stream->stream_class->trace->uuid);
caf929fa 516 if (stream->stream_class->trace->parent.path[0])
87148dc7 517 fprintf(fp, ", at path: \"%s\"",
caf929fa 518 stream->stream_class->trace->parent.path);
87148dc7
MD
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
31262354 529static
1cf393f6 530int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream)
31262354
MD
531{
532 struct ctf_stream_pos *pos =
533 container_of(ppos, struct ctf_stream_pos, parent);
f380e105 534 struct ctf_stream_declaration *stream_class = stream->stream_class;
c716f83b 535 struct ctf_event_definition *event;
31262354 536 uint64_t id = 0;
31262354
MD
537 int ret;
538
3abe83c7
MD
539 /* We need to check for EOF here for empty files. */
540 if (unlikely(pos->offset == EOF))
541 return EOF;
542
5f643ed7
MD
543 ctf_pos_get_event(pos);
544
90fcbacc
JD
545 /* save the current position as a restore point */
546 pos->last_offset = pos->offset;
90fcbacc 547
3abe83c7
MD
548 /*
549 * This is the EOF check after we've advanced the position in
550 * ctf_pos_get_event.
551 */
7f3f572b 552 if (unlikely(pos->offset == EOF))
31262354 553 return EOF;
8793d9f8 554
500634be
JD
555 /* Stream is inactive for now (live reading). */
556 if (unlikely(pos->content_size == 0))
8793d9f8 557 return EAGAIN;
500634be
JD
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))
a5260047
JD
565 return EAGAIN;
566
5f643ed7 567 assert(pos->offset < pos->content_size);
31262354
MD
568
569 /* Read event header */
7f3f572b 570 if (likely(stream->stream_event_header)) {
a35173fe 571 struct definition_integer *integer_definition;
0d69b916 572 struct bt_definition *variant;
a35173fe 573
e28d4618 574 ret = generic_rw(ppos, &stream->stream_event_header->p);
7f3f572b 575 if (unlikely(ret))
31262354
MD
576 goto error;
577 /* lookup event id */
bf78e2cf 578 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
579 if (integer_definition) {
580 id = integer_definition->value._unsigned;
581 } else {
582 struct definition_enum *enum_definition;
583
9e3274b0 584 enum_definition = bt_lookup_enum(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
585 if (enum_definition) {
586 id = enum_definition->integer->value._unsigned;
587 }
31262354 588 }
764af3f4 589
ebae302b 590 variant = bt_lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e 591 if (variant) {
bf78e2cf 592 integer_definition = bt_lookup_integer(variant, "id", FALSE);
ccdb988e
MD
593 if (integer_definition) {
594 id = integer_definition->value._unsigned;
595 }
596 }
c87a8eb2 597 stream->event_id = id;
ccdb988e 598
764af3f4 599 /* lookup timestamp */
5e2eb0ae 600 stream->has_timestamp = 0;
bf78e2cf 601 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
a35173fe 602 if (integer_definition) {
2b9a764d 603 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 604 stream->has_timestamp = 1;
a35173fe 605 } else {
ccdb988e 606 if (variant) {
bf78e2cf 607 integer_definition = bt_lookup_integer(variant, "timestamp", FALSE);
a35173fe 608 if (integer_definition) {
2b9a764d 609 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 610 stream->has_timestamp = 1;
a35173fe
MD
611 }
612 }
764af3f4 613 }
31262354
MD
614 }
615
616 /* Read stream-declared event context */
e28d4618
MD
617 if (stream->stream_event_context) {
618 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
619 if (ret)
620 goto error;
621 }
622
7f3f572b 623 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 624 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
625 return -EINVAL;
626 }
e28d4618 627 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 628 if (unlikely(!event)) {
3394d22e 629 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
630 return -EINVAL;
631 }
632
633 /* Read event-declared event context */
e28d4618
MD
634 if (event->event_context) {
635 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
636 if (ret)
637 goto error;
638 }
639
640 /* Read event payload */
7f3f572b 641 if (likely(event->event_fields)) {
e28d4618 642 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
643 if (ret)
644 goto error;
645 }
646
28f35f0a
MD
647 if (pos->last_offset == pos->offset) {
648 fprintf(stderr, "[error] Invalid 0 byte event encountered.\n");
649 return -EINVAL;
650 }
651
31262354
MD
652 return 0;
653
654error:
64f5abe1 655 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
656 return ret;
657}
658
659static
1cf393f6 660int ctf_write_event(struct bt_stream_pos *pos, struct ctf_stream_definition *stream)
31262354 661{
f380e105 662 struct ctf_stream_declaration *stream_class = stream->stream_class;
c716f83b 663 struct ctf_event_definition *event;
c87a8eb2 664 uint64_t id;
31262354
MD
665 int ret;
666
c87a8eb2
MD
667 id = stream->event_id;
668
31262354 669 /* print event header */
7f3f572b 670 if (likely(stream->stream_event_header)) {
e28d4618 671 ret = generic_rw(pos, &stream->stream_event_header->p);
31262354
MD
672 if (ret)
673 goto error;
674 }
675
676 /* print stream-declared event context */
e28d4618
MD
677 if (stream->stream_event_context) {
678 ret = generic_rw(pos, &stream->stream_event_context->p);
31262354
MD
679 if (ret)
680 goto error;
681 }
682
7f3f572b 683 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 684 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
685 return -EINVAL;
686 }
e28d4618 687 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 688 if (unlikely(!event)) {
3394d22e 689 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
690 return -EINVAL;
691 }
692
693 /* print event-declared event context */
e28d4618
MD
694 if (event->event_context) {
695 ret = generic_rw(pos, &event->event_context->p);
31262354
MD
696 if (ret)
697 goto error;
698 }
699
700 /* Read and print event payload */
7f3f572b 701 if (likely(event->event_fields)) {
e28d4618 702 ret = generic_rw(pos, &event->event_fields->p);
31262354
MD
703 if (ret)
704 goto error;
705 }
706
707 return 0;
708
709error:
3394d22e 710 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
711 return ret;
712}
713
e69dd258
JD
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
ca334c72
MD
815int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
816 int fd, int open_flags)
8c572eba
MD
817{
818 pos->fd = fd;
37fcdd19 819 if (fd >= 0) {
992e8cc0 820 pos->packet_index = g_array_new(FALSE, TRUE,
37fcdd19
JD
821 sizeof(struct packet_index));
822 } else {
992e8cc0 823 pos->packet_index = NULL;
37fcdd19 824 }
8563e754
MD
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;
31262354 830 pos->parent.event_cb = ctf_read_event;
ca334c72 831 pos->parent.trace = trace;
8563e754 832 break;
8563e754 833 case O_RDWR:
c88c09f9 834 pos->prot = PROT_READ | PROT_WRITE;
8563e754
MD
835 pos->flags = MAP_SHARED;
836 pos->parent.rw_table = write_dispatch_table;
31262354 837 pos->parent.event_cb = ctf_write_event;
ca334c72 838 pos->parent.trace = trace;
8563e754
MD
839 break;
840 default:
841 assert(0);
8c572eba 842 }
f824ae04 843 return 0;
8c572eba
MD
844}
845
f824ae04 846int ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba 847{
c88c09f9 848 if ((pos->prot & PROT_WRITE) && pos->content_size_loc)
8c572eba 849 *pos->content_size_loc = pos->offset;
aee35fcc 850 if (pos->base_mma) {
08c82b90
MD
851 int ret;
852
8c572eba 853 /* unmap old base */
aee35fcc 854 ret = munmap_align(pos->base_mma);
8c572eba 855 if (ret) {
3394d22e 856 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
8c572eba 857 strerror(errno));
f824ae04 858 return -1;
8c572eba
MD
859 }
860 }
992e8cc0
MD
861 if (pos->packet_index)
862 (void) g_array_free(pos->packet_index, TRUE);
f824ae04 863 return 0;
8c572eba
MD
864}
865
f1f52630
MD
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;
6246fd54 871 uint64_t packets_lost_diff = 0;
f1f52630
MD
872
873 /* Update packet index time information */
2654fe9b
MD
874
875 /* Current packet begin/end */
876 stream->current.real.begin =
877 cur_index->ts_real.timestamp_begin;
878 stream->current.cycles.begin =
f1f52630 879 cur_index->ts_cycles.timestamp_begin;
2654fe9b 880 stream->current.real.end =
f1f52630 881 cur_index->ts_real.timestamp_end;
2654fe9b
MD
882 stream->current.cycles.end =
883 cur_index->ts_cycles.timestamp_end;
f1f52630
MD
884
885 /* Update packet index discarded event information */
886 events_discarded_diff = cur_index->events_discarded;
887 if (prev_index) {
2654fe9b
MD
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
f1f52630 898 events_discarded_diff -= prev_index->events_discarded;
6246fd54
JD
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 }
f1f52630
MD
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 }
2654fe9b
MD
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;
f1f52630
MD
922 }
923 stream->events_discarded = events_discarded_diff;
6246fd54 924 stream->packets_lost = packets_lost_diff;
f1f52630
MD
925}
926
7da9b2f3
JD
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
20d0dcf9
MD
1025/*
1026 * for SEEK_CUR: go to next packet.
34b8fff6 1027 * for SEEK_SET: go to packet numer (index).
20d0dcf9 1028 */
1cf393f6 1029void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
0f980a35 1030{
d6425aaf
MD
1031 struct ctf_stream_pos *pos =
1032 container_of(stream_pos, struct ctf_stream_pos, parent);
75cc2c35
MD
1033 struct ctf_file_stream *file_stream =
1034 container_of(pos, struct ctf_file_stream, pos);
0f980a35 1035 int ret;
2654fe9b 1036 struct packet_index *packet_index, *prev_index;
0f980a35 1037
5bfcad93
MD
1038 switch (whence) {
1039 case SEEK_CUR:
1040 case SEEK_SET: /* Fall-through */
1041 break; /* OK */
1042 default:
1043 assert(0);
1044 }
1045
c88c09f9 1046 if ((pos->prot & PROT_WRITE) && pos->content_size_loc)
8c572eba 1047 *pos->content_size_loc = pos->offset;
0f980a35 1048
aee35fcc 1049 if (pos->base_mma) {
0f980a35 1050 /* unmap old base */
aee35fcc 1051 ret = munmap_align(pos->base_mma);
0f980a35 1052 if (ret) {
3394d22e 1053 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
1054 strerror(errno));
1055 assert(0);
1056 }
aee35fcc 1057 pos->base_mma = NULL;
0f980a35
MD
1058 }
1059
8c572eba 1060 /*
46322b33 1061 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
1062 * except to get exactly at the beginning of the next packet.
1063 */
c88c09f9 1064 if (pos->prot & PROT_WRITE) {
989c73bc
MD
1065 switch (whence) {
1066 case SEEK_CUR:
1067 /* The writer will add padding */
e0a5b455 1068 pos->mmap_offset += pos->packet_size / CHAR_BIT;
989c73bc
MD
1069 break;
1070 case SEEK_SET:
20d0dcf9 1071 assert(index == 0); /* only seek supported for now */
989c73bc
MD
1072 pos->cur_index = 0;
1073 break;
1074 default:
1075 assert(0);
1076 }
8c572eba
MD
1077 pos->content_size = -1U; /* Unknown at this point */
1078 pos->packet_size = WRITE_PACKET_LEN;
d9548894
MD
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);
847bf71a 1084 pos->offset = 0;
8c572eba 1085 } else {
5f643ed7 1086 read_next_packet:
847bf71a
MD
1087 switch (whence) {
1088 case SEEK_CUR:
41e82e00 1089 {
7d97fad9
MD
1090 if (pos->offset == EOF) {
1091 return;
1092 }
992e8cc0 1093 assert(pos->cur_index < pos->packet_index->len);
847bf71a 1094 /* The reader will expect us to skip padding */
8c572eba 1095 ++pos->cur_index;
847bf71a 1096 break;
41e82e00 1097 }
847bf71a 1098 case SEEK_SET:
992e8cc0 1099 if (index >= pos->packet_index->len) {
c309df1c
MD
1100 pos->offset = EOF;
1101 return;
1102 }
20d0dcf9 1103 pos->cur_index = index;
847bf71a
MD
1104 break;
1105 default:
1106 assert(0);
1107 }
2654fe9b 1108
024ffe47
MD
1109 if (pos->cur_index >= pos->packet_index->len) {
1110 pos->offset = EOF;
1111 return;
1112 }
1113
2654fe9b
MD
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
2654fe9b
MD
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) {
6246fd54 1133 ctf_print_discarded_lost(stderr, &file_stream->parent);
2654fe9b
MD
1134 }
1135
992e8cc0 1136 packet_index = &g_array_index(pos->packet_index,
03798a93
JD
1137 struct packet_index,
1138 pos->cur_index);
992e8cc0 1139 file_stream->parent.cycles_timestamp = packet_index->ts_cycles.timestamp_begin;
03798a93 1140
992e8cc0 1141 file_stream->parent.real_timestamp = packet_index->ts_real.timestamp_begin;
8c572eba
MD
1142
1143 /* Lookup context/packet size in index */
e69dd258
JD
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 }
20d0dcf9
MD
1150 pos->content_size = packet_index->content_size;
1151 pos->packet_size = packet_index->packet_size;
e69dd258 1152 pos->mmap_offset = packet_index->offset;
500634be
JD
1153 pos->data_offset = packet_index->data_offset;
1154 if (pos->data_offset < packet_index->content_size) {
75cc2c35 1155 pos->offset = 0; /* will read headers */
500634be 1156 } else if (pos->data_offset == packet_index->content_size) {
5f643ed7 1157 /* empty packet */
20d0dcf9 1158 pos->offset = packet_index->data_offset;
3abe83c7 1159 whence = SEEK_CUR;
5f643ed7 1160 goto read_next_packet;
7eda6fc7 1161 } else {
2b9a764d
MD
1162 pos->offset = EOF;
1163 return;
1164 }
8c572eba 1165 }
0f980a35 1166 /* map new base. Need mapping length from header. */
aee35fcc
MD
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) {
3394d22e 1170 fprintf(stderr, "[error] mmap error %s.\n",
847bf71a
MD
1171 strerror(errno));
1172 assert(0);
1173 }
75cc2c35
MD
1174
1175 /* update trace_packet_header and stream_packet_context */
c88c09f9
JG
1176 if (!(pos->prot & PROT_WRITE) &&
1177 file_stream->parent.trace_packet_header) {
75cc2c35 1178 /* Read packet header */
2d0bea29 1179 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
75cc2c35
MD
1180 assert(!ret);
1181 }
c88c09f9
JG
1182 if (!(pos->prot & PROT_WRITE) &&
1183 file_stream->parent.stream_packet_context) {
75cc2c35 1184 /* Read packet context */
2d0bea29 1185 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
75cc2c35
MD
1186 assert(!ret);
1187 }
0f980a35
MD
1188}
1189
b4c19c1e
MD
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);
a0fe7d97 1198 if (len != 1) {
b4c19c1e
MD
1199 goto end;
1200 }
1201 if (magic == TSDL_MAGIC) {
1202 ret = 1;
1203 td->byte_order = BYTE_ORDER;
0d336fdf 1204 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
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;
0d336fdf 1209 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
1210 }
1211end:
1212 rewind(fp);
1213 return ret;
1214}
1215
5c262147
MD
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;
9a19a512 1232
5c262147
MD
1233 }
1234
1235 /* eventually return an error instead of warning */
1236warning:
3394d22e 1237 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
5c262147
MD
1238 major, minor);
1239 return 0;
1240}
1241
b4c19c1e 1242static
0c880b0a 1243int ctf_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
b4c19c1e
MD
1244 FILE *out)
1245{
1246 struct metadata_packet_header header;
a0fe7d97 1247 size_t readlen, writelen, toread;
f8254867 1248 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
b4c19c1e
MD
1249 int ret = 0;
1250
a91a962e 1251 readlen = fread(&header, header_sizeof(header), 1, in);
a0fe7d97 1252 if (readlen < 1)
b4c19c1e
MD
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)
3394d22e 1262 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
b4c19c1e 1263 if (header.compression_scheme) {
3394d22e 1264 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
b4c19c1e
MD
1265 header.compression_scheme);
1266 return -EINVAL;
1267 }
1268 if (header.encryption_scheme) {
3394d22e 1269 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
b4c19c1e
MD
1270 header.encryption_scheme);
1271 return -EINVAL;
1272 }
1273 if (header.checksum_scheme) {
3394d22e 1274 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
b4c19c1e
MD
1275 header.checksum_scheme);
1276 return -EINVAL;
1277 }
5c262147
MD
1278 if (check_version(header.major, header.minor) < 0)
1279 return -EINVAL;
b4c19c1e
MD
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 {
19dd40db 1284 if (bt_uuid_compare(header.uuid, td->uuid))
b4c19c1e
MD
1285 return -EINVAL;
1286 }
1287
b1ccd079
MD
1288 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
1289 return -EINVAL;
1290
255b2138 1291 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
a0fe7d97
MD
1292
1293 for (;;) {
f8254867 1294 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
b4c19c1e
MD
1295 if (ferror(in)) {
1296 ret = -EINVAL;
1297 break;
1298 }
4152822b 1299 if (babeltrace_debug) {
f8254867 1300 buf[readlen] = '\0';
3394d22e 1301 fprintf(stderr, "[debug] metadata packet read: %s\n",
4152822b
MD
1302 buf);
1303 }
1304
b4c19c1e
MD
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 }
a0fe7d97
MD
1314 toread -= readlen;
1315 if (!toread) {
7f4b5c4d 1316 ret = 0; /* continue reading next packet */
ddbc52af 1317 goto read_padding;
a0fe7d97 1318 }
b4c19c1e
MD
1319 }
1320 return ret;
ddbc52af
MD
1321
1322read_padding:
1323 toread = (header.packet_size - header.content_size) / CHAR_BIT;
1324 ret = fseek(in, toread, SEEK_CUR);
1325 if (ret < 0) {
3394d22e 1326 fprintf(stderr, "[warning] Missing padding at end of file\n");
ddbc52af
MD
1327 ret = 0;
1328 }
1329 return ret;
b4c19c1e
MD
1330}
1331
1332static
0c880b0a 1333int ctf_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
b4c19c1e
MD
1334 char **buf)
1335{
1336 FILE *in, *out;
abc40d24 1337 size_t size, buflen;
b4c19c1e
MD
1338 int ret;
1339
1340 in = *fp;
c4f5487e
MD
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 */
f8370579 1346 out = babeltrace_open_memstream(buf, &size);
a569a564
MD
1347 if (out == NULL) {
1348 perror("Metadata open_memstream");
b4c19c1e 1349 return -errno;
a569a564 1350 }
b4c19c1e 1351 for (;;) {
0c880b0a 1352 ret = ctf_trace_metadata_packet_read(td, in, out);
7f4b5c4d 1353 if (ret) {
b4c19c1e 1354 break;
7f4b5c4d
MD
1355 }
1356 if (feof(in)) {
1357 ret = 0;
b4c19c1e
MD
1358 break;
1359 }
1360 }
f8370579
MD
1361 /* close to flush the buffer */
1362 ret = babeltrace_close_memstream(buf, &size, out);
1363 if (ret < 0) {
f824ae04
MD
1364 int closeret;
1365
f8370579 1366 perror("babeltrace_flush_memstream");
f824ae04
MD
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");
f8370579 1377 }
b4c19c1e 1378 /* open for reading */
abc40d24
MD
1379 buflen = strlen(*buf);
1380 if (!buflen) {
1381 *fp = NULL;
493330cb 1382 return -ENOENT;
abc40d24
MD
1383 }
1384 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
a569a564
MD
1385 if (!*fp) {
1386 perror("Metadata fmemopen");
1387 return -errno;
1388 }
b4c19c1e
MD
1389 return 0;
1390}
1391
65102a8c 1392static
0c880b0a
MD
1393int ctf_trace_metadata_read(struct ctf_trace *td, FILE *metadata_fp,
1394 struct ctf_scanner *scanner, int append)
65102a8c 1395{
2d0bea29 1396 struct ctf_file_stream *metadata_stream;
65102a8c 1397 FILE *fp;
b4c19c1e 1398 char *buf = NULL;
f824ae04 1399 int ret = 0, closeret;
65102a8c 1400
2d0bea29 1401 metadata_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 1402 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
b086c01a 1403
ae23d232
JD
1404 if (metadata_fp) {
1405 fp = metadata_fp;
bcbfb8bf 1406 metadata_stream->pos.fd = -1;
ae23d232
JD
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) {
3394d22e 1411 fprintf(stderr, "Unable to open metadata.\n");
f824ae04
MD
1412 ret = -1;
1413 goto end_free;
ae23d232 1414 }
65102a8c 1415
ae23d232
JD
1416 fp = fdopen(metadata_stream->pos.fd, "r");
1417 if (!fp) {
3394d22e 1418 fprintf(stderr, "[error] Unable to open metadata stream.\n");
a569a564 1419 perror("Metadata stream open");
ae23d232
JD
1420 ret = -errno;
1421 goto end_stream;
1422 }
f824ae04
MD
1423 /* fd now belongs to fp */
1424 metadata_stream->pos.fd = -1;
ae23d232 1425 }
65102a8c
MD
1426 if (babeltrace_debug)
1427 yydebug = 1;
1428
b4c19c1e 1429 if (packet_metadata(td, fp)) {
0c880b0a 1430 ret = ctf_trace_metadata_stream_read(td, &fp, &buf);
abc40d24 1431 if (ret) {
6514b4af 1432 goto end;
abc40d24 1433 }
7237592a
MD
1434 td->metadata_string = buf;
1435 td->metadata_packetized = 1;
da75b0f7 1436 } else {
0c880b0a
MD
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);
5c262147 1452 }
b4c19c1e
MD
1453 }
1454
cb2f43ee 1455 ret = ctf_scanner_append_ast(scanner, fp);
65102a8c 1456 if (ret) {
3394d22e 1457 fprintf(stderr, "[error] Error creating AST\n");
65102a8c
MD
1458 goto end;
1459 }
1460
1461 if (babeltrace_debug) {
3394d22e 1462 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
65102a8c 1463 if (ret) {
3394d22e 1464 fprintf(stderr, "[error] Error visiting AST for XML output\n");
65102a8c
MD
1465 goto end;
1466 }
1467 }
1468
3394d22e 1469 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
65102a8c 1470 if (ret) {
3394d22e 1471 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
1472 goto end;
1473 }
3394d22e 1474 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
ac5c6ca0 1475 td, td->byte_order);
65102a8c 1476 if (ret) {
3394d22e 1477 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
1478 goto end;
1479 }
1480end:
f824ae04
MD
1481 if (fp) {
1482 closeret = fclose(fp);
1483 if (closeret) {
1484 perror("Error on fclose");
1485 }
1486 }
65102a8c 1487end_stream:
f824ae04
MD
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:
2d0bea29
MD
1495 if (ret)
1496 g_free(metadata_stream);
0f980a35
MD
1497 return ret;
1498}
1499
e28d4618 1500static
c716f83b 1501struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
9e88d150 1502 struct ctf_stream_definition *stream,
4716614a 1503 struct ctf_event_declaration *event)
e28d4618 1504{
c716f83b 1505 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
e28d4618
MD
1506
1507 if (event->context_decl) {
0d69b916 1508 struct bt_definition *definition =
e28d4618
MD
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 }
42dc00b7 1514 stream_event->event_context = container_of(definition,
e28d4618 1515 struct definition_struct, p);
42dc00b7 1516 stream->parent_def_scope = stream_event->event_context->p.scope;
e28d4618
MD
1517 }
1518 if (event->fields_decl) {
0d69b916 1519 struct bt_definition *definition =
e28d4618
MD
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 }
42dc00b7 1525 stream_event->event_fields = container_of(definition,
e28d4618 1526 struct definition_struct, p);
42dc00b7 1527 stream->parent_def_scope = stream_event->event_fields->p.scope;
e28d4618 1528 }
d3ded99d 1529 stream_event->stream = stream;
42dc00b7 1530 return stream_event;
e28d4618
MD
1531
1532error:
42dc00b7 1533 if (stream_event->event_fields)
13fad8b6 1534 bt_definition_unref(&stream_event->event_fields->p);
42dc00b7 1535 if (stream_event->event_context)
13fad8b6 1536 bt_definition_unref(&stream_event->event_context->p);
888ec52a
MD
1537 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1538 g_quark_to_string(event->name));
e28d4618
MD
1539 return NULL;
1540}
1541
0c880b0a
MD
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
e28d4618 1572static
9e88d150 1573int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618 1574{
f380e105 1575 struct ctf_stream_declaration *stream_class;
e28d4618
MD
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) {
0d69b916 1585 struct bt_definition *definition =
e28d4618
MD
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) {
0d69b916 1597 struct bt_definition *definition =
e28d4618
MD
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) {
0d69b916 1609 struct bt_definition *definition =
e28d4618
MD
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();
0c880b0a
MD
1621 ret = copy_event_declarations_stream_class_to_stream(td,
1622 stream_class, stream);
1623 if (ret)
1624 goto error_event;
e28d4618
MD
1625 return 0;
1626
1627error_event:
1628 for (i = 0; i < stream->events_by_id->len; i++) {
c716f83b 1629 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
42dc00b7
MD
1630 if (stream_event)
1631 g_free(stream_event);
e28d4618
MD
1632 }
1633 g_ptr_array_free(stream->events_by_id, TRUE);
1634error:
1635 if (stream->stream_event_context)
13fad8b6 1636 bt_definition_unref(&stream->stream_event_context->p);
e28d4618 1637 if (stream->stream_event_header)
13fad8b6 1638 bt_definition_unref(&stream->stream_event_header->p);
e28d4618 1639 if (stream->stream_packet_context)
13fad8b6 1640 bt_definition_unref(&stream->stream_packet_context->p);
888ec52a
MD
1641 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1642 stream_class->stream_id, strerror(-ret));
e28d4618
MD
1643 return ret;
1644}
1645
5bfcad93
MD
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
0f980a35 1671static
ec323464
MD
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)
0f980a35 1676{
ec323464 1677 struct packet_index packet_index;
ec323464 1678 uint64_t stream_id = 0;
653906a4 1679 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
ec323464 1680 int first_packet = 0;
653906a4 1681 int len_index;
0f980a35
MD
1682 int ret;
1683
ec323464 1684begin:
c2e0c69b 1685 memset(&packet_index, 0, sizeof(packet_index));
ec323464
MD
1686 if (!pos->mmap_offset) {
1687 first_packet = 1;
1688 }
8895362d 1689
ec323464
MD
1690 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1691 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1692 }
0f980a35 1693
ec323464
MD
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;
0f980a35 1701 }
ec323464
MD
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;
ec323464
MD
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;
888ec52a 1725 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
ec323464
MD
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,
992e8cc0 1738 file_stream->pos.packet_index->len,
ec323464
MD
1739 (ssize_t) pos->mmap_offset);
1740 return -EINVAL;
0f980a35 1741 }
ec323464 1742 }
0f980a35 1743
ec323464
MD
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];
0f980a35 1751
ec323464 1752 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
9a19a512 1753 assert(field->declaration->id == BT_CTF_TYPE_ID_ARRAY);
ec323464
MD
1754 defarray = container_of(field, struct definition_array, p);
1755 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
0f980a35 1756
ec323464
MD
1757 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1758 struct bt_definition *elem;
0f980a35 1759
ec323464
MD
1760 elem = bt_array_index(defarray, i);
1761 uuidval[i] = bt_get_unsigned_int(elem);
0f980a35 1762 }
19dd40db 1763 ret = bt_uuid_compare(td->uuid, uuidval);
ec323464
MD
1764 if (ret) {
1765 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1766 return -EINVAL;
1767 }
1768 }
0f980a35 1769
ec323464
MD
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;
0f980a35 1773
ec323464
MD
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);
0f980a35 1776 }
ec323464 1777 }
0f980a35 1778
ec323464
MD
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) {
5bfcad93 1786 ret = stream_assign_class(td, file_stream, stream_id);
ec323464
MD
1787 if (ret)
1788 return ret;
1789 }
dc48ecad 1790
ec323464
MD
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;
888ec52a 1797 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
ec323464
MD
1798 return ret;
1799 }
95b34f38
MD
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"));
ec323464
MD
1802 if (len_index >= 0) {
1803 struct bt_definition *field;
dc48ecad 1804
ec323464 1805 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
95b34f38 1806 packet_index.packet_size = bt_get_unsigned_int(field);
ec323464
MD
1807 } else {
1808 /* Use file size for packet size */
95b34f38 1809 packet_index.packet_size = filesize * CHAR_BIT;
ec323464 1810 }
75cc2c35 1811
95b34f38
MD
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"));
ec323464
MD
1814 if (len_index >= 0) {
1815 struct bt_definition *field;
75cc2c35 1816
ec323464 1817 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
95b34f38 1818 packet_index.content_size = bt_get_unsigned_int(field);
ec323464 1819 } else {
95b34f38
MD
1820 /* Use packet size if non-zero, else file size */
1821 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
ec323464 1822 }
41e82e00 1823
ec323464
MD
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;
41e82e00 1828
ec323464 1829 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
992e8cc0 1830 packet_index.ts_cycles.timestamp_begin = bt_get_unsigned_int(field);
4c62e2d8 1831 if (file_stream->parent.stream_class->trace->parent.collection) {
992e8cc0 1832 packet_index.ts_real.timestamp_begin =
ec323464
MD
1833 ctf_get_real_timestamp(
1834 &file_stream->parent,
992e8cc0 1835 packet_index.ts_cycles.timestamp_begin);
41e82e00 1836 }
0f980a35 1837 }
546293fa 1838
ec323464
MD
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);
992e8cc0 1845 packet_index.ts_cycles.timestamp_end = bt_get_unsigned_int(field);
4c62e2d8 1846 if (file_stream->parent.stream_class->trace->parent.collection) {
992e8cc0 1847 packet_index.ts_real.timestamp_end =
ec323464
MD
1848 ctf_get_real_timestamp(
1849 &file_stream->parent,
992e8cc0 1850 packet_index.ts_cycles.timestamp_end);
ec323464 1851 }
546293fa
MD
1852 }
1853
ec323464
MD
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);
546293fa 1862 }
6246fd54
JD
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 }
ec323464
MD
1876 } else {
1877 /* Use file size for packet size */
2d686891
MD
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;
ec323464
MD
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 }
546293fa 1895
a7ac9efd
MD
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
2d686891
MD
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
ec323464
MD
1906 /* Save position after header and context */
1907 packet_index.data_offset = pos->offset;
0f980a35 1908
ec323464 1909 /* add index to packet array */
992e8cc0 1910 g_array_append_val(file_stream->pos.packet_index, packet_index);
0f980a35 1911
ec323464
MD
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 */
888ec52a 1929 fprintf(stderr, "[error] Packet mapping length overflow\n");
ec323464 1930 return -EFAULT;
0f980a35 1931 }
ec323464
MD
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;
0f980a35 1945
ec323464
MD
1946 ret = fstat(pos->fd, &filestats);
1947 if (ret < 0)
1948 return ret;
1949
5bfcad93
MD
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
ec323464
MD
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 }
0f980a35
MD
1980 return 0;
1981}
1982
e28d4618 1983static
9e88d150 1984int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618
MD
1985{
1986 int ret;
1987
1988 if (td->packet_header_decl) {
0d69b916 1989 struct bt_definition *definition =
e28d4618
MD
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 }
9a19a512 1996 stream->trace_packet_header =
e28d4618
MD
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:
888ec52a 2004 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
e28d4618
MD
2005 return ret;
2006}
2007
0ace7505
JD
2008static
2009int import_stream_packet_index(struct ctf_trace *td,
2010 struct ctf_file_stream *file_stream)
2011{
0ace7505 2012 struct ctf_stream_pos *pos;
3ecd366e 2013 struct ctf_packet_index *ctf_index = NULL;
0ace7505
JD
2014 struct ctf_packet_index_file_hdr index_hdr;
2015 struct packet_index index;
480ef057 2016 uint32_t packet_index_len, index_minor;
0ace7505
JD
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) {
2b8dbf9a
MJ
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,
0ace7505
JD
2040 CTF_INDEX_MINOR);
2041 ret = -1;
2042 goto error;
2043 }
480ef057
JD
2044 index_minor = be32toh(index_hdr.index_minor);
2045
e83ce12a
JD
2046 packet_index_len = be32toh(index_hdr.packet_index_len);
2047 if (packet_index_len == 0) {
a74d9cb2
MD
2048 fprintf(stderr, "[error] Packet index length cannot be 0.\n");
2049 ret = -1;
2050 goto error;
2051 }
3ecd366e
MD
2052 /*
2053 * Allocate the index length found in header, not internal
2054 * representation.
2055 */
e83ce12a
JD
2056 ctf_index = g_malloc0(packet_index_len);
2057 while (fread(ctf_index, packet_index_len, 1,
ad40ac1a 2058 pos->index_fp) == 1) {
0ace7505 2059 uint64_t stream_id;
51e0087f 2060 struct ctf_stream_declaration *stream = NULL;
0ace7505
JD
2061
2062 memset(&index, 0, sizeof(index));
3ecd366e
MD
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);
0ace7505 2069 index.events_discarded_len = 64;
e69dd258 2070 index.data_offset = -1;
3ecd366e 2071 stream_id = be64toh(ctf_index->stream_id);
480ef057
JD
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 }
0ace7505
JD
2076
2077 if (!first_packet) {
2078 /* add index to packet array */
992e8cc0 2079 g_array_append_val(file_stream->pos.packet_index, index);
0ace7505
JD
2080 continue;
2081 }
2082
2083 file_stream->parent.stream_id = stream_id;
51e0087f
JG
2084 if (stream_id < td->streams->len) {
2085 stream = g_ptr_array_index(td->streams, stream_id);
2086 }
0ace7505
JD
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 */
992e8cc0 2100 g_array_append_val(file_stream->pos.packet_index, index);
0ace7505
JD
2101 }
2102
dae407df
JD
2103 /* Index containing only the header. */
2104 if (!file_stream->parent.stream_class) {
2105 ret = -1;
2106 goto error;
2107 }
2108
0ace7505
JD
2109 ret = 0;
2110
2111error:
3ecd366e 2112 g_free(ctf_index);
0ace7505
JD
2113 return ret;
2114}
2115
0f980a35
MD
2116/*
2117 * Note: many file streams can inherit from the same stream class
2118 * description (metadata).
2119 */
2120static
b086c01a 2121int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1cf393f6 2122 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
b086c01a 2123 int whence))
0f980a35 2124{
f824ae04 2125 int ret, fd, closeret;
0f980a35 2126 struct ctf_file_stream *file_stream;
ff075710 2127 struct stat statbuf;
0ace7505 2128 char *index_name;
0f980a35 2129
ff075710
MD
2130 fd = openat(td->dirfd, path, flags);
2131 if (fd < 0) {
a569a564 2132 perror("File stream openat()");
ff075710 2133 ret = fd;
0f980a35 2134 goto error;
a569a564 2135 }
ff075710
MD
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)) {
0ace7505
JD
2144 if (strncmp(path, "index", 5) != 0) {
2145 fprintf(stderr, "[warning] Skipping directory '%s' "
2146 "found in trace\n", path);
2147 }
ff075710
MD
2148 ret = 0;
2149 goto fd_is_dir_ok;
2150 }
de52f946
MD
2151 if (!statbuf.st_size) {
2152 /** Skip empty files. */
2153 ret = 0;
2154 goto fd_is_empty_file;
2155 }
ff075710 2156
0f980a35 2157 file_stream = g_new0(struct ctf_file_stream, 1);
3a25e036 2158 file_stream->pos.last_offset = LAST_OFFSET_POISON;
0ace7505
JD
2159 file_stream->pos.fd = -1;
2160 file_stream->pos.index_fp = NULL;
b086c01a 2161
87148dc7
MD
2162 strncpy(file_stream->parent.path, path, PATH_MAX);
2163 file_stream->parent.path[PATH_MAX - 1] = '\0';
2164
06789ffd
MD
2165 if (packet_seek) {
2166 file_stream->pos.packet_seek = packet_seek;
b086c01a 2167 } else {
06789ffd 2168 fprintf(stderr, "[error] packet_seek function undefined.\n");
b086c01a
JD
2169 ret = -1;
2170 goto error_def;
2171 }
2172
ca334c72 2173 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
f824ae04
MD
2174 if (ret)
2175 goto error_def;
2d0bea29 2176 ret = create_trace_definitions(td, &file_stream->parent);
e28d4618
MD
2177 if (ret)
2178 goto error_def;
25ccc85b 2179 /*
50052405 2180 * For now, only a single clock per trace is supported.
25ccc85b 2181 */
7ec78969 2182 file_stream->parent.current_clock = td->parent.single_clock;
0ace7505
JD
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");
de52f946 2190 ret = -ENOMEM;
0ace7505 2191 goto error_def;
888ec52a 2192 }
0ace7505
JD
2193 snprintf(index_name, strlen(path) + sizeof(INDEX_PATH),
2194 INDEX_PATH, path);
2195
0bb657dc 2196 if (bt_faccessat(td->dirfd, td->parent.path, index_name, O_RDONLY, 0) < 0) {
0ace7505
JD
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");
2f0c6a52
MD
2210 if (!file_stream->pos.index_fp) {
2211 perror("fdopen() error");
2212 goto error_free;
2213 }
0ace7505
JD
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
0f980a35 2227 /* Add stream file to stream class */
2d0bea29
MD
2228 g_ptr_array_add(file_stream->parent.stream_class->streams,
2229 &file_stream->parent);
0f980a35
MD
2230 return 0;
2231
2232error_index:
0ace7505
JD
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 }
2d0bea29 2239 if (file_stream->parent.trace_packet_header)
13fad8b6 2240 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
0ace7505
JD
2241error_free:
2242 free(index_name);
e28d4618 2243error_def:
f824ae04
MD
2244 closeret = ctf_fini_pos(&file_stream->pos);
2245 if (closeret) {
2246 fprintf(stderr, "Error on ctf_fini_pos\n");
2247 }
0f980a35 2248 g_free(file_stream);
de52f946 2249fd_is_empty_file:
ff075710
MD
2250fd_is_dir_ok:
2251fstat_error:
f824ae04
MD
2252 closeret = close(fd);
2253 if (closeret) {
2254 perror("Error on fd close");
2255 }
0f980a35 2256error:
65102a8c
MD
2257 return ret;
2258}
2259
bbefb8dd 2260static
5b80ddfb 2261int ctf_open_trace_read(struct ctf_trace *td,
8c250d87 2262 const char *path, int flags,
1cf393f6 2263 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 2264 int whence), FILE *metadata_fp)
bbefb8dd 2265{
6514b4af 2266 struct ctf_scanner *scanner;
f824ae04 2267 int ret, closeret;
65102a8c
MD
2268 struct dirent *dirent;
2269 struct dirent *diriter;
2270 size_t dirent_len;
6038c87e 2271 int pc_name_max;
0ace7505 2272 char *ext;
bbefb8dd 2273
46322b33 2274 td->flags = flags;
bbefb8dd
MD
2275
2276 /* Open trace directory */
46322b33
MD
2277 td->dir = opendir(path);
2278 if (!td->dir) {
6a6b384c 2279 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
bbefb8dd
MD
2280 ret = -ENOENT;
2281 goto error;
2282 }
2283
46322b33
MD
2284 td->dirfd = open(path, 0);
2285 if (td->dirfd < 0) {
6a6b384c 2286 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
a569a564
MD
2287 perror("Trace directory open");
2288 ret = -errno;
65102a8c
MD
2289 goto error_dirfd;
2290 }
caf929fa
MD
2291 strncpy(td->parent.path, path, sizeof(td->parent.path));
2292 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
0f980a35 2293
65102a8c
MD
2294 /*
2295 * Keep the metadata file separate.
0c880b0a
MD
2296 * Keep scanner object local to the open. We don't support
2297 * incremental metadata append for on-disk traces.
65102a8c 2298 */
6514b4af
MD
2299 scanner = ctf_scanner_alloc();
2300 if (!scanner) {
2301 fprintf(stderr, "[error] Error allocating scanner\n");
2302 ret = -ENOMEM;
2303 goto error_metadata;
2304 }
0c880b0a 2305 ret = ctf_trace_metadata_read(td, metadata_fp, scanner, 0);
6514b4af 2306 ctf_scanner_free(scanner);
65102a8c 2307 if (ret) {
251fc08c
MD
2308 if (ret == -ENOENT) {
2309 fprintf(stderr, "[warning] Empty metadata.\n");
2310 }
6a6b384c 2311 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
65102a8c
MD
2312 goto error_metadata;
2313 }
bbefb8dd
MD
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.
bbefb8dd
MD
2319 */
2320
6038c87e
MJ
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;
bbefb8dd 2330
65102a8c 2331 dirent = malloc(dirent_len);
bbefb8dd 2332
65102a8c 2333 for (;;) {
46322b33 2334 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 2335 if (ret) {
3394d22e 2336 fprintf(stderr, "[error] Readdir error.\n");
65102a8c 2337 goto readdir_error;
65102a8c
MD
2338 }
2339 if (!diriter)
2340 break;
d8ea2d29
MD
2341 /* Ignore hidden files, ., .. and metadata. */
2342 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
2343 || !strcmp(diriter->d_name, "..")
2344 || !strcmp(diriter->d_name, "metadata"))
2345 continue;
0ace7505
JD
2346
2347 /* Ignore index files : *.idx */
2348 ext = strrchr(diriter->d_name, '.');
2349 if (ext && (!strcmp(ext, ".idx"))) {
2350 continue;
2351 }
2352
06789ffd
MD
2353 ret = ctf_open_file_stream_read(td, diriter->d_name,
2354 flags, packet_seek);
dc48ecad 2355 if (ret) {
3394d22e 2356 fprintf(stderr, "[error] Open file stream error.\n");
dc48ecad
MD
2357 goto readdir_error;
2358 }
65102a8c 2359 }
bbefb8dd 2360
65102a8c 2361 free(dirent);
bbefb8dd 2362 return 0;
65102a8c
MD
2363
2364readdir_error:
2365 free(dirent);
2366error_metadata:
f824ae04
MD
2367 closeret = close(td->dirfd);
2368 if (closeret) {
2369 perror("Error on fd close");
2370 }
65102a8c 2371error_dirfd:
f824ae04
MD
2372 closeret = closedir(td->dir);
2373 if (closeret) {
2374 perror("Error on closedir");
2375 }
bbefb8dd
MD
2376error:
2377 return ret;
2378}
2379
03798a93
JD
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 */
e9378815 2385static
1b8455b7 2386struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1cf393f6 2387 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
ae23d232 2388 int whence), FILE *metadata_fp)
bbefb8dd 2389{
46322b33 2390 struct ctf_trace *td;
bbefb8dd
MD
2391 int ret;
2392
2715de36
MD
2393 /*
2394 * If packet_seek is NULL, we provide our default version.
2395 */
2396 if (!packet_seek)
2397 packet_seek = ctf_packet_seek;
2398
46322b33 2399 td = g_new0(struct ctf_trace, 1);
bbefb8dd 2400
8c572eba 2401 switch (flags & O_ACCMODE) {
bbefb8dd 2402 case O_RDONLY:
b61922b5 2403 if (!path) {
3394d22e 2404 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
b61922b5
MD
2405 goto error;
2406 }
06789ffd 2407 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
bbefb8dd
MD
2408 if (ret)
2409 goto error;
2410 break;
989c73bc 2411 case O_RDWR:
3394d22e 2412 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
46322b33 2413 goto error;
bbefb8dd 2414 default:
3394d22e 2415 fprintf(stderr, "[error] Incorrect open flags.\n");
bbefb8dd
MD
2416 goto error;
2417 }
2418
c40a57e5
AB
2419 ret = trace_debug_info_create(td);
2420 if (ret) {
2421 goto error;
2422 }
2423
46322b33 2424 return &td->parent;
bbefb8dd 2425error:
c40a57e5 2426 trace_debug_info_destroy(td);
bbefb8dd
MD
2427 g_free(td);
2428 return NULL;
2429}
2430
2e937fb4 2431static
f571dfb1 2432void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
c150f912 2433 struct bt_mmap_stream *mmap_info)
f571dfb1
JD
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;
aee35fcc 2440 pos->base_mma = NULL;
f571dfb1
JD
2441 pos->offset = 0;
2442 pos->dummy = false;
2443 pos->cur_index = 0;
f571dfb1
JD
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;
731087d8 2448 pos->priv = mmap_info->priv;
f1f52630
MD
2449 pos->packet_index = g_array_new(FALSE, TRUE,
2450 sizeof(struct packet_index));
f571dfb1
JD
2451}
2452
2453static
2454int prepare_mmap_stream_definition(struct ctf_trace *td,
5805251d
JD
2455 struct ctf_file_stream *file_stream,
2456 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2457 int whence))
f571dfb1 2458{
f380e105 2459 struct ctf_stream_declaration *stream;
5805251d 2460 uint64_t stream_id;
f571dfb1
JD
2461 int ret;
2462
5805251d
JD
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;
f571dfb1 2466 if (stream_id >= td->streams->len) {
3394d22e 2467 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
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) {
3394d22e 2474 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
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,
c150f912 2487 struct bt_mmap_stream *mmap_info,
1cf393f6 2488 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
f571dfb1
JD
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);
5805251d 2495 file_stream->parent.stream_id = -1ULL;
3a25e036 2496 file_stream->pos.last_offset = LAST_OFFSET_POISON;
f571dfb1
JD
2497 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
2498
06789ffd 2499 file_stream->pos.packet_seek = packet_seek;
f571dfb1
JD
2500
2501 ret = create_trace_definitions(td, &file_stream->parent);
2502 if (ret) {
2503 goto error_def;
2504 }
2505
5805251d 2506 ret = prepare_mmap_stream_definition(td, file_stream, packet_seek);
f571dfb1
JD
2507 if (ret)
2508 goto error_index;
2509
f7bbd502 2510 /*
50052405 2511 * For now, only a single clock per trace is supported.
f7bbd502 2512 */
7ec78969 2513 file_stream->parent.current_clock = td->parent.single_clock;
f7bbd502 2514
f571dfb1
JD
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)
13fad8b6 2522 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
f571dfb1
JD
2523error_def:
2524 g_free(file_stream);
2525 return ret;
2526}
2527
2e937fb4 2528static
f571dfb1 2529int ctf_open_mmap_trace_read(struct ctf_trace *td,
c150f912 2530 struct bt_mmap_stream_list *mmap_list,
1cf393f6 2531 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
f571dfb1
JD
2532 int whence),
2533 FILE *metadata_fp)
2534{
2535 int ret;
c150f912 2536 struct bt_mmap_stream *mmap_info;
f571dfb1 2537
0c880b0a
MD
2538 td->scanner = ctf_scanner_alloc();
2539 if (!td->scanner) {
6514b4af
MD
2540 fprintf(stderr, "[error] Error allocating scanner\n");
2541 ret = -ENOMEM;
0c880b0a 2542 goto error;
6514b4af 2543 }
0c880b0a 2544 ret = ctf_trace_metadata_read(td, metadata_fp, td->scanner, 0);
f571dfb1 2545 if (ret) {
251fc08c
MD
2546 if (ret == -ENOENT) {
2547 fprintf(stderr, "[warning] Empty metadata.\n");
2548 }
f571dfb1
JD
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 */
3122e6f0 2556 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
06789ffd 2557 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
f571dfb1 2558 if (ret) {
3394d22e 2559 fprintf(stderr, "[error] Open file mmap stream error.\n");
f571dfb1
JD
2560 goto error;
2561 }
2562 }
f571dfb1
JD
2563 return 0;
2564
2565error:
0c880b0a 2566 ctf_scanner_free(td->scanner);
f571dfb1
JD
2567 return ret;
2568}
2569
2570static
1b8455b7 2571struct bt_trace_descriptor *ctf_open_mmap_trace(
c150f912 2572 struct bt_mmap_stream_list *mmap_list,
1cf393f6 2573 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
20d0dcf9 2574 int whence),
f571dfb1
JD
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 }
06789ffd
MD
2585 if (!packet_seek) {
2586 fprintf(stderr, "[error] packet_seek function undefined.\n");
f571dfb1
JD
2587 goto error;
2588 }
2589 td = g_new0(struct ctf_trace, 1);
b5a1fa45 2590 td->dirfd = -1;
06789ffd 2591 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
f571dfb1
JD
2592 if (ret)
2593 goto error_free;
2594
c40a57e5
AB
2595 ret = trace_debug_info_create(td);
2596 if (ret) {
2597 goto error_free;
2598 }
2599
f571dfb1
JD
2600 return &td->parent;
2601
2602error_free:
2603 g_free(td);
2604error:
2605 return NULL;
2606}
2607
0c880b0a
MD
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
03798a93 2643static
1b8455b7 2644int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
03798a93
JD
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;
992e8cc0 2668 if (!stream_pos->packet_index)
afe9cd4a
JD
2669 continue;
2670
992e8cc0 2671 for (k = 0; k < stream_pos->packet_index->len; k++) {
03798a93 2672 struct packet_index *index;
03798a93 2673
992e8cc0 2674 index = &g_array_index(stream_pos->packet_index,
03798a93 2675 struct packet_index, k);
992e8cc0 2676 index->ts_real.timestamp_begin =
03798a93 2677 ctf_get_real_timestamp(stream,
992e8cc0
MD
2678 index->ts_cycles.timestamp_begin);
2679 index->ts_real.timestamp_end =
03798a93 2680 ctf_get_real_timestamp(stream,
992e8cc0 2681 index->ts_cycles.timestamp_end);
03798a93
JD
2682 }
2683 }
2684 }
2685 return 0;
2686}
2687
0f980a35 2688static
f824ae04 2689int ctf_close_file_stream(struct ctf_file_stream *file_stream)
0f980a35 2690{
f824ae04
MD
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 }
500634be
JD
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 }
f824ae04
MD
2704 }
2705 return 0;
0f980a35
MD
2706}
2707
e9378815 2708static
1b8455b7 2709int ctf_close_trace(struct bt_trace_descriptor *tdp)
bbefb8dd 2710{
46322b33 2711 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
08c82b90 2712 int ret;
0f980a35 2713
46322b33 2714 if (td->streams) {
08c82b90
MD
2715 int i;
2716
46322b33 2717 for (i = 0; i < td->streams->len; i++) {
f380e105 2718 struct ctf_stream_declaration *stream;
0f980a35 2719 int j;
e9378815 2720
46322b33 2721 stream = g_ptr_array_index(td->streams, i);
e9378815
MD
2722 if (!stream)
2723 continue;
2d0bea29 2724 for (j = 0; j < stream->streams->len; j++) {
0f980a35 2725 struct ctf_file_stream *file_stream;
15d4fe3c
JD
2726 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2727 struct ctf_file_stream, parent);
f824ae04
MD
2728 ret = ctf_close_file_stream(file_stream);
2729 if (ret)
2730 return ret;
0f980a35 2731 }
e003ab50 2732 }
e003ab50 2733 }
15d4fe3c 2734 ctf_destroy_metadata(td);
0c880b0a 2735 ctf_scanner_free(td->scanner);
500634be
JD
2736 if (td->dirfd >= 0) {
2737 ret = close(td->dirfd);
2738 if (ret) {
2739 perror("Error closing dirfd");
2740 return ret;
2741 }
f824ae04 2742 }
500634be
JD
2743 if (td->dir) {
2744 ret = closedir(td->dir);
2745 if (ret) {
2746 perror("Error closedir");
2747 return ret;
2748 }
f824ae04 2749 }
7237592a 2750 free(td->metadata_string);
c40a57e5 2751 trace_debug_info_destroy(td);
bbefb8dd 2752 g_free(td);
f824ae04 2753 return 0;
bbefb8dd
MD
2754}
2755
98a04903 2756static
1b8455b7 2757void ctf_set_context(struct bt_trace_descriptor *descriptor,
98a04903
JD
2758 struct bt_context *ctx)
2759{
2760 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2761 parent);
2762
45807148 2763 td->parent.ctx = ctx;
98a04903
JD
2764}
2765
2766static
1b8455b7 2767void ctf_set_handle(struct bt_trace_descriptor *descriptor,
98a04903
JD
2768 struct bt_trace_handle *handle)
2769{
2770 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2771 parent);
2772
4d086981 2773 td->parent.handle = handle;
98a04903
JD
2774}
2775
95febab3 2776static
7fb21036 2777void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
2778{
2779 int ret;
2780
4c8bfb7e 2781 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
2782 ret = bt_register_format(&ctf_format);
2783 assert(!ret);
2784}
698f0fe4 2785
95febab3
MD
2786static
2787void __attribute__((destructor)) ctf_exit(void)
2788{
2789 bt_unregister_format(&ctf_format);
2790}
This page took 0.220245 seconds and 4 git commands to generate.