Remove Babeltrace 1 files and reorganize the tree
[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.h>
41 #include <babeltrace/endian.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 = -1;
144 goto end;
145 }
146
147 if (event_class->id >= 0) {
148 ret = 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 = -1;
160 }
161
162 if (ret < 0) {
163 /* means ID is not set */
164 ret = -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 uint32_t id)
175 {
176 int ret = 0;
177 struct bt_value *obj = NULL;
178 struct bt_ctf_stream_class *stream_class = NULL;
179
180
181 if (!event_class) {
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 int bt_ctf_event_class_get_attribute_count(
266 struct bt_ctf_event_class *event_class)
267 {
268 int ret = 0;
269
270 if (!event_class) {
271 ret = -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(
283 struct bt_ctf_event_class *event_class, int 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(struct bt_ctf_event_class *event_class,
300 int 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_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_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 int bt_ctf_event_class_get_field_count(
404 struct bt_ctf_event_class *event_class)
405 {
406 int ret;
407
408 if (!event_class) {
409 ret = -1;
410 goto end;
411 }
412
413 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
414 BT_CTF_TYPE_ID_STRUCT) {
415 ret = -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_field(struct bt_ctf_event_class *event_class,
425 const char **field_name, struct bt_ctf_field_type **field_type,
426 int index)
427 {
428 int ret;
429
430 if (!event_class || index < 0) {
431 ret = -1;
432 goto end;
433 }
434
435 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
436 BT_CTF_TYPE_ID_STRUCT) {
437 ret = -1;
438 goto end;
439 }
440
441 ret = bt_ctf_field_type_structure_get_field(event_class->fields,
442 field_name, field_type, index);
443 end:
444 return ret;
445 }
446
447 struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name(
448 struct bt_ctf_event_class *event_class, const char *name)
449 {
450 GQuark name_quark;
451 struct bt_ctf_field_type *field_type = NULL;
452
453 if (!event_class || !name) {
454 goto end;
455 }
456
457 if (bt_ctf_field_type_get_type_id(event_class->fields) !=
458 BT_CTF_TYPE_ID_STRUCT) {
459 goto end;
460 }
461
462 name_quark = g_quark_try_string(name);
463 if (!name_quark) {
464 goto end;
465 }
466
467 /*
468 * No need to increment field_type's reference count since getting it
469 * from the structure already does.
470 */
471 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
472 event_class->fields, name);
473 end:
474 return field_type;
475 }
476
477 struct bt_ctf_field_type *bt_ctf_event_class_get_context_type(
478 struct bt_ctf_event_class *event_class)
479 {
480 struct bt_ctf_field_type *context_type = NULL;
481
482 if (!event_class || !event_class->context) {
483 goto end;
484 }
485
486 bt_get(event_class->context);
487 context_type = event_class->context;
488 end:
489 return context_type;
490 }
491
492 int bt_ctf_event_class_set_context_type(
493 struct bt_ctf_event_class *event_class,
494 struct bt_ctf_field_type *context)
495 {
496 int ret = 0;
497
498 if (!event_class || event_class->frozen) {
499 ret = -1;
500 goto end;
501 }
502
503 if (context && bt_ctf_field_type_get_type_id(context) !=
504 BT_CTF_TYPE_ID_STRUCT) {
505 ret = -1;
506 goto end;
507 }
508
509 bt_put(event_class->context);
510 event_class->context = bt_get(context);
511 end:
512 return ret;
513
514 }
515
516 void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
517 {
518 bt_get(event_class);
519 }
520
521 void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
522 {
523 bt_put(event_class);
524 }
525
526 BT_HIDDEN
527 int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
528 uint32_t stream_id)
529 {
530 int ret = 0;
531 struct bt_value *obj;
532
533 obj = bt_value_integer_create_init(stream_id);
534
535 if (!obj) {
536 ret = -1;
537 goto end;
538 }
539
540 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
541 "stream_id", obj);
542
543 if (event_class->frozen) {
544 bt_ctf_attributes_freeze(event_class->attributes);
545 }
546
547 end:
548 BT_PUT(obj);
549 return ret;
550 }
551
552 static
553 void bt_ctf_event_class_destroy(struct bt_object *obj)
554 {
555 struct bt_ctf_event_class *event_class;
556
557 event_class = container_of(obj, struct bt_ctf_event_class, base);
558 bt_ctf_attributes_destroy(event_class->attributes);
559 bt_put(event_class->context);
560 bt_put(event_class->fields);
561 g_free(event_class);
562 }
563
564 BT_HIDDEN
565 void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class)
566 {
567 assert(event_class);
568 event_class->frozen = 1;
569 event_class->name = bt_ctf_event_class_get_name(event_class);
570 event_class->id = bt_ctf_event_class_get_id(event_class);
571 bt_ctf_field_type_freeze(event_class->context);
572 bt_ctf_field_type_freeze(event_class->fields);
573 bt_ctf_attributes_freeze(event_class->attributes);
574 }
575
576 BT_HIDDEN
577 int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
578 struct metadata_context *context)
579 {
580 int i;
581 int count;
582 int ret = 0;
583 struct bt_value *attr_value = NULL;
584
585 assert(event_class);
586 assert(context);
587
588 context->current_indentation_level = 1;
589 g_string_assign(context->field_name, "");
590 g_string_append(context->string, "event {\n");
591 count = bt_ctf_event_class_get_attribute_count(event_class);
592
593 if (count < 0) {
594 ret = -1;
595 goto end;
596 }
597
598 for (i = 0; i < count; ++i) {
599 const char *attr_name = NULL;
600
601 attr_name = bt_ctf_event_class_get_attribute_name(
602 event_class, i);
603 attr_value = bt_ctf_event_class_get_attribute_value(
604 event_class, i);
605
606 if (!attr_name || !attr_value) {
607 ret = -1;
608 goto end;
609 }
610
611 switch (bt_value_get_type(attr_value)) {
612 case BT_VALUE_TYPE_INTEGER:
613 {
614 int64_t value;
615
616 ret = bt_value_integer_get(attr_value, &value);
617
618 if (ret) {
619 goto end;
620 }
621
622 g_string_append_printf(context->string,
623 "\t%s = %" PRId64 ";\n", attr_name, value);
624 break;
625 }
626
627 case BT_VALUE_TYPE_STRING:
628 {
629 const char *value;
630
631 ret = bt_value_string_get(attr_value, &value);
632
633 if (ret) {
634 goto end;
635 }
636
637 g_string_append_printf(context->string,
638 "\t%s = \"%s\";\n", attr_name, value);
639 break;
640 }
641
642 default:
643 /* should never happen */
644 assert(false);
645 break;
646 }
647
648 BT_PUT(attr_value);
649 }
650
651 if (event_class->context) {
652 g_string_append(context->string, "\tcontext := ");
653 ret = bt_ctf_field_type_serialize(event_class->context,
654 context);
655 if (ret) {
656 goto end;
657 }
658 g_string_append(context->string, ";\n");
659 }
660
661 if (event_class->fields) {
662 g_string_append(context->string, "\tfields := ");
663 ret = bt_ctf_field_type_serialize(event_class->fields, context);
664 if (ret) {
665 goto end;
666 }
667 g_string_append(context->string, ";\n");
668 }
669
670 g_string_append(context->string, "};\n\n");
671 end:
672 context->current_indentation_level = 0;
673 BT_PUT(attr_value);
674 return ret;
675 }
This page took 0.044748 seconds and 4 git commands to generate.