lib: update and simplify the `bt_object` API
[babeltrace.git] / lib / ctf-ir / event.c
1 /*
2 * event.c
3 *
4 * Babeltrace CTF IR - Event
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #define BT_LOG_TAG "EVENT"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/ctf-ir/fields-internal.h>
34 #include <babeltrace/ctf-ir/field-types-internal.h>
35 #include <babeltrace/ctf-ir/clock-class.h>
36 #include <babeltrace/ctf-ir/clock-value.h>
37 #include <babeltrace/ctf-ir/clock-value-internal.h>
38 #include <babeltrace/ctf-ir/clock-class-internal.h>
39 #include <babeltrace/ctf-ir/event-internal.h>
40 #include <babeltrace/ctf-ir/event-class.h>
41 #include <babeltrace/ctf-ir/event-class-internal.h>
42 #include <babeltrace/ctf-ir/stream-class.h>
43 #include <babeltrace/ctf-ir/stream-class-internal.h>
44 #include <babeltrace/ctf-ir/stream-internal.h>
45 #include <babeltrace/ctf-ir/packet.h>
46 #include <babeltrace/ctf-ir/packet-internal.h>
47 #include <babeltrace/ctf-ir/trace.h>
48 #include <babeltrace/ctf-ir/trace-internal.h>
49 #include <babeltrace/ctf-ir/validation-internal.h>
50 #include <babeltrace/ctf-ir/packet-internal.h>
51 #include <babeltrace/ctf-ir/utils.h>
52 #include <babeltrace/ref.h>
53 #include <babeltrace/ctf-ir/attributes-internal.h>
54 #include <babeltrace/compiler-internal.h>
55 #include <babeltrace/assert-internal.h>
56 #include <inttypes.h>
57
58 static
59 int bt_event_common_validate_types_for_create(
60 struct bt_event_class_common *event_class,
61 struct bt_validation_output *validation_output,
62 bt_validation_flag_copy_field_type_func copy_field_type_func)
63 {
64 int ret;
65 enum bt_validation_flag validation_flags =
66 BT_VALIDATION_FLAG_STREAM |
67 BT_VALIDATION_FLAG_EVENT;
68 struct bt_trace_common *trace = NULL;
69 struct bt_stream_class_common *stream_class = NULL;
70 struct bt_field_type_common *packet_header_type = NULL;
71 struct bt_field_type_common *packet_context_type = NULL;
72 struct bt_field_type_common *event_header_type = NULL;
73 struct bt_field_type_common *stream_event_ctx_type = NULL;
74 struct bt_field_type_common *event_context_type = NULL;
75 struct bt_field_type_common *event_payload_type = NULL;
76 int trace_valid = 0;
77 struct bt_value *environment = NULL;
78
79 stream_class = bt_event_class_common_borrow_stream_class(event_class);
80 BT_ASSERT(stream_class);
81 trace = bt_stream_class_common_borrow_trace(stream_class);
82 if (trace) {
83 BT_LOGD_STR("Event class is part of a trace.");
84 packet_header_type =
85 bt_trace_common_borrow_packet_header_field_type(trace);
86 trace_valid = trace->valid;
87 BT_ASSERT(trace_valid);
88 environment = trace->environment;
89 }
90
91 packet_context_type =
92 bt_stream_class_common_borrow_packet_context_field_type(
93 stream_class);
94 event_header_type =
95 bt_stream_class_common_borrow_event_header_field_type(
96 stream_class);
97 stream_event_ctx_type =
98 bt_stream_class_common_borrow_event_context_field_type(
99 stream_class);
100 event_context_type =
101 bt_event_class_common_borrow_context_field_type(event_class);
102 event_payload_type =
103 bt_event_class_common_borrow_payload_field_type(event_class);
104 ret = bt_validate_class_types(environment, packet_header_type,
105 packet_context_type, event_header_type, stream_event_ctx_type,
106 event_context_type, event_payload_type, trace_valid,
107 stream_class->valid, event_class->valid,
108 validation_output, validation_flags, copy_field_type_func);
109 if (ret) {
110 /*
111 * This means something went wrong during the validation
112 * process, not that the objects are invalid.
113 */
114 BT_LOGE("Failed to validate event and parents: ret=%d", ret);
115 goto error;
116 }
117
118 if ((validation_output->valid_flags & validation_flags) !=
119 validation_flags) {
120 /* Invalid trace/stream class/event class */
121 BT_LOGW("Invalid trace, stream class, or event class: "
122 "valid-flags=0x%x", validation_output->valid_flags);
123 goto error;
124 }
125
126 goto end;
127
128 error:
129 bt_validation_output_put_types(validation_output);
130 ret = -1;
131
132 end:
133 return ret;
134 }
135
136 static
137 int bt_event_common_create_fields(
138 struct bt_stream_class_common *stream_class,
139 struct bt_validation_output *validation_output,
140 create_field_func create_field_func,
141 release_field_func release_field_func,
142 create_header_field_func create_header_field_func,
143 release_header_field_func release_header_field_func,
144 struct bt_field_wrapper **header_field,
145 struct bt_field_common **stream_event_context_field,
146 struct bt_field_common **context_field,
147 struct bt_field_common **payload_field)
148 {
149 int ret = 0;
150
151 if (validation_output->event_header_type) {
152 BT_LOGD("Creating initial event header field: ft-addr=%p",
153 validation_output->event_header_type);
154 *header_field =
155 create_header_field_func(stream_class,
156 validation_output->event_header_type);
157 if (!*header_field) {
158 BT_LOGE_STR("Cannot create initial event header field object.");
159 goto error;
160 }
161 }
162
163 if (validation_output->stream_event_ctx_type) {
164 BT_LOGD("Creating initial stream event context field: ft-addr=%p",
165 validation_output->stream_event_ctx_type);
166 *stream_event_context_field = create_field_func(
167 validation_output->stream_event_ctx_type);
168 if (!*stream_event_context_field) {
169 BT_LOGE_STR("Cannot create initial stream event context field object.");
170 goto error;
171 }
172 }
173
174 if (validation_output->event_context_type) {
175 BT_LOGD("Creating initial event context field: ft-addr=%p",
176 validation_output->event_context_type);
177 *context_field = create_field_func(
178 validation_output->event_context_type);
179 if (!*context_field) {
180 BT_LOGE_STR("Cannot create initial event context field object.");
181 goto error;
182 }
183 }
184
185 if (validation_output->event_payload_type) {
186 BT_LOGD("Creating initial event payload field: ft-addr=%p",
187 validation_output->event_payload_type);
188 *payload_field = create_field_func(
189 validation_output->event_payload_type);
190 if (!*payload_field) {
191 BT_LOGE_STR("Cannot create initial event payload field object.");
192 goto error;
193 }
194 }
195
196 goto end;
197
198 error:
199 if (*header_field) {
200 release_header_field_func(*header_field, stream_class);
201 }
202
203 if (*stream_event_context_field) {
204 release_field_func(*stream_event_context_field);
205 }
206
207 if (*context_field) {
208 release_field_func(*context_field);
209 }
210
211 if (*payload_field) {
212 release_field_func(*payload_field);
213 }
214
215 ret = -1;
216
217 end:
218 return ret;
219 }
220
221 BT_HIDDEN
222 int _bt_event_common_validate(struct bt_event_common *event)
223 {
224 int ret = 0;
225 struct bt_stream_class_common *stream_class;
226
227 BT_ASSERT(event);
228 if (event->header_field) {
229 ret = bt_field_common_validate_recursive(
230 event->header_field->field);
231 if (ret) {
232 BT_ASSERT_PRE_MSG("Invalid event's header field: "
233 "%![event-]+_e, %![field-]+_f",
234 event, event->header_field->field);
235 goto end;
236 }
237 }
238
239 stream_class = bt_event_class_common_borrow_stream_class(event->class);
240
241 /*
242 * We should not have been able to create the event without associating
243 * the event class to a stream class.
244 */
245 BT_ASSERT(stream_class);
246
247 if (stream_class->event_context_field_type) {
248 ret = bt_field_common_validate_recursive(
249 event->stream_event_context_field);
250 if (ret) {
251 BT_ASSERT_PRE_MSG("Invalid event's stream event context field: "
252 "%![event-]+_e, %![field-]+_f",
253 event, event->stream_event_context_field);
254 goto end;
255 }
256 }
257
258 if (event->class->context_field_type) {
259 ret = bt_field_common_validate_recursive(event->context_field);
260 if (ret) {
261 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
262 "%![event-]+_e, %![field-]+_f",
263 event, event->context_field);
264 goto end;
265 }
266 }
267
268 ret = bt_field_common_validate_recursive(event->payload_field);
269 if (ret) {
270 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
271 "%![event-]+_e, %![field-]+_f",
272 event, event->payload_field);
273 goto end;
274 }
275
276 end:
277 return ret;
278 }
279
280 BT_HIDDEN
281 void _bt_event_common_freeze(struct bt_event_common *event)
282 {
283 BT_ASSERT(event);
284
285 if (event->frozen) {
286 return;
287 }
288
289 BT_LOGD("Freezing event: addr=%p, "
290 "event-class-name=\"%s\", event-class-id=%" PRId64,
291 event, bt_event_class_common_get_name(event->class),
292 bt_event_class_common_get_id(event->class));
293
294 if (event->header_field) {
295 BT_LOGD_STR("Freezing event's header field.");
296 bt_field_common_set_is_frozen_recursive(
297 event->header_field->field, true);
298 }
299
300 if (event->stream_event_context_field) {
301 BT_LOGD_STR("Freezing event's stream event context field.");
302 bt_field_common_set_is_frozen_recursive(
303 event->stream_event_context_field, true);
304 }
305
306 if (event->context_field) {
307 BT_LOGD_STR("Freezing event's context field.");
308 bt_field_common_set_is_frozen_recursive(event->context_field, true);
309 }
310
311 if (event->payload_field) {
312 BT_LOGD_STR("Freezing event's payload field.");
313 bt_field_common_set_is_frozen_recursive(event->payload_field, true);
314 }
315
316 event->frozen = 1;
317 }
318
319 BT_HIDDEN
320 int bt_event_common_initialize(struct bt_event_common *event,
321 struct bt_event_class_common *event_class,
322 struct bt_clock_class *init_expected_clock_class,
323 bool is_shared_with_parent, bt_object_release_func release_func,
324 bt_validation_flag_copy_field_type_func field_type_copy_func,
325 bool must_be_in_trace,
326 int (*map_clock_classes_func)(struct bt_stream_class_common *stream_class,
327 struct bt_field_type_common *packet_context_field_type,
328 struct bt_field_type_common *event_header_field_type),
329 create_field_func create_field_func,
330 release_field_func release_field_func,
331 create_header_field_func create_header_field_func,
332 release_header_field_func release_header_field_func)
333 {
334 int ret;
335 struct bt_trace_common *trace = NULL;
336 struct bt_stream_class_common *stream_class = NULL;
337 struct bt_field_wrapper *event_header = NULL;
338 struct bt_field_common *stream_event_context = NULL;
339 struct bt_field_common *event_context = NULL;
340 struct bt_field_common *event_payload = NULL;
341 struct bt_validation_output validation_output = { 0 };
342 struct bt_clock_class *expected_clock_class =
343 init_expected_clock_class ? bt_get(init_expected_clock_class) :
344 NULL;
345
346 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
347 BT_LOGD("Initializing common event object: event-class-addr=%p, "
348 "event-class-name=\"%s\", event-class-id=%" PRId64,
349 event_class, bt_event_class_common_get_name(event_class),
350 bt_event_class_common_get_id(event_class));
351
352 stream_class = bt_event_class_common_borrow_stream_class(event_class);
353 BT_ASSERT_PRE(stream_class,
354 "Event class is not part of a stream class: %!+_E", event_class);
355
356 /* The event class was frozen when added to its stream class */
357 BT_ASSERT(event_class->frozen);
358 trace = bt_stream_class_common_borrow_trace(stream_class);
359
360 if (must_be_in_trace) {
361 BT_ASSERT_PRE(trace,
362 "Event class's stream class is not part of a trace: "
363 "%![ec-]+_E, %![ec-]+_S", event_class, stream_class);
364 }
365
366 /*
367 * This must be called before anything that can fail because on
368 * failure, the caller releases the reference to `event` to
369 * destroy it.
370 */
371 if (is_shared_with_parent) {
372 bt_object_init_shared_with_parent(&event->base, release_func);
373 } else {
374 bt_object_init_unique(&event->base);
375 }
376
377 if (!stream_class->frozen) {
378 /*
379 * Because this function freezes the stream class,
380 * validate that this stream class contains at most a
381 * single clock class so that we set its expected clock
382 * class for future checks.
383 */
384 ret = bt_stream_class_common_validate_single_clock_class(
385 stream_class, &expected_clock_class);
386 if (ret) {
387 BT_LOGW("Event class's stream class or one of its event "
388 "classes contains a field type which is not "
389 "recursively mapped to the expected "
390 "clock class: "
391 "stream-class-addr=%p, "
392 "stream-class-id=%" PRId64 ", "
393 "stream-class-name=\"%s\", "
394 "expected-clock-class-addr=%p, "
395 "expected-clock-class-name=\"%s\"",
396 stream_class,
397 bt_stream_class_common_get_id(stream_class),
398 bt_stream_class_common_get_name(stream_class),
399 expected_clock_class,
400 expected_clock_class ?
401 bt_clock_class_get_name(expected_clock_class) :
402 NULL);
403 goto error;
404 }
405 }
406
407 /* Validate the trace, the stream class, and the event class */
408 ret = bt_event_common_validate_types_for_create(
409 event_class, &validation_output, field_type_copy_func);
410 if (ret) {
411 /* bt_event_common_validate_types_for_create() logs errors */
412 goto error;
413 }
414
415 if (map_clock_classes_func) {
416 /*
417 * Safe to automatically map selected fields to the
418 * stream's clock's class here because the stream class
419 * is about to be frozen.
420 */
421 if (map_clock_classes_func(stream_class,
422 validation_output.packet_context_type,
423 validation_output.event_header_type)) {
424 BT_LOGW_STR("Cannot automatically map selected stream class's "
425 "field types to stream class's clock's class.");
426 goto error;
427 }
428 }
429
430 /*
431 * event does not share a common ancestor with the event class; it has
432 * to guarantee its existence by holding a reference. This reference
433 * shall be released once the event is associated to a stream since,
434 * from that point, the event and its class will share the same
435 * lifetime.
436 */
437 event->class = bt_get(event_class);
438
439 ret = bt_event_common_create_fields(stream_class,
440 &validation_output,
441 create_field_func, release_field_func,
442 create_header_field_func, release_header_field_func,
443 &event_header, &stream_event_context, &event_context,
444 &event_payload);
445 if (ret) {
446 /* bt_event_common_create_fields() logs errors */
447 goto error;
448 }
449
450 /*
451 * At this point all the fields are created, potentially from
452 * validated copies of field types, so that the field types and
453 * fields can be replaced in the trace, stream class,
454 * event class, and created event.
455 */
456 bt_validation_replace_types(trace, stream_class, event_class,
457 &validation_output,
458 BT_VALIDATION_FLAG_STREAM | BT_VALIDATION_FLAG_EVENT);
459 event->header_field = event_header;
460 event_header = NULL;
461 event->stream_event_context_field = stream_event_context;
462 stream_event_context = NULL;
463 event->context_field = event_context;
464 event_context = NULL;
465 event->payload_field = event_payload;
466 event_payload = NULL;
467
468 /*
469 * Put what was not moved in bt_validation_replace_types().
470 */
471 bt_validation_output_put_types(&validation_output);
472
473 /*
474 * Freeze the stream class since the event header must not be changed
475 * anymore.
476 */
477 bt_stream_class_common_freeze(stream_class);
478
479 /*
480 * It is safe to set the stream class's unique clock class
481 * now because the stream class is frozen.
482 */
483 if (expected_clock_class) {
484 BT_MOVE(stream_class->clock_class, expected_clock_class);
485 }
486
487 /*
488 * Mark stream class, and event class as valid since
489 * they're all frozen now.
490 */
491 stream_class->valid = 1;
492 event_class->valid = 1;
493
494 /* Put stuff we borrowed from the event class */
495 BT_LOGD("Initialized event object: addr=%p, event-class-name=\"%s\", "
496 "event-class-id=%" PRId64,
497 event, bt_event_class_common_get_name(event->class),
498 bt_event_class_common_get_id(event->class));
499 goto end;
500
501 error:
502 bt_validation_output_put_types(&validation_output);
503 bt_put(expected_clock_class);
504
505 if (event_header) {
506 release_header_field_func(event_header, stream_class);
507 }
508
509 if (stream_event_context) {
510 release_field_func(stream_event_context);
511 }
512
513 if (event_context) {
514 release_field_func(event_context);
515 }
516
517 if (event_payload) {
518 release_field_func(event_payload);
519 }
520
521 ret = -1;
522
523 end:
524 return ret;
525 }
526
527 static
528 void bt_event_header_field_recycle(struct bt_field_wrapper *field_wrapper,
529 struct bt_stream_class *stream_class)
530 {
531 BT_ASSERT(field_wrapper);
532 BT_LIB_LOGD("Recycling event header field: "
533 "addr=%p, %![sc-]+S, %![field-]+f", field_wrapper,
534 stream_class, field_wrapper->field);
535 bt_object_pool_recycle_object(
536 &stream_class->event_header_field_pool,
537 field_wrapper);
538 }
539
540 static
541 struct bt_field_wrapper *create_event_header_field(
542 struct bt_stream_class *stream_class,
543 struct bt_field_type_common *ft)
544 {
545 struct bt_field_wrapper *field_wrapper = NULL;
546
547 field_wrapper = bt_field_wrapper_create(
548 &stream_class->event_header_field_pool, (void *) ft);
549 if (!field_wrapper) {
550 goto error;
551 }
552
553 goto end;
554
555 error:
556 if (field_wrapper) {
557 bt_event_header_field_recycle(field_wrapper, stream_class);
558 field_wrapper = NULL;
559 }
560
561 end:
562 return field_wrapper;
563 }
564
565 BT_HIDDEN
566 struct bt_event *bt_event_new(struct bt_event_class *event_class)
567 {
568 int ret;
569 struct bt_event *event = NULL;
570 struct bt_stream_class *stream_class;
571
572 event = g_new0(struct bt_event, 1);
573 if (!event) {
574 BT_LOGE_STR("Failed to allocate one event.");
575 goto error;
576 }
577
578 ret = bt_event_common_initialize(BT_TO_COMMON(event),
579 BT_TO_COMMON(event_class), NULL, false, NULL,
580 (bt_validation_flag_copy_field_type_func) bt_field_type_copy,
581 true, NULL,
582 (create_field_func) bt_field_create_recursive,
583 (release_field_func) bt_field_destroy_recursive,
584 (create_header_field_func) create_event_header_field,
585 (release_header_field_func) bt_event_header_field_recycle);
586 if (ret) {
587 /* bt_event_common_initialize() logs errors */
588 goto error;
589 }
590
591 event->clock_values = g_hash_table_new_full(g_direct_hash,
592 g_direct_equal, NULL,
593 (GDestroyNotify) bt_clock_value_recycle);
594 BT_ASSERT(event->clock_values);
595 stream_class = bt_event_class_borrow_stream_class(event_class);
596 BT_ASSERT(stream_class);
597
598 if (stream_class->common.clock_class) {
599 struct bt_clock_value *clock_value;
600
601 clock_value = bt_clock_value_create(
602 stream_class->common.clock_class);
603 if (!clock_value) {
604 BT_LIB_LOGE("Cannot create clock value from clock class: "
605 "%![cc-]+K", stream_class->common.clock_class);
606 goto error;
607 }
608
609 g_hash_table_insert(event->clock_values,
610 stream_class->common.clock_class, clock_value);
611 }
612
613 goto end;
614
615 error:
616 if (event) {
617 bt_event_destroy(event);
618 event = NULL;
619 }
620
621 end:
622 return event;
623 }
624
625 BT_ASSERT_PRE_FUNC
626 static inline
627 void _bt_event_reset_dev_mode(struct bt_event *event)
628 {
629 GHashTableIter iter;
630 gpointer key, value;
631
632 BT_ASSERT(event);
633
634 if (event->common.header_field) {
635 bt_field_common_set_is_frozen_recursive(
636 event->common.header_field->field, false);
637 bt_field_common_reset_recursive(
638 event->common.header_field->field);
639 }
640
641 if (event->common.stream_event_context_field) {
642 bt_field_common_set_is_frozen_recursive(
643 event->common.stream_event_context_field, false);
644 bt_field_common_reset_recursive(
645 event->common.stream_event_context_field);
646 }
647
648 if (event->common.context_field) {
649 bt_field_common_set_is_frozen_recursive(
650 event->common.context_field, false);
651 bt_field_common_reset_recursive(event->common.context_field);
652 }
653
654 if (event->common.payload_field) {
655 bt_field_common_set_is_frozen_recursive(
656 event->common.payload_field, false);
657 bt_field_common_reset_recursive(event->common.payload_field);
658 }
659
660 g_hash_table_iter_init(&iter, event->clock_values);
661 while (g_hash_table_iter_next(&iter, &key, &value)) {
662 struct bt_clock_value *clock_value = value;
663
664 BT_ASSERT(clock_value);
665 bt_clock_value_reset(clock_value);
666 bt_clock_value_set_is_frozen(clock_value, false);
667 }
668 }
669
670 #ifdef BT_DEV_MODE
671 # define bt_event_reset_dev_mode _bt_event_reset_dev_mode
672 #else
673 # define bt_event_reset_dev_mode(_x)
674 #endif
675
676 static inline
677 void bt_event_reset(struct bt_event *event)
678 {
679 BT_ASSERT(event);
680 event->common.frozen = false;
681 bt_event_reset_dev_mode(event);
682 BT_PUT(event->packet);
683 }
684
685 BT_HIDDEN
686 struct bt_event *bt_event_create(struct bt_event_class *event_class,
687 struct bt_packet *packet)
688 {
689 int ret;
690 struct bt_event *event = NULL;
691
692 BT_ASSERT(event_class);
693 event = bt_object_pool_create_object(&event_class->event_pool);
694 if (!event) {
695 BT_LIB_LOGE("Cannot allocate one event from event class's event pool: "
696 "%![event-class-]+E", event_class);
697 goto error;
698 }
699
700 if (!event->common.class) {
701 event->common.class = bt_get(event_class);
702 }
703
704 BT_ASSERT(packet);
705 ret = bt_event_set_packet(event, packet);
706 if (ret) {
707 BT_LIB_LOGE("Cannot set event's packet: "
708 "%![event-]+e, %![packet-]+a", event, packet);
709 goto error;
710 }
711
712 goto end;
713
714 error:
715 if (event) {
716 bt_event_recycle(event);
717 event = NULL;
718 }
719
720 end:
721 return event;
722 }
723
724 struct bt_event_class *bt_event_borrow_class(struct bt_event *event)
725 {
726 BT_ASSERT_PRE_NON_NULL(event, "Event");
727 return BT_FROM_COMMON(
728 bt_event_common_borrow_class(BT_TO_COMMON(event)));
729 }
730
731 struct bt_stream *bt_event_borrow_stream(struct bt_event *event)
732 {
733 BT_ASSERT_PRE_NON_NULL(event, "Event");
734 return event->packet ? event->packet->stream : NULL;
735 }
736
737 struct bt_field *bt_event_borrow_payload(struct bt_event *event)
738 {
739 return BT_FROM_COMMON(
740 bt_event_common_borrow_payload(BT_TO_COMMON(event)));
741 }
742
743 struct bt_field *bt_event_borrow_header(struct bt_event *event)
744 {
745 return BT_FROM_COMMON(
746 bt_event_common_borrow_header(BT_TO_COMMON(event)));
747 }
748
749 struct bt_field *bt_event_borrow_context(struct bt_event *event)
750 {
751 return BT_FROM_COMMON(
752 bt_event_common_borrow_context(BT_TO_COMMON(event)));
753 }
754
755 struct bt_field *bt_event_borrow_stream_event_context(
756 struct bt_event *event)
757 {
758 return BT_FROM_COMMON(bt_event_common_borrow_stream_event_context(
759 BT_TO_COMMON(event)));
760 }
761
762 static
763 void release_event_header_field(struct bt_field_wrapper *field_wrapper,
764 struct bt_event_common *event_common)
765 {
766 struct bt_event *event = BT_FROM_COMMON(event_common);
767 struct bt_event_class *event_class = bt_event_borrow_class(event);
768
769 if (!event_class) {
770 bt_field_wrapper_destroy(field_wrapper);
771 } else {
772 struct bt_stream_class *stream_class =
773 bt_event_class_borrow_stream_class(event_class);
774
775 BT_ASSERT(stream_class);
776 bt_event_header_field_recycle(field_wrapper, stream_class);
777 }
778 }
779
780 BT_HIDDEN
781 void bt_event_destroy(struct bt_event *event)
782 {
783 BT_ASSERT(event);
784 bt_event_common_finalize((void *) event,
785 (void *) bt_field_destroy_recursive,
786 (void *) release_event_header_field);
787 g_hash_table_destroy(event->clock_values);
788 BT_LOGD_STR("Putting event's packet.");
789 bt_put(event->packet);
790 g_free(event);
791 }
792
793 struct bt_clock_value *bt_event_borrow_clock_value(
794 struct bt_event *event, struct bt_clock_class *clock_class)
795 {
796 struct bt_clock_value *clock_value = NULL;
797
798 BT_ASSERT_PRE_NON_NULL(event, "Event");
799 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
800 clock_value = g_hash_table_lookup(event->clock_values, clock_class);
801 if (!clock_value) {
802 BT_LOGV("No clock value associated to the given clock class: "
803 "event-addr=%p, event-class-name=\"%s\", "
804 "event-class-id=%" PRId64 ", clock-class-addr=%p, "
805 "clock-class-name=\"%s\"", event,
806 bt_event_class_common_get_name(event->common.class),
807 bt_event_class_common_get_id(event->common.class),
808 clock_class, bt_clock_class_get_name(clock_class));
809 goto end;
810 }
811
812 end:
813 return clock_value;
814 }
815
816 struct bt_packet *bt_event_borrow_packet(struct bt_event *event)
817 {
818 struct bt_packet *packet = NULL;
819
820 BT_ASSERT_PRE_NON_NULL(event, "Event");
821 if (!event->packet) {
822 BT_LOGV("Event has no current packet: addr=%p, "
823 "event-class-name=\"%s\", event-class-id=%" PRId64,
824 event, bt_event_class_common_get_name(event->common.class),
825 bt_event_class_common_get_id(event->common.class));
826 goto end;
827 }
828
829 packet = event->packet;
830
831 end:
832 return packet;
833 }
834
835 BT_HIDDEN
836 int bt_event_set_packet(struct bt_event *event, struct bt_packet *packet)
837 {
838 BT_ASSERT_PRE_NON_NULL(event, "Event");
839 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
840 BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_TO_COMMON(event), "Event");
841
842 /*
843 * Make sure the new packet was created by this event's
844 * stream, if it is set.
845 */
846 if (bt_event_borrow_stream(event)) {
847 BT_ASSERT_PRE(packet->stream == bt_event_borrow_stream(event),
848 "Packet's stream and event's stream differ: "
849 "%![event-]+e, %![packet-]+a",
850 event, packet);
851 } else {
852 BT_ASSERT_PRE(bt_event_class_borrow_stream_class(
853 BT_FROM_COMMON(event->common.class)) ==
854 BT_FROM_COMMON(packet->stream->common.stream_class),
855 "Packet's stream class and event's stream class differ: "
856 "%![event-]+e, %![packet-]+a",
857 event, packet);
858 }
859
860 bt_get(packet);
861 BT_MOVE(event->packet, packet);
862 BT_LOGV("Set event's packet: event-addr=%p, "
863 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
864 "packet-addr=%p",
865 event, bt_event_class_common_get_name(event->common.class),
866 bt_event_class_common_get_id(event->common.class), packet);
867 return 0;
868 }
869
870 BT_HIDDEN
871 void _bt_event_freeze(struct bt_event *event)
872 {
873 _bt_event_common_freeze(BT_TO_COMMON(event));
874 BT_LOGD_STR("Freezing event's packet.");
875 bt_packet_freeze(event->packet);
876 }
877
878 BT_HIDDEN
879 void bt_event_recycle(struct bt_event *event)
880 {
881 struct bt_event_class *event_class;
882
883 BT_ASSERT(event);
884 BT_LIB_LOGD("Recycling event: %!+e", event);
885
886 /*
887 * Those are the important ordered steps:
888 *
889 * 1. Reset the event object (put any permanent reference it
890 * has, unfreeze it and its fields in developer mode, etc.),
891 * but do NOT put its class's reference. This event class
892 * contains the pool to which we're about to recycle this
893 * event object, so we must guarantee its existence thanks
894 * to this existing reference.
895 *
896 * 2. Move the event class reference to our `event_class`
897 * variable so that we can set the event's class member
898 * to NULL before recycling it. We CANNOT do this after
899 * we put the event class reference because this bt_put()
900 * could destroy the event class, also destroying its
901 * event pool, thus also destroying our event object (this
902 * would result in an invalid write access).
903 *
904 * 3. Recycle the event object.
905 *
906 * 4. Put our event class reference.
907 */
908 bt_event_reset(event);
909 event_class = BT_FROM_COMMON(event->common.class);
910 BT_ASSERT(event_class);
911 event->common.class = NULL;
912 bt_object_pool_recycle_object(&event_class->event_pool, event);
913 bt_put(event_class);
914 }
915
916 int bt_event_move_header(struct bt_event *event,
917 struct bt_event_header_field *header_field)
918 {
919 struct bt_stream_class *stream_class;
920 struct bt_field_wrapper *field_wrapper = (void *) header_field;
921
922 BT_ASSERT_PRE_NON_NULL(event, "Event");
923 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Header field");
924 BT_ASSERT_PRE_HOT(BT_TO_COMMON(event), "Event", ": +%!+e", event);
925 stream_class = bt_event_class_borrow_stream_class(
926 bt_event_borrow_class(event));
927 BT_ASSERT_PRE(stream_class->common.event_header_field_type,
928 "Stream class has no event header field type: %!+S",
929 stream_class);
930
931 /* Recycle current header field: always exists */
932 BT_ASSERT(event->common.header_field);
933 bt_event_header_field_recycle(event->common.header_field,
934 stream_class);
935
936 /* Move new field */
937 event->common.header_field = (void *) field_wrapper;
938 return 0;
939 }
This page took 0.047297 seconds and 4 git commands to generate.