2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com>
7 * Babeltrace - CTF message iterator
10 #ifndef CTF_MSG_ITER_H
11 #define CTF_MSG_ITER_H
17 #include <babeltrace2/babeltrace.h>
19 #include "../metadata/tsdl/ctf-meta.hpp"
25 } /* namespace bt2c */
28 * @file ctf-msg-iter.h
30 * CTF message iterator
32 * This is a common internal API used by CTF source plugins. It allows
33 * one to get messages from a user-provided medium.
37 * Medium operations status codes. These use the same values as
40 enum ctf_msg_iter_medium_status
45 * The medium function called by the message iterator
46 * function reached the end of the file.
48 CTF_MSG_ITER_MEDIUM_STATUS_EOF = 1,
51 * There is no data available right now, try again later.
53 CTF_MSG_ITER_MEDIUM_STATUS_AGAIN = 11,
56 CTF_MSG_ITER_MEDIUM_STATUS_ERROR = -1,
59 CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR = -12,
61 /** Everything okay. */
62 CTF_MSG_ITER_MEDIUM_STATUS_OK = 0,
65 inline const char *format_as(const ctf_msg_iter_medium_status status) noexcept
68 case CTF_MSG_ITER_MEDIUM_STATUS_EOF:
71 case CTF_MSG_ITER_MEDIUM_STATUS_AGAIN:
74 case CTF_MSG_ITER_MEDIUM_STATUS_ERROR:
77 case CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR:
78 return "MEMORY_ERROR";
80 case CTF_MSG_ITER_MEDIUM_STATUS_OK:
88 * CTF message iterator API status code.
90 enum ctf_msg_iter_status
95 * The medium function called by the message iterator
96 * function reached the end of the file.
98 CTF_MSG_ITER_STATUS_EOF = CTF_MSG_ITER_MEDIUM_STATUS_EOF,
101 * There is no data available right now, try again later.
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
109 CTF_MSG_ITER_STATUS_AGAIN = CTF_MSG_ITER_MEDIUM_STATUS_AGAIN,
111 /** General error. */
112 CTF_MSG_ITER_STATUS_ERROR = CTF_MSG_ITER_MEDIUM_STATUS_ERROR,
115 CTF_MSG_ITER_STATUS_MEMORY_ERROR = CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR,
117 /** Everything okay. */
118 CTF_MSG_ITER_STATUS_OK = CTF_MSG_ITER_MEDIUM_STATUS_OK,
121 inline const char *format_as(ctf_msg_iter_status status) noexcept
124 case CTF_MSG_ITER_STATUS_EOF:
127 case CTF_MSG_ITER_STATUS_AGAIN:
130 case CTF_MSG_ITER_STATUS_ERROR:
133 case CTF_MSG_ITER_STATUS_MEMORY_ERROR:
134 return "MEMORY_ERROR";
136 case CTF_MSG_ITER_STATUS_OK:
146 * Those user functions are called by the message iterator
147 * functions to request medium actions.
149 struct ctf_msg_iter_medium_ops
152 * Returns the next byte buffer to be used by the binary file
153 * reader to deserialize binary data.
155 * This function \em must be defined.
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
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.
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
176 * This function must return one of the following statuses:
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
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.
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
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)
217 enum ctf_msg_iter_medium_status (*request_bytes)(size_t request_sz, uint8_t **buffer_addr,
218 size_t *buffer_sz, void *data);
221 * Repositions the underlying stream's position.
223 * This *optional* method repositions the underlying stream
224 * to a given absolute position in the medium.
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
230 enum ctf_msg_iter_medium_status (*seek)(off_t offset, void *data);
233 * Called when the message iterator wishes to inform the medium that it
234 * is about to start a new packet.
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
239 enum ctf_msg_iter_medium_status (*switch_packet)(void *data);
242 * Returns a stream instance (weak reference) for the given
245 * This is called after a packet header is read, and the
246 * corresponding stream class is found by the message
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
256 bt_stream *(*borrow_stream)(bt_stream_class *stream_class, int64_t stream_id, void *data);
260 * Creates a CTF message iterator.
262 * Upon successful completion, the reference count of \p trace is
265 * @param trace Trace to read
266 * @param max_request_sz Maximum buffer size, in bytes, to
268 * ctf_msg_iter_medium_ops::request_bytes()
270 * @param medops Medium operations
271 * @param medops_data User data (passed to medium operations)
272 * @returns New CTF message iterator on
273 * success, or \c NULL on error
275 struct ctf_msg_iter *ctf_msg_iter_create(struct ctf_trace_class *tc, size_t max_request_sz,
276 struct ctf_msg_iter_medium_ops medops, void *medops_data,
277 bt_self_message_iterator *self_msg_iter,
278 const bt2c::Logger& logger);
281 * Destroys a CTF message iterator, freeing all internal resources.
283 * The registered trace's reference count is decremented.
285 * @param msg_iter CTF message iterator
287 void ctf_msg_iter_destroy(struct ctf_msg_iter *msg_iter);
290 * Returns the next message from a CTF message iterator.
292 * Upon successful completion, #CTF_MSG_ITER_STATUS_OK is
293 * returned, and the next message is written to \p msg.
294 * In this case, the caller is responsible for calling
295 * bt_message_put() on the returned message.
297 * If this function returns #CTF_MSG_ITER_STATUS_AGAIN, the caller
298 * should make sure that data becomes available to its medium, and
299 * call this function again, until another status is returned.
301 * @param msg_iter CTF message iterator
302 * @param message Returned message if the function's
303 * return value is #CTF_MSG_ITER_STATUS_OK
304 * @returns One of #ctf_msg_iter_status values
306 enum ctf_msg_iter_status ctf_msg_iter_get_next_message(struct ctf_msg_iter *msg_it,
307 const bt_message **message);
309 struct ctf_msg_iter_packet_properties
311 int64_t exp_packet_total_size;
312 int64_t exp_packet_content_size;
313 uint64_t stream_class_id;
314 int64_t data_stream_id;
318 uint64_t discarded_events;
320 uint64_t beginning_clock;
325 enum ctf_msg_iter_status
326 ctf_msg_iter_get_packet_properties(struct ctf_msg_iter *msg_it,
327 struct ctf_msg_iter_packet_properties *props);
329 enum ctf_msg_iter_status
330 ctf_msg_iter_curr_packet_first_event_clock_snapshot(struct ctf_msg_iter *msg_it,
331 uint64_t *first_event_cs);
333 enum ctf_msg_iter_status
334 ctf_msg_iter_curr_packet_last_event_clock_snapshot(struct ctf_msg_iter *msg_it,
335 uint64_t *last_event_cs);
337 enum ctf_msg_iter_status ctf_msg_iter_seek(struct ctf_msg_iter *msg_it, off_t offset);
340 * Resets the iterator so that the next requested medium bytes are
341 * assumed to be the first bytes of a new stream. Depending on
342 * ctf_msg_iter_set_emit_stream_beginning_message(), the first message
343 * which this iterator emits after calling ctf_msg_iter_reset() is of
344 * type `CTF_MESSAGE_TYPE_STREAM_BEGINNING`.
346 void ctf_msg_iter_reset(struct ctf_msg_iter *msg_it);
349 * Like ctf_msg_iter_reset(), but preserves stream-dependent state.
351 void ctf_msg_iter_reset_for_next_stream_file(struct ctf_msg_iter *msg_it);
353 void ctf_msg_iter_set_dry_run(struct ctf_msg_iter *msg_it, bool val);
355 #endif /* CTF_MSG_ITER_H */