Commit | Line | Data |
---|---|---|
1e649dff | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
1e649dff | 3 | * |
0235b0db | 4 | * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com> |
1e649dff PP |
5 | */ |
6 | ||
0235b0db MJ |
7 | #ifndef _METADATA_DECODER_H |
8 | #define _METADATA_DECODER_H | |
9 | ||
087cd0f5 | 10 | #include <stdio.h> |
1e649dff | 11 | |
3fadfbc0 | 12 | #include <babeltrace2/babeltrace.h> |
1e649dff | 13 | |
06be9946 | 14 | #include "common/uuid.h" |
0f5c5d5c SM |
15 | #include "cpp-common/bt2c/logging.hpp" |
16 | #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */ | |
06be9946 | 17 | |
c942e7a2 SM |
18 | #include "../../../src/clk-cls-cfg.hpp" |
19 | ||
20 | /* A CTF metadata decoder object */ | |
21 | struct ctf_metadata_decoder; | |
22 | ||
1e649dff | 23 | /* CTF metadata decoder status */ |
4164020e SM |
24 | enum ctf_metadata_decoder_status |
25 | { | |
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, | |
1e649dff PP |
32 | }; |
33 | ||
0f5c5d5c SM |
34 | inline const char *format_as(ctf_metadata_decoder_status status) noexcept |
35 | { | |
36 | switch (status) { | |
37 | case CTF_METADATA_DECODER_STATUS_OK: | |
38 | return "CTF_METADATA_DECODER_STATUS_OK"; | |
39 | ||
40 | case CTF_METADATA_DECODER_STATUS_NONE: | |
41 | return "CTF_METADATA_DECODER_STATUS_NONE"; | |
42 | ||
43 | case CTF_METADATA_DECODER_STATUS_ERROR: | |
44 | return "CTF_METADATA_DECODER_STATUS_ERROR"; | |
45 | ||
46 | case CTF_METADATA_DECODER_STATUS_INCOMPLETE: | |
47 | return "CTF_METADATA_DECODER_STATUS_INCOMPLETE"; | |
48 | ||
49 | case CTF_METADATA_DECODER_STATUS_INVAL_VERSION: | |
50 | return "CTF_METADATA_DECODER_STATUS_INVAL_VERSION"; | |
51 | ||
52 | case CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR: | |
53 | return "CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR"; | |
54 | } | |
55 | ||
56 | bt_common_abort(); | |
57 | } | |
58 | ||
a2a54545 | 59 | /* Decoding configuration */ |
4164020e SM |
60 | struct ctf_metadata_decoder_config |
61 | { | |
0f5c5d5c SM |
62 | explicit ctf_metadata_decoder_config(const bt2c::Logger& parentLogger) : |
63 | logger {parentLogger, "PLUGIN/CTF/META/DECODER-CONFIG"} | |
64 | { | |
65 | } | |
4164020e | 66 | |
0f5c5d5c SM |
67 | bt2c::Logger logger; |
68 | ||
69 | /* Weak, used to create a bt_trace_class, if not nullptr. */ | |
afb0f12b | 70 | bt_self_component *self_comp = nullptr; |
4164020e | 71 | |
c942e7a2 | 72 | ctf::src::ClkClsCfg clkClsCfg; |
4164020e SM |
73 | |
74 | /* True to create trace class objects */ | |
afb0f12b | 75 | bool create_trace_class = false; |
4164020e SM |
76 | |
77 | /* | |
78 | * True to keep the plain text when content is appended with | |
79 | * ctf_metadata_decoder_append_content(). | |
80 | */ | |
afb0f12b | 81 | bool keep_plain_text = false; |
a2a54545 PP |
82 | }; |
83 | ||
1fa280c9 SM |
84 | struct ctf_metadata_decoder_deleter |
85 | { | |
86 | void operator()(struct ctf_metadata_decoder *decoder); | |
87 | }; | |
88 | ||
89 | using ctf_metadata_decoder_up = std::unique_ptr<ctf_metadata_decoder, ctf_metadata_decoder_deleter>; | |
90 | ||
1e649dff | 91 | /* |
862ca4ed | 92 | * Creates a CTF metadata decoder. |
1e649dff PP |
93 | * |
94 | * Returns `NULL` on error. | |
95 | */ | |
1fa280c9 | 96 | ctf_metadata_decoder_up |
4164020e | 97 | ctf_metadata_decoder_create(const struct ctf_metadata_decoder_config *config); |
1e649dff PP |
98 | |
99 | /* | |
100 | * Destroys a CTF metadata decoder that you created with | |
101 | * ctf_metadata_decoder_create(). | |
102 | */ | |
4164020e | 103 | void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder *metadata_decoder); |
1e649dff PP |
104 | |
105 | /* | |
06be9946 | 106 | * Appends content to the metadata decoder. |
1e649dff PP |
107 | * |
108 | * This function reads the metadata from the current position of `fp` | |
06be9946 | 109 | * until the end of this file stream. |
1e649dff PP |
110 | * |
111 | * The metadata can be packetized or not. | |
112 | * | |
06be9946 PP |
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 | |
1e649dff PP |
115 | * function returns `CTF_METADATA_DECODER_STATUS_INCOMPLETE`. If this |
116 | * function returns `CTF_METADATA_DECODER_STATUS_INCOMPLETE`, then you | |
06be9946 | 117 | * need to call it again with the _same_ metadata and more to make it |
1e649dff PP |
118 | * complete. For example: |
119 | * | |
120 | * First call: event { name = hell | |
121 | * Second call: event { name = hello_world; ... }; | |
122 | * | |
1e649dff PP |
123 | * If everything goes as expected, this function returns |
124 | * `CTF_METADATA_DECODER_STATUS_OK`. | |
125 | */ | |
4164020e SM |
126 | enum ctf_metadata_decoder_status |
127 | ctf_metadata_decoder_append_content(struct ctf_metadata_decoder *metadata_decoder, FILE *fp); | |
1e649dff | 128 | |
06be9946 PP |
129 | /* |
130 | * Returns the trace IR trace class of this metadata decoder (new | |
131 | * reference). | |
132 | * | |
133 | * Returns `NULL` if there's none yet or if the metadata decoder is not | |
134 | * configured to create trace classes. | |
135 | */ | |
4164020e | 136 | bt_trace_class *ctf_metadata_decoder_get_ir_trace_class(struct ctf_metadata_decoder *mdec); |
44c440bc | 137 | |
06be9946 PP |
138 | /* |
139 | * Returns the CTF IR trace class of this metadata decoder. | |
140 | * | |
141 | * Returns `NULL` if there's none yet or if the metadata decoder is not | |
142 | * configured to create trace classes. | |
143 | */ | |
4164020e SM |
144 | struct ctf_trace_class * |
145 | ctf_metadata_decoder_borrow_ctf_trace_class(struct ctf_metadata_decoder *mdec); | |
1e649dff PP |
146 | |
147 | /* | |
06be9946 PP |
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 | |
151 | * packet. | |
1e649dff | 152 | */ |
4164020e | 153 | int ctf_metadata_decoder_is_packetized(FILE *fp, bool *is_packetized, int *byte_order, |
0f5c5d5c | 154 | const bt2c::Logger& logger); |
1e649dff PP |
155 | |
156 | /* | |
06be9946 PP |
157 | * Returns the byte order of the decoder's metadata stream as set by the |
158 | * last call to ctf_metadata_decoder_append_content(). | |
159 | * | |
160 | * Returns -1 if unknown (plain text content). | |
1e649dff | 161 | */ |
06be9946 PP |
162 | int ctf_metadata_decoder_get_byte_order(struct ctf_metadata_decoder *mdec); |
163 | ||
164 | /* | |
165 | * Returns the UUID of the decoder's metadata stream as set by the last | |
166 | * call to ctf_metadata_decoder_append_content(). | |
1a6da3f9 | 167 | */ |
4164020e | 168 | int ctf_metadata_decoder_get_uuid(struct ctf_metadata_decoder *mdec, bt_uuid_t uuid); |
1a6da3f9 PP |
169 | |
170 | /* | |
171 | * Returns the UUID of the decoder's trace class, if available. | |
06be9946 | 172 | * |
1a6da3f9 PP |
173 | * Returns: |
174 | * | |
175 | * * `CTF_METADATA_DECODER_STATUS_OK`: success. | |
176 | * * `CTF_METADATA_DECODER_STATUS_NONE`: no UUID. | |
177 | * * `CTF_METADATA_DECODER_STATUS_INCOMPLETE`: missing metadata content. | |
06be9946 | 178 | */ |
4164020e SM |
179 | enum ctf_metadata_decoder_status |
180 | ctf_metadata_decoder_get_trace_class_uuid(struct ctf_metadata_decoder *mdec, bt_uuid_t uuid); | |
06be9946 PP |
181 | |
182 | /* | |
183 | * Returns the metadata decoder's current metadata text. | |
184 | */ | |
06be9946 | 185 | const char *ctf_metadata_decoder_get_text(struct ctf_metadata_decoder *mdec); |
1e649dff | 186 | |
4164020e SM |
187 | static inline bool ctf_metadata_decoder_is_packet_version_valid(unsigned int major, |
188 | unsigned int minor) | |
3c8252a5 | 189 | { |
4164020e | 190 | return major == 1 && minor == 8; |
3c8252a5 PP |
191 | } |
192 | ||
1e649dff | 193 | #endif /* _METADATA_DECODER_H */ |