| 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 <sys/mman.h> |
| 24 | #include <errno.h> |
| 25 | #include <stdint.h> |
| 26 | #include <unistd.h> |
| 27 | #include <glib.h> |
| 28 | |
| 29 | struct packet_index { |
| 30 | off_t offset; /* offset of the packet in the file, in bytes */ |
| 31 | size_t packet_size; /* packet size, in bits */ |
| 32 | size_t content_size; /* content size, in bits */ |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * Always update stream_pos with move_pos and init_pos. |
| 37 | */ |
| 38 | struct stream_pos { |
| 39 | int fd; /* backing file fd. -1 if unset. */ |
| 40 | GArray *packet_index; /* contains struct packet_index */ |
| 41 | int prot; /* mmap protection */ |
| 42 | int flags; /* mmap flags */ |
| 43 | |
| 44 | /* Current position */ |
| 45 | off_t mmap_offset; /* mmap offset in the file, in bytes */ |
| 46 | size_t packet_size; /* current packet size, in bits */ |
| 47 | size_t content_size; /* current content size, in bits */ |
| 48 | uint32_t *content_size_loc; /* pointer to current content size */ |
| 49 | char *base; /* mmap base address */ |
| 50 | size_t offset; /* offset from base, in bits */ |
| 51 | size_t cur_index; /* current index in packet index */ |
| 52 | |
| 53 | int dummy; /* dummy position, for length calculation */ |
| 54 | }; |
| 55 | |
| 56 | /* |
| 57 | * IMPORTANT: All lengths (len) and offsets (start, end) are expressed in bits, |
| 58 | * *not* in bytes. |
| 59 | * |
| 60 | * All write primitives, as well as read for dynamically sized entities, can |
| 61 | * receive a NULL ptr/dest parameter. In this case, no write is performed, but |
| 62 | * the size is returned. |
| 63 | */ |
| 64 | |
| 65 | uint64_t ctf_uint_read(struct stream_pos *pos, |
| 66 | const struct declaration_integer *integer_declaration); |
| 67 | int64_t ctf_int_read(struct stream_pos *pos, |
| 68 | const struct declaration_integer *integer_declaration); |
| 69 | void ctf_uint_write(struct stream_pos *pos, |
| 70 | const struct declaration_integer *integer_declaration, |
| 71 | uint64_t v); |
| 72 | void ctf_int_write(struct stream_pos *pos, |
| 73 | const struct declaration_integer *integer_declaration, |
| 74 | int64_t v); |
| 75 | |
| 76 | double ctf_double_read(struct stream_pos *pos, |
| 77 | const struct declaration_float *src); |
| 78 | void ctf_double_write(struct stream_pos *pos, |
| 79 | const struct declaration_float *dest, |
| 80 | double v); |
| 81 | long double ctf_ldouble_read(struct stream_pos *pos, |
| 82 | const struct declaration_float *src); |
| 83 | void ctf_ldouble_write(struct stream_pos *pos, |
| 84 | const struct declaration_float *dest, |
| 85 | long double v); |
| 86 | void ctf_float_copy(struct stream_pos *destp, |
| 87 | struct stream_pos *srcp, |
| 88 | const struct declaration_float *float_declaration); |
| 89 | |
| 90 | void ctf_string_copy(struct stream_pos *dest, struct stream_pos *src, |
| 91 | const struct declaration_string *string_declaration); |
| 92 | void ctf_string_read(char **dest, struct stream_pos *src, |
| 93 | const struct declaration_string *string_declaration); |
| 94 | void ctf_string_write(struct stream_pos *dest, const char *src, |
| 95 | const struct declaration_string *string_declaration); |
| 96 | void ctf_string_free_temp(char *string); |
| 97 | |
| 98 | GArray *ctf_enum_read(struct stream_pos *pos, |
| 99 | const struct declaration_enum *src); |
| 100 | void ctf_enum_write(struct stream_pos *pos, |
| 101 | const struct declaration_enum *dest, |
| 102 | GQuark q); |
| 103 | void ctf_struct_begin(struct stream_pos *pos, |
| 104 | const struct declaration_struct *struct_declaration); |
| 105 | void ctf_struct_end(struct stream_pos *pos, |
| 106 | const struct declaration_struct *struct_declaration); |
| 107 | void ctf_variant_begin(struct stream_pos *pos, |
| 108 | const struct declaration_variant *variant_declaration); |
| 109 | void ctf_variant_end(struct stream_pos *pos, |
| 110 | const struct declaration_variant *variant_declaration); |
| 111 | void ctf_array_begin(struct stream_pos *pos, |
| 112 | const struct declaration_array *array_declaration); |
| 113 | void ctf_array_end(struct stream_pos *pos, |
| 114 | const struct declaration_array *array_declaration); |
| 115 | void ctf_sequence_begin(struct stream_pos *pos, |
| 116 | const struct declaration_sequence *sequence_declaration); |
| 117 | void ctf_sequence_end(struct stream_pos *pos, |
| 118 | const struct declaration_sequence *sequence_declaration); |
| 119 | |
| 120 | void move_pos_slow(struct stream_pos *pos, size_t offset); |
| 121 | |
| 122 | void init_pos(struct stream_pos *pos, int fd); |
| 123 | void fini_pos(struct stream_pos *pos); |
| 124 | |
| 125 | /* |
| 126 | * move_pos - move position of a relative bit offset |
| 127 | * |
| 128 | * TODO: allow larger files by updating base too. |
| 129 | */ |
| 130 | static inline |
| 131 | void move_pos(struct stream_pos *pos, size_t bit_offset) |
| 132 | { |
| 133 | if (pos->fd >= 0) { |
| 134 | if (((pos->prot == PROT_READ) |
| 135 | && (pos->offset + bit_offset >= pos->content_size)) |
| 136 | || ((pos->prot == PROT_WRITE) |
| 137 | && (pos->offset + bit_offset >= pos->packet_size))) { |
| 138 | move_pos_slow(pos, bit_offset); |
| 139 | return; |
| 140 | } |
| 141 | } |
| 142 | pos->offset += bit_offset; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * align_pos - align position on a bit offset (> 0) |
| 147 | * |
| 148 | * TODO: allow larger files by updating base too. |
| 149 | */ |
| 150 | static inline |
| 151 | void align_pos(struct stream_pos *pos, size_t bit_offset) |
| 152 | { |
| 153 | move_pos(pos, offset_align(pos->offset, bit_offset)); |
| 154 | } |
| 155 | |
| 156 | static inline |
| 157 | char *get_pos_addr(struct stream_pos *pos) |
| 158 | { |
| 159 | /* Only makes sense to get the address after aligning on CHAR_BIT */ |
| 160 | assert(!(pos->offset % CHAR_BIT)); |
| 161 | return pos->base + (pos->offset / CHAR_BIT); |
| 162 | } |
| 163 | |
| 164 | static inline |
| 165 | void dummy_pos(struct stream_pos *pos, struct stream_pos *dummy) |
| 166 | { |
| 167 | memcpy(dummy, pos, sizeof(struct stream_pos)); |
| 168 | dummy->dummy = 1; |
| 169 | dummy->fd = -1; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Check if current packet can hold data. |
| 174 | * Returns 0 for success, negative error otherwise. |
| 175 | */ |
| 176 | static inline |
| 177 | int pos_packet(struct stream_pos *dummy) |
| 178 | { |
| 179 | if (dummy->offset > dummy->packet_size) |
| 180 | return -ENOSPC; |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static inline |
| 185 | void pos_pad_packet(struct stream_pos *pos) |
| 186 | { |
| 187 | move_pos(pos, pos->packet_size - pos->offset); |
| 188 | } |
| 189 | |
| 190 | #endif /* _BABELTRACE_CTF_TYPES_H */ |