Commit | Line | Data |
---|---|---|
44c440bc | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
44c440bc | 3 | * |
0235b0db | 4 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> |
44c440bc PP |
5 | */ |
6 | ||
4164020e | 7 | #define BT_COMP_LOG_SELF_COMP (log_cfg->self_comp) |
50f6fce8 | 8 | #define BT_COMP_LOG_SELF_COMP_CLASS (log_cfg->self_comp_class) |
4164020e SM |
9 | #define BT_LOG_OUTPUT_LEVEL (log_cfg->log_level) |
10 | #define BT_LOG_TAG "PLUGIN/CTF/META/VALIDATE" | |
d9c39b0a | 11 | #include "logging/comp-logging.h" |
44c440bc | 12 | |
3fadfbc0 | 13 | #include <babeltrace2/babeltrace.h> |
91d81473 | 14 | #include "common/macros.h" |
578e048b | 15 | #include "common/assert.h" |
44c440bc PP |
16 | #include <glib.h> |
17 | #include <stdint.h> | |
18 | #include <string.h> | |
19 | #include <inttypes.h> | |
20 | ||
087cd0f5 SM |
21 | #include "ctf-meta-visitors.hpp" |
22 | #include "logging.hpp" | |
44c440bc | 23 | |
4164020e | 24 | static int validate_stream_class(struct ctf_stream_class *sc, struct meta_log_config *log_cfg) |
44c440bc | 25 | { |
4164020e SM |
26 | int ret = 0; |
27 | struct ctf_field_class_int *int_fc; | |
28 | struct ctf_field_class *fc; | |
29 | ||
30 | if (sc->is_translated) { | |
31 | goto end; | |
32 | } | |
33 | ||
34 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
35 | ctf_field_class_as_struct(sc->packet_context_fc), "timestamp_begin"); | |
36 | if (fc) { | |
37 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
38 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
39 | "Invalid packet context field class: " | |
40 | "`timestamp_begin` member is not an integer field class."); | |
41 | goto invalid; | |
42 | } | |
43 | ||
44 | int_fc = ctf_field_class_as_int(fc); | |
45 | ||
46 | if (int_fc->is_signed) { | |
47 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet context field class: " | |
48 | "`timestamp_begin` member is signed."); | |
49 | goto invalid; | |
50 | } | |
51 | } | |
52 | ||
53 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
54 | ctf_field_class_as_struct(sc->packet_context_fc), "timestamp_end"); | |
55 | if (fc) { | |
56 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
57 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
58 | "Invalid packet context field class: " | |
59 | "`timestamp_end` member is not an integer field class."); | |
60 | goto invalid; | |
61 | } | |
62 | ||
63 | int_fc = ctf_field_class_as_int(fc); | |
64 | ||
65 | if (int_fc->is_signed) { | |
66 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet context field class: " | |
67 | "`timestamp_end` member is signed."); | |
68 | goto invalid; | |
69 | } | |
70 | } | |
71 | ||
72 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
73 | ctf_field_class_as_struct(sc->packet_context_fc), "events_discarded"); | |
74 | if (fc) { | |
75 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
76 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
77 | "Invalid packet context field class: " | |
78 | "`events_discarded` member is not an integer field class."); | |
79 | goto invalid; | |
80 | } | |
81 | ||
82 | int_fc = ctf_field_class_as_int(fc); | |
83 | ||
84 | if (int_fc->is_signed) { | |
85 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet context field class: " | |
86 | "`events_discarded` member is signed."); | |
87 | goto invalid; | |
88 | } | |
89 | } | |
90 | ||
91 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
92 | ctf_field_class_as_struct(sc->packet_context_fc), "packet_seq_num"); | |
93 | if (fc) { | |
94 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
95 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
96 | "Invalid packet context field class: " | |
97 | "`packet_seq_num` member is not an integer field class."); | |
98 | goto invalid; | |
99 | } | |
100 | ||
101 | int_fc = ctf_field_class_as_int(fc); | |
102 | ||
103 | if (int_fc->is_signed) { | |
104 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet context field class: " | |
105 | "`packet_seq_num` member is signed."); | |
106 | goto invalid; | |
107 | } | |
108 | } | |
109 | ||
110 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
111 | ctf_field_class_as_struct(sc->packet_context_fc), "packet_size"); | |
112 | if (fc) { | |
113 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
114 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
115 | "Invalid packet context field class: " | |
116 | "`packet_size` member is not an integer field class."); | |
117 | goto invalid; | |
118 | } | |
119 | ||
120 | int_fc = ctf_field_class_as_int(fc); | |
121 | ||
122 | if (int_fc->is_signed) { | |
123 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet context field class: " | |
124 | "`packet_size` member is signed."); | |
125 | goto invalid; | |
126 | } | |
127 | } | |
128 | ||
129 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
130 | ctf_field_class_as_struct(sc->packet_context_fc), "content_size"); | |
131 | if (fc) { | |
132 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
133 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
134 | "Invalid packet context field class: " | |
135 | "`content_size` member is not an integer field class."); | |
136 | goto invalid; | |
137 | } | |
138 | ||
139 | int_fc = ctf_field_class_as_int(fc); | |
140 | ||
141 | if (int_fc->is_signed) { | |
142 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet context field class: " | |
143 | "`content_size` member is signed."); | |
144 | goto invalid; | |
145 | } | |
146 | } | |
147 | ||
148 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
149 | ctf_field_class_as_struct(sc->event_header_fc), "id"); | |
150 | if (fc) { | |
151 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
152 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid event header field class: " | |
153 | "`id` member is not an integer field class."); | |
154 | goto invalid; | |
155 | } | |
156 | ||
157 | int_fc = ctf_field_class_as_int(fc); | |
158 | ||
159 | if (int_fc->is_signed) { | |
160 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid event header field class: " | |
161 | "`id` member is signed."); | |
162 | goto invalid; | |
163 | } | |
164 | } else { | |
165 | if (sc->event_classes->len > 1) { | |
166 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid event header field class: " | |
167 | "missing `id` member as there's " | |
168 | "more than one event class."); | |
169 | goto invalid; | |
170 | } | |
171 | } | |
172 | ||
173 | goto end; | |
44c440bc PP |
174 | |
175 | invalid: | |
4164020e | 176 | ret = -1; |
44c440bc PP |
177 | |
178 | end: | |
4164020e | 179 | return ret; |
44c440bc PP |
180 | } |
181 | ||
182 | BT_HIDDEN | |
4164020e | 183 | int ctf_trace_class_validate(struct ctf_trace_class *ctf_tc, struct meta_log_config *log_cfg) |
44c440bc | 184 | { |
4164020e SM |
185 | int ret = 0; |
186 | struct ctf_field_class_int *int_fc; | |
187 | uint64_t i; | |
188 | ||
189 | if (!ctf_tc->is_translated) { | |
190 | struct ctf_field_class *fc; | |
191 | ||
192 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
193 | ctf_field_class_as_struct(ctf_tc->packet_header_fc), "magic"); | |
194 | if (fc) { | |
195 | struct ctf_named_field_class *named_fc = ctf_field_class_struct_borrow_member_by_index( | |
196 | ctf_field_class_as_struct(ctf_tc->packet_header_fc), 0); | |
197 | ||
198 | if (named_fc->fc != fc) { | |
199 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet header field class: " | |
200 | "`magic` member is not the first member."); | |
201 | goto invalid; | |
202 | } | |
203 | ||
204 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
205 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
206 | "Invalid packet header field class: " | |
207 | "`magic` member is not an integer field class."); | |
208 | goto invalid; | |
209 | } | |
210 | ||
211 | int_fc = ctf_field_class_as_int(fc); | |
212 | ||
213 | if (int_fc->is_signed) { | |
214 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet header field class: " | |
215 | "`magic` member is signed."); | |
216 | goto invalid; | |
217 | } | |
218 | ||
219 | if (int_fc->base.size != 32) { | |
220 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet header field class: " | |
221 | "`magic` member is not 32-bit."); | |
222 | goto invalid; | |
223 | } | |
224 | } | |
225 | ||
226 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
227 | ctf_field_class_as_struct(ctf_tc->packet_header_fc), "stream_id"); | |
228 | if (fc) { | |
229 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
230 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
231 | "Invalid packet header field class: " | |
232 | "`stream_id` member is not an integer field class."); | |
233 | goto invalid; | |
234 | } | |
235 | ||
236 | int_fc = ctf_field_class_as_int(fc); | |
237 | ||
238 | if (int_fc->is_signed) { | |
239 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet header field class: " | |
240 | "`stream_id` member is signed."); | |
241 | goto invalid; | |
242 | } | |
243 | } else { | |
244 | if (ctf_tc->stream_classes->len > 1) { | |
245 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet header field class: " | |
246 | "missing `stream_id` member as there's " | |
247 | "more than one stream class."); | |
248 | goto invalid; | |
249 | } | |
250 | } | |
251 | ||
252 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
253 | ctf_field_class_as_struct(ctf_tc->packet_header_fc), "stream_instance_id"); | |
254 | if (fc) { | |
255 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
256 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
257 | "Invalid packet header field class: " | |
258 | "`stream_instance_id` member is not an integer field class."); | |
259 | goto invalid; | |
260 | } | |
261 | ||
262 | int_fc = ctf_field_class_as_int(fc); | |
263 | ||
264 | if (int_fc->is_signed) { | |
265 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet header field class: " | |
266 | "`stream_instance_id` member is signed."); | |
267 | goto invalid; | |
268 | } | |
269 | } | |
270 | ||
271 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
272 | ctf_field_class_as_struct(ctf_tc->packet_header_fc), "uuid"); | |
273 | if (fc) { | |
274 | if (fc->type != CTF_FIELD_CLASS_TYPE_ARRAY) { | |
275 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
276 | "Invalid packet header field class: " | |
277 | "`uuid` member is not an array field class."); | |
278 | goto invalid; | |
279 | } | |
280 | ||
281 | ctf_field_class_array *array_fc = ctf_field_class_as_array(fc); | |
282 | ||
283 | if (array_fc->length != 16) { | |
284 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
285 | "Invalid packet header field class: " | |
286 | "`uuid` member is not a 16-element array field class."); | |
287 | goto invalid; | |
288 | } | |
289 | ||
290 | if (array_fc->base.elem_fc->type != CTF_FIELD_CLASS_TYPE_INT) { | |
291 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( | |
292 | "Invalid packet header field class: " | |
293 | "`uuid` member's element field class is not " | |
294 | "an integer field class."); | |
295 | goto invalid; | |
296 | } | |
297 | ||
298 | int_fc = ctf_field_class_as_int(array_fc->base.elem_fc); | |
299 | ||
300 | if (int_fc->is_signed) { | |
301 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet header field class: " | |
302 | "`uuid` member's element field class " | |
303 | "is a signed integer field class."); | |
304 | goto invalid; | |
305 | } | |
306 | ||
307 | if (int_fc->base.size != 8) { | |
308 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet header field class: " | |
309 | "`uuid` member's element field class " | |
310 | "is not an 8-bit integer field class."); | |
311 | goto invalid; | |
312 | } | |
313 | ||
314 | if (int_fc->base.base.alignment != 8) { | |
315 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid packet header field class: " | |
316 | "`uuid` member's element field class's " | |
317 | "alignment is not 8."); | |
318 | goto invalid; | |
319 | } | |
320 | } | |
321 | } | |
322 | ||
323 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { | |
324 | struct ctf_stream_class *sc = (ctf_stream_class *) ctf_tc->stream_classes->pdata[i]; | |
325 | ||
326 | ret = validate_stream_class(sc, log_cfg); | |
327 | if (ret) { | |
328 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid stream class: sc-id=%" PRIu64, | |
329 | sc->id); | |
330 | goto invalid; | |
331 | } | |
332 | } | |
333 | ||
334 | goto end; | |
44c440bc PP |
335 | |
336 | invalid: | |
4164020e | 337 | ret = -1; |
44c440bc PP |
338 | |
339 | end: | |
4164020e | 340 | return ret; |
44c440bc | 341 | } |