Make API CTF-agnostic
[deliverable/babeltrace.git] / lib / ctf-ir / event-class.c
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
29 #define BT_LOG_TAG "EVENT-CLASS"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/ctf-ir/clock-value-internal.h>
34 #include <babeltrace/ctf-ir/fields-internal.h>
35 #include <babeltrace/ctf-ir/field-types.h>
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>
39 #include <babeltrace/ctf-ir/event-internal.h>
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>
43 #include <babeltrace/ctf-ir/utils.h>
44 #include <babeltrace/ctf-ir/utils-internal.h>
45 #include <babeltrace/ctf-ir/resolve-field-path-internal.h>
46 #include <babeltrace/ref.h>
47 #include <babeltrace/ctf-ir/attributes-internal.h>
48 #include <babeltrace/compiler-internal.h>
49 #include <babeltrace/endian-internal.h>
50 #include <babeltrace/types.h>
51 #include <babeltrace/values-internal.h>
52 #include <babeltrace/assert-internal.h>
53 #include <inttypes.h>
54 #include <stdlib.h>
55
56 #define BT_ASSERT_PRE_EVENT_CLASS_HOT(_ec) \
57 BT_ASSERT_PRE_HOT((_ec), "Event class", ": %!+E", (_ec))
58
59 static
60 void destroy_event_class(struct bt_object *obj)
61 {
62 struct bt_event_class *event_class = (void *) obj;
63
64 BT_LIB_LOGD("Destroying event class: %!+E", event_class);
65
66 if (event_class->name.str) {
67 g_string_free(event_class->name.str, TRUE);
68 }
69
70 if (event_class->emf_uri.str) {
71 g_string_free(event_class->emf_uri.str, TRUE);
72 }
73
74 BT_LOGD_STR("Putting context field type.");
75 bt_put(event_class->specific_context_ft);
76 BT_LOGD_STR("Putting payload field type.");
77 bt_put(event_class->payload_ft);
78 bt_object_pool_finalize(&event_class->event_pool);
79 g_free(obj);
80 }
81
82 static
83 void free_event(struct bt_event *event,
84 struct bt_event_class *event_class)
85 {
86 bt_event_destroy(event);
87 }
88
89 BT_ASSERT_PRE_FUNC
90 static
91 bool event_class_id_is_unique(struct bt_stream_class *stream_class, uint64_t id)
92 {
93 uint64_t i;
94 bool is_unique = true;
95
96 for (i = 0; i < stream_class->event_classes->len; i++) {
97 struct bt_event_class *ec =
98 stream_class->event_classes->pdata[i];
99
100 if (ec->id == id) {
101 is_unique = false;
102 goto end;
103 }
104 }
105
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);
123 event_class = g_new0(struct bt_event_class, 1);
124 if (!event_class) {
125 BT_LOGE_STR("Failed to allocate one event class.");
126 goto error;
127 }
128
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;
146 }
147
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
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);
162 goto end;
163
164 error:
165 BT_PUT(event_class);
166
167 end:
168 return event_class;
169 }
170
171 struct bt_event_class *bt_event_class_create(
172 struct bt_stream_class *stream_class)
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);
178 return create_event_class_with_id(stream_class,
179 (uint64_t) stream_class->event_classes->len);
180 }
181
182 struct bt_event_class *bt_event_class_create_with_id(
183 struct bt_stream_class *stream_class, uint64_t id)
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);
188 return create_event_class_with_id(stream_class, id);
189 }
190
191 const char *bt_event_class_get_name(struct bt_event_class *event_class)
192 {
193 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
194 return event_class->name.value;
195 }
196
197 int bt_event_class_set_name(struct bt_event_class *event_class,
198 const char *name)
199 {
200 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
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;
205 BT_LIB_LOGV("Set event class's name: %!+E", event_class);
206 return 0;
207 }
208
209 uint64_t bt_event_class_get_id(struct bt_event_class *event_class)
210 {
211 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
212 return event_class->id;
213 }
214
215 enum bt_property_availability bt_event_class_get_log_level(
216 struct bt_event_class *event_class,
217 enum bt_event_class_log_level *log_level)
218 {
219 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
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;
224 }
225
226 int bt_event_class_set_log_level(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 return 0;
235 }
236
237 const char *bt_event_class_get_emf_uri(struct bt_event_class *event_class)
238 {
239 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
240 return event_class->emf_uri.value;
241 }
242
243 int bt_event_class_set_emf_uri(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 struct bt_field_type *bt_event_class_borrow_specific_context_field_type(
263 struct bt_event_class *event_class)
264 {
265 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
266 return event_class->specific_context_ft;
267 }
268
269 int bt_event_class_set_specific_context_field_type(
270 struct bt_event_class *event_class,
271 struct bt_field_type *field_type)
272 {
273 int ret;
274 struct bt_stream_class *stream_class;
275 struct bt_trace *trace;
276 struct bt_resolve_field_path_context resolve_ctx = {
277 .packet_header = NULL,
278 .packet_context = NULL,
279 .event_header = NULL,
280 .event_common_context = NULL,
281 .event_specific_context = field_type,
282 .event_payload = NULL,
283 };
284
285 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
286 BT_ASSERT_PRE_NON_NULL(field_type, "Field type");
287 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
288 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type) ==
289 BT_FIELD_TYPE_ID_STRUCTURE,
290 "Specific context field type is not a structure field type: "
291 "%!+F", field_type);
292 stream_class = bt_event_class_borrow_stream_class_inline(
293 event_class);
294 trace = bt_stream_class_borrow_trace_inline(stream_class);
295 resolve_ctx.packet_header = trace->packet_header_ft;
296 resolve_ctx.packet_context = stream_class->packet_context_ft;
297 resolve_ctx.event_header = stream_class->event_header_ft;
298 resolve_ctx.event_common_context =
299 stream_class->event_common_context_ft;
300
301 ret = bt_resolve_field_paths(field_type, &resolve_ctx);
302 if (ret) {
303 goto end;
304 }
305
306 bt_field_type_make_part_of_trace(field_type);
307 bt_put(event_class->specific_context_ft);
308 event_class->specific_context_ft = bt_get(field_type);
309 bt_field_type_freeze(field_type);
310 BT_LIB_LOGV("Set event class's specific context field type: %!+E",
311 event_class);
312
313 end:
314 return ret;
315 }
316
317 struct bt_field_type *bt_event_class_borrow_payload_field_type(
318 struct bt_event_class *event_class)
319 {
320 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
321 return event_class->payload_ft;
322 }
323
324 int bt_event_class_set_payload_field_type(struct bt_event_class *event_class,
325 struct bt_field_type *field_type)
326 {
327 int ret;
328 struct bt_stream_class *stream_class;
329 struct bt_trace *trace;
330 struct bt_resolve_field_path_context resolve_ctx = {
331 .packet_header = NULL,
332 .packet_context = NULL,
333 .event_header = NULL,
334 .event_common_context = NULL,
335 .event_specific_context = NULL,
336 .event_payload = field_type,
337 };
338
339 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
340 BT_ASSERT_PRE_NON_NULL(field_type, "Field type");
341 BT_ASSERT_PRE_EVENT_CLASS_HOT(event_class);
342 BT_ASSERT_PRE(bt_field_type_get_type_id(field_type) ==
343 BT_FIELD_TYPE_ID_STRUCTURE,
344 "Payload field type is not a structure field type: %!+F",
345 field_type);
346 stream_class = bt_event_class_borrow_stream_class_inline(
347 event_class);
348 trace = bt_stream_class_borrow_trace_inline(stream_class);
349 resolve_ctx.packet_header = trace->packet_header_ft;
350 resolve_ctx.packet_context = stream_class->packet_context_ft;
351 resolve_ctx.event_header = stream_class->event_header_ft;
352 resolve_ctx.event_common_context =
353 stream_class->event_common_context_ft;
354 resolve_ctx.event_specific_context = event_class->specific_context_ft;
355
356 ret = bt_resolve_field_paths(field_type, &resolve_ctx);
357 if (ret) {
358 goto end;
359 }
360
361 bt_field_type_make_part_of_trace(field_type);
362 bt_put(event_class->payload_ft);
363 event_class->payload_ft = bt_get(field_type);
364 bt_field_type_freeze(field_type);
365 BT_LIB_LOGV("Set event class's payload field type: %!+E", event_class);
366
367 end:
368 return ret;
369 }
370
371 BT_HIDDEN
372 void _bt_event_class_freeze(struct bt_event_class *event_class)
373 {
374 /* The field types are already frozen */
375 BT_ASSERT(event_class);
376 BT_LIB_LOGD("Freezing event class: %!+E", event_class);
377 event_class->frozen = true;
378 }
This page took 0.038842 seconds and 5 git commands to generate.