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