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