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 | 23 | #include <inttypes.h> |
b4c19c1e | 24 | #include <stdio.h> |
0f980a35 MD |
25 | #include <uuid/uuid.h> |
26 | #include <sys/mman.h> | |
bbefb8dd | 27 | #include <errno.h> |
b4c19c1e | 28 | #include <endian.h> |
bbefb8dd | 29 | #include <sys/types.h> |
65102a8c | 30 | #include <sys/stat.h> |
bbefb8dd | 31 | #include <fcntl.h> |
65102a8c | 32 | #include <dirent.h> |
bbefb8dd | 33 | #include <glib.h> |
65102a8c MD |
34 | #include <unistd.h> |
35 | #include <stdlib.h> | |
36 | ||
65102a8c MD |
37 | #include "metadata/ctf-scanner.h" |
38 | #include "metadata/ctf-parser.h" | |
39 | #include "metadata/ctf-ast.h" | |
40 | ||
0f980a35 MD |
41 | /* |
42 | * We currently simply map a page to read the packet header and packet | |
8c572eba | 43 | * context to get the packet length and content length. (in bits) |
0f980a35 | 44 | */ |
8c572eba MD |
45 | #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT) |
46 | #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT) | |
0f980a35 MD |
47 | #define UUID_LEN 16 /* uuid by value len */ |
48 | ||
a0fe7d97 MD |
49 | #ifndef min |
50 | #define min(a, b) (((a) < (b)) ? (a) : (b)) | |
51 | #endif | |
52 | ||
65102a8c | 53 | extern int yydebug; |
bbefb8dd | 54 | |
bbefb8dd MD |
55 | struct trace_descriptor *ctf_open_trace(const char *path, int flags); |
56 | void ctf_close_trace(struct trace_descriptor *descriptor); | |
fc93b2bd | 57 | |
1ae19169 MD |
58 | static |
59 | rw_dispatch read_dispatch_table[] = { | |
d11e9c49 MD |
60 | [ CTF_TYPE_INTEGER ] = ctf_integer_read, |
61 | [ CTF_TYPE_FLOAT ] = ctf_float_read, | |
62 | [ CTF_TYPE_ENUM ] = ctf_enum_read, | |
63 | [ CTF_TYPE_STRING ] = ctf_string_read, | |
64 | [ CTF_TYPE_STRUCT ] = ctf_struct_rw, | |
65 | [ CTF_TYPE_VARIANT ] = ctf_variant_rw, | |
81dee1bb MD |
66 | [ CTF_TYPE_ARRAY ] = ctf_array_read, |
67 | [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read, | |
d11e9c49 MD |
68 | }; |
69 | ||
1ae19169 MD |
70 | static |
71 | rw_dispatch write_dispatch_table[] = { | |
d11e9c49 MD |
72 | [ CTF_TYPE_INTEGER ] = ctf_integer_write, |
73 | [ CTF_TYPE_FLOAT ] = ctf_float_write, | |
74 | [ CTF_TYPE_ENUM ] = ctf_enum_write, | |
75 | [ CTF_TYPE_STRING ] = ctf_string_write, | |
76 | [ CTF_TYPE_STRUCT ] = ctf_struct_rw, | |
77 | [ CTF_TYPE_VARIANT ] = ctf_variant_rw, | |
81dee1bb MD |
78 | [ CTF_TYPE_ARRAY ] = ctf_array_write, |
79 | [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write, | |
d11e9c49 MD |
80 | }; |
81 | ||
1ae19169 | 82 | static |
d11e9c49 | 83 | struct format ctf_format = { |
bbefb8dd MD |
84 | .open_trace = ctf_open_trace, |
85 | .close_trace = ctf_close_trace, | |
fc93b2bd MD |
86 | }; |
87 | ||
31262354 | 88 | static |
764af3f4 | 89 | int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream) |
31262354 MD |
90 | { |
91 | struct ctf_stream_pos *pos = | |
92 | container_of(ppos, struct ctf_stream_pos, parent); | |
764af3f4 | 93 | struct ctf_stream_class *stream_class = stream->stream_class; |
31262354 MD |
94 | struct ctf_event *event_class; |
95 | uint64_t id = 0; | |
31262354 MD |
96 | int ret; |
97 | ||
98 | if (pos->offset == EOF) | |
99 | return EOF; | |
100 | ||
101 | /* Read event header */ | |
102 | if (stream_class->event_header) { | |
a35173fe | 103 | struct definition_integer *integer_definition; |
ccdb988e | 104 | struct definition *variant; |
a35173fe | 105 | |
31262354 MD |
106 | ret = generic_rw(ppos, &stream_class->event_header->p); |
107 | if (ret) | |
108 | goto error; | |
109 | /* lookup event id */ | |
a35173fe MD |
110 | integer_definition = lookup_integer(&stream_class->event_header->p, "id", FALSE); |
111 | if (integer_definition) { | |
112 | id = integer_definition->value._unsigned; | |
113 | } else { | |
114 | struct definition_enum *enum_definition; | |
115 | ||
116 | enum_definition = lookup_enum(&stream_class->event_header->p, "id", FALSE); | |
117 | if (enum_definition) { | |
118 | id = enum_definition->integer->value._unsigned; | |
119 | } | |
31262354 | 120 | } |
764af3f4 | 121 | |
ccdb988e MD |
122 | variant = lookup_variant(&stream_class->event_header->p, "v"); |
123 | if (variant) { | |
124 | integer_definition = lookup_integer(variant, "id", FALSE); | |
125 | if (integer_definition) { | |
126 | id = integer_definition->value._unsigned; | |
127 | } | |
128 | } | |
129 | ||
764af3f4 | 130 | /* lookup timestamp */ |
a35173fe MD |
131 | integer_definition = lookup_integer(&stream_class->event_header->p, "timestamp", FALSE); |
132 | if (integer_definition) { | |
133 | stream->timestamp = integer_definition->value._unsigned; | |
134 | } else { | |
ccdb988e MD |
135 | if (variant) { |
136 | integer_definition = lookup_integer(variant, "timestamp", FALSE); | |
a35173fe MD |
137 | if (integer_definition) { |
138 | stream->timestamp = integer_definition->value._unsigned; | |
139 | } | |
140 | } | |
764af3f4 | 141 | } |
31262354 MD |
142 | } |
143 | ||
144 | /* Read stream-declared event context */ | |
145 | if (stream_class->event_context) { | |
146 | ret = generic_rw(ppos, &stream_class->event_context->p); | |
147 | if (ret) | |
148 | goto error; | |
149 | } | |
150 | ||
151 | if (id >= stream_class->events_by_id->len) { | |
152 | fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id); | |
153 | return -EINVAL; | |
154 | } | |
155 | event_class = g_ptr_array_index(stream_class->events_by_id, id); | |
156 | if (!event_class) { | |
157 | fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id); | |
158 | return -EINVAL; | |
159 | } | |
160 | ||
161 | /* Read event-declared event context */ | |
162 | if (event_class->context) { | |
163 | ret = generic_rw(ppos, &event_class->context->p); | |
164 | if (ret) | |
165 | goto error; | |
166 | } | |
167 | ||
168 | /* Read event payload */ | |
169 | if (event_class->fields) { | |
170 | ret = generic_rw(ppos, &event_class->fields->p); | |
171 | if (ret) | |
172 | goto error; | |
173 | } | |
174 | ||
175 | return 0; | |
176 | ||
177 | error: | |
178 | fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n"); | |
179 | return ret; | |
180 | } | |
181 | ||
182 | static | |
764af3f4 | 183 | int ctf_write_event(struct stream_pos *pos, struct ctf_stream *stream) |
31262354 | 184 | { |
764af3f4 | 185 | struct ctf_stream_class *stream_class = stream->stream_class; |
31262354 MD |
186 | struct ctf_event *event_class; |
187 | uint64_t id = 0; | |
31262354 MD |
188 | int ret; |
189 | ||
190 | /* print event header */ | |
191 | if (stream_class->event_header) { | |
ccdb988e MD |
192 | struct definition_integer *integer_definition; |
193 | struct definition *variant; | |
194 | ||
31262354 | 195 | /* lookup event id */ |
ccdb988e MD |
196 | integer_definition = lookup_integer(&stream_class->event_header->p, "id", FALSE); |
197 | if (integer_definition) { | |
198 | id = integer_definition->value._unsigned; | |
199 | } else { | |
200 | struct definition_enum *enum_definition; | |
201 | ||
202 | enum_definition = lookup_enum(&stream_class->event_header->p, "id", FALSE); | |
203 | if (enum_definition) { | |
204 | id = enum_definition->integer->value._unsigned; | |
205 | } | |
206 | } | |
207 | ||
208 | variant = lookup_variant(&stream_class->event_header->p, "v"); | |
209 | if (variant) { | |
210 | integer_definition = lookup_integer(variant, "id", FALSE); | |
211 | if (integer_definition) { | |
212 | id = integer_definition->value._unsigned; | |
213 | } | |
31262354 MD |
214 | } |
215 | ||
216 | ret = generic_rw(pos, &stream_class->event_header->p); | |
217 | if (ret) | |
218 | goto error; | |
219 | } | |
220 | ||
221 | /* print stream-declared event context */ | |
222 | if (stream_class->event_context) { | |
223 | ret = generic_rw(pos, &stream_class->event_context->p); | |
224 | if (ret) | |
225 | goto error; | |
226 | } | |
227 | ||
228 | if (id >= stream_class->events_by_id->len) { | |
229 | fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id); | |
230 | return -EINVAL; | |
231 | } | |
232 | event_class = g_ptr_array_index(stream_class->events_by_id, id); | |
233 | if (!event_class) { | |
234 | fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id); | |
235 | return -EINVAL; | |
236 | } | |
237 | ||
238 | /* print event-declared event context */ | |
239 | if (event_class->context) { | |
240 | ret = generic_rw(pos, &event_class->context->p); | |
241 | if (ret) | |
242 | goto error; | |
243 | } | |
244 | ||
245 | /* Read and print event payload */ | |
246 | if (event_class->fields) { | |
247 | ret = generic_rw(pos, &event_class->fields->p); | |
248 | if (ret) | |
249 | goto error; | |
250 | } | |
251 | ||
252 | return 0; | |
253 | ||
254 | error: | |
255 | fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n"); | |
256 | return ret; | |
257 | } | |
258 | ||
8563e754 | 259 | void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags) |
8c572eba MD |
260 | { |
261 | pos->fd = fd; | |
262 | pos->mmap_offset = 0; | |
263 | pos->packet_size = 0; | |
264 | pos->content_size = 0; | |
265 | pos->content_size_loc = NULL; | |
266 | pos->base = NULL; | |
267 | pos->offset = 0; | |
268 | pos->dummy = false; | |
8c572eba | 269 | pos->cur_index = 0; |
8563e754 MD |
270 | if (fd >= 0) |
271 | pos->packet_index = g_array_new(FALSE, TRUE, | |
272 | sizeof(struct packet_index)); | |
273 | else | |
274 | pos->packet_index = NULL; | |
8563e754 MD |
275 | switch (open_flags & O_ACCMODE) { |
276 | case O_RDONLY: | |
277 | pos->prot = PROT_READ; | |
278 | pos->flags = MAP_PRIVATE; | |
279 | pos->parent.rw_table = read_dispatch_table; | |
31262354 | 280 | pos->parent.event_cb = ctf_read_event; |
8563e754 | 281 | break; |
8563e754 MD |
282 | case O_RDWR: |
283 | pos->prot = PROT_WRITE; /* Write has priority */ | |
284 | pos->flags = MAP_SHARED; | |
285 | pos->parent.rw_table = write_dispatch_table; | |
31262354 | 286 | pos->parent.event_cb = ctf_write_event; |
8563e754 | 287 | if (fd >= 0) |
847bf71a | 288 | ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */ |
8563e754 MD |
289 | break; |
290 | default: | |
291 | assert(0); | |
8c572eba MD |
292 | } |
293 | } | |
294 | ||
46322b33 | 295 | void ctf_fini_pos(struct ctf_stream_pos *pos) |
8c572eba MD |
296 | { |
297 | int ret; | |
298 | ||
299 | if (pos->prot == PROT_WRITE && pos->content_size_loc) | |
300 | *pos->content_size_loc = pos->offset; | |
301 | if (pos->base) { | |
302 | /* unmap old base */ | |
303 | ret = munmap(pos->base, pos->packet_size / CHAR_BIT); | |
304 | if (ret) { | |
46322b33 | 305 | fprintf(stdout, "[error] Unable to unmap old base: %s.\n", |
8c572eba MD |
306 | strerror(errno)); |
307 | assert(0); | |
308 | } | |
309 | } | |
310 | (void) g_array_free(pos->packet_index, TRUE); | |
311 | } | |
312 | ||
847bf71a | 313 | void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence) |
0f980a35 MD |
314 | { |
315 | int ret; | |
8c572eba MD |
316 | off_t off; |
317 | struct packet_index *index; | |
0f980a35 | 318 | |
8c572eba MD |
319 | if (pos->prot == PROT_WRITE && pos->content_size_loc) |
320 | *pos->content_size_loc = pos->offset; | |
0f980a35 MD |
321 | |
322 | if (pos->base) { | |
323 | /* unmap old base */ | |
8c572eba | 324 | ret = munmap(pos->base, pos->packet_size / CHAR_BIT); |
0f980a35 | 325 | if (ret) { |
46322b33 | 326 | fprintf(stdout, "[error] Unable to unmap old base: %s.\n", |
0f980a35 MD |
327 | strerror(errno)); |
328 | assert(0); | |
329 | } | |
847bf71a | 330 | pos->base = NULL; |
0f980a35 MD |
331 | } |
332 | ||
8c572eba | 333 | /* |
46322b33 | 334 | * The caller should never ask for ctf_move_pos across packets, |
8c572eba MD |
335 | * except to get exactly at the beginning of the next packet. |
336 | */ | |
337 | if (pos->prot == PROT_WRITE) { | |
989c73bc MD |
338 | switch (whence) { |
339 | case SEEK_CUR: | |
340 | /* The writer will add padding */ | |
341 | assert(pos->offset + offset == pos->packet_size); | |
8c572eba | 342 | pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT; |
989c73bc MD |
343 | break; |
344 | case SEEK_SET: | |
345 | assert(offset == 0); /* only seek supported for now */ | |
346 | pos->cur_index = 0; | |
347 | break; | |
348 | default: | |
349 | assert(0); | |
350 | } | |
8c572eba MD |
351 | pos->content_size = -1U; /* Unknown at this point */ |
352 | pos->packet_size = WRITE_PACKET_LEN; | |
989c73bc MD |
353 | off = posix_fallocate(pos->fd, pos->mmap_offset, |
354 | pos->packet_size / CHAR_BIT); | |
8c572eba | 355 | assert(off >= 0); |
847bf71a | 356 | pos->offset = 0; |
8c572eba | 357 | } else { |
847bf71a MD |
358 | switch (whence) { |
359 | case SEEK_CUR: | |
360 | /* The reader will expect us to skip padding */ | |
361 | assert(pos->offset + offset == pos->content_size); | |
8c572eba | 362 | ++pos->cur_index; |
847bf71a MD |
363 | break; |
364 | case SEEK_SET: | |
365 | assert(offset == 0); /* only seek supported for now */ | |
366 | pos->cur_index = 0; | |
367 | break; | |
368 | default: | |
369 | assert(0); | |
370 | } | |
371 | if (pos->cur_index >= pos->packet_index->len) { | |
670977d3 | 372 | pos->offset = EOF; |
847bf71a MD |
373 | return; |
374 | } | |
8c572eba MD |
375 | index = &g_array_index(pos->packet_index, struct packet_index, |
376 | pos->cur_index); | |
377 | pos->mmap_offset = index->offset; | |
378 | ||
379 | /* Lookup context/packet size in index */ | |
380 | pos->content_size = index->content_size; | |
381 | pos->packet_size = index->packet_size; | |
847bf71a | 382 | pos->offset = index->data_offset; |
8c572eba | 383 | } |
0f980a35 | 384 | /* map new base. Need mapping length from header. */ |
8c572eba MD |
385 | pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot, |
386 | pos->flags, pos->fd, pos->mmap_offset); | |
847bf71a MD |
387 | if (pos->base == MAP_FAILED) { |
388 | fprintf(stdout, "[error] mmap error %s.\n", | |
389 | strerror(errno)); | |
390 | assert(0); | |
391 | } | |
0f980a35 MD |
392 | } |
393 | ||
b4c19c1e MD |
394 | static |
395 | int packet_metadata(struct ctf_trace *td, FILE *fp) | |
396 | { | |
397 | uint32_t magic; | |
398 | size_t len; | |
399 | int ret = 0; | |
400 | ||
401 | len = fread(&magic, sizeof(magic), 1, fp); | |
a0fe7d97 | 402 | if (len != 1) { |
b4c19c1e MD |
403 | goto end; |
404 | } | |
405 | if (magic == TSDL_MAGIC) { | |
406 | ret = 1; | |
407 | td->byte_order = BYTE_ORDER; | |
408 | } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) { | |
409 | ret = 1; | |
410 | td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ? | |
411 | LITTLE_ENDIAN : BIG_ENDIAN; | |
412 | } | |
a0fe7d97 | 413 | CTF_TRACE_SET_FIELD(td, byte_order); |
b4c19c1e MD |
414 | end: |
415 | rewind(fp); | |
416 | return ret; | |
417 | } | |
418 | ||
419 | static | |
420 | int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in, | |
421 | FILE *out) | |
422 | { | |
423 | struct metadata_packet_header header; | |
a0fe7d97 | 424 | size_t readlen, writelen, toread; |
b4c19c1e MD |
425 | char buf[4096]; |
426 | int ret = 0; | |
427 | ||
a91a962e | 428 | readlen = fread(&header, header_sizeof(header), 1, in); |
a0fe7d97 | 429 | if (readlen < 1) |
b4c19c1e MD |
430 | return -EINVAL; |
431 | ||
432 | if (td->byte_order != BYTE_ORDER) { | |
433 | header.magic = GUINT32_SWAP_LE_BE(header.magic); | |
434 | header.checksum = GUINT32_SWAP_LE_BE(header.checksum); | |
435 | header.content_size = GUINT32_SWAP_LE_BE(header.content_size); | |
436 | header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size); | |
437 | } | |
438 | if (header.checksum) | |
439 | fprintf(stdout, "[warning] checksum verification not supported yet.\n"); | |
440 | if (header.compression_scheme) { | |
441 | fprintf(stdout, "[error] compression (%u) not supported yet.\n", | |
442 | header.compression_scheme); | |
443 | return -EINVAL; | |
444 | } | |
445 | if (header.encryption_scheme) { | |
446 | fprintf(stdout, "[error] encryption (%u) not supported yet.\n", | |
447 | header.encryption_scheme); | |
448 | return -EINVAL; | |
449 | } | |
450 | if (header.checksum_scheme) { | |
451 | fprintf(stdout, "[error] checksum (%u) not supported yet.\n", | |
452 | header.checksum_scheme); | |
453 | return -EINVAL; | |
454 | } | |
455 | if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) { | |
456 | memcpy(td->uuid, header.uuid, sizeof(header.uuid)); | |
457 | CTF_TRACE_SET_FIELD(td, uuid); | |
458 | } else { | |
459 | if (uuid_compare(header.uuid, td->uuid)) | |
460 | return -EINVAL; | |
461 | } | |
462 | ||
a0fe7d97 MD |
463 | toread = header.content_size / CHAR_BIT; |
464 | ||
465 | for (;;) { | |
466 | readlen = fread(buf, sizeof(char), min(sizeof(buf), toread), in); | |
b4c19c1e MD |
467 | if (ferror(in)) { |
468 | ret = -EINVAL; | |
469 | break; | |
470 | } | |
4152822b MD |
471 | if (babeltrace_debug) { |
472 | fprintf(stdout, "[debug] metadata packet read: %s\n", | |
473 | buf); | |
474 | } | |
475 | ||
b4c19c1e MD |
476 | writelen = fwrite(buf, sizeof(char), readlen, out); |
477 | if (writelen < readlen) { | |
478 | ret = -EIO; | |
479 | break; | |
480 | } | |
481 | if (ferror(out)) { | |
482 | ret = -EINVAL; | |
483 | break; | |
484 | } | |
a0fe7d97 MD |
485 | toread -= readlen; |
486 | if (!toread) { | |
7f4b5c4d | 487 | ret = 0; /* continue reading next packet */ |
a0fe7d97 MD |
488 | break; |
489 | } | |
b4c19c1e MD |
490 | } |
491 | return ret; | |
492 | } | |
493 | ||
494 | static | |
495 | int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp, | |
496 | char **buf) | |
497 | { | |
498 | FILE *in, *out; | |
499 | size_t size; | |
500 | int ret; | |
501 | ||
502 | in = *fp; | |
c4f5487e MD |
503 | /* |
504 | * Using strlen on *buf instead of size of open_memstream | |
505 | * because its size includes garbage at the end (after final | |
506 | * \0). This is the allocated size, not the actual string size. | |
507 | */ | |
b4c19c1e MD |
508 | out = open_memstream(buf, &size); |
509 | if (out == NULL) | |
510 | return -errno; | |
511 | ||
512 | for (;;) { | |
513 | ret = ctf_open_trace_metadata_packet_read(td, in, out); | |
7f4b5c4d | 514 | if (ret) { |
b4c19c1e | 515 | break; |
7f4b5c4d MD |
516 | } |
517 | if (feof(in)) { | |
518 | ret = 0; | |
b4c19c1e MD |
519 | break; |
520 | } | |
521 | } | |
522 | fclose(out); /* flush the buffer */ | |
523 | fclose(in); | |
524 | /* open for reading */ | |
c4f5487e | 525 | *fp = fmemopen(*buf, strlen(*buf), "rb"); |
b4c19c1e MD |
526 | return 0; |
527 | } | |
528 | ||
65102a8c | 529 | static |
46322b33 | 530 | int ctf_open_trace_metadata_read(struct ctf_trace *td) |
65102a8c MD |
531 | { |
532 | struct ctf_scanner *scanner; | |
533 | FILE *fp; | |
b4c19c1e | 534 | char *buf = NULL; |
65102a8c MD |
535 | int ret = 0; |
536 | ||
46322b33 MD |
537 | td->metadata.pos.fd = openat(td->dirfd, "metadata", O_RDONLY); |
538 | if (td->metadata.pos.fd < 0) { | |
65102a8c | 539 | fprintf(stdout, "Unable to open metadata.\n"); |
46322b33 | 540 | return td->metadata.pos.fd; |
65102a8c MD |
541 | } |
542 | ||
543 | if (babeltrace_debug) | |
544 | yydebug = 1; | |
545 | ||
46322b33 | 546 | fp = fdopen(td->metadata.pos.fd, "r"); |
65102a8c | 547 | if (!fp) { |
46322b33 | 548 | fprintf(stdout, "[error] Unable to open metadata stream.\n"); |
65102a8c MD |
549 | ret = -errno; |
550 | goto end_stream; | |
551 | } | |
552 | ||
b4c19c1e MD |
553 | if (packet_metadata(td, fp)) { |
554 | ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf); | |
555 | if (ret) | |
556 | goto end_packet_read; | |
557 | } | |
558 | ||
65102a8c MD |
559 | scanner = ctf_scanner_alloc(fp); |
560 | if (!scanner) { | |
46322b33 | 561 | fprintf(stdout, "[error] Error allocating scanner\n"); |
65102a8c MD |
562 | ret = -ENOMEM; |
563 | goto end_scanner_alloc; | |
564 | } | |
565 | ret = ctf_scanner_append_ast(scanner); | |
566 | if (ret) { | |
46322b33 | 567 | fprintf(stdout, "[error] Error creating AST\n"); |
65102a8c MD |
568 | goto end; |
569 | } | |
570 | ||
571 | if (babeltrace_debug) { | |
572 | ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root); | |
573 | if (ret) { | |
46322b33 | 574 | fprintf(stdout, "[error] Error visiting AST for XML output\n"); |
65102a8c MD |
575 | goto end; |
576 | } | |
577 | } | |
578 | ||
579 | ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root); | |
580 | if (ret) { | |
46322b33 | 581 | fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret); |
65102a8c MD |
582 | goto end; |
583 | } | |
584 | ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root, | |
46322b33 | 585 | td, BYTE_ORDER); |
65102a8c | 586 | if (ret) { |
46322b33 | 587 | fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret); |
65102a8c MD |
588 | goto end; |
589 | } | |
590 | end: | |
591 | ctf_scanner_free(scanner); | |
592 | end_scanner_alloc: | |
b4c19c1e | 593 | end_packet_read: |
65102a8c | 594 | fclose(fp); |
b4c19c1e | 595 | free(buf); |
65102a8c | 596 | end_stream: |
46322b33 | 597 | close(td->metadata.pos.fd); |
0f980a35 MD |
598 | return ret; |
599 | } | |
600 | ||
601 | ||
602 | static | |
46322b33 | 603 | int create_stream_packet_index(struct ctf_trace *td, |
0f980a35 MD |
604 | struct ctf_file_stream *file_stream) |
605 | { | |
aa6bffae | 606 | struct ctf_stream_class *stream; |
0f980a35 | 607 | int len_index; |
46322b33 | 608 | struct ctf_stream_pos *pos; |
0f980a35 MD |
609 | struct stat filestats; |
610 | struct packet_index packet_index; | |
611 | int first_packet = 1; | |
612 | int ret; | |
613 | ||
614 | pos = &file_stream->pos; | |
615 | ||
616 | ret = fstat(pos->fd, &filestats); | |
617 | if (ret < 0) | |
618 | return ret; | |
619 | ||
620 | for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) { | |
621 | uint64_t stream_id = 0; | |
622 | ||
623 | if (pos->base) { | |
624 | /* unmap old base */ | |
8c572eba | 625 | ret = munmap(pos->base, pos->packet_size / CHAR_BIT); |
0f980a35 | 626 | if (ret) { |
46322b33 | 627 | fprintf(stdout, "[error] Unable to unmap old base: %s.\n", |
0f980a35 MD |
628 | strerror(errno)); |
629 | return ret; | |
630 | } | |
8c572eba | 631 | pos->base = NULL; |
0f980a35 MD |
632 | } |
633 | /* map new base. Need mapping length from header. */ | |
8c572eba | 634 | pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ, |
0f980a35 | 635 | MAP_PRIVATE, pos->fd, pos->mmap_offset); |
dc48ecad MD |
636 | pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */ |
637 | pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */ | |
0f980a35 MD |
638 | pos->offset = 0; /* Position of the packet header */ |
639 | ||
8c572eba MD |
640 | packet_index.offset = pos->mmap_offset; |
641 | packet_index.content_size = 0; | |
642 | packet_index.packet_size = 0; | |
643 | ||
0f980a35 | 644 | /* read and check header, set stream id (and check) */ |
46322b33 | 645 | if (td->packet_header) { |
0f980a35 | 646 | /* Read packet header */ |
c5e74408 MD |
647 | ret = generic_rw(&pos->parent, &td->packet_header->p); |
648 | if (ret) | |
649 | return ret; | |
46322b33 | 650 | len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic")); |
0f980a35 MD |
651 | if (len_index >= 0) { |
652 | struct definition_integer *defint; | |
b1a2f580 | 653 | struct definition *field; |
0f980a35 | 654 | |
46322b33 | 655 | field = struct_definition_get_field_from_index(td->packet_header, len_index); |
b1a2f580 MD |
656 | assert(field->declaration->id == CTF_TYPE_INTEGER); |
657 | defint = container_of(field, struct definition_integer, p); | |
0f980a35 MD |
658 | assert(defint->declaration->signedness == FALSE); |
659 | if (defint->value._unsigned != CTF_MAGIC) { | |
d8ea2d29 | 660 | fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n", |
8c572eba MD |
661 | defint->value._unsigned, |
662 | file_stream->pos.packet_index->len, | |
663 | (ssize_t) pos->mmap_offset); | |
0f980a35 MD |
664 | return -EINVAL; |
665 | } | |
666 | } | |
667 | ||
668 | /* check uuid */ | |
b4c19c1e | 669 | len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("uuid")); |
0f980a35 MD |
670 | if (len_index >= 0) { |
671 | struct definition_array *defarray; | |
b1a2f580 | 672 | struct definition *field; |
0f980a35 MD |
673 | uint64_t i; |
674 | uint8_t uuidval[UUID_LEN]; | |
675 | ||
46322b33 | 676 | field = struct_definition_get_field_from_index(td->packet_header, len_index); |
b1a2f580 MD |
677 | assert(field->declaration->id == CTF_TYPE_ARRAY); |
678 | defarray = container_of(field, struct definition_array, p); | |
3838df27 | 679 | assert(array_len(defarray) == UUID_LEN); |
0f980a35 MD |
680 | assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER); |
681 | ||
682 | for (i = 0; i < UUID_LEN; i++) { | |
683 | struct definition *elem; | |
684 | struct definition_integer *defint; | |
685 | ||
686 | elem = array_index(defarray, i); | |
687 | assert(elem); | |
688 | defint = container_of(elem, struct definition_integer, p); | |
689 | uuidval[i] = defint->value._unsigned; | |
690 | } | |
46322b33 | 691 | ret = uuid_compare(td->uuid, uuidval); |
0f980a35 MD |
692 | if (ret) { |
693 | fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n"); | |
694 | return -EINVAL; | |
695 | } | |
696 | } | |
697 | ||
698 | ||
46322b33 | 699 | len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("stream_id")); |
0f980a35 MD |
700 | if (len_index >= 0) { |
701 | struct definition_integer *defint; | |
b1a2f580 | 702 | struct definition *field; |
0f980a35 | 703 | |
46322b33 | 704 | field = struct_definition_get_field_from_index(td->packet_header, len_index); |
b1a2f580 MD |
705 | assert(field->declaration->id == CTF_TYPE_INTEGER); |
706 | defint = container_of(field, struct definition_integer, p); | |
0f980a35 MD |
707 | assert(defint->declaration->signedness == FALSE); |
708 | stream_id = defint->value._unsigned; | |
709 | } | |
710 | } | |
711 | ||
712 | if (!first_packet && file_stream->stream_id != stream_id) { | |
713 | fprintf(stdout, "[error] Stream ID is changing within a stream.\n"); | |
714 | return -EINVAL; | |
715 | } | |
716 | if (first_packet) { | |
717 | file_stream->stream_id = stream_id; | |
46322b33 | 718 | if (stream_id >= td->streams->len) { |
0f980a35 MD |
719 | fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id); |
720 | return -EINVAL; | |
721 | } | |
46322b33 | 722 | stream = g_ptr_array_index(td->streams, stream_id); |
0f980a35 MD |
723 | if (!stream) { |
724 | fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id); | |
725 | return -EINVAL; | |
726 | } | |
764af3f4 | 727 | file_stream->stream.stream_class = stream; |
0f980a35 MD |
728 | } |
729 | first_packet = 0; | |
730 | ||
dc48ecad MD |
731 | if (stream->packet_context) { |
732 | /* Read packet context */ | |
c5e74408 MD |
733 | ret = generic_rw(&pos->parent, &stream->packet_context->p); |
734 | if (ret) | |
735 | return ret; | |
dc48ecad MD |
736 | /* read content size from header */ |
737 | len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size")); | |
738 | if (len_index >= 0) { | |
739 | struct definition_integer *defint; | |
b1a2f580 | 740 | struct definition *field; |
dc48ecad MD |
741 | |
742 | field = struct_definition_get_field_from_index(stream->packet_context, len_index); | |
b1a2f580 MD |
743 | assert(field->declaration->id == CTF_TYPE_INTEGER); |
744 | defint = container_of(field, struct definition_integer, p); | |
dc48ecad | 745 | assert(defint->declaration->signedness == FALSE); |
8c572eba | 746 | packet_index.content_size = defint->value._unsigned; |
dc48ecad MD |
747 | } else { |
748 | /* Use file size for packet size */ | |
8c572eba | 749 | packet_index.content_size = filestats.st_size * CHAR_BIT; |
dc48ecad MD |
750 | } |
751 | ||
752 | /* read packet size from header */ | |
753 | len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size")); | |
754 | if (len_index >= 0) { | |
755 | struct definition_integer *defint; | |
b1a2f580 | 756 | struct definition *field; |
dc48ecad MD |
757 | |
758 | field = struct_definition_get_field_from_index(stream->packet_context, len_index); | |
b1a2f580 MD |
759 | assert(field->declaration->id == CTF_TYPE_INTEGER); |
760 | defint = container_of(field, struct definition_integer, p); | |
dc48ecad | 761 | assert(defint->declaration->signedness == FALSE); |
8c572eba | 762 | packet_index.packet_size = defint->value._unsigned; |
dc48ecad MD |
763 | } else { |
764 | /* Use content size if non-zero, else file size */ | |
8c572eba | 765 | packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT; |
dc48ecad | 766 | } |
0f980a35 MD |
767 | } else { |
768 | /* Use file size for packet size */ | |
8c572eba | 769 | packet_index.content_size = filestats.st_size * CHAR_BIT; |
0f980a35 | 770 | /* Use content size if non-zero, else file size */ |
8c572eba | 771 | packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT; |
0f980a35 | 772 | } |
546293fa MD |
773 | |
774 | /* Validate content size and packet size values */ | |
775 | if (packet_index.content_size > packet_index.packet_size) { | |
776 | fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n", | |
777 | packet_index.content_size, packet_index.packet_size); | |
778 | return -EINVAL; | |
779 | } | |
780 | ||
58b0b883 MD |
781 | if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) { |
782 | fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n", | |
783 | packet_index.content_size, (filestats.st_size - packet_index.offset) * CHAR_BIT); | |
546293fa MD |
784 | return -EINVAL; |
785 | } | |
786 | ||
847bf71a MD |
787 | /* Save position after header and context */ |
788 | packet_index.data_offset = pos->offset; | |
0f980a35 | 789 | |
0f980a35 MD |
790 | /* add index to packet array */ |
791 | g_array_append_val(file_stream->pos.packet_index, packet_index); | |
792 | ||
8c572eba | 793 | pos->mmap_offset += packet_index.packet_size / CHAR_BIT; |
0f980a35 MD |
794 | } |
795 | ||
847bf71a MD |
796 | /* Move pos back to beginning of file */ |
797 | ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */ | |
798 | ||
0f980a35 MD |
799 | return 0; |
800 | } | |
801 | ||
802 | /* | |
803 | * Note: many file streams can inherit from the same stream class | |
804 | * description (metadata). | |
805 | */ | |
806 | static | |
46322b33 | 807 | int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags) |
0f980a35 MD |
808 | { |
809 | int ret; | |
810 | struct ctf_file_stream *file_stream; | |
811 | ||
46322b33 | 812 | ret = openat(td->dirfd, path, flags); |
0f980a35 MD |
813 | if (ret < 0) |
814 | goto error; | |
815 | file_stream = g_new0(struct ctf_file_stream, 1); | |
8563e754 | 816 | ctf_init_pos(&file_stream->pos, ret, flags); |
0f980a35 MD |
817 | ret = create_stream_packet_index(td, file_stream); |
818 | if (ret) | |
819 | goto error_index; | |
820 | /* Add stream file to stream class */ | |
764af3f4 | 821 | g_ptr_array_add(file_stream->stream.stream_class->files, file_stream); |
0f980a35 MD |
822 | return 0; |
823 | ||
824 | error_index: | |
46322b33 | 825 | ctf_fini_pos(&file_stream->pos); |
0f980a35 MD |
826 | close(file_stream->pos.fd); |
827 | g_free(file_stream); | |
828 | error: | |
65102a8c MD |
829 | return ret; |
830 | } | |
831 | ||
bbefb8dd | 832 | static |
46322b33 | 833 | int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags) |
bbefb8dd MD |
834 | { |
835 | int ret; | |
65102a8c MD |
836 | struct dirent *dirent; |
837 | struct dirent *diriter; | |
838 | size_t dirent_len; | |
bbefb8dd | 839 | |
46322b33 | 840 | td->flags = flags; |
bbefb8dd MD |
841 | |
842 | /* Open trace directory */ | |
46322b33 MD |
843 | td->dir = opendir(path); |
844 | if (!td->dir) { | |
dc48ecad | 845 | fprintf(stdout, "[error] Unable to open trace directory.\n"); |
bbefb8dd MD |
846 | ret = -ENOENT; |
847 | goto error; | |
848 | } | |
849 | ||
46322b33 MD |
850 | td->dirfd = open(path, 0); |
851 | if (td->dirfd < 0) { | |
dc48ecad | 852 | fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n"); |
65102a8c MD |
853 | ret = -ENOENT; |
854 | goto error_dirfd; | |
855 | } | |
0f980a35 | 856 | |
65102a8c MD |
857 | /* |
858 | * Keep the metadata file separate. | |
859 | */ | |
bbefb8dd | 860 | |
65102a8c MD |
861 | ret = ctf_open_trace_metadata_read(td); |
862 | if (ret) { | |
863 | goto error_metadata; | |
864 | } | |
bbefb8dd MD |
865 | |
866 | /* | |
867 | * Open each stream: for each file, try to open, check magic | |
868 | * number, and get the stream ID to add to the right location in | |
869 | * the stream array. | |
bbefb8dd MD |
870 | */ |
871 | ||
65102a8c | 872 | dirent_len = offsetof(struct dirent, d_name) + |
46322b33 | 873 | fpathconf(td->dirfd, _PC_NAME_MAX) + 1; |
bbefb8dd | 874 | |
65102a8c | 875 | dirent = malloc(dirent_len); |
bbefb8dd | 876 | |
65102a8c | 877 | for (;;) { |
46322b33 | 878 | ret = readdir_r(td->dir, dirent, &diriter); |
65102a8c | 879 | if (ret) { |
dc48ecad | 880 | fprintf(stdout, "[error] Readdir error.\n"); |
65102a8c | 881 | goto readdir_error; |
65102a8c MD |
882 | } |
883 | if (!diriter) | |
884 | break; | |
d8ea2d29 MD |
885 | /* Ignore hidden files, ., .. and metadata. */ |
886 | if (!strncmp(diriter->d_name, ".", 1) | |
65102a8c MD |
887 | || !strcmp(diriter->d_name, "..") |
888 | || !strcmp(diriter->d_name, "metadata")) | |
889 | continue; | |
dc48ecad MD |
890 | ret = ctf_open_file_stream_read(td, diriter->d_name, flags); |
891 | if (ret) { | |
892 | fprintf(stdout, "[error] Open file stream error.\n"); | |
893 | goto readdir_error; | |
894 | } | |
65102a8c | 895 | } |
bbefb8dd | 896 | |
65102a8c | 897 | free(dirent); |
bbefb8dd | 898 | return 0; |
65102a8c MD |
899 | |
900 | readdir_error: | |
901 | free(dirent); | |
902 | error_metadata: | |
46322b33 | 903 | close(td->dirfd); |
65102a8c | 904 | error_dirfd: |
46322b33 | 905 | closedir(td->dir); |
bbefb8dd MD |
906 | error: |
907 | return ret; | |
908 | } | |
909 | ||
bbefb8dd MD |
910 | struct trace_descriptor *ctf_open_trace(const char *path, int flags) |
911 | { | |
46322b33 | 912 | struct ctf_trace *td; |
bbefb8dd MD |
913 | int ret; |
914 | ||
46322b33 | 915 | td = g_new0(struct ctf_trace, 1); |
bbefb8dd | 916 | |
8c572eba | 917 | switch (flags & O_ACCMODE) { |
bbefb8dd | 918 | case O_RDONLY: |
b61922b5 MD |
919 | if (!path) { |
920 | fprintf(stdout, "[error] Path missing for input CTF trace.\n"); | |
921 | goto error; | |
922 | } | |
bbefb8dd MD |
923 | ret = ctf_open_trace_read(td, path, flags); |
924 | if (ret) | |
925 | goto error; | |
926 | break; | |
989c73bc | 927 | case O_RDWR: |
46322b33 MD |
928 | fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n"); |
929 | goto error; | |
bbefb8dd | 930 | default: |
46322b33 | 931 | fprintf(stdout, "[error] Incorrect open flags.\n"); |
bbefb8dd MD |
932 | goto error; |
933 | } | |
934 | ||
46322b33 | 935 | return &td->parent; |
bbefb8dd MD |
936 | error: |
937 | g_free(td); | |
938 | return NULL; | |
939 | } | |
940 | ||
0f980a35 MD |
941 | static |
942 | void ctf_close_file_stream(struct ctf_file_stream *file_stream) | |
943 | { | |
46322b33 | 944 | ctf_fini_pos(&file_stream->pos); |
0f980a35 MD |
945 | close(file_stream->pos.fd); |
946 | } | |
947 | ||
46322b33 | 948 | void ctf_close_trace(struct trace_descriptor *tdp) |
bbefb8dd | 949 | { |
46322b33 | 950 | struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent); |
0f980a35 MD |
951 | int i; |
952 | ||
46322b33 MD |
953 | if (td->streams) { |
954 | for (i = 0; i < td->streams->len; i++) { | |
aa6bffae | 955 | struct ctf_stream_class *stream; |
0f980a35 | 956 | int j; |
46322b33 | 957 | stream = g_ptr_array_index(td->streams, i); |
0f980a35 MD |
958 | for (j = 0; j < stream->files->len; j++) { |
959 | struct ctf_file_stream *file_stream; | |
2c117823 | 960 | file_stream = g_ptr_array_index(stream->files, j); |
0f980a35 MD |
961 | ctf_close_file_stream(file_stream); |
962 | } | |
963 | ||
964 | } | |
46322b33 | 965 | g_ptr_array_free(td->streams, TRUE); |
0f980a35 | 966 | } |
46322b33 | 967 | closedir(td->dir); |
bbefb8dd MD |
968 | g_free(td); |
969 | } | |
970 | ||
7fb21036 | 971 | void __attribute__((constructor)) ctf_init(void) |
fc93b2bd MD |
972 | { |
973 | int ret; | |
974 | ||
4c8bfb7e | 975 | ctf_format.name = g_quark_from_static_string("ctf"); |
fc93b2bd MD |
976 | ret = bt_register_format(&ctf_format); |
977 | assert(!ret); | |
978 | } | |
698f0fe4 MD |
979 | |
980 | /* TODO: finalize */ |