Add "encoding" for sequence and array of integers
[babeltrace.git] / formats / ctf / ctf.c
CommitLineData
fc93b2bd
MD
1/*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * Format registration.
5 *
c054553d 6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fc93b2bd 7 *
ccd7e1c8
MD
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
fc93b2bd 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
fc93b2bd
MD
17 */
18
19#include <babeltrace/format.h>
20#include <babeltrace/ctf/types.h>
bbefb8dd 21#include <babeltrace/ctf/metadata.h>
65102a8c 22#include <babeltrace/babeltrace.h>
0f980a35 23#include <inttypes.h>
b4c19c1e 24#include <stdio.h>
0f980a35
MD
25#include <uuid/uuid.h>
26#include <sys/mman.h>
bbefb8dd 27#include <errno.h>
b4c19c1e 28#include <endian.h>
bbefb8dd 29#include <sys/types.h>
65102a8c 30#include <sys/stat.h>
bbefb8dd 31#include <fcntl.h>
65102a8c 32#include <dirent.h>
bbefb8dd 33#include <glib.h>
65102a8c
MD
34#include <unistd.h>
35#include <stdlib.h>
36
65102a8c
MD
37#include "metadata/ctf-scanner.h"
38#include "metadata/ctf-parser.h"
39#include "metadata/ctf-ast.h"
40
0f980a35
MD
41/*
42 * We currently simply map a page to read the packet header and packet
8c572eba 43 * context to get the packet length and content length. (in bits)
0f980a35 44 */
8c572eba
MD
45#define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
46#define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
0f980a35
MD
47#define UUID_LEN 16 /* uuid by value len */
48
a0fe7d97
MD
49#ifndef min
50#define min(a, b) (((a) < (b)) ? (a) : (b))
51#endif
52
65102a8c 53extern int yydebug;
bbefb8dd 54
bbefb8dd
MD
55struct trace_descriptor *ctf_open_trace(const char *path, int flags);
56void ctf_close_trace(struct trace_descriptor *descriptor);
fc93b2bd 57
1ae19169
MD
58static
59rw_dispatch read_dispatch_table[] = {
d11e9c49
MD
60 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
61 [ CTF_TYPE_FLOAT ] = ctf_float_read,
62 [ CTF_TYPE_ENUM ] = ctf_enum_read,
63 [ CTF_TYPE_STRING ] = ctf_string_read,
64 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
65 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
66 [ CTF_TYPE_ARRAY ] = ctf_array_read,
67 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
d11e9c49
MD
68};
69
1ae19169
MD
70static
71rw_dispatch write_dispatch_table[] = {
d11e9c49
MD
72 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
73 [ CTF_TYPE_FLOAT ] = ctf_float_write,
74 [ CTF_TYPE_ENUM ] = ctf_enum_write,
75 [ CTF_TYPE_STRING ] = ctf_string_write,
76 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
77 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
78 [ CTF_TYPE_ARRAY ] = ctf_array_write,
79 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
d11e9c49
MD
80};
81
1ae19169 82static
d11e9c49 83struct format ctf_format = {
bbefb8dd
MD
84 .open_trace = ctf_open_trace,
85 .close_trace = ctf_close_trace,
fc93b2bd
MD
86};
87
31262354 88static
764af3f4 89int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream)
31262354
MD
90{
91 struct ctf_stream_pos *pos =
92 container_of(ppos, struct ctf_stream_pos, parent);
764af3f4 93 struct ctf_stream_class *stream_class = stream->stream_class;
31262354
MD
94 struct ctf_event *event_class;
95 uint64_t id = 0;
96 int len_index;
97 int ret;
98
99 if (pos->offset == EOF)
100 return EOF;
101
102 /* Read event header */
103 if (stream_class->event_header) {
104 ret = generic_rw(ppos, &stream_class->event_header->p);
105 if (ret)
106 goto error;
107 /* lookup event id */
108 len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl,
109 g_quark_from_static_string("id"));
110 if (len_index >= 0) {
111 struct definition_integer *defint;
112 struct definition *field;
113
114 field = struct_definition_get_field_from_index(stream_class->event_header, len_index);
115 assert(field->declaration->id == CTF_TYPE_INTEGER);
116 defint = container_of(field, struct definition_integer, p);
117 assert(defint->declaration->signedness == FALSE);
118 id = defint->value._unsigned; /* set id */
119 }
764af3f4
MD
120
121 /* lookup timestamp */
122 len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl,
123 g_quark_from_static_string("timestamp"));
124 if (len_index >= 0) {
125 struct definition_integer *defint;
126 struct definition *field;
127
128 field = struct_definition_get_field_from_index(stream_class->event_header, len_index);
129 assert(field->declaration->id == CTF_TYPE_INTEGER);
130 defint = container_of(field, struct definition_integer, p);
131 assert(defint->declaration->signedness == FALSE);
132 /* update timestamp */
133 stream->timestamp = defint->value._unsigned;
134 }
135
31262354
MD
136 }
137
138 /* Read stream-declared event context */
139 if (stream_class->event_context) {
140 ret = generic_rw(ppos, &stream_class->event_context->p);
141 if (ret)
142 goto error;
143 }
144
145 if (id >= stream_class->events_by_id->len) {
146 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
147 return -EINVAL;
148 }
149 event_class = g_ptr_array_index(stream_class->events_by_id, id);
150 if (!event_class) {
151 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
152 return -EINVAL;
153 }
154
155 /* Read event-declared event context */
156 if (event_class->context) {
157 ret = generic_rw(ppos, &event_class->context->p);
158 if (ret)
159 goto error;
160 }
161
162 /* Read event payload */
163 if (event_class->fields) {
164 ret = generic_rw(ppos, &event_class->fields->p);
165 if (ret)
166 goto error;
167 }
168
169 return 0;
170
171error:
172 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
173 return ret;
174}
175
176static
764af3f4 177int ctf_write_event(struct stream_pos *pos, struct ctf_stream *stream)
31262354 178{
764af3f4 179 struct ctf_stream_class *stream_class = stream->stream_class;
31262354
MD
180 struct ctf_event *event_class;
181 uint64_t id = 0;
182 int len_index;
183 int ret;
184
185 /* print event header */
186 if (stream_class->event_header) {
187 /* lookup event id */
188 len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl,
189 g_quark_from_static_string("id"));
190 if (len_index >= 0) {
191 struct definition_integer *defint;
192 struct definition *field;
193
194 field = struct_definition_get_field_from_index(stream_class->event_header, len_index);
195 assert(field->declaration->id == CTF_TYPE_INTEGER);
196 defint = container_of(field, struct definition_integer, p);
197 assert(defint->declaration->signedness == FALSE);
198 id = defint->value._unsigned; /* set id */
199 }
200
201 ret = generic_rw(pos, &stream_class->event_header->p);
202 if (ret)
203 goto error;
204 }
205
206 /* print stream-declared event context */
207 if (stream_class->event_context) {
208 ret = generic_rw(pos, &stream_class->event_context->p);
209 if (ret)
210 goto error;
211 }
212
213 if (id >= stream_class->events_by_id->len) {
214 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
215 return -EINVAL;
216 }
217 event_class = g_ptr_array_index(stream_class->events_by_id, id);
218 if (!event_class) {
219 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
220 return -EINVAL;
221 }
222
223 /* print event-declared event context */
224 if (event_class->context) {
225 ret = generic_rw(pos, &event_class->context->p);
226 if (ret)
227 goto error;
228 }
229
230 /* Read and print event payload */
231 if (event_class->fields) {
232 ret = generic_rw(pos, &event_class->fields->p);
233 if (ret)
234 goto error;
235 }
236
237 return 0;
238
239error:
240 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
241 return ret;
242}
243
8563e754 244void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
8c572eba
MD
245{
246 pos->fd = fd;
247 pos->mmap_offset = 0;
248 pos->packet_size = 0;
249 pos->content_size = 0;
250 pos->content_size_loc = NULL;
251 pos->base = NULL;
252 pos->offset = 0;
253 pos->dummy = false;
8c572eba 254 pos->cur_index = 0;
8563e754
MD
255 if (fd >= 0)
256 pos->packet_index = g_array_new(FALSE, TRUE,
257 sizeof(struct packet_index));
258 else
259 pos->packet_index = NULL;
8563e754
MD
260 switch (open_flags & O_ACCMODE) {
261 case O_RDONLY:
262 pos->prot = PROT_READ;
263 pos->flags = MAP_PRIVATE;
264 pos->parent.rw_table = read_dispatch_table;
31262354 265 pos->parent.event_cb = ctf_read_event;
8563e754 266 break;
8563e754
MD
267 case O_RDWR:
268 pos->prot = PROT_WRITE; /* Write has priority */
269 pos->flags = MAP_SHARED;
270 pos->parent.rw_table = write_dispatch_table;
31262354 271 pos->parent.event_cb = ctf_write_event;
8563e754 272 if (fd >= 0)
847bf71a 273 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
8563e754
MD
274 break;
275 default:
276 assert(0);
8c572eba
MD
277 }
278}
279
46322b33 280void ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba
MD
281{
282 int ret;
283
284 if (pos->prot == PROT_WRITE && pos->content_size_loc)
285 *pos->content_size_loc = pos->offset;
286 if (pos->base) {
287 /* unmap old base */
288 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
289 if (ret) {
46322b33 290 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
8c572eba
MD
291 strerror(errno));
292 assert(0);
293 }
294 }
295 (void) g_array_free(pos->packet_index, TRUE);
296}
297
847bf71a 298void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
0f980a35
MD
299{
300 int ret;
8c572eba
MD
301 off_t off;
302 struct packet_index *index;
0f980a35 303
8c572eba
MD
304 if (pos->prot == PROT_WRITE && pos->content_size_loc)
305 *pos->content_size_loc = pos->offset;
0f980a35
MD
306
307 if (pos->base) {
308 /* unmap old base */
8c572eba 309 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 310 if (ret) {
46322b33 311 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
312 strerror(errno));
313 assert(0);
314 }
847bf71a 315 pos->base = NULL;
0f980a35
MD
316 }
317
8c572eba 318 /*
46322b33 319 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
320 * except to get exactly at the beginning of the next packet.
321 */
322 if (pos->prot == PROT_WRITE) {
989c73bc
MD
323 switch (whence) {
324 case SEEK_CUR:
325 /* The writer will add padding */
326 assert(pos->offset + offset == pos->packet_size);
8c572eba 327 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
989c73bc
MD
328 break;
329 case SEEK_SET:
330 assert(offset == 0); /* only seek supported for now */
331 pos->cur_index = 0;
332 break;
333 default:
334 assert(0);
335 }
8c572eba
MD
336 pos->content_size = -1U; /* Unknown at this point */
337 pos->packet_size = WRITE_PACKET_LEN;
989c73bc
MD
338 off = posix_fallocate(pos->fd, pos->mmap_offset,
339 pos->packet_size / CHAR_BIT);
8c572eba 340 assert(off >= 0);
847bf71a 341 pos->offset = 0;
8c572eba 342 } else {
847bf71a
MD
343 switch (whence) {
344 case SEEK_CUR:
345 /* The reader will expect us to skip padding */
346 assert(pos->offset + offset == pos->content_size);
8c572eba 347 ++pos->cur_index;
847bf71a
MD
348 break;
349 case SEEK_SET:
350 assert(offset == 0); /* only seek supported for now */
351 pos->cur_index = 0;
352 break;
353 default:
354 assert(0);
355 }
356 if (pos->cur_index >= pos->packet_index->len) {
670977d3 357 pos->offset = EOF;
847bf71a
MD
358 return;
359 }
8c572eba
MD
360 index = &g_array_index(pos->packet_index, struct packet_index,
361 pos->cur_index);
362 pos->mmap_offset = index->offset;
363
364 /* Lookup context/packet size in index */
365 pos->content_size = index->content_size;
366 pos->packet_size = index->packet_size;
847bf71a 367 pos->offset = index->data_offset;
8c572eba 368 }
0f980a35 369 /* map new base. Need mapping length from header. */
8c572eba
MD
370 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
371 pos->flags, pos->fd, pos->mmap_offset);
847bf71a
MD
372 if (pos->base == MAP_FAILED) {
373 fprintf(stdout, "[error] mmap error %s.\n",
374 strerror(errno));
375 assert(0);
376 }
0f980a35
MD
377}
378
b4c19c1e
MD
379static
380int packet_metadata(struct ctf_trace *td, FILE *fp)
381{
382 uint32_t magic;
383 size_t len;
384 int ret = 0;
385
386 len = fread(&magic, sizeof(magic), 1, fp);
a0fe7d97 387 if (len != 1) {
b4c19c1e
MD
388 goto end;
389 }
390 if (magic == TSDL_MAGIC) {
391 ret = 1;
392 td->byte_order = BYTE_ORDER;
393 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
394 ret = 1;
395 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
396 LITTLE_ENDIAN : BIG_ENDIAN;
397 }
a0fe7d97 398 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
399end:
400 rewind(fp);
401 return ret;
402}
403
404static
405int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
406 FILE *out)
407{
408 struct metadata_packet_header header;
a0fe7d97 409 size_t readlen, writelen, toread;
b4c19c1e
MD
410 char buf[4096];
411 int ret = 0;
412
a91a962e 413 readlen = fread(&header, header_sizeof(header), 1, in);
a0fe7d97 414 if (readlen < 1)
b4c19c1e
MD
415 return -EINVAL;
416
417 if (td->byte_order != BYTE_ORDER) {
418 header.magic = GUINT32_SWAP_LE_BE(header.magic);
419 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
420 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
421 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
422 }
423 if (header.checksum)
424 fprintf(stdout, "[warning] checksum verification not supported yet.\n");
425 if (header.compression_scheme) {
426 fprintf(stdout, "[error] compression (%u) not supported yet.\n",
427 header.compression_scheme);
428 return -EINVAL;
429 }
430 if (header.encryption_scheme) {
431 fprintf(stdout, "[error] encryption (%u) not supported yet.\n",
432 header.encryption_scheme);
433 return -EINVAL;
434 }
435 if (header.checksum_scheme) {
436 fprintf(stdout, "[error] checksum (%u) not supported yet.\n",
437 header.checksum_scheme);
438 return -EINVAL;
439 }
440 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
441 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
442 CTF_TRACE_SET_FIELD(td, uuid);
443 } else {
444 if (uuid_compare(header.uuid, td->uuid))
445 return -EINVAL;
446 }
447
a0fe7d97
MD
448 toread = header.content_size / CHAR_BIT;
449
450 for (;;) {
451 readlen = fread(buf, sizeof(char), min(sizeof(buf), toread), in);
b4c19c1e
MD
452 if (ferror(in)) {
453 ret = -EINVAL;
454 break;
455 }
a0fe7d97 456 printf("read %s\n", buf);
b4c19c1e
MD
457 writelen = fwrite(buf, sizeof(char), readlen, out);
458 if (writelen < readlen) {
459 ret = -EIO;
460 break;
461 }
462 if (ferror(out)) {
463 ret = -EINVAL;
464 break;
465 }
a0fe7d97
MD
466 toread -= readlen;
467 if (!toread) {
7f4b5c4d 468 ret = 0; /* continue reading next packet */
a0fe7d97
MD
469 break;
470 }
b4c19c1e
MD
471 }
472 return ret;
473}
474
475static
476int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
477 char **buf)
478{
479 FILE *in, *out;
480 size_t size;
481 int ret;
482
483 in = *fp;
484 out = open_memstream(buf, &size);
485 if (out == NULL)
486 return -errno;
487
488 for (;;) {
489 ret = ctf_open_trace_metadata_packet_read(td, in, out);
7f4b5c4d 490 if (ret) {
b4c19c1e 491 break;
7f4b5c4d
MD
492 }
493 if (feof(in)) {
494 ret = 0;
b4c19c1e
MD
495 break;
496 }
497 }
498 fclose(out); /* flush the buffer */
499 fclose(in);
500 /* open for reading */
501 *fp = fmemopen(*buf, size, "rb");
502 return 0;
503}
504
65102a8c 505static
46322b33 506int ctf_open_trace_metadata_read(struct ctf_trace *td)
65102a8c
MD
507{
508 struct ctf_scanner *scanner;
509 FILE *fp;
b4c19c1e 510 char *buf = NULL;
65102a8c
MD
511 int ret = 0;
512
46322b33
MD
513 td->metadata.pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
514 if (td->metadata.pos.fd < 0) {
65102a8c 515 fprintf(stdout, "Unable to open metadata.\n");
46322b33 516 return td->metadata.pos.fd;
65102a8c
MD
517 }
518
519 if (babeltrace_debug)
520 yydebug = 1;
521
46322b33 522 fp = fdopen(td->metadata.pos.fd, "r");
65102a8c 523 if (!fp) {
46322b33 524 fprintf(stdout, "[error] Unable to open metadata stream.\n");
65102a8c
MD
525 ret = -errno;
526 goto end_stream;
527 }
528
b4c19c1e
MD
529 if (packet_metadata(td, fp)) {
530 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
531 if (ret)
532 goto end_packet_read;
533 }
534
65102a8c
MD
535 scanner = ctf_scanner_alloc(fp);
536 if (!scanner) {
46322b33 537 fprintf(stdout, "[error] Error allocating scanner\n");
65102a8c
MD
538 ret = -ENOMEM;
539 goto end_scanner_alloc;
540 }
541 ret = ctf_scanner_append_ast(scanner);
542 if (ret) {
46322b33 543 fprintf(stdout, "[error] Error creating AST\n");
65102a8c
MD
544 goto end;
545 }
546
547 if (babeltrace_debug) {
548 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
549 if (ret) {
46322b33 550 fprintf(stdout, "[error] Error visiting AST for XML output\n");
65102a8c
MD
551 goto end;
552 }
553 }
554
555 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
556 if (ret) {
46322b33 557 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
558 goto end;
559 }
560 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
46322b33 561 td, BYTE_ORDER);
65102a8c 562 if (ret) {
46322b33 563 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
564 goto end;
565 }
566end:
567 ctf_scanner_free(scanner);
568end_scanner_alloc:
b4c19c1e 569end_packet_read:
65102a8c 570 fclose(fp);
b4c19c1e 571 free(buf);
65102a8c 572end_stream:
46322b33 573 close(td->metadata.pos.fd);
0f980a35
MD
574 return ret;
575}
576
577
578static
46322b33 579int create_stream_packet_index(struct ctf_trace *td,
0f980a35
MD
580 struct ctf_file_stream *file_stream)
581{
aa6bffae 582 struct ctf_stream_class *stream;
0f980a35 583 int len_index;
46322b33 584 struct ctf_stream_pos *pos;
0f980a35
MD
585 struct stat filestats;
586 struct packet_index packet_index;
587 int first_packet = 1;
588 int ret;
589
590 pos = &file_stream->pos;
591
592 ret = fstat(pos->fd, &filestats);
593 if (ret < 0)
594 return ret;
595
596 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
597 uint64_t stream_id = 0;
598
599 if (pos->base) {
600 /* unmap old base */
8c572eba 601 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 602 if (ret) {
46322b33 603 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
604 strerror(errno));
605 return ret;
606 }
8c572eba 607 pos->base = NULL;
0f980a35
MD
608 }
609 /* map new base. Need mapping length from header. */
8c572eba 610 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
0f980a35 611 MAP_PRIVATE, pos->fd, pos->mmap_offset);
dc48ecad
MD
612 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
613 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
0f980a35
MD
614 pos->offset = 0; /* Position of the packet header */
615
8c572eba
MD
616 packet_index.offset = pos->mmap_offset;
617 packet_index.content_size = 0;
618 packet_index.packet_size = 0;
619
0f980a35 620 /* read and check header, set stream id (and check) */
46322b33 621 if (td->packet_header) {
0f980a35 622 /* Read packet header */
c5e74408
MD
623 ret = generic_rw(&pos->parent, &td->packet_header->p);
624 if (ret)
625 return ret;
46322b33 626 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic"));
0f980a35
MD
627 if (len_index >= 0) {
628 struct definition_integer *defint;
b1a2f580 629 struct definition *field;
0f980a35 630
46322b33 631 field = struct_definition_get_field_from_index(td->packet_header, len_index);
b1a2f580
MD
632 assert(field->declaration->id == CTF_TYPE_INTEGER);
633 defint = container_of(field, struct definition_integer, p);
0f980a35
MD
634 assert(defint->declaration->signedness == FALSE);
635 if (defint->value._unsigned != CTF_MAGIC) {
d8ea2d29 636 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
8c572eba
MD
637 defint->value._unsigned,
638 file_stream->pos.packet_index->len,
639 (ssize_t) pos->mmap_offset);
0f980a35
MD
640 return -EINVAL;
641 }
642 }
643
644 /* check uuid */
b4c19c1e 645 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("uuid"));
0f980a35
MD
646 if (len_index >= 0) {
647 struct definition_array *defarray;
b1a2f580 648 struct definition *field;
0f980a35
MD
649 uint64_t i;
650 uint8_t uuidval[UUID_LEN];
651
46322b33 652 field = struct_definition_get_field_from_index(td->packet_header, len_index);
b1a2f580
MD
653 assert(field->declaration->id == CTF_TYPE_ARRAY);
654 defarray = container_of(field, struct definition_array, p);
3838df27 655 assert(array_len(defarray) == UUID_LEN);
0f980a35
MD
656 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
657
658 for (i = 0; i < UUID_LEN; i++) {
659 struct definition *elem;
660 struct definition_integer *defint;
661
662 elem = array_index(defarray, i);
663 assert(elem);
664 defint = container_of(elem, struct definition_integer, p);
665 uuidval[i] = defint->value._unsigned;
666 }
46322b33 667 ret = uuid_compare(td->uuid, uuidval);
0f980a35
MD
668 if (ret) {
669 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
670 return -EINVAL;
671 }
672 }
673
674
46322b33 675 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("stream_id"));
0f980a35
MD
676 if (len_index >= 0) {
677 struct definition_integer *defint;
b1a2f580 678 struct definition *field;
0f980a35 679
46322b33 680 field = struct_definition_get_field_from_index(td->packet_header, len_index);
b1a2f580
MD
681 assert(field->declaration->id == CTF_TYPE_INTEGER);
682 defint = container_of(field, struct definition_integer, p);
0f980a35
MD
683 assert(defint->declaration->signedness == FALSE);
684 stream_id = defint->value._unsigned;
685 }
686 }
687
688 if (!first_packet && file_stream->stream_id != stream_id) {
689 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
690 return -EINVAL;
691 }
692 if (first_packet) {
693 file_stream->stream_id = stream_id;
46322b33 694 if (stream_id >= td->streams->len) {
0f980a35
MD
695 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
696 return -EINVAL;
697 }
46322b33 698 stream = g_ptr_array_index(td->streams, stream_id);
0f980a35
MD
699 if (!stream) {
700 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
701 return -EINVAL;
702 }
764af3f4 703 file_stream->stream.stream_class = stream;
0f980a35
MD
704 }
705 first_packet = 0;
706
dc48ecad
MD
707 if (stream->packet_context) {
708 /* Read packet context */
c5e74408
MD
709 ret = generic_rw(&pos->parent, &stream->packet_context->p);
710 if (ret)
711 return ret;
dc48ecad
MD
712 /* read content size from header */
713 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size"));
714 if (len_index >= 0) {
715 struct definition_integer *defint;
b1a2f580 716 struct definition *field;
dc48ecad
MD
717
718 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
b1a2f580
MD
719 assert(field->declaration->id == CTF_TYPE_INTEGER);
720 defint = container_of(field, struct definition_integer, p);
dc48ecad 721 assert(defint->declaration->signedness == FALSE);
8c572eba 722 packet_index.content_size = defint->value._unsigned;
dc48ecad
MD
723 } else {
724 /* Use file size for packet size */
8c572eba 725 packet_index.content_size = filestats.st_size * CHAR_BIT;
dc48ecad
MD
726 }
727
728 /* read packet size from header */
729 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size"));
730 if (len_index >= 0) {
731 struct definition_integer *defint;
b1a2f580 732 struct definition *field;
dc48ecad
MD
733
734 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
b1a2f580
MD
735 assert(field->declaration->id == CTF_TYPE_INTEGER);
736 defint = container_of(field, struct definition_integer, p);
dc48ecad 737 assert(defint->declaration->signedness == FALSE);
8c572eba 738 packet_index.packet_size = defint->value._unsigned;
dc48ecad
MD
739 } else {
740 /* Use content size if non-zero, else file size */
8c572eba 741 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
dc48ecad 742 }
0f980a35
MD
743 } else {
744 /* Use file size for packet size */
8c572eba 745 packet_index.content_size = filestats.st_size * CHAR_BIT;
0f980a35 746 /* Use content size if non-zero, else file size */
8c572eba 747 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
0f980a35 748 }
546293fa
MD
749
750 /* Validate content size and packet size values */
751 if (packet_index.content_size > packet_index.packet_size) {
752 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
753 packet_index.content_size, packet_index.packet_size);
754 return -EINVAL;
755 }
756
58b0b883
MD
757 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
758 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
759 packet_index.content_size, (filestats.st_size - packet_index.offset) * CHAR_BIT);
546293fa
MD
760 return -EINVAL;
761 }
762
847bf71a
MD
763 /* Save position after header and context */
764 packet_index.data_offset = pos->offset;
0f980a35 765
0f980a35
MD
766 /* add index to packet array */
767 g_array_append_val(file_stream->pos.packet_index, packet_index);
768
8c572eba 769 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
0f980a35
MD
770 }
771
847bf71a
MD
772 /* Move pos back to beginning of file */
773 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
774
0f980a35
MD
775 return 0;
776}
777
778/*
779 * Note: many file streams can inherit from the same stream class
780 * description (metadata).
781 */
782static
46322b33 783int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
0f980a35
MD
784{
785 int ret;
786 struct ctf_file_stream *file_stream;
787
46322b33 788 ret = openat(td->dirfd, path, flags);
0f980a35
MD
789 if (ret < 0)
790 goto error;
791 file_stream = g_new0(struct ctf_file_stream, 1);
8563e754 792 ctf_init_pos(&file_stream->pos, ret, flags);
0f980a35
MD
793 ret = create_stream_packet_index(td, file_stream);
794 if (ret)
795 goto error_index;
796 /* Add stream file to stream class */
764af3f4 797 g_ptr_array_add(file_stream->stream.stream_class->files, file_stream);
0f980a35
MD
798 return 0;
799
800error_index:
46322b33 801 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
802 close(file_stream->pos.fd);
803 g_free(file_stream);
804error:
65102a8c
MD
805 return ret;
806}
807
bbefb8dd 808static
46322b33 809int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
bbefb8dd
MD
810{
811 int ret;
65102a8c
MD
812 struct dirent *dirent;
813 struct dirent *diriter;
814 size_t dirent_len;
bbefb8dd 815
46322b33 816 td->flags = flags;
bbefb8dd
MD
817
818 /* Open trace directory */
46322b33
MD
819 td->dir = opendir(path);
820 if (!td->dir) {
dc48ecad 821 fprintf(stdout, "[error] Unable to open trace directory.\n");
bbefb8dd
MD
822 ret = -ENOENT;
823 goto error;
824 }
825
46322b33
MD
826 td->dirfd = open(path, 0);
827 if (td->dirfd < 0) {
dc48ecad 828 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
65102a8c
MD
829 ret = -ENOENT;
830 goto error_dirfd;
831 }
0f980a35 832
65102a8c
MD
833 /*
834 * Keep the metadata file separate.
835 */
bbefb8dd 836
65102a8c
MD
837 ret = ctf_open_trace_metadata_read(td);
838 if (ret) {
839 goto error_metadata;
840 }
bbefb8dd
MD
841
842 /*
843 * Open each stream: for each file, try to open, check magic
844 * number, and get the stream ID to add to the right location in
845 * the stream array.
bbefb8dd
MD
846 */
847
65102a8c 848 dirent_len = offsetof(struct dirent, d_name) +
46322b33 849 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 850
65102a8c 851 dirent = malloc(dirent_len);
bbefb8dd 852
65102a8c 853 for (;;) {
46322b33 854 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 855 if (ret) {
dc48ecad 856 fprintf(stdout, "[error] Readdir error.\n");
65102a8c 857 goto readdir_error;
65102a8c
MD
858 }
859 if (!diriter)
860 break;
d8ea2d29
MD
861 /* Ignore hidden files, ., .. and metadata. */
862 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
863 || !strcmp(diriter->d_name, "..")
864 || !strcmp(diriter->d_name, "metadata"))
865 continue;
dc48ecad
MD
866 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
867 if (ret) {
868 fprintf(stdout, "[error] Open file stream error.\n");
869 goto readdir_error;
870 }
65102a8c 871 }
bbefb8dd 872
65102a8c 873 free(dirent);
bbefb8dd 874 return 0;
65102a8c
MD
875
876readdir_error:
877 free(dirent);
878error_metadata:
46322b33 879 close(td->dirfd);
65102a8c 880error_dirfd:
46322b33 881 closedir(td->dir);
bbefb8dd
MD
882error:
883 return ret;
884}
885
bbefb8dd
MD
886struct trace_descriptor *ctf_open_trace(const char *path, int flags)
887{
46322b33 888 struct ctf_trace *td;
bbefb8dd
MD
889 int ret;
890
46322b33 891 td = g_new0(struct ctf_trace, 1);
bbefb8dd 892
8c572eba 893 switch (flags & O_ACCMODE) {
bbefb8dd 894 case O_RDONLY:
b61922b5
MD
895 if (!path) {
896 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
897 goto error;
898 }
bbefb8dd
MD
899 ret = ctf_open_trace_read(td, path, flags);
900 if (ret)
901 goto error;
902 break;
989c73bc 903 case O_RDWR:
46322b33
MD
904 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
905 goto error;
bbefb8dd 906 default:
46322b33 907 fprintf(stdout, "[error] Incorrect open flags.\n");
bbefb8dd
MD
908 goto error;
909 }
910
46322b33 911 return &td->parent;
bbefb8dd
MD
912error:
913 g_free(td);
914 return NULL;
915}
916
0f980a35
MD
917static
918void ctf_close_file_stream(struct ctf_file_stream *file_stream)
919{
46322b33 920 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
921 close(file_stream->pos.fd);
922}
923
46322b33 924void ctf_close_trace(struct trace_descriptor *tdp)
bbefb8dd 925{
46322b33 926 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
0f980a35
MD
927 int i;
928
46322b33
MD
929 if (td->streams) {
930 for (i = 0; i < td->streams->len; i++) {
aa6bffae 931 struct ctf_stream_class *stream;
0f980a35 932 int j;
46322b33 933 stream = g_ptr_array_index(td->streams, i);
0f980a35
MD
934 for (j = 0; j < stream->files->len; j++) {
935 struct ctf_file_stream *file_stream;
2c117823 936 file_stream = g_ptr_array_index(stream->files, j);
0f980a35
MD
937 ctf_close_file_stream(file_stream);
938 }
939
940 }
46322b33 941 g_ptr_array_free(td->streams, TRUE);
0f980a35 942 }
46322b33 943 closedir(td->dir);
bbefb8dd
MD
944 g_free(td);
945}
946
7fb21036 947void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
948{
949 int ret;
950
4c8bfb7e 951 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
952 ret = bt_register_format(&ctf_format);
953 assert(!ret);
954}
698f0fe4
MD
955
956/* TODO: finalize */
This page took 0.068366 seconds and 4 git commands to generate.