| 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_OUTPUT_LEVEL log_level |
| 16 | #define BT_LOG_TAG "PLUGIN/CTF/META/UPDATE-DEF-CC" |
| 17 | #include "logging/log.h" |
| 18 | |
| 19 | #include <babeltrace2/babeltrace.h> |
| 20 | #include "common/macros.h" |
| 21 | #include "common/assert.h" |
| 22 | #include <glib.h> |
| 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | #include <inttypes.h> |
| 26 | |
| 27 | #include "ctf-meta-visitors.h" |
| 28 | |
| 29 | static inline |
| 30 | int find_mapped_clock_class(struct ctf_field_class *fc, |
| 31 | struct ctf_clock_class **clock_class, |
| 32 | bt_logging_level log_level) |
| 33 | { |
| 34 | int ret = 0; |
| 35 | uint64_t i; |
| 36 | |
| 37 | if (!fc) { |
| 38 | goto end; |
| 39 | } |
| 40 | |
| 41 | switch (fc->type) { |
| 42 | case CTF_FIELD_CLASS_TYPE_INT: |
| 43 | case CTF_FIELD_CLASS_TYPE_ENUM: |
| 44 | { |
| 45 | struct ctf_field_class_int *int_fc = (void *) fc; |
| 46 | |
| 47 | if (int_fc->mapped_clock_class) { |
| 48 | if (*clock_class && *clock_class != |
| 49 | int_fc->mapped_clock_class) { |
| 50 | BT_LOGE("Stream class contains more than one " |
| 51 | "clock class: expected-cc-name=\"%s\", " |
| 52 | "other-cc-name=\"%s\"", |
| 53 | (*clock_class)->name->str, |
| 54 | int_fc->mapped_clock_class->name->str); |
| 55 | ret = -1; |
| 56 | goto end; |
| 57 | } |
| 58 | |
| 59 | *clock_class = int_fc->mapped_clock_class; |
| 60 | } |
| 61 | |
| 62 | break; |
| 63 | } |
| 64 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
| 65 | { |
| 66 | struct ctf_field_class_struct *struct_fc = (void *) fc; |
| 67 | |
| 68 | for (i = 0; i < struct_fc->members->len; i++) { |
| 69 | struct ctf_named_field_class *named_fc = |
| 70 | ctf_field_class_struct_borrow_member_by_index( |
| 71 | struct_fc, i); |
| 72 | |
| 73 | ret = find_mapped_clock_class(named_fc->fc, |
| 74 | clock_class, log_level); |
| 75 | if (ret) { |
| 76 | goto end; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | break; |
| 81 | } |
| 82 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 83 | { |
| 84 | struct ctf_field_class_variant *var_fc = (void *) fc; |
| 85 | |
| 86 | for (i = 0; i < var_fc->options->len; i++) { |
| 87 | struct ctf_named_field_class *named_fc = |
| 88 | ctf_field_class_variant_borrow_option_by_index( |
| 89 | var_fc, i); |
| 90 | |
| 91 | ret = find_mapped_clock_class(named_fc->fc, |
| 92 | clock_class, log_level); |
| 93 | if (ret) { |
| 94 | goto end; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | break; |
| 99 | } |
| 100 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
| 101 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 102 | { |
| 103 | struct ctf_field_class_array_base *array_fc = (void *) fc; |
| 104 | |
| 105 | ret = find_mapped_clock_class(array_fc->elem_fc, clock_class, |
| 106 | log_level); |
| 107 | if (ret) { |
| 108 | goto end; |
| 109 | } |
| 110 | |
| 111 | break; |
| 112 | } |
| 113 | default: |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | end: |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | static inline |
| 122 | int update_stream_class_default_clock_class( |
| 123 | struct ctf_stream_class *stream_class, |
| 124 | bt_logging_level log_level) |
| 125 | { |
| 126 | int ret = 0; |
| 127 | struct ctf_clock_class *clock_class = |
| 128 | stream_class->default_clock_class; |
| 129 | uint64_t i; |
| 130 | |
| 131 | ret = find_mapped_clock_class(stream_class->packet_context_fc, |
| 132 | &clock_class, log_level); |
| 133 | if (ret) { |
| 134 | goto end; |
| 135 | } |
| 136 | |
| 137 | ret = find_mapped_clock_class(stream_class->event_header_fc, |
| 138 | &clock_class, log_level); |
| 139 | if (ret) { |
| 140 | goto end; |
| 141 | } |
| 142 | |
| 143 | ret = find_mapped_clock_class(stream_class->event_common_context_fc, |
| 144 | &clock_class, log_level); |
| 145 | if (ret) { |
| 146 | goto end; |
| 147 | } |
| 148 | |
| 149 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 150 | struct ctf_event_class *event_class = |
| 151 | stream_class->event_classes->pdata[i]; |
| 152 | |
| 153 | ret = find_mapped_clock_class(event_class->spec_context_fc, |
| 154 | &clock_class, log_level); |
| 155 | if (ret) { |
| 156 | goto end; |
| 157 | } |
| 158 | |
| 159 | ret = find_mapped_clock_class(event_class->payload_fc, |
| 160 | &clock_class, log_level); |
| 161 | if (ret) { |
| 162 | goto end; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (!stream_class->default_clock_class) { |
| 167 | stream_class->default_clock_class = clock_class; |
| 168 | } |
| 169 | |
| 170 | end: |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | BT_HIDDEN |
| 175 | int ctf_trace_class_update_default_clock_classes(struct ctf_trace_class *ctf_tc, |
| 176 | bt_logging_level log_level) |
| 177 | { |
| 178 | uint64_t i; |
| 179 | int ret = 0; |
| 180 | struct ctf_clock_class *clock_class = NULL; |
| 181 | |
| 182 | ret = find_mapped_clock_class(ctf_tc->packet_header_fc, &clock_class, |
| 183 | log_level); |
| 184 | if (ret) { |
| 185 | goto end; |
| 186 | } |
| 187 | |
| 188 | if (clock_class) { |
| 189 | ret = -1; |
| 190 | goto end; |
| 191 | } |
| 192 | |
| 193 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { |
| 194 | struct ctf_stream_class *sc = |
| 195 | ctf_tc->stream_classes->pdata[i]; |
| 196 | |
| 197 | ret = update_stream_class_default_clock_class( |
| 198 | ctf_tc->stream_classes->pdata[i], log_level); |
| 199 | if (ret) { |
| 200 | BT_LOGE("Stream class contains more than one " |
| 201 | "clock class: stream-class-id=%" PRIu64, |
| 202 | sc->id); |
| 203 | goto end; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | end: |
| 208 | return ret; |
| 209 | } |