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