Make API CTF-agnostic
[babeltrace.git] / lib / ctf-ir / trace.c
CommitLineData
bc37ae52
JG
1/*
2 * trace.c
3 *
4 * Babeltrace CTF IR - Trace
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>
bc37ae52 33#include <babeltrace/ctf-ir/trace-internal.h>
ac0c6bdd 34#include <babeltrace/ctf-ir/clock-class-internal.h>
bc37ae52
JG
35#include <babeltrace/ctf-ir/stream-internal.h>
36#include <babeltrace/ctf-ir/stream-class-internal.h>
09840de5 37#include <babeltrace/ctf-ir/event-internal.h>
272df73e
PP
38#include <babeltrace/ctf-ir/event-class.h>
39#include <babeltrace/ctf-ir/event-class-internal.h>
bc37ae52 40#include <babeltrace/ctf-writer/functor-internal.h>
ac0c6bdd 41#include <babeltrace/ctf-writer/clock-internal.h>
312c056a 42#include <babeltrace/ctf-ir/field-wrapper-internal.h>
2e33ac5a 43#include <babeltrace/ctf-ir/field-types-internal.h>
44e0a4f5 44#include <babeltrace/ctf-ir/attributes-internal.h>
726106d3 45#include <babeltrace/ctf-ir/utils-internal.h>
44c440bc 46#include <babeltrace/ctf-ir/resolve-field-path-internal.h>
3d9990ac 47#include <babeltrace/compiler-internal.h>
dac5c838 48#include <babeltrace/values.h>
95c09b3a 49#include <babeltrace/values-internal.h>
83509119 50#include <babeltrace/ref.h>
c55a9f58 51#include <babeltrace/types.h>
3d9990ac 52#include <babeltrace/endian-internal.h>
f6ccaed9 53#include <babeltrace/assert-internal.h>
44c440bc 54#include <babeltrace/compat/glib-internal.h>
dc3fffef 55#include <inttypes.h>
544d0515 56#include <stdint.h>
4a32fda0 57#include <string.h>
0fbb9a9f 58#include <stdlib.h>
bc37ae52 59
50842bdc
PP
60struct bt_trace_is_static_listener_elem {
61 bt_trace_is_static_listener func;
62 bt_trace_listener_removed removed;
3602afb0
PP
63 void *data;
64};
65
44c440bc
PP
66#define BT_ASSERT_PRE_TRACE_HOT(_trace) \
67 BT_ASSERT_PRE_HOT((_trace), "Trace", ": %!+t", (_trace))
cb6f1f7d 68
bc37ae52 69static
44c440bc 70void destroy_trace(struct bt_object *obj)
3dca2276
PP
71{
72 struct bt_trace *trace = (void *) obj;
bc37ae52 73
44c440bc 74 BT_LIB_LOGD("Destroying trace object: %!+t", trace);
bc37ae52 75
3dca2276
PP
76 /*
77 * Call remove listeners first so that everything else still
78 * exists in the trace.
79 */
80 if (trace->is_static_listeners) {
81 size_t i;
bc37ae52 82
3dca2276
PP
83 for (i = 0; i < trace->is_static_listeners->len; i++) {
84 struct bt_trace_is_static_listener_elem elem =
85 g_array_index(trace->is_static_listeners,
86 struct bt_trace_is_static_listener_elem, i);
bc37ae52 87
3dca2276
PP
88 if (elem.removed) {
89 elem.removed(trace, elem.data);
90 }
91 }
92
93 g_array_free(trace->is_static_listeners, TRUE);
bc37ae52
JG
94 }
95
312c056a 96 bt_object_pool_finalize(&trace->packet_header_field_pool);
3dca2276 97
44c440bc
PP
98 if (trace->environment) {
99 BT_LOGD_STR("Destroying environment attributes.");
100 bt_attributes_destroy(trace->environment);
95c09b3a
PP
101 }
102
44c440bc
PP
103 if (trace->name.str) {
104 g_string_free(trace->name.str, TRUE);
95c09b3a
PP
105 }
106
44c440bc
PP
107 if (trace->streams) {
108 BT_LOGD_STR("Destroying streams.");
109 g_ptr_array_free(trace->streams, TRUE);
bc37ae52
JG
110 }
111
44c440bc
PP
112 if (trace->stream_classes) {
113 BT_LOGD_STR("Destroying stream classes.");
114 g_ptr_array_free(trace->stream_classes, TRUE);
7f800dc7
PP
115 }
116
44c440bc
PP
117 if (trace->stream_classes_stream_count) {
118 g_hash_table_destroy(trace->stream_classes_stream_count);
119 }
3dca2276 120
44c440bc
PP
121 BT_LOGD_STR("Putting packet header field type.");
122 bt_put(trace->packet_header_ft);
123 g_free(trace);
3dca2276
PP
124}
125
312c056a
PP
126static
127void free_packet_header_field(struct bt_field_wrapper *field_wrapper,
128 struct bt_trace *trace)
129{
130 bt_field_wrapper_destroy(field_wrapper);
131}
132
3dca2276
PP
133struct bt_trace *bt_trace_create(void)
134{
135 struct bt_trace *trace = NULL;
136 int ret;
137
44c440bc 138 BT_LOGD_STR("Creating default trace object.");
3dca2276
PP
139 trace = g_new0(struct bt_trace, 1);
140 if (!trace) {
141 BT_LOGE_STR("Failed to allocate one trace.");
142 goto error;
143 }
144
44c440bc
PP
145 bt_object_init_shared_with_parent(&trace->base, destroy_trace);
146 trace->streams = g_ptr_array_new_with_free_func(
147 (GDestroyNotify) bt_object_try_spec_release);
148 if (!trace->streams) {
149 BT_LOGE_STR("Failed to allocate one GPtrArray.");
3dca2276
PP
150 goto error;
151 }
152
44c440bc
PP
153 trace->stream_classes = g_ptr_array_new_with_free_func(
154 (GDestroyNotify) bt_object_try_spec_release);
155 if (!trace->stream_classes) {
95c09b3a 156 BT_LOGE_STR("Failed to allocate one GPtrArray.");
9b888ff3
JG
157 goto error;
158 }
159
44c440bc
PP
160 trace->stream_classes_stream_count = g_hash_table_new(g_direct_hash,
161 g_direct_equal);
162 if (!trace->stream_classes_stream_count) {
163 BT_LOGE_STR("Failed to allocate one GHashTable.");
164 goto error;
165 }
166
167 trace->name.str = g_string_new(NULL);
168 if (!trace->name.str) {
169 BT_LOGE_STR("Failed to allocate one GString.");
170 goto error;
171 }
172
173 trace->environment = bt_attributes_create();
174 if (!trace->environment) {
175 BT_LOGE_STR("Cannot create empty attributes object.");
176 goto error;
177 }
178
3602afb0 179 trace->is_static_listeners = g_array_new(FALSE, TRUE,
50842bdc 180 sizeof(struct bt_trace_is_static_listener_elem));
3602afb0
PP
181 if (!trace->is_static_listeners) {
182 BT_LOGE_STR("Failed to allocate one GArray.");
183 goto error;
184 }
185
44c440bc 186 trace->assigns_automatic_stream_class_id = true;
312c056a
PP
187 ret = bt_object_pool_initialize(&trace->packet_header_field_pool,
188 (bt_object_pool_new_object_func) bt_field_wrapper_new,
189 (bt_object_pool_destroy_object_func) free_packet_header_field,
190 trace);
191 if (ret) {
192 BT_LOGE("Failed to initialize packet header field pool: ret=%d",
193 ret);
194 goto error;
195 }
196
44c440bc
PP
197 BT_LIB_LOGD("Created trace object: %!+t", trace);
198 goto end;
bc37ae52 199
bc37ae52 200error:
83509119 201 BT_PUT(trace);
44c440bc
PP
202
203end:
bc37ae52
JG
204 return trace;
205}
206
50842bdc 207const char *bt_trace_get_name(struct bt_trace *trace)
e96045d4 208{
cb6f1f7d 209 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 210 return trace->name.value;
e96045d4
JG
211}
212
cb6f1f7d 213int bt_trace_set_name(struct bt_trace *trace, const char *name)
e96045d4 214{
44c440bc
PP
215 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
216 BT_ASSERT_PRE_NON_NULL(name, "Name");
217 BT_ASSERT_PRE_TRACE_HOT(trace);
218 g_string_assign(trace->name.str, name);
219 trace->name.value = trace->name.str->str;
220 BT_LIB_LOGV("Set trace's name: %!+t", trace);
221 return 0;
e96045d4
JG
222}
223
44c440bc 224bt_uuid bt_trace_get_uuid(struct bt_trace *trace)
4a32fda0 225{
cb6f1f7d 226 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 227 return trace->uuid.value;
4a32fda0
PP
228}
229
44c440bc 230int bt_trace_set_uuid(struct bt_trace *trace, bt_uuid uuid)
4a32fda0 231{
44c440bc
PP
232 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
233 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
234 BT_ASSERT_PRE_TRACE_HOT(trace);
235 memcpy(trace->uuid.uuid, uuid, BABELTRACE_UUID_LEN);
236 trace->uuid.value = trace->uuid.uuid;
237 BT_LIB_LOGV("Set trace's UUID: %!+t", trace);
238 return 0;
4a32fda0
PP
239}
240
44c440bc
PP
241BT_ASSERT_FUNC
242static
243bool trace_has_environment_entry(struct bt_trace *trace, const char *name)
bc37ae52 244{
44c440bc 245 struct bt_value *attribute;
95c09b3a 246
44c440bc 247 BT_ASSERT(trace);
95c09b3a 248
44c440bc
PP
249 attribute = bt_attributes_borrow_field_value_by_name(
250 trace->environment, name);
251 return attribute != NULL;
252}
a0d12916 253
44c440bc
PP
254static
255int set_environment_entry(struct bt_trace *trace, const char *name,
256 struct bt_value *value)
257{
258 int ret;
a0d12916 259
44c440bc
PP
260 BT_ASSERT(trace);
261 BT_ASSERT(name);
262 BT_ASSERT(value);
263 BT_ASSERT_PRE(!trace->frozen ||
264 !trace_has_environment_entry(trace, name),
265 "Trace is frozen: cannot replace environment entry: "
266 "%![trace-]+t, entry-name=\"%s\"", trace, name);
50842bdc 267 ret = bt_attributes_set_field_value(trace->environment, name,
7f800dc7 268 value);
44c440bc 269 bt_value_freeze(value);
95c09b3a 270 if (ret) {
44c440bc
PP
271 BT_LIB_LOGE("Cannot set trace's environment entry: "
272 "%![trace-]+t, entry-name=\"%s\"", trace, name);
95c09b3a 273 } else {
44c440bc
PP
274 BT_LIB_LOGV("Set trace's environment entry: "
275 "%![trace-]+t, entry-name=\"%s\"", trace, name);
95c09b3a 276 }
bc37ae52 277
7f800dc7
PP
278 return ret;
279}
bc37ae52 280
44c440bc 281int bt_trace_set_environment_entry_string(struct bt_trace *trace,
7f800dc7
PP
282 const char *name, const char *value)
283{
44c440bc
PP
284 int ret;
285 struct bt_value *value_obj;
bc37ae52 286
44c440bc
PP
287 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
288 BT_ASSERT_PRE_NON_NULL(name, "Name");
289 BT_ASSERT_PRE_NON_NULL(value, "Value");
290 value_obj = bt_value_string_create_init(value);
291 if (!value_obj) {
292 BT_LOGE_STR("Cannot create a string value object.");
7f800dc7
PP
293 ret = -1;
294 goto end;
bc37ae52
JG
295 }
296
44c440bc
PP
297 /* set_environment_entry() logs errors */
298 ret = set_environment_entry(trace, name, value_obj);
7f800dc7
PP
299
300end:
44c440bc 301 bt_put(value_obj);
3487c9f3
JG
302 return ret;
303}
304
44c440bc 305int bt_trace_set_environment_entry_integer(
cb6f1f7d 306 struct bt_trace *trace, const char *name, int64_t value)
3487c9f3 307{
44c440bc
PP
308 int ret;
309 struct bt_value *value_obj;
3487c9f3 310
44c440bc
PP
311 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
312 BT_ASSERT_PRE_NON_NULL(name, "Name");
313 value_obj = bt_value_integer_create_init(value);
314 if (!value_obj) {
315 BT_LOGE_STR("Cannot create an integer value object.");
3487c9f3 316 ret = -1;
7f800dc7 317 goto end;
3487c9f3
JG
318 }
319
44c440bc
PP
320 /* set_environment_entry() logs errors */
321 ret = set_environment_entry(trace, name, value_obj);
95c09b3a 322
7f800dc7 323end:
44c440bc 324 bt_put(value_obj);
bc37ae52
JG
325 return ret;
326}
327
44c440bc 328uint64_t bt_trace_get_environment_entry_count(struct bt_trace *trace)
3dca2276 329{
cb6f1f7d
PP
330 int64_t ret;
331
332 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
333 ret = bt_attributes_get_count(trace->environment);
334 BT_ASSERT(ret >= 0);
44c440bc 335 return (uint64_t) ret;
e6fa2160
JG
336}
337
44c440bc
PP
338void bt_trace_borrow_environment_entry_by_index(
339 struct bt_trace *trace, uint64_t index,
340 const char **name, struct bt_value **value)
e6fa2160 341{
cb6f1f7d 342 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc
PP
343 BT_ASSERT_PRE_NON_NULL(name, "Name");
344 BT_ASSERT_PRE_NON_NULL(value, "Value");
345 BT_ASSERT_PRE_VALID_INDEX(index,
346 bt_attributes_get_count(trace->environment));
347 *value = bt_attributes_borrow_field_value(trace->environment, index);
348 BT_ASSERT(*value);
349 *name = bt_attributes_get_field_name(trace->environment, index);
350 BT_ASSERT(*name);
e6fa2160
JG
351}
352
44c440bc 353struct bt_value *bt_trace_borrow_environment_entry_value_by_name(
50842bdc 354 struct bt_trace *trace, const char *name)
e6fa2160 355{
cb6f1f7d
PP
356 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
357 BT_ASSERT_PRE_NON_NULL(name, "Name");
358 return 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
44c440bc
PP
434struct bt_field_type *bt_trace_borrow_packet_header_field_type(
435 struct bt_trace *trace)
436{
437 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
438 return trace->packet_header_ft;
439}
e011d2c1 440
44c440bc
PP
441int bt_trace_set_packet_header_field_type(struct bt_trace *trace,
442 struct bt_field_type *field_type)
443{
444 int ret;
445 struct bt_resolve_field_path_context resolve_ctx = {
446 .packet_header = field_type,
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
PP
454 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
455 BT_ASSERT_PRE_NON_NULL(field_type, "Field type");
456 BT_ASSERT_PRE_TRACE_HOT(trace);
457 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type) ==
458 BT_FIELD_TYPE_ID_STRUCTURE,
459 "Packet header field type is not a structure field type: %!+F",
460 field_type);
461 ret = bt_resolve_field_paths(field_type, &resolve_ctx);
462 if (ret) {
463 goto end;
e011d2c1
PP
464 }
465
44c440bc
PP
466 bt_field_type_make_part_of_trace(field_type);
467 bt_put(trace->packet_header_ft);
468 trace->packet_header_ft = bt_get(field_type);
469 bt_field_type_freeze(field_type);
470 BT_LIB_LOGV("Set trace's packet header field type: %!+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{
602 /* The packet header field type is already frozen */
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.091031 seconds and 4 git commands to generate.