| 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 (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 = 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 %zu: %s", |
| 114 | ds_file->mmap_len, ds_file->file->path->str, |
| 115 | ds_file->file->fp, 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, void *data) |
| 180 | { |
| 181 | struct ctf_fs_ds_file *ds_file = data; |
| 182 | struct bt_ctf_stream_class *ds_file_stream_class; |
| 183 | struct bt_ctf_stream *stream = NULL; |
| 184 | |
| 185 | ds_file_stream_class = bt_ctf_stream_get_class(ds_file->stream); |
| 186 | bt_put(ds_file_stream_class); |
| 187 | |
| 188 | if (stream_class != ds_file_stream_class) { |
| 189 | /* |
| 190 | * Not supported: two packets described by two different |
| 191 | * stream classes within the same data stream file. |
| 192 | */ |
| 193 | goto end; |
| 194 | } |
| 195 | |
| 196 | stream = ds_file->stream; |
| 197 | |
| 198 | end: |
| 199 | return stream; |
| 200 | } |
| 201 | |
| 202 | static struct bt_ctf_notif_iter_medium_ops medops = { |
| 203 | .request_bytes = medop_request_bytes, |
| 204 | .get_stream = medop_get_stream, |
| 205 | }; |
| 206 | |
| 207 | static |
| 208 | int build_index_from_idx_file(struct ctf_fs_ds_file *ds_file) |
| 209 | { |
| 210 | int ret = 0; |
| 211 | gchar *directory = NULL; |
| 212 | gchar *basename = NULL; |
| 213 | GString *index_basename = NULL; |
| 214 | gchar *index_file_path = NULL; |
| 215 | GMappedFile *mapped_file = NULL; |
| 216 | gsize filesize; |
| 217 | const struct ctf_packet_index_file_hdr *header; |
| 218 | const char *mmap_begin, *file_pos; |
| 219 | struct index_entry *index; |
| 220 | uint64_t total_packets_size = 0; |
| 221 | size_t file_index_entry_size; |
| 222 | size_t file_entry_count; |
| 223 | size_t i; |
| 224 | |
| 225 | /* Look for index file in relative path index/name.idx. */ |
| 226 | basename = g_path_get_basename(ds_file->file->path->str); |
| 227 | if (!basename) { |
| 228 | BT_LOGE("Failed to get the basename of datastream file %s", |
| 229 | ds_file->file->path->str); |
| 230 | ret = -1; |
| 231 | goto end; |
| 232 | } |
| 233 | |
| 234 | directory = g_path_get_dirname(ds_file->file->path->str); |
| 235 | if (!directory) { |
| 236 | BT_LOGE("Failed to get dirname of datastream file %s", |
| 237 | ds_file->file->path->str); |
| 238 | ret = -1; |
| 239 | goto end; |
| 240 | } |
| 241 | |
| 242 | index_basename = g_string_new(basename); |
| 243 | if (!index_basename) { |
| 244 | BT_LOGE("Failed to allocate index file basename string"); |
| 245 | ret = -1; |
| 246 | goto end; |
| 247 | } |
| 248 | |
| 249 | g_string_append(index_basename, ".idx"); |
| 250 | index_file_path = g_build_filename(directory, "index", |
| 251 | index_basename->str, NULL); |
| 252 | mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL); |
| 253 | if (!mapped_file) { |
| 254 | BT_LOGD("Failed to create new mapped file %s", |
| 255 | index_file_path); |
| 256 | ret = -1; |
| 257 | goto end; |
| 258 | } |
| 259 | filesize = g_mapped_file_get_length(mapped_file); |
| 260 | if (filesize < sizeof(*header)) { |
| 261 | BT_LOGW("Invalid LTTng trace index file: file size < header size"); |
| 262 | ret = -1; |
| 263 | goto end; |
| 264 | } |
| 265 | |
| 266 | mmap_begin = g_mapped_file_get_contents(mapped_file); |
| 267 | header = (struct ctf_packet_index_file_hdr *) mmap_begin; |
| 268 | |
| 269 | file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header); |
| 270 | if (be32toh(header->magic) != CTF_INDEX_MAGIC) { |
| 271 | BT_LOGW("Invalid LTTng trace index: \"magic\" validation failed"); |
| 272 | ret = -1; |
| 273 | goto end; |
| 274 | } |
| 275 | |
| 276 | file_index_entry_size = be32toh(header->packet_index_len); |
| 277 | file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size; |
| 278 | if ((filesize - sizeof(*header)) % (file_entry_count * file_index_entry_size)) { |
| 279 | BT_LOGW("Invalid index file size; not a multiple of index entry size"); |
| 280 | ret = -1; |
| 281 | goto end; |
| 282 | } |
| 283 | |
| 284 | ds_file->index.entries = g_array_sized_new(FALSE, TRUE, |
| 285 | sizeof(struct index_entry), file_entry_count); |
| 286 | if (!ds_file->index.entries) { |
| 287 | ret = -1; |
| 288 | goto end; |
| 289 | } |
| 290 | index = (struct index_entry *) ds_file->index.entries->data; |
| 291 | for (i = 0; i < file_entry_count; i++) { |
| 292 | struct ctf_packet_index *file_index = |
| 293 | (struct ctf_packet_index *) file_pos; |
| 294 | uint64_t packet_size = be64toh(file_index->packet_size); |
| 295 | |
| 296 | if (packet_size % CHAR_BIT) { |
| 297 | ret = -1; |
| 298 | BT_LOGW("Invalid packet size encountered in index file"); |
| 299 | goto invalid_index; |
| 300 | } |
| 301 | |
| 302 | /* Convert size in bits to bytes. */ |
| 303 | packet_size /= CHAR_BIT; |
| 304 | index->packet_size = packet_size; |
| 305 | |
| 306 | index->offset = be64toh(file_index->offset); |
| 307 | if (i != 0 && index->offset < (index - 1)->offset) { |
| 308 | BT_LOGW("Invalid, non-monotonic, packet offset encountered in index file"); |
| 309 | ret = -1; |
| 310 | goto invalid_index; |
| 311 | } |
| 312 | |
| 313 | index->timestamp_begin = be64toh(file_index->timestamp_begin); |
| 314 | index->timestamp_end = be64toh(file_index->timestamp_end); |
| 315 | if (index->timestamp_end < index->timestamp_begin) { |
| 316 | BT_LOGW("Invalid packet time bounds encountered in index file"); |
| 317 | ret = -1; |
| 318 | goto invalid_index; |
| 319 | } |
| 320 | |
| 321 | total_packets_size += packet_size; |
| 322 | file_pos += file_index_entry_size; |
| 323 | index++; |
| 324 | } |
| 325 | |
| 326 | /* Validate that the index addresses the complete stream. */ |
| 327 | if (ds_file->file->size != total_packets_size) { |
| 328 | BT_LOGW("Invalid index; indexed size != stream file size"); |
| 329 | ret = -1; |
| 330 | goto invalid_index; |
| 331 | } |
| 332 | end: |
| 333 | g_free(directory); |
| 334 | g_free(basename); |
| 335 | g_free(index_file_path); |
| 336 | if (index_basename) { |
| 337 | g_string_free(index_basename, TRUE); |
| 338 | } |
| 339 | if (mapped_file) { |
| 340 | g_mapped_file_unref(mapped_file); |
| 341 | } |
| 342 | return ret; |
| 343 | invalid_index: |
| 344 | g_array_free(ds_file->index.entries, TRUE); |
| 345 | goto end; |
| 346 | } |
| 347 | |
| 348 | static |
| 349 | int build_index_from_data_stream_file(struct ctf_fs_ds_file *stream) |
| 350 | { |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static |
| 355 | int init_stream_index(struct ctf_fs_ds_file *ds_file) |
| 356 | { |
| 357 | int ret; |
| 358 | |
| 359 | ret = build_index_from_idx_file(ds_file); |
| 360 | if (!ret) { |
| 361 | goto end; |
| 362 | } |
| 363 | |
| 364 | ret = build_index_from_data_stream_file(ds_file); |
| 365 | end: |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | BT_HIDDEN |
| 370 | struct ctf_fs_ds_file *ctf_fs_ds_file_create( |
| 371 | struct ctf_fs_trace *ctf_fs_trace, |
| 372 | struct bt_ctf_stream *stream, const char *path, |
| 373 | bool build_index) |
| 374 | { |
| 375 | int ret; |
| 376 | const size_t page_size = bt_common_get_page_size(); |
| 377 | struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1); |
| 378 | |
| 379 | if (!ds_file) { |
| 380 | goto error; |
| 381 | } |
| 382 | |
| 383 | ds_file->file = ctf_fs_file_create(); |
| 384 | if (!ds_file->file) { |
| 385 | goto error; |
| 386 | } |
| 387 | |
| 388 | ds_file->stream = bt_get(stream); |
| 389 | ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map); |
| 390 | g_string_assign(ds_file->file->path, path); |
| 391 | ret = ctf_fs_file_open(ds_file->file, "rb"); |
| 392 | if (ret) { |
| 393 | goto error; |
| 394 | } |
| 395 | |
| 396 | ds_file->notif_iter = bt_ctf_notif_iter_create( |
| 397 | ctf_fs_trace->metadata->trace, page_size, medops, ds_file); |
| 398 | if (!ds_file->notif_iter) { |
| 399 | goto error; |
| 400 | } |
| 401 | |
| 402 | ds_file->mmap_max_len = page_size * 2048; |
| 403 | |
| 404 | if (build_index) { |
| 405 | ret = init_stream_index(ds_file); |
| 406 | if (ret) { |
| 407 | goto error; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | goto end; |
| 412 | |
| 413 | error: |
| 414 | /* Do not touch "borrowed" file. */ |
| 415 | ctf_fs_ds_file_destroy(ds_file); |
| 416 | ds_file = NULL; |
| 417 | |
| 418 | end: |
| 419 | return ds_file; |
| 420 | } |
| 421 | |
| 422 | BT_HIDDEN |
| 423 | void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file) |
| 424 | { |
| 425 | if (!ds_file) { |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | bt_put(ds_file->cc_prio_map); |
| 430 | bt_put(ds_file->stream); |
| 431 | (void) ds_file_munmap(ds_file); |
| 432 | |
| 433 | if (ds_file->file) { |
| 434 | ctf_fs_file_destroy(ds_file->file); |
| 435 | } |
| 436 | |
| 437 | if (ds_file->notif_iter) { |
| 438 | bt_ctf_notif_iter_destroy(ds_file->notif_iter); |
| 439 | } |
| 440 | |
| 441 | if (ds_file->index.entries) { |
| 442 | g_array_free(ds_file->index.entries, TRUE); |
| 443 | } |
| 444 | |
| 445 | g_free(ds_file); |
| 446 | } |
| 447 | |
| 448 | BT_HIDDEN |
| 449 | struct bt_notification_iterator_next_return ctf_fs_ds_file_next( |
| 450 | struct ctf_fs_ds_file *ds_file) |
| 451 | { |
| 452 | enum bt_ctf_notif_iter_status notif_iter_status; |
| 453 | struct bt_notification_iterator_next_return ret = { |
| 454 | .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR, |
| 455 | .notification = NULL, |
| 456 | }; |
| 457 | |
| 458 | notif_iter_status = bt_ctf_notif_iter_get_next_notification( |
| 459 | ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification); |
| 460 | |
| 461 | switch (notif_iter_status) { |
| 462 | case BT_CTF_NOTIF_ITER_STATUS_EOF: |
| 463 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END; |
| 464 | break; |
| 465 | case BT_CTF_NOTIF_ITER_STATUS_OK: |
| 466 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 467 | break; |
| 468 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: |
| 469 | /* |
| 470 | * Should not make it this far as this is |
| 471 | * medium-specific; there is nothing for the user to do |
| 472 | * and it should have been handled upstream. |
| 473 | */ |
| 474 | abort(); |
| 475 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: |
| 476 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: |
| 477 | default: |
| 478 | ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 479 | break; |
| 480 | } |
| 481 | |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | BT_HIDDEN |
| 486 | int ctf_fs_ds_file_get_packet_header_context_fields( |
| 487 | struct ctf_fs_trace *ctf_fs_trace, const char *path, |
| 488 | struct bt_ctf_field **packet_header_field, |
| 489 | struct bt_ctf_field **packet_context_field) |
| 490 | { |
| 491 | enum bt_ctf_notif_iter_status notif_iter_status; |
| 492 | struct ctf_fs_ds_file *ds_file; |
| 493 | int ret = 0; |
| 494 | |
| 495 | ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, path, false); |
| 496 | if (!ds_file) { |
| 497 | goto error; |
| 498 | } |
| 499 | |
| 500 | notif_iter_status = bt_ctf_notif_iter_get_packet_header_context_fields( |
| 501 | ds_file->notif_iter, packet_header_field, packet_context_field); |
| 502 | switch (notif_iter_status) { |
| 503 | case BT_CTF_NOTIF_ITER_STATUS_EOF: |
| 504 | case BT_CTF_NOTIF_ITER_STATUS_OK: |
| 505 | break; |
| 506 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: |
| 507 | abort(); |
| 508 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: |
| 509 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: |
| 510 | default: |
| 511 | goto error; |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | goto end; |
| 516 | |
| 517 | error: |
| 518 | ret = -1; |
| 519 | |
| 520 | if (packet_header_field) { |
| 521 | bt_put(*packet_header_field); |
| 522 | } |
| 523 | |
| 524 | if (packet_context_field) { |
| 525 | bt_put(*packet_context_field); |
| 526 | } |
| 527 | |
| 528 | end: |
| 529 | ctf_fs_ds_file_destroy(ds_file); |
| 530 | return ret; |
| 531 | } |