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