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