Commit | Line | Data |
---|---|---|
44c440bc 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 | ||
28 | static | |
29 | int validate_stream_class(struct ctf_stream_class *sc) | |
30 | { | |
31 | int ret = 0; | |
5cd6d0e5 PP |
32 | struct ctf_field_class_int *int_fc; |
33 | struct ctf_field_class *fc; | |
44c440bc PP |
34 | bool has_total_size = false; |
35 | bool has_content_size = false; | |
36 | ||
37 | if (sc->is_translated) { | |
38 | goto end; | |
39 | } | |
40 | ||
5cd6d0e5 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."); | |
44c440bc PP |
48 | goto invalid; |
49 | } | |
50 | ||
5cd6d0e5 | 51 | int_fc = (void *) fc; |
44c440bc | 52 | |
5cd6d0e5 PP |
53 | if (int_fc->is_signed) { |
54 | BT_LOGE_STR("Invalid packet context field class: " | |
44c440bc PP |
55 | "`timestamp_begin` member is signed."); |
56 | goto invalid; | |
57 | } | |
58 | } | |
59 | ||
5cd6d0e5 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."); | |
44c440bc PP |
67 | goto invalid; |
68 | } | |
69 | ||
5cd6d0e5 | 70 | int_fc = (void *) fc; |
44c440bc | 71 | |
5cd6d0e5 PP |
72 | if (int_fc->is_signed) { |
73 | BT_LOGE_STR("Invalid packet context field class: " | |
44c440bc PP |
74 | "`timestamp_end` member is signed."); |
75 | goto invalid; | |
76 | } | |
77 | } | |
78 | ||
5cd6d0e5 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."); | |
44c440bc PP |
86 | goto invalid; |
87 | } | |
88 | ||
5cd6d0e5 | 89 | int_fc = (void *) fc; |
44c440bc | 90 | |
5cd6d0e5 PP |
91 | if (int_fc->is_signed) { |
92 | BT_LOGE_STR("Invalid packet context field class: " | |
44c440bc PP |
93 | "`events_discarded` member is signed."); |
94 | goto invalid; | |
95 | } | |
96 | } | |
97 | ||
5cd6d0e5 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."); | |
44c440bc PP |
105 | goto invalid; |
106 | } | |
107 | ||
5cd6d0e5 | 108 | int_fc = (void *) fc; |
44c440bc | 109 | |
5cd6d0e5 PP |
110 | if (int_fc->is_signed) { |
111 | BT_LOGE_STR("Invalid packet context field class: " | |
44c440bc PP |
112 | "`packet_seq_num` member is signed."); |
113 | goto invalid; | |
114 | } | |
115 | } | |
116 | ||
5cd6d0e5 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."); | |
44c440bc PP |
124 | goto invalid; |
125 | } | |
126 | ||
5cd6d0e5 | 127 | int_fc = (void *) fc; |
44c440bc | 128 | |
5cd6d0e5 PP |
129 | if (int_fc->is_signed) { |
130 | BT_LOGE_STR("Invalid packet context field class: " | |
44c440bc PP |
131 | "`packet_size` member is signed."); |
132 | goto invalid; | |
133 | } | |
134 | ||
135 | has_total_size = true; | |
136 | } | |
137 | ||
5cd6d0e5 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."); | |
44c440bc PP |
145 | goto invalid; |
146 | } | |
147 | ||
5cd6d0e5 | 148 | int_fc = (void *) fc; |
44c440bc | 149 | |
5cd6d0e5 PP |
150 | if (int_fc->is_signed) { |
151 | BT_LOGE_STR("Invalid packet context field class: " | |
44c440bc 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) { | |
5cd6d0e5 | 160 | BT_LOGE_STR("Invalid packet context field class: " |
44c440bc PP |
161 | "`content_size` member exists without " |
162 | "`packet_size` member."); | |
163 | goto invalid; | |
164 | } | |
165 | ||
5cd6d0e5 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."); | |
44c440bc PP |
173 | goto invalid; |
174 | } | |
175 | ||
5cd6d0e5 | 176 | int_fc = (void *) fc; |
44c440bc | 177 | |
5cd6d0e5 PP |
178 | if (int_fc->is_signed) { |
179 | BT_LOGE_STR("Invalid event header field class: " | |
44c440bc PP |
180 | "`id` member is signed."); |
181 | goto invalid; | |
182 | } | |
183 | } else { | |
184 | if (sc->event_classes->len > 1) { | |
5cd6d0e5 | 185 | BT_LOGE_STR("Invalid event header field class: " |
44c440bc PP |
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; | |
5cd6d0e5 | 205 | struct ctf_field_class_int *int_fc; |
44c440bc PP |
206 | uint64_t i; |
207 | ||
208 | if (!ctf_tc->is_translated) { | |
5cd6d0e5 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, | |
44c440bc PP |
217 | 0); |
218 | ||
5cd6d0e5 PP |
219 | if (named_fc->fc != fc) { |
220 | BT_LOGE_STR("Invalid packet header field class: " | |
44c440bc PP |
221 | "`magic` member is not the first member."); |
222 | goto invalid; | |
223 | } | |
224 | ||
5cd6d0e5 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."); | |
44c440bc PP |
229 | goto invalid; |
230 | } | |
231 | ||
5cd6d0e5 | 232 | int_fc = (void *) fc; |
44c440bc | 233 | |
5cd6d0e5 PP |
234 | if (int_fc->is_signed) { |
235 | BT_LOGE_STR("Invalid packet header field class: " | |
44c440bc PP |
236 | "`magic` member is signed."); |
237 | goto invalid; | |
238 | } | |
239 | ||
5cd6d0e5 PP |
240 | if (int_fc->base.size != 32) { |
241 | BT_LOGE_STR("Invalid packet header field class: " | |
44c440bc PP |
242 | "`magic` member is not 32-bit."); |
243 | goto invalid; | |
244 | } | |
245 | } | |
246 | ||
5cd6d0e5 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."); | |
44c440bc PP |
254 | goto invalid; |
255 | } | |
256 | ||
5cd6d0e5 | 257 | int_fc = (void *) fc; |
44c440bc | 258 | |
5cd6d0e5 PP |
259 | if (int_fc->is_signed) { |
260 | BT_LOGE_STR("Invalid packet header field class: " | |
44c440bc PP |
261 | "`stream_id` member is signed."); |
262 | goto invalid; | |
263 | } | |
264 | } else { | |
265 | if (ctf_tc->stream_classes->len > 1) { | |
5cd6d0e5 | 266 | BT_LOGE_STR("Invalid packet header field class: " |
44c440bc PP |
267 | "missing `stream_id` member as there's " |
268 | "more than one stream class."); | |
269 | goto invalid; | |
270 | } | |
271 | } | |
272 | ||
5cd6d0e5 PP |
273 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
274 | (void *) ctf_tc->packet_header_fc, | |
44c440bc | 275 | "stream_instance_id"); |
5cd6d0e5 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."); | |
44c440bc PP |
281 | goto invalid; |
282 | } | |
283 | ||
5cd6d0e5 | 284 | int_fc = (void *) fc; |
44c440bc | 285 | |
5cd6d0e5 PP |
286 | if (int_fc->is_signed) { |
287 | BT_LOGE_STR("Invalid packet header field class: " | |
44c440bc PP |
288 | "`stream_instance_id` member is signed."); |
289 | goto invalid; | |
290 | } | |
291 | } | |
292 | ||
5cd6d0e5 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; | |
44c440bc | 297 | |
5cd6d0e5 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."); | |
44c440bc PP |
301 | goto invalid; |
302 | } | |
303 | ||
5cd6d0e5 | 304 | array_fc = (void *) fc; |
44c440bc | 305 | |
5cd6d0e5 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."); | |
44c440bc PP |
309 | goto invalid; |
310 | } | |
311 | ||
5cd6d0e5 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."); | |
44c440bc PP |
316 | goto invalid; |
317 | } | |
318 | ||
5cd6d0e5 | 319 | int_fc = (void *) array_fc->base.elem_fc; |
44c440bc | 320 | |
5cd6d0e5 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."); | |
44c440bc PP |
325 | goto invalid; |
326 | } | |
327 | ||
5cd6d0e5 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."); | |
44c440bc PP |
332 | goto invalid; |
333 | } | |
334 | ||
5cd6d0e5 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 " | |
44c440bc 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 | ||
357 | invalid: | |
358 | ret = -1; | |
359 | ||
360 | end: | |
361 | return ret; | |
362 | } |