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