Always evaluate BT_ASSERT(); add BT_ASSERT_DBG() for debug mode only
[babeltrace.git] / src / lib / trace-ir / event.c
CommitLineData
273b65be 1/*
f2b0325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
de9dd397 3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
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
b03487ab 24#define BT_LOG_TAG "LIB/EVENT"
1633ef46 25#include "lib/logging.h"
7b010242 26
57952005 27#include "lib/assert-pre.h"
71c5da58 28#include <babeltrace2/trace-ir/event-const.h>
71c5da58 29#include <babeltrace2/trace-ir/event-class.h>
71c5da58
MJ
30#include <babeltrace2/trace-ir/stream-class.h>
31#include <babeltrace2/trace-ir/stream-class-const.h>
71c5da58 32#include <babeltrace2/trace-ir/packet.h>
71c5da58 33#include <babeltrace2/trace-ir/trace.h>
57952005
MJ
34#include "common/assert.h"
35#include "compat/compiler.h"
7b010242 36#include <inttypes.h>
273b65be 37
57952005
MJ
38#include "field.h"
39#include "field-class.h"
40#include "event.h"
41#include "stream-class.h"
42#include "stream.h"
43#include "packet.h"
44#include "trace.h"
45#include "packet.h"
46#include "attributes.h"
47#include "event-class.h"
48
8deee039 49BT_HIDDEN
78cf9df6 50void _bt_event_set_is_frozen(const struct bt_event *event, bool is_frozen)
8deee039 51{
ec4a3354 52 BT_ASSERT_DBG(event);
7b33a0e0
PP
53 BT_LIB_LOGD("Setting event's frozen state: %!+e, is-frozen=%d",
54 event, is_frozen);
a6918753 55
7b33a0e0
PP
56 if (event->common_context_field) {
57 BT_LOGD_STR("Setting event's common context field's frozen state.");
58 bt_field_set_is_frozen(
59 event->common_context_field, is_frozen);
a6918753
PP
60 }
61
7b33a0e0
PP
62 if (event->specific_context_field) {
63 BT_LOGD_STR("Setting event's specific context field's frozen state.");
64 bt_field_set_is_frozen(event->specific_context_field,
c5a24b0a 65 is_frozen);
a6918753
PP
66 }
67
68 if (event->payload_field) {
7b33a0e0
PP
69 BT_LOGD_STR("Setting event's payload field's frozen state.");
70 bt_field_set_is_frozen(event->payload_field,
c5a24b0a 71 is_frozen);
a6918753
PP
72 }
73
78cf9df6 74 ((struct bt_event *) event)->frozen = is_frozen;
37a93d41
PP
75
76 if (event->packet) {
77 BT_LOGD_STR("Setting event's packet's frozen state.");
78 bt_packet_set_is_frozen(event->packet, is_frozen);
79 }
8deee039
PP
80}
81
a6918753
PP
82BT_HIDDEN
83struct bt_event *bt_event_new(struct bt_event_class *event_class)
8deee039 84{
8deee039 85 struct bt_event *event = NULL;
a6918753 86 struct bt_stream_class *stream_class;
939190b3 87 struct bt_field_class *fc;
8deee039 88
7b33a0e0 89 BT_ASSERT(event_class);
8deee039
PP
90 event = g_new0(struct bt_event, 1);
91 if (!event) {
a8f90e5d 92 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event.");
8deee039
PP
93 goto error;
94 }
95
7b33a0e0 96 bt_object_init_unique(&event->base);
a6918753
PP
97 stream_class = bt_event_class_borrow_stream_class(event_class);
98 BT_ASSERT(stream_class);
78cf9df6 99 fc = stream_class->event_common_context_fc;
939190b3
PP
100 if (fc) {
101 event->common_context_field = bt_field_create(fc);
7b33a0e0
PP
102 if (!event->common_context_field) {
103 /* bt_field_create() logs errors */
104 goto error;
105 }
106 }
107
78cf9df6 108 fc = event_class->specific_context_fc;
939190b3
PP
109 if (fc) {
110 event->specific_context_field = bt_field_create(fc);
7b33a0e0
PP
111 if (!event->specific_context_field) {
112 /* bt_field_create() logs errors */
113 goto error;
114 }
115 }
116
78cf9df6 117 fc = event_class->payload_fc;
939190b3
PP
118 if (fc) {
119 event->payload_field = bt_field_create(fc);
7b33a0e0
PP
120 if (!event->payload_field) {
121 /* bt_field_create() logs errors */
122 goto error;
123 }
124 }
125
8deee039
PP
126 goto end;
127
128error:
a6918753
PP
129 if (event) {
130 bt_event_destroy(event);
131 event = NULL;
132 }
133
134end:
135 return event;
136}
137
5fe68922 138struct bt_event_class *bt_event_borrow_class(struct bt_event *event)
2f100782 139{
fa6cfec3 140 BT_ASSERT_PRE_DEV_NON_NULL(event, "Event");
18acc6f8 141 return event->class;
2f100782
JG
142}
143
78cf9df6
PP
144const struct bt_event_class *bt_event_borrow_class_const(
145 const struct bt_event *event)
146{
147 return bt_event_borrow_class((void *) event);
148}
149
8b45963b 150struct bt_stream *bt_event_borrow_stream(struct bt_event *event)
8b45963b 151{
fa6cfec3 152 BT_ASSERT_PRE_DEV_NON_NULL(event, "Event");
37a93d41 153 return event->stream;
8b45963b
PP
154}
155
78cf9df6
PP
156const struct bt_stream *bt_event_borrow_stream_const(
157 const struct bt_event *event)
158{
159 return bt_event_borrow_stream((void *) event);
160}
161
7b33a0e0 162struct bt_field *bt_event_borrow_common_context_field(struct bt_event *event)
662e778c 163{
fa6cfec3 164 BT_ASSERT_PRE_DEV_NON_NULL(event, "Event");
7b33a0e0 165 return event->common_context_field;
662e778c
JG
166}
167
78cf9df6
PP
168const struct bt_field *bt_event_borrow_common_context_field_const(
169 const struct bt_event *event)
9e550e5f 170{
fa6cfec3 171 BT_ASSERT_PRE_DEV_NON_NULL(event, "Event");
78cf9df6 172 return event->common_context_field;
9e550e5f
PP
173}
174
7b33a0e0 175struct bt_field *bt_event_borrow_specific_context_field(struct bt_event *event)
f655a84d 176{
fa6cfec3 177 BT_ASSERT_PRE_DEV_NON_NULL(event, "Event");
7b33a0e0 178 return event->specific_context_field;
f655a84d
JG
179}
180
78cf9df6
PP
181const struct bt_field *bt_event_borrow_specific_context_field_const(
182 const struct bt_event *event)
9e550e5f 183{
fa6cfec3 184 BT_ASSERT_PRE_DEV_NON_NULL(event, "Event");
78cf9df6 185 return event->specific_context_field;
9e550e5f
PP
186}
187
7b33a0e0 188struct bt_field *bt_event_borrow_payload_field(struct bt_event *event)
5fd2e9fd 189{
fa6cfec3 190 BT_ASSERT_PRE_DEV_NON_NULL(event, "Event");
7b33a0e0 191 return event->payload_field;
5fd2e9fd
PP
192}
193
78cf9df6
PP
194const struct bt_field *bt_event_borrow_payload_field_const(
195 const struct bt_event *event)
9e550e5f 196{
fa6cfec3 197 BT_ASSERT_PRE_DEV_NON_NULL(event, "Event");
78cf9df6 198 return event->payload_field;
9e550e5f
PP
199}
200
7b33a0e0
PP
201BT_HIDDEN
202void bt_event_destroy(struct bt_event *event)
18acc6f8 203{
7b33a0e0
PP
204 BT_ASSERT(event);
205 BT_LIB_LOGD("Destroying event: %!+e", event);
18acc6f8 206
7b33a0e0
PP
207 if (event->common_context_field) {
208 BT_LOGD_STR("Destroying event's stream event context field.");
209 bt_field_destroy(event->common_context_field);
1248f5ea 210 event->common_context_field = NULL;
18acc6f8
PP
211 }
212
7b33a0e0
PP
213 if (event->specific_context_field) {
214 BT_LOGD_STR("Destroying event's context field.");
215 bt_field_destroy(event->specific_context_field);
1248f5ea 216 event->specific_context_field = NULL;
18acc6f8
PP
217 }
218
219 if (event->payload_field) {
7b33a0e0
PP
220 BT_LOGD_STR("Destroying event's payload field.");
221 bt_field_destroy(event->payload_field);
1248f5ea 222 event->payload_field = NULL;
18acc6f8
PP
223 }
224
7b33a0e0 225 BT_LOGD_STR("Putting event's class.");
8138bfe1 226 bt_object_put_ref(event->class);
03039f21 227 BT_LOGD_STR("Putting event's packet.");
1248f5ea 228 BT_OBJECT_PUT_REF_AND_RESET(event->packet);
37a93d41
PP
229 BT_LOGD_STR("Putting event's stream.");
230 BT_OBJECT_PUT_REF_AND_RESET(event->stream);
273b65be
JG
231 g_free(event);
232}
233
5fe68922 234struct bt_packet *bt_event_borrow_packet(struct bt_event *event)
5c0f40f4 235{
fa6cfec3 236 BT_ASSERT_PRE_DEV_NON_NULL(event, "Event");
7b33a0e0 237 return event->packet;
5c0f40f4
JG
238}
239
78cf9df6
PP
240const struct bt_packet *bt_event_borrow_packet_const(
241 const struct bt_event *event)
9e550e5f 242{
78cf9df6 243 return bt_event_borrow_packet((void *) event);
9e550e5f 244}
This page took 0.10393 seconds and 4 git commands to generate.