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