| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #include <glib.h> |
| 8 | #include <inttypes.h> |
| 9 | #include <stdint.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include <babeltrace2/babeltrace.h> |
| 13 | |
| 14 | #include "common/assert.h" |
| 15 | #include "common/macros.h" |
| 16 | |
| 17 | #include "ctf-meta-visitors.hpp" |
| 18 | |
| 19 | static inline int set_text_array_sequence_field_class(struct ctf_field_class *fc) |
| 20 | { |
| 21 | int ret = 0; |
| 22 | uint64_t i; |
| 23 | |
| 24 | if (!fc) { |
| 25 | goto end; |
| 26 | } |
| 27 | |
| 28 | switch (fc->type) { |
| 29 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
| 30 | { |
| 31 | struct ctf_field_class_struct *struct_fc = ctf_field_class_as_struct(fc); |
| 32 | |
| 33 | for (i = 0; i < struct_fc->members->len; i++) { |
| 34 | struct ctf_named_field_class *named_fc = |
| 35 | ctf_field_class_struct_borrow_member_by_index(struct_fc, i); |
| 36 | |
| 37 | ret = set_text_array_sequence_field_class(named_fc->fc); |
| 38 | if (ret) { |
| 39 | goto end; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | break; |
| 44 | } |
| 45 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 46 | { |
| 47 | struct ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc); |
| 48 | |
| 49 | for (i = 0; i < var_fc->options->len; i++) { |
| 50 | struct ctf_named_field_class *named_fc = |
| 51 | ctf_field_class_variant_borrow_option_by_index(var_fc, i); |
| 52 | |
| 53 | ret = set_text_array_sequence_field_class(named_fc->fc); |
| 54 | if (ret) { |
| 55 | goto end; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | break; |
| 60 | } |
| 61 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
| 62 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 63 | { |
| 64 | struct ctf_field_class_array_base *array_fc = ctf_field_class_as_array_base(fc); |
| 65 | |
| 66 | if (array_fc->elem_fc->type == CTF_FIELD_CLASS_TYPE_INT || |
| 67 | array_fc->elem_fc->type == CTF_FIELD_CLASS_TYPE_ENUM) { |
| 68 | struct ctf_field_class_int *int_fc = ctf_field_class_as_int(array_fc->elem_fc); |
| 69 | |
| 70 | if (int_fc->base.base.alignment == 8 && int_fc->base.size == 8 && |
| 71 | int_fc->encoding == CTF_ENCODING_UTF8) { |
| 72 | array_fc->is_text = true; |
| 73 | |
| 74 | /* |
| 75 | * Force integer element to be unsigned; |
| 76 | * this makes the decoder enter a single |
| 77 | * path when reading a text |
| 78 | * array/sequence and we can safely |
| 79 | * decode bytes as characters anyway. |
| 80 | */ |
| 81 | int_fc->is_signed = false; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | ret = set_text_array_sequence_field_class(array_fc->elem_fc); |
| 86 | if (ret) { |
| 87 | goto end; |
| 88 | } |
| 89 | |
| 90 | break; |
| 91 | } |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | end: |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | int ctf_trace_class_update_text_array_sequence(struct ctf_trace_class *ctf_tc) |
| 101 | { |
| 102 | int ret = 0; |
| 103 | uint64_t i; |
| 104 | |
| 105 | if (!ctf_tc->is_translated) { |
| 106 | ret = set_text_array_sequence_field_class(ctf_tc->packet_header_fc); |
| 107 | if (ret) { |
| 108 | goto end; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { |
| 113 | ctf_stream_class *sc = (ctf_stream_class *) ctf_tc->stream_classes->pdata[i]; |
| 114 | uint64_t j; |
| 115 | |
| 116 | if (!sc->is_translated) { |
| 117 | ret = set_text_array_sequence_field_class(sc->packet_context_fc); |
| 118 | if (ret) { |
| 119 | goto end; |
| 120 | } |
| 121 | |
| 122 | ret = set_text_array_sequence_field_class(sc->event_header_fc); |
| 123 | if (ret) { |
| 124 | goto end; |
| 125 | } |
| 126 | |
| 127 | ret = set_text_array_sequence_field_class(sc->event_common_context_fc); |
| 128 | if (ret) { |
| 129 | goto end; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | for (j = 0; j < sc->event_classes->len; j++) { |
| 134 | struct ctf_event_class *ec = (ctf_event_class *) sc->event_classes->pdata[j]; |
| 135 | |
| 136 | if (ec->is_translated) { |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | ret = set_text_array_sequence_field_class(ec->spec_context_fc); |
| 141 | if (ret) { |
| 142 | goto end; |
| 143 | } |
| 144 | |
| 145 | ret = set_text_array_sequence_field_class(ec->payload_fc); |
| 146 | if (ret) { |
| 147 | goto end; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | end: |
| 153 | return ret; |
| 154 | } |