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