flt.utils.muxer: add IWYU pragma
[babeltrace.git] / src / lib / trace-ir / trace.c
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8#define BT_LOG_TAG "LIB/TRACE"
9#include "lib/logging.h"
10
11#include "lib/assert-cond.h"
12#include <babeltrace2/trace-ir/trace.h>
13#include <babeltrace2/trace-ir/event-class.h>
14#include "compat/compiler.h"
15#include <babeltrace2/value.h>
16#include "lib/value.h"
17#include <babeltrace2/types.h>
18#include "compat/endian.h"
19#include "common/assert.h"
20#include "compat/glib.h"
21#include <inttypes.h>
22#include <stdint.h>
23#include <string.h>
24#include <stdbool.h>
25#include <stdlib.h>
26
27#include "attributes.h"
28#include "stream-class.h"
29#include "stream.h"
30#include "trace-class.h"
31#include "trace.h"
32#include "lib/value.h"
33#include "lib/func-status.h"
34
35struct bt_trace_destruction_listener_elem {
36 bt_trace_destruction_listener_func func;
37 void *data;
38};
39
40#define BT_ASSERT_PRE_DEV_TRACE_HOT(_trace) \
41 BT_ASSERT_PRE_DEV_HOT("trace", (_trace), "Trace", ": %!+t", (_trace))
42
43#define DESTRUCTION_LISTENER_FUNC_NAME \
44 "bt_trace_class_destruction_listener_func"
45
46static
47void destroy_trace(struct bt_object *obj)
48{
49 struct bt_trace *trace = (void *) obj;
50
51 BT_LIB_LOGD("Destroying trace object: %!+t", trace);
52 BT_OBJECT_PUT_REF_AND_RESET(trace->user_attributes);
53
54 /*
55 * Call destruction listener functions so that everything else
56 * still exists in the trace.
57 */
58 if (trace->destruction_listeners) {
59 uint64_t i;
60 const struct bt_error *saved_error;
61
62 BT_LIB_LOGD("Calling trace destruction listener(s): %!+t", trace);
63
64 /*
65 * The trace's reference count is 0 if we're here. Increment
66 * it to avoid a double-destroy (possibly infinitely recursive).
67 * This could happen for example if a destruction listener did
68 * bt_object_get_ref() (or anything that causes
69 * bt_object_get_ref() to be called) on the trace (ref.
70 * count goes from 0 to 1), and then bt_object_put_ref(): the
71 * reference count would go from 1 to 0 again and this function
72 * would be called again.
73 */
74 trace->base.ref_count++;
75
76 saved_error = bt_current_thread_take_error();
77
78 /* Call all the trace destruction listeners */
79 for (i = 0; i < trace->destruction_listeners->len; i++) {
80 struct bt_trace_destruction_listener_elem elem =
81 bt_g_array_index(trace->destruction_listeners,
82 struct bt_trace_destruction_listener_elem, i);
83
84 if (elem.func) {
85 elem.func(trace, elem.data);
86 BT_ASSERT_POST_NO_ERROR(
87 DESTRUCTION_LISTENER_FUNC_NAME);
88 }
89
90 /*
91 * The destruction listener should not have kept a
92 * reference to the trace.
93 */
94 BT_ASSERT_POST(DESTRUCTION_LISTENER_FUNC_NAME,
95 "trace-reference-count-not-changed",
96 trace->base.ref_count == 1,
97 "Destruction listener kept a reference to the trace being destroyed: %![trace-]+t",
98 trace);
99 }
100 g_array_free(trace->destruction_listeners, TRUE);
101 trace->destruction_listeners = NULL;
102
103 if (saved_error) {
104 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
105 }
106 }
107
108 g_free(trace->ns);
109 g_free(trace->name);
110
111 if (trace->class->mip_version >= 1) {
112 g_free(trace->uid_or_uuid.uid);
113 trace->uid_or_uuid.uid = NULL;
114 }
115
116 if (trace->environment) {
117 BT_LOGD_STR("Destroying environment attributes.");
118 bt_attributes_destroy(trace->environment);
119 trace->environment = NULL;
120 }
121
122 if (trace->streams) {
123 BT_LOGD_STR("Destroying streams.");
124 g_ptr_array_free(trace->streams, TRUE);
125 trace->streams = NULL;
126 }
127
128 if (trace->stream_classes_stream_count) {
129 g_hash_table_destroy(trace->stream_classes_stream_count);
130 trace->stream_classes_stream_count = NULL;
131 }
132
133 BT_LOGD_STR("Putting trace's class.");
134 bt_object_put_ref(trace->class);
135 trace->class = NULL;
136 g_free(trace);
137}
138
139BT_EXPORT
140struct bt_trace *bt_trace_create(struct bt_trace_class *tc)
141{
142 struct bt_trace *trace = NULL;
143
144 BT_ASSERT_PRE_NO_ERROR();
145
146 BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc);
147 trace = g_new0(struct bt_trace, 1);
148 if (!trace) {
149 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace.");
150 goto error;
151 }
152
153 bt_object_init_shared(&trace->base, destroy_trace);
154 trace->user_attributes = bt_value_map_create();
155 if (!trace->user_attributes) {
156 BT_LIB_LOGE_APPEND_CAUSE(
157 "Failed to create a map value object.");
158 goto error;
159 }
160
161 trace->streams = g_ptr_array_new_with_free_func(
162 (GDestroyNotify) bt_object_try_spec_release);
163 if (!trace->streams) {
164 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
165 goto error;
166 }
167
168 trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash,
169 g_direct_equal);
170 if (!trace->stream_classes_stream_count) {
171 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable.");
172 goto error;
173 }
174
175 trace->environment = bt_attributes_create();
176 if (!trace->environment) {
177 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object.");
178 goto error;
179 }
180
181 trace->destruction_listeners = g_array_new(FALSE, TRUE,
182 sizeof(struct bt_trace_destruction_listener_elem));
183 if (!trace->destruction_listeners) {
184 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
185 goto error;
186 }
187
188 trace->class = tc;
189 bt_object_get_ref_no_null_check(trace->class);
190 BT_LIB_LOGD("Created trace object: %!+t", trace);
191 goto end;
192
193error:
194 BT_OBJECT_PUT_REF_AND_RESET(trace);
195
196end:
197 return trace;
198}
199
200BT_EXPORT
201const char *bt_trace_get_namespace(const struct bt_trace *trace)
202{
203 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
204 BT_ASSERT_PRE_TRACE_MIP_VERSION_GE(trace, 1);
205 return trace->ns;
206}
207
208BT_EXPORT
209enum bt_trace_set_namespace_status bt_trace_set_namespace(
210 struct bt_trace *trace, const char *ns)
211{
212 BT_ASSERT_PRE_NO_ERROR();
213 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
214 BT_ASSERT_PRE_NAME_NON_NULL(ns);
215 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
216 BT_ASSERT_PRE_TRACE_MIP_VERSION_GE(trace, 1);
217 g_free(trace->ns);
218 trace->ns = g_strdup(ns);
219 BT_LIB_LOGD("Set trace's namespace: %!+t", trace);
220 return BT_FUNC_STATUS_OK;
221}
222
223BT_EXPORT
224const char *bt_trace_get_name(const struct bt_trace *trace)
225{
226 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
227 return trace->name;
228}
229
230BT_EXPORT
231enum bt_trace_set_name_status bt_trace_set_name(struct bt_trace *trace,
232 const char *name)
233{
234 BT_ASSERT_PRE_NO_ERROR();
235 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
236 BT_ASSERT_PRE_NAME_NON_NULL(name);
237 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
238 g_free(trace->name);
239 trace->name = g_strdup(name);
240 BT_LIB_LOGD("Set trace's name: %!+t", trace);
241 return BT_FUNC_STATUS_OK;
242}
243
244BT_EXPORT
245bt_uuid bt_trace_get_uuid(const struct bt_trace *trace)
246{
247 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
248 BT_ASSERT_PRE_TC_MIP_VERSION_EQ(trace->class, 0);
249 return trace->uid_or_uuid.uuid.value;
250}
251
252BT_EXPORT
253const char *bt_trace_get_uid(const bt_trace *trace)
254{
255 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
256 BT_ASSERT_PRE_TC_MIP_VERSION_GE(trace->class, 1);
257 return trace->uid_or_uuid.uid;
258}
259
260BT_EXPORT
261void bt_trace_set_uuid(struct bt_trace *trace, bt_uuid uuid)
262{
263 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
264 BT_ASSERT_PRE_UUID_NON_NULL(uuid);
265 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
266 BT_ASSERT_PRE_TC_MIP_VERSION_EQ(trace->class, 0);
267 bt_uuid_copy(trace->uid_or_uuid.uuid.uuid, uuid);
268 trace->uid_or_uuid.uuid.value = trace->uid_or_uuid.uuid.uuid;
269 BT_LIB_LOGD("Set trace's UUID: %!+t", trace);
270}
271
272BT_EXPORT
273enum bt_trace_set_uid_status bt_trace_set_uid(bt_trace *trace, const char *uid)
274{
275 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
276 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
277 BT_ASSERT_PRE_TC_MIP_VERSION_GE(trace->class, 1);
278 BT_ASSERT_PRE_UID_NON_NULL(uid);
279 g_free(trace->uid_or_uuid.uid);
280 trace->uid_or_uuid.uid = g_strdup(uid);
281 BT_LIB_LOGD("Set trace's UID: %!+t", trace);
282 return BT_FUNC_STATUS_OK;
283}
284
285static
286bool trace_has_environment_entry(const struct bt_trace *trace, const char *name)
287{
288 BT_ASSERT(trace);
289
290 return bt_attributes_borrow_field_value_by_name(
291 trace->environment, name);
292}
293
294static
295enum bt_trace_set_environment_entry_status set_environment_entry(
296 struct bt_trace *trace,
297 const char *name, struct bt_value *value)
298{
299 int ret;
300
301 BT_ASSERT(trace);
302 BT_ASSERT(name);
303 BT_ASSERT(value);
304 BT_ASSERT_PRE("not-frozen:trace",
305 !trace->frozen ||
306 !trace_has_environment_entry(trace, name),
307 "Trace is frozen: cannot replace environment entry: "
308 "%![trace-]+t, entry-name=\"%s\"", trace, name);
309 ret = bt_attributes_set_field_value(trace->environment, name,
310 value);
311 if (ret) {
312 ret = BT_FUNC_STATUS_MEMORY_ERROR;
313 BT_LIB_LOGE_APPEND_CAUSE(
314 "Cannot set trace's environment entry: "
315 "%![trace-]+t, entry-name=\"%s\"", trace, name);
316 } else {
317 bt_value_freeze(value);
318 BT_LIB_LOGD("Set trace's environment entry: "
319 "%![trace-]+t, entry-name=\"%s\"", trace, name);
320 }
321
322 return ret;
323}
324
325BT_EXPORT
326enum bt_trace_set_environment_entry_status
327bt_trace_set_environment_entry_string(
328 struct bt_trace *trace, const char *name, const char *value)
329{
330 int ret;
331 struct bt_value *value_obj;
332
333 BT_ASSERT_PRE_NO_ERROR();
334 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
335 BT_ASSERT_PRE_NAME_NON_NULL(name);
336 BT_ASSERT_PRE_NON_NULL("value", value, "Value");
337
338 value_obj = bt_value_string_create_init(value);
339 if (!value_obj) {
340 BT_LIB_LOGE_APPEND_CAUSE(
341 "Cannot create a string value object.");
342 ret = -1;
343 goto end;
344 }
345
346 /* set_environment_entry() logs errors */
347 ret = set_environment_entry(trace, name, value_obj);
348
349end:
350 bt_object_put_ref(value_obj);
351 return ret;
352}
353
354BT_EXPORT
355enum bt_trace_set_environment_entry_status
356bt_trace_set_environment_entry_integer(
357 struct bt_trace *trace, const char *name, int64_t value)
358{
359 int ret;
360 struct bt_value *value_obj;
361
362 BT_ASSERT_PRE_NO_ERROR();
363 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
364 BT_ASSERT_PRE_NAME_NON_NULL(name);
365
366 value_obj = bt_value_integer_signed_create_init(value);
367 if (!value_obj) {
368 BT_LIB_LOGE_APPEND_CAUSE(
369 "Cannot create an integer value object.");
370 ret = BT_FUNC_STATUS_MEMORY_ERROR;
371 goto end;
372 }
373
374 /* set_environment_entry() logs errors */
375 ret = set_environment_entry(trace, name, value_obj);
376
377end:
378 bt_object_put_ref(value_obj);
379 return ret;
380}
381
382BT_EXPORT
383uint64_t bt_trace_get_environment_entry_count(const struct bt_trace *trace)
384{
385 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
386 return bt_attributes_get_count(trace->environment);
387}
388
389BT_EXPORT
390void bt_trace_borrow_environment_entry_by_index_const(
391 const struct bt_trace *trace, uint64_t index,
392 const char **name, const struct bt_value **value)
393{
394 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
395 BT_ASSERT_PRE_DEV_NAME_NON_NULL(name);
396 BT_ASSERT_PRE_DEV_NON_NULL("value-object-output", value,
397 "Value object (output)");
398 BT_ASSERT_PRE_DEV_VALID_INDEX(index,
399 bt_attributes_get_count(trace->environment));
400 *value = bt_attributes_borrow_field_value(trace->environment, index);
401 BT_ASSERT(*value);
402 *name = bt_attributes_get_field_name(trace->environment, index);
403 BT_ASSERT(*name);
404}
405
406BT_EXPORT
407const struct bt_value *bt_trace_borrow_environment_entry_value_by_name_const(
408 const struct bt_trace *trace, const char *name)
409{
410 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
411 BT_ASSERT_PRE_DEV_NAME_NON_NULL(name);
412 return bt_attributes_borrow_field_value_by_name(trace->environment,
413 name);
414}
415
416BT_EXPORT
417uint64_t bt_trace_get_stream_count(const struct bt_trace *trace)
418{
419 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
420 return (uint64_t) trace->streams->len;
421}
422
423BT_EXPORT
424struct bt_stream *bt_trace_borrow_stream_by_index(
425 struct bt_trace *trace, uint64_t index)
426{
427 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
428 BT_ASSERT_PRE_DEV_VALID_INDEX(index, trace->streams->len);
429 return g_ptr_array_index(trace->streams, index);
430}
431
432BT_EXPORT
433const struct bt_stream *bt_trace_borrow_stream_by_index_const(
434 const struct bt_trace *trace, uint64_t index)
435{
436 return bt_trace_borrow_stream_by_index((void *) trace, index);
437}
438
439BT_EXPORT
440struct bt_stream *bt_trace_borrow_stream_by_id(struct bt_trace *trace,
441 uint64_t id)
442{
443 struct bt_stream *stream = NULL;
444 uint64_t i;
445
446 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
447
448 for (i = 0; i < trace->streams->len; i++) {
449 struct bt_stream *stream_candidate =
450 g_ptr_array_index(trace->streams, i);
451
452 if (stream_candidate->id == id) {
453 stream = stream_candidate;
454 goto end;
455 }
456 }
457
458end:
459 return stream;
460}
461
462BT_EXPORT
463const struct bt_stream *bt_trace_borrow_stream_by_id_const(
464 const struct bt_trace *trace, uint64_t id)
465{
466 return bt_trace_borrow_stream_by_id((void *) trace, id);
467}
468
469BT_EXPORT
470enum bt_trace_add_listener_status bt_trace_add_destruction_listener(
471 const struct bt_trace *c_trace,
472 bt_trace_destruction_listener_func listener,
473 void *data, bt_listener_id *listener_id)
474{
475 struct bt_trace *trace = (void *) c_trace;
476 uint64_t i;
477 struct bt_trace_destruction_listener_elem new_elem = {
478 .func = listener,
479 .data = data,
480 };
481
482 BT_ASSERT_PRE_NO_ERROR();
483 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
484 BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(listener);
485
486 /* Find the next available spot */
487 for (i = 0; i < trace->destruction_listeners->len; i++) {
488 struct bt_trace_destruction_listener_elem elem =
489 bt_g_array_index(trace->destruction_listeners,
490 struct bt_trace_destruction_listener_elem, i);
491
492 if (!elem.func) {
493 break;
494 }
495 }
496
497 if (i == trace->destruction_listeners->len) {
498 g_array_append_val(trace->destruction_listeners, new_elem);
499 } else {
500 g_array_insert_val(trace->destruction_listeners, i, new_elem);
501 }
502
503 if (listener_id) {
504 *listener_id = i;
505 }
506
507 BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, "
508 "listener-id=%" PRIu64, trace, i);
509 return BT_FUNC_STATUS_OK;
510}
511
512static
513bool has_listener_id(const struct bt_trace *trace, uint64_t listener_id)
514{
515 BT_ASSERT(listener_id < trace->destruction_listeners->len);
516 return (&bt_g_array_index(trace->destruction_listeners,
517 struct bt_trace_destruction_listener_elem,
518 listener_id))->func;
519}
520
521BT_EXPORT
522enum bt_trace_remove_listener_status bt_trace_remove_destruction_listener(
523 const struct bt_trace *c_trace, bt_listener_id listener_id)
524{
525 struct bt_trace *trace = (void *) c_trace;
526 struct bt_trace_destruction_listener_elem *elem;
527
528 BT_ASSERT_PRE_NO_ERROR();
529 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
530 BT_ASSERT_PRE("listener-id-exists",
531 has_listener_id(trace, listener_id),
532 "Trace has no such trace destruction listener ID: "
533 "%![trace-]+t, %" PRIu64, trace, listener_id);
534 elem = &bt_g_array_index(trace->destruction_listeners,
535 struct bt_trace_destruction_listener_elem,
536 listener_id);
537 BT_ASSERT(elem->func);
538
539 elem->func = NULL;
540 elem->data = NULL;
541 BT_LIB_LOGD("Removed \"trace destruction listener: "
542 "%![trace-]+t, listener-id=%" PRIu64,
543 trace, listener_id);
544 return BT_FUNC_STATUS_OK;
545}
546
547void _bt_trace_freeze(const struct bt_trace *trace)
548{
549 BT_ASSERT(trace);
550 BT_LIB_LOGD("Freezing trace's class: %!+T", trace->class);
551 bt_trace_class_freeze(trace->class);
552 BT_LIB_LOGD("Freezing trace's user attributes: %!+v",
553 trace->user_attributes);
554 bt_value_freeze(trace->user_attributes);
555 BT_LIB_LOGD("Freezing trace: %!+t", trace);
556 ((struct bt_trace *) trace)->frozen = true;
557}
558
559void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream)
560{
561 guint count = 0;
562
563 bt_object_set_parent(&stream->base, &trace->base);
564 g_ptr_array_add(trace->streams, stream);
565 bt_trace_freeze(trace);
566
567 if (bt_g_hash_table_contains(trace->stream_classes_stream_count,
568 stream->class)) {
569 count = GPOINTER_TO_UINT(g_hash_table_lookup(
570 trace->stream_classes_stream_count, stream->class));
571 }
572
573 g_hash_table_insert(trace->stream_classes_stream_count,
574 stream->class, GUINT_TO_POINTER(count + 1));
575}
576
577uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace *trace,
578 const struct bt_stream_class *stream_class)
579{
580 gpointer orig_key;
581 gpointer value;
582 uint64_t id = 0;
583
584 BT_ASSERT(stream_class);
585 BT_ASSERT(trace);
586 if (g_hash_table_lookup_extended(trace->stream_classes_stream_count,
587 stream_class, &orig_key, &value)) {
588 id = (uint64_t) GPOINTER_TO_UINT(value);
589 }
590
591 return id;
592}
593
594BT_EXPORT
595struct bt_trace_class *bt_trace_borrow_class(struct bt_trace *trace)
596{
597 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
598 return trace->class;
599}
600
601BT_EXPORT
602const struct bt_trace_class *bt_trace_borrow_class_const(
603 const struct bt_trace *trace)
604{
605 return bt_trace_borrow_class((void *) trace);
606}
607
608BT_EXPORT
609const struct bt_value *bt_trace_borrow_user_attributes_const(
610 const struct bt_trace *trace)
611{
612 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
613 return trace->user_attributes;
614}
615
616BT_EXPORT
617struct bt_value *bt_trace_borrow_user_attributes(struct bt_trace *trace)
618{
619 return (void *) bt_trace_borrow_user_attributes_const((void *) trace);
620}
621
622BT_EXPORT
623void bt_trace_set_user_attributes(
624 struct bt_trace *trace,
625 const struct bt_value *user_attributes)
626{
627 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
628 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
629 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
630 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
631 bt_object_put_ref_no_null_check(trace->user_attributes);
632 trace->user_attributes = (void *) user_attributes;
633 bt_object_get_ref_no_null_check(trace->user_attributes);
634}
635
636BT_EXPORT
637void bt_trace_get_ref(const struct bt_trace *trace)
638{
639 bt_object_get_ref(trace);
640}
641
642BT_EXPORT
643void bt_trace_put_ref(const struct bt_trace *trace)
644{
645 bt_object_put_ref(trace);
646}
This page took 0.026109 seconds and 5 git commands to generate.