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