Move to kernel style SPDX license identifiers
[babeltrace.git] / src / ctf-writer / event.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"
9 #include "logging.h"
10
11 #include <inttypes.h>
12 #include <stdbool.h>
13
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>
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"
37
38 static
39 int 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;
57 struct bt_ctf_private_value *environment = NULL;
58
59 stream_class = bt_ctf_event_class_common_borrow_stream_class(event_class);
60 BT_ASSERT_DBG(stream_class);
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;
67 BT_ASSERT_DBG(trace_valid);
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
108 error:
109 bt_ctf_validation_output_put_types(validation_output);
110 ret = -1;
111
112 end:
113 return ret;
114 }
115
116 static
117 int bt_ctf_event_common_create_fields(
118 struct bt_ctf_stream_class_common *stream_class,
119 struct bt_ctf_validation_output *validation_output,
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,
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
178 error:
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
197 end:
198 return ret;
199 }
200
201 BT_HIDDEN
202 int _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
207 BT_ASSERT_DBG(event);
208 if (event->header_field) {
209 ret = bt_ctf_field_common_validate_recursive(
210 event->header_field->field);
211 if (ret) {
212 BT_CTF_ASSERT_PRE_MSG("Invalid event's header field: "
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 */
225 BT_ASSERT_DBG(stream_class);
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) {
231 BT_CTF_ASSERT_PRE_MSG("Invalid event's stream event context field: "
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) {
241 BT_CTF_ASSERT_PRE_MSG("Invalid event's payload field: "
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) {
250 BT_CTF_ASSERT_PRE_MSG("Invalid event's payload field: "
251 "event-addr=%p, field-addr=%p",
252 event, event->payload_field);
253 goto end;
254 }
255
256 end:
257 return ret;
258 }
259
260 BT_HIDDEN
261 void _bt_ctf_event_common_set_is_frozen(struct bt_ctf_event_common *event,
262 bool is_frozen)
263 {
264 BT_ASSERT_DBG(event);
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
297 BT_HIDDEN
298 int 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,
301 bool is_shared_with_parent, bt_ctf_object_release_func release_func,
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),
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)
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 =
321 init_expected_clock_class ? bt_ctf_object_get_ref(init_expected_clock_class) :
322 NULL;
323
324 BT_CTF_ASSERT_PRE_NON_NULL(event_class, "Event class");
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);
331 BT_CTF_ASSERT_PRE(stream_class,
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 */
336 BT_ASSERT_DBG(event_class->frozen);
337 trace = bt_ctf_stream_class_common_borrow_trace(stream_class);
338
339 if (must_be_in_trace) {
340 BT_CTF_ASSERT_PRE(trace,
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) {
351 bt_ctf_object_init_shared_with_parent(&event->base, release_func);
352 } else {
353 bt_ctf_object_init_unique(&event->base);
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 */
416 event->class = bt_ctf_object_get_ref(event_class);
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) {
463 BT_CTF_OBJECT_MOVE_REF(stream_class->clock_class, expected_clock_class);
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
480 error:
481 bt_ctf_validation_output_put_types(&validation_output);
482 bt_ctf_object_put_ref(expected_clock_class);
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
502 end:
503 return ret;
504 }
505
506 static
507 int 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)
510 {
511 int ret = bt_ctf_stream_class_map_clock_class(
512 BT_CTF_FROM_COMMON(stream_class),
513 BT_CTF_FROM_COMMON(packet_context_type),
514 BT_CTF_FROM_COMMON(event_header_type));
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
523 static
524 void destroy_event_header_field(struct bt_ctf_field_wrapper *field_wrapper,
525 struct bt_ctf_stream_class *stream_class)
526 {
527 BT_ASSERT_DBG(field_wrapper);
528 bt_ctf_object_put_ref(field_wrapper->field);
529 bt_ctf_field_wrapper_destroy(field_wrapper);
530 }
531
532 static
533 struct bt_ctf_field_wrapper *create_event_header_field(
534 struct bt_ctf_stream_class *stream_class,
535 struct bt_ctf_field_type_common *ft)
536 {
537 struct bt_ctf_field_wrapper *field_wrapper = NULL;
538 struct bt_ctf_field *field = bt_ctf_field_create((void *) ft);
539
540 if (!field) {
541 goto error;
542 }
543
544 field_wrapper = bt_ctf_field_wrapper_new(NULL);
545 if (!field_wrapper) {
546 goto error;
547 }
548
549 field_wrapper->field = (void *) field;
550 field = NULL;
551 goto end;
552
553 error:
554 bt_ctf_object_put_ref(field);
555
556 if (field_wrapper) {
557 destroy_event_header_field(field_wrapper, stream_class);
558 field_wrapper = NULL;
559 }
560
561 end:
562 return field_wrapper;
563 }
564
565 static
566 void release_event_header_field(struct bt_ctf_field_wrapper *field_wrapper,
567 struct bt_ctf_event_common *event_common)
568 {
569 BT_ASSERT_DBG(field_wrapper);
570 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_wrapper->field);
571 bt_ctf_field_wrapper_destroy(field_wrapper);
572 }
573
574 static
575 void bt_ctf_event_destroy(struct bt_ctf_object *obj)
576 {
577 bt_ctf_event_common_finalize(obj, (void *) bt_ctf_object_put_ref,
578 (void *) release_event_header_field);
579 g_free(obj);
580 }
581
582 struct 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;
586 struct bt_ctf_clock_class *expected_clock_class = NULL;
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 =
596 BT_CTF_FROM_COMMON(bt_ctf_event_class_common_borrow_stream_class(
597 BT_CTF_TO_COMMON(event_class)));
598
599 if (stream_class && stream_class->clock) {
600 expected_clock_class = stream_class->clock->clock_class;
601 }
602 }
603
604 ret = bt_ctf_event_common_initialize(BT_CTF_TO_COMMON(event),
605 BT_CTF_TO_COMMON(event_class), expected_clock_class,
606 true, bt_ctf_event_destroy,
607 (bt_ctf_validation_flag_copy_field_type_func)
608 bt_ctf_field_type_copy,
609 false, map_clock_classes_func,
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);
614 if (ret) {
615 /* bt_ctf_event_common_initialize() logs errors */
616 goto error;
617 }
618
619 goto end;
620
621 error:
622 BT_CTF_OBJECT_PUT_REF_AND_RESET(event);
623
624 end:
625 return event;
626 }
627
628 struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event)
629 {
630 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
631 return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event)));
632 }
633
634 static
635 struct bt_ctf_stream *bt_ctf_event_borrow_stream(struct bt_ctf_event *event)
636 {
637 BT_ASSERT_DBG(event);
638 return (struct bt_ctf_stream *)
639 bt_ctf_object_borrow_parent(&BT_CTF_TO_COMMON(event)->base);
640 }
641
642 struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event)
643 {
644 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
645 return bt_ctf_object_get_ref(bt_ctf_event_borrow_stream(event));
646 }
647
648 int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name,
649 struct bt_ctf_field *field)
650 {
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");
654 return bt_ctf_field_structure_set_field_by_name(
655 (void *) event->common.payload_field, name, field);
656 }
657
658 struct 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
663 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
664
665 if (name) {
666 field = bt_ctf_field_structure_get_field_by_name(
667 BT_CTF_FROM_COMMON(event->common.payload_field), name);
668 } else {
669 field = BT_CTF_FROM_COMMON(event->common.payload_field);
670 bt_ctf_object_get_ref(field);
671 }
672
673 return field;
674 }
675
676 struct bt_ctf_field *bt_ctf_event_get_payload_field(
677 struct bt_ctf_event *event)
678 {
679 return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_payload(BT_CTF_TO_COMMON(event)));
680 }
681
682 struct bt_ctf_field *bt_ctf_event_get_header(struct bt_ctf_event *event)
683 {
684 return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_header(BT_CTF_TO_COMMON(event)));
685 }
686
687 struct bt_ctf_field *bt_ctf_event_get_context(struct bt_ctf_event *event)
688 {
689 return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_context(BT_CTF_TO_COMMON(event)));
690 }
691
692 struct bt_ctf_field *bt_ctf_event_get_stream_event_context(
693 struct bt_ctf_event *event)
694 {
695 return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_stream_event_context(
696 BT_CTF_TO_COMMON(event)));
697 }
698
699 BT_HIDDEN
700 int bt_ctf_event_serialize(struct bt_ctf_event *event,
701 struct bt_ctfser *ctfser,
702 enum bt_ctf_byte_order native_byte_order)
703 {
704 int ret = 0;
705
706 BT_ASSERT_DBG(event);
707 BT_ASSERT_DBG(ctfser);
708
709 BT_LOGT_STR("Serializing event's context field.");
710 if (event->common.context_field) {
711 ret = bt_ctf_field_serialize_recursive(
712 (void *) event->common.context_field, ctfser,
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,
719 bt_ctf_event_class_common_get_name(event->common.class),
720 bt_ctf_event_class_common_get_id(event->common.class));
721 goto end;
722 }
723 }
724
725 BT_LOGT_STR("Serializing event's payload field.");
726 if (event->common.payload_field) {
727 ret = bt_ctf_field_serialize_recursive(
728 (void *) event->common.payload_field, ctfser,
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,
735 bt_ctf_event_class_common_get_name(event->common.class),
736 bt_ctf_event_class_common_get_id(event->common.class));
737 goto end;
738 }
739 }
740
741 end:
742 return ret;
743 }
744
745 int bt_ctf_event_set_header(struct bt_ctf_event *event,
746 struct bt_ctf_field *header)
747 {
748 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
749 BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event");
750
751 /*
752 * Ensure the provided header's type matches the one registered to the
753 * stream class.
754 */
755 if (header) {
756 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
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,
759 "Header field's type is different from the "
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);
764 } else {
765 BT_CTF_ASSERT_PRE(!bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type,
766 "Setting no event header field, "
767 "but event header field type is not NULL: "
768 "event-addr=%p, header-ft-addr=%p",
769 event,
770 bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type);
771 }
772
773 bt_ctf_object_put_ref(event->common.header_field->field);
774 event->common.header_field->field = bt_ctf_object_get_ref(header);
775 BT_LOGT("Set event's header field: event-addr=%p, "
776 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
777 "header-field-addr=%p",
778 event, bt_ctf_event_class_common_get_name(event->common.class),
779 bt_ctf_event_class_common_get_id(event->common.class), header);
780 return 0;
781 }
782
783 int bt_ctf_event_common_set_payload(struct bt_ctf_event *event,
784 struct bt_ctf_field *payload)
785 {
786 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
787 BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event");
788
789 if (payload) {
790 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
791 ((struct bt_ctf_field_common *) payload)->type,
792 event->common.class->payload_field_type) == 0,
793 "Payload field's type is different from the "
794 "expected field type: event-addr=%p, ft-addr=%p, "
795 "expected-ft-addr=%p",
796 event,
797 ((struct bt_ctf_field_common *) payload)->type,
798 event->common.class->payload_field_type);
799 } else {
800 BT_CTF_ASSERT_PRE(!event->common.class->payload_field_type,
801 "Setting no event payload field, "
802 "but event payload field type is not NULL: "
803 "event-addr=%p, payload-ft-addr=%p",
804 event, event->common.class->payload_field_type);
805 }
806
807 bt_ctf_object_put_ref(event->common.payload_field);
808 event->common.payload_field = bt_ctf_object_get_ref(payload);
809 BT_LOGT("Set event's payload field: event-addr=%p, "
810 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
811 "payload-field-addr=%p",
812 event, bt_ctf_event_class_common_get_name(event->common.class),
813 bt_ctf_event_class_common_get_id(event->common.class), payload);
814 return 0;
815 }
816
817 int bt_ctf_event_set_context(struct bt_ctf_event *event,
818 struct bt_ctf_field *context)
819 {
820 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
821 BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event");
822
823 if (context) {
824 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
825 ((struct bt_ctf_field_common *) context)->type,
826 event->common.class->context_field_type) == 0,
827 "Context field's type is different from the "
828 "expected field type: event-addr=%p, ft-addr=%p, "
829 "expected-ft-addr=%p",
830 event, ((struct bt_ctf_field_common *) context)->type,
831 event->common.class->context_field_type);
832 } else {
833 BT_CTF_ASSERT_PRE(!event->common.class->context_field_type,
834 "Setting no event context field, "
835 "but event context field type is not NULL: "
836 "event-addr=%p, context-ft-addr=%p",
837 event, event->common.class->context_field_type);
838 }
839
840 bt_ctf_object_put_ref(event->common.context_field);
841 event->common.context_field = bt_ctf_object_get_ref(context);
842 BT_LOGT("Set event's context field: event-addr=%p, "
843 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
844 "context-field-addr=%p",
845 event, bt_ctf_event_class_common_get_name(event->common.class),
846 bt_ctf_event_class_common_get_id(event->common.class), context);
847 return 0;
848 }
849
850 int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event,
851 struct bt_ctf_field *stream_event_context)
852 {
853 BT_CTF_ASSERT_PRE_NON_NULL(event, "Event");
854 BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event");
855
856 if (stream_event_context) {
857 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
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,
860 "Stream event context field's type is different from the "
861 "expected field type: event-addr=%p, ft-addr=%p, "
862 "expected-ft-addr=%p",
863 event,
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);
866 } else {
867 BT_CTF_ASSERT_PRE(!bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type,
868 "Setting no stream event context field, "
869 "but stream event context field type is not NULL: "
870 "event-addr=%p, context-ft-addr=%p",
871 event,
872 bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type);
873 }
874
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);
877 BT_LOGT("Set event's stream event context field: event-addr=%p, "
878 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
879 "stream-event-context-field-addr=%p",
880 event, bt_ctf_event_class_common_get_name(event->common.class),
881 bt_ctf_event_class_common_get_id(event->common.class),
882 stream_event_context);
883 return 0;
884 }
This page took 0.04765 seconds and 4 git commands to generate.