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