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