e375039b84c1bbf132add88d1dd9d29afc84e043
[babeltrace.git] / formats / ctf / ctf.c
1 /*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * Format registration.
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/format.h>
20 #include <babeltrace/ctf/types.h>
21 #include <babeltrace/ctf/metadata.h>
22 #include <babeltrace/babeltrace.h>
23 #include <inttypes.h>
24 #include <uuid/uuid.h>
25 #include <sys/mman.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <dirent.h>
31 #include <glib.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34
35 #include "metadata/ctf-scanner.h"
36 #include "metadata/ctf-parser.h"
37 #include "metadata/ctf-ast.h"
38
39 /*
40 * We currently simply map a page to read the packet header and packet
41 * context to get the packet length and content length. (in bits)
42 */
43 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
44 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
45 #define UUID_LEN 16 /* uuid by value len */
46
47 extern int yydebug;
48
49 struct trace_descriptor *ctf_open_trace(const char *path, int flags);
50 void ctf_close_trace(struct trace_descriptor *descriptor);
51
52 static
53 rw_dispatch read_dispatch_table[] = {
54 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
55 [ CTF_TYPE_FLOAT ] = ctf_float_read,
56 [ CTF_TYPE_ENUM ] = ctf_enum_read,
57 [ CTF_TYPE_STRING ] = ctf_string_read,
58 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
59 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
60 [ CTF_TYPE_ARRAY ] = ctf_array_rw,
61 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_rw,
62 };
63
64 static
65 rw_dispatch write_dispatch_table[] = {
66 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
67 [ CTF_TYPE_FLOAT ] = ctf_float_write,
68 [ CTF_TYPE_ENUM ] = ctf_enum_write,
69 [ CTF_TYPE_STRING ] = ctf_string_write,
70 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
71 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
72 [ CTF_TYPE_ARRAY ] = ctf_array_rw,
73 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_rw,
74 };
75
76 static
77 struct format ctf_format = {
78 .open_trace = ctf_open_trace,
79 .close_trace = ctf_close_trace,
80 };
81
82 void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
83 {
84 pos->fd = fd;
85 pos->mmap_offset = 0;
86 pos->packet_size = 0;
87 pos->content_size = 0;
88 pos->content_size_loc = NULL;
89 pos->base = NULL;
90 pos->offset = 0;
91 pos->dummy = false;
92 pos->cur_index = 0;
93 if (fd >= 0)
94 pos->packet_index = g_array_new(FALSE, TRUE,
95 sizeof(struct packet_index));
96 else
97 pos->packet_index = NULL;
98 switch (open_flags & O_ACCMODE) {
99 case O_RDONLY:
100 pos->prot = PROT_READ;
101 pos->flags = MAP_PRIVATE;
102 pos->parent.rw_table = read_dispatch_table;
103 break;
104 case O_WRONLY:
105 case O_RDWR:
106 pos->prot = PROT_WRITE; /* Write has priority */
107 pos->flags = MAP_SHARED;
108 pos->parent.rw_table = write_dispatch_table;
109 if (fd >= 0)
110 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
111 break;
112 default:
113 assert(0);
114 }
115 }
116
117 void ctf_fini_pos(struct ctf_stream_pos *pos)
118 {
119 int ret;
120
121 if (pos->prot == PROT_WRITE && pos->content_size_loc)
122 *pos->content_size_loc = pos->offset;
123 if (pos->base) {
124 /* unmap old base */
125 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
126 if (ret) {
127 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
128 strerror(errno));
129 assert(0);
130 }
131 }
132 (void) g_array_free(pos->packet_index, TRUE);
133 }
134
135 void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
136 {
137 int ret;
138 off_t off;
139 struct packet_index *index;
140
141 /* Only allow random seek in read mode */
142 assert(pos->prot != PROT_WRITE || whence == SEEK_CUR);
143
144 if (pos->prot == PROT_WRITE && pos->content_size_loc)
145 *pos->content_size_loc = pos->offset;
146
147 if (pos->base) {
148 /* unmap old base */
149 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
150 if (ret) {
151 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
152 strerror(errno));
153 assert(0);
154 }
155 pos->base = NULL;
156 }
157
158 /*
159 * The caller should never ask for ctf_move_pos across packets,
160 * except to get exactly at the beginning of the next packet.
161 */
162 if (pos->prot == PROT_WRITE) {
163 /* The writer will add padding */
164 assert(pos->offset + offset == pos->packet_size);
165
166 /*
167 * Don't increment for initial stream move (only condition where
168 * pos->offset can be 0.
169 */
170 if (pos->offset)
171 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
172 pos->content_size = -1U; /* Unknown at this point */
173 pos->packet_size = WRITE_PACKET_LEN;
174 off = posix_fallocate(pos->fd, pos->mmap_offset, pos->packet_size / CHAR_BIT);
175 assert(off >= 0);
176 pos->offset = 0;
177 } else {
178 switch (whence) {
179 case SEEK_CUR:
180 /* The reader will expect us to skip padding */
181 assert(pos->offset + offset == pos->content_size);
182 ++pos->cur_index;
183 break;
184 case SEEK_SET:
185 assert(offset == 0); /* only seek supported for now */
186 pos->cur_index = 0;
187 break;
188 default:
189 assert(0);
190 }
191 if (pos->cur_index >= pos->packet_index->len) {
192 pos->offset = -EOF;
193 return;
194 }
195 index = &g_array_index(pos->packet_index, struct packet_index,
196 pos->cur_index);
197 pos->mmap_offset = index->offset;
198
199 /* Lookup context/packet size in index */
200 pos->content_size = index->content_size;
201 pos->packet_size = index->packet_size;
202 pos->offset = index->data_offset;
203 }
204 /* map new base. Need mapping length from header. */
205 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
206 pos->flags, pos->fd, pos->mmap_offset);
207 if (pos->base == MAP_FAILED) {
208 fprintf(stdout, "[error] mmap error %s.\n",
209 strerror(errno));
210 assert(0);
211 }
212 }
213
214 /*
215 * TODO: for now, we treat the metadata file as a simple text file
216 * (without any header nor packets nor padding).
217 */
218 static
219 int ctf_open_trace_metadata_read(struct ctf_trace *td)
220 {
221 struct ctf_scanner *scanner;
222 FILE *fp;
223 int ret = 0;
224
225 td->metadata.pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
226 if (td->metadata.pos.fd < 0) {
227 fprintf(stdout, "Unable to open metadata.\n");
228 return td->metadata.pos.fd;
229 }
230
231 if (babeltrace_debug)
232 yydebug = 1;
233
234 fp = fdopen(td->metadata.pos.fd, "r");
235 if (!fp) {
236 fprintf(stdout, "[error] Unable to open metadata stream.\n");
237 ret = -errno;
238 goto end_stream;
239 }
240
241 scanner = ctf_scanner_alloc(fp);
242 if (!scanner) {
243 fprintf(stdout, "[error] Error allocating scanner\n");
244 ret = -ENOMEM;
245 goto end_scanner_alloc;
246 }
247 ret = ctf_scanner_append_ast(scanner);
248 if (ret) {
249 fprintf(stdout, "[error] Error creating AST\n");
250 goto end;
251 }
252
253 if (babeltrace_debug) {
254 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
255 if (ret) {
256 fprintf(stdout, "[error] Error visiting AST for XML output\n");
257 goto end;
258 }
259 }
260
261 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
262 if (ret) {
263 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
264 goto end;
265 }
266 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
267 td, BYTE_ORDER);
268 if (ret) {
269 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
270 goto end;
271 }
272 end:
273 ctf_scanner_free(scanner);
274 end_scanner_alloc:
275 fclose(fp);
276 end_stream:
277 close(td->metadata.pos.fd);
278 return ret;
279 }
280
281
282 static
283 int create_stream_packet_index(struct ctf_trace *td,
284 struct ctf_file_stream *file_stream)
285 {
286 struct ctf_stream *stream;
287 int len_index;
288 struct ctf_stream_pos *pos;
289 struct stat filestats;
290 struct packet_index packet_index;
291 int first_packet = 1;
292 int ret;
293
294 pos = &file_stream->pos;
295
296 ret = fstat(pos->fd, &filestats);
297 if (ret < 0)
298 return ret;
299
300 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
301 uint64_t stream_id = 0;
302
303 if (pos->base) {
304 /* unmap old base */
305 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
306 if (ret) {
307 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
308 strerror(errno));
309 return ret;
310 }
311 pos->base = NULL;
312 }
313 /* map new base. Need mapping length from header. */
314 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
315 MAP_PRIVATE, pos->fd, pos->mmap_offset);
316 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
317 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
318 pos->offset = 0; /* Position of the packet header */
319
320 packet_index.offset = pos->mmap_offset;
321 packet_index.content_size = 0;
322 packet_index.packet_size = 0;
323
324 /* read and check header, set stream id (and check) */
325 if (td->packet_header) {
326 /* Read packet header */
327 generic_rw(&pos->parent, &td->packet_header->p);
328
329 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic"));
330 if (len_index >= 0) {
331 struct definition_integer *defint;
332 struct field *field;
333
334 field = struct_definition_get_field_from_index(td->packet_header, len_index);
335 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
336 defint = container_of(field->definition, struct definition_integer, p);
337 assert(defint->declaration->signedness == FALSE);
338 if (defint->value._unsigned != CTF_MAGIC) {
339 fprintf(stdout, "[error] Invalid magic number %" PRIX64 " at packet %u (file offset %zd).\n",
340 defint->value._unsigned,
341 file_stream->pos.packet_index->len,
342 (ssize_t) pos->mmap_offset);
343 return -EINVAL;
344 }
345 }
346
347 /* check uuid */
348 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("trace_uuid"));
349 if (len_index >= 0) {
350 struct definition_array *defarray;
351 struct field *field;
352 uint64_t i;
353 uint8_t uuidval[UUID_LEN];
354
355 field = struct_definition_get_field_from_index(td->packet_header, len_index);
356 assert(field->definition->declaration->id == CTF_TYPE_ARRAY);
357 defarray = container_of(field->definition, struct definition_array, p);
358 assert(array_len(defarray) == UUID_LEN);
359 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
360
361 for (i = 0; i < UUID_LEN; i++) {
362 struct definition *elem;
363 struct definition_integer *defint;
364
365 elem = array_index(defarray, i);
366 assert(elem);
367 defint = container_of(elem, struct definition_integer, p);
368 uuidval[i] = defint->value._unsigned;
369 }
370 ret = uuid_compare(td->uuid, uuidval);
371 if (ret) {
372 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
373 return -EINVAL;
374 }
375 }
376
377
378 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("stream_id"));
379 if (len_index >= 0) {
380 struct definition_integer *defint;
381 struct field *field;
382
383 field = struct_definition_get_field_from_index(td->packet_header, len_index);
384 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
385 defint = container_of(field->definition, struct definition_integer, p);
386 assert(defint->declaration->signedness == FALSE);
387 stream_id = defint->value._unsigned;
388 }
389 }
390
391 if (!first_packet && file_stream->stream_id != stream_id) {
392 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
393 return -EINVAL;
394 }
395 if (first_packet) {
396 file_stream->stream_id = stream_id;
397 if (stream_id >= td->streams->len) {
398 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
399 return -EINVAL;
400 }
401 stream = g_ptr_array_index(td->streams, stream_id);
402 if (!stream) {
403 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
404 return -EINVAL;
405 }
406 file_stream->stream = stream;
407 }
408 first_packet = 0;
409
410 if (stream->packet_context) {
411 /* Read packet context */
412 generic_rw(&pos->parent, &stream->packet_context->p);
413
414 /* read content size from header */
415 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size"));
416 if (len_index >= 0) {
417 struct definition_integer *defint;
418 struct field *field;
419
420 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
421 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
422 defint = container_of(field->definition, struct definition_integer, p);
423 assert(defint->declaration->signedness == FALSE);
424 packet_index.content_size = defint->value._unsigned;
425 } else {
426 /* Use file size for packet size */
427 packet_index.content_size = filestats.st_size * CHAR_BIT;
428 }
429
430 /* read packet size from header */
431 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size"));
432 if (len_index >= 0) {
433 struct definition_integer *defint;
434 struct field *field;
435
436 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
437 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
438 defint = container_of(field->definition, struct definition_integer, p);
439 assert(defint->declaration->signedness == FALSE);
440 packet_index.packet_size = defint->value._unsigned;
441 } else {
442 /* Use content size if non-zero, else file size */
443 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
444 }
445 } else {
446 /* Use file size for packet size */
447 packet_index.content_size = filestats.st_size * CHAR_BIT;
448 /* Use content size if non-zero, else file size */
449 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
450 }
451 /* Save position after header and context */
452 packet_index.data_offset = pos->offset;
453
454 /* add index to packet array */
455 g_array_append_val(file_stream->pos.packet_index, packet_index);
456
457 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
458 }
459
460 /* Move pos back to beginning of file */
461 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
462
463 return 0;
464 }
465
466 /*
467 * Note: many file streams can inherit from the same stream class
468 * description (metadata).
469 */
470 static
471 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
472 {
473 int ret;
474 struct ctf_file_stream *file_stream;
475
476 ret = openat(td->dirfd, path, flags);
477 if (ret < 0)
478 goto error;
479 file_stream = g_new0(struct ctf_file_stream, 1);
480 ctf_init_pos(&file_stream->pos, ret, flags);
481 ret = create_stream_packet_index(td, file_stream);
482 if (ret)
483 goto error_index;
484 /* Add stream file to stream class */
485 g_ptr_array_add(file_stream->stream->files, file_stream);
486 return 0;
487
488 error_index:
489 ctf_fini_pos(&file_stream->pos);
490 close(file_stream->pos.fd);
491 g_free(file_stream);
492 error:
493 return ret;
494 }
495
496 static
497 int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
498 {
499 int ret;
500 struct dirent *dirent;
501 struct dirent *diriter;
502 size_t dirent_len;
503
504 td->flags = flags;
505
506 /* Open trace directory */
507 td->dir = opendir(path);
508 if (!td->dir) {
509 fprintf(stdout, "[error] Unable to open trace directory.\n");
510 ret = -ENOENT;
511 goto error;
512 }
513
514 td->dirfd = open(path, 0);
515 if (td->dirfd < 0) {
516 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
517 ret = -ENOENT;
518 goto error_dirfd;
519 }
520
521 td->streams = g_ptr_array_new();
522
523 /*
524 * Keep the metadata file separate.
525 */
526
527 ret = ctf_open_trace_metadata_read(td);
528 if (ret) {
529 goto error_metadata;
530 }
531
532 /*
533 * Open each stream: for each file, try to open, check magic
534 * number, and get the stream ID to add to the right location in
535 * the stream array.
536 */
537
538 dirent_len = offsetof(struct dirent, d_name) +
539 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
540
541 dirent = malloc(dirent_len);
542
543 for (;;) {
544 ret = readdir_r(td->dir, dirent, &diriter);
545 if (ret) {
546 fprintf(stdout, "[error] Readdir error.\n");
547 goto readdir_error;
548 }
549 if (!diriter)
550 break;
551 if (!strcmp(diriter->d_name, ".")
552 || !strcmp(diriter->d_name, "..")
553 || !strcmp(diriter->d_name, "metadata"))
554 continue;
555 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
556 if (ret) {
557 fprintf(stdout, "[error] Open file stream error.\n");
558 goto readdir_error;
559 }
560 }
561
562 free(dirent);
563 return 0;
564
565 readdir_error:
566 free(dirent);
567 error_metadata:
568 g_ptr_array_free(td->streams, TRUE);
569 close(td->dirfd);
570 error_dirfd:
571 closedir(td->dir);
572 error:
573 return ret;
574 }
575
576 static
577 int ctf_open_trace_write(struct ctf_trace *td, const char *path, int flags)
578 {
579 int ret;
580
581 ret = mkdir(path, S_IRWXU|S_IRWXG);
582 if (ret)
583 return ret;
584
585 /* Open trace directory */
586 td->dir = opendir(path);
587 if (!td->dir) {
588 fprintf(stdout, "[error] Unable to open trace directory.\n");
589 ret = -ENOENT;
590 goto error;
591 }
592
593
594 return 0;
595
596 error:
597 return ret;
598 }
599
600 struct trace_descriptor *ctf_open_trace(const char *path, int flags)
601 {
602 struct ctf_trace *td;
603 int ret;
604
605 td = g_new0(struct ctf_trace, 1);
606
607 switch (flags & O_ACCMODE) {
608 case O_RDONLY:
609 ret = ctf_open_trace_read(td, path, flags);
610 if (ret)
611 goto error;
612 break;
613 case O_WRONLY:
614 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
615 goto error;
616 #if 0
617 ret = ctf_open_trace_write(td, path, flags);
618 if (ret)
619 goto error;
620 #endif //0
621 break;
622 default:
623 fprintf(stdout, "[error] Incorrect open flags.\n");
624 goto error;
625 }
626
627 return &td->parent;
628 error:
629 g_free(td);
630 return NULL;
631 }
632
633 static
634 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
635 {
636 ctf_fini_pos(&file_stream->pos);
637 close(file_stream->pos.fd);
638 }
639
640 void ctf_close_trace(struct trace_descriptor *tdp)
641 {
642 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
643 int i;
644
645 if (td->streams) {
646 for (i = 0; i < td->streams->len; i++) {
647 struct ctf_stream *stream;
648 int j;
649 stream = g_ptr_array_index(td->streams, i);
650 for (j = 0; j < stream->files->len; j++) {
651 struct ctf_file_stream *file_stream;
652 file_stream = g_ptr_array_index(stream->files, j);
653 ctf_close_file_stream(file_stream);
654 }
655
656 }
657 g_ptr_array_free(td->streams, TRUE);
658 }
659 closedir(td->dir);
660 g_free(td);
661 }
662
663 void __attribute__((constructor)) ctf_init(void)
664 {
665 int ret;
666
667 ctf_format.name = g_quark_from_static_string("ctf");
668 ret = bt_register_format(&ctf_format);
669 assert(!ret);
670 }
671
672 /* TODO: finalize */
This page took 0.04165 seconds and 3 git commands to generate.