Add field validation logging
[babeltrace.git] / formats / 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.h>
41 #include <babeltrace/endian.h>
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
61 event_class->id = -1;
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
116 if (event_class->name) {
117 name = event_class->name;
118 goto end;
119 }
120
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
146 if (event_class->id >= 0) {
147 ret = event_class->id;
148 goto end;
149 }
150
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
223 if (!strcmp(name, "id") || !strcmp(name, "loglevel")) {
224 if (!bt_value_is_integer(value)) {
225 ret = -1;
226 goto end;
227 }
228 } else if (!strcmp(name, "name") || !strcmp(name, "model.emf.uri")) {
229 if (!bt_value_is_string(value)) {
230 ret = -1;
231 goto end;
232 }
233 } else {
234 /* unknown attribute */
235 ret = -1;
236 goto end;
237 }
238
239 /* "id" special case: >= 0 */
240 if (!strcmp(name, "id")) {
241 int64_t val;
242
243 ret = bt_value_integer_get(value, &val);
244
245 if (ret) {
246 goto end;
247 }
248
249 if (val < 0) {
250 ret = -1;
251 goto end;
252 }
253 }
254
255 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
256 name, value);
257
258 end:
259 return ret;
260 }
261
262 int bt_ctf_event_class_get_attribute_count(
263 struct bt_ctf_event_class *event_class)
264 {
265 int ret = 0;
266
267 if (!event_class) {
268 ret = -1;
269 goto end;
270 }
271
272 ret = bt_ctf_attributes_get_count(event_class->attributes);
273
274 end:
275 return ret;
276 }
277
278 const char *
279 bt_ctf_event_class_get_attribute_name(
280 struct bt_ctf_event_class *event_class, int index)
281 {
282 const char *ret;
283
284 if (!event_class) {
285 ret = NULL;
286 goto end;
287 }
288
289 ret = bt_ctf_attributes_get_field_name(event_class->attributes, index);
290
291 end:
292 return ret;
293 }
294
295 struct bt_value *
296 bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class *event_class,
297 int index)
298 {
299 struct bt_value *ret;
300
301 if (!event_class) {
302 ret = NULL;
303 goto end;
304 }
305
306 ret = bt_ctf_attributes_get_field_value(event_class->attributes, index);
307
308 end:
309 return ret;
310 }
311
312 struct bt_value *
313 bt_ctf_event_class_get_attribute_value_by_name(
314 struct bt_ctf_event_class *event_class, const char *name)
315 {
316 struct bt_value *ret;
317
318 if (!event_class || !name) {
319 ret = NULL;
320 goto end;
321 }
322
323 ret = bt_ctf_attributes_get_field_value_by_name(event_class->attributes,
324 name);
325
326 end:
327 return ret;
328
329 }
330
331 struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class(
332 struct bt_ctf_event_class *event_class)
333 {
334 return (struct bt_ctf_stream_class *) bt_object_get_parent(event_class);
335 }
336
337 struct bt_ctf_field_type *bt_ctf_event_class_get_payload_type(
338 struct bt_ctf_event_class *event_class)
339 {
340 struct bt_ctf_field_type *payload = NULL;
341
342 if (!event_class) {
343 goto end;
344 }
345
346 bt_get(event_class->fields);
347 payload = event_class->fields;
348 end:
349 return payload;
350 }
351
352 int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class *event_class,
353 struct bt_ctf_field_type *payload)
354 {
355 int ret = 0;
356
357 if (!event_class || !payload ||
358 bt_ctf_field_type_get_type_id(payload) !=
359 BT_CTF_TYPE_ID_STRUCT) {
360 ret = -1;
361 goto end;
362 }
363
364 bt_get(payload);
365 bt_put(event_class->fields);
366 event_class->fields = payload;
367 end:
368 return ret;
369 }
370
371 int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
372 struct bt_ctf_field_type *type,
373 const char *name)
374 {
375 int ret = 0;
376
377 if (!event_class || !type || bt_ctf_validate_identifier(name) ||
378 event_class->frozen) {
379 ret = -1;
380 goto end;
381 }
382
383 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
384 BT_CTF_TYPE_ID_STRUCT) {
385 ret = -1;
386 goto end;
387 }
388
389 ret = bt_ctf_field_type_structure_add_field(event_class->fields,
390 type, name);
391 end:
392 return ret;
393 }
394
395 int 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);
412 end:
413 return ret;
414 }
415
416 int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class,
417 const char **field_name, struct bt_ctf_field_type **field_type,
418 int index)
419 {
420 int ret;
421
422 if (!event_class || index < 0) {
423 ret = -1;
424 goto end;
425 }
426
427 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
428 BT_CTF_TYPE_ID_STRUCT) {
429 ret = -1;
430 goto end;
431 }
432
433 ret = bt_ctf_field_type_structure_get_field(event_class->fields,
434 field_name, field_type, index);
435 end:
436 return ret;
437 }
438
439 struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name(
440 struct bt_ctf_event_class *event_class, const char *name)
441 {
442 GQuark name_quark;
443 struct bt_ctf_field_type *field_type = NULL;
444
445 if (!event_class || !name) {
446 goto end;
447 }
448
449 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
450 BT_CTF_TYPE_ID_STRUCT) {
451 goto end;
452 }
453
454 name_quark = g_quark_try_string(name);
455 if (!name_quark) {
456 goto end;
457 }
458
459 /*
460 * No need to increment field_type's reference count since getting it
461 * from the structure already does.
462 */
463 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
464 event_class->fields, name);
465 end:
466 return field_type;
467 }
468
469 struct bt_ctf_field_type *bt_ctf_event_class_get_context_type(
470 struct bt_ctf_event_class *event_class)
471 {
472 struct bt_ctf_field_type *context_type = NULL;
473
474 if (!event_class || !event_class->context) {
475 goto end;
476 }
477
478 bt_get(event_class->context);
479 context_type = event_class->context;
480 end:
481 return context_type;
482 }
483
484 int bt_ctf_event_class_set_context_type(
485 struct bt_ctf_event_class *event_class,
486 struct bt_ctf_field_type *context)
487 {
488 int ret = 0;
489
490 if (!event_class || !context || event_class->frozen) {
491 ret = -1;
492 goto end;
493 }
494
495 if (bt_ctf_field_type_get_type_id(context) != BT_CTF_TYPE_ID_STRUCT) {
496 ret = -1;
497 goto end;
498 }
499
500 bt_get(context);
501 bt_put(event_class->context);
502 event_class->context = context;
503 end:
504 return ret;
505
506 }
507
508 void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
509 {
510 bt_get(event_class);
511 }
512
513 void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
514 {
515 bt_put(event_class);
516 }
517
518 BT_HIDDEN
519 int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
520 uint32_t stream_id)
521 {
522 int ret = 0;
523 struct bt_value *obj;
524
525 obj = bt_value_integer_create_init(stream_id);
526
527 if (!obj) {
528 ret = -1;
529 goto end;
530 }
531
532 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
533 "stream_id", obj);
534
535 if (event_class->frozen) {
536 bt_ctf_attributes_freeze(event_class->attributes);
537 }
538
539 end:
540 BT_PUT(obj);
541 return ret;
542 }
543
544 static
545 void bt_ctf_event_class_destroy(struct bt_object *obj)
546 {
547 struct bt_ctf_event_class *event_class;
548
549 event_class = container_of(obj, struct bt_ctf_event_class, base);
550 bt_ctf_attributes_destroy(event_class->attributes);
551 bt_put(event_class->context);
552 bt_put(event_class->fields);
553 g_free(event_class);
554 }
555
556 BT_HIDDEN
557 void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class)
558 {
559 assert(event_class);
560 event_class->frozen = 1;
561 event_class->name = bt_ctf_event_class_get_name(event_class);
562 event_class->id = bt_ctf_event_class_get_id(event_class);
563 bt_ctf_field_type_freeze(event_class->context);
564 bt_ctf_field_type_freeze(event_class->fields);
565 bt_ctf_attributes_freeze(event_class->attributes);
566 }
567
568 BT_HIDDEN
569 int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
570 struct metadata_context *context)
571 {
572 int i;
573 int count;
574 int ret = 0;
575 struct bt_value *attr_value = NULL;
576
577 assert(event_class);
578 assert(context);
579
580 context->current_indentation_level = 1;
581 g_string_assign(context->field_name, "");
582 g_string_append(context->string, "event {\n");
583 count = bt_ctf_event_class_get_attribute_count(event_class);
584
585 if (count < 0) {
586 ret = -1;
587 goto end;
588 }
589
590 for (i = 0; i < count; ++i) {
591 const char *attr_name = NULL;
592
593 attr_name = bt_ctf_event_class_get_attribute_name(
594 event_class, i);
595 attr_value = bt_ctf_event_class_get_attribute_value(
596 event_class, i);
597
598 if (!attr_name || !attr_value) {
599 ret = -1;
600 goto end;
601 }
602
603 switch (bt_value_get_type(attr_value)) {
604 case BT_VALUE_TYPE_INTEGER:
605 {
606 int64_t value;
607
608 ret = bt_value_integer_get(attr_value, &value);
609
610 if (ret) {
611 goto end;
612 }
613
614 g_string_append_printf(context->string,
615 "\t%s = %" PRId64 ";\n", attr_name, value);
616 break;
617 }
618
619 case BT_VALUE_TYPE_STRING:
620 {
621 const char *value;
622
623 ret = bt_value_string_get(attr_value, &value);
624
625 if (ret) {
626 goto end;
627 }
628
629 g_string_append_printf(context->string,
630 "\t%s = \"%s\";\n", attr_name, value);
631 break;
632 }
633
634 default:
635 /* should never happen */
636 assert(false);
637 break;
638 }
639
640 BT_PUT(attr_value);
641 }
642
643 if (event_class->context) {
644 g_string_append(context->string, "\tcontext := ");
645 ret = bt_ctf_field_type_serialize(event_class->context,
646 context);
647 if (ret) {
648 goto end;
649 }
650 g_string_append(context->string, ";\n");
651 }
652
653 if (event_class->fields) {
654 g_string_append(context->string, "\tfields := ");
655 ret = bt_ctf_field_type_serialize(event_class->fields, context);
656 if (ret) {
657 goto end;
658 }
659 g_string_append(context->string, ";\n");
660 }
661
662 g_string_append(context->string, "};\n\n");
663 end:
664 context->current_indentation_level = 0;
665 BT_PUT(attr_value);
666 return ret;
667 }
668
669 void bt_ctf_event_class_set_native_byte_order(
670 struct bt_ctf_event_class *event_class,
671 int byte_order)
672 {
673 if (!event_class) {
674 return;
675 }
676
677 assert(byte_order == 0 || byte_order == LITTLE_ENDIAN ||
678 byte_order == BIG_ENDIAN);
679
680 bt_ctf_field_type_set_native_byte_order(event_class->context,
681 byte_order);
682 bt_ctf_field_type_set_native_byte_order(event_class->fields,
683 byte_order);
684 }
This page took 0.044044 seconds and 4 git commands to generate.