| 1 | /* |
| 2 | * Copyright 2018 - Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | */ |
| 14 | |
| 15 | #define BT_LOG_TAG "PLUGIN-CTF-METADATA-META-UPDATE-MEANINGS" |
| 16 | #include "logging.h" |
| 17 | |
| 18 | #include <babeltrace2/babeltrace.h> |
| 19 | #include <babeltrace2/babeltrace-internal.h> |
| 20 | #include <babeltrace2/assert-internal.h> |
| 21 | #include <glib.h> |
| 22 | #include <stdint.h> |
| 23 | #include <string.h> |
| 24 | #include <inttypes.h> |
| 25 | |
| 26 | #include "ctf-meta-visitors.h" |
| 27 | |
| 28 | static |
| 29 | int set_int_field_class_meaning_by_name(struct ctf_field_class *fc, |
| 30 | const char *field_name, const char *id_name, |
| 31 | enum ctf_field_class_meaning meaning) |
| 32 | { |
| 33 | int ret = 0; |
| 34 | uint64_t i; |
| 35 | |
| 36 | if (!fc) { |
| 37 | goto end; |
| 38 | } |
| 39 | |
| 40 | switch (fc->type) { |
| 41 | case CTF_FIELD_CLASS_TYPE_INT: |
| 42 | case CTF_FIELD_CLASS_TYPE_ENUM: |
| 43 | { |
| 44 | struct ctf_field_class_int *int_fc = (void *) fc; |
| 45 | |
| 46 | if (field_name && strcmp(field_name, id_name) == 0) { |
| 47 | int_fc->meaning = meaning; |
| 48 | } |
| 49 | |
| 50 | break; |
| 51 | } |
| 52 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
| 53 | { |
| 54 | struct ctf_field_class_struct *struct_fc = (void *) fc; |
| 55 | |
| 56 | for (i = 0; i < struct_fc->members->len; i++) { |
| 57 | struct ctf_named_field_class *named_fc = |
| 58 | ctf_field_class_struct_borrow_member_by_index( |
| 59 | struct_fc, i); |
| 60 | |
| 61 | ret = set_int_field_class_meaning_by_name(named_fc->fc, |
| 62 | named_fc->name->str, id_name, meaning); |
| 63 | if (ret) { |
| 64 | goto end; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | break; |
| 69 | } |
| 70 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 71 | { |
| 72 | struct ctf_field_class_variant *var_fc = (void *) fc; |
| 73 | |
| 74 | for (i = 0; i < var_fc->options->len; i++) { |
| 75 | struct ctf_named_field_class *named_fc = |
| 76 | ctf_field_class_variant_borrow_option_by_index( |
| 77 | var_fc, i); |
| 78 | |
| 79 | ret = set_int_field_class_meaning_by_name(named_fc->fc, |
| 80 | NULL, id_name, meaning); |
| 81 | if (ret) { |
| 82 | goto end; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | break; |
| 87 | } |
| 88 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
| 89 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 90 | { |
| 91 | struct ctf_field_class_array_base *array_fc = (void *) fc; |
| 92 | |
| 93 | ret = set_int_field_class_meaning_by_name(array_fc->elem_fc, |
| 94 | NULL, id_name, meaning); |
| 95 | if (ret) { |
| 96 | goto end; |
| 97 | } |
| 98 | |
| 99 | break; |
| 100 | } |
| 101 | default: |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | end: |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static |
| 110 | int update_stream_class_meanings(struct ctf_stream_class *sc) |
| 111 | { |
| 112 | int ret = 0; |
| 113 | struct ctf_field_class_int *int_fc; |
| 114 | uint64_t i; |
| 115 | |
| 116 | if (!sc->is_translated) { |
| 117 | if (sc->packet_context_fc) { |
| 118 | int_fc = ctf_field_class_struct_borrow_member_int_field_class_by_name( |
| 119 | (void *) sc->packet_context_fc, "timestamp_begin"); |
| 120 | if (int_fc) { |
| 121 | int_fc->meaning = CTF_FIELD_CLASS_MEANING_PACKET_BEGINNING_TIME; |
| 122 | } |
| 123 | |
| 124 | int_fc = ctf_field_class_struct_borrow_member_int_field_class_by_name( |
| 125 | (void *) sc->packet_context_fc, "timestamp_end"); |
| 126 | if (int_fc) { |
| 127 | int_fc->meaning = CTF_FIELD_CLASS_MEANING_PACKET_END_TIME; |
| 128 | |
| 129 | /* |
| 130 | * Remove mapped clock class to avoid updating |
| 131 | * the clock immediately when decoding. |
| 132 | */ |
| 133 | int_fc->mapped_clock_class = NULL; |
| 134 | } |
| 135 | |
| 136 | int_fc = ctf_field_class_struct_borrow_member_int_field_class_by_name( |
| 137 | (void *) sc->packet_context_fc, "events_discarded"); |
| 138 | if (int_fc) { |
| 139 | int_fc->meaning = CTF_FIELD_CLASS_MEANING_DISC_EV_REC_COUNTER_SNAPSHOT; |
| 140 | } |
| 141 | |
| 142 | int_fc = ctf_field_class_struct_borrow_member_int_field_class_by_name( |
| 143 | (void *) sc->packet_context_fc, "packet_seq_num"); |
| 144 | if (int_fc) { |
| 145 | int_fc->meaning = CTF_FIELD_CLASS_MEANING_PACKET_COUNTER_SNAPSHOT; |
| 146 | |
| 147 | } |
| 148 | |
| 149 | int_fc = ctf_field_class_struct_borrow_member_int_field_class_by_name( |
| 150 | (void *) sc->packet_context_fc, "packet_size"); |
| 151 | if (int_fc) { |
| 152 | int_fc->meaning = CTF_FIELD_CLASS_MEANING_EXP_PACKET_TOTAL_SIZE; |
| 153 | } |
| 154 | |
| 155 | int_fc = ctf_field_class_struct_borrow_member_int_field_class_by_name( |
| 156 | (void *) sc->packet_context_fc, "content_size"); |
| 157 | if (int_fc) { |
| 158 | int_fc->meaning = CTF_FIELD_CLASS_MEANING_EXP_PACKET_CONTENT_SIZE; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (sc->event_header_fc) { |
| 163 | ret = set_int_field_class_meaning_by_name( |
| 164 | sc->event_header_fc, NULL, "id", |
| 165 | CTF_FIELD_CLASS_MEANING_EVENT_CLASS_ID); |
| 166 | if (ret) { |
| 167 | goto end; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | for (i = 0; i < sc->event_classes->len; i++) { |
| 173 | struct ctf_event_class *ec = sc->event_classes->pdata[i]; |
| 174 | |
| 175 | if (ec->is_translated) { |
| 176 | continue; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | end: |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | BT_HIDDEN |
| 185 | int ctf_trace_class_update_meanings(struct ctf_trace_class *ctf_tc) |
| 186 | { |
| 187 | int ret = 0; |
| 188 | struct ctf_field_class_int *int_fc; |
| 189 | struct ctf_named_field_class *named_fc; |
| 190 | uint64_t i; |
| 191 | |
| 192 | if (!ctf_tc->is_translated && ctf_tc->packet_header_fc) { |
| 193 | int_fc = ctf_field_class_struct_borrow_member_int_field_class_by_name( |
| 194 | (void *) ctf_tc->packet_header_fc, "magic"); |
| 195 | if (int_fc) { |
| 196 | int_fc->meaning = CTF_FIELD_CLASS_MEANING_MAGIC; |
| 197 | } |
| 198 | |
| 199 | int_fc = ctf_field_class_struct_borrow_member_int_field_class_by_name( |
| 200 | (void *) ctf_tc->packet_header_fc, "stream_id"); |
| 201 | if (int_fc) { |
| 202 | int_fc->meaning = CTF_FIELD_CLASS_MEANING_STREAM_CLASS_ID; |
| 203 | } |
| 204 | |
| 205 | int_fc = ctf_field_class_struct_borrow_member_int_field_class_by_name( |
| 206 | (void *) ctf_tc->packet_header_fc, |
| 207 | "stream_instance_id"); |
| 208 | if (int_fc) { |
| 209 | int_fc->meaning = CTF_FIELD_CLASS_MEANING_DATA_STREAM_ID; |
| 210 | } |
| 211 | |
| 212 | named_fc = ctf_field_class_struct_borrow_member_by_name( |
| 213 | (void *) ctf_tc->packet_header_fc, "uuid"); |
| 214 | if (named_fc && named_fc->fc->type == CTF_FIELD_CLASS_TYPE_ARRAY) { |
| 215 | struct ctf_field_class_array *array_fc = |
| 216 | (void *) named_fc->fc; |
| 217 | |
| 218 | array_fc->meaning = CTF_FIELD_CLASS_MEANING_UUID; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { |
| 223 | ret = update_stream_class_meanings( |
| 224 | ctf_tc->stream_classes->pdata[i]); |
| 225 | if (ret) { |
| 226 | goto end; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | end: |
| 231 | return ret; |
| 232 | } |