2 * SPDX-License-Identifier: MIT
4 * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com>
7 #ifndef _METADATA_DECODER_H
8 #define _METADATA_DECODER_H
12 #include <babeltrace2/babeltrace.h>
14 #include "common/uuid.h"
15 #include "cpp-common/bt2c/logging.hpp"
16 #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
18 #include "../../../src/clk-cls-cfg.hpp"
20 /* A CTF metadata decoder object */
21 struct ctf_metadata_decoder;
23 /* CTF metadata decoder status */
24 enum ctf_metadata_decoder_status
26 CTF_METADATA_DECODER_STATUS_OK = 0,
27 CTF_METADATA_DECODER_STATUS_NONE = 1,
28 CTF_METADATA_DECODER_STATUS_ERROR = -1,
29 CTF_METADATA_DECODER_STATUS_INCOMPLETE = -2,
30 CTF_METADATA_DECODER_STATUS_INVAL_VERSION = -3,
31 CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR = -4,
34 inline const char *format_as(ctf_metadata_decoder_status status) noexcept
37 case CTF_METADATA_DECODER_STATUS_OK:
38 return "CTF_METADATA_DECODER_STATUS_OK";
40 case CTF_METADATA_DECODER_STATUS_NONE:
41 return "CTF_METADATA_DECODER_STATUS_NONE";
43 case CTF_METADATA_DECODER_STATUS_ERROR:
44 return "CTF_METADATA_DECODER_STATUS_ERROR";
46 case CTF_METADATA_DECODER_STATUS_INCOMPLETE:
47 return "CTF_METADATA_DECODER_STATUS_INCOMPLETE";
49 case CTF_METADATA_DECODER_STATUS_INVAL_VERSION:
50 return "CTF_METADATA_DECODER_STATUS_INVAL_VERSION";
52 case CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR:
53 return "CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR";
59 /* Decoding configuration */
60 struct ctf_metadata_decoder_config
62 explicit ctf_metadata_decoder_config(const bt2c::Logger& parentLogger) :
63 logger {parentLogger, "PLUGIN/CTF/META/DECODER-CONFIG"}
69 /* Weak, used to create a bt_trace_class, if not nullptr. */
70 bt_self_component *self_comp = nullptr;
72 ctf::src::ClkClsCfg clkClsCfg;
74 /* True to create trace class objects */
75 bool create_trace_class = false;
78 * True to keep the plain text when content is appended with
79 * ctf_metadata_decoder_append_content().
81 bool keep_plain_text = false;
84 struct ctf_metadata_decoder_deleter
86 void operator()(struct ctf_metadata_decoder *decoder);
89 using ctf_metadata_decoder_up = std::unique_ptr<ctf_metadata_decoder, ctf_metadata_decoder_deleter>;
92 * Creates a CTF metadata decoder.
94 * Returns `NULL` on error.
96 ctf_metadata_decoder_up
97 ctf_metadata_decoder_create(const struct ctf_metadata_decoder_config *config);
100 * Destroys a CTF metadata decoder that you created with
101 * ctf_metadata_decoder_create().
103 void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder *metadata_decoder);
106 * Appends content to the metadata decoder.
108 * This function reads the metadata from the current position of `fp`
109 * until the end of this file stream.
111 * The metadata can be packetized or not.
113 * The metadata chunk needs to be complete and lexically scannable, that
114 * is, zero or more complete top-level blocks. If it's incomplete, this
115 * function returns `CTF_METADATA_DECODER_STATUS_INCOMPLETE`. If this
116 * function returns `CTF_METADATA_DECODER_STATUS_INCOMPLETE`, then you
117 * need to call it again with the _same_ metadata and more to make it
118 * complete. For example:
120 * First call: event { name = hell
121 * Second call: event { name = hello_world; ... };
123 * If everything goes as expected, this function returns
124 * `CTF_METADATA_DECODER_STATUS_OK`.
126 enum ctf_metadata_decoder_status
127 ctf_metadata_decoder_append_content(struct ctf_metadata_decoder *metadata_decoder, FILE *fp);
130 * Returns the trace IR trace class of this metadata decoder (new
133 * Returns `NULL` if there's none yet or if the metadata decoder is not
134 * configured to create trace classes.
136 bt_trace_class *ctf_metadata_decoder_get_ir_trace_class(struct ctf_metadata_decoder *mdec);
139 * Returns the CTF IR trace class of this metadata decoder.
141 * Returns `NULL` if there's none yet or if the metadata decoder is not
142 * configured to create trace classes.
144 struct ctf_trace_class *
145 ctf_metadata_decoder_borrow_ctf_trace_class(struct ctf_metadata_decoder *mdec);
148 * Checks whether or not a given metadata file stream `fp` is
149 * packetized, setting `is_packetized` accordingly on success. On
150 * success, also sets `*byte_order` to the byte order of the first
153 int ctf_metadata_decoder_is_packetized(FILE *fp, bool *is_packetized, int *byte_order,
154 const bt2c::Logger& logger);
157 * Returns the byte order of the decoder's metadata stream as set by the
158 * last call to ctf_metadata_decoder_append_content().
160 * Returns -1 if unknown (plain text content).
162 int ctf_metadata_decoder_get_byte_order(struct ctf_metadata_decoder *mdec);
165 * Returns the UUID of the decoder's metadata stream as set by the last
166 * call to ctf_metadata_decoder_append_content().
168 int ctf_metadata_decoder_get_uuid(struct ctf_metadata_decoder *mdec, bt_uuid_t uuid);
171 * Returns the UUID of the decoder's trace class, if available.
175 * * `CTF_METADATA_DECODER_STATUS_OK`: success.
176 * * `CTF_METADATA_DECODER_STATUS_NONE`: no UUID.
177 * * `CTF_METADATA_DECODER_STATUS_INCOMPLETE`: missing metadata content.
179 enum ctf_metadata_decoder_status
180 ctf_metadata_decoder_get_trace_class_uuid(struct ctf_metadata_decoder *mdec, bt_uuid_t uuid);
183 * Returns the metadata decoder's current metadata text.
185 const char *ctf_metadata_decoder_get_text(struct ctf_metadata_decoder *mdec);
187 static inline bool ctf_metadata_decoder_is_packet_version_valid(unsigned int major,
190 return major == 1 && minor == 8;
193 #endif /* _METADATA_DECODER_H */