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