5 * Babeltrace - CTF message iterator
7 * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation
8 * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
33 #include <babeltrace2/babeltrace.h>
34 #include "common/macros.h"
36 #include "../metadata/ctf-meta.h"
39 * @file ctf-msg-iter.h
41 * CTF message iterator
43 * This is a common internal API used by CTF source plugins. It allows
44 * one to get messages from a user-provided medium.
48 * Medium operations status codes.
50 enum ctf_msg_iter_medium_status
{
54 * The medium function called by the message iterator
55 * function reached the end of the file.
57 CTF_MSG_ITER_MEDIUM_STATUS_EOF
= 1,
60 * There is no data available right now, try again later.
62 CTF_MSG_ITER_MEDIUM_STATUS_AGAIN
= 11,
64 /** Unsupported operation. */
65 CTF_MSG_ITER_MEDIUM_STATUS_UNSUPPORTED
= -3,
68 CTF_MSG_ITER_MEDIUM_STATUS_ERROR
= -1,
70 /** Everything okay. */
71 CTF_MSG_ITER_MEDIUM_STATUS_OK
= 0,
75 * CTF message iterator API status code.
77 enum ctf_msg_iter_status
{
81 * The medium function called by the message iterator
82 * function reached the end of the file.
84 CTF_MSG_ITER_STATUS_EOF
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
,
87 * There is no data available right now, try again later.
89 * Some condition resulted in the
90 * ctf_msg_iter_medium_ops::request_bytes() user function not
91 * having access to any data now. You should retry calling the
92 * last called message iterator function once the situation
95 CTF_MSG_ITER_STATUS_AGAIN
= CTF_MSG_ITER_MEDIUM_STATUS_AGAIN
,
97 /** Unsupported operation. */
98 CTF_MSG_ITER_STATUS_UNSUPPORTED
= CTF_MSG_ITER_MEDIUM_STATUS_UNSUPPORTED
,
100 /** General error. */
101 CTF_MSG_ITER_STATUS_ERROR
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
,
103 /** Everything okay. */
104 CTF_MSG_ITER_STATUS_OK
= CTF_MSG_ITER_MEDIUM_STATUS_OK
,
110 * Those user functions are called by the message iterator
111 * functions to request medium actions.
113 struct ctf_msg_iter_medium_ops
{
115 * Returns the next byte buffer to be used by the binary file
116 * reader to deserialize binary data.
118 * This function \em must be defined.
120 * The purpose of this function is to return a buffer of bytes
121 * to the message iterator, of a maximum of \p request_sz
122 * bytes. If this function cannot return a buffer of at least
123 * \p request_sz bytes, it may return a smaller buffer. In
124 * either cases, \p buffer_sz must be set to the returned buffer
127 * The returned buffer's ownership remains the medium, in that
128 * it won't be freed by the message iterator functions. The
129 * returned buffer won't be modified by the message
130 * iterator functions either.
132 * When this function is called for the first time for a given
133 * file, the offset within the file is considered to be 0. The
134 * next times this function is called, the returned buffer's
135 * byte offset within the complete file must be the previous
136 * offset plus the last returned value of \p buffer_sz by this
139 * This function must return one of the following statuses:
141 * - <b>#CTF_MSG_ITER_MEDIUM_STATUS_OK</b>: Everything
142 * is okay, i.e. \p buffer_sz is set to a positive value
143 * reflecting the number of available bytes in the buffer
144 * starting at the address written in \p buffer_addr.
145 * - <b>#CTF_MSG_ITER_MEDIUM_STATUS_AGAIN</b>: No data is
146 * available right now. In this case, the message
147 * iterator function called by the user returns
148 * #CTF_MSG_ITER_STATUS_AGAIN, and it is the user's
149 * responsibility to make sure enough data becomes available
150 * before calling the \em same message iterator
151 * function again to continue the decoding process.
152 * - <b>#CTF_MSG_ITER_MEDIUM_STATUS_EOF</b>: The end of
153 * the file was reached, and no more data will ever be
154 * available for this file. In this case, the message
155 * iterator function called by the user returns
156 * #CTF_MSG_ITER_STATUS_EOF. This must \em not be
157 * returned when returning at least one byte of data to the
158 * caller, i.e. this must be returned when there's
159 * absolutely nothing left; should the request size be
160 * larger than what's left in the file, this function must
161 * return what's left, setting \p buffer_sz to the number of
162 * remaining bytes, and return
163 * #CTF_MSG_ITER_MEDIUM_STATUS_EOF on the \em following
165 * - <b>#CTF_MSG_ITER_MEDIUM_STATUS_ERROR</b>: A fatal
166 * error occured during this operation. In this case, the
167 * message iterator function called by the user returns
168 * #CTF_MSG_ITER_STATUS_ERROR.
170 * If #CTF_MSG_ITER_MEDIUM_STATUS_OK is not returned, the
171 * values of \p buffer_sz and \p buffer_addr are \em ignored by
174 * @param request_sz Requested buffer size (bytes)
175 * @param buffer_addr Returned buffer address
176 * @param buffer_sz Returned buffer's size (bytes)
177 * @param data User data
178 * @returns Status code (see description above)
180 enum ctf_msg_iter_medium_status (* request_bytes
)(size_t request_sz
,
181 uint8_t **buffer_addr
, size_t *buffer_sz
, void *data
);
184 * Repositions the underlying stream's position.
186 * This *optional* method repositions the underlying stream
187 * to a given absolute position in the medium.
189 * @param offset Offset to use for the given directive
190 * @param data User data
191 * @returns One of #ctf_msg_iter_medium_status values
193 enum ctf_msg_iter_medium_status (* seek
)(off_t offset
, void *data
);
196 * Returns a stream instance (weak reference) for the given
199 * This is called after a packet header is read, and the
200 * corresponding stream class is found by the message
203 * @param stream_class Stream class of the stream to get
204 * @param stream_id Stream (instance) ID of the stream
205 * to get (-1ULL if not available)
206 * @param data User data
207 * @returns Stream instance (weak reference) or
210 bt_stream
* (* borrow_stream
)(bt_stream_class
*stream_class
,
211 int64_t stream_id
, void *data
);
214 /** CTF message iterator. */
218 * Creates a CTF message iterator.
220 * Upon successful completion, the reference count of \p trace is
223 * @param trace Trace to read
224 * @param max_request_sz Maximum buffer size, in bytes, to
226 * ctf_msg_iter_medium_ops::request_bytes()
228 * @param medops Medium operations
229 * @param medops_data User data (passed to medium operations)
230 * @returns New CTF message iterator on
231 * success, or \c NULL on error
234 struct ctf_msg_iter
*ctf_msg_iter_create(
235 struct ctf_trace_class
*tc
,
236 size_t max_request_sz
,
237 struct ctf_msg_iter_medium_ops medops
,
239 bt_logging_level log_level
,
240 bt_self_component
*self_comp
,
241 bt_self_message_iterator
*self_msg_iter
);
244 * Destroys a CTF message iterator, freeing all internal resources.
246 * The registered trace's reference count is decremented.
248 * @param msg_iter CTF message iterator
251 void ctf_msg_iter_destroy(struct ctf_msg_iter
*msg_iter
);
254 * Returns the next message from a CTF message iterator.
256 * Upon successful completion, #CTF_MSG_ITER_STATUS_OK is
257 * returned, and the next message is written to \p msg.
258 * In this case, the caller is responsible for calling
259 * bt_message_put() on the returned message.
261 * If this function returns #CTF_MSG_ITER_STATUS_AGAIN, the caller
262 * should make sure that data becomes available to its medium, and
263 * call this function again, until another status is returned.
265 * @param msg_iter CTF message iterator
266 * @param message Returned message if the function's
267 * return value is #CTF_MSG_ITER_STATUS_OK
268 * @returns One of #ctf_msg_iter_status values
271 enum ctf_msg_iter_status
ctf_msg_iter_get_next_message(
272 struct ctf_msg_iter
*msg_it
,
273 const bt_message
**message
);
275 struct ctf_msg_iter_packet_properties
{
276 int64_t exp_packet_total_size
;
277 int64_t exp_packet_content_size
;
278 uint64_t stream_class_id
;
279 int64_t data_stream_id
;
282 uint64_t discarded_events
;
284 uint64_t beginning_clock
;
290 enum ctf_msg_iter_status
ctf_msg_iter_get_packet_properties(
291 struct ctf_msg_iter
*msg_it
,
292 struct ctf_msg_iter_packet_properties
*props
);
295 enum ctf_msg_iter_status
ctf_msg_iter_curr_packet_first_event_clock_snapshot(
296 struct ctf_msg_iter
*msg_it
, uint64_t *first_event_cs
);
299 enum ctf_msg_iter_status
ctf_msg_iter_curr_packet_last_event_clock_snapshot(
300 struct ctf_msg_iter
*msg_it
, uint64_t *last_event_cs
);
303 void ctf_msg_iter_set_medops_data(struct ctf_msg_iter
*msg_it
,
307 enum ctf_msg_iter_status
ctf_msg_iter_seek(
308 struct ctf_msg_iter
*msg_it
, off_t offset
);
311 * Resets the iterator so that the next requested medium bytes are
312 * assumed to be the first bytes of a new stream. Depending on
313 * ctf_msg_iter_set_emit_stream_beginning_message(), the first message
314 * which this iterator emits after calling ctf_msg_iter_reset() is of
315 * type `CTF_MESSAGE_TYPE_STREAM_BEGINNING`.
318 void ctf_msg_iter_reset(struct ctf_msg_iter
*msg_it
);
321 * Like ctf_msg_iter_reset(), but preserves stream-dependent state.
324 void ctf_msg_iter_reset_for_next_stream_file(struct ctf_msg_iter
*msg_it
);
327 void ctf_msg_iter_set_emit_stream_beginning_message(struct ctf_msg_iter
*msg_it
,
331 void ctf_msg_iter_set_emit_stream_end_message(struct ctf_msg_iter
*msg_it
,
335 void ctf_msg_iter_set_dry_run(struct ctf_msg_iter
*msg_it
,
339 const char *ctf_msg_iter_medium_status_string(
340 enum ctf_msg_iter_medium_status status
)
343 case CTF_MSG_ITER_MEDIUM_STATUS_EOF
:
345 case CTF_MSG_ITER_MEDIUM_STATUS_AGAIN
:
347 case CTF_MSG_ITER_MEDIUM_STATUS_ERROR
:
349 case CTF_MSG_ITER_MEDIUM_STATUS_OK
:
357 const char *ctf_msg_iter_status_string(
358 enum ctf_msg_iter_status status
)
361 case CTF_MSG_ITER_STATUS_EOF
:
363 case CTF_MSG_ITER_STATUS_AGAIN
:
365 case CTF_MSG_ITER_STATUS_ERROR
:
367 case CTF_MSG_ITER_STATUS_OK
:
374 #endif /* CTF_MSG_ITER_H */