a032334ced4e4f4c40faf2e0233cef1f5c220116
[deliverable/babeltrace.git] / src / plugins / ctf / fs-src / fs.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2015-2017 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 *
7 * Babeltrace CTF file system Reader Component
8 */
9
10 #define BT_CLOG_CFG logCfg
11 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS"
12
13 #include "common/common.h"
14 #include <babeltrace2/babeltrace.h>
15 #include "common/uuid.h"
16 #include <glib.h>
17 #include "common/assert.h"
18 #include <inttypes.h>
19 #include <stdbool.h>
20 #include "fs.hpp"
21 #include "metadata.hpp"
22 #include "data-stream-file.hpp"
23 #include "file.hpp"
24 #include "../common/src/metadata/tsdl/decoder.hpp"
25 #include "../common/src/metadata/tsdl/ctf-meta-configure-ir-trace.hpp"
26 #include "../common/src/msg-iter/msg-iter.hpp"
27 #include "query.hpp"
28 #include "plugins/common/param-validation/param-validation.h"
29 #include "cpp-common/cfg-logging.hpp"
30 #include "cpp-common/cfg-logging-error-reporting.hpp"
31 #include "cpp-common/exc.hpp"
32 #include "cpp-common/make-unique.hpp"
33 #include <vector>
34
35 struct tracer_info
36 {
37 const char *name;
38 int64_t major;
39 int64_t minor;
40 int64_t patch;
41 };
42
43 static void ctf_fs_msg_iter_data_destroy(struct ctf_fs_msg_iter_data *msg_iter_data)
44 {
45 if (!msg_iter_data) {
46 return;
47 }
48
49 if (msg_iter_data->msg_iter) {
50 ctf_msg_iter_destroy(msg_iter_data->msg_iter);
51 }
52
53 if (msg_iter_data->msg_iter_medops_data) {
54 ctf_fs_ds_group_medops_data_destroy(msg_iter_data->msg_iter_medops_data);
55 }
56
57 delete msg_iter_data;
58 }
59
60 static bt_message_iterator_class_next_method_status
61 ctf_fs_iterator_next_one(struct ctf_fs_msg_iter_data *msg_iter_data, const bt_message **out_msg)
62 {
63 bt_message_iterator_class_next_method_status status;
64 enum ctf_msg_iter_status msg_iter_status;
65 const bt2_common::LogCfg& logCfg = msg_iter_data->logCfg;
66
67 msg_iter_status = ctf_msg_iter_get_next_message(msg_iter_data->msg_iter, out_msg);
68
69 switch (msg_iter_status) {
70 case CTF_MSG_ITER_STATUS_OK:
71 /* Cool, message has been written to *out_msg. */
72 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
73 break;
74
75 case CTF_MSG_ITER_STATUS_EOF:
76 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
77 break;
78
79 case CTF_MSG_ITER_STATUS_AGAIN:
80 /*
81 * Should not make it this far as this is
82 * medium-specific; there is nothing for the user to do
83 * and it should have been handled upstream.
84 */
85 bt_common_abort();
86
87 case CTF_MSG_ITER_STATUS_ERROR:
88 BT_CLOGE_APPEND_CAUSE("Failed to get next message from CTF message iterator.");
89 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
90 break;
91
92 case CTF_MSG_ITER_STATUS_MEMORY_ERROR:
93 BT_CLOGE_APPEND_CAUSE("Failed to get next message from CTF message iterator.");
94 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
95 break;
96
97 default:
98 bt_common_abort();
99 }
100
101 return status;
102 }
103
104 BT_HIDDEN
105 bt_message_iterator_class_next_method_status
106 ctf_fs_iterator_next(bt_self_message_iterator *iterator, bt_message_array_const msgs,
107 uint64_t capacity, uint64_t *count)
108 {
109 struct ctf_fs_msg_iter_data *msg_iter_data =
110 (struct ctf_fs_msg_iter_data *) bt_self_message_iterator_get_data(iterator);
111 const bt2_common::LogCfg& logCfg = msg_iter_data->logCfg;
112
113 try {
114 bt_message_iterator_class_next_method_status status;
115 uint64_t i = 0;
116
117 if (G_UNLIKELY(msg_iter_data->next_saved_error)) {
118 /*
119 * Last time we were called, we hit an error but had some
120 * messages to deliver, so we stashed the error here. Return
121 * it now.
122 */
123 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(msg_iter_data->next_saved_error);
124 status = msg_iter_data->next_saved_status;
125 goto end;
126 }
127
128 do {
129 status = ctf_fs_iterator_next_one(msg_iter_data, &msgs[i]);
130 if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
131 i++;
132 }
133 } while (i < capacity && status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK);
134
135 if (i > 0) {
136 /*
137 * Even if ctf_fs_iterator_next_one() returned something
138 * else than BT_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK, we
139 * accumulated message objects in the output
140 * message array, so we need to return
141 * BT_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK so that they are
142 * transfered to downstream. This other status occurs
143 * again the next time muxer_msg_iter_do_next() is
144 * called, possibly without any accumulated
145 * message, in which case we'll return it.
146 */
147 if (status < 0) {
148 /*
149 * Save this error for the next _next call. Assume that
150 * this component always appends error causes when
151 * returning an error status code, which will cause the
152 * current thread error to be non-NULL.
153 */
154 msg_iter_data->next_saved_error = bt_current_thread_take_error();
155 BT_ASSERT(msg_iter_data->next_saved_error);
156 msg_iter_data->next_saved_status = status;
157 }
158
159 *count = i;
160 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
161 }
162
163 end:
164 return status;
165 } catch (const std::bad_alloc&) {
166 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
167 } catch (const bt2_common::Error&) {
168 BT_CLOGE_APPEND_CAUSE("Failed to fetch next messages");
169 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
170 }
171 }
172
173 BT_HIDDEN
174 bt_message_iterator_class_seek_beginning_method_status
175 ctf_fs_iterator_seek_beginning(bt_self_message_iterator *it)
176 {
177 struct ctf_fs_msg_iter_data *msg_iter_data =
178 (struct ctf_fs_msg_iter_data *) bt_self_message_iterator_get_data(it);
179
180 BT_ASSERT(msg_iter_data);
181
182 const bt2_common::LogCfg& logCfg = msg_iter_data->logCfg;
183
184 try {
185 ctf_msg_iter_reset(msg_iter_data->msg_iter);
186 ctf_fs_ds_group_medops_data_reset(msg_iter_data->msg_iter_medops_data);
187
188 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK;
189 } catch (const std::bad_alloc&) {
190 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_MEMORY_ERROR;
191 } catch (const bt2_common::Error&) {
192 BT_CLOGE_APPEND_CAUSE("Failed to seek beginning");
193 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_ERROR;
194 }
195 }
196
197 BT_HIDDEN
198 void ctf_fs_iterator_finalize(bt_self_message_iterator *it)
199 {
200 ctf_fs_msg_iter_data_destroy(
201 (struct ctf_fs_msg_iter_data *) bt_self_message_iterator_get_data(it));
202 }
203
204 static bt_message_iterator_class_initialize_method_status
205 ctf_msg_iter_medium_status_to_msg_iter_initialize_status(enum ctf_msg_iter_medium_status status)
206 {
207 switch (status) {
208 case CTF_MSG_ITER_MEDIUM_STATUS_EOF:
209 case CTF_MSG_ITER_MEDIUM_STATUS_AGAIN:
210 case CTF_MSG_ITER_MEDIUM_STATUS_ERROR:
211 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
212 case CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR:
213 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
214 case CTF_MSG_ITER_MEDIUM_STATUS_OK:
215 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
216 }
217
218 bt_common_abort();
219 }
220
221 BT_HIDDEN
222 bt_message_iterator_class_initialize_method_status
223 ctf_fs_iterator_init(bt_self_message_iterator *self_msg_iter,
224 bt_self_message_iterator_configuration *config,
225 bt_self_component_port_output *self_port)
226 {
227 bt_self_component_port *self_comp_port =
228 bt_self_component_port_output_as_self_component_port(self_port);
229 ctf_fs_port_data *port_data =
230 (ctf_fs_port_data *) bt_self_component_port_get_data(self_comp_port);
231 BT_ASSERT(port_data);
232
233 bt2_common::LogCfg logCfg {port_data->ctf_fs->logCfg.logLevel(), *self_msg_iter};
234
235 try {
236 bt_message_iterator_class_initialize_method_status status;
237 enum ctf_msg_iter_medium_status medium_status;
238
239 ctf_fs_msg_iter_data *msg_iter_data = new ctf_fs_msg_iter_data {logCfg};
240
241 msg_iter_data->self_msg_iter = self_msg_iter;
242 msg_iter_data->ds_file_group = port_data->ds_file_group;
243
244 medium_status =
245 ctf_fs_ds_group_medops_data_create(msg_iter_data->ds_file_group, self_msg_iter, logCfg,
246 &msg_iter_data->msg_iter_medops_data);
247 BT_ASSERT(medium_status == CTF_MSG_ITER_MEDIUM_STATUS_OK ||
248 medium_status == CTF_MSG_ITER_MEDIUM_STATUS_ERROR ||
249 medium_status == CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR);
250 if (medium_status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
251 BT_CLOGE_APPEND_CAUSE("Failed to create ctf_fs_ds_group_medops");
252 status = ctf_msg_iter_medium_status_to_msg_iter_initialize_status(medium_status);
253 goto error;
254 }
255
256 msg_iter_data->msg_iter = ctf_msg_iter_create(
257 msg_iter_data->ds_file_group->ctf_fs_trace->metadata->tc,
258 bt_common_get_page_size(logCfg.logLevel()) * 8, ctf_fs_ds_group_medops,
259 msg_iter_data->msg_iter_medops_data, self_msg_iter, logCfg);
260 if (!msg_iter_data->msg_iter) {
261 BT_CLOGE_APPEND_CAUSE("Cannot create a CTF message iterator.");
262 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
263 goto error;
264 }
265
266 /*
267 * This iterator can seek forward if its stream class has a default
268 * clock class.
269 */
270 if (msg_iter_data->ds_file_group->sc->default_clock_class) {
271 bt_self_message_iterator_configuration_set_can_seek_forward(config, true);
272 }
273
274 bt_self_message_iterator_set_data(self_msg_iter, msg_iter_data);
275 msg_iter_data = NULL;
276
277 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
278 goto end;
279
280 error:
281 bt_self_message_iterator_set_data(self_msg_iter, NULL);
282
283 end:
284 ctf_fs_msg_iter_data_destroy(msg_iter_data);
285 return status;
286 } catch (const std::bad_alloc&) {
287 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
288 } catch (const bt2_common::Error&) {
289 BT_CLOGE_APPEND_CAUSE("Failed to initialize iterator");
290 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
291 }
292 }
293
294 static void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace)
295 {
296 if (!ctf_fs_trace) {
297 return;
298 }
299
300 BT_TRACE_PUT_REF_AND_RESET(ctf_fs_trace->trace);
301
302 if (ctf_fs_trace->path) {
303 g_string_free(ctf_fs_trace->path, TRUE);
304 }
305
306 if (ctf_fs_trace->metadata) {
307 ctf_fs_metadata_fini(ctf_fs_trace->metadata);
308 delete ctf_fs_trace->metadata;
309 }
310
311 delete ctf_fs_trace;
312 }
313
314 void ctf_fs_trace_deleter::operator()(ctf_fs_trace *trace)
315 {
316 ctf_fs_trace_destroy(trace);
317 }
318
319 BT_HIDDEN
320 void ctf_fs_destroy(struct ctf_fs_component *ctf_fs)
321 {
322 if (!ctf_fs) {
323 return;
324 }
325
326 delete ctf_fs;
327 }
328
329 void ctf_fs_component_deleter::operator()(ctf_fs_component *comp)
330 {
331 ctf_fs_destroy(comp);
332 }
333
334 ctf_fs_component::UP ctf_fs_component_create(const bt2_common::LogCfg& logCfg)
335 {
336 return ctf_fs_component::UP {new ctf_fs_component {logCfg}};
337 }
338
339 void ctf_fs_finalize(bt_self_component_source *component)
340 {
341 ctf_fs_destroy((struct ctf_fs_component *) bt_self_component_get_data(
342 bt_self_component_source_as_self_component(component)));
343 }
344
345 bt2_common::GCharUP ctf_fs_make_port_name(struct ctf_fs_ds_file_group *ds_file_group)
346 {
347 GString *name = g_string_new(NULL);
348
349 /*
350 * The unique port name is generated by concatenating unique identifiers
351 * for:
352 *
353 * - the trace
354 * - the stream class
355 * - the stream
356 */
357
358 /* For the trace, use the uuid if present, else the path. */
359 if (ds_file_group->ctf_fs_trace->metadata->tc->is_uuid_set) {
360 char uuid_str[BT_UUID_STR_LEN + 1];
361
362 bt_uuid_to_str(ds_file_group->ctf_fs_trace->metadata->tc->uuid, uuid_str);
363 g_string_assign(name, uuid_str);
364 } else {
365 g_string_assign(name, ds_file_group->ctf_fs_trace->path->str);
366 }
367
368 /*
369 * For the stream class, use the id if present. We can omit this field
370 * otherwise, as there will only be a single stream class.
371 */
372 if (ds_file_group->sc->id != UINT64_C(-1)) {
373 g_string_append_printf(name, " | %" PRIu64, ds_file_group->sc->id);
374 }
375
376 /* For the stream, use the id if present, else, use the path. */
377 if (ds_file_group->stream_id != UINT64_C(-1)) {
378 g_string_append_printf(name, " | %" PRIu64, ds_file_group->stream_id);
379 } else {
380 BT_ASSERT(ds_file_group->ds_file_infos.size() == 1);
381 const ctf_fs_ds_file_info& ds_file_info = *ds_file_group->ds_file_infos[0];
382 g_string_append_printf(name, " | %s", ds_file_info.path.c_str());
383 }
384
385 return bt2_common::GCharUP {g_string_free(name, FALSE)};
386 }
387
388 static int create_one_port_for_trace(struct ctf_fs_component *ctf_fs,
389 struct ctf_fs_trace *ctf_fs_trace,
390 struct ctf_fs_ds_file_group *ds_file_group,
391 bt_self_component_source *self_comp_src)
392 {
393 int ret = 0;
394 ctf_fs_port_data::UP port_data;
395 const bt2_common::LogCfg& logCfg = ctf_fs->logCfg;
396
397 bt2_common::GCharUP port_name = ctf_fs_make_port_name(ds_file_group);
398 if (!port_name) {
399 goto error;
400 }
401
402 BT_CLOGI("Creating one port named `%s`", port_name.get());
403
404 /* Create output port for this file */
405 port_data = bt2_common::makeUnique<ctf_fs_port_data>();
406 port_data->ctf_fs = ctf_fs;
407 port_data->ds_file_group = ds_file_group;
408 ret = bt_self_component_source_add_output_port(self_comp_src, port_name.get(), port_data.get(),
409 NULL);
410 if (ret) {
411 goto error;
412 }
413
414 ctf_fs->port_data.emplace_back(std::move(port_data));
415 goto end;
416
417 error:
418 ret = -1;
419
420 end:
421 return ret;
422 }
423
424 static int create_ports_for_trace(struct ctf_fs_component *ctf_fs,
425 struct ctf_fs_trace *ctf_fs_trace,
426 bt_self_component_source *self_comp_src)
427 {
428 int ret = 0;
429 const bt2_common::LogCfg& logCfg = ctf_fs->logCfg;
430
431 /* Create one output port for each stream file group */
432 for (ctf_fs_ds_file_group::UP& ds_file_group : ctf_fs_trace->ds_file_groups) {
433 ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace, ds_file_group.get(), self_comp_src);
434 if (ret) {
435 BT_CLOGE_APPEND_CAUSE("Cannot create output port.");
436 goto end;
437 }
438 }
439
440 end:
441 return ret;
442 }
443
444 /* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
445 static void array_insert(GPtrArray *array, gpointer element, size_t pos)
446 {
447 size_t original_array_len = array->len;
448
449 /* Allocate an unused element at the end of the array. */
450 g_ptr_array_add(array, NULL);
451
452 /* If we are not inserting at the end, move the elements by one. */
453 if (pos < original_array_len) {
454 memmove(&(array->pdata[pos + 1]), &(array->pdata[pos]),
455 (original_array_len - pos) * sizeof(gpointer));
456 }
457
458 /* Insert the value. */
459 array->pdata[pos] = element;
460 }
461
462 /*
463 * Insert ds_file_info in ds_file_group's list of ds_file_infos at the right
464 * place to keep it sorted.
465 */
466
467 static void ds_file_group_insert_ds_file_info_sorted(struct ctf_fs_ds_file_group *ds_file_group,
468 ctf_fs_ds_file_info::UP ds_file_info)
469 {
470 /* Find the spot where to insert this ds_file_info. */
471 auto it = ds_file_group->ds_file_infos.begin();
472
473 for (; it != ds_file_group->ds_file_infos.end(); ++it) {
474 const ctf_fs_ds_file_info& other_ds_file_info = **it;
475
476 if (ds_file_info->begin_ns < other_ds_file_info.begin_ns) {
477 break;
478 }
479 }
480
481 ds_file_group->ds_file_infos.insert(it, std::move(ds_file_info));
482 }
483
484 static bool ds_index_entries_equal(const struct ctf_fs_ds_index_entry *left,
485 const struct ctf_fs_ds_index_entry *right)
486 {
487 if (left->packetSize != right->packetSize) {
488 return false;
489 }
490
491 if (left->timestamp_begin != right->timestamp_begin) {
492 return false;
493 }
494
495 if (left->timestamp_end != right->timestamp_end) {
496 return false;
497 }
498
499 if (left->packet_seq_num != right->packet_seq_num) {
500 return false;
501 }
502
503 return true;
504 }
505
506 /*
507 * Insert `entry` into `index`, without duplication.
508 *
509 * The entry is inserted only if there isn't an identical entry already.
510 *
511 * In any case, the ownership of `entry` is transferred to this function. So if
512 * the entry is not inserted, it is freed.
513 */
514
515 static void ds_index_insert_ds_index_entry_sorted(struct ctf_fs_ds_index *index,
516 struct ctf_fs_ds_index_entry *entry)
517 {
518 guint i;
519 struct ctf_fs_ds_index_entry *other_entry = NULL;
520
521 /* Find the spot where to insert this index entry. */
522 for (i = 0; i < index->entries->len; i++) {
523 other_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, i);
524
525 if (entry->timestamp_begin_ns <= other_entry->timestamp_begin_ns) {
526 break;
527 }
528 }
529
530 /*
531 * Insert the entry only if a duplicate doesn't already exist.
532 *
533 * There can be duplicate packets if reading multiple overlapping
534 * snapshots of the same trace. We then want the index to contain
535 * a reference to only one copy of that packet.
536 */
537 if (i == index->entries->len || !ds_index_entries_equal(entry, other_entry)) {
538 array_insert(index->entries, entry, i);
539 } else {
540 delete entry;
541 }
542 }
543
544 static void merge_ctf_fs_ds_indexes(struct ctf_fs_ds_index *dest, struct ctf_fs_ds_index *src)
545 {
546 guint i;
547
548 for (i = 0; i < src->entries->len; i++) {
549 struct ctf_fs_ds_index_entry *entry =
550 (struct ctf_fs_ds_index_entry *) g_ptr_array_index(src->entries, i);
551
552 /*
553 * Ownership of the ctf_fs_ds_index_entry is transferred to
554 * ds_index_insert_ds_index_entry_sorted.
555 */
556 g_ptr_array_index(src->entries, i) = NULL;
557 ds_index_insert_ds_index_entry_sorted(dest, entry);
558 }
559 }
560
561 static int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, const char *path)
562 {
563 int64_t stream_instance_id = -1;
564 int64_t begin_ns = -1;
565 struct ctf_fs_ds_file_group *ds_file_group = NULL;
566 ctf_fs_ds_file_group::UP new_ds_file_group;
567 int ret;
568 struct ctf_fs_ds_file *ds_file = NULL;
569 ctf_fs_ds_file_info::UP ds_file_info;
570 struct ctf_fs_ds_index *index = NULL;
571 struct ctf_msg_iter *msg_iter = NULL;
572 struct ctf_stream_class *sc = NULL;
573 struct ctf_msg_iter_packet_properties props;
574 const bt2_common::LogCfg& logCfg = ctf_fs_trace->logCfg;
575
576 /*
577 * Create a temporary ds_file to read some properties about the data
578 * stream file.
579 */
580 ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, path, logCfg);
581 if (!ds_file) {
582 goto error;
583 }
584
585 /* Create a temporary iterator to read the ds_file. */
586 msg_iter = ctf_msg_iter_create(ctf_fs_trace->metadata->tc,
587 bt_common_get_page_size(logCfg.logLevel()) * 8,
588 ctf_fs_ds_file_medops, ds_file, nullptr, logCfg);
589 if (!msg_iter) {
590 BT_CLOGE_STR("Cannot create a CTF message iterator.");
591 goto error;
592 }
593
594 ctf_msg_iter_set_dry_run(msg_iter, true);
595
596 ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
597 if (ret) {
598 BT_CLOGE_APPEND_CAUSE(
599 "Cannot get stream file's first packet's header and context fields (`%s`).", path);
600 goto error;
601 }
602
603 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props.stream_class_id);
604 BT_ASSERT(sc);
605 stream_instance_id = props.data_stream_id;
606
607 if (props.snapshots.beginning_clock != UINT64_C(-1)) {
608 BT_ASSERT(sc->default_clock_class);
609 ret = bt_util_clock_cycles_to_ns_from_origin(
610 props.snapshots.beginning_clock, sc->default_clock_class->frequency,
611 sc->default_clock_class->offset_seconds, sc->default_clock_class->offset_cycles,
612 &begin_ns);
613 if (ret) {
614 BT_CLOGE_APPEND_CAUSE("Cannot convert clock cycles to nanoseconds from origin (`%s`).",
615 path);
616 goto error;
617 }
618 }
619
620 ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns);
621 if (!ds_file_info) {
622 goto error;
623 }
624
625 index = ctf_fs_ds_file_build_index(ds_file, ds_file_info.get(), msg_iter).release();
626 if (!index) {
627 BT_CLOGE_APPEND_CAUSE("Failed to index CTF stream file \'%s\'", ds_file->file->path->str);
628 goto error;
629 }
630
631 if (begin_ns == -1) {
632 /*
633 * No beginning timestamp to sort the stream files
634 * within a stream file group, so consider that this
635 * file must be the only one within its group.
636 */
637 stream_instance_id = -1;
638 }
639
640 if (stream_instance_id == -1) {
641 /*
642 * No stream instance ID or no beginning timestamp:
643 * create a unique stream file group for this stream
644 * file because, even if there's a stream instance ID,
645 * there's no timestamp to order the file within its
646 * group.
647 */
648 new_ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, sc, UINT64_C(-1), index);
649 /* Ownership of index is transferred. */
650 index = NULL;
651
652 if (!new_ds_file_group) {
653 goto error;
654 }
655
656 ds_file_group_insert_ds_file_info_sorted(new_ds_file_group.get(), std::move(ds_file_info));
657 ctf_fs_trace->ds_file_groups.emplace_back(std::move(new_ds_file_group));
658 goto end;
659 }
660
661 BT_ASSERT(stream_instance_id != -1);
662 BT_ASSERT(begin_ns != -1);
663
664 /* Find an existing stream file group with this ID */
665 for (ctf_fs_ds_file_group::UP& candidate : ctf_fs_trace->ds_file_groups) {
666 if (candidate->sc == sc && candidate->stream_id == stream_instance_id) {
667 ds_file_group = candidate.get();
668 break;
669 }
670 }
671
672 if (!ds_file_group) {
673 new_ds_file_group =
674 ctf_fs_ds_file_group_create(ctf_fs_trace, sc, stream_instance_id, index);
675 /* Ownership of index is transferred. */
676 index = NULL;
677 if (!new_ds_file_group) {
678 goto error;
679 }
680
681 ds_file_group = new_ds_file_group.get();
682 ctf_fs_trace->ds_file_groups.emplace_back(std::move(new_ds_file_group));
683 } else {
684 merge_ctf_fs_ds_indexes(ds_file_group->index, index);
685 }
686
687 ds_file_group_insert_ds_file_info_sorted(ds_file_group, std::move(ds_file_info));
688
689 goto end;
690
691 error:
692 ret = -1;
693
694 end:
695 ctf_fs_ds_file_destroy(ds_file);
696
697 if (msg_iter) {
698 ctf_msg_iter_destroy(msg_iter);
699 }
700
701 ctf_fs_ds_index_destroy(index);
702 return ret;
703 }
704
705 static int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace)
706 {
707 int ret = 0;
708 const char *basename;
709 GError *error = NULL;
710 GDir *dir = NULL;
711 const bt2_common::LogCfg& logCfg = ctf_fs_trace->logCfg;
712
713 /* Check each file in the path directory, except specific ones */
714 dir = g_dir_open(ctf_fs_trace->path->str, 0, &error);
715 if (!dir) {
716 BT_CLOGE_APPEND_CAUSE("Cannot open directory `%s`: %s (code %d)", ctf_fs_trace->path->str,
717 error->message, error->code);
718 goto error;
719 }
720
721 while ((basename = g_dir_read_name(dir))) {
722 struct ctf_fs_file *file;
723
724 if (strcmp(basename, CTF_FS_METADATA_FILENAME) == 0) {
725 /* Ignore the metadata stream. */
726 BT_CLOGI("Ignoring metadata file `%s" G_DIR_SEPARATOR_S "%s`", ctf_fs_trace->path->str,
727 basename);
728 continue;
729 }
730
731 if (basename[0] == '.') {
732 BT_CLOGI("Ignoring hidden file `%s" G_DIR_SEPARATOR_S "%s`", ctf_fs_trace->path->str,
733 basename);
734 continue;
735 }
736
737 /* Create the file. */
738 file = ctf_fs_file_create(logCfg);
739 if (!file) {
740 BT_CLOGE_APPEND_CAUSE("Cannot create stream file object for file `%s" G_DIR_SEPARATOR_S
741 "%s`",
742 ctf_fs_trace->path->str, basename);
743 goto error;
744 }
745
746 /* Create full path string. */
747 g_string_append_printf(file->path, "%s" G_DIR_SEPARATOR_S "%s", ctf_fs_trace->path->str,
748 basename);
749 if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) {
750 BT_CLOGI("Ignoring non-regular file `%s`", file->path->str);
751 ctf_fs_file_destroy(file);
752 file = NULL;
753 continue;
754 }
755
756 ret = ctf_fs_file_open(file, "rb");
757 if (ret) {
758 BT_CLOGE_APPEND_CAUSE("Cannot open stream file `%s`", file->path->str);
759 goto error;
760 }
761
762 if (file->size == 0) {
763 /* Skip empty stream. */
764 BT_CLOGI("Ignoring empty file `%s`", file->path->str);
765 ctf_fs_file_destroy(file);
766 continue;
767 }
768
769 ret = add_ds_file_to_ds_file_group(ctf_fs_trace, file->path->str);
770 if (ret) {
771 BT_CLOGE_APPEND_CAUSE("Cannot add stream file `%s` to stream file group",
772 file->path->str);
773 ctf_fs_file_destroy(file);
774 goto error;
775 }
776
777 ctf_fs_file_destroy(file);
778 }
779
780 goto end;
781
782 error:
783 ret = -1;
784
785 end:
786 if (dir) {
787 g_dir_close(dir);
788 dir = NULL;
789 }
790
791 if (error) {
792 g_error_free(error);
793 }
794
795 return ret;
796 }
797
798 static int set_trace_name(bt_trace *trace, const char *name_suffix,
799 const bt2_common::LogCfg& logCfg)
800 {
801 int ret = 0;
802 const bt_value *val;
803 GString *name;
804
805 name = g_string_new(NULL);
806 if (!name) {
807 BT_CLOGE_STR("Failed to allocate a GString.");
808 ret = -1;
809 goto end;
810 }
811
812 /*
813 * Check if we have a trace environment string value named `hostname`.
814 * If so, use it as the trace name's prefix.
815 */
816 val = bt_trace_borrow_environment_entry_value_by_name_const(trace, "hostname");
817 if (val && bt_value_is_string(val)) {
818 g_string_append(name, bt_value_string_get(val));
819
820 if (name_suffix) {
821 g_string_append_c(name, G_DIR_SEPARATOR);
822 }
823 }
824
825 if (name_suffix) {
826 g_string_append(name, name_suffix);
827 }
828
829 ret = bt_trace_set_name(trace, name->str);
830 if (ret) {
831 goto end;
832 }
833
834 goto end;
835
836 end:
837 if (name) {
838 g_string_free(name, TRUE);
839 }
840
841 return ret;
842 }
843
844 static ctf_fs_trace::UP ctf_fs_trace_create(const char *path, const char *name,
845 ctf::src::ClkClsCfg clkClsCfg,
846 bt_self_component *selfComp,
847 const bt2_common::LogCfg& logCfg)
848 {
849 int ret;
850
851 ctf_fs_trace::UP ctf_fs_trace {new struct ctf_fs_trace(logCfg)};
852 ctf_fs_trace->path = g_string_new(path);
853 if (!ctf_fs_trace->path) {
854 goto error;
855 }
856
857 ctf_fs_trace->metadata = new ctf_fs_metadata;
858 ctf_fs_metadata_init(ctf_fs_trace->metadata);
859
860 ret = ctf_fs_metadata_set_trace_class(ctf_fs_trace.get(), clkClsCfg, selfComp, logCfg);
861 if (ret) {
862 goto error;
863 }
864
865 if (ctf_fs_trace->metadata->trace_class) {
866 ctf_fs_trace->trace = bt_trace_create(ctf_fs_trace->metadata->trace_class);
867 if (!ctf_fs_trace->trace) {
868 goto error;
869 }
870 }
871
872 if (ctf_fs_trace->trace) {
873 ret = ctf_trace_class_configure_ir_trace(ctf_fs_trace->metadata->tc, ctf_fs_trace->trace);
874 if (ret) {
875 goto error;
876 }
877
878 ret = set_trace_name(ctf_fs_trace->trace, name, logCfg);
879 if (ret) {
880 goto error;
881 }
882 }
883
884 ret = create_ds_file_groups(ctf_fs_trace.get());
885 if (ret) {
886 goto error;
887 }
888
889 goto end;
890
891 error:
892 ctf_fs_trace.reset();
893
894 end:
895 return ctf_fs_trace;
896 }
897
898 static int path_is_ctf_trace(const char *path)
899 {
900 GString *metadata_path = g_string_new(NULL);
901 int ret = 0;
902
903 if (!metadata_path) {
904 ret = -1;
905 goto end;
906 }
907
908 g_string_printf(metadata_path, "%s" G_DIR_SEPARATOR_S "%s", path, CTF_FS_METADATA_FILENAME);
909
910 if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) {
911 ret = 1;
912 goto end;
913 }
914
915 end:
916 g_string_free(metadata_path, TRUE);
917 return ret;
918 }
919
920 /* Helper for ctf_fs_component_create_ctf_fs_trace, to handle a single path. */
921
922 static int ctf_fs_component_create_ctf_fs_trace_one_path(struct ctf_fs_component *ctf_fs,
923 const char *path_param,
924 const char *trace_name,
925 std::vector<ctf_fs_trace::UP>& traces,
926 bt_self_component *selfComp)
927 {
928 ctf_fs_trace::UP ctf_fs_trace;
929 int ret;
930 GString *norm_path;
931 const bt2_common::LogCfg& logCfg = ctf_fs->logCfg;
932
933 norm_path = bt_common_normalize_path(path_param, NULL);
934 if (!norm_path) {
935 BT_CLOGE_APPEND_CAUSE("Failed to normalize path: `%s`.", path_param);
936 goto error;
937 }
938
939 ret = path_is_ctf_trace(norm_path->str);
940 if (ret < 0) {
941 BT_CLOGE_APPEND_CAUSE("Failed to check if path is a CTF trace: path=%s", norm_path->str);
942 goto error;
943 } else if (ret == 0) {
944 BT_CLOGE_APPEND_CAUSE("Path is not a CTF trace (does not contain a metadata file): `%s`.",
945 norm_path->str);
946 goto error;
947 }
948
949 // FIXME: Remove or ifdef for __MINGW32__
950 if (strcmp(norm_path->str, "/") == 0) {
951 BT_CLOGE_APPEND_CAUSE("Opening a trace in `/` is not supported.");
952 ret = -1;
953 goto end;
954 }
955
956 ctf_fs_trace =
957 ctf_fs_trace_create(norm_path->str, trace_name, ctf_fs->clkClsCfg, selfComp, logCfg);
958 if (!ctf_fs_trace) {
959 BT_CLOGE_APPEND_CAUSE("Cannot create trace for `%s`.", norm_path->str);
960 goto error;
961 }
962
963 traces.emplace_back(std::move(ctf_fs_trace));
964
965 ret = 0;
966 goto end;
967
968 error:
969 ret = -1;
970
971 end:
972 if (norm_path) {
973 g_string_free(norm_path, TRUE);
974 }
975
976 return ret;
977 }
978
979 /*
980 * Count the number of stream and event classes defined by this trace's metadata.
981 *
982 * This is used to determine which metadata is the "latest", out of multiple
983 * traces sharing the same UUID. It is assumed that amongst all these metadatas,
984 * a bigger metadata is a superset of a smaller metadata. Therefore, it is
985 * enough to just count the classes.
986 */
987
988 static unsigned int metadata_count_stream_and_event_classes(struct ctf_fs_trace *trace)
989 {
990 unsigned int num = trace->metadata->tc->stream_classes->len;
991 guint i;
992
993 for (i = 0; i < trace->metadata->tc->stream_classes->len; i++) {
994 struct ctf_stream_class *sc =
995 (struct ctf_stream_class *) trace->metadata->tc->stream_classes->pdata[i];
996 num += sc->event_classes->len;
997 }
998
999 return num;
1000 }
1001
1002 /*
1003 * Merge the src ds_file_group into dest. This consists of merging their
1004 * ds_file_infos, making sure to keep the result sorted.
1005 */
1006
1007 static void merge_ctf_fs_ds_file_groups(struct ctf_fs_ds_file_group *dest,
1008 ctf_fs_ds_file_group::UP src)
1009 {
1010 for (ctf_fs_ds_file_info::UP& ds_file_info : src->ds_file_infos) {
1011 ds_file_group_insert_ds_file_info_sorted(dest, std::move(ds_file_info));
1012 }
1013
1014 /* Merge both indexes. */
1015 merge_ctf_fs_ds_indexes(dest->index, src->index);
1016 }
1017
1018 /* Merge src_trace's data stream file groups into dest_trace's. */
1019
1020 static int merge_matching_ctf_fs_ds_file_groups(struct ctf_fs_trace *dest_trace,
1021 ctf_fs_trace::UP src_trace)
1022 {
1023 std::vector<ctf_fs_ds_file_group::UP>& dest = dest_trace->ds_file_groups;
1024 std::vector<ctf_fs_ds_file_group::UP>& src = src_trace->ds_file_groups;
1025 int ret = 0;
1026
1027 /*
1028 * Save the initial length of dest: we only want to check against the
1029 * original elements in the inner loop.
1030 */
1031 size_t dest_len = dest.size();
1032
1033 for (ctf_fs_ds_file_group::UP& src_group : src) {
1034 struct ctf_fs_ds_file_group *dest_group = NULL;
1035
1036 /* A stream instance without ID can't match a stream in the other trace. */
1037 if (src_group->stream_id != -1) {
1038 /* Let's search for a matching ds_file_group in the destination. */
1039 for (size_t d_i = 0; d_i < dest_len; ++d_i) {
1040 ctf_fs_ds_file_group *candidate_dest = dest[d_i].get();
1041
1042 /* Can't match a stream instance without ID. */
1043 if (candidate_dest->stream_id == -1) {
1044 continue;
1045 }
1046
1047 /*
1048 * If the two groups have the same stream instance id
1049 * and belong to the same stream class (stream instance
1050 * ids are per-stream class), they represent the same
1051 * stream instance.
1052 */
1053 if (candidate_dest->stream_id != src_group->stream_id ||
1054 candidate_dest->sc->id != src_group->sc->id) {
1055 continue;
1056 }
1057
1058 dest_group = candidate_dest;
1059 break;
1060 }
1061 }
1062
1063 /*
1064 * Didn't find a friend in dest to merge our src_group into?
1065 * Create a new empty one. This can happen if a stream was
1066 * active in the source trace chunk but not in the destination
1067 * trace chunk.
1068 */
1069 if (!dest_group) {
1070 struct ctf_stream_class *sc;
1071 struct ctf_fs_ds_index *index;
1072
1073 sc = ctf_trace_class_borrow_stream_class_by_id(dest_trace->metadata->tc,
1074 src_group->sc->id);
1075 BT_ASSERT(sc);
1076
1077 index = ctf_fs_ds_index_create(dest_trace->logCfg).release();
1078 if (!index) {
1079 ret = -1;
1080 goto end;
1081 }
1082
1083 ctf_fs_ds_file_group::UP new_dest_group =
1084 ctf_fs_ds_file_group_create(dest_trace, sc, src_group->stream_id, index);
1085 /* Ownership of index is transferred. */
1086 index = NULL;
1087 if (!new_dest_group) {
1088 ret = -1;
1089 goto end;
1090 }
1091
1092 dest_group = new_dest_group.get();
1093 dest_trace->ds_file_groups.emplace_back(std::move(new_dest_group));
1094 }
1095
1096 BT_ASSERT(dest_group);
1097 merge_ctf_fs_ds_file_groups(dest_group, std::move(src_group));
1098 }
1099
1100 end:
1101 return ret;
1102 }
1103
1104 /*
1105 * Collapse the given traces, which must all share the same UUID, in a single
1106 * one.
1107 *
1108 * The trace with the most expansive metadata is chosen and all other traces
1109 * are merged into that one. On return, the elements of `traces` are nullptr
1110 * and the merged trace is placed in `out_trace`.
1111 */
1112
1113 static int merge_ctf_fs_traces(std::vector<ctf_fs_trace::UP> traces, ctf_fs_trace::UP& out_trace)
1114 {
1115 unsigned int winner_count;
1116 struct ctf_fs_trace *winner;
1117 guint i, winner_i;
1118 int ret = 0;
1119
1120 BT_ASSERT(traces.size() >= 2);
1121
1122 winner_count = metadata_count_stream_and_event_classes(traces[0].get());
1123 winner = traces[0].get();
1124 winner_i = 0;
1125
1126 /* Find the trace with the largest metadata. */
1127 for (i = 1; i < traces.size(); i++) {
1128 ctf_fs_trace *candidate = traces[i].get();
1129 unsigned int candidate_count;
1130
1131 /* A bit of sanity check. */
1132 BT_ASSERT(bt_uuid_compare(winner->metadata->tc->uuid, candidate->metadata->tc->uuid) == 0);
1133
1134 candidate_count = metadata_count_stream_and_event_classes(candidate);
1135
1136 if (candidate_count > winner_count) {
1137 winner_count = candidate_count;
1138 winner = candidate;
1139 winner_i = i;
1140 }
1141 }
1142
1143 /* Merge all the other traces in the winning trace. */
1144 for (ctf_fs_trace::UP& trace : traces) {
1145 /* Don't merge the winner into itself. */
1146 if (trace.get() == winner) {
1147 continue;
1148 }
1149
1150 /* Merge trace's data stream file groups into winner's. */
1151 ret = merge_matching_ctf_fs_ds_file_groups(winner, std::move(trace));
1152 if (ret) {
1153 goto end;
1154 }
1155 }
1156
1157 /*
1158 * Move the winner out of the array, into `*out_trace`.
1159 */
1160 out_trace = std::move(traces[winner_i]);
1161
1162 end:
1163 return ret;
1164 }
1165
1166 enum target_event
1167 {
1168 FIRST_EVENT,
1169 LAST_EVENT,
1170 };
1171
1172 static int decode_clock_snapshot_after_event(struct ctf_fs_trace *ctf_fs_trace,
1173 struct ctf_clock_class *default_cc,
1174 struct ctf_fs_ds_index_entry *index_entry,
1175 enum target_event target_event, uint64_t *cs,
1176 int64_t *ts_ns)
1177 {
1178 enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK;
1179 struct ctf_fs_ds_file *ds_file = NULL;
1180 struct ctf_msg_iter *msg_iter = NULL;
1181 const bt2_common::LogCfg& logCfg = ctf_fs_trace->logCfg;
1182 int ret = 0;
1183
1184 BT_ASSERT(ctf_fs_trace);
1185 BT_ASSERT(index_entry);
1186 BT_ASSERT(index_entry->path);
1187
1188 ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, index_entry->path, logCfg);
1189 if (!ds_file) {
1190 BT_CLOGE_APPEND_CAUSE("Failed to create a ctf_fs_ds_file");
1191 ret = -1;
1192 goto end;
1193 }
1194
1195 BT_ASSERT(ctf_fs_trace->metadata);
1196 BT_ASSERT(ctf_fs_trace->metadata->tc);
1197
1198 msg_iter = ctf_msg_iter_create(ctf_fs_trace->metadata->tc,
1199 bt_common_get_page_size(logCfg.logLevel()) * 8,
1200 ctf_fs_ds_file_medops, ds_file, NULL, logCfg);
1201 if (!msg_iter) {
1202 /* ctf_msg_iter_create() logs errors. */
1203 ret = -1;
1204 goto end;
1205 }
1206
1207 /*
1208 * Turn on dry run mode to prevent the creation and usage of Babeltrace
1209 * library objects (bt_field, bt_message_*, etc.).
1210 */
1211 ctf_msg_iter_set_dry_run(msg_iter, true);
1212
1213 /* Seek to the beginning of the target packet. */
1214 iter_status = ctf_msg_iter_seek(msg_iter, index_entry->offset.bytes());
1215 if (iter_status) {
1216 /* ctf_msg_iter_seek() logs errors. */
1217 ret = -1;
1218 goto end;
1219 }
1220
1221 switch (target_event) {
1222 case FIRST_EVENT:
1223 /*
1224 * Start to decode the packet until we reach the end of
1225 * the first event. To extract the first event's clock
1226 * snapshot.
1227 */
1228 iter_status = ctf_msg_iter_curr_packet_first_event_clock_snapshot(msg_iter, cs);
1229 break;
1230 case LAST_EVENT:
1231 /* Decode the packet to extract the last event's clock snapshot. */
1232 iter_status = ctf_msg_iter_curr_packet_last_event_clock_snapshot(msg_iter, cs);
1233 break;
1234 default:
1235 bt_common_abort();
1236 }
1237 if (iter_status) {
1238 ret = -1;
1239 goto end;
1240 }
1241
1242 /* Convert clock snapshot to timestamp. */
1243 ret = bt_util_clock_cycles_to_ns_from_origin(
1244 *cs, default_cc->frequency, default_cc->offset_seconds, default_cc->offset_cycles, ts_ns);
1245 if (ret) {
1246 BT_CLOGE_APPEND_CAUSE("Failed to convert clock snapshot to timestamp");
1247 goto end;
1248 }
1249
1250 end:
1251 if (ds_file) {
1252 ctf_fs_ds_file_destroy(ds_file);
1253 }
1254 if (msg_iter) {
1255 ctf_msg_iter_destroy(msg_iter);
1256 }
1257
1258 return ret;
1259 }
1260
1261 static int decode_packet_first_event_timestamp(struct ctf_fs_trace *ctf_fs_trace,
1262 struct ctf_clock_class *default_cc,
1263 struct ctf_fs_ds_index_entry *index_entry,
1264 uint64_t *cs, int64_t *ts_ns)
1265 {
1266 return decode_clock_snapshot_after_event(ctf_fs_trace, default_cc, index_entry, FIRST_EVENT, cs,
1267 ts_ns);
1268 }
1269
1270 static int decode_packet_last_event_timestamp(struct ctf_fs_trace *ctf_fs_trace,
1271 struct ctf_clock_class *default_cc,
1272 struct ctf_fs_ds_index_entry *index_entry,
1273 uint64_t *cs, int64_t *ts_ns)
1274 {
1275 return decode_clock_snapshot_after_event(ctf_fs_trace, default_cc, index_entry, LAST_EVENT, cs,
1276 ts_ns);
1277 }
1278
1279 /*
1280 * Fix up packet index entries for lttng's "event-after-packet" bug.
1281 * Some buggy lttng tracer versions may emit events with a timestamp that is
1282 * larger (after) than the timestamp_end of the their packets.
1283 *
1284 * To fix up this erroneous data we do the following:
1285 * 1. If it's not the stream file's last packet: set the packet index entry's
1286 * end time to the next packet's beginning time.
1287 * 2. If it's the stream file's last packet, set the packet index entry's end
1288 * time to the packet's last event's time, if any, or to the packet's
1289 * beginning time otherwise.
1290 *
1291 * Known buggy tracer versions:
1292 * - before lttng-ust 2.11.0
1293 * - before lttng-module 2.11.0
1294 * - before lttng-module 2.10.10
1295 * - before lttng-module 2.9.13
1296 */
1297 static int fix_index_lttng_event_after_packet_bug(struct ctf_fs_trace *trace)
1298 {
1299 int ret = 0;
1300 const bt2_common::LogCfg& logCfg = trace->logCfg;
1301
1302 for (ctf_fs_ds_file_group::UP& ds_file_group : trace->ds_file_groups) {
1303 guint entry_i;
1304 struct ctf_clock_class *default_cc;
1305 struct ctf_fs_ds_index_entry *last_entry;
1306 struct ctf_fs_ds_index *index;
1307
1308 BT_ASSERT(ds_file_group);
1309 index = ds_file_group->index;
1310
1311 BT_ASSERT(index);
1312 BT_ASSERT(index->entries);
1313 BT_ASSERT(index->entries->len > 0);
1314
1315 /*
1316 * Iterate over all entries but the last one. The last one is
1317 * fixed differently after.
1318 */
1319 for (entry_i = 0; entry_i < index->entries->len - 1; entry_i++) {
1320 struct ctf_fs_ds_index_entry *curr_entry, *next_entry;
1321
1322 curr_entry = (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_i);
1323 next_entry = (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_i + 1);
1324
1325 /*
1326 * 1. Set the current index entry `end` timestamp to
1327 * the next index entry `begin` timestamp.
1328 */
1329 curr_entry->timestamp_end = next_entry->timestamp_begin;
1330 curr_entry->timestamp_end_ns = next_entry->timestamp_begin_ns;
1331 }
1332
1333 /*
1334 * 2. Fix the last entry by decoding the last event of the last
1335 * packet.
1336 */
1337 last_entry =
1338 (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, index->entries->len - 1);
1339 BT_ASSERT(last_entry);
1340
1341 BT_ASSERT(ds_file_group->sc->default_clock_class);
1342 default_cc = ds_file_group->sc->default_clock_class;
1343
1344 /*
1345 * Decode packet to read the timestamp of the last event of the
1346 * entry.
1347 */
1348 ret = decode_packet_last_event_timestamp(trace, default_cc, last_entry,
1349 &last_entry->timestamp_end,
1350 &last_entry->timestamp_end_ns);
1351 if (ret) {
1352 BT_CLOGE_APPEND_CAUSE(
1353 "Failed to decode stream's last packet to get its last event's clock snapshot.");
1354 goto end;
1355 }
1356 }
1357
1358 end:
1359 return ret;
1360 }
1361
1362 /*
1363 * Fix up packet index entries for barectf's "event-before-packet" bug.
1364 * Some buggy barectf tracer versions may emit events with a timestamp that is
1365 * less than the timestamp_begin of the their packets.
1366 *
1367 * To fix up this erroneous data we do the following:
1368 * 1. Starting at the second index entry, set the timestamp_begin of the
1369 * current entry to the timestamp of the first event of the packet.
1370 * 2. Set the previous entry's timestamp_end to the timestamp_begin of the
1371 * current packet.
1372 *
1373 * Known buggy tracer versions:
1374 * - before barectf 2.3.1
1375 */
1376 static int fix_index_barectf_event_before_packet_bug(struct ctf_fs_trace *trace)
1377 {
1378 int ret = 0;
1379 const bt2_common::LogCfg& logCfg = trace->logCfg;
1380
1381 for (ctf_fs_ds_file_group::UP& ds_file_group : trace->ds_file_groups) {
1382 guint entry_i;
1383 struct ctf_clock_class *default_cc;
1384 struct ctf_fs_ds_index *index = ds_file_group->index;
1385
1386 BT_ASSERT(index);
1387 BT_ASSERT(index->entries);
1388 BT_ASSERT(index->entries->len > 0);
1389
1390 BT_ASSERT(ds_file_group->sc->default_clock_class);
1391 default_cc = ds_file_group->sc->default_clock_class;
1392
1393 /*
1394 * 1. Iterate over the index, starting from the second entry
1395 * (index = 1).
1396 */
1397 for (entry_i = 1; entry_i < index->entries->len; entry_i++) {
1398 ctf_fs_ds_index_entry *prev_entry =
1399 (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_i - 1);
1400 ctf_fs_ds_index_entry *curr_entry =
1401 (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_i);
1402 /*
1403 * 2. Set the current entry `begin` timestamp to the
1404 * timestamp of the first event of the current packet.
1405 */
1406 ret = decode_packet_first_event_timestamp(trace, default_cc, curr_entry,
1407 &curr_entry->timestamp_begin,
1408 &curr_entry->timestamp_begin_ns);
1409 if (ret) {
1410 BT_CLOGE_APPEND_CAUSE("Failed to decode first event's clock snapshot");
1411 goto end;
1412 }
1413
1414 /*
1415 * 3. Set the previous entry `end` timestamp to the
1416 * timestamp of the first event of the current packet.
1417 */
1418 prev_entry->timestamp_end = curr_entry->timestamp_begin;
1419 prev_entry->timestamp_end_ns = curr_entry->timestamp_begin_ns;
1420 }
1421 }
1422 end:
1423 return ret;
1424 }
1425
1426 /*
1427 * When using the lttng-crash feature it's likely that the last packets of each
1428 * stream have their timestamp_end set to zero. This is caused by the fact that
1429 * the tracer crashed and was not able to properly close the packets.
1430 *
1431 * To fix up this erroneous data we do the following:
1432 * For each index entry, if the entry's timestamp_end is 0 and the
1433 * timestamp_begin is not 0:
1434 * - If it's the stream file's last packet: set the packet index entry's end
1435 * time to the packet's last event's time, if any, or to the packet's
1436 * beginning time otherwise.
1437 * - If it's not the stream file's last packet: set the packet index
1438 * entry's end time to the next packet's beginning time.
1439 *
1440 * Affected versions:
1441 * - All current and future lttng-ust and lttng-modules versions.
1442 */
1443 static int fix_index_lttng_crash_quirk(struct ctf_fs_trace *trace)
1444 {
1445 int ret = 0;
1446 const bt2_common::LogCfg& logCfg = trace->logCfg;
1447
1448 for (ctf_fs_ds_file_group::UP& ds_file_group : trace->ds_file_groups) {
1449 guint entry_idx;
1450 struct ctf_clock_class *default_cc;
1451 struct ctf_fs_ds_index *index;
1452
1453 BT_ASSERT(ds_file_group);
1454 index = ds_file_group->index;
1455
1456 BT_ASSERT(ds_file_group->sc->default_clock_class);
1457 default_cc = ds_file_group->sc->default_clock_class;
1458
1459 BT_ASSERT(index);
1460 BT_ASSERT(index->entries);
1461 BT_ASSERT(index->entries->len > 0);
1462
1463 ctf_fs_ds_index_entry *last_entry =
1464 (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, index->entries->len - 1);
1465 BT_ASSERT(last_entry);
1466
1467 /* 1. Fix the last entry first. */
1468 if (last_entry->timestamp_end == 0 && last_entry->timestamp_begin != 0) {
1469 /*
1470 * Decode packet to read the timestamp of the
1471 * last event of the stream file.
1472 */
1473 ret = decode_packet_last_event_timestamp(trace, default_cc, last_entry,
1474 &last_entry->timestamp_end,
1475 &last_entry->timestamp_end_ns);
1476 if (ret) {
1477 BT_CLOGE_APPEND_CAUSE("Failed to decode last event's clock snapshot");
1478 goto end;
1479 }
1480 }
1481
1482 /* Iterate over all entries but the last one. */
1483 for (entry_idx = 0; entry_idx < index->entries->len - 1; entry_idx++) {
1484 ctf_fs_ds_index_entry *curr_entry =
1485 (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_idx);
1486 ctf_fs_ds_index_entry *next_entry =
1487 (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_idx + 1);
1488
1489 if (curr_entry->timestamp_end == 0 && curr_entry->timestamp_begin != 0) {
1490 /*
1491 * 2. Set the current index entry `end` timestamp to
1492 * the next index entry `begin` timestamp.
1493 */
1494 curr_entry->timestamp_end = next_entry->timestamp_begin;
1495 curr_entry->timestamp_end_ns = next_entry->timestamp_begin_ns;
1496 }
1497 }
1498 }
1499
1500 end:
1501 return ret;
1502 }
1503
1504 /*
1505 * Extract the tracer information necessary to compare versions.
1506 * Returns 0 on success, and -1 if the extraction is not successful because the
1507 * necessary fields are absents in the trace metadata.
1508 */
1509 static int extract_tracer_info(struct ctf_fs_trace *trace, struct tracer_info *current_tracer_info)
1510 {
1511 int ret = 0;
1512 struct ctf_trace_class_env_entry *entry;
1513
1514 /* Clear the current_tracer_info struct */
1515 memset(current_tracer_info, 0, sizeof(*current_tracer_info));
1516
1517 /*
1518 * To compare 2 tracer versions, at least the tracer name and it's
1519 * major version are needed. If one of these is missing, consider it an
1520 * extraction failure.
1521 */
1522 entry = ctf_trace_class_borrow_env_entry_by_name(trace->metadata->tc, "tracer_name");
1523 if (!entry || entry->type != CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR) {
1524 goto missing_bare_minimum;
1525 }
1526
1527 /* Set tracer name. */
1528 current_tracer_info->name = entry->value.str->str;
1529
1530 entry = ctf_trace_class_borrow_env_entry_by_name(trace->metadata->tc, "tracer_major");
1531 if (!entry || entry->type != CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT) {
1532 goto missing_bare_minimum;
1533 }
1534
1535 /* Set major version number. */
1536 current_tracer_info->major = entry->value.i;
1537
1538 entry = ctf_trace_class_borrow_env_entry_by_name(trace->metadata->tc, "tracer_minor");
1539 if (!entry || entry->type != CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT) {
1540 goto end;
1541 }
1542
1543 /* Set minor version number. */
1544 current_tracer_info->minor = entry->value.i;
1545
1546 entry = ctf_trace_class_borrow_env_entry_by_name(trace->metadata->tc, "tracer_patch");
1547 if (!entry) {
1548 /*
1549 * If `tracer_patch` doesn't exist `tracer_patchlevel` might.
1550 * For example, `lttng-modules` uses entry name
1551 * `tracer_patchlevel`.
1552 */
1553 entry = ctf_trace_class_borrow_env_entry_by_name(trace->metadata->tc, "tracer_patchlevel");
1554 }
1555
1556 if (!entry || entry->type != CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT) {
1557 goto end;
1558 }
1559
1560 /* Set patch version number. */
1561 current_tracer_info->patch = entry->value.i;
1562
1563 goto end;
1564
1565 missing_bare_minimum:
1566 ret = -1;
1567 end:
1568 return ret;
1569 }
1570
1571 static bool is_tracer_affected_by_lttng_event_after_packet_bug(struct tracer_info *curr_tracer_info)
1572 {
1573 bool is_affected = false;
1574
1575 if (strcmp(curr_tracer_info->name, "lttng-ust") == 0) {
1576 if (curr_tracer_info->major < 2) {
1577 is_affected = true;
1578 } else if (curr_tracer_info->major == 2) {
1579 /* fixed in lttng-ust 2.11.0 */
1580 if (curr_tracer_info->minor < 11) {
1581 is_affected = true;
1582 }
1583 }
1584 } else if (strcmp(curr_tracer_info->name, "lttng-modules") == 0) {
1585 if (curr_tracer_info->major < 2) {
1586 is_affected = true;
1587 } else if (curr_tracer_info->major == 2) {
1588 /* fixed in lttng-modules 2.11.0 */
1589 if (curr_tracer_info->minor == 10) {
1590 /* fixed in lttng-modules 2.10.10 */
1591 if (curr_tracer_info->patch < 10) {
1592 is_affected = true;
1593 }
1594 } else if (curr_tracer_info->minor == 9) {
1595 /* fixed in lttng-modules 2.9.13 */
1596 if (curr_tracer_info->patch < 13) {
1597 is_affected = true;
1598 }
1599 } else if (curr_tracer_info->minor < 9) {
1600 is_affected = true;
1601 }
1602 }
1603 }
1604
1605 return is_affected;
1606 }
1607
1608 static bool
1609 is_tracer_affected_by_barectf_event_before_packet_bug(struct tracer_info *curr_tracer_info)
1610 {
1611 bool is_affected = false;
1612
1613 if (strcmp(curr_tracer_info->name, "barectf") == 0) {
1614 if (curr_tracer_info->major < 2) {
1615 is_affected = true;
1616 } else if (curr_tracer_info->major == 2) {
1617 if (curr_tracer_info->minor < 3) {
1618 is_affected = true;
1619 } else if (curr_tracer_info->minor == 3) {
1620 /* fixed in barectf 2.3.1 */
1621 if (curr_tracer_info->patch < 1) {
1622 is_affected = true;
1623 }
1624 }
1625 }
1626 }
1627
1628 return is_affected;
1629 }
1630
1631 static bool is_tracer_affected_by_lttng_crash_quirk(struct tracer_info *curr_tracer_info)
1632 {
1633 bool is_affected = false;
1634
1635 /* All LTTng tracer may be affected by this lttng crash quirk. */
1636 if (strcmp(curr_tracer_info->name, "lttng-ust") == 0) {
1637 is_affected = true;
1638 } else if (strcmp(curr_tracer_info->name, "lttng-modules") == 0) {
1639 is_affected = true;
1640 }
1641
1642 return is_affected;
1643 }
1644
1645 /*
1646 * Looks for trace produced by known buggy tracers and fix up the index
1647 * produced earlier.
1648 */
1649 static int fix_packet_index_tracer_bugs(struct ctf_fs_component *ctf_fs)
1650 {
1651 int ret = 0;
1652 struct tracer_info current_tracer_info;
1653 const bt2_common::LogCfg& logCfg = ctf_fs->logCfg;
1654
1655 ret = extract_tracer_info(ctf_fs->trace.get(), &current_tracer_info);
1656 if (ret) {
1657 /*
1658 * A trace may not have all the necessary environment
1659 * entries to do the tracer version comparison.
1660 * At least, the tracer name and major version number
1661 * are needed. Failing to extract these entries is not
1662 * an error.
1663 */
1664 ret = 0;
1665 BT_LOGI_STR("Cannot extract tracer information necessary to compare with buggy versions.");
1666 goto end;
1667 ;
1668 }
1669
1670 /* Check if the trace may be affected by old tracer bugs. */
1671 if (is_tracer_affected_by_lttng_event_after_packet_bug(&current_tracer_info)) {
1672 BT_LOGI_STR("Trace may be affected by LTTng tracer packet timestamp bug. Fixing up.");
1673 ret = fix_index_lttng_event_after_packet_bug(ctf_fs->trace.get());
1674 if (ret) {
1675 BT_CLOGE_APPEND_CAUSE("Failed to fix LTTng event-after-packet bug.");
1676 goto end;
1677 }
1678 ctf_fs->trace->metadata->tc->quirks.lttng_event_after_packet = true;
1679 }
1680
1681 if (is_tracer_affected_by_barectf_event_before_packet_bug(&current_tracer_info)) {
1682 BT_LOGI_STR("Trace may be affected by barectf tracer packet timestamp bug. Fixing up.");
1683 ret = fix_index_barectf_event_before_packet_bug(ctf_fs->trace.get());
1684 if (ret) {
1685 BT_CLOGE_APPEND_CAUSE("Failed to fix barectf event-before-packet bug.");
1686 goto end;
1687 }
1688 ctf_fs->trace->metadata->tc->quirks.barectf_event_before_packet = true;
1689 }
1690
1691 if (is_tracer_affected_by_lttng_crash_quirk(&current_tracer_info)) {
1692 ret = fix_index_lttng_crash_quirk(ctf_fs->trace.get());
1693 if (ret) {
1694 BT_CLOGE_APPEND_CAUSE("Failed to fix lttng-crash timestamp quirks.");
1695 goto end;
1696 }
1697 ctf_fs->trace->metadata->tc->quirks.lttng_crash = true;
1698 }
1699
1700 end:
1701 return ret;
1702 }
1703
1704 static bool compare_ds_file_groups_by_first_path(const ctf_fs_ds_file_group::UP& ds_file_group_a,
1705 const ctf_fs_ds_file_group::UP& ds_file_group_b)
1706 {
1707 BT_ASSERT(!ds_file_group_a->ds_file_infos.empty());
1708 BT_ASSERT(!ds_file_group_b->ds_file_infos.empty());
1709
1710 const ctf_fs_ds_file_info& first_ds_file_info_a = *ds_file_group_a->ds_file_infos[0];
1711 const ctf_fs_ds_file_info& first_ds_file_info_b = *ds_file_group_b->ds_file_infos[0];
1712
1713 return first_ds_file_info_a.path < first_ds_file_info_b.path;
1714 }
1715
1716 static gint compare_strings(gconstpointer p_a, gconstpointer p_b)
1717 {
1718 const char *a = *((const char **) p_a);
1719 const char *b = *((const char **) p_b);
1720
1721 return strcmp(a, b);
1722 }
1723
1724 int ctf_fs_component_create_ctf_fs_trace(struct ctf_fs_component *ctf_fs,
1725 const bt_value *paths_value,
1726 const bt_value *trace_name_value,
1727 bt_self_component *selfComp)
1728 {
1729 int ret = 0;
1730 uint64_t i;
1731 const bt2_common::LogCfg& logCfg = ctf_fs->logCfg;
1732 GPtrArray *paths = NULL;
1733 std::vector<ctf_fs_trace::UP> traces;
1734 const char *trace_name;
1735
1736 BT_ASSERT(bt_value_get_type(paths_value) == BT_VALUE_TYPE_ARRAY);
1737 BT_ASSERT(!bt_value_array_is_empty(paths_value));
1738
1739 paths = g_ptr_array_new_with_free_func(g_free);
1740 if (!paths) {
1741 BT_CLOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
1742 goto error;
1743 }
1744
1745 trace_name = trace_name_value ? bt_value_string_get(trace_name_value) : NULL;
1746
1747 /*
1748 * Create a sorted array of the paths, to make the execution of this
1749 * component deterministic.
1750 */
1751 for (i = 0; i < bt_value_array_get_length(paths_value); i++) {
1752 const bt_value *path_value = bt_value_array_borrow_element_by_index_const(paths_value, i);
1753 const char *input = bt_value_string_get(path_value);
1754 gchar *input_copy;
1755
1756 input_copy = g_strdup(input);
1757 if (!input_copy) {
1758 BT_CLOGE_APPEND_CAUSE("Failed to copy a string.");
1759 goto error;
1760 }
1761
1762 g_ptr_array_add(paths, input_copy);
1763 }
1764
1765 g_ptr_array_sort(paths, compare_strings);
1766
1767 /* Create a separate ctf_fs_trace object for each path. */
1768 for (i = 0; i < paths->len; i++) {
1769 const char *path = (const char *) g_ptr_array_index(paths, i);
1770
1771 ret = ctf_fs_component_create_ctf_fs_trace_one_path(ctf_fs, path, trace_name, traces,
1772 selfComp);
1773 if (ret) {
1774 goto end;
1775 }
1776 }
1777
1778 if (traces.size() > 1) {
1779 ctf_fs_trace *first_trace = traces[0].get();
1780 const uint8_t *first_trace_uuid = first_trace->metadata->tc->uuid;
1781
1782 /*
1783 * We have more than one trace, they must all share the same
1784 * UUID, verify that.
1785 */
1786 for (i = 0; i < traces.size(); i++) {
1787 ctf_fs_trace *this_trace = traces[i].get();
1788 const uint8_t *this_trace_uuid = this_trace->metadata->tc->uuid;
1789
1790 if (!this_trace->metadata->tc->is_uuid_set) {
1791 BT_CLOGE_APPEND_CAUSE(
1792 "Multiple traces given, but a trace does not have a UUID: path=%s",
1793 this_trace->path->str);
1794 goto error;
1795 }
1796
1797 if (bt_uuid_compare(first_trace_uuid, this_trace_uuid) != 0) {
1798 char first_trace_uuid_str[BT_UUID_STR_LEN + 1];
1799 char this_trace_uuid_str[BT_UUID_STR_LEN + 1];
1800
1801 bt_uuid_to_str(first_trace_uuid, first_trace_uuid_str);
1802 bt_uuid_to_str(this_trace_uuid, this_trace_uuid_str);
1803
1804 BT_CLOGE_APPEND_CAUSE("Multiple traces given, but UUIDs don't match: "
1805 "first-trace-uuid=%s, first-trace-path=%s, "
1806 "trace-uuid=%s, trace-path=%s",
1807 first_trace_uuid_str, first_trace->path->str,
1808 this_trace_uuid_str, this_trace->path->str);
1809 goto error;
1810 }
1811 }
1812
1813 ret = merge_ctf_fs_traces(std::move(traces), ctf_fs->trace);
1814 if (ret) {
1815 BT_CLOGE_APPEND_CAUSE("Failed to merge traces with the same UUID.");
1816 goto error;
1817 }
1818 } else {
1819 /* Just one trace, it may or may not have a UUID, both are fine. */
1820 ctf_fs->trace = std::move(traces[0]);
1821 }
1822
1823 ret = fix_packet_index_tracer_bugs(ctf_fs);
1824 if (ret) {
1825 BT_CLOGE_APPEND_CAUSE("Failed to fix packet index tracer bugs.");
1826 }
1827
1828 /*
1829 * Sort data stream file groups by first data stream file info
1830 * path to get a deterministic order. This order influences the
1831 * order of the output ports. It also influences the order of
1832 * the automatic stream IDs if the trace's packet headers do not
1833 * contain a `stream_instance_id` field, in which case the data
1834 * stream file to stream ID association is always the same,
1835 * whatever the build and the system.
1836 *
1837 * Having a deterministic order here can help debugging and
1838 * testing.
1839 */
1840 std::sort(ctf_fs->trace->ds_file_groups.begin(), ctf_fs->trace->ds_file_groups.end(),
1841 compare_ds_file_groups_by_first_path);
1842 goto end;
1843 error:
1844 ret = -1;
1845
1846 end:
1847 if (paths) {
1848 g_ptr_array_free(paths, TRUE);
1849 }
1850
1851 return ret;
1852 }
1853
1854 static GString *get_stream_instance_unique_name(struct ctf_fs_ds_file_group *ds_file_group)
1855 {
1856 GString *name;
1857 struct ctf_fs_ds_file_info *ds_file_info;
1858
1859 name = g_string_new(NULL);
1860 if (!name) {
1861 goto end;
1862 }
1863
1864 /*
1865 * If there's more than one stream file in the stream file
1866 * group, the first (earliest) stream file's path is used as
1867 * the stream's unique name.
1868 */
1869 BT_ASSERT(!ds_file_group->ds_file_infos.empty());
1870 ds_file_info = ds_file_group->ds_file_infos[0].get();
1871 g_string_assign(name, ds_file_info->path.c_str());
1872
1873 end:
1874 return name;
1875 }
1876
1877 /* Create the IR stream objects for ctf_fs_trace. */
1878
1879 static int create_streams_for_trace(struct ctf_fs_trace *ctf_fs_trace)
1880 {
1881 int ret;
1882 GString *name = NULL;
1883 const bt2_common::LogCfg& logCfg = ctf_fs_trace->logCfg;
1884
1885 for (ctf_fs_ds_file_group::UP& ds_file_group : ctf_fs_trace->ds_file_groups) {
1886 name = get_stream_instance_unique_name(ds_file_group.get());
1887
1888 if (!name) {
1889 goto error;
1890 }
1891
1892 BT_ASSERT(ds_file_group->sc->ir_sc);
1893 BT_ASSERT(ctf_fs_trace->trace);
1894
1895 if (ds_file_group->stream_id == UINT64_C(-1)) {
1896 /* No stream ID: use 0 */
1897 ds_file_group->stream = bt_stream_create_with_id(
1898 ds_file_group->sc->ir_sc, ctf_fs_trace->trace, ctf_fs_trace->next_stream_id);
1899 ctf_fs_trace->next_stream_id++;
1900 } else {
1901 /* Specific stream ID */
1902 ds_file_group->stream = bt_stream_create_with_id(
1903 ds_file_group->sc->ir_sc, ctf_fs_trace->trace, (uint64_t) ds_file_group->stream_id);
1904 }
1905
1906 if (!ds_file_group->stream) {
1907 BT_CLOGE_APPEND_CAUSE("Cannot create stream for DS file group: "
1908 "addr=%p, stream-name=\"%s\"",
1909 ds_file_group.get(), name->str);
1910 goto error;
1911 }
1912
1913 ret = bt_stream_set_name(ds_file_group->stream, name->str);
1914 if (ret) {
1915 BT_CLOGE_APPEND_CAUSE("Cannot set stream's name: "
1916 "addr=%p, stream-name=\"%s\"",
1917 ds_file_group->stream, name->str);
1918 goto error;
1919 }
1920
1921 g_string_free(name, TRUE);
1922 name = NULL;
1923 }
1924
1925 ret = 0;
1926 goto end;
1927
1928 error:
1929 ret = -1;
1930
1931 end:
1932
1933 if (name) {
1934 g_string_free(name, TRUE);
1935 }
1936 return ret;
1937 }
1938
1939 static const bt_param_validation_value_descr inputs_elem_descr =
1940 bt_param_validation_value_descr::makeString();
1941
1942 static bt_param_validation_map_value_entry_descr fs_params_entries_descr[] = {
1943 {"inputs", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY,
1944 bt_param_validation_value_descr::makeArray(1, BT_PARAM_VALIDATION_INFINITE,
1945 inputs_elem_descr)},
1946 {"trace-name", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL,
1947 bt_param_validation_value_descr::makeString()},
1948 {"clock-class-offset-s", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL,
1949 bt_param_validation_value_descr::makeSignedInteger()},
1950 {"clock-class-offset-ns", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL,
1951 bt_param_validation_value_descr::makeSignedInteger()},
1952 {"force-clock-class-origin-unix-epoch", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL,
1953 bt_param_validation_value_descr::makeBool()},
1954 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END};
1955
1956 bool read_src_fs_parameters(const bt_value *params, const bt_value **inputs,
1957 const bt_value **trace_name, struct ctf_fs_component *ctf_fs)
1958 {
1959 bool ret;
1960 const bt_value *value;
1961 enum bt_param_validation_status validate_value_status;
1962 gchar *error = NULL;
1963 const bt2_common::LogCfg& logCfg = ctf_fs->logCfg;
1964
1965 validate_value_status = bt_param_validation_validate(params, fs_params_entries_descr, &error);
1966 if (validate_value_status != BT_PARAM_VALIDATION_STATUS_OK) {
1967 BT_CLOGE_APPEND_CAUSE("%s", error);
1968 ret = false;
1969 goto end;
1970 }
1971
1972 /* inputs parameter */
1973 *inputs = bt_value_map_borrow_entry_value_const(params, "inputs");
1974
1975 /* clock-class-offset-s parameter */
1976 value = bt_value_map_borrow_entry_value_const(params, "clock-class-offset-s");
1977 if (value) {
1978 ctf_fs->clkClsCfg.offsetSec = bt_value_integer_signed_get(value);
1979 }
1980
1981 /* clock-class-offset-ns parameter */
1982 value = bt_value_map_borrow_entry_value_const(params, "clock-class-offset-ns");
1983 if (value) {
1984 ctf_fs->clkClsCfg.offsetNanoSec = bt_value_integer_signed_get(value);
1985 }
1986
1987 /* force-clock-class-origin-unix-epoch parameter */
1988 value = bt_value_map_borrow_entry_value_const(params, "force-clock-class-origin-unix-epoch");
1989 if (value) {
1990 ctf_fs->clkClsCfg.forceOriginIsUnixEpoch = bt_value_bool_get(value);
1991 }
1992
1993 /* trace-name parameter */
1994 *trace_name = bt_value_map_borrow_entry_value_const(params, "trace-name");
1995
1996 ret = true;
1997
1998 end:
1999 g_free(error);
2000 return ret;
2001 }
2002
2003 static ctf_fs_component::UP ctf_fs_create(const bt_value *params,
2004 bt_self_component_source *self_comp_src,
2005 const bt2_common::LogCfg& logCfg)
2006 {
2007 const bt_value *inputs_value;
2008 const bt_value *trace_name_value;
2009 bt_self_component *self_comp = bt_self_component_source_as_self_component(self_comp_src);
2010
2011 ctf_fs_component::UP ctf_fs = ctf_fs_component_create(logCfg);
2012 if (!ctf_fs) {
2013 return nullptr;
2014 }
2015
2016 if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value, ctf_fs.get())) {
2017 return nullptr;
2018 }
2019
2020 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs.get(), inputs_value, trace_name_value,
2021 self_comp)) {
2022 return nullptr;
2023 }
2024
2025 if (create_streams_for_trace(ctf_fs->trace.get())) {
2026 return nullptr;
2027 }
2028
2029 if (create_ports_for_trace(ctf_fs.get(), ctf_fs->trace.get(), self_comp_src)) {
2030 return nullptr;
2031 }
2032
2033 return ctf_fs;
2034 }
2035
2036 BT_HIDDEN
2037 bt_component_class_initialize_method_status
2038 ctf_fs_init(bt_self_component_source *self_comp_src, bt_self_component_source_configuration *config,
2039 const bt_value *params, __attribute__((unused)) void *init_method_data)
2040 {
2041 bt_self_component *selfComp = bt_self_component_source_as_self_component(self_comp_src);
2042 const bt_component *comp = bt_self_component_as_component(selfComp);
2043 bt_logging_level logLevel = bt_component_get_logging_level(comp);
2044 bt2_common::LogCfg logCfg(logLevel, *selfComp);
2045
2046 try {
2047 ctf_fs_component::UP ctf_fs = ctf_fs_create(params, self_comp_src, logCfg);
2048 if (!ctf_fs) {
2049 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
2050 }
2051
2052 bt_self_component_set_data(selfComp, ctf_fs.release());
2053
2054 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
2055 } catch (const std::bad_alloc&) {
2056 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
2057 } catch (const bt2_common::Error&) {
2058 BT_CLOGE_APPEND_CAUSE("Failed to initialize component");
2059 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
2060 }
2061 }
2062
2063 BT_HIDDEN
2064 bt_component_class_query_method_status ctf_fs_query(bt_self_component_class_source *comp_class_src,
2065 bt_private_query_executor *priv_query_exec,
2066 const char *object, const bt_value *params,
2067 __attribute__((unused)) void *method_data,
2068 const bt_value **result)
2069 {
2070 const bt_query_executor *query_exec =
2071 bt_private_query_executor_as_query_executor_const(priv_query_exec);
2072 bt_logging_level log_level = bt_query_executor_get_logging_level(query_exec);
2073 bt_self_component_class *comp_class =
2074 bt_self_component_class_source_as_self_component_class(comp_class_src);
2075 bt2_common::LogCfg logCfg(log_level, *comp_class);
2076
2077 try {
2078 bt2::ConstMapValue paramsObj(params);
2079 nonstd::optional<bt2::Value::Shared> resultObj;
2080
2081 if (strcmp(object, "metadata-info") == 0) {
2082 resultObj = metadata_info_query(paramsObj, logCfg);
2083 } else if (strcmp(object, "babeltrace.trace-infos") == 0) {
2084 resultObj = trace_infos_query(paramsObj, logCfg);
2085 } else if (!strcmp(object, "babeltrace.support-info")) {
2086 resultObj = support_info_query(paramsObj, logCfg);
2087 } else {
2088 BT_LOGE("Unknown query object `%s`", object);
2089 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT;
2090 }
2091
2092 *result = resultObj->release().libObjPtr();
2093
2094 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
2095 } catch (const std::bad_alloc&) {
2096 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
2097 } catch (const bt2_common::Error&) {
2098 BT_CLOGE_APPEND_CAUSE("Failed to execute query: object=%s", object);
2099 return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
2100 }
2101 }
This page took 0.11852 seconds and 5 git commands to generate.