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