Commit | Line | Data |
---|---|---|
10b7a2e4 | 1 | /* |
f2b0325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
10b7a2e4 PP |
3 | * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
4 | * | |
10b7a2e4 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> | |
0f15f666 | 40 | #include <babeltrace/trace-ir/field-class-internal.h> |
10b7a2e4 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> | |
0f15f666 PP |
45 | #include <babeltrace/value.h> |
46 | #include <babeltrace/value-const.h> | |
47 | #include <babeltrace/value-internal.h> | |
10b7a2e4 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); | |
10b7a2e4 PP |
66 | |
67 | if (tc->environment) { | |
68 | BT_LOGD_STR("Destroying environment attributes."); | |
69 | bt_attributes_destroy(tc->environment); | |
70 | tc->environment = NULL; | |
71 | } | |
72 | ||
73 | if (tc->name.str) { | |
74 | g_string_free(tc->name.str, TRUE); | |
75 | tc->name.str = NULL; | |
76 | tc->name.value = NULL; | |
77 | } | |
78 | ||
79 | if (tc->stream_classes) { | |
80 | BT_LOGD_STR("Destroying stream classes."); | |
81 | g_ptr_array_free(tc->stream_classes, TRUE); | |
82 | tc->stream_classes = NULL; | |
83 | } | |
84 | ||
10b7a2e4 PP |
85 | g_free(tc); |
86 | } | |
87 | ||
e5e71bf8 | 88 | struct bt_trace_class *bt_trace_class_create(bt_self_component *self_comp) |
10b7a2e4 PP |
89 | { |
90 | struct bt_trace_class *tc = NULL; | |
10b7a2e4 | 91 | |
e5e71bf8 | 92 | BT_ASSERT_PRE_NON_NULL(self_comp, "Self component"); |
10b7a2e4 PP |
93 | BT_LOGD_STR("Creating default trace class object."); |
94 | tc = g_new0(struct bt_trace_class, 1); | |
95 | if (!tc) { | |
96 | BT_LOGE_STR("Failed to allocate one trace class."); | |
97 | goto error; | |
98 | } | |
99 | ||
100 | bt_object_init_shared_with_parent(&tc->base, destroy_trace_class); | |
101 | ||
102 | tc->stream_classes = g_ptr_array_new_with_free_func( | |
103 | (GDestroyNotify) bt_object_try_spec_release); | |
104 | if (!tc->stream_classes) { | |
105 | BT_LOGE_STR("Failed to allocate one GPtrArray."); | |
106 | goto error; | |
107 | } | |
108 | ||
109 | tc->name.str = g_string_new(NULL); | |
110 | if (!tc->name.str) { | |
111 | BT_LOGE_STR("Failed to allocate one GString."); | |
112 | goto error; | |
113 | } | |
114 | ||
115 | tc->environment = bt_attributes_create(); | |
116 | if (!tc->environment) { | |
117 | BT_LOGE_STR("Cannot create empty attributes object."); | |
118 | goto error; | |
119 | } | |
120 | ||
121 | tc->assigns_automatic_stream_class_id = true; | |
10b7a2e4 PP |
122 | BT_LIB_LOGD("Created trace class object: %!+T", tc); |
123 | goto end; | |
124 | ||
125 | error: | |
126 | BT_OBJECT_PUT_REF_AND_RESET(tc); | |
127 | ||
128 | end: | |
129 | return tc; | |
130 | } | |
131 | ||
132 | const char *bt_trace_class_get_name(const struct bt_trace_class *tc) | |
133 | { | |
134 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
135 | return tc->name.value; | |
136 | } | |
137 | ||
a9ea68b0 PP |
138 | enum bt_trace_class_status bt_trace_class_set_name( |
139 | struct bt_trace_class *tc, const char *name) | |
10b7a2e4 PP |
140 | { |
141 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
142 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
143 | BT_ASSERT_PRE_TRACE_CLASS_HOT(tc); | |
144 | g_string_assign(tc->name.str, name); | |
145 | tc->name.value = tc->name.str->str; | |
146 | BT_LIB_LOGV("Set trace class's name: %!+T", tc); | |
a9ea68b0 | 147 | return BT_TRACE_CLASS_STATUS_OK; |
10b7a2e4 PP |
148 | } |
149 | ||
150 | bt_uuid bt_trace_class_get_uuid(const struct bt_trace_class *tc) | |
151 | { | |
152 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
153 | return tc->uuid.value; | |
154 | } | |
155 | ||
156 | void bt_trace_class_set_uuid(struct bt_trace_class *tc, bt_uuid uuid) | |
157 | { | |
158 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
159 | BT_ASSERT_PRE_NON_NULL(uuid, "UUID"); | |
160 | BT_ASSERT_PRE_TRACE_CLASS_HOT(tc); | |
161 | memcpy(tc->uuid.uuid, uuid, BABELTRACE_UUID_LEN); | |
162 | tc->uuid.value = tc->uuid.uuid; | |
163 | BT_LIB_LOGV("Set trace class's UUID: %!+T", tc); | |
164 | } | |
165 | ||
166 | BT_ASSERT_FUNC | |
167 | static | |
168 | bool trace_has_environment_entry(const struct bt_trace_class *tc, const char *name) | |
169 | { | |
170 | BT_ASSERT(tc); | |
171 | ||
172 | return bt_attributes_borrow_field_value_by_name( | |
173 | tc->environment, name) != NULL; | |
174 | } | |
175 | ||
176 | static | |
a9ea68b0 PP |
177 | enum bt_trace_class_status set_environment_entry(struct bt_trace_class *tc, |
178 | const char *name, struct bt_value *value) | |
10b7a2e4 PP |
179 | { |
180 | int ret; | |
181 | ||
182 | BT_ASSERT(tc); | |
183 | BT_ASSERT(name); | |
184 | BT_ASSERT(value); | |
185 | BT_ASSERT_PRE(!tc->frozen || | |
186 | !trace_has_environment_entry(tc, name), | |
187 | "Trace class is frozen: cannot replace environment entry: " | |
188 | "%![tc-]+T, entry-name=\"%s\"", tc, name); | |
189 | ret = bt_attributes_set_field_value(tc->environment, name, | |
190 | value); | |
10b7a2e4 | 191 | if (ret) { |
a9ea68b0 | 192 | ret = BT_TRACE_CLASS_STATUS_NOMEM; |
10b7a2e4 PP |
193 | BT_LIB_LOGE("Cannot set trace class's environment entry: " |
194 | "%![tc-]+T, entry-name=\"%s\"", tc, name); | |
195 | } else { | |
a9ea68b0 | 196 | bt_value_freeze(value); |
10b7a2e4 PP |
197 | BT_LIB_LOGV("Set trace class's environment entry: " |
198 | "%![tc-]+T, entry-name=\"%s\"", tc, name); | |
199 | } | |
200 | ||
201 | return ret; | |
202 | } | |
203 | ||
a9ea68b0 | 204 | enum bt_trace_class_status bt_trace_class_set_environment_entry_string( |
10b7a2e4 PP |
205 | struct bt_trace_class *tc, const char *name, const char *value) |
206 | { | |
207 | int ret; | |
208 | struct bt_value *value_obj; | |
209 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
210 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
211 | BT_ASSERT_PRE_NON_NULL(value, "Value"); | |
212 | value_obj = bt_value_string_create_init(value); | |
213 | if (!value_obj) { | |
214 | BT_LOGE_STR("Cannot create a string value object."); | |
215 | ret = -1; | |
216 | goto end; | |
217 | } | |
218 | ||
219 | /* set_environment_entry() logs errors */ | |
220 | ret = set_environment_entry(tc, name, value_obj); | |
221 | ||
222 | end: | |
223 | bt_object_put_ref(value_obj); | |
224 | return ret; | |
225 | } | |
226 | ||
a9ea68b0 PP |
227 | enum bt_trace_class_status bt_trace_class_set_environment_entry_integer( |
228 | struct bt_trace_class *tc, const char *name, int64_t value) | |
10b7a2e4 PP |
229 | { |
230 | int ret; | |
231 | struct bt_value *value_obj; | |
232 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
233 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
234 | value_obj = bt_value_integer_create_init(value); | |
235 | if (!value_obj) { | |
236 | BT_LOGE_STR("Cannot create an integer value object."); | |
a9ea68b0 | 237 | ret = BT_TRACE_CLASS_STATUS_NOMEM; |
10b7a2e4 PP |
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 | ||
249 | uint64_t bt_trace_class_get_environment_entry_count(const struct bt_trace_class *tc) | |
250 | { | |
251 | int64_t ret; | |
252 | ||
253 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
254 | ret = bt_attributes_get_count(tc->environment); | |
255 | BT_ASSERT(ret >= 0); | |
256 | return (uint64_t) ret; | |
257 | } | |
258 | ||
259 | void bt_trace_class_borrow_environment_entry_by_index_const( | |
260 | const struct bt_trace_class *tc, uint64_t index, | |
261 | const char **name, const struct bt_value **value) | |
262 | { | |
263 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
264 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
265 | BT_ASSERT_PRE_NON_NULL(value, "Value"); | |
266 | BT_ASSERT_PRE_VALID_INDEX(index, | |
267 | bt_attributes_get_count(tc->environment)); | |
268 | *value = bt_attributes_borrow_field_value(tc->environment, index); | |
269 | BT_ASSERT(*value); | |
270 | *name = bt_attributes_get_field_name(tc->environment, index); | |
271 | BT_ASSERT(*name); | |
272 | } | |
273 | ||
274 | const struct bt_value *bt_trace_class_borrow_environment_entry_value_by_name_const( | |
275 | const struct bt_trace_class *tc, const char *name) | |
276 | { | |
277 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
278 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
279 | return bt_attributes_borrow_field_value_by_name(tc->environment, | |
280 | name); | |
281 | } | |
282 | ||
283 | uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class *tc) | |
284 | { | |
285 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
286 | return (uint64_t) tc->stream_classes->len; | |
287 | } | |
288 | ||
289 | struct bt_stream_class *bt_trace_class_borrow_stream_class_by_index( | |
290 | struct bt_trace_class *tc, uint64_t index) | |
291 | { | |
292 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
293 | BT_ASSERT_PRE_VALID_INDEX(index, tc->stream_classes->len); | |
294 | return g_ptr_array_index(tc->stream_classes, index); | |
295 | } | |
296 | ||
297 | const struct bt_stream_class * | |
298 | bt_trace_class_borrow_stream_class_by_index_const( | |
299 | const struct bt_trace_class *tc, uint64_t index) | |
300 | { | |
301 | return bt_trace_class_borrow_stream_class_by_index( | |
302 | (void *) tc, index); | |
303 | } | |
304 | ||
305 | struct bt_stream_class *bt_trace_class_borrow_stream_class_by_id( | |
306 | struct bt_trace_class *tc, uint64_t id) | |
307 | { | |
308 | struct bt_stream_class *stream_class = NULL; | |
309 | uint64_t i; | |
310 | ||
311 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
312 | ||
313 | for (i = 0; i < tc->stream_classes->len; i++) { | |
314 | struct bt_stream_class *stream_class_candidate = | |
315 | g_ptr_array_index(tc->stream_classes, i); | |
316 | ||
317 | if (stream_class_candidate->id == id) { | |
318 | stream_class = stream_class_candidate; | |
319 | goto end; | |
320 | } | |
321 | } | |
322 | ||
323 | end: | |
324 | return stream_class; | |
325 | } | |
326 | ||
327 | const struct bt_stream_class * | |
328 | bt_trace_class_borrow_stream_class_by_id_const( | |
329 | const struct bt_trace_class *tc, uint64_t id) | |
330 | { | |
331 | return bt_trace_class_borrow_stream_class_by_id((void *) tc, id); | |
332 | } | |
333 | ||
10b7a2e4 PP |
334 | BT_HIDDEN |
335 | void _bt_trace_class_freeze(const struct bt_trace_class *tc) | |
336 | { | |
10b7a2e4 PP |
337 | BT_ASSERT(tc); |
338 | BT_LIB_LOGD("Freezing trace class: %!+T", tc); | |
339 | ((struct bt_trace_class *) tc)->frozen = true; | |
340 | } | |
341 | ||
342 | bt_bool bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class *tc) | |
343 | { | |
344 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
345 | return (bt_bool) tc->assigns_automatic_stream_class_id; | |
346 | } | |
347 | ||
348 | void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class *tc, | |
349 | bt_bool value) | |
350 | { | |
351 | BT_ASSERT_PRE_NON_NULL(tc, "Trace class"); | |
352 | BT_ASSERT_PRE_TRACE_CLASS_HOT(tc); | |
353 | tc->assigns_automatic_stream_class_id = (bool) value; | |
354 | BT_LIB_LOGV("Set trace class's automatic stream class ID " | |
355 | "assignment property: %!+T", tc); | |
356 | } | |
8c6884d9 PP |
357 | |
358 | void bt_trace_class_get_ref(const struct bt_trace_class *trace_class) | |
359 | { | |
360 | bt_object_get_ref(trace_class); | |
361 | } | |
362 | ||
363 | void bt_trace_class_put_ref(const struct bt_trace_class *trace_class) | |
364 | { | |
365 | bt_object_put_ref(trace_class); | |
366 | } |