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 | ||
0746848c | 15 | #define BT_LOG_OUTPUT_LEVEL log_level |
350ad6c1 | 16 | #define BT_LOG_TAG "PLUGIN/CTF/META/VALIDATE" |
0746848c | 17 | #include "logging/log.h" |
44c440bc | 18 | |
3fadfbc0 | 19 | #include <babeltrace2/babeltrace.h> |
91d81473 | 20 | #include "common/macros.h" |
578e048b | 21 | #include "common/assert.h" |
44c440bc PP |
22 | #include <glib.h> |
23 | #include <stdint.h> | |
24 | #include <string.h> | |
25 | #include <inttypes.h> | |
26 | ||
27 | #include "ctf-meta-visitors.h" | |
28 | ||
29 | static | |
0746848c PP |
30 | int validate_stream_class(struct ctf_stream_class *sc, |
31 | bt_logging_level log_level) | |
44c440bc PP |
32 | { |
33 | int ret = 0; | |
5cd6d0e5 PP |
34 | struct ctf_field_class_int *int_fc; |
35 | struct ctf_field_class *fc; | |
44c440bc PP |
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) { | |
864cad70 PP |
44 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
45 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
5cd6d0e5 PP |
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) { | |
864cad70 PP |
63 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
64 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
5cd6d0e5 PP |
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) { | |
864cad70 PP |
82 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
83 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
5cd6d0e5 PP |
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) { | |
864cad70 PP |
101 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
102 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
5cd6d0e5 PP |
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) { | |
864cad70 PP |
120 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
121 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
5cd6d0e5 PP |
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 | } | |
44c440bc PP |
134 | } |
135 | ||
5cd6d0e5 PP |
136 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
137 | (void *) sc->packet_context_fc, "content_size"); | |
138 | if (fc) { | |
864cad70 PP |
139 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
140 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
5cd6d0e5 PP |
141 | BT_LOGE_STR("Invalid packet context field class: " |
142 | "`content_size` member is not an integer field class."); | |
44c440bc PP |
143 | goto invalid; |
144 | } | |
145 | ||
5cd6d0e5 | 146 | int_fc = (void *) fc; |
44c440bc | 147 | |
5cd6d0e5 PP |
148 | if (int_fc->is_signed) { |
149 | BT_LOGE_STR("Invalid packet context field class: " | |
44c440bc PP |
150 | "`content_size` member is signed."); |
151 | goto invalid; | |
152 | } | |
44c440bc PP |
153 | } |
154 | ||
5cd6d0e5 PP |
155 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
156 | (void *) sc->event_header_fc, "id"); | |
157 | if (fc) { | |
864cad70 PP |
158 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
159 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
5cd6d0e5 PP |
160 | BT_LOGE_STR("Invalid event header field class: " |
161 | "`id` member is not an integer field class."); | |
44c440bc PP |
162 | goto invalid; |
163 | } | |
164 | ||
5cd6d0e5 | 165 | int_fc = (void *) fc; |
44c440bc | 166 | |
5cd6d0e5 PP |
167 | if (int_fc->is_signed) { |
168 | BT_LOGE_STR("Invalid event header field class: " | |
44c440bc PP |
169 | "`id` member is signed."); |
170 | goto invalid; | |
171 | } | |
172 | } else { | |
173 | if (sc->event_classes->len > 1) { | |
5cd6d0e5 | 174 | BT_LOGE_STR("Invalid event header field class: " |
44c440bc PP |
175 | "missing `id` member as there's " |
176 | "more than one event class."); | |
177 | goto invalid; | |
178 | } | |
179 | } | |
180 | ||
181 | goto end; | |
182 | ||
183 | invalid: | |
184 | ret = -1; | |
185 | ||
186 | end: | |
187 | return ret; | |
188 | } | |
189 | ||
190 | BT_HIDDEN | |
0746848c PP |
191 | int ctf_trace_class_validate(struct ctf_trace_class *ctf_tc, |
192 | bt_logging_level log_level) | |
44c440bc PP |
193 | { |
194 | int ret = 0; | |
5cd6d0e5 | 195 | struct ctf_field_class_int *int_fc; |
44c440bc PP |
196 | uint64_t i; |
197 | ||
198 | if (!ctf_tc->is_translated) { | |
5cd6d0e5 PP |
199 | struct ctf_field_class *fc; |
200 | ||
201 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( | |
202 | (void *) ctf_tc->packet_header_fc, "magic"); | |
203 | if (fc) { | |
204 | struct ctf_named_field_class *named_fc = | |
205 | ctf_field_class_struct_borrow_member_by_index( | |
206 | (void *) ctf_tc->packet_header_fc, | |
44c440bc PP |
207 | 0); |
208 | ||
5cd6d0e5 PP |
209 | if (named_fc->fc != fc) { |
210 | BT_LOGE_STR("Invalid packet header field class: " | |
44c440bc PP |
211 | "`magic` member is not the first member."); |
212 | goto invalid; | |
213 | } | |
214 | ||
864cad70 PP |
215 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
216 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
5cd6d0e5 PP |
217 | BT_LOGE_STR("Invalid packet header field class: " |
218 | "`magic` member is not an integer field class."); | |
44c440bc PP |
219 | goto invalid; |
220 | } | |
221 | ||
5cd6d0e5 | 222 | int_fc = (void *) fc; |
44c440bc | 223 | |
5cd6d0e5 PP |
224 | if (int_fc->is_signed) { |
225 | BT_LOGE_STR("Invalid packet header field class: " | |
44c440bc PP |
226 | "`magic` member is signed."); |
227 | goto invalid; | |
228 | } | |
229 | ||
5cd6d0e5 PP |
230 | if (int_fc->base.size != 32) { |
231 | BT_LOGE_STR("Invalid packet header field class: " | |
44c440bc PP |
232 | "`magic` member is not 32-bit."); |
233 | goto invalid; | |
234 | } | |
235 | } | |
236 | ||
5cd6d0e5 PP |
237 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
238 | (void *) ctf_tc->packet_header_fc, "stream_id"); | |
239 | if (fc) { | |
864cad70 PP |
240 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
241 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
5cd6d0e5 PP |
242 | BT_LOGE_STR("Invalid packet header field class: " |
243 | "`stream_id` member is not an integer field class."); | |
44c440bc PP |
244 | goto invalid; |
245 | } | |
246 | ||
5cd6d0e5 | 247 | int_fc = (void *) fc; |
44c440bc | 248 | |
5cd6d0e5 PP |
249 | if (int_fc->is_signed) { |
250 | BT_LOGE_STR("Invalid packet header field class: " | |
44c440bc PP |
251 | "`stream_id` member is signed."); |
252 | goto invalid; | |
253 | } | |
254 | } else { | |
255 | if (ctf_tc->stream_classes->len > 1) { | |
5cd6d0e5 | 256 | BT_LOGE_STR("Invalid packet header field class: " |
44c440bc PP |
257 | "missing `stream_id` member as there's " |
258 | "more than one stream class."); | |
259 | goto invalid; | |
260 | } | |
261 | } | |
262 | ||
5cd6d0e5 PP |
263 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
264 | (void *) ctf_tc->packet_header_fc, | |
44c440bc | 265 | "stream_instance_id"); |
5cd6d0e5 | 266 | if (fc) { |
864cad70 PP |
267 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
268 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { | |
5cd6d0e5 PP |
269 | BT_LOGE_STR("Invalid packet header field class: " |
270 | "`stream_instance_id` member is not an integer field class."); | |
44c440bc PP |
271 | goto invalid; |
272 | } | |
273 | ||
5cd6d0e5 | 274 | int_fc = (void *) fc; |
44c440bc | 275 | |
5cd6d0e5 PP |
276 | if (int_fc->is_signed) { |
277 | BT_LOGE_STR("Invalid packet header field class: " | |
44c440bc PP |
278 | "`stream_instance_id` member is signed."); |
279 | goto invalid; | |
280 | } | |
281 | } | |
282 | ||
5cd6d0e5 PP |
283 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
284 | (void *) ctf_tc->packet_header_fc, "uuid"); | |
285 | if (fc) { | |
286 | struct ctf_field_class_array *array_fc = (void *) fc; | |
44c440bc | 287 | |
864cad70 | 288 | if (fc->type != CTF_FIELD_CLASS_TYPE_ARRAY) { |
5cd6d0e5 PP |
289 | BT_LOGE_STR("Invalid packet header field class: " |
290 | "`uuid` member is not an array field class."); | |
44c440bc PP |
291 | goto invalid; |
292 | } | |
293 | ||
5cd6d0e5 | 294 | array_fc = (void *) fc; |
44c440bc | 295 | |
5cd6d0e5 PP |
296 | if (array_fc->length != 16) { |
297 | BT_LOGE_STR("Invalid packet header field class: " | |
298 | "`uuid` member is not a 16-element array field class."); | |
44c440bc PP |
299 | goto invalid; |
300 | } | |
301 | ||
864cad70 | 302 | if (array_fc->base.elem_fc->type != CTF_FIELD_CLASS_TYPE_INT) { |
5cd6d0e5 PP |
303 | BT_LOGE_STR("Invalid packet header field class: " |
304 | "`uuid` member's element field class is not " | |
305 | "an integer field class."); | |
44c440bc PP |
306 | goto invalid; |
307 | } | |
308 | ||
5cd6d0e5 | 309 | int_fc = (void *) array_fc->base.elem_fc; |
44c440bc | 310 | |
5cd6d0e5 PP |
311 | if (int_fc->is_signed) { |
312 | BT_LOGE_STR("Invalid packet header field class: " | |
313 | "`uuid` member's element field class " | |
314 | "is a signed integer field class."); | |
44c440bc PP |
315 | goto invalid; |
316 | } | |
317 | ||
5cd6d0e5 PP |
318 | if (int_fc->base.size != 8) { |
319 | BT_LOGE_STR("Invalid packet header field class: " | |
320 | "`uuid` member's element field class " | |
321 | "is not an 8-bit integer field class."); | |
44c440bc PP |
322 | goto invalid; |
323 | } | |
324 | ||
5cd6d0e5 PP |
325 | if (int_fc->base.base.alignment != 8) { |
326 | BT_LOGE_STR("Invalid packet header field class: " | |
327 | "`uuid` member's element field class's " | |
44c440bc PP |
328 | "alignment is not 8."); |
329 | goto invalid; | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
334 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { | |
335 | struct ctf_stream_class *sc = | |
336 | ctf_tc->stream_classes->pdata[i]; | |
337 | ||
0746848c | 338 | ret = validate_stream_class(sc, log_level); |
44c440bc PP |
339 | if (ret) { |
340 | BT_LOGE("Invalid stream class: sc-id=%" PRIu64, sc->id); | |
341 | goto invalid; | |
342 | } | |
343 | } | |
344 | ||
345 | goto end; | |
346 | ||
347 | invalid: | |
348 | ret = -1; | |
349 | ||
350 | end: | |
351 | return ret; | |
352 | } |