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