trace{-class}.c: BT_ASSERT_PRE() -> BT_ASSERT_POST() (ref. count check)
[babeltrace.git] / src / lib / trace-ir / trace.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "LIB/TRACE"
25 #include "lib/logging.h"
26
27 #include "lib/assert-pre.h"
28 #include "lib/assert-post.h"
29 #include <babeltrace2/trace-ir/trace.h>
30 #include <babeltrace2/trace-ir/trace-const.h>
31 #include <babeltrace2/trace-ir/event-class.h>
32 #include "ctf-writer/functor.h"
33 #include "ctf-writer/clock.h"
34 #include "compat/compiler.h"
35 #include <babeltrace2/value.h>
36 #include <babeltrace2/value-const.h>
37 #include "lib/value.h"
38 #include <babeltrace2/types.h>
39 #include "compat/endian.h"
40 #include "common/assert.h"
41 #include "compat/glib.h"
42 #include <inttypes.h>
43 #include <stdint.h>
44 #include <string.h>
45 #include <stdlib.h>
46
47 #include "attributes.h"
48 #include "clock-class.h"
49 #include "event-class.h"
50 #include "event.h"
51 #include "field-class.h"
52 #include "field-wrapper.h"
53 #include "resolve-field-path.h"
54 #include "stream-class.h"
55 #include "stream.h"
56 #include "trace-class.h"
57 #include "trace.h"
58 #include "utils.h"
59 #include "lib/value.h"
60 #include "lib/func-status.h"
61
62 struct bt_trace_destruction_listener_elem {
63 bt_trace_destruction_listener_func func;
64 void *data;
65 };
66
67 #define BT_ASSERT_PRE_DEV_TRACE_HOT(_trace) \
68 BT_ASSERT_PRE_DEV_HOT((_trace), "Trace", ": %!+t", (_trace))
69
70 static
71 void destroy_trace(struct bt_object *obj)
72 {
73 struct bt_trace *trace = (void *) obj;
74
75 BT_LIB_LOGD("Destroying trace object: %!+t", trace);
76 BT_OBJECT_PUT_REF_AND_RESET(trace->user_attributes);
77
78 /*
79 * Call destruction listener functions so that everything else
80 * still exists in the trace.
81 */
82 if (trace->destruction_listeners) {
83 uint64_t i;
84 BT_LIB_LOGD("Calling trace destruction listener(s): %!+t", trace);
85
86 /*
87 * The trace's reference count is 0 if we're here. Increment
88 * it to avoid a double-destroy (possibly infinitely recursive).
89 * This could happen for example if a destruction listener did
90 * bt_object_get_ref() (or anything that causes
91 * bt_object_get_ref() to be called) on the trace (ref.
92 * count goes from 0 to 1), and then bt_object_put_ref(): the
93 * reference count would go from 1 to 0 again and this function
94 * would be called again.
95 */
96 trace->base.ref_count++;
97
98 /* Call all the trace destruction listeners */
99 for (i = 0; i < trace->destruction_listeners->len; i++) {
100 struct bt_trace_destruction_listener_elem elem =
101 g_array_index(trace->destruction_listeners,
102 struct bt_trace_destruction_listener_elem, i);
103
104 if (elem.func) {
105 elem.func(trace, elem.data);
106 }
107
108 /*
109 * The destruction listener should not have kept a
110 * reference to the trace.
111 */
112 BT_ASSERT_POST(trace->base.ref_count == 1, "Destruction listener kept a reference to the trace being destroyed: %![trace-]+t", trace);
113 }
114 g_array_free(trace->destruction_listeners, TRUE);
115 trace->destruction_listeners = NULL;
116 }
117
118 if (trace->name.str) {
119 g_string_free(trace->name.str, TRUE);
120 trace->name.str = NULL;
121 trace->name.value = NULL;
122 }
123
124 if (trace->environment) {
125 BT_LOGD_STR("Destroying environment attributes.");
126 bt_attributes_destroy(trace->environment);
127 trace->environment = NULL;
128 }
129
130 if (trace->streams) {
131 BT_LOGD_STR("Destroying streams.");
132 g_ptr_array_free(trace->streams, TRUE);
133 trace->streams = NULL;
134 }
135
136 if (trace->stream_classes_stream_count) {
137 g_hash_table_destroy(trace->stream_classes_stream_count);
138 trace->stream_classes_stream_count = NULL;
139 }
140
141 BT_LOGD_STR("Putting trace's class.");
142 bt_object_put_ref(trace->class);
143 trace->class = NULL;
144 g_free(trace);
145 }
146
147 struct bt_trace *bt_trace_create(struct bt_trace_class *tc)
148 {
149 struct bt_trace *trace = NULL;
150
151 BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc);
152 trace = g_new0(struct bt_trace, 1);
153 if (!trace) {
154 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace.");
155 goto error;
156 }
157
158 bt_object_init_shared(&trace->base, destroy_trace);
159 trace->user_attributes = bt_value_map_create();
160 if (!trace->user_attributes) {
161 BT_LIB_LOGE_APPEND_CAUSE(
162 "Failed to create a map value object.");
163 goto error;
164 }
165
166 trace->streams = g_ptr_array_new_with_free_func(
167 (GDestroyNotify) bt_object_try_spec_release);
168 if (!trace->streams) {
169 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
170 goto error;
171 }
172
173 trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash,
174 g_direct_equal);
175 if (!trace->stream_classes_stream_count) {
176 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable.");
177 goto error;
178 }
179
180 trace->name.str = g_string_new(NULL);
181 if (!trace->name.str) {
182 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
183 goto error;
184 }
185
186 trace->environment = bt_attributes_create();
187 if (!trace->environment) {
188 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object.");
189 goto error;
190 }
191
192 trace->destruction_listeners = g_array_new(FALSE, TRUE,
193 sizeof(struct bt_trace_destruction_listener_elem));
194 if (!trace->destruction_listeners) {
195 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
196 goto error;
197 }
198
199 trace->class = tc;
200 bt_object_get_ref_no_null_check(trace->class);
201 BT_LIB_LOGD("Created trace object: %!+t", trace);
202 goto end;
203
204 error:
205 BT_OBJECT_PUT_REF_AND_RESET(trace);
206
207 end:
208 return trace;
209 }
210
211 const char *bt_trace_get_name(const struct bt_trace *trace)
212 {
213 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
214 return trace->name.value;
215 }
216
217 enum bt_trace_set_name_status bt_trace_set_name(struct bt_trace *trace,
218 const char *name)
219 {
220 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
221 BT_ASSERT_PRE_NON_NULL(name, "Name");
222 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
223 g_string_assign(trace->name.str, name);
224 trace->name.value = trace->name.str->str;
225 BT_LIB_LOGD("Set trace's name: %!+t", trace);
226 return BT_FUNC_STATUS_OK;
227 }
228
229 bt_uuid bt_trace_get_uuid(const struct bt_trace *trace)
230 {
231 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
232 return trace->uuid.value;
233 }
234
235 void bt_trace_set_uuid(struct bt_trace *trace, bt_uuid uuid)
236 {
237 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
238 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
239 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
240 bt_uuid_copy(trace->uuid.uuid, uuid);
241 trace->uuid.value = trace->uuid.uuid;
242 BT_LIB_LOGD("Set trace's UUID: %!+t", trace);
243 }
244
245 BT_ASSERT_FUNC
246 static
247 bool trace_has_environment_entry(const struct bt_trace *trace, const char *name)
248 {
249 BT_ASSERT(trace);
250
251 return bt_attributes_borrow_field_value_by_name(
252 trace->environment, name);
253 }
254
255 static
256 enum bt_trace_set_environment_entry_status set_environment_entry(
257 struct bt_trace *trace,
258 const char *name, struct bt_value *value)
259 {
260 int ret;
261
262 BT_ASSERT(trace);
263 BT_ASSERT(name);
264 BT_ASSERT(value);
265 BT_ASSERT_PRE(!trace->frozen ||
266 !trace_has_environment_entry(trace, name),
267 "Trace is frozen: cannot replace environment entry: "
268 "%![trace-]+t, entry-name=\"%s\"", trace, name);
269 ret = bt_attributes_set_field_value(trace->environment, name,
270 value);
271 if (ret) {
272 ret = BT_FUNC_STATUS_MEMORY_ERROR;
273 BT_LIB_LOGE_APPEND_CAUSE(
274 "Cannot set trace's environment entry: "
275 "%![trace-]+t, entry-name=\"%s\"", trace, name);
276 } else {
277 bt_value_freeze(value);
278 BT_LIB_LOGD("Set trace's environment entry: "
279 "%![trace-]+t, entry-name=\"%s\"", trace, name);
280 }
281
282 return ret;
283 }
284
285 enum bt_trace_set_environment_entry_status
286 bt_trace_set_environment_entry_string(
287 struct bt_trace *trace, const char *name, const char *value)
288 {
289 int ret;
290 struct bt_value *value_obj;
291 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
292 BT_ASSERT_PRE_NON_NULL(name, "Name");
293 BT_ASSERT_PRE_NON_NULL(value, "Value");
294 value_obj = bt_value_string_create_init(value);
295 if (!value_obj) {
296 BT_LIB_LOGE_APPEND_CAUSE(
297 "Cannot create a string value object.");
298 ret = -1;
299 goto end;
300 }
301
302 /* set_environment_entry() logs errors */
303 ret = set_environment_entry(trace, name, value_obj);
304
305 end:
306 bt_object_put_ref(value_obj);
307 return ret;
308 }
309
310 enum bt_trace_set_environment_entry_status
311 bt_trace_set_environment_entry_integer(
312 struct bt_trace *trace, const char *name, int64_t value)
313 {
314 int ret;
315 struct bt_value *value_obj;
316 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
317 BT_ASSERT_PRE_NON_NULL(name, "Name");
318 value_obj = bt_value_integer_signed_create_init(value);
319 if (!value_obj) {
320 BT_LIB_LOGE_APPEND_CAUSE(
321 "Cannot create an integer value object.");
322 ret = BT_FUNC_STATUS_MEMORY_ERROR;
323 goto end;
324 }
325
326 /* set_environment_entry() logs errors */
327 ret = set_environment_entry(trace, name, value_obj);
328
329 end:
330 bt_object_put_ref(value_obj);
331 return ret;
332 }
333
334 uint64_t bt_trace_get_environment_entry_count(const struct bt_trace *trace)
335 {
336 int64_t ret;
337
338 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
339 ret = bt_attributes_get_count(trace->environment);
340 BT_ASSERT(ret >= 0);
341 return (uint64_t) ret;
342 }
343
344 void bt_trace_borrow_environment_entry_by_index_const(
345 const struct bt_trace *trace, uint64_t index,
346 const char **name, const struct bt_value **value)
347 {
348 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
349 BT_ASSERT_PRE_DEV_NON_NULL(name, "Name");
350 BT_ASSERT_PRE_DEV_NON_NULL(value, "Value");
351 BT_ASSERT_PRE_DEV_VALID_INDEX(index,
352 bt_attributes_get_count(trace->environment));
353 *value = bt_attributes_borrow_field_value(trace->environment, index);
354 BT_ASSERT(*value);
355 *name = bt_attributes_get_field_name(trace->environment, index);
356 BT_ASSERT(*name);
357 }
358
359 const struct bt_value *bt_trace_borrow_environment_entry_value_by_name_const(
360 const struct bt_trace *trace, const char *name)
361 {
362 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
363 BT_ASSERT_PRE_DEV_NON_NULL(name, "Name");
364 return bt_attributes_borrow_field_value_by_name(trace->environment,
365 name);
366 }
367
368 uint64_t bt_trace_get_stream_count(const struct bt_trace *trace)
369 {
370 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
371 return (uint64_t) trace->streams->len;
372 }
373
374 struct bt_stream *bt_trace_borrow_stream_by_index(
375 struct bt_trace *trace, uint64_t index)
376 {
377 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
378 BT_ASSERT_PRE_DEV_VALID_INDEX(index, trace->streams->len);
379 return g_ptr_array_index(trace->streams, index);
380 }
381
382 const struct bt_stream *bt_trace_borrow_stream_by_index_const(
383 const struct bt_trace *trace, uint64_t index)
384 {
385 return bt_trace_borrow_stream_by_index((void *) trace, index);
386 }
387
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_NON_NULL(trace, "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 const struct bt_stream *bt_trace_borrow_stream_by_id_const(
411 const struct bt_trace *trace, uint64_t id)
412 {
413 return bt_trace_borrow_stream_by_id((void *) trace, id);
414 }
415
416 enum bt_trace_add_listener_status bt_trace_add_destruction_listener(
417 const struct bt_trace *c_trace,
418 bt_trace_destruction_listener_func listener,
419 void *data, bt_listener_id *listener_id)
420 {
421 struct bt_trace *trace = (void *) c_trace;
422 uint64_t i;
423 struct bt_trace_destruction_listener_elem new_elem = {
424 .func = listener,
425 .data = data,
426 };
427
428 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
429 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
430
431 /* Find the next available spot */
432 for (i = 0; i < trace->destruction_listeners->len; i++) {
433 struct bt_trace_destruction_listener_elem elem =
434 g_array_index(trace->destruction_listeners,
435 struct bt_trace_destruction_listener_elem, i);
436
437 if (!elem.func) {
438 break;
439 }
440 }
441
442 if (i == trace->destruction_listeners->len) {
443 g_array_append_val(trace->destruction_listeners, new_elem);
444 } else {
445 g_array_insert_val(trace->destruction_listeners, i, new_elem);
446 }
447
448 if (listener_id) {
449 *listener_id = i;
450 }
451
452 BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, "
453 "listener-id=%" PRIu64, trace, i);
454 return BT_FUNC_STATUS_OK;
455 }
456
457 static
458 bool has_listener_id(const struct bt_trace *trace, uint64_t listener_id)
459 {
460 BT_ASSERT(listener_id < trace->destruction_listeners->len);
461 return (&g_array_index(trace->destruction_listeners,
462 struct bt_trace_destruction_listener_elem,
463 listener_id))->func;
464 }
465
466 enum bt_trace_remove_listener_status bt_trace_remove_destruction_listener(
467 const struct bt_trace *c_trace, bt_listener_id listener_id)
468 {
469 struct bt_trace *trace = (void *) c_trace;
470 struct bt_trace_destruction_listener_elem *elem;
471
472 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
473 BT_ASSERT_PRE(has_listener_id(trace, listener_id),
474 "Trace has no such trace destruction listener ID: "
475 "%![trace-]+t, %" PRIu64, trace, listener_id);
476 elem = &g_array_index(trace->destruction_listeners,
477 struct bt_trace_destruction_listener_elem,
478 listener_id);
479 BT_ASSERT(elem->func);
480
481 elem->func = NULL;
482 elem->data = NULL;
483 BT_LIB_LOGD("Removed \"trace destruction listener: "
484 "%![trace-]+t, listener-id=%" PRIu64,
485 trace, listener_id);
486 return BT_FUNC_STATUS_OK;
487 }
488
489 BT_HIDDEN
490 void _bt_trace_freeze(const struct bt_trace *trace)
491 {
492 BT_ASSERT(trace);
493 BT_LIB_LOGD("Freezing trace's class: %!+T", trace->class);
494 bt_trace_class_freeze(trace->class);
495 BT_LIB_LOGD("Freezing trace's user attributes: %!+v",
496 trace->user_attributes);
497 bt_value_freeze(trace->user_attributes);
498 BT_LIB_LOGD("Freezing trace: %!+t", trace);
499 ((struct bt_trace *) trace)->frozen = true;
500 }
501
502 BT_HIDDEN
503 void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream)
504 {
505 guint count = 0;
506
507 bt_object_set_parent(&stream->base, &trace->base);
508 g_ptr_array_add(trace->streams, stream);
509 bt_trace_freeze(trace);
510
511 if (bt_g_hash_table_contains(trace->stream_classes_stream_count,
512 stream->class)) {
513 count = GPOINTER_TO_UINT(g_hash_table_lookup(
514 trace->stream_classes_stream_count, stream->class));
515 }
516
517 g_hash_table_insert(trace->stream_classes_stream_count,
518 stream->class, GUINT_TO_POINTER(count + 1));
519 }
520
521 BT_HIDDEN
522 uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace *trace,
523 const struct bt_stream_class *stream_class)
524 {
525 gpointer orig_key;
526 gpointer value;
527 uint64_t id = 0;
528
529 BT_ASSERT(stream_class);
530 BT_ASSERT(trace);
531 if (g_hash_table_lookup_extended(trace->stream_classes_stream_count,
532 stream_class, &orig_key, &value)) {
533 id = (uint64_t) GPOINTER_TO_UINT(value);
534 }
535
536 return id;
537 }
538
539 struct bt_trace_class *bt_trace_borrow_class(struct bt_trace *trace)
540 {
541 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
542 return trace->class;
543 }
544
545 const struct bt_trace_class *bt_trace_borrow_class_const(
546 const struct bt_trace *trace)
547 {
548 return bt_trace_borrow_class((void *) trace);
549 }
550
551 const struct bt_value *bt_trace_borrow_user_attributes_const(
552 const struct bt_trace *trace)
553 {
554 BT_ASSERT_PRE_DEV_NON_NULL(trace, "Trace");
555 return trace->user_attributes;
556 }
557
558 struct bt_value *bt_trace_borrow_user_attributes(struct bt_trace *trace)
559 {
560 return (void *) bt_trace_borrow_user_attributes_const((void *) trace);
561 }
562
563 void bt_trace_set_user_attributes(
564 struct bt_trace *trace,
565 const struct bt_value *user_attributes)
566 {
567 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
568 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
569 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
570 "User attributes object is not a map value object.");
571 BT_ASSERT_PRE_DEV_TRACE_HOT(trace);
572 bt_object_put_ref_no_null_check(trace->user_attributes);
573 trace->user_attributes = (void *) user_attributes;
574 bt_object_get_ref_no_null_check(trace->user_attributes);
575 }
576
577 void bt_trace_get_ref(const struct bt_trace *trace)
578 {
579 bt_object_get_ref(trace);
580 }
581
582 void bt_trace_put_ref(const struct bt_trace *trace)
583 {
584 bt_object_put_ref(trace);
585 }
This page took 0.041031 seconds and 4 git commands to generate.