Fix: lib: add NULL check for destroy_listeners in destroy_component_class
[babeltrace.git] / src / ctf-writer / event.c
CommitLineData
3dca2276 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
3dca2276
PP
4 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3dca2276
PP
6 */
7
350ad6c1 8#define BT_LOG_TAG "CTF-WRITER/EVENT"
67d2ce02 9#include "logging.h"
3dca2276 10
578e048b 11#include <inttypes.h>
c4f23e30 12#include <stdbool.h>
578e048b 13
217cf9d3
PP
14#include <babeltrace2-ctf-writer/event.h>
15#include <babeltrace2-ctf-writer/fields.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/trace.h>
20#include <babeltrace2-ctf-writer/utils.h>
578e048b
MJ
21
22#include "common/assert.h"
23#include "compat/compiler.h"
24
25#include "assert-pre.h"
26#include "attributes.h"
27#include "clock-class.h"
28#include "clock.h"
29#include "event-class.h"
30#include "event.h"
31#include "fields.h"
32#include "field-types.h"
33#include "stream-class.h"
34#include "stream.h"
35#include "trace.h"
36#include "validation.h"
3dca2276
PP
37
38static
16ca5ff0
PP
39int bt_ctf_event_common_validate_types_for_create(
40 struct bt_ctf_event_class_common *event_class,
41 struct bt_ctf_validation_output *validation_output,
42 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func)
43{
44 int ret;
45 enum bt_ctf_validation_flag validation_flags =
46 BT_CTF_VALIDATION_FLAG_STREAM |
47 BT_CTF_VALIDATION_FLAG_EVENT;
48 struct bt_ctf_trace_common *trace = NULL;
49 struct bt_ctf_stream_class_common *stream_class = NULL;
50 struct bt_ctf_field_type_common *packet_header_type = NULL;
51 struct bt_ctf_field_type_common *packet_context_type = NULL;
52 struct bt_ctf_field_type_common *event_header_type = NULL;
53 struct bt_ctf_field_type_common *stream_event_ctx_type = NULL;
54 struct bt_ctf_field_type_common *event_context_type = NULL;
55 struct bt_ctf_field_type_common *event_payload_type = NULL;
56 int trace_valid = 0;
e1e02a22 57 struct bt_ctf_private_value *environment = NULL;
16ca5ff0
PP
58
59 stream_class = bt_ctf_event_class_common_borrow_stream_class(event_class);
98b15851 60 BT_ASSERT_DBG(stream_class);
16ca5ff0
PP
61 trace = bt_ctf_stream_class_common_borrow_trace(stream_class);
62 if (trace) {
63 BT_LOGD_STR("Event class is part of a trace.");
64 packet_header_type =
65 bt_ctf_trace_common_borrow_packet_header_field_type(trace);
66 trace_valid = trace->valid;
98b15851 67 BT_ASSERT_DBG(trace_valid);
16ca5ff0
PP
68 environment = trace->environment;
69 }
70
71 packet_context_type =
72 bt_ctf_stream_class_common_borrow_packet_context_field_type(
73 stream_class);
74 event_header_type =
75 bt_ctf_stream_class_common_borrow_event_header_field_type(
76 stream_class);
77 stream_event_ctx_type =
78 bt_ctf_stream_class_common_borrow_event_context_field_type(
79 stream_class);
80 event_context_type =
81 bt_ctf_event_class_common_borrow_context_field_type(event_class);
82 event_payload_type =
83 bt_ctf_event_class_common_borrow_payload_field_type(event_class);
84 ret = bt_ctf_validate_class_types(environment, packet_header_type,
85 packet_context_type, event_header_type, stream_event_ctx_type,
86 event_context_type, event_payload_type, trace_valid,
87 stream_class->valid, event_class->valid,
88 validation_output, validation_flags, copy_field_type_func);
89 if (ret) {
90 /*
91 * This means something went wrong during the validation
92 * process, not that the objects are invalid.
93 */
94 BT_LOGE("Failed to validate event and parents: ret=%d", ret);
95 goto error;
96 }
97
98 if ((validation_output->valid_flags & validation_flags) !=
99 validation_flags) {
100 /* Invalid trace/stream class/event class */
101 BT_LOGW("Invalid trace, stream class, or event class: "
102 "valid-flags=0x%x", validation_output->valid_flags);
103 goto error;
104 }
105
106 goto end;
107
108error:
109 bt_ctf_validation_output_put_types(validation_output);
110 ret = -1;
111
112end:
113 return ret;
114}
3dca2276
PP
115
116static
16ca5ff0
PP
117int bt_ctf_event_common_create_fields(
118 struct bt_ctf_stream_class_common *stream_class,
119 struct bt_ctf_validation_output *validation_output,
51804836
SM
120 create_field_func_type create_field_func,
121 release_field_func_type release_field_func,
122 create_header_field_func_type create_header_field_func,
123 release_header_field_func_type release_header_field_func,
16ca5ff0
PP
124 struct bt_ctf_field_wrapper **header_field,
125 struct bt_ctf_field_common **stream_event_context_field,
126 struct bt_ctf_field_common **context_field,
127 struct bt_ctf_field_common **payload_field)
128{
129 int ret = 0;
130
131 if (validation_output->event_header_type) {
132 BT_LOGD("Creating initial event header field: ft-addr=%p",
133 validation_output->event_header_type);
134 *header_field =
135 create_header_field_func(stream_class,
136 validation_output->event_header_type);
137 if (!*header_field) {
138 BT_LOGE_STR("Cannot create initial event header field object.");
139 goto error;
140 }
141 }
142
143 if (validation_output->stream_event_ctx_type) {
144 BT_LOGD("Creating initial stream event context field: ft-addr=%p",
145 validation_output->stream_event_ctx_type);
146 *stream_event_context_field = create_field_func(
147 validation_output->stream_event_ctx_type);
148 if (!*stream_event_context_field) {
149 BT_LOGE_STR("Cannot create initial stream event context field object.");
150 goto error;
151 }
152 }
153
154 if (validation_output->event_context_type) {
155 BT_LOGD("Creating initial event context field: ft-addr=%p",
156 validation_output->event_context_type);
157 *context_field = create_field_func(
158 validation_output->event_context_type);
159 if (!*context_field) {
160 BT_LOGE_STR("Cannot create initial event context field object.");
161 goto error;
162 }
163 }
164
165 if (validation_output->event_payload_type) {
166 BT_LOGD("Creating initial event payload field: ft-addr=%p",
167 validation_output->event_payload_type);
168 *payload_field = create_field_func(
169 validation_output->event_payload_type);
170 if (!*payload_field) {
171 BT_LOGE_STR("Cannot create initial event payload field object.");
172 goto error;
173 }
174 }
175
176 goto end;
177
178error:
179 if (*header_field) {
180 release_header_field_func(*header_field, stream_class);
181 }
182
183 if (*stream_event_context_field) {
184 release_field_func(*stream_event_context_field);
185 }
186
187 if (*context_field) {
188 release_field_func(*context_field);
189 }
190
191 if (*payload_field) {
192 release_field_func(*payload_field);
193 }
194
195 ret = -1;
196
197end:
198 return ret;
199}
200
201BT_HIDDEN
202int _bt_ctf_event_common_validate(struct bt_ctf_event_common *event)
203{
204 int ret = 0;
205 struct bt_ctf_stream_class_common *stream_class;
206
98b15851 207 BT_ASSERT_DBG(event);
16ca5ff0
PP
208 if (event->header_field) {
209 ret = bt_ctf_field_common_validate_recursive(
210 event->header_field->field);
211 if (ret) {
67d2ce02 212 BT_CTF_ASSERT_PRE_MSG("Invalid event's header field: "
16ca5ff0
PP
213 "event-addr=%p, field-addr=%p",
214 event, event->header_field->field);
215 goto end;
216 }
217 }
218
219 stream_class = bt_ctf_event_class_common_borrow_stream_class(event->class);
220
221 /*
222 * We should not have been able to create the event without associating
223 * the event class to a stream class.
224 */
98b15851 225 BT_ASSERT_DBG(stream_class);
16ca5ff0
PP
226
227 if (stream_class->event_context_field_type) {
228 ret = bt_ctf_field_common_validate_recursive(
229 event->stream_event_context_field);
230 if (ret) {
67d2ce02 231 BT_CTF_ASSERT_PRE_MSG("Invalid event's stream event context field: "
16ca5ff0
PP
232 "event-addr=%p, field-addr=%p",
233 event, event->stream_event_context_field);
234 goto end;
235 }
236 }
237
238 if (event->class->context_field_type) {
239 ret = bt_ctf_field_common_validate_recursive(event->context_field);
240 if (ret) {
67d2ce02 241 BT_CTF_ASSERT_PRE_MSG("Invalid event's payload field: "
16ca5ff0
PP
242 "event-addr=%p, field-addr=%p",
243 event, event->context_field);
244 goto end;
245 }
246 }
247
248 ret = bt_ctf_field_common_validate_recursive(event->payload_field);
249 if (ret) {
67d2ce02 250 BT_CTF_ASSERT_PRE_MSG("Invalid event's payload field: "
16ca5ff0
PP
251 "event-addr=%p, field-addr=%p",
252 event, event->payload_field);
253 goto end;
254 }
255
256end:
257 return ret;
258}
259
260BT_HIDDEN
261void _bt_ctf_event_common_set_is_frozen(struct bt_ctf_event_common *event,
262 bool is_frozen)
263{
98b15851 264 BT_ASSERT_DBG(event);
16ca5ff0
PP
265 BT_LOGD("Freezing event: addr=%p, "
266 "event-class-name=\"%s\", event-class-id=%" PRId64,
267 event, bt_ctf_event_class_common_get_name(event->class),
268 bt_ctf_event_class_common_get_id(event->class));
269
270 if (event->header_field) {
271 BT_LOGD_STR("Freezing event's header field.");
272 bt_ctf_field_common_set_is_frozen_recursive(
273 event->header_field->field, is_frozen);
274 }
275
276 if (event->stream_event_context_field) {
277 BT_LOGD_STR("Freezing event's stream event context field.");
278 bt_ctf_field_common_set_is_frozen_recursive(
279 event->stream_event_context_field, is_frozen);
280 }
281
282 if (event->context_field) {
283 BT_LOGD_STR("Freezing event's context field.");
284 bt_ctf_field_common_set_is_frozen_recursive(event->context_field,
285 is_frozen);
286 }
287
288 if (event->payload_field) {
289 BT_LOGD_STR("Freezing event's payload field.");
290 bt_ctf_field_common_set_is_frozen_recursive(event->payload_field,
291 is_frozen);
292 }
293
294 event->frozen = is_frozen;
295}
296
297BT_HIDDEN
298int bt_ctf_event_common_initialize(struct bt_ctf_event_common *event,
299 struct bt_ctf_event_class_common *event_class,
300 struct bt_ctf_clock_class *init_expected_clock_class,
e1e02a22 301 bool is_shared_with_parent, bt_ctf_object_release_func release_func,
16ca5ff0
PP
302 bt_ctf_validation_flag_copy_field_type_func field_type_copy_func,
303 bool must_be_in_trace,
304 int (*map_clock_classes_func)(struct bt_ctf_stream_class_common *stream_class,
305 struct bt_ctf_field_type_common *packet_context_field_type,
306 struct bt_ctf_field_type_common *event_header_field_type),
51804836
SM
307 create_field_func_type create_field_func,
308 release_field_func_type release_field_func,
309 create_header_field_func_type create_header_field_func,
310 release_header_field_func_type release_header_field_func)
16ca5ff0
PP
311{
312 int ret;
313 struct bt_ctf_trace_common *trace = NULL;
314 struct bt_ctf_stream_class_common *stream_class = NULL;
315 struct bt_ctf_field_wrapper *event_header = NULL;
316 struct bt_ctf_field_common *stream_event_context = NULL;
317 struct bt_ctf_field_common *event_context = NULL;
318 struct bt_ctf_field_common *event_payload = NULL;
319 struct bt_ctf_validation_output validation_output = { 0 };
320 struct bt_ctf_clock_class *expected_clock_class =
e1e02a22 321 init_expected_clock_class ? bt_ctf_object_get_ref(init_expected_clock_class) :
16ca5ff0
PP
322 NULL;
323
67d2ce02 324 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
16ca5ff0
PP
325 BT_LOGD("Initializing common event object: event-class-addr=%p, "
326 "event-class-name=\"%s\", event-class-id=%" PRId64,
327 event_class, bt_ctf_event_class_common_get_name(event_class),
328 bt_ctf_event_class_common_get_id(event_class));
329
330 stream_class = bt_ctf_event_class_common_borrow_stream_class(event_class);
67d2ce02 331 BT_CTF_ASSERT_PRE(stream_class,
16ca5ff0
PP
332 "Event class is not part of a stream class: event-class-addr=%p",
333 event_class);
334
335 /* The event class was frozen when added to its stream class */
98b15851 336 BT_ASSERT_DBG(event_class->frozen);
16ca5ff0
PP
337 trace = bt_ctf_stream_class_common_borrow_trace(stream_class);
338
339 if (must_be_in_trace) {
67d2ce02 340 BT_CTF_ASSERT_PRE(trace,
16ca5ff0
PP
341 "Event class's stream class is not part of a trace: "
342 "ec-addr=%p, sc-addr=%p", event_class, stream_class);
343 }
344
345 /*
346 * This must be called before anything that can fail because on
347 * failure, the caller releases the reference to `event` to
348 * destroy it.
349 */
350 if (is_shared_with_parent) {
e1e02a22 351 bt_ctf_object_init_shared_with_parent(&event->base, release_func);
16ca5ff0 352 } else {
e1e02a22 353 bt_ctf_object_init_unique(&event->base);
16ca5ff0
PP
354 }
355
356 if (!stream_class->frozen) {
357 /*
358 * Because this function freezes the stream class,
359 * validate that this stream class contains at most a
360 * single clock class so that we set its expected clock
361 * class for future checks.
362 */
363 ret = bt_ctf_stream_class_common_validate_single_clock_class(
364 stream_class, &expected_clock_class);
365 if (ret) {
366 BT_LOGW("Event class's stream class or one of its event "
367 "classes contains a field type which is not "
368 "recursively mapped to the expected "
369 "clock class: "
370 "stream-class-addr=%p, "
371 "stream-class-id=%" PRId64 ", "
372 "stream-class-name=\"%s\", "
373 "expected-clock-class-addr=%p, "
374 "expected-clock-class-name=\"%s\"",
375 stream_class,
376 bt_ctf_stream_class_common_get_id(stream_class),
377 bt_ctf_stream_class_common_get_name(stream_class),
378 expected_clock_class,
379 expected_clock_class ?
380 bt_ctf_clock_class_get_name(expected_clock_class) :
381 NULL);
382 goto error;
383 }
384 }
385
386 /* Validate the trace, the stream class, and the event class */
387 ret = bt_ctf_event_common_validate_types_for_create(
388 event_class, &validation_output, field_type_copy_func);
389 if (ret) {
390 /* bt_ctf_event_common_validate_types_for_create() logs errors */
391 goto error;
392 }
393
394 if (map_clock_classes_func) {
395 /*
396 * Safe to automatically map selected fields to the
397 * stream's clock's class here because the stream class
398 * is about to be frozen.
399 */
400 if (map_clock_classes_func(stream_class,
401 validation_output.packet_context_type,
402 validation_output.event_header_type)) {
403 BT_LOGW_STR("Cannot automatically map selected stream class's "
404 "field types to stream class's clock's class.");
405 goto error;
406 }
407 }
408
409 /*
410 * event does not share a common ancestor with the event class; it has
411 * to guarantee its existence by holding a reference. This reference
412 * shall be released once the event is associated to a stream since,
413 * from that point, the event and its class will share the same
414 * lifetime.
415 */
e1e02a22 416 event->class = bt_ctf_object_get_ref(event_class);
16ca5ff0
PP
417
418 ret = bt_ctf_event_common_create_fields(stream_class,
419 &validation_output,
420 create_field_func, release_field_func,
421 create_header_field_func, release_header_field_func,
422 &event_header, &stream_event_context, &event_context,
423 &event_payload);
424 if (ret) {
425 /* bt_ctf_event_common_create_fields() logs errors */
426 goto error;
427 }
428
429 /*
430 * At this point all the fields are created, potentially from
431 * validated copies of field types, so that the field types and
432 * fields can be replaced in the trace, stream class,
433 * event class, and created event.
434 */
435 bt_ctf_validation_replace_types(trace, stream_class, event_class,
436 &validation_output,
437 BT_CTF_VALIDATION_FLAG_STREAM | BT_CTF_VALIDATION_FLAG_EVENT);
438 event->header_field = event_header;
439 event_header = NULL;
440 event->stream_event_context_field = stream_event_context;
441 stream_event_context = NULL;
442 event->context_field = event_context;
443 event_context = NULL;
444 event->payload_field = event_payload;
445 event_payload = NULL;
446
447 /*
448 * Put what was not moved in bt_ctf_validation_replace_types().
449 */
450 bt_ctf_validation_output_put_types(&validation_output);
451
452 /*
453 * Freeze the stream class since the event header must not be changed
454 * anymore.
455 */
456 bt_ctf_stream_class_common_freeze(stream_class);
457
458 /*
459 * It is safe to set the stream class's unique clock class
460 * now because the stream class is frozen.
461 */
462 if (expected_clock_class) {
e1e02a22 463 BT_CTF_OBJECT_MOVE_REF(stream_class->clock_class, expected_clock_class);
16ca5ff0
PP
464 }
465
466 /*
467 * Mark stream class, and event class as valid since
468 * they're all frozen now.
469 */
470 stream_class->valid = 1;
471 event_class->valid = 1;
472
473 /* Put stuff we borrowed from the event class */
474 BT_LOGD("Initialized event object: addr=%p, event-class-name=\"%s\", "
475 "event-class-id=%" PRId64,
476 event, bt_ctf_event_class_common_get_name(event->class),
477 bt_ctf_event_class_common_get_id(event->class));
478 goto end;
479
480error:
481 bt_ctf_validation_output_put_types(&validation_output);
e1e02a22 482 bt_ctf_object_put_ref(expected_clock_class);
16ca5ff0
PP
483
484 if (event_header) {
485 release_header_field_func(event_header, stream_class);
486 }
487
488 if (stream_event_context) {
489 release_field_func(stream_event_context);
490 }
491
492 if (event_context) {
493 release_field_func(event_context);
494 }
495
496 if (event_payload) {
497 release_field_func(event_payload);
498 }
499
500 ret = -1;
501
502end:
503 return ret;
504}
505
7c7301d5 506static
16ca5ff0
PP
507int map_clock_classes_func(struct bt_ctf_stream_class_common *stream_class,
508 struct bt_ctf_field_type_common *packet_context_type,
509 struct bt_ctf_field_type_common *event_header_type)
3dca2276
PP
510{
511 int ret = bt_ctf_stream_class_map_clock_class(
16ca5ff0
PP
512 BT_CTF_FROM_COMMON(stream_class),
513 BT_CTF_FROM_COMMON(packet_context_type),
514 BT_CTF_FROM_COMMON(event_header_type));
3dca2276
PP
515
516 if (ret) {
517 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
518 }
519
520 return ret;
521}
522
312c056a 523static
9c7f2f85
SM
524void destroy_event_header_field(struct bt_ctf_field_wrapper *field_wrapper,
525 struct bt_ctf_stream_class *stream_class)
312c056a 526{
98b15851 527 BT_ASSERT_DBG(field_wrapper);
e1e02a22 528 bt_ctf_object_put_ref(field_wrapper->field);
16ca5ff0 529 bt_ctf_field_wrapper_destroy(field_wrapper);
312c056a
PP
530}
531
532static
16ca5ff0
PP
533struct bt_ctf_field_wrapper *create_event_header_field(
534 struct bt_ctf_stream_class *stream_class,
535 struct bt_ctf_field_type_common *ft)
312c056a 536{
16ca5ff0 537 struct bt_ctf_field_wrapper *field_wrapper = NULL;
312c056a
PP
538 struct bt_ctf_field *field = bt_ctf_field_create((void *) ft);
539
540 if (!field) {
541 goto error;
542 }
543
16ca5ff0 544 field_wrapper = bt_ctf_field_wrapper_new(NULL);
312c056a
PP
545 if (!field_wrapper) {
546 goto error;
547 }
548
549 field_wrapper->field = (void *) field;
550 field = NULL;
551 goto end;
552
553error:
e1e02a22 554 bt_ctf_object_put_ref(field);
312c056a
PP
555
556 if (field_wrapper) {
9c7f2f85 557 destroy_event_header_field(field_wrapper, stream_class);
312c056a
PP
558 field_wrapper = NULL;
559 }
560
561end:
562 return field_wrapper;
563}
564
16ca5ff0
PP
565static
566void release_event_header_field(struct bt_ctf_field_wrapper *field_wrapper,
567 struct bt_ctf_event_common *event_common)
568{
98b15851 569 BT_ASSERT_DBG(field_wrapper);
e1e02a22 570 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_wrapper->field);
16ca5ff0
PP
571 bt_ctf_field_wrapper_destroy(field_wrapper);
572}
573
574static
e1e02a22 575void bt_ctf_event_destroy(struct bt_ctf_object *obj)
16ca5ff0 576{
e1e02a22 577 bt_ctf_event_common_finalize(obj, (void *) bt_ctf_object_put_ref,
16ca5ff0
PP
578 (void *) release_event_header_field);
579 g_free(obj);
580}
581
3dca2276
PP
582struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
583{
584 int ret;
585 struct bt_ctf_event *event = NULL;
16ca5ff0 586 struct bt_ctf_clock_class *expected_clock_class = NULL;
3dca2276
PP
587
588 event = g_new0(struct bt_ctf_event, 1);
589 if (!event) {
590 BT_LOGE_STR("Failed to allocate one CTF writer event.");
591 goto error;
592 }
593
594 if (event_class) {
595 struct bt_ctf_stream_class *stream_class =
16ca5ff0
PP
596 BT_CTF_FROM_COMMON(bt_ctf_event_class_common_borrow_stream_class(
597 BT_CTF_TO_COMMON(event_class)));
3dca2276
PP
598
599 if (stream_class && stream_class->clock) {
16ca5ff0 600 expected_clock_class = stream_class->clock->clock_class;
3dca2276
PP
601 }
602 }
603
16ca5ff0
PP
604 ret = bt_ctf_event_common_initialize(BT_CTF_TO_COMMON(event),
605 BT_CTF_TO_COMMON(event_class), expected_clock_class,
3fea54f6 606 true, bt_ctf_event_destroy,
16ca5ff0 607 (bt_ctf_validation_flag_copy_field_type_func)
3dca2276
PP
608 bt_ctf_field_type_copy,
609 false, map_clock_classes_func,
51804836
SM
610 (create_field_func_type) bt_ctf_field_create,
611 (release_field_func_type) bt_ctf_object_put_ref,
612 (create_header_field_func_type) create_event_header_field,
613 (release_header_field_func_type) destroy_event_header_field);
3dca2276 614 if (ret) {
16ca5ff0 615 /* bt_ctf_event_common_initialize() logs errors */
3dca2276
PP
616 goto error;
617 }
618
619 goto end;
620
621error:
e1e02a22 622 BT_CTF_OBJECT_PUT_REF_AND_RESET(event);
3dca2276
PP
623
624end:
625 return event;
626}
627
628struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event)
629{
67d2ce02 630 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
e1e02a22 631 return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event)));
3dca2276
PP
632}
633
7c7301d5 634static
3dca2276
PP
635struct bt_ctf_stream *bt_ctf_event_borrow_stream(struct bt_ctf_event *event)
636{
98b15851 637 BT_ASSERT_DBG(event);
3dca2276 638 return (struct bt_ctf_stream *)
e1e02a22 639 bt_ctf_object_borrow_parent(&BT_CTF_TO_COMMON(event)->base);
3dca2276
PP
640}
641
642struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event)
643{
67d2ce02 644 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
e1e02a22 645 return bt_ctf_object_get_ref(bt_ctf_event_borrow_stream(event));
3dca2276
PP
646}
647
648int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name,
649 struct bt_ctf_field *field)
650{
67d2ce02
MJ
651 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
652 BT_CTF_ASSERT_PRE_NON_NULL(field, "Payload field");
653 BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event");
3dca2276 654 return bt_ctf_field_structure_set_field_by_name(
312c056a 655 (void *) event->common.payload_field, name, field);
3dca2276
PP
656}
657
658struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event,
659 const char *name)
660{
661 struct bt_ctf_field *field = NULL;
662
67d2ce02 663 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
3dca2276
PP
664
665 if (name) {
666 field = bt_ctf_field_structure_get_field_by_name(
16ca5ff0 667 BT_CTF_FROM_COMMON(event->common.payload_field), name);
3dca2276 668 } else {
16ca5ff0 669 field = BT_CTF_FROM_COMMON(event->common.payload_field);
e1e02a22 670 bt_ctf_object_get_ref(field);
3dca2276
PP
671 }
672
673 return field;
674}
675
676struct bt_ctf_field *bt_ctf_event_get_payload_field(
677 struct bt_ctf_event *event)
678{
e1e02a22 679 return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_payload(BT_CTF_TO_COMMON(event)));
3dca2276
PP
680}
681
3dca2276
PP
682struct bt_ctf_field *bt_ctf_event_get_header(struct bt_ctf_event *event)
683{
e1e02a22 684 return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_header(BT_CTF_TO_COMMON(event)));
3dca2276
PP
685}
686
3dca2276
PP
687struct bt_ctf_field *bt_ctf_event_get_context(struct bt_ctf_event *event)
688{
e1e02a22 689 return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_context(BT_CTF_TO_COMMON(event)));
3dca2276
PP
690}
691
3dca2276
PP
692struct bt_ctf_field *bt_ctf_event_get_stream_event_context(
693 struct bt_ctf_event *event)
694{
e1e02a22 695 return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_stream_event_context(
16ca5ff0 696 BT_CTF_TO_COMMON(event)));
3dca2276
PP
697}
698
699BT_HIDDEN
700int bt_ctf_event_serialize(struct bt_ctf_event *event,
013f35c6 701 struct bt_ctfser *ctfser,
3dca2276
PP
702 enum bt_ctf_byte_order native_byte_order)
703{
704 int ret = 0;
705
98b15851
PP
706 BT_ASSERT_DBG(event);
707 BT_ASSERT_DBG(ctfser);
3dca2276 708
ef267d12 709 BT_LOGT_STR("Serializing event's context field.");
3dca2276
PP
710 if (event->common.context_field) {
711 ret = bt_ctf_field_serialize_recursive(
013f35c6 712 (void *) event->common.context_field, ctfser,
3dca2276
PP
713 native_byte_order);
714 if (ret) {
715 BT_LOGW("Cannot serialize event's context field: "
716 "event-addr=%p, event-class-name=\"%s\", "
717 "event-class-id=%" PRId64,
718 event,
16ca5ff0
PP
719 bt_ctf_event_class_common_get_name(event->common.class),
720 bt_ctf_event_class_common_get_id(event->common.class));
3dca2276
PP
721 goto end;
722 }
723 }
724
ef267d12 725 BT_LOGT_STR("Serializing event's payload field.");
3dca2276
PP
726 if (event->common.payload_field) {
727 ret = bt_ctf_field_serialize_recursive(
013f35c6 728 (void *) event->common.payload_field, ctfser,
3dca2276
PP
729 native_byte_order);
730 if (ret) {
731 BT_LOGW("Cannot serialize event's payload field: "
732 "event-addr=%p, event-class-name=\"%s\", "
733 "event-class-id=%" PRId64,
734 event,
16ca5ff0
PP
735 bt_ctf_event_class_common_get_name(event->common.class),
736 bt_ctf_event_class_common_get_id(event->common.class));
3dca2276
PP
737 goto end;
738 }
739 }
740
741end:
742 return ret;
743}
744
312c056a
PP
745int bt_ctf_event_set_header(struct bt_ctf_event *event,
746 struct bt_ctf_field *header)
747{
67d2ce02
MJ
748 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
749 BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event");
312c056a
PP
750
751 /*
752 * Ensure the provided header's type matches the one registered to the
753 * stream class.
754 */
755 if (header) {
67d2ce02 756 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
16ca5ff0
PP
757 ((struct bt_ctf_field_common *) header)->type,
758 bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type) == 0,
312c056a 759 "Header field's type is different from the "
16ca5ff0
PP
760 "expected field type: event-addr=%p, ft-addr=%p, "
761 "expected-ft-addr=%p",
762 event, ((struct bt_ctf_field_common *) header)->type,
763 bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type);
312c056a 764 } else {
67d2ce02 765 BT_CTF_ASSERT_PRE(!bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type,
312c056a 766 "Setting no event header field, "
67d2ce02 767 "but event header field type is not NULL: "
16ca5ff0 768 "event-addr=%p, header-ft-addr=%p",
312c056a 769 event,
16ca5ff0 770 bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type);
312c056a
PP
771 }
772
e1e02a22
PP
773 bt_ctf_object_put_ref(event->common.header_field->field);
774 event->common.header_field->field = bt_ctf_object_get_ref(header);
ef267d12 775 BT_LOGT("Set event's header field: event-addr=%p, "
312c056a
PP
776 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
777 "header-field-addr=%p",
16ca5ff0
PP
778 event, bt_ctf_event_class_common_get_name(event->common.class),
779 bt_ctf_event_class_common_get_id(event->common.class), header);
312c056a
PP
780 return 0;
781}
782
783int bt_ctf_event_common_set_payload(struct bt_ctf_event *event,
784 struct bt_ctf_field *payload)
785{
67d2ce02
MJ
786 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
787 BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event");
312c056a
PP
788
789 if (payload) {
67d2ce02 790 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
16ca5ff0 791 ((struct bt_ctf_field_common *) payload)->type,
312c056a
PP
792 event->common.class->payload_field_type) == 0,
793 "Payload field's type is different from the "
16ca5ff0
PP
794 "expected field type: event-addr=%p, ft-addr=%p, "
795 "expected-ft-addr=%p",
312c056a 796 event,
16ca5ff0 797 ((struct bt_ctf_field_common *) payload)->type,
312c056a
PP
798 event->common.class->payload_field_type);
799 } else {
67d2ce02 800 BT_CTF_ASSERT_PRE(!event->common.class->payload_field_type,
312c056a 801 "Setting no event payload field, "
67d2ce02 802 "but event payload field type is not NULL: "
16ca5ff0 803 "event-addr=%p, payload-ft-addr=%p",
312c056a
PP
804 event, event->common.class->payload_field_type);
805 }
806
e1e02a22
PP
807 bt_ctf_object_put_ref(event->common.payload_field);
808 event->common.payload_field = bt_ctf_object_get_ref(payload);
ef267d12 809 BT_LOGT("Set event's payload field: event-addr=%p, "
312c056a
PP
810 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
811 "payload-field-addr=%p",
16ca5ff0
PP
812 event, bt_ctf_event_class_common_get_name(event->common.class),
813 bt_ctf_event_class_common_get_id(event->common.class), payload);
312c056a
PP
814 return 0;
815}
816
817int bt_ctf_event_set_context(struct bt_ctf_event *event,
818 struct bt_ctf_field *context)
819{
67d2ce02
MJ
820 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
821 BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event");
312c056a
PP
822
823 if (context) {
67d2ce02 824 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
16ca5ff0 825 ((struct bt_ctf_field_common *) context)->type,
312c056a
PP
826 event->common.class->context_field_type) == 0,
827 "Context field's type is different from the "
16ca5ff0
PP
828 "expected field type: event-addr=%p, ft-addr=%p, "
829 "expected-ft-addr=%p",
830 event, ((struct bt_ctf_field_common *) context)->type,
312c056a
PP
831 event->common.class->context_field_type);
832 } else {
67d2ce02 833 BT_CTF_ASSERT_PRE(!event->common.class->context_field_type,
312c056a 834 "Setting no event context field, "
67d2ce02 835 "but event context field type is not NULL: "
16ca5ff0 836 "event-addr=%p, context-ft-addr=%p",
312c056a
PP
837 event, event->common.class->context_field_type);
838 }
839
e1e02a22
PP
840 bt_ctf_object_put_ref(event->common.context_field);
841 event->common.context_field = bt_ctf_object_get_ref(context);
ef267d12 842 BT_LOGT("Set event's context field: event-addr=%p, "
312c056a
PP
843 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
844 "context-field-addr=%p",
16ca5ff0
PP
845 event, bt_ctf_event_class_common_get_name(event->common.class),
846 bt_ctf_event_class_common_get_id(event->common.class), context);
312c056a
PP
847 return 0;
848}
849
850int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event,
851 struct bt_ctf_field *stream_event_context)
852{
67d2ce02
MJ
853 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
854 BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event");
312c056a
PP
855
856 if (stream_event_context) {
67d2ce02 857 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
16ca5ff0
PP
858 ((struct bt_ctf_field_common *) stream_event_context)->type,
859 bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type) == 0,
312c056a 860 "Stream event context field's type is different from the "
16ca5ff0
PP
861 "expected field type: event-addr=%p, ft-addr=%p, "
862 "expected-ft-addr=%p",
312c056a 863 event,
16ca5ff0
PP
864 ((struct bt_ctf_field_common *) stream_event_context)->type,
865 bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type);
312c056a 866 } else {
67d2ce02 867 BT_CTF_ASSERT_PRE(!bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type,
312c056a 868 "Setting no stream event context field, "
67d2ce02 869 "but stream event context field type is not NULL: "
16ca5ff0 870 "event-addr=%p, context-ft-addr=%p",
312c056a 871 event,
16ca5ff0 872 bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type);
312c056a
PP
873 }
874
e1e02a22
PP
875 bt_ctf_object_put_ref(event->common.stream_event_context_field);
876 event->common.stream_event_context_field = bt_ctf_object_get_ref(stream_event_context);
ef267d12 877 BT_LOGT("Set event's stream event context field: event-addr=%p, "
312c056a
PP
878 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
879 "stream-event-context-field-addr=%p",
16ca5ff0
PP
880 event, bt_ctf_event_class_common_get_name(event->common.class),
881 bt_ctf_event_class_common_get_id(event->common.class),
312c056a
PP
882 stream_event_context);
883 return 0;
884}
This page took 0.128539 seconds and 4 git commands to generate.