| 1 | /* |
| 2 | * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com> |
| 3 | * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdint.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stdbool.h> |
| 30 | #include <glib.h> |
| 31 | #include <inttypes.h> |
| 32 | #include <babeltrace/compat/mman-internal.h> |
| 33 | #include <babeltrace/endian-internal.h> |
| 34 | #include <babeltrace/ctf-ir/stream.h> |
| 35 | #include <babeltrace/graph/notification-iterator.h> |
| 36 | #include <babeltrace/graph/notification-stream.h> |
| 37 | #include <babeltrace/graph/notification-event.h> |
| 38 | #include <babeltrace/graph/notification-packet.h> |
| 39 | #include <babeltrace/common-internal.h> |
| 40 | #include "file.h" |
| 41 | #include "metadata.h" |
| 42 | #include "../common/notif-iter/notif-iter.h" |
| 43 | #include <assert.h> |
| 44 | #include "data-stream-file.h" |
| 45 | #include <string.h> |
| 46 | |
| 47 | #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC-DS" |
| 48 | #include "logging.h" |
| 49 | |
| 50 | static inline |
| 51 | size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file) |
| 52 | { |
| 53 | return ds_file->mmap_valid_len - ds_file->request_offset; |
| 54 | } |
| 55 | |
| 56 | static |
| 57 | int ds_file_munmap(struct ctf_fs_ds_file *ds_file) |
| 58 | { |
| 59 | int ret = 0; |
| 60 | |
| 61 | if (!ds_file || !ds_file->mmap_addr) { |
| 62 | goto end; |
| 63 | } |
| 64 | |
| 65 | if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) { |
| 66 | BT_LOGE("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s", |
| 67 | ds_file->mmap_addr, ds_file->mmap_len, |
| 68 | ds_file->file->path->str, ds_file->file->fp, |
| 69 | strerror(errno)); |
| 70 | ret = -1; |
| 71 | goto end; |
| 72 | } |
| 73 | |
| 74 | ds_file->mmap_addr = NULL; |
| 75 | |
| 76 | end: |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | static |
| 81 | enum bt_ctf_notif_iter_medium_status ds_file_mmap_next( |
| 82 | struct ctf_fs_ds_file *ds_file) |
| 83 | { |
| 84 | const size_t page_size = bt_common_get_page_size(); |
| 85 | enum bt_ctf_notif_iter_medium_status ret = |
| 86 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK; |
| 87 | |
| 88 | /* Unmap old region */ |
| 89 | if (ds_file->mmap_addr) { |
| 90 | if (ds_file_munmap(ds_file)) { |
| 91 | goto error; |
| 92 | } |
| 93 | |
| 94 | ds_file->mmap_offset += ds_file->mmap_valid_len; |
| 95 | ds_file->request_offset = 0; |
| 96 | } |
| 97 | |
| 98 | ds_file->mmap_valid_len = MIN(ds_file->file->size - ds_file->mmap_offset, |
| 99 | ds_file->mmap_max_len); |
| 100 | if (ds_file->mmap_valid_len == 0) { |
| 101 | ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF; |
| 102 | goto end; |
| 103 | } |
| 104 | /* Round up to next page, assuming page size being a power of 2. */ |
| 105 | ds_file->mmap_len = (ds_file->mmap_valid_len + page_size - 1) |
| 106 | & ~(page_size - 1); |
| 107 | /* Map new region */ |
| 108 | assert(ds_file->mmap_len); |
| 109 | ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len, |
| 110 | PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp), |
| 111 | ds_file->mmap_offset); |
| 112 | if (ds_file->mmap_addr == MAP_FAILED) { |
| 113 | BT_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s", |
| 114 | ds_file->mmap_len, ds_file->file->path->str, |
| 115 | ds_file->file->fp, (intmax_t) ds_file->mmap_offset, |
| 116 | strerror(errno)); |
| 117 | goto error; |
| 118 | } |
| 119 | |
| 120 | goto end; |
| 121 | error: |
| 122 | ds_file_munmap(ds_file); |
| 123 | ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
| 124 | end: |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | static |
| 129 | enum bt_ctf_notif_iter_medium_status medop_request_bytes( |
| 130 | size_t request_sz, uint8_t **buffer_addr, |
| 131 | size_t *buffer_sz, void *data) |
| 132 | { |
| 133 | enum bt_ctf_notif_iter_medium_status status = |
| 134 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK; |
| 135 | struct ctf_fs_ds_file *ds_file = data; |
| 136 | |
| 137 | if (request_sz == 0) { |
| 138 | goto end; |
| 139 | } |
| 140 | |
| 141 | /* Check if we have at least one memory-mapped byte left */ |
| 142 | if (remaining_mmap_bytes(ds_file) == 0) { |
| 143 | /* Are we at the end of the file? */ |
| 144 | if (ds_file->mmap_offset >= ds_file->file->size) { |
| 145 | BT_LOGD("Reached end of file \"%s\" (%p)", |
| 146 | ds_file->file->path->str, ds_file->file->fp); |
| 147 | status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF; |
| 148 | goto end; |
| 149 | } |
| 150 | |
| 151 | status = ds_file_mmap_next(ds_file); |
| 152 | switch (status) { |
| 153 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK: |
| 154 | break; |
| 155 | case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF: |
| 156 | goto end; |
| 157 | default: |
| 158 | BT_LOGE("Cannot memory-map next region of file \"%s\" (%p)", |
| 159 | ds_file->file->path->str, |
| 160 | ds_file->file->fp); |
| 161 | goto error; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz); |
| 166 | *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset; |
| 167 | ds_file->request_offset += *buffer_sz; |
| 168 | goto end; |
| 169 | |
| 170 | error: |
| 171 | status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR; |
| 172 | |
| 173 | end: |
| 174 | return status; |
| 175 | } |
| 176 | |
| 177 | static |
| 178 | struct bt_ctf_stream *medop_get_stream( |
| 179 | struct bt_ctf_stream_class *stream_class, uint64_t stream_id, |
| 180 | void *data) |
| 181 | { |
| 182 | struct ctf_fs_ds_file *ds_file = data; |
| 183 | struct bt_ctf_stream_class *ds_file_stream_class; |
| 184 | struct bt_ctf_stream *stream = NULL; |
| 185 | |
| 186 | ds_file_stream_class = bt_ctf_stream_get_class(ds_file->stream); |
| 187 | bt_put(ds_file_stream_class); |
| 188 | |
| 189 | if (stream_class != ds_file_stream_class) { |
| 190 | /* |
| 191 | * Not supported: two packets described by two different |
| 192 | * stream classes within the same data stream file. |
| 193 | */ |
| 194 | goto end; |
| 195 | } |
| 196 | |
| 197 | stream = ds_file->stream; |
| 198 | |
| 199 | end: |
| 200 | return stream; |
| 201 | } |
| 202 | |
| 203 | BT_HIDDEN |
| 204 | struct bt_ctf_notif_iter_medium_ops ctf_fs_ds_file_medops = { |
| 205 | .request_bytes = medop_request_bytes, |
| 206 | .get_stream = medop_get_stream, |
| 207 | }; |
| 208 | |
| 209 | static |
| 210 | struct ctf_fs_ds_index *ctf_fs_ds_index_create(size_t length) |
| 211 | { |
| 212 | struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1); |
| 213 | |
| 214 | if (!index) { |
| 215 | BT_LOGE_STR("Failed to allocate index"); |
| 216 | goto error; |
| 217 | } |
| 218 | |
| 219 | index->entries = g_array_sized_new(FALSE, TRUE, |
| 220 | sizeof(struct ctf_fs_ds_index_entry), length); |
| 221 | if (!index->entries) { |
| 222 | BT_LOGE("Failed to allocate %zu index entries.", length); |
| 223 | goto error; |
| 224 | } |
| 225 | g_array_set_size(index->entries, length); |
| 226 | end: |
| 227 | return index; |
| 228 | error: |
| 229 | ctf_fs_ds_index_destroy(index); |
| 230 | goto end; |
| 231 | } |
| 232 | |
| 233 | static |
| 234 | struct bt_ctf_clock_class *get_field_mapped_clock_class( |
| 235 | struct bt_ctf_field *field) |
| 236 | { |
| 237 | struct bt_ctf_field_type *field_type; |
| 238 | struct bt_ctf_clock_class *clock_class = NULL; |
| 239 | |
| 240 | field_type = bt_ctf_field_get_type(field); |
| 241 | if (!field_type) { |
| 242 | goto end; |
| 243 | } |
| 244 | |
| 245 | clock_class = bt_ctf_field_type_integer_get_mapped_clock_class( |
| 246 | field_type); |
| 247 | if (!clock_class) { |
| 248 | goto end; |
| 249 | } |
| 250 | end: |
| 251 | bt_put(field_type); |
| 252 | return clock_class; |
| 253 | } |
| 254 | |
| 255 | static |
| 256 | int get_ds_file_packet_bounds_clock_classes(struct ctf_fs_ds_file *ds_file, |
| 257 | struct bt_ctf_clock_class **_timestamp_begin_cc, |
| 258 | struct bt_ctf_clock_class **_timestamp_end_cc) |
| 259 | { |
| 260 | int ret; |
| 261 | struct bt_ctf_field *timestamp_field = NULL; |
| 262 | struct bt_ctf_field *packet_context_field = NULL; |
| 263 | struct bt_ctf_clock_class *timestamp_begin_cc = NULL; |
| 264 | struct bt_ctf_clock_class *timestamp_end_cc = NULL; |
| 265 | |
| 266 | ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file, |
| 267 | NULL, &packet_context_field); |
| 268 | if (ret || !packet_context_field) { |
| 269 | BT_LOGD("Cannot retrieve packet context field of stream \'%s\'", |
| 270 | ds_file->file->path->str); |
| 271 | ret = -1; |
| 272 | goto end; |
| 273 | } |
| 274 | |
| 275 | timestamp_field = bt_ctf_field_structure_get_field_by_name( |
| 276 | packet_context_field, "timestamp_begin"); |
| 277 | if (!timestamp_field) { |
| 278 | BT_LOGD("Cannot retrieve timestamp_begin field in packet context of stream \'%s\'", |
| 279 | ds_file->file->path->str); |
| 280 | ret = -1; |
| 281 | goto end; |
| 282 | } |
| 283 | |
| 284 | timestamp_begin_cc = get_field_mapped_clock_class(timestamp_field); |
| 285 | if (!timestamp_begin_cc) { |
| 286 | BT_LOGD("Cannot retrieve the clock mapped to timestamp_begin of stream \'%s\'", |
| 287 | ds_file->file->path->str); |
| 288 | } |
| 289 | BT_PUT(timestamp_field); |
| 290 | |
| 291 | timestamp_field = bt_ctf_field_structure_get_field_by_name( |
| 292 | packet_context_field, "timestamp_end"); |
| 293 | if (!timestamp_field) { |
| 294 | BT_LOGD("Cannot retrieve timestamp_end field in packet context of stream \'%s\'", |
| 295 | ds_file->file->path->str); |
| 296 | ret = -1; |
| 297 | goto end; |
| 298 | } |
| 299 | |
| 300 | timestamp_end_cc = get_field_mapped_clock_class(timestamp_field); |
| 301 | if (!timestamp_end_cc) { |
| 302 | BT_LOGD("Cannot retrieve the clock mapped to timestamp_end in stream \'%s\'", |
| 303 | ds_file->file->path->str); |
| 304 | } |
| 305 | |
| 306 | if (_timestamp_begin_cc) { |
| 307 | *_timestamp_begin_cc = bt_get(timestamp_begin_cc); |
| 308 | } |
| 309 | if (_timestamp_end_cc) { |
| 310 | *_timestamp_end_cc = bt_get(timestamp_end_cc); |
| 311 | } |
| 312 | end: |
| 313 | bt_put(packet_context_field); |
| 314 | bt_put(timestamp_field); |
| 315 | bt_put(timestamp_begin_cc); |
| 316 | bt_put(timestamp_end_cc); |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | static |
| 321 | int convert_cycles_to_ns(struct bt_ctf_clock_class *clock_class, |
| 322 | uint64_t cycles, int64_t *ns) |
| 323 | { |
| 324 | int ret = 0; |
| 325 | struct bt_ctf_clock_value *clock_value; |
| 326 | |
| 327 | assert(ns); |
| 328 | clock_value = bt_ctf_clock_value_create(clock_class, cycles); |
| 329 | if (!clock_value) { |
| 330 | ret = -1; |
| 331 | goto end; |
| 332 | } |
| 333 | |
| 334 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns); |
| 335 | if (ret) { |
| 336 | goto end; |
| 337 | } |
| 338 | end: |
| 339 | bt_put(clock_value); |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | static |
| 344 | struct ctf_fs_ds_index *build_index_from_idx_file( |
| 345 | struct ctf_fs_ds_file *ds_file) |
| 346 | { |
| 347 | int ret; |
| 348 | gchar *directory = NULL; |
| 349 | gchar *basename = NULL; |
| 350 | GString *index_basename = NULL; |
| 351 | gchar *index_file_path = NULL; |
| 352 | GMappedFile *mapped_file = NULL; |
| 353 | gsize filesize; |
| 354 | const char *mmap_begin = NULL, *file_pos = NULL; |
| 355 | const struct ctf_packet_index_file_hdr *header = NULL; |
| 356 | struct ctf_fs_ds_index *index = NULL; |
| 357 | struct ctf_fs_ds_index_entry *index_entry = NULL; |
| 358 | uint64_t total_packets_size = 0; |
| 359 | size_t file_index_entry_size; |
| 360 | size_t file_entry_count; |
| 361 | size_t i; |
| 362 | struct bt_ctf_clock_class *timestamp_begin_cc = NULL; |
| 363 | struct bt_ctf_clock_class *timestamp_end_cc = NULL; |
| 364 | |
| 365 | BT_LOGD("Building index from .idx file of stream file %s", |
| 366 | ds_file->file->path->str); |
| 367 | |
| 368 | ret = get_ds_file_packet_bounds_clock_classes(ds_file, |
| 369 | ×tamp_begin_cc, ×tamp_end_cc); |
| 370 | if (ret) { |
| 371 | BT_LOGD("Cannot get clock classes of \"timestamp_begin\" and \"timestamp_end\" fields"); |
| 372 | goto error; |
| 373 | } |
| 374 | |
| 375 | /* Look for index file in relative path index/name.idx. */ |
| 376 | basename = g_path_get_basename(ds_file->file->path->str); |
| 377 | if (!basename) { |
| 378 | BT_LOGE("Cannot get the basename of datastream file %s", |
| 379 | ds_file->file->path->str); |
| 380 | goto error; |
| 381 | } |
| 382 | |
| 383 | directory = g_path_get_dirname(ds_file->file->path->str); |
| 384 | if (!directory) { |
| 385 | BT_LOGE("Cannot get dirname of datastream file %s", |
| 386 | ds_file->file->path->str); |
| 387 | goto error; |
| 388 | } |
| 389 | |
| 390 | index_basename = g_string_new(basename); |
| 391 | if (!index_basename) { |
| 392 | BT_LOGE("Cannot allocate index file basename string"); |
| 393 | goto error; |
| 394 | } |
| 395 | |
| 396 | g_string_append(index_basename, ".idx"); |
| 397 | index_file_path = g_build_filename(directory, "index", |
| 398 | index_basename->str, NULL); |
| 399 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); |
| 400 | if (!mapped_file) { |
| 401 | BT_LOGD("Cannot create new mapped file %s", |
| 402 | index_file_path); |
| 403 | goto error; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * The g_mapped_file API limits us to 4GB files on 32-bit. |
| 408 | * Traces with such large indexes have never been seen in the wild, |
| 409 | * but this would need to be adjusted to support them. |
| 410 | */ |
| 411 | filesize = g_mapped_file_get_length(mapped_file); |
| 412 | if (filesize < sizeof(*header)) { |
| 413 | BT_LOGW("Invalid LTTng trace index file: " |
| 414 | "file size (%zu bytes) < header size (%zu bytes)", |
| 415 | filesize, sizeof(*header)); |
| 416 | goto error; |
| 417 | } |
| 418 | |
| 419 | mmap_begin = g_mapped_file_get_contents(mapped_file); |
| 420 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; |
| 421 | |
| 422 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); |
| 423 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { |
| 424 | BT_LOGW("Invalid LTTng trace index: \"magic\" field validation failed"); |
| 425 | goto error; |
| 426 | } |
| 427 | |
| 428 | file_index_entry_size = be32toh(header->packet_index_len); |
| 429 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; |
| 430 | if ((filesize - sizeof(*header)) % file_index_entry_size) { |
| 431 | BT_LOGW("Invalid LTTng trace index: the index's size after the header " |
| 432 | "(%zu bytes) is not a multiple of the index entry size " |
| 433 | "(%zu bytes)", (filesize - sizeof(*header)), |
| 434 | sizeof(*header)); |
| 435 | goto error; |
| 436 | } |
| 437 | |
| 438 | index = ctf_fs_ds_index_create(file_entry_count); |
| 439 | if (!index) { |
| 440 | goto error; |
| 441 | } |
| 442 | |
| 443 | index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index( |
| 444 | index->entries, struct ctf_fs_ds_index_entry, 0); |
| 445 | for (i = 0; i < file_entry_count; i++) { |
| 446 | struct ctf_packet_index *file_index = |
| 447 | (struct ctf_packet_index *) file_pos; |
| 448 | uint64_t packet_size = be64toh(file_index->packet_size); |
| 449 | |
| 450 | if (packet_size % CHAR_BIT) { |
| 451 | BT_LOGW("Invalid packet size encountered in LTTng trace index file"); |
| 452 | goto error; |
| 453 | } |
| 454 | |
| 455 | /* Convert size in bits to bytes. */ |
| 456 | packet_size /= CHAR_BIT; |
| 457 | index_entry->packet_size = packet_size; |
| 458 | |
| 459 | index_entry->offset = be64toh(file_index->offset); |
| 460 | if (i != 0 && index_entry->offset < (index_entry - 1)->offset) { |
| 461 | BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: " |
| 462 | "previous offset=%" PRIu64 ", current offset=%" PRIu64, |
| 463 | (index_entry - 1)->offset, index_entry->offset); |
| 464 | goto error; |
| 465 | } |
| 466 | |
| 467 | index_entry->timestamp_begin = be64toh(file_index->timestamp_begin); |
| 468 | index_entry->timestamp_end = be64toh(file_index->timestamp_end); |
| 469 | if (index_entry->timestamp_end < index_entry->timestamp_begin) { |
| 470 | BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): " |
| 471 | "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64, |
| 472 | index_entry->timestamp_begin, |
| 473 | index_entry->timestamp_end); |
| 474 | goto error; |
| 475 | } |
| 476 | |
| 477 | /* Convert the packet's bound to nanoseconds since Epoch. */ |
| 478 | ret = convert_cycles_to_ns(timestamp_begin_cc, |
| 479 | index_entry->timestamp_begin, |
| 480 | &index_entry->timestamp_begin_ns); |
| 481 | if (ret) { |
| 482 | BT_LOGD("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing"); |
| 483 | goto error; |
| 484 | } |
| 485 | ret = convert_cycles_to_ns(timestamp_end_cc, |
| 486 | index_entry->timestamp_end, |
| 487 | &index_entry->timestamp_end_ns); |
| 488 | if (ret) { |
| 489 | BT_LOGD("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing"); |
| 490 | goto error; |
| 491 | } |
| 492 | |
| 493 | total_packets_size += packet_size; |
| 494 | file_pos += file_index_entry_size; |
| 495 | index_entry++; |
| 496 | } |
| 497 | |
| 498 | /* Validate that the index addresses the complete stream. */ |
| 499 | if (ds_file->file->size != total_packets_size) { |
| 500 | BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: " |
| 501 | "file_size=%" PRIu64 ", total_packets_size=%" PRIu64, |
| 502 | ds_file->file->size, total_packets_size); |
| 503 | goto error; |
| 504 | } |
| 505 | end: |
| 506 | g_free(directory); |
| 507 | g_free(basename); |
| 508 | g_free(index_file_path); |
| 509 | if (index_basename) { |
| 510 | g_string_free(index_basename, TRUE); |
| 511 | } |
| 512 | if (mapped_file) { |
| 513 | g_mapped_file_unref(mapped_file); |
| 514 | } |
| 515 | bt_put(timestamp_begin_cc); |
| 516 | bt_put(timestamp_end_cc); |
| 517 | return index; |
| 518 | error: |
| 519 | ctf_fs_ds_index_destroy(index); |
| 520 | index = NULL; |
| 521 | goto end; |
| 522 | } |
| 523 | |
| 524 | BT_HIDDEN |
| 525 | struct ctf_fs_ds_file *ctf_fs_ds_file_create( |
| 526 | struct ctf_fs_trace *ctf_fs_trace, |
| 527 | struct bt_ctf_notif_iter *notif_iter, |
| 528 | struct bt_ctf_stream *stream, const char *path) |
| 529 | { |
| 530 | int ret; |
| 531 | const size_t page_size = bt_common_get_page_size(); |
| 532 | struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1); |
| 533 | |
| 534 | if (!ds_file) { |
| 535 | goto error; |
| 536 | } |
| 537 | |
| 538 | ds_file->file = ctf_fs_file_create(); |
| 539 | if (!ds_file->file) { |
| 540 | goto error; |
| 541 | } |
| 542 | |
| 543 | ds_file->stream = bt_get(stream); |
| 544 | ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map); |
| 545 | g_string_assign(ds_file->file->path, path); |
| 546 | ret = ctf_fs_file_open(ds_file->file, "rb"); |
| 547 | if (ret) { |
| 548 | goto error; |
| 549 | } |
| 550 | |
| 551 | ds_file->notif_iter = notif_iter; |
| 552 | bt_ctf_notif_iter_set_medops_data(ds_file->notif_iter, ds_file); |
| 553 | if (!ds_file->notif_iter) { |
| 554 | goto error; |
| 555 | } |
| 556 | |
| 557 | ds_file->mmap_max_len = page_size * 2048; |
| 558 | |
| 559 | goto end; |
| 560 | |
| 561 | error: |
| 562 | /* Do not touch "borrowed" file. */ |
| 563 | ctf_fs_ds_file_destroy(ds_file); |
| 564 | ds_file = NULL; |
| 565 | |
| 566 | end: |
| 567 | return ds_file; |
| 568 | } |
| 569 | |
| 570 | BT_HIDDEN |
| 571 | struct ctf_fs_ds_index *ctf_fs_ds_file_build_index( |
| 572 | struct ctf_fs_ds_file *ds_file) |
| 573 | { |
| 574 | return build_index_from_idx_file(ds_file); |
| 575 | } |
| 576 | |
| 577 | BT_HIDDEN |
| 578 | void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file) |
| 579 | { |
| 580 | if (!ds_file) { |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | bt_put(ds_file->cc_prio_map); |
| 585 | bt_put(ds_file->stream); |
| 586 | (void) ds_file_munmap(ds_file); |
| 587 | |
| 588 | if (ds_file->file) { |
| 589 | ctf_fs_file_destroy(ds_file->file); |
| 590 | } |
| 591 | |
| 592 | g_free(ds_file); |
| 593 | } |
| 594 | |
| 595 | BT_HIDDEN |
| 596 | struct bt_notification_iterator_next_return ctf_fs_ds_file_next( |
| 597 | struct ctf_fs_ds_file *ds_file) |
| 598 | { |
| 599 | enum bt_ctf_notif_iter_status notif_iter_status; |
| 600 | struct bt_notification_iterator_next_return ret = { |
| 601 | .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR, |
| 602 | .notification = NULL, |
| 603 | }; |
| 604 | |
| 605 | notif_iter_status = bt_ctf_notif_iter_get_next_notification( |
| 606 | ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification); |
| 607 | |
| 608 | switch (notif_iter_status) { |
| 609 | case BT_CTF_NOTIF_ITER_STATUS_EOF: |
| 610 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END; |
| 611 | break; |
| 612 | case BT_CTF_NOTIF_ITER_STATUS_OK: |
| 613 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 614 | break; |
| 615 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: |
| 616 | /* |
| 617 | * Should not make it this far as this is |
| 618 | * medium-specific; there is nothing for the user to do |
| 619 | * and it should have been handled upstream. |
| 620 | */ |
| 621 | abort(); |
| 622 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: |
| 623 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: |
| 624 | default: |
| 625 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 626 | break; |
| 627 | } |
| 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | BT_HIDDEN |
| 633 | int ctf_fs_ds_file_get_packet_header_context_fields( |
| 634 | struct ctf_fs_ds_file *ds_file, |
| 635 | struct bt_ctf_field **packet_header_field, |
| 636 | struct bt_ctf_field **packet_context_field) |
| 637 | { |
| 638 | enum bt_ctf_notif_iter_status notif_iter_status; |
| 639 | int ret = 0; |
| 640 | |
| 641 | assert(ds_file); |
| 642 | notif_iter_status = bt_ctf_notif_iter_get_packet_header_context_fields( |
| 643 | ds_file->notif_iter, packet_header_field, packet_context_field); |
| 644 | switch (notif_iter_status) { |
| 645 | case BT_CTF_NOTIF_ITER_STATUS_EOF: |
| 646 | case BT_CTF_NOTIF_ITER_STATUS_OK: |
| 647 | break; |
| 648 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: |
| 649 | abort(); |
| 650 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: |
| 651 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: |
| 652 | default: |
| 653 | goto error; |
| 654 | break; |
| 655 | } |
| 656 | |
| 657 | goto end; |
| 658 | |
| 659 | error: |
| 660 | ret = -1; |
| 661 | |
| 662 | if (packet_header_field) { |
| 663 | bt_put(*packet_header_field); |
| 664 | } |
| 665 | |
| 666 | if (packet_context_field) { |
| 667 | bt_put(*packet_context_field); |
| 668 | } |
| 669 | |
| 670 | end: |
| 671 | return ret; |
| 672 | } |
| 673 | |
| 674 | BT_HIDDEN |
| 675 | void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index) |
| 676 | { |
| 677 | if (!index) { |
| 678 | return; |
| 679 | } |
| 680 | |
| 681 | if (index->entries) { |
| 682 | g_array_free(index->entries, TRUE); |
| 683 | } |
| 684 | g_free(index); |
| 685 | } |