lib: update copyrights
[babeltrace.git] / lib / trace-ir / event-class.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
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 "EVENT-CLASS"
25 #include <babeltrace/lib-logging-internal.h>
26
27 #include <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/trace-ir/clock-value-internal.h>
29 #include <babeltrace/trace-ir/field-internal.h>
30 #include <babeltrace/trace-ir/field-class.h>
31 #include <babeltrace/trace-ir/field-class-internal.h>
32 #include <babeltrace/trace-ir/event-class.h>
33 #include <babeltrace/trace-ir/event-class-const.h>
34 #include <babeltrace/trace-ir/event-class-internal.h>
35 #include <babeltrace/trace-ir/event-internal.h>
36 #include <babeltrace/trace-ir/stream-class.h>
37 #include <babeltrace/trace-ir/stream-class-internal.h>
38 #include <babeltrace/trace-ir/trace-internal.h>
39 #include <babeltrace/trace-ir/utils-internal.h>
40 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
41 #include <babeltrace/object.h>
42 #include <babeltrace/trace-ir/attributes-internal.h>
43 #include <babeltrace/compiler-internal.h>
44 #include <babeltrace/endian-internal.h>
45 #include <babeltrace/types.h>
46 #include <babeltrace/value-internal.h>
47 #include <babeltrace/assert-internal.h>
48 #include <inttypes.h>
49 #include <stdlib.h>
50
51 #define BT_ASSERT_PRE_EVENT_CLASS_HOT(_ec) \
52 BT_ASSERT_PRE_HOT(((const struct bt_event_class *) (_ec)), \
53 "Event class", ": %!+E", (_ec))
54
55 static
56 void destroy_event_class(struct bt_object *obj)
57 {
58 struct bt_event_class *event_class = (void *) obj;
59
60 BT_LIB_LOGD("Destroying event class: %!+E", event_class);
61
62 if (event_class->name.str) {
63 g_string_free(event_class->name.str, TRUE);
64 event_class->name.str = NULL;
65 }
66
67 if (event_class->emf_uri.str) {
68 g_string_free(event_class->emf_uri.str, TRUE);
69 event_class->emf_uri.str = NULL;
70 }
71
72 BT_LOGD_STR("Putting context field class.");
73 BT_OBJECT_PUT_REF_AND_RESET(event_class->specific_context_fc);
74 BT_LOGD_STR("Putting payload field class.");
75 BT_OBJECT_PUT_REF_AND_RESET(event_class->payload_fc);
76 bt_object_pool_finalize(&event_class->event_pool);
77 g_free(obj);
78 }
79
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
87 BT_ASSERT_PRE_FUNC
88 static
89 bool event_class_id_is_unique(const struct bt_stream_class *stream_class,
90 uint64_t id)
91 {
92 uint64_t i;
93 bool is_unique = true;
94
95 for (i = 0; i < stream_class->event_classes->len; i++) {
96 const 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 }
103 }
104
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);
122 event_class = g_new0(struct bt_event_class, 1);
123 if (!event_class) {
124 BT_LOGE_STR("Failed to allocate one event class.");
125 goto error;
126 }
127
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;
145 }
146
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
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);
161 goto end;
162
163 error:
164 BT_OBJECT_PUT_REF_AND_RESET(event_class);
165
166 end:
167 return event_class;
168 }
169
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
190 const char *bt_event_class_get_name(const struct bt_event_class *event_class)
191 {
192 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
193 return event_class->name.value;
194 }
195
196 int bt_event_class_set_name(struct bt_event_class *event_class,
197 const char *name)
198 {
199 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
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;
206 }
207
208 uint64_t bt_event_class_get_id(const struct bt_event_class *event_class)
209 {
210 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
211 return event_class->id;
212 }
213
214 enum bt_property_availability bt_event_class_get_log_level(
215 const struct bt_event_class *event_class,
216 enum bt_event_class_log_level *log_level)
217 {
218 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
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;
223 }
224
225 void bt_event_class_set_log_level(
226 struct bt_event_class *event_class,
227 enum bt_event_class_log_level log_level)
228 {
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);
233 BT_LIB_LOGV("Set event class's log level: %!+E", event_class);
234 }
235
236 const char *bt_event_class_get_emf_uri(const struct bt_event_class *event_class)
237 {
238 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
239 return event_class->emf_uri.value;
240 }
241
242 int bt_event_class_set_emf_uri(
243 struct bt_event_class *event_class,
244 const char *emf_uri)
245 {
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;
251 BT_LIB_LOGV("Set event class's EMF URI: %!+E", event_class);
252 return 0;
253 }
254
255 struct bt_stream_class *bt_event_class_borrow_stream_class(
256 struct bt_event_class *event_class)
257 {
258 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
259 return bt_event_class_borrow_stream_class_inline(event_class);
260 }
261
262 const struct bt_stream_class *
263 bt_event_class_borrow_stream_class_const(
264 const struct bt_event_class *event_class)
265 {
266 return bt_event_class_borrow_stream_class((void *) event_class);
267 }
268
269 const struct bt_field_class *
270 bt_event_class_borrow_specific_context_field_class_const(
271 const struct bt_event_class *event_class)
272 {
273 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
274 return event_class->specific_context_fc;
275 }
276
277 int bt_event_class_set_specific_context_field_class(
278 struct bt_event_class *event_class,
279 struct bt_field_class *field_class)
280 {
281 int ret;
282 struct bt_stream_class *stream_class;
283 struct bt_trace_class *trace_class;
284 struct bt_resolve_field_path_context resolve_ctx = {
285 .packet_header = NULL,
286 .packet_context = NULL,
287 .event_header = NULL,
288 .event_common_context = NULL,
289 .event_specific_context = field_class,
290 .event_payload = NULL,
291 };
292
293 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
294 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
295 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
296 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
297 BT_FIELD_CLASS_TYPE_STRUCTURE,
298 "Specific context field class is not a structure field class: "
299 "%!+F", field_class);
300 stream_class = bt_event_class_borrow_stream_class_inline(
301 event_class);
302 trace_class = bt_stream_class_borrow_trace_class_inline(stream_class);
303 resolve_ctx.packet_header = trace_class->packet_header_fc;
304 resolve_ctx.packet_context = stream_class->packet_context_fc;
305 resolve_ctx.event_header = stream_class->event_header_fc;
306 resolve_ctx.event_common_context =
307 stream_class->event_common_context_fc;
308
309 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
310 if (ret) {
311 goto end;
312 }
313
314 bt_field_class_make_part_of_trace_class(field_class);
315 bt_object_put_ref(event_class->specific_context_fc);
316 event_class->specific_context_fc = field_class;
317 bt_object_get_no_null_check(event_class->specific_context_fc);
318 bt_field_class_freeze(field_class);
319 BT_LIB_LOGV("Set event class's specific context field class: %!+E",
320 event_class);
321
322 end:
323 return ret;
324 }
325
326 const struct bt_field_class *bt_event_class_borrow_payload_field_class_const(
327 const struct bt_event_class *event_class)
328 {
329 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
330 return event_class->payload_fc;
331 }
332
333 int bt_event_class_set_payload_field_class(
334 struct bt_event_class *event_class,
335 struct bt_field_class *field_class)
336 {
337 int ret;
338 struct bt_stream_class *stream_class;
339 struct bt_trace_class *trace_class;
340 struct bt_resolve_field_path_context resolve_ctx = {
341 .packet_header = NULL,
342 .packet_context = NULL,
343 .event_header = NULL,
344 .event_common_context = NULL,
345 .event_specific_context = NULL,
346 .event_payload = field_class,
347 };
348
349 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
350 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
351 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
352 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
353 BT_FIELD_CLASS_TYPE_STRUCTURE,
354 "Payload field class is not a structure field class: %!+F",
355 field_class);
356 stream_class = bt_event_class_borrow_stream_class_inline(
357 event_class);
358 trace_class = bt_stream_class_borrow_trace_class_inline(stream_class);
359 resolve_ctx.packet_header = trace_class->packet_header_fc;
360 resolve_ctx.packet_context = stream_class->packet_context_fc;
361 resolve_ctx.event_header = stream_class->event_header_fc;
362 resolve_ctx.event_common_context =
363 stream_class->event_common_context_fc;
364 resolve_ctx.event_specific_context = event_class->specific_context_fc;
365
366 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
367 if (ret) {
368 goto end;
369 }
370
371 bt_field_class_make_part_of_trace_class(field_class);
372 bt_object_put_ref(event_class->payload_fc);
373 event_class->payload_fc = field_class;
374 bt_object_get_no_null_check(event_class->payload_fc);
375 bt_field_class_freeze(field_class);
376 BT_LIB_LOGV("Set event class's payload field class: %!+E", event_class);
377
378 end:
379 return ret;
380 }
381
382 BT_HIDDEN
383 void _bt_event_class_freeze(const struct bt_event_class *event_class)
384 {
385 /* The field classes are already frozen */
386 BT_ASSERT(event_class);
387 BT_LIB_LOGD("Freezing event class: %!+E", event_class);
388 ((struct bt_event_class *) event_class)->frozen = true;
389 }
This page took 0.037886 seconds and 4 git commands to generate.