| 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 | |
| 31 | #define BT_LOG_TAG "CTF-WRITER-SERIALIZE" |
| 32 | #include <babeltrace/lib-logging-internal.h> |
| 33 | |
| 34 | #include <babeltrace/align-internal.h> |
| 35 | #include <babeltrace/bitfield-internal.h> |
| 36 | #include <babeltrace/common-internal.h> |
| 37 | #include <babeltrace/compat/fcntl-internal.h> |
| 38 | #include <babeltrace/ctf-writer/field-types-internal.h> |
| 39 | #include <babeltrace/ctf-writer/field-types.h> |
| 40 | #include <babeltrace/ctf-writer/fields-internal.h> |
| 41 | #include <babeltrace/ctf-writer/fields.h> |
| 42 | #include <babeltrace/ctf-writer/serialize-internal.h> |
| 43 | #include <babeltrace/ctf-writer/utils-internal.h> |
| 44 | #include <babeltrace/endian-internal.h> |
| 45 | #include <babeltrace/mmap-align-internal.h> |
| 46 | #include <babeltrace/types.h> |
| 47 | #include <glib.h> |
| 48 | #include <stdint.h> |
| 49 | #include <stdio.h> |
| 50 | #include <stdlib.h> |
| 51 | |
| 52 | #if (FLT_RADIX != 2) |
| 53 | # error "Unsupported floating point radix" |
| 54 | #endif |
| 55 | |
| 56 | union intval { |
| 57 | int64_t signd; |
| 58 | uint64_t unsignd; |
| 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * The aligned read/write functions are expected to be faster than the |
| 63 | * bitfield variants. They will be enabled eventually as an |
| 64 | * optimisation. |
| 65 | */ |
| 66 | static |
| 67 | int aligned_integer_write(struct bt_ctf_stream_pos *pos, union intval value, |
| 68 | unsigned int alignment, unsigned int size, bt_bool is_signed, |
| 69 | enum bt_ctf_byte_order byte_order) |
| 70 | { |
| 71 | /* reverse byte order */ |
| 72 | bt_bool rbo = (byte_order != BT_CTF_MY_BYTE_ORDER); |
| 73 | |
| 74 | if (!bt_ctf_stream_pos_align(pos, alignment)) |
| 75 | return -EFAULT; |
| 76 | |
| 77 | if (!bt_ctf_stream_pos_access_ok(pos, size)) |
| 78 | return -EFAULT; |
| 79 | |
| 80 | BT_ASSERT(!(pos->offset % CHAR_BIT)); |
| 81 | if (!is_signed) { |
| 82 | switch (size) { |
| 83 | case 8: |
| 84 | { |
| 85 | uint8_t v = value.unsignd; |
| 86 | |
| 87 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); |
| 88 | break; |
| 89 | } |
| 90 | case 16: |
| 91 | { |
| 92 | uint16_t v = value.unsignd; |
| 93 | |
| 94 | if (rbo) |
| 95 | v = GUINT16_SWAP_LE_BE(v); |
| 96 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); |
| 97 | break; |
| 98 | } |
| 99 | case 32: |
| 100 | { |
| 101 | uint32_t v = value.unsignd; |
| 102 | |
| 103 | if (rbo) |
| 104 | v = GUINT32_SWAP_LE_BE(v); |
| 105 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); |
| 106 | break; |
| 107 | } |
| 108 | case 64: |
| 109 | { |
| 110 | uint64_t v = value.unsignd; |
| 111 | |
| 112 | if (rbo) |
| 113 | v = GUINT64_SWAP_LE_BE(v); |
| 114 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); |
| 115 | break; |
| 116 | } |
| 117 | default: |
| 118 | abort(); |
| 119 | } |
| 120 | } else { |
| 121 | switch (size) { |
| 122 | case 8: |
| 123 | { |
| 124 | uint8_t v = value.signd; |
| 125 | |
| 126 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); |
| 127 | break; |
| 128 | } |
| 129 | case 16: |
| 130 | { |
| 131 | int16_t v = value.signd; |
| 132 | |
| 133 | if (rbo) |
| 134 | v = GUINT16_SWAP_LE_BE(v); |
| 135 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); |
| 136 | break; |
| 137 | } |
| 138 | case 32: |
| 139 | { |
| 140 | int32_t v = value.signd; |
| 141 | |
| 142 | if (rbo) |
| 143 | v = GUINT32_SWAP_LE_BE(v); |
| 144 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); |
| 145 | break; |
| 146 | } |
| 147 | case 64: |
| 148 | { |
| 149 | int64_t v = value.signd; |
| 150 | |
| 151 | if (rbo) |
| 152 | v = GUINT64_SWAP_LE_BE(v); |
| 153 | memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v)); |
| 154 | break; |
| 155 | } |
| 156 | default: |
| 157 | abort(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (!bt_ctf_stream_pos_move(pos, size)) |
| 162 | return -EFAULT; |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static |
| 167 | int integer_write(struct bt_ctf_stream_pos *pos, union intval value, |
| 168 | unsigned int alignment, unsigned int size, bt_bool is_signed, |
| 169 | enum bt_ctf_byte_order byte_order) |
| 170 | { |
| 171 | if (!(alignment % CHAR_BIT) |
| 172 | && !(size % CHAR_BIT)) { |
| 173 | return aligned_integer_write(pos, value, alignment, |
| 174 | size, is_signed, byte_order); |
| 175 | } |
| 176 | |
| 177 | if (!bt_ctf_stream_pos_align(pos, alignment)) |
| 178 | return -EFAULT; |
| 179 | |
| 180 | if (!bt_ctf_stream_pos_access_ok(pos, size)) |
| 181 | return -EFAULT; |
| 182 | |
| 183 | if (!is_signed) { |
| 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); |
| 188 | else |
| 189 | bt_bitfield_write_be(mmap_align_addr(pos->base_mma) + |
| 190 | pos->mmap_base_offset, unsigned char, |
| 191 | pos->offset, size, value.unsignd); |
| 192 | } else { |
| 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); |
| 197 | else |
| 198 | bt_bitfield_write_be(mmap_align_addr(pos->base_mma) + |
| 199 | pos->mmap_base_offset, unsigned char, |
| 200 | pos->offset, size, value.signd); |
| 201 | } |
| 202 | |
| 203 | if (!bt_ctf_stream_pos_move(pos, size)) |
| 204 | return -EFAULT; |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | BT_HIDDEN |
| 209 | int bt_ctf_field_integer_write(struct bt_ctf_field_common *field, |
| 210 | struct bt_ctf_stream_pos *pos, |
| 211 | enum bt_ctf_byte_order native_byte_order) |
| 212 | { |
| 213 | struct bt_ctf_field_type_common_integer *int_type = |
| 214 | BT_CTF_FROM_COMMON(field->type); |
| 215 | struct bt_ctf_field_common_integer *int_field = BT_CTF_FROM_COMMON(field); |
| 216 | enum bt_ctf_byte_order byte_order; |
| 217 | union intval value; |
| 218 | |
| 219 | byte_order = (int) int_type->user_byte_order; |
| 220 | if ((int) byte_order == BT_CTF_BYTE_ORDER_NATIVE) { |
| 221 | byte_order = native_byte_order; |
| 222 | } |
| 223 | |
| 224 | value.signd = int_field->payload.signd; |
| 225 | value.unsignd = int_field->payload.unsignd; |
| 226 | return integer_write(pos, value, int_type->common.alignment, |
| 227 | int_type->size, int_type->is_signed, |
| 228 | byte_order); |
| 229 | } |
| 230 | |
| 231 | BT_HIDDEN |
| 232 | int bt_ctf_field_floating_point_write( |
| 233 | struct bt_ctf_field_common *field, |
| 234 | struct bt_ctf_stream_pos *pos, |
| 235 | enum bt_ctf_byte_order native_byte_order) |
| 236 | { |
| 237 | struct bt_ctf_field_type_common_floating_point *flt_type = |
| 238 | BT_CTF_FROM_COMMON(field->type); |
| 239 | struct bt_ctf_field_common_floating_point *flt_field = BT_CTF_FROM_COMMON(field); |
| 240 | enum bt_ctf_byte_order byte_order; |
| 241 | union intval value; |
| 242 | unsigned int size; |
| 243 | |
| 244 | byte_order = (int) flt_type->user_byte_order; |
| 245 | if ((int) byte_order == BT_CTF_BYTE_ORDER_NATIVE) { |
| 246 | byte_order = native_byte_order; |
| 247 | } |
| 248 | |
| 249 | if (flt_type->mant_dig == FLT_MANT_DIG) { |
| 250 | union u32f { |
| 251 | uint32_t u; |
| 252 | float f; |
| 253 | } u32f; |
| 254 | |
| 255 | u32f.f = (float) flt_field->payload; |
| 256 | value.unsignd = u32f.u; |
| 257 | size = 32; |
| 258 | } else if (flt_type->mant_dig == DBL_MANT_DIG) { |
| 259 | union u64d { |
| 260 | uint64_t u; |
| 261 | double d; |
| 262 | } u64d; |
| 263 | |
| 264 | u64d.d = flt_field->payload; |
| 265 | value.unsignd = u64d.u; |
| 266 | size = 64; |
| 267 | } else { |
| 268 | return -EINVAL; |
| 269 | } |
| 270 | |
| 271 | return integer_write(pos, value, flt_type->common.alignment, |
| 272 | size, BT_FALSE, byte_order); |
| 273 | } |
| 274 | |
| 275 | BT_HIDDEN |
| 276 | void bt_ctf_stream_pos_packet_seek(struct bt_ctf_stream_pos *pos, size_t index, |
| 277 | int whence) |
| 278 | { |
| 279 | int ret; |
| 280 | |
| 281 | BT_ASSERT(whence == SEEK_CUR && index == 0); |
| 282 | |
| 283 | if (pos->base_mma) { |
| 284 | /* unmap old base */ |
| 285 | ret = munmap_align(pos->base_mma); |
| 286 | if (ret) { |
| 287 | // FIXME: this can legitimately fail? |
| 288 | abort(); |
| 289 | } |
| 290 | pos->base_mma = NULL; |
| 291 | } |
| 292 | |
| 293 | /* The writer will add padding */ |
| 294 | pos->mmap_offset += pos->packet_size / CHAR_BIT; |
| 295 | pos->packet_size = PACKET_LEN_INCREMENT; |
| 296 | do { |
| 297 | ret = bt_posix_fallocate(pos->fd, pos->mmap_offset, |
| 298 | pos->packet_size / CHAR_BIT); |
| 299 | } while (ret == EINTR); |
| 300 | BT_ASSERT(ret == 0); |
| 301 | pos->offset = 0; |
| 302 | |
| 303 | /* map new base. Need mapping length from header. */ |
| 304 | pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot, |
| 305 | pos->flags, pos->fd, pos->mmap_offset); |
| 306 | if (pos->base_mma == MAP_FAILED) { |
| 307 | // FIXME: this can legitimately fail? |
| 308 | abort(); |
| 309 | } |
| 310 | } |