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