lib: update copyrights
[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/object.h>
49#include <babeltrace/types.h>
50#include <babeltrace/endian-internal.h>
51#include <babeltrace/assert-internal.h>
52#include <babeltrace/compat/glib-internal.h>
53#include <inttypes.h>
54#include <stdint.h>
55#include <string.h>
56#include <stdlib.h>
57
58#define BT_ASSERT_PRE_TRACE_CLASS_HOT(_tc) \
59 BT_ASSERT_PRE_HOT((_tc), "Trace class", ": %!+T", (_tc))
60
61static
62void destroy_trace_class(struct bt_object *obj)
63{
64 struct bt_trace_class *tc = (void *) obj;
65
66 BT_LIB_LOGD("Destroying trace class object: %!+T", tc);
67 bt_object_pool_finalize(&tc->packet_header_field_pool);
68
69 if (tc->environment) {
70 BT_LOGD_STR("Destroying environment attributes.");
71 bt_attributes_destroy(tc->environment);
72 tc->environment = NULL;
73 }
74
75 if (tc->name.str) {
76 g_string_free(tc->name.str, TRUE);
77 tc->name.str = NULL;
78 tc->name.value = NULL;
79 }
80
81 if (tc->stream_classes) {
82 BT_LOGD_STR("Destroying stream classes.");
83 g_ptr_array_free(tc->stream_classes, TRUE);
84 tc->stream_classes = NULL;
85 }
86
e6276565 87 BT_LOGD_STR("Putting packet header field class.");
862ca4ed
PP
88 bt_object_put_ref(tc->packet_header_fc);
89 tc->packet_header_fc = NULL;
90 g_free(tc);
91}
92
93static
94void free_packet_header_field(struct bt_field_wrapper *field_wrapper,
95 struct bt_trace_class *tc)
96{
97 bt_field_wrapper_destroy(field_wrapper);
98}
99
100struct bt_trace_class *bt_trace_class_create(void)
101{
102 struct bt_trace_class *tc = NULL;
103 int ret;
104
105 BT_LOGD_STR("Creating default trace class object.");
106 tc = g_new0(struct bt_trace_class, 1);
107 if (!tc) {
108 BT_LOGE_STR("Failed to allocate one trace class.");
109 goto error;
110 }
111
112 bt_object_init_shared_with_parent(&tc->base, destroy_trace_class);
113
114 tc->stream_classes = g_ptr_array_new_with_free_func(
115 (GDestroyNotify) bt_object_try_spec_release);
116 if (!tc->stream_classes) {
117 BT_LOGE_STR("Failed to allocate one GPtrArray.");
118 goto error;
119 }
120
121 tc->name.str = g_string_new(NULL);
122 if (!tc->name.str) {
123 BT_LOGE_STR("Failed to allocate one GString.");
124 goto error;
125 }
126
127 tc->environment = bt_attributes_create();
128 if (!tc->environment) {
129 BT_LOGE_STR("Cannot create empty attributes object.");
130 goto error;
131 }
132
133 tc->assigns_automatic_stream_class_id = true;
134 ret = bt_object_pool_initialize(&tc->packet_header_field_pool,
135 (bt_object_pool_new_object_func) bt_field_wrapper_new,
136 (bt_object_pool_destroy_object_func) free_packet_header_field,
137 tc);
138 if (ret) {
139 BT_LOGE("Failed to initialize packet header field pool: ret=%d",
140 ret);
141 goto error;
142 }
143
144 BT_LIB_LOGD("Created trace class object: %!+T", tc);
145 goto end;
146
147error:
148 BT_OBJECT_PUT_REF_AND_RESET(tc);
149
150end:
151 return tc;
152}
153
154const char *bt_trace_class_get_name(const struct bt_trace_class *tc)
155{
156 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
157 return tc->name.value;
158}
159
160int bt_trace_class_set_name(struct bt_trace_class *tc, const char *name)
161{
162 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
163 BT_ASSERT_PRE_NON_NULL(name, "Name");
164 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
165 g_string_assign(tc->name.str, name);
166 tc->name.value = tc->name.str->str;
167 BT_LIB_LOGV("Set trace class's name: %!+T", tc);
168 return 0;
169}
170
171bt_uuid bt_trace_class_get_uuid(const struct bt_trace_class *tc)
172{
173 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
174 return tc->uuid.value;
175}
176
177void bt_trace_class_set_uuid(struct bt_trace_class *tc, bt_uuid uuid)
178{
179 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
180 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
181 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
182 memcpy(tc->uuid.uuid, uuid, BABELTRACE_UUID_LEN);
183 tc->uuid.value = tc->uuid.uuid;
184 BT_LIB_LOGV("Set trace class's UUID: %!+T", tc);
185}
186
187BT_ASSERT_FUNC
188static
189bool trace_has_environment_entry(const struct bt_trace_class *tc, const char *name)
190{
191 BT_ASSERT(tc);
192
193 return bt_attributes_borrow_field_value_by_name(
194 tc->environment, name) != NULL;
195}
196
197static
198int set_environment_entry(struct bt_trace_class *tc, const char *name,
199 struct bt_value *value)
200{
201 int ret;
202
203 BT_ASSERT(tc);
204 BT_ASSERT(name);
205 BT_ASSERT(value);
206 BT_ASSERT_PRE(!tc->frozen ||
207 !trace_has_environment_entry(tc, name),
208 "Trace class is frozen: cannot replace environment entry: "
209 "%![tc-]+T, entry-name=\"%s\"", tc, name);
210 ret = bt_attributes_set_field_value(tc->environment, name,
211 value);
212 bt_value_freeze(value);
213 if (ret) {
214 BT_LIB_LOGE("Cannot set trace class's environment entry: "
215 "%![tc-]+T, entry-name=\"%s\"", tc, name);
216 } else {
217 BT_LIB_LOGV("Set trace class's environment entry: "
218 "%![tc-]+T, entry-name=\"%s\"", tc, name);
219 }
220
221 return ret;
222}
223
224int bt_trace_class_set_environment_entry_string(
225 struct bt_trace_class *tc, const char *name, const char *value)
226{
227 int ret;
228 struct bt_value *value_obj;
229 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
230 BT_ASSERT_PRE_NON_NULL(name, "Name");
231 BT_ASSERT_PRE_NON_NULL(value, "Value");
232 value_obj = bt_value_string_create_init(value);
233 if (!value_obj) {
234 BT_LOGE_STR("Cannot create a string value object.");
235 ret = -1;
236 goto end;
237 }
238
239 /* set_environment_entry() logs errors */
240 ret = set_environment_entry(tc, name, value_obj);
241
242end:
243 bt_object_put_ref(value_obj);
244 return ret;
245}
246
247int bt_trace_class_set_environment_entry_integer(struct bt_trace_class *tc,
248 const char *name, int64_t value)
249{
250 int ret;
251 struct bt_value *value_obj;
252 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
253 BT_ASSERT_PRE_NON_NULL(name, "Name");
254 value_obj = bt_value_integer_create_init(value);
255 if (!value_obj) {
256 BT_LOGE_STR("Cannot create an integer value object.");
257 ret = -1;
258 goto end;
259 }
260
261 /* set_environment_entry() logs errors */
262 ret = set_environment_entry(tc, name, value_obj);
263
264end:
265 bt_object_put_ref(value_obj);
266 return ret;
267}
268
269uint64_t bt_trace_class_get_environment_entry_count(const struct bt_trace_class *tc)
270{
271 int64_t ret;
272
273 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
274 ret = bt_attributes_get_count(tc->environment);
275 BT_ASSERT(ret >= 0);
276 return (uint64_t) ret;
277}
278
279void bt_trace_class_borrow_environment_entry_by_index_const(
280 const struct bt_trace_class *tc, uint64_t index,
281 const char **name, const struct bt_value **value)
282{
283 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
284 BT_ASSERT_PRE_NON_NULL(name, "Name");
285 BT_ASSERT_PRE_NON_NULL(value, "Value");
286 BT_ASSERT_PRE_VALID_INDEX(index,
287 bt_attributes_get_count(tc->environment));
288 *value = bt_attributes_borrow_field_value(tc->environment, index);
289 BT_ASSERT(*value);
290 *name = bt_attributes_get_field_name(tc->environment, index);
291 BT_ASSERT(*name);
292}
293
294const struct bt_value *bt_trace_class_borrow_environment_entry_value_by_name_const(
295 const struct bt_trace_class *tc, const char *name)
296{
297 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
298 BT_ASSERT_PRE_NON_NULL(name, "Name");
299 return bt_attributes_borrow_field_value_by_name(tc->environment,
300 name);
301}
302
303uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc)
304{
305 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
306 return (uint64_t) tc->stream_classes->len;
307}
308
309struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index(
310 struct bt_trace_class *tc, uint64_t index)
311{
312 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
313 BT_ASSERT_PRE_VALID_INDEX(index, tc->stream_classes->len);
314 return g_ptr_array_index(tc->stream_classes, index);
315}
316
317const struct bt_stream_class *
318bt_trace_class_borrow_stream_class_by_index_const(
319 const struct bt_trace_class *tc, uint64_t index)
320{
321 return bt_trace_class_borrow_stream_class_by_index(
322 (void *) tc, index);
323}
324
325struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id(
326 struct bt_trace_class *tc, uint64_t id)
327{
328 struct bt_stream_class *stream_class = NULL;
329 uint64_t i;
330
331 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
332
333 for (i = 0; i < tc->stream_classes->len; i++) {
334 struct bt_stream_class *stream_class_candidate =
335 g_ptr_array_index(tc->stream_classes, i);
336
337 if (stream_class_candidate->id == id) {
338 stream_class = stream_class_candidate;
339 goto end;
340 }
341 }
342
343end:
344 return stream_class;
345}
346
347const struct bt_stream_class *
348bt_trace_class_borrow_stream_class_by_id_const(
349 const struct bt_trace_class *tc, uint64_t id)
350{
351 return bt_trace_class_borrow_stream_class_by_id((void *) tc, id);
352}
353
354const struct bt_field_class *bt_trace_class_borrow_packet_header_field_class_const(
355 const struct bt_trace_class *tc)
356{
357 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
358 return tc->packet_header_fc;
359}
360
361int bt_trace_class_set_packet_header_field_class(
362 struct bt_trace_class *tc,
363 struct bt_field_class *field_class)
364{
365 int ret;
366 struct bt_resolve_field_path_context resolve_ctx = {
367 .packet_header = field_class,
368 .packet_context = NULL,
369 .event_header = NULL,
370 .event_common_context = NULL,
371 .event_specific_context = NULL,
372 .event_payload = NULL,
373 };
374
375 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
376 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
377 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
378 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
379 BT_FIELD_CLASS_TYPE_STRUCTURE,
e6276565 380 "Packet header field class is not a structure field class: %!+F",
862ca4ed
PP
381 field_class);
382 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
383 if (ret) {
384 goto end;
385 }
386
387 bt_field_class_make_part_of_trace_class(field_class);
388 bt_object_put_ref(tc->packet_header_fc);
389 tc->packet_header_fc = field_class;
390 bt_object_get_no_null_check(tc->packet_header_fc);
391 bt_field_class_freeze(field_class);
e6276565 392 BT_LIB_LOGV("Set trace class's packet header field class: %!+T", tc);
862ca4ed
PP
393
394end:
395 return ret;
396}
397
398BT_HIDDEN
399void _bt_trace_class_freeze(const struct bt_trace_class *tc)
400{
e6276565 401 /* The packet header field class is already frozen */
862ca4ed
PP
402 BT_ASSERT(tc);
403 BT_LIB_LOGD("Freezing trace class: %!+T", tc);
404 ((struct bt_trace_class *) tc)->frozen = true;
405}
406
407bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc)
408{
409 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
410 return (bt_bool) tc->assigns_automatic_stream_class_id;
411}
412
413void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc,
414 bt_bool value)
415{
416 BT_ASSERT_PRE_NON_NULL(tc, "Trace class");
417 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc);
418 tc->assigns_automatic_stream_class_id = (bool) value;
419 BT_LIB_LOGV("Set trace class's automatic stream class ID "
420 "assignment property: %!+T", tc);
421}
This page took 0.03857 seconds and 4 git commands to generate.