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