Fix: unchecked bt_ctf_event_set_clock_value return value
[babeltrace.git] / plugins / ctf / fs-src / data-stream-file.c
CommitLineData
e98a2d6e 1/*
94cf822e 2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
fc9a526c 3 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
e98a2d6e
PP
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
e98a2d6e
PP
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
0fbb9a9f 25#include <stdlib.h>
e98a2d6e
PP
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 <sys/mman.h>
33#include <babeltrace/ctf-ir/stream.h>
b2e0c907 34#include <babeltrace/graph/notification-iterator.h>
4f1f88a6
PP
35#include <babeltrace/graph/notification-stream.h>
36#include <babeltrace/graph/notification-event.h>
37#include <babeltrace/graph/notification-packet.h>
78586d8a
JG
38#include "file.h"
39#include "metadata.h"
40#include "../common/notif-iter/notif-iter.h"
41#include <assert.h>
94cf822e 42#include "data-stream-file.h"
e98a2d6e
PP
43
44#define PRINT_ERR_STREAM ctf_fs->error_fp
45#define PRINT_PREFIX "ctf-fs-data-stream"
cd00e1d3
MD
46#define PRINT_DBG_CHECK ctf_fs_debug
47#include "../print.h"
e98a2d6e 48
4f1f88a6 49static inline
94cf822e 50size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
e98a2d6e 51{
94cf822e 52 return ds_file->mmap_valid_len - ds_file->request_offset;
e98a2d6e
PP
53}
54
5b29e799 55static
94cf822e 56int ds_file_munmap(struct ctf_fs_ds_file *ds_file)
e98a2d6e 57{
fc9a526c 58 int ret = 0;
94cf822e 59 struct ctf_fs_component *ctf_fs = ds_file->file->ctf_fs;
e98a2d6e 60
94cf822e
PP
61 if (!ds_file->mmap_addr) {
62 goto end;
63 }
64
65 if (munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
e98a2d6e 66 PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n",
94cf822e
PP
67 ds_file->mmap_addr, ds_file->mmap_len,
68 ds_file->file->path->str, ds_file->file->fp,
e98a2d6e 69 strerror(errno));
fc9a526c
JG
70 ret = -1;
71 goto end;
e98a2d6e 72 }
94cf822e
PP
73
74 ds_file->mmap_addr = NULL;
75
fc9a526c
JG
76end:
77 return ret;
e98a2d6e
PP
78}
79
5b29e799 80static
94cf822e
PP
81enum bt_ctf_notif_iter_medium_status ds_file_mmap_next(
82 struct ctf_fs_ds_file *ds_file)
e98a2d6e 83{
e0f6a64a
JG
84 enum bt_ctf_notif_iter_medium_status ret =
85 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
94cf822e 86 struct ctf_fs_component *ctf_fs = ds_file->file->ctf_fs;
e98a2d6e
PP
87
88 /* Unmap old region */
94cf822e
PP
89 if (ds_file->mmap_addr) {
90 if (ds_file_munmap(ds_file)) {
e98a2d6e
PP
91 goto error;
92 }
93
94cf822e
PP
94 ds_file->mmap_offset += ds_file->mmap_valid_len;
95 ds_file->request_offset = 0;
e98a2d6e
PP
96 }
97
94cf822e
PP
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) {
e0f6a64a
JG
101 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
102 goto end;
103 }
fc9a526c 104 /* Round up to next page, assuming page size being a power of 2. */
94cf822e 105 ds_file->mmap_len = (ds_file->mmap_valid_len + ctf_fs->page_size - 1)
fc9a526c 106 & ~(ctf_fs->page_size - 1);
e98a2d6e 107 /* Map new region */
94cf822e
PP
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) {
e98a2d6e 113 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
94cf822e
PP
114 ds_file->mmap_len, ds_file->file->path->str,
115 ds_file->file->fp, ds_file->mmap_offset,
78586d8a 116 strerror(errno));
e98a2d6e
PP
117 goto error;
118 }
119
120 goto end;
e98a2d6e 121error:
94cf822e 122 ds_file_munmap(ds_file);
e0f6a64a 123 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
e98a2d6e
PP
124end:
125 return ret;
126}
127
5b29e799
JG
128static
129enum bt_ctf_notif_iter_medium_status medop_request_bytes(
e98a2d6e
PP
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;
94cf822e
PP
135 struct ctf_fs_ds_file *ds_file = data;
136 struct ctf_fs_component *ctf_fs = ds_file->file->ctf_fs;
e98a2d6e
PP
137
138 if (request_sz == 0) {
139 goto end;
140 }
141
e98a2d6e 142 /* Check if we have at least one memory-mapped byte left */
94cf822e 143 if (remaining_mmap_bytes(ds_file) == 0) {
e98a2d6e 144 /* Are we at the end of the file? */
94cf822e 145 if (ds_file->mmap_offset >= ds_file->file->size) {
e98a2d6e 146 PDBG("Reached end of file \"%s\" (%p)\n",
94cf822e 147 ds_file->file->path->str, ds_file->file->fp);
e98a2d6e
PP
148 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
149 goto end;
150 }
151
94cf822e 152 status = ds_file_mmap_next(ds_file);
e0f6a64a
JG
153 switch (status) {
154 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK:
155 break;
156 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF:
157 goto end;
158 default:
e98a2d6e 159 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
94cf822e
PP
160 ds_file->file->path->str,
161 ds_file->file->fp);
e98a2d6e
PP
162 goto error;
163 }
164 }
165
94cf822e
PP
166 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
167 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset;
168 ds_file->request_offset += *buffer_sz;
e98a2d6e
PP
169 goto end;
170
171error:
172 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
173
174end:
175 return status;
176}
177
5b29e799
JG
178static
179struct bt_ctf_stream *medop_get_stream(
e98a2d6e
PP
180 struct bt_ctf_stream_class *stream_class, void *data)
181{
94cf822e
PP
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;
e98a2d6e
PP
195 }
196
94cf822e
PP
197 stream = ds_file->stream;
198
199end:
200 return stream;
e98a2d6e
PP
201}
202
203static struct bt_ctf_notif_iter_medium_ops medops = {
204 .request_bytes = medop_request_bytes,
205 .get_stream = medop_get_stream,
206};
207
b6c3dcb2 208static
94cf822e 209int build_index_from_idx_file(struct ctf_fs_ds_file *ds_file)
b6c3dcb2
JG
210{
211 int ret = 0;
212 gchar *directory = NULL;
213 gchar *basename = NULL;
214 GString *index_basename = NULL;
215 gchar *index_file_path = NULL;
216 GMappedFile *mapped_file = NULL;
217 gsize filesize;
218 const struct ctf_packet_index_file_hdr *header;
219 const char *mmap_begin, *file_pos;
220 struct index_entry *index;
221 uint64_t total_packets_size = 0;
222 size_t file_index_entry_size;
223 size_t file_entry_count;
224 size_t i;
225
226 /* Look for index file in relative path index/name.idx. */
94cf822e 227 basename = g_path_get_basename(ds_file->file->path->str);
b6c3dcb2
JG
228 if (!basename) {
229 ret = -1;
230 goto end;
231 }
232
94cf822e 233 directory = g_path_get_dirname(ds_file->file->path->str);
b6c3dcb2
JG
234 if (!directory) {
235 ret = -1;
236 goto end;
237 }
238
239 index_basename = g_string_new(basename);
240 if (!index_basename) {
241 ret = -1;
242 goto end;
243 }
244
245 g_string_append(index_basename, ".idx");
246 index_file_path = g_build_filename(directory, "index",
247 index_basename->str, NULL);
248 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
06367fa3
JG
249 if (!mapped_file) {
250 ret = -1;
251 goto end;
252 }
b6c3dcb2
JG
253 filesize = g_mapped_file_get_length(mapped_file);
254 if (filesize < sizeof(*header)) {
255 printf_error("Invalid LTTng trace index: file size < header size");
256 ret = -1;
257 goto end;
258 }
259
260 mmap_begin = g_mapped_file_get_contents(mapped_file);
261 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
262
263 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
264 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
265 printf_error("Invalid LTTng trace index: \"magic\" validation failed");
266 ret = -1;
267 goto end;
268 }
b6c3dcb2
JG
269
270 file_index_entry_size = be32toh(header->packet_index_len);
271 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
272 if ((filesize - sizeof(*header)) % (file_entry_count * file_index_entry_size)) {
273 printf_error("Invalid index file size; not a multiple of index entry size");
274 ret = -1;
275 goto end;
276 }
277
94cf822e 278 ds_file->index.entries = g_array_sized_new(FALSE, TRUE,
b6c3dcb2 279 sizeof(struct index_entry), file_entry_count);
94cf822e 280 if (!ds_file->index.entries) {
b6c3dcb2
JG
281 ret = -1;
282 goto end;
283 }
94cf822e 284 index = (struct index_entry *) ds_file->index.entries->data;
b6c3dcb2
JG
285 for (i = 0; i < file_entry_count; i++) {
286 struct ctf_packet_index *file_index =
287 (struct ctf_packet_index *) file_pos;
288 uint64_t packet_size = be64toh(file_index->packet_size);
289
290 if (packet_size % CHAR_BIT) {
291 ret = -1;
292 printf_error("Invalid packet size encountered in index file");
293 goto invalid_index;
294 }
295
296 /* Convert size in bits to bytes. */
297 packet_size /= CHAR_BIT;
298 index->packet_size = packet_size;
299
300 index->offset = be64toh(file_index->offset);
301 if (i != 0 && index->offset < (index - 1)->offset) {
302 printf_error("Invalid, non-monotonic, packet offset encountered in index file");
303 ret = -1;
304 goto invalid_index;
305 }
306
307 index->timestamp_begin = be64toh(file_index->timestamp_begin);
308 index->timestamp_end = be64toh(file_index->timestamp_end);
309 if (index->timestamp_end < index->timestamp_begin) {
310 printf_error("Invalid packet time bounds encountered in index file");
311 ret = -1;
312 goto invalid_index;
313 }
314
315 total_packets_size += packet_size;
316 file_pos += file_index_entry_size;
317 index++;
318 }
319
320 /* Validate that the index addresses the complete stream. */
94cf822e 321 if (ds_file->file->size != total_packets_size) {
b6c3dcb2
JG
322 printf_error("Invalid index; indexed size != stream file size");
323 ret = -1;
324 goto invalid_index;
325 }
326end:
327 g_free(directory);
328 g_free(basename);
329 g_free(index_file_path);
06367fa3
JG
330 if (index_basename) {
331 g_string_free(index_basename, TRUE);
332 }
333 if (mapped_file) {
334 g_mapped_file_unref(mapped_file);
335 }
b6c3dcb2
JG
336 return ret;
337invalid_index:
94cf822e 338 g_array_free(ds_file->index.entries, TRUE);
b6c3dcb2
JG
339 goto end;
340}
341
342static
94cf822e 343int build_index_from_data_stream_file(struct ctf_fs_ds_file *stream)
b6c3dcb2 344{
4f1f88a6 345 return 0;
b6c3dcb2
JG
346}
347
348static
94cf822e 349int init_stream_index(struct ctf_fs_ds_file *ds_file)
b6c3dcb2
JG
350{
351 int ret;
352
94cf822e 353 ret = build_index_from_idx_file(ds_file);
b6c3dcb2
JG
354 if (!ret) {
355 goto end;
356 }
357
94cf822e 358 ret = build_index_from_data_stream_file(ds_file);
b6c3dcb2
JG
359end:
360 return ret;
361}
362
e7a4393b 363BT_HIDDEN
94cf822e
PP
364struct ctf_fs_ds_file *ctf_fs_ds_file_create(
365 struct ctf_fs_trace *ctf_fs_trace,
366 struct bt_ctf_stream *stream, const char *path,
367 bool build_index)
e98a2d6e 368{
b6c3dcb2 369 int ret;
94cf822e 370 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
e98a2d6e 371
94cf822e 372 if (!ds_file) {
e98a2d6e
PP
373 goto error;
374 }
375
94cf822e
PP
376 ds_file->file = ctf_fs_file_create(ctf_fs_trace->ctf_fs);
377 if (!ds_file->file) {
4f1f88a6
PP
378 goto error;
379 }
380
94cf822e
PP
381 ds_file->stream = bt_get(stream);
382 ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map);
383 g_string_assign(ds_file->file->path, path);
384 ret = ctf_fs_file_open(ctf_fs_trace->ctf_fs, ds_file->file, "rb");
4f1f88a6
PP
385 if (ret) {
386 goto error;
387 }
388
94cf822e 389 ds_file->notif_iter = bt_ctf_notif_iter_create(
1a9f7075 390 ctf_fs_trace->metadata->trace,
94cf822e 391 ctf_fs_trace->ctf_fs->page_size, medops, ds_file,
1a9f7075 392 ctf_fs_trace->ctf_fs->error_fp);
94cf822e 393 if (!ds_file->notif_iter) {
e98a2d6e
PP
394 goto error;
395 }
5b29e799 396
94cf822e
PP
397 ds_file->mmap_max_len = ctf_fs_trace->ctf_fs->page_size * 2048;
398
399 if (build_index) {
400 ret = init_stream_index(ds_file);
401 if (ret) {
402 goto error;
403 }
b6c3dcb2 404 }
1a9f7075 405
e98a2d6e 406 goto end;
1a9f7075 407
e98a2d6e 408error:
78586d8a 409 /* Do not touch "borrowed" file. */
94cf822e
PP
410 ctf_fs_ds_file_destroy(ds_file);
411 ds_file = NULL;
1a9f7075 412
e98a2d6e 413end:
94cf822e 414 return ds_file;
e98a2d6e
PP
415}
416
5b29e799 417BT_HIDDEN
94cf822e 418void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
e98a2d6e 419{
94cf822e 420 if (!ds_file) {
5b29e799 421 return;
043e2020
JG
422 }
423
94cf822e
PP
424 bt_put(ds_file->cc_prio_map);
425 bt_put(ds_file->stream);
426 (void) ds_file_munmap(ds_file);
0982a26d 427
94cf822e
PP
428 if (ds_file->file) {
429 ctf_fs_file_destroy(ds_file->file);
e98a2d6e
PP
430 }
431
94cf822e
PP
432 if (ds_file->notif_iter) {
433 bt_ctf_notif_iter_destroy(ds_file->notif_iter);
043e2020 434 }
5b29e799 435
94cf822e
PP
436 if (ds_file->index.entries) {
437 g_array_free(ds_file->index.entries, TRUE);
78586d8a 438 }
5b29e799 439
94cf822e 440 g_free(ds_file);
e98a2d6e 441}
4f1f88a6
PP
442
443BT_HIDDEN
94cf822e
PP
444struct bt_notification_iterator_next_return ctf_fs_ds_file_next(
445 struct ctf_fs_ds_file *ds_file)
4f1f88a6
PP
446{
447 enum bt_ctf_notif_iter_status notif_iter_status;
448 struct bt_notification_iterator_next_return ret = {
449 .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR,
450 .notification = NULL,
451 };
452
8915b7a7 453 notif_iter_status = bt_ctf_notif_iter_get_next_notification(
94cf822e 454 ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification);
4f1f88a6 455
4f1f88a6
PP
456 switch (notif_iter_status) {
457 case BT_CTF_NOTIF_ITER_STATUS_EOF:
458 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END;
459 break;
460 case BT_CTF_NOTIF_ITER_STATUS_OK:
461 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
462 break;
463 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
464 /*
465 * Should not make it this far as this is
466 * medium-specific; there is nothing for the user to do
467 * and it should have been handled upstream.
468 */
0fbb9a9f 469 abort();
4f1f88a6
PP
470 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
471 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
472 default:
473 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
474 break;
475 }
476
477 return ret;
478}
94cf822e
PP
479
480BT_HIDDEN
481int ctf_fs_ds_file_get_packet_header_context_fields(
482 struct ctf_fs_trace *ctf_fs_trace, const char *path,
483 struct bt_ctf_field **packet_header_field,
484 struct bt_ctf_field **packet_context_field)
485{
486 enum bt_ctf_notif_iter_status notif_iter_status;
487 struct ctf_fs_ds_file *ds_file;
488 int ret = 0;
489
490 ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, path, false);
491 if (!ds_file) {
492 goto error;
493 }
494
495 notif_iter_status = bt_ctf_notif_iter_get_packet_header_context_fields(
496 ds_file->notif_iter, packet_header_field, packet_context_field);
497 switch (notif_iter_status) {
498 case BT_CTF_NOTIF_ITER_STATUS_EOF:
499 case BT_CTF_NOTIF_ITER_STATUS_OK:
500 break;
501 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
0fbb9a9f 502 abort();
94cf822e
PP
503 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
504 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
505 default:
506 goto error;
507 break;
508 }
509
510 goto end;
511
512error:
513 ret = -1;
514
515 if (packet_header_field) {
516 bt_put(*packet_header_field);
517 }
518
519 if (packet_context_field) {
520 bt_put(*packet_context_field);
521 }
522
523end:
524 ctf_fs_ds_file_destroy(ds_file);
525 return ret;
526}
This page took 0.051981 seconds and 4 git commands to generate.