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