2 * SPDX-License-Identifier: MIT
4 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Babeltrace CTF file system Reader Component queries
10 #include <glib/gstdio.h>
11 #include <sys/types.h>
13 #include <babeltrace2/babeltrace.h>
15 #include "../common/src/metadata/tsdl/decoder.hpp"
19 #define METADATA_TEXT_SIG "/* CTF 1.8"
28 bt_component_class_query_method_status
metadata_info_query(const bt_value
*params
,
29 const bt2c::Logger
& logger
,
30 const bt_value
**user_result
)
32 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
33 bt_value
*result
= NULL
;
34 const bt_value
*path_value
= NULL
;
35 FILE *metadata_fp
= NULL
;
40 struct ctf_metadata_decoder
*decoder
= NULL
;
41 ctf_metadata_decoder_config decoder_cfg
{logger
};
42 enum ctf_metadata_decoder_status decoder_status
;
43 GString
*g_metadata_text
= NULL
;
44 const char *plaintext
;
46 result
= bt_value_map_create();
48 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
54 if (!bt_value_is_map(params
)) {
55 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Query parameters is not a map value object.");
56 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
60 path_value
= bt_value_map_borrow_entry_value_const(params
, "path");
62 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Mandatory `path` parameter missing");
63 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
67 if (!bt_value_is_string(path_value
)) {
68 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "`path` parameter is required to be a string value");
69 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
73 path
= bt_value_string_get(path_value
);
76 metadata_fp
= ctf_fs_metadata_open_file(path
, logger
);
78 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Cannot open trace metadata: path=\"{}\".", path
);
82 ret
= ctf_metadata_decoder_is_packetized(metadata_fp
, &is_packetized
, &bo
, logger
);
84 BT_CPPLOGE_APPEND_CAUSE_SPEC(
85 logger
, "Cannot check whether or not the metadata stream is packetized: path=\"{}\".",
90 decoder_cfg
.keep_plain_text
= true;
91 decoder
= ctf_metadata_decoder_create(&decoder_cfg
);
93 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Cannot create metadata decoder: path=\"{}\".", path
);
98 decoder_status
= ctf_metadata_decoder_append_content(decoder
, metadata_fp
);
100 BT_CPPLOGE_APPEND_CAUSE_SPEC(
101 logger
, "Cannot update metadata decoder's content: path=\"{}\".", path
);
105 plaintext
= ctf_metadata_decoder_get_text(decoder
);
106 g_metadata_text
= g_string_new(NULL
);
108 if (!g_metadata_text
) {
112 if (strncmp(plaintext
, METADATA_TEXT_SIG
, sizeof(METADATA_TEXT_SIG
) - 1) != 0) {
113 g_string_assign(g_metadata_text
, METADATA_TEXT_SIG
);
114 g_string_append(g_metadata_text
, " */\n\n");
117 g_string_append(g_metadata_text
, plaintext
);
119 ret
= bt_value_map_insert_string_entry(result
, "text", g_metadata_text
->str
);
121 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Cannot insert metadata text into query result.");
125 ret
= bt_value_map_insert_bool_entry(result
, "is-packetized", is_packetized
);
127 BT_CPPLOGE_APPEND_CAUSE_SPEC(
128 logger
, "Cannot insert \"is-packetized\" attribute into query result.");
135 BT_VALUE_PUT_REF_AND_RESET(result
);
139 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
143 if (g_metadata_text
) {
144 g_string_free(g_metadata_text
, TRUE
);
146 ctf_metadata_decoder_destroy(decoder
);
149 ret
= fclose(metadata_fp
);
151 BT_CPPLOGE_ERRNO_SPEC(logger
, "Cannot close metadata file stream", ": path=\"{}\"",
156 *user_result
= result
;
160 static int add_range(bt_value
*info
, struct range
*range
, const char *range_name
)
163 bt_value_map_insert_entry_status status
;
171 status
= bt_value_map_insert_empty_map_entry(info
, range_name
, &range_map
);
172 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
177 status
= bt_value_map_insert_signed_integer_entry(range_map
, "begin", range
->begin_ns
);
178 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
183 status
= bt_value_map_insert_signed_integer_entry(range_map
, "end", range
->end_ns
);
184 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
193 static int populate_stream_info(struct ctf_fs_ds_file_group
*group
, bt_value
*group_info
,
194 struct range
*stream_range
)
197 bt_value_map_insert_entry_status insert_status
;
198 struct ctf_fs_ds_index_entry
*first_ds_index_entry
, *last_ds_index_entry
;
199 bt2c::GCharUP port_name
;
202 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
203 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
204 * the timestamp_begin of the first index entry and the timestamp_end
205 * of the last index entry.
207 BT_ASSERT(group
->index
);
208 BT_ASSERT(group
->index
->entries
);
209 BT_ASSERT(group
->index
->entries
->len
> 0);
212 first_ds_index_entry
=
213 (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(group
->index
->entries
, 0);
216 last_ds_index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
217 group
->index
->entries
, group
->index
->entries
->len
- 1);
219 stream_range
->begin_ns
= first_ds_index_entry
->timestamp_begin_ns
;
220 stream_range
->end_ns
= last_ds_index_entry
->timestamp_end_ns
;
223 * If any of the begin and end timestamps is not set it means that
224 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
225 * in their packet context so we can't set the range.
228 stream_range
->begin_ns
!= UINT64_C(-1) && stream_range
->end_ns
!= UINT64_C(-1);
230 ret
= add_range(group_info
, stream_range
, "range-ns");
235 port_name
= ctf_fs_make_port_name(group
);
241 insert_status
= bt_value_map_insert_string_entry(group_info
, "port-name", port_name
.get());
242 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
251 static int populate_trace_info(const struct ctf_fs_trace
*trace
, bt_value
*trace_info
,
252 const bt2c::Logger
& logger
)
256 bt_value_map_insert_entry_status insert_status
;
257 bt_value_array_append_element_status append_status
;
258 bt_value
*file_groups
= NULL
;
260 BT_ASSERT(trace
->ds_file_groups
);
261 /* Add trace range info only if it contains streams. */
262 if (trace
->ds_file_groups
->len
== 0) {
264 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Trace has no streams: trace-path={}",
269 insert_status
= bt_value_map_insert_empty_array_entry(trace_info
, "stream-infos", &file_groups
);
270 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
275 /* Find range of all stream groups, and of the trace. */
276 for (group_idx
= 0; group_idx
< trace
->ds_file_groups
->len
; group_idx
++) {
277 bt_value
*group_info
;
279 ctf_fs_ds_file_group
*group
=
280 (ctf_fs_ds_file_group
*) g_ptr_array_index(trace
->ds_file_groups
, group_idx
);
282 append_status
= bt_value_array_append_empty_map_element(file_groups
, &group_info
);
283 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
288 ret
= populate_stream_info(group
, group_info
, &group_range
);
298 bt_component_class_query_method_status
299 trace_infos_query(const bt_value
*params
, const bt2c::Logger
& logger
, const bt_value
**user_result
)
301 ctf_fs_component::UP ctf_fs
;
302 bt_component_class_query_method_status status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
303 bt_value
*result
= NULL
;
304 const bt_value
*inputs_value
= NULL
;
305 const bt_value
*trace_name_value
;
307 bt_value
*trace_info
= NULL
;
308 bt_value_array_append_element_status append_status
;
312 if (!bt_value_is_map(params
)) {
313 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Query parameters is not a map value object.");
314 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
318 ctf_fs
= ctf_fs_component_create(logger
);
323 if (!read_src_fs_parameters(params
, &inputs_value
, &trace_name_value
, ctf_fs
.get())) {
324 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
328 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs
.get(), inputs_value
, trace_name_value
, NULL
)) {
332 result
= bt_value_array_create();
334 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
338 append_status
= bt_value_array_append_empty_map_element(result
, &trace_info
);
339 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
340 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Failed to create trace info map.");
344 ret
= populate_trace_info(ctf_fs
->trace
, trace_info
, logger
);
352 BT_VALUE_PUT_REF_AND_RESET(result
);
355 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
359 *user_result
= result
;
363 bt_component_class_query_method_status
364 support_info_query(const bt_value
*params
, const bt2c::Logger
& logger
, const bt_value
**user_result
)
366 const bt_value
*input_type_value
;
367 const char *input_type
;
368 bt_component_class_query_method_status status
;
369 bt_value_map_insert_entry_status insert_entry_status
;
371 gchar
*metadata_path
= NULL
;
372 bt_value
*result
= NULL
;
373 struct ctf_metadata_decoder
*metadata_decoder
= NULL
;
374 FILE *metadata_file
= NULL
;
375 char uuid_str
[BT_UUID_STR_LEN
+ 1];
376 bool has_uuid
= false;
377 const bt_value
*input_value
;
380 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
381 BT_ASSERT(input_type_value
);
382 BT_ASSERT(bt_value_get_type(input_type_value
) == BT_VALUE_TYPE_STRING
);
383 input_type
= bt_value_string_get(input_type_value
);
385 if (strcmp(input_type
, "directory") != 0) {
389 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
390 BT_ASSERT(input_value
);
391 BT_ASSERT(bt_value_get_type(input_value
) == BT_VALUE_TYPE_STRING
);
392 input
= bt_value_string_get(input_value
);
394 metadata_path
= g_build_filename(input
, CTF_FS_METADATA_FILENAME
, NULL
);
395 if (!metadata_path
) {
396 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
400 metadata_file
= g_fopen(metadata_path
, "rb");
402 enum ctf_metadata_decoder_status decoder_status
;
405 ctf_metadata_decoder_config metadata_decoder_config
{logger
};
407 metadata_decoder
= ctf_metadata_decoder_create(&metadata_decoder_config
);
408 if (!metadata_decoder
) {
409 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
413 decoder_status
= ctf_metadata_decoder_append_content(metadata_decoder
, metadata_file
);
414 if (decoder_status
!= CTF_METADATA_DECODER_STATUS_OK
) {
415 BT_CPPLOGW_SPEC(logger
, "cannot append metadata content: metadata-decoder-status={}",
417 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
422 * We were able to parse the metadata file, so we are
423 * confident it's a CTF trace.
427 /* If the trace has a UUID, return the stringified UUID as the group. */
428 if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder
, uuid
) == 0) {
429 bt_uuid_to_str(uuid
, uuid_str
);
435 result
= bt_value_map_create();
437 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
441 insert_entry_status
= bt_value_map_insert_real_entry(result
, "weight", weight
);
442 if (insert_entry_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
443 status
= (bt_component_class_query_method_status
) insert_entry_status
;
447 /* We are not supposed to have weight == 0 and a UUID. */
448 BT_ASSERT(weight
> 0 || !has_uuid
);
450 if (weight
> 0 && has_uuid
) {
451 insert_entry_status
= bt_value_map_insert_string_entry(result
, "group", uuid_str
);
452 if (insert_entry_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
453 status
= (bt_component_class_query_method_status
) insert_entry_status
;
458 *user_result
= result
;
460 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
463 g_free(metadata_path
);
464 bt_value_put_ref(result
);
465 ctf_metadata_decoder_destroy(metadata_decoder
);