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