| 1 |
/* |
| 2 |
* Common Trace Format |
| 3 |
* |
| 4 |
* Integers read/write functions. |
| 5 |
* |
| 6 |
* Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 |
* |
| 8 |
* Author: Mathieu Desnoyers <[email protected]> |
| 9 |
* |
| 10 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 |
* of this software and associated documentation files (the "Software"), to deal |
| 12 |
* in the Software without restriction, including without limitation the rights |
| 13 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 |
* copies of the Software, and to permit persons to whom the Software is |
| 15 |
* furnished to do so, subject to the following conditions: |
| 16 |
* |
| 17 |
* The above copyright notice and this permission notice shall be included in |
| 18 |
* all copies or substantial portions of the Software. |
| 19 |
* |
| 20 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 |
* SOFTWARE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <babeltrace/ctf/types.h> |
| 30 |
#include <babeltrace/bitfield.h> |
| 31 |
#include <stdint.h> |
| 32 |
#include <glib.h> |
| 33 |
#include <babeltrace/endian.h> |
| 34 |
|
| 35 |
/* |
| 36 |
* The aligned read/write functions are expected to be faster than the |
| 37 |
* bitfield variants. They will be enabled eventually as an |
| 38 |
* optimisation. |
| 39 |
*/ |
| 40 |
|
| 41 |
static |
| 42 |
int _aligned_integer_read(struct bt_stream_pos *ppos, |
| 43 |
struct bt_definition *definition) |
| 44 |
{ |
| 45 |
struct definition_integer *integer_definition = |
| 46 |
container_of(definition, struct definition_integer, p); |
| 47 |
const struct declaration_integer *integer_declaration = |
| 48 |
integer_definition->declaration; |
| 49 |
struct ctf_stream_pos *pos = ctf_pos(ppos); |
| 50 |
int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */ |
| 51 |
|
| 52 |
if (!ctf_align_pos(pos, integer_declaration->p.alignment)) |
| 53 |
return -EFAULT; |
| 54 |
|
| 55 |
if (!ctf_pos_access_ok(pos, integer_declaration->len)) |
| 56 |
return -EFAULT; |
| 57 |
|
| 58 |
assert(!(pos->offset % CHAR_BIT)); |
| 59 |
if (!integer_declaration->signedness) { |
| 60 |
switch (integer_declaration->len) { |
| 61 |
case 8: |
| 62 |
{ |
| 63 |
uint8_t v; |
| 64 |
|
| 65 |
memcpy(&v, ctf_get_pos_addr(pos), sizeof(v)); |
| 66 |
integer_definition->value._unsigned = v; |
| 67 |
break; |
| 68 |
} |
| 69 |
case 16: |
| 70 |
{ |
| 71 |
uint16_t v; |
| 72 |
|
| 73 |
memcpy(&v, ctf_get_pos_addr(pos), sizeof(v)); |
| 74 |
integer_definition->value._unsigned = |
| 75 |
rbo ? GUINT16_SWAP_LE_BE(v) : v; |
| 76 |
break; |
| 77 |
} |
| 78 |
case 32: |
| 79 |
{ |
| 80 |
uint32_t v; |
| 81 |
|
| 82 |
memcpy(&v, ctf_get_pos_addr(pos), sizeof(v)); |
| 83 |
integer_definition->value._unsigned = |
| 84 |
rbo ? GUINT32_SWAP_LE_BE(v) : v; |
| 85 |
break; |
| 86 |
} |
| 87 |
case 64: |
| 88 |
{ |
| 89 |
uint64_t v; |
| 90 |
|
| 91 |
memcpy(&v, ctf_get_pos_addr(pos), sizeof(v)); |
| 92 |
integer_definition->value._unsigned = |
| 93 |
rbo ? GUINT64_SWAP_LE_BE(v) : v; |
| 94 |
break; |
| 95 |
} |
| 96 |
default: |
| 97 |
assert(0); |
| 98 |
} |
| 99 |
} else { |
| 100 |
switch (integer_declaration->len) { |
| 101 |
case 8: |
| 102 |
{ |
| 103 |
int8_t v; |
| 104 |
|
| 105 |
memcpy(&v, ctf_get_pos_addr(pos), sizeof(v)); |
| 106 |
integer_definition->value._signed = v; |
| 107 |
break; |
| 108 |
} |
| 109 |
case 16: |
| 110 |
{ |
| 111 |
int16_t v; |
| 112 |
|
| 113 |
memcpy(&v, ctf_get_pos_addr(pos), sizeof(v)); |
| 114 |
integer_definition->value._signed = |
| 115 |
rbo ? (int16_t) GUINT16_SWAP_LE_BE(v) : v; |
| 116 |
break; |
| 117 |
} |
| 118 |
case 32: |
| 119 |
{ |
| 120 |
int32_t v; |
| 121 |
|
| 122 |
memcpy(&v, ctf_get_pos_addr(pos), sizeof(v)); |
| 123 |
integer_definition->value._signed = |
| 124 |
rbo ? (int32_t) GUINT32_SWAP_LE_BE(v) : v; |
| 125 |
break; |
| 126 |
} |
| 127 |
case 64: |
| 128 |
{ |
| 129 |
int64_t v; |
| 130 |
|
| 131 |
memcpy(&v, ctf_get_pos_addr(pos), sizeof(v)); |
| 132 |
integer_definition->value._signed = |
| 133 |
rbo ? (int64_t) GUINT64_SWAP_LE_BE(v) : v; |
| 134 |
break; |
| 135 |
} |
| 136 |
default: |
| 137 |
assert(0); |
| 138 |
} |
| 139 |
} |
| 140 |
if (!ctf_move_pos(pos, integer_declaration->len)) |
| 141 |
return -EFAULT; |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
static |
| 146 |
int _aligned_integer_write(struct bt_stream_pos *ppos, |
| 147 |
struct bt_definition *definition) |
| 148 |
{ |
| 149 |
struct definition_integer *integer_definition = |
| 150 |
container_of(definition, struct definition_integer, p); |
| 151 |
const struct declaration_integer *integer_declaration = |
| 152 |
integer_definition->declaration; |
| 153 |
struct ctf_stream_pos *pos = ctf_pos(ppos); |
| 154 |
int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */ |
| 155 |
|
| 156 |
if (!ctf_align_pos(pos, integer_declaration->p.alignment)) |
| 157 |
return -EFAULT; |
| 158 |
|
| 159 |
if (!ctf_pos_access_ok(pos, integer_declaration->len)) |
| 160 |
return -EFAULT; |
| 161 |
|
| 162 |
assert(!(pos->offset % CHAR_BIT)); |
| 163 |
if (pos->dummy) |
| 164 |
goto end; |
| 165 |
if (!integer_declaration->signedness) { |
| 166 |
switch (integer_declaration->len) { |
| 167 |
case 8: |
| 168 |
{ |
| 169 |
uint8_t v = integer_definition->value._unsigned; |
| 170 |
|
| 171 |
memcpy(ctf_get_pos_addr(pos), &v, sizeof(v)); |
| 172 |
break; |
| 173 |
} |
| 174 |
case 16: |
| 175 |
{ |
| 176 |
uint16_t v = integer_definition->value._unsigned; |
| 177 |
|
| 178 |
if (rbo) |
| 179 |
v = GUINT16_SWAP_LE_BE(v); |
| 180 |
memcpy(ctf_get_pos_addr(pos), &v, sizeof(v)); |
| 181 |
break; |
| 182 |
} |
| 183 |
case 32: |
| 184 |
{ |
| 185 |
uint32_t v = integer_definition->value._unsigned; |
| 186 |
|
| 187 |
if (rbo) |
| 188 |
v = GUINT32_SWAP_LE_BE(v); |
| 189 |
memcpy(ctf_get_pos_addr(pos), &v, sizeof(v)); |
| 190 |
break; |
| 191 |
} |
| 192 |
case 64: |
| 193 |
{ |
| 194 |
uint64_t v = integer_definition->value._unsigned; |
| 195 |
|
| 196 |
if (rbo) |
| 197 |
v = GUINT64_SWAP_LE_BE(v); |
| 198 |
memcpy(ctf_get_pos_addr(pos), &v, sizeof(v)); |
| 199 |
break; |
| 200 |
} |
| 201 |
default: |
| 202 |
assert(0); |
| 203 |
} |
| 204 |
} else { |
| 205 |
switch (integer_declaration->len) { |
| 206 |
case 8: |
| 207 |
{ |
| 208 |
uint8_t v = integer_definition->value._signed; |
| 209 |
|
| 210 |
memcpy(ctf_get_pos_addr(pos), &v, sizeof(v)); |
| 211 |
break; |
| 212 |
} |
| 213 |
case 16: |
| 214 |
{ |
| 215 |
int16_t v = integer_definition->value._signed; |
| 216 |
|
| 217 |
if (rbo) |
| 218 |
v = GUINT16_SWAP_LE_BE(v); |
| 219 |
memcpy(ctf_get_pos_addr(pos), &v, sizeof(v)); |
| 220 |
break; |
| 221 |
} |
| 222 |
case 32: |
| 223 |
{ |
| 224 |
int32_t v = integer_definition->value._signed; |
| 225 |
|
| 226 |
if (rbo) |
| 227 |
v = GUINT32_SWAP_LE_BE(v); |
| 228 |
memcpy(ctf_get_pos_addr(pos), &v, sizeof(v)); |
| 229 |
break; |
| 230 |
} |
| 231 |
case 64: |
| 232 |
{ |
| 233 |
int64_t v = integer_definition->value._signed; |
| 234 |
|
| 235 |
if (rbo) |
| 236 |
v = GUINT64_SWAP_LE_BE(v); |
| 237 |
memcpy(ctf_get_pos_addr(pos), &v, sizeof(v)); |
| 238 |
break; |
| 239 |
} |
| 240 |
default: |
| 241 |
assert(0); |
| 242 |
} |
| 243 |
} |
| 244 |
end: |
| 245 |
if (!ctf_move_pos(pos, integer_declaration->len)) |
| 246 |
return -EFAULT; |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
int ctf_integer_read(struct bt_stream_pos *ppos, struct bt_definition *definition) |
| 251 |
{ |
| 252 |
struct definition_integer *integer_definition = |
| 253 |
container_of(definition, struct definition_integer, p); |
| 254 |
const struct declaration_integer *integer_declaration = |
| 255 |
integer_definition->declaration; |
| 256 |
struct ctf_stream_pos *pos = ctf_pos(ppos); |
| 257 |
|
| 258 |
if (!(integer_declaration->p.alignment % CHAR_BIT) |
| 259 |
&& !(integer_declaration->len % CHAR_BIT)) { |
| 260 |
return _aligned_integer_read(ppos, definition); |
| 261 |
} |
| 262 |
|
| 263 |
if (!ctf_align_pos(pos, integer_declaration->p.alignment)) |
| 264 |
return -EFAULT; |
| 265 |
|
| 266 |
if (!ctf_pos_access_ok(pos, integer_declaration->len)) |
| 267 |
return -EFAULT; |
| 268 |
|
| 269 |
if (!integer_declaration->signedness) { |
| 270 |
if (integer_declaration->byte_order == LITTLE_ENDIAN) |
| 271 |
bt_bitfield_read_le(mmap_align_addr(pos->base_mma) + |
| 272 |
pos->mmap_base_offset, unsigned char, |
| 273 |
pos->offset, integer_declaration->len, |
| 274 |
&integer_definition->value._unsigned); |
| 275 |
else |
| 276 |
bt_bitfield_read_be(mmap_align_addr(pos->base_mma) + |
| 277 |
pos->mmap_base_offset, unsigned char, |
| 278 |
pos->offset, integer_declaration->len, |
| 279 |
&integer_definition->value._unsigned); |
| 280 |
} else { |
| 281 |
if (integer_declaration->byte_order == LITTLE_ENDIAN) |
| 282 |
bt_bitfield_read_le(mmap_align_addr(pos->base_mma) + |
| 283 |
pos->mmap_base_offset, unsigned char, |
| 284 |
pos->offset, integer_declaration->len, |
| 285 |
&integer_definition->value._signed); |
| 286 |
else |
| 287 |
bt_bitfield_read_be(mmap_align_addr(pos->base_mma) + |
| 288 |
pos->mmap_base_offset, unsigned char, |
| 289 |
pos->offset, integer_declaration->len, |
| 290 |
&integer_definition->value._signed); |
| 291 |
} |
| 292 |
if (!ctf_move_pos(pos, integer_declaration->len)) |
| 293 |
return -EFAULT; |
| 294 |
return 0; |
| 295 |
} |
| 296 |
|
| 297 |
int ctf_integer_write(struct bt_stream_pos *ppos, struct bt_definition *definition) |
| 298 |
{ |
| 299 |
struct definition_integer *integer_definition = |
| 300 |
container_of(definition, struct definition_integer, p); |
| 301 |
const struct declaration_integer *integer_declaration = |
| 302 |
integer_definition->declaration; |
| 303 |
struct ctf_stream_pos *pos = ctf_pos(ppos); |
| 304 |
|
| 305 |
if (!(integer_declaration->p.alignment % CHAR_BIT) |
| 306 |
&& !(integer_declaration->len % CHAR_BIT)) { |
| 307 |
return _aligned_integer_write(ppos, definition); |
| 308 |
} |
| 309 |
|
| 310 |
if (!ctf_align_pos(pos, integer_declaration->p.alignment)) |
| 311 |
return -EFAULT; |
| 312 |
|
| 313 |
if (!ctf_pos_access_ok(pos, integer_declaration->len)) |
| 314 |
return -EFAULT; |
| 315 |
|
| 316 |
if (pos->dummy) |
| 317 |
goto end; |
| 318 |
if (!integer_declaration->signedness) { |
| 319 |
if (integer_declaration->byte_order == LITTLE_ENDIAN) |
| 320 |
bt_bitfield_write_le(mmap_align_addr(pos->base_mma) + |
| 321 |
pos->mmap_base_offset, unsigned char, |
| 322 |
pos->offset, integer_declaration->len, |
| 323 |
integer_definition->value._unsigned); |
| 324 |
else |
| 325 |
bt_bitfield_write_be(mmap_align_addr(pos->base_mma) + |
| 326 |
pos->mmap_base_offset, unsigned char, |
| 327 |
pos->offset, integer_declaration->len, |
| 328 |
integer_definition->value._unsigned); |
| 329 |
} else { |
| 330 |
if (integer_declaration->byte_order == LITTLE_ENDIAN) |
| 331 |
bt_bitfield_write_le(mmap_align_addr(pos->base_mma) + |
| 332 |
pos->mmap_base_offset, unsigned char, |
| 333 |
pos->offset, integer_declaration->len, |
| 334 |
integer_definition->value._signed); |
| 335 |
else |
| 336 |
bt_bitfield_write_be(mmap_align_addr(pos->base_mma) + |
| 337 |
pos->mmap_base_offset, unsigned char, |
| 338 |
pos->offset, integer_declaration->len, |
| 339 |
integer_definition->value._signed); |
| 340 |
} |
| 341 |
end: |
| 342 |
if (!ctf_move_pos(pos, integer_declaration->len)) |
| 343 |
return -EFAULT; |
| 344 |
return 0; |
| 345 |
} |