Commit | Line | Data |
---|---|---|
e98a2d6e PP |
1 | #ifndef CTF_BTR_H |
2 | #define CTF_BTR_H | |
3 | ||
4 | /* | |
5 | * Babeltrace - CTF binary type reader (BTR) | |
6 | * | |
7 | * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation | |
8 | * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
29 | #include <stdint.h> | |
30 | #include <stddef.h> | |
31 | #include <stdio.h> | |
e0831b38 | 32 | #include <babeltrace/babeltrace.h> |
e98a2d6e PP |
33 | #include <babeltrace/babeltrace-internal.h> |
34 | ||
7b33a0e0 PP |
35 | #include "../metadata/ctf-meta.h" |
36 | ||
e98a2d6e PP |
37 | /** |
38 | * @file ctf-btr.h | |
39 | * | |
40 | * Event-driven CTF binary type reader (BTR). | |
41 | * | |
42 | * This is a common, internal API used by CTF source plugins. It allows | |
43 | * a binary CTF IR field type to be decoded from user-provided buffers. | |
44 | * As the type is decoded (and, possibly, its nested types), registered | |
45 | * user callback functions are called. | |
46 | * | |
47 | * This API is only concerned with reading one CTF type at a time from | |
48 | * one or more buffer of bytes. It does not know CTF dynamic scopes, | |
49 | * events, or streams. Sequence lengths and selected variant types are | |
50 | * requested to the user when needed. | |
51 | */ | |
52 | ||
53 | /** | |
54 | * Binary type reader API status codes. | |
55 | */ | |
839d52a5 | 56 | enum bt_btr_status { |
c44c3e70 | 57 | /** Out of memory. */ |
a6918753 | 58 | BT_BTR_STATUS_ENOMEM = -5, |
e98a2d6e PP |
59 | /** |
60 | * The binary stream reader reached the end of the user-provided | |
61 | * buffer, but data is still needed to finish decoding the | |
62 | * requested type. | |
63 | * | |
839d52a5 PP |
64 | * The user needs to call bt_btr_continue() as long as |
65 | * #BT_BTR_STATUS_EOF is returned to complete the decoding | |
e98a2d6e PP |
66 | * process of a given type. |
67 | */ | |
839d52a5 | 68 | BT_BTR_STATUS_EOF = 1, |
e98a2d6e PP |
69 | |
70 | /** Invalid argument. */ | |
a6918753 | 71 | BT_BTR_STATUS_INVAL = -3, |
e98a2d6e PP |
72 | |
73 | /** General error. */ | |
a6918753 | 74 | BT_BTR_STATUS_ERROR = -1, |
e98a2d6e PP |
75 | |
76 | /** Everything okay. */ | |
839d52a5 | 77 | BT_BTR_STATUS_OK = 0, |
e98a2d6e PP |
78 | }; |
79 | ||
80 | /** Type reader. */ | |
839d52a5 | 81 | struct bt_btr; |
e98a2d6e | 82 | |
7b33a0e0 PP |
83 | typedef enum bt_btr_status (* bt_btr_unsigned_int_cb_func)(uint64_t, |
84 | struct ctf_field_type *, void *); | |
85 | ||
e98a2d6e PP |
86 | /* |
87 | * Type reader user callback functions. | |
88 | */ | |
839d52a5 | 89 | struct bt_btr_cbs { |
e98a2d6e PP |
90 | /** |
91 | * Type callback functions. | |
92 | * | |
93 | * This CTF binary type reader is event-driven. The following | |
94 | * functions are called during the decoding process, either when | |
95 | * a compound type begins/ends, or when a basic type is | |
96 | * completely decoded (along with its value). | |
97 | * | |
7b33a0e0 | 98 | * Each function also receives the CTF field type associated |
e98a2d6e | 99 | * with the call, and user data (registered to the type reader |
7b33a0e0 | 100 | * calling them). |
e98a2d6e | 101 | * |
7b33a0e0 PP |
102 | * Actual trace IR fields are \em not created here; this would |
103 | * be the responsibility of a type reader's user (the provider | |
104 | * of those callback functions). | |
e98a2d6e PP |
105 | * |
106 | * All the type callback functions return one of the following | |
107 | * values: | |
108 | * | |
839d52a5 | 109 | * - <b>#BT_BTR_STATUS_OK</b>: Everything is okay; |
e98a2d6e | 110 | * continue the decoding process. |
839d52a5 | 111 | * - <b>#BT_BTR_STATUS_ERROR</b>: General error (reported |
e98a2d6e PP |
112 | * to type reader's user). |
113 | * | |
114 | * Any member of this structure may be set to \c NULL, should | |
115 | * a specific notification be not needed. | |
116 | */ | |
117 | struct { | |
118 | /** | |
119 | * Called when a signed integer type is completely | |
120 | * decoded. This could also be the supporting signed | |
121 | * integer type of an enumeration type (\p type will | |
122 | * indicate this). | |
123 | * | |
124 | * @param value Signed integer value | |
7b33a0e0 | 125 | * @param type Integer or enumeration type |
e98a2d6e | 126 | * @param data User data |
839d52a5 PP |
127 | * @returns #BT_BTR_STATUS_OK or |
128 | * #BT_BTR_STATUS_ERROR | |
e98a2d6e | 129 | */ |
839d52a5 | 130 | enum bt_btr_status (* signed_int)(int64_t value, |
7b33a0e0 | 131 | struct ctf_field_type *type, void *data); |
e98a2d6e PP |
132 | |
133 | /** | |
134 | * Called when an unsigned integer type is completely | |
135 | * decoded. This could also be the supporting signed | |
136 | * integer type of an enumeration type (\p type will | |
137 | * indicate this). | |
138 | * | |
139 | * @param value Unsigned integer value | |
7b33a0e0 | 140 | * @param type Integer or enumeration type |
e98a2d6e | 141 | * @param data User data |
839d52a5 PP |
142 | * @returns #BT_BTR_STATUS_OK or |
143 | * #BT_BTR_STATUS_ERROR | |
e98a2d6e | 144 | */ |
7b33a0e0 | 145 | bt_btr_unsigned_int_cb_func unsigned_int; |
e98a2d6e PP |
146 | |
147 | /** | |
148 | * Called when a floating point number type is | |
149 | * completely decoded. | |
150 | * | |
151 | * @param value Floating point number value | |
7b33a0e0 | 152 | * @param type Floating point number type |
e98a2d6e | 153 | * @param data User data |
839d52a5 PP |
154 | * @returns #BT_BTR_STATUS_OK or |
155 | * #BT_BTR_STATUS_ERROR | |
e98a2d6e | 156 | */ |
839d52a5 | 157 | enum bt_btr_status (* floating_point)(double value, |
7b33a0e0 | 158 | struct ctf_field_type *type, void *data); |
e98a2d6e PP |
159 | |
160 | /** | |
161 | * Called when a string type begins. | |
162 | * | |
163 | * All the following user callback function calls will | |
839d52a5 | 164 | * be made to bt_btr_cbs::types::string(), each of |
e98a2d6e PP |
165 | * them providing one substring of the complete string |
166 | * type's value. | |
167 | * | |
7b33a0e0 | 168 | * @param type Beginning string type |
e98a2d6e | 169 | * @param data User data |
839d52a5 PP |
170 | * @returns #BT_BTR_STATUS_OK or |
171 | * #BT_BTR_STATUS_ERROR | |
e98a2d6e | 172 | */ |
839d52a5 | 173 | enum bt_btr_status (* string_begin)( |
7b33a0e0 | 174 | struct ctf_field_type *type, void *data); |
e98a2d6e PP |
175 | |
176 | /** | |
177 | * Called when a string type's substring is decoded | |
839d52a5 PP |
178 | * (between a call to bt_btr_cbs::types::string_begin() |
179 | * and a call to bt_btr_cbs::types::string_end()). | |
e98a2d6e PP |
180 | * |
181 | * @param value String value (\em not null-terminated) | |
182 | * @param len String value length | |
7b33a0e0 | 183 | * @param type String type |
e98a2d6e | 184 | * @param data User data |
839d52a5 PP |
185 | * @returns #BT_BTR_STATUS_OK or |
186 | * #BT_BTR_STATUS_ERROR | |
e98a2d6e | 187 | */ |
839d52a5 | 188 | enum bt_btr_status (* string)(const char *value, |
7b33a0e0 | 189 | size_t len, struct ctf_field_type *type, |
e98a2d6e PP |
190 | void *data); |
191 | ||
192 | /** | |
193 | * Called when a string type ends. | |
194 | * | |
7b33a0e0 | 195 | * @param type Ending string type |
e98a2d6e | 196 | * @param data User data |
839d52a5 PP |
197 | * @returns #BT_BTR_STATUS_OK or |
198 | * #BT_BTR_STATUS_ERROR | |
e98a2d6e | 199 | */ |
839d52a5 | 200 | enum bt_btr_status (* string_end)( |
7b33a0e0 | 201 | struct ctf_field_type *type, void *data); |
e98a2d6e PP |
202 | |
203 | /** | |
204 | * Called when a compound type begins. | |
205 | * | |
206 | * All the following type callback function calls will | |
207 | * signal sequential elements of this compound type, | |
208 | * until the next corresponding | |
839d52a5 | 209 | * bt_btr_cbs::types::compound_end() is called. |
e98a2d6e PP |
210 | * |
211 | * If \p type is a variant type, then only one type | |
212 | * callback function call will follow before the call to | |
839d52a5 | 213 | * bt_btr_cbs::types::compound_end(). This single |
e98a2d6e PP |
214 | * call indicates the selected type of this variant |
215 | * type. | |
216 | * | |
7b33a0e0 | 217 | * @param type Beginning compound type |
e98a2d6e | 218 | * @param data User data |
839d52a5 PP |
219 | * @returns #BT_BTR_STATUS_OK or |
220 | * #BT_BTR_STATUS_ERROR | |
e98a2d6e | 221 | */ |
839d52a5 | 222 | enum bt_btr_status (* compound_begin)( |
7b33a0e0 | 223 | struct ctf_field_type *type, void *data); |
e98a2d6e PP |
224 | |
225 | /** | |
226 | * Called when a compound type ends. | |
227 | * | |
7b33a0e0 | 228 | * @param type Ending compound type |
e98a2d6e | 229 | * @param data User data |
839d52a5 PP |
230 | * @returns #BT_BTR_STATUS_OK or |
231 | * #BT_BTR_STATUS_ERROR | |
e98a2d6e | 232 | */ |
839d52a5 | 233 | enum bt_btr_status (* compound_end)( |
7b33a0e0 | 234 | struct ctf_field_type *type, void *data); |
e98a2d6e PP |
235 | } types; |
236 | ||
237 | /** | |
238 | * Query callback functions are used when the type reader needs | |
239 | * dynamic information, i.e. a sequence type's current length | |
240 | * or a variant type's current selected type. | |
241 | * | |
242 | * Both functions need to be set unless it is known that no | |
243 | * sequences or variants will have to be decoded. | |
244 | */ | |
245 | struct { | |
246 | /** | |
247 | * Called to query the current length of a given sequence | |
248 | * type. | |
249 | * | |
7b33a0e0 | 250 | * @param type Sequence type |
e98a2d6e PP |
251 | * @param data User data |
252 | * @returns Sequence length or | |
839d52a5 | 253 | * #BT_BTR_STATUS_ERROR on error |
e98a2d6e | 254 | */ |
7b33a0e0 | 255 | int64_t (* get_sequence_length)(struct ctf_field_type *type, |
e98a2d6e PP |
256 | void *data); |
257 | ||
258 | /** | |
259 | * Called to query the current selected type of a given | |
260 | * variant type. | |
261 | * | |
7b33a0e0 | 262 | * @param type Variant type |
e98a2d6e PP |
263 | * @param data User data |
264 | * @returns Current selected type (owned by | |
265 | * this) or \c NULL on error | |
266 | */ | |
7b33a0e0 PP |
267 | struct ctf_field_type * (* borrow_variant_selected_field_type)( |
268 | struct ctf_field_type *type, void *data); | |
e98a2d6e PP |
269 | } query; |
270 | }; | |
271 | ||
272 | /** | |
273 | * Creates a CTF binary type reader. | |
274 | * | |
275 | * @param cbs User callback functions | |
276 | * @param data User data (passed to user callback functions) | |
e98a2d6e PP |
277 | * @returns New binary type reader on success, or \c NULL on error |
278 | */ | |
7b33a0e0 | 279 | BT_HIDDEN |
839d52a5 | 280 | struct bt_btr *bt_btr_create(struct bt_btr_cbs cbs, void *data); |
e98a2d6e PP |
281 | |
282 | /** | |
283 | * Destroys a CTF binary type reader, freeing all internal resources. | |
284 | * | |
285 | * @param btr Binary type reader | |
286 | */ | |
7b33a0e0 | 287 | BT_HIDDEN |
839d52a5 | 288 | void bt_btr_destroy(struct bt_btr *btr); |
e98a2d6e PP |
289 | |
290 | /** | |
291 | * Decodes a given CTF type from a buffer of bytes. | |
292 | * | |
293 | * The number of \em bits consumed by this function is returned. | |
294 | * | |
295 | * The \p status output parameter is where a status is written, amongst | |
296 | * the following: | |
297 | * | |
839d52a5 PP |
298 | * - <b>#BT_BTR_STATUS_OK</b>: Decoding is done. |
299 | * - <b>#BT_BTR_STATUS_EOF</b>: The end of the buffer was reached, | |
e98a2d6e | 300 | * but more data is needed to finish the decoding process of the |
839d52a5 PP |
301 | * requested type. The user needs to call bt_btr_continue() |
302 | * as long as #BT_BTR_STATUS_EOF is returned to complete the | |
e98a2d6e | 303 | * decoding process of the original type. |
839d52a5 PP |
304 | * - <b>#BT_BTR_STATUS_INVAL</b>: Invalid argument. |
305 | * - <b>#BT_BTR_STATUS_ERROR</b>: General error. | |
e98a2d6e PP |
306 | * |
307 | * Calling this function resets the type reader's internal state. If | |
839d52a5 PP |
308 | * #BT_BTR_STATUS_EOF is returned, bt_btr_continue() needs to |
309 | * be called next, \em not bt_btr_decode(). | |
e98a2d6e PP |
310 | * |
311 | * @param btr Binary type reader | |
7b33a0e0 | 312 | * @param type Type to decode |
e98a2d6e PP |
313 | * @param buf Buffer |
314 | * @param offset Offset of first bit from \p buf (bits) | |
315 | * @param packet_offset Offset of \p offset within the CTF | |
316 | * binary packet containing \p type (bits) | |
317 | * @param sz Size of buffer in bytes (from \p buf) | |
318 | * @param status Returned status (see description above) | |
400fca1c | 319 | * @returns Number of consumed bits |
e98a2d6e | 320 | */ |
7b33a0e0 | 321 | BT_HIDDEN |
839d52a5 | 322 | size_t bt_btr_start(struct bt_btr *btr, |
7b33a0e0 | 323 | struct ctf_field_type *type, const uint8_t *buf, |
e98a2d6e | 324 | size_t offset, size_t packet_offset, size_t sz, |
839d52a5 | 325 | enum bt_btr_status *status); |
e98a2d6e PP |
326 | |
327 | /** | |
328 | * Continues the decoding process a given CTF type. | |
329 | * | |
330 | * The number of bits consumed by this function is returned. | |
331 | * | |
332 | * The \p status output parameter is where a status is placed, amongst | |
333 | * the following: | |
334 | * | |
839d52a5 PP |
335 | * - <b>#BT_BTR_STATUS_OK</b>: decoding is done. |
336 | * - <b>#BT_BTR_STATUS_EOF</b>: the end of the buffer was reached, | |
e98a2d6e | 337 | * but more data is needed to finish the decoding process of the |
839d52a5 PP |
338 | * requested type. The user needs to call bt_btr_continue() |
339 | * as long as #BT_BTR_STATUS_EOF is returned to complete the | |
e98a2d6e | 340 | * decoding process of the original type. |
839d52a5 PP |
341 | * - <b>#BT_BTR_STATUS_INVAL</b>: invalid argument. |
342 | * - <b>#BT_BTR_STATUS_ERROR</b>: general error. | |
e98a2d6e PP |
343 | * |
344 | * @param btr Binary type reader | |
345 | * @param buf Buffer | |
346 | * @param sz Size of buffer in bytes (from \p offset) | |
347 | * @param status Returned status (see description above) | |
400fca1c | 348 | * @returns Number of consumed bits |
e98a2d6e | 349 | */ |
7b33a0e0 | 350 | BT_HIDDEN |
839d52a5 | 351 | size_t bt_btr_continue(struct bt_btr *btr, |
e98a2d6e | 352 | const uint8_t *buf, size_t sz, |
839d52a5 | 353 | enum bt_btr_status *status); |
e98a2d6e | 354 | |
7b33a0e0 PP |
355 | BT_HIDDEN |
356 | void bt_btr_set_unsigned_int_cb(struct bt_btr *btr, | |
357 | bt_btr_unsigned_int_cb_func cb); | |
358 | ||
dfef8367 | 359 | static inline |
839d52a5 | 360 | const char *bt_btr_status_string(enum bt_btr_status status) |
dfef8367 PP |
361 | { |
362 | switch (status) { | |
839d52a5 PP |
363 | case BT_BTR_STATUS_ENOMEM: |
364 | return "BT_BTR_STATUS_ENOMEM"; | |
365 | case BT_BTR_STATUS_EOF: | |
366 | return "BT_BTR_STATUS_EOF"; | |
367 | case BT_BTR_STATUS_INVAL: | |
368 | return "BT_BTR_STATUS_INVAL"; | |
369 | case BT_BTR_STATUS_ERROR: | |
370 | return "BT_BTR_STATUS_ERROR"; | |
371 | case BT_BTR_STATUS_OK: | |
372 | return "BT_BTR_STATUS_OK"; | |
dfef8367 PP |
373 | default: |
374 | return "(unknown)"; | |
375 | } | |
376 | } | |
377 | ||
e98a2d6e | 378 | #endif /* CTF_BTR_H */ |