bt_value_map_extend(): make base/extension objects `const`
[babeltrace.git] / lib / trace-ir / trace.c
CommitLineData
bc37ae52
JG
1/*
2 * trace.c
3 *
56e18c4c 4 * Babeltrace trace IR - Trace
bc37ae52
JG
5 *
6 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
e011d2c1
PP
29#define BT_LOG_TAG "TRACE"
30#include <babeltrace/lib-logging-internal.h>
31
3dca2276 32#include <babeltrace/assert-pre-internal.h>
56e18c4c
PP
33#include <babeltrace/trace-ir/trace-internal.h>
34#include <babeltrace/trace-ir/clock-class-internal.h>
35#include <babeltrace/trace-ir/stream-internal.h>
36#include <babeltrace/trace-ir/stream-class-internal.h>
37#include <babeltrace/trace-ir/event-internal.h>
38#include <babeltrace/trace-ir/event-class.h>
39#include <babeltrace/trace-ir/event-class-internal.h>
bc37ae52 40#include <babeltrace/ctf-writer/functor-internal.h>
ac0c6bdd 41#include <babeltrace/ctf-writer/clock-internal.h>
56e18c4c 42#include <babeltrace/trace-ir/field-wrapper-internal.h>
5cd6d0e5 43#include <babeltrace/trace-ir/field-classes-internal.h>
56e18c4c
PP
44#include <babeltrace/trace-ir/attributes-internal.h>
45#include <babeltrace/trace-ir/utils-internal.h>
46#include <babeltrace/trace-ir/resolve-field-path-internal.h>
3d9990ac 47#include <babeltrace/compiler-internal.h>
dac5c838 48#include <babeltrace/values.h>
da91b29a 49#include <babeltrace/private-values.h>
95c09b3a 50#include <babeltrace/values-internal.h>
65300d60 51#include <babeltrace/object.h>
c55a9f58 52#include <babeltrace/types.h>
3d9990ac 53#include <babeltrace/endian-internal.h>
f6ccaed9 54#include <babeltrace/assert-internal.h>
44c440bc 55#include <babeltrace/compat/glib-internal.h>
dc3fffef 56#include <inttypes.h>
544d0515 57#include <stdint.h>
4a32fda0 58#include <string.h>
0fbb9a9f 59#include <stdlib.h>
bc37ae52 60
50842bdc
PP
61struct bt_trace_is_static_listener_elem {
62 bt_trace_is_static_listener func;
63 bt_trace_listener_removed removed;
3602afb0
PP
64 void *data;
65};
66
44c440bc
PP
67#define BT_ASSERT_PRE_TRACE_HOT(_trace) \
68 BT_ASSERT_PRE_HOT((_trace), "Trace", ": %!+t", (_trace))
cb6f1f7d 69
bc37ae52 70static
44c440bc 71void destroy_trace(struct bt_object *obj)
3dca2276
PP
72{
73 struct bt_trace *trace = (void *) obj;
bc37ae52 74
44c440bc 75 BT_LIB_LOGD("Destroying trace object: %!+t", trace);
bc37ae52 76
3dca2276
PP
77 /*
78 * Call remove listeners first so that everything else still
79 * exists in the trace.
80 */
81 if (trace->is_static_listeners) {
82 size_t i;
bc37ae52 83
3dca2276
PP
84 for (i = 0; i < trace->is_static_listeners->len; i++) {
85 struct bt_trace_is_static_listener_elem elem =
86 g_array_index(trace->is_static_listeners,
87 struct bt_trace_is_static_listener_elem, i);
bc37ae52 88
3dca2276
PP
89 if (elem.removed) {
90 elem.removed(trace, elem.data);
91 }
92 }
93
94 g_array_free(trace->is_static_listeners, TRUE);
bc37ae52
JG
95 }
96
312c056a 97 bt_object_pool_finalize(&trace->packet_header_field_pool);
3dca2276 98
44c440bc
PP
99 if (trace->environment) {
100 BT_LOGD_STR("Destroying environment attributes.");
101 bt_attributes_destroy(trace->environment);
95c09b3a
PP
102 }
103
44c440bc
PP
104 if (trace->name.str) {
105 g_string_free(trace->name.str, TRUE);
95c09b3a
PP
106 }
107
44c440bc
PP
108 if (trace->streams) {
109 BT_LOGD_STR("Destroying streams.");
110 g_ptr_array_free(trace->streams, TRUE);
bc37ae52
JG
111 }
112
44c440bc
PP
113 if (trace->stream_classes) {
114 BT_LOGD_STR("Destroying stream classes.");
115 g_ptr_array_free(trace->stream_classes, TRUE);
7f800dc7
PP
116 }
117
44c440bc
PP
118 if (trace->stream_classes_stream_count) {
119 g_hash_table_destroy(trace->stream_classes_stream_count);
120 }
3dca2276 121
5cd6d0e5 122 BT_LOGD_STR("Putting packet header field classe.");
65300d60 123 bt_object_put_ref(trace->packet_header_fc);
44c440bc 124 g_free(trace);
3dca2276
PP
125}
126
312c056a
PP
127static
128void free_packet_header_field(struct bt_field_wrapper *field_wrapper,
129 struct bt_trace *trace)
130{
131 bt_field_wrapper_destroy(field_wrapper);
132}
133
3dca2276
PP
134struct bt_trace *bt_trace_create(void)
135{
136 struct bt_trace *trace = NULL;
137 int ret;
138
44c440bc 139 BT_LOGD_STR("Creating default trace object.");
3dca2276
PP
140 trace = g_new0(struct bt_trace, 1);
141 if (!trace) {
142 BT_LOGE_STR("Failed to allocate one trace.");
143 goto error;
144 }
145
44c440bc
PP
146 bt_object_init_shared_with_parent(&trace->base, destroy_trace);
147 trace->streams = g_ptr_array_new_with_free_func(
148 (GDestroyNotify) bt_object_try_spec_release);
149 if (!trace->streams) {
150 BT_LOGE_STR("Failed to allocate one GPtrArray.");
3dca2276
PP
151 goto error;
152 }
153
44c440bc
PP
154 trace->stream_classes = g_ptr_array_new_with_free_func(
155 (GDestroyNotify) bt_object_try_spec_release);
156 if (!trace->stream_classes) {
95c09b3a 157 BT_LOGE_STR("Failed to allocate one GPtrArray.");
9b888ff3
JG
158 goto error;
159 }
160
44c440bc
PP
161 trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash,
162 g_direct_equal);
163 if (!trace->stream_classes_stream_count) {
164 BT_LOGE_STR("Failed to allocate one GHashTable.");
165 goto error;
166 }
167
168 trace->name.str = g_string_new(NULL);
169 if (!trace->name.str) {
170 BT_LOGE_STR("Failed to allocate one GString.");
171 goto error;
172 }
173
174 trace->environment = bt_attributes_create();
175 if (!trace->environment) {
176 BT_LOGE_STR("Cannot create empty attributes object.");
177 goto error;
178 }
179
3602afb0 180 trace->is_static_listeners = g_array_new(FALSE, TRUE,
50842bdc 181 sizeof(struct bt_trace_is_static_listener_elem));
3602afb0
PP
182 if (!trace->is_static_listeners) {
183 BT_LOGE_STR("Failed to allocate one GArray.");
184 goto error;
185 }
186
44c440bc 187 trace->assigns_automatic_stream_class_id = true;
312c056a
PP
188 ret = bt_object_pool_initialize(&trace->packet_header_field_pool,
189 (bt_object_pool_new_object_func) bt_field_wrapper_new,
190 (bt_object_pool_destroy_object_func) free_packet_header_field,
191 trace);
192 if (ret) {
193 BT_LOGE("Failed to initialize packet header field pool: ret=%d",
194 ret);
195 goto error;
196 }
197
44c440bc
PP
198 BT_LIB_LOGD("Created trace object: %!+t", trace);
199 goto end;
bc37ae52 200
bc37ae52 201error:
65300d60 202 BT_OBJECT_PUT_REF_AND_RESET(trace);
44c440bc
PP
203
204end:
bc37ae52
JG
205 return trace;
206}
207
50842bdc 208const char *bt_trace_get_name(struct bt_trace *trace)
e96045d4 209{
cb6f1f7d 210 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 211 return trace->name.value;
e96045d4
JG
212}
213
cb6f1f7d 214int bt_trace_set_name(struct bt_trace *trace, const char *name)
e96045d4 215{
44c440bc
PP
216 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
217 BT_ASSERT_PRE_NON_NULL(name, "Name");
218 BT_ASSERT_PRE_TRACE_HOT(trace);
219 g_string_assign(trace->name.str, name);
220 trace->name.value = trace->name.str->str;
221 BT_LIB_LOGV("Set trace's name: %!+t", trace);
222 return 0;
e96045d4
JG
223}
224
44c440bc 225bt_uuid bt_trace_get_uuid(struct bt_trace *trace)
4a32fda0 226{
cb6f1f7d 227 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 228 return trace->uuid.value;
4a32fda0
PP
229}
230
44c440bc 231int bt_trace_set_uuid(struct bt_trace *trace, bt_uuid uuid)
4a32fda0 232{
44c440bc
PP
233 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
234 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
235 BT_ASSERT_PRE_TRACE_HOT(trace);
236 memcpy(trace->uuid.uuid, uuid, BABELTRACE_UUID_LEN);
237 trace->uuid.value = trace->uuid.uuid;
238 BT_LIB_LOGV("Set trace's UUID: %!+t", trace);
239 return 0;
4a32fda0
PP
240}
241
44c440bc
PP
242BT_ASSERT_FUNC
243static
244bool trace_has_environment_entry(struct bt_trace *trace, const char *name)
bc37ae52 245{
44c440bc 246 BT_ASSERT(trace);
95c09b3a 247
da91b29a
PP
248 return bt_attributes_borrow_field_value_by_name(
249 trace->environment, name) != NULL;
44c440bc 250}
a0d12916 251
44c440bc
PP
252static
253int set_environment_entry(struct bt_trace *trace, const char *name,
da91b29a 254 struct bt_private_value *value)
44c440bc
PP
255{
256 int ret;
a0d12916 257
44c440bc
PP
258 BT_ASSERT(trace);
259 BT_ASSERT(name);
260 BT_ASSERT(value);
261 BT_ASSERT_PRE(!trace->frozen ||
262 !trace_has_environment_entry(trace, name),
263 "Trace is frozen: cannot replace environment entry: "
264 "%![trace-]+t, entry-name=\"%s\"", trace, name);
50842bdc 265 ret = bt_attributes_set_field_value(trace->environment, name,
7f800dc7 266 value);
da91b29a 267 bt_value_freeze(bt_value_borrow_from_private(value));
95c09b3a 268 if (ret) {
44c440bc
PP
269 BT_LIB_LOGE("Cannot set trace's environment entry: "
270 "%![trace-]+t, entry-name=\"%s\"", trace, name);
95c09b3a 271 } else {
44c440bc
PP
272 BT_LIB_LOGV("Set trace's environment entry: "
273 "%![trace-]+t, entry-name=\"%s\"", trace, name);
95c09b3a 274 }
bc37ae52 275
7f800dc7
PP
276 return ret;
277}
bc37ae52 278
44c440bc 279int bt_trace_set_environment_entry_string(struct bt_trace *trace,
7f800dc7
PP
280 const char *name, const char *value)
281{
44c440bc 282 int ret;
da91b29a 283 struct bt_private_value *value_obj;
bc37ae52 284
44c440bc
PP
285 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
286 BT_ASSERT_PRE_NON_NULL(name, "Name");
287 BT_ASSERT_PRE_NON_NULL(value, "Value");
da91b29a 288 value_obj = bt_private_value_string_create_init(value);
44c440bc
PP
289 if (!value_obj) {
290 BT_LOGE_STR("Cannot create a string value object.");
7f800dc7
PP
291 ret = -1;
292 goto end;
bc37ae52
JG
293 }
294
44c440bc
PP
295 /* set_environment_entry() logs errors */
296 ret = set_environment_entry(trace, name, value_obj);
7f800dc7
PP
297
298end:
65300d60 299 bt_object_put_ref(value_obj);
3487c9f3
JG
300 return ret;
301}
302
44c440bc 303int bt_trace_set_environment_entry_integer(
cb6f1f7d 304 struct bt_trace *trace, const char *name, int64_t value)
3487c9f3 305{
44c440bc 306 int ret;
da91b29a 307 struct bt_private_value *value_obj;
3487c9f3 308
44c440bc
PP
309 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
310 BT_ASSERT_PRE_NON_NULL(name, "Name");
da91b29a 311 value_obj = bt_private_value_integer_create_init(value);
44c440bc
PP
312 if (!value_obj) {
313 BT_LOGE_STR("Cannot create an integer value object.");
3487c9f3 314 ret = -1;
7f800dc7 315 goto end;
3487c9f3
JG
316 }
317
44c440bc
PP
318 /* set_environment_entry() logs errors */
319 ret = set_environment_entry(trace, name, value_obj);
95c09b3a 320
7f800dc7 321end:
65300d60 322 bt_object_put_ref(value_obj);
bc37ae52
JG
323 return ret;
324}
325
44c440bc 326uint64_t bt_trace_get_environment_entry_count(struct bt_trace *trace)
3dca2276 327{
cb6f1f7d
PP
328 int64_t ret;
329
330 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
331 ret = bt_attributes_get_count(trace->environment);
332 BT_ASSERT(ret >= 0);
44c440bc 333 return (uint64_t) ret;
e6fa2160
JG
334}
335
44c440bc
PP
336void bt_trace_borrow_environment_entry_by_index(
337 struct bt_trace *trace, uint64_t index,
338 const char **name, struct bt_value **value)
e6fa2160 339{
cb6f1f7d 340 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc
PP
341 BT_ASSERT_PRE_NON_NULL(name, "Name");
342 BT_ASSERT_PRE_NON_NULL(value, "Value");
343 BT_ASSERT_PRE_VALID_INDEX(index,
344 bt_attributes_get_count(trace->environment));
da91b29a
PP
345 *value = bt_value_borrow_from_private(
346 bt_attributes_borrow_field_value(trace->environment, index));
44c440bc
PP
347 BT_ASSERT(*value);
348 *name = bt_attributes_get_field_name(trace->environment, index);
349 BT_ASSERT(*name);
e6fa2160
JG
350}
351
44c440bc 352struct bt_value *bt_trace_borrow_environment_entry_value_by_name(
50842bdc 353 struct bt_trace *trace, const char *name)
e6fa2160 354{
cb6f1f7d
PP
355 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
356 BT_ASSERT_PRE_NON_NULL(name, "Name");
da91b29a
PP
357 return bt_value_borrow_from_private(
358 bt_attributes_borrow_field_value_by_name(trace->environment,
359 name));
e6fa2160
JG
360}
361
44c440bc 362uint64_t bt_trace_get_stream_count(struct bt_trace *trace)
bc37ae52 363{
44c440bc
PP
364 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
365 return (uint64_t) trace->streams->len;
366}
95c09b3a 367
44c440bc
PP
368struct bt_stream *bt_trace_borrow_stream_by_index(
369 struct bt_trace *trace, uint64_t index)
370{
371 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
372 BT_ASSERT_PRE_VALID_INDEX(index, trace->streams->len);
373 return g_ptr_array_index(trace->streams, index);
374}
cb6f1f7d 375
44c440bc
PP
376struct bt_stream *bt_trace_borrow_stream_by_id(
377 struct bt_trace *trace, uint64_t id)
378{
379 struct bt_stream *stream = NULL;
380 uint64_t i;
bc37ae52 381
44c440bc 382 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
bc37ae52 383
44c440bc
PP
384 for (i = 0; i < trace->streams->len; i++) {
385 struct bt_stream *stream_candidate =
386 g_ptr_array_index(trace->streams, i);
cfeb617e 387
44c440bc
PP
388 if (stream_candidate->id == id) {
389 stream = stream_candidate;
390 goto end;
391 }
6474e016 392 }
95c09b3a 393
bc37ae52 394end:
44c440bc 395 return stream;
bc37ae52
JG
396}
397
44c440bc 398uint64_t bt_trace_get_stream_class_count(struct bt_trace *trace)
884cd6c3 399{
cb6f1f7d 400 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 401 return (uint64_t) trace->stream_classes->len;
884cd6c3
JG
402}
403
44c440bc 404struct bt_stream_class *bt_trace_borrow_stream_class_by_index(
50842bdc 405 struct bt_trace *trace, uint64_t index)
884cd6c3 406{
cb6f1f7d 407 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc
PP
408 BT_ASSERT_PRE_VALID_INDEX(index, trace->stream_classes->len);
409 return g_ptr_array_index(trace->stream_classes, index);
884cd6c3
JG
410}
411
44c440bc
PP
412struct bt_stream_class *bt_trace_borrow_stream_class_by_id(
413 struct bt_trace *trace, uint64_t id)
e011d2c1 414{
44c440bc
PP
415 struct bt_stream_class *stream_class = NULL;
416 uint64_t i;
e011d2c1 417
44c440bc 418 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
e011d2c1 419
44c440bc
PP
420 for (i = 0; i < trace->stream_classes->len; i++) {
421 struct bt_stream_class *stream_class_candidate =
422 g_ptr_array_index(trace->stream_classes, i);
e011d2c1 423
44c440bc
PP
424 if (stream_class_candidate->id == id) {
425 stream_class = stream_class_candidate;
426 goto end;
e011d2c1 427 }
44c440bc 428 }
e011d2c1 429
44c440bc
PP
430end:
431 return stream_class;
432}
e011d2c1 433
5cd6d0e5 434struct bt_field_class *bt_trace_borrow_packet_header_field_class(
44c440bc
PP
435 struct bt_trace *trace)
436{
437 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
5cd6d0e5 438 return trace->packet_header_fc;
44c440bc 439}
e011d2c1 440
5cd6d0e5
PP
441int bt_trace_set_packet_header_field_class(struct bt_trace *trace,
442 struct bt_field_class *field_class)
44c440bc
PP
443{
444 int ret;
445 struct bt_resolve_field_path_context resolve_ctx = {
5cd6d0e5 446 .packet_header = field_class,
44c440bc
PP
447 .packet_context = NULL,
448 .event_header = NULL,
449 .event_common_context = NULL,
450 .event_specific_context = NULL,
451 .event_payload = NULL,
452 };
e011d2c1 453
44c440bc 454 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
5cd6d0e5 455 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
44c440bc 456 BT_ASSERT_PRE_TRACE_HOT(trace);
864cad70
PP
457 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
458 BT_FIELD_CLASS_TYPE_STRUCTURE,
5cd6d0e5
PP
459 "Packet header field classe is not a structure field classe: %!+F",
460 field_class);
461 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
44c440bc
PP
462 if (ret) {
463 goto end;
e011d2c1
PP
464 }
465
5cd6d0e5 466 bt_field_class_make_part_of_trace(field_class);
65300d60
PP
467 bt_object_put_ref(trace->packet_header_fc);
468 trace->packet_header_fc = bt_object_get_ref(field_class);
5cd6d0e5
PP
469 bt_field_class_freeze(field_class);
470 BT_LIB_LOGV("Set trace's packet header field classe: %!+t", trace);
e011d2c1 471
8bf65fbd
JG
472end:
473 return ret;
474}
475
50842bdc 476bt_bool bt_trace_is_static(struct bt_trace *trace)
5acf2ae6 477{
d975f66c 478 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 479 return (bt_bool) trace->is_static;
5acf2ae6
PP
480}
481
44c440bc 482int bt_trace_make_static(struct bt_trace *trace)
5acf2ae6 483{
44c440bc 484 uint64_t i;
726106d3 485
44c440bc
PP
486 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
487 trace->is_static = true;
cb6f1f7d 488 bt_trace_freeze(trace);
44c440bc 489 BT_LIB_LOGV("Trace is now static: %!+t", trace);
5acf2ae6 490
3602afb0
PP
491 /* Call all the "trace is static" listeners */
492 for (i = 0; i < trace->is_static_listeners->len; i++) {
50842bdc 493 struct bt_trace_is_static_listener_elem elem =
3602afb0 494 g_array_index(trace->is_static_listeners,
50842bdc 495 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
496
497 if (elem.func) {
498 elem.func(trace, elem.data);
499 }
500 }
501
44c440bc 502 return 0;
3602afb0
PP
503}
504
50842bdc
PP
505int bt_trace_add_is_static_listener(struct bt_trace *trace,
506 bt_trace_is_static_listener listener,
44c440bc
PP
507 bt_trace_listener_removed listener_removed, void *data,
508 uint64_t *listener_id)
3602afb0 509{
44c440bc 510 uint64_t i;
50842bdc 511 struct bt_trace_is_static_listener_elem new_elem = {
3602afb0 512 .func = listener,
8480c8cc 513 .removed = listener_removed,
3602afb0
PP
514 .data = data,
515 };
516
44c440bc
PP
517 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
518 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
519 BT_ASSERT_PRE(!trace->is_static,
520 "Trace is already static: %!+t", trace);
521 BT_ASSERT_PRE(trace->in_remove_listener,
522 "Cannot call this function while executing a "
523 "remove listener: %!+t", trace);
8480c8cc 524
3602afb0
PP
525 /* Find the next available spot */
526 for (i = 0; i < trace->is_static_listeners->len; i++) {
50842bdc 527 struct bt_trace_is_static_listener_elem elem =
3602afb0 528 g_array_index(trace->is_static_listeners,
50842bdc 529 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
530
531 if (!elem.func) {
532 break;
533 }
534 }
535
536 if (i == trace->is_static_listeners->len) {
537 g_array_append_val(trace->is_static_listeners, new_elem);
538 } else {
539 g_array_insert_val(trace->is_static_listeners, i, new_elem);
540 }
541
44c440bc
PP
542 if (listener_id) {
543 *listener_id = i;
544 }
3602afb0 545
44c440bc
PP
546 BT_LIB_LOGV("Added \"trace is static\" listener: "
547 "%![trace-]+t, listener-id=%" PRIu64, trace, i);
548 return 0;
549}
550
551BT_ASSERT_PRE_FUNC
552static
553bool has_listener_id(struct bt_trace *trace, uint64_t listener_id)
554{
555 BT_ASSERT(listener_id < trace->is_static_listeners->len);
556 return (&g_array_index(trace->is_static_listeners,
557 struct bt_trace_is_static_listener_elem,
558 listener_id))->func != NULL;
3602afb0
PP
559}
560
50842bdc 561int bt_trace_remove_is_static_listener(
44c440bc 562 struct bt_trace *trace, uint64_t listener_id)
3602afb0 563{
50842bdc 564 struct bt_trace_is_static_listener_elem *elem;
3602afb0 565
44c440bc
PP
566 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
567 BT_ASSERT_PRE(!trace->is_static,
568 "Trace is already static: %!+t", trace);
569 BT_ASSERT_PRE(trace->in_remove_listener,
570 "Cannot call this function while executing a "
571 "remove listener: %!+t", trace);
572 BT_ASSERT_PRE(has_listener_id(trace, listener_id),
573 "Trace has no such \"trace is static\" listener ID: "
574 "%![trace-]+t, %" PRIu64, trace, listener_id);
3602afb0 575 elem = &g_array_index(trace->is_static_listeners,
50842bdc 576 struct bt_trace_is_static_listener_elem,
3602afb0 577 listener_id);
44c440bc 578 BT_ASSERT(elem->func);
3602afb0 579
8480c8cc
PP
580 if (elem->removed) {
581 /* Call remove listener */
44c440bc
PP
582 BT_LIB_LOGV("Calling remove listener: "
583 "%![trace-]+t, listener-id=%" PRIu64,
584 trace, listener_id);
585 trace->in_remove_listener = true;
8480c8cc 586 elem->removed(trace, elem->data);
44c440bc 587 trace->in_remove_listener = false;
8480c8cc
PP
588 }
589
3602afb0 590 elem->func = NULL;
8480c8cc 591 elem->removed = NULL;
3602afb0 592 elem->data = NULL;
44c440bc
PP
593 BT_LIB_LOGV("Removed \"trace is static\" listener: "
594 "%![trace-]+t, listener-id=%" PRIu64,
595 trace, listener_id);
596 return 0;
597}
3602afb0 598
44c440bc
PP
599BT_HIDDEN
600void _bt_trace_freeze(struct bt_trace *trace)
601{
5cd6d0e5 602 /* The packet header field classe is already frozen */
44c440bc
PP
603 BT_ASSERT(trace);
604 BT_LIB_LOGD("Freezing trace: %!+t", trace);
605 trace->frozen = true;
5acf2ae6 606}
312c056a 607
44c440bc 608bt_bool bt_trace_assigns_automatic_stream_class_id(struct bt_trace *trace)
312c056a 609{
44c440bc
PP
610 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
611 return (bt_bool) trace->assigns_automatic_stream_class_id;
612}
312c056a 613
44c440bc
PP
614int bt_trace_set_assigns_automatic_stream_class_id(
615 struct bt_trace *trace, bt_bool value)
616{
312c056a 617 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc
PP
618 BT_ASSERT_PRE_TRACE_HOT(trace);
619 trace->assigns_automatic_stream_class_id = (bool) value;
620 BT_LIB_LOGV("Set trace's automatic stream class ID "
621 "assignment property: %!+t", trace);
622 return 0;
623}
312c056a 624
44c440bc
PP
625BT_HIDDEN
626void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream)
627{
628 guint count = 0;
629
630 bt_object_set_parent(&stream->base, &trace->base);
631 g_ptr_array_add(trace->streams, stream);
cb6f1f7d 632 bt_trace_freeze(trace);
312c056a 633
44c440bc
PP
634 if (bt_g_hash_table_contains(trace->stream_classes_stream_count,
635 stream->class)) {
636 count = GPOINTER_TO_UINT(g_hash_table_lookup(
637 trace->stream_classes_stream_count, stream->class));
312c056a
PP
638 }
639
44c440bc
PP
640 g_hash_table_insert(trace->stream_classes_stream_count,
641 stream->class, GUINT_TO_POINTER(count + 1));
642}
643
644BT_HIDDEN
645uint64_t bt_trace_get_automatic_stream_id(struct bt_trace *trace,
646 struct bt_stream_class *stream_class)
647{
648 gpointer orig_key;
649 gpointer value;
650 uint64_t id = 0;
651
652 BT_ASSERT(stream_class);
653 BT_ASSERT(trace);
654 if (g_hash_table_lookup_extended(trace->stream_classes_stream_count,
655 stream_class, &orig_key, &value)) {
656 id = (uint64_t) GPOINTER_TO_UINT(value);
657 }
658
659 return id;
312c056a 660}
This page took 0.087492 seconds and 4 git commands to generate.