lib: add unsigned and signed integer value API
[babeltrace.git] / lib / trace-ir / trace-class.c
CommitLineData
862ca4ed 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
862ca4ed
PP
3 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
862ca4ed
PP
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#define BT_LOG_TAG "TRACE"
25#include <babeltrace/lib-logging-internal.h>
26
27#include <babeltrace/assert-pre-internal.h>
28#include <babeltrace/trace-ir/trace-class.h>
29#include <babeltrace/trace-ir/trace-class-const.h>
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>
37#include <babeltrace/ctf-writer/functor-internal.h>
38#include <babeltrace/ctf-writer/clock-internal.h>
39#include <babeltrace/trace-ir/field-wrapper-internal.h>
c6bd8523 40#include <babeltrace/trace-ir/field-class-internal.h>
862ca4ed
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>
44#include <babeltrace/compiler-internal.h>
c6bd8523
PP
45#include <babeltrace/value.h>
46#include <babeltrace/value-const.h>
47#include <babeltrace/value-internal.h>
862ca4ed
PP
48#include <babeltrace/types.h>
49#include <babeltrace/endian-internal.h>
50#include <babeltrace/assert-internal.h>
51#include <babeltrace/compat/glib-internal.h>
52#include <inttypes.h>
53#include <stdint.h>
54#include <string.h>
55#include <stdlib.h>
56
ad5268b5
FD
57struct bt_trace_class_destruction_listener_elem {
58 bt_trace_class_destruction_listener_func func;
59 void *data;
60};
61
862ca4ed
PP
62#define BT_ASSERT_PRE_TRACE_CLASS_HOT(_tc) \
63 BT_ASSERT_PRE_HOT((_tc), "Trace class", ": %!+T", (_tc))
64
65static
66void destroy_trace_class(struct bt_object *obj)
67{
68 struct bt_trace_class *tc = (void *) obj;
69
70 BT_LIB_LOGD("Destroying trace class object: %!+T", tc);
ad5268b5
FD
71 /*
72 * Call destruction listener functions so that everything else
73 * still exists in the trace class.
74 */
75 if (tc->destruction_listeners) {
76 uint64_t i;
77 BT_LIB_LOGV("Calling trace class destruction listener(s): %!+T", tc);
78 /* Call all the trace class destruction listeners */
79 for (i = 0; i < tc->destruction_listeners->len; i++) {
80 struct bt_trace_class_destruction_listener_elem elem =
81 g_array_index(tc->destruction_listeners,
82 struct bt_trace_class_destruction_listener_elem, i);
83
84 if (elem.func) {
85 elem.func(tc, elem.data);
86 }
87 }
88 g_array_free(tc->destruction_listeners, TRUE);
89 tc->destruction_listeners = NULL;
90 }
862ca4ed
PP
91
92 if (tc->environment) {
93 BT_LOGD_STR("Destroying environment attributes.");
94 bt_attributes_destroy(tc->environment);
95 tc->environment = NULL;
96 }
97
98 if (tc->name.str) {
99 g_string_free(tc->name.str, TRUE);
100 tc->name.str = NULL;
101 tc->name.value = NULL;
102 }
103
104 if (tc->stream_classes) {
105 BT_LOGD_STR("Destroying stream classes.");
106 g_ptr_array_free(tc->stream_classes, TRUE);
107 tc->stream_classes = NULL;
108 }
109
862ca4ed
PP
110 g_free(tc);
111}
112
41693723 113struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp)
862ca4ed
PP
114{
115 struct bt_trace_class *tc = NULL;
862ca4ed 116
41693723 117 BT_ASSERT_PRE_NON_NULL(self_comp, "Self component");
862ca4ed
PP
118 BT_LOGD_STR("Creating default trace class object.");
119 tc = g_new0(struct bt_trace_class, 1);
120 if (!tc) {
121 BT_LOGE_STR("Failed to allocate one trace class.");
122 goto error;
123 }
124
125 bt_object_init_shared_with_parent(&tc->base, destroy_trace_class);
126
127 tc->stream_classes = g_ptr_array_new_with_free_func(
128 (GDestroyNotify) bt_object_try_spec_release);
129 if (!tc->stream_classes) {
130 BT_LOGE_STR("Failed to allocate one GPtrArray.");
131 goto error;
132 }
133
134 tc->name.str = g_string_new(NULL);
135 if (!tc->name.str) {
136 BT_LOGE_STR("Failed to allocate one GString.");
137 goto error;
138 }
139
140 tc->environment = bt_attributes_create();
141 if (!tc->environment) {
142 BT_LOGE_STR("Cannot create empty attributes object.");
143 goto error;
144 }
145
ad5268b5
FD
146 tc->destruction_listeners = g_array_new(FALSE, TRUE,
147 sizeof(struct bt_trace_class_destruction_listener_elem));
148 if (!tc->destruction_listeners) {
149 BT_LOGE_STR("Failed to allocate one GArray.");
150 goto error;
151 }
152
862ca4ed 153 tc->assigns_automatic_stream_class_id = true;
862ca4ed
PP
154 BT_LIB_LOGD("Created trace class object: %!+T", tc);
155 goto end;
156
157error:
158 BT_OBJECT_PUT_REF_AND_RESET(tc);
159
160end:
161 return tc;
162}
163
164const char *bt_trace_class_get_name(const struct bt_trace_class *tc)
165{
166 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
167 return tc->name.value;
168}
169
a1ed915c
PP
170enum bt_trace_class_status bt_trace_class_set_name(
171 struct bt_trace_class *tc, const char *name)
862ca4ed
PP
172{
173 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
174 BT_ASSERT_PRE_NON_NULL(name, "Name");
175 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
176 g_string_assign(tc->name.str, name);
177 tc->name.value = tc->name.str->str;
178 BT_LIB_LOGV("Set trace class's name: %!+T", tc);
a1ed915c 179 return BT_TRACE_CLASS_STATUS_OK;
862ca4ed
PP
180}
181
182bt_uuid bt_trace_class_get_uuid(const struct bt_trace_class *tc)
183{
184 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
185 return tc->uuid.value;
186}
187
188void bt_trace_class_set_uuid(struct bt_trace_class *tc, bt_uuid uuid)
189{
190 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
191 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
192 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
193 memcpy(tc->uuid.uuid, uuid, BABELTRACE_UUID_LEN);
194 tc->uuid.value = tc->uuid.uuid;
195 BT_LIB_LOGV("Set trace class's UUID: %!+T", tc);
196}
197
ad5268b5
FD
198enum bt_trace_class_status bt_trace_class_add_destruction_listener(
199 const struct bt_trace_class *_tc,
200 bt_trace_class_destruction_listener_func listener,
201 void *data, uint64_t *listener_id)
202{
203 struct bt_trace_class *tc = (void *) _tc;
204 uint64_t i;
205 struct bt_trace_class_destruction_listener_elem new_elem = {
206 .func = listener,
207 .data = data,
208 };
209
210 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
211 BT_ASSERT_PRE_NON_NULL(listener, "Listener");
212
213 /* Find the next available spot */
214 for (i = 0; i < tc->destruction_listeners->len; i++) {
215 struct bt_trace_class_destruction_listener_elem elem =
216 g_array_index(tc->destruction_listeners,
217 struct bt_trace_class_destruction_listener_elem, i);
218
219 if (!elem.func) {
220 break;
221 }
222 }
223
224 if (i == tc->destruction_listeners->len) {
225 g_array_append_val(tc->destruction_listeners, new_elem);
226 } else {
227 g_array_insert_val(tc->destruction_listeners, i, new_elem);
228 }
229
230 if (listener_id) {
231 *listener_id = i;
232 }
233
234 BT_LIB_LOGV("Added trace class destruction listener: %![tc-]+T, "
235 "listener-id=%" PRIu64, tc, i);
236 return BT_TRACE_CLASS_STATUS_OK;
237}
238
239BT_ASSERT_PRE_FUNC
240static
241bool has_listener_id(const struct bt_trace_class *tc, uint64_t listener_id)
242{
243 BT_ASSERT(listener_id < tc->destruction_listeners->len);
244 return (&g_array_index(tc->destruction_listeners,
245 struct bt_trace_class_destruction_listener_elem,
246 listener_id))->func != NULL;
247}
248
249enum bt_trace_class_status bt_trace_class_remove_destruction_listener(
250 const struct bt_trace_class *_tc, uint64_t listener_id)
251{
252 struct bt_trace_class *tc = (void *) _tc;
253 struct bt_trace_class_destruction_listener_elem *elem;
254
255 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
256 BT_ASSERT_PRE(has_listener_id(tc, listener_id),
257 "Trace class has no such trace class destruction listener ID: "
258 "%![tc-]+T, %" PRIu64, tc, listener_id);
259 elem = &g_array_index(tc->destruction_listeners,
260 struct bt_trace_class_destruction_listener_elem,
261 listener_id);
262 BT_ASSERT(elem->func);
263
264 elem->func = NULL;
265 elem->data = NULL;
266 BT_LIB_LOGV("Removed trace class destruction listener: "
267 "%![tc-]+T, listener-id=%" PRIu64,
268 tc, listener_id);
269 return BT_TRACE_CLASS_STATUS_OK;
270}
271
862ca4ed
PP
272BT_ASSERT_FUNC
273static
274bool trace_has_environment_entry(const struct bt_trace_class *tc, const char *name)
275{
276 BT_ASSERT(tc);
277
278 return bt_attributes_borrow_field_value_by_name(
279 tc->environment, name) != NULL;
280}
281
282static
a1ed915c
PP
283enum bt_trace_class_status set_environment_entry(struct bt_trace_class *tc,
284 const char *name, struct bt_value *value)
862ca4ed
PP
285{
286 int ret;
287
288 BT_ASSERT(tc);
289 BT_ASSERT(name);
290 BT_ASSERT(value);
291 BT_ASSERT_PRE(!tc->frozen ||
292 !trace_has_environment_entry(tc, name),
293 "Trace class is frozen: cannot replace environment entry: "
294 "%![tc-]+T, entry-name=\"%s\"", tc, name);
295 ret = bt_attributes_set_field_value(tc->environment, name,
296 value);
862ca4ed 297 if (ret) {
a1ed915c 298 ret = BT_TRACE_CLASS_STATUS_NOMEM;
862ca4ed
PP
299 BT_LIB_LOGE("Cannot set trace class's environment entry: "
300 "%![tc-]+T, entry-name=\"%s\"", tc, name);
301 } else {
a1ed915c 302 bt_value_freeze(value);
862ca4ed
PP
303 BT_LIB_LOGV("Set trace class's environment entry: "
304 "%![tc-]+T, entry-name=\"%s\"", tc, name);
305 }
306
307 return ret;
308}
309
a1ed915c 310enum bt_trace_class_status bt_trace_class_set_environment_entry_string(
862ca4ed
PP
311 struct bt_trace_class *tc, const char *name, const char *value)
312{
313 int ret;
314 struct bt_value *value_obj;
315 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
316 BT_ASSERT_PRE_NON_NULL(name, "Name");
317 BT_ASSERT_PRE_NON_NULL(value, "Value");
318 value_obj = bt_value_string_create_init(value);
319 if (!value_obj) {
320 BT_LOGE_STR("Cannot create a string value object.");
321 ret = -1;
322 goto end;
323 }
324
325 /* set_environment_entry() logs errors */
326 ret = set_environment_entry(tc, name, value_obj);
327
328end:
329 bt_object_put_ref(value_obj);
330 return ret;
331}
332
a1ed915c
PP
333enum bt_trace_class_status bt_trace_class_set_environment_entry_integer(
334 struct bt_trace_class *tc, const char *name, int64_t value)
862ca4ed
PP
335{
336 int ret;
337 struct bt_value *value_obj;
338 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
339 BT_ASSERT_PRE_NON_NULL(name, "Name");
fdd3a2da 340 value_obj = bt_value_signed_integer_create_init(value);
862ca4ed
PP
341 if (!value_obj) {
342 BT_LOGE_STR("Cannot create an integer value object.");
a1ed915c 343 ret = BT_TRACE_CLASS_STATUS_NOMEM;
862ca4ed
PP
344 goto end;
345 }
346
347 /* set_environment_entry() logs errors */
348 ret = set_environment_entry(tc, name, value_obj);
349
350end:
351 bt_object_put_ref(value_obj);
352 return ret;
353}
354
355uint64_t bt_trace_class_get_environment_entry_count(const struct bt_trace_class *tc)
356{
357 int64_t ret;
358
359 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
360 ret = bt_attributes_get_count(tc->environment);
361 BT_ASSERT(ret >= 0);
362 return (uint64_t) ret;
363}
364
365void bt_trace_class_borrow_environment_entry_by_index_const(
366 const struct bt_trace_class *tc, uint64_t index,
367 const char **name, const struct bt_value **value)
368{
369 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
370 BT_ASSERT_PRE_NON_NULL(name, "Name");
371 BT_ASSERT_PRE_NON_NULL(value, "Value");
372 BT_ASSERT_PRE_VALID_INDEX(index,
373 bt_attributes_get_count(tc->environment));
374 *value = bt_attributes_borrow_field_value(tc->environment, index);
375 BT_ASSERT(*value);
376 *name = bt_attributes_get_field_name(tc->environment, index);
377 BT_ASSERT(*name);
378}
379
380const struct bt_value *bt_trace_class_borrow_environment_entry_value_by_name_const(
381 const struct bt_trace_class *tc, const char *name)
382{
383 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
384 BT_ASSERT_PRE_NON_NULL(name, "Name");
385 return bt_attributes_borrow_field_value_by_name(tc->environment,
386 name);
387}
388
389uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc)
390{
391 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
392 return (uint64_t) tc->stream_classes->len;
393}
394
395struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index(
396 struct bt_trace_class *tc, uint64_t index)
397{
398 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
399 BT_ASSERT_PRE_VALID_INDEX(index, tc->stream_classes->len);
400 return g_ptr_array_index(tc->stream_classes, index);
401}
402
403const struct bt_stream_class *
404bt_trace_class_borrow_stream_class_by_index_const(
405 const struct bt_trace_class *tc, uint64_t index)
406{
407 return bt_trace_class_borrow_stream_class_by_index(
408 (void *) tc, index);
409}
410
411struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id(
412 struct bt_trace_class *tc, uint64_t id)
413{
414 struct bt_stream_class *stream_class = NULL;
415 uint64_t i;
416
417 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
418
419 for (i = 0; i < tc->stream_classes->len; i++) {
420 struct bt_stream_class *stream_class_candidate =
421 g_ptr_array_index(tc->stream_classes, i);
422
423 if (stream_class_candidate->id == id) {
424 stream_class = stream_class_candidate;
425 goto end;
426 }
427 }
428
429end:
430 return stream_class;
431}
432
433const struct bt_stream_class *
434bt_trace_class_borrow_stream_class_by_id_const(
435 const struct bt_trace_class *tc, uint64_t id)
436{
437 return bt_trace_class_borrow_stream_class_by_id((void *) tc, id);
438}
439
862ca4ed
PP
440BT_HIDDEN
441void _bt_trace_class_freeze(const struct bt_trace_class *tc)
442{
862ca4ed
PP
443 BT_ASSERT(tc);
444 BT_LIB_LOGD("Freezing trace class: %!+T", tc);
445 ((struct bt_trace_class *) tc)->frozen = true;
446}
447
448bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc)
449{
450 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
451 return (bt_bool) tc->assigns_automatic_stream_class_id;
452}
453
454void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc,
455 bt_bool value)
456{
457 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
458 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
459 tc->assigns_automatic_stream_class_id = (bool) value;
460 BT_LIB_LOGV("Set trace class's automatic stream class ID "
461 "assignment property: %!+T", tc);
462}
c5b9b441
PP
463
464void bt_trace_class_get_ref(const struct bt_trace_class *trace_class)
465{
466 bt_object_get_ref(trace_class);
467}
468
469void bt_trace_class_put_ref(const struct bt_trace_class *trace_class)
470{
471 bt_object_put_ref(trace_class);
472}
This page took 0.043594 seconds and 4 git commands to generate.