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