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