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