lib/ctf-ir/attributes.c: add assert()
[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
f8b979f9
PP
29#define BT_LOG_TAG "EVENT-CLASS"
30#include <babeltrace/lib-logging-internal.h>
31
272df73e
PP
32#include <babeltrace/ctf-ir/fields-internal.h>
33#include <babeltrace/ctf-ir/field-types-internal.h>
34#include <babeltrace/ctf-ir/event-class.h>
35#include <babeltrace/ctf-ir/event-class-internal.h>
36#include <babeltrace/ctf-ir/stream-class.h>
37#include <babeltrace/ctf-ir/stream-class-internal.h>
38#include <babeltrace/ctf-ir/trace-internal.h>
39#include <babeltrace/ctf-ir/validation-internal.h>
40#include <babeltrace/ctf-ir/utils.h>
41#include <babeltrace/ref.h>
42#include <babeltrace/ctf-ir/attributes-internal.h>
3d9990ac
PP
43#include <babeltrace/compiler-internal.h>
44#include <babeltrace/endian-internal.h>
c55a9f58 45#include <babeltrace/types.h>
f8b979f9 46#include <babeltrace/values-internal.h>
dc3fffef 47#include <inttypes.h>
272df73e
PP
48
49static
50void bt_ctf_event_class_destroy(struct bt_object *obj);
51
52struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name)
53{
54 int ret;
55 struct bt_value *obj = NULL;
56 struct bt_ctf_event_class *event_class = NULL;
57
f8b979f9
PP
58 BT_LOGD("Creating event class object: name=\"%s\"",
59 name);
60
272df73e 61 if (bt_ctf_validate_identifier(name)) {
f8b979f9
PP
62 BT_LOGW("Invalid parameter: event class's name is not a valid CTF identifier: "
63 "name=\"%s\"", name);
272df73e
PP
64 goto error;
65 }
66
67 event_class = g_new0(struct bt_ctf_event_class, 1);
68 if (!event_class) {
f8b979f9 69 BT_LOGE_STR("Failed to allocate one event class.");
272df73e
PP
70 goto error;
71 }
72
5990dd44 73 event_class->id = -1;
272df73e
PP
74 bt_object_init(event_class, bt_ctf_event_class_destroy);
75 event_class->fields = bt_ctf_field_type_structure_create();
76 if (!event_class->fields) {
f8b979f9 77 BT_LOGE_STR("Cannot create event class's initial payload field type object.");
272df73e
PP
78 goto error;
79 }
80
53a1cae7
PP
81 event_class->context = bt_ctf_field_type_structure_create();
82 if (!event_class->context) {
f8b979f9 83 BT_LOGE_STR("Cannot create event class's initial context field type object.");
53a1cae7
PP
84 goto error;
85 }
86
272df73e
PP
87 event_class->attributes = bt_ctf_attributes_create();
88 if (!event_class->attributes) {
f8b979f9 89 BT_LOGE_STR("Cannot create event class's attributes object.");
272df73e
PP
90 goto error;
91 }
92
93 obj = bt_value_integer_create_init(-1);
94 if (!obj) {
f8b979f9 95 BT_LOGE_STR("Cannot create integer value object.");
272df73e
PP
96 goto error;
97 }
98
99 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
100 "id", obj);
101 if (ret) {
f8b979f9
PP
102 BT_LOGE("Cannot set event class's attributes's `id` field: ret=%d",
103 ret);
272df73e
PP
104 goto error;
105 }
106
107 BT_PUT(obj);
108
109 obj = bt_value_string_create_init(name);
110 if (!obj) {
f8b979f9 111 BT_LOGE_STR("Cannot create string value object.");
272df73e
PP
112 goto error;
113 }
114
115 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
116 "name", obj);
117 if (ret) {
f8b979f9
PP
118 BT_LOGE("Cannot set event class's attributes's `name` field: ret=%d",
119 ret);
272df73e
PP
120 goto error;
121 }
122
123 BT_PUT(obj);
f8b979f9
PP
124 BT_LOGD("Created event class object: addr=%p, name=\"%s\"",
125 event_class, bt_ctf_event_class_get_name(event_class));
272df73e
PP
126 return event_class;
127
128error:
129 BT_PUT(event_class);
130 BT_PUT(obj);
131 return event_class;
132}
133
134const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class)
135{
136 struct bt_value *obj = NULL;
137 const char *name = NULL;
f8b979f9 138 int ret;
272df73e
PP
139
140 if (!event_class) {
f8b979f9 141 BT_LOGW_STR("Invalid parameter: event class is NULL.");
272df73e
PP
142 goto end;
143 }
144
5990dd44
JG
145 if (event_class->name) {
146 name = event_class->name;
147 goto end;
148 }
149
272df73e
PP
150 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
151 BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX);
f8b979f9
PP
152 assert(obj);
153 ret = bt_value_string_get(obj, &name);
154 assert(ret == 0);
272df73e
PP
155
156end:
157 BT_PUT(obj);
158 return name;
159}
160
161int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class)
162{
163 struct bt_value *obj = NULL;
164 int64_t ret = 0;
f8b979f9 165 int int_ret;
272df73e
PP
166
167 if (!event_class) {
f8b979f9 168 BT_LOGW_STR("Invalid parameter: event class is NULL.");
9ac68eb1 169 ret = (int64_t) -1;
272df73e
PP
170 goto end;
171 }
172
5990dd44 173 if (event_class->id >= 0) {
9ac68eb1 174 ret = (int64_t) event_class->id;
5990dd44
JG
175 goto end;
176 }
177
272df73e
PP
178 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
179 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX);
f8b979f9
PP
180 assert(obj);
181 int_ret = bt_value_integer_get(obj, &ret);
182 assert(int_ret == 0);
272df73e
PP
183 if (ret < 0) {
184 /* means ID is not set */
f8b979f9
PP
185 BT_LOGV("Event class's ID is not set: addr=%p, name=\"%s\"",
186 event_class, bt_ctf_event_class_get_name(event_class));
9ac68eb1 187 ret = (int64_t) -1;
272df73e
PP
188 goto end;
189 }
190
191end:
192 BT_PUT(obj);
193 return ret;
194}
195
196int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
9ac68eb1 197 uint64_t id_param)
272df73e
PP
198{
199 int ret = 0;
200 struct bt_value *obj = NULL;
9ac68eb1 201 int64_t id = (int64_t) id_param;
272df73e 202
f8b979f9
PP
203 if (!event_class) {
204 BT_LOGW_STR("Invalid parameter: event class is NULL.");
272df73e
PP
205 ret = -1;
206 goto end;
207 }
208
f8b979f9
PP
209 if (event_class->frozen) {
210 BT_LOGW("Invalid parameter: event class is frozen: "
211 "addr=%p, name=\"%s\", id=%" PRId64,
212 event_class, bt_ctf_event_class_get_name(event_class),
213 bt_ctf_event_class_get_id(event_class));
272df73e
PP
214 ret = -1;
215 goto end;
216 }
217
f8b979f9
PP
218 if (id < 0) {
219 BT_LOGW("Invalid parameter: invalid event class's ID: "
220 "addr=%p, name=\"%s\", id=%" PRIu64,
221 event_class, bt_ctf_event_class_get_name(event_class),
222 id_param);
272df73e
PP
223 ret = -1;
224 goto end;
225 }
226
f8b979f9
PP
227 obj = bt_ctf_attributes_get_field_value(event_class->attributes,
228 BT_CTF_EVENT_CLASS_ATTR_ID_INDEX);
229 assert(obj);
230 ret = bt_value_integer_set(obj, id);
231 assert(ret == 0);
232 BT_LOGV("Set event class's ID: "
233 "addr=%p, name=\"%s\", id=%" PRId64,
234 event_class, bt_ctf_event_class_get_name(event_class), id);
235
272df73e
PP
236end:
237 BT_PUT(obj);
272df73e
PP
238 return ret;
239}
240
241int bt_ctf_event_class_set_attribute(
242 struct bt_ctf_event_class *event_class, const char *name,
243 struct bt_value *value)
244{
245 int ret = 0;
246
f8b979f9
PP
247 if (!event_class || !name || !value) {
248 BT_LOGW("Invalid parameter: event class, name, or value is NULL: "
249 "event-class-addr=%p, name-addr=%p, value-addr=%p",
250 event_class, name, value);
251 ret = -1;
252 goto end;
253 }
254
255 if (event_class->frozen) {
256 BT_LOGW("Invalid parameter: event class is frozen: "
257 "addr=%p, name=\"%s\", id=%" PRId64 ", attr-name=\"%s\"",
258 event_class, bt_ctf_event_class_get_name(event_class),
259 bt_ctf_event_class_get_id(event_class), name);
272df73e
PP
260 ret = -1;
261 goto end;
262 }
263
2073703a
JG
264 if (!strcmp(name, "id") || !strcmp(name, "loglevel") ||
265 !strcmp(name, "stream_id")) {
272df73e 266 if (!bt_value_is_integer(value)) {
f8b979f9
PP
267 BT_LOGW("Invalid parameter: this event class's attribute must have an integer value: "
268 "event-class-addr=%p, event-class-name=\"%s\", "
269 "event-class-id=%" PRId64 ", attr-name=\"%s\", "
270 "attr-type=%s", event_class,
271 bt_ctf_event_class_get_name(event_class),
272 bt_ctf_event_class_get_id(event_class), name,
273 bt_value_type_string(bt_value_get_type(value)));
272df73e
PP
274 ret = -1;
275 goto end;
276 }
c3c30b08
MD
277 } else if (!strcmp(name, "name") || !strcmp(name, "model.emf.uri") ||
278 !strcmp(name, "loglevel_string")) {
272df73e 279 if (!bt_value_is_string(value)) {
f8b979f9
PP
280 BT_LOGW("Invalid parameter: this event class's attribute must have a string value: "
281 "event-class-addr=%p, event-class-name=\"%s\", "
282 "event-class-id=%" PRId64 ", attr-name=\"%s\", "
283 "attr-type=%s", event_class,
284 bt_ctf_event_class_get_name(event_class),
285 bt_ctf_event_class_get_id(event_class), name,
286 bt_value_type_string(bt_value_get_type(value)));
272df73e
PP
287 ret = -1;
288 goto end;
289 }
290 } else {
291 /* unknown attribute */
f8b979f9
PP
292 BT_LOGW("Invalid parameter: unknown event class's attribute name: "
293 "event-class-addr=%p, event-class-name=\"%s\", "
294 "event-class-id=%" PRId64 ", attr-name=\"%s\"",
295 event_class, bt_ctf_event_class_get_name(event_class),
296 bt_ctf_event_class_get_id(event_class), name);
272df73e
PP
297 ret = -1;
298 goto end;
299 }
300
301 /* "id" special case: >= 0 */
302 if (!strcmp(name, "id")) {
303 int64_t val;
304
305 ret = bt_value_integer_get(value, &val);
f8b979f9
PP
306 assert(ret == 0);
307 ret = bt_ctf_event_class_set_id(event_class, (uint64_t) val);
272df73e
PP
308 if (ret) {
309 goto end;
310 }
272df73e
PP
311 }
312
313 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
2073703a 314 name, value);
f8b979f9
PP
315 assert(ret == 0);
316
317 if (BT_LOG_ON_VERBOSE) {
318 if (bt_value_is_integer(value)) {
319 int64_t val;
320
321 ret = bt_value_integer_get(value, &val);
322 assert(ret == 0);
323 BT_LOGV("Set event class's integer attribute: "
324 "event-class-addr=%p, event-class-name=\"%s\", "
325 "event-class-id=%" PRId64 ", attr-name=\"%s\", "
326 "attr-value=%" PRId64,
327 event_class, bt_ctf_event_class_get_name(event_class),
328 bt_ctf_event_class_get_id(event_class), name,
329 val);
330 } else if (bt_value_is_string(value)) {
331 const char *val;
332
333 ret = bt_value_string_get(value, &val);
334 assert(ret == 0);
335 BT_LOGV("Set event class's string attribute: "
336 "event-class-addr=%p, event-class-name=\"%s\", "
337 "event-class-id=%" PRId64 ", attr-name=\"%s\", "
338 "attr-value=\"%s\"",
339 event_class, bt_ctf_event_class_get_name(event_class),
340 bt_ctf_event_class_get_id(event_class), name,
341 val);
342 }
343 }
272df73e
PP
344
345end:
346 return ret;
347}
348
544d0515 349int64_t bt_ctf_event_class_get_attribute_count(
272df73e
PP
350 struct bt_ctf_event_class *event_class)
351{
9ac68eb1 352 int64_t ret;
272df73e
PP
353
354 if (!event_class) {
f8b979f9 355 BT_LOGW_STR("Invalid parameter: event class is NULL.");
9ac68eb1 356 ret = (int64_t) -1;
272df73e
PP
357 goto end;
358 }
359
360 ret = bt_ctf_attributes_get_count(event_class->attributes);
f8b979f9 361 assert(ret >= 0);
272df73e
PP
362
363end:
364 return ret;
365}
366
367const char *
9ac68eb1
PP
368bt_ctf_event_class_get_attribute_name_by_index(
369 struct bt_ctf_event_class *event_class, uint64_t index)
272df73e
PP
370{
371 const char *ret;
372
373 if (!event_class) {
f8b979f9 374 BT_LOGW_STR("Invalid parameter: event class is NULL.");
272df73e
PP
375 ret = NULL;
376 goto end;
377 }
378
379 ret = bt_ctf_attributes_get_field_name(event_class->attributes, index);
f8b979f9
PP
380 if (ret) {
381 BT_LOGW("Cannot get event class's attribute name by index: "
382 "addr=%p, name=\"%s\", id=%" PRId64 ", index=%" PRIu64,
383 event_class, bt_ctf_event_class_get_name(event_class),
384 bt_ctf_event_class_get_id(event_class), index);
385 }
272df73e
PP
386
387end:
388 return ret;
389}
390
391struct bt_value *
9ac68eb1
PP
392bt_ctf_event_class_get_attribute_value_by_index(
393 struct bt_ctf_event_class *event_class, uint64_t index)
272df73e
PP
394{
395 struct bt_value *ret;
396
397 if (!event_class) {
f8b979f9 398 BT_LOGW_STR("Invalid parameter: event class is NULL.");
272df73e
PP
399 ret = NULL;
400 goto end;
401 }
402
403 ret = bt_ctf_attributes_get_field_value(event_class->attributes, index);
f8b979f9
PP
404 if (ret) {
405 BT_LOGW("Cannot get event class's attribute value by index: "
406 "addr=%p, name=\"%s\", id=%" PRId64 ", index=%" PRIu64,
407 event_class, bt_ctf_event_class_get_name(event_class),
408 bt_ctf_event_class_get_id(event_class), index);
409 }
272df73e
PP
410
411end:
412 return ret;
413}
414
415struct bt_value *
416bt_ctf_event_class_get_attribute_value_by_name(
417 struct bt_ctf_event_class *event_class, const char *name)
418{
419 struct bt_value *ret;
420
421 if (!event_class || !name) {
f8b979f9
PP
422 BT_LOGW("Invalid parameter: event class or name is NULL: ",
423 "event-class-addr=%p, name-addr=%p",
424 event_class, name);
272df73e
PP
425 ret = NULL;
426 goto end;
427 }
428
429 ret = bt_ctf_attributes_get_field_value_by_name(event_class->attributes,
430 name);
f8b979f9
PP
431 if (ret) {
432 BT_LOGV("Cannot find event class's attribute: "
433 "addr=%p, event-class-name=\"%s\", id=%" PRId64 ", "
434 "attr-name=\"%s\"",
435 event_class, bt_ctf_event_class_get_name(event_class),
436 bt_ctf_event_class_get_id(event_class), name);
437 }
272df73e
PP
438
439end:
440 return ret;
441
442}
443
444struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class(
445 struct bt_ctf_event_class *event_class)
446{
dc3fffef
PP
447 return event_class ?
448 bt_get(bt_ctf_event_class_borrow_stream_class(event_class)) :
449 NULL;
272df73e
PP
450}
451
452struct bt_ctf_field_type *bt_ctf_event_class_get_payload_type(
453 struct bt_ctf_event_class *event_class)
454{
455 struct bt_ctf_field_type *payload = NULL;
456
457 if (!event_class) {
f8b979f9 458 BT_LOGW_STR("Invalid parameter: event class is NULL.");
272df73e
PP
459 goto end;
460 }
461
462 bt_get(event_class->fields);
463 payload = event_class->fields;
464end:
465 return payload;
466}
467
468int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class *event_class,
469 struct bt_ctf_field_type *payload)
470{
471 int ret = 0;
472
835b2d10 473 if (!event_class) {
f8b979f9 474 BT_LOGW_STR("Invalid parameter: event class is NULL.");
835b2d10
JG
475 ret = -1;
476 goto end;
477 }
478
479 if (payload && bt_ctf_field_type_get_type_id(payload) !=
1487a16a 480 BT_CTF_FIELD_TYPE_ID_STRUCT) {
f8b979f9
PP
481 BT_LOGW("Invalid parameter: event class's payload field type must be a structure: "
482 "addr=%p, name=\"%s\", id=%" PRId64 ", "
483 "payload-ft-addr=%p, payload-ft-id=%s",
484 event_class, bt_ctf_event_class_get_name(event_class),
485 bt_ctf_event_class_get_id(event_class), payload,
486 bt_ctf_field_type_id_string(
487 bt_ctf_field_type_get_type_id(payload)));
272df73e
PP
488 ret = -1;
489 goto end;
490 }
491
272df73e 492 bt_put(event_class->fields);
835b2d10 493 event_class->fields = bt_get(payload);
f8b979f9
PP
494 BT_LOGV("Set event class's payload field type: "
495 "event-class-addr=%p, event-class-name=\"%s\", "
496 "event-class-id=%" PRId64 ", payload-ft-addr=%p",
497 event_class, bt_ctf_event_class_get_name(event_class),
498 bt_ctf_event_class_get_id(event_class), payload);
272df73e
PP
499end:
500 return ret;
501}
502
503int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
504 struct bt_ctf_field_type *type,
505 const char *name)
506{
507 int ret = 0;
508
f8b979f9
PP
509 if (!event_class || !type) {
510 BT_LOGW("Invalid parameter: event class or field type is NULL: "
511 "event-class-addr=%p, field-type-addr=%p",
512 event_class, type);
513 ret = -1;
514 goto end;
515 }
516
517 if (bt_ctf_validate_identifier(name)) {
518 BT_LOGW("Invalid parameter: event class's payload field type's field name is not a valid CTF identifier: "
519 "addr=%p, name=\"%s\", id=%" PRId64 ", field-name=\"%s\"",
520 event_class, bt_ctf_event_class_get_name(event_class),
521 bt_ctf_event_class_get_id(event_class),
522 name);
272df73e
PP
523 ret = -1;
524 goto end;
525 }
526
f8b979f9
PP
527 if (event_class->frozen) {
528 BT_LOGW("Invalid parameter: event class is frozen: "
529 "addr=%p, name=\"%s\", id=%" PRId64,
530 event_class, bt_ctf_event_class_get_name(event_class),
531 bt_ctf_event_class_get_id(event_class));
272df73e
PP
532 ret = -1;
533 goto end;
534 }
535
f8b979f9
PP
536 if (!event_class->fields) {
537 BT_LOGW("Event class has no payload field type: "
538 "addr=%p, name=\"%s\", id=%" PRId64,
539 event_class, bt_ctf_event_class_get_name(event_class),
540 bt_ctf_event_class_get_id(event_class));
541 ret = -1;
542 goto end;
543 }
544
545 assert(bt_ctf_field_type_get_type_id(event_class->fields) ==
546 BT_CTF_FIELD_TYPE_ID_STRUCT);
272df73e
PP
547 ret = bt_ctf_field_type_structure_add_field(event_class->fields,
548 type, name);
f8b979f9
PP
549 BT_LOGV("Added field to event class's payload field type: "
550 "event-class-addr=%p, event-class-name=\"%s\", "
551 "event-class-id=%" PRId64 ", field-name=\"%s\", ft-addr=%p",
552 event_class, bt_ctf_event_class_get_name(event_class),
553 bt_ctf_event_class_get_id(event_class), name, type);
272df73e
PP
554end:
555 return ret;
556}
557
9ac68eb1 558int64_t bt_ctf_event_class_get_payload_type_field_count(
272df73e
PP
559 struct bt_ctf_event_class *event_class)
560{
544d0515 561 int64_t ret;
272df73e
PP
562
563 if (!event_class) {
f8b979f9 564 BT_LOGW_STR("Invalid parameter: event class is NULL.");
9ac68eb1 565 ret = (int64_t) -1;
272df73e
PP
566 goto end;
567 }
568
f8b979f9
PP
569 if (!event_class->fields) {
570 BT_LOGV("Event class has no payload field type: "
571 "addr=%p, name=\"%s\", id=%" PRId64,
572 event_class, bt_ctf_event_class_get_name(event_class),
573 bt_ctf_event_class_get_id(event_class));
9ac68eb1 574 ret = (int64_t) -1;
272df73e
PP
575 goto end;
576 }
577
f8b979f9
PP
578 assert(bt_ctf_field_type_get_type_id(event_class->fields) ==
579 BT_CTF_FIELD_TYPE_ID_STRUCT);
272df73e
PP
580 ret = bt_ctf_field_type_structure_get_field_count(event_class->fields);
581end:
582 return ret;
583}
584
9ac68eb1
PP
585int bt_ctf_event_class_get_payload_type_field_by_index(
586 struct bt_ctf_event_class *event_class,
272df73e 587 const char **field_name, struct bt_ctf_field_type **field_type,
9ac68eb1 588 uint64_t index)
272df73e
PP
589{
590 int ret;
591
9ac68eb1 592 if (!event_class) {
f8b979f9 593 BT_LOGW_STR("Invalid parameter: event class is NULL.");
272df73e
PP
594 ret = -1;
595 goto end;
596 }
597
f8b979f9
PP
598 if (!event_class->fields) {
599 BT_LOGV("Event class has no payload field type: "
600 "addr=%p, name=\"%s\", id=%" PRId64 ", index=%" PRIu64,
601 event_class, bt_ctf_event_class_get_name(event_class),
602 bt_ctf_event_class_get_id(event_class), index);
272df73e
PP
603 ret = -1;
604 goto end;
605 }
606
f8b979f9
PP
607 assert(bt_ctf_field_type_get_type_id(event_class->fields) ==
608 BT_CTF_FIELD_TYPE_ID_STRUCT);
272df73e
PP
609 ret = bt_ctf_field_type_structure_get_field(event_class->fields,
610 field_name, field_type, index);
611end:
612 return ret;
613}
614
9ac68eb1
PP
615struct bt_ctf_field_type *
616bt_ctf_event_class_get_payload_type_field_type_by_name(
272df73e
PP
617 struct bt_ctf_event_class *event_class, const char *name)
618{
619 GQuark name_quark;
620 struct bt_ctf_field_type *field_type = NULL;
621
622 if (!event_class || !name) {
f8b979f9
PP
623 BT_LOGW("Invalid parameter: event class or name is NULL: ",
624 "event-class-addr=%p, name-addr=%p",
625 event_class, name);
272df73e
PP
626 goto end;
627 }
628
f8b979f9
PP
629 if (!event_class->fields) {
630 BT_LOGV("Event class has no payload field type: "
631 "addr=%p, name=\"%s\", id=%" PRId64,
632 event_class, bt_ctf_event_class_get_name(event_class),
633 bt_ctf_event_class_get_id(event_class));
272df73e
PP
634 goto end;
635 }
636
f8b979f9
PP
637 assert(bt_ctf_field_type_get_type_id(event_class->fields) ==
638 BT_CTF_FIELD_TYPE_ID_STRUCT);
272df73e
PP
639 name_quark = g_quark_try_string(name);
640 if (!name_quark) {
f8b979f9 641 BT_LOGE("Cannot get GQuark: string=\"%s\"", name);
272df73e
PP
642 goto end;
643 }
644
645 /*
646 * No need to increment field_type's reference count since getting it
647 * from the structure already does.
648 */
649 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
650 event_class->fields, name);
651end:
652 return field_type;
653}
654
655struct bt_ctf_field_type *bt_ctf_event_class_get_context_type(
656 struct bt_ctf_event_class *event_class)
657{
658 struct bt_ctf_field_type *context_type = NULL;
659
f8b979f9
PP
660 if (!event_class) {
661 BT_LOGW_STR("Invalid parameter: event class is NULL.");
662 goto end;
663 }
664
665 if (!event_class->context) {
666 BT_LOGV("Event class has no context field type: "
667 "addr=%p, name=\"%s\", id=%" PRId64,
668 event_class, bt_ctf_event_class_get_name(event_class),
669 bt_ctf_event_class_get_id(event_class));
272df73e
PP
670 goto end;
671 }
672
673 bt_get(event_class->context);
674 context_type = event_class->context;
675end:
676 return context_type;
677}
678
679int bt_ctf_event_class_set_context_type(
680 struct bt_ctf_event_class *event_class,
681 struct bt_ctf_field_type *context)
682{
683 int ret = 0;
684
f8b979f9
PP
685 if (!event_class) {
686 BT_LOGW_STR("Invalid parameter: event class is NULL.");
687 ret = -1;
688 goto end;
689 }
690
691 if (event_class->frozen) {
692 BT_LOGW("Invalid parameter: event class is frozen: "
693 "addr=%p, name=\"%s\", id=%" PRId64,
694 event_class, bt_ctf_event_class_get_name(event_class),
695 bt_ctf_event_class_get_id(event_class));
272df73e
PP
696 ret = -1;
697 goto end;
698 }
699
835b2d10 700 if (context && bt_ctf_field_type_get_type_id(context) !=
1487a16a 701 BT_CTF_FIELD_TYPE_ID_STRUCT) {
f8b979f9
PP
702 BT_LOGW("Invalid parameter: event class's context field type must be a structure: "
703 "addr=%p, name=\"%s\", id=%" PRId64 ", "
704 "context-ft-id=%s",
705 event_class, bt_ctf_event_class_get_name(event_class),
706 bt_ctf_event_class_get_id(event_class),
707 bt_ctf_field_type_id_string(
708 bt_ctf_field_type_get_type_id(context)));
272df73e
PP
709 ret = -1;
710 goto end;
711 }
712
272df73e 713 bt_put(event_class->context);
835b2d10 714 event_class->context = bt_get(context);
f8b979f9
PP
715 BT_LOGV("Set event class's context field type: "
716 "event-class-addr=%p, event-class-name=\"%s\", "
717 "event-class-id=%" PRId64 ", context-ft-addr=%p",
718 event_class, bt_ctf_event_class_get_name(event_class),
719 bt_ctf_event_class_get_id(event_class), context);
272df73e
PP
720end:
721 return ret;
722
723}
724
f8b979f9 725/* Pre-2.0 CTF writer backward compatibility */
272df73e
PP
726void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
727{
728 bt_get(event_class);
729}
730
f8b979f9 731/* Pre-2.0 CTF writer backward compatibility */
272df73e
PP
732void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
733{
734 bt_put(event_class);
735}
736
737BT_HIDDEN
738int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
9ac68eb1 739 uint64_t stream_id_param)
272df73e
PP
740{
741 int ret = 0;
9ac68eb1
PP
742 struct bt_value *obj = NULL;
743 int64_t stream_id = (int64_t) stream_id_param;
744
745 assert(event_class);
f8b979f9 746 assert(stream_id >= 0);
272df73e 747 obj = bt_value_integer_create_init(stream_id);
272df73e 748 if (!obj) {
f8b979f9 749 BT_LOGE_STR("Cannot create integer value object.");
272df73e
PP
750 ret = -1;
751 goto end;
752 }
753
754 ret = bt_ctf_attributes_set_field_value(event_class->attributes,
755 "stream_id", obj);
f8b979f9
PP
756 if (ret) {
757 BT_LOGE("Cannot set event class's attributes's `stream_id` field: "
758 "addr=%p, name=\"%s\", id=%" PRId64 ", ret=%d",
759 event_class, bt_ctf_event_class_get_name(event_class),
760 bt_ctf_event_class_get_id(event_class), ret);
761 goto end;
762 }
272df73e
PP
763
764 if (event_class->frozen) {
f8b979f9 765 BT_LOGV_STR("Freezing event class's attributes because event class is frozen.");
272df73e
PP
766 bt_ctf_attributes_freeze(event_class->attributes);
767 }
768
f8b979f9
PP
769 BT_LOGV("Set event class's stream class ID: "
770 "event-class-addr=%p, event-class-name=\"%s\", "
771 "event-class-id=%" PRId64 ", stream-class-id=%" PRId64,
772 event_class, bt_ctf_event_class_get_name(event_class),
773 bt_ctf_event_class_get_id(event_class), stream_id);
774
272df73e
PP
775end:
776 BT_PUT(obj);
777 return ret;
778}
779
780static
781void bt_ctf_event_class_destroy(struct bt_object *obj)
782{
783 struct bt_ctf_event_class *event_class;
784
785 event_class = container_of(obj, struct bt_ctf_event_class, base);
f8b979f9
PP
786 BT_LOGD("Destroying event class: addr=%p, name=\"%s\", id=%" PRId64,
787 event_class, bt_ctf_event_class_get_name(event_class),
788 bt_ctf_event_class_get_id(event_class));
272df73e
PP
789 bt_ctf_attributes_destroy(event_class->attributes);
790 bt_put(event_class->context);
791 bt_put(event_class->fields);
792 g_free(event_class);
793}
794
795BT_HIDDEN
796void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class)
797{
798 assert(event_class);
f8b979f9
PP
799
800 if (event_class->frozen) {
801 return;
802 }
803
804 BT_LOGD("Freezing event class: addr=%p, name=\"%s\", id=%" PRId64,
805 event_class, bt_ctf_event_class_get_name(event_class),
806 bt_ctf_event_class_get_id(event_class));
272df73e 807 event_class->frozen = 1;
5990dd44
JG
808 event_class->name = bt_ctf_event_class_get_name(event_class);
809 event_class->id = bt_ctf_event_class_get_id(event_class);
272df73e
PP
810 bt_ctf_field_type_freeze(event_class->context);
811 bt_ctf_field_type_freeze(event_class->fields);
812 bt_ctf_attributes_freeze(event_class->attributes);
813}
814
815BT_HIDDEN
816int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
817 struct metadata_context *context)
818{
544d0515
PP
819 int64_t i;
820 int64_t count;
272df73e
PP
821 int ret = 0;
822 struct bt_value *attr_value = NULL;
823
824 assert(event_class);
825 assert(context);
f8b979f9
PP
826 BT_LOGD("Serializing event class's metadata: "
827 "event-class-addr=%p, event-class-name=\"%s\", "
828 "event-class-id=%" PRId64 ", metadata-context-addr=%p",
829 event_class, bt_ctf_event_class_get_name(event_class),
830 bt_ctf_event_class_get_id(event_class), context);
272df73e
PP
831 context->current_indentation_level = 1;
832 g_string_assign(context->field_name, "");
833 g_string_append(context->string, "event {\n");
834 count = bt_ctf_event_class_get_attribute_count(event_class);
f8b979f9 835 assert(count >= 0);
272df73e
PP
836
837 for (i = 0; i < count; ++i) {
838 const char *attr_name = NULL;
839
9ac68eb1 840 attr_name = bt_ctf_event_class_get_attribute_name_by_index(
272df73e 841 event_class, i);
f8b979f9 842 assert(attr_name);
9ac68eb1 843 attr_value = bt_ctf_event_class_get_attribute_value_by_index(
272df73e 844 event_class, i);
f8b979f9 845 assert(attr_value);
272df73e
PP
846
847 switch (bt_value_get_type(attr_value)) {
848 case BT_VALUE_TYPE_INTEGER:
849 {
850 int64_t value;
851
852 ret = bt_value_integer_get(attr_value, &value);
f8b979f9 853 assert(ret == 0);
272df73e
PP
854 g_string_append_printf(context->string,
855 "\t%s = %" PRId64 ";\n", attr_name, value);
856 break;
857 }
858
859 case BT_VALUE_TYPE_STRING:
860 {
861 const char *value;
862
863 ret = bt_value_string_get(attr_value, &value);
f8b979f9 864 assert(ret == 0);
272df73e
PP
865 g_string_append_printf(context->string,
866 "\t%s = \"%s\";\n", attr_name, value);
867 break;
868 }
869
870 default:
871 /* should never happen */
c55a9f58 872 assert(BT_FALSE);
272df73e
PP
873 break;
874 }
875
876 BT_PUT(attr_value);
877 }
878
879 if (event_class->context) {
880 g_string_append(context->string, "\tcontext := ");
881 ret = bt_ctf_field_type_serialize(event_class->context,
882 context);
883 if (ret) {
66871d36 884 BT_LOGW("Cannot serialize event class's context field type's metadata: "
f8b979f9 885 "ret=%d", ret);
272df73e
PP
886 goto end;
887 }
888 g_string_append(context->string, ";\n");
889 }
890
891 if (event_class->fields) {
892 g_string_append(context->string, "\tfields := ");
893 ret = bt_ctf_field_type_serialize(event_class->fields, context);
894 if (ret) {
66871d36 895 BT_LOGW("Cannot serialize event class's payload field type's metadata: "
f8b979f9 896 "ret=%d", ret);
272df73e
PP
897 goto end;
898 }
899 g_string_append(context->string, ";\n");
900 }
901
902 g_string_append(context->string, "};\n\n");
903end:
904 context->current_indentation_level = 0;
905 BT_PUT(attr_value);
906 return ret;
907}
This page took 0.070206 seconds and 4 git commands to generate.