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