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