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.
43 #define MAX_PACKET_HEADER_LEN getpagesize()
44 #define UUID_LEN 16 /* uuid by value len */
48 struct trace_descriptor
{
49 struct ctf_trace ctf_trace
;
52 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
);
53 void ctf_close_trace(struct trace_descriptor
*descriptor
);
55 static struct format ctf_format
= {
56 .uint_read
= ctf_uint_read
,
57 .int_read
= ctf_int_read
,
58 .uint_write
= ctf_uint_write
,
59 .int_write
= ctf_int_write
,
60 .double_read
= ctf_double_read
,
61 .double_write
= ctf_double_write
,
62 .ldouble_read
= ctf_ldouble_read
,
63 .ldouble_write
= ctf_ldouble_write
,
64 .float_copy
= ctf_float_copy
,
65 .string_copy
= ctf_string_copy
,
66 .string_read
= ctf_string_read
,
67 .string_write
= ctf_string_write
,
68 .string_free_temp
= ctf_string_free_temp
,
69 .enum_read
= ctf_enum_read
,
70 .enum_write
= ctf_enum_write
,
71 .struct_begin
= ctf_struct_begin
,
72 .struct_end
= ctf_struct_end
,
73 .variant_begin
= ctf_variant_begin
,
74 .variant_end
= ctf_variant_end
,
75 .array_begin
= ctf_array_begin
,
76 .array_end
= ctf_array_end
,
77 .sequence_begin
= ctf_sequence_begin
,
78 .sequence_end
= ctf_sequence_end
,
79 .open_trace
= ctf_open_trace
,
80 .close_trace
= ctf_close_trace
,
83 void move_pos_slow(struct stream_pos
*pos
, size_t offset
)
88 * The caller should never ask for move_pos across packets,
89 * except to get exactly at the beginning of the next packet.
91 assert(pos
->offset
+ offset
== pos
->content_size
);
95 ret
= munmap(pos
->base
, pos
->packet_size
);
97 fprintf(stdout
, "Unable to unmap old base: %s.\n",
103 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
104 /* map new base. Need mapping length from header. */
105 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
, PROT_READ
,
106 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
107 pos
->content_size
= 0; /* Unknown at this point */
108 pos
->packet_size
= 0; /* Unknown at this point */
113 * TODO: for now, we treat the metadata file as a simple text file
114 * (without any header nor packets nor padding).
117 int ctf_open_trace_metadata_read(struct trace_descriptor
*td
)
119 struct ctf_scanner
*scanner
;
123 td
->ctf_trace
.metadata
.pos
.fd
= openat(td
->ctf_trace
.dirfd
,
124 "metadata", O_RDONLY
);
125 if (td
->ctf_trace
.metadata
.pos
.fd
< 0) {
126 fprintf(stdout
, "Unable to open metadata.\n");
127 return td
->ctf_trace
.metadata
.pos
.fd
;
130 if (babeltrace_debug
)
133 fp
= fdopen(td
->ctf_trace
.metadata
.pos
.fd
, "r");
135 fprintf(stdout
, "Unable to open metadata stream.\n");
140 scanner
= ctf_scanner_alloc(fp
);
142 fprintf(stdout
, "Error allocating scanner\n");
144 goto end_scanner_alloc
;
146 ret
= ctf_scanner_append_ast(scanner
);
148 fprintf(stdout
, "Error creating AST\n");
152 if (babeltrace_debug
) {
153 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
155 fprintf(stdout
, "Error visiting AST for XML output\n");
160 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
162 fprintf(stdout
, "Error in CTF semantic validation %d\n", ret
);
165 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
166 &td
->ctf_trace
, BYTE_ORDER
);
168 fprintf(stdout
, "Error in CTF metadata constructor %d\n", ret
);
172 ctf_scanner_free(scanner
);
176 close(td
->ctf_trace
.metadata
.pos
.fd
);
182 int create_stream_packet_index(struct trace_descriptor
*td
,
183 struct ctf_file_stream
*file_stream
)
185 struct ctf_stream
*stream
;
187 struct stream_pos
*pos
;
188 struct stat filestats
;
189 struct packet_index packet_index
;
190 int first_packet
= 1;
193 pos
= &file_stream
->pos
;
195 ret
= fstat(pos
->fd
, &filestats
);
199 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
200 uint64_t stream_id
= 0;
204 ret
= munmap(pos
->base
, pos
->packet_size
);
206 fprintf(stdout
, "Unable to unmap old base: %s.\n",
211 /* map new base. Need mapping length from header. */
212 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
, PROT_READ
,
213 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
214 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
215 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
216 pos
->offset
= 0; /* Position of the packet header */
218 /* read and check header, set stream id (and check) */
219 if (td
->ctf_trace
.packet_header
) {
220 /* Read packet header */
221 td
->ctf_trace
.packet_header
->p
.declaration
->copy(NULL
, NULL
,
222 pos
, &ctf_format
, &td
->ctf_trace
.packet_header
->p
);
224 len_index
= struct_declaration_lookup_field_index(td
->ctf_trace
.packet_header
->declaration
, g_quark_from_static_string("magic"));
225 if (len_index
>= 0) {
226 struct definition_integer
*defint
;
229 field
= struct_definition_get_field_from_index(td
->ctf_trace
.packet_header
, len_index
);
230 assert(field
->definition
->declaration
->id
== CTF_TYPE_INTEGER
);
231 defint
= container_of(field
->definition
, struct definition_integer
, p
);
232 assert(defint
->declaration
->signedness
== FALSE
);
233 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
234 fprintf(stdout
, "[error] Invalid magic number %" PRIX64
".\n",
235 defint
->value
._unsigned
);
241 len_index
= struct_declaration_lookup_field_index(td
->ctf_trace
.packet_header
->declaration
, g_quark_from_static_string("trace_uuid"));
242 if (len_index
>= 0) {
243 struct definition_array
*defarray
;
246 uint8_t uuidval
[UUID_LEN
];
249 field
= struct_definition_get_field_from_index(td
->ctf_trace
.packet_header
, len_index
);
250 assert(field
->definition
->declaration
->id
== CTF_TYPE_ARRAY
);
251 defarray
= container_of(field
->definition
, struct definition_array
, p
);
252 assert(array_len(defarray
) == UUID_LEN
);
253 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
255 for (i
= 0; i
< UUID_LEN
; i
++) {
256 struct definition
*elem
;
257 struct definition_integer
*defint
;
259 elem
= array_index(defarray
, i
);
261 defint
= container_of(elem
, struct definition_integer
, p
);
262 uuidval
[i
] = defint
->value
._unsigned
;
264 ret
= uuid_compare(td
->ctf_trace
.uuid
, uuidval
);
266 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
272 len_index
= struct_declaration_lookup_field_index(td
->ctf_trace
.packet_header
->declaration
, g_quark_from_static_string("stream_id"));
273 if (len_index
>= 0) {
274 struct definition_integer
*defint
;
277 field
= struct_definition_get_field_from_index(td
->ctf_trace
.packet_header
, len_index
);
278 assert(field
->definition
->declaration
->id
== CTF_TYPE_INTEGER
);
279 defint
= container_of(field
->definition
, struct definition_integer
, p
);
280 assert(defint
->declaration
->signedness
== FALSE
);
281 stream_id
= defint
->value
._unsigned
;
285 if (!first_packet
&& file_stream
->stream_id
!= stream_id
) {
286 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
290 file_stream
->stream_id
= stream_id
;
291 if (stream_id
>= td
->ctf_trace
.streams
->len
) {
292 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
295 stream
= g_ptr_array_index(td
->ctf_trace
.streams
, stream_id
);
297 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
300 file_stream
->stream
= stream
;
304 if (stream
->packet_context
) {
305 /* Read packet context */
306 stream
->packet_context
->p
.declaration
->copy(NULL
, NULL
,
307 pos
, &ctf_format
, &stream
->packet_context
->p
);
309 /* read content size from header */
310 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("content_size"));
311 if (len_index
>= 0) {
312 struct definition_integer
*defint
;
315 field
= struct_definition_get_field_from_index(stream
->packet_context
, 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 pos
->content_size
= defint
->value
._unsigned
;
321 /* Use file size for packet size */
322 pos
->content_size
= filestats
.st_size
* CHAR_BIT
;
325 /* read packet size from header */
326 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("packet_size"));
327 if (len_index
>= 0) {
328 struct definition_integer
*defint
;
331 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
332 assert(field
->definition
->declaration
->id
== CTF_TYPE_INTEGER
);
333 defint
= container_of(field
->definition
, struct definition_integer
, p
);
334 assert(defint
->declaration
->signedness
== FALSE
);
335 pos
->packet_size
= defint
->value
._unsigned
;
337 /* Use content size if non-zero, else file size */
338 pos
->packet_size
= pos
->content_size
? : filestats
.st_size
* CHAR_BIT
;
341 /* Use file size for packet size */
342 pos
->content_size
= filestats
.st_size
* CHAR_BIT
;
343 /* Use content size if non-zero, else file size */
344 pos
->packet_size
= pos
->content_size
? : filestats
.st_size
* CHAR_BIT
;
347 packet_index
.offset
= pos
->mmap_offset
;
348 packet_index
.content_size
= pos
->content_size
;
349 packet_index
.packet_size
= pos
->packet_size
;
350 /* add index to packet array */
351 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
353 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
360 * Note: many file streams can inherit from the same stream class
361 * description (metadata).
364 int ctf_open_file_stream_read(struct trace_descriptor
*td
, const char *path
, int flags
)
367 struct ctf_file_stream
*file_stream
;
369 ret
= openat(td
->ctf_trace
.dirfd
, path
, flags
);
372 file_stream
= g_new0(struct ctf_file_stream
, 1);
373 file_stream
->pos
.fd
= ret
;
374 file_stream
->pos
.packet_index
= g_array_new(FALSE
, TRUE
,
375 sizeof(struct packet_index
));
376 ret
= create_stream_packet_index(td
, file_stream
);
379 /* Add stream file to stream class */
380 g_ptr_array_add(file_stream
->stream
->files
, file_stream
);
384 (void) g_array_free(file_stream
->pos
.packet_index
, TRUE
);
385 close(file_stream
->pos
.fd
);
392 int ctf_open_trace_read(struct trace_descriptor
*td
, const char *path
, int flags
)
395 struct dirent
*dirent
;
396 struct dirent
*diriter
;
399 td
->ctf_trace
.flags
= flags
;
401 /* Open trace directory */
402 td
->ctf_trace
.dir
= opendir(path
);
403 if (!td
->ctf_trace
.dir
) {
404 fprintf(stdout
, "[error] Unable to open trace directory.\n");
409 td
->ctf_trace
.dirfd
= open(path
, 0);
410 if (td
->ctf_trace
.dirfd
< 0) {
411 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
416 td
->ctf_trace
.streams
= g_ptr_array_new();
419 * Keep the metadata file separate.
422 ret
= ctf_open_trace_metadata_read(td
);
428 * Open each stream: for each file, try to open, check magic
429 * number, and get the stream ID to add to the right location in
433 dirent_len
= offsetof(struct dirent
, d_name
) +
434 fpathconf(td
->ctf_trace
.dirfd
, _PC_NAME_MAX
) + 1;
436 dirent
= malloc(dirent_len
);
439 ret
= readdir_r(td
->ctf_trace
.dir
, dirent
, &diriter
);
441 fprintf(stdout
, "[error] Readdir error.\n");
446 if (!strcmp(diriter
->d_name
, ".")
447 || !strcmp(diriter
->d_name
, "..")
448 || !strcmp(diriter
->d_name
, "metadata"))
450 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
);
452 fprintf(stdout
, "[error] Open file stream error.\n");
463 g_ptr_array_free(td
->ctf_trace
.streams
, TRUE
);
464 close(td
->ctf_trace
.dirfd
);
466 closedir(td
->ctf_trace
.dir
);
472 int ctf_open_trace_write(struct trace_descriptor
*td
, const char *path
, int flags
)
476 ret
= mkdir(path
, S_IRWXU
|S_IRWXG
);
480 /* Open trace directory */
481 td
->ctf_trace
.dir
= opendir(path
);
482 if (!td
->ctf_trace
.dir
) {
483 fprintf(stdout
, "Unable to open trace directory.\n");
495 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
497 struct trace_descriptor
*td
;
500 td
= g_new0(struct trace_descriptor
, 1);
504 ret
= ctf_open_trace_read(td
, path
, flags
);
509 ret
= ctf_open_trace_write(td
, path
, flags
);
514 fprintf(stdout
, "Incorrect open flags.\n");
525 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
527 (void) g_array_free(file_stream
->pos
.packet_index
, TRUE
);
528 close(file_stream
->pos
.fd
);
531 void ctf_close_trace(struct trace_descriptor
*td
)
535 if (td
->ctf_trace
.streams
) {
536 for (i
= 0; i
< td
->ctf_trace
.streams
->len
; i
++) {
537 struct ctf_stream
*stream
;
540 stream
= g_ptr_array_index(td
->ctf_trace
.streams
, i
);
541 for (j
= 0; j
< stream
->files
->len
; j
++) {
542 struct ctf_file_stream
*file_stream
;
543 file_stream
= g_ptr_array_index(td
->ctf_trace
.streams
, j
);
544 ctf_close_file_stream(file_stream
);
548 g_ptr_array_free(td
->ctf_trace
.streams
, TRUE
);
550 closedir(td
->ctf_trace
.dir
);
554 void __attribute__((constructor
)) ctf_init(void)
558 ctf_format
.name
= g_quark_from_static_string("ctf");
559 ret
= bt_register_format(&ctf_format
);