9b2f0fbf65dc195b426433de7596f6c18850aba2
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 <sys/types.h>
33 #include "metadata/ctf-scanner.h"
34 #include "metadata/ctf-parser.h"
35 #include "metadata/ctf-ast.h"
39 struct trace_descriptor
{
40 struct ctf_trace ctf_trace
;
43 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
);
44 void ctf_close_trace(struct trace_descriptor
*descriptor
);
46 static struct format ctf_format
= {
47 .uint_read
= ctf_uint_read
,
48 .int_read
= ctf_int_read
,
49 .uint_write
= ctf_uint_write
,
50 .int_write
= ctf_int_write
,
51 .double_read
= ctf_double_read
,
52 .double_write
= ctf_double_write
,
53 .float_copy
= ctf_float_copy
,
54 .string_copy
= ctf_string_copy
,
55 .string_read
= ctf_string_read
,
56 .string_write
= ctf_string_write
,
57 .string_free_temp
= ctf_string_free_temp
,
58 .enum_read
= ctf_enum_read
,
59 .enum_write
= ctf_enum_write
,
60 .struct_begin
= ctf_struct_begin
,
61 .struct_end
= ctf_struct_end
,
62 .variant_begin
= ctf_variant_begin
,
63 .variant_end
= ctf_variant_end
,
64 .array_begin
= ctf_array_begin
,
65 .array_end
= ctf_array_end
,
66 .sequence_begin
= ctf_sequence_begin
,
67 .sequence_end
= ctf_sequence_end
,
68 .open_trace
= ctf_open_trace
,
69 .close_trace
= ctf_close_trace
,
73 * TODO: for now, we treat the metadata file as a simple text file
74 * (without any header nor packets nor padding).
77 int ctf_open_trace_metadata_read(struct trace_descriptor
*td
)
79 struct ctf_scanner
*scanner
;
83 td
->ctf_trace
.metadata
.fd
= openat(td
->ctf_trace
.dirfd
,
84 "metadata", O_RDONLY
);
85 if (td
->ctf_trace
.metadata
.fd
< 0) {
86 fprintf(stdout
, "Unable to open metadata.\n");
87 return td
->ctf_trace
.metadata
.fd
;
93 fp
= fdopen(td
->ctf_trace
.metadata
.fd
, "r");
95 fprintf(stdout
, "Unable to open metadata stream.\n");
100 scanner
= ctf_scanner_alloc(fp
);
102 fprintf(stdout
, "Error allocating scanner\n");
104 goto end_scanner_alloc
;
106 ret
= ctf_scanner_append_ast(scanner
);
108 fprintf(stdout
, "Error creating AST\n");
112 if (babeltrace_debug
) {
113 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
115 fprintf(stdout
, "Error visiting AST for XML output\n");
120 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
122 fprintf(stdout
, "Error in CTF semantic validation %d\n", ret
);
125 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
126 &td
->ctf_trace
, BYTE_ORDER
);
128 fprintf(stdout
, "Error in CTF metadata constructor %d\n", ret
);
132 ctf_scanner_free(scanner
);
136 close(td
->ctf_trace
.metadata
.fd
);
141 int ctf_open_trace_read(struct trace_descriptor
*td
, const char *path
, int flags
)
144 struct dirent
*dirent
;
145 struct dirent
*diriter
;
148 td
->ctf_trace
.flags
= flags
;
150 /* Open trace directory */
151 td
->ctf_trace
.dir
= opendir(path
);
152 if (!td
->ctf_trace
.dir
) {
153 fprintf(stdout
, "Unable to open trace directory.\n");
158 td
->ctf_trace
.dirfd
= open(path
, 0);
159 if (td
->ctf_trace
.dirfd
< 0) {
160 fprintf(stdout
, "Unable to open trace directory file descriptor.\n");
165 * Keep the metadata file separate.
168 ret
= ctf_open_trace_metadata_read(td
);
174 * Open each stream: for each file, try to open, check magic
175 * number, and get the stream ID to add to the right location in
179 dirent_len
= offsetof(struct dirent
, d_name
) +
180 fpathconf(td
->ctf_trace
.dirfd
, _PC_NAME_MAX
) + 1;
182 dirent
= malloc(dirent_len
);
185 ret
= readdir_r(td
->ctf_trace
.dir
, dirent
, &diriter
);
187 fprintf(stdout
, "Readdir error.\n");
193 if (!strcmp(diriter
->d_name
, ".")
194 || !strcmp(diriter
->d_name
, "..")
195 || !strcmp(diriter
->d_name
, "metadata"))
205 close(td
->ctf_trace
.dirfd
);
207 closedir(td
->ctf_trace
.dir
);
213 int ctf_open_trace_write(struct trace_descriptor
*td
, const char *path
, int flags
)
217 ret
= mkdir(path
, S_IRWXU
|S_IRWXG
);
221 /* Open trace directory */
222 td
->ctf_trace
.dir
= opendir(path
);
223 if (!td
->ctf_trace
.dir
) {
224 fprintf(stdout
, "Unable to open trace directory.\n");
236 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
238 struct trace_descriptor
*td
;
241 td
= g_new0(struct trace_descriptor
, 1);
245 ret
= ctf_open_trace_read(td
, path
, flags
);
250 ret
= ctf_open_trace_write(td
, path
, flags
);
255 fprintf(stdout
, "Incorrect open flags.\n");
265 void ctf_close_trace(struct trace_descriptor
*td
)
267 closedir(td
->ctf_trace
.dir
);
271 void __attribute__((constructor
)) ctf_init(void)
275 ctf_format
.name
= g_quark_from_static_string("ctf");
276 ret
= bt_register_format(&ctf_format
);
This page took 0.061161 seconds and 4 git commands to generate.