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