Commit | Line | Data |
---|---|---|
272df73e | 1 | /* |
e2f7325d | 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 | ||
350ad6c1 | 24 | #define BT_LOG_TAG "LIB/EVENT-CLASS" |
c2d9d9cf | 25 | #include "lib/logging.h" |
f8b979f9 | 26 | |
578e048b | 27 | #include "lib/assert-pre.h" |
3fadfbc0 | 28 | #include <babeltrace2/trace-ir/field-class.h> |
3fadfbc0 MJ |
29 | #include <babeltrace2/trace-ir/event-class.h> |
30 | #include <babeltrace2/trace-ir/event-class-const.h> | |
3fadfbc0 | 31 | #include <babeltrace2/trace-ir/stream-class.h> |
578e048b MJ |
32 | #include "compat/compiler.h" |
33 | #include "compat/endian.h" | |
3fadfbc0 | 34 | #include <babeltrace2/types.h> |
578e048b MJ |
35 | #include "lib/value.h" |
36 | #include "common/assert.h" | |
dc3fffef | 37 | #include <inttypes.h> |
0fbb9a9f | 38 | #include <stdlib.h> |
272df73e | 39 | |
578e048b 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" | |
d24d5663 | 50 | #include "lib/func-status.h" |
578e048b | 51 | |
44c440bc | 52 | #define BT_ASSERT_PRE_EVENT_CLASS_HOT(_ec) \ |
40f4ba76 | 53 | BT_ASSERT_PRE_HOT(((const struct bt_event_class *) (_ec)), \ |
e5be10ef | 54 | "Event class", ": %!+E", (_ec)) |
44c440bc PP |
55 | |
56 | static | |
57 | void destroy_event_class(struct bt_object *obj) | |
272df73e | 58 | { |
44c440bc | 59 | struct bt_event_class *event_class = (void *) obj; |
272df73e | 60 | |
44c440bc | 61 | BT_LIB_LOGD("Destroying event class: %!+E", event_class); |
f8b979f9 | 62 | |
44c440bc PP |
63 | if (event_class->name.str) { |
64 | g_string_free(event_class->name.str, TRUE); | |
238b7404 | 65 | event_class->name.str = NULL; |
cf76ce92 PP |
66 | } |
67 | ||
44c440bc PP |
68 | if (event_class->emf_uri.str) { |
69 | g_string_free(event_class->emf_uri.str, TRUE); | |
238b7404 | 70 | event_class->emf_uri.str = NULL; |
272df73e PP |
71 | } |
72 | ||
e6276565 | 73 | BT_LOGD_STR("Putting context field class."); |
238b7404 | 74 | BT_OBJECT_PUT_REF_AND_RESET(event_class->specific_context_fc); |
e6276565 | 75 | BT_LOGD_STR("Putting payload field class."); |
238b7404 | 76 | BT_OBJECT_PUT_REF_AND_RESET(event_class->payload_fc); |
312c056a | 77 | bt_object_pool_finalize(&event_class->event_pool); |
3dca2276 | 78 | g_free(obj); |
272df73e PP |
79 | } |
80 | ||
312c056a 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 | ||
44c440bc PP |
88 | BT_ASSERT_PRE_FUNC |
89 | static | |
40f4ba76 PP |
90 | bool event_class_id_is_unique(const struct bt_stream_class *stream_class, |
91 | uint64_t id) | |
272df73e | 92 | { |
44c440bc PP |
93 | uint64_t i; |
94 | bool is_unique = true; | |
272df73e | 95 | |
44c440bc | 96 | for (i = 0; i < stream_class->event_classes->len; i++) { |
40f4ba76 | 97 | const struct bt_event_class *ec = |
44c440bc PP |
98 | stream_class->event_classes->pdata[i]; |
99 | ||
100 | if (ec->id == id) { | |
101 | is_unique = false; | |
102 | goto end; | |
103 | } | |
cf76ce92 | 104 | } |
272df73e | 105 | |
44c440bc 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); | |
3dca2276 | 123 | event_class = g_new0(struct bt_event_class, 1); |
272df73e | 124 | if (!event_class) { |
870631a2 | 125 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class."); |
3dca2276 | 126 | goto error; |
272df73e PP |
127 | } |
128 | ||
44c440bc 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) { | |
870631a2 | 136 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
44c440bc PP |
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) { | |
870631a2 | 143 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
44c440bc PP |
144 | ret = -1; |
145 | goto end; | |
272df73e PP |
146 | } |
147 | ||
312c056a 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) { | |
870631a2 PP |
153 | BT_LIB_LOGE_APPEND_CAUSE( |
154 | "Failed to initialize event pool: ret=%d", | |
312c056a PP |
155 | ret); |
156 | goto error; | |
157 | } | |
158 | ||
44c440bc PP |
159 | bt_object_set_parent(&event_class->base, &stream_class->base); |
160 | g_ptr_array_add(stream_class->event_classes, event_class); | |
161 | bt_stream_class_freeze(stream_class); | |
162 | BT_LIB_LOGD("Created event class object: %!+E", event_class); | |
3dca2276 | 163 | goto end; |
272df73e | 164 | |
3dca2276 | 165 | error: |
65300d60 | 166 | BT_OBJECT_PUT_REF_AND_RESET(event_class); |
272df73e PP |
167 | |
168 | end: | |
3dca2276 | 169 | return event_class; |
272df73e PP |
170 | } |
171 | ||
40f4ba76 PP |
172 | struct bt_event_class *bt_event_class_create( |
173 | struct bt_stream_class *stream_class) | |
44c440bc PP |
174 | { |
175 | BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class"); | |
176 | BT_ASSERT_PRE(stream_class->assigns_automatic_event_class_id, | |
177 | "Stream class does not automatically assigns event class IDs: " | |
178 | "%![sc-]+S", stream_class); | |
40f4ba76 | 179 | return create_event_class_with_id(stream_class, |
44c440bc PP |
180 | (uint64_t) stream_class->event_classes->len); |
181 | } | |
182 | ||
40f4ba76 PP |
183 | struct bt_event_class *bt_event_class_create_with_id( |
184 | struct bt_stream_class *stream_class, uint64_t id) | |
44c440bc PP |
185 | { |
186 | BT_ASSERT_PRE(!stream_class->assigns_automatic_event_class_id, | |
187 | "Stream class automatically assigns event class IDs: " | |
188 | "%![sc-]+S", stream_class); | |
40f4ba76 | 189 | return create_event_class_with_id(stream_class, id); |
44c440bc PP |
190 | } |
191 | ||
40f4ba76 | 192 | const char *bt_event_class_get_name(const struct bt_event_class *event_class) |
272df73e | 193 | { |
cb6f1f7d | 194 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc | 195 | return event_class->name.value; |
272df73e PP |
196 | } |
197 | ||
d24d5663 | 198 | enum bt_event_class_set_name_status bt_event_class_set_name( |
d08300f5 | 199 | struct bt_event_class *event_class, const char *name) |
272df73e | 200 | { |
cb6f1f7d | 201 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc PP |
202 | BT_ASSERT_PRE_NON_NULL(name, "Name"); |
203 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); | |
204 | g_string_assign(event_class->name.str, name); | |
205 | event_class->name.value = event_class->name.str->str; | |
3f7d4d90 | 206 | BT_LIB_LOGD("Set event class's name: %!+E", event_class); |
d24d5663 | 207 | return BT_FUNC_STATUS_OK; |
272df73e PP |
208 | } |
209 | ||
40f4ba76 | 210 | uint64_t bt_event_class_get_id(const struct bt_event_class *event_class) |
272df73e | 211 | { |
44c440bc PP |
212 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
213 | return event_class->id; | |
272df73e PP |
214 | } |
215 | ||
44c440bc | 216 | enum bt_property_availability bt_event_class_get_log_level( |
40f4ba76 | 217 | const struct bt_event_class *event_class, |
44c440bc | 218 | enum bt_event_class_log_level *log_level) |
272df73e | 219 | { |
cb6f1f7d | 220 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc PP |
221 | BT_ASSERT_PRE_NON_NULL(log_level, "Log level (output)"); |
222 | *log_level = (enum bt_event_class_log_level) | |
223 | event_class->log_level.value; | |
224 | return event_class->log_level.base.avail; | |
272df73e PP |
225 | } |
226 | ||
40f4ba76 PP |
227 | void bt_event_class_set_log_level( |
228 | struct bt_event_class *event_class, | |
3dca2276 | 229 | enum bt_event_class_log_level log_level) |
272df73e | 230 | { |
44c440bc PP |
231 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
232 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); | |
233 | bt_property_uint_set(&event_class->log_level, | |
234 | (uint64_t) log_level); | |
3f7d4d90 | 235 | BT_LIB_LOGD("Set event class's log level: %!+E", event_class); |
272df73e PP |
236 | } |
237 | ||
40f4ba76 | 238 | const char *bt_event_class_get_emf_uri(const struct bt_event_class *event_class) |
272df73e | 239 | { |
cb6f1f7d | 240 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc | 241 | return event_class->emf_uri.value; |
272df73e PP |
242 | } |
243 | ||
d24d5663 | 244 | enum bt_event_class_set_emf_uri_status bt_event_class_set_emf_uri( |
40f4ba76 | 245 | struct bt_event_class *event_class, |
3dca2276 | 246 | const char *emf_uri) |
272df73e | 247 | { |
44c440bc PP |
248 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
249 | BT_ASSERT_PRE_NON_NULL(emf_uri, "EMF URI"); | |
250 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); | |
251 | g_string_assign(event_class->emf_uri.str, emf_uri); | |
252 | event_class->emf_uri.value = event_class->emf_uri.str->str; | |
3f7d4d90 | 253 | BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class); |
d24d5663 | 254 | return BT_FUNC_STATUS_OK; |
272df73e PP |
255 | } |
256 | ||
094ff7c0 | 257 | struct bt_stream_class *bt_event_class_borrow_stream_class( |
50842bdc | 258 | struct bt_event_class *event_class) |
272df73e | 259 | { |
d975f66c | 260 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc | 261 | return bt_event_class_borrow_stream_class_inline(event_class); |
272df73e PP |
262 | } |
263 | ||
40f4ba76 PP |
264 | const struct bt_stream_class * |
265 | bt_event_class_borrow_stream_class_const( | |
266 | const struct bt_event_class *event_class) | |
e5be10ef | 267 | { |
40f4ba76 | 268 | return bt_event_class_borrow_stream_class((void *) event_class); |
e5be10ef PP |
269 | } |
270 | ||
40f4ba76 PP |
271 | const struct bt_field_class * |
272 | bt_event_class_borrow_specific_context_field_class_const( | |
273 | const struct bt_event_class *event_class) | |
272df73e | 274 | { |
cb6f1f7d | 275 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
5cd6d0e5 | 276 | return event_class->specific_context_fc; |
272df73e PP |
277 | } |
278 | ||
740faaf4 PP |
279 | struct bt_field_class * |
280 | bt_event_class_borrow_specific_context_field_class( | |
281 | struct bt_event_class *event_class) | |
282 | { | |
283 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); | |
284 | return event_class->specific_context_fc; | |
285 | } | |
286 | ||
d24d5663 PP |
287 | enum bt_event_class_set_field_class_status |
288 | bt_event_class_set_specific_context_field_class( | |
40f4ba76 PP |
289 | struct bt_event_class *event_class, |
290 | struct bt_field_class *field_class) | |
272df73e | 291 | { |
44c440bc PP |
292 | int ret; |
293 | struct bt_stream_class *stream_class; | |
44c440bc | 294 | struct bt_resolve_field_path_context resolve_ctx = { |
44c440bc | 295 | .packet_context = NULL, |
44c440bc | 296 | .event_common_context = NULL, |
5cd6d0e5 | 297 | .event_specific_context = field_class, |
44c440bc PP |
298 | .event_payload = NULL, |
299 | }; | |
cb6f1f7d | 300 | |
44c440bc | 301 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
5cd6d0e5 | 302 | BT_ASSERT_PRE_NON_NULL(field_class, "Field class"); |
44c440bc | 303 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); |
864cad70 PP |
304 | BT_ASSERT_PRE(bt_field_class_get_type(field_class) == |
305 | BT_FIELD_CLASS_TYPE_STRUCTURE, | |
e6276565 | 306 | "Specific context field class is not a structure field class: " |
5cd6d0e5 | 307 | "%!+F", field_class); |
44c440bc PP |
308 | stream_class = bt_event_class_borrow_stream_class_inline( |
309 | event_class); | |
5cd6d0e5 | 310 | resolve_ctx.packet_context = stream_class->packet_context_fc; |
44c440bc | 311 | resolve_ctx.event_common_context = |
5cd6d0e5 | 312 | stream_class->event_common_context_fc; |
44c440bc | 313 | |
5cd6d0e5 | 314 | ret = bt_resolve_field_paths(field_class, &resolve_ctx); |
44c440bc | 315 | if (ret) { |
d08300f5 PP |
316 | /* |
317 | * This is the only reason for which | |
318 | * bt_resolve_field_paths() can fail: anything else | |
319 | * would be because a precondition is not satisfied. | |
320 | */ | |
d24d5663 | 321 | ret = BT_FUNC_STATUS_MEMORY_ERROR; |
cb6f1f7d PP |
322 | goto end; |
323 | } | |
324 | ||
862ca4ed | 325 | bt_field_class_make_part_of_trace_class(field_class); |
65300d60 | 326 | bt_object_put_ref(event_class->specific_context_fc); |
398454ed PP |
327 | event_class->specific_context_fc = field_class; |
328 | bt_object_get_no_null_check(event_class->specific_context_fc); | |
5cd6d0e5 | 329 | bt_field_class_freeze(field_class); |
3f7d4d90 | 330 | BT_LIB_LOGD("Set event class's specific context field class: %!+E", |
44c440bc | 331 | event_class); |
cb6f1f7d | 332 | |
cb6f1f7d PP |
333 | end: |
334 | return ret; | |
272df73e PP |
335 | } |
336 | ||
40f4ba76 PP |
337 | const struct bt_field_class *bt_event_class_borrow_payload_field_class_const( |
338 | const struct bt_event_class *event_class) | |
272df73e | 339 | { |
cb6f1f7d | 340 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
5cd6d0e5 | 341 | return event_class->payload_fc; |
272df73e PP |
342 | } |
343 | ||
740faaf4 PP |
344 | struct bt_field_class *bt_event_class_borrow_payload_field_class( |
345 | struct bt_event_class *event_class) | |
346 | { | |
347 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); | |
348 | return event_class->payload_fc; | |
349 | } | |
350 | ||
d24d5663 PP |
351 | enum bt_event_class_set_field_class_status |
352 | bt_event_class_set_payload_field_class( | |
40f4ba76 PP |
353 | struct bt_event_class *event_class, |
354 | struct bt_field_class *field_class) | |
272df73e | 355 | { |
44c440bc PP |
356 | int ret; |
357 | struct bt_stream_class *stream_class; | |
44c440bc | 358 | struct bt_resolve_field_path_context resolve_ctx = { |
44c440bc | 359 | .packet_context = NULL, |
44c440bc PP |
360 | .event_common_context = NULL, |
361 | .event_specific_context = NULL, | |
5cd6d0e5 | 362 | .event_payload = field_class, |
44c440bc | 363 | }; |
cb6f1f7d | 364 | |
44c440bc | 365 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
5cd6d0e5 | 366 | BT_ASSERT_PRE_NON_NULL(field_class, "Field class"); |
44c440bc | 367 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); |
864cad70 PP |
368 | BT_ASSERT_PRE(bt_field_class_get_type(field_class) == |
369 | BT_FIELD_CLASS_TYPE_STRUCTURE, | |
e6276565 | 370 | "Payload field class is not a structure field class: %!+F", |
5cd6d0e5 | 371 | field_class); |
44c440bc PP |
372 | stream_class = bt_event_class_borrow_stream_class_inline( |
373 | event_class); | |
5cd6d0e5 | 374 | resolve_ctx.packet_context = stream_class->packet_context_fc; |
44c440bc | 375 | resolve_ctx.event_common_context = |
5cd6d0e5 PP |
376 | stream_class->event_common_context_fc; |
377 | resolve_ctx.event_specific_context = event_class->specific_context_fc; | |
44c440bc | 378 | |
5cd6d0e5 | 379 | ret = bt_resolve_field_paths(field_class, &resolve_ctx); |
44c440bc | 380 | if (ret) { |
d08300f5 PP |
381 | /* |
382 | * This is the only reason for which | |
383 | * bt_resolve_field_paths() can fail: anything else | |
384 | * would be because a precondition is not satisfied. | |
385 | */ | |
d24d5663 | 386 | ret = BT_FUNC_STATUS_MEMORY_ERROR; |
cb6f1f7d PP |
387 | goto end; |
388 | } | |
389 | ||
862ca4ed | 390 | bt_field_class_make_part_of_trace_class(field_class); |
65300d60 | 391 | bt_object_put_ref(event_class->payload_fc); |
398454ed PP |
392 | event_class->payload_fc = field_class; |
393 | bt_object_get_no_null_check(event_class->payload_fc); | |
5cd6d0e5 | 394 | bt_field_class_freeze(field_class); |
3f7d4d90 | 395 | BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class); |
cb6f1f7d PP |
396 | |
397 | end: | |
398 | return ret; | |
272df73e PP |
399 | } |
400 | ||
401 | BT_HIDDEN | |
40f4ba76 | 402 | void _bt_event_class_freeze(const struct bt_event_class *event_class) |
2a3ced3c | 403 | { |
5cd6d0e5 | 404 | /* The field classes are already frozen */ |
f6ccaed9 | 405 | BT_ASSERT(event_class); |
44c440bc | 406 | BT_LIB_LOGD("Freezing event class: %!+E", event_class); |
40f4ba76 | 407 | ((struct bt_event_class *) event_class)->frozen = true; |
2a3ced3c | 408 | } |
c5b9b441 PP |
409 | |
410 | void bt_event_class_get_ref(const struct bt_event_class *event_class) | |
411 | { | |
412 | bt_object_get_ref(event_class); | |
413 | } | |
414 | ||
415 | void bt_event_class_put_ref(const struct bt_event_class *event_class) | |
416 | { | |
417 | bt_object_put_ref(event_class); | |
418 | } |