Commit | Line | Data |
---|---|---|
272df73e | 1 | /* |
f2b0325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
272df73e PP |
3 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
4 | * | |
272df73e 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 | ||
b03487ab | 24 | #define BT_LOG_TAG "LIB/EVENT-CLASS" |
1633ef46 | 25 | #include "lib/logging.h" |
f8b979f9 | 26 | |
57952005 | 27 | #include "lib/assert-pre.h" |
71c5da58 | 28 | #include <babeltrace2/trace-ir/field-class.h> |
71c5da58 MJ |
29 | #include <babeltrace2/trace-ir/event-class.h> |
30 | #include <babeltrace2/trace-ir/event-class-const.h> | |
71c5da58 | 31 | #include <babeltrace2/trace-ir/stream-class.h> |
57952005 MJ |
32 | #include "compat/compiler.h" |
33 | #include "compat/endian.h" | |
71c5da58 | 34 | #include <babeltrace2/types.h> |
57952005 MJ |
35 | #include "lib/value.h" |
36 | #include "common/assert.h" | |
dc3fffef | 37 | #include <inttypes.h> |
0fbb9a9f | 38 | #include <stdlib.h> |
272df73e | 39 | |
57952005 MJ |
40 | #include "attributes.h" |
41 | #include "clock-snapshot.h" | |
42 | #include "event-class.h" | |
43 | #include "event.h" | |
44 | #include "field-class.h" | |
45 | #include "field.h" | |
46 | #include "resolve-field-path.h" | |
47 | #include "stream-class.h" | |
48 | #include "trace.h" | |
49 | #include "utils.h" | |
fb25b9e3 | 50 | #include "lib/func-status.h" |
57952005 | 51 | |
7b33a0e0 | 52 | #define BT_ASSERT_PRE_EVENT_CLASS_HOT(_ec) \ |
78cf9df6 | 53 | BT_ASSERT_PRE_HOT(((const struct bt_event_class *) (_ec)), \ |
9e550e5f | 54 | "Event class", ": %!+E", (_ec)) |
7b33a0e0 PP |
55 | |
56 | static | |
57 | void destroy_event_class(struct bt_object *obj) | |
272df73e | 58 | { |
7b33a0e0 | 59 | struct bt_event_class *event_class = (void *) obj; |
272df73e | 60 | |
7b33a0e0 | 61 | BT_LIB_LOGD("Destroying event class: %!+E", event_class); |
f8b979f9 | 62 | |
7b33a0e0 PP |
63 | if (event_class->name.str) { |
64 | g_string_free(event_class->name.str, TRUE); | |
1248f5ea | 65 | event_class->name.str = NULL; |
9cf5d083 PP |
66 | } |
67 | ||
7b33a0e0 PP |
68 | if (event_class->emf_uri.str) { |
69 | g_string_free(event_class->emf_uri.str, TRUE); | |
1248f5ea | 70 | event_class->emf_uri.str = NULL; |
272df73e PP |
71 | } |
72 | ||
66fd07a5 | 73 | BT_LOGD_STR("Putting context field class."); |
1248f5ea | 74 | BT_OBJECT_PUT_REF_AND_RESET(event_class->specific_context_fc); |
66fd07a5 | 75 | BT_LOGD_STR("Putting payload field class."); |
1248f5ea | 76 | BT_OBJECT_PUT_REF_AND_RESET(event_class->payload_fc); |
a6918753 | 77 | bt_object_pool_finalize(&event_class->event_pool); |
8deee039 | 78 | g_free(obj); |
272df73e PP |
79 | } |
80 | ||
a6918753 PP |
81 | static |
82 | void free_event(struct bt_event *event, | |
83 | struct bt_event_class *event_class) | |
84 | { | |
85 | bt_event_destroy(event); | |
86 | } | |
87 | ||
7b33a0e0 PP |
88 | BT_ASSERT_PRE_FUNC |
89 | static | |
78cf9df6 PP |
90 | bool event_class_id_is_unique(const struct bt_stream_class *stream_class, |
91 | uint64_t id) | |
272df73e | 92 | { |
7b33a0e0 PP |
93 | uint64_t i; |
94 | bool is_unique = true; | |
272df73e | 95 | |
7b33a0e0 | 96 | for (i = 0; i < stream_class->event_classes->len; i++) { |
78cf9df6 | 97 | const struct bt_event_class *ec = |
7b33a0e0 PP |
98 | stream_class->event_classes->pdata[i]; |
99 | ||
100 | if (ec->id == id) { | |
101 | is_unique = false; | |
102 | goto end; | |
103 | } | |
9cf5d083 | 104 | } |
272df73e | 105 | |
7b33a0e0 PP |
106 | end: |
107 | return is_unique; | |
108 | } | |
109 | ||
110 | static | |
111 | struct bt_event_class *create_event_class_with_id( | |
112 | struct bt_stream_class *stream_class, uint64_t id) | |
113 | { | |
114 | int ret; | |
115 | struct bt_event_class *event_class; | |
116 | ||
117 | BT_ASSERT(stream_class); | |
118 | BT_ASSERT_PRE(event_class_id_is_unique(stream_class, id), | |
119 | "Duplicate event class ID: %![sc-]+S, id=%" PRIu64, | |
120 | stream_class, id); | |
121 | BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64, | |
122 | stream_class, id); | |
8deee039 | 123 | event_class = g_new0(struct bt_event_class, 1); |
272df73e | 124 | if (!event_class) { |
8deee039 PP |
125 | BT_LOGE_STR("Failed to allocate one event class."); |
126 | goto error; | |
272df73e PP |
127 | } |
128 | ||
7b33a0e0 PP |
129 | bt_object_init_shared_with_parent(&event_class->base, |
130 | destroy_event_class); | |
131 | event_class->id = id; | |
132 | bt_property_uint_init(&event_class->log_level, | |
133 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0); | |
134 | event_class->name.str = g_string_new(NULL); | |
135 | if (!event_class->name.str) { | |
136 | BT_LOGE_STR("Failed to allocate a GString."); | |
137 | ret = -1; | |
138 | goto end; | |
139 | } | |
140 | ||
141 | event_class->emf_uri.str = g_string_new(NULL); | |
142 | if (!event_class->emf_uri.str) { | |
143 | BT_LOGE_STR("Failed to allocate a GString."); | |
144 | ret = -1; | |
145 | goto end; | |
272df73e PP |
146 | } |
147 | ||
a6918753 PP |
148 | ret = bt_object_pool_initialize(&event_class->event_pool, |
149 | (bt_object_pool_new_object_func) bt_event_new, | |
150 | (bt_object_pool_destroy_object_func) free_event, | |
151 | event_class); | |
152 | if (ret) { | |
153 | BT_LOGE("Failed to initialize event pool: ret=%d", | |
154 | ret); | |
155 | goto error; | |
156 | } | |
157 | ||
7b33a0e0 PP |
158 | bt_object_set_parent(&event_class->base, &stream_class->base); |
159 | g_ptr_array_add(stream_class->event_classes, event_class); | |
160 | bt_stream_class_freeze(stream_class); | |
161 | BT_LIB_LOGD("Created event class object: %!+E", event_class); | |
8deee039 | 162 | goto end; |
272df73e | 163 | |
8deee039 | 164 | error: |
8138bfe1 | 165 | BT_OBJECT_PUT_REF_AND_RESET(event_class); |
272df73e PP |
166 | |
167 | end: | |
8deee039 | 168 | return event_class; |
272df73e PP |
169 | } |
170 | ||
78cf9df6 PP |
171 | struct bt_event_class *bt_event_class_create( |
172 | struct bt_stream_class *stream_class) | |
7b33a0e0 PP |
173 | { |
174 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); | |
175 | BT_ASSERT_PRE(stream_class->assigns_automatic_event_class_id, | |
176 | "Stream class does not automatically assigns event class IDs: " | |
177 | "%![sc-]+S", stream_class); | |
78cf9df6 | 178 | return create_event_class_with_id(stream_class, |
7b33a0e0 PP |
179 | (uint64_t) stream_class->event_classes->len); |
180 | } | |
181 | ||
78cf9df6 PP |
182 | struct bt_event_class *bt_event_class_create_with_id( |
183 | struct bt_stream_class *stream_class, uint64_t id) | |
7b33a0e0 PP |
184 | { |
185 | BT_ASSERT_PRE(!stream_class->assigns_automatic_event_class_id, | |
186 | "Stream class automatically assigns event class IDs: " | |
187 | "%![sc-]+S", stream_class); | |
78cf9df6 | 188 | return create_event_class_with_id(stream_class, id); |
7b33a0e0 PP |
189 | } |
190 | ||
78cf9df6 | 191 | const char *bt_event_class_get_name(const struct bt_event_class *event_class) |
272df73e | 192 | { |
18acc6f8 | 193 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
7b33a0e0 | 194 | return event_class->name.value; |
272df73e PP |
195 | } |
196 | ||
fb25b9e3 | 197 | enum bt_event_class_set_name_status bt_event_class_set_name( |
4caa7d56 | 198 | struct bt_event_class *event_class, const char *name) |
272df73e | 199 | { |
18acc6f8 | 200 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
7b33a0e0 PP |
201 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
202 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); | |
203 | g_string_assign(event_class->name.str, name); | |
204 | event_class->name.value = event_class->name.str->str; | |
a684a357 | 205 | BT_LIB_LOGD("Set event class's name: %!+E", event_class); |
fb25b9e3 | 206 | return BT_FUNC_STATUS_OK; |
272df73e PP |
207 | } |
208 | ||
78cf9df6 | 209 | uint64_t bt_event_class_get_id(const struct bt_event_class *event_class) |
272df73e | 210 | { |
7b33a0e0 PP |
211 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
212 | return event_class->id; | |
272df73e PP |
213 | } |
214 | ||
7b33a0e0 | 215 | enum bt_property_availability bt_event_class_get_log_level( |
78cf9df6 | 216 | const struct bt_event_class *event_class, |
7b33a0e0 | 217 | enum bt_event_class_log_level *log_level) |
272df73e | 218 | { |
18acc6f8 | 219 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
7b33a0e0 PP |
220 | BT_ASSERT_PRE_NON_NULL(log_level, "Log level (output)"); |
221 | *log_level = (enum bt_event_class_log_level) | |
222 | event_class->log_level.value; | |
223 | return event_class->log_level.base.avail; | |
272df73e PP |
224 | } |
225 | ||
78cf9df6 PP |
226 | void bt_event_class_set_log_level( |
227 | struct bt_event_class *event_class, | |
8deee039 | 228 | enum bt_event_class_log_level log_level) |
272df73e | 229 | { |
7b33a0e0 PP |
230 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
231 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); | |
232 | bt_property_uint_set(&event_class->log_level, | |
233 | (uint64_t) log_level); | |
a684a357 | 234 | BT_LIB_LOGD("Set event class's log level: %!+E", event_class); |
272df73e PP |
235 | } |
236 | ||
78cf9df6 | 237 | const char *bt_event_class_get_emf_uri(const struct bt_event_class *event_class) |
272df73e | 238 | { |
18acc6f8 | 239 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
7b33a0e0 | 240 | return event_class->emf_uri.value; |
272df73e PP |
241 | } |
242 | ||
fb25b9e3 | 243 | enum bt_event_class_set_emf_uri_status bt_event_class_set_emf_uri( |
78cf9df6 | 244 | struct bt_event_class *event_class, |
8deee039 | 245 | const char *emf_uri) |
272df73e | 246 | { |
7b33a0e0 PP |
247 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
248 | BT_ASSERT_PRE_NON_NULL(emf_uri, "EMF URI"); | |
249 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); | |
250 | g_string_assign(event_class->emf_uri.str, emf_uri); | |
251 | event_class->emf_uri.value = event_class->emf_uri.str->str; | |
a684a357 | 252 | BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class); |
fb25b9e3 | 253 | return BT_FUNC_STATUS_OK; |
272df73e PP |
254 | } |
255 | ||
5fe68922 | 256 | struct bt_stream_class *bt_event_class_borrow_stream_class( |
839d52a5 | 257 | struct bt_event_class *event_class) |
272df73e | 258 | { |
44ea72c5 | 259 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
7b33a0e0 | 260 | return bt_event_class_borrow_stream_class_inline(event_class); |
272df73e PP |
261 | } |
262 | ||
78cf9df6 PP |
263 | const struct bt_stream_class * |
264 | bt_event_class_borrow_stream_class_const( | |
265 | const struct bt_event_class *event_class) | |
9e550e5f | 266 | { |
78cf9df6 | 267 | return bt_event_class_borrow_stream_class((void *) event_class); |
9e550e5f PP |
268 | } |
269 | ||
78cf9df6 PP |
270 | const struct bt_field_class * |
271 | bt_event_class_borrow_specific_context_field_class_const( | |
272 | const struct bt_event_class *event_class) | |
272df73e | 273 | { |
18acc6f8 | 274 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
939190b3 | 275 | return event_class->specific_context_fc; |
272df73e PP |
276 | } |
277 | ||
909d0a89 PP |
278 | struct bt_field_class * |
279 | bt_event_class_borrow_specific_context_field_class( | |
280 | struct bt_event_class *event_class) | |
281 | { | |
282 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); | |
283 | return event_class->specific_context_fc; | |
284 | } | |
285 | ||
fb25b9e3 PP |
286 | enum bt_event_class_set_field_class_status |
287 | bt_event_class_set_specific_context_field_class( | |
78cf9df6 PP |
288 | struct bt_event_class *event_class, |
289 | struct bt_field_class *field_class) | |
272df73e | 290 | { |
7b33a0e0 PP |
291 | int ret; |
292 | struct bt_stream_class *stream_class; | |
7b33a0e0 | 293 | struct bt_resolve_field_path_context resolve_ctx = { |
7b33a0e0 | 294 | .packet_context = NULL, |
7b33a0e0 | 295 | .event_common_context = NULL, |
939190b3 | 296 | .event_specific_context = field_class, |
7b33a0e0 PP |
297 | .event_payload = NULL, |
298 | }; | |
18acc6f8 | 299 | |
7b33a0e0 | 300 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
939190b3 | 301 | BT_ASSERT_PRE_NON_NULL(field_class, "Field class"); |
7b33a0e0 | 302 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); |
af0c18e3 PP |
303 | BT_ASSERT_PRE(bt_field_class_get_type(field_class) == |
304 | BT_FIELD_CLASS_TYPE_STRUCTURE, | |
66fd07a5 | 305 | "Specific context field class is not a structure field class: " |
939190b3 | 306 | "%!+F", field_class); |
7b33a0e0 PP |
307 | stream_class = bt_event_class_borrow_stream_class_inline( |
308 | event_class); | |
939190b3 | 309 | resolve_ctx.packet_context = stream_class->packet_context_fc; |
7b33a0e0 | 310 | resolve_ctx.event_common_context = |
939190b3 | 311 | stream_class->event_common_context_fc; |
7b33a0e0 | 312 | |
939190b3 | 313 | ret = bt_resolve_field_paths(field_class, &resolve_ctx); |
7b33a0e0 | 314 | if (ret) { |
4caa7d56 PP |
315 | /* |
316 | * This is the only reason for which | |
317 | * bt_resolve_field_paths() can fail: anything else | |
318 | * would be because a precondition is not satisfied. | |
319 | */ | |
fb25b9e3 | 320 | ret = BT_FUNC_STATUS_MEMORY_ERROR; |
18acc6f8 PP |
321 | goto end; |
322 | } | |
323 | ||
10b7a2e4 | 324 | bt_field_class_make_part_of_trace_class(field_class); |
8138bfe1 | 325 | bt_object_put_ref(event_class->specific_context_fc); |
4b70020d PP |
326 | event_class->specific_context_fc = field_class; |
327 | bt_object_get_no_null_check(event_class->specific_context_fc); | |
939190b3 | 328 | bt_field_class_freeze(field_class); |
a684a357 | 329 | BT_LIB_LOGD("Set event class's specific context field class: %!+E", |
7b33a0e0 | 330 | event_class); |
18acc6f8 | 331 | |
18acc6f8 PP |
332 | end: |
333 | return ret; | |
272df73e PP |
334 | } |
335 | ||
78cf9df6 PP |
336 | const struct bt_field_class *bt_event_class_borrow_payload_field_class_const( |
337 | const struct bt_event_class *event_class) | |
272df73e | 338 | { |
18acc6f8 | 339 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
939190b3 | 340 | return event_class->payload_fc; |
272df73e PP |
341 | } |
342 | ||
909d0a89 PP |
343 | struct bt_field_class *bt_event_class_borrow_payload_field_class( |
344 | struct bt_event_class *event_class) | |
345 | { | |
346 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); | |
347 | return event_class->payload_fc; | |
348 | } | |
349 | ||
fb25b9e3 PP |
350 | enum bt_event_class_set_field_class_status |
351 | bt_event_class_set_payload_field_class( | |
78cf9df6 PP |
352 | struct bt_event_class *event_class, |
353 | struct bt_field_class *field_class) | |
272df73e | 354 | { |
7b33a0e0 PP |
355 | int ret; |
356 | struct bt_stream_class *stream_class; | |
7b33a0e0 | 357 | struct bt_resolve_field_path_context resolve_ctx = { |
7b33a0e0 | 358 | .packet_context = NULL, |
7b33a0e0 PP |
359 | .event_common_context = NULL, |
360 | .event_specific_context = NULL, | |
939190b3 | 361 | .event_payload = field_class, |
7b33a0e0 | 362 | }; |
18acc6f8 | 363 | |
7b33a0e0 | 364 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
939190b3 | 365 | BT_ASSERT_PRE_NON_NULL(field_class, "Field class"); |
7b33a0e0 | 366 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); |
af0c18e3 PP |
367 | BT_ASSERT_PRE(bt_field_class_get_type(field_class) == |
368 | BT_FIELD_CLASS_TYPE_STRUCTURE, | |
66fd07a5 | 369 | "Payload field class is not a structure field class: %!+F", |
939190b3 | 370 | field_class); |
7b33a0e0 PP |
371 | stream_class = bt_event_class_borrow_stream_class_inline( |
372 | event_class); | |
939190b3 | 373 | resolve_ctx.packet_context = stream_class->packet_context_fc; |
7b33a0e0 | 374 | resolve_ctx.event_common_context = |
939190b3 PP |
375 | stream_class->event_common_context_fc; |
376 | resolve_ctx.event_specific_context = event_class->specific_context_fc; | |
7b33a0e0 | 377 | |
939190b3 | 378 | ret = bt_resolve_field_paths(field_class, &resolve_ctx); |
7b33a0e0 | 379 | if (ret) { |
4caa7d56 PP |
380 | /* |
381 | * This is the only reason for which | |
382 | * bt_resolve_field_paths() can fail: anything else | |
383 | * would be because a precondition is not satisfied. | |
384 | */ | |
fb25b9e3 | 385 | ret = BT_FUNC_STATUS_MEMORY_ERROR; |
18acc6f8 PP |
386 | goto end; |
387 | } | |
388 | ||
10b7a2e4 | 389 | bt_field_class_make_part_of_trace_class(field_class); |
8138bfe1 | 390 | bt_object_put_ref(event_class->payload_fc); |
4b70020d PP |
391 | event_class->payload_fc = field_class; |
392 | bt_object_get_no_null_check(event_class->payload_fc); | |
939190b3 | 393 | bt_field_class_freeze(field_class); |
a684a357 | 394 | BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class); |
18acc6f8 PP |
395 | |
396 | end: | |
397 | return ret; | |
272df73e PP |
398 | } |
399 | ||
400 | BT_HIDDEN | |
78cf9df6 | 401 | void _bt_event_class_freeze(const struct bt_event_class *event_class) |
8c4a29ba | 402 | { |
939190b3 | 403 | /* The field classes are already frozen */ |
8b45963b | 404 | BT_ASSERT(event_class); |
7b33a0e0 | 405 | BT_LIB_LOGD("Freezing event class: %!+E", event_class); |
78cf9df6 | 406 | ((struct bt_event_class *) event_class)->frozen = true; |
8c4a29ba | 407 | } |
8c6884d9 PP |
408 | |
409 | void bt_event_class_get_ref(const struct bt_event_class *event_class) | |
410 | { | |
411 | bt_object_get_ref(event_class); | |
412 | } | |
413 | ||
414 | void bt_event_class_put_ref(const struct bt_event_class *event_class) | |
415 | { | |
416 | bt_object_put_ref(event_class); | |
417 | } |