Commit | Line | Data |
---|---|---|
fc93b2bd MD |
1 | /* |
2 | * BabelTrace - Common Trace Format (CTF) | |
3 | * | |
4 | * Format registration. | |
5 | * | |
c054553d | 6 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
fc93b2bd | 7 | * |
ccd7e1c8 MD |
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: | |
fc93b2bd | 14 | * |
ccd7e1c8 MD |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
fc93b2bd MD |
17 | */ |
18 | ||
19 | #include <babeltrace/format.h> | |
20 | #include <babeltrace/ctf/types.h> | |
bbefb8dd | 21 | #include <babeltrace/ctf/metadata.h> |
65102a8c | 22 | #include <babeltrace/babeltrace.h> |
0f980a35 MD |
23 | #include <inttypes.h> |
24 | #include <uuid/uuid.h> | |
25 | #include <sys/mman.h> | |
bbefb8dd | 26 | #include <errno.h> |
bbefb8dd | 27 | #include <sys/types.h> |
65102a8c | 28 | #include <sys/stat.h> |
bbefb8dd | 29 | #include <fcntl.h> |
65102a8c | 30 | #include <dirent.h> |
bbefb8dd | 31 | #include <glib.h> |
65102a8c MD |
32 | #include <unistd.h> |
33 | #include <stdlib.h> | |
34 | ||
65102a8c MD |
35 | #include "metadata/ctf-scanner.h" |
36 | #include "metadata/ctf-parser.h" | |
37 | #include "metadata/ctf-ast.h" | |
38 | ||
0f980a35 MD |
39 | /* |
40 | * We currently simply map a page to read the packet header and packet | |
8c572eba | 41 | * context to get the packet length and content length. (in bits) |
0f980a35 | 42 | */ |
8c572eba MD |
43 | #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT) |
44 | #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT) | |
0f980a35 MD |
45 | #define UUID_LEN 16 /* uuid by value len */ |
46 | ||
65102a8c | 47 | extern int yydebug; |
bbefb8dd | 48 | |
bbefb8dd MD |
49 | struct trace_descriptor *ctf_open_trace(const char *path, int flags); |
50 | void ctf_close_trace(struct trace_descriptor *descriptor); | |
fc93b2bd | 51 | |
d11e9c49 MD |
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, | |
61 | }; | |
62 | ||
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, | |
72 | }; | |
73 | ||
74 | struct format ctf_format = { | |
bbefb8dd MD |
75 | .open_trace = ctf_open_trace, |
76 | .close_trace = ctf_close_trace, | |
fc93b2bd MD |
77 | }; |
78 | ||
8563e754 | 79 | void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags) |
8c572eba MD |
80 | { |
81 | pos->fd = fd; | |
82 | pos->mmap_offset = 0; | |
83 | pos->packet_size = 0; | |
84 | pos->content_size = 0; | |
85 | pos->content_size_loc = NULL; | |
86 | pos->base = NULL; | |
87 | pos->offset = 0; | |
88 | pos->dummy = false; | |
8c572eba | 89 | pos->cur_index = 0; |
8563e754 MD |
90 | if (fd >= 0) |
91 | pos->packet_index = g_array_new(FALSE, TRUE, | |
92 | sizeof(struct packet_index)); | |
93 | else | |
94 | pos->packet_index = NULL; | |
8563e754 MD |
95 | switch (open_flags & O_ACCMODE) { |
96 | case O_RDONLY: | |
97 | pos->prot = PROT_READ; | |
98 | pos->flags = MAP_PRIVATE; | |
99 | pos->parent.rw_table = read_dispatch_table; | |
100 | break; | |
101 | case O_WRONLY: | |
102 | case O_RDWR: | |
103 | pos->prot = PROT_WRITE; /* Write has priority */ | |
104 | pos->flags = MAP_SHARED; | |
105 | pos->parent.rw_table = write_dispatch_table; | |
106 | if (fd >= 0) | |
46322b33 | 107 | ctf_move_pos_slow(pos, 0); /* position for write */ |
8563e754 MD |
108 | break; |
109 | default: | |
110 | assert(0); | |
8c572eba MD |
111 | } |
112 | } | |
113 | ||
46322b33 | 114 | void ctf_fini_pos(struct ctf_stream_pos *pos) |
8c572eba MD |
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) { | |
46322b33 | 124 | fprintf(stdout, "[error] Unable to unmap old base: %s.\n", |
8c572eba MD |
125 | strerror(errno)); |
126 | assert(0); | |
127 | } | |
128 | } | |
129 | (void) g_array_free(pos->packet_index, TRUE); | |
130 | } | |
131 | ||
46322b33 | 132 | void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset) |
0f980a35 MD |
133 | { |
134 | int ret; | |
8c572eba MD |
135 | off_t off; |
136 | struct packet_index *index; | |
0f980a35 | 137 | |
8c572eba MD |
138 | |
139 | if (pos->prot == PROT_WRITE && pos->content_size_loc) | |
140 | *pos->content_size_loc = pos->offset; | |
0f980a35 MD |
141 | |
142 | if (pos->base) { | |
143 | /* unmap old base */ | |
8c572eba | 144 | ret = munmap(pos->base, pos->packet_size / CHAR_BIT); |
0f980a35 | 145 | if (ret) { |
46322b33 | 146 | fprintf(stdout, "[error] Unable to unmap old base: %s.\n", |
0f980a35 MD |
147 | strerror(errno)); |
148 | assert(0); | |
149 | } | |
150 | } | |
151 | ||
8c572eba | 152 | /* |
46322b33 | 153 | * The caller should never ask for ctf_move_pos across packets, |
8c572eba MD |
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 | } | |
0f980a35 | 188 | /* map new base. Need mapping length from header. */ |
8c572eba MD |
189 | pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot, |
190 | pos->flags, pos->fd, pos->mmap_offset); | |
191 | pos->offset = 0; | |
0f980a35 MD |
192 | } |
193 | ||
65102a8c MD |
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 | |
46322b33 | 199 | int ctf_open_trace_metadata_read(struct ctf_trace *td) |
65102a8c MD |
200 | { |
201 | struct ctf_scanner *scanner; | |
202 | FILE *fp; | |
203 | int ret = 0; | |
204 | ||
46322b33 MD |
205 | td->metadata.pos.fd = openat(td->dirfd, "metadata", O_RDONLY); |
206 | if (td->metadata.pos.fd < 0) { | |
65102a8c | 207 | fprintf(stdout, "Unable to open metadata.\n"); |
46322b33 | 208 | return td->metadata.pos.fd; |
65102a8c MD |
209 | } |
210 | ||
211 | if (babeltrace_debug) | |
212 | yydebug = 1; | |
213 | ||
46322b33 | 214 | fp = fdopen(td->metadata.pos.fd, "r"); |
65102a8c | 215 | if (!fp) { |
46322b33 | 216 | fprintf(stdout, "[error] Unable to open metadata stream.\n"); |
65102a8c MD |
217 | ret = -errno; |
218 | goto end_stream; | |
219 | } | |
220 | ||
221 | scanner = ctf_scanner_alloc(fp); | |
222 | if (!scanner) { | |
46322b33 | 223 | fprintf(stdout, "[error] Error allocating scanner\n"); |
65102a8c MD |
224 | ret = -ENOMEM; |
225 | goto end_scanner_alloc; | |
226 | } | |
227 | ret = ctf_scanner_append_ast(scanner); | |
228 | if (ret) { | |
46322b33 | 229 | fprintf(stdout, "[error] Error creating AST\n"); |
65102a8c MD |
230 | goto end; |
231 | } | |
232 | ||
233 | if (babeltrace_debug) { | |
234 | ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root); | |
235 | if (ret) { | |
46322b33 | 236 | fprintf(stdout, "[error] Error visiting AST for XML output\n"); |
65102a8c MD |
237 | goto end; |
238 | } | |
239 | } | |
240 | ||
241 | ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root); | |
242 | if (ret) { | |
46322b33 | 243 | fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret); |
65102a8c MD |
244 | goto end; |
245 | } | |
246 | ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root, | |
46322b33 | 247 | td, BYTE_ORDER); |
65102a8c | 248 | if (ret) { |
46322b33 | 249 | fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret); |
65102a8c MD |
250 | goto end; |
251 | } | |
252 | end: | |
253 | ctf_scanner_free(scanner); | |
254 | end_scanner_alloc: | |
255 | fclose(fp); | |
256 | end_stream: | |
46322b33 | 257 | close(td->metadata.pos.fd); |
0f980a35 MD |
258 | return ret; |
259 | } | |
260 | ||
261 | ||
262 | static | |
46322b33 | 263 | int create_stream_packet_index(struct ctf_trace *td, |
0f980a35 MD |
264 | struct ctf_file_stream *file_stream) |
265 | { | |
266 | struct ctf_stream *stream; | |
267 | int len_index; | |
46322b33 | 268 | struct ctf_stream_pos *pos; |
0f980a35 MD |
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 */ | |
8c572eba | 285 | ret = munmap(pos->base, pos->packet_size / CHAR_BIT); |
0f980a35 | 286 | if (ret) { |
46322b33 | 287 | fprintf(stdout, "[error] Unable to unmap old base: %s.\n", |
0f980a35 MD |
288 | strerror(errno)); |
289 | return ret; | |
290 | } | |
8c572eba | 291 | pos->base = NULL; |
0f980a35 MD |
292 | } |
293 | /* map new base. Need mapping length from header. */ | |
8c572eba | 294 | pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ, |
0f980a35 | 295 | MAP_PRIVATE, pos->fd, pos->mmap_offset); |
dc48ecad MD |
296 | pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */ |
297 | pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */ | |
0f980a35 MD |
298 | pos->offset = 0; /* Position of the packet header */ |
299 | ||
8c572eba MD |
300 | packet_index.offset = pos->mmap_offset; |
301 | packet_index.content_size = 0; | |
302 | packet_index.packet_size = 0; | |
303 | ||
0f980a35 | 304 | /* read and check header, set stream id (and check) */ |
46322b33 | 305 | if (td->packet_header) { |
0f980a35 | 306 | /* Read packet header */ |
d11e9c49 | 307 | generic_rw(&pos->parent, &td->packet_header->p); |
0f980a35 | 308 | |
46322b33 | 309 | len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic")); |
0f980a35 MD |
310 | if (len_index >= 0) { |
311 | struct definition_integer *defint; | |
312 | struct field *field; | |
313 | ||
46322b33 | 314 | field = struct_definition_get_field_from_index(td->packet_header, len_index); |
0f980a35 MD |
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) { | |
8c572eba MD |
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); | |
0f980a35 MD |
323 | return -EINVAL; |
324 | } | |
325 | } | |
326 | ||
327 | /* check uuid */ | |
46322b33 | 328 | len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("trace_uuid")); |
0f980a35 MD |
329 | if (len_index >= 0) { |
330 | struct definition_array *defarray; | |
331 | struct field *field; | |
332 | uint64_t i; | |
333 | uint8_t uuidval[UUID_LEN]; | |
334 | ||
46322b33 | 335 | field = struct_definition_get_field_from_index(td->packet_header, len_index); |
0f980a35 MD |
336 | assert(field->definition->declaration->id == CTF_TYPE_ARRAY); |
337 | defarray = container_of(field->definition, struct definition_array, p); | |
3838df27 | 338 | assert(array_len(defarray) == UUID_LEN); |
0f980a35 MD |
339 | assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER); |
340 | ||
341 | for (i = 0; i < UUID_LEN; i++) { | |
342 | struct definition *elem; | |
343 | struct definition_integer *defint; | |
344 | ||
345 | elem = array_index(defarray, i); | |
346 | assert(elem); | |
347 | defint = container_of(elem, struct definition_integer, p); | |
348 | uuidval[i] = defint->value._unsigned; | |
349 | } | |
46322b33 | 350 | ret = uuid_compare(td->uuid, uuidval); |
0f980a35 MD |
351 | if (ret) { |
352 | fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n"); | |
353 | return -EINVAL; | |
354 | } | |
355 | } | |
356 | ||
357 | ||
46322b33 | 358 | len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("stream_id")); |
0f980a35 MD |
359 | if (len_index >= 0) { |
360 | struct definition_integer *defint; | |
361 | struct field *field; | |
362 | ||
46322b33 | 363 | field = struct_definition_get_field_from_index(td->packet_header, len_index); |
0f980a35 MD |
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; | |
368 | } | |
369 | } | |
370 | ||
371 | if (!first_packet && file_stream->stream_id != stream_id) { | |
372 | fprintf(stdout, "[error] Stream ID is changing within a stream.\n"); | |
373 | return -EINVAL; | |
374 | } | |
375 | if (first_packet) { | |
376 | file_stream->stream_id = stream_id; | |
46322b33 | 377 | if (stream_id >= td->streams->len) { |
0f980a35 MD |
378 | fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id); |
379 | return -EINVAL; | |
380 | } | |
46322b33 | 381 | stream = g_ptr_array_index(td->streams, stream_id); |
0f980a35 MD |
382 | if (!stream) { |
383 | fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id); | |
384 | return -EINVAL; | |
385 | } | |
386 | file_stream->stream = stream; | |
387 | } | |
388 | first_packet = 0; | |
389 | ||
dc48ecad MD |
390 | if (stream->packet_context) { |
391 | /* Read packet context */ | |
d11e9c49 | 392 | generic_rw(&pos->parent, &stream->packet_context->p); |
dc48ecad MD |
393 | |
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; | |
398 | struct field *field; | |
399 | ||
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); | |
8c572eba | 404 | packet_index.content_size = defint->value._unsigned; |
dc48ecad MD |
405 | } else { |
406 | /* Use file size for packet size */ | |
8c572eba | 407 | packet_index.content_size = filestats.st_size * CHAR_BIT; |
dc48ecad MD |
408 | } |
409 | ||
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; | |
414 | struct field *field; | |
415 | ||
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); | |
8c572eba | 420 | packet_index.packet_size = defint->value._unsigned; |
dc48ecad MD |
421 | } else { |
422 | /* Use content size if non-zero, else file size */ | |
8c572eba | 423 | packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT; |
dc48ecad | 424 | } |
0f980a35 MD |
425 | } else { |
426 | /* Use file size for packet size */ | |
8c572eba | 427 | packet_index.content_size = filestats.st_size * CHAR_BIT; |
0f980a35 | 428 | /* Use content size if non-zero, else file size */ |
8c572eba | 429 | packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT; |
0f980a35 MD |
430 | } |
431 | ||
0f980a35 MD |
432 | /* add index to packet array */ |
433 | g_array_append_val(file_stream->pos.packet_index, packet_index); | |
434 | ||
8c572eba | 435 | pos->mmap_offset += packet_index.packet_size / CHAR_BIT; |
0f980a35 MD |
436 | } |
437 | ||
438 | return 0; | |
439 | } | |
440 | ||
441 | /* | |
442 | * Note: many file streams can inherit from the same stream class | |
443 | * description (metadata). | |
444 | */ | |
445 | static | |
46322b33 | 446 | int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags) |
0f980a35 MD |
447 | { |
448 | int ret; | |
449 | struct ctf_file_stream *file_stream; | |
450 | ||
46322b33 | 451 | ret = openat(td->dirfd, path, flags); |
0f980a35 MD |
452 | if (ret < 0) |
453 | goto error; | |
454 | file_stream = g_new0(struct ctf_file_stream, 1); | |
8563e754 | 455 | ctf_init_pos(&file_stream->pos, ret, flags); |
0f980a35 MD |
456 | ret = create_stream_packet_index(td, file_stream); |
457 | if (ret) | |
458 | goto error_index; | |
459 | /* Add stream file to stream class */ | |
460 | g_ptr_array_add(file_stream->stream->files, file_stream); | |
461 | return 0; | |
462 | ||
463 | error_index: | |
46322b33 | 464 | ctf_fini_pos(&file_stream->pos); |
0f980a35 MD |
465 | close(file_stream->pos.fd); |
466 | g_free(file_stream); | |
467 | error: | |
65102a8c MD |
468 | return ret; |
469 | } | |
470 | ||
bbefb8dd | 471 | static |
46322b33 | 472 | int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags) |
bbefb8dd MD |
473 | { |
474 | int ret; | |
65102a8c MD |
475 | struct dirent *dirent; |
476 | struct dirent *diriter; | |
477 | size_t dirent_len; | |
bbefb8dd | 478 | |
46322b33 | 479 | td->flags = flags; |
bbefb8dd MD |
480 | |
481 | /* Open trace directory */ | |
46322b33 MD |
482 | td->dir = opendir(path); |
483 | if (!td->dir) { | |
dc48ecad | 484 | fprintf(stdout, "[error] Unable to open trace directory.\n"); |
bbefb8dd MD |
485 | ret = -ENOENT; |
486 | goto error; | |
487 | } | |
488 | ||
46322b33 MD |
489 | td->dirfd = open(path, 0); |
490 | if (td->dirfd < 0) { | |
dc48ecad | 491 | fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n"); |
65102a8c MD |
492 | ret = -ENOENT; |
493 | goto error_dirfd; | |
494 | } | |
0f980a35 | 495 | |
46322b33 | 496 | td->streams = g_ptr_array_new(); |
0f980a35 | 497 | |
65102a8c MD |
498 | /* |
499 | * Keep the metadata file separate. | |
500 | */ | |
bbefb8dd | 501 | |
65102a8c MD |
502 | ret = ctf_open_trace_metadata_read(td); |
503 | if (ret) { | |
504 | goto error_metadata; | |
505 | } | |
bbefb8dd MD |
506 | |
507 | /* | |
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 | |
510 | * the stream array. | |
bbefb8dd MD |
511 | */ |
512 | ||
65102a8c | 513 | dirent_len = offsetof(struct dirent, d_name) + |
46322b33 | 514 | fpathconf(td->dirfd, _PC_NAME_MAX) + 1; |
bbefb8dd | 515 | |
65102a8c | 516 | dirent = malloc(dirent_len); |
bbefb8dd | 517 | |
65102a8c | 518 | for (;;) { |
46322b33 | 519 | ret = readdir_r(td->dir, dirent, &diriter); |
65102a8c | 520 | if (ret) { |
dc48ecad | 521 | fprintf(stdout, "[error] Readdir error.\n"); |
65102a8c | 522 | goto readdir_error; |
65102a8c MD |
523 | } |
524 | if (!diriter) | |
525 | break; | |
526 | if (!strcmp(diriter->d_name, ".") | |
527 | || !strcmp(diriter->d_name, "..") | |
528 | || !strcmp(diriter->d_name, "metadata")) | |
529 | continue; | |
dc48ecad MD |
530 | ret = ctf_open_file_stream_read(td, diriter->d_name, flags); |
531 | if (ret) { | |
532 | fprintf(stdout, "[error] Open file stream error.\n"); | |
533 | goto readdir_error; | |
534 | } | |
65102a8c | 535 | } |
bbefb8dd | 536 | |
65102a8c | 537 | free(dirent); |
bbefb8dd | 538 | return 0; |
65102a8c MD |
539 | |
540 | readdir_error: | |
541 | free(dirent); | |
542 | error_metadata: | |
46322b33 MD |
543 | g_ptr_array_free(td->streams, TRUE); |
544 | close(td->dirfd); | |
65102a8c | 545 | error_dirfd: |
46322b33 | 546 | closedir(td->dir); |
bbefb8dd MD |
547 | error: |
548 | return ret; | |
549 | } | |
550 | ||
551 | static | |
46322b33 | 552 | int ctf_open_trace_write(struct ctf_trace *td, const char *path, int flags) |
bbefb8dd MD |
553 | { |
554 | int ret; | |
555 | ||
556 | ret = mkdir(path, S_IRWXU|S_IRWXG); | |
557 | if (ret) | |
558 | return ret; | |
559 | ||
65102a8c | 560 | /* Open trace directory */ |
46322b33 MD |
561 | td->dir = opendir(path); |
562 | if (!td->dir) { | |
563 | fprintf(stdout, "[error] Unable to open trace directory.\n"); | |
65102a8c MD |
564 | ret = -ENOENT; |
565 | goto error; | |
566 | } | |
567 | ||
568 | ||
bbefb8dd | 569 | return 0; |
65102a8c MD |
570 | |
571 | error: | |
572 | return ret; | |
bbefb8dd MD |
573 | } |
574 | ||
575 | struct trace_descriptor *ctf_open_trace(const char *path, int flags) | |
576 | { | |
46322b33 | 577 | struct ctf_trace *td; |
bbefb8dd MD |
578 | int ret; |
579 | ||
46322b33 | 580 | td = g_new0(struct ctf_trace, 1); |
bbefb8dd | 581 | |
8c572eba | 582 | switch (flags & O_ACCMODE) { |
bbefb8dd MD |
583 | case O_RDONLY: |
584 | ret = ctf_open_trace_read(td, path, flags); | |
585 | if (ret) | |
586 | goto error; | |
587 | break; | |
588 | case O_WRONLY: | |
46322b33 MD |
589 | fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n"); |
590 | goto error; | |
591 | #if 0 | |
bbefb8dd MD |
592 | ret = ctf_open_trace_write(td, path, flags); |
593 | if (ret) | |
594 | goto error; | |
46322b33 | 595 | #endif //0 |
bbefb8dd MD |
596 | break; |
597 | default: | |
46322b33 | 598 | fprintf(stdout, "[error] Incorrect open flags.\n"); |
bbefb8dd MD |
599 | goto error; |
600 | } | |
601 | ||
46322b33 | 602 | return &td->parent; |
bbefb8dd MD |
603 | error: |
604 | g_free(td); | |
605 | return NULL; | |
606 | } | |
607 | ||
0f980a35 MD |
608 | static |
609 | void ctf_close_file_stream(struct ctf_file_stream *file_stream) | |
610 | { | |
46322b33 | 611 | ctf_fini_pos(&file_stream->pos); |
0f980a35 MD |
612 | close(file_stream->pos.fd); |
613 | } | |
614 | ||
46322b33 | 615 | void ctf_close_trace(struct trace_descriptor *tdp) |
bbefb8dd | 616 | { |
46322b33 | 617 | struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent); |
0f980a35 MD |
618 | int i; |
619 | ||
46322b33 MD |
620 | if (td->streams) { |
621 | for (i = 0; i < td->streams->len; i++) { | |
0f980a35 MD |
622 | struct ctf_stream *stream; |
623 | int j; | |
46322b33 | 624 | stream = g_ptr_array_index(td->streams, i); |
0f980a35 MD |
625 | for (j = 0; j < stream->files->len; j++) { |
626 | struct ctf_file_stream *file_stream; | |
2c117823 | 627 | file_stream = g_ptr_array_index(stream->files, j); |
0f980a35 MD |
628 | ctf_close_file_stream(file_stream); |
629 | } | |
630 | ||
631 | } | |
46322b33 | 632 | g_ptr_array_free(td->streams, TRUE); |
0f980a35 | 633 | } |
46322b33 | 634 | closedir(td->dir); |
bbefb8dd MD |
635 | g_free(td); |
636 | } | |
637 | ||
7fb21036 | 638 | void __attribute__((constructor)) ctf_init(void) |
fc93b2bd MD |
639 | { |
640 | int ret; | |
641 | ||
4c8bfb7e | 642 | ctf_format.name = g_quark_from_static_string("ctf"); |
fc93b2bd MD |
643 | ret = bt_register_format(&ctf_format); |
644 | assert(!ret); | |
645 | } | |
698f0fe4 MD |
646 | |
647 | /* TODO: finalize */ |