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