| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2020 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #include <babeltrace2/babeltrace.h> |
| 8 | #include "common/macros.h" |
| 9 | #include "common/assert.h" |
| 10 | #include <glib.h> |
| 11 | #include <stdint.h> |
| 12 | #include <string.h> |
| 13 | #include <inttypes.h> |
| 14 | |
| 15 | #include "ctf-meta-visitors.hpp" |
| 16 | |
| 17 | static inline int set_alignments(struct ctf_field_class *fc) |
| 18 | { |
| 19 | int ret = 0; |
| 20 | uint64_t i; |
| 21 | |
| 22 | if (!fc) { |
| 23 | goto end; |
| 24 | } |
| 25 | |
| 26 | switch (fc->type) { |
| 27 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
| 28 | { |
| 29 | struct ctf_field_class_struct *struct_fc = ctf_field_class_as_struct(fc); |
| 30 | |
| 31 | for (i = 0; i < struct_fc->members->len; i++) { |
| 32 | struct ctf_named_field_class *named_fc = |
| 33 | ctf_field_class_struct_borrow_member_by_index(struct_fc, i); |
| 34 | |
| 35 | ret = set_alignments(named_fc->fc); |
| 36 | if (ret) { |
| 37 | goto end; |
| 38 | } |
| 39 | |
| 40 | if (named_fc->fc->alignment > fc->alignment) { |
| 41 | fc->alignment = named_fc->fc->alignment; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | break; |
| 46 | } |
| 47 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 48 | { |
| 49 | struct ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc); |
| 50 | |
| 51 | for (i = 0; i < var_fc->options->len; i++) { |
| 52 | struct ctf_named_field_class *named_fc = |
| 53 | ctf_field_class_variant_borrow_option_by_index(var_fc, i); |
| 54 | |
| 55 | ret = set_alignments(named_fc->fc); |
| 56 | if (ret) { |
| 57 | goto end; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | break; |
| 62 | } |
| 63 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
| 64 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 65 | { |
| 66 | struct ctf_field_class_array_base *array_fc = ctf_field_class_as_array_base(fc); |
| 67 | |
| 68 | ret = set_alignments(array_fc->elem_fc); |
| 69 | if (ret) { |
| 70 | goto end; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Use the alignment of the array/sequence field class's |
| 75 | * element FC as its own alignment. |
| 76 | * |
| 77 | * This is especially important when the array/sequence |
| 78 | * field's effective length is zero: as per CTF 1.8, the |
| 79 | * stream data decoding process still needs to align the |
| 80 | * cursor using the element's alignment [1]: |
| 81 | * |
| 82 | * > Arrays are always aligned on their element |
| 83 | * > alignment requirement. |
| 84 | * |
| 85 | * For example: |
| 86 | * |
| 87 | * struct { |
| 88 | * integer { size = 8; } a; |
| 89 | * integer { size = 8; align = 16; } b[0]; |
| 90 | * integer { size = 8; } c; |
| 91 | * }; |
| 92 | * |
| 93 | * When using this to decode the bytes 1, 2, and 3, then |
| 94 | * the decoded values are: |
| 95 | * |
| 96 | * `a`: 1 |
| 97 | * `b`: [] |
| 98 | * `c`: 3 |
| 99 | * |
| 100 | * [1]: https://diamon.org/ctf/#spec4.2.3 |
| 101 | */ |
| 102 | array_fc->base.alignment = array_fc->elem_fc->alignment; |
| 103 | break; |
| 104 | } |
| 105 | default: |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | end: |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | BT_HIDDEN |
| 114 | int ctf_trace_class_update_alignments(struct ctf_trace_class *ctf_tc) |
| 115 | { |
| 116 | int ret = 0; |
| 117 | uint64_t i; |
| 118 | |
| 119 | if (!ctf_tc->is_translated) { |
| 120 | ret = set_alignments(ctf_tc->packet_header_fc); |
| 121 | if (ret) { |
| 122 | goto end; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { |
| 127 | ctf_stream_class *sc = (ctf_stream_class *) ctf_tc->stream_classes->pdata[i]; |
| 128 | uint64_t j; |
| 129 | |
| 130 | if (!sc->is_translated) { |
| 131 | ret = set_alignments(sc->packet_context_fc); |
| 132 | if (ret) { |
| 133 | goto end; |
| 134 | } |
| 135 | |
| 136 | ret = set_alignments(sc->event_header_fc); |
| 137 | if (ret) { |
| 138 | goto end; |
| 139 | } |
| 140 | |
| 141 | ret = set_alignments(sc->event_common_context_fc); |
| 142 | if (ret) { |
| 143 | goto end; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | for (j = 0; j < sc->event_classes->len; j++) { |
| 148 | struct ctf_event_class *ec = (ctf_event_class *) sc->event_classes->pdata[j]; |
| 149 | |
| 150 | if (ec->is_translated) { |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | ret = set_alignments(ec->spec_context_fc); |
| 155 | if (ret) { |
| 156 | goto end; |
| 157 | } |
| 158 | |
| 159 | ret = set_alignments(ec->payload_fc); |
| 160 | if (ret) { |
| 161 | goto end; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | end: |
| 167 | return ret; |
| 168 | } |