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