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