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