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