Commit | Line | Data |
---|---|---|
272df73e PP |
1 | /* |
2 | * event-class.c | |
3 | * | |
4 | * Babeltrace CTF IR - Event class | |
5 | * | |
6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
f8b979f9 PP |
29 | #define BT_LOG_TAG "EVENT-CLASS" |
30 | #include <babeltrace/lib-logging-internal.h> | |
31 | ||
3dca2276 | 32 | #include <babeltrace/assert-pre-internal.h> |
312c056a | 33 | #include <babeltrace/ctf-ir/clock-value-internal.h> |
272df73e | 34 | #include <babeltrace/ctf-ir/fields-internal.h> |
44c440bc | 35 | #include <babeltrace/ctf-ir/field-types.h> |
272df73e PP |
36 | #include <babeltrace/ctf-ir/field-types-internal.h> |
37 | #include <babeltrace/ctf-ir/event-class.h> | |
38 | #include <babeltrace/ctf-ir/event-class-internal.h> | |
312c056a | 39 | #include <babeltrace/ctf-ir/event-internal.h> |
272df73e PP |
40 | #include <babeltrace/ctf-ir/stream-class.h> |
41 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
42 | #include <babeltrace/ctf-ir/trace-internal.h> | |
2a3ced3c | 43 | #include <babeltrace/ctf-ir/utils-internal.h> |
44c440bc | 44 | #include <babeltrace/ctf-ir/resolve-field-path-internal.h> |
272df73e PP |
45 | #include <babeltrace/ref.h> |
46 | #include <babeltrace/ctf-ir/attributes-internal.h> | |
3d9990ac PP |
47 | #include <babeltrace/compiler-internal.h> |
48 | #include <babeltrace/endian-internal.h> | |
c55a9f58 | 49 | #include <babeltrace/types.h> |
f8b979f9 | 50 | #include <babeltrace/values-internal.h> |
f6ccaed9 | 51 | #include <babeltrace/assert-internal.h> |
dc3fffef | 52 | #include <inttypes.h> |
0fbb9a9f | 53 | #include <stdlib.h> |
272df73e | 54 | |
44c440bc PP |
55 | #define BT_ASSERT_PRE_EVENT_CLASS_HOT(_ec) \ |
56 | BT_ASSERT_PRE_HOT((_ec), "Event class", ": %!+E", (_ec)) | |
57 | ||
58 | static | |
59 | void destroy_event_class(struct bt_object *obj) | |
272df73e | 60 | { |
44c440bc | 61 | struct bt_event_class *event_class = (void *) obj; |
272df73e | 62 | |
44c440bc | 63 | BT_LIB_LOGD("Destroying event class: %!+E", event_class); |
f8b979f9 | 64 | |
44c440bc PP |
65 | if (event_class->name.str) { |
66 | g_string_free(event_class->name.str, TRUE); | |
cf76ce92 PP |
67 | } |
68 | ||
44c440bc PP |
69 | if (event_class->emf_uri.str) { |
70 | g_string_free(event_class->emf_uri.str, TRUE); | |
272df73e PP |
71 | } |
72 | ||
3dca2276 | 73 | BT_LOGD_STR("Putting context field type."); |
44c440bc | 74 | bt_put(event_class->specific_context_ft); |
3dca2276 | 75 | BT_LOGD_STR("Putting payload field type."); |
44c440bc | 76 | bt_put(event_class->payload_ft); |
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 | |
90 | bool event_class_id_is_unique(struct bt_stream_class *stream_class, uint64_t id) | |
272df73e | 91 | { |
44c440bc PP |
92 | uint64_t i; |
93 | bool is_unique = true; | |
272df73e | 94 | |
44c440bc PP |
95 | for (i = 0; i < stream_class->event_classes->len; i++) { |
96 | struct bt_event_class *ec = | |
97 | stream_class->event_classes->pdata[i]; | |
98 | ||
99 | if (ec->id == id) { | |
100 | is_unique = false; | |
101 | goto end; | |
102 | } | |
cf76ce92 | 103 | } |
272df73e | 104 | |
44c440bc 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); | |
3dca2276 | 122 | event_class = g_new0(struct bt_event_class, 1); |
272df73e | 123 | if (!event_class) { |
3dca2276 PP |
124 | BT_LOGE_STR("Failed to allocate one event class."); |
125 | goto error; | |
272df73e PP |
126 | } |
127 | ||
44c440bc 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 | ||
312c056a 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 | ||
44c440bc 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); | |
3dca2276 | 161 | goto end; |
272df73e | 162 | |
3dca2276 | 163 | error: |
44c440bc | 164 | BT_PUT(event_class); |
272df73e PP |
165 | |
166 | end: | |
3dca2276 | 167 | return event_class; |
272df73e PP |
168 | } |
169 | ||
44c440bc PP |
170 | struct bt_event_class *bt_event_class_create( |
171 | struct bt_stream_class *stream_class) | |
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); | |
177 | return create_event_class_with_id(stream_class, | |
178 | (uint64_t) stream_class->event_classes->len); | |
179 | } | |
180 | ||
181 | struct bt_event_class *bt_event_class_create_with_id( | |
182 | struct bt_stream_class *stream_class, uint64_t id) | |
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); | |
187 | return create_event_class_with_id(stream_class, id); | |
188 | } | |
189 | ||
3dca2276 | 190 | const char *bt_event_class_get_name(struct bt_event_class *event_class) |
272df73e | 191 | { |
cb6f1f7d | 192 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc | 193 | return event_class->name.value; |
272df73e PP |
194 | } |
195 | ||
44c440bc PP |
196 | int bt_event_class_set_name(struct bt_event_class *event_class, |
197 | const char *name) | |
272df73e | 198 | { |
cb6f1f7d | 199 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc 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; | |
204 | BT_LIB_LOGV("Set event class's name: %!+E", event_class); | |
205 | return 0; | |
272df73e PP |
206 | } |
207 | ||
44c440bc | 208 | uint64_t bt_event_class_get_id(struct bt_event_class *event_class) |
272df73e | 209 | { |
44c440bc PP |
210 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
211 | return event_class->id; | |
272df73e PP |
212 | } |
213 | ||
44c440bc PP |
214 | enum bt_property_availability bt_event_class_get_log_level( |
215 | struct bt_event_class *event_class, | |
216 | enum bt_event_class_log_level *log_level) | |
272df73e | 217 | { |
cb6f1f7d | 218 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc 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 | ||
3dca2276 PP |
225 | int bt_event_class_set_log_level(struct bt_event_class *event_class, |
226 | enum bt_event_class_log_level log_level) | |
272df73e | 227 | { |
44c440bc PP |
228 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
229 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); | |
230 | bt_property_uint_set(&event_class->log_level, | |
231 | (uint64_t) log_level); | |
232 | BT_LIB_LOGV("Set event class's log level: %!+E", event_class); | |
233 | return 0; | |
272df73e PP |
234 | } |
235 | ||
3dca2276 | 236 | const char *bt_event_class_get_emf_uri(struct bt_event_class *event_class) |
272df73e | 237 | { |
cb6f1f7d | 238 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc | 239 | return event_class->emf_uri.value; |
272df73e PP |
240 | } |
241 | ||
3dca2276 PP |
242 | int bt_event_class_set_emf_uri(struct bt_event_class *event_class, |
243 | const char *emf_uri) | |
272df73e | 244 | { |
44c440bc PP |
245 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
246 | BT_ASSERT_PRE_NON_NULL(emf_uri, "EMF URI"); | |
247 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); | |
248 | g_string_assign(event_class->emf_uri.str, emf_uri); | |
249 | event_class->emf_uri.value = event_class->emf_uri.str->str; | |
250 | BT_LIB_LOGV("Set event class's EMF URI: %!+E", event_class); | |
251 | return 0; | |
272df73e PP |
252 | } |
253 | ||
094ff7c0 | 254 | struct bt_stream_class *bt_event_class_borrow_stream_class( |
50842bdc | 255 | struct bt_event_class *event_class) |
272df73e | 256 | { |
d975f66c | 257 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc | 258 | return bt_event_class_borrow_stream_class_inline(event_class); |
272df73e PP |
259 | } |
260 | ||
44c440bc | 261 | struct bt_field_type *bt_event_class_borrow_specific_context_field_type( |
3dca2276 | 262 | struct bt_event_class *event_class) |
272df73e | 263 | { |
cb6f1f7d | 264 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc | 265 | return event_class->specific_context_ft; |
272df73e PP |
266 | } |
267 | ||
44c440bc PP |
268 | int bt_event_class_set_specific_context_field_type( |
269 | struct bt_event_class *event_class, | |
3dca2276 | 270 | struct bt_field_type *field_type) |
272df73e | 271 | { |
44c440bc PP |
272 | int ret; |
273 | struct bt_stream_class *stream_class; | |
274 | struct bt_trace *trace; | |
275 | struct bt_resolve_field_path_context resolve_ctx = { | |
276 | .packet_header = NULL, | |
277 | .packet_context = NULL, | |
278 | .event_header = NULL, | |
279 | .event_common_context = NULL, | |
280 | .event_specific_context = field_type, | |
281 | .event_payload = NULL, | |
282 | }; | |
cb6f1f7d | 283 | |
44c440bc PP |
284 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
285 | BT_ASSERT_PRE_NON_NULL(field_type, "Field type"); | |
286 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); | |
287 | BT_ASSERT_PRE(bt_field_type_get_type_id(field_type) == | |
288 | BT_FIELD_TYPE_ID_STRUCTURE, | |
289 | "Specific context field type is not a structure field type: " | |
290 | "%!+F", field_type); | |
291 | stream_class = bt_event_class_borrow_stream_class_inline( | |
292 | event_class); | |
293 | trace = bt_stream_class_borrow_trace_inline(stream_class); | |
294 | resolve_ctx.packet_header = trace->packet_header_ft; | |
295 | resolve_ctx.packet_context = stream_class->packet_context_ft; | |
296 | resolve_ctx.event_header = stream_class->event_header_ft; | |
297 | resolve_ctx.event_common_context = | |
298 | stream_class->event_common_context_ft; | |
299 | ||
300 | ret = bt_resolve_field_paths(field_type, &resolve_ctx); | |
301 | if (ret) { | |
cb6f1f7d PP |
302 | goto end; |
303 | } | |
304 | ||
44c440bc PP |
305 | bt_field_type_make_part_of_trace(field_type); |
306 | bt_put(event_class->specific_context_ft); | |
307 | event_class->specific_context_ft = bt_get(field_type); | |
308 | bt_field_type_freeze(field_type); | |
309 | BT_LIB_LOGV("Set event class's specific context field type: %!+E", | |
310 | event_class); | |
cb6f1f7d | 311 | |
cb6f1f7d PP |
312 | end: |
313 | return ret; | |
272df73e PP |
314 | } |
315 | ||
44c440bc | 316 | struct bt_field_type *bt_event_class_borrow_payload_field_type( |
3dca2276 | 317 | struct bt_event_class *event_class) |
272df73e | 318 | { |
cb6f1f7d | 319 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
44c440bc | 320 | return event_class->payload_ft; |
272df73e PP |
321 | } |
322 | ||
44c440bc | 323 | int bt_event_class_set_payload_field_type(struct bt_event_class *event_class, |
3dca2276 | 324 | struct bt_field_type *field_type) |
272df73e | 325 | { |
44c440bc PP |
326 | int ret; |
327 | struct bt_stream_class *stream_class; | |
328 | struct bt_trace *trace; | |
329 | struct bt_resolve_field_path_context resolve_ctx = { | |
330 | .packet_header = NULL, | |
331 | .packet_context = NULL, | |
332 | .event_header = NULL, | |
333 | .event_common_context = NULL, | |
334 | .event_specific_context = NULL, | |
335 | .event_payload = field_type, | |
336 | }; | |
cb6f1f7d | 337 | |
44c440bc PP |
338 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); |
339 | BT_ASSERT_PRE_NON_NULL(field_type, "Field type"); | |
340 | BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class); | |
341 | BT_ASSERT_PRE(bt_field_type_get_type_id(field_type) == | |
342 | BT_FIELD_TYPE_ID_STRUCTURE, | |
343 | "Payload field type is not a structure field type: %!+F", | |
344 | field_type); | |
345 | stream_class = bt_event_class_borrow_stream_class_inline( | |
346 | event_class); | |
347 | trace = bt_stream_class_borrow_trace_inline(stream_class); | |
348 | resolve_ctx.packet_header = trace->packet_header_ft; | |
349 | resolve_ctx.packet_context = stream_class->packet_context_ft; | |
350 | resolve_ctx.event_header = stream_class->event_header_ft; | |
351 | resolve_ctx.event_common_context = | |
352 | stream_class->event_common_context_ft; | |
353 | resolve_ctx.event_specific_context = event_class->specific_context_ft; | |
354 | ||
355 | ret = bt_resolve_field_paths(field_type, &resolve_ctx); | |
356 | if (ret) { | |
cb6f1f7d PP |
357 | goto end; |
358 | } | |
359 | ||
44c440bc PP |
360 | bt_field_type_make_part_of_trace(field_type); |
361 | bt_put(event_class->payload_ft); | |
362 | event_class->payload_ft = bt_get(field_type); | |
363 | bt_field_type_freeze(field_type); | |
364 | BT_LIB_LOGV("Set event class's payload field type: %!+E", event_class); | |
cb6f1f7d PP |
365 | |
366 | end: | |
367 | return ret; | |
272df73e PP |
368 | } |
369 | ||
370 | BT_HIDDEN | |
44c440bc | 371 | void _bt_event_class_freeze(struct bt_event_class *event_class) |
2a3ced3c | 372 | { |
44c440bc | 373 | /* The field types are already frozen */ |
f6ccaed9 | 374 | BT_ASSERT(event_class); |
44c440bc PP |
375 | BT_LIB_LOGD("Freezing event class: %!+E", event_class); |
376 | event_class->frozen = true; | |
2a3ced3c | 377 | } |