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 "cpp-common/bt2/exc.hpp"
16 #include "cpp-common/bt2c/glib-up.hpp"
17 #include "cpp-common/bt2c/libc-up.hpp"
19 #include "plugins/common/param-validation/param-validation.h"
21 #include "../common/src/metadata/tsdl/decoder.hpp"
22 #include "data-stream-file.hpp"
24 #include "metadata.hpp"
27 #define METADATA_TEXT_SIG "/* CTF 1.8"
36 static bt_param_validation_map_value_entry_descr metadataInfoQueryParamsDesc
[] = {
37 {"path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
38 bt_param_validation_value_descr::makeString()},
39 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
41 bt2::Value::Shared
metadata_info_query(const bt2::ConstMapValue params
, const bt2c::Logger
& logger
)
43 gchar
*validateError
= nullptr;
44 const auto validationStatus
= bt_param_validation_validate(
45 params
.libObjPtr(), metadataInfoQueryParamsDesc
, &validateError
);
47 if (validationStatus
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
48 throw bt2::MemoryError
{};
49 } else if (validationStatus
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
50 const bt2c::GCharUP deleter
{validateError
};
52 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
, "{}", validateError
);
55 const auto path
= params
["path"]->asString().value();
56 bt2c::FileUP metadataFp
{ctf_fs_metadata_open_file(path
, logger
)};
58 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
,
59 "Cannot open trace metadata: path=\"{}\".", path
);
64 int ret
= ctf_metadata_decoder_is_packetized(metadataFp
.get(), &is_packetized
, &bo
, logger
);
67 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(
69 "Cannot check whether or not the metadata stream is packetized: path=\"{}\".", path
);
72 ctf_metadata_decoder_config decoder_cfg
{logger
};
73 decoder_cfg
.keep_plain_text
= true;
74 ctf_metadata_decoder_up decoder
= ctf_metadata_decoder_create(&decoder_cfg
);
76 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(
77 logger
, bt2::Error
, "Cannot create metadata decoder: path=\"{}}\".", path
);
80 rewind(metadataFp
.get());
81 ctf_metadata_decoder_status decoder_status
=
82 ctf_metadata_decoder_append_content(decoder
.get(), metadataFp
.get());
84 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(
85 logger
, bt2::Error
, "Cannot update metadata decoder's content: path=\"{}\".", path
);
88 const char *plain_text
= ctf_metadata_decoder_get_text(decoder
.get());
89 std::string metadata_text
;
91 if (strncmp(plain_text
, METADATA_TEXT_SIG
, sizeof(METADATA_TEXT_SIG
) - 1) != 0) {
92 metadata_text
= METADATA_TEXT_SIG
;
93 metadata_text
+= " */\n\n";
96 metadata_text
+= plain_text
;
98 const auto result
= bt2::MapValue::create();
99 result
->insert("text", metadata_text
);
100 result
->insert("is-packetized", is_packetized
);
105 static void add_range(const bt2::MapValue info
, struct range
*range
, const char *range_name
)
112 const auto rangeMap
= info
.insertEmptyMap(range_name
);
113 rangeMap
.insert("begin", range
->begin_ns
);
114 rangeMap
.insert("end", range
->end_ns
);
117 static void populate_stream_info(struct ctf_fs_ds_file_group
*group
, const bt2::MapValue groupInfo
,
118 struct range
*stream_range
, const bt2c::Logger
& logger
)
121 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
122 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
123 * the timestamp_begin of the first index entry and the timestamp_end
124 * of the last index entry.
126 BT_ASSERT(group
->index
);
127 BT_ASSERT(group
->index
->entries
);
128 BT_ASSERT(group
->index
->entries
->len
> 0);
131 ctf_fs_ds_index_entry
*first_ds_index_entry
=
132 (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(group
->index
->entries
, 0);
135 ctf_fs_ds_index_entry
*last_ds_index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
136 group
->index
->entries
, group
->index
->entries
->len
- 1);
138 stream_range
->begin_ns
= first_ds_index_entry
->timestamp_begin_ns
;
139 stream_range
->end_ns
= last_ds_index_entry
->timestamp_end_ns
;
142 * If any of the begin and end timestamps is not set it means that
143 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
144 * in their packet context so we can't set the range.
147 stream_range
->begin_ns
!= UINT64_C(-1) && stream_range
->end_ns
!= UINT64_C(-1);
149 add_range(groupInfo
, stream_range
, "range-ns");
151 bt2c::GCharUP portName
= ctf_fs_make_port_name(group
);
153 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
, "Failed to make port name");
156 groupInfo
.insert("port-name", portName
.get());
159 static void populate_trace_info(const struct ctf_fs_trace
*trace
, const bt2::MapValue traceInfo
,
160 const bt2c::Logger
& logger
)
162 /* Add trace range info only if it contains streams. */
163 if (trace
->ds_file_groups
.empty()) {
164 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(
165 logger
, bt2::Error
, "Trace has no streams: trace-path={}", trace
->path
->str
);
168 const auto fileGroups
= traceInfo
.insertEmptyArray("stream-infos");
170 /* Find range of all stream groups, and of the trace. */
171 for (const auto& group
: trace
->ds_file_groups
) {
173 const auto groupInfo
= fileGroups
.appendEmptyMap();
174 populate_stream_info(group
.get(), groupInfo
, &group_range
, logger
);
178 bt2::Value::Shared
trace_infos_query(const bt2::ConstMapValue params
, const bt2c::Logger
& logger
)
180 ctf_fs_component::UP ctf_fs
= ctf_fs_component_create(logger
);
182 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
,
183 "Cannot create ctf_fs_component");
186 const bt_value
*inputs_value
= NULL
;
187 const bt_value
*trace_name_value
;
189 if (!read_src_fs_parameters(params
.libObjPtr(), &inputs_value
, &trace_name_value
,
191 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
, "Failed to read parameters");
194 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs
.get(), inputs_value
, trace_name_value
, NULL
)) {
195 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
, "Failed to create trace");
198 const auto result
= bt2::ArrayValue::create();
199 const auto traceInfo
= result
->appendEmptyMap();
200 populate_trace_info(ctf_fs
->trace
.get(), traceInfo
, logger
);
205 static bt_param_validation_map_value_entry_descr supportInfoQueryParamsDesc
[] = {
206 {"type", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
207 bt_param_validation_value_descr::makeString()},
208 {"input", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
,
209 bt_param_validation_value_descr::makeString()},
210 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
};
212 bt2::Value::Shared
support_info_query(const bt2::ConstMapValue params
, const bt2c::Logger
& logger
)
214 gchar
*validateError
= NULL
;
215 const auto validationStatus
= bt_param_validation_validate(
216 params
.libObjPtr(), supportInfoQueryParamsDesc
, &validateError
);
218 if (validationStatus
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
219 throw bt2::MemoryError
{};
220 } else if (validationStatus
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
221 const bt2c::GCharUP deleter
{validateError
};
223 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
, "{}", validateError
);
226 const auto type
= params
["type"]->asString().value();
228 if (strcmp(type
, "directory") != 0) {
230 * The input type is not a directory so we are 100% sure it's not a CTF
231 * 1.8 trace as it would need a directory with at least 1 metadata file
232 * and 1 data stream file.
234 const auto result
= bt2::MapValue::create();
235 result
->insert("weight", 0.0f
);
239 const auto input
= params
["input"]->asString().value();
241 bt2c::GCharUP metadataPath
{g_build_filename(input
, CTF_FS_METADATA_FILENAME
, NULL
)};
243 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
, "Failed to read parameters");
247 char uuid_str
[BT_UUID_STR_LEN
+ 1];
248 bool has_uuid
= false;
249 bt2c::FileUP metadataFile
{g_fopen(metadataPath
.get(), "rb")};
251 enum ctf_metadata_decoder_status decoder_status
;
254 ctf_metadata_decoder_config metadata_decoder_config
{logger
};
256 ctf_metadata_decoder_up metadata_decoder
=
257 ctf_metadata_decoder_create(&metadata_decoder_config
);
258 if (!metadata_decoder
) {
259 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger
, bt2::Error
,
260 "Failed to create metadata decoder");
264 ctf_metadata_decoder_append_content(metadata_decoder
.get(), metadataFile
.get());
265 if (decoder_status
!= CTF_METADATA_DECODER_STATUS_OK
) {
266 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(
267 logger
, bt2::Error
, "Failed to append metadata content: metadata-decoder-status={}",
272 * We were able to parse the metadata file, so we are
273 * confident it's a CTF trace.
277 /* If the trace has a UUID, return the stringified UUID as the group. */
278 if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder
.get(), uuid
) == 0) {
279 bt_uuid_to_str(uuid
, uuid_str
);
284 const auto result
= bt2::MapValue::create();
285 result
->insert("weight", weight
);
287 /* We are not supposed to have weight == 0 and a UUID. */
288 BT_ASSERT(weight
> 0 || !has_uuid
);
290 if (weight
> 0 && has_uuid
) {
291 result
->insert("group", uuid_str
);