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