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