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