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 .float_copy
= ctf_float_copy
,
63 .string_copy
= ctf_string_copy
,
64 .string_read
= ctf_string_read
,
65 .string_write
= ctf_string_write
,
66 .string_free_temp
= ctf_string_free_temp
,
67 .enum_read
= ctf_enum_read
,
68 .enum_write
= ctf_enum_write
,
69 .struct_begin
= ctf_struct_begin
,
70 .struct_end
= ctf_struct_end
,
71 .variant_begin
= ctf_variant_begin
,
72 .variant_end
= ctf_variant_end
,
73 .array_begin
= ctf_array_begin
,
74 .array_end
= ctf_array_end
,
75 .sequence_begin
= ctf_sequence_begin
,
76 .sequence_end
= ctf_sequence_end
,
77 .open_trace
= ctf_open_trace
,
78 .close_trace
= ctf_close_trace
,
81 void move_pos_slow(struct stream_pos
*pos
, size_t offset
)
86 * The caller should never ask for move_pos across packets,
87 * except to get exactly at the beginning of the next packet.
89 assert(pos
->offset
+ offset
== pos
->content_size
);
93 ret
= munmap(pos
->base
, pos
->packet_size
);
95 fprintf(stdout
, "Unable to unmap old base: %s.\n",
101 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
102 /* map new base. Need mapping length from header. */
103 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
, PROT_READ
,
104 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
105 pos
->content_size
= 0; /* Unknown at this point */
106 pos
->packet_size
= 0; /* Unknown at this point */
111 * TODO: for now, we treat the metadata file as a simple text file
112 * (without any header nor packets nor padding).
115 int ctf_open_trace_metadata_read(struct trace_descriptor
*td
)
117 struct ctf_scanner
*scanner
;
121 td
->ctf_trace
.metadata
.pos
.fd
= openat(td
->ctf_trace
.dirfd
,
122 "metadata", O_RDONLY
);
123 if (td
->ctf_trace
.metadata
.pos
.fd
< 0) {
124 fprintf(stdout
, "Unable to open metadata.\n");
125 return td
->ctf_trace
.metadata
.pos
.fd
;
128 if (babeltrace_debug
)
131 fp
= fdopen(td
->ctf_trace
.metadata
.pos
.fd
, "r");
133 fprintf(stdout
, "Unable to open metadata stream.\n");
138 scanner
= ctf_scanner_alloc(fp
);
140 fprintf(stdout
, "Error allocating scanner\n");
142 goto end_scanner_alloc
;
144 ret
= ctf_scanner_append_ast(scanner
);
146 fprintf(stdout
, "Error creating AST\n");
150 if (babeltrace_debug
) {
151 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
153 fprintf(stdout
, "Error visiting AST for XML output\n");
158 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
160 fprintf(stdout
, "Error in CTF semantic validation %d\n", ret
);
163 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
164 &td
->ctf_trace
, BYTE_ORDER
);
166 fprintf(stdout
, "Error in CTF metadata constructor %d\n", ret
);
170 ctf_scanner_free(scanner
);
174 close(td
->ctf_trace
.metadata
.pos
.fd
);
180 int create_stream_packet_index(struct trace_descriptor
*td
,
181 struct ctf_file_stream
*file_stream
)
183 struct ctf_stream
*stream
;
185 struct stream_pos
*pos
;
186 struct stat filestats
;
187 struct packet_index packet_index
;
188 int first_packet
= 1;
191 pos
= &file_stream
->pos
;
193 ret
= fstat(pos
->fd
, &filestats
);
197 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
198 uint64_t stream_id
= 0;
202 ret
= munmap(pos
->base
, pos
->packet_size
);
204 fprintf(stdout
, "Unable to unmap old base: %s.\n",
209 /* map new base. Need mapping length from header. */
210 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
, PROT_READ
,
211 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
212 pos
->content_size
= 0; /* Unknown at this point */
213 pos
->packet_size
= 0; /* Unknown at this point */
214 pos
->offset
= 0; /* Position of the packet header */
216 /* read and check header, set stream id (and check) */
217 if (td
->ctf_trace
.packet_header
) {
218 /* Read packet header */
219 td
->ctf_trace
.packet_header
->p
.declaration
->copy(NULL
, NULL
,
220 pos
, &ctf_format
, &td
->ctf_trace
.packet_header
->p
);
222 len_index
= struct_declaration_lookup_field_index(td
->ctf_trace
.packet_header
->declaration
, g_quark_from_static_string("magic"));
223 if (len_index
>= 0) {
224 struct definition_integer
*defint
;
227 field
= struct_definition_get_field_from_index(td
->ctf_trace
.packet_header
, len_index
);
228 assert(field
->definition
->declaration
->id
== CTF_TYPE_INTEGER
);
229 defint
= container_of(field
->definition
, struct definition_integer
, p
);
230 assert(defint
->declaration
->signedness
== FALSE
);
231 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
232 fprintf(stdout
, "[error] Invalid magic number %" PRIX64
".\n",
233 defint
->value
._unsigned
);
239 len_index
= struct_declaration_lookup_field_index(td
->ctf_trace
.packet_header
->declaration
, g_quark_from_static_string("trace_uuid"));
240 if (len_index
>= 0) {
241 struct definition_array
*defarray
;
244 uint8_t uuidval
[UUID_LEN
];
247 field
= struct_definition_get_field_from_index(td
->ctf_trace
.packet_header
, len_index
);
248 assert(field
->definition
->declaration
->id
== CTF_TYPE_ARRAY
);
249 defarray
= container_of(field
->definition
, struct definition_array
, p
);
250 assert(array_len(defarray
) == UUID_LEN
);
251 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
253 for (i
= 0; i
< UUID_LEN
; i
++) {
254 struct definition
*elem
;
255 struct definition_integer
*defint
;
257 elem
= array_index(defarray
, i
);
259 defint
= container_of(elem
, struct definition_integer
, p
);
260 uuidval
[i
] = defint
->value
._unsigned
;
262 ret
= uuid_compare(td
->ctf_trace
.uuid
, uuidval
);
264 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
270 len_index
= struct_declaration_lookup_field_index(td
->ctf_trace
.packet_header
->declaration
, g_quark_from_static_string("stream_id"));
271 if (len_index
>= 0) {
272 struct definition_integer
*defint
;
275 field
= struct_definition_get_field_from_index(td
->ctf_trace
.packet_header
, len_index
);
276 assert(field
->definition
->declaration
->id
== CTF_TYPE_INTEGER
);
277 defint
= container_of(field
->definition
, struct definition_integer
, p
);
278 assert(defint
->declaration
->signedness
== FALSE
);
279 stream_id
= defint
->value
._unsigned
;
283 if (!first_packet
&& file_stream
->stream_id
!= stream_id
) {
284 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
288 file_stream
->stream_id
= stream_id
;
289 if (stream_id
>= td
->ctf_trace
.streams
->len
) {
290 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
293 stream
= g_ptr_array_index(td
->ctf_trace
.streams
, stream_id
);
295 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
298 file_stream
->stream
= stream
;
302 /* Read packet context */
303 stream
->packet_context
->p
.declaration
->copy(NULL
, NULL
,
304 pos
, &ctf_format
, &stream
->packet_context
->p
);
306 /* read content size from header */
307 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("content_size"));
308 if (len_index
>= 0) {
309 struct definition_integer
*defint
;
312 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
313 assert(field
->definition
->declaration
->id
== CTF_TYPE_INTEGER
);
314 defint
= container_of(field
->definition
, struct definition_integer
, p
);
315 assert(defint
->declaration
->signedness
== FALSE
);
316 pos
->content_size
= defint
->value
._unsigned
;
318 /* Use file size for packet size */
319 pos
->content_size
= filestats
.st_size
* CHAR_BIT
;
322 /* read packet size from header */
323 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("packet_size"));
324 if (len_index
>= 0) {
325 struct definition_integer
*defint
;
328 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
329 assert(field
->definition
->declaration
->id
== CTF_TYPE_INTEGER
);
330 defint
= container_of(field
->definition
, struct definition_integer
, p
);
331 assert(defint
->declaration
->signedness
== FALSE
);
332 pos
->packet_size
= defint
->value
._unsigned
;
334 /* Use content size if non-zero, else file size */
335 pos
->packet_size
= pos
->content_size
? : filestats
.st_size
* CHAR_BIT
;
338 packet_index
.offset
= pos
->mmap_offset
;
339 packet_index
.content_size
= pos
->content_size
;
340 packet_index
.packet_size
= pos
->packet_size
;
341 /* add index to packet array */
342 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
344 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
351 * Note: many file streams can inherit from the same stream class
352 * description (metadata).
355 int ctf_open_file_stream_read(struct trace_descriptor
*td
, const char *path
, int flags
)
358 struct ctf_file_stream
*file_stream
;
360 ret
= openat(td
->ctf_trace
.dirfd
, path
, flags
);
363 file_stream
= g_new0(struct ctf_file_stream
, 1);
364 file_stream
->pos
.fd
= ret
;
365 file_stream
->pos
.packet_index
= g_array_new(FALSE
, TRUE
,
366 sizeof(struct packet_index
));
367 ret
= create_stream_packet_index(td
, file_stream
);
370 /* Add stream file to stream class */
371 g_ptr_array_add(file_stream
->stream
->files
, file_stream
);
375 (void) g_array_free(file_stream
->pos
.packet_index
, TRUE
);
376 close(file_stream
->pos
.fd
);
383 int ctf_open_trace_read(struct trace_descriptor
*td
, const char *path
, int flags
)
386 struct dirent
*dirent
;
387 struct dirent
*diriter
;
390 td
->ctf_trace
.flags
= flags
;
392 /* Open trace directory */
393 td
->ctf_trace
.dir
= opendir(path
);
394 if (!td
->ctf_trace
.dir
) {
395 fprintf(stdout
, "Unable to open trace directory.\n");
400 td
->ctf_trace
.dirfd
= open(path
, 0);
401 if (td
->ctf_trace
.dirfd
< 0) {
402 fprintf(stdout
, "Unable to open trace directory file descriptor.\n");
407 td
->ctf_trace
.streams
= g_ptr_array_new();
410 * Keep the metadata file separate.
413 ret
= ctf_open_trace_metadata_read(td
);
419 * Open each stream: for each file, try to open, check magic
420 * number, and get the stream ID to add to the right location in
424 dirent_len
= offsetof(struct dirent
, d_name
) +
425 fpathconf(td
->ctf_trace
.dirfd
, _PC_NAME_MAX
) + 1;
427 dirent
= malloc(dirent_len
);
430 ret
= readdir_r(td
->ctf_trace
.dir
, dirent
, &diriter
);
432 fprintf(stdout
, "Readdir error.\n");
437 if (!strcmp(diriter
->d_name
, ".")
438 || !strcmp(diriter
->d_name
, "..")
439 || !strcmp(diriter
->d_name
, "metadata"))
441 /* TODO: open file stream */
450 g_ptr_array_free(td
->ctf_trace
.streams
, TRUE
);
451 close(td
->ctf_trace
.dirfd
);
453 closedir(td
->ctf_trace
.dir
);
459 int ctf_open_trace_write(struct trace_descriptor
*td
, const char *path
, int flags
)
463 ret
= mkdir(path
, S_IRWXU
|S_IRWXG
);
467 /* Open trace directory */
468 td
->ctf_trace
.dir
= opendir(path
);
469 if (!td
->ctf_trace
.dir
) {
470 fprintf(stdout
, "Unable to open trace directory.\n");
482 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
484 struct trace_descriptor
*td
;
487 td
= g_new0(struct trace_descriptor
, 1);
491 ret
= ctf_open_trace_read(td
, path
, flags
);
496 ret
= ctf_open_trace_write(td
, path
, flags
);
501 fprintf(stdout
, "Incorrect open flags.\n");
512 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
514 (void) g_array_free(file_stream
->pos
.packet_index
, TRUE
);
515 close(file_stream
->pos
.fd
);
518 void ctf_close_trace(struct trace_descriptor
*td
)
522 if (td
->ctf_trace
.streams
) {
523 for (i
= 0; i
< td
->ctf_trace
.streams
->len
; i
++) {
524 struct ctf_stream
*stream
;
527 stream
= g_ptr_array_index(td
->ctf_trace
.streams
, i
);
528 for (j
= 0; j
< stream
->files
->len
; j
++) {
529 struct ctf_file_stream
*file_stream
;
530 file_stream
= g_ptr_array_index(td
->ctf_trace
.streams
, j
);
531 ctf_close_file_stream(file_stream
);
535 g_ptr_array_free(td
->ctf_trace
.streams
, TRUE
);
537 closedir(td
->ctf_trace
.dir
);
541 void __attribute__((constructor
)) ctf_init(void)
545 ctf_format
.name
= g_quark_from_static_string("ctf");
546 ret
= bt_register_format(&ctf_format
);