Fix: lib: add NULL check for destroy_listeners in destroy_component_class
[babeltrace.git] / src / ctf-writer / event-class.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 */
7
8 #define BT_LOG_TAG "CTF-WRITER/EVENT-CLASS"
9 #include "logging.h"
10
11 #include <glib.h>
12 #include <inttypes.h>
13 #include <stdlib.h>
14
15 #include <babeltrace2-ctf-writer/event.h>
16 #include <babeltrace2-ctf-writer/field-types.h>
17 #include <babeltrace2-ctf-writer/object.h>
18 #include <babeltrace2-ctf-writer/stream-class.h>
19 #include <babeltrace2-ctf-writer/utils.h>
20 #include <babeltrace2/types.h>
21
22 #include "common/assert.h"
23 #include "compat/compiler.h"
24 #include "compat/endian.h"
25
26 #include "assert-pre.h"
27 #include "attributes.h"
28 #include "event-class.h"
29 #include "event.h"
30 #include "fields.h"
31 #include "field-types.h"
32 #include "stream-class.h"
33 #include "trace.h"
34 #include "utils.h"
35 #include "validation.h"
36 #include "values.h"
37 #include "writer.h"
38
39 BT_HIDDEN
40 void bt_ctf_event_class_common_finalize(struct bt_ctf_object *obj)
41 {
42 struct bt_ctf_event_class_common *event_class;
43
44 event_class = container_of(obj, struct bt_ctf_event_class_common, base);
45 BT_LOGD("Finalizing common event class: addr=%p, name=\"%s\", id=%" PRId64,
46 event_class, bt_ctf_event_class_common_get_name(event_class),
47 bt_ctf_event_class_common_get_id(event_class));
48
49 if (event_class->name) {
50 g_string_free(event_class->name, TRUE);
51 }
52
53 if (event_class->emf_uri) {
54 g_string_free(event_class->emf_uri, TRUE);
55 }
56
57 BT_LOGD_STR("Putting context field type.");
58 bt_ctf_object_put_ref(event_class->context_field_type);
59 BT_LOGD_STR("Putting payload field type.");
60 bt_ctf_object_put_ref(event_class->payload_field_type);
61 }
62
63 BT_HIDDEN
64 int bt_ctf_event_class_common_initialize(struct bt_ctf_event_class_common *event_class,
65 const char *name, bt_ctf_object_release_func release_func,
66 bt_ctf_field_type_structure_create_func ft_struct_create_func)
67 {
68 int ret = 0;
69
70 BT_LOGD("Initializing common event class object: name=\"%s\"",
71 name);
72 bt_ctf_object_init_shared_with_parent(&event_class->base, release_func);
73 event_class->payload_field_type = ft_struct_create_func();
74 if (!event_class->payload_field_type) {
75 BT_LOGE_STR("Cannot create event class's initial payload field type object.");
76 goto error;
77 }
78
79 event_class->id = -1;
80 event_class->name = g_string_new(name);
81 if (!event_class->name) {
82 BT_LOGE_STR("Failed to allocate a GString.");
83 goto error;
84 }
85
86 event_class->emf_uri = g_string_new(NULL);
87 if (!event_class->emf_uri) {
88 BT_LOGE_STR("Failed to allocate a GString.");
89 goto error;
90 }
91
92 event_class->log_level = BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED;
93 BT_LOGD("Initialized common event class object: addr=%p, name=\"%s\"",
94 event_class, bt_ctf_event_class_common_get_name(event_class));
95 return ret;
96
97 error:
98 ret = -1;
99 return ret;
100 }
101
102 BT_HIDDEN
103 void bt_ctf_event_class_common_freeze(struct bt_ctf_event_class_common *event_class)
104 {
105 BT_ASSERT_DBG(event_class);
106
107 if (event_class->frozen) {
108 return;
109 }
110
111 BT_LOGD("Freezing event class: addr=%p, name=\"%s\", id=%" PRId64,
112 event_class, bt_ctf_event_class_common_get_name(event_class),
113 bt_ctf_event_class_common_get_id(event_class));
114 event_class->frozen = 1;
115 BT_LOGD_STR("Freezing event class's context field type.");
116 bt_ctf_field_type_common_freeze(event_class->context_field_type);
117 BT_LOGD_STR("Freezing event class's payload field type.");
118 bt_ctf_field_type_common_freeze(event_class->payload_field_type);
119 }
120
121 BT_HIDDEN
122 int bt_ctf_event_class_common_validate_single_clock_class(
123 struct bt_ctf_event_class_common *event_class,
124 struct bt_ctf_clock_class **expected_clock_class)
125 {
126 int ret = 0;
127
128 BT_ASSERT_DBG(event_class);
129 BT_ASSERT_DBG(expected_clock_class);
130 ret = bt_ctf_field_type_common_validate_single_clock_class(
131 event_class->context_field_type,
132 expected_clock_class);
133 if (ret) {
134 BT_LOGW("Event class's context field type "
135 "is not recursively mapped to the "
136 "expected clock class: "
137 "event-class-addr=%p, "
138 "event-class-name=\"%s\", "
139 "event-class-id=%" PRId64 ", "
140 "ft-addr=%p",
141 event_class,
142 bt_ctf_event_class_common_get_name(event_class),
143 event_class->id,
144 event_class->context_field_type);
145 goto end;
146 }
147
148 ret = bt_ctf_field_type_common_validate_single_clock_class(
149 event_class->payload_field_type,
150 expected_clock_class);
151 if (ret) {
152 BT_LOGW("Event class's payload field type "
153 "is not recursively mapped to the "
154 "expected clock class: "
155 "event-class-addr=%p, "
156 "event-class-name=\"%s\", "
157 "event-class-id=%" PRId64 ", "
158 "ft-addr=%p",
159 event_class,
160 bt_ctf_event_class_common_get_name(event_class),
161 event_class->id,
162 event_class->payload_field_type);
163 goto end;
164 }
165
166 end:
167 return ret;
168 }
169
170 static
171 void bt_ctf_event_class_destroy(struct bt_ctf_object *obj)
172 {
173 bt_ctf_event_class_common_finalize(obj);
174 g_free(obj);
175 }
176
177 struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name)
178 {
179 struct bt_ctf_event_class *ctf_event_class = NULL;
180 int ret;
181
182 if (!name) {
183 BT_LOGW_STR("Invalid parameter: name is NULL.");
184 goto error;
185 }
186
187 BT_LOGD("Creating event class object: name=\"%s\"",
188 name);
189 ctf_event_class = g_new0(struct bt_ctf_event_class, 1);
190 if (!ctf_event_class) {
191 BT_LOGE_STR("Failed to allocate one event class.");
192 goto error;
193 }
194
195 ret = bt_ctf_event_class_common_initialize(BT_CTF_TO_COMMON(ctf_event_class),
196 name, bt_ctf_event_class_destroy,
197 (bt_ctf_field_type_structure_create_func)
198 bt_ctf_field_type_structure_create);
199 if (ret) {
200 goto error;
201 }
202
203 goto end;
204
205 error:
206 bt_ctf_object_put_ref(ctf_event_class);
207
208 end:
209 return ctf_event_class;
210 }
211
212 const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class)
213 {
214 return bt_ctf_event_class_common_get_name(BT_CTF_TO_COMMON(event_class));
215 }
216
217 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class)
218 {
219 return bt_ctf_event_class_common_get_id(BT_CTF_TO_COMMON(event_class));
220 }
221
222 int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
223 uint64_t id)
224 {
225 return bt_ctf_event_class_common_set_id(BT_CTF_TO_COMMON(event_class), id);
226 }
227
228 enum bt_ctf_event_class_log_level bt_ctf_event_class_get_log_level(
229 struct bt_ctf_event_class *event_class)
230 {
231 return bt_ctf_event_class_common_get_log_level(BT_CTF_TO_COMMON(event_class));
232 }
233
234 int bt_ctf_event_class_set_log_level(struct bt_ctf_event_class *event_class,
235 enum bt_ctf_event_class_log_level log_level)
236 {
237 return bt_ctf_event_class_common_set_log_level(BT_CTF_TO_COMMON(event_class),
238 log_level);
239 }
240
241 const char *bt_ctf_event_class_get_emf_uri(
242 struct bt_ctf_event_class *event_class)
243 {
244 return bt_ctf_event_class_common_get_emf_uri(BT_CTF_TO_COMMON(event_class));
245 }
246
247 int bt_ctf_event_class_set_emf_uri(struct bt_ctf_event_class *event_class,
248 const char *emf_uri)
249 {
250 return bt_ctf_event_class_common_set_emf_uri(BT_CTF_TO_COMMON(event_class),
251 emf_uri);
252 }
253
254 struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class(
255 struct bt_ctf_event_class *event_class)
256 {
257 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
258 return bt_ctf_object_get_ref(bt_ctf_event_class_common_borrow_stream_class(
259 BT_CTF_TO_COMMON(event_class)));
260 }
261
262 struct bt_ctf_field_type *bt_ctf_event_class_get_payload_field_type(
263 struct bt_ctf_event_class *event_class)
264 {
265 return bt_ctf_object_get_ref(bt_ctf_event_class_common_borrow_payload_field_type(
266 BT_CTF_TO_COMMON(event_class)));
267 }
268
269 int bt_ctf_event_class_set_payload_field_type(
270 struct bt_ctf_event_class *event_class,
271 struct bt_ctf_field_type *field_type)
272 {
273 return bt_ctf_event_class_common_set_payload_field_type(
274 BT_CTF_TO_COMMON(event_class), (void *) field_type);
275 }
276
277 struct bt_ctf_field_type *bt_ctf_event_class_get_context_field_type(
278 struct bt_ctf_event_class *event_class)
279 {
280 return bt_ctf_object_get_ref(bt_ctf_event_class_common_borrow_context_field_type(
281 BT_CTF_TO_COMMON(event_class)));
282 }
283
284 int bt_ctf_event_class_set_context_field_type(
285 struct bt_ctf_event_class *event_class,
286 struct bt_ctf_field_type *field_type)
287 {
288 return bt_ctf_event_class_common_set_context_field_type(
289 BT_CTF_TO_COMMON(event_class), (void *) field_type);
290 }
291
292 int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
293 struct bt_ctf_field_type *type,
294 const char *name)
295 {
296 int ret = 0;
297
298 if (!event_class || !type) {
299 BT_LOGW("Invalid parameter: event class or field type is NULL: "
300 "event-class-addr=%p, field-type-addr=%p",
301 event_class, type);
302 ret = -1;
303 goto end;
304 }
305
306 if (!bt_ctf_identifier_is_valid(name)) {
307 BT_LOGW("Invalid parameter: event class's payload field type's field name is not a valid CTF identifier: "
308 "addr=%p, name=\"%s\", id=%" PRId64 ", field-name=\"%s\"",
309 event_class, bt_ctf_event_class_get_name(event_class),
310 bt_ctf_event_class_get_id(event_class),
311 name);
312 ret = -1;
313 goto end;
314 }
315
316 if (event_class->common.frozen) {
317 BT_LOGW("Invalid parameter: event class is frozen: "
318 "addr=%p, name=\"%s\", id=%" PRId64,
319 event_class, bt_ctf_event_class_get_name(event_class),
320 bt_ctf_event_class_get_id(event_class));
321 ret = -1;
322 goto end;
323 }
324
325 if (!event_class->common.payload_field_type) {
326 BT_LOGW("Event class has no payload field type: "
327 "addr=%p, name=\"%s\", id=%" PRId64,
328 event_class, bt_ctf_event_class_get_name(event_class),
329 bt_ctf_event_class_get_id(event_class));
330 ret = -1;
331 goto end;
332 }
333
334 BT_ASSERT_DBG(bt_ctf_field_type_common_get_type_id(
335 event_class->common.payload_field_type) ==
336 BT_CTF_FIELD_TYPE_ID_STRUCT);
337 ret = bt_ctf_field_type_structure_add_field(
338 (void *) event_class->common.payload_field_type,
339 (void *) type, name);
340 BT_LOGT("Added field to event class's payload field type: "
341 "event-class-addr=%p, event-class-name=\"%s\", "
342 "event-class-id=%" PRId64 ", field-name=\"%s\", ft-addr=%p",
343 event_class, bt_ctf_event_class_get_name(event_class),
344 bt_ctf_event_class_get_id(event_class), name, type);
345 end:
346 return ret;
347 }
348
349 int64_t bt_ctf_event_class_get_payload_type_field_count(
350 struct bt_ctf_event_class *event_class)
351 {
352 int64_t ret;
353
354 if (!event_class) {
355 BT_LOGW_STR("Invalid parameter: event class is NULL.");
356 ret = (int64_t) -1;
357 goto end;
358 }
359
360 if (!event_class->common.payload_field_type) {
361 BT_LOGT("Event class has no payload field type: "
362 "addr=%p, name=\"%s\", id=%" PRId64,
363 event_class, bt_ctf_event_class_get_name(event_class),
364 bt_ctf_event_class_get_id(event_class));
365 ret = (int64_t) -1;
366 goto end;
367 }
368
369 BT_ASSERT_DBG(bt_ctf_field_type_common_get_type_id(
370 event_class->common.payload_field_type) ==
371 BT_CTF_FIELD_TYPE_ID_STRUCT);
372 ret = bt_ctf_field_type_common_structure_get_field_count(
373 event_class->common.payload_field_type);
374 end:
375 return ret;
376 }
377
378 int bt_ctf_event_class_get_payload_type_field_by_index(
379 struct bt_ctf_event_class *event_class,
380 const char **field_name, struct bt_ctf_field_type **field_type,
381 uint64_t index)
382 {
383 int ret;
384
385 if (!event_class) {
386 BT_LOGW_STR("Invalid parameter: event class is NULL.");
387 ret = -1;
388 goto end;
389 }
390
391 if (!event_class->common.payload_field_type) {
392 BT_LOGT("Event class has no payload field type: "
393 "addr=%p, name=\"%s\", id=%" PRId64 ", index=%" PRIu64,
394 event_class, bt_ctf_event_class_get_name(event_class),
395 bt_ctf_event_class_get_id(event_class), index);
396 ret = -1;
397 goto end;
398 }
399
400 BT_ASSERT_DBG(bt_ctf_field_type_common_get_type_id(
401 event_class->common.payload_field_type) ==
402 BT_CTF_FIELD_TYPE_ID_STRUCT);
403 ret = bt_ctf_field_type_structure_get_field_by_index(
404 (void *) event_class->common.payload_field_type,
405 field_name, (void *) field_type, index);
406
407 end:
408 return ret;
409 }
410
411 struct bt_ctf_field_type *
412 bt_ctf_event_class_get_payload_type_field_type_by_name(
413 struct bt_ctf_event_class *event_class, const char *name)
414 {
415 GQuark name_quark;
416 struct bt_ctf_field_type *field_type = NULL;
417
418 if (!event_class || !name) {
419 BT_LOGW("Invalid parameter: event class or name is NULL: "
420 "event-class-addr=%p, name-addr=%p",
421 event_class, name);
422 goto end;
423 }
424
425 if (!event_class->common.payload_field_type) {
426 BT_LOGT("Event class has no payload field type: "
427 "addr=%p, name=\"%s\", id=%" PRId64,
428 event_class, bt_ctf_event_class_get_name(event_class),
429 bt_ctf_event_class_get_id(event_class));
430 goto end;
431 }
432
433 BT_ASSERT_DBG(bt_ctf_field_type_common_get_type_id(
434 event_class->common.payload_field_type) ==
435 BT_CTF_FIELD_TYPE_ID_STRUCT);
436 name_quark = g_quark_try_string(name);
437 if (!name_quark) {
438 BT_LOGE("Cannot get GQuark: string=\"%s\"", name);
439 goto end;
440 }
441
442 /*
443 * No need to increment field_type's reference count since getting it
444 * from the structure already does.
445 */
446 field_type = (void *)
447 bt_ctf_field_type_structure_get_field_type_by_name(
448 (void *) event_class->common.payload_field_type, name);
449
450 end:
451 return field_type;
452 }
453
454 BT_HIDDEN
455 int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
456 struct metadata_context *context)
457 {
458 int ret = 0;
459 struct bt_ctf_value *attr_value = NULL;
460
461 BT_ASSERT_DBG(event_class);
462 BT_ASSERT_DBG(context);
463 BT_LOGD("Serializing event class's metadata: "
464 "event-class-addr=%p, event-class-name=\"%s\", "
465 "event-class-id=%" PRId64 ", metadata-context-addr=%p",
466 event_class, bt_ctf_event_class_get_name(event_class),
467 bt_ctf_event_class_get_id(event_class), context);
468 context->current_indentation_level = 1;
469 g_string_assign(context->field_name, "");
470 g_string_append(context->string, "event {\n");
471
472 /* Serialize attributes */
473 g_string_append_printf(context->string, "\tname = \"%s\";\n",
474 event_class->common.name->str);
475 BT_ASSERT_DBG(event_class->common.id >= 0);
476 g_string_append_printf(context->string, "\tid = %" PRId64 ";\n",
477 event_class->common.id);
478 g_string_append_printf(context->string, "\tstream_id = %" PRId64 ";\n",
479 bt_ctf_stream_class_common_get_id(
480 bt_ctf_event_class_common_borrow_stream_class(
481 BT_CTF_TO_COMMON(event_class))));
482
483 if (event_class->common.log_level !=
484 BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED) {
485 g_string_append_printf(context->string, "\tloglevel = %d;\n",
486 (int) event_class->common.log_level);
487 }
488
489 if (event_class->common.emf_uri->len > 0) {
490 g_string_append_printf(context->string, "\tmodel.emf.uri = \"%s\";\n",
491 event_class->common.emf_uri->str);
492 }
493
494 /* Serialize context field type */
495 if (event_class->common.context_field_type) {
496 g_string_append(context->string, "\tcontext := ");
497 BT_LOGD_STR("Serializing event class's context field type metadata.");
498 ret = bt_ctf_field_type_serialize_recursive(
499 (void *) event_class->common.context_field_type,
500 context);
501 if (ret) {
502 BT_LOGW("Cannot serialize event class's context field type's metadata: "
503 "ret=%d", ret);
504 goto end;
505 }
506 g_string_append(context->string, ";\n");
507 }
508
509 /* Serialize payload field type */
510 if (event_class->common.payload_field_type) {
511 g_string_append(context->string, "\tfields := ");
512 BT_LOGD_STR("Serializing event class's payload field type metadata.");
513 ret = bt_ctf_field_type_serialize_recursive(
514 (void *) event_class->common.payload_field_type,
515 context);
516 if (ret) {
517 BT_LOGW("Cannot serialize event class's payload field type's metadata: "
518 "ret=%d", ret);
519 goto end;
520 }
521 g_string_append(context->string, ";\n");
522 }
523
524 g_string_append(context->string, "};\n\n");
525
526 end:
527 context->current_indentation_level = 0;
528 BT_CTF_OBJECT_PUT_REF_AND_RESET(attr_value);
529 return ret;
530 }
531
532 struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name(
533 struct bt_ctf_event_class *event_class, const char *name)
534 {
535 GQuark name_quark;
536 struct bt_ctf_field_type *field_type = NULL;
537
538 if (!event_class || !name) {
539 BT_LOGW("Invalid parameter: event class or name is NULL: "
540 "event-class-addr=%p, name-addr=%p",
541 event_class, name);
542 goto end;
543 }
544
545 if (!event_class->common.payload_field_type) {
546 BT_LOGT("Event class has no payload field type: "
547 "addr=%p, name=\"%s\", id=%" PRId64,
548 event_class,
549 bt_ctf_event_class_get_name(event_class),
550 bt_ctf_event_class_get_id(event_class));
551 goto end;
552 }
553
554 BT_ASSERT_DBG(event_class->common.payload_field_type->id ==
555 BT_CTF_FIELD_TYPE_ID_STRUCT);
556 name_quark = g_quark_try_string(name);
557 if (!name_quark) {
558 BT_LOGE("Cannot get GQuark: string=\"%s\"", name);
559 goto end;
560 }
561
562 /*
563 * No need to increment field_type's reference count since getting it
564 * from the structure already does.
565 */
566 field_type = bt_ctf_object_get_ref(
567 bt_ctf_field_type_common_structure_borrow_field_type_by_name(
568 event_class->common.payload_field_type, name));
569
570 end:
571 return field_type;
572 }
This page took 0.051894 seconds and 4 git commands to generate.