Commit | Line | Data |
---|---|---|
5cd6d0e5 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
5cd6d0e5 PP |
3 | * |
4 | * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation | |
5 | * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com> | |
6 | * | |
0235b0db | 7 | * Babeltrace - CTF binary field class reader (BFCR) |
5cd6d0e5 PP |
8 | */ |
9 | ||
0235b0db MJ |
10 | #ifndef CTF_BFCR_H |
11 | #define CTF_BFCR_H | |
12 | ||
5cd6d0e5 | 13 | #include <stddef.h> |
c802cacb | 14 | #include <stdint.h> |
5cd6d0e5 | 15 | #include <stdio.h> |
c802cacb | 16 | |
3fadfbc0 | 17 | #include <babeltrace2/babeltrace.h> |
c802cacb | 18 | |
91d81473 | 19 | #include "common/macros.h" |
5cd6d0e5 | 20 | |
087cd0f5 | 21 | #include "../metadata/ctf-meta.hpp" |
5cd6d0e5 PP |
22 | |
23 | /** | |
24 | * @file bfcr.h | |
25 | * | |
26 | * Event-driven CTF binary field class reader (BFCR). | |
27 | * | |
28 | * This is a common, internal API used by CTF source plugins. It allows | |
29 | * a binary CTF IR field class to be decoded from user-provided buffers. | |
30 | * As the class is decoded (and, possibly, its nested classes), | |
31 | * registered user callback functions are called. | |
32 | * | |
33 | * This API is only concerned with reading one CTF class at a time from | |
34 | * one or more buffer of bytes. It does not know CTF dynamic scopes, | |
35 | * events, or streams. Sequence lengths and selected variant classes are | |
36 | * requested to the user when needed. | |
37 | */ | |
38 | ||
39 | /** | |
40 | * Binary class reader API status codes. | |
41 | */ | |
4164020e SM |
42 | enum bt_bfcr_status |
43 | { | |
44 | /** Out of memory. */ | |
45 | BT_BFCR_STATUS_ENOMEM = -5, | |
46 | /** | |
47 | * The binary stream reader reached the end of the user-provided | |
48 | * buffer, but data is still needed to finish decoding the | |
49 | * requested class. | |
50 | * | |
51 | * The user needs to call bt_bfcr_continue() as long as | |
52 | * #BT_BFCR_STATUS_EOF is returned to complete the decoding | |
53 | * process of a given class. | |
54 | */ | |
55 | BT_BFCR_STATUS_EOF = 1, | |
5cd6d0e5 | 56 | |
4164020e SM |
57 | /** Invalid argument. */ |
58 | BT_BFCR_STATUS_INVAL = -3, | |
5cd6d0e5 | 59 | |
4164020e SM |
60 | /** General error. */ |
61 | BT_BFCR_STATUS_ERROR = -1, | |
5cd6d0e5 | 62 | |
4164020e SM |
63 | /** Everything okay. */ |
64 | BT_BFCR_STATUS_OK = 0, | |
5cd6d0e5 PP |
65 | }; |
66 | ||
67 | /** Field class reader. */ | |
68 | struct bt_bfcr; | |
69 | ||
4164020e SM |
70 | typedef enum bt_bfcr_status (*bt_bfcr_unsigned_int_cb_func)(uint64_t, struct ctf_field_class *, |
71 | void *); | |
5cd6d0e5 PP |
72 | |
73 | /* | |
74 | * Field class reader user callback functions. | |
75 | */ | |
4164020e SM |
76 | struct bt_bfcr_cbs |
77 | { | |
78 | /** | |
79 | * Field class callback functions. | |
80 | * | |
81 | * This CTF binary class reader is event-driven. The following | |
82 | * functions are called during the decoding process, either when | |
83 | * a compound class begins/ends, or when a basic class is | |
84 | * completely decoded (along with its value). | |
85 | * | |
86 | * Each function also receives the CTF field class associated | |
87 | * with the call, and user data (registered to the class reader | |
88 | * calling them). | |
89 | * | |
90 | * Actual trace IR fields are \em not created here; this would | |
91 | * be the responsibility of a class reader's user (the provider | |
92 | * of those callback functions). | |
93 | * | |
94 | * All the class callback functions return one of the following | |
95 | * values: | |
96 | * | |
97 | * - <b>#BT_BFCR_STATUS_OK</b>: Everything is okay; | |
98 | * continue the decoding process. | |
99 | * - <b>#BT_BFCR_STATUS_ERROR</b>: General error (reported | |
100 | * to class reader's user). | |
101 | * | |
102 | * Any member of this structure may be set to \c NULL, should | |
103 | * a specific message be not needed. | |
104 | */ | |
105 | struct | |
106 | { | |
107 | /** | |
108 | * Called when a signed integer class is completely | |
109 | * decoded. This could also be the supporting signed | |
110 | * integer class of an enumeration class (\p class will | |
111 | * indicate this). | |
112 | * | |
113 | * @param value Signed integer value | |
114 | * @param class Integer or enumeration class | |
115 | * @param data User data | |
116 | * @returns #BT_BFCR_STATUS_OK or | |
117 | * #BT_BFCR_STATUS_ERROR | |
118 | */ | |
119 | enum bt_bfcr_status (*signed_int)(int64_t value, struct ctf_field_class *cls, void *data); | |
5cd6d0e5 | 120 | |
4164020e SM |
121 | /** |
122 | * Called when an unsigned integer class is completely | |
123 | * decoded. This could also be the supporting signed | |
124 | * integer class of an enumeration class (\p class will | |
125 | * indicate this). | |
126 | * | |
127 | * @param value Unsigned integer value | |
128 | * @param class Integer or enumeration class | |
129 | * @param data User data | |
130 | * @returns #BT_BFCR_STATUS_OK or | |
131 | * #BT_BFCR_STATUS_ERROR | |
132 | */ | |
133 | bt_bfcr_unsigned_int_cb_func unsigned_int; | |
5cd6d0e5 | 134 | |
4164020e SM |
135 | /** |
136 | * Called when a floating point number class is | |
137 | * completely decoded. | |
138 | * | |
139 | * @param value Floating point number value | |
140 | * @param class Floating point number class | |
141 | * @param data User data | |
142 | * @returns #BT_BFCR_STATUS_OK or | |
143 | * #BT_BFCR_STATUS_ERROR | |
144 | */ | |
145 | enum bt_bfcr_status (*floating_point)(double value, struct ctf_field_class *cls, | |
146 | void *data); | |
5cd6d0e5 | 147 | |
4164020e SM |
148 | /** |
149 | * Called when a string class begins. | |
150 | * | |
151 | * All the following user callback function calls will | |
152 | * be made to bt_bfcr_cbs::classes::string(), each of | |
153 | * them providing one substring of the complete string | |
154 | * class's value. | |
155 | * | |
156 | * @param class Beginning string class | |
157 | * @param data User data | |
158 | * @returns #BT_BFCR_STATUS_OK or | |
159 | * #BT_BFCR_STATUS_ERROR | |
160 | */ | |
161 | enum bt_bfcr_status (*string_begin)(struct ctf_field_class *cls, void *data); | |
5cd6d0e5 | 162 | |
4164020e SM |
163 | /** |
164 | * Called when a string class's substring is decoded | |
165 | * (between a call to bt_bfcr_cbs::classes::string_begin() | |
166 | * and a call to bt_bfcr_cbs::classes::string_end()). | |
167 | * | |
168 | * @param value String value (\em not null-terminated) | |
169 | * @param len String value length | |
170 | * @param class String class | |
171 | * @param data User data | |
172 | * @returns #BT_BFCR_STATUS_OK or | |
173 | * #BT_BFCR_STATUS_ERROR | |
174 | */ | |
175 | enum bt_bfcr_status (*string)(const char *value, size_t len, struct ctf_field_class *cls, | |
176 | void *data); | |
5cd6d0e5 | 177 | |
4164020e SM |
178 | /** |
179 | * Called when a string class ends. | |
180 | * | |
181 | * @param class Ending string class | |
182 | * @param data User data | |
183 | * @returns #BT_BFCR_STATUS_OK or | |
184 | * #BT_BFCR_STATUS_ERROR | |
185 | */ | |
186 | enum bt_bfcr_status (*string_end)(struct ctf_field_class *cls, void *data); | |
5cd6d0e5 | 187 | |
4164020e SM |
188 | /** |
189 | * Called when a compound class begins. | |
190 | * | |
191 | * All the following class callback function calls will | |
192 | * signal sequential elements of this compound class, | |
193 | * until the next corresponding | |
194 | * bt_bfcr_cbs::classes::compound_end() is called. | |
195 | * | |
196 | * If \p class is a variant class, then only one class | |
197 | * callback function call will follow before the call to | |
198 | * bt_bfcr_cbs::classes::compound_end(). This single | |
199 | * call indicates the selected class of this variant | |
200 | * class. | |
201 | * | |
202 | * @param class Beginning compound class | |
203 | * @param data User data | |
204 | * @returns #BT_BFCR_STATUS_OK or | |
205 | * #BT_BFCR_STATUS_ERROR | |
206 | */ | |
207 | enum bt_bfcr_status (*compound_begin)(struct ctf_field_class *cls, void *data); | |
5cd6d0e5 | 208 | |
4164020e SM |
209 | /** |
210 | * Called when a compound class ends. | |
211 | * | |
212 | * @param class Ending compound class | |
213 | * @param data User data | |
214 | * @returns #BT_BFCR_STATUS_OK or | |
215 | * #BT_BFCR_STATUS_ERROR | |
216 | */ | |
217 | enum bt_bfcr_status (*compound_end)(struct ctf_field_class *cls, void *data); | |
218 | } classes; | |
5cd6d0e5 | 219 | |
4164020e SM |
220 | /** |
221 | * Query callback functions are used when the class reader needs | |
222 | * dynamic information, i.e. a sequence class's current length | |
223 | * or a variant class's current selected class. | |
224 | * | |
225 | * Both functions need to be set unless it is known that no | |
226 | * sequences or variants will have to be decoded. | |
227 | */ | |
228 | struct | |
229 | { | |
230 | /** | |
231 | * Called to query the current length of a given sequence | |
232 | * class. | |
233 | * | |
234 | * @param class Sequence class | |
235 | * @param data User data | |
236 | * @returns Sequence length or | |
237 | * #BT_BFCR_STATUS_ERROR on error | |
238 | */ | |
239 | int64_t (*get_sequence_length)(struct ctf_field_class *cls, void *data); | |
5cd6d0e5 | 240 | |
4164020e SM |
241 | /** |
242 | * Called to query the current selected class of a given | |
243 | * variant class. | |
244 | * | |
245 | * @param class Variant class | |
246 | * @param data User data | |
247 | * @returns Current selected class (owned by | |
248 | * this) or \c NULL on error | |
249 | */ | |
250 | struct ctf_field_class *(*borrow_variant_selected_field_class)(struct ctf_field_class *cls, | |
251 | void *data); | |
252 | } query; | |
5cd6d0e5 PP |
253 | }; |
254 | ||
255 | /** | |
256 | * Creates a CTF binary class reader. | |
257 | * | |
258 | * @param cbs User callback functions | |
259 | * @param data User data (passed to user callback functions) | |
260 | * @returns New binary class reader on success, or \c NULL on error | |
261 | */ | |
4164020e SM |
262 | struct bt_bfcr *bt_bfcr_create(struct bt_bfcr_cbs cbs, void *data, bt_logging_level log_level, |
263 | bt_self_component *self_comp); | |
5cd6d0e5 PP |
264 | |
265 | /** | |
266 | * Destroys a CTF binary class reader, freeing all internal resources. | |
267 | * | |
268 | * @param bfcr Binary class reader | |
269 | */ | |
5cd6d0e5 PP |
270 | void bt_bfcr_destroy(struct bt_bfcr *bfcr); |
271 | ||
272 | /** | |
273 | * Decodes a given CTF class from a buffer of bytes. | |
274 | * | |
275 | * The number of \em bits consumed by this function is returned. | |
276 | * | |
277 | * The \p status output parameter is where a status is written, amongst | |
278 | * the following: | |
279 | * | |
280 | * - <b>#BT_BFCR_STATUS_OK</b>: Decoding is done. | |
281 | * - <b>#BT_BFCR_STATUS_EOF</b>: The end of the buffer was reached, | |
282 | * but more data is needed to finish the decoding process of the | |
283 | * requested class. The user needs to call bt_bfcr_continue() | |
284 | * as long as #BT_BFCR_STATUS_EOF is returned to complete the | |
285 | * decoding process of the original class. | |
286 | * - <b>#BT_BFCR_STATUS_INVAL</b>: Invalid argument. | |
287 | * - <b>#BT_BFCR_STATUS_ERROR</b>: General error. | |
288 | * | |
289 | * Calling this function resets the class reader's internal state. If | |
290 | * #BT_BFCR_STATUS_EOF is returned, bt_bfcr_continue() needs to | |
291 | * be called next, \em not bt_bfcr_decode(). | |
292 | * | |
293 | * @param bfcr Binary class reader | |
294 | * @param class Field class to decode | |
295 | * @param buf Buffer | |
296 | * @param offset Offset of first bit from \p buf (bits) | |
297 | * @param packet_offset Offset of \p offset within the CTF | |
298 | * binary packet containing \p class (bits) | |
299 | * @param sz Size of buffer in bytes (from \p buf) | |
300 | * @param status Returned status (see description above) | |
301 | * @returns Number of consumed bits | |
302 | */ | |
4164020e SM |
303 | size_t bt_bfcr_start(struct bt_bfcr *bfcr, struct ctf_field_class *cls, const uint8_t *buf, |
304 | size_t offset, size_t packet_offset, size_t sz, enum bt_bfcr_status *status); | |
5cd6d0e5 PP |
305 | |
306 | /** | |
307 | * Continues the decoding process a given CTF class. | |
308 | * | |
309 | * The number of bits consumed by this function is returned. | |
310 | * | |
311 | * The \p status output parameter is where a status is placed, amongst | |
312 | * the following: | |
313 | * | |
314 | * - <b>#BT_BFCR_STATUS_OK</b>: decoding is done. | |
315 | * - <b>#BT_BFCR_STATUS_EOF</b>: the end of the buffer was reached, | |
316 | * but more data is needed to finish the decoding process of the | |
317 | * requested class. The user needs to call bt_bfcr_continue() | |
318 | * as long as #BT_BFCR_STATUS_EOF is returned to complete the | |
319 | * decoding process of the original class. | |
320 | * - <b>#BT_BFCR_STATUS_INVAL</b>: invalid argument. | |
321 | * - <b>#BT_BFCR_STATUS_ERROR</b>: general error. | |
322 | * | |
323 | * @param bfcr Binary class reader | |
324 | * @param buf Buffer | |
325 | * @param sz Size of buffer in bytes (from \p offset) | |
326 | * @param status Returned status (see description above) | |
327 | * @returns Number of consumed bits | |
328 | */ | |
4164020e SM |
329 | size_t bt_bfcr_continue(struct bt_bfcr *bfcr, const uint8_t *buf, size_t sz, |
330 | enum bt_bfcr_status *status); | |
5cd6d0e5 | 331 | |
4164020e | 332 | void bt_bfcr_set_unsigned_int_cb(struct bt_bfcr *bfcr, bt_bfcr_unsigned_int_cb_func cb); |
5cd6d0e5 | 333 | |
4164020e | 334 | static inline const char *bt_bfcr_status_string(enum bt_bfcr_status status) |
5cd6d0e5 | 335 | { |
4164020e SM |
336 | switch (status) { |
337 | case BT_BFCR_STATUS_ENOMEM: | |
338 | return "ENOMEM"; | |
339 | case BT_BFCR_STATUS_EOF: | |
340 | return "EOF"; | |
341 | case BT_BFCR_STATUS_INVAL: | |
342 | return "INVAL"; | |
343 | case BT_BFCR_STATUS_ERROR: | |
344 | return "ERROR"; | |
345 | case BT_BFCR_STATUS_OK: | |
346 | return "OK"; | |
347 | } | |
00dff0ec | 348 | |
4164020e | 349 | bt_common_abort(); |
5cd6d0e5 PP |
350 | } |
351 | ||
352 | #endif /* CTF_BFCR_H */ |