Standardize `!ptr` i/o `ptr == NULL`, `ptr` i/o `ptr != NULL`
[babeltrace.git] / src / lib / trace-ir / trace-class.c
CommitLineData
862ca4ed 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
862ca4ed
PP
3 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
862ca4ed
PP
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 "LIB/TRACE"
c2d9d9cf 25#include "lib/logging.h"
862ca4ed 26
578e048b 27#include "lib/assert-pre.h"
3fadfbc0
MJ
28#include <babeltrace2/trace-ir/trace-class.h>
29#include <babeltrace2/trace-ir/trace-class-const.h>
3fadfbc0 30#include <babeltrace2/trace-ir/event-class.h>
578e048b
MJ
31#include "ctf-writer/functor.h"
32#include "ctf-writer/clock.h"
33#include "compat/compiler.h"
3fadfbc0
MJ
34#include <babeltrace2/value.h>
35#include <babeltrace2/value-const.h>
578e048b 36#include "lib/value.h"
3fadfbc0 37#include <babeltrace2/types.h>
578e048b
MJ
38#include "compat/endian.h"
39#include "common/assert.h"
40#include "compat/glib.h"
862ca4ed
PP
41#include <inttypes.h>
42#include <stdint.h>
43#include <string.h>
44#include <stdlib.h>
45
578e048b
MJ
46#include "clock-class.h"
47#include "event-class.h"
48#include "event.h"
49#include "field-class.h"
50#include "field-wrapper.h"
51#include "resolve-field-path.h"
52#include "stream-class.h"
53#include "stream.h"
54#include "trace.h"
55#include "utils.h"
d24d5663 56#include "lib/func-status.h"
578e048b 57
ad5268b5
FD
58struct bt_trace_class_destruction_listener_elem {
59 bt_trace_class_destruction_listener_func func;
60 void *data;
61};
62
bdb288b3
PP
63#define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
64 BT_ASSERT_PRE_DEV_HOT((_tc), "Trace class", ": %!+T", (_tc))
862ca4ed
PP
65
66static
67void destroy_trace_class(struct bt_object *obj)
68{
69 struct bt_trace_class *tc = (void *) obj;
70
71 BT_LIB_LOGD("Destroying trace class object: %!+T", tc);
ad5268b5
FD
72 /*
73 * Call destruction listener functions so that everything else
74 * still exists in the trace class.
75 */
76 if (tc->destruction_listeners) {
77 uint64_t i;
3f7d4d90 78 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc);
c47a6bec
SM
79
80 /*
81 * The trace class' reference count is 0 if we're here. Increment
82 * it to avoid a double-destroy (possibly infinitely recursive).
83 * This could happen for example if a destruction listener did
84 * bt_object_get_ref() (or anything that causes
85 * bt_object_get_ref() to be called) on the trace class (ref.
86 * count goes from 0 to 1), and then bt_object_put_ref(): the
87 * reference count would go from 1 to 0 again and this function
88 * would be called again.
89 */
90 tc->base.ref_count++;
91
ad5268b5
FD
92 /* Call all the trace class destruction listeners */
93 for (i = 0; i < tc->destruction_listeners->len; i++) {
94 struct bt_trace_class_destruction_listener_elem elem =
95 g_array_index(tc->destruction_listeners,
96 struct bt_trace_class_destruction_listener_elem, i);
97
98 if (elem.func) {
99 elem.func(tc, elem.data);
100 }
c47a6bec
SM
101
102 /*
103 * The destruction listener should not have kept a
104 * reference to the trace class.
105 */
106 BT_ASSERT_PRE(tc->base.ref_count == 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc);
ad5268b5
FD
107 }
108 g_array_free(tc->destruction_listeners, TRUE);
109 tc->destruction_listeners = NULL;
110 }
862ca4ed 111
862ca4ed
PP
112 if (tc->stream_classes) {
113 BT_LOGD_STR("Destroying stream classes.");
114 g_ptr_array_free(tc->stream_classes, TRUE);
115 tc->stream_classes = NULL;
116 }
117
862ca4ed
PP
118 g_free(tc);
119}
120
41693723 121struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp)
862ca4ed
PP
122{
123 struct bt_trace_class *tc = NULL;
862ca4ed 124
41693723 125 BT_ASSERT_PRE_NON_NULL(self_comp, "Self component");
862ca4ed
PP
126 BT_LOGD_STR("Creating default trace class object.");
127 tc = g_new0(struct bt_trace_class, 1);
128 if (!tc) {
870631a2 129 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
862ca4ed
PP
130 goto error;
131 }
132
133 bt_object_init_shared_with_parent(&tc->base, destroy_trace_class);
134
135 tc->stream_classes = g_ptr_array_new_with_free_func(
136 (GDestroyNotify) bt_object_try_spec_release);
137 if (!tc->stream_classes) {
870631a2 138 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
862ca4ed
PP
139 goto error;
140 }
141
ad5268b5
FD
142 tc->destruction_listeners = g_array_new(FALSE, TRUE,
143 sizeof(struct bt_trace_class_destruction_listener_elem));
144 if (!tc->destruction_listeners) {
870631a2 145 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
ad5268b5
FD
146 goto error;
147 }
148
862ca4ed 149 tc->assigns_automatic_stream_class_id = true;
862ca4ed
PP
150 BT_LIB_LOGD("Created trace class object: %!+T", tc);
151 goto end;
152
153error:
154 BT_OBJECT_PUT_REF_AND_RESET(tc);
155
156end:
157 return tc;
158}
159
d24d5663 160enum bt_trace_class_add_listener_status bt_trace_class_add_destruction_listener(
ad5268b5
FD
161 const struct bt_trace_class *_tc,
162 bt_trace_class_destruction_listener_func listener,
163 void *data, uint64_t *listener_id)
164{
165 struct bt_trace_class *tc = (void *) _tc;
166 uint64_t i;
167 struct bt_trace_class_destruction_listener_elem new_elem = {
168 .func = listener,
169 .data = data,
170 };
171
172 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
173 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
174
175 /* Find the next available spot */
176 for (i = 0; i < tc->destruction_listeners->len; i++) {
177 struct bt_trace_class_destruction_listener_elem elem =
178 g_array_index(tc->destruction_listeners,
179 struct bt_trace_class_destruction_listener_elem, i);
180
181 if (!elem.func) {
182 break;
183 }
184 }
185
186 if (i == tc->destruction_listeners->len) {
187 g_array_append_val(tc->destruction_listeners, new_elem);
188 } else {
189 g_array_insert_val(tc->destruction_listeners, i, new_elem);
190 }
191
192 if (listener_id) {
193 *listener_id = i;
194 }
195
3f7d4d90 196 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
ad5268b5 197 "listener-id=%" PRIu64, tc, i);
d24d5663 198 return BT_FUNC_STATUS_OK;
ad5268b5
FD
199}
200
ad5268b5
FD
201static
202bool has_listener_id(const struct bt_trace_class *tc, uint64_t listener_id)
203{
204 BT_ASSERT(listener_id < tc->destruction_listeners->len);
205 return (&g_array_index(tc->destruction_listeners,
206 struct bt_trace_class_destruction_listener_elem,
5084732e 207 listener_id))->func;
ad5268b5
FD
208}
209
d24d5663 210enum bt_trace_class_remove_listener_status bt_trace_class_remove_destruction_listener(
ad5268b5
FD
211 const struct bt_trace_class *_tc, uint64_t listener_id)
212{
213 struct bt_trace_class *tc = (void *) _tc;
214 struct bt_trace_class_destruction_listener_elem *elem;
215
216 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
217 BT_ASSERT_PRE(has_listener_id(tc, listener_id),
218 "Trace class has no such trace class destruction listener ID: "
219 "%![tc-]+T, %" PRIu64, tc, listener_id);
220 elem = &g_array_index(tc->destruction_listeners,
221 struct bt_trace_class_destruction_listener_elem,
222 listener_id);
223 BT_ASSERT(elem->func);
224
225 elem->func = NULL;
226 elem->data = NULL;
3f7d4d90 227 BT_LIB_LOGD("Removed trace class destruction listener: "
ad5268b5
FD
228 "%![tc-]+T, listener-id=%" PRIu64,
229 tc, listener_id);
d24d5663 230 return BT_FUNC_STATUS_OK;
ad5268b5
FD
231}
232
862ca4ed
PP
233uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc)
234{
bdb288b3 235 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
236 return (uint64_t) tc->stream_classes->len;
237}
238
239struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index(
240 struct bt_trace_class *tc, uint64_t index)
241{
bdb288b3
PP
242 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
243 BT_ASSERT_PRE_DEV_VALID_INDEX(index, tc->stream_classes->len);
862ca4ed
PP
244 return g_ptr_array_index(tc->stream_classes, index);
245}
246
247const struct bt_stream_class *
248bt_trace_class_borrow_stream_class_by_index_const(
249 const struct bt_trace_class *tc, uint64_t index)
250{
251 return bt_trace_class_borrow_stream_class_by_index(
252 (void *) tc, index);
253}
254
255struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id(
256 struct bt_trace_class *tc, uint64_t id)
257{
258 struct bt_stream_class *stream_class = NULL;
259 uint64_t i;
260
bdb288b3 261 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
262
263 for (i = 0; i < tc->stream_classes->len; i++) {
264 struct bt_stream_class *stream_class_candidate =
265 g_ptr_array_index(tc->stream_classes, i);
266
267 if (stream_class_candidate->id == id) {
268 stream_class = stream_class_candidate;
269 goto end;
270 }
271 }
272
273end:
274 return stream_class;
275}
276
277const struct bt_stream_class *
278bt_trace_class_borrow_stream_class_by_id_const(
279 const struct bt_trace_class *tc, uint64_t id)
280{
281 return bt_trace_class_borrow_stream_class_by_id((void *) tc, id);
282}
283
862ca4ed
PP
284BT_HIDDEN
285void _bt_trace_class_freeze(const struct bt_trace_class *tc)
286{
862ca4ed
PP
287 BT_ASSERT(tc);
288 BT_LIB_LOGD("Freezing trace class: %!+T", tc);
289 ((struct bt_trace_class *) tc)->frozen = true;
290}
291
292bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc)
293{
bdb288b3 294 BT_ASSERT_PRE_DEV_NON_NULL(tc, "Trace class");
862ca4ed
PP
295 return (bt_bool) tc->assigns_automatic_stream_class_id;
296}
297
298void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc,
299 bt_bool value)
300{
301 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
bdb288b3 302 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc);
862ca4ed 303 tc->assigns_automatic_stream_class_id = (bool) value;
3f7d4d90 304 BT_LIB_LOGD("Set trace class's automatic stream class ID "
862ca4ed
PP
305 "assignment property: %!+T", tc);
306}
c5b9b441
PP
307
308void bt_trace_class_get_ref(const struct bt_trace_class *trace_class)
309{
310 bt_object_get_ref(trace_class);
311}
312
313void bt_trace_class_put_ref(const struct bt_trace_class *trace_class)
314{
315 bt_object_put_ref(trace_class);
316}
This page took 0.046009 seconds and 4 git commands to generate.