Commit | Line | Data |
---|---|---|
b09a5592 PP |
1 | #ifndef CTF_MSG_ITER_H |
2 | #define CTF_MSG_ITER_H | |
e98a2d6e PP |
3 | |
4 | /* | |
b09a5592 | 5 | * Babeltrace - CTF message iterator |
bbbd35bf | 6 | * |
e98a2d6e PP |
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 <stdio.h> | |
31 | #include <stddef.h> | |
71c5da58 | 32 | #include <babeltrace2/babeltrace.h> |
85e7137b | 33 | #include "common/macros.h" |
e98a2d6e | 34 | |
7b33a0e0 PP |
35 | #include "../metadata/ctf-meta.h" |
36 | ||
e98a2d6e | 37 | /** |
b09a5592 | 38 | * @file ctf-msg-iter.h |
e98a2d6e | 39 | * |
b09a5592 | 40 | * CTF message iterator |
bbbd35bf | 41 | * |
e98a2d6e | 42 | * This is a common internal API used by CTF source plugins. It allows |
b09a5592 | 43 | * one to get messages from a user-provided medium. |
e98a2d6e PP |
44 | */ |
45 | ||
46 | /** | |
47 | * Medium operations status codes. | |
48 | */ | |
b09a5592 | 49 | enum bt_msg_iter_medium_status { |
e98a2d6e PP |
50 | /** |
51 | * End of file. | |
52 | * | |
b09a5592 | 53 | * The medium function called by the message iterator |
e98a2d6e PP |
54 | * function reached the end of the file. |
55 | */ | |
b09a5592 | 56 | BT_MSG_ITER_MEDIUM_STATUS_EOF = 1, |
e98a2d6e PP |
57 | |
58 | /** | |
59 | * There is no data available right now, try again later. | |
60 | */ | |
b09a5592 | 61 | BT_MSG_ITER_MEDIUM_STATUS_AGAIN = 11, |
bb230c9b JG |
62 | |
63 | /** Unsupported operation. */ | |
b09a5592 | 64 | BT_MSG_ITER_MEDIUM_STATUS_UNSUPPORTED = -3, |
e98a2d6e PP |
65 | |
66 | /** Invalid argument. */ | |
b09a5592 | 67 | BT_MSG_ITER_MEDIUM_STATUS_INVAL = -2, |
e98a2d6e PP |
68 | |
69 | /** General error. */ | |
b09a5592 | 70 | BT_MSG_ITER_MEDIUM_STATUS_ERROR = -1, |
e98a2d6e PP |
71 | |
72 | /** Everything okay. */ | |
b09a5592 | 73 | BT_MSG_ITER_MEDIUM_STATUS_OK = 0, |
e98a2d6e PP |
74 | }; |
75 | ||
76 | /** | |
b09a5592 | 77 | * CTF message iterator API status code. |
e98a2d6e | 78 | */ |
b09a5592 | 79 | enum bt_msg_iter_status { |
e98a2d6e PP |
80 | /** |
81 | * End of file. | |
82 | * | |
b09a5592 | 83 | * The medium function called by the message iterator |
e98a2d6e PP |
84 | * function reached the end of the file. |
85 | */ | |
b09a5592 | 86 | BT_MSG_ITER_STATUS_EOF = BT_MSG_ITER_MEDIUM_STATUS_EOF, |
e98a2d6e PP |
87 | |
88 | /** | |
89 | * There is no data available right now, try again later. | |
90 | * | |
91 | * Some condition resulted in the | |
b09a5592 | 92 | * bt_msg_iter_medium_ops::request_bytes() user function not |
e98a2d6e | 93 | * having access to any data now. You should retry calling the |
b09a5592 | 94 | * last called message iterator function once the situation |
e98a2d6e PP |
95 | * is resolved. |
96 | */ | |
b09a5592 | 97 | BT_MSG_ITER_STATUS_AGAIN = BT_MSG_ITER_MEDIUM_STATUS_AGAIN, |
e98a2d6e PP |
98 | |
99 | /** Invalid argument. */ | |
b09a5592 | 100 | BT_MSG_ITER_STATUS_INVAL = BT_MSG_ITER_MEDIUM_STATUS_INVAL, |
e98a2d6e | 101 | |
bb230c9b | 102 | /** Unsupported operation. */ |
b09a5592 | 103 | BT_MSG_ITER_STATUS_UNSUPPORTED = BT_MSG_ITER_MEDIUM_STATUS_UNSUPPORTED, |
bb230c9b | 104 | |
e98a2d6e | 105 | /** General error. */ |
b09a5592 | 106 | BT_MSG_ITER_STATUS_ERROR = BT_MSG_ITER_MEDIUM_STATUS_ERROR, |
e98a2d6e PP |
107 | |
108 | /** Everything okay. */ | |
b09a5592 | 109 | BT_MSG_ITER_STATUS_OK = 0, |
e98a2d6e PP |
110 | }; |
111 | ||
bb230c9b | 112 | /** |
b09a5592 | 113 | * CTF message iterator seek operation directives. |
bb230c9b | 114 | */ |
b09a5592 | 115 | enum bt_msg_iter_seek_whence { |
bb230c9b JG |
116 | /** |
117 | * Set the iterator's position to an absolute offset in the underlying | |
118 | * medium. | |
119 | */ | |
b09a5592 | 120 | BT_MSG_ITER_SEEK_WHENCE_SET, |
bb230c9b JG |
121 | }; |
122 | ||
e98a2d6e PP |
123 | /** |
124 | * Medium operations. | |
125 | * | |
b09a5592 | 126 | * Those user functions are called by the message iterator |
e98a2d6e PP |
127 | * functions to request medium actions. |
128 | */ | |
b09a5592 | 129 | struct bt_msg_iter_medium_ops { |
e98a2d6e PP |
130 | /** |
131 | * Returns the next byte buffer to be used by the binary file | |
132 | * reader to deserialize binary data. | |
133 | * | |
134 | * This function \em must be defined. | |
135 | * | |
136 | * The purpose of this function is to return a buffer of bytes | |
b09a5592 | 137 | * to the message iterator, of a maximum of \p request_sz |
e98a2d6e PP |
138 | * bytes. If this function cannot return a buffer of at least |
139 | * \p request_sz bytes, it may return a smaller buffer. In | |
140 | * either cases, \p buffer_sz must be set to the returned buffer | |
141 | * size (in bytes). | |
142 | * | |
143 | * The returned buffer's ownership remains the medium, in that | |
b09a5592 PP |
144 | * it won't be freed by the message iterator functions. The |
145 | * returned buffer won't be modified by the message | |
e98a2d6e PP |
146 | * iterator functions either. |
147 | * | |
148 | * When this function is called for the first time for a given | |
149 | * file, the offset within the file is considered to be 0. The | |
150 | * next times this function is called, the returned buffer's | |
151 | * byte offset within the complete file must be the previous | |
152 | * offset plus the last returned value of \p buffer_sz by this | |
153 | * medium. | |
154 | * | |
155 | * This function must return one of the following statuses: | |
156 | * | |
b09a5592 | 157 | * - <b>#BT_MSG_ITER_MEDIUM_STATUS_OK</b>: Everything |
e98a2d6e PP |
158 | * is okay, i.e. \p buffer_sz is set to a positive value |
159 | * reflecting the number of available bytes in the buffer | |
160 | * starting at the address written in \p buffer_addr. | |
b09a5592 PP |
161 | * - <b>#BT_MSG_ITER_MEDIUM_STATUS_AGAIN</b>: No data is |
162 | * available right now. In this case, the message | |
e98a2d6e | 163 | * iterator function called by the user returns |
b09a5592 | 164 | * #BT_MSG_ITER_STATUS_AGAIN, and it is the user's |
e98a2d6e | 165 | * responsibility to make sure enough data becomes available |
b09a5592 | 166 | * before calling the \em same message iterator |
e98a2d6e | 167 | * function again to continue the decoding process. |
b09a5592 | 168 | * - <b>#BT_MSG_ITER_MEDIUM_STATUS_EOF</b>: The end of |
e98a2d6e | 169 | * the file was reached, and no more data will ever be |
b09a5592 | 170 | * available for this file. In this case, the message |
e98a2d6e | 171 | * iterator function called by the user returns |
b09a5592 | 172 | * #BT_MSG_ITER_STATUS_EOF. This must \em not be |
e98a2d6e PP |
173 | * returned when returning at least one byte of data to the |
174 | * caller, i.e. this must be returned when there's | |
175 | * absolutely nothing left; should the request size be | |
176 | * larger than what's left in the file, this function must | |
177 | * return what's left, setting \p buffer_sz to the number of | |
178 | * remaining bytes, and return | |
b09a5592 | 179 | * #BT_MSG_ITER_MEDIUM_STATUS_EOF on the \em following |
e98a2d6e | 180 | * call. |
b09a5592 | 181 | * - <b>#BT_MSG_ITER_MEDIUM_STATUS_ERROR</b>: A fatal |
e98a2d6e | 182 | * error occured during this operation. In this case, the |
b09a5592 PP |
183 | * message iterator function called by the user returns |
184 | * #BT_MSG_ITER_STATUS_ERROR. | |
e98a2d6e | 185 | * |
b09a5592 | 186 | * If #BT_MSG_ITER_MEDIUM_STATUS_OK is not returned, the |
e98a2d6e PP |
187 | * values of \p buffer_sz and \p buffer_addr are \em ignored by |
188 | * the caller. | |
189 | * | |
190 | * @param request_sz Requested buffer size (bytes) | |
191 | * @param buffer_addr Returned buffer address | |
192 | * @param buffer_sz Returned buffer's size (bytes) | |
193 | * @param data User data | |
194 | * @returns Status code (see description above) | |
195 | */ | |
bbbd35bf PP |
196 | enum bt_msg_iter_medium_status (* request_bytes)(size_t request_sz, |
197 | uint8_t **buffer_addr, size_t *buffer_sz, void *data); | |
e98a2d6e | 198 | |
bb230c9b JG |
199 | /** |
200 | * Repositions the underlying stream's position. | |
201 | * | |
202 | * This *optional* method repositions the underlying stream | |
203 | * to a given absolute or relative position, as indicated by | |
204 | * the whence directive. | |
205 | * | |
b09a5592 | 206 | * @param whence One of #bt_msg_iter_seek_whence values |
bb230c9b JG |
207 | * @param offset Offset to use for the given directive |
208 | * @param data User data | |
b09a5592 | 209 | * @returns One of #bt_msg_iter_medium_status values |
bb230c9b | 210 | */ |
b09a5592 PP |
211 | enum bt_msg_iter_medium_status (* seek)( |
212 | enum bt_msg_iter_seek_whence whence, | |
bb230c9b JG |
213 | off_t offset, void *data); |
214 | ||
e98a2d6e PP |
215 | /** |
216 | * Returns a stream instance (weak reference) for the given | |
217 | * stream class. | |
218 | * | |
219 | * This is called after a packet header is read, and the | |
b09a5592 | 220 | * corresponding stream class is found by the message |
e98a2d6e PP |
221 | * iterator. |
222 | * | |
0659c536 PP |
223 | * @param stream_class Stream class of the stream to get |
224 | * @param stream_id Stream (instance) ID of the stream | |
225 | * to get (-1ULL if not available) | |
e98a2d6e PP |
226 | * @param data User data |
227 | * @returns Stream instance (weak reference) or | |
228 | * \c NULL on error | |
229 | */ | |
bbbd35bf | 230 | bt_stream * (* borrow_stream)(bt_stream_class *stream_class, |
7b33a0e0 | 231 | int64_t stream_id, void *data); |
e98a2d6e PP |
232 | }; |
233 | ||
b09a5592 PP |
234 | /** CTF message iterator. */ |
235 | struct bt_msg_iter; | |
e98a2d6e | 236 | |
e98a2d6e | 237 | /** |
b09a5592 | 238 | * Creates a CTF message iterator. |
e98a2d6e PP |
239 | * |
240 | * Upon successful completion, the reference count of \p trace is | |
241 | * incremented. | |
242 | * | |
243 | * @param trace Trace to read | |
244 | * @param max_request_sz Maximum buffer size, in bytes, to | |
245 | * request to | |
b09a5592 | 246 | * bt_msg_iter_medium_ops::request_bytes() |
e98a2d6e PP |
247 | * at a time |
248 | * @param medops Medium operations | |
249 | * @param medops_data User data (passed to medium operations) | |
b09a5592 | 250 | * @returns New CTF message iterator on |
e98a2d6e PP |
251 | * success, or \c NULL on error |
252 | */ | |
2cf1d51e | 253 | BT_HIDDEN |
b09a5592 PP |
254 | struct bt_msg_iter *bt_msg_iter_create(struct ctf_trace_class *tc, |
255 | size_t max_request_sz, struct bt_msg_iter_medium_ops medops, | |
55314f2a | 256 | void *medops_data); |
e98a2d6e PP |
257 | |
258 | /** | |
b09a5592 | 259 | * Destroys a CTF message iterator, freeing all internal resources. |
e98a2d6e PP |
260 | * |
261 | * The registered trace's reference count is decremented. | |
262 | * | |
b09a5592 | 263 | * @param msg_iter CTF message iterator |
e98a2d6e | 264 | */ |
2cf1d51e | 265 | BT_HIDDEN |
b09a5592 | 266 | void bt_msg_iter_destroy(struct bt_msg_iter *msg_iter); |
e98a2d6e | 267 | |
e98a2d6e | 268 | /** |
b09a5592 | 269 | * Returns the next message from a CTF message iterator. |
e98a2d6e | 270 | * |
b09a5592 PP |
271 | * Upon successful completion, #BT_MSG_ITER_STATUS_OK is |
272 | * returned, and the next message is written to \p msg. | |
e98a2d6e | 273 | * In this case, the caller is responsible for calling |
b09a5592 | 274 | * bt_message_put() on the returned message. |
e98a2d6e | 275 | * |
b09a5592 | 276 | * If this function returns #BT_MSG_ITER_STATUS_AGAIN, the caller |
e98a2d6e PP |
277 | * should make sure that data becomes available to its medium, and |
278 | * call this function again, until another status is returned. | |
279 | * | |
b09a5592 PP |
280 | * @param msg_iter CTF message iterator |
281 | * @param message Returned message if the function's | |
282 | * return value is #BT_MSG_ITER_STATUS_OK | |
283 | * @returns One of #bt_msg_iter_status values | |
e98a2d6e | 284 | */ |
2cf1d51e | 285 | BT_HIDDEN |
b09a5592 PP |
286 | enum bt_msg_iter_status bt_msg_iter_get_next_message( |
287 | struct bt_msg_iter *notit, | |
288 | bt_self_message_iterator *msg_iter, | |
289 | bt_message **message); | |
e98a2d6e | 290 | |
b09a5592 | 291 | struct bt_msg_iter_packet_properties { |
bbbd35bf PP |
292 | int64_t exp_packet_total_size; |
293 | int64_t exp_packet_content_size; | |
7b33a0e0 PP |
294 | uint64_t stream_class_id; |
295 | int64_t data_stream_id; | |
296 | ||
297 | struct { | |
298 | uint64_t discarded_events; | |
299 | uint64_t packets; | |
300 | uint64_t beginning_clock; | |
301 | uint64_t end_clock; | |
302 | } snapshots; | |
303 | }; | |
304 | ||
305 | BT_HIDDEN | |
b09a5592 PP |
306 | enum bt_msg_iter_status bt_msg_iter_get_packet_properties( |
307 | struct bt_msg_iter *notit, | |
308 | struct bt_msg_iter_packet_properties *props); | |
7b33a0e0 | 309 | |
55fa6491 | 310 | BT_HIDDEN |
b09a5592 | 311 | void bt_msg_iter_set_medops_data(struct bt_msg_iter *notit, |
55fa6491 PP |
312 | void *medops_data); |
313 | ||
bb230c9b | 314 | BT_HIDDEN |
b09a5592 PP |
315 | enum bt_msg_iter_status bt_msg_iter_seek( |
316 | struct bt_msg_iter *notit, off_t offset); | |
bb230c9b | 317 | |
6ff151ad PP |
318 | /* |
319 | * Resets the iterator so that the next requested medium bytes are | |
bbbd35bf PP |
320 | * assumed to be the first bytes of a new stream. Depending on |
321 | * bt_msg_iter_set_emit_stream_beginning_message(), the first message | |
322 | * which this iterator emits after calling bt_msg_iter_reset() is of | |
323 | * type `BT_MESSAGE_TYPE_STREAM_BEGINNING`. | |
6ff151ad PP |
324 | */ |
325 | BT_HIDDEN | |
b09a5592 | 326 | void bt_msg_iter_reset(struct bt_msg_iter *notit); |
6ff151ad | 327 | |
989925d1 PP |
328 | /* |
329 | * Like bt_msg_iter_reset(), but preserves stream-dependent state. | |
330 | */ | |
331 | BT_HIDDEN | |
332 | void bt_msg_iter_reset_for_next_stream_file(struct bt_msg_iter *notit); | |
333 | ||
7b33a0e0 | 334 | BT_HIDDEN |
bbbd35bf PP |
335 | void bt_msg_iter_set_emit_stream_beginning_message(struct bt_msg_iter *notit, |
336 | bool val); | |
337 | ||
338 | BT_HIDDEN | |
339 | void bt_msg_iter_set_emit_stream_end_message(struct bt_msg_iter *notit, | |
340 | bool val); | |
7b33a0e0 | 341 | |
fdf0e7a0 | 342 | static inline |
b09a5592 PP |
343 | const char *bt_msg_iter_medium_status_string( |
344 | enum bt_msg_iter_medium_status status) | |
fdf0e7a0 PP |
345 | { |
346 | switch (status) { | |
b09a5592 PP |
347 | case BT_MSG_ITER_MEDIUM_STATUS_EOF: |
348 | return "BT_MSG_ITER_MEDIUM_STATUS_EOF"; | |
349 | case BT_MSG_ITER_MEDIUM_STATUS_AGAIN: | |
350 | return "BT_MSG_ITER_MEDIUM_STATUS_AGAIN"; | |
351 | case BT_MSG_ITER_MEDIUM_STATUS_INVAL: | |
352 | return "BT_MSG_ITER_MEDIUM_STATUS_INVAL"; | |
353 | case BT_MSG_ITER_MEDIUM_STATUS_ERROR: | |
354 | return "BT_MSG_ITER_MEDIUM_STATUS_ERROR"; | |
355 | case BT_MSG_ITER_MEDIUM_STATUS_OK: | |
356 | return "BT_MSG_ITER_MEDIUM_STATUS_OK"; | |
fdf0e7a0 PP |
357 | default: |
358 | return "(unknown)"; | |
359 | } | |
360 | } | |
361 | ||
362 | static inline | |
b09a5592 PP |
363 | const char *bt_msg_iter_status_string( |
364 | enum bt_msg_iter_status status) | |
fdf0e7a0 PP |
365 | { |
366 | switch (status) { | |
b09a5592 PP |
367 | case BT_MSG_ITER_STATUS_EOF: |
368 | return "BT_MSG_ITER_STATUS_EOF"; | |
369 | case BT_MSG_ITER_STATUS_AGAIN: | |
370 | return "BT_MSG_ITER_STATUS_AGAIN"; | |
371 | case BT_MSG_ITER_STATUS_INVAL: | |
372 | return "BT_MSG_ITER_STATUS_INVAL"; | |
373 | case BT_MSG_ITER_STATUS_ERROR: | |
374 | return "BT_MSG_ITER_STATUS_ERROR"; | |
375 | case BT_MSG_ITER_STATUS_OK: | |
376 | return "BT_MSG_ITER_STATUS_OK"; | |
fdf0e7a0 PP |
377 | default: |
378 | return "(unknown)"; | |
379 | } | |
380 | } | |
381 | ||
b09a5592 | 382 | #endif /* CTF_MSG_ITER_H */ |