Hide bt_ctf_event_class_* symbols
[babeltrace.git] / formats / ctf / ir / event-class.c
CommitLineData
1c822dfb
JG
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>
41#include <babeltrace/endian.h>
42
43static
44void bt_ctf_event_class_destroy(struct bt_object *obj);
45
46struct 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
61 bt_object_init(event_class, bt_ctf_event_class_destroy);
62 event_class->fields = bt_ctf_field_type_structure_create();
63 if (!event_class->fields) {
64 goto error;
65 }
66
67 event_class->attributes = bt_ctf_attributes_create();
68 if (!event_class->attributes) {
69 goto error;
70 }
71
72 obj = bt_value_integer_create_init(-1);
73 if (!obj) {
74 goto error;
75 }
76
77 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
78 "id", obj);
79 if (ret) {
80 goto error;
81 }
82
83 BT_PUT(obj);
84
85 obj = bt_value_string_create_init(name);
86 if (!obj) {
87 goto error;
88 }
89
90 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
91 "name", obj);
92 if (ret) {
93 goto error;
94 }
95
96 BT_PUT(obj);
97
98 return event_class;
99
100error:
101 BT_PUT(event_class);
102 BT_PUT(obj);
103 return event_class;
104}
105
55667b40 106BT_HIDDEN
1c822dfb
JG
107const 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
116 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
117 BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX);
118 if (!obj) {
119 goto end;
120 }
121
122 if (bt_value_string_get(obj, &name)) {
123 name = NULL;
124 }
125
126end:
127 BT_PUT(obj);
128 return name;
129}
130
55667b40 131BT_HIDDEN
1c822dfb
JG
132int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class)
133{
134 struct bt_value *obj = NULL;
135 int64_t ret = 0;
136
137 if (!event_class) {
138 ret = -1;
139 goto end;
140 }
141
142 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
143 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX);
144 if (!obj) {
145 goto end;
146 }
147
148 if (bt_value_integer_get(obj, &ret)) {
149 ret = -1;
150 }
151
152 if (ret < 0) {
153 /* means ID is not set */
154 ret = -1;
155 goto end;
156 }
157
158end:
159 BT_PUT(obj);
160 return ret;
161}
162
55667b40 163BT_HIDDEN
1c822dfb
JG
164int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
165 uint32_t id)
166{
167 int ret = 0;
168 struct bt_value *obj = NULL;
169 struct bt_ctf_stream_class *stream_class = NULL;
170
171
172 if (!event_class) {
173 ret = -1;
174 goto end;
175 }
176
177 stream_class = bt_ctf_event_class_get_stream_class(event_class);
178 if (stream_class) {
179 /*
180 * We don't allow changing the id if the event class has already
181 * been added to a stream class.
182 */
183 ret = -1;
184 goto end;
185 }
186
187 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
188 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX);
189 if (!obj) {
190 goto end;
191 }
192
193 if (bt_value_integer_set(obj, id)) {
194 ret = -1;
195 goto end;
196 }
197
198end:
199 BT_PUT(obj);
200 BT_PUT(stream_class);
201 return ret;
202}
203
204int bt_ctf_event_class_set_attribute(
205 struct bt_ctf_event_class *event_class, const char *name,
206 struct bt_value *value)
207{
208 int ret = 0;
209
210 if (!event_class || !name || !value || event_class->frozen) {
211 ret = -1;
212 goto end;
213 }
214
215 if (!strcmp(name, "id") || !strcmp(name, "loglevel")) {
216 if (!bt_value_is_integer(value)) {
217 ret = -1;
218 goto end;
219 }
220 } else if (!strcmp(name, "name") || !strcmp(name, "model.emf.uri")) {
221 if (!bt_value_is_string(value)) {
222 ret = -1;
223 goto end;
224 }
225 } else {
226 /* unknown attribute */
227 ret = -1;
228 goto end;
229 }
230
231 /* "id" special case: >= 0 */
232 if (!strcmp(name, "id")) {
233 int64_t val;
234
235 ret = bt_value_integer_get(value, &val);
236
237 if (ret) {
238 goto end;
239 }
240
241 if (val < 0) {
242 ret = -1;
243 goto end;
244 }
245 }
246
247 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
248 name, value);
249
250end:
251 return ret;
252}
253
55667b40 254BT_HIDDEN
1c822dfb
JG
255int bt_ctf_event_class_get_attribute_count(
256 struct bt_ctf_event_class *event_class)
257{
258 int ret = 0;
259
260 if (!event_class) {
261 ret = -1;
262 goto end;
263 }
264
265 ret = bt_ctf_attributes_get_count(event_class->attributes);
266
267end:
268 return ret;
269}
270
55667b40 271BT_HIDDEN
1c822dfb
JG
272const char *
273bt_ctf_event_class_get_attribute_name(
274 struct bt_ctf_event_class *event_class, int index)
275{
276 const char *ret;
277
278 if (!event_class) {
279 ret = NULL;
280 goto end;
281 }
282
283 ret = bt_ctf_attributes_get_field_name(event_class->attributes, index);
284
285end:
286 return ret;
287}
288
55667b40 289BT_HIDDEN
1c822dfb
JG
290struct bt_value *
291bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class *event_class,
292 int index)
293{
294 struct bt_value *ret;
295
296 if (!event_class) {
297 ret = NULL;
298 goto end;
299 }
300
301 ret = bt_ctf_attributes_get_field_value(event_class->attributes, index);
302
303end:
304 return ret;
305}
306
55667b40 307BT_HIDDEN
1c822dfb
JG
308struct bt_value *
309bt_ctf_event_class_get_attribute_value_by_name(
310 struct bt_ctf_event_class *event_class, const char *name)
311{
312 struct bt_value *ret;
313
314 if (!event_class || !name) {
315 ret = NULL;
316 goto end;
317 }
318
319 ret = bt_ctf_attributes_get_field_value_by_name(event_class->attributes,
320 name);
321
322end:
323 return ret;
324
325}
326
55667b40 327BT_HIDDEN
1c822dfb
JG
328struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class(
329 struct bt_ctf_event_class *event_class)
330{
331 return (struct bt_ctf_stream_class *) bt_object_get_parent(event_class);
332}
333
55667b40 334BT_HIDDEN
1c822dfb
JG
335struct bt_ctf_field_type *bt_ctf_event_class_get_payload_type(
336 struct bt_ctf_event_class *event_class)
337{
338 struct bt_ctf_field_type *payload = NULL;
339
340 if (!event_class) {
341 goto end;
342 }
343
344 bt_get(event_class->fields);
345 payload = event_class->fields;
346end:
347 return payload;
348}
349
55667b40 350BT_HIDDEN
1c822dfb
JG
351int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class *event_class,
352 struct bt_ctf_field_type *payload)
353{
354 int ret = 0;
355
356 if (!event_class || !payload ||
357 bt_ctf_field_type_get_type_id(payload) !=
358 BT_CTF_TYPE_ID_STRUCT) {
359 ret = -1;
360 goto end;
361 }
362
363 bt_get(payload);
364 bt_put(event_class->fields);
365 event_class->fields = payload;
366end:
367 return ret;
368}
369
370int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
371 struct bt_ctf_field_type *type,
372 const char *name)
373{
374 int ret = 0;
375
376 if (!event_class || !type || bt_ctf_validate_identifier(name) ||
377 event_class->frozen) {
378 ret = -1;
379 goto end;
380 }
381
382 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
383 BT_CTF_TYPE_ID_STRUCT) {
384 ret = -1;
385 goto end;
386 }
387
388 ret = bt_ctf_field_type_structure_add_field(event_class->fields,
389 type, name);
390end:
391 return ret;
392}
393
55667b40 394BT_HIDDEN
1c822dfb
JG
395int bt_ctf_event_class_get_field_count(
396 struct bt_ctf_event_class *event_class)
397{
398 int ret;
399
400 if (!event_class) {
401 ret = -1;
402 goto end;
403 }
404
405 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
406 BT_CTF_TYPE_ID_STRUCT) {
407 ret = -1;
408 goto end;
409 }
410
411 ret = bt_ctf_field_type_structure_get_field_count(event_class->fields);
412end:
413 return ret;
414}
415
55667b40 416BT_HIDDEN
1c822dfb
JG
417int 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);
436end:
437 return ret;
438}
439
440struct 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);
466end:
467 return field_type;
468}
469
55667b40 470BT_HIDDEN
1c822dfb
JG
471struct bt_ctf_field_type *bt_ctf_event_class_get_context_type(
472 struct bt_ctf_event_class *event_class)
473{
474 struct bt_ctf_field_type *context_type = NULL;
475
476 if (!event_class || !event_class->context) {
477 goto end;
478 }
479
480 bt_get(event_class->context);
481 context_type = event_class->context;
482end:
483 return context_type;
484}
485
55667b40 486BT_HIDDEN
1c822dfb
JG
487int bt_ctf_event_class_set_context_type(
488 struct bt_ctf_event_class *event_class,
489 struct bt_ctf_field_type *context)
490{
491 int ret = 0;
492
493 if (!event_class || !context || event_class->frozen) {
494 ret = -1;
495 goto end;
496 }
497
498 if (bt_ctf_field_type_get_type_id(context) != BT_CTF_TYPE_ID_STRUCT) {
499 ret = -1;
500 goto end;
501 }
502
503 bt_get(context);
504 bt_put(event_class->context);
505 event_class->context = context;
506end:
507 return ret;
508
509}
510
511void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
512{
513 bt_get(event_class);
514}
515
516void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
517{
518 bt_put(event_class);
519}
520
521BT_HIDDEN
522int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
523 uint32_t stream_id)
524{
525 int ret = 0;
526 struct bt_value *obj;
527
528 obj = bt_value_integer_create_init(stream_id);
529
530 if (!obj) {
531 ret = -1;
532 goto end;
533 }
534
535 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
536 "stream_id", obj);
537
538 if (event_class->frozen) {
539 bt_ctf_attributes_freeze(event_class->attributes);
540 }
541
542end:
543 BT_PUT(obj);
544 return ret;
545}
546
547static
548void bt_ctf_event_class_destroy(struct bt_object *obj)
549{
550 struct bt_ctf_event_class *event_class;
551
552 event_class = container_of(obj, struct bt_ctf_event_class, base);
553 bt_ctf_attributes_destroy(event_class->attributes);
554 bt_put(event_class->context);
555 bt_put(event_class->fields);
556 g_free(event_class);
557}
558
559BT_HIDDEN
560void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class)
561{
562 assert(event_class);
563 event_class->frozen = 1;
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
569BT_HIDDEN
570int 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");
664end:
665 context->current_indentation_level = 0;
666 BT_PUT(attr_value);
667 return ret;
668}
669
670void 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}
This page took 0.048041 seconds and 4 git commands to generate.