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