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