Commit | Line | Data |
---|---|---|
d79865b9 MD |
1 | #ifndef _BABELTRACE_CTF_TYPES_H |
2 | #define _BABELTRACE_CTF_TYPES_H | |
6dc2ca62 MD |
3 | |
4 | /* | |
5 | * Common Trace Format | |
6 | * | |
7 | * Type header | |
8 | * | |
ccd7e1c8 | 9 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
6dc2ca62 | 10 | * |
ccd7e1c8 MD |
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: | |
de0ba614 | 17 | * |
ccd7e1c8 MD |
18 | * The above copyright notice and this permission notice shall be included in |
19 | * all copies or substantial portions of the Software. | |
6dc2ca62 MD |
20 | */ |
21 | ||
4c8bfb7e | 22 | #include <babeltrace/types.h> |
8c572eba MD |
23 | #include <sys/mman.h> |
24 | #include <errno.h> | |
6dc2ca62 | 25 | #include <stdint.h> |
0f980a35 | 26 | #include <unistd.h> |
6dc2ca62 MD |
27 | #include <glib.h> |
28 | ||
0f980a35 MD |
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 */ | |
dd2544fd MD |
33 | }; |
34 | ||
dd2544fd | 35 | /* |
46322b33 | 36 | * Always update ctf_stream_pos with ctf_move_pos and ctf_init_pos. |
dd2544fd | 37 | */ |
46322b33 MD |
38 | struct ctf_stream_pos { |
39 | struct stream_pos parent; | |
0f980a35 MD |
40 | int fd; /* backing file fd. -1 if unset. */ |
41 | GArray *packet_index; /* contains struct packet_index */ | |
8c572eba MD |
42 | int prot; /* mmap protection */ |
43 | int flags; /* mmap flags */ | |
dd2544fd | 44 | |
0f980a35 MD |
45 | /* Current position */ |
46 | off_t mmap_offset; /* mmap offset in the file, in bytes */ | |
47 | size_t packet_size; /* current packet size, in bits */ | |
48 | size_t content_size; /* current content size, in bits */ | |
8c572eba | 49 | uint32_t *content_size_loc; /* pointer to current content size */ |
0f980a35 MD |
50 | char *base; /* mmap base address */ |
51 | size_t offset; /* offset from base, in bits */ | |
8c572eba | 52 | size_t cur_index; /* current index in packet index */ |
dd2544fd | 53 | |
0f980a35 MD |
54 | int dummy; /* dummy position, for length calculation */ |
55 | }; | |
dd2544fd | 56 | |
46322b33 MD |
57 | static inline |
58 | struct ctf_stream_pos *ctf_pos(struct stream_pos *pos) | |
59 | { | |
60 | return container_of(pos, struct ctf_stream_pos, parent); | |
61 | } | |
62 | ||
6dc2ca62 | 63 | /* |
de0ba614 MD |
64 | * IMPORTANT: All lengths (len) and offsets (start, end) are expressed in bits, |
65 | * *not* in bytes. | |
66 | * | |
67 | * All write primitives, as well as read for dynamically sized entities, can | |
6dc2ca62 MD |
68 | * receive a NULL ptr/dest parameter. In this case, no write is performed, but |
69 | * the size is returned. | |
70 | */ | |
71 | ||
bed864a7 | 72 | uint64_t ctf_uint_read(struct stream_pos *pos, |
f6625916 | 73 | const struct declaration_integer *integer_declaration); |
bed864a7 | 74 | int64_t ctf_int_read(struct stream_pos *pos, |
f6625916 | 75 | const struct declaration_integer *integer_declaration); |
bed864a7 | 76 | void ctf_uint_write(struct stream_pos *pos, |
f6625916 | 77 | const struct declaration_integer *integer_declaration, |
bed864a7 MD |
78 | uint64_t v); |
79 | void ctf_int_write(struct stream_pos *pos, | |
f6625916 | 80 | const struct declaration_integer *integer_declaration, |
bed864a7 | 81 | int64_t v); |
6dc2ca62 | 82 | |
bed864a7 | 83 | double ctf_double_read(struct stream_pos *pos, |
f6625916 | 84 | const struct declaration_float *src); |
bed864a7 | 85 | void ctf_double_write(struct stream_pos *pos, |
f6625916 | 86 | const struct declaration_float *dest, |
bed864a7 MD |
87 | double v); |
88 | long double ctf_ldouble_read(struct stream_pos *pos, | |
f6625916 | 89 | const struct declaration_float *src); |
bed864a7 | 90 | void ctf_ldouble_write(struct stream_pos *pos, |
f6625916 | 91 | const struct declaration_float *dest, |
bed864a7 | 92 | long double v); |
4c8bfb7e MD |
93 | void ctf_float_copy(struct stream_pos *destp, |
94 | struct stream_pos *srcp, | |
f6625916 | 95 | const struct declaration_float *float_declaration); |
6dc2ca62 | 96 | |
bed864a7 | 97 | void ctf_string_copy(struct stream_pos *dest, struct stream_pos *src, |
f6625916 | 98 | const struct declaration_string *string_declaration); |
47e0f2e2 | 99 | void ctf_string_read(char **dest, struct stream_pos *src, |
f6625916 | 100 | const struct declaration_string *string_declaration); |
47e0f2e2 | 101 | void ctf_string_write(struct stream_pos *dest, const char *src, |
f6625916 | 102 | const struct declaration_string *string_declaration); |
47e0f2e2 | 103 | void ctf_string_free_temp(char *string); |
6dc2ca62 | 104 | |
47e0f2e2 | 105 | GArray *ctf_enum_read(struct stream_pos *pos, |
f6625916 | 106 | const struct declaration_enum *src); |
bed864a7 | 107 | void ctf_enum_write(struct stream_pos *pos, |
f6625916 | 108 | const struct declaration_enum *dest, |
bed864a7 | 109 | GQuark q); |
11796b96 | 110 | void ctf_struct_begin(struct stream_pos *pos, |
f6625916 | 111 | const struct declaration_struct *struct_declaration); |
11796b96 | 112 | void ctf_struct_end(struct stream_pos *pos, |
f6625916 | 113 | const struct declaration_struct *struct_declaration); |
6b71274a | 114 | void ctf_variant_begin(struct stream_pos *pos, |
f6625916 | 115 | const struct declaration_variant *variant_declaration); |
6b71274a | 116 | void ctf_variant_end(struct stream_pos *pos, |
f6625916 | 117 | const struct declaration_variant *variant_declaration); |
d06d03db | 118 | void ctf_array_begin(struct stream_pos *pos, |
f6625916 | 119 | const struct declaration_array *array_declaration); |
d06d03db | 120 | void ctf_array_end(struct stream_pos *pos, |
f6625916 | 121 | const struct declaration_array *array_declaration); |
d06d03db | 122 | void ctf_sequence_begin(struct stream_pos *pos, |
f6625916 | 123 | const struct declaration_sequence *sequence_declaration); |
d06d03db | 124 | void ctf_sequence_end(struct stream_pos *pos, |
f6625916 | 125 | const struct declaration_sequence *sequence_declaration); |
6dc2ca62 | 126 | |
46322b33 | 127 | void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset); |
0f980a35 | 128 | |
46322b33 MD |
129 | void ctf_init_pos(struct ctf_stream_pos *pos, int fd); |
130 | void ctf_fini_pos(struct ctf_stream_pos *pos); | |
0f980a35 MD |
131 | |
132 | /* | |
133 | * move_pos - move position of a relative bit offset | |
134 | * | |
135 | * TODO: allow larger files by updating base too. | |
136 | */ | |
137 | static inline | |
46322b33 | 138 | void ctf_move_pos(struct ctf_stream_pos *pos, size_t bit_offset) |
0f980a35 | 139 | { |
8c572eba MD |
140 | if (pos->fd >= 0) { |
141 | if (((pos->prot == PROT_READ) | |
142 | && (pos->offset + bit_offset >= pos->content_size)) | |
143 | || ((pos->prot == PROT_WRITE) | |
144 | && (pos->offset + bit_offset >= pos->packet_size))) { | |
46322b33 | 145 | ctf_move_pos_slow(pos, bit_offset); |
8c572eba MD |
146 | return; |
147 | } | |
148 | } | |
149 | pos->offset += bit_offset; | |
0f980a35 MD |
150 | } |
151 | ||
152 | /* | |
153 | * align_pos - align position on a bit offset (> 0) | |
154 | * | |
155 | * TODO: allow larger files by updating base too. | |
156 | */ | |
157 | static inline | |
46322b33 | 158 | void ctf_align_pos(struct ctf_stream_pos *pos, size_t bit_offset) |
0f980a35 | 159 | { |
46322b33 | 160 | ctf_move_pos(pos, offset_align(pos->offset, bit_offset)); |
0f980a35 MD |
161 | } |
162 | ||
163 | static inline | |
46322b33 | 164 | char *ctf_get_pos_addr(struct ctf_stream_pos *pos) |
0f980a35 MD |
165 | { |
166 | /* Only makes sense to get the address after aligning on CHAR_BIT */ | |
167 | assert(!(pos->offset % CHAR_BIT)); | |
168 | return pos->base + (pos->offset / CHAR_BIT); | |
169 | } | |
170 | ||
8c572eba | 171 | static inline |
46322b33 | 172 | void ctf_dummy_pos(struct ctf_stream_pos *pos, struct ctf_stream_pos *dummy) |
8c572eba | 173 | { |
46322b33 | 174 | memcpy(dummy, pos, sizeof(struct ctf_stream_pos)); |
8c572eba MD |
175 | dummy->dummy = 1; |
176 | dummy->fd = -1; | |
177 | } | |
178 | ||
179 | /* | |
180 | * Check if current packet can hold data. | |
181 | * Returns 0 for success, negative error otherwise. | |
182 | */ | |
183 | static inline | |
46322b33 | 184 | int ctf_pos_packet(struct ctf_stream_pos *dummy) |
8c572eba MD |
185 | { |
186 | if (dummy->offset > dummy->packet_size) | |
187 | return -ENOSPC; | |
188 | return 0; | |
189 | } | |
190 | ||
191 | static inline | |
46322b33 | 192 | void ctf_pos_pad_packet(struct ctf_stream_pos *pos) |
8c572eba | 193 | { |
46322b33 | 194 | ctf_move_pos(pos, pos->packet_size - pos->offset); |
8c572eba MD |
195 | } |
196 | ||
d79865b9 | 197 | #endif /* _BABELTRACE_CTF_TYPES_H */ |