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