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, | |
60 | int whence)); | |
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, | |
616 | int whence)) | |
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 | ||
2d0bea29 MD |
634 | td->metadata = &metadata_stream->parent; |
635 | metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY); | |
636 | if (metadata_stream->pos.fd < 0) { | |
65102a8c | 637 | fprintf(stdout, "Unable to open metadata.\n"); |
2d0bea29 | 638 | return metadata_stream->pos.fd; |
65102a8c MD |
639 | } |
640 | ||
641 | if (babeltrace_debug) | |
642 | yydebug = 1; | |
643 | ||
2d0bea29 | 644 | fp = fdopen(metadata_stream->pos.fd, "r"); |
65102a8c | 645 | if (!fp) { |
46322b33 | 646 | fprintf(stdout, "[error] Unable to open metadata stream.\n"); |
65102a8c MD |
647 | ret = -errno; |
648 | goto end_stream; | |
649 | } | |
650 | ||
b4c19c1e MD |
651 | if (packet_metadata(td, fp)) { |
652 | ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf); | |
653 | if (ret) | |
654 | goto end_packet_read; | |
da75b0f7 | 655 | } else { |
5c262147 MD |
656 | unsigned int major, minor; |
657 | ssize_t nr_items; | |
8c2df3f5 | 658 | |
da75b0f7 | 659 | td->byte_order = BYTE_ORDER; |
8c2df3f5 | 660 | |
5c262147 MD |
661 | /* Check text-only metadata header and version */ |
662 | nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor); | |
663 | if (nr_items < 2) | |
664 | fprintf(stdout, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n"); | |
665 | if (check_version(major, minor) < 0) { | |
666 | ret = -EINVAL; | |
667 | goto end_packet_read; | |
668 | } | |
8c2df3f5 | 669 | rewind(fp); |
b4c19c1e MD |
670 | } |
671 | ||
65102a8c MD |
672 | scanner = ctf_scanner_alloc(fp); |
673 | if (!scanner) { | |
46322b33 | 674 | fprintf(stdout, "[error] Error allocating scanner\n"); |
65102a8c MD |
675 | ret = -ENOMEM; |
676 | goto end_scanner_alloc; | |
677 | } | |
678 | ret = ctf_scanner_append_ast(scanner); | |
679 | if (ret) { | |
46322b33 | 680 | fprintf(stdout, "[error] Error creating AST\n"); |
65102a8c MD |
681 | goto end; |
682 | } | |
683 | ||
684 | if (babeltrace_debug) { | |
685 | ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root); | |
686 | if (ret) { | |
46322b33 | 687 | fprintf(stdout, "[error] Error visiting AST for XML output\n"); |
65102a8c MD |
688 | goto end; |
689 | } | |
690 | } | |
691 | ||
692 | ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root); | |
693 | if (ret) { | |
46322b33 | 694 | fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret); |
65102a8c MD |
695 | goto end; |
696 | } | |
697 | ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root, | |
ac5c6ca0 | 698 | td, td->byte_order); |
65102a8c | 699 | if (ret) { |
46322b33 | 700 | fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret); |
65102a8c MD |
701 | goto end; |
702 | } | |
703 | end: | |
704 | ctf_scanner_free(scanner); | |
705 | end_scanner_alloc: | |
b4c19c1e | 706 | end_packet_read: |
65102a8c | 707 | fclose(fp); |
b4c19c1e | 708 | free(buf); |
65102a8c | 709 | end_stream: |
2d0bea29 MD |
710 | close(metadata_stream->pos.fd); |
711 | if (ret) | |
712 | g_free(metadata_stream); | |
0f980a35 MD |
713 | return ret; |
714 | } | |
715 | ||
e28d4618 | 716 | static |
42dc00b7 MD |
717 | struct ctf_stream_event *create_event_definitions(struct ctf_trace *td, |
718 | struct ctf_stream *stream, | |
719 | struct ctf_event *event) | |
e28d4618 | 720 | { |
42dc00b7 | 721 | struct ctf_stream_event *stream_event = g_new0(struct ctf_stream_event, 1); |
e28d4618 MD |
722 | |
723 | if (event->context_decl) { | |
724 | struct definition *definition = | |
725 | event->context_decl->p.definition_new(&event->context_decl->p, | |
726 | stream->parent_def_scope, 0, 0, "event.context"); | |
727 | if (!definition) { | |
728 | goto error; | |
729 | } | |
42dc00b7 | 730 | stream_event->event_context = container_of(definition, |
e28d4618 | 731 | struct definition_struct, p); |
42dc00b7 | 732 | stream->parent_def_scope = stream_event->event_context->p.scope; |
e28d4618 MD |
733 | } |
734 | if (event->fields_decl) { | |
735 | struct definition *definition = | |
736 | event->fields_decl->p.definition_new(&event->fields_decl->p, | |
737 | stream->parent_def_scope, 0, 0, "event.fields"); | |
738 | if (!definition) { | |
739 | goto error; | |
740 | } | |
42dc00b7 | 741 | stream_event->event_fields = container_of(definition, |
e28d4618 | 742 | struct definition_struct, p); |
42dc00b7 | 743 | stream->parent_def_scope = stream_event->event_fields->p.scope; |
e28d4618 | 744 | } |
42dc00b7 | 745 | return stream_event; |
e28d4618 MD |
746 | |
747 | error: | |
42dc00b7 MD |
748 | if (stream_event->event_fields) |
749 | definition_unref(&stream_event->event_fields->p); | |
750 | if (stream_event->event_context) | |
751 | definition_unref(&stream_event->event_context->p); | |
e28d4618 MD |
752 | return NULL; |
753 | } | |
754 | ||
755 | static | |
756 | int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream) | |
757 | { | |
758 | struct ctf_stream_class *stream_class; | |
759 | int ret; | |
760 | int i; | |
761 | ||
762 | if (stream->stream_definitions_created) | |
763 | return 0; | |
764 | ||
765 | stream_class = stream->stream_class; | |
766 | ||
767 | if (stream_class->packet_context_decl) { | |
768 | struct definition *definition = | |
769 | stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p, | |
770 | stream->parent_def_scope, 0, 0, "stream.packet.context"); | |
771 | if (!definition) { | |
772 | ret = -EINVAL; | |
773 | goto error; | |
774 | } | |
775 | stream->stream_packet_context = container_of(definition, | |
776 | struct definition_struct, p); | |
777 | stream->parent_def_scope = stream->stream_packet_context->p.scope; | |
778 | } | |
779 | if (stream_class->event_header_decl) { | |
780 | struct definition *definition = | |
781 | stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p, | |
782 | stream->parent_def_scope, 0, 0, "stream.event.header"); | |
783 | if (!definition) { | |
784 | ret = -EINVAL; | |
785 | goto error; | |
786 | } | |
787 | stream->stream_event_header = | |
788 | container_of(definition, struct definition_struct, p); | |
789 | stream->parent_def_scope = stream->stream_event_header->p.scope; | |
790 | } | |
791 | if (stream_class->event_context_decl) { | |
792 | struct definition *definition = | |
793 | stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p, | |
794 | stream->parent_def_scope, 0, 0, "stream.event.context"); | |
795 | if (!definition) { | |
796 | ret = -EINVAL; | |
797 | goto error; | |
798 | } | |
799 | stream->stream_event_context = | |
800 | container_of(definition, struct definition_struct, p); | |
801 | stream->parent_def_scope = stream->stream_event_context->p.scope; | |
802 | } | |
803 | stream->events_by_id = g_ptr_array_new(); | |
804 | g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len); | |
805 | for (i = 0; i < stream->events_by_id->len; i++) { | |
806 | struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i); | |
42dc00b7 | 807 | struct ctf_stream_event *stream_event; |
e28d4618 MD |
808 | |
809 | if (!event) | |
810 | continue; | |
42dc00b7 MD |
811 | stream_event = create_event_definitions(td, stream, event); |
812 | if (!stream_event) | |
e28d4618 | 813 | goto error_event; |
42dc00b7 | 814 | g_ptr_array_index(stream->events_by_id, i) = stream_event; |
e28d4618 MD |
815 | } |
816 | return 0; | |
817 | ||
818 | error_event: | |
819 | for (i = 0; i < stream->events_by_id->len; i++) { | |
42dc00b7 MD |
820 | struct ctf_stream_event *stream_event = g_ptr_array_index(stream->events_by_id, i); |
821 | if (stream_event) | |
822 | g_free(stream_event); | |
e28d4618 MD |
823 | } |
824 | g_ptr_array_free(stream->events_by_id, TRUE); | |
825 | error: | |
826 | if (stream->stream_event_context) | |
827 | definition_unref(&stream->stream_event_context->p); | |
828 | if (stream->stream_event_header) | |
829 | definition_unref(&stream->stream_event_header->p); | |
830 | if (stream->stream_packet_context) | |
831 | definition_unref(&stream->stream_packet_context->p); | |
832 | return ret; | |
833 | } | |
834 | ||
0f980a35 MD |
835 | |
836 | static | |
46322b33 | 837 | int create_stream_packet_index(struct ctf_trace *td, |
0f980a35 MD |
838 | struct ctf_file_stream *file_stream) |
839 | { | |
aa6bffae | 840 | struct ctf_stream_class *stream; |
0f980a35 | 841 | int len_index; |
46322b33 | 842 | struct ctf_stream_pos *pos; |
0f980a35 MD |
843 | struct stat filestats; |
844 | struct packet_index packet_index; | |
845 | int first_packet = 1; | |
846 | int ret; | |
847 | ||
848 | pos = &file_stream->pos; | |
849 | ||
850 | ret = fstat(pos->fd, &filestats); | |
851 | if (ret < 0) | |
852 | return ret; | |
853 | ||
8895362d MD |
854 | if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT) |
855 | return -EINVAL; | |
856 | ||
0f980a35 MD |
857 | for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) { |
858 | uint64_t stream_id = 0; | |
859 | ||
860 | if (pos->base) { | |
861 | /* unmap old base */ | |
8c572eba | 862 | ret = munmap(pos->base, pos->packet_size / CHAR_BIT); |
0f980a35 | 863 | if (ret) { |
46322b33 | 864 | fprintf(stdout, "[error] Unable to unmap old base: %s.\n", |
0f980a35 MD |
865 | strerror(errno)); |
866 | return ret; | |
867 | } | |
8c572eba | 868 | pos->base = NULL; |
0f980a35 MD |
869 | } |
870 | /* map new base. Need mapping length from header. */ | |
8c572eba | 871 | pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ, |
0f980a35 | 872 | MAP_PRIVATE, pos->fd, pos->mmap_offset); |
dc48ecad MD |
873 | pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */ |
874 | pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */ | |
0f980a35 MD |
875 | pos->offset = 0; /* Position of the packet header */ |
876 | ||
8c572eba MD |
877 | packet_index.offset = pos->mmap_offset; |
878 | packet_index.content_size = 0; | |
879 | packet_index.packet_size = 0; | |
75cc2c35 MD |
880 | packet_index.timestamp_begin = 0; |
881 | packet_index.timestamp_end = 0; | |
8c572eba | 882 | |
0f980a35 | 883 | /* read and check header, set stream id (and check) */ |
2d0bea29 | 884 | if (file_stream->parent.trace_packet_header) { |
0f980a35 | 885 | /* Read packet header */ |
2d0bea29 | 886 | ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p); |
c5e74408 MD |
887 | if (ret) |
888 | return ret; | |
2d0bea29 | 889 | len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic")); |
0f980a35 MD |
890 | if (len_index >= 0) { |
891 | struct definition_integer *defint; | |
b1a2f580 | 892 | struct definition *field; |
0f980a35 | 893 | |
2d0bea29 | 894 | field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index); |
b1a2f580 MD |
895 | assert(field->declaration->id == CTF_TYPE_INTEGER); |
896 | defint = container_of(field, struct definition_integer, p); | |
0f980a35 MD |
897 | assert(defint->declaration->signedness == FALSE); |
898 | if (defint->value._unsigned != CTF_MAGIC) { | |
d8ea2d29 | 899 | fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n", |
8c572eba MD |
900 | defint->value._unsigned, |
901 | file_stream->pos.packet_index->len, | |
902 | (ssize_t) pos->mmap_offset); | |
0f980a35 MD |
903 | return -EINVAL; |
904 | } | |
905 | } | |
906 | ||
907 | /* check uuid */ | |
2d0bea29 | 908 | len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid")); |
0f980a35 MD |
909 | if (len_index >= 0) { |
910 | struct definition_array *defarray; | |
b1a2f580 | 911 | struct definition *field; |
0f980a35 MD |
912 | uint64_t i; |
913 | uint8_t uuidval[UUID_LEN]; | |
914 | ||
2d0bea29 | 915 | field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index); |
b1a2f580 MD |
916 | assert(field->declaration->id == CTF_TYPE_ARRAY); |
917 | defarray = container_of(field, struct definition_array, p); | |
3838df27 | 918 | assert(array_len(defarray) == UUID_LEN); |
0f980a35 MD |
919 | assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER); |
920 | ||
921 | for (i = 0; i < UUID_LEN; i++) { | |
922 | struct definition *elem; | |
923 | struct definition_integer *defint; | |
924 | ||
925 | elem = array_index(defarray, i); | |
926 | assert(elem); | |
927 | defint = container_of(elem, struct definition_integer, p); | |
928 | uuidval[i] = defint->value._unsigned; | |
929 | } | |
46322b33 | 930 | ret = uuid_compare(td->uuid, uuidval); |
0f980a35 MD |
931 | if (ret) { |
932 | fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n"); | |
933 | return -EINVAL; | |
934 | } | |
935 | } | |
936 | ||
937 | ||
2d0bea29 | 938 | len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id")); |
0f980a35 MD |
939 | if (len_index >= 0) { |
940 | struct definition_integer *defint; | |
b1a2f580 | 941 | struct definition *field; |
0f980a35 | 942 | |
2d0bea29 | 943 | field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index); |
b1a2f580 MD |
944 | assert(field->declaration->id == CTF_TYPE_INTEGER); |
945 | defint = container_of(field, struct definition_integer, p); | |
0f980a35 MD |
946 | assert(defint->declaration->signedness == FALSE); |
947 | stream_id = defint->value._unsigned; | |
948 | } | |
949 | } | |
950 | ||
2d0bea29 | 951 | if (!first_packet && file_stream->parent.stream_id != stream_id) { |
0f980a35 MD |
952 | fprintf(stdout, "[error] Stream ID is changing within a stream.\n"); |
953 | return -EINVAL; | |
954 | } | |
955 | if (first_packet) { | |
2d0bea29 | 956 | file_stream->parent.stream_id = stream_id; |
46322b33 | 957 | if (stream_id >= td->streams->len) { |
0f980a35 MD |
958 | fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id); |
959 | return -EINVAL; | |
960 | } | |
46322b33 | 961 | stream = g_ptr_array_index(td->streams, stream_id); |
0f980a35 MD |
962 | if (!stream) { |
963 | fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id); | |
964 | return -EINVAL; | |
965 | } | |
2d0bea29 | 966 | file_stream->parent.stream_class = stream; |
01c76b24 | 967 | ret = create_stream_definitions(td, &file_stream->parent); |
862434ec MD |
968 | if (ret) |
969 | return ret; | |
0f980a35 MD |
970 | } |
971 | first_packet = 0; | |
972 | ||
2d0bea29 | 973 | if (file_stream->parent.stream_packet_context) { |
dc48ecad | 974 | /* Read packet context */ |
2d0bea29 | 975 | ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p); |
c5e74408 MD |
976 | if (ret) |
977 | return ret; | |
dc48ecad | 978 | /* read content size from header */ |
2d0bea29 | 979 | len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size")); |
dc48ecad MD |
980 | if (len_index >= 0) { |
981 | struct definition_integer *defint; | |
b1a2f580 | 982 | struct definition *field; |
dc48ecad | 983 | |
2d0bea29 | 984 | field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index); |
b1a2f580 MD |
985 | assert(field->declaration->id == CTF_TYPE_INTEGER); |
986 | defint = container_of(field, struct definition_integer, p); | |
dc48ecad | 987 | assert(defint->declaration->signedness == FALSE); |
8c572eba | 988 | packet_index.content_size = defint->value._unsigned; |
dc48ecad MD |
989 | } else { |
990 | /* Use file size for packet size */ | |
8c572eba | 991 | packet_index.content_size = filestats.st_size * CHAR_BIT; |
dc48ecad MD |
992 | } |
993 | ||
994 | /* read packet size from header */ | |
2d0bea29 | 995 | len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size")); |
dc48ecad MD |
996 | if (len_index >= 0) { |
997 | struct definition_integer *defint; | |
b1a2f580 | 998 | struct definition *field; |
dc48ecad | 999 | |
2d0bea29 | 1000 | field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index); |
b1a2f580 MD |
1001 | assert(field->declaration->id == CTF_TYPE_INTEGER); |
1002 | defint = container_of(field, struct definition_integer, p); | |
dc48ecad | 1003 | assert(defint->declaration->signedness == FALSE); |
8c572eba | 1004 | packet_index.packet_size = defint->value._unsigned; |
dc48ecad MD |
1005 | } else { |
1006 | /* Use content size if non-zero, else file size */ | |
8c572eba | 1007 | packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT; |
dc48ecad | 1008 | } |
75cc2c35 MD |
1009 | |
1010 | /* read timestamp begin from header */ | |
2d0bea29 | 1011 | len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin")); |
75cc2c35 MD |
1012 | if (len_index >= 0) { |
1013 | struct definition_integer *defint; | |
1014 | struct definition *field; | |
1015 | ||
2d0bea29 | 1016 | field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index); |
75cc2c35 MD |
1017 | assert(field->declaration->id == CTF_TYPE_INTEGER); |
1018 | defint = container_of(field, struct definition_integer, p); | |
1019 | assert(defint->declaration->signedness == FALSE); | |
1020 | packet_index.timestamp_begin = defint->value._unsigned; | |
1021 | } | |
1022 | ||
1023 | /* read timestamp end from header */ | |
2d0bea29 | 1024 | len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end")); |
75cc2c35 MD |
1025 | if (len_index >= 0) { |
1026 | struct definition_integer *defint; | |
1027 | struct definition *field; | |
1028 | ||
2d0bea29 | 1029 | field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index); |
75cc2c35 MD |
1030 | assert(field->declaration->id == CTF_TYPE_INTEGER); |
1031 | defint = container_of(field, struct definition_integer, p); | |
1032 | assert(defint->declaration->signedness == FALSE); | |
1033 | packet_index.timestamp_end = defint->value._unsigned; | |
1034 | } | |
0f980a35 MD |
1035 | } else { |
1036 | /* Use file size for packet size */ | |
8c572eba | 1037 | packet_index.content_size = filestats.st_size * CHAR_BIT; |
0f980a35 | 1038 | /* Use content size if non-zero, else file size */ |
8c572eba | 1039 | packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT; |
0f980a35 | 1040 | } |
546293fa MD |
1041 | |
1042 | /* Validate content size and packet size values */ | |
1043 | if (packet_index.content_size > packet_index.packet_size) { | |
1044 | fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n", | |
1045 | packet_index.content_size, packet_index.packet_size); | |
1046 | return -EINVAL; | |
1047 | } | |
1048 | ||
58b0b883 MD |
1049 | if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) { |
1050 | fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n", | |
5aebb2ee | 1051 | packet_index.content_size, (size_t) (filestats.st_size - packet_index.offset) * CHAR_BIT); |
546293fa MD |
1052 | return -EINVAL; |
1053 | } | |
1054 | ||
847bf71a MD |
1055 | /* Save position after header and context */ |
1056 | packet_index.data_offset = pos->offset; | |
0f980a35 | 1057 | |
0f980a35 MD |
1058 | /* add index to packet array */ |
1059 | g_array_append_val(file_stream->pos.packet_index, packet_index); | |
1060 | ||
8c572eba | 1061 | pos->mmap_offset += packet_index.packet_size / CHAR_BIT; |
0f980a35 MD |
1062 | } |
1063 | ||
847bf71a MD |
1064 | /* Move pos back to beginning of file */ |
1065 | ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */ | |
1066 | ||
0f980a35 MD |
1067 | return 0; |
1068 | } | |
1069 | ||
e28d4618 MD |
1070 | static |
1071 | int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream) | |
1072 | { | |
1073 | int ret; | |
1074 | ||
1075 | if (td->packet_header_decl) { | |
1076 | struct definition *definition = | |
1077 | td->packet_header_decl->p.definition_new(&td->packet_header_decl->p, | |
1078 | stream->parent_def_scope, 0, 0, "trace.packet.header"); | |
1079 | if (!definition) { | |
1080 | ret = -EINVAL; | |
1081 | goto error; | |
1082 | } | |
1083 | stream->trace_packet_header = | |
1084 | container_of(definition, struct definition_struct, p); | |
1085 | stream->parent_def_scope = stream->trace_packet_header->p.scope; | |
1086 | } | |
1087 | ||
1088 | return 0; | |
1089 | ||
1090 | error: | |
1091 | return ret; | |
1092 | } | |
1093 | ||
0f980a35 MD |
1094 | /* |
1095 | * Note: many file streams can inherit from the same stream class | |
1096 | * description (metadata). | |
1097 | */ | |
1098 | static | |
b086c01a JD |
1099 | int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags, |
1100 | void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, | |
1101 | int whence)) | |
0f980a35 MD |
1102 | { |
1103 | int ret; | |
1104 | struct ctf_file_stream *file_stream; | |
1105 | ||
46322b33 | 1106 | ret = openat(td->dirfd, path, flags); |
0f980a35 MD |
1107 | if (ret < 0) |
1108 | goto error; | |
1109 | file_stream = g_new0(struct ctf_file_stream, 1); | |
b086c01a JD |
1110 | |
1111 | if (move_pos_slow) { | |
1112 | file_stream->pos.move_pos_slow = move_pos_slow; | |
1113 | } else { | |
1114 | fprintf(stderr, "[error] move_pos_slow function undefined.\n"); | |
1115 | ret = -1; | |
1116 | goto error_def; | |
1117 | } | |
1118 | ||
8563e754 | 1119 | ctf_init_pos(&file_stream->pos, ret, flags); |
2d0bea29 | 1120 | ret = create_trace_definitions(td, &file_stream->parent); |
e28d4618 MD |
1121 | if (ret) |
1122 | goto error_def; | |
0f980a35 MD |
1123 | ret = create_stream_packet_index(td, file_stream); |
1124 | if (ret) | |
1125 | goto error_index; | |
1126 | /* Add stream file to stream class */ | |
2d0bea29 MD |
1127 | g_ptr_array_add(file_stream->parent.stream_class->streams, |
1128 | &file_stream->parent); | |
0f980a35 MD |
1129 | return 0; |
1130 | ||
1131 | error_index: | |
2d0bea29 MD |
1132 | if (file_stream->parent.trace_packet_header) |
1133 | definition_unref(&file_stream->parent.trace_packet_header->p); | |
e28d4618 | 1134 | error_def: |
46322b33 | 1135 | ctf_fini_pos(&file_stream->pos); |
0f980a35 MD |
1136 | close(file_stream->pos.fd); |
1137 | g_free(file_stream); | |
1138 | error: | |
65102a8c MD |
1139 | return ret; |
1140 | } | |
1141 | ||
bbefb8dd | 1142 | static |
b086c01a JD |
1143 | int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags, |
1144 | void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, | |
1145 | int whence)) | |
bbefb8dd MD |
1146 | { |
1147 | int ret; | |
65102a8c MD |
1148 | struct dirent *dirent; |
1149 | struct dirent *diriter; | |
1150 | size_t dirent_len; | |
bbefb8dd | 1151 | |
46322b33 | 1152 | td->flags = flags; |
bbefb8dd MD |
1153 | |
1154 | /* Open trace directory */ | |
46322b33 MD |
1155 | td->dir = opendir(path); |
1156 | if (!td->dir) { | |
dc48ecad | 1157 | fprintf(stdout, "[error] Unable to open trace directory.\n"); |
bbefb8dd MD |
1158 | ret = -ENOENT; |
1159 | goto error; | |
1160 | } | |
1161 | ||
46322b33 MD |
1162 | td->dirfd = open(path, 0); |
1163 | if (td->dirfd < 0) { | |
dc48ecad | 1164 | fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n"); |
65102a8c MD |
1165 | ret = -ENOENT; |
1166 | goto error_dirfd; | |
1167 | } | |
0f980a35 | 1168 | |
65102a8c MD |
1169 | /* |
1170 | * Keep the metadata file separate. | |
1171 | */ | |
bbefb8dd | 1172 | |
b086c01a | 1173 | ret = ctf_open_trace_metadata_read(td, move_pos_slow); |
65102a8c MD |
1174 | if (ret) { |
1175 | goto error_metadata; | |
1176 | } | |
bbefb8dd MD |
1177 | |
1178 | /* | |
1179 | * Open each stream: for each file, try to open, check magic | |
1180 | * number, and get the stream ID to add to the right location in | |
1181 | * the stream array. | |
bbefb8dd MD |
1182 | */ |
1183 | ||
65102a8c | 1184 | dirent_len = offsetof(struct dirent, d_name) + |
46322b33 | 1185 | fpathconf(td->dirfd, _PC_NAME_MAX) + 1; |
bbefb8dd | 1186 | |
65102a8c | 1187 | dirent = malloc(dirent_len); |
bbefb8dd | 1188 | |
65102a8c | 1189 | for (;;) { |
46322b33 | 1190 | ret = readdir_r(td->dir, dirent, &diriter); |
65102a8c | 1191 | if (ret) { |
dc48ecad | 1192 | fprintf(stdout, "[error] Readdir error.\n"); |
65102a8c | 1193 | goto readdir_error; |
65102a8c MD |
1194 | } |
1195 | if (!diriter) | |
1196 | break; | |
d8ea2d29 MD |
1197 | /* Ignore hidden files, ., .. and metadata. */ |
1198 | if (!strncmp(diriter->d_name, ".", 1) | |
65102a8c MD |
1199 | || !strcmp(diriter->d_name, "..") |
1200 | || !strcmp(diriter->d_name, "metadata")) | |
1201 | continue; | |
b086c01a | 1202 | ret = ctf_open_file_stream_read(td, diriter->d_name, flags, move_pos_slow); |
dc48ecad MD |
1203 | if (ret) { |
1204 | fprintf(stdout, "[error] Open file stream error.\n"); | |
1205 | goto readdir_error; | |
1206 | } | |
65102a8c | 1207 | } |
bbefb8dd | 1208 | |
65102a8c | 1209 | free(dirent); |
bbefb8dd | 1210 | return 0; |
65102a8c MD |
1211 | |
1212 | readdir_error: | |
1213 | free(dirent); | |
1214 | error_metadata: | |
46322b33 | 1215 | close(td->dirfd); |
65102a8c | 1216 | error_dirfd: |
46322b33 | 1217 | closedir(td->dir); |
bbefb8dd MD |
1218 | error: |
1219 | return ret; | |
1220 | } | |
1221 | ||
e9378815 | 1222 | static |
b086c01a JD |
1223 | struct trace_descriptor *ctf_open_trace(const char *path, int flags, |
1224 | void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, | |
1225 | int whence)) | |
bbefb8dd | 1226 | { |
46322b33 | 1227 | struct ctf_trace *td; |
bbefb8dd MD |
1228 | int ret; |
1229 | ||
46322b33 | 1230 | td = g_new0(struct ctf_trace, 1); |
bbefb8dd | 1231 | |
8c572eba | 1232 | switch (flags & O_ACCMODE) { |
bbefb8dd | 1233 | case O_RDONLY: |
b61922b5 MD |
1234 | if (!path) { |
1235 | fprintf(stdout, "[error] Path missing for input CTF trace.\n"); | |
1236 | goto error; | |
1237 | } | |
b086c01a | 1238 | ret = ctf_open_trace_read(td, path, flags, move_pos_slow); |
bbefb8dd MD |
1239 | if (ret) |
1240 | goto error; | |
1241 | break; | |
989c73bc | 1242 | case O_RDWR: |
46322b33 MD |
1243 | fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n"); |
1244 | goto error; | |
bbefb8dd | 1245 | default: |
46322b33 | 1246 | fprintf(stdout, "[error] Incorrect open flags.\n"); |
bbefb8dd MD |
1247 | goto error; |
1248 | } | |
1249 | ||
46322b33 | 1250 | return &td->parent; |
bbefb8dd MD |
1251 | error: |
1252 | g_free(td); | |
1253 | return NULL; | |
1254 | } | |
1255 | ||
0f980a35 MD |
1256 | static |
1257 | void ctf_close_file_stream(struct ctf_file_stream *file_stream) | |
1258 | { | |
46322b33 | 1259 | ctf_fini_pos(&file_stream->pos); |
0f980a35 MD |
1260 | close(file_stream->pos.fd); |
1261 | } | |
1262 | ||
e9378815 | 1263 | static |
46322b33 | 1264 | void ctf_close_trace(struct trace_descriptor *tdp) |
bbefb8dd | 1265 | { |
46322b33 | 1266 | struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent); |
0f980a35 MD |
1267 | int i; |
1268 | ||
46322b33 MD |
1269 | if (td->streams) { |
1270 | for (i = 0; i < td->streams->len; i++) { | |
aa6bffae | 1271 | struct ctf_stream_class *stream; |
0f980a35 | 1272 | int j; |
e9378815 | 1273 | |
46322b33 | 1274 | stream = g_ptr_array_index(td->streams, i); |
e9378815 MD |
1275 | if (!stream) |
1276 | continue; | |
2d0bea29 | 1277 | for (j = 0; j < stream->streams->len; j++) { |
0f980a35 | 1278 | struct ctf_file_stream *file_stream; |
2d0bea29 | 1279 | file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent); |
0f980a35 MD |
1280 | ctf_close_file_stream(file_stream); |
1281 | } | |
1282 | ||
1283 | } | |
46322b33 | 1284 | g_ptr_array_free(td->streams, TRUE); |
0f980a35 | 1285 | } |
46322b33 | 1286 | closedir(td->dir); |
bbefb8dd MD |
1287 | g_free(td); |
1288 | } | |
1289 | ||
7fb21036 | 1290 | void __attribute__((constructor)) ctf_init(void) |
fc93b2bd MD |
1291 | { |
1292 | int ret; | |
1293 | ||
4c8bfb7e | 1294 | ctf_format.name = g_quark_from_static_string("ctf"); |
fc93b2bd MD |
1295 | ret = bt_register_format(&ctf_format); |
1296 | assert(!ret); | |
1297 | } | |
698f0fe4 MD |
1298 | |
1299 | /* TODO: finalize */ |