| 1 | #ifndef _BABELTRACE_CTF_TYPES_H |
| 2 | #define _BABELTRACE_CTF_TYPES_H |
| 3 | |
| 4 | /* |
| 5 | * Common Trace Format |
| 6 | * |
| 7 | * Type header |
| 8 | * |
| 9 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | */ |
| 21 | |
| 22 | #include <babeltrace/types.h> |
| 23 | #include <stdint.h> |
| 24 | #include <unistd.h> |
| 25 | #include <glib.h> |
| 26 | |
| 27 | struct packet_index { |
| 28 | off_t offset; /* offset of the packet in the file, in bytes */ |
| 29 | size_t packet_size; /* packet size, in bits */ |
| 30 | size_t content_size; /* content size, in bits */ |
| 31 | }; |
| 32 | |
| 33 | /* |
| 34 | * Always update stream_pos with move_pos and init_pos. |
| 35 | */ |
| 36 | struct stream_pos { |
| 37 | int fd; /* backing file fd. -1 if unset. */ |
| 38 | GArray *packet_index; /* contains struct packet_index */ |
| 39 | |
| 40 | /* Current position */ |
| 41 | off_t mmap_offset; /* mmap offset in the file, in bytes */ |
| 42 | size_t packet_size; /* current packet size, in bits */ |
| 43 | size_t content_size; /* current content size, in bits */ |
| 44 | char *base; /* mmap base address */ |
| 45 | size_t offset; /* offset from base, in bits */ |
| 46 | |
| 47 | int dummy; /* dummy position, for length calculation */ |
| 48 | }; |
| 49 | |
| 50 | /* |
| 51 | * IMPORTANT: All lengths (len) and offsets (start, end) are expressed in bits, |
| 52 | * *not* in bytes. |
| 53 | * |
| 54 | * All write primitives, as well as read for dynamically sized entities, can |
| 55 | * receive a NULL ptr/dest parameter. In this case, no write is performed, but |
| 56 | * the size is returned. |
| 57 | */ |
| 58 | |
| 59 | uint64_t ctf_uint_read(struct stream_pos *pos, |
| 60 | const struct declaration_integer *integer_declaration); |
| 61 | int64_t ctf_int_read(struct stream_pos *pos, |
| 62 | const struct declaration_integer *integer_declaration); |
| 63 | void ctf_uint_write(struct stream_pos *pos, |
| 64 | const struct declaration_integer *integer_declaration, |
| 65 | uint64_t v); |
| 66 | void ctf_int_write(struct stream_pos *pos, |
| 67 | const struct declaration_integer *integer_declaration, |
| 68 | int64_t v); |
| 69 | |
| 70 | double ctf_double_read(struct stream_pos *pos, |
| 71 | const struct declaration_float *src); |
| 72 | void ctf_double_write(struct stream_pos *pos, |
| 73 | const struct declaration_float *dest, |
| 74 | double v); |
| 75 | long double ctf_ldouble_read(struct stream_pos *pos, |
| 76 | const struct declaration_float *src); |
| 77 | void ctf_ldouble_write(struct stream_pos *pos, |
| 78 | const struct declaration_float *dest, |
| 79 | long double v); |
| 80 | void ctf_float_copy(struct stream_pos *destp, |
| 81 | struct stream_pos *srcp, |
| 82 | const struct declaration_float *float_declaration); |
| 83 | |
| 84 | void ctf_string_copy(struct stream_pos *dest, struct stream_pos *src, |
| 85 | const struct declaration_string *string_declaration); |
| 86 | void ctf_string_read(char **dest, struct stream_pos *src, |
| 87 | const struct declaration_string *string_declaration); |
| 88 | void ctf_string_write(struct stream_pos *dest, const char *src, |
| 89 | const struct declaration_string *string_declaration); |
| 90 | void ctf_string_free_temp(char *string); |
| 91 | |
| 92 | GArray *ctf_enum_read(struct stream_pos *pos, |
| 93 | const struct declaration_enum *src); |
| 94 | void ctf_enum_write(struct stream_pos *pos, |
| 95 | const struct declaration_enum *dest, |
| 96 | GQuark q); |
| 97 | void ctf_struct_begin(struct stream_pos *pos, |
| 98 | const struct declaration_struct *struct_declaration); |
| 99 | void ctf_struct_end(struct stream_pos *pos, |
| 100 | const struct declaration_struct *struct_declaration); |
| 101 | void ctf_variant_begin(struct stream_pos *pos, |
| 102 | const struct declaration_variant *variant_declaration); |
| 103 | void ctf_variant_end(struct stream_pos *pos, |
| 104 | const struct declaration_variant *variant_declaration); |
| 105 | void ctf_array_begin(struct stream_pos *pos, |
| 106 | const struct declaration_array *array_declaration); |
| 107 | void ctf_array_end(struct stream_pos *pos, |
| 108 | const struct declaration_array *array_declaration); |
| 109 | void ctf_sequence_begin(struct stream_pos *pos, |
| 110 | const struct declaration_sequence *sequence_declaration); |
| 111 | void ctf_sequence_end(struct stream_pos *pos, |
| 112 | const struct declaration_sequence *sequence_declaration); |
| 113 | |
| 114 | void move_pos_slow(struct stream_pos *pos, size_t offset); |
| 115 | |
| 116 | static inline |
| 117 | void init_pos(struct stream_pos *pos, int fd) |
| 118 | { |
| 119 | pos->fd = fd; |
| 120 | pos->mmap_offset = 0; |
| 121 | pos->packet_size = 0; |
| 122 | pos->content_size = 0; |
| 123 | pos->base = NULL; |
| 124 | pos->offset = 0; |
| 125 | pos->dummy = false; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * move_pos - move position of a relative bit offset |
| 130 | * |
| 131 | * TODO: allow larger files by updating base too. |
| 132 | */ |
| 133 | static inline |
| 134 | void move_pos(struct stream_pos *pos, size_t offset) |
| 135 | { |
| 136 | if (pos->fd >= 0 && pos->offset + offset >= pos->content_size) |
| 137 | move_pos_slow(pos, offset); |
| 138 | else |
| 139 | pos->offset += offset; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * align_pos - align position on a bit offset (> 0) |
| 144 | * |
| 145 | * TODO: allow larger files by updating base too. |
| 146 | */ |
| 147 | static inline |
| 148 | void align_pos(struct stream_pos *pos, size_t offset) |
| 149 | { |
| 150 | pos->offset += offset_align(pos->offset, offset); |
| 151 | } |
| 152 | |
| 153 | static inline |
| 154 | void copy_pos(struct stream_pos *dest, struct stream_pos *src) |
| 155 | { |
| 156 | memcpy(dest, src, sizeof(struct stream_pos)); |
| 157 | } |
| 158 | |
| 159 | static inline |
| 160 | char *get_pos_addr(struct stream_pos *pos) |
| 161 | { |
| 162 | /* Only makes sense to get the address after aligning on CHAR_BIT */ |
| 163 | assert(!(pos->offset % CHAR_BIT)); |
| 164 | return pos->base + (pos->offset / CHAR_BIT); |
| 165 | } |
| 166 | |
| 167 | #endif /* _BABELTRACE_CTF_TYPES_H */ |