2 * BabelTrace - Common Trace Format (CTF)
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/format.h>
20 #include <babeltrace/ctf/types.h>
21 #include <babeltrace/ctf/metadata.h>
22 #include <babeltrace/babeltrace.h>
24 #include <uuid/uuid.h>
27 #include <sys/types.h>
35 #include "metadata/ctf-scanner.h"
36 #include "metadata/ctf-parser.h"
37 #include "metadata/ctf-ast.h"
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)
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 */
49 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
);
50 void ctf_close_trace(struct trace_descriptor
*descriptor
);
52 static rw_dispatch read_dispatch_table
[] = {
53 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
54 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
55 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
56 [ CTF_TYPE_STRING
] = ctf_string_read
,
57 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
58 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
59 [ CTF_TYPE_ARRAY
] = ctf_array_rw
,
60 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_rw
,
63 static rw_dispatch write_dispatch_table
[] = {
64 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
65 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
66 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
67 [ CTF_TYPE_STRING
] = ctf_string_write
,
68 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
69 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
70 [ CTF_TYPE_ARRAY
] = ctf_array_rw
,
71 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_rw
,
74 struct format ctf_format
= {
75 .open_trace
= ctf_open_trace
,
76 .close_trace
= ctf_close_trace
,
79 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
84 pos
->content_size
= 0;
85 pos
->content_size_loc
= NULL
;
91 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
92 sizeof(struct packet_index
));
94 pos
->packet_index
= NULL
;
95 switch (open_flags
& O_ACCMODE
) {
97 pos
->prot
= PROT_READ
;
98 pos
->flags
= MAP_PRIVATE
;
99 pos
->parent
.rw_table
= read_dispatch_table
;
103 pos
->prot
= PROT_WRITE
; /* Write has priority */
104 pos
->flags
= MAP_SHARED
;
105 pos
->parent
.rw_table
= write_dispatch_table
;
107 ctf_move_pos_slow(pos
, 0); /* position for write */
114 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
118 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
119 *pos
->content_size_loc
= pos
->offset
;
122 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
124 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
129 (void) g_array_free(pos
->packet_index
, TRUE
);
132 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
)
136 struct packet_index
*index
;
139 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
140 *pos
->content_size_loc
= pos
->offset
;
144 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
146 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
153 * The caller should never ask for ctf_move_pos across packets,
154 * except to get exactly at the beginning of the next packet.
156 if (pos
->prot
== PROT_WRITE
) {
157 /* The writer will add padding */
158 assert(pos
->offset
+ offset
== pos
->packet_size
);
161 * Don't increment for initial stream move (only condition where
162 * pos->offset can be 0.
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
);
171 /* The reader will expect us to skip padding */
172 assert(pos
->offset
+ offset
== pos
->content_size
);
175 * Don't increment for initial stream move (only condition where
176 * pos->offset can be 0).
180 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
182 pos
->mmap_offset
= index
->offset
;
184 /* Lookup context/packet size in index */
185 pos
->content_size
= index
->content_size
;
186 pos
->packet_size
= index
->packet_size
;
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
);
195 * TODO: for now, we treat the metadata file as a simple text file
196 * (without any header nor packets nor padding).
199 int ctf_open_trace_metadata_read(struct ctf_trace
*td
)
201 struct ctf_scanner
*scanner
;
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
;
211 if (babeltrace_debug
)
214 fp
= fdopen(td
->metadata
.pos
.fd
, "r");
216 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
221 scanner
= ctf_scanner_alloc(fp
);
223 fprintf(stdout
, "[error] Error allocating scanner\n");
225 goto end_scanner_alloc
;
227 ret
= ctf_scanner_append_ast(scanner
);
229 fprintf(stdout
, "[error] Error creating AST\n");
233 if (babeltrace_debug
) {
234 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
236 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
241 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
243 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
246 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
249 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
253 ctf_scanner_free(scanner
);
257 close(td
->metadata
.pos
.fd
);
263 int create_stream_packet_index(struct ctf_trace
*td
,
264 struct ctf_file_stream
*file_stream
)
266 struct ctf_stream
*stream
;
268 struct ctf_stream_pos
*pos
;
269 struct stat filestats
;
270 struct packet_index packet_index
;
271 int first_packet
= 1;
274 pos
= &file_stream
->pos
;
276 ret
= fstat(pos
->fd
, &filestats
);
280 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
281 uint64_t stream_id
= 0;
285 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
287 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
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 */
300 packet_index
.offset
= pos
->mmap_offset
;
301 packet_index
.content_size
= 0;
302 packet_index
.packet_size
= 0;
304 /* read and check header, set stream id (and check) */
305 if (td
->packet_header
) {
306 /* Read packet header */
307 generic_rw(&pos
->parent
, &td
->packet_header
->p
);
309 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("magic"));
310 if (len_index
>= 0) {
311 struct definition_integer
*defint
;
314 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
315 assert(field
->definition
->declaration
->id
== CTF_TYPE_INTEGER
);
316 defint
= container_of(field
->definition
, struct definition_integer
, p
);
317 assert(defint
->declaration
->signedness
== FALSE
);
318 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
319 fprintf(stdout
, "[error] Invalid magic number %" PRIX64
" at packet %u (file offset %zd).\n",
320 defint
->value
._unsigned
,
321 file_stream
->pos
.packet_index
->len
,
322 (ssize_t
) pos
->mmap_offset
);
328 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("trace_uuid"));
329 if (len_index
>= 0) {
330 struct definition_array
*defarray
;
333 uint8_t uuidval
[UUID_LEN
];
335 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
336 assert(field
->definition
->declaration
->id
== CTF_TYPE_ARRAY
);
337 defarray
= container_of(field
->definition
, struct definition_array
, p
);
338 assert(array_len(defarray
) == UUID_LEN
);
339 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
341 for (i
= 0; i
< UUID_LEN
; i
++) {
342 struct definition
*elem
;
343 struct definition_integer
*defint
;
345 elem
= array_index(defarray
, i
);
347 defint
= container_of(elem
, struct definition_integer
, p
);
348 uuidval
[i
] = defint
->value
._unsigned
;
350 ret
= uuid_compare(td
->uuid
, uuidval
);
352 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
358 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("stream_id"));
359 if (len_index
>= 0) {
360 struct definition_integer
*defint
;
363 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
364 assert(field
->definition
->declaration
->id
== CTF_TYPE_INTEGER
);
365 defint
= container_of(field
->definition
, struct definition_integer
, p
);
366 assert(defint
->declaration
->signedness
== FALSE
);
367 stream_id
= defint
->value
._unsigned
;
371 if (!first_packet
&& file_stream
->stream_id
!= stream_id
) {
372 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
376 file_stream
->stream_id
= stream_id
;
377 if (stream_id
>= td
->streams
->len
) {
378 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
381 stream
= g_ptr_array_index(td
->streams
, stream_id
);
383 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
386 file_stream
->stream
= stream
;
390 if (stream
->packet_context
) {
391 /* Read packet context */
392 generic_rw(&pos
->parent
, &stream
->packet_context
->p
);
394 /* read content size from header */
395 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("content_size"));
396 if (len_index
>= 0) {
397 struct definition_integer
*defint
;
400 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
401 assert(field
->definition
->declaration
->id
== CTF_TYPE_INTEGER
);
402 defint
= container_of(field
->definition
, struct definition_integer
, p
);
403 assert(defint
->declaration
->signedness
== FALSE
);
404 packet_index
.content_size
= defint
->value
._unsigned
;
406 /* Use file size for packet size */
407 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
410 /* read packet size from header */
411 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("packet_size"));
412 if (len_index
>= 0) {
413 struct definition_integer
*defint
;
416 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
417 assert(field
->definition
->declaration
->id
== CTF_TYPE_INTEGER
);
418 defint
= container_of(field
->definition
, struct definition_integer
, p
);
419 assert(defint
->declaration
->signedness
== FALSE
);
420 packet_index
.packet_size
= defint
->value
._unsigned
;
422 /* Use content size if non-zero, else file size */
423 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
426 /* Use file size for packet size */
427 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
428 /* Use content size if non-zero, else file size */
429 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
432 /* add index to packet array */
433 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
435 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
442 * Note: many file streams can inherit from the same stream class
443 * description (metadata).
446 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
)
449 struct ctf_file_stream
*file_stream
;
451 ret
= openat(td
->dirfd
, path
, flags
);
454 file_stream
= g_new0(struct ctf_file_stream
, 1);
455 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
456 ret
= create_stream_packet_index(td
, file_stream
);
459 /* Add stream file to stream class */
460 g_ptr_array_add(file_stream
->stream
->files
, file_stream
);
464 ctf_fini_pos(&file_stream
->pos
);
465 close(file_stream
->pos
.fd
);
472 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
)
475 struct dirent
*dirent
;
476 struct dirent
*diriter
;
481 /* Open trace directory */
482 td
->dir
= opendir(path
);
484 fprintf(stdout
, "[error] Unable to open trace directory.\n");
489 td
->dirfd
= open(path
, 0);
491 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
496 td
->streams
= g_ptr_array_new();
499 * Keep the metadata file separate.
502 ret
= ctf_open_trace_metadata_read(td
);
508 * Open each stream: for each file, try to open, check magic
509 * number, and get the stream ID to add to the right location in
513 dirent_len
= offsetof(struct dirent
, d_name
) +
514 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
516 dirent
= malloc(dirent_len
);
519 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
521 fprintf(stdout
, "[error] Readdir error.\n");
526 if (!strcmp(diriter
->d_name
, ".")
527 || !strcmp(diriter
->d_name
, "..")
528 || !strcmp(diriter
->d_name
, "metadata"))
530 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
);
532 fprintf(stdout
, "[error] Open file stream error.\n");
543 g_ptr_array_free(td
->streams
, TRUE
);
552 int ctf_open_trace_write(struct ctf_trace
*td
, const char *path
, int flags
)
556 ret
= mkdir(path
, S_IRWXU
|S_IRWXG
);
560 /* Open trace directory */
561 td
->dir
= opendir(path
);
563 fprintf(stdout
, "[error] Unable to open trace directory.\n");
575 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
577 struct ctf_trace
*td
;
580 td
= g_new0(struct ctf_trace
, 1);
582 switch (flags
& O_ACCMODE
) {
584 ret
= ctf_open_trace_read(td
, path
, flags
);
589 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
592 ret
= ctf_open_trace_write(td
, path
, flags
);
598 fprintf(stdout
, "[error] Incorrect open flags.\n");
609 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
611 ctf_fini_pos(&file_stream
->pos
);
612 close(file_stream
->pos
.fd
);
615 void ctf_close_trace(struct trace_descriptor
*tdp
)
617 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
621 for (i
= 0; i
< td
->streams
->len
; i
++) {
622 struct ctf_stream
*stream
;
624 stream
= g_ptr_array_index(td
->streams
, i
);
625 for (j
= 0; j
< stream
->files
->len
; j
++) {
626 struct ctf_file_stream
*file_stream
;
627 file_stream
= g_ptr_array_index(stream
->files
, j
);
628 ctf_close_file_stream(file_stream
);
632 g_ptr_array_free(td
->streams
, TRUE
);
638 void __attribute__((constructor
)) ctf_init(void)
642 ctf_format
.name
= g_quark_from_static_string("ctf");
643 ret
= bt_register_format(&ctf_format
);