lib: use BT_LIB_LOG*_APPEND_CAUSE() where appropriate
[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 "attributes.h"
47#include "clock-class.h"
48#include "event-class.h"
49#include "event.h"
50#include "field-class.h"
51#include "field-wrapper.h"
52#include "resolve-field-path.h"
53#include "stream-class.h"
54#include "stream.h"
55#include "trace.h"
56#include "utils.h"
d24d5663 57#include "lib/func-status.h"
578e048b 58
ad5268b5
FD
59struct bt_trace_class_destruction_listener_elem {
60 bt_trace_class_destruction_listener_func func;
61 void *data;
62};
63
862ca4ed
PP
64#define BT_ASSERT_PRE_TRACE_CLASS_HOT(_tc) \
65 BT_ASSERT_PRE_HOT((_tc), "Trace class", ": %!+T", (_tc))
66
67static
68void destroy_trace_class(struct bt_object *obj)
69{
70 struct bt_trace_class *tc = (void *) obj;
71
72 BT_LIB_LOGD("Destroying trace class object: %!+T", tc);
ad5268b5
FD
73 /*
74 * Call destruction listener functions so that everything else
75 * still exists in the trace class.
76 */
77 if (tc->destruction_listeners) {
78 uint64_t i;
3f7d4d90 79 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc);
c47a6bec
SM
80
81 /*
82 * The trace class' reference count is 0 if we're here. Increment
83 * it to avoid a double-destroy (possibly infinitely recursive).
84 * This could happen for example if a destruction listener did
85 * bt_object_get_ref() (or anything that causes
86 * bt_object_get_ref() to be called) on the trace class (ref.
87 * count goes from 0 to 1), and then bt_object_put_ref(): the
88 * reference count would go from 1 to 0 again and this function
89 * would be called again.
90 */
91 tc->base.ref_count++;
92
ad5268b5
FD
93 /* Call all the trace class destruction listeners */
94 for (i = 0; i < tc->destruction_listeners->len; i++) {
95 struct bt_trace_class_destruction_listener_elem elem =
96 g_array_index(tc->destruction_listeners,
97 struct bt_trace_class_destruction_listener_elem, i);
98
99 if (elem.func) {
100 elem.func(tc, elem.data);
101 }
c47a6bec
SM
102
103 /*
104 * The destruction listener should not have kept a
105 * reference to the trace class.
106 */
107 BT_ASSERT_PRE(tc->base.ref_count == 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc);
ad5268b5
FD
108 }
109 g_array_free(tc->destruction_listeners, TRUE);
110 tc->destruction_listeners = NULL;
111 }
862ca4ed
PP
112
113 if (tc->environment) {
114 BT_LOGD_STR("Destroying environment attributes.");
115 bt_attributes_destroy(tc->environment);
116 tc->environment = NULL;
117 }
118
119 if (tc->name.str) {
120 g_string_free(tc->name.str, TRUE);
121 tc->name.str = NULL;
122 tc->name.value = NULL;
123 }
124
125 if (tc->stream_classes) {
126 BT_LOGD_STR("Destroying stream classes.");
127 g_ptr_array_free(tc->stream_classes, TRUE);
128 tc->stream_classes = NULL;
129 }
130
862ca4ed
PP
131 g_free(tc);
132}
133
41693723 134struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp)
862ca4ed
PP
135{
136 struct bt_trace_class *tc = NULL;
862ca4ed 137
41693723 138 BT_ASSERT_PRE_NON_NULL(self_comp, "Self component");
862ca4ed
PP
139 BT_LOGD_STR("Creating default trace class object.");
140 tc = g_new0(struct bt_trace_class, 1);
141 if (!tc) {
870631a2 142 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
862ca4ed
PP
143 goto error;
144 }
145
146 bt_object_init_shared_with_parent(&tc->base, destroy_trace_class);
147
148 tc->stream_classes = g_ptr_array_new_with_free_func(
149 (GDestroyNotify) bt_object_try_spec_release);
150 if (!tc->stream_classes) {
870631a2 151 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
862ca4ed
PP
152 goto error;
153 }
154
155 tc->name.str = g_string_new(NULL);
156 if (!tc->name.str) {
870631a2 157 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
862ca4ed
PP
158 goto error;
159 }
160
161 tc->environment = bt_attributes_create();
162 if (!tc->environment) {
870631a2 163 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object.");
862ca4ed
PP
164 goto error;
165 }
166
ad5268b5
FD
167 tc->destruction_listeners = g_array_new(FALSE, TRUE,
168 sizeof(struct bt_trace_class_destruction_listener_elem));
169 if (!tc->destruction_listeners) {
870631a2 170 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
ad5268b5
FD
171 goto error;
172 }
173
862ca4ed 174 tc->assigns_automatic_stream_class_id = true;
862ca4ed
PP
175 BT_LIB_LOGD("Created trace class object: %!+T", tc);
176 goto end;
177
178error:
179 BT_OBJECT_PUT_REF_AND_RESET(tc);
180
181end:
182 return tc;
183}
184
185const char *bt_trace_class_get_name(const struct bt_trace_class *tc)
186{
187 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
188 return tc->name.value;
189}
190
d24d5663 191enum bt_trace_class_set_name_status bt_trace_class_set_name(
a1ed915c 192 struct bt_trace_class *tc, const char *name)
862ca4ed
PP
193{
194 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
195 BT_ASSERT_PRE_NON_NULL(name, "Name");
196 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
197 g_string_assign(tc->name.str, name);
198 tc->name.value = tc->name.str->str;
3f7d4d90 199 BT_LIB_LOGD("Set trace class's name: %!+T", tc);
d24d5663 200 return BT_FUNC_STATUS_OK;
862ca4ed
PP
201}
202
203bt_uuid bt_trace_class_get_uuid(const struct bt_trace_class *tc)
204{
205 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
206 return tc->uuid.value;
207}
208
209void bt_trace_class_set_uuid(struct bt_trace_class *tc, bt_uuid uuid)
210{
211 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
212 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
213 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
214 memcpy(tc->uuid.uuid, uuid, BABELTRACE_UUID_LEN);
215 tc->uuid.value = tc->uuid.uuid;
3f7d4d90 216 BT_LIB_LOGD("Set trace class's UUID: %!+T", tc);
862ca4ed
PP
217}
218
d24d5663 219enum bt_trace_class_add_listener_status bt_trace_class_add_destruction_listener(
ad5268b5
FD
220 const struct bt_trace_class *_tc,
221 bt_trace_class_destruction_listener_func listener,
222 void *data, uint64_t *listener_id)
223{
224 struct bt_trace_class *tc = (void *) _tc;
225 uint64_t i;
226 struct bt_trace_class_destruction_listener_elem new_elem = {
227 .func = listener,
228 .data = data,
229 };
230
231 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
232 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
233
234 /* Find the next available spot */
235 for (i = 0; i < tc->destruction_listeners->len; i++) {
236 struct bt_trace_class_destruction_listener_elem elem =
237 g_array_index(tc->destruction_listeners,
238 struct bt_trace_class_destruction_listener_elem, i);
239
240 if (!elem.func) {
241 break;
242 }
243 }
244
245 if (i == tc->destruction_listeners->len) {
246 g_array_append_val(tc->destruction_listeners, new_elem);
247 } else {
248 g_array_insert_val(tc->destruction_listeners, i, new_elem);
249 }
250
251 if (listener_id) {
252 *listener_id = i;
253 }
254
3f7d4d90 255 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
ad5268b5 256 "listener-id=%" PRIu64, tc, i);
d24d5663 257 return BT_FUNC_STATUS_OK;
ad5268b5
FD
258}
259
260BT_ASSERT_PRE_FUNC
261static
262bool has_listener_id(const struct bt_trace_class *tc, uint64_t listener_id)
263{
264 BT_ASSERT(listener_id < tc->destruction_listeners->len);
265 return (&g_array_index(tc->destruction_listeners,
266 struct bt_trace_class_destruction_listener_elem,
267 listener_id))->func != NULL;
268}
269
d24d5663 270enum bt_trace_class_remove_listener_status bt_trace_class_remove_destruction_listener(
ad5268b5
FD
271 const struct bt_trace_class *_tc, uint64_t listener_id)
272{
273 struct bt_trace_class *tc = (void *) _tc;
274 struct bt_trace_class_destruction_listener_elem *elem;
275
276 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
277 BT_ASSERT_PRE(has_listener_id(tc, listener_id),
278 "Trace class has no such trace class destruction listener ID: "
279 "%![tc-]+T, %" PRIu64, tc, listener_id);
280 elem = &g_array_index(tc->destruction_listeners,
281 struct bt_trace_class_destruction_listener_elem,
282 listener_id);
283 BT_ASSERT(elem->func);
284
285 elem->func = NULL;
286 elem->data = NULL;
3f7d4d90 287 BT_LIB_LOGD("Removed trace class destruction listener: "
ad5268b5
FD
288 "%![tc-]+T, listener-id=%" PRIu64,
289 tc, listener_id);
d24d5663 290 return BT_FUNC_STATUS_OK;
ad5268b5
FD
291}
292
862ca4ed
PP
293BT_ASSERT_FUNC
294static
295bool trace_has_environment_entry(const struct bt_trace_class *tc, const char *name)
296{
297 BT_ASSERT(tc);
298
299 return bt_attributes_borrow_field_value_by_name(
300 tc->environment, name) != NULL;
301}
302
303static
d24d5663
PP
304enum bt_trace_class_set_environment_entry_status set_environment_entry(
305 struct bt_trace_class *tc,
a1ed915c 306 const char *name, struct bt_value *value)
862ca4ed
PP
307{
308 int ret;
309
310 BT_ASSERT(tc);
311 BT_ASSERT(name);
312 BT_ASSERT(value);
313 BT_ASSERT_PRE(!tc->frozen ||
314 !trace_has_environment_entry(tc, name),
315 "Trace class is frozen: cannot replace environment entry: "
316 "%![tc-]+T, entry-name=\"%s\"", tc, name);
317 ret = bt_attributes_set_field_value(tc->environment, name,
318 value);
862ca4ed 319 if (ret) {
d24d5663 320 ret = BT_FUNC_STATUS_MEMORY_ERROR;
870631a2
PP
321 BT_LIB_LOGE_APPEND_CAUSE(
322 "Cannot set trace class's environment entry: "
862ca4ed
PP
323 "%![tc-]+T, entry-name=\"%s\"", tc, name);
324 } else {
a1ed915c 325 bt_value_freeze(value);
3f7d4d90 326 BT_LIB_LOGD("Set trace class's environment entry: "
862ca4ed
PP
327 "%![tc-]+T, entry-name=\"%s\"", tc, name);
328 }
329
330 return ret;
331}
332
d24d5663
PP
333enum bt_trace_class_set_environment_entry_status
334bt_trace_class_set_environment_entry_string(
862ca4ed
PP
335 struct bt_trace_class *tc, const char *name, const char *value)
336{
337 int ret;
338 struct bt_value *value_obj;
339 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
340 BT_ASSERT_PRE_NON_NULL(name, "Name");
341 BT_ASSERT_PRE_NON_NULL(value, "Value");
342 value_obj = bt_value_string_create_init(value);
343 if (!value_obj) {
870631a2
PP
344 BT_LIB_LOGE_APPEND_CAUSE(
345 "Cannot create a string value object.");
862ca4ed
PP
346 ret = -1;
347 goto end;
348 }
349
350 /* set_environment_entry() logs errors */
351 ret = set_environment_entry(tc, name, value_obj);
352
353end:
354 bt_object_put_ref(value_obj);
355 return ret;
356}
357
d24d5663
PP
358enum bt_trace_class_set_environment_entry_status
359bt_trace_class_set_environment_entry_integer(
a1ed915c 360 struct bt_trace_class *tc, const char *name, int64_t value)
862ca4ed
PP
361{
362 int ret;
363 struct bt_value *value_obj;
364 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
365 BT_ASSERT_PRE_NON_NULL(name, "Name");
fdd3a2da 366 value_obj = bt_value_signed_integer_create_init(value);
862ca4ed 367 if (!value_obj) {
870631a2
PP
368 BT_LIB_LOGE_APPEND_CAUSE(
369 "Cannot create an integer value object.");
d24d5663 370 ret = BT_FUNC_STATUS_MEMORY_ERROR;
862ca4ed
PP
371 goto end;
372 }
373
374 /* set_environment_entry() logs errors */
375 ret = set_environment_entry(tc, name, value_obj);
376
377end:
378 bt_object_put_ref(value_obj);
379 return ret;
380}
381
382uint64_t bt_trace_class_get_environment_entry_count(const struct bt_trace_class *tc)
383{
384 int64_t ret;
385
386 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
387 ret = bt_attributes_get_count(tc->environment);
388 BT_ASSERT(ret >= 0);
389 return (uint64_t) ret;
390}
391
392void bt_trace_class_borrow_environment_entry_by_index_const(
393 const struct bt_trace_class *tc, uint64_t index,
394 const char **name, const struct bt_value **value)
395{
396 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
397 BT_ASSERT_PRE_NON_NULL(name, "Name");
398 BT_ASSERT_PRE_NON_NULL(value, "Value");
399 BT_ASSERT_PRE_VALID_INDEX(index,
400 bt_attributes_get_count(tc->environment));
401 *value = bt_attributes_borrow_field_value(tc->environment, index);
402 BT_ASSERT(*value);
403 *name = bt_attributes_get_field_name(tc->environment, index);
404 BT_ASSERT(*name);
405}
406
407const struct bt_value *bt_trace_class_borrow_environment_entry_value_by_name_const(
408 const struct bt_trace_class *tc, const char *name)
409{
410 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
411 BT_ASSERT_PRE_NON_NULL(name, "Name");
412 return bt_attributes_borrow_field_value_by_name(tc->environment,
413 name);
414}
415
416uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc)
417{
418 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
419 return (uint64_t) tc->stream_classes->len;
420}
421
422struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index(
423 struct bt_trace_class *tc, uint64_t index)
424{
425 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
426 BT_ASSERT_PRE_VALID_INDEX(index, tc->stream_classes->len);
427 return g_ptr_array_index(tc->stream_classes, index);
428}
429
430const struct bt_stream_class *
431bt_trace_class_borrow_stream_class_by_index_const(
432 const struct bt_trace_class *tc, uint64_t index)
433{
434 return bt_trace_class_borrow_stream_class_by_index(
435 (void *) tc, index);
436}
437
438struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id(
439 struct bt_trace_class *tc, uint64_t id)
440{
441 struct bt_stream_class *stream_class = NULL;
442 uint64_t i;
443
444 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
445
446 for (i = 0; i < tc->stream_classes->len; i++) {
447 struct bt_stream_class *stream_class_candidate =
448 g_ptr_array_index(tc->stream_classes, i);
449
450 if (stream_class_candidate->id == id) {
451 stream_class = stream_class_candidate;
452 goto end;
453 }
454 }
455
456end:
457 return stream_class;
458}
459
460const struct bt_stream_class *
461bt_trace_class_borrow_stream_class_by_id_const(
462 const struct bt_trace_class *tc, uint64_t id)
463{
464 return bt_trace_class_borrow_stream_class_by_id((void *) tc, id);
465}
466
862ca4ed
PP
467BT_HIDDEN
468void _bt_trace_class_freeze(const struct bt_trace_class *tc)
469{
862ca4ed
PP
470 BT_ASSERT(tc);
471 BT_LIB_LOGD("Freezing trace class: %!+T", tc);
472 ((struct bt_trace_class *) tc)->frozen = true;
473}
474
475bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc)
476{
477 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
478 return (bt_bool) tc->assigns_automatic_stream_class_id;
479}
480
481void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc,
482 bt_bool value)
483{
484 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
485 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
486 tc->assigns_automatic_stream_class_id = (bool) value;
3f7d4d90 487 BT_LIB_LOGD("Set trace class's automatic stream class ID "
862ca4ed
PP
488 "assignment property: %!+T", tc);
489}
c5b9b441
PP
490
491void bt_trace_class_get_ref(const struct bt_trace_class *trace_class)
492{
493 bt_object_get_ref(trace_class);
494}
495
496void bt_trace_class_put_ref(const struct bt_trace_class *trace_class)
497{
498 bt_object_put_ref(trace_class);
499}
This page took 0.050608 seconds and 4 git commands to generate.