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