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 | ||
29 | #include <babeltrace/ctf-ir/fields-internal.h> | |
30 | #include <babeltrace/ctf-ir/field-types-internal.h> | |
31 | #include <babeltrace/ctf-ir/event-class.h> | |
32 | #include <babeltrace/ctf-ir/event-class-internal.h> | |
33 | #include <babeltrace/ctf-ir/stream-class.h> | |
34 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
35 | #include <babeltrace/ctf-ir/trace-internal.h> | |
36 | #include <babeltrace/ctf-ir/validation-internal.h> | |
37 | #include <babeltrace/ctf-ir/utils.h> | |
38 | #include <babeltrace/ref.h> | |
39 | #include <babeltrace/ctf-ir/attributes-internal.h> | |
40 | #include <babeltrace/compiler.h> | |
6d9fe143 | 41 | #include <babeltrace/endian.h> |
272df73e PP |
42 | |
43 | static | |
44 | void bt_ctf_event_class_destroy(struct bt_object *obj); | |
45 | ||
46 | struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name) | |
47 | { | |
48 | int ret; | |
49 | struct bt_value *obj = NULL; | |
50 | struct bt_ctf_event_class *event_class = NULL; | |
51 | ||
52 | if (bt_ctf_validate_identifier(name)) { | |
53 | goto error; | |
54 | } | |
55 | ||
56 | event_class = g_new0(struct bt_ctf_event_class, 1); | |
57 | if (!event_class) { | |
58 | goto error; | |
59 | } | |
60 | ||
5990dd44 | 61 | event_class->id = -1; |
272df73e PP |
62 | bt_object_init(event_class, bt_ctf_event_class_destroy); |
63 | event_class->fields = bt_ctf_field_type_structure_create(); | |
64 | if (!event_class->fields) { | |
65 | goto error; | |
66 | } | |
67 | ||
68 | event_class->attributes = bt_ctf_attributes_create(); | |
69 | if (!event_class->attributes) { | |
70 | goto error; | |
71 | } | |
72 | ||
73 | obj = bt_value_integer_create_init(-1); | |
74 | if (!obj) { | |
75 | goto error; | |
76 | } | |
77 | ||
78 | ret = bt_ctf_attributes_set_field_value(event_class->attributes, | |
79 | "id", obj); | |
80 | if (ret) { | |
81 | goto error; | |
82 | } | |
83 | ||
84 | BT_PUT(obj); | |
85 | ||
86 | obj = bt_value_string_create_init(name); | |
87 | if (!obj) { | |
88 | goto error; | |
89 | } | |
90 | ||
91 | ret = bt_ctf_attributes_set_field_value(event_class->attributes, | |
92 | "name", obj); | |
93 | if (ret) { | |
94 | goto error; | |
95 | } | |
96 | ||
97 | BT_PUT(obj); | |
98 | ||
99 | return event_class; | |
100 | ||
101 | error: | |
102 | BT_PUT(event_class); | |
103 | BT_PUT(obj); | |
104 | return event_class; | |
105 | } | |
106 | ||
107 | const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class) | |
108 | { | |
109 | struct bt_value *obj = NULL; | |
110 | const char *name = NULL; | |
111 | ||
112 | if (!event_class) { | |
113 | goto end; | |
114 | } | |
115 | ||
5990dd44 JG |
116 | if (event_class->name) { |
117 | name = event_class->name; | |
118 | goto end; | |
119 | } | |
120 | ||
272df73e PP |
121 | obj = bt_ctf_attributes_get_field_value(event_class->attributes, |
122 | BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX); | |
123 | if (!obj) { | |
124 | goto end; | |
125 | } | |
126 | ||
127 | if (bt_value_string_get(obj, &name)) { | |
128 | name = NULL; | |
129 | } | |
130 | ||
131 | end: | |
132 | BT_PUT(obj); | |
133 | return name; | |
134 | } | |
135 | ||
136 | int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class) | |
137 | { | |
138 | struct bt_value *obj = NULL; | |
139 | int64_t ret = 0; | |
140 | ||
141 | if (!event_class) { | |
142 | ret = -1; | |
143 | goto end; | |
144 | } | |
145 | ||
5990dd44 JG |
146 | if (event_class->id >= 0) { |
147 | ret = event_class->id; | |
148 | goto end; | |
149 | } | |
150 | ||
272df73e PP |
151 | obj = bt_ctf_attributes_get_field_value(event_class->attributes, |
152 | BT_CTF_EVENT_CLASS_ATTR_ID_INDEX); | |
153 | if (!obj) { | |
154 | goto end; | |
155 | } | |
156 | ||
157 | if (bt_value_integer_get(obj, &ret)) { | |
158 | ret = -1; | |
159 | } | |
160 | ||
161 | if (ret < 0) { | |
162 | /* means ID is not set */ | |
163 | ret = -1; | |
164 | goto end; | |
165 | } | |
166 | ||
167 | end: | |
168 | BT_PUT(obj); | |
169 | return ret; | |
170 | } | |
171 | ||
172 | int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class, | |
173 | uint32_t id) | |
174 | { | |
175 | int ret = 0; | |
176 | struct bt_value *obj = NULL; | |
177 | struct bt_ctf_stream_class *stream_class = NULL; | |
178 | ||
179 | ||
180 | if (!event_class) { | |
181 | ret = -1; | |
182 | goto end; | |
183 | } | |
184 | ||
185 | stream_class = bt_ctf_event_class_get_stream_class(event_class); | |
186 | if (stream_class) { | |
187 | /* | |
188 | * We don't allow changing the id if the event class has already | |
189 | * been added to a stream class. | |
190 | */ | |
191 | ret = -1; | |
192 | goto end; | |
193 | } | |
194 | ||
195 | obj = bt_ctf_attributes_get_field_value(event_class->attributes, | |
196 | BT_CTF_EVENT_CLASS_ATTR_ID_INDEX); | |
197 | if (!obj) { | |
198 | goto end; | |
199 | } | |
200 | ||
201 | if (bt_value_integer_set(obj, id)) { | |
202 | ret = -1; | |
203 | goto end; | |
204 | } | |
205 | ||
206 | end: | |
207 | BT_PUT(obj); | |
208 | BT_PUT(stream_class); | |
209 | return ret; | |
210 | } | |
211 | ||
212 | int bt_ctf_event_class_set_attribute( | |
213 | struct bt_ctf_event_class *event_class, const char *name, | |
214 | struct bt_value *value) | |
215 | { | |
216 | int ret = 0; | |
217 | ||
218 | if (!event_class || !name || !value || event_class->frozen) { | |
219 | ret = -1; | |
220 | goto end; | |
221 | } | |
222 | ||
2073703a JG |
223 | if (!strcmp(name, "id") || !strcmp(name, "loglevel") || |
224 | !strcmp(name, "stream_id")) { | |
272df73e PP |
225 | if (!bt_value_is_integer(value)) { |
226 | ret = -1; | |
227 | goto end; | |
228 | } | |
229 | } else if (!strcmp(name, "name") || !strcmp(name, "model.emf.uri")) { | |
230 | if (!bt_value_is_string(value)) { | |
231 | ret = -1; | |
232 | goto end; | |
233 | } | |
234 | } else { | |
235 | /* unknown attribute */ | |
236 | ret = -1; | |
237 | goto end; | |
238 | } | |
239 | ||
240 | /* "id" special case: >= 0 */ | |
241 | if (!strcmp(name, "id")) { | |
242 | int64_t val; | |
243 | ||
244 | ret = bt_value_integer_get(value, &val); | |
245 | ||
246 | if (ret) { | |
247 | goto end; | |
248 | } | |
249 | ||
250 | if (val < 0) { | |
251 | ret = -1; | |
252 | goto end; | |
253 | } | |
254 | } | |
255 | ||
256 | ret = bt_ctf_attributes_set_field_value(event_class->attributes, | |
2073703a | 257 | name, value); |
272df73e PP |
258 | |
259 | end: | |
260 | return ret; | |
261 | } | |
262 | ||
263 | int bt_ctf_event_class_get_attribute_count( | |
264 | struct bt_ctf_event_class *event_class) | |
265 | { | |
266 | int ret = 0; | |
267 | ||
268 | if (!event_class) { | |
269 | ret = -1; | |
270 | goto end; | |
271 | } | |
272 | ||
273 | ret = bt_ctf_attributes_get_count(event_class->attributes); | |
274 | ||
275 | end: | |
276 | return ret; | |
277 | } | |
278 | ||
279 | const char * | |
280 | bt_ctf_event_class_get_attribute_name( | |
281 | struct bt_ctf_event_class *event_class, int index) | |
282 | { | |
283 | const char *ret; | |
284 | ||
285 | if (!event_class) { | |
286 | ret = NULL; | |
287 | goto end; | |
288 | } | |
289 | ||
290 | ret = bt_ctf_attributes_get_field_name(event_class->attributes, index); | |
291 | ||
292 | end: | |
293 | return ret; | |
294 | } | |
295 | ||
296 | struct bt_value * | |
297 | bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class *event_class, | |
298 | int index) | |
299 | { | |
300 | struct bt_value *ret; | |
301 | ||
302 | if (!event_class) { | |
303 | ret = NULL; | |
304 | goto end; | |
305 | } | |
306 | ||
307 | ret = bt_ctf_attributes_get_field_value(event_class->attributes, index); | |
308 | ||
309 | end: | |
310 | return ret; | |
311 | } | |
312 | ||
313 | struct bt_value * | |
314 | bt_ctf_event_class_get_attribute_value_by_name( | |
315 | struct bt_ctf_event_class *event_class, const char *name) | |
316 | { | |
317 | struct bt_value *ret; | |
318 | ||
319 | if (!event_class || !name) { | |
320 | ret = NULL; | |
321 | goto end; | |
322 | } | |
323 | ||
324 | ret = bt_ctf_attributes_get_field_value_by_name(event_class->attributes, | |
325 | name); | |
326 | ||
327 | end: | |
328 | return ret; | |
329 | ||
330 | } | |
331 | ||
332 | struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class( | |
333 | struct bt_ctf_event_class *event_class) | |
334 | { | |
335 | return (struct bt_ctf_stream_class *) bt_object_get_parent(event_class); | |
336 | } | |
337 | ||
338 | struct bt_ctf_field_type *bt_ctf_event_class_get_payload_type( | |
339 | struct bt_ctf_event_class *event_class) | |
340 | { | |
341 | struct bt_ctf_field_type *payload = NULL; | |
342 | ||
343 | if (!event_class) { | |
344 | goto end; | |
345 | } | |
346 | ||
347 | bt_get(event_class->fields); | |
348 | payload = event_class->fields; | |
349 | end: | |
350 | return payload; | |
351 | } | |
352 | ||
353 | int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class *event_class, | |
354 | struct bt_ctf_field_type *payload) | |
355 | { | |
356 | int ret = 0; | |
357 | ||
358 | if (!event_class || !payload || | |
359 | bt_ctf_field_type_get_type_id(payload) != | |
360 | BT_CTF_TYPE_ID_STRUCT) { | |
361 | ret = -1; | |
362 | goto end; | |
363 | } | |
364 | ||
365 | bt_get(payload); | |
366 | bt_put(event_class->fields); | |
367 | event_class->fields = payload; | |
368 | end: | |
369 | return ret; | |
370 | } | |
371 | ||
372 | int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, | |
373 | struct bt_ctf_field_type *type, | |
374 | const char *name) | |
375 | { | |
376 | int ret = 0; | |
377 | ||
378 | if (!event_class || !type || bt_ctf_validate_identifier(name) || | |
379 | event_class->frozen) { | |
380 | ret = -1; | |
381 | goto end; | |
382 | } | |
383 | ||
384 | if (bt_ctf_field_type_get_type_id(event_class->fields) != | |
385 | BT_CTF_TYPE_ID_STRUCT) { | |
386 | ret = -1; | |
387 | goto end; | |
388 | } | |
389 | ||
390 | ret = bt_ctf_field_type_structure_add_field(event_class->fields, | |
391 | type, name); | |
392 | end: | |
393 | return ret; | |
394 | } | |
395 | ||
396 | int bt_ctf_event_class_get_field_count( | |
397 | struct bt_ctf_event_class *event_class) | |
398 | { | |
399 | int ret; | |
400 | ||
401 | if (!event_class) { | |
402 | ret = -1; | |
403 | goto end; | |
404 | } | |
405 | ||
406 | if (bt_ctf_field_type_get_type_id(event_class->fields) != | |
407 | BT_CTF_TYPE_ID_STRUCT) { | |
408 | ret = -1; | |
409 | goto end; | |
410 | } | |
411 | ||
412 | ret = bt_ctf_field_type_structure_get_field_count(event_class->fields); | |
413 | end: | |
414 | return ret; | |
415 | } | |
416 | ||
417 | int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class, | |
418 | const char **field_name, struct bt_ctf_field_type **field_type, | |
419 | int index) | |
420 | { | |
421 | int ret; | |
422 | ||
423 | if (!event_class || index < 0) { | |
424 | ret = -1; | |
425 | goto end; | |
426 | } | |
427 | ||
428 | if (bt_ctf_field_type_get_type_id(event_class->fields) != | |
429 | BT_CTF_TYPE_ID_STRUCT) { | |
430 | ret = -1; | |
431 | goto end; | |
432 | } | |
433 | ||
434 | ret = bt_ctf_field_type_structure_get_field(event_class->fields, | |
435 | field_name, field_type, index); | |
436 | end: | |
437 | return ret; | |
438 | } | |
439 | ||
440 | struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name( | |
441 | struct bt_ctf_event_class *event_class, const char *name) | |
442 | { | |
443 | GQuark name_quark; | |
444 | struct bt_ctf_field_type *field_type = NULL; | |
445 | ||
446 | if (!event_class || !name) { | |
447 | goto end; | |
448 | } | |
449 | ||
450 | if (bt_ctf_field_type_get_type_id(event_class->fields) != | |
451 | BT_CTF_TYPE_ID_STRUCT) { | |
452 | goto end; | |
453 | } | |
454 | ||
455 | name_quark = g_quark_try_string(name); | |
456 | if (!name_quark) { | |
457 | goto end; | |
458 | } | |
459 | ||
460 | /* | |
461 | * No need to increment field_type's reference count since getting it | |
462 | * from the structure already does. | |
463 | */ | |
464 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
465 | event_class->fields, name); | |
466 | end: | |
467 | return field_type; | |
468 | } | |
469 | ||
470 | struct bt_ctf_field_type *bt_ctf_event_class_get_context_type( | |
471 | struct bt_ctf_event_class *event_class) | |
472 | { | |
473 | struct bt_ctf_field_type *context_type = NULL; | |
474 | ||
475 | if (!event_class || !event_class->context) { | |
476 | goto end; | |
477 | } | |
478 | ||
479 | bt_get(event_class->context); | |
480 | context_type = event_class->context; | |
481 | end: | |
482 | return context_type; | |
483 | } | |
484 | ||
485 | int bt_ctf_event_class_set_context_type( | |
486 | struct bt_ctf_event_class *event_class, | |
487 | struct bt_ctf_field_type *context) | |
488 | { | |
489 | int ret = 0; | |
490 | ||
491 | if (!event_class || !context || event_class->frozen) { | |
492 | ret = -1; | |
493 | goto end; | |
494 | } | |
495 | ||
496 | if (bt_ctf_field_type_get_type_id(context) != BT_CTF_TYPE_ID_STRUCT) { | |
497 | ret = -1; | |
498 | goto end; | |
499 | } | |
500 | ||
501 | bt_get(context); | |
502 | bt_put(event_class->context); | |
503 | event_class->context = context; | |
504 | end: | |
505 | return ret; | |
506 | ||
507 | } | |
508 | ||
509 | void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class) | |
510 | { | |
511 | bt_get(event_class); | |
512 | } | |
513 | ||
514 | void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class) | |
515 | { | |
516 | bt_put(event_class); | |
517 | } | |
518 | ||
519 | BT_HIDDEN | |
520 | int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class, | |
521 | uint32_t stream_id) | |
522 | { | |
523 | int ret = 0; | |
524 | struct bt_value *obj; | |
525 | ||
526 | obj = bt_value_integer_create_init(stream_id); | |
527 | ||
528 | if (!obj) { | |
529 | ret = -1; | |
530 | goto end; | |
531 | } | |
532 | ||
533 | ret = bt_ctf_attributes_set_field_value(event_class->attributes, | |
534 | "stream_id", obj); | |
535 | ||
536 | if (event_class->frozen) { | |
537 | bt_ctf_attributes_freeze(event_class->attributes); | |
538 | } | |
539 | ||
540 | end: | |
541 | BT_PUT(obj); | |
542 | return ret; | |
543 | } | |
544 | ||
545 | static | |
546 | void bt_ctf_event_class_destroy(struct bt_object *obj) | |
547 | { | |
548 | struct bt_ctf_event_class *event_class; | |
549 | ||
550 | event_class = container_of(obj, struct bt_ctf_event_class, base); | |
551 | bt_ctf_attributes_destroy(event_class->attributes); | |
552 | bt_put(event_class->context); | |
553 | bt_put(event_class->fields); | |
554 | g_free(event_class); | |
555 | } | |
556 | ||
557 | BT_HIDDEN | |
558 | void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class) | |
559 | { | |
560 | assert(event_class); | |
561 | event_class->frozen = 1; | |
5990dd44 JG |
562 | event_class->name = bt_ctf_event_class_get_name(event_class); |
563 | event_class->id = bt_ctf_event_class_get_id(event_class); | |
272df73e PP |
564 | bt_ctf_field_type_freeze(event_class->context); |
565 | bt_ctf_field_type_freeze(event_class->fields); | |
566 | bt_ctf_attributes_freeze(event_class->attributes); | |
567 | } | |
568 | ||
569 | BT_HIDDEN | |
570 | int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class, | |
571 | struct metadata_context *context) | |
572 | { | |
573 | int i; | |
574 | int count; | |
575 | int ret = 0; | |
576 | struct bt_value *attr_value = NULL; | |
577 | ||
578 | assert(event_class); | |
579 | assert(context); | |
580 | ||
581 | context->current_indentation_level = 1; | |
582 | g_string_assign(context->field_name, ""); | |
583 | g_string_append(context->string, "event {\n"); | |
584 | count = bt_ctf_event_class_get_attribute_count(event_class); | |
585 | ||
586 | if (count < 0) { | |
587 | ret = -1; | |
588 | goto end; | |
589 | } | |
590 | ||
591 | for (i = 0; i < count; ++i) { | |
592 | const char *attr_name = NULL; | |
593 | ||
594 | attr_name = bt_ctf_event_class_get_attribute_name( | |
595 | event_class, i); | |
596 | attr_value = bt_ctf_event_class_get_attribute_value( | |
597 | event_class, i); | |
598 | ||
599 | if (!attr_name || !attr_value) { | |
600 | ret = -1; | |
601 | goto end; | |
602 | } | |
603 | ||
604 | switch (bt_value_get_type(attr_value)) { | |
605 | case BT_VALUE_TYPE_INTEGER: | |
606 | { | |
607 | int64_t value; | |
608 | ||
609 | ret = bt_value_integer_get(attr_value, &value); | |
610 | ||
611 | if (ret) { | |
612 | goto end; | |
613 | } | |
614 | ||
615 | g_string_append_printf(context->string, | |
616 | "\t%s = %" PRId64 ";\n", attr_name, value); | |
617 | break; | |
618 | } | |
619 | ||
620 | case BT_VALUE_TYPE_STRING: | |
621 | { | |
622 | const char *value; | |
623 | ||
624 | ret = bt_value_string_get(attr_value, &value); | |
625 | ||
626 | if (ret) { | |
627 | goto end; | |
628 | } | |
629 | ||
630 | g_string_append_printf(context->string, | |
631 | "\t%s = \"%s\";\n", attr_name, value); | |
632 | break; | |
633 | } | |
634 | ||
635 | default: | |
636 | /* should never happen */ | |
637 | assert(false); | |
638 | break; | |
639 | } | |
640 | ||
641 | BT_PUT(attr_value); | |
642 | } | |
643 | ||
644 | if (event_class->context) { | |
645 | g_string_append(context->string, "\tcontext := "); | |
646 | ret = bt_ctf_field_type_serialize(event_class->context, | |
647 | context); | |
648 | if (ret) { | |
649 | goto end; | |
650 | } | |
651 | g_string_append(context->string, ";\n"); | |
652 | } | |
653 | ||
654 | if (event_class->fields) { | |
655 | g_string_append(context->string, "\tfields := "); | |
656 | ret = bt_ctf_field_type_serialize(event_class->fields, context); | |
657 | if (ret) { | |
658 | goto end; | |
659 | } | |
660 | g_string_append(context->string, ";\n"); | |
661 | } | |
662 | ||
663 | g_string_append(context->string, "};\n\n"); | |
664 | end: | |
665 | context->current_indentation_level = 0; | |
666 | BT_PUT(attr_value); | |
667 | return ret; | |
668 | } | |
669 | ||
670 | void bt_ctf_event_class_set_native_byte_order( | |
671 | struct bt_ctf_event_class *event_class, | |
672 | int byte_order) | |
673 | { | |
674 | if (!event_class) { | |
675 | return; | |
676 | } | |
677 | ||
678 | assert(byte_order == 0 || byte_order == LITTLE_ENDIAN || | |
679 | byte_order == BIG_ENDIAN); | |
680 | ||
681 | bt_ctf_field_type_set_native_byte_order(event_class->context, | |
682 | byte_order); | |
683 | bt_ctf_field_type_set_native_byte_order(event_class->fields, | |
684 | byte_order); | |
685 | } |