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