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