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