ctf: make ctf_msg_iter_seek assert that the seek callback is not NULL
[babeltrace.git] / src / plugins / ctf / common / msg-iter / msg-iter.h
1 #ifndef CTF_MSG_ITER_H
2 #define CTF_MSG_ITER_H
3
4 /*
5 * Babeltrace - CTF message iterator
6 *
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
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stddef.h>
33 #include <babeltrace2/babeltrace.h>
34 #include "common/macros.h"
35
36 #include "../metadata/ctf-meta.h"
37
38 /**
39 * @file ctf-msg-iter.h
40 *
41 * CTF message iterator
42 *
43 * This is a common internal API used by CTF source plugins. It allows
44 * one to get messages from a user-provided medium.
45 */
46
47 /**
48 * Medium operations status codes.
49 */
50 enum ctf_msg_iter_medium_status {
51 /**
52 * End of file.
53 *
54 * The medium function called by the message iterator
55 * function reached the end of the file.
56 */
57 CTF_MSG_ITER_MEDIUM_STATUS_EOF = 1,
58
59 /**
60 * There is no data available right now, try again later.
61 */
62 CTF_MSG_ITER_MEDIUM_STATUS_AGAIN = 11,
63
64 /** General error. */
65 CTF_MSG_ITER_MEDIUM_STATUS_ERROR = -1,
66
67 /** Everything okay. */
68 CTF_MSG_ITER_MEDIUM_STATUS_OK = 0,
69 };
70
71 /**
72 * CTF message iterator API status code.
73 */
74 enum ctf_msg_iter_status {
75 /**
76 * End of file.
77 *
78 * The medium function called by the message iterator
79 * function reached the end of the file.
80 */
81 CTF_MSG_ITER_STATUS_EOF = CTF_MSG_ITER_MEDIUM_STATUS_EOF,
82
83 /**
84 * There is no data available right now, try again later.
85 *
86 * Some condition resulted in the
87 * ctf_msg_iter_medium_ops::request_bytes() user function not
88 * having access to any data now. You should retry calling the
89 * last called message iterator function once the situation
90 * is resolved.
91 */
92 CTF_MSG_ITER_STATUS_AGAIN = CTF_MSG_ITER_MEDIUM_STATUS_AGAIN,
93
94 /** General error. */
95 CTF_MSG_ITER_STATUS_ERROR = CTF_MSG_ITER_MEDIUM_STATUS_ERROR,
96
97 /** Everything okay. */
98 CTF_MSG_ITER_STATUS_OK = CTF_MSG_ITER_MEDIUM_STATUS_OK,
99 };
100
101 /**
102 * Medium operations.
103 *
104 * Those user functions are called by the message iterator
105 * functions to request medium actions.
106 */
107 struct ctf_msg_iter_medium_ops {
108 /**
109 * Returns the next byte buffer to be used by the binary file
110 * reader to deserialize binary data.
111 *
112 * This function \em must be defined.
113 *
114 * The purpose of this function is to return a buffer of bytes
115 * to the message iterator, of a maximum of \p request_sz
116 * bytes. If this function cannot return a buffer of at least
117 * \p request_sz bytes, it may return a smaller buffer. In
118 * either cases, \p buffer_sz must be set to the returned buffer
119 * size (in bytes).
120 *
121 * The returned buffer's ownership remains the medium, in that
122 * it won't be freed by the message iterator functions. The
123 * returned buffer won't be modified by the message
124 * iterator functions either.
125 *
126 * When this function is called for the first time for a given
127 * file, the offset within the file is considered to be 0. The
128 * next times this function is called, the returned buffer's
129 * byte offset within the complete file must be the previous
130 * offset plus the last returned value of \p buffer_sz by this
131 * medium.
132 *
133 * This function must return one of the following statuses:
134 *
135 * - <b>#CTF_MSG_ITER_MEDIUM_STATUS_OK</b>: Everything
136 * is okay, i.e. \p buffer_sz is set to a positive value
137 * reflecting the number of available bytes in the buffer
138 * starting at the address written in \p buffer_addr.
139 * - <b>#CTF_MSG_ITER_MEDIUM_STATUS_AGAIN</b>: No data is
140 * available right now. In this case, the message
141 * iterator function called by the user returns
142 * #CTF_MSG_ITER_STATUS_AGAIN, and it is the user's
143 * responsibility to make sure enough data becomes available
144 * before calling the \em same message iterator
145 * function again to continue the decoding process.
146 * - <b>#CTF_MSG_ITER_MEDIUM_STATUS_EOF</b>: The end of
147 * the file was reached, and no more data will ever be
148 * available for this file. In this case, the message
149 * iterator function called by the user returns
150 * #CTF_MSG_ITER_STATUS_EOF. This must \em not be
151 * returned when returning at least one byte of data to the
152 * caller, i.e. this must be returned when there's
153 * absolutely nothing left; should the request size be
154 * larger than what's left in the file, this function must
155 * return what's left, setting \p buffer_sz to the number of
156 * remaining bytes, and return
157 * #CTF_MSG_ITER_MEDIUM_STATUS_EOF on the \em following
158 * call.
159 * - <b>#CTF_MSG_ITER_MEDIUM_STATUS_ERROR</b>: A fatal
160 * error occured during this operation. In this case, the
161 * message iterator function called by the user returns
162 * #CTF_MSG_ITER_STATUS_ERROR.
163 *
164 * If #CTF_MSG_ITER_MEDIUM_STATUS_OK is not returned, the
165 * values of \p buffer_sz and \p buffer_addr are \em ignored by
166 * the caller.
167 *
168 * @param request_sz Requested buffer size (bytes)
169 * @param buffer_addr Returned buffer address
170 * @param buffer_sz Returned buffer's size (bytes)
171 * @param data User data
172 * @returns Status code (see description above)
173 */
174 enum ctf_msg_iter_medium_status (* request_bytes)(size_t request_sz,
175 uint8_t **buffer_addr, size_t *buffer_sz, void *data);
176
177 /**
178 * Repositions the underlying stream's position.
179 *
180 * This *optional* method repositions the underlying stream
181 * to a given absolute position in the medium.
182 *
183 * @param offset Offset to use for the given directive
184 * @param data User data
185 * @returns One of #ctf_msg_iter_medium_status values
186 */
187 enum ctf_msg_iter_medium_status (* seek)(off_t offset, void *data);
188
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
194 * corresponding stream class is found by the message
195 * iterator.
196 *
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)
200 * @param data User data
201 * @returns Stream instance (weak reference) or
202 * \c NULL on error
203 */
204 bt_stream * (* borrow_stream)(bt_stream_class *stream_class,
205 int64_t stream_id, void *data);
206 };
207
208 /** CTF message iterator. */
209 struct ctf_msg_iter;
210
211 /**
212 * Creates a CTF message iterator.
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
220 * ctf_msg_iter_medium_ops::request_bytes()
221 * at a time
222 * @param medops Medium operations
223 * @param medops_data User data (passed to medium operations)
224 * @returns New CTF message iterator on
225 * success, or \c NULL on error
226 */
227 BT_HIDDEN
228 struct 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);
236
237 /**
238 * Destroys a CTF message iterator, freeing all internal resources.
239 *
240 * The registered trace's reference count is decremented.
241 *
242 * @param msg_iter CTF message iterator
243 */
244 BT_HIDDEN
245 void ctf_msg_iter_destroy(struct ctf_msg_iter *msg_iter);
246
247 /**
248 * Returns the next message from a CTF message iterator.
249 *
250 * Upon successful completion, #CTF_MSG_ITER_STATUS_OK is
251 * returned, and the next message is written to \p msg.
252 * In this case, the caller is responsible for calling
253 * bt_message_put() on the returned message.
254 *
255 * If this function returns #CTF_MSG_ITER_STATUS_AGAIN, the caller
256 * should make sure that data becomes available to its medium, and
257 * call this function again, until another status is returned.
258 *
259 * @param msg_iter CTF message iterator
260 * @param message Returned message if the function's
261 * return value is #CTF_MSG_ITER_STATUS_OK
262 * @returns One of #ctf_msg_iter_status values
263 */
264 BT_HIDDEN
265 enum ctf_msg_iter_status ctf_msg_iter_get_next_message(
266 struct ctf_msg_iter *msg_it,
267 const bt_message **message);
268
269 struct ctf_msg_iter_packet_properties {
270 int64_t exp_packet_total_size;
271 int64_t exp_packet_content_size;
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
283 BT_HIDDEN
284 enum 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);
287
288 BT_HIDDEN
289 enum 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);
291
292 BT_HIDDEN
293 enum 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);
295
296 BT_HIDDEN
297 void ctf_msg_iter_set_medops_data(struct ctf_msg_iter *msg_it,
298 void *medops_data);
299
300 BT_HIDDEN
301 enum ctf_msg_iter_status ctf_msg_iter_seek(
302 struct ctf_msg_iter *msg_it, off_t offset);
303
304 /*
305 * Resets the iterator so that the next requested medium bytes are
306 * assumed to be the first bytes of a new stream. Depending on
307 * ctf_msg_iter_set_emit_stream_beginning_message(), the first message
308 * which this iterator emits after calling ctf_msg_iter_reset() is of
309 * type `CTF_MESSAGE_TYPE_STREAM_BEGINNING`.
310 */
311 BT_HIDDEN
312 void ctf_msg_iter_reset(struct ctf_msg_iter *msg_it);
313
314 /*
315 * Like ctf_msg_iter_reset(), but preserves stream-dependent state.
316 */
317 BT_HIDDEN
318 void ctf_msg_iter_reset_for_next_stream_file(struct ctf_msg_iter *msg_it);
319
320 BT_HIDDEN
321 void ctf_msg_iter_set_emit_stream_beginning_message(struct ctf_msg_iter *msg_it,
322 bool val);
323
324 BT_HIDDEN
325 void ctf_msg_iter_set_emit_stream_end_message(struct ctf_msg_iter *msg_it,
326 bool val);
327
328 BT_HIDDEN
329 void ctf_msg_iter_set_dry_run(struct ctf_msg_iter *msg_it,
330 bool val);
331
332 static inline
333 const char *ctf_msg_iter_medium_status_string(
334 enum ctf_msg_iter_medium_status status)
335 {
336 switch (status) {
337 case CTF_MSG_ITER_MEDIUM_STATUS_EOF:
338 return "EOF";
339 case CTF_MSG_ITER_MEDIUM_STATUS_AGAIN:
340 return "AGAIN";
341 case CTF_MSG_ITER_MEDIUM_STATUS_ERROR:
342 return "ERROR";
343 case CTF_MSG_ITER_MEDIUM_STATUS_OK:
344 return "OK";
345 default:
346 return "(unknown)";
347 }
348 }
349
350 static inline
351 const char *ctf_msg_iter_status_string(
352 enum ctf_msg_iter_status status)
353 {
354 switch (status) {
355 case CTF_MSG_ITER_STATUS_EOF:
356 return "EOF";
357 case CTF_MSG_ITER_STATUS_AGAIN:
358 return "AGAIN";
359 case CTF_MSG_ITER_STATUS_ERROR:
360 return "ERROR";
361 case CTF_MSG_ITER_STATUS_OK:
362 return "OK";
363 default:
364 return "(unknown)";
365 }
366 }
367
368 #endif /* CTF_MSG_ITER_H */
This page took 0.036409 seconds and 4 git commands to generate.