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