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