Commit | Line | Data |
---|---|---|
dc3fffef PP |
1 | /* |
2 | * serialize.c | |
3 | * | |
4 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation | |
5 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
6 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
7 | * | |
8 | * The original author of the serialization functions for Babeltrace 1 | |
9 | * is Mathieu Desnoyers. Philippe Proulx modified the functions in 2017 | |
10 | * to use Babeltrace 2 objects. | |
11 | * | |
12 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
13 | * of this software and associated documentation files (the "Software"), to deal | |
14 | * in the Software without restriction, including without limitation the rights | |
15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
16 | * copies of the Software, and to permit persons to whom the Software is | |
17 | * furnished to do so, subject to the following conditions: | |
18 | * | |
19 | * The above copyright notice and this permission notice shall be included in | |
20 | * all copies or substantial portions of the Software. | |
21 | * | |
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
28 | * SOFTWARE. | |
29 | */ | |
30 | ||
0fbb9a9f | 31 | #include <stdlib.h> |
dc3fffef PP |
32 | #include <stdio.h> |
33 | #include <stdint.h> | |
dc3fffef PP |
34 | #include <babeltrace/ctf-ir/field-types.h> |
35 | #include <babeltrace/ctf-ir/field-types-internal.h> | |
36 | #include <babeltrace/ctf-ir/fields.h> | |
37 | #include <babeltrace/ctf-ir/fields-internal.h> | |
38 | #include <babeltrace/ctf-writer/serialize-internal.h> | |
3d9990ac PP |
39 | #include <babeltrace/align-internal.h> |
40 | #include <babeltrace/mmap-align-internal.h> | |
41 | #include <babeltrace/endian-internal.h> | |
42 | #include <babeltrace/bitfield-internal.h> | |
43 | #include <babeltrace/compat/fcntl-internal.h> | |
c55a9f58 | 44 | #include <babeltrace/types.h> |
dc3fffef PP |
45 | #include <glib.h> |
46 | ||
dc3fffef PP |
47 | #if (FLT_RADIX != 2) |
48 | # error "Unsupported floating point radix" | |
49 | #endif | |
50 | ||
51 | union intval { | |
52 | int64_t signd; | |
53 | uint64_t unsignd; | |
54 | }; | |
55 | ||
56 | /* | |
57 | * The aligned read/write functions are expected to be faster than the | |
58 | * bitfield variants. They will be enabled eventually as an | |
59 | * optimisation. | |
60 | */ | |
61 | static | |
62 | int aligned_integer_write(struct bt_ctf_stream_pos *pos, | |
63 | union intval value, unsigned int alignment, unsigned int size, | |
c55a9f58 | 64 | bt_bool is_signed, enum bt_ctf_byte_order byte_order) |
dc3fffef | 65 | { |
c55a9f58 | 66 | bt_bool rbo = (byte_order != BT_CTF_MY_BYTE_ORDER); /* reverse byte order */ |
dc3fffef PP |
67 | |
68 | if (!bt_ctf_stream_pos_align(pos, alignment)) | |
69 | return -EFAULT; | |
70 | ||
71 | if (!bt_ctf_stream_pos_access_ok(pos, size)) | |
72 | return -EFAULT; | |
73 | ||
74 | assert(!(pos->offset % CHAR_BIT)); | |
75 | if (!is_signed) { | |
76 | switch (size) { | |
77 | case 8: | |
78 | { | |
79 | uint8_t v = value.unsignd; | |
80 | ||
81 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); | |
82 | break; | |
83 | } | |
84 | case 16: | |
85 | { | |
86 | uint16_t v = value.unsignd; | |
87 | ||
88 | if (rbo) | |
89 | v = GUINT16_SWAP_LE_BE(v); | |
90 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); | |
91 | break; | |
92 | } | |
93 | case 32: | |
94 | { | |
95 | uint32_t v = value.unsignd; | |
96 | ||
97 | if (rbo) | |
98 | v = GUINT32_SWAP_LE_BE(v); | |
99 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); | |
100 | break; | |
101 | } | |
102 | case 64: | |
103 | { | |
104 | uint64_t v = value.unsignd; | |
105 | ||
106 | if (rbo) | |
107 | v = GUINT64_SWAP_LE_BE(v); | |
108 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); | |
109 | break; | |
110 | } | |
111 | default: | |
0fbb9a9f | 112 | abort(); |
dc3fffef PP |
113 | } |
114 | } else { | |
115 | switch (size) { | |
116 | case 8: | |
117 | { | |
118 | uint8_t v = value.signd; | |
119 | ||
120 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); | |
121 | break; | |
122 | } | |
123 | case 16: | |
124 | { | |
125 | int16_t v = value.signd; | |
126 | ||
127 | if (rbo) | |
128 | v = GUINT16_SWAP_LE_BE(v); | |
129 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); | |
130 | break; | |
131 | } | |
132 | case 32: | |
133 | { | |
134 | int32_t v = value.signd; | |
135 | ||
136 | if (rbo) | |
137 | v = GUINT32_SWAP_LE_BE(v); | |
138 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); | |
139 | break; | |
140 | } | |
141 | case 64: | |
142 | { | |
143 | int64_t v = value.signd; | |
144 | ||
145 | if (rbo) | |
146 | v = GUINT64_SWAP_LE_BE(v); | |
147 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); | |
148 | break; | |
149 | } | |
150 | default: | |
0fbb9a9f | 151 | abort(); |
dc3fffef PP |
152 | } |
153 | } | |
154 | ||
155 | if (!bt_ctf_stream_pos_move(pos, size)) | |
156 | return -EFAULT; | |
157 | return 0; | |
158 | } | |
159 | ||
160 | static | |
161 | int integer_write(struct bt_ctf_stream_pos *pos, union intval value, | |
c55a9f58 | 162 | unsigned int alignment, unsigned int size, bt_bool is_signed, |
dc3fffef PP |
163 | enum bt_ctf_byte_order byte_order) |
164 | { | |
165 | if (!(alignment % CHAR_BIT) | |
166 | && !(size % CHAR_BIT)) { | |
167 | return aligned_integer_write(pos, value, alignment, | |
168 | size, is_signed, byte_order); | |
169 | } | |
170 | ||
171 | if (!bt_ctf_stream_pos_align(pos, alignment)) | |
172 | return -EFAULT; | |
173 | ||
174 | if (!bt_ctf_stream_pos_access_ok(pos, size)) | |
175 | return -EFAULT; | |
176 | ||
177 | if (!is_signed) { | |
178 | if (byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) | |
179 | bt_bitfield_write_le(mmap_align_addr(pos->base_mma) + | |
180 | pos->mmap_base_offset, unsigned char, | |
181 | pos->offset, size, value.unsignd); | |
182 | else | |
183 | bt_bitfield_write_be(mmap_align_addr(pos->base_mma) + | |
184 | pos->mmap_base_offset, unsigned char, | |
185 | pos->offset, size, value.unsignd); | |
186 | } else { | |
187 | if (byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) | |
188 | bt_bitfield_write_le(mmap_align_addr(pos->base_mma) + | |
189 | pos->mmap_base_offset, unsigned char, | |
190 | pos->offset, size, value.signd); | |
191 | else | |
192 | bt_bitfield_write_be(mmap_align_addr(pos->base_mma) + | |
193 | pos->mmap_base_offset, unsigned char, | |
194 | pos->offset, size, value.signd); | |
195 | } | |
196 | ||
197 | if (!bt_ctf_stream_pos_move(pos, size)) | |
198 | return -EFAULT; | |
199 | return 0; | |
200 | } | |
201 | ||
202 | BT_HIDDEN | |
203 | int bt_ctf_field_integer_write(struct bt_ctf_field_integer *int_field, | |
204 | struct bt_ctf_stream_pos *pos, | |
205 | enum bt_ctf_byte_order native_byte_order) | |
206 | { | |
207 | struct bt_ctf_field_type *type = int_field->parent.type; | |
208 | struct bt_ctf_field_type_integer *int_type = (void *) type; | |
209 | enum bt_ctf_byte_order byte_order; | |
210 | union intval value; | |
211 | ||
212 | byte_order = int_type->user_byte_order; | |
213 | if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) { | |
214 | byte_order = native_byte_order; | |
215 | } | |
216 | ||
217 | value.signd = int_field->payload.signd; | |
218 | value.unsignd = int_field->payload.unsignd; | |
219 | return integer_write(pos, value, type->alignment, | |
220 | int_type->size, int_type->is_signed, | |
221 | byte_order); | |
222 | } | |
223 | ||
224 | BT_HIDDEN | |
225 | int bt_ctf_field_floating_point_write( | |
226 | struct bt_ctf_field_floating_point *flt_field, | |
227 | struct bt_ctf_stream_pos *pos, | |
228 | enum bt_ctf_byte_order native_byte_order) | |
229 | { | |
230 | struct bt_ctf_field_type *type = flt_field->parent.type; | |
231 | struct bt_ctf_field_type_floating_point *flt_type = (void *) type; | |
232 | enum bt_ctf_byte_order byte_order; | |
233 | union intval value; | |
234 | unsigned int size; | |
235 | ||
236 | byte_order = flt_type->user_byte_order; | |
237 | if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) { | |
238 | byte_order = native_byte_order; | |
239 | } | |
240 | ||
241 | if (flt_type->mant_dig == FLT_MANT_DIG) { | |
242 | union u32f { | |
243 | uint32_t u; | |
244 | float f; | |
245 | } u32f; | |
246 | ||
247 | u32f.f = (float) flt_field->payload; | |
248 | value.unsignd = u32f.u; | |
249 | size = 32; | |
250 | } else if (flt_type->mant_dig == DBL_MANT_DIG) { | |
251 | union u64d { | |
252 | uint64_t u; | |
253 | double d; | |
254 | } u64d; | |
255 | ||
256 | u64d.d = flt_field->payload; | |
257 | value.unsignd = u64d.u; | |
258 | size = 64; | |
259 | } else { | |
260 | return -EINVAL; | |
261 | } | |
262 | ||
c55a9f58 | 263 | return integer_write(pos, value, type->alignment, size, BT_FALSE, |
dc3fffef PP |
264 | byte_order); |
265 | } | |
266 | ||
267 | BT_HIDDEN | |
268 | void bt_ctf_stream_pos_packet_seek(struct bt_ctf_stream_pos *pos, size_t index, | |
269 | int whence) | |
270 | { | |
271 | int ret; | |
272 | ||
273 | assert(whence == SEEK_CUR && index == 0); | |
274 | ||
275 | if (pos->base_mma) { | |
276 | /* unmap old base */ | |
277 | ret = munmap_align(pos->base_mma); | |
278 | if (ret) { | |
0fbb9a9f PP |
279 | // FIXME: this can legitimately fail? |
280 | abort(); | |
dc3fffef PP |
281 | } |
282 | pos->base_mma = NULL; | |
283 | } | |
284 | ||
285 | /* The writer will add padding */ | |
286 | pos->mmap_offset += pos->packet_size / CHAR_BIT; | |
dc3fffef PP |
287 | pos->packet_size = getpagesize() * 8 * CHAR_BIT; |
288 | do { | |
289 | ret = bt_posix_fallocate(pos->fd, pos->mmap_offset, | |
290 | pos->packet_size / CHAR_BIT); | |
291 | } while (ret == EINTR); | |
292 | assert(ret == 0); | |
293 | pos->offset = 0; | |
294 | ||
295 | /* map new base. Need mapping length from header. */ | |
296 | pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot, | |
297 | pos->flags, pos->fd, pos->mmap_offset); | |
298 | if (pos->base_mma == MAP_FAILED) { | |
0fbb9a9f PP |
299 | // FIXME: this can legitimately fail? |
300 | abort(); | |
dc3fffef PP |
301 | } |
302 | } |