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