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