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