Make API CTF-agnostic
[babeltrace.git] / plugins / ctf / common / btr / btr.h
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>
32 #include <babeltrace/babeltrace.h>
33 #include <babeltrace/babeltrace-internal.h>
34
35 #include "../metadata/ctf-meta.h"
36
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 */
56 enum bt_btr_status {
57 /** Out of memory. */
58 BT_BTR_STATUS_ENOMEM = -5,
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 *
64 * The user needs to call bt_btr_continue() as long as
65 * #BT_BTR_STATUS_EOF is returned to complete the decoding
66 * process of a given type.
67 */
68 BT_BTR_STATUS_EOF = 1,
69
70 /** Invalid argument. */
71 BT_BTR_STATUS_INVAL = -3,
72
73 /** General error. */
74 BT_BTR_STATUS_ERROR = -1,
75
76 /** Everything okay. */
77 BT_BTR_STATUS_OK = 0,
78 };
79
80 /** Type reader. */
81 struct bt_btr;
82
83 typedef enum bt_btr_status (* bt_btr_unsigned_int_cb_func)(uint64_t,
84 struct ctf_field_type *, void *);
85
86 /*
87 * Type reader user callback functions.
88 */
89 struct bt_btr_cbs {
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 *
98 * Each function also receives the CTF field type associated
99 * with the call, and user data (registered to the type reader
100 * calling them).
101 *
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).
105 *
106 * All the type callback functions return one of the following
107 * values:
108 *
109 * - <b>#BT_BTR_STATUS_OK</b>: Everything is okay;
110 * continue the decoding process.
111 * - <b>#BT_BTR_STATUS_ERROR</b>: General error (reported
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
125 * @param type Integer or enumeration type
126 * @param data User data
127 * @returns #BT_BTR_STATUS_OK or
128 * #BT_BTR_STATUS_ERROR
129 */
130 enum bt_btr_status (* signed_int)(int64_t value,
131 struct ctf_field_type *type, void *data);
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
140 * @param type Integer or enumeration type
141 * @param data User data
142 * @returns #BT_BTR_STATUS_OK or
143 * #BT_BTR_STATUS_ERROR
144 */
145 bt_btr_unsigned_int_cb_func unsigned_int;
146
147 /**
148 * Called when a floating point number type is
149 * completely decoded.
150 *
151 * @param value Floating point number value
152 * @param type Floating point number type
153 * @param data User data
154 * @returns #BT_BTR_STATUS_OK or
155 * #BT_BTR_STATUS_ERROR
156 */
157 enum bt_btr_status (* floating_point)(double value,
158 struct ctf_field_type *type, void *data);
159
160 /**
161 * Called when a string type begins.
162 *
163 * All the following user callback function calls will
164 * be made to bt_btr_cbs::types::string(), each of
165 * them providing one substring of the complete string
166 * type's value.
167 *
168 * @param type Beginning string type
169 * @param data User data
170 * @returns #BT_BTR_STATUS_OK or
171 * #BT_BTR_STATUS_ERROR
172 */
173 enum bt_btr_status (* string_begin)(
174 struct ctf_field_type *type, void *data);
175
176 /**
177 * Called when a string type's substring is decoded
178 * (between a call to bt_btr_cbs::types::string_begin()
179 * and a call to bt_btr_cbs::types::string_end()).
180 *
181 * @param value String value (\em not null-terminated)
182 * @param len String value length
183 * @param type String type
184 * @param data User data
185 * @returns #BT_BTR_STATUS_OK or
186 * #BT_BTR_STATUS_ERROR
187 */
188 enum bt_btr_status (* string)(const char *value,
189 size_t len, struct ctf_field_type *type,
190 void *data);
191
192 /**
193 * Called when a string type ends.
194 *
195 * @param type Ending string type
196 * @param data User data
197 * @returns #BT_BTR_STATUS_OK or
198 * #BT_BTR_STATUS_ERROR
199 */
200 enum bt_btr_status (* string_end)(
201 struct ctf_field_type *type, void *data);
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
209 * bt_btr_cbs::types::compound_end() is called.
210 *
211 * If \p type is a variant type, then only one type
212 * callback function call will follow before the call to
213 * bt_btr_cbs::types::compound_end(). This single
214 * call indicates the selected type of this variant
215 * type.
216 *
217 * @param type Beginning compound type
218 * @param data User data
219 * @returns #BT_BTR_STATUS_OK or
220 * #BT_BTR_STATUS_ERROR
221 */
222 enum bt_btr_status (* compound_begin)(
223 struct ctf_field_type *type, void *data);
224
225 /**
226 * Called when a compound type ends.
227 *
228 * @param type Ending compound type
229 * @param data User data
230 * @returns #BT_BTR_STATUS_OK or
231 * #BT_BTR_STATUS_ERROR
232 */
233 enum bt_btr_status (* compound_end)(
234 struct ctf_field_type *type, void *data);
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 *
250 * @param type Sequence type
251 * @param data User data
252 * @returns Sequence length or
253 * #BT_BTR_STATUS_ERROR on error
254 */
255 int64_t (* get_sequence_length)(struct ctf_field_type *type,
256 void *data);
257
258 /**
259 * Called to query the current selected type of a given
260 * variant type.
261 *
262 * @param type Variant type
263 * @param data User data
264 * @returns Current selected type (owned by
265 * this) or \c NULL on error
266 */
267 struct ctf_field_type * (* borrow_variant_selected_field_type)(
268 struct ctf_field_type *type, void *data);
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)
277 * @returns New binary type reader on success, or \c NULL on error
278 */
279 BT_HIDDEN
280 struct bt_btr *bt_btr_create(struct bt_btr_cbs cbs, void *data);
281
282 /**
283 * Destroys a CTF binary type reader, freeing all internal resources.
284 *
285 * @param btr Binary type reader
286 */
287 BT_HIDDEN
288 void bt_btr_destroy(struct bt_btr *btr);
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 *
298 * - <b>#BT_BTR_STATUS_OK</b>: Decoding is done.
299 * - <b>#BT_BTR_STATUS_EOF</b>: The end of the buffer was reached,
300 * but more data is needed to finish the decoding process of the
301 * requested type. The user needs to call bt_btr_continue()
302 * as long as #BT_BTR_STATUS_EOF is returned to complete the
303 * decoding process of the original type.
304 * - <b>#BT_BTR_STATUS_INVAL</b>: Invalid argument.
305 * - <b>#BT_BTR_STATUS_ERROR</b>: General error.
306 *
307 * Calling this function resets the type reader's internal state. If
308 * #BT_BTR_STATUS_EOF is returned, bt_btr_continue() needs to
309 * be called next, \em not bt_btr_decode().
310 *
311 * @param btr Binary type reader
312 * @param type Type to decode
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)
319 * @returns Number of consumed bits
320 */
321 BT_HIDDEN
322 size_t bt_btr_start(struct bt_btr *btr,
323 struct ctf_field_type *type, const uint8_t *buf,
324 size_t offset, size_t packet_offset, size_t sz,
325 enum bt_btr_status *status);
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 *
335 * - <b>#BT_BTR_STATUS_OK</b>: decoding is done.
336 * - <b>#BT_BTR_STATUS_EOF</b>: the end of the buffer was reached,
337 * but more data is needed to finish the decoding process of the
338 * requested type. The user needs to call bt_btr_continue()
339 * as long as #BT_BTR_STATUS_EOF is returned to complete the
340 * decoding process of the original type.
341 * - <b>#BT_BTR_STATUS_INVAL</b>: invalid argument.
342 * - <b>#BT_BTR_STATUS_ERROR</b>: general error.
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)
348 * @returns Number of consumed bits
349 */
350 BT_HIDDEN
351 size_t bt_btr_continue(struct bt_btr *btr,
352 const uint8_t *buf, size_t sz,
353 enum bt_btr_status *status);
354
355 BT_HIDDEN
356 void bt_btr_set_unsigned_int_cb(struct bt_btr *btr,
357 bt_btr_unsigned_int_cb_func cb);
358
359 static inline
360 const char *bt_btr_status_string(enum bt_btr_status status)
361 {
362 switch (status) {
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";
373 default:
374 return "(unknown)";
375 }
376 }
377
378 #endif /* CTF_BTR_H */
This page took 0.036264 seconds and 4 git commands to generate.