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