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