doc/api/libbabeltrace2/DoxygenLayout.xml: use `topics` tab
[babeltrace.git] / src / lib / trace-ir / trace.c
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
35 struct 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
46 static
47 void 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->name);
109
110 if (trace->environment) {
111 BT_LOGD_STR("Destroying environment attributes.");
112 bt_attributes_destroy(trace->environment);
113 trace->environment = NULL;
114 }
115
116 if (trace->streams) {
117 BT_LOGD_STR("Destroying streams.");
118 g_ptr_array_free(trace->streams, TRUE);
119 trace->streams = NULL;
120 }
121
122 if (trace->stream_classes_stream_count) {
123 g_hash_table_destroy(trace->stream_classes_stream_count);
124 trace->stream_classes_stream_count = NULL;
125 }
126
127 BT_LOGD_STR("Putting trace's class.");
128 bt_object_put_ref(trace->class);
129 trace->class = NULL;
130 g_free(trace);
131 }
132
133 BT_EXPORT
134 struct bt_trace *bt_trace_create(struct bt_trace_class *tc)
135 {
136 struct bt_trace *trace = NULL;
137
138 BT_ASSERT_PRE_NO_ERROR();
139
140 BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc);
141 trace = g_new0(struct bt_trace, 1);
142 if (!trace) {
143 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace.");
144 goto error;
145 }
146
147 bt_object_init_shared(&trace->base, destroy_trace);
148 trace->user_attributes = bt_value_map_create();
149 if (!trace->user_attributes) {
150 BT_LIB_LOGE_APPEND_CAUSE(
151 "Failed to create a map value object.");
152 goto error;
153 }
154
155 trace->streams = g_ptr_array_new_with_free_func(
156 (GDestroyNotify) bt_object_try_spec_release);
157 if (!trace->streams) {
158 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
159 goto error;
160 }
161
162 trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash,
163 g_direct_equal);
164 if (!trace->stream_classes_stream_count) {
165 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable.");
166 goto error;
167 }
168
169 trace->environment = bt_attributes_create();
170 if (!trace->environment) {
171 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object.");
172 goto error;
173 }
174
175 trace->destruction_listeners = g_array_new(FALSE, TRUE,
176 sizeof(struct bt_trace_destruction_listener_elem));
177 if (!trace->destruction_listeners) {
178 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
179 goto error;
180 }
181
182 trace->class = tc;
183 bt_object_get_ref_no_null_check(trace->class);
184 BT_LIB_LOGD("Created trace object: %!+t", trace);
185 goto end;
186
187 error:
188 BT_OBJECT_PUT_REF_AND_RESET(trace);
189
190 end:
191 return trace;
192 }
193
194 BT_EXPORT
195 const char *bt_trace_get_name(const struct bt_trace *trace)
196 {
197 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
198 return trace->name;
199 }
200
201 BT_EXPORT
202 enum bt_trace_set_name_status bt_trace_set_name(struct bt_trace *trace,
203 const char *name)
204 {
205 BT_ASSERT_PRE_NO_ERROR();
206 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
207 BT_ASSERT_PRE_NAME_NON_NULL(name);
208 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
209 g_free(trace->name);
210 trace->name = g_strdup(name);
211 BT_LIB_LOGD("Set trace's name: %!+t", trace);
212 return BT_FUNC_STATUS_OK;
213 }
214
215 BT_EXPORT
216 bt_uuid bt_trace_get_uuid(const struct bt_trace *trace)
217 {
218 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
219 return trace->uuid.value;
220 }
221
222 BT_EXPORT
223 void bt_trace_set_uuid(struct bt_trace *trace, bt_uuid uuid)
224 {
225 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
226 BT_ASSERT_PRE_UUID_NON_NULL(uuid);
227 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
228 bt_uuid_copy(trace->uuid.uuid, uuid);
229 trace->uuid.value = trace->uuid.uuid;
230 BT_LIB_LOGD("Set trace's UUID: %!+t", trace);
231 }
232
233 static
234 bool trace_has_environment_entry(const struct bt_trace *trace, const char *name)
235 {
236 BT_ASSERT(trace);
237
238 return bt_attributes_borrow_field_value_by_name(
239 trace->environment, name);
240 }
241
242 static
243 enum bt_trace_set_environment_entry_status set_environment_entry(
244 struct bt_trace *trace,
245 const char *name, struct bt_value *value)
246 {
247 int ret;
248
249 BT_ASSERT(trace);
250 BT_ASSERT(name);
251 BT_ASSERT(value);
252 BT_ASSERT_PRE("not-frozen:trace",
253 !trace->frozen ||
254 !trace_has_environment_entry(trace, name),
255 "Trace is frozen: cannot replace environment entry: "
256 "%![trace-]+t, entry-name=\"%s\"", trace, name);
257 ret = bt_attributes_set_field_value(trace->environment, name,
258 value);
259 if (ret) {
260 ret = BT_FUNC_STATUS_MEMORY_ERROR;
261 BT_LIB_LOGE_APPEND_CAUSE(
262 "Cannot set trace's environment entry: "
263 "%![trace-]+t, entry-name=\"%s\"", trace, name);
264 } else {
265 bt_value_freeze(value);
266 BT_LIB_LOGD("Set trace's environment entry: "
267 "%![trace-]+t, entry-name=\"%s\"", trace, name);
268 }
269
270 return ret;
271 }
272
273 BT_EXPORT
274 enum bt_trace_set_environment_entry_status
275 bt_trace_set_environment_entry_string(
276 struct bt_trace *trace, const char *name, const char *value)
277 {
278 int ret;
279 struct bt_value *value_obj;
280
281 BT_ASSERT_PRE_NO_ERROR();
282 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
283 BT_ASSERT_PRE_NAME_NON_NULL(name);
284 BT_ASSERT_PRE_NON_NULL("value", value, "Value");
285
286 value_obj = bt_value_string_create_init(value);
287 if (!value_obj) {
288 BT_LIB_LOGE_APPEND_CAUSE(
289 "Cannot create a string value object.");
290 ret = -1;
291 goto end;
292 }
293
294 /* set_environment_entry() logs errors */
295 ret = set_environment_entry(trace, name, value_obj);
296
297 end:
298 bt_object_put_ref(value_obj);
299 return ret;
300 }
301
302 BT_EXPORT
303 enum bt_trace_set_environment_entry_status
304 bt_trace_set_environment_entry_integer(
305 struct bt_trace *trace, const char *name, int64_t value)
306 {
307 int ret;
308 struct bt_value *value_obj;
309
310 BT_ASSERT_PRE_NO_ERROR();
311 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
312 BT_ASSERT_PRE_NAME_NON_NULL(name);
313
314 value_obj = bt_value_integer_signed_create_init(value);
315 if (!value_obj) {
316 BT_LIB_LOGE_APPEND_CAUSE(
317 "Cannot create an integer value object.");
318 ret = BT_FUNC_STATUS_MEMORY_ERROR;
319 goto end;
320 }
321
322 /* set_environment_entry() logs errors */
323 ret = set_environment_entry(trace, name, value_obj);
324
325 end:
326 bt_object_put_ref(value_obj);
327 return ret;
328 }
329
330 BT_EXPORT
331 uint64_t bt_trace_get_environment_entry_count(const struct bt_trace *trace)
332 {
333 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
334 return bt_attributes_get_count(trace->environment);
335 }
336
337 BT_EXPORT
338 void bt_trace_borrow_environment_entry_by_index_const(
339 const struct bt_trace *trace, uint64_t index,
340 const char **name, const struct bt_value **value)
341 {
342 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
343 BT_ASSERT_PRE_DEV_NAME_NON_NULL(name);
344 BT_ASSERT_PRE_DEV_NON_NULL("value-object-output", value,
345 "Value object (output)");
346 BT_ASSERT_PRE_DEV_VALID_INDEX(index,
347 bt_attributes_get_count(trace->environment));
348 *value = bt_attributes_borrow_field_value(trace->environment, index);
349 BT_ASSERT(*value);
350 *name = bt_attributes_get_field_name(trace->environment, index);
351 BT_ASSERT(*name);
352 }
353
354 BT_EXPORT
355 const struct bt_value *bt_trace_borrow_environment_entry_value_by_name_const(
356 const struct bt_trace *trace, const char *name)
357 {
358 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
359 BT_ASSERT_PRE_DEV_NAME_NON_NULL(name);
360 return bt_attributes_borrow_field_value_by_name(trace->environment,
361 name);
362 }
363
364 BT_EXPORT
365 uint64_t bt_trace_get_stream_count(const struct bt_trace *trace)
366 {
367 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
368 return (uint64_t) trace->streams->len;
369 }
370
371 BT_EXPORT
372 struct bt_stream *bt_trace_borrow_stream_by_index(
373 struct bt_trace *trace, uint64_t index)
374 {
375 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
376 BT_ASSERT_PRE_DEV_VALID_INDEX(index, trace->streams->len);
377 return g_ptr_array_index(trace->streams, index);
378 }
379
380 BT_EXPORT
381 const struct bt_stream *bt_trace_borrow_stream_by_index_const(
382 const struct bt_trace *trace, uint64_t index)
383 {
384 return bt_trace_borrow_stream_by_index((void *) trace, index);
385 }
386
387 BT_EXPORT
388 struct bt_stream *bt_trace_borrow_stream_by_id(struct bt_trace *trace,
389 uint64_t id)
390 {
391 struct bt_stream *stream = NULL;
392 uint64_t i;
393
394 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
395
396 for (i = 0; i < trace->streams->len; i++) {
397 struct bt_stream *stream_candidate =
398 g_ptr_array_index(trace->streams, i);
399
400 if (stream_candidate->id == id) {
401 stream = stream_candidate;
402 goto end;
403 }
404 }
405
406 end:
407 return stream;
408 }
409
410 BT_EXPORT
411 const struct bt_stream *bt_trace_borrow_stream_by_id_const(
412 const struct bt_trace *trace, uint64_t id)
413 {
414 return bt_trace_borrow_stream_by_id((void *) trace, id);
415 }
416
417 BT_EXPORT
418 enum bt_trace_add_listener_status bt_trace_add_destruction_listener(
419 const struct bt_trace *c_trace,
420 bt_trace_destruction_listener_func listener,
421 void *data, bt_listener_id *listener_id)
422 {
423 struct bt_trace *trace = (void *) c_trace;
424 uint64_t i;
425 struct bt_trace_destruction_listener_elem new_elem = {
426 .func = listener,
427 .data = data,
428 };
429
430 BT_ASSERT_PRE_NO_ERROR();
431 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
432 BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(listener);
433
434 /* Find the next available spot */
435 for (i = 0; i < trace->destruction_listeners->len; i++) {
436 struct bt_trace_destruction_listener_elem elem =
437 bt_g_array_index(trace->destruction_listeners,
438 struct bt_trace_destruction_listener_elem, i);
439
440 if (!elem.func) {
441 break;
442 }
443 }
444
445 if (i == trace->destruction_listeners->len) {
446 g_array_append_val(trace->destruction_listeners, new_elem);
447 } else {
448 g_array_insert_val(trace->destruction_listeners, i, new_elem);
449 }
450
451 if (listener_id) {
452 *listener_id = i;
453 }
454
455 BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, "
456 "listener-id=%" PRIu64, trace, i);
457 return BT_FUNC_STATUS_OK;
458 }
459
460 static
461 bool has_listener_id(const struct bt_trace *trace, uint64_t listener_id)
462 {
463 BT_ASSERT(listener_id < trace->destruction_listeners->len);
464 return (&bt_g_array_index(trace->destruction_listeners,
465 struct bt_trace_destruction_listener_elem,
466 listener_id))->func;
467 }
468
469 BT_EXPORT
470 enum bt_trace_remove_listener_status bt_trace_remove_destruction_listener(
471 const struct bt_trace *c_trace, bt_listener_id listener_id)
472 {
473 struct bt_trace *trace = (void *) c_trace;
474 struct bt_trace_destruction_listener_elem *elem;
475
476 BT_ASSERT_PRE_NO_ERROR();
477 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
478 BT_ASSERT_PRE("listener-id-exists",
479 has_listener_id(trace, listener_id),
480 "Trace has no such trace destruction listener ID: "
481 "%![trace-]+t, %" PRIu64, trace, listener_id);
482 elem = &bt_g_array_index(trace->destruction_listeners,
483 struct bt_trace_destruction_listener_elem,
484 listener_id);
485 BT_ASSERT(elem->func);
486
487 elem->func = NULL;
488 elem->data = NULL;
489 BT_LIB_LOGD("Removed \"trace destruction listener: "
490 "%![trace-]+t, listener-id=%" PRIu64,
491 trace, listener_id);
492 return BT_FUNC_STATUS_OK;
493 }
494
495 void _bt_trace_freeze(const struct bt_trace *trace)
496 {
497 BT_ASSERT(trace);
498 BT_LIB_LOGD("Freezing trace's class: %!+T", trace->class);
499 bt_trace_class_freeze(trace->class);
500 BT_LIB_LOGD("Freezing trace's user attributes: %!+v",
501 trace->user_attributes);
502 bt_value_freeze(trace->user_attributes);
503 BT_LIB_LOGD("Freezing trace: %!+t", trace);
504 ((struct bt_trace *) trace)->frozen = true;
505 }
506
507 void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream)
508 {
509 guint count = 0;
510
511 bt_object_set_parent(&stream->base, &trace->base);
512 g_ptr_array_add(trace->streams, stream);
513 bt_trace_freeze(trace);
514
515 if (bt_g_hash_table_contains(trace->stream_classes_stream_count,
516 stream->class)) {
517 count = GPOINTER_TO_UINT(g_hash_table_lookup(
518 trace->stream_classes_stream_count, stream->class));
519 }
520
521 g_hash_table_insert(trace->stream_classes_stream_count,
522 stream->class, GUINT_TO_POINTER(count + 1));
523 }
524
525 uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace *trace,
526 const struct bt_stream_class *stream_class)
527 {
528 gpointer orig_key;
529 gpointer value;
530 uint64_t id = 0;
531
532 BT_ASSERT(stream_class);
533 BT_ASSERT(trace);
534 if (g_hash_table_lookup_extended(trace->stream_classes_stream_count,
535 stream_class, &orig_key, &value)) {
536 id = (uint64_t) GPOINTER_TO_UINT(value);
537 }
538
539 return id;
540 }
541
542 BT_EXPORT
543 struct bt_trace_class *bt_trace_borrow_class(struct bt_trace *trace)
544 {
545 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
546 return trace->class;
547 }
548
549 BT_EXPORT
550 const struct bt_trace_class *bt_trace_borrow_class_const(
551 const struct bt_trace *trace)
552 {
553 return bt_trace_borrow_class((void *) trace);
554 }
555
556 BT_EXPORT
557 const struct bt_value *bt_trace_borrow_user_attributes_const(
558 const struct bt_trace *trace)
559 {
560 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace);
561 return trace->user_attributes;
562 }
563
564 BT_EXPORT
565 struct bt_value *bt_trace_borrow_user_attributes(struct bt_trace *trace)
566 {
567 return (void *) bt_trace_borrow_user_attributes_const((void *) trace);
568 }
569
570 BT_EXPORT
571 void bt_trace_set_user_attributes(
572 struct bt_trace *trace,
573 const struct bt_value *user_attributes)
574 {
575 BT_ASSERT_PRE_TRACE_NON_NULL(trace);
576 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
577 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
578 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
579 bt_object_put_ref_no_null_check(trace->user_attributes);
580 trace->user_attributes = (void *) user_attributes;
581 bt_object_get_ref_no_null_check(trace->user_attributes);
582 }
583
584 BT_EXPORT
585 void bt_trace_get_ref(const struct bt_trace *trace)
586 {
587 bt_object_get_ref(trace);
588 }
589
590 BT_EXPORT
591 void bt_trace_put_ref(const struct bt_trace *trace)
592 {
593 bt_object_put_ref(trace);
594 }
This page took 0.042786 seconds and 4 git commands to generate.