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