Fix: sessiond: rotation thread: fatal error when not finding a session
[lttng-tools.git] / src / bin / lttng-sessiond / ust-field-utils.cpp
CommitLineData
98b73e88 1/*
ab5be9fa 2 * Copyright (C) 2018 Francis Deslauriers <francis.deslauriers@efficios.com>
98b73e88 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
98b73e88 5 *
98b73e88
FD
6 */
7
8#include <stdbool.h>
9#include <string.h>
10
11#include "ust-field-utils.h"
12
13/*
b623cb6a
MJ
14 * The lttng_ust_ctl_field is made of a combination of C basic types
15 * lttng_ust_ctl_basic_type and _lttng_ust_ctl_basic_type.
98b73e88 16 *
b623cb6a
MJ
17 * lttng_ust_ctl_basic_type contains an enumeration describing the abstract type.
18 * _lttng_ust_ctl_basic_type does _NOT_ contain an enumeration describing the
98b73e88
FD
19 * abstract type.
20 *
21 * A layer is needed to use the same code for both structures.
b623cb6a
MJ
22 * When dealing with _lttng_ust_ctl_basic_type, we need to use the abstract type of
23 * the lttng_ust_ctl_type struct.
98b73e88
FD
24 */
25
26/*
b623cb6a 27 * Compare two lttng_ust_ctl_integer_type fields.
98b73e88
FD
28 * Returns 1 if both are identical.
29 */
b623cb6a
MJ
30static bool match_lttng_ust_ctl_field_integer(const struct lttng_ust_ctl_integer_type *first,
31 const struct lttng_ust_ctl_integer_type *second)
98b73e88
FD
32{
33 if (first->size != second->size) {
34 goto no_match;
35 }
36 if (first->alignment != second->alignment) {
37 goto no_match;
38 }
39 if (first->signedness != second->signedness) {
40 goto no_match;
41 }
42 if (first->encoding != second->encoding) {
43 goto no_match;
44 }
45 if (first->base != second->base) {
46 goto no_match;
47 }
48 if (first->reverse_byte_order != second->reverse_byte_order) {
49 goto no_match;
50 }
51
52 return true;
53
54no_match:
55 return false;
56}
57
58/*
b623cb6a 59 * Compare two _lttng_ust_ctl_basic_type fields known to be of type integer.
98b73e88
FD
60 * Returns 1 if both are identical.
61 */
b623cb6a
MJ
62static bool match_lttng_ust_ctl_field_integer_from_raw_basic_type(
63 const union _lttng_ust_ctl_basic_type *first,
64 const union _lttng_ust_ctl_basic_type *second)
98b73e88 65{
b623cb6a 66 return match_lttng_ust_ctl_field_integer(&first->integer, &second->integer);
98b73e88
FD
67}
68
69/*
b623cb6a 70 * Compare two _lttng_ust_ctl_basic_type fields known to be of type enum.
98b73e88
FD
71 * Returns 1 if both are identical.
72 */
b623cb6a
MJ
73static bool match_lttng_ust_ctl_field_enum_from_raw_basic_type(
74 const union _lttng_ust_ctl_basic_type *first,
75 const union _lttng_ust_ctl_basic_type *second)
98b73e88
FD
76{
77 /*
78 * Compare enumeration ID. Enumeration ID is provided to the application by
79 * the session daemon before event registration.
80 */
81 if (first->enumeration.id != second->enumeration.id) {
82 goto no_match;
83 }
84
85 /*
86 * Sanity check of the name and container type. Those were already checked
87 * during enum registration.
88 */
89 if (strncmp(first->enumeration.name, second->enumeration.name,
fc4b93fa 90 LTTNG_UST_ABI_SYM_NAME_LEN)) {
98b73e88
FD
91 goto no_match;
92 }
b623cb6a 93 if (!match_lttng_ust_ctl_field_integer(&first->enumeration.container_type,
98b73e88
FD
94 &second->enumeration.container_type)) {
95 goto no_match;
96 }
97
98 return true;
99
100no_match:
101 return false;
102}
103
104/*
b623cb6a 105 * Compare two _lttng_ust_ctl_basic_type fields known to be of type string.
98b73e88
FD
106 * Returns 1 if both are identical.
107 */
b623cb6a
MJ
108static bool match_lttng_ust_ctl_field_string_from_raw_basic_type(
109 const union _lttng_ust_ctl_basic_type *first,
110 const union _lttng_ust_ctl_basic_type *second)
98b73e88
FD
111{
112 return first->string.encoding == second->string.encoding;
113}
114
115/*
b623cb6a 116 * Compare two _lttng_ust_ctl_basic_type fields known to be of type float.
98b73e88
FD
117 * Returns 1 if both are identical.
118 */
b623cb6a
MJ
119static bool match_lttng_ust_ctl_field_float_from_raw_basic_type(
120 const union _lttng_ust_ctl_basic_type *first,
121 const union _lttng_ust_ctl_basic_type *second)
98b73e88
FD
122{
123 if (first->_float.exp_dig != second->_float.exp_dig) {
124 goto no_match;
125 }
126
127 if (first->_float.mant_dig != second->_float.mant_dig) {
128 goto no_match;
129 }
130
131 if (first->_float.reverse_byte_order !=
132 second->_float.reverse_byte_order) {
133 goto no_match;
134 }
135
136 if (first->_float.alignment != second->_float.alignment) {
137 goto no_match;
138 }
139
140 return true;
141
142no_match:
143 return false;
144}
145
146/*
b623cb6a 147 * Compare two _lttng_ust_ctl_basic_type fields given their respective abstract types.
98b73e88
FD
148 * Returns 1 if both are identical.
149 */
b623cb6a
MJ
150static bool match_lttng_ust_ctl_field_raw_basic_type(
151 enum lttng_ust_ctl_abstract_types first_atype,
152 const union _lttng_ust_ctl_basic_type *first,
153 enum lttng_ust_ctl_abstract_types second_atype,
154 const union _lttng_ust_ctl_basic_type *second)
98b73e88
FD
155{
156 if (first_atype != second_atype) {
157 goto no_match;
158 }
159
160 switch (first_atype) {
b623cb6a
MJ
161 case lttng_ust_ctl_atype_integer:
162 if (!match_lttng_ust_ctl_field_integer_from_raw_basic_type(first, second)) {
98b73e88
FD
163 goto no_match;
164 }
165 break;
b623cb6a
MJ
166 case lttng_ust_ctl_atype_enum:
167 if (!match_lttng_ust_ctl_field_enum_from_raw_basic_type(first, second)) {
98b73e88
FD
168 goto no_match;
169 }
170 break;
b623cb6a
MJ
171 case lttng_ust_ctl_atype_string:
172 if (!match_lttng_ust_ctl_field_string_from_raw_basic_type(first, second)) {
98b73e88
FD
173 goto no_match;
174 }
175 break;
b623cb6a
MJ
176 case lttng_ust_ctl_atype_float:
177 if (!match_lttng_ust_ctl_field_float_from_raw_basic_type(first, second)) {
98b73e88
FD
178 goto no_match;
179 }
180 break;
181 default:
182 goto no_match;
183 }
184
185 return true;
186
187no_match:
188 return false;
189}
190
191/*
b623cb6a
MJ
192 * Compatibility layer between the lttng_ust_ctl_basic_type struct and
193 * _lttng_ust_ctl_basic_type union.
98b73e88 194 */
b623cb6a
MJ
195static bool match_lttng_ust_ctl_field_basic_type(const struct lttng_ust_ctl_basic_type *first,
196 const struct lttng_ust_ctl_basic_type *second)
98b73e88 197{
b623cb6a 198 return match_lttng_ust_ctl_field_raw_basic_type(first->atype, &first->u.basic,
98b73e88
FD
199 second->atype, &second->u.basic);
200}
201
b623cb6a
MJ
202int match_lttng_ust_ctl_field(const struct lttng_ust_ctl_field *first,
203 const struct lttng_ust_ctl_field *second)
98b73e88
FD
204{
205 /* Check the name of the field is identical. */
fc4b93fa 206 if (strncmp(first->name, second->name, LTTNG_UST_ABI_SYM_NAME_LEN)) {
98b73e88
FD
207 goto no_match;
208 }
209
210 /* Check the field type is identical. */
211 if (first->type.atype != second->type.atype) {
212 goto no_match;
213 }
214
215 /* Check the field layout. */
216 switch (first->type.atype) {
b623cb6a
MJ
217 case lttng_ust_ctl_atype_integer:
218 case lttng_ust_ctl_atype_enum:
219 case lttng_ust_ctl_atype_string:
220 case lttng_ust_ctl_atype_float:
221 if (!match_lttng_ust_ctl_field_raw_basic_type(first->type.atype,
0d32d1a9
MD
222 &first->type.u.legacy.basic, second->type.atype,
223 &second->type.u.legacy.basic)) {
98b73e88
FD
224 goto no_match;
225 }
226 break;
b623cb6a 227 case lttng_ust_ctl_atype_sequence:
98b73e88 228 /* Match element type of the sequence. */
b623cb6a 229 if (!match_lttng_ust_ctl_field_basic_type(&first->type.u.legacy.sequence.elem_type,
0d32d1a9 230 &second->type.u.legacy.sequence.elem_type)) {
98b73e88
FD
231 goto no_match;
232 }
233
234 /* Match length type of the sequence. */
b623cb6a 235 if (!match_lttng_ust_ctl_field_basic_type(&first->type.u.legacy.sequence.length_type,
0d32d1a9 236 &second->type.u.legacy.sequence.length_type)) {
98b73e88
FD
237 goto no_match;
238 }
239 break;
b623cb6a 240 case lttng_ust_ctl_atype_array:
98b73e88 241 /* Match element type of the array. */
b623cb6a 242 if (!match_lttng_ust_ctl_field_basic_type(&first->type.u.legacy.array.elem_type,
0d32d1a9 243 &second->type.u.legacy.array.elem_type)) {
98b73e88
FD
244 goto no_match;
245 }
246
247 /* Match length of the array. */
0d32d1a9 248 if (first->type.u.legacy.array.length != second->type.u.legacy.array.length) {
98b73e88
FD
249 goto no_match;
250 }
251 break;
b623cb6a 252 case lttng_ust_ctl_atype_variant:
98b73e88 253 /* Compare number of choice of the variants. */
0d32d1a9
MD
254 if (first->type.u.legacy.variant.nr_choices !=
255 second->type.u.legacy.variant.nr_choices) {
98b73e88
FD
256 goto no_match;
257 }
258
259 /* Compare tag name of the variants. */
0d32d1a9
MD
260 if (strncmp(first->type.u.legacy.variant.tag_name,
261 second->type.u.legacy.variant.tag_name,
fc4b93fa 262 LTTNG_UST_ABI_SYM_NAME_LEN)) {
98b73e88
FD
263 goto no_match;
264 }
265 break;
b623cb6a 266 case lttng_ust_ctl_atype_struct:
98b73e88 267 /* Compare number of fields of the structs. */
0d32d1a9
MD
268 if (first->type.u.legacy._struct.nr_fields != second->type.u.legacy._struct.nr_fields) {
269 goto no_match;
270 }
271 break;
b623cb6a 272 case lttng_ust_ctl_atype_sequence_nestable:
0d32d1a9
MD
273 if (first->type.u.sequence_nestable.alignment != second->type.u.sequence_nestable.alignment) {
274 goto no_match;
275 }
276 /* Compare length_name of the sequences. */
277 if (strncmp(first->type.u.sequence_nestable.length_name,
278 second->type.u.sequence_nestable.length_name,
fc4b93fa 279 LTTNG_UST_ABI_SYM_NAME_LEN)) {
0d32d1a9
MD
280 goto no_match;
281 }
282 /* Comparison will be done when marshalling following items. */
283 break;
b623cb6a 284 case lttng_ust_ctl_atype_array_nestable:
0d32d1a9
MD
285 if (first->type.u.array_nestable.alignment != second->type.u.array_nestable.alignment) {
286 goto no_match;
287 }
288 /* Match length of the array. */
289 if (first->type.u.array_nestable.length != second->type.u.array_nestable.length) {
290 goto no_match;
291 }
292 /* Comparison of element type will be done when marshalling following item. */
293 break;
b623cb6a 294 case lttng_ust_ctl_atype_enum_nestable:
0d32d1a9
MD
295 if (first->type.u.enum_nestable.id != second->type.u.enum_nestable.id) {
296 goto no_match;
297 }
298 /* Compare name of the enums. */
299 if (strncmp(first->type.u.enum_nestable.name,
300 second->type.u.enum_nestable.name,
fc4b93fa 301 LTTNG_UST_ABI_SYM_NAME_LEN)) {
0d32d1a9
MD
302 goto no_match;
303 }
304 /* Comparison of element type will be done when marshalling following item. */
305 break;
b623cb6a 306 case lttng_ust_ctl_atype_struct_nestable:
0d32d1a9
MD
307 if (first->type.u.struct_nestable.alignment != second->type.u.struct_nestable.alignment) {
308 goto no_match;
309 }
310 /* Compare number of fields of the structs. */
311 if (first->type.u.struct_nestable.nr_fields != second->type.u.struct_nestable.nr_fields) {
312 goto no_match;
313 }
314 break;
b623cb6a 315 case lttng_ust_ctl_atype_variant_nestable:
0d32d1a9
MD
316 if (first->type.u.variant_nestable.alignment != second->type.u.variant_nestable.alignment) {
317 goto no_match;
318 }
319 /* Compare number of choice of the variants. */
320 if (first->type.u.variant_nestable.nr_choices !=
321 second->type.u.variant_nestable.nr_choices) {
322 goto no_match;
323 }
324
325 /* Compare tag name of the variants. */
326 if (strncmp(first->type.u.variant_nestable.tag_name,
327 second->type.u.variant_nestable.tag_name,
fc4b93fa 328 LTTNG_UST_ABI_SYM_NAME_LEN)) {
98b73e88
FD
329 goto no_match;
330 }
331 break;
332 default:
333 goto no_match;
334 }
335
336 return true;
337
338no_match:
339 return false;
340}
fb277293
MD
341
342/*
343 * Compare two arrays of UST fields.
344 * Return true if both arrays have identical field definitions, false otherwise.
345 */
346bool match_lttng_ust_ctl_field_array(const struct lttng_ust_ctl_field *first,
347 size_t nr_first,
348 const struct lttng_ust_ctl_field *second,
349 size_t nr_second)
350{
351 size_t i;
352 const size_t nr_fields = nr_first;
353
354 /* Compare the array lengths. */
355 if (nr_first != nr_second) {
356 goto no_match;
357 }
358
359 /* Compare each field individually. */
360 for (i = 0; i < nr_fields; i++) {
361 if (!match_lttng_ust_ctl_field(&first[i], &second[i])) {
362 goto no_match;
363 }
364 }
365
366 return true;
367
368no_match:
369 return false;
370}
This page took 0.055518 seconds and 4 git commands to generate.