Commit | Line | Data |
---|---|---|
44c440bc PP |
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 | ||
0746848c | 15 | #define BT_LOG_OUTPUT_LEVEL log_level |
350ad6c1 | 16 | #define BT_LOG_TAG "PLUGIN/CTF/META/UPDATE-DEF-CC" |
0746848c | 17 | #include "logging/log.h" |
44c440bc | 18 | |
3fadfbc0 | 19 | #include <babeltrace2/babeltrace.h> |
91d81473 | 20 | #include "common/macros.h" |
578e048b | 21 | #include "common/assert.h" |
44c440bc PP |
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 | |
5cd6d0e5 | 30 | int find_mapped_clock_class(struct ctf_field_class *fc, |
0746848c PP |
31 | struct ctf_clock_class **clock_class, |
32 | bt_logging_level log_level) | |
44c440bc PP |
33 | { |
34 | int ret = 0; | |
35 | uint64_t i; | |
36 | ||
5cd6d0e5 | 37 | if (!fc) { |
44c440bc PP |
38 | goto end; |
39 | } | |
40 | ||
864cad70 PP |
41 | switch (fc->type) { |
42 | case CTF_FIELD_CLASS_TYPE_INT: | |
43 | case CTF_FIELD_CLASS_TYPE_ENUM: | |
44c440bc | 44 | { |
5cd6d0e5 | 45 | struct ctf_field_class_int *int_fc = (void *) fc; |
44c440bc | 46 | |
5cd6d0e5 | 47 | if (int_fc->mapped_clock_class) { |
44c440bc | 48 | if (*clock_class && *clock_class != |
5cd6d0e5 | 49 | int_fc->mapped_clock_class) { |
44c440bc PP |
50 | BT_LOGE("Stream class contains more than one " |
51 | "clock class: expected-cc-name=\"%s\", " | |
52 | "other-cc-name=\"%s\"", | |
0f2d58c9 PP |
53 | (*clock_class)->name->str, |
54 | int_fc->mapped_clock_class->name->str); | |
44c440bc PP |
55 | ret = -1; |
56 | goto end; | |
57 | } | |
58 | ||
5cd6d0e5 | 59 | *clock_class = int_fc->mapped_clock_class; |
44c440bc PP |
60 | } |
61 | ||
62 | break; | |
63 | } | |
864cad70 | 64 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
44c440bc | 65 | { |
5cd6d0e5 | 66 | struct ctf_field_class_struct *struct_fc = (void *) fc; |
44c440bc | 67 | |
5cd6d0e5 PP |
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); | |
44c440bc | 72 | |
5cd6d0e5 | 73 | ret = find_mapped_clock_class(named_fc->fc, |
0746848c | 74 | clock_class, log_level); |
44c440bc PP |
75 | if (ret) { |
76 | goto end; | |
77 | } | |
78 | } | |
79 | ||
80 | break; | |
81 | } | |
864cad70 | 82 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
44c440bc | 83 | { |
5cd6d0e5 | 84 | struct ctf_field_class_variant *var_fc = (void *) fc; |
44c440bc | 85 | |
5cd6d0e5 PP |
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); | |
44c440bc | 90 | |
5cd6d0e5 | 91 | ret = find_mapped_clock_class(named_fc->fc, |
0746848c | 92 | clock_class, log_level); |
44c440bc PP |
93 | if (ret) { |
94 | goto end; | |
95 | } | |
96 | } | |
97 | ||
98 | break; | |
99 | } | |
864cad70 PP |
100 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
101 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
44c440bc | 102 | { |
5cd6d0e5 | 103 | struct ctf_field_class_array_base *array_fc = (void *) fc; |
44c440bc | 104 | |
0746848c PP |
105 | ret = find_mapped_clock_class(array_fc->elem_fc, clock_class, |
106 | log_level); | |
44c440bc PP |
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( | |
0746848c PP |
123 | struct ctf_stream_class *stream_class, |
124 | bt_logging_level log_level) | |
44c440bc PP |
125 | { |
126 | int ret = 0; | |
0f2d58c9 | 127 | struct ctf_clock_class *clock_class = |
e5be10ef | 128 | stream_class->default_clock_class; |
44c440bc PP |
129 | uint64_t i; |
130 | ||
5cd6d0e5 | 131 | ret = find_mapped_clock_class(stream_class->packet_context_fc, |
0746848c | 132 | &clock_class, log_level); |
44c440bc PP |
133 | if (ret) { |
134 | goto end; | |
135 | } | |
136 | ||
5cd6d0e5 | 137 | ret = find_mapped_clock_class(stream_class->event_header_fc, |
0746848c | 138 | &clock_class, log_level); |
44c440bc PP |
139 | if (ret) { |
140 | goto end; | |
141 | } | |
142 | ||
5cd6d0e5 | 143 | ret = find_mapped_clock_class(stream_class->event_common_context_fc, |
0746848c | 144 | &clock_class, log_level); |
44c440bc PP |
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 | ||
5cd6d0e5 | 153 | ret = find_mapped_clock_class(event_class->spec_context_fc, |
0746848c | 154 | &clock_class, log_level); |
44c440bc PP |
155 | if (ret) { |
156 | goto end; | |
157 | } | |
158 | ||
5cd6d0e5 | 159 | ret = find_mapped_clock_class(event_class->payload_fc, |
0746848c | 160 | &clock_class, log_level); |
44c440bc PP |
161 | if (ret) { |
162 | goto end; | |
163 | } | |
164 | } | |
165 | ||
166 | if (!stream_class->default_clock_class) { | |
398454ed | 167 | stream_class->default_clock_class = clock_class; |
44c440bc PP |
168 | } |
169 | ||
170 | end: | |
171 | return ret; | |
172 | } | |
173 | ||
174 | BT_HIDDEN | |
0746848c PP |
175 | int ctf_trace_class_update_default_clock_classes(struct ctf_trace_class *ctf_tc, |
176 | bt_logging_level log_level) | |
44c440bc PP |
177 | { |
178 | uint64_t i; | |
179 | int ret = 0; | |
0f2d58c9 | 180 | struct ctf_clock_class *clock_class = NULL; |
44c440bc | 181 | |
0746848c PP |
182 | ret = find_mapped_clock_class(ctf_tc->packet_header_fc, &clock_class, |
183 | log_level); | |
44c440bc PP |
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( | |
0746848c | 198 | ctf_tc->stream_classes->pdata[i], log_level); |
44c440bc PP |
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 | } |