Fix: common: uninitialized lttng::ctl:error field
[lttng-tools.git] / src / common / event-field-value.cpp
1 /*
2 * event-field-value.c
3 *
4 * Linux Trace Toolkit Control Library
5 *
6 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
7 *
8 * SPDX-License-Identifier: LGPL-2.1-only
9 *
10 */
11
12 #define _LGPL_SOURCE
13 #include <stddef.h>
14 #include <stdbool.h>
15
16 #include <common/error.hpp>
17 #include <common/macros.hpp>
18 #include <lttng/event-field-value-internal.hpp>
19
20 static
21 struct lttng_event_field_value *create_empty_field_val(
22 enum lttng_event_field_value_type type, size_t size)
23 {
24 struct lttng_event_field_value *field_val;
25
26 field_val = zmalloc<lttng_event_field_value>(size);
27 if (!field_val) {
28 goto end;
29 }
30
31 field_val->type = type;
32
33 end:
34 return field_val;
35 }
36
37 struct lttng_event_field_value *lttng_event_field_value_uint_create(
38 uint64_t val)
39 {
40 struct lttng_event_field_value_uint *field_val;
41
42 field_val = lttng::utils::container_of(create_empty_field_val(
43 LTTNG_EVENT_FIELD_VALUE_TYPE_UNSIGNED_INT,
44 sizeof(*field_val)),
45 &lttng_event_field_value_uint::parent);
46 if (!field_val) {
47 goto error;
48 }
49
50 field_val->val = val;
51 goto end;
52
53 error:
54 lttng_event_field_value_destroy(&field_val->parent);
55
56 end:
57 return &field_val->parent;
58 }
59
60 struct lttng_event_field_value *lttng_event_field_value_int_create(
61 int64_t val)
62 {
63 struct lttng_event_field_value_int *field_val;
64
65 field_val = lttng::utils::container_of(create_empty_field_val(
66 LTTNG_EVENT_FIELD_VALUE_TYPE_SIGNED_INT,
67 sizeof(*field_val)),
68 &lttng_event_field_value_int::parent);
69 if (!field_val) {
70 goto error;
71 }
72
73 field_val->val = val;
74 goto end;
75
76 error:
77 lttng_event_field_value_destroy(&field_val->parent);
78
79 end:
80 return &field_val->parent;
81 }
82
83 static
84 struct lttng_event_field_value_enum *create_enum_field_val(
85 enum lttng_event_field_value_type type, size_t size)
86 {
87 struct lttng_event_field_value_enum *field_val;
88
89 field_val = lttng::utils::container_of(create_empty_field_val(type, size),
90 &lttng_event_field_value_enum::parent);
91 if (!field_val) {
92 goto error;
93 }
94
95 lttng_dynamic_pointer_array_init(&field_val->labels, free);
96 goto end;
97
98 error:
99 lttng_event_field_value_destroy(&field_val->parent);
100
101 end:
102 return field_val;
103 }
104
105 struct lttng_event_field_value *lttng_event_field_value_enum_uint_create(
106 uint64_t val)
107 {
108 struct lttng_event_field_value_enum_uint *field_val;
109
110 field_val = lttng::utils::container_of(create_enum_field_val(
111 LTTNG_EVENT_FIELD_VALUE_TYPE_UNSIGNED_ENUM,
112 sizeof(*field_val)),
113 &lttng_event_field_value_enum_uint::parent);
114 if (!field_val) {
115 goto error;
116 }
117
118 field_val->val = val;
119 goto end;
120
121 error:
122 lttng_event_field_value_destroy(&field_val->parent.parent);
123
124 end:
125 return &field_val->parent.parent;
126 }
127
128 struct lttng_event_field_value *lttng_event_field_value_enum_int_create(
129 int64_t val)
130 {
131 struct lttng_event_field_value_enum_int *field_val;
132
133 field_val = lttng::utils::container_of(create_enum_field_val(
134 LTTNG_EVENT_FIELD_VALUE_TYPE_SIGNED_ENUM,
135 sizeof(*field_val)),
136 &lttng_event_field_value_enum_int::parent);
137 if (!field_val) {
138 goto error;
139 }
140
141 field_val->val = val;
142 goto end;
143
144 error:
145 lttng_event_field_value_destroy(&field_val->parent.parent);
146
147 end:
148 return &field_val->parent.parent;
149 }
150
151 struct lttng_event_field_value *lttng_event_field_value_real_create(double val)
152 {
153 struct lttng_event_field_value_real *field_val = lttng::utils::container_of(
154 create_empty_field_val(
155 LTTNG_EVENT_FIELD_VALUE_TYPE_REAL,
156 sizeof(*field_val)),
157 &lttng_event_field_value_real::parent);
158
159 if (!field_val) {
160 goto error;
161 }
162
163 field_val->val = val;
164 goto end;
165
166 error:
167 lttng_event_field_value_destroy(&field_val->parent);
168
169 end:
170 return &field_val->parent;
171 }
172
173 struct lttng_event_field_value *lttng_event_field_value_string_create_with_size(
174 const char *val, size_t size)
175 {
176 struct lttng_event_field_value_string *field_val = lttng::utils::container_of(
177 create_empty_field_val(
178 LTTNG_EVENT_FIELD_VALUE_TYPE_STRING,
179 sizeof(*field_val)),
180 &lttng_event_field_value_string::parent);
181
182 if (!field_val) {
183 goto error;
184 }
185
186 LTTNG_ASSERT(val);
187 field_val->val = strndup(val, size);
188 if (!field_val->val) {
189 goto error;
190 }
191
192 goto end;
193
194 error:
195 lttng_event_field_value_destroy(&field_val->parent);
196
197 end:
198 return &field_val->parent;
199 }
200
201 struct lttng_event_field_value *lttng_event_field_value_string_create(
202 const char *val)
203 {
204 LTTNG_ASSERT(val);
205 return lttng_event_field_value_string_create_with_size(val,
206 strlen(val));
207 }
208
209 static
210 void destroy_field_val(void *field_val)
211 {
212 lttng_event_field_value_destroy((lttng_event_field_value *) field_val);
213 }
214
215 struct lttng_event_field_value *lttng_event_field_value_array_create(void)
216 {
217 struct lttng_event_field_value_array *field_val = lttng::utils::container_of(
218 create_empty_field_val(
219 LTTNG_EVENT_FIELD_VALUE_TYPE_ARRAY,
220 sizeof(*field_val)),
221 &lttng_event_field_value_array::parent);
222
223 if (!field_val) {
224 goto error;
225 }
226
227 lttng_dynamic_pointer_array_init(&field_val->elems, destroy_field_val);
228 goto end;
229
230 error:
231 lttng_event_field_value_destroy(&field_val->parent);
232
233 end:
234 return &field_val->parent;
235 }
236
237 void lttng_event_field_value_destroy(struct lttng_event_field_value *field_val)
238 {
239 if (!field_val) {
240 goto end;
241 }
242
243 switch (field_val->type) {
244 case LTTNG_EVENT_FIELD_VALUE_TYPE_UNSIGNED_ENUM:
245 case LTTNG_EVENT_FIELD_VALUE_TYPE_SIGNED_ENUM:
246 {
247 struct lttng_event_field_value_enum *enum_field_val =
248 lttng::utils::container_of(field_val,
249 &lttng_event_field_value_enum::parent);
250
251 lttng_dynamic_pointer_array_reset(&enum_field_val->labels);
252 break;
253 }
254 case LTTNG_EVENT_FIELD_VALUE_TYPE_STRING:
255 {
256 struct lttng_event_field_value_string *str_field_val =
257 lttng::utils::container_of(field_val,
258 &lttng_event_field_value_string::parent);
259
260 free(str_field_val->val);
261 break;
262 }
263 case LTTNG_EVENT_FIELD_VALUE_TYPE_ARRAY:
264 {
265 struct lttng_event_field_value_array *array_field_expr =
266 lttng::utils::container_of(field_val,
267 &lttng_event_field_value_array::parent);
268
269 lttng_dynamic_pointer_array_reset(&array_field_expr->elems);
270 break;
271 }
272 default:
273 break;
274 }
275
276 free(field_val);
277
278 end:
279 return;
280 }
281
282 int lttng_event_field_value_enum_append_label_with_size(
283 struct lttng_event_field_value *field_val,
284 const char *label, size_t size)
285 {
286 int ret;
287 char *new_label;
288
289 LTTNG_ASSERT(field_val);
290 LTTNG_ASSERT(label);
291 new_label = strndup(label, size);
292 if (!new_label) {
293 ret = -1;
294 goto end;
295 }
296
297 ret = lttng_dynamic_pointer_array_add_pointer(
298 &lttng::utils::container_of(field_val,
299 &lttng_event_field_value_enum::parent)->labels,
300 new_label);
301 if (ret == 0) {
302 new_label = NULL;
303 }
304
305 end:
306 free(new_label);
307 return ret;
308 }
309
310 int lttng_event_field_value_enum_append_label(
311 struct lttng_event_field_value *field_val,
312 const char *label)
313 {
314 LTTNG_ASSERT(label);
315 return lttng_event_field_value_enum_append_label_with_size(field_val,
316 label, strlen(label));
317 }
318
319 int lttng_event_field_value_array_append(
320 struct lttng_event_field_value *array_field_val,
321 struct lttng_event_field_value *field_val)
322 {
323 LTTNG_ASSERT(array_field_val);
324 LTTNG_ASSERT(field_val);
325 return lttng_dynamic_pointer_array_add_pointer(
326 &lttng::utils::container_of(array_field_val,
327 &lttng_event_field_value_array::parent)->elems,
328 field_val);
329 }
330
331 int lttng_event_field_value_array_append_unavailable(
332 struct lttng_event_field_value *array_field_val)
333 {
334 LTTNG_ASSERT(array_field_val);
335 return lttng_dynamic_pointer_array_add_pointer(
336 &lttng::utils::container_of(array_field_val,
337 &lttng_event_field_value_array::parent)->elems,
338 NULL);
339 }
340
341 enum lttng_event_field_value_type lttng_event_field_value_get_type(
342 const struct lttng_event_field_value *field_val)
343 {
344 enum lttng_event_field_value_type type;
345
346 if (!field_val) {
347 type = LTTNG_EVENT_FIELD_VALUE_TYPE_INVALID;
348 goto end;
349 }
350
351 type = field_val->type;
352
353 end:
354 return type;
355 }
356
357 enum lttng_event_field_value_status
358 lttng_event_field_value_unsigned_int_get_value(
359 const struct lttng_event_field_value *field_val, uint64_t *val)
360 {
361 enum lttng_event_field_value_status status;
362
363 if (!field_val || !val) {
364 status = LTTNG_EVENT_FIELD_VALUE_STATUS_INVALID;
365 goto end;
366 }
367
368 switch (field_val->type) {
369 case LTTNG_EVENT_FIELD_VALUE_TYPE_UNSIGNED_INT:
370 *val = lttng::utils::container_of(field_val,
371 &lttng_event_field_value_uint::parent)->val;
372 break;
373 case LTTNG_EVENT_FIELD_VALUE_TYPE_UNSIGNED_ENUM:
374 {
375 const struct lttng_event_field_value_enum *field_val_enum =
376 lttng::utils::container_of(
377 field_val, &lttng_event_field_value_enum::parent);
378 const struct lttng_event_field_value_enum_uint *field_val_enum_uint =
379 lttng::utils::container_of(field_val_enum,
380 &lttng_event_field_value_enum_uint::parent);
381 *val = field_val_enum_uint->val;
382 break;
383 }
384 default:
385 status = LTTNG_EVENT_FIELD_VALUE_STATUS_INVALID;
386 goto end;
387 }
388
389 status = LTTNG_EVENT_FIELD_VALUE_STATUS_OK;
390
391 end:
392 return status;
393 }
394
395 enum lttng_event_field_value_status
396 lttng_event_field_value_signed_int_get_value(
397 const struct lttng_event_field_value *field_val, int64_t *val)
398 {
399 enum lttng_event_field_value_status status;
400
401 if (!field_val || !val) {
402 status = LTTNG_EVENT_FIELD_VALUE_STATUS_INVALID;
403 goto end;
404 }
405
406 switch (field_val->type) {
407 case LTTNG_EVENT_FIELD_VALUE_TYPE_SIGNED_INT:
408 *val = lttng::utils::container_of(field_val,
409 &lttng_event_field_value_int::parent)->val;
410 break;
411 case LTTNG_EVENT_FIELD_VALUE_TYPE_SIGNED_ENUM:
412 {
413 const struct lttng_event_field_value_enum *field_val_enum = lttng::utils::container_of(
414 field_val,
415 &lttng_event_field_value_enum::parent);
416 const struct lttng_event_field_value_enum_int *field_val_enum_uint =
417 lttng::utils::container_of(field_val_enum,
418 &lttng_event_field_value_enum_int::parent);
419 *val = field_val_enum_uint->val;
420 break;
421 }
422 default:
423 status = LTTNG_EVENT_FIELD_VALUE_STATUS_INVALID;
424 goto end;
425 }
426
427 status = LTTNG_EVENT_FIELD_VALUE_STATUS_OK;
428
429 end:
430 return status;
431 }
432
433 enum lttng_event_field_value_status
434 lttng_event_field_value_real_get_value(
435 const struct lttng_event_field_value *field_val, double *val)
436 {
437 enum lttng_event_field_value_status status;
438
439 if (!field_val || field_val->type != LTTNG_EVENT_FIELD_VALUE_TYPE_REAL ||
440 !val) {
441 status = LTTNG_EVENT_FIELD_VALUE_STATUS_INVALID;
442 goto end;
443 }
444
445 *val = lttng::utils::container_of(field_val,
446 &lttng_event_field_value_real::parent)->val;
447 status = LTTNG_EVENT_FIELD_VALUE_STATUS_OK;
448
449 end:
450 return status;
451 }
452
453 static
454 bool is_enum_field_val(const struct lttng_event_field_value *field_val)
455 {
456 return field_val->type == LTTNG_EVENT_FIELD_VALUE_TYPE_UNSIGNED_ENUM ||
457 field_val->type == LTTNG_EVENT_FIELD_VALUE_TYPE_SIGNED_ENUM;
458 }
459
460 enum lttng_event_field_value_status
461 lttng_event_field_value_enum_get_label_count(
462 const struct lttng_event_field_value *field_val,
463 unsigned int *count)
464 {
465 enum lttng_event_field_value_status status;
466
467 if (!field_val || !is_enum_field_val(field_val) || !count) {
468 status = LTTNG_EVENT_FIELD_VALUE_STATUS_INVALID;
469 goto end;
470 }
471
472 *count = (unsigned int) lttng_dynamic_pointer_array_get_count(
473 &lttng::utils::container_of(field_val,
474 &lttng_event_field_value_enum::parent)->labels);
475 status = LTTNG_EVENT_FIELD_VALUE_STATUS_OK;
476
477 end:
478 return status;
479 }
480
481 const char *lttng_event_field_value_enum_get_label_at_index(
482 const struct lttng_event_field_value *field_val,
483 unsigned int index)
484 {
485 const char *ret;
486 const struct lttng_event_field_value_enum *enum_field_val;
487
488 if (!field_val || !is_enum_field_val(field_val)) {
489 ret = NULL;
490 goto end;
491 }
492
493 enum_field_val = lttng::utils::container_of(field_val,
494 &lttng_event_field_value_enum::parent);
495
496 if (index >= lttng_dynamic_pointer_array_get_count(&enum_field_val->labels)) {
497 ret = NULL;
498 goto end;
499 }
500
501 ret = (const char *) lttng_dynamic_pointer_array_get_pointer(&enum_field_val->labels,
502 index);
503
504 end:
505 return ret;
506 }
507
508 enum lttng_event_field_value_status lttng_event_field_value_string_get_value(
509 const struct lttng_event_field_value *field_val,
510 const char **value)
511 {
512 enum lttng_event_field_value_status status;
513
514 if (!field_val || field_val->type != LTTNG_EVENT_FIELD_VALUE_TYPE_STRING) {
515 status = LTTNG_EVENT_FIELD_VALUE_STATUS_INVALID;
516 goto end;
517 }
518
519 *value = lttng::utils::container_of(field_val,
520 &lttng_event_field_value_string::parent)->val;
521 status = LTTNG_EVENT_FIELD_VALUE_STATUS_OK;
522
523 end:
524 return status;
525 }
526
527 enum lttng_event_field_value_status lttng_event_field_value_array_get_length(
528 const struct lttng_event_field_value *field_val,
529 unsigned int *length)
530 {
531 enum lttng_event_field_value_status status;
532
533 if (!field_val || field_val->type != LTTNG_EVENT_FIELD_VALUE_TYPE_ARRAY ||
534 !length) {
535 status = LTTNG_EVENT_FIELD_VALUE_STATUS_INVALID;
536 goto end;
537 }
538
539 *length = (unsigned int) lttng_dynamic_pointer_array_get_count(
540 &lttng::utils::container_of(field_val,
541 &lttng_event_field_value_array::parent)->elems);
542 status = LTTNG_EVENT_FIELD_VALUE_STATUS_OK;
543
544 end:
545 return status;
546 }
547
548 enum lttng_event_field_value_status
549 lttng_event_field_value_array_get_element_at_index(
550 const struct lttng_event_field_value *field_val,
551 unsigned int index,
552 const struct lttng_event_field_value **elem_field_val)
553 {
554 enum lttng_event_field_value_status status;
555 const struct lttng_event_field_value_array *array_field_val;
556
557 if (!field_val || field_val->type != LTTNG_EVENT_FIELD_VALUE_TYPE_ARRAY ||
558 !elem_field_val) {
559 status = LTTNG_EVENT_FIELD_VALUE_STATUS_INVALID;
560 goto end;
561 }
562
563 array_field_val = lttng::utils::container_of(
564 field_val, &lttng_event_field_value_array::parent);
565
566 if (index >= lttng_dynamic_pointer_array_get_count(&array_field_val->elems)) {
567 status = LTTNG_EVENT_FIELD_VALUE_STATUS_INVALID;
568 goto end;
569 }
570
571 *elem_field_val = (lttng_event_field_value *) lttng_dynamic_pointer_array_get_pointer(
572 &array_field_val->elems, index);
573 if (*elem_field_val) {
574 status = LTTNG_EVENT_FIELD_VALUE_STATUS_OK;
575 } else {
576 status = LTTNG_EVENT_FIELD_VALUE_STATUS_UNAVAILABLE;
577 }
578
579 end:
580 return status;
581 }
This page took 0.04192 seconds and 4 git commands to generate.