Commit | Line | Data |
---|---|---|
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 | ||
60 | static | |
61 | void 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 | ||
92 | static | |
93 | void 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 | ||
41693723 | 99 | struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp) |
862ca4ed PP |
100 | { |
101 | struct bt_trace_class *tc = NULL; | |
102 | int ret; | |
103 | ||
41693723 | 104 | BT_ASSERT_PRE_NON_NULL(self_comp, "Self component"); |
862ca4ed PP |
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 | ||
147 | error: | |
148 | BT_OBJECT_PUT_REF_AND_RESET(tc); | |
149 | ||
150 | end: | |
151 | return tc; | |
152 | } | |
153 | ||
154 | const 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 | ||
a1ed915c PP |
160 | enum bt_trace_class_status bt_trace_class_set_name( |
161 | struct bt_trace_class *tc, const char *name) | |
862ca4ed PP |
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); | |
a1ed915c | 169 | return BT_TRACE_CLASS_STATUS_OK; |
862ca4ed PP |
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 | |
a1ed915c PP |
199 | enum bt_trace_class_status set_environment_entry(struct bt_trace_class *tc, |
200 | const char *name, struct bt_value *value) | |
862ca4ed PP |
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); | |
862ca4ed | 213 | if (ret) { |
a1ed915c | 214 | ret = BT_TRACE_CLASS_STATUS_NOMEM; |
862ca4ed PP |
215 | BT_LIB_LOGE("Cannot set trace class's environment entry: " |
216 | "%![tc-]+T, entry-name=\"%s\"", tc, name); | |
217 | } else { | |
a1ed915c | 218 | bt_value_freeze(value); |
862ca4ed PP |
219 | BT_LIB_LOGV("Set trace class's environment entry: " |
220 | "%![tc-]+T, entry-name=\"%s\"", tc, name); | |
221 | } | |
222 | ||
223 | return ret; | |
224 | } | |
225 | ||
a1ed915c | 226 | enum bt_trace_class_status bt_trace_class_set_environment_entry_string( |
862ca4ed PP |
227 | struct bt_trace_class *tc, const char *name, const char *value) |
228 | { | |
229 | int ret; | |
230 | struct bt_value *value_obj; | |
231 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
232 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
233 | BT_ASSERT_PRE_NON_NULL(value, "Value"); | |
234 | value_obj = bt_value_string_create_init(value); | |
235 | if (!value_obj) { | |
236 | BT_LOGE_STR("Cannot create a string value object."); | |
237 | ret = -1; | |
238 | goto end; | |
239 | } | |
240 | ||
241 | /* set_environment_entry() logs errors */ | |
242 | ret = set_environment_entry(tc, name, value_obj); | |
243 | ||
244 | end: | |
245 | bt_object_put_ref(value_obj); | |
246 | return ret; | |
247 | } | |
248 | ||
a1ed915c PP |
249 | enum bt_trace_class_status bt_trace_class_set_environment_entry_integer( |
250 | struct bt_trace_class *tc, const char *name, int64_t value) | |
862ca4ed PP |
251 | { |
252 | int ret; | |
253 | struct bt_value *value_obj; | |
254 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
255 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
256 | value_obj = bt_value_integer_create_init(value); | |
257 | if (!value_obj) { | |
258 | BT_LOGE_STR("Cannot create an integer value object."); | |
a1ed915c | 259 | ret = BT_TRACE_CLASS_STATUS_NOMEM; |
862ca4ed PP |
260 | goto end; |
261 | } | |
262 | ||
263 | /* set_environment_entry() logs errors */ | |
264 | ret = set_environment_entry(tc, name, value_obj); | |
265 | ||
266 | end: | |
267 | bt_object_put_ref(value_obj); | |
268 | return ret; | |
269 | } | |
270 | ||
271 | uint64_t bt_trace_class_get_environment_entry_count(const struct bt_trace_class *tc) | |
272 | { | |
273 | int64_t ret; | |
274 | ||
275 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
276 | ret = bt_attributes_get_count(tc->environment); | |
277 | BT_ASSERT(ret >= 0); | |
278 | return (uint64_t) ret; | |
279 | } | |
280 | ||
281 | void bt_trace_class_borrow_environment_entry_by_index_const( | |
282 | const struct bt_trace_class *tc, uint64_t index, | |
283 | const char **name, const struct bt_value **value) | |
284 | { | |
285 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
286 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
287 | BT_ASSERT_PRE_NON_NULL(value, "Value"); | |
288 | BT_ASSERT_PRE_VALID_INDEX(index, | |
289 | bt_attributes_get_count(tc->environment)); | |
290 | *value = bt_attributes_borrow_field_value(tc->environment, index); | |
291 | BT_ASSERT(*value); | |
292 | *name = bt_attributes_get_field_name(tc->environment, index); | |
293 | BT_ASSERT(*name); | |
294 | } | |
295 | ||
296 | const struct bt_value *bt_trace_class_borrow_environment_entry_value_by_name_const( | |
297 | const struct bt_trace_class *tc, const char *name) | |
298 | { | |
299 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
300 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
301 | return bt_attributes_borrow_field_value_by_name(tc->environment, | |
302 | name); | |
303 | } | |
304 | ||
305 | uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc) | |
306 | { | |
307 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
308 | return (uint64_t) tc->stream_classes->len; | |
309 | } | |
310 | ||
311 | struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index( | |
312 | struct bt_trace_class *tc, uint64_t index) | |
313 | { | |
314 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
315 | BT_ASSERT_PRE_VALID_INDEX(index, tc->stream_classes->len); | |
316 | return g_ptr_array_index(tc->stream_classes, index); | |
317 | } | |
318 | ||
319 | const struct bt_stream_class * | |
320 | bt_trace_class_borrow_stream_class_by_index_const( | |
321 | const struct bt_trace_class *tc, uint64_t index) | |
322 | { | |
323 | return bt_trace_class_borrow_stream_class_by_index( | |
324 | (void *) tc, index); | |
325 | } | |
326 | ||
327 | struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id( | |
328 | struct bt_trace_class *tc, uint64_t id) | |
329 | { | |
330 | struct bt_stream_class *stream_class = NULL; | |
331 | uint64_t i; | |
332 | ||
333 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
334 | ||
335 | for (i = 0; i < tc->stream_classes->len; i++) { | |
336 | struct bt_stream_class *stream_class_candidate = | |
337 | g_ptr_array_index(tc->stream_classes, i); | |
338 | ||
339 | if (stream_class_candidate->id == id) { | |
340 | stream_class = stream_class_candidate; | |
341 | goto end; | |
342 | } | |
343 | } | |
344 | ||
345 | end: | |
346 | return stream_class; | |
347 | } | |
348 | ||
349 | const struct bt_stream_class * | |
350 | bt_trace_class_borrow_stream_class_by_id_const( | |
351 | const struct bt_trace_class *tc, uint64_t id) | |
352 | { | |
353 | return bt_trace_class_borrow_stream_class_by_id((void *) tc, id); | |
354 | } | |
355 | ||
356 | const struct bt_field_class *bt_trace_class_borrow_packet_header_field_class_const( | |
357 | const struct bt_trace_class *tc) | |
358 | { | |
359 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
360 | return tc->packet_header_fc; | |
361 | } | |
362 | ||
a1ed915c | 363 | enum bt_trace_class_status bt_trace_class_set_packet_header_field_class( |
862ca4ed PP |
364 | struct bt_trace_class *tc, |
365 | struct bt_field_class *field_class) | |
366 | { | |
367 | int ret; | |
368 | struct bt_resolve_field_path_context resolve_ctx = { | |
369 | .packet_header = field_class, | |
370 | .packet_context = NULL, | |
371 | .event_header = NULL, | |
372 | .event_common_context = NULL, | |
373 | .event_specific_context = NULL, | |
374 | .event_payload = NULL, | |
375 | }; | |
376 | ||
377 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
378 | BT_ASSERT_PRE_NON_NULL(field_class, "Field class"); | |
379 | BT_ASSERT_PRE_TRACE_CLASS_HOT(tc); | |
380 | BT_ASSERT_PRE(bt_field_class_get_type(field_class) == | |
381 | BT_FIELD_CLASS_TYPE_STRUCTURE, | |
e6276565 | 382 | "Packet header field class is not a structure field class: %!+F", |
862ca4ed PP |
383 | field_class); |
384 | ret = bt_resolve_field_paths(field_class, &resolve_ctx); | |
385 | if (ret) { | |
a1ed915c PP |
386 | /* |
387 | * This is the only reason for which | |
388 | * bt_resolve_field_paths() can fail: anything else | |
389 | * would be because a precondition is not satisfied. | |
390 | */ | |
391 | ret = BT_TRACE_CLASS_STATUS_NOMEM; | |
862ca4ed PP |
392 | goto end; |
393 | } | |
394 | ||
395 | bt_field_class_make_part_of_trace_class(field_class); | |
396 | bt_object_put_ref(tc->packet_header_fc); | |
397 | tc->packet_header_fc = field_class; | |
398 | bt_object_get_no_null_check(tc->packet_header_fc); | |
399 | bt_field_class_freeze(field_class); | |
e6276565 | 400 | BT_LIB_LOGV("Set trace class's packet header field class: %!+T", tc); |
862ca4ed PP |
401 | |
402 | end: | |
403 | return ret; | |
404 | } | |
405 | ||
406 | BT_HIDDEN | |
407 | void _bt_trace_class_freeze(const struct bt_trace_class *tc) | |
408 | { | |
e6276565 | 409 | /* The packet header field class is already frozen */ |
862ca4ed PP |
410 | BT_ASSERT(tc); |
411 | BT_LIB_LOGD("Freezing trace class: %!+T", tc); | |
412 | ((struct bt_trace_class *) tc)->frozen = true; | |
413 | } | |
414 | ||
415 | bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc) | |
416 | { | |
417 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
418 | return (bt_bool) tc->assigns_automatic_stream_class_id; | |
419 | } | |
420 | ||
421 | void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc, | |
422 | bt_bool value) | |
423 | { | |
424 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
425 | BT_ASSERT_PRE_TRACE_CLASS_HOT(tc); | |
426 | tc->assigns_automatic_stream_class_id = (bool) value; | |
427 | BT_LIB_LOGV("Set trace class's automatic stream class ID " | |
428 | "assignment property: %!+T", tc); | |
429 | } | |
c5b9b441 PP |
430 | |
431 | void bt_trace_class_get_ref(const struct bt_trace_class *trace_class) | |
432 | { | |
433 | bt_object_get_ref(trace_class); | |
434 | } | |
435 | ||
436 | void bt_trace_class_put_ref(const struct bt_trace_class *trace_class) | |
437 | { | |
438 | bt_object_put_ref(trace_class); | |
439 | } |