| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #define BT_LOG_TAG "LIB/EVENT" |
| 9 | #include "lib/logging.h" |
| 10 | |
| 11 | #include "lib/assert-cond.h" |
| 12 | #include <babeltrace2/trace-ir/event.h> |
| 13 | #include <babeltrace2/trace-ir/event-class.h> |
| 14 | #include <babeltrace2/trace-ir/stream-class.h> |
| 15 | #include <babeltrace2/trace-ir/packet.h> |
| 16 | #include <babeltrace2/trace-ir/trace.h> |
| 17 | #include "common/assert.h" |
| 18 | #include "compat/compiler.h" |
| 19 | #include <stdbool.h> |
| 20 | |
| 21 | #include "field.h" |
| 22 | #include "field-class.h" |
| 23 | #include "event.h" |
| 24 | #include "stream-class.h" |
| 25 | #include "stream.h" |
| 26 | #include "packet.h" |
| 27 | #include "packet.h" |
| 28 | #include "event-class.h" |
| 29 | |
| 30 | void _bt_event_set_is_frozen(const struct bt_event *event, bool is_frozen) |
| 31 | { |
| 32 | BT_ASSERT_DBG(event); |
| 33 | BT_LIB_LOGD("Setting event's frozen state: %!+e, is-frozen=%d", |
| 34 | event, is_frozen); |
| 35 | |
| 36 | if (event->common_context_field) { |
| 37 | BT_LOGD_STR("Setting event's common context field's frozen state."); |
| 38 | bt_field_set_is_frozen( |
| 39 | event->common_context_field, is_frozen); |
| 40 | } |
| 41 | |
| 42 | if (event->specific_context_field) { |
| 43 | BT_LOGD_STR("Setting event's specific context field's frozen state."); |
| 44 | bt_field_set_is_frozen(event->specific_context_field, |
| 45 | is_frozen); |
| 46 | } |
| 47 | |
| 48 | if (event->payload_field) { |
| 49 | BT_LOGD_STR("Setting event's payload field's frozen state."); |
| 50 | bt_field_set_is_frozen(event->payload_field, |
| 51 | is_frozen); |
| 52 | } |
| 53 | |
| 54 | ((struct bt_event *) event)->frozen = is_frozen; |
| 55 | |
| 56 | if (event->packet) { |
| 57 | BT_LOGD_STR("Setting event's packet's frozen state."); |
| 58 | bt_packet_set_is_frozen(event->packet, is_frozen); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | struct bt_event *bt_event_new(struct bt_event_class *event_class) |
| 63 | { |
| 64 | struct bt_event *event = NULL; |
| 65 | struct bt_stream_class *stream_class; |
| 66 | struct bt_field_class *fc; |
| 67 | |
| 68 | BT_ASSERT(event_class); |
| 69 | event = g_new0(struct bt_event, 1); |
| 70 | if (!event) { |
| 71 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event."); |
| 72 | goto error; |
| 73 | } |
| 74 | |
| 75 | bt_object_init_unique(&event->base); |
| 76 | stream_class = bt_event_class_borrow_stream_class(event_class); |
| 77 | BT_ASSERT(stream_class); |
| 78 | fc = stream_class->event_common_context_fc; |
| 79 | if (fc) { |
| 80 | event->common_context_field = bt_field_create(fc); |
| 81 | if (!event->common_context_field) { |
| 82 | /* bt_field_create() logs errors */ |
| 83 | goto error; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fc = event_class->specific_context_fc; |
| 88 | if (fc) { |
| 89 | event->specific_context_field = bt_field_create(fc); |
| 90 | if (!event->specific_context_field) { |
| 91 | /* bt_field_create() logs errors */ |
| 92 | goto error; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | fc = event_class->payload_fc; |
| 97 | if (fc) { |
| 98 | event->payload_field = bt_field_create(fc); |
| 99 | if (!event->payload_field) { |
| 100 | /* bt_field_create() logs errors */ |
| 101 | goto error; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | goto end; |
| 106 | |
| 107 | error: |
| 108 | if (event) { |
| 109 | bt_event_destroy(event); |
| 110 | event = NULL; |
| 111 | } |
| 112 | |
| 113 | end: |
| 114 | return event; |
| 115 | } |
| 116 | |
| 117 | BT_EXPORT |
| 118 | struct bt_event_class *bt_event_borrow_class(struct bt_event *event) |
| 119 | { |
| 120 | BT_ASSERT_PRE_DEV_EVENT_NON_NULL(event); |
| 121 | return event->class; |
| 122 | } |
| 123 | |
| 124 | BT_EXPORT |
| 125 | const struct bt_event_class *bt_event_borrow_class_const( |
| 126 | const struct bt_event *event) |
| 127 | { |
| 128 | return bt_event_borrow_class((void *) event); |
| 129 | } |
| 130 | |
| 131 | BT_EXPORT |
| 132 | struct bt_stream *bt_event_borrow_stream(struct bt_event *event) |
| 133 | { |
| 134 | BT_ASSERT_PRE_DEV_EVENT_NON_NULL(event); |
| 135 | return event->stream; |
| 136 | } |
| 137 | |
| 138 | BT_EXPORT |
| 139 | const struct bt_stream *bt_event_borrow_stream_const( |
| 140 | const struct bt_event *event) |
| 141 | { |
| 142 | return bt_event_borrow_stream((void *) event); |
| 143 | } |
| 144 | |
| 145 | BT_EXPORT |
| 146 | struct bt_field *bt_event_borrow_common_context_field(struct bt_event *event) |
| 147 | { |
| 148 | BT_ASSERT_PRE_DEV_EVENT_NON_NULL(event); |
| 149 | return event->common_context_field; |
| 150 | } |
| 151 | |
| 152 | BT_EXPORT |
| 153 | const struct bt_field *bt_event_borrow_common_context_field_const( |
| 154 | const struct bt_event *event) |
| 155 | { |
| 156 | BT_ASSERT_PRE_DEV_EVENT_NON_NULL(event); |
| 157 | return event->common_context_field; |
| 158 | } |
| 159 | |
| 160 | BT_EXPORT |
| 161 | struct bt_field *bt_event_borrow_specific_context_field(struct bt_event *event) |
| 162 | { |
| 163 | BT_ASSERT_PRE_DEV_EVENT_NON_NULL(event); |
| 164 | return event->specific_context_field; |
| 165 | } |
| 166 | |
| 167 | BT_EXPORT |
| 168 | const struct bt_field *bt_event_borrow_specific_context_field_const( |
| 169 | const struct bt_event *event) |
| 170 | { |
| 171 | BT_ASSERT_PRE_DEV_EVENT_NON_NULL(event); |
| 172 | return event->specific_context_field; |
| 173 | } |
| 174 | |
| 175 | BT_EXPORT |
| 176 | struct bt_field *bt_event_borrow_payload_field(struct bt_event *event) |
| 177 | { |
| 178 | BT_ASSERT_PRE_DEV_EVENT_NON_NULL(event); |
| 179 | return event->payload_field; |
| 180 | } |
| 181 | |
| 182 | BT_EXPORT |
| 183 | const struct bt_field *bt_event_borrow_payload_field_const( |
| 184 | const struct bt_event *event) |
| 185 | { |
| 186 | BT_ASSERT_PRE_DEV_EVENT_NON_NULL(event); |
| 187 | return event->payload_field; |
| 188 | } |
| 189 | |
| 190 | void bt_event_destroy(struct bt_event *event) |
| 191 | { |
| 192 | BT_ASSERT(event); |
| 193 | BT_LIB_LOGD("Destroying event: %!+e", event); |
| 194 | |
| 195 | if (event->common_context_field) { |
| 196 | BT_LOGD_STR("Destroying event's stream event context field."); |
| 197 | bt_field_destroy(event->common_context_field); |
| 198 | event->common_context_field = NULL; |
| 199 | } |
| 200 | |
| 201 | if (event->specific_context_field) { |
| 202 | BT_LOGD_STR("Destroying event's context field."); |
| 203 | bt_field_destroy(event->specific_context_field); |
| 204 | event->specific_context_field = NULL; |
| 205 | } |
| 206 | |
| 207 | if (event->payload_field) { |
| 208 | BT_LOGD_STR("Destroying event's payload field."); |
| 209 | bt_field_destroy(event->payload_field); |
| 210 | event->payload_field = NULL; |
| 211 | } |
| 212 | |
| 213 | BT_LOGD_STR("Putting event's class."); |
| 214 | bt_object_put_ref(event->class); |
| 215 | BT_LOGD_STR("Putting event's packet."); |
| 216 | BT_OBJECT_PUT_REF_AND_RESET(event->packet); |
| 217 | BT_LOGD_STR("Putting event's stream."); |
| 218 | BT_OBJECT_PUT_REF_AND_RESET(event->stream); |
| 219 | g_free(event); |
| 220 | } |
| 221 | |
| 222 | BT_EXPORT |
| 223 | struct bt_packet *bt_event_borrow_packet(struct bt_event *event) |
| 224 | { |
| 225 | BT_ASSERT_PRE_DEV_EVENT_NON_NULL(event); |
| 226 | return event->packet; |
| 227 | } |
| 228 | |
| 229 | BT_EXPORT |
| 230 | const struct bt_packet *bt_event_borrow_packet_const( |
| 231 | const struct bt_event *event) |
| 232 | { |
| 233 | return bt_event_borrow_packet((void *) event); |
| 234 | } |