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