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