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>
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.
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:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
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
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>
39 #include <babeltrace/align.h>
40 #include <babeltrace/mmap-align.h>
41 #include <babeltrace/endian.h>
42 #include <babeltrace/bitfield.h>
43 #include <babeltrace/compat/fcntl.h>
46 /* "Native" to CTF IR byte order */
47 #if (BYTE_ORDER == LITTLE_ENDIAN)
48 # define MY_BT_CTF_BYTE_ORDER BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
50 # define MY_BT_CTF_BYTE_ORDER BT_CTF_BYTE_ORDER_BIG_ENDIAN
54 # error "Unsupported floating point radix"
63 * The aligned read/write functions are expected to be faster than the
64 * bitfield variants. They will be enabled eventually as an
68 int aligned_integer_write(struct bt_ctf_stream_pos
*pos
,
69 union intval value
, unsigned int alignment
, unsigned int size
,
70 bool is_signed
, enum bt_ctf_byte_order byte_order
)
72 bool rbo
= (byte_order
!= MY_BT_CTF_BYTE_ORDER
); /* reverse byte order */
74 if (!bt_ctf_stream_pos_align(pos
, alignment
))
77 if (!bt_ctf_stream_pos_access_ok(pos
, size
))
80 assert(!(pos
->offset
% CHAR_BIT
));
85 uint8_t v
= value
.unsignd
;
87 memcpy(bt_ctf_stream_pos_get_addr(pos
), &v
, sizeof(v
));
92 uint16_t v
= value
.unsignd
;
95 v
= GUINT16_SWAP_LE_BE(v
);
96 memcpy(bt_ctf_stream_pos_get_addr(pos
), &v
, sizeof(v
));
101 uint32_t v
= value
.unsignd
;
104 v
= GUINT32_SWAP_LE_BE(v
);
105 memcpy(bt_ctf_stream_pos_get_addr(pos
), &v
, sizeof(v
));
110 uint64_t v
= value
.unsignd
;
113 v
= GUINT64_SWAP_LE_BE(v
);
114 memcpy(bt_ctf_stream_pos_get_addr(pos
), &v
, sizeof(v
));
124 uint8_t v
= value
.signd
;
126 memcpy(bt_ctf_stream_pos_get_addr(pos
), &v
, sizeof(v
));
131 int16_t v
= value
.signd
;
134 v
= GUINT16_SWAP_LE_BE(v
);
135 memcpy(bt_ctf_stream_pos_get_addr(pos
), &v
, sizeof(v
));
140 int32_t v
= value
.signd
;
143 v
= GUINT32_SWAP_LE_BE(v
);
144 memcpy(bt_ctf_stream_pos_get_addr(pos
), &v
, sizeof(v
));
149 int64_t v
= value
.signd
;
152 v
= GUINT64_SWAP_LE_BE(v
);
153 memcpy(bt_ctf_stream_pos_get_addr(pos
), &v
, sizeof(v
));
161 if (!bt_ctf_stream_pos_move(pos
, size
))
167 int integer_write(struct bt_ctf_stream_pos
*pos
, union intval value
,
168 unsigned int alignment
, unsigned int size
, bool is_signed
,
169 enum bt_ctf_byte_order byte_order
)
171 if (!(alignment
% CHAR_BIT
)
172 && !(size
% CHAR_BIT
)) {
173 return aligned_integer_write(pos
, value
, alignment
,
174 size
, is_signed
, byte_order
);
177 if (!bt_ctf_stream_pos_align(pos
, alignment
))
180 if (!bt_ctf_stream_pos_access_ok(pos
, size
))
184 if (byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
)
185 bt_bitfield_write_le(mmap_align_addr(pos
->base_mma
) +
186 pos
->mmap_base_offset
, unsigned char,
187 pos
->offset
, size
, value
.unsignd
);
189 bt_bitfield_write_be(mmap_align_addr(pos
->base_mma
) +
190 pos
->mmap_base_offset
, unsigned char,
191 pos
->offset
, size
, value
.unsignd
);
193 if (byte_order
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
)
194 bt_bitfield_write_le(mmap_align_addr(pos
->base_mma
) +
195 pos
->mmap_base_offset
, unsigned char,
196 pos
->offset
, size
, value
.signd
);
198 bt_bitfield_write_be(mmap_align_addr(pos
->base_mma
) +
199 pos
->mmap_base_offset
, unsigned char,
200 pos
->offset
, size
, value
.signd
);
203 if (!bt_ctf_stream_pos_move(pos
, size
))
209 int bt_ctf_field_integer_write(struct bt_ctf_field_integer
*int_field
,
210 struct bt_ctf_stream_pos
*pos
,
211 enum bt_ctf_byte_order native_byte_order
)
213 struct bt_ctf_field_type
*type
= int_field
->parent
.type
;
214 struct bt_ctf_field_type_integer
*int_type
= (void *) type
;
215 enum bt_ctf_byte_order byte_order
;
218 byte_order
= int_type
->user_byte_order
;
219 if (byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
220 byte_order
= native_byte_order
;
223 value
.signd
= int_field
->payload
.signd
;
224 value
.unsignd
= int_field
->payload
.unsignd
;
225 return integer_write(pos
, value
, type
->alignment
,
226 int_type
->size
, int_type
->is_signed
,
231 int bt_ctf_field_floating_point_write(
232 struct bt_ctf_field_floating_point
*flt_field
,
233 struct bt_ctf_stream_pos
*pos
,
234 enum bt_ctf_byte_order native_byte_order
)
236 struct bt_ctf_field_type
*type
= flt_field
->parent
.type
;
237 struct bt_ctf_field_type_floating_point
*flt_type
= (void *) type
;
238 enum bt_ctf_byte_order byte_order
;
242 byte_order
= flt_type
->user_byte_order
;
243 if (byte_order
== BT_CTF_BYTE_ORDER_NATIVE
) {
244 byte_order
= native_byte_order
;
247 if (flt_type
->mant_dig
== FLT_MANT_DIG
) {
253 u32f
.f
= (float) flt_field
->payload
;
254 value
.unsignd
= u32f
.u
;
256 } else if (flt_type
->mant_dig
== DBL_MANT_DIG
) {
262 u64d
.d
= flt_field
->payload
;
263 value
.unsignd
= u64d
.u
;
269 return integer_write(pos
, value
, type
->alignment
, size
, false,
274 void bt_ctf_stream_pos_packet_seek(struct bt_ctf_stream_pos
*pos
, size_t index
,
279 assert(whence
== SEEK_CUR
&& index
== 0);
283 ret
= munmap_align(pos
->base_mma
);
287 pos
->base_mma
= NULL
;
290 /* The writer will add padding */
291 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
292 pos
->content_size
= -1U; /* Unknown at this point */
293 pos
->packet_size
= getpagesize() * 8 * CHAR_BIT
;
295 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
296 pos
->packet_size
/ CHAR_BIT
);
297 } while (ret
== EINTR
);
301 /* map new base. Need mapping length from header. */
302 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
303 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
304 if (pos
->base_mma
== MAP_FAILED
) {