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. | |
c462e188 MD |
19 | * |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
fc93b2bd MD |
27 | */ |
28 | ||
29 | #include <babeltrace/format.h> | |
30 | #include <babeltrace/ctf/types.h> | |
bbefb8dd | 31 | #include <babeltrace/ctf/metadata.h> |
70bd0a12 | 32 | #include <babeltrace/babeltrace-internal.h> |
e4195791 | 33 | #include <babeltrace/ctf/events-internal.h> |
98a04903 JD |
34 | #include <babeltrace/trace-handle-internal.h> |
35 | #include <babeltrace/context-internal.h> | |
43e34335 MD |
36 | #include <babeltrace/uuid.h> |
37 | #include <babeltrace/endian.h> | |
0f980a35 | 38 | #include <inttypes.h> |
b4c19c1e | 39 | #include <stdio.h> |
0f980a35 | 40 | #include <sys/mman.h> |
bbefb8dd | 41 | #include <errno.h> |
bbefb8dd | 42 | #include <sys/types.h> |
65102a8c | 43 | #include <sys/stat.h> |
bbefb8dd | 44 | #include <fcntl.h> |
65102a8c | 45 | #include <dirent.h> |
bbefb8dd | 46 | #include <glib.h> |
65102a8c MD |
47 | #include <unistd.h> |
48 | #include <stdlib.h> | |
49 | ||
65102a8c MD |
50 | #include "metadata/ctf-scanner.h" |
51 | #include "metadata/ctf-parser.h" | |
52 | #include "metadata/ctf-ast.h" | |
c34ea0fa | 53 | #include "events-private.h" |
f8370579 | 54 | #include "memstream.h" |
65102a8c | 55 | |
0f980a35 MD |
56 | /* |
57 | * We currently simply map a page to read the packet header and packet | |
8c572eba | 58 | * context to get the packet length and content length. (in bits) |
0f980a35 | 59 | */ |
8c572eba MD |
60 | #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT) |
61 | #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT) | |
0f980a35 | 62 | |
a0fe7d97 MD |
63 | #ifndef min |
64 | #define min(a, b) (((a) < (b)) ? (a) : (b)) | |
65 | #endif | |
66 | ||
7d97fad9 MD |
67 | #define NSEC_PER_SEC 1000000000ULL |
68 | ||
03798a93 | 69 | int opt_clock_cycles, |
7d97fad9 MD |
70 | opt_clock_seconds, |
71 | opt_clock_date, | |
72 | opt_clock_gmt; | |
73 | ||
74 | uint64_t opt_clock_offset; | |
75 | ||
65102a8c | 76 | extern int yydebug; |
bbefb8dd | 77 | |
e9378815 | 78 | static |
1b8455b7 | 79 | struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags, |
1cf393f6 | 80 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
20d0dcf9 MD |
81 | int whence), |
82 | FILE *metadata_fp); | |
e9378815 | 83 | static |
1b8455b7 | 84 | struct bt_trace_descriptor *ctf_open_mmap_trace( |
c150f912 | 85 | struct bt_mmap_stream_list *mmap_list, |
1cf393f6 | 86 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
20d0dcf9 | 87 | int whence), |
f571dfb1 | 88 | FILE *metadata_fp); |
98a04903 | 89 | static |
1b8455b7 | 90 | void ctf_set_context(struct bt_trace_descriptor *descriptor, |
98a04903 JD |
91 | struct bt_context *ctx); |
92 | static | |
1b8455b7 | 93 | void ctf_set_handle(struct bt_trace_descriptor *descriptor, |
98a04903 | 94 | struct bt_trace_handle *handle); |
f571dfb1 JD |
95 | |
96 | static | |
1b8455b7 | 97 | int ctf_close_trace(struct bt_trace_descriptor *descriptor); |
30c276af | 98 | static |
1b8455b7 | 99 | uint64_t ctf_timestamp_begin(struct bt_trace_descriptor *descriptor, |
03798a93 | 100 | struct bt_trace_handle *handle, enum bt_clock_type type); |
30c276af | 101 | static |
1b8455b7 | 102 | uint64_t ctf_timestamp_end(struct bt_trace_descriptor *descriptor, |
03798a93 JD |
103 | struct bt_trace_handle *handle, enum bt_clock_type type); |
104 | static | |
1b8455b7 | 105 | int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp); |
fc93b2bd | 106 | |
1ae19169 MD |
107 | static |
108 | rw_dispatch read_dispatch_table[] = { | |
d11e9c49 MD |
109 | [ CTF_TYPE_INTEGER ] = ctf_integer_read, |
110 | [ CTF_TYPE_FLOAT ] = ctf_float_read, | |
111 | [ CTF_TYPE_ENUM ] = ctf_enum_read, | |
112 | [ CTF_TYPE_STRING ] = ctf_string_read, | |
113 | [ CTF_TYPE_STRUCT ] = ctf_struct_rw, | |
114 | [ CTF_TYPE_VARIANT ] = ctf_variant_rw, | |
81dee1bb MD |
115 | [ CTF_TYPE_ARRAY ] = ctf_array_read, |
116 | [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read, | |
d11e9c49 MD |
117 | }; |
118 | ||
1ae19169 MD |
119 | static |
120 | rw_dispatch write_dispatch_table[] = { | |
d11e9c49 MD |
121 | [ CTF_TYPE_INTEGER ] = ctf_integer_write, |
122 | [ CTF_TYPE_FLOAT ] = ctf_float_write, | |
123 | [ CTF_TYPE_ENUM ] = ctf_enum_write, | |
124 | [ CTF_TYPE_STRING ] = ctf_string_write, | |
125 | [ CTF_TYPE_STRUCT ] = ctf_struct_rw, | |
126 | [ CTF_TYPE_VARIANT ] = ctf_variant_rw, | |
81dee1bb MD |
127 | [ CTF_TYPE_ARRAY ] = ctf_array_write, |
128 | [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write, | |
d11e9c49 MD |
129 | }; |
130 | ||
1ae19169 | 131 | static |
37b99bdb | 132 | struct bt_format ctf_format = { |
bbefb8dd | 133 | .open_trace = ctf_open_trace, |
f571dfb1 | 134 | .open_mmap_trace = ctf_open_mmap_trace, |
bbefb8dd | 135 | .close_trace = ctf_close_trace, |
98a04903 JD |
136 | .set_context = ctf_set_context, |
137 | .set_handle = ctf_set_handle, | |
30c276af JD |
138 | .timestamp_begin = ctf_timestamp_begin, |
139 | .timestamp_end = ctf_timestamp_end, | |
03798a93 | 140 | .convert_index_timestamp = ctf_convert_index_timestamp, |
fc93b2bd MD |
141 | }; |
142 | ||
30c276af | 143 | static |
1b8455b7 | 144 | uint64_t ctf_timestamp_begin(struct bt_trace_descriptor *descriptor, |
03798a93 | 145 | struct bt_trace_handle *handle, enum bt_clock_type type) |
30c276af JD |
146 | { |
147 | struct ctf_trace *tin; | |
148 | uint64_t begin = ULLONG_MAX; | |
149 | int i, j; | |
150 | ||
151 | tin = container_of(descriptor, struct ctf_trace, parent); | |
152 | ||
153 | if (!tin) | |
154 | goto error; | |
155 | ||
156 | /* for each stream_class */ | |
157 | for (i = 0; i < tin->streams->len; i++) { | |
158 | struct ctf_stream_declaration *stream_class; | |
159 | ||
160 | stream_class = g_ptr_array_index(tin->streams, i); | |
1b01ffc2 MD |
161 | if (!stream_class) |
162 | continue; | |
30c276af JD |
163 | /* for each file_stream */ |
164 | for (j = 0; j < stream_class->streams->len; j++) { | |
165 | struct ctf_stream_definition *stream; | |
166 | struct ctf_file_stream *cfs; | |
167 | struct ctf_stream_pos *stream_pos; | |
168 | struct packet_index *index; | |
169 | ||
170 | stream = g_ptr_array_index(stream_class->streams, j); | |
171 | cfs = container_of(stream, struct ctf_file_stream, | |
172 | parent); | |
173 | stream_pos = &cfs->pos; | |
174 | ||
03798a93 JD |
175 | if (!stream_pos->packet_real_index) |
176 | goto error; | |
177 | ||
afe9cd4a JD |
178 | if (stream_pos->packet_real_index->len <= 0) |
179 | continue; | |
180 | ||
03798a93 JD |
181 | if (type == BT_CLOCK_REAL) { |
182 | index = &g_array_index(stream_pos->packet_real_index, | |
183 | struct packet_index, | |
184 | stream_pos->packet_real_index->len - 1); | |
185 | } else if (type == BT_CLOCK_CYCLES) { | |
186 | index = &g_array_index(stream_pos->packet_cycles_index, | |
187 | struct packet_index, | |
188 | stream_pos->packet_real_index->len - 1); | |
189 | ||
190 | } else { | |
191 | goto error; | |
192 | } | |
30c276af JD |
193 | if (index->timestamp_begin < begin) |
194 | begin = index->timestamp_begin; | |
195 | } | |
196 | } | |
197 | ||
198 | return begin; | |
199 | ||
200 | error: | |
201 | return -1ULL; | |
202 | } | |
203 | ||
204 | static | |
1b8455b7 | 205 | uint64_t ctf_timestamp_end(struct bt_trace_descriptor *descriptor, |
03798a93 | 206 | struct bt_trace_handle *handle, enum bt_clock_type type) |
30c276af JD |
207 | { |
208 | struct ctf_trace *tin; | |
209 | uint64_t end = 0; | |
210 | int i, j; | |
211 | ||
212 | tin = container_of(descriptor, struct ctf_trace, parent); | |
213 | ||
214 | if (!tin) | |
215 | goto error; | |
216 | ||
217 | /* for each stream_class */ | |
218 | for (i = 0; i < tin->streams->len; i++) { | |
219 | struct ctf_stream_declaration *stream_class; | |
220 | ||
221 | stream_class = g_ptr_array_index(tin->streams, i); | |
1b01ffc2 MD |
222 | if (!stream_class) |
223 | continue; | |
30c276af JD |
224 | /* for each file_stream */ |
225 | for (j = 0; j < stream_class->streams->len; j++) { | |
226 | struct ctf_stream_definition *stream; | |
227 | struct ctf_file_stream *cfs; | |
228 | struct ctf_stream_pos *stream_pos; | |
229 | struct packet_index *index; | |
230 | ||
231 | stream = g_ptr_array_index(stream_class->streams, j); | |
232 | cfs = container_of(stream, struct ctf_file_stream, | |
233 | parent); | |
234 | stream_pos = &cfs->pos; | |
235 | ||
03798a93 JD |
236 | if (!stream_pos->packet_real_index) |
237 | goto error; | |
238 | ||
afe9cd4a JD |
239 | if (stream_pos->packet_real_index->len <= 0) |
240 | continue; | |
241 | ||
03798a93 JD |
242 | if (type == BT_CLOCK_REAL) { |
243 | index = &g_array_index(stream_pos->packet_real_index, | |
244 | struct packet_index, | |
245 | stream_pos->packet_real_index->len - 1); | |
246 | } else if (type == BT_CLOCK_CYCLES) { | |
247 | index = &g_array_index(stream_pos->packet_cycles_index, | |
248 | struct packet_index, | |
249 | stream_pos->packet_real_index->len - 1); | |
250 | ||
251 | } else { | |
252 | goto error; | |
253 | } | |
30c276af JD |
254 | if (index->timestamp_end > end) |
255 | end = index->timestamp_end; | |
256 | } | |
257 | } | |
258 | ||
259 | return end; | |
260 | ||
261 | error: | |
262 | return -1ULL; | |
263 | } | |
264 | ||
25ccc85b | 265 | /* |
03798a93 | 266 | * Update stream current timestamp |
25ccc85b | 267 | */ |
2b9a764d | 268 | static |
9e88d150 | 269 | void ctf_update_timestamp(struct ctf_stream_definition *stream, |
2b9a764d MD |
270 | struct definition_integer *integer_definition) |
271 | { | |
272 | struct declaration_integer *integer_declaration = | |
273 | integer_definition->declaration; | |
274 | uint64_t oldval, newval, updateval; | |
275 | ||
7f3f572b | 276 | if (unlikely(integer_declaration->len == 64)) { |
03798a93 JD |
277 | stream->prev_cycles_timestamp = stream->cycles_timestamp; |
278 | stream->cycles_timestamp = integer_definition->value._unsigned; | |
279 | stream->prev_real_timestamp = ctf_get_real_timestamp(stream, | |
280 | stream->prev_cycles_timestamp); | |
281 | stream->real_timestamp = ctf_get_real_timestamp(stream, | |
282 | stream->cycles_timestamp); | |
2b9a764d MD |
283 | return; |
284 | } | |
285 | /* keep low bits */ | |
03798a93 | 286 | oldval = stream->cycles_timestamp; |
2b9a764d MD |
287 | oldval &= (1ULL << integer_declaration->len) - 1; |
288 | newval = integer_definition->value._unsigned; | |
289 | /* Test for overflow by comparing low bits */ | |
290 | if (newval < oldval) | |
291 | newval += 1ULL << integer_declaration->len; | |
292 | /* updateval contains old high bits, and new low bits (sum) */ | |
03798a93 | 293 | updateval = stream->cycles_timestamp; |
2b9a764d MD |
294 | updateval &= ~((1ULL << integer_declaration->len) - 1); |
295 | updateval += newval; | |
03798a93 JD |
296 | stream->prev_cycles_timestamp = stream->cycles_timestamp; |
297 | stream->cycles_timestamp = updateval; | |
298 | ||
299 | /* convert to real timestamp */ | |
300 | stream->prev_real_timestamp = ctf_get_real_timestamp(stream, | |
301 | stream->prev_cycles_timestamp); | |
302 | stream->real_timestamp = ctf_get_real_timestamp(stream, | |
303 | stream->cycles_timestamp); | |
2b9a764d MD |
304 | } |
305 | ||
25ccc85b MD |
306 | /* |
307 | * Print timestamp, rescaling clock frequency to nanoseconds and | |
308 | * applying offsets as needed (unix time). | |
309 | */ | |
2e937fb4 | 310 | static |
03798a93 | 311 | void ctf_print_timestamp_real(FILE *fp, |
9e88d150 | 312 | struct ctf_stream_definition *stream, |
7d97fad9 MD |
313 | uint64_t timestamp) |
314 | { | |
315 | uint64_t ts_sec = 0, ts_nsec; | |
7d97fad9 | 316 | |
03798a93 | 317 | ts_nsec = timestamp; |
7d97fad9 | 318 | |
c34ea0fa | 319 | /* Add command-line offset */ |
7d97fad9 MD |
320 | ts_sec += opt_clock_offset; |
321 | ||
322 | ts_sec += ts_nsec / NSEC_PER_SEC; | |
323 | ts_nsec = ts_nsec % NSEC_PER_SEC; | |
324 | ||
325 | if (!opt_clock_seconds) { | |
326 | struct tm tm; | |
327 | time_t time_s = (time_t) ts_sec; | |
328 | ||
329 | if (!opt_clock_gmt) { | |
330 | struct tm *res; | |
331 | ||
332 | res = localtime_r(&time_s, &tm); | |
333 | if (!res) { | |
334 | fprintf(stderr, "[warning] Unable to get localtime.\n"); | |
335 | goto seconds; | |
336 | } | |
337 | } else { | |
338 | struct tm *res; | |
339 | ||
340 | res = gmtime_r(&time_s, &tm); | |
341 | if (!res) { | |
342 | fprintf(stderr, "[warning] Unable to get gmtime.\n"); | |
343 | goto seconds; | |
344 | } | |
345 | } | |
346 | if (opt_clock_date) { | |
347 | char timestr[26]; | |
348 | size_t res; | |
349 | ||
350 | /* Print date and time */ | |
351 | res = strftime(timestr, sizeof(timestr), | |
352 | "%F ", &tm); | |
353 | if (!res) { | |
354 | fprintf(stderr, "[warning] Unable to print ascii time.\n"); | |
355 | goto seconds; | |
356 | } | |
357 | fprintf(fp, "%s", timestr); | |
358 | } | |
359 | /* Print time in HH:MM:SS.ns */ | |
360 | fprintf(fp, "%02d:%02d:%02d.%09" PRIu64, | |
361 | tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec); | |
362 | goto end; | |
363 | } | |
364 | seconds: | |
365 | fprintf(fp, "%3" PRIu64 ".%09" PRIu64, | |
366 | ts_sec, ts_nsec); | |
367 | ||
368 | end: | |
369 | return; | |
370 | } | |
371 | ||
03798a93 JD |
372 | /* |
373 | * Print timestamp, in cycles | |
374 | */ | |
2e937fb4 | 375 | static |
03798a93 JD |
376 | void ctf_print_timestamp_cycles(FILE *fp, |
377 | struct ctf_stream_definition *stream, | |
378 | uint64_t timestamp) | |
379 | { | |
380 | fprintf(fp, "%020" PRIu64, timestamp); | |
381 | } | |
382 | ||
383 | void ctf_print_timestamp(FILE *fp, | |
384 | struct ctf_stream_definition *stream, | |
385 | uint64_t timestamp) | |
386 | { | |
387 | if (opt_clock_cycles) { | |
388 | ctf_print_timestamp_cycles(fp, stream, timestamp); | |
389 | } else { | |
390 | ctf_print_timestamp_real(fp, stream, timestamp); | |
391 | } | |
392 | } | |
393 | ||
87148dc7 MD |
394 | static |
395 | void print_uuid(FILE *fp, unsigned char *uuid) | |
396 | { | |
397 | int i; | |
398 | ||
399 | for (i = 0; i < BABELTRACE_UUID_LEN; i++) | |
400 | fprintf(fp, "%x", (unsigned int) uuid[i]); | |
401 | } | |
402 | ||
c829a65c MD |
403 | void ctf_print_discarded(FILE *fp, struct ctf_stream_definition *stream, |
404 | int end_stream) | |
87148dc7 | 405 | { |
c829a65c MD |
406 | fprintf(fp, "[warning] Tracer discarded %" PRIu64 " events %sbetween [", |
407 | stream->events_discarded, | |
408 | end_stream ? "at end of stream " : ""); | |
87148dc7 MD |
409 | if (opt_clock_cycles) { |
410 | ctf_print_timestamp(fp, stream, | |
411 | stream->prev_cycles_timestamp); | |
412 | fprintf(fp, "] and ["); | |
413 | ctf_print_timestamp(fp, stream, | |
414 | stream->prev_cycles_timestamp_end); | |
415 | } else { | |
416 | ctf_print_timestamp(fp, stream, | |
417 | stream->prev_real_timestamp); | |
418 | fprintf(fp, "] and ["); | |
419 | ctf_print_timestamp(fp, stream, | |
420 | stream->prev_real_timestamp_end); | |
421 | } | |
422 | fprintf(fp, "] in trace UUID "); | |
423 | print_uuid(fp, stream->stream_class->trace->uuid); | |
424 | if (stream->stream_class->trace->path[0]) | |
425 | fprintf(fp, ", at path: \"%s\"", | |
426 | stream->stream_class->trace->path); | |
427 | ||
428 | fprintf(fp, ", within stream id %" PRIu64, stream->stream_id); | |
429 | if (stream->path[0]) | |
430 | fprintf(fp, ", at relative path: \"%s\"", stream->path); | |
431 | fprintf(fp, ". "); | |
432 | fprintf(fp, "You should consider recording a new trace with larger " | |
433 | "buffers or with fewer events enabled.\n"); | |
434 | fflush(fp); | |
435 | } | |
436 | ||
31262354 | 437 | static |
1cf393f6 | 438 | int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream) |
31262354 MD |
439 | { |
440 | struct ctf_stream_pos *pos = | |
441 | container_of(ppos, struct ctf_stream_pos, parent); | |
f380e105 | 442 | struct ctf_stream_declaration *stream_class = stream->stream_class; |
c716f83b | 443 | struct ctf_event_definition *event; |
31262354 | 444 | uint64_t id = 0; |
31262354 MD |
445 | int ret; |
446 | ||
3abe83c7 MD |
447 | /* We need to check for EOF here for empty files. */ |
448 | if (unlikely(pos->offset == EOF)) | |
449 | return EOF; | |
450 | ||
5f643ed7 MD |
451 | ctf_pos_get_event(pos); |
452 | ||
90fcbacc JD |
453 | /* save the current position as a restore point */ |
454 | pos->last_offset = pos->offset; | |
90fcbacc | 455 | |
3abe83c7 MD |
456 | /* |
457 | * This is the EOF check after we've advanced the position in | |
458 | * ctf_pos_get_event. | |
459 | */ | |
7f3f572b | 460 | if (unlikely(pos->offset == EOF)) |
31262354 | 461 | return EOF; |
5f643ed7 | 462 | assert(pos->offset < pos->content_size); |
31262354 MD |
463 | |
464 | /* Read event header */ | |
7f3f572b | 465 | if (likely(stream->stream_event_header)) { |
a35173fe | 466 | struct definition_integer *integer_definition; |
0d69b916 | 467 | struct bt_definition *variant; |
a35173fe | 468 | |
e28d4618 | 469 | ret = generic_rw(ppos, &stream->stream_event_header->p); |
7f3f572b | 470 | if (unlikely(ret)) |
31262354 MD |
471 | goto error; |
472 | /* lookup event id */ | |
bf78e2cf | 473 | integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "id", FALSE); |
a35173fe MD |
474 | if (integer_definition) { |
475 | id = integer_definition->value._unsigned; | |
476 | } else { | |
477 | struct definition_enum *enum_definition; | |
478 | ||
9e3274b0 | 479 | enum_definition = bt_lookup_enum(&stream->stream_event_header->p, "id", FALSE); |
a35173fe MD |
480 | if (enum_definition) { |
481 | id = enum_definition->integer->value._unsigned; | |
482 | } | |
31262354 | 483 | } |
764af3f4 | 484 | |
ebae302b | 485 | variant = bt_lookup_variant(&stream->stream_event_header->p, "v"); |
ccdb988e | 486 | if (variant) { |
bf78e2cf | 487 | integer_definition = bt_lookup_integer(variant, "id", FALSE); |
ccdb988e MD |
488 | if (integer_definition) { |
489 | id = integer_definition->value._unsigned; | |
490 | } | |
491 | } | |
c87a8eb2 | 492 | stream->event_id = id; |
ccdb988e | 493 | |
764af3f4 | 494 | /* lookup timestamp */ |
5e2eb0ae | 495 | stream->has_timestamp = 0; |
bf78e2cf | 496 | integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE); |
a35173fe | 497 | if (integer_definition) { |
2b9a764d | 498 | ctf_update_timestamp(stream, integer_definition); |
5e2eb0ae | 499 | stream->has_timestamp = 1; |
a35173fe | 500 | } else { |
ccdb988e | 501 | if (variant) { |
bf78e2cf | 502 | integer_definition = bt_lookup_integer(variant, "timestamp", FALSE); |
a35173fe | 503 | if (integer_definition) { |
2b9a764d | 504 | ctf_update_timestamp(stream, integer_definition); |
5e2eb0ae | 505 | stream->has_timestamp = 1; |
a35173fe MD |
506 | } |
507 | } | |
764af3f4 | 508 | } |
31262354 MD |
509 | } |
510 | ||
511 | /* Read stream-declared event context */ | |
e28d4618 MD |
512 | if (stream->stream_event_context) { |
513 | ret = generic_rw(ppos, &stream->stream_event_context->p); | |
31262354 MD |
514 | if (ret) |
515 | goto error; | |
516 | } | |
517 | ||
7f3f572b | 518 | if (unlikely(id >= stream_class->events_by_id->len)) { |
3394d22e | 519 | fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id); |
31262354 MD |
520 | return -EINVAL; |
521 | } | |
e28d4618 | 522 | event = g_ptr_array_index(stream->events_by_id, id); |
7f3f572b | 523 | if (unlikely(!event)) { |
3394d22e | 524 | fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id); |
31262354 MD |
525 | return -EINVAL; |
526 | } | |
527 | ||
528 | /* Read event-declared event context */ | |
e28d4618 MD |
529 | if (event->event_context) { |
530 | ret = generic_rw(ppos, &event->event_context->p); | |
31262354 MD |
531 | if (ret) |
532 | goto error; | |
533 | } | |
534 | ||
535 | /* Read event payload */ | |
7f3f572b | 536 | if (likely(event->event_fields)) { |
e28d4618 | 537 | ret = generic_rw(ppos, &event->event_fields->p); |
31262354 MD |
538 | if (ret) |
539 | goto error; | |
540 | } | |
541 | ||
542 | return 0; | |
543 | ||
544 | error: | |
3394d22e | 545 | 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 |
546 | return ret; |
547 | } | |
548 | ||
549 | static | |
1cf393f6 | 550 | int ctf_write_event(struct bt_stream_pos *pos, struct ctf_stream_definition *stream) |
31262354 | 551 | { |
f380e105 | 552 | struct ctf_stream_declaration *stream_class = stream->stream_class; |
c716f83b | 553 | struct ctf_event_definition *event; |
c87a8eb2 | 554 | uint64_t id; |
31262354 MD |
555 | int ret; |
556 | ||
c87a8eb2 MD |
557 | id = stream->event_id; |
558 | ||
31262354 | 559 | /* print event header */ |
7f3f572b | 560 | if (likely(stream->stream_event_header)) { |
e28d4618 | 561 | ret = generic_rw(pos, &stream->stream_event_header->p); |
31262354 MD |
562 | if (ret) |
563 | goto error; | |
564 | } | |
565 | ||
566 | /* print stream-declared event context */ | |
e28d4618 MD |
567 | if (stream->stream_event_context) { |
568 | ret = generic_rw(pos, &stream->stream_event_context->p); | |
31262354 MD |
569 | if (ret) |
570 | goto error; | |
571 | } | |
572 | ||
7f3f572b | 573 | if (unlikely(id >= stream_class->events_by_id->len)) { |
3394d22e | 574 | fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id); |
31262354 MD |
575 | return -EINVAL; |
576 | } | |
e28d4618 | 577 | event = g_ptr_array_index(stream->events_by_id, id); |
7f3f572b | 578 | if (unlikely(!event)) { |
3394d22e | 579 | fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id); |
31262354 MD |
580 | return -EINVAL; |
581 | } | |
582 | ||
583 | /* print event-declared event context */ | |
e28d4618 MD |
584 | if (event->event_context) { |
585 | ret = generic_rw(pos, &event->event_context->p); | |
31262354 MD |
586 | if (ret) |
587 | goto error; | |
588 | } | |
589 | ||
590 | /* Read and print event payload */ | |
7f3f572b | 591 | if (likely(event->event_fields)) { |
e28d4618 | 592 | ret = generic_rw(pos, &event->event_fields->p); |
31262354 MD |
593 | if (ret) |
594 | goto error; | |
595 | } | |
596 | ||
597 | return 0; | |
598 | ||
599 | error: | |
3394d22e | 600 | 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 |
601 | return ret; |
602 | } | |
603 | ||
f824ae04 | 604 | int ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags) |
8c572eba MD |
605 | { |
606 | pos->fd = fd; | |
37fcdd19 | 607 | if (fd >= 0) { |
03798a93 | 608 | pos->packet_cycles_index = g_array_new(FALSE, TRUE, |
8563e754 | 609 | sizeof(struct packet_index)); |
37fcdd19 JD |
610 | pos->packet_real_index = g_array_new(FALSE, TRUE, |
611 | sizeof(struct packet_index)); | |
612 | } else { | |
03798a93 | 613 | pos->packet_cycles_index = NULL; |
37fcdd19 JD |
614 | pos->packet_real_index = NULL; |
615 | } | |
8563e754 MD |
616 | switch (open_flags & O_ACCMODE) { |
617 | case O_RDONLY: | |
618 | pos->prot = PROT_READ; | |
619 | pos->flags = MAP_PRIVATE; | |
620 | pos->parent.rw_table = read_dispatch_table; | |
31262354 | 621 | pos->parent.event_cb = ctf_read_event; |
8563e754 | 622 | break; |
8563e754 MD |
623 | case O_RDWR: |
624 | pos->prot = PROT_WRITE; /* Write has priority */ | |
625 | pos->flags = MAP_SHARED; | |
626 | pos->parent.rw_table = write_dispatch_table; | |
31262354 | 627 | pos->parent.event_cb = ctf_write_event; |
8563e754 | 628 | if (fd >= 0) |
06789ffd | 629 | ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */ |
8563e754 MD |
630 | break; |
631 | default: | |
632 | assert(0); | |
8c572eba | 633 | } |
f824ae04 | 634 | return 0; |
8c572eba MD |
635 | } |
636 | ||
f824ae04 | 637 | int ctf_fini_pos(struct ctf_stream_pos *pos) |
8c572eba | 638 | { |
8c572eba MD |
639 | if (pos->prot == PROT_WRITE && pos->content_size_loc) |
640 | *pos->content_size_loc = pos->offset; | |
aee35fcc | 641 | if (pos->base_mma) { |
08c82b90 MD |
642 | int ret; |
643 | ||
8c572eba | 644 | /* unmap old base */ |
aee35fcc | 645 | ret = munmap_align(pos->base_mma); |
8c572eba | 646 | if (ret) { |
3394d22e | 647 | fprintf(stderr, "[error] Unable to unmap old base: %s.\n", |
8c572eba | 648 | strerror(errno)); |
f824ae04 | 649 | return -1; |
8c572eba MD |
650 | } |
651 | } | |
37fcdd19 JD |
652 | if (pos->packet_cycles_index) |
653 | (void) g_array_free(pos->packet_cycles_index, TRUE); | |
654 | if (pos->packet_real_index) | |
655 | (void) g_array_free(pos->packet_real_index, TRUE); | |
f824ae04 | 656 | return 0; |
8c572eba MD |
657 | } |
658 | ||
20d0dcf9 MD |
659 | /* |
660 | * for SEEK_CUR: go to next packet. | |
661 | * for SEEK_POS: go to packet numer (index). | |
662 | */ | |
1cf393f6 | 663 | void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence) |
0f980a35 | 664 | { |
d6425aaf MD |
665 | struct ctf_stream_pos *pos = |
666 | container_of(stream_pos, struct ctf_stream_pos, parent); | |
75cc2c35 MD |
667 | struct ctf_file_stream *file_stream = |
668 | container_of(pos, struct ctf_file_stream, pos); | |
0f980a35 | 669 | int ret; |
8c572eba | 670 | off_t off; |
20d0dcf9 | 671 | struct packet_index *packet_index; |
0f980a35 | 672 | |
8c572eba MD |
673 | if (pos->prot == PROT_WRITE && pos->content_size_loc) |
674 | *pos->content_size_loc = pos->offset; | |
0f980a35 | 675 | |
aee35fcc | 676 | if (pos->base_mma) { |
0f980a35 | 677 | /* unmap old base */ |
aee35fcc | 678 | ret = munmap_align(pos->base_mma); |
0f980a35 | 679 | if (ret) { |
3394d22e | 680 | fprintf(stderr, "[error] Unable to unmap old base: %s.\n", |
0f980a35 MD |
681 | strerror(errno)); |
682 | assert(0); | |
683 | } | |
aee35fcc | 684 | pos->base_mma = NULL; |
0f980a35 MD |
685 | } |
686 | ||
8c572eba | 687 | /* |
46322b33 | 688 | * The caller should never ask for ctf_move_pos across packets, |
8c572eba MD |
689 | * except to get exactly at the beginning of the next packet. |
690 | */ | |
691 | if (pos->prot == PROT_WRITE) { | |
989c73bc MD |
692 | switch (whence) { |
693 | case SEEK_CUR: | |
694 | /* The writer will add padding */ | |
8c572eba | 695 | pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT; |
989c73bc MD |
696 | break; |
697 | case SEEK_SET: | |
20d0dcf9 | 698 | assert(index == 0); /* only seek supported for now */ |
989c73bc MD |
699 | pos->cur_index = 0; |
700 | break; | |
701 | default: | |
702 | assert(0); | |
703 | } | |
8c572eba MD |
704 | pos->content_size = -1U; /* Unknown at this point */ |
705 | pos->packet_size = WRITE_PACKET_LEN; | |
989c73bc MD |
706 | off = posix_fallocate(pos->fd, pos->mmap_offset, |
707 | pos->packet_size / CHAR_BIT); | |
8c572eba | 708 | assert(off >= 0); |
847bf71a | 709 | pos->offset = 0; |
8c572eba | 710 | } else { |
5f643ed7 | 711 | read_next_packet: |
847bf71a MD |
712 | switch (whence) { |
713 | case SEEK_CUR: | |
41e82e00 | 714 | { |
4c4ba021 | 715 | uint64_t events_discarded_diff; |
41e82e00 | 716 | |
7d97fad9 MD |
717 | if (pos->offset == EOF) { |
718 | return; | |
719 | } | |
3217ac37 | 720 | /* For printing discarded event count */ |
03798a93 | 721 | packet_index = &g_array_index(pos->packet_cycles_index, |
41e82e00 | 722 | struct packet_index, pos->cur_index); |
03798a93 JD |
723 | file_stream->parent.prev_cycles_timestamp_end = |
724 | packet_index->timestamp_end; | |
725 | file_stream->parent.prev_cycles_timestamp = | |
726 | packet_index->timestamp_begin; | |
727 | ||
728 | packet_index = &g_array_index(pos->packet_real_index, | |
729 | struct packet_index, pos->cur_index); | |
730 | file_stream->parent.prev_real_timestamp_end = | |
20d0dcf9 | 731 | packet_index->timestamp_end; |
03798a93 JD |
732 | file_stream->parent.prev_real_timestamp = |
733 | packet_index->timestamp_begin; | |
734 | ||
735 | events_discarded_diff = packet_index->events_discarded; | |
41e82e00 | 736 | if (pos->cur_index > 0) { |
03798a93 | 737 | packet_index = &g_array_index(pos->packet_real_index, |
41e82e00 MD |
738 | struct packet_index, |
739 | pos->cur_index - 1); | |
20d0dcf9 | 740 | events_discarded_diff -= packet_index->events_discarded; |
4c4ba021 MD |
741 | /* |
742 | * Deal with 32-bit wrap-around if the | |
743 | * tracer provided a 32-bit field. | |
744 | */ | |
745 | if (packet_index->events_discarded_len == 32) { | |
746 | events_discarded_diff = (uint32_t) events_discarded_diff; | |
747 | } | |
41e82e00 | 748 | } |
fca04958 | 749 | file_stream->parent.events_discarded = events_discarded_diff; |
03798a93 JD |
750 | file_stream->parent.prev_real_timestamp = file_stream->parent.real_timestamp; |
751 | file_stream->parent.prev_cycles_timestamp = file_stream->parent.cycles_timestamp; | |
847bf71a | 752 | /* The reader will expect us to skip padding */ |
8c572eba | 753 | ++pos->cur_index; |
847bf71a | 754 | break; |
41e82e00 | 755 | } |
847bf71a | 756 | case SEEK_SET: |
f60efc0e JD |
757 | packet_index = &g_array_index(pos->packet_cycles_index, |
758 | struct packet_index, index); | |
759 | pos->last_events_discarded = packet_index->events_discarded; | |
20d0dcf9 | 760 | pos->cur_index = index; |
03798a93 JD |
761 | file_stream->parent.prev_real_timestamp = 0; |
762 | file_stream->parent.prev_real_timestamp_end = 0; | |
763 | file_stream->parent.prev_cycles_timestamp = 0; | |
764 | file_stream->parent.prev_cycles_timestamp_end = 0; | |
847bf71a MD |
765 | break; |
766 | default: | |
767 | assert(0); | |
768 | } | |
03798a93 | 769 | if (pos->cur_index >= pos->packet_real_index->len) { |
7d97fad9 | 770 | /* |
87148dc7 MD |
771 | * We need to check if we are in trace read or |
772 | * called from packet indexing. In this last | |
773 | * case, the collection is not there, so we | |
774 | * cannot print the timestamps. | |
7d97fad9 | 775 | */ |
87148dc7 | 776 | if ((&file_stream->parent)->stream_class->trace->collection) { |
badea1a2 | 777 | /* |
87148dc7 MD |
778 | * When a stream reaches the end of the |
779 | * file, we need to show the number of | |
780 | * events discarded ourselves, because | |
781 | * there is no next event scheduled to | |
782 | * be printed in the output. | |
badea1a2 | 783 | */ |
87148dc7 | 784 | if (file_stream->parent.events_discarded) { |
badea1a2 | 785 | fflush(stdout); |
c829a65c MD |
786 | ctf_print_discarded(stderr, |
787 | &file_stream->parent, | |
788 | 1); | |
87148dc7 | 789 | file_stream->parent.events_discarded = 0; |
badea1a2 | 790 | } |
7d97fad9 | 791 | } |
670977d3 | 792 | pos->offset = EOF; |
847bf71a MD |
793 | return; |
794 | } | |
03798a93 JD |
795 | packet_index = &g_array_index(pos->packet_cycles_index, |
796 | struct packet_index, | |
797 | pos->cur_index); | |
798 | file_stream->parent.cycles_timestamp = packet_index->timestamp_begin; | |
799 | ||
800 | packet_index = &g_array_index(pos->packet_real_index, | |
801 | struct packet_index, | |
802 | pos->cur_index); | |
803 | file_stream->parent.real_timestamp = packet_index->timestamp_begin; | |
20d0dcf9 | 804 | pos->mmap_offset = packet_index->offset; |
8c572eba MD |
805 | |
806 | /* Lookup context/packet size in index */ | |
20d0dcf9 MD |
807 | pos->content_size = packet_index->content_size; |
808 | pos->packet_size = packet_index->packet_size; | |
809 | if (packet_index->data_offset < packet_index->content_size) { | |
75cc2c35 | 810 | pos->offset = 0; /* will read headers */ |
20d0dcf9 | 811 | } else if (packet_index->data_offset == packet_index->content_size) { |
5f643ed7 | 812 | /* empty packet */ |
20d0dcf9 | 813 | pos->offset = packet_index->data_offset; |
3abe83c7 | 814 | whence = SEEK_CUR; |
5f643ed7 | 815 | goto read_next_packet; |
7eda6fc7 | 816 | } else { |
2b9a764d MD |
817 | pos->offset = EOF; |
818 | return; | |
819 | } | |
8c572eba | 820 | } |
0f980a35 | 821 | /* map new base. Need mapping length from header. */ |
aee35fcc MD |
822 | pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot, |
823 | pos->flags, pos->fd, pos->mmap_offset); | |
824 | if (pos->base_mma == MAP_FAILED) { | |
3394d22e | 825 | fprintf(stderr, "[error] mmap error %s.\n", |
847bf71a MD |
826 | strerror(errno)); |
827 | assert(0); | |
828 | } | |
75cc2c35 MD |
829 | |
830 | /* update trace_packet_header and stream_packet_context */ | |
2d0bea29 | 831 | if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) { |
75cc2c35 | 832 | /* Read packet header */ |
2d0bea29 | 833 | ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p); |
75cc2c35 MD |
834 | assert(!ret); |
835 | } | |
2d0bea29 | 836 | if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) { |
75cc2c35 | 837 | /* Read packet context */ |
2d0bea29 | 838 | ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p); |
75cc2c35 MD |
839 | assert(!ret); |
840 | } | |
0f980a35 MD |
841 | } |
842 | ||
b4c19c1e MD |
843 | static |
844 | int packet_metadata(struct ctf_trace *td, FILE *fp) | |
845 | { | |
846 | uint32_t magic; | |
847 | size_t len; | |
848 | int ret = 0; | |
849 | ||
850 | len = fread(&magic, sizeof(magic), 1, fp); | |
a0fe7d97 | 851 | if (len != 1) { |
b4c19c1e MD |
852 | goto end; |
853 | } | |
854 | if (magic == TSDL_MAGIC) { | |
855 | ret = 1; | |
856 | td->byte_order = BYTE_ORDER; | |
0d336fdf | 857 | CTF_TRACE_SET_FIELD(td, byte_order); |
b4c19c1e MD |
858 | } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) { |
859 | ret = 1; | |
860 | td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ? | |
861 | LITTLE_ENDIAN : BIG_ENDIAN; | |
0d336fdf | 862 | CTF_TRACE_SET_FIELD(td, byte_order); |
b4c19c1e MD |
863 | } |
864 | end: | |
865 | rewind(fp); | |
866 | return ret; | |
867 | } | |
868 | ||
5c262147 MD |
869 | /* |
870 | * Returns 0 on success, -1 on error. | |
871 | */ | |
872 | static | |
873 | int check_version(unsigned int major, unsigned int minor) | |
874 | { | |
875 | switch (major) { | |
876 | case 1: | |
877 | switch (minor) { | |
878 | case 8: | |
879 | return 0; | |
880 | default: | |
881 | goto warning; | |
882 | } | |
883 | default: | |
884 | goto warning; | |
885 | ||
886 | } | |
887 | ||
888 | /* eventually return an error instead of warning */ | |
889 | warning: | |
3394d22e | 890 | fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n", |
5c262147 MD |
891 | major, minor); |
892 | return 0; | |
893 | } | |
894 | ||
b4c19c1e MD |
895 | static |
896 | int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in, | |
897 | FILE *out) | |
898 | { | |
899 | struct metadata_packet_header header; | |
a0fe7d97 | 900 | size_t readlen, writelen, toread; |
f8254867 | 901 | char buf[4096 + 1]; /* + 1 for debug-mode \0 */ |
b4c19c1e MD |
902 | int ret = 0; |
903 | ||
a91a962e | 904 | readlen = fread(&header, header_sizeof(header), 1, in); |
a0fe7d97 | 905 | if (readlen < 1) |
b4c19c1e MD |
906 | return -EINVAL; |
907 | ||
908 | if (td->byte_order != BYTE_ORDER) { | |
909 | header.magic = GUINT32_SWAP_LE_BE(header.magic); | |
910 | header.checksum = GUINT32_SWAP_LE_BE(header.checksum); | |
911 | header.content_size = GUINT32_SWAP_LE_BE(header.content_size); | |
912 | header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size); | |
913 | } | |
914 | if (header.checksum) | |
3394d22e | 915 | fprintf(stderr, "[warning] checksum verification not supported yet.\n"); |
b4c19c1e | 916 | if (header.compression_scheme) { |
3394d22e | 917 | fprintf(stderr, "[error] compression (%u) not supported yet.\n", |
b4c19c1e MD |
918 | header.compression_scheme); |
919 | return -EINVAL; | |
920 | } | |
921 | if (header.encryption_scheme) { | |
3394d22e | 922 | fprintf(stderr, "[error] encryption (%u) not supported yet.\n", |
b4c19c1e MD |
923 | header.encryption_scheme); |
924 | return -EINVAL; | |
925 | } | |
926 | if (header.checksum_scheme) { | |
3394d22e | 927 | fprintf(stderr, "[error] checksum (%u) not supported yet.\n", |
b4c19c1e MD |
928 | header.checksum_scheme); |
929 | return -EINVAL; | |
930 | } | |
5c262147 MD |
931 | if (check_version(header.major, header.minor) < 0) |
932 | return -EINVAL; | |
b4c19c1e MD |
933 | if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) { |
934 | memcpy(td->uuid, header.uuid, sizeof(header.uuid)); | |
935 | CTF_TRACE_SET_FIELD(td, uuid); | |
936 | } else { | |
43e34335 | 937 | if (babeltrace_uuid_compare(header.uuid, td->uuid)) |
b4c19c1e MD |
938 | return -EINVAL; |
939 | } | |
940 | ||
b1ccd079 MD |
941 | if ((header.content_size / CHAR_BIT) < header_sizeof(header)) |
942 | return -EINVAL; | |
943 | ||
255b2138 | 944 | toread = (header.content_size / CHAR_BIT) - header_sizeof(header); |
a0fe7d97 MD |
945 | |
946 | for (;;) { | |
f8254867 | 947 | readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in); |
b4c19c1e MD |
948 | if (ferror(in)) { |
949 | ret = -EINVAL; | |
950 | break; | |
951 | } | |
4152822b | 952 | if (babeltrace_debug) { |
f8254867 | 953 | buf[readlen] = '\0'; |
3394d22e | 954 | fprintf(stderr, "[debug] metadata packet read: %s\n", |
4152822b MD |
955 | buf); |
956 | } | |
957 | ||
b4c19c1e MD |
958 | writelen = fwrite(buf, sizeof(char), readlen, out); |
959 | if (writelen < readlen) { | |
960 | ret = -EIO; | |
961 | break; | |
962 | } | |
963 | if (ferror(out)) { | |
964 | ret = -EINVAL; | |
965 | break; | |
966 | } | |
a0fe7d97 MD |
967 | toread -= readlen; |
968 | if (!toread) { | |
7f4b5c4d | 969 | ret = 0; /* continue reading next packet */ |
ddbc52af | 970 | goto read_padding; |
a0fe7d97 | 971 | } |
b4c19c1e MD |
972 | } |
973 | return ret; | |
ddbc52af MD |
974 | |
975 | read_padding: | |
976 | toread = (header.packet_size - header.content_size) / CHAR_BIT; | |
977 | ret = fseek(in, toread, SEEK_CUR); | |
978 | if (ret < 0) { | |
3394d22e | 979 | fprintf(stderr, "[warning] Missing padding at end of file\n"); |
ddbc52af MD |
980 | ret = 0; |
981 | } | |
982 | return ret; | |
b4c19c1e MD |
983 | } |
984 | ||
985 | static | |
986 | int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp, | |
987 | char **buf) | |
988 | { | |
989 | FILE *in, *out; | |
abc40d24 | 990 | size_t size, buflen; |
b4c19c1e MD |
991 | int ret; |
992 | ||
993 | in = *fp; | |
c4f5487e MD |
994 | /* |
995 | * Using strlen on *buf instead of size of open_memstream | |
996 | * because its size includes garbage at the end (after final | |
997 | * \0). This is the allocated size, not the actual string size. | |
998 | */ | |
f8370579 | 999 | out = babeltrace_open_memstream(buf, &size); |
a569a564 MD |
1000 | if (out == NULL) { |
1001 | perror("Metadata open_memstream"); | |
b4c19c1e | 1002 | return -errno; |
a569a564 | 1003 | } |
b4c19c1e MD |
1004 | for (;;) { |
1005 | ret = ctf_open_trace_metadata_packet_read(td, in, out); | |
7f4b5c4d | 1006 | if (ret) { |
b4c19c1e | 1007 | break; |
7f4b5c4d MD |
1008 | } |
1009 | if (feof(in)) { | |
1010 | ret = 0; | |
b4c19c1e MD |
1011 | break; |
1012 | } | |
1013 | } | |
f8370579 MD |
1014 | /* close to flush the buffer */ |
1015 | ret = babeltrace_close_memstream(buf, &size, out); | |
1016 | if (ret < 0) { | |
f824ae04 MD |
1017 | int closeret; |
1018 | ||
f8370579 | 1019 | perror("babeltrace_flush_memstream"); |
f824ae04 MD |
1020 | ret = -errno; |
1021 | closeret = fclose(in); | |
1022 | if (closeret) { | |
1023 | perror("Error in fclose"); | |
1024 | } | |
1025 | return ret; | |
1026 | } | |
1027 | ret = fclose(in); | |
1028 | if (ret) { | |
1029 | perror("Error in fclose"); | |
f8370579 | 1030 | } |
b4c19c1e | 1031 | /* open for reading */ |
abc40d24 MD |
1032 | buflen = strlen(*buf); |
1033 | if (!buflen) { | |
1034 | *fp = NULL; | |
1035 | return -ENODATA; | |
1036 | } | |
1037 | *fp = babeltrace_fmemopen(*buf, buflen, "rb"); | |
a569a564 MD |
1038 | if (!*fp) { |
1039 | perror("Metadata fmemopen"); | |
1040 | return -errno; | |
1041 | } | |
b4c19c1e MD |
1042 | return 0; |
1043 | } | |
1044 | ||
65102a8c | 1045 | static |
b086c01a | 1046 | int ctf_open_trace_metadata_read(struct ctf_trace *td, |
1cf393f6 | 1047 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
ae23d232 | 1048 | int whence), FILE *metadata_fp) |
65102a8c MD |
1049 | { |
1050 | struct ctf_scanner *scanner; | |
2d0bea29 | 1051 | struct ctf_file_stream *metadata_stream; |
65102a8c | 1052 | FILE *fp; |
b4c19c1e | 1053 | char *buf = NULL; |
f824ae04 | 1054 | int ret = 0, closeret; |
65102a8c | 1055 | |
2d0bea29 | 1056 | metadata_stream = g_new0(struct ctf_file_stream, 1); |
3a25e036 | 1057 | metadata_stream->pos.last_offset = LAST_OFFSET_POISON; |
b086c01a | 1058 | |
06789ffd MD |
1059 | if (packet_seek) { |
1060 | metadata_stream->pos.packet_seek = packet_seek; | |
b086c01a | 1061 | } else { |
06789ffd | 1062 | fprintf(stderr, "[error] packet_seek function undefined.\n"); |
b086c01a | 1063 | ret = -1; |
f824ae04 | 1064 | goto end_free; |
b086c01a JD |
1065 | } |
1066 | ||
ae23d232 JD |
1067 | if (metadata_fp) { |
1068 | fp = metadata_fp; | |
bcbfb8bf | 1069 | metadata_stream->pos.fd = -1; |
ae23d232 JD |
1070 | } else { |
1071 | td->metadata = &metadata_stream->parent; | |
1072 | metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY); | |
1073 | if (metadata_stream->pos.fd < 0) { | |
3394d22e | 1074 | fprintf(stderr, "Unable to open metadata.\n"); |
f824ae04 MD |
1075 | ret = -1; |
1076 | goto end_free; | |
ae23d232 | 1077 | } |
65102a8c | 1078 | |
ae23d232 JD |
1079 | fp = fdopen(metadata_stream->pos.fd, "r"); |
1080 | if (!fp) { | |
3394d22e | 1081 | fprintf(stderr, "[error] Unable to open metadata stream.\n"); |
a569a564 | 1082 | perror("Metadata stream open"); |
ae23d232 JD |
1083 | ret = -errno; |
1084 | goto end_stream; | |
1085 | } | |
f824ae04 MD |
1086 | /* fd now belongs to fp */ |
1087 | metadata_stream->pos.fd = -1; | |
ae23d232 | 1088 | } |
65102a8c MD |
1089 | if (babeltrace_debug) |
1090 | yydebug = 1; | |
1091 | ||
b4c19c1e MD |
1092 | if (packet_metadata(td, fp)) { |
1093 | ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf); | |
abc40d24 MD |
1094 | if (ret) { |
1095 | /* Warn about empty metadata */ | |
1096 | fprintf(stderr, "[warning] Empty metadata.\n"); | |
b4c19c1e | 1097 | goto end_packet_read; |
abc40d24 | 1098 | } |
da75b0f7 | 1099 | } else { |
5c262147 MD |
1100 | unsigned int major, minor; |
1101 | ssize_t nr_items; | |
8c2df3f5 | 1102 | |
da75b0f7 | 1103 | td->byte_order = BYTE_ORDER; |
8c2df3f5 | 1104 | |
5c262147 MD |
1105 | /* Check text-only metadata header and version */ |
1106 | nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor); | |
1107 | if (nr_items < 2) | |
3394d22e | 1108 | fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n"); |
5c262147 MD |
1109 | if (check_version(major, minor) < 0) { |
1110 | ret = -EINVAL; | |
1111 | goto end_packet_read; | |
1112 | } | |
8c2df3f5 | 1113 | rewind(fp); |
b4c19c1e MD |
1114 | } |
1115 | ||
65102a8c MD |
1116 | scanner = ctf_scanner_alloc(fp); |
1117 | if (!scanner) { | |
3394d22e | 1118 | fprintf(stderr, "[error] Error allocating scanner\n"); |
65102a8c MD |
1119 | ret = -ENOMEM; |
1120 | goto end_scanner_alloc; | |
1121 | } | |
1122 | ret = ctf_scanner_append_ast(scanner); | |
1123 | if (ret) { | |
3394d22e | 1124 | fprintf(stderr, "[error] Error creating AST\n"); |
65102a8c MD |
1125 | goto end; |
1126 | } | |
1127 | ||
1128 | if (babeltrace_debug) { | |
3394d22e | 1129 | ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root); |
65102a8c | 1130 | if (ret) { |
3394d22e | 1131 | fprintf(stderr, "[error] Error visiting AST for XML output\n"); |
65102a8c MD |
1132 | goto end; |
1133 | } | |
1134 | } | |
1135 | ||
3394d22e | 1136 | ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root); |
65102a8c | 1137 | if (ret) { |
3394d22e | 1138 | fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret); |
65102a8c MD |
1139 | goto end; |
1140 | } | |
3394d22e | 1141 | ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root, |
ac5c6ca0 | 1142 | td, td->byte_order); |
65102a8c | 1143 | if (ret) { |
3394d22e | 1144 | fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret); |
65102a8c MD |
1145 | goto end; |
1146 | } | |
1147 | end: | |
1148 | ctf_scanner_free(scanner); | |
1149 | end_scanner_alloc: | |
b4c19c1e | 1150 | end_packet_read: |
f824ae04 MD |
1151 | if (fp) { |
1152 | closeret = fclose(fp); | |
1153 | if (closeret) { | |
1154 | perror("Error on fclose"); | |
1155 | } | |
1156 | } | |
b4c19c1e | 1157 | free(buf); |
65102a8c | 1158 | end_stream: |
f824ae04 MD |
1159 | if (metadata_stream->pos.fd >= 0) { |
1160 | closeret = close(metadata_stream->pos.fd); | |
1161 | if (closeret) { | |
1162 | perror("Error on metadata stream fd close"); | |
1163 | } | |
1164 | } | |
1165 | end_free: | |
2d0bea29 MD |
1166 | if (ret) |
1167 | g_free(metadata_stream); | |
0f980a35 MD |
1168 | return ret; |
1169 | } | |
1170 | ||
e28d4618 | 1171 | static |
c716f83b | 1172 | struct ctf_event_definition *create_event_definitions(struct ctf_trace *td, |
9e88d150 | 1173 | struct ctf_stream_definition *stream, |
4716614a | 1174 | struct ctf_event_declaration *event) |
e28d4618 | 1175 | { |
c716f83b | 1176 | struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1); |
e28d4618 MD |
1177 | |
1178 | if (event->context_decl) { | |
0d69b916 | 1179 | struct bt_definition *definition = |
e28d4618 MD |
1180 | event->context_decl->p.definition_new(&event->context_decl->p, |
1181 | stream->parent_def_scope, 0, 0, "event.context"); | |
1182 | if (!definition) { | |
1183 | goto error; | |
1184 | } | |
42dc00b7 | 1185 | stream_event->event_context = container_of(definition, |
e28d4618 | 1186 | struct definition_struct, p); |
42dc00b7 | 1187 | stream->parent_def_scope = stream_event->event_context->p.scope; |
e28d4618 MD |
1188 | } |
1189 | if (event->fields_decl) { | |
0d69b916 | 1190 | struct bt_definition *definition = |
e28d4618 MD |
1191 | event->fields_decl->p.definition_new(&event->fields_decl->p, |
1192 | stream->parent_def_scope, 0, 0, "event.fields"); | |
1193 | if (!definition) { | |
1194 | goto error; | |
1195 | } | |
42dc00b7 | 1196 | stream_event->event_fields = container_of(definition, |
e28d4618 | 1197 | struct definition_struct, p); |
42dc00b7 | 1198 | stream->parent_def_scope = stream_event->event_fields->p.scope; |
e28d4618 | 1199 | } |
d3ded99d | 1200 | stream_event->stream = stream; |
42dc00b7 | 1201 | return stream_event; |
e28d4618 MD |
1202 | |
1203 | error: | |
42dc00b7 | 1204 | if (stream_event->event_fields) |
13fad8b6 | 1205 | bt_definition_unref(&stream_event->event_fields->p); |
42dc00b7 | 1206 | if (stream_event->event_context) |
13fad8b6 | 1207 | bt_definition_unref(&stream_event->event_context->p); |
e28d4618 MD |
1208 | return NULL; |
1209 | } | |
1210 | ||
1211 | static | |
9e88d150 | 1212 | int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream) |
e28d4618 | 1213 | { |
f380e105 | 1214 | struct ctf_stream_declaration *stream_class; |
e28d4618 MD |
1215 | int ret; |
1216 | int i; | |
1217 | ||
1218 | if (stream->stream_definitions_created) | |
1219 | return 0; | |
1220 | ||
1221 | stream_class = stream->stream_class; | |
1222 | ||
1223 | if (stream_class->packet_context_decl) { | |
0d69b916 | 1224 | struct bt_definition *definition = |
e28d4618 MD |
1225 | stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p, |
1226 | stream->parent_def_scope, 0, 0, "stream.packet.context"); | |
1227 | if (!definition) { | |
1228 | ret = -EINVAL; | |
1229 | goto error; | |
1230 | } | |
1231 | stream->stream_packet_context = container_of(definition, | |
1232 | struct definition_struct, p); | |
1233 | stream->parent_def_scope = stream->stream_packet_context->p.scope; | |
1234 | } | |
1235 | if (stream_class->event_header_decl) { | |
0d69b916 | 1236 | struct bt_definition *definition = |
e28d4618 MD |
1237 | stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p, |
1238 | stream->parent_def_scope, 0, 0, "stream.event.header"); | |
1239 | if (!definition) { | |
1240 | ret = -EINVAL; | |
1241 | goto error; | |
1242 | } | |
1243 | stream->stream_event_header = | |
1244 | container_of(definition, struct definition_struct, p); | |
1245 | stream->parent_def_scope = stream->stream_event_header->p.scope; | |
1246 | } | |
1247 | if (stream_class->event_context_decl) { | |
0d69b916 | 1248 | struct bt_definition *definition = |
e28d4618 MD |
1249 | stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p, |
1250 | stream->parent_def_scope, 0, 0, "stream.event.context"); | |
1251 | if (!definition) { | |
1252 | ret = -EINVAL; | |
1253 | goto error; | |
1254 | } | |
1255 | stream->stream_event_context = | |
1256 | container_of(definition, struct definition_struct, p); | |
1257 | stream->parent_def_scope = stream->stream_event_context->p.scope; | |
1258 | } | |
1259 | stream->events_by_id = g_ptr_array_new(); | |
1260 | g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len); | |
1261 | for (i = 0; i < stream->events_by_id->len; i++) { | |
4716614a | 1262 | struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i); |
c716f83b | 1263 | struct ctf_event_definition *stream_event; |
e28d4618 MD |
1264 | |
1265 | if (!event) | |
1266 | continue; | |
42dc00b7 MD |
1267 | stream_event = create_event_definitions(td, stream, event); |
1268 | if (!stream_event) | |
e28d4618 | 1269 | goto error_event; |
42dc00b7 | 1270 | g_ptr_array_index(stream->events_by_id, i) = stream_event; |
e28d4618 MD |
1271 | } |
1272 | return 0; | |
1273 | ||
1274 | error_event: | |
1275 | for (i = 0; i < stream->events_by_id->len; i++) { | |
c716f83b | 1276 | struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i); |
42dc00b7 MD |
1277 | if (stream_event) |
1278 | g_free(stream_event); | |
e28d4618 MD |
1279 | } |
1280 | g_ptr_array_free(stream->events_by_id, TRUE); | |
1281 | error: | |
1282 | if (stream->stream_event_context) | |
13fad8b6 | 1283 | bt_definition_unref(&stream->stream_event_context->p); |
e28d4618 | 1284 | if (stream->stream_event_header) |
13fad8b6 | 1285 | bt_definition_unref(&stream->stream_event_header->p); |
e28d4618 | 1286 | if (stream->stream_packet_context) |
13fad8b6 | 1287 | bt_definition_unref(&stream->stream_packet_context->p); |
e28d4618 MD |
1288 | return ret; |
1289 | } | |
1290 | ||
0f980a35 MD |
1291 | |
1292 | static | |
46322b33 | 1293 | int create_stream_packet_index(struct ctf_trace *td, |
0f980a35 MD |
1294 | struct ctf_file_stream *file_stream) |
1295 | { | |
f380e105 | 1296 | struct ctf_stream_declaration *stream; |
0f980a35 | 1297 | int len_index; |
46322b33 | 1298 | struct ctf_stream_pos *pos; |
0f980a35 MD |
1299 | struct stat filestats; |
1300 | struct packet_index packet_index; | |
1301 | int first_packet = 1; | |
1302 | int ret; | |
1303 | ||
1304 | pos = &file_stream->pos; | |
1305 | ||
1306 | ret = fstat(pos->fd, &filestats); | |
1307 | if (ret < 0) | |
1308 | return ret; | |
1309 | ||
8895362d MD |
1310 | if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT) |
1311 | return -EINVAL; | |
1312 | ||
0f980a35 MD |
1313 | for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) { |
1314 | uint64_t stream_id = 0; | |
1315 | ||
aee35fcc | 1316 | if (pos->base_mma) { |
0f980a35 | 1317 | /* unmap old base */ |
aee35fcc | 1318 | ret = munmap_align(pos->base_mma); |
0f980a35 | 1319 | if (ret) { |
3394d22e | 1320 | fprintf(stderr, "[error] Unable to unmap old base: %s.\n", |
0f980a35 MD |
1321 | strerror(errno)); |
1322 | return ret; | |
1323 | } | |
aee35fcc | 1324 | pos->base_mma = NULL; |
0f980a35 MD |
1325 | } |
1326 | /* map new base. Need mapping length from header. */ | |
aee35fcc | 1327 | pos->base_mma = mmap_align(MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ, |
0f980a35 | 1328 | MAP_PRIVATE, pos->fd, pos->mmap_offset); |
aee35fcc | 1329 | assert(pos->base_mma != MAP_FAILED); |
dc48ecad MD |
1330 | pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */ |
1331 | pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */ | |
0f980a35 MD |
1332 | pos->offset = 0; /* Position of the packet header */ |
1333 | ||
8c572eba MD |
1334 | packet_index.offset = pos->mmap_offset; |
1335 | packet_index.content_size = 0; | |
1336 | packet_index.packet_size = 0; | |
75cc2c35 MD |
1337 | packet_index.timestamp_begin = 0; |
1338 | packet_index.timestamp_end = 0; | |
41e82e00 | 1339 | packet_index.events_discarded = 0; |
15d4fe3c | 1340 | packet_index.events_discarded_len = 0; |
8c572eba | 1341 | |
0f980a35 | 1342 | /* read and check header, set stream id (and check) */ |
2d0bea29 | 1343 | if (file_stream->parent.trace_packet_header) { |
0f980a35 | 1344 | /* Read packet header */ |
2d0bea29 | 1345 | ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p); |
c5e74408 MD |
1346 | if (ret) |
1347 | return ret; | |
c8c98132 | 1348 | len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic")); |
0f980a35 | 1349 | if (len_index >= 0) { |
0d69b916 | 1350 | struct bt_definition *field; |
f4c56ac2 | 1351 | uint64_t magic; |
0f980a35 | 1352 | |
c8c98132 | 1353 | field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index); |
fe2b0e24 | 1354 | magic = bt_get_unsigned_int(field); |
f4c56ac2 | 1355 | if (magic != CTF_MAGIC) { |
3394d22e | 1356 | fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n", |
f4c56ac2 | 1357 | magic, |
03798a93 | 1358 | file_stream->pos.packet_cycles_index->len, |
8c572eba | 1359 | (ssize_t) pos->mmap_offset); |
0f980a35 MD |
1360 | return -EINVAL; |
1361 | } | |
1362 | } | |
1363 | ||
1364 | /* check uuid */ | |
c8c98132 | 1365 | len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid")); |
0f980a35 MD |
1366 | if (len_index >= 0) { |
1367 | struct definition_array *defarray; | |
0d69b916 | 1368 | struct bt_definition *field; |
0f980a35 | 1369 | uint64_t i; |
43e34335 | 1370 | uint8_t uuidval[BABELTRACE_UUID_LEN]; |
0f980a35 | 1371 | |
c8c98132 | 1372 | field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index); |
b1a2f580 MD |
1373 | assert(field->declaration->id == CTF_TYPE_ARRAY); |
1374 | defarray = container_of(field, struct definition_array, p); | |
dcaa2e7d | 1375 | assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN); |
0f980a35 | 1376 | |
43e34335 | 1377 | for (i = 0; i < BABELTRACE_UUID_LEN; i++) { |
0d69b916 | 1378 | struct bt_definition *elem; |
0f980a35 | 1379 | |
dcaa2e7d | 1380 | elem = bt_array_index(defarray, i); |
fe2b0e24 | 1381 | uuidval[i] = bt_get_unsigned_int(elem); |
0f980a35 | 1382 | } |
43e34335 | 1383 | ret = babeltrace_uuid_compare(td->uuid, uuidval); |
0f980a35 | 1384 | if (ret) { |
3394d22e | 1385 | fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n"); |
0f980a35 MD |
1386 | return -EINVAL; |
1387 | } | |
1388 | } | |
1389 | ||
1390 | ||
c8c98132 | 1391 | len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id")); |
0f980a35 | 1392 | if (len_index >= 0) { |
0d69b916 | 1393 | struct bt_definition *field; |
0f980a35 | 1394 | |
c8c98132 | 1395 | field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index); |
fe2b0e24 | 1396 | stream_id = bt_get_unsigned_int(field); |
0f980a35 MD |
1397 | } |
1398 | } | |
1399 | ||
2d0bea29 | 1400 | if (!first_packet && file_stream->parent.stream_id != stream_id) { |
3257b190 MD |
1401 | fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n", |
1402 | stream_id, | |
1403 | file_stream->parent.stream_id); | |
0f980a35 MD |
1404 | return -EINVAL; |
1405 | } | |
1406 | if (first_packet) { | |
2d0bea29 | 1407 | file_stream->parent.stream_id = stream_id; |
46322b33 | 1408 | if (stream_id >= td->streams->len) { |
3394d22e | 1409 | fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id); |
0f980a35 MD |
1410 | return -EINVAL; |
1411 | } | |
46322b33 | 1412 | stream = g_ptr_array_index(td->streams, stream_id); |
0f980a35 | 1413 | if (!stream) { |
3394d22e | 1414 | fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id); |
0f980a35 MD |
1415 | return -EINVAL; |
1416 | } | |
2d0bea29 | 1417 | file_stream->parent.stream_class = stream; |
01c76b24 | 1418 | ret = create_stream_definitions(td, &file_stream->parent); |
862434ec MD |
1419 | if (ret) |
1420 | return ret; | |
0f980a35 MD |
1421 | } |
1422 | first_packet = 0; | |
1423 | ||
2d0bea29 | 1424 | if (file_stream->parent.stream_packet_context) { |
dc48ecad | 1425 | /* Read packet context */ |
2d0bea29 | 1426 | ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p); |
c5e74408 MD |
1427 | if (ret) |
1428 | return ret; | |
dc48ecad | 1429 | /* read content size from header */ |
c8c98132 | 1430 | len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size")); |
dc48ecad | 1431 | if (len_index >= 0) { |
0d69b916 | 1432 | struct bt_definition *field; |
dc48ecad | 1433 | |
c8c98132 | 1434 | field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index); |
fe2b0e24 | 1435 | packet_index.content_size = bt_get_unsigned_int(field); |
dc48ecad MD |
1436 | } else { |
1437 | /* Use file size for packet size */ | |
8c572eba | 1438 | packet_index.content_size = filestats.st_size * CHAR_BIT; |
dc48ecad MD |
1439 | } |
1440 | ||
1441 | /* read packet size from header */ | |
c8c98132 | 1442 | len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size")); |
dc48ecad | 1443 | if (len_index >= 0) { |
0d69b916 | 1444 | struct bt_definition *field; |
dc48ecad | 1445 | |
c8c98132 | 1446 | field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index); |
fe2b0e24 | 1447 | packet_index.packet_size = bt_get_unsigned_int(field); |
dc48ecad MD |
1448 | } else { |
1449 | /* Use content size if non-zero, else file size */ | |
8c572eba | 1450 | packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT; |
dc48ecad | 1451 | } |
75cc2c35 MD |
1452 | |
1453 | /* read timestamp begin from header */ | |
c8c98132 | 1454 | len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin")); |
75cc2c35 | 1455 | if (len_index >= 0) { |
0d69b916 | 1456 | struct bt_definition *field; |
75cc2c35 | 1457 | |
c8c98132 | 1458 | field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index); |
fe2b0e24 | 1459 | packet_index.timestamp_begin = bt_get_unsigned_int(field); |
03798a93 JD |
1460 | if (file_stream->parent.stream_class->trace->collection) { |
1461 | packet_index.timestamp_begin = | |
1462 | ctf_get_real_timestamp( | |
1463 | &file_stream->parent, | |
1464 | packet_index.timestamp_begin); | |
1465 | } | |
75cc2c35 MD |
1466 | } |
1467 | ||
1468 | /* read timestamp end from header */ | |
c8c98132 | 1469 | len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end")); |
75cc2c35 | 1470 | if (len_index >= 0) { |
0d69b916 | 1471 | struct bt_definition *field; |
75cc2c35 | 1472 | |
c8c98132 | 1473 | field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index); |
fe2b0e24 | 1474 | packet_index.timestamp_end = bt_get_unsigned_int(field); |
03798a93 JD |
1475 | if (file_stream->parent.stream_class->trace->collection) { |
1476 | packet_index.timestamp_end = | |
1477 | ctf_get_real_timestamp( | |
1478 | &file_stream->parent, | |
1479 | packet_index.timestamp_end); | |
1480 | } | |
75cc2c35 | 1481 | } |
41e82e00 MD |
1482 | |
1483 | /* read events discarded from header */ | |
c8c98132 | 1484 | len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded")); |
41e82e00 | 1485 | if (len_index >= 0) { |
0d69b916 | 1486 | struct bt_definition *field; |
41e82e00 | 1487 | |
c8c98132 | 1488 | field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index); |
fe2b0e24 JD |
1489 | packet_index.events_discarded = bt_get_unsigned_int(field); |
1490 | packet_index.events_discarded_len = bt_get_int_len(field); | |
41e82e00 | 1491 | } |
0f980a35 MD |
1492 | } else { |
1493 | /* Use file size for packet size */ | |
8c572eba | 1494 | packet_index.content_size = filestats.st_size * CHAR_BIT; |
0f980a35 | 1495 | /* Use content size if non-zero, else file size */ |
8c572eba | 1496 | packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT; |
0f980a35 | 1497 | } |
546293fa MD |
1498 | |
1499 | /* Validate content size and packet size values */ | |
1500 | if (packet_index.content_size > packet_index.packet_size) { | |
7e18eedf | 1501 | fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n", |
546293fa MD |
1502 | packet_index.content_size, packet_index.packet_size); |
1503 | return -EINVAL; | |
1504 | } | |
1505 | ||
7e18eedf JN |
1506 | if (packet_index.packet_size > ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT) { |
1507 | fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n", | |
0fe3c2c9 | 1508 | packet_index.packet_size, ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT); |
546293fa MD |
1509 | return -EINVAL; |
1510 | } | |
1511 | ||
847bf71a MD |
1512 | /* Save position after header and context */ |
1513 | packet_index.data_offset = pos->offset; | |
0f980a35 | 1514 | |
0f980a35 | 1515 | /* add index to packet array */ |
03798a93 | 1516 | g_array_append_val(file_stream->pos.packet_cycles_index, packet_index); |
0f980a35 | 1517 | |
8c572eba | 1518 | pos->mmap_offset += packet_index.packet_size / CHAR_BIT; |
0f980a35 MD |
1519 | } |
1520 | ||
1521 | return 0; | |
1522 | } | |
1523 | ||
e28d4618 | 1524 | static |
9e88d150 | 1525 | int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream) |
e28d4618 MD |
1526 | { |
1527 | int ret; | |
1528 | ||
1529 | if (td->packet_header_decl) { | |
0d69b916 | 1530 | struct bt_definition *definition = |
e28d4618 MD |
1531 | td->packet_header_decl->p.definition_new(&td->packet_header_decl->p, |
1532 | stream->parent_def_scope, 0, 0, "trace.packet.header"); | |
1533 | if (!definition) { | |
1534 | ret = -EINVAL; | |
1535 | goto error; | |
1536 | } | |
1537 | stream->trace_packet_header = | |
1538 | container_of(definition, struct definition_struct, p); | |
1539 | stream->parent_def_scope = stream->trace_packet_header->p.scope; | |
1540 | } | |
1541 | ||
1542 | return 0; | |
1543 | ||
1544 | error: | |
1545 | return ret; | |
1546 | } | |
1547 | ||
0f980a35 MD |
1548 | /* |
1549 | * Note: many file streams can inherit from the same stream class | |
1550 | * description (metadata). | |
1551 | */ | |
1552 | static | |
b086c01a | 1553 | int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags, |
1cf393f6 | 1554 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
b086c01a | 1555 | int whence)) |
0f980a35 | 1556 | { |
f824ae04 | 1557 | int ret, fd, closeret; |
0f980a35 | 1558 | struct ctf_file_stream *file_stream; |
ff075710 | 1559 | struct stat statbuf; |
0f980a35 | 1560 | |
ff075710 MD |
1561 | fd = openat(td->dirfd, path, flags); |
1562 | if (fd < 0) { | |
a569a564 | 1563 | perror("File stream openat()"); |
ff075710 | 1564 | ret = fd; |
0f980a35 | 1565 | goto error; |
a569a564 | 1566 | } |
ff075710 MD |
1567 | |
1568 | /* Don't try to mmap subdirectories. Skip them, return success. */ | |
1569 | ret = fstat(fd, &statbuf); | |
1570 | if (ret) { | |
1571 | perror("File stream fstat()"); | |
1572 | goto fstat_error; | |
1573 | } | |
1574 | if (S_ISDIR(statbuf.st_mode)) { | |
1575 | fprintf(stderr, "[warning] Skipping directory '%s' found in trace\n", path); | |
1576 | ret = 0; | |
1577 | goto fd_is_dir_ok; | |
1578 | } | |
1579 | ||
0f980a35 | 1580 | file_stream = g_new0(struct ctf_file_stream, 1); |
3a25e036 | 1581 | file_stream->pos.last_offset = LAST_OFFSET_POISON; |
b086c01a | 1582 | |
87148dc7 MD |
1583 | strncpy(file_stream->parent.path, path, PATH_MAX); |
1584 | file_stream->parent.path[PATH_MAX - 1] = '\0'; | |
1585 | ||
06789ffd MD |
1586 | if (packet_seek) { |
1587 | file_stream->pos.packet_seek = packet_seek; | |
b086c01a | 1588 | } else { |
06789ffd | 1589 | fprintf(stderr, "[error] packet_seek function undefined.\n"); |
b086c01a JD |
1590 | ret = -1; |
1591 | goto error_def; | |
1592 | } | |
1593 | ||
f824ae04 MD |
1594 | ret = ctf_init_pos(&file_stream->pos, fd, flags); |
1595 | if (ret) | |
1596 | goto error_def; | |
2d0bea29 | 1597 | ret = create_trace_definitions(td, &file_stream->parent); |
e28d4618 MD |
1598 | if (ret) |
1599 | goto error_def; | |
25ccc85b | 1600 | /* |
50052405 | 1601 | * For now, only a single clock per trace is supported. |
25ccc85b MD |
1602 | */ |
1603 | file_stream->parent.current_clock = td->single_clock; | |
0f980a35 MD |
1604 | ret = create_stream_packet_index(td, file_stream); |
1605 | if (ret) | |
1606 | goto error_index; | |
1607 | /* Add stream file to stream class */ | |
2d0bea29 MD |
1608 | g_ptr_array_add(file_stream->parent.stream_class->streams, |
1609 | &file_stream->parent); | |
0f980a35 MD |
1610 | return 0; |
1611 | ||
1612 | error_index: | |
2d0bea29 | 1613 | if (file_stream->parent.trace_packet_header) |
13fad8b6 | 1614 | bt_definition_unref(&file_stream->parent.trace_packet_header->p); |
e28d4618 | 1615 | error_def: |
f824ae04 MD |
1616 | closeret = ctf_fini_pos(&file_stream->pos); |
1617 | if (closeret) { | |
1618 | fprintf(stderr, "Error on ctf_fini_pos\n"); | |
1619 | } | |
0f980a35 | 1620 | g_free(file_stream); |
ff075710 MD |
1621 | fd_is_dir_ok: |
1622 | fstat_error: | |
f824ae04 MD |
1623 | closeret = close(fd); |
1624 | if (closeret) { | |
1625 | perror("Error on fd close"); | |
1626 | } | |
0f980a35 | 1627 | error: |
65102a8c MD |
1628 | return ret; |
1629 | } | |
1630 | ||
bbefb8dd | 1631 | static |
5b80ddfb | 1632 | int ctf_open_trace_read(struct ctf_trace *td, |
8c250d87 | 1633 | const char *path, int flags, |
1cf393f6 | 1634 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
ae23d232 | 1635 | int whence), FILE *metadata_fp) |
bbefb8dd | 1636 | { |
f824ae04 | 1637 | int ret, closeret; |
65102a8c MD |
1638 | struct dirent *dirent; |
1639 | struct dirent *diriter; | |
1640 | size_t dirent_len; | |
bbefb8dd | 1641 | |
46322b33 | 1642 | td->flags = flags; |
bbefb8dd MD |
1643 | |
1644 | /* Open trace directory */ | |
46322b33 MD |
1645 | td->dir = opendir(path); |
1646 | if (!td->dir) { | |
6a6b384c | 1647 | fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path); |
bbefb8dd MD |
1648 | ret = -ENOENT; |
1649 | goto error; | |
1650 | } | |
1651 | ||
46322b33 MD |
1652 | td->dirfd = open(path, 0); |
1653 | if (td->dirfd < 0) { | |
6a6b384c | 1654 | fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path); |
a569a564 MD |
1655 | perror("Trace directory open"); |
1656 | ret = -errno; | |
65102a8c MD |
1657 | goto error_dirfd; |
1658 | } | |
5b80ddfb MD |
1659 | strncpy(td->path, path, sizeof(td->path)); |
1660 | td->path[sizeof(td->path) - 1] = '\0'; | |
0f980a35 | 1661 | |
65102a8c MD |
1662 | /* |
1663 | * Keep the metadata file separate. | |
1664 | */ | |
bbefb8dd | 1665 | |
06789ffd | 1666 | ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp); |
65102a8c | 1667 | if (ret) { |
6a6b384c | 1668 | fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path); |
65102a8c MD |
1669 | goto error_metadata; |
1670 | } | |
bbefb8dd MD |
1671 | |
1672 | /* | |
1673 | * Open each stream: for each file, try to open, check magic | |
1674 | * number, and get the stream ID to add to the right location in | |
1675 | * the stream array. | |
bbefb8dd MD |
1676 | */ |
1677 | ||
65102a8c | 1678 | dirent_len = offsetof(struct dirent, d_name) + |
46322b33 | 1679 | fpathconf(td->dirfd, _PC_NAME_MAX) + 1; |
bbefb8dd | 1680 | |
65102a8c | 1681 | dirent = malloc(dirent_len); |
bbefb8dd | 1682 | |
65102a8c | 1683 | for (;;) { |
46322b33 | 1684 | ret = readdir_r(td->dir, dirent, &diriter); |
65102a8c | 1685 | if (ret) { |
3394d22e | 1686 | fprintf(stderr, "[error] Readdir error.\n"); |
65102a8c | 1687 | goto readdir_error; |
65102a8c MD |
1688 | } |
1689 | if (!diriter) | |
1690 | break; | |
d8ea2d29 MD |
1691 | /* Ignore hidden files, ., .. and metadata. */ |
1692 | if (!strncmp(diriter->d_name, ".", 1) | |
65102a8c MD |
1693 | || !strcmp(diriter->d_name, "..") |
1694 | || !strcmp(diriter->d_name, "metadata")) | |
1695 | continue; | |
06789ffd MD |
1696 | ret = ctf_open_file_stream_read(td, diriter->d_name, |
1697 | flags, packet_seek); | |
dc48ecad | 1698 | if (ret) { |
3394d22e | 1699 | fprintf(stderr, "[error] Open file stream error.\n"); |
dc48ecad MD |
1700 | goto readdir_error; |
1701 | } | |
65102a8c | 1702 | } |
bbefb8dd | 1703 | |
65102a8c | 1704 | free(dirent); |
bbefb8dd | 1705 | return 0; |
65102a8c MD |
1706 | |
1707 | readdir_error: | |
1708 | free(dirent); | |
1709 | error_metadata: | |
f824ae04 MD |
1710 | closeret = close(td->dirfd); |
1711 | if (closeret) { | |
1712 | perror("Error on fd close"); | |
1713 | } | |
65102a8c | 1714 | error_dirfd: |
f824ae04 MD |
1715 | closeret = closedir(td->dir); |
1716 | if (closeret) { | |
1717 | perror("Error on closedir"); | |
1718 | } | |
bbefb8dd MD |
1719 | error: |
1720 | return ret; | |
1721 | } | |
1722 | ||
03798a93 JD |
1723 | /* |
1724 | * ctf_open_trace: Open a CTF trace and index it. | |
1725 | * Note that the user must seek the trace after the open (using the iterator) | |
1726 | * since the index creation read it entirely. | |
1727 | */ | |
e9378815 | 1728 | static |
1b8455b7 | 1729 | struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags, |
1cf393f6 | 1730 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
ae23d232 | 1731 | int whence), FILE *metadata_fp) |
bbefb8dd | 1732 | { |
46322b33 | 1733 | struct ctf_trace *td; |
bbefb8dd MD |
1734 | int ret; |
1735 | ||
2715de36 MD |
1736 | /* |
1737 | * If packet_seek is NULL, we provide our default version. | |
1738 | */ | |
1739 | if (!packet_seek) | |
1740 | packet_seek = ctf_packet_seek; | |
1741 | ||
46322b33 | 1742 | td = g_new0(struct ctf_trace, 1); |
bbefb8dd | 1743 | |
8c572eba | 1744 | switch (flags & O_ACCMODE) { |
bbefb8dd | 1745 | case O_RDONLY: |
b61922b5 | 1746 | if (!path) { |
3394d22e | 1747 | fprintf(stderr, "[error] Path missing for input CTF trace.\n"); |
b61922b5 MD |
1748 | goto error; |
1749 | } | |
06789ffd | 1750 | ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp); |
bbefb8dd MD |
1751 | if (ret) |
1752 | goto error; | |
1753 | break; | |
989c73bc | 1754 | case O_RDWR: |
3394d22e | 1755 | fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n"); |
46322b33 | 1756 | goto error; |
bbefb8dd | 1757 | default: |
3394d22e | 1758 | fprintf(stderr, "[error] Incorrect open flags.\n"); |
bbefb8dd MD |
1759 | goto error; |
1760 | } | |
1761 | ||
46322b33 | 1762 | return &td->parent; |
bbefb8dd MD |
1763 | error: |
1764 | g_free(td); | |
1765 | return NULL; | |
1766 | } | |
1767 | ||
2e937fb4 | 1768 | static |
f571dfb1 | 1769 | void ctf_init_mmap_pos(struct ctf_stream_pos *pos, |
c150f912 | 1770 | struct bt_mmap_stream *mmap_info) |
f571dfb1 JD |
1771 | { |
1772 | pos->mmap_offset = 0; | |
1773 | pos->packet_size = 0; | |
1774 | pos->content_size = 0; | |
1775 | pos->content_size_loc = NULL; | |
1776 | pos->fd = mmap_info->fd; | |
aee35fcc | 1777 | pos->base_mma = NULL; |
f571dfb1 JD |
1778 | pos->offset = 0; |
1779 | pos->dummy = false; | |
1780 | pos->cur_index = 0; | |
03798a93 JD |
1781 | pos->packet_cycles_index = NULL; |
1782 | pos->packet_real_index = NULL; | |
f571dfb1 JD |
1783 | pos->prot = PROT_READ; |
1784 | pos->flags = MAP_PRIVATE; | |
1785 | pos->parent.rw_table = read_dispatch_table; | |
1786 | pos->parent.event_cb = ctf_read_event; | |
1787 | } | |
1788 | ||
1789 | static | |
1790 | int prepare_mmap_stream_definition(struct ctf_trace *td, | |
1791 | struct ctf_file_stream *file_stream) | |
1792 | { | |
f380e105 | 1793 | struct ctf_stream_declaration *stream; |
f571dfb1 JD |
1794 | uint64_t stream_id = 0; |
1795 | int ret; | |
1796 | ||
1797 | file_stream->parent.stream_id = stream_id; | |
1798 | if (stream_id >= td->streams->len) { | |
3394d22e | 1799 | fprintf(stderr, "[error] Stream %" PRIu64 " is not declared " |
f571dfb1 JD |
1800 | "in metadata.\n", stream_id); |
1801 | ret = -EINVAL; | |
1802 | goto end; | |
1803 | } | |
1804 | stream = g_ptr_array_index(td->streams, stream_id); | |
1805 | if (!stream) { | |
3394d22e | 1806 | fprintf(stderr, "[error] Stream %" PRIu64 " is not declared " |
f571dfb1 JD |
1807 | "in metadata.\n", stream_id); |
1808 | ret = -EINVAL; | |
1809 | goto end; | |
1810 | } | |
1811 | file_stream->parent.stream_class = stream; | |
1812 | ret = create_stream_definitions(td, &file_stream->parent); | |
1813 | end: | |
1814 | return ret; | |
1815 | } | |
1816 | ||
1817 | static | |
1818 | int ctf_open_mmap_stream_read(struct ctf_trace *td, | |
c150f912 | 1819 | struct bt_mmap_stream *mmap_info, |
1cf393f6 | 1820 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
f571dfb1 JD |
1821 | int whence)) |
1822 | { | |
1823 | int ret; | |
1824 | struct ctf_file_stream *file_stream; | |
1825 | ||
1826 | file_stream = g_new0(struct ctf_file_stream, 1); | |
3a25e036 | 1827 | file_stream->pos.last_offset = LAST_OFFSET_POISON; |
f571dfb1 JD |
1828 | ctf_init_mmap_pos(&file_stream->pos, mmap_info); |
1829 | ||
06789ffd | 1830 | file_stream->pos.packet_seek = packet_seek; |
f571dfb1 JD |
1831 | |
1832 | ret = create_trace_definitions(td, &file_stream->parent); | |
1833 | if (ret) { | |
1834 | goto error_def; | |
1835 | } | |
1836 | ||
1837 | ret = prepare_mmap_stream_definition(td, file_stream); | |
1838 | if (ret) | |
1839 | goto error_index; | |
1840 | ||
f7bbd502 | 1841 | /* |
50052405 | 1842 | * For now, only a single clock per trace is supported. |
f7bbd502 JD |
1843 | */ |
1844 | file_stream->parent.current_clock = td->single_clock; | |
1845 | ||
f571dfb1 JD |
1846 | /* Add stream file to stream class */ |
1847 | g_ptr_array_add(file_stream->parent.stream_class->streams, | |
1848 | &file_stream->parent); | |
1849 | return 0; | |
1850 | ||
1851 | error_index: | |
1852 | if (file_stream->parent.trace_packet_header) | |
13fad8b6 | 1853 | bt_definition_unref(&file_stream->parent.trace_packet_header->p); |
f571dfb1 JD |
1854 | error_def: |
1855 | g_free(file_stream); | |
1856 | return ret; | |
1857 | } | |
1858 | ||
2e937fb4 | 1859 | static |
f571dfb1 | 1860 | int ctf_open_mmap_trace_read(struct ctf_trace *td, |
c150f912 | 1861 | struct bt_mmap_stream_list *mmap_list, |
1cf393f6 | 1862 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
f571dfb1 JD |
1863 | int whence), |
1864 | FILE *metadata_fp) | |
1865 | { | |
1866 | int ret; | |
c150f912 | 1867 | struct bt_mmap_stream *mmap_info; |
f571dfb1 | 1868 | |
06789ffd | 1869 | ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp); |
f571dfb1 JD |
1870 | if (ret) { |
1871 | goto error; | |
1872 | } | |
1873 | ||
1874 | /* | |
1875 | * for each stream, try to open, check magic number, and get the | |
1876 | * stream ID to add to the right location in the stream array. | |
1877 | */ | |
3122e6f0 | 1878 | bt_list_for_each_entry(mmap_info, &mmap_list->head, list) { |
06789ffd | 1879 | ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek); |
f571dfb1 | 1880 | if (ret) { |
3394d22e | 1881 | fprintf(stderr, "[error] Open file mmap stream error.\n"); |
f571dfb1 JD |
1882 | goto error; |
1883 | } | |
1884 | } | |
1885 | ||
1886 | return 0; | |
1887 | ||
1888 | error: | |
1889 | return ret; | |
1890 | } | |
1891 | ||
1892 | static | |
1b8455b7 | 1893 | struct bt_trace_descriptor *ctf_open_mmap_trace( |
c150f912 | 1894 | struct bt_mmap_stream_list *mmap_list, |
1cf393f6 | 1895 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, |
20d0dcf9 | 1896 | int whence), |
f571dfb1 JD |
1897 | FILE *metadata_fp) |
1898 | { | |
1899 | struct ctf_trace *td; | |
1900 | int ret; | |
1901 | ||
1902 | if (!metadata_fp) { | |
1903 | fprintf(stderr, "[error] No metadata file pointer associated, " | |
1904 | "required for mmap parsing\n"); | |
1905 | goto error; | |
1906 | } | |
06789ffd MD |
1907 | if (!packet_seek) { |
1908 | fprintf(stderr, "[error] packet_seek function undefined.\n"); | |
f571dfb1 JD |
1909 | goto error; |
1910 | } | |
1911 | td = g_new0(struct ctf_trace, 1); | |
06789ffd | 1912 | ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp); |
f571dfb1 JD |
1913 | if (ret) |
1914 | goto error_free; | |
1915 | ||
1916 | return &td->parent; | |
1917 | ||
1918 | error_free: | |
1919 | g_free(td); | |
1920 | error: | |
1921 | return NULL; | |
1922 | } | |
1923 | ||
03798a93 | 1924 | static |
1b8455b7 | 1925 | int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp) |
03798a93 JD |
1926 | { |
1927 | int i, j, k; | |
1928 | struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent); | |
1929 | ||
1930 | /* for each stream_class */ | |
1931 | for (i = 0; i < td->streams->len; i++) { | |
1932 | struct ctf_stream_declaration *stream_class; | |
1933 | ||
1934 | stream_class = g_ptr_array_index(td->streams, i); | |
1935 | if (!stream_class) | |
1936 | continue; | |
1937 | /* for each file_stream */ | |
1938 | for (j = 0; j < stream_class->streams->len; j++) { | |
1939 | struct ctf_stream_definition *stream; | |
1940 | struct ctf_stream_pos *stream_pos; | |
1941 | struct ctf_file_stream *cfs; | |
1942 | ||
1943 | stream = g_ptr_array_index(stream_class->streams, j); | |
1944 | if (!stream) | |
1945 | continue; | |
1946 | cfs = container_of(stream, struct ctf_file_stream, | |
1947 | parent); | |
1948 | stream_pos = &cfs->pos; | |
afe9cd4a JD |
1949 | if (!stream_pos->packet_cycles_index) |
1950 | continue; | |
1951 | ||
03798a93 JD |
1952 | for (k = 0; k < stream_pos->packet_cycles_index->len; k++) { |
1953 | struct packet_index *index; | |
1954 | struct packet_index new_index; | |
1955 | ||
1956 | index = &g_array_index(stream_pos->packet_cycles_index, | |
1957 | struct packet_index, k); | |
1958 | memcpy(&new_index, index, | |
1959 | sizeof(struct packet_index)); | |
1960 | new_index.timestamp_begin = | |
1961 | ctf_get_real_timestamp(stream, | |
1962 | index->timestamp_begin); | |
1963 | new_index.timestamp_end = | |
1964 | ctf_get_real_timestamp(stream, | |
1965 | index->timestamp_end); | |
1966 | g_array_append_val(stream_pos->packet_real_index, | |
1967 | new_index); | |
1968 | } | |
1969 | } | |
1970 | } | |
1971 | return 0; | |
1972 | } | |
1973 | ||
0f980a35 | 1974 | static |
f824ae04 | 1975 | int ctf_close_file_stream(struct ctf_file_stream *file_stream) |
0f980a35 | 1976 | { |
f824ae04 MD |
1977 | int ret; |
1978 | ||
1979 | ret = ctf_fini_pos(&file_stream->pos); | |
1980 | if (ret) { | |
1981 | fprintf(stderr, "Error on ctf_fini_pos\n"); | |
1982 | return -1; | |
1983 | } | |
1984 | ret = close(file_stream->pos.fd); | |
1985 | if (ret) { | |
1986 | perror("Error closing file fd"); | |
1987 | return -1; | |
1988 | } | |
1989 | return 0; | |
0f980a35 MD |
1990 | } |
1991 | ||
e9378815 | 1992 | static |
1b8455b7 | 1993 | int ctf_close_trace(struct bt_trace_descriptor *tdp) |
bbefb8dd | 1994 | { |
46322b33 | 1995 | struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent); |
08c82b90 | 1996 | int ret; |
0f980a35 | 1997 | |
46322b33 | 1998 | if (td->streams) { |
08c82b90 MD |
1999 | int i; |
2000 | ||
46322b33 | 2001 | for (i = 0; i < td->streams->len; i++) { |
f380e105 | 2002 | struct ctf_stream_declaration *stream; |
0f980a35 | 2003 | int j; |
e9378815 | 2004 | |
46322b33 | 2005 | stream = g_ptr_array_index(td->streams, i); |
e9378815 MD |
2006 | if (!stream) |
2007 | continue; | |
2d0bea29 | 2008 | for (j = 0; j < stream->streams->len; j++) { |
0f980a35 | 2009 | struct ctf_file_stream *file_stream; |
15d4fe3c JD |
2010 | file_stream = container_of(g_ptr_array_index(stream->streams, j), |
2011 | struct ctf_file_stream, parent); | |
f824ae04 MD |
2012 | ret = ctf_close_file_stream(file_stream); |
2013 | if (ret) | |
2014 | return ret; | |
0f980a35 | 2015 | } |
e003ab50 | 2016 | } |
e003ab50 | 2017 | } |
15d4fe3c | 2018 | ctf_destroy_metadata(td); |
f824ae04 MD |
2019 | ret = close(td->dirfd); |
2020 | if (ret) { | |
2021 | perror("Error closing dirfd"); | |
2022 | return ret; | |
2023 | } | |
2024 | ret = closedir(td->dir); | |
2025 | if (ret) { | |
2026 | perror("Error closedir"); | |
2027 | return ret; | |
2028 | } | |
bbefb8dd | 2029 | g_free(td); |
f824ae04 | 2030 | return 0; |
bbefb8dd MD |
2031 | } |
2032 | ||
98a04903 | 2033 | static |
1b8455b7 | 2034 | void ctf_set_context(struct bt_trace_descriptor *descriptor, |
98a04903 JD |
2035 | struct bt_context *ctx) |
2036 | { | |
2037 | struct ctf_trace *td = container_of(descriptor, struct ctf_trace, | |
2038 | parent); | |
2039 | ||
2040 | td->ctx = ctx; | |
2041 | } | |
2042 | ||
2043 | static | |
1b8455b7 | 2044 | void ctf_set_handle(struct bt_trace_descriptor *descriptor, |
98a04903 JD |
2045 | struct bt_trace_handle *handle) |
2046 | { | |
2047 | struct ctf_trace *td = container_of(descriptor, struct ctf_trace, | |
2048 | parent); | |
2049 | ||
2050 | td->handle = handle; | |
2051 | } | |
2052 | ||
95febab3 | 2053 | static |
7fb21036 | 2054 | void __attribute__((constructor)) ctf_init(void) |
fc93b2bd MD |
2055 | { |
2056 | int ret; | |
2057 | ||
4c8bfb7e | 2058 | ctf_format.name = g_quark_from_static_string("ctf"); |
fc93b2bd MD |
2059 | ret = bt_register_format(&ctf_format); |
2060 | assert(!ret); | |
2061 | } | |
698f0fe4 | 2062 | |
95febab3 MD |
2063 | static |
2064 | void __attribute__((destructor)) ctf_exit(void) | |
2065 | { | |
2066 | bt_unregister_format(&ctf_format); | |
2067 | } |