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" | |
578e048b | 25 | #include "lib/lib-logging.h" |
862ca4ed | 26 | |
578e048b | 27 | #include "lib/assert-pre.h" |
3fadfbc0 MJ |
28 | #include <babeltrace2/trace-ir/trace-class.h> |
29 | #include <babeltrace2/trace-ir/trace-class-const.h> | |
3fadfbc0 | 30 | #include <babeltrace2/trace-ir/event-class.h> |
578e048b MJ |
31 | #include "ctf-writer/functor.h" |
32 | #include "ctf-writer/clock.h" | |
33 | #include "compat/compiler.h" | |
3fadfbc0 MJ |
34 | #include <babeltrace2/value.h> |
35 | #include <babeltrace2/value-const.h> | |
578e048b | 36 | #include "lib/value.h" |
3fadfbc0 | 37 | #include <babeltrace2/types.h> |
578e048b MJ |
38 | #include "compat/endian.h" |
39 | #include "common/assert.h" | |
40 | #include "compat/glib.h" | |
862ca4ed PP |
41 | #include <inttypes.h> |
42 | #include <stdint.h> | |
43 | #include <string.h> | |
44 | #include <stdlib.h> | |
45 | ||
578e048b MJ |
46 | #include "attributes.h" |
47 | #include "clock-class.h" | |
48 | #include "event-class.h" | |
49 | #include "event.h" | |
50 | #include "field-class.h" | |
51 | #include "field-wrapper.h" | |
52 | #include "resolve-field-path.h" | |
53 | #include "stream-class.h" | |
54 | #include "stream.h" | |
55 | #include "trace.h" | |
56 | #include "utils.h" | |
57 | ||
ad5268b5 FD |
58 | struct bt_trace_class_destruction_listener_elem { |
59 | bt_trace_class_destruction_listener_func func; | |
60 | void *data; | |
61 | }; | |
62 | ||
862ca4ed PP |
63 | #define BT_ASSERT_PRE_TRACE_CLASS_HOT(_tc) \ |
64 | BT_ASSERT_PRE_HOT((_tc), "Trace class", ": %!+T", (_tc)) | |
65 | ||
66 | static | |
67 | void destroy_trace_class(struct bt_object *obj) | |
68 | { | |
69 | struct bt_trace_class *tc = (void *) obj; | |
70 | ||
71 | BT_LIB_LOGD("Destroying trace class object: %!+T", tc); | |
ad5268b5 FD |
72 | /* |
73 | * Call destruction listener functions so that everything else | |
74 | * still exists in the trace class. | |
75 | */ | |
76 | if (tc->destruction_listeners) { | |
77 | uint64_t i; | |
3f7d4d90 | 78 | BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc); |
c47a6bec SM |
79 | |
80 | /* | |
81 | * The trace class' reference count is 0 if we're here. Increment | |
82 | * it to avoid a double-destroy (possibly infinitely recursive). | |
83 | * This could happen for example if a destruction listener did | |
84 | * bt_object_get_ref() (or anything that causes | |
85 | * bt_object_get_ref() to be called) on the trace class (ref. | |
86 | * count goes from 0 to 1), and then bt_object_put_ref(): the | |
87 | * reference count would go from 1 to 0 again and this function | |
88 | * would be called again. | |
89 | */ | |
90 | tc->base.ref_count++; | |
91 | ||
ad5268b5 FD |
92 | /* Call all the trace class destruction listeners */ |
93 | for (i = 0; i < tc->destruction_listeners->len; i++) { | |
94 | struct bt_trace_class_destruction_listener_elem elem = | |
95 | g_array_index(tc->destruction_listeners, | |
96 | struct bt_trace_class_destruction_listener_elem, i); | |
97 | ||
98 | if (elem.func) { | |
99 | elem.func(tc, elem.data); | |
100 | } | |
c47a6bec SM |
101 | |
102 | /* | |
103 | * The destruction listener should not have kept a | |
104 | * reference to the trace class. | |
105 | */ | |
106 | BT_ASSERT_PRE(tc->base.ref_count == 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc); | |
ad5268b5 FD |
107 | } |
108 | g_array_free(tc->destruction_listeners, TRUE); | |
109 | tc->destruction_listeners = NULL; | |
110 | } | |
862ca4ed PP |
111 | |
112 | if (tc->environment) { | |
113 | BT_LOGD_STR("Destroying environment attributes."); | |
114 | bt_attributes_destroy(tc->environment); | |
115 | tc->environment = NULL; | |
116 | } | |
117 | ||
118 | if (tc->name.str) { | |
119 | g_string_free(tc->name.str, TRUE); | |
120 | tc->name.str = NULL; | |
121 | tc->name.value = NULL; | |
122 | } | |
123 | ||
124 | if (tc->stream_classes) { | |
125 | BT_LOGD_STR("Destroying stream classes."); | |
126 | g_ptr_array_free(tc->stream_classes, TRUE); | |
127 | tc->stream_classes = NULL; | |
128 | } | |
129 | ||
862ca4ed PP |
130 | g_free(tc); |
131 | } | |
132 | ||
41693723 | 133 | struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp) |
862ca4ed PP |
134 | { |
135 | struct bt_trace_class *tc = NULL; | |
862ca4ed | 136 | |
41693723 | 137 | BT_ASSERT_PRE_NON_NULL(self_comp, "Self component"); |
862ca4ed PP |
138 | BT_LOGD_STR("Creating default trace class object."); |
139 | tc = g_new0(struct bt_trace_class, 1); | |
140 | if (!tc) { | |
141 | BT_LOGE_STR("Failed to allocate one trace class."); | |
142 | goto error; | |
143 | } | |
144 | ||
145 | bt_object_init_shared_with_parent(&tc->base, destroy_trace_class); | |
146 | ||
147 | tc->stream_classes = g_ptr_array_new_with_free_func( | |
148 | (GDestroyNotify) bt_object_try_spec_release); | |
149 | if (!tc->stream_classes) { | |
150 | BT_LOGE_STR("Failed to allocate one GPtrArray."); | |
151 | goto error; | |
152 | } | |
153 | ||
154 | tc->name.str = g_string_new(NULL); | |
155 | if (!tc->name.str) { | |
156 | BT_LOGE_STR("Failed to allocate one GString."); | |
157 | goto error; | |
158 | } | |
159 | ||
160 | tc->environment = bt_attributes_create(); | |
161 | if (!tc->environment) { | |
162 | BT_LOGE_STR("Cannot create empty attributes object."); | |
163 | goto error; | |
164 | } | |
165 | ||
ad5268b5 FD |
166 | tc->destruction_listeners = g_array_new(FALSE, TRUE, |
167 | sizeof(struct bt_trace_class_destruction_listener_elem)); | |
168 | if (!tc->destruction_listeners) { | |
169 | BT_LOGE_STR("Failed to allocate one GArray."); | |
170 | goto error; | |
171 | } | |
172 | ||
862ca4ed | 173 | tc->assigns_automatic_stream_class_id = true; |
862ca4ed PP |
174 | BT_LIB_LOGD("Created trace class object: %!+T", tc); |
175 | goto end; | |
176 | ||
177 | error: | |
178 | BT_OBJECT_PUT_REF_AND_RESET(tc); | |
179 | ||
180 | end: | |
181 | return tc; | |
182 | } | |
183 | ||
184 | const char *bt_trace_class_get_name(const struct bt_trace_class *tc) | |
185 | { | |
186 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
187 | return tc->name.value; | |
188 | } | |
189 | ||
a1ed915c PP |
190 | enum bt_trace_class_status bt_trace_class_set_name( |
191 | struct bt_trace_class *tc, const char *name) | |
862ca4ed PP |
192 | { |
193 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
194 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
195 | BT_ASSERT_PRE_TRACE_CLASS_HOT(tc); | |
196 | g_string_assign(tc->name.str, name); | |
197 | tc->name.value = tc->name.str->str; | |
3f7d4d90 | 198 | BT_LIB_LOGD("Set trace class's name: %!+T", tc); |
a1ed915c | 199 | return BT_TRACE_CLASS_STATUS_OK; |
862ca4ed PP |
200 | } |
201 | ||
202 | bt_uuid bt_trace_class_get_uuid(const struct bt_trace_class *tc) | |
203 | { | |
204 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
205 | return tc->uuid.value; | |
206 | } | |
207 | ||
208 | void bt_trace_class_set_uuid(struct bt_trace_class *tc, bt_uuid uuid) | |
209 | { | |
210 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
211 | BT_ASSERT_PRE_NON_NULL(uuid, "UUID"); | |
212 | BT_ASSERT_PRE_TRACE_CLASS_HOT(tc); | |
213 | memcpy(tc->uuid.uuid, uuid, BABELTRACE_UUID_LEN); | |
214 | tc->uuid.value = tc->uuid.uuid; | |
3f7d4d90 | 215 | BT_LIB_LOGD("Set trace class's UUID: %!+T", tc); |
862ca4ed PP |
216 | } |
217 | ||
ad5268b5 FD |
218 | enum bt_trace_class_status bt_trace_class_add_destruction_listener( |
219 | const struct bt_trace_class *_tc, | |
220 | bt_trace_class_destruction_listener_func listener, | |
221 | void *data, uint64_t *listener_id) | |
222 | { | |
223 | struct bt_trace_class *tc = (void *) _tc; | |
224 | uint64_t i; | |
225 | struct bt_trace_class_destruction_listener_elem new_elem = { | |
226 | .func = listener, | |
227 | .data = data, | |
228 | }; | |
229 | ||
230 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
231 | BT_ASSERT_PRE_NON_NULL(listener, "Listener"); | |
232 | ||
233 | /* Find the next available spot */ | |
234 | for (i = 0; i < tc->destruction_listeners->len; i++) { | |
235 | struct bt_trace_class_destruction_listener_elem elem = | |
236 | g_array_index(tc->destruction_listeners, | |
237 | struct bt_trace_class_destruction_listener_elem, i); | |
238 | ||
239 | if (!elem.func) { | |
240 | break; | |
241 | } | |
242 | } | |
243 | ||
244 | if (i == tc->destruction_listeners->len) { | |
245 | g_array_append_val(tc->destruction_listeners, new_elem); | |
246 | } else { | |
247 | g_array_insert_val(tc->destruction_listeners, i, new_elem); | |
248 | } | |
249 | ||
250 | if (listener_id) { | |
251 | *listener_id = i; | |
252 | } | |
253 | ||
3f7d4d90 | 254 | BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, " |
ad5268b5 FD |
255 | "listener-id=%" PRIu64, tc, i); |
256 | return BT_TRACE_CLASS_STATUS_OK; | |
257 | } | |
258 | ||
259 | BT_ASSERT_PRE_FUNC | |
260 | static | |
261 | bool has_listener_id(const struct bt_trace_class *tc, uint64_t listener_id) | |
262 | { | |
263 | BT_ASSERT(listener_id < tc->destruction_listeners->len); | |
264 | return (&g_array_index(tc->destruction_listeners, | |
265 | struct bt_trace_class_destruction_listener_elem, | |
266 | listener_id))->func != NULL; | |
267 | } | |
268 | ||
269 | enum bt_trace_class_status bt_trace_class_remove_destruction_listener( | |
270 | const struct bt_trace_class *_tc, uint64_t listener_id) | |
271 | { | |
272 | struct bt_trace_class *tc = (void *) _tc; | |
273 | struct bt_trace_class_destruction_listener_elem *elem; | |
274 | ||
275 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
276 | BT_ASSERT_PRE(has_listener_id(tc, listener_id), | |
277 | "Trace class has no such trace class destruction listener ID: " | |
278 | "%![tc-]+T, %" PRIu64, tc, listener_id); | |
279 | elem = &g_array_index(tc->destruction_listeners, | |
280 | struct bt_trace_class_destruction_listener_elem, | |
281 | listener_id); | |
282 | BT_ASSERT(elem->func); | |
283 | ||
284 | elem->func = NULL; | |
285 | elem->data = NULL; | |
3f7d4d90 | 286 | BT_LIB_LOGD("Removed trace class destruction listener: " |
ad5268b5 FD |
287 | "%![tc-]+T, listener-id=%" PRIu64, |
288 | tc, listener_id); | |
289 | return BT_TRACE_CLASS_STATUS_OK; | |
290 | } | |
291 | ||
862ca4ed PP |
292 | BT_ASSERT_FUNC |
293 | static | |
294 | bool trace_has_environment_entry(const struct bt_trace_class *tc, const char *name) | |
295 | { | |
296 | BT_ASSERT(tc); | |
297 | ||
298 | return bt_attributes_borrow_field_value_by_name( | |
299 | tc->environment, name) != NULL; | |
300 | } | |
301 | ||
302 | static | |
a1ed915c PP |
303 | enum bt_trace_class_status set_environment_entry(struct bt_trace_class *tc, |
304 | const char *name, struct bt_value *value) | |
862ca4ed PP |
305 | { |
306 | int ret; | |
307 | ||
308 | BT_ASSERT(tc); | |
309 | BT_ASSERT(name); | |
310 | BT_ASSERT(value); | |
311 | BT_ASSERT_PRE(!tc->frozen || | |
312 | !trace_has_environment_entry(tc, name), | |
313 | "Trace class is frozen: cannot replace environment entry: " | |
314 | "%![tc-]+T, entry-name=\"%s\"", tc, name); | |
315 | ret = bt_attributes_set_field_value(tc->environment, name, | |
316 | value); | |
862ca4ed | 317 | if (ret) { |
a1ed915c | 318 | ret = BT_TRACE_CLASS_STATUS_NOMEM; |
862ca4ed PP |
319 | BT_LIB_LOGE("Cannot set trace class's environment entry: " |
320 | "%![tc-]+T, entry-name=\"%s\"", tc, name); | |
321 | } else { | |
a1ed915c | 322 | bt_value_freeze(value); |
3f7d4d90 | 323 | BT_LIB_LOGD("Set trace class's environment entry: " |
862ca4ed PP |
324 | "%![tc-]+T, entry-name=\"%s\"", tc, name); |
325 | } | |
326 | ||
327 | return ret; | |
328 | } | |
329 | ||
a1ed915c | 330 | enum bt_trace_class_status bt_trace_class_set_environment_entry_string( |
862ca4ed PP |
331 | struct bt_trace_class *tc, const char *name, const char *value) |
332 | { | |
333 | int ret; | |
334 | struct bt_value *value_obj; | |
335 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
336 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
337 | BT_ASSERT_PRE_NON_NULL(value, "Value"); | |
338 | value_obj = bt_value_string_create_init(value); | |
339 | if (!value_obj) { | |
340 | BT_LOGE_STR("Cannot create a string value object."); | |
341 | ret = -1; | |
342 | goto end; | |
343 | } | |
344 | ||
345 | /* set_environment_entry() logs errors */ | |
346 | ret = set_environment_entry(tc, name, value_obj); | |
347 | ||
348 | end: | |
349 | bt_object_put_ref(value_obj); | |
350 | return ret; | |
351 | } | |
352 | ||
a1ed915c PP |
353 | enum bt_trace_class_status bt_trace_class_set_environment_entry_integer( |
354 | struct bt_trace_class *tc, const char *name, int64_t value) | |
862ca4ed PP |
355 | { |
356 | int ret; | |
357 | struct bt_value *value_obj; | |
358 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
359 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
fdd3a2da | 360 | value_obj = bt_value_signed_integer_create_init(value); |
862ca4ed PP |
361 | if (!value_obj) { |
362 | BT_LOGE_STR("Cannot create an integer value object."); | |
a1ed915c | 363 | ret = BT_TRACE_CLASS_STATUS_NOMEM; |
862ca4ed PP |
364 | goto end; |
365 | } | |
366 | ||
367 | /* set_environment_entry() logs errors */ | |
368 | ret = set_environment_entry(tc, name, value_obj); | |
369 | ||
370 | end: | |
371 | bt_object_put_ref(value_obj); | |
372 | return ret; | |
373 | } | |
374 | ||
375 | uint64_t bt_trace_class_get_environment_entry_count(const struct bt_trace_class *tc) | |
376 | { | |
377 | int64_t ret; | |
378 | ||
379 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
380 | ret = bt_attributes_get_count(tc->environment); | |
381 | BT_ASSERT(ret >= 0); | |
382 | return (uint64_t) ret; | |
383 | } | |
384 | ||
385 | void bt_trace_class_borrow_environment_entry_by_index_const( | |
386 | const struct bt_trace_class *tc, uint64_t index, | |
387 | const char **name, const struct bt_value **value) | |
388 | { | |
389 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
390 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
391 | BT_ASSERT_PRE_NON_NULL(value, "Value"); | |
392 | BT_ASSERT_PRE_VALID_INDEX(index, | |
393 | bt_attributes_get_count(tc->environment)); | |
394 | *value = bt_attributes_borrow_field_value(tc->environment, index); | |
395 | BT_ASSERT(*value); | |
396 | *name = bt_attributes_get_field_name(tc->environment, index); | |
397 | BT_ASSERT(*name); | |
398 | } | |
399 | ||
400 | const struct bt_value *bt_trace_class_borrow_environment_entry_value_by_name_const( | |
401 | const struct bt_trace_class *tc, const char *name) | |
402 | { | |
403 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
404 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
405 | return bt_attributes_borrow_field_value_by_name(tc->environment, | |
406 | name); | |
407 | } | |
408 | ||
409 | uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc) | |
410 | { | |
411 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
412 | return (uint64_t) tc->stream_classes->len; | |
413 | } | |
414 | ||
415 | struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index( | |
416 | struct bt_trace_class *tc, uint64_t index) | |
417 | { | |
418 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
419 | BT_ASSERT_PRE_VALID_INDEX(index, tc->stream_classes->len); | |
420 | return g_ptr_array_index(tc->stream_classes, index); | |
421 | } | |
422 | ||
423 | const struct bt_stream_class * | |
424 | bt_trace_class_borrow_stream_class_by_index_const( | |
425 | const struct bt_trace_class *tc, uint64_t index) | |
426 | { | |
427 | return bt_trace_class_borrow_stream_class_by_index( | |
428 | (void *) tc, index); | |
429 | } | |
430 | ||
431 | struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id( | |
432 | struct bt_trace_class *tc, uint64_t id) | |
433 | { | |
434 | struct bt_stream_class *stream_class = NULL; | |
435 | uint64_t i; | |
436 | ||
437 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
438 | ||
439 | for (i = 0; i < tc->stream_classes->len; i++) { | |
440 | struct bt_stream_class *stream_class_candidate = | |
441 | g_ptr_array_index(tc->stream_classes, i); | |
442 | ||
443 | if (stream_class_candidate->id == id) { | |
444 | stream_class = stream_class_candidate; | |
445 | goto end; | |
446 | } | |
447 | } | |
448 | ||
449 | end: | |
450 | return stream_class; | |
451 | } | |
452 | ||
453 | const struct bt_stream_class * | |
454 | bt_trace_class_borrow_stream_class_by_id_const( | |
455 | const struct bt_trace_class *tc, uint64_t id) | |
456 | { | |
457 | return bt_trace_class_borrow_stream_class_by_id((void *) tc, id); | |
458 | } | |
459 | ||
862ca4ed PP |
460 | BT_HIDDEN |
461 | void _bt_trace_class_freeze(const struct bt_trace_class *tc) | |
462 | { | |
862ca4ed PP |
463 | BT_ASSERT(tc); |
464 | BT_LIB_LOGD("Freezing trace class: %!+T", tc); | |
465 | ((struct bt_trace_class *) tc)->frozen = true; | |
466 | } | |
467 | ||
468 | bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc) | |
469 | { | |
470 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
471 | return (bt_bool) tc->assigns_automatic_stream_class_id; | |
472 | } | |
473 | ||
474 | void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc, | |
475 | bt_bool value) | |
476 | { | |
477 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
478 | BT_ASSERT_PRE_TRACE_CLASS_HOT(tc); | |
479 | tc->assigns_automatic_stream_class_id = (bool) value; | |
3f7d4d90 | 480 | BT_LIB_LOGD("Set trace class's automatic stream class ID " |
862ca4ed PP |
481 | "assignment property: %!+T", tc); |
482 | } | |
c5b9b441 PP |
483 | |
484 | void bt_trace_class_get_ref(const struct bt_trace_class *trace_class) | |
485 | { | |
486 | bt_object_get_ref(trace_class); | |
487 | } | |
488 | ||
489 | void bt_trace_class_put_ref(const struct bt_trace_class *trace_class) | |
490 | { | |
491 | bt_object_put_ref(trace_class); | |
492 | } |