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