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