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
3dca2276 28#include <babeltrace/assert-pre-internal.h>
e5be10ef 29#include <babeltrace/trace-ir/private-trace.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>
da91b29a 46#include <babeltrace/private-values.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 {
e5be10ef
PP
59 bt_private_trace_is_static_listener func;
60 bt_private_trace_listener_removed 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);
bc37ae52
JG
92 }
93
312c056a 94 bt_object_pool_finalize(&trace->packet_header_field_pool);
3dca2276 95
44c440bc
PP
96 if (trace->environment) {
97 BT_LOGD_STR("Destroying environment attributes.");
98 bt_attributes_destroy(trace->environment);
95c09b3a
PP
99 }
100
44c440bc
PP
101 if (trace->name.str) {
102 g_string_free(trace->name.str, TRUE);
95c09b3a
PP
103 }
104
44c440bc
PP
105 if (trace->streams) {
106 BT_LOGD_STR("Destroying streams.");
107 g_ptr_array_free(trace->streams, TRUE);
bc37ae52
JG
108 }
109
44c440bc
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
44c440bc
PP
115 if (trace->stream_classes_stream_count) {
116 g_hash_table_destroy(trace->stream_classes_stream_count);
117 }
3dca2276 118
5cd6d0e5 119 BT_LOGD_STR("Putting packet header field classe.");
65300d60 120 bt_object_put_ref(trace->packet_header_fc);
44c440bc 121 g_free(trace);
3dca2276
PP
122}
123
312c056a
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
e5be10ef 131struct bt_private_trace *bt_private_trace_create(void)
3dca2276
PP
132{
133 struct bt_trace *trace = NULL;
134 int ret;
135
44c440bc 136 BT_LOGD_STR("Creating default trace object.");
3dca2276
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
44c440bc
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.");
3dca2276
PP
148 goto error;
149 }
150
44c440bc
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
44c440bc
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,
50842bdc 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
44c440bc 184 trace->assigns_automatic_stream_class_id = true;
312c056a
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
44c440bc
PP
195 BT_LIB_LOGD("Created trace object: %!+t", trace);
196 goto end;
bc37ae52 197
bc37ae52 198error:
65300d60 199 BT_OBJECT_PUT_REF_AND_RESET(trace);
44c440bc
PP
200
201end:
e5be10ef 202 return (void *) trace;
bc37ae52
JG
203}
204
50842bdc 205const char *bt_trace_get_name(struct bt_trace *trace)
e96045d4 206{
cb6f1f7d 207 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 208 return trace->name.value;
e96045d4
JG
209}
210
e5be10ef
PP
211int bt_private_trace_set_name(struct bt_private_trace *priv_trace,
212 const char *name)
e96045d4 213{
e5be10ef
PP
214 struct bt_trace *trace = (void *) priv_trace;
215
44c440bc
PP
216 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
217 BT_ASSERT_PRE_NON_NULL(name, "Name");
218 BT_ASSERT_PRE_TRACE_HOT(trace);
219 g_string_assign(trace->name.str, name);
220 trace->name.value = trace->name.str->str;
221 BT_LIB_LOGV("Set trace's name: %!+t", trace);
222 return 0;
e96045d4
JG
223}
224
44c440bc 225bt_uuid bt_trace_get_uuid(struct bt_trace *trace)
4a32fda0 226{
cb6f1f7d 227 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 228 return trace->uuid.value;
4a32fda0
PP
229}
230
140e6d94
PP
231void bt_private_trace_set_uuid(struct bt_private_trace *priv_trace,
232 bt_uuid uuid)
4a32fda0 233{
e5be10ef
PP
234 struct bt_trace *trace = (void *) priv_trace;
235
44c440bc
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
44c440bc
PP
244BT_ASSERT_FUNC
245static
246bool trace_has_environment_entry(struct bt_trace *trace, const char *name)
bc37ae52 247{
44c440bc 248 BT_ASSERT(trace);
95c09b3a 249
da91b29a
PP
250 return bt_attributes_borrow_field_value_by_name(
251 trace->environment, name) != NULL;
44c440bc 252}
a0d12916 253
44c440bc
PP
254static
255int set_environment_entry(struct bt_trace *trace, const char *name,
da91b29a 256 struct bt_private_value *value)
44c440bc
PP
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);
d94d92ac 269 bt_value_freeze(bt_private_value_borrow_value(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
28e6ca8b 281int bt_private_trace_set_environment_entry_string(
e5be10ef 282 struct bt_private_trace *priv_trace,
7f800dc7
PP
283 const char *name, const char *value)
284{
44c440bc 285 int ret;
da91b29a 286 struct bt_private_value *value_obj;
e5be10ef 287 struct bt_trace *trace = (void *) priv_trace;
bc37ae52 288
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");
da91b29a 292 value_obj = bt_private_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
28e6ca8b 307int bt_private_trace_set_environment_entry_integer(
e5be10ef
PP
308 struct bt_private_trace *priv_trace,
309 const char *name, int64_t value)
3487c9f3 310{
44c440bc 311 int ret;
da91b29a 312 struct bt_private_value *value_obj;
e5be10ef 313 struct bt_trace *trace = (void *) priv_trace;
3487c9f3 314
44c440bc
PP
315 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
316 BT_ASSERT_PRE_NON_NULL(name, "Name");
da91b29a 317 value_obj = bt_private_value_integer_create_init(value);
44c440bc
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
44c440bc
PP
324 /* set_environment_entry() logs errors */
325 ret = set_environment_entry(trace, name, value_obj);
95c09b3a 326
7f800dc7 327end:
65300d60 328 bt_object_put_ref(value_obj);
bc37ae52
JG
329 return ret;
330}
331
44c440bc 332uint64_t bt_trace_get_environment_entry_count(struct bt_trace *trace)
3dca2276 333{
cb6f1f7d
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);
44c440bc 339 return (uint64_t) ret;
e6fa2160
JG
340}
341
44c440bc
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{
cb6f1f7d 346 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc
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));
d94d92ac 351 *value = bt_private_value_borrow_value(
da91b29a 352 bt_attributes_borrow_field_value(trace->environment, index));
44c440bc
PP
353 BT_ASSERT(*value);
354 *name = bt_attributes_get_field_name(trace->environment, index);
355 BT_ASSERT(*name);
e6fa2160
JG
356}
357
28e6ca8b 358void bt_private_trace_borrow_environment_entry_by_index(
e5be10ef
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
44c440bc 366struct bt_value *bt_trace_borrow_environment_entry_value_by_name(
50842bdc 367 struct bt_trace *trace, const char *name)
e6fa2160 368{
cb6f1f7d
PP
369 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
370 BT_ASSERT_PRE_NON_NULL(name, "Name");
d94d92ac 371 return bt_private_value_borrow_value(
da91b29a
PP
372 bt_attributes_borrow_field_value_by_name(trace->environment,
373 name));
e6fa2160
JG
374}
375
e5be10ef 376struct bt_private_value *
28e6ca8b 377bt_private_trace_borrow_environment_entry_value_by_name(
e5be10ef
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
44c440bc 384uint64_t bt_trace_get_stream_count(struct bt_trace *trace)
bc37ae52 385{
44c440bc
PP
386 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
387 return (uint64_t) trace->streams->len;
388}
95c09b3a 389
44c440bc
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}
cb6f1f7d 397
28e6ca8b 398struct bt_private_stream *bt_private_trace_borrow_stream_by_index(
e5be10ef
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
44c440bc
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
44c440bc 410 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
bc37ae52 411
44c440bc
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
44c440bc
PP
416 if (stream_candidate->id == id) {
417 stream = stream_candidate;
418 goto end;
419 }
6474e016 420 }
95c09b3a 421
bc37ae52 422end:
44c440bc 423 return stream;
bc37ae52
JG
424}
425
e5be10ef
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
44c440bc 432uint64_t bt_trace_get_stream_class_count(struct bt_trace *trace)
884cd6c3 433{
cb6f1f7d 434 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 435 return (uint64_t) trace->stream_classes->len;
884cd6c3
JG
436}
437
44c440bc 438struct bt_stream_class *bt_trace_borrow_stream_class_by_index(
50842bdc 439 struct bt_trace *trace, uint64_t index)
884cd6c3 440{
cb6f1f7d 441 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc
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
e5be10ef 446struct bt_private_stream_class *
28e6ca8b 447bt_private_trace_borrow_stream_class_by_index(
e5be10ef
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
44c440bc
PP
454struct bt_stream_class *bt_trace_borrow_stream_class_by_id(
455 struct bt_trace *trace, uint64_t id)
e011d2c1 456{
44c440bc
PP
457 struct bt_stream_class *stream_class = NULL;
458 uint64_t i;
e011d2c1 459
44c440bc 460 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
e011d2c1 461
44c440bc
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
44c440bc
PP
466 if (stream_class_candidate->id == id) {
467 stream_class = stream_class_candidate;
468 goto end;
e011d2c1 469 }
44c440bc 470 }
e011d2c1 471
44c440bc
PP
472end:
473 return stream_class;
474}
e011d2c1 475
e5be10ef 476struct bt_private_stream_class *
28e6ca8b 477bt_private_trace_borrow_stream_class_by_id(
e5be10ef
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
5cd6d0e5 483struct bt_field_class *bt_trace_borrow_packet_header_field_class(
44c440bc
PP
484 struct bt_trace *trace)
485{
486 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
5cd6d0e5 487 return trace->packet_header_fc;
44c440bc 488}
e011d2c1 489
28e6ca8b 490int bt_private_trace_set_packet_header_field_class(
e5be10ef
PP
491 struct bt_private_trace *priv_trace,
492 struct bt_private_field_class *priv_field_class)
44c440bc
PP
493{
494 int ret;
e5be10ef
PP
495 struct bt_trace *trace = (void *) priv_trace;
496 struct bt_field_class *field_class = (void *) priv_field_class;
44c440bc 497 struct bt_resolve_field_path_context resolve_ctx = {
5cd6d0e5 498 .packet_header = field_class,
44c440bc
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
44c440bc 506 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
5cd6d0e5 507 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
44c440bc 508 BT_ASSERT_PRE_TRACE_HOT(trace);
864cad70
PP
509 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
510 BT_FIELD_CLASS_TYPE_STRUCTURE,
5cd6d0e5
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);
44c440bc
PP
514 if (ret) {
515 goto end;
e011d2c1
PP
516 }
517
5cd6d0e5 518 bt_field_class_make_part_of_trace(field_class);
65300d60
PP
519 bt_object_put_ref(trace->packet_header_fc);
520 trace->packet_header_fc = bt_object_get_ref(field_class);
5cd6d0e5
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
50842bdc 528bt_bool bt_trace_is_static(struct bt_trace *trace)
5acf2ae6 529{
d975f66c 530 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc 531 return (bt_bool) trace->is_static;
5acf2ae6
PP
532}
533
e5be10ef 534int bt_private_trace_make_static(struct bt_private_trace *priv_trace)
5acf2ae6 535{
e5be10ef 536 struct bt_trace *trace = (void *) priv_trace;
44c440bc 537 uint64_t i;
726106d3 538
44c440bc
PP
539 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
540 trace->is_static = true;
cb6f1f7d 541 bt_trace_freeze(trace);
44c440bc 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++) {
50842bdc 546 struct bt_trace_is_static_listener_elem elem =
3602afb0 547 g_array_index(trace->is_static_listeners,
50842bdc 548 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
549
550 if (elem.func) {
e5be10ef 551 elem.func((void *) trace, elem.data);
3602afb0
PP
552 }
553 }
554
44c440bc 555 return 0;
3602afb0
PP
556}
557
e5be10ef
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,
44c440bc 562 uint64_t *listener_id)
3602afb0 563{
e5be10ef 564 struct bt_trace *trace = (void *) priv_trace;
44c440bc 565 uint64_t i;
50842bdc 566 struct bt_trace_is_static_listener_elem new_elem = {
3602afb0 567 .func = listener,
8480c8cc 568 .removed = listener_removed,
3602afb0
PP
569 .data = data,
570 };
571
44c440bc
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);
8480c8cc 579
3602afb0
PP
580 /* Find the next available spot */
581 for (i = 0; i < trace->is_static_listeners->len; i++) {
50842bdc 582 struct bt_trace_is_static_listener_elem elem =
3602afb0 583 g_array_index(trace->is_static_listeners,
50842bdc 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
44c440bc
PP
597 if (listener_id) {
598 *listener_id = i;
599 }
3602afb0 600
44c440bc
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
e5be10ef
PP
616int bt_private_trace_remove_is_static_listener(
617 struct bt_private_trace *priv_trace, uint64_t listener_id)
3602afb0 618{
e5be10ef 619 struct bt_trace *trace = (void *) priv_trace;
50842bdc 620 struct bt_trace_is_static_listener_elem *elem;
3602afb0 621
44c440bc
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,
50842bdc 632 struct bt_trace_is_static_listener_elem,
3602afb0 633 listener_id);
44c440bc 634 BT_ASSERT(elem->func);
3602afb0 635
8480c8cc
PP
636 if (elem->removed) {
637 /* Call remove listener */
44c440bc
PP
638 BT_LIB_LOGV("Calling remove listener: "
639 "%![trace-]+t, listener-id=%" PRIu64,
640 trace, listener_id);
641 trace->in_remove_listener = true;
e5be10ef 642 elem->removed((void *) trace, elem->data);
44c440bc 643 trace->in_remove_listener = false;
8480c8cc
PP
644 }
645
3602afb0 646 elem->func = NULL;
8480c8cc 647 elem->removed = NULL;
3602afb0 648 elem->data = NULL;
44c440bc
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
44c440bc
PP
655BT_HIDDEN
656void _bt_trace_freeze(struct bt_trace *trace)
657{
5cd6d0e5 658 /* The packet header field classe is already frozen */
44c440bc
PP
659 BT_ASSERT(trace);
660 BT_LIB_LOGD("Freezing trace: %!+t", trace);
661 trace->frozen = true;
5acf2ae6 662}
312c056a 663
44c440bc 664bt_bool bt_trace_assigns_automatic_stream_class_id(struct bt_trace *trace)
312c056a 665{
44c440bc
PP
666 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
667 return (bt_bool) trace->assigns_automatic_stream_class_id;
668}
312c056a 669
140e6d94 670void bt_private_trace_set_assigns_automatic_stream_class_id(
e5be10ef 671 struct bt_private_trace *priv_trace, bt_bool value)
44c440bc 672{
e5be10ef
PP
673 struct bt_trace *trace = (void *) priv_trace;
674
312c056a 675 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
44c440bc
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);
44c440bc 680}
312c056a 681
44c440bc
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);
cb6f1f7d 689 bt_trace_freeze(trace);
312c056a 690
44c440bc
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));
312c056a
PP
695 }
696
44c440bc
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;
312c056a 717}
This page took 0.094082 seconds and 4 git commands to generate.