lib: make values 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
8deee039 28#include <babeltrace/assert-pre-internal.h>
9e550e5f 29#include <babeltrace/trace-ir/private-trace.h>
108b91d0
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>
108b91d0 39#include <babeltrace/trace-ir/field-wrapper-internal.h>
939190b3 40#include <babeltrace/trace-ir/field-classes-internal.h>
108b91d0
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>
ce141536 46#include <babeltrace/values-const.h>
95c09b3a 47#include <babeltrace/values-internal.h>
8138bfe1 48#include <babeltrace/object.h>
c55a9f58 49#include <babeltrace/types.h>
3d9990ac 50#include <babeltrace/endian-internal.h>
8b45963b 51#include <babeltrace/assert-internal.h>
7b33a0e0 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
839d52a5 58struct bt_trace_is_static_listener_elem {
9e550e5f
PP
59 bt_private_trace_is_static_listener func;
60 bt_private_trace_listener_removed removed;
3602afb0
PP
61 void *data;
62};
63
7b33a0e0
PP
64#define BT_ASSERT_PRE_TRACE_HOT(_trace) \
65 BT_ASSERT_PRE_HOT((_trace), "Trace", ": %!+t", (_trace))
18acc6f8 66
bc37ae52 67static
7b33a0e0 68void destroy_trace(struct bt_object *obj)
8deee039
PP
69{
70 struct bt_trace *trace = (void *) obj;
bc37ae52 71
7b33a0e0 72 BT_LIB_LOGD("Destroying trace object: %!+t", trace);
bc37ae52 73
8deee039
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
8deee039
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
8deee039 86 if (elem.removed) {
9e550e5f 87 elem.removed((void *) trace, elem.data);
8deee039
PP
88 }
89 }
90
91 g_array_free(trace->is_static_listeners, TRUE);
1248f5ea 92 trace->is_static_listeners = NULL;
bc37ae52
JG
93 }
94
a6918753 95 bt_object_pool_finalize(&trace->packet_header_field_pool);
8deee039 96
7b33a0e0
PP
97 if (trace->environment) {
98 BT_LOGD_STR("Destroying environment attributes.");
99 bt_attributes_destroy(trace->environment);
1248f5ea 100 trace->environment = NULL;
95c09b3a
PP
101 }
102
7b33a0e0
PP
103 if (trace->name.str) {
104 g_string_free(trace->name.str, TRUE);
1248f5ea
PP
105 trace->name.str = NULL;
106 trace->name.value = NULL;
95c09b3a
PP
107 }
108
7b33a0e0
PP
109 if (trace->streams) {
110 BT_LOGD_STR("Destroying streams.");
111 g_ptr_array_free(trace->streams, TRUE);
1248f5ea 112 trace->streams = NULL;
bc37ae52
JG
113 }
114
7b33a0e0
PP
115 if (trace->stream_classes) {
116 BT_LOGD_STR("Destroying stream classes.");
117 g_ptr_array_free(trace->stream_classes, TRUE);
1248f5ea 118 trace->stream_classes = NULL;
7f800dc7
PP
119 }
120
7b33a0e0
PP
121 if (trace->stream_classes_stream_count) {
122 g_hash_table_destroy(trace->stream_classes_stream_count);
1248f5ea 123 trace->stream_classes_stream_count = NULL;
7b33a0e0 124 }
8deee039 125
939190b3 126 BT_LOGD_STR("Putting packet header field classe.");
8138bfe1 127 bt_object_put_ref(trace->packet_header_fc);
1248f5ea 128 trace->packet_header_fc = NULL;
7b33a0e0 129 g_free(trace);
8deee039
PP
130}
131
a6918753
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
9e550e5f 139struct bt_private_trace *bt_private_trace_create(void)
8deee039
PP
140{
141 struct bt_trace *trace = NULL;
142 int ret;
143
7b33a0e0 144 BT_LOGD_STR("Creating default trace object.");
8deee039
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
7b33a0e0
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.");
8deee039
PP
156 goto error;
157 }
158
7b33a0e0
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
7b33a0e0
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,
839d52a5 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
7b33a0e0 192 trace->assigns_automatic_stream_class_id = true;
a6918753
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
7b33a0e0
PP
203 BT_LIB_LOGD("Created trace object: %!+t", trace);
204 goto end;
bc37ae52 205
bc37ae52 206error:
8138bfe1 207 BT_OBJECT_PUT_REF_AND_RESET(trace);
7b33a0e0
PP
208
209end:
9e550e5f 210 return (void *) trace;
bc37ae52
JG
211}
212
839d52a5 213const char *bt_trace_get_name(struct bt_trace *trace)
e96045d4 214{
18acc6f8 215 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
7b33a0e0 216 return trace->name.value;
e96045d4
JG
217}
218
9e550e5f
PP
219int bt_private_trace_set_name(struct bt_private_trace *priv_trace,
220 const char *name)
e96045d4 221{
9e550e5f
PP
222 struct bt_trace *trace = (void *) priv_trace;
223
7b33a0e0
PP
224 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
225 BT_ASSERT_PRE_NON_NULL(name, "Name");
226 BT_ASSERT_PRE_TRACE_HOT(trace);
227 g_string_assign(trace->name.str, name);
228 trace->name.value = trace->name.str->str;
229 BT_LIB_LOGV("Set trace's name: %!+t", trace);
230 return 0;
e96045d4
JG
231}
232
7b33a0e0 233bt_uuid bt_trace_get_uuid(struct bt_trace *trace)
4a32fda0 234{
18acc6f8 235 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
7b33a0e0 236 return trace->uuid.value;
4a32fda0
PP
237}
238
c8bbf821
PP
239void bt_private_trace_set_uuid(struct bt_private_trace *priv_trace,
240 bt_uuid uuid)
4a32fda0 241{
9e550e5f
PP
242 struct bt_trace *trace = (void *) priv_trace;
243
7b33a0e0
PP
244 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
245 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
246 BT_ASSERT_PRE_TRACE_HOT(trace);
247 memcpy(trace->uuid.uuid, uuid, BABELTRACE_UUID_LEN);
248 trace->uuid.value = trace->uuid.uuid;
249 BT_LIB_LOGV("Set trace's UUID: %!+t", trace);
4a32fda0
PP
250}
251
7b33a0e0
PP
252BT_ASSERT_FUNC
253static
254bool trace_has_environment_entry(struct bt_trace *trace, const char *name)
bc37ae52 255{
7b33a0e0 256 BT_ASSERT(trace);
95c09b3a 257
17582c6d
PP
258 return bt_attributes_borrow_field_value_by_name(
259 trace->environment, name) != NULL;
7b33a0e0 260}
a0d12916 261
7b33a0e0
PP
262static
263int set_environment_entry(struct bt_trace *trace, const char *name,
ce141536 264 struct bt_value *value)
7b33a0e0
PP
265{
266 int ret;
a0d12916 267
7b33a0e0
PP
268 BT_ASSERT(trace);
269 BT_ASSERT(name);
270 BT_ASSERT(value);
271 BT_ASSERT_PRE(!trace->frozen ||
272 !trace_has_environment_entry(trace, name),
273 "Trace is frozen: cannot replace environment entry: "
274 "%![trace-]+t, entry-name=\"%s\"", trace, name);
839d52a5 275 ret = bt_attributes_set_field_value(trace->environment, name,
7f800dc7 276 value);
ce141536 277 bt_value_freeze(value);
95c09b3a 278 if (ret) {
7b33a0e0
PP
279 BT_LIB_LOGE("Cannot set trace's environment entry: "
280 "%![trace-]+t, entry-name=\"%s\"", trace, name);
95c09b3a 281 } else {
7b33a0e0
PP
282 BT_LIB_LOGV("Set trace's environment entry: "
283 "%![trace-]+t, entry-name=\"%s\"", trace, name);
95c09b3a 284 }
bc37ae52 285
7f800dc7
PP
286 return ret;
287}
bc37ae52 288
96854e6a 289int bt_private_trace_set_environment_entry_string(
9e550e5f 290 struct bt_private_trace *priv_trace,
7f800dc7
PP
291 const char *name, const char *value)
292{
7b33a0e0 293 int ret;
ce141536 294 struct bt_value *value_obj;
9e550e5f 295 struct bt_trace *trace = (void *) priv_trace;
bc37ae52 296
7b33a0e0
PP
297 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
298 BT_ASSERT_PRE_NON_NULL(name, "Name");
299 BT_ASSERT_PRE_NON_NULL(value, "Value");
ce141536 300 value_obj = bt_value_string_create_init(value);
7b33a0e0
PP
301 if (!value_obj) {
302 BT_LOGE_STR("Cannot create a string value object.");
7f800dc7
PP
303 ret = -1;
304 goto end;
bc37ae52
JG
305 }
306
7b33a0e0
PP
307 /* set_environment_entry() logs errors */
308 ret = set_environment_entry(trace, name, value_obj);
7f800dc7
PP
309
310end:
8138bfe1 311 bt_object_put_ref(value_obj);
3487c9f3
JG
312 return ret;
313}
314
96854e6a 315int bt_private_trace_set_environment_entry_integer(
9e550e5f
PP
316 struct bt_private_trace *priv_trace,
317 const char *name, int64_t value)
3487c9f3 318{
7b33a0e0 319 int ret;
ce141536 320 struct bt_value *value_obj;
9e550e5f 321 struct bt_trace *trace = (void *) priv_trace;
3487c9f3 322
7b33a0e0
PP
323 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
324 BT_ASSERT_PRE_NON_NULL(name, "Name");
ce141536 325 value_obj = bt_value_integer_create_init(value);
7b33a0e0
PP
326 if (!value_obj) {
327 BT_LOGE_STR("Cannot create an integer value object.");
3487c9f3 328 ret = -1;
7f800dc7 329 goto end;
3487c9f3
JG
330 }
331
7b33a0e0
PP
332 /* set_environment_entry() logs errors */
333 ret = set_environment_entry(trace, name, value_obj);
95c09b3a 334
7f800dc7 335end:
8138bfe1 336 bt_object_put_ref(value_obj);
bc37ae52
JG
337 return ret;
338}
339
7b33a0e0 340uint64_t bt_trace_get_environment_entry_count(struct bt_trace *trace)
8deee039 341{
18acc6f8
PP
342 int64_t ret;
343
344 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
345 ret = bt_attributes_get_count(trace->environment);
346 BT_ASSERT(ret >= 0);
7b33a0e0 347 return (uint64_t) ret;
e6fa2160
JG
348}
349
7b33a0e0
PP
350void bt_trace_borrow_environment_entry_by_index(
351 struct bt_trace *trace, uint64_t index,
ce141536 352 const char **name, const struct bt_value **value)
e6fa2160 353{
18acc6f8 354 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
7b33a0e0
PP
355 BT_ASSERT_PRE_NON_NULL(name, "Name");
356 BT_ASSERT_PRE_NON_NULL(value, "Value");
357 BT_ASSERT_PRE_VALID_INDEX(index,
358 bt_attributes_get_count(trace->environment));
ce141536 359 *value = bt_attributes_borrow_field_value(trace->environment, index);
7b33a0e0
PP
360 BT_ASSERT(*value);
361 *name = bt_attributes_get_field_name(trace->environment, index);
362 BT_ASSERT(*name);
e6fa2160
JG
363}
364
96854e6a 365void bt_private_trace_borrow_environment_entry_by_index(
9e550e5f 366 struct bt_private_trace *trace, uint64_t index,
ce141536 367 const char **name, const struct bt_value **value)
9e550e5f
PP
368{
369 bt_trace_borrow_environment_entry_by_index((void *) trace,
370 index, name, (void *) value);
371}
372
ce141536 373const struct bt_value *bt_trace_borrow_environment_entry_value_by_name(
839d52a5 374 struct bt_trace *trace, const char *name)
e6fa2160 375{
18acc6f8
PP
376 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
377 BT_ASSERT_PRE_NON_NULL(name, "Name");
ce141536
PP
378 return bt_attributes_borrow_field_value_by_name(trace->environment,
379 name);
e6fa2160
JG
380}
381
ce141536 382const struct bt_value *
96854e6a 383bt_private_trace_borrow_environment_entry_value_by_name(
9e550e5f
PP
384 struct bt_private_trace *trace, const char *name)
385{
386 return (void *) bt_trace_borrow_environment_entry_value_by_name(
387 (void *) trace, name);
388}
389
7b33a0e0 390uint64_t bt_trace_get_stream_count(struct bt_trace *trace)
bc37ae52 391{
7b33a0e0
PP
392 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
393 return (uint64_t) trace->streams->len;
394}
95c09b3a 395
7b33a0e0
PP
396struct bt_stream *bt_trace_borrow_stream_by_index(
397 struct bt_trace *trace, uint64_t index)
398{
399 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
400 BT_ASSERT_PRE_VALID_INDEX(index, trace->streams->len);
401 return g_ptr_array_index(trace->streams, index);
402}
18acc6f8 403
96854e6a 404struct bt_private_stream *bt_private_trace_borrow_stream_by_index(
9e550e5f
PP
405 struct bt_private_trace *trace, uint64_t index)
406{
407 return (void *) bt_trace_borrow_stream_by_index((void *) trace, index);
408}
409
7b33a0e0
PP
410struct bt_stream *bt_trace_borrow_stream_by_id(
411 struct bt_trace *trace, uint64_t id)
412{
413 struct bt_stream *stream = NULL;
414 uint64_t i;
bc37ae52 415
7b33a0e0 416 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
bc37ae52 417
7b33a0e0
PP
418 for (i = 0; i < trace->streams->len; i++) {
419 struct bt_stream *stream_candidate =
420 g_ptr_array_index(trace->streams, i);
cfeb617e 421
7b33a0e0
PP
422 if (stream_candidate->id == id) {
423 stream = stream_candidate;
424 goto end;
425 }
6474e016 426 }
95c09b3a 427
bc37ae52 428end:
7b33a0e0 429 return stream;
bc37ae52
JG
430}
431
9e550e5f
PP
432struct bt_private_stream *bt_private_trace_borrow_private_stream_by_id(
433 struct bt_private_trace *trace, uint64_t id)
434{
435 return (void *) bt_trace_borrow_stream_by_id((void *) trace, id);
436}
437
7b33a0e0 438uint64_t bt_trace_get_stream_class_count(struct bt_trace *trace)
884cd6c3 439{
18acc6f8 440 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
7b33a0e0 441 return (uint64_t) trace->stream_classes->len;
884cd6c3
JG
442}
443
7b33a0e0 444struct bt_stream_class *bt_trace_borrow_stream_class_by_index(
839d52a5 445 struct bt_trace *trace, uint64_t index)
884cd6c3 446{
18acc6f8 447 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
7b33a0e0
PP
448 BT_ASSERT_PRE_VALID_INDEX(index, trace->stream_classes->len);
449 return g_ptr_array_index(trace->stream_classes, index);
884cd6c3
JG
450}
451
9e550e5f 452struct bt_private_stream_class *
96854e6a 453bt_private_trace_borrow_stream_class_by_index(
9e550e5f
PP
454 struct bt_private_trace *trace, uint64_t index)
455{
456 return (void *) bt_trace_borrow_stream_class_by_index(
457 (void *) trace, index);
458}
459
7b33a0e0
PP
460struct bt_stream_class *bt_trace_borrow_stream_class_by_id(
461 struct bt_trace *trace, uint64_t id)
e011d2c1 462{
7b33a0e0
PP
463 struct bt_stream_class *stream_class = NULL;
464 uint64_t i;
e011d2c1 465
7b33a0e0 466 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
e011d2c1 467
7b33a0e0
PP
468 for (i = 0; i < trace->stream_classes->len; i++) {
469 struct bt_stream_class *stream_class_candidate =
470 g_ptr_array_index(trace->stream_classes, i);
e011d2c1 471
7b33a0e0
PP
472 if (stream_class_candidate->id == id) {
473 stream_class = stream_class_candidate;
474 goto end;
e011d2c1 475 }
7b33a0e0 476 }
e011d2c1 477
7b33a0e0
PP
478end:
479 return stream_class;
480}
e011d2c1 481
9e550e5f 482struct bt_private_stream_class *
96854e6a 483bt_private_trace_borrow_stream_class_by_id(
9e550e5f
PP
484 struct bt_private_trace *trace, uint64_t id)
485{
486 return (void *) bt_trace_borrow_stream_class_by_id((void *) trace, id);
487}
488
939190b3 489struct bt_field_class *bt_trace_borrow_packet_header_field_class(
7b33a0e0
PP
490 struct bt_trace *trace)
491{
492 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
939190b3 493 return trace->packet_header_fc;
7b33a0e0 494}
e011d2c1 495
96854e6a 496int bt_private_trace_set_packet_header_field_class(
9e550e5f
PP
497 struct bt_private_trace *priv_trace,
498 struct bt_private_field_class *priv_field_class)
7b33a0e0
PP
499{
500 int ret;
9e550e5f
PP
501 struct bt_trace *trace = (void *) priv_trace;
502 struct bt_field_class *field_class = (void *) priv_field_class;
7b33a0e0 503 struct bt_resolve_field_path_context resolve_ctx = {
939190b3 504 .packet_header = field_class,
7b33a0e0
PP
505 .packet_context = NULL,
506 .event_header = NULL,
507 .event_common_context = NULL,
508 .event_specific_context = NULL,
509 .event_payload = NULL,
510 };
e011d2c1 511
7b33a0e0 512 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
939190b3 513 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
7b33a0e0 514 BT_ASSERT_PRE_TRACE_HOT(trace);
af0c18e3
PP
515 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
516 BT_FIELD_CLASS_TYPE_STRUCTURE,
939190b3
PP
517 "Packet header field classe is not a structure field classe: %!+F",
518 field_class);
519 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
7b33a0e0
PP
520 if (ret) {
521 goto end;
e011d2c1
PP
522 }
523
939190b3 524 bt_field_class_make_part_of_trace(field_class);
8138bfe1 525 bt_object_put_ref(trace->packet_header_fc);
4b70020d
PP
526 trace->packet_header_fc = field_class;
527 bt_object_get_no_null_check(trace->packet_header_fc);
939190b3
PP
528 bt_field_class_freeze(field_class);
529 BT_LIB_LOGV("Set trace's packet header field classe: %!+t", trace);
e011d2c1 530
8bf65fbd
JG
531end:
532 return ret;
533}
534
839d52a5 535bt_bool bt_trace_is_static(struct bt_trace *trace)
5acf2ae6 536{
44ea72c5 537 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
7b33a0e0 538 return (bt_bool) trace->is_static;
5acf2ae6
PP
539}
540
9e550e5f 541int bt_private_trace_make_static(struct bt_private_trace *priv_trace)
5acf2ae6 542{
9e550e5f 543 struct bt_trace *trace = (void *) priv_trace;
7b33a0e0 544 uint64_t i;
bae99cf9 545
7b33a0e0
PP
546 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
547 trace->is_static = true;
18acc6f8 548 bt_trace_freeze(trace);
7b33a0e0 549 BT_LIB_LOGV("Trace is now static: %!+t", trace);
5acf2ae6 550
3602afb0
PP
551 /* Call all the "trace is static" listeners */
552 for (i = 0; i < trace->is_static_listeners->len; i++) {
839d52a5 553 struct bt_trace_is_static_listener_elem elem =
3602afb0 554 g_array_index(trace->is_static_listeners,
839d52a5 555 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
556
557 if (elem.func) {
9e550e5f 558 elem.func((void *) trace, elem.data);
3602afb0
PP
559 }
560 }
561
7b33a0e0 562 return 0;
3602afb0
PP
563}
564
9e550e5f
PP
565int bt_private_trace_add_is_static_listener(
566 struct bt_private_trace *priv_trace,
567 bt_private_trace_is_static_listener listener,
568 bt_private_trace_listener_removed listener_removed, void *data,
7b33a0e0 569 uint64_t *listener_id)
3602afb0 570{
9e550e5f 571 struct bt_trace *trace = (void *) priv_trace;
7b33a0e0 572 uint64_t i;
839d52a5 573 struct bt_trace_is_static_listener_elem new_elem = {
3602afb0 574 .func = listener,
9962ad4b 575 .removed = listener_removed,
3602afb0
PP
576 .data = data,
577 };
578
7b33a0e0
PP
579 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
580 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
581 BT_ASSERT_PRE(!trace->is_static,
582 "Trace is already static: %!+t", trace);
583 BT_ASSERT_PRE(trace->in_remove_listener,
584 "Cannot call this function while executing a "
585 "remove listener: %!+t", trace);
9962ad4b 586
3602afb0
PP
587 /* Find the next available spot */
588 for (i = 0; i < trace->is_static_listeners->len; i++) {
839d52a5 589 struct bt_trace_is_static_listener_elem elem =
3602afb0 590 g_array_index(trace->is_static_listeners,
839d52a5 591 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
592
593 if (!elem.func) {
594 break;
595 }
596 }
597
598 if (i == trace->is_static_listeners->len) {
599 g_array_append_val(trace->is_static_listeners, new_elem);
600 } else {
601 g_array_insert_val(trace->is_static_listeners, i, new_elem);
602 }
603
7b33a0e0
PP
604 if (listener_id) {
605 *listener_id = i;
606 }
3602afb0 607
7b33a0e0
PP
608 BT_LIB_LOGV("Added \"trace is static\" listener: "
609 "%![trace-]+t, listener-id=%" PRIu64, trace, i);
610 return 0;
611}
612
613BT_ASSERT_PRE_FUNC
614static
615bool has_listener_id(struct bt_trace *trace, uint64_t listener_id)
616{
617 BT_ASSERT(listener_id < trace->is_static_listeners->len);
618 return (&g_array_index(trace->is_static_listeners,
619 struct bt_trace_is_static_listener_elem,
620 listener_id))->func != NULL;
3602afb0
PP
621}
622
9e550e5f
PP
623int bt_private_trace_remove_is_static_listener(
624 struct bt_private_trace *priv_trace, uint64_t listener_id)
3602afb0 625{
9e550e5f 626 struct bt_trace *trace = (void *) priv_trace;
839d52a5 627 struct bt_trace_is_static_listener_elem *elem;
3602afb0 628
7b33a0e0
PP
629 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
630 BT_ASSERT_PRE(!trace->is_static,
631 "Trace is already static: %!+t", trace);
632 BT_ASSERT_PRE(trace->in_remove_listener,
633 "Cannot call this function while executing a "
634 "remove listener: %!+t", trace);
635 BT_ASSERT_PRE(has_listener_id(trace, listener_id),
636 "Trace has no such \"trace is static\" listener ID: "
637 "%![trace-]+t, %" PRIu64, trace, listener_id);
3602afb0 638 elem = &g_array_index(trace->is_static_listeners,
839d52a5 639 struct bt_trace_is_static_listener_elem,
3602afb0 640 listener_id);
7b33a0e0 641 BT_ASSERT(elem->func);
3602afb0 642
9962ad4b
PP
643 if (elem->removed) {
644 /* Call remove listener */
7b33a0e0
PP
645 BT_LIB_LOGV("Calling remove listener: "
646 "%![trace-]+t, listener-id=%" PRIu64,
647 trace, listener_id);
648 trace->in_remove_listener = true;
9e550e5f 649 elem->removed((void *) trace, elem->data);
7b33a0e0 650 trace->in_remove_listener = false;
9962ad4b
PP
651 }
652
3602afb0 653 elem->func = NULL;
9962ad4b 654 elem->removed = NULL;
3602afb0 655 elem->data = NULL;
7b33a0e0
PP
656 BT_LIB_LOGV("Removed \"trace is static\" listener: "
657 "%![trace-]+t, listener-id=%" PRIu64,
658 trace, listener_id);
659 return 0;
660}
3602afb0 661
7b33a0e0
PP
662BT_HIDDEN
663void _bt_trace_freeze(struct bt_trace *trace)
664{
939190b3 665 /* The packet header field classe is already frozen */
7b33a0e0
PP
666 BT_ASSERT(trace);
667 BT_LIB_LOGD("Freezing trace: %!+t", trace);
668 trace->frozen = true;
5acf2ae6 669}
a6918753 670
7b33a0e0 671bt_bool bt_trace_assigns_automatic_stream_class_id(struct bt_trace *trace)
a6918753 672{
7b33a0e0
PP
673 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
674 return (bt_bool) trace->assigns_automatic_stream_class_id;
675}
a6918753 676
c8bbf821 677void bt_private_trace_set_assigns_automatic_stream_class_id(
9e550e5f 678 struct bt_private_trace *priv_trace, bt_bool value)
7b33a0e0 679{
9e550e5f
PP
680 struct bt_trace *trace = (void *) priv_trace;
681
a6918753 682 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
7b33a0e0
PP
683 BT_ASSERT_PRE_TRACE_HOT(trace);
684 trace->assigns_automatic_stream_class_id = (bool) value;
685 BT_LIB_LOGV("Set trace's automatic stream class ID "
686 "assignment property: %!+t", trace);
7b33a0e0 687}
a6918753 688
7b33a0e0
PP
689BT_HIDDEN
690void bt_trace_add_stream(struct bt_trace *trace, struct bt_stream *stream)
691{
692 guint count = 0;
693
694 bt_object_set_parent(&stream->base, &trace->base);
695 g_ptr_array_add(trace->streams, stream);
18acc6f8 696 bt_trace_freeze(trace);
a6918753 697
7b33a0e0
PP
698 if (bt_g_hash_table_contains(trace->stream_classes_stream_count,
699 stream->class)) {
700 count = GPOINTER_TO_UINT(g_hash_table_lookup(
701 trace->stream_classes_stream_count, stream->class));
a6918753
PP
702 }
703
7b33a0e0
PP
704 g_hash_table_insert(trace->stream_classes_stream_count,
705 stream->class, GUINT_TO_POINTER(count + 1));
706}
707
708BT_HIDDEN
709uint64_t bt_trace_get_automatic_stream_id(struct bt_trace *trace,
710 struct bt_stream_class *stream_class)
711{
712 gpointer orig_key;
713 gpointer value;
714 uint64_t id = 0;
715
716 BT_ASSERT(stream_class);
717 BT_ASSERT(trace);
718 if (g_hash_table_lookup_extended(trace->stream_classes_stream_count,
719 stream_class, &orig_key, &value)) {
720 id = (uint64_t) GPOINTER_TO_UINT(value);
721 }
722
723 return id;
a6918753 724}
This page took 0.098228 seconds and 4 git commands to generate.