Workaround glib g_module_open bug
[babeltrace.git] / formats / ctf / ir / stream-class.c
CommitLineData
11b0cdc8 1/*
3f043b05 2 * stream-class.c
11b0cdc8 3 *
d2dc44b6 4 * Babeltrace CTF IR - Stream Class
11b0cdc8 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
11b0cdc8
JG
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-writer/clock.h>
30#include <babeltrace/ctf-ir/clock-internal.h>
31#include <babeltrace/ctf-writer/event.h>
272df73e 32#include <babeltrace/ctf-ir/event-class-internal.h>
11b0cdc8 33#include <babeltrace/ctf-ir/event-internal.h>
2e33ac5a
PP
34#include <babeltrace/ctf-ir/field-types-internal.h>
35#include <babeltrace/ctf-ir/fields-internal.h>
11b0cdc8
JG
36#include <babeltrace/ctf-writer/stream.h>
37#include <babeltrace/ctf-ir/stream-class-internal.h>
09840de5 38#include <babeltrace/ctf-ir/validation-internal.h>
8bf65fbd 39#include <babeltrace/ctf-ir/visitor-internal.h>
11b0cdc8 40#include <babeltrace/ctf-writer/functor-internal.h>
654c1444 41#include <babeltrace/ctf-ir/utils.h>
83509119 42#include <babeltrace/ref.h>
11b0cdc8
JG
43#include <babeltrace/compiler.h>
44#include <babeltrace/align.h>
a0b720b2 45#include <babeltrace/endian.h>
11b0cdc8
JG
46
47static
83509119 48void bt_ctf_stream_class_destroy(struct bt_object *obj);
11b0cdc8 49static
662e778c 50int init_event_header(struct bt_ctf_stream_class *stream_class);
11b0cdc8 51static
662e778c 52int init_packet_context(struct bt_ctf_stream_class *stream_class);
11b0cdc8
JG
53
54struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
55{
12c8a1a3 56 int ret;
11b0cdc8
JG
57 struct bt_ctf_stream_class *stream_class = NULL;
58
3ea33115 59 if (name && bt_ctf_validate_identifier(name)) {
11b0cdc8
JG
60 goto error;
61 }
62
63 stream_class = g_new0(struct bt_ctf_stream_class, 1);
64 if (!stream_class) {
65 goto error;
66 }
67
68 stream_class->name = g_string_new(name);
69 stream_class->event_classes = g_ptr_array_new_with_free_func(
e6a8e8e4 70 (GDestroyNotify) bt_object_release);
11b0cdc8 71 if (!stream_class->event_classes) {
83509119 72 goto error;
11b0cdc8
JG
73 }
74
662e778c
JG
75 ret = init_event_header(stream_class);
76 if (ret) {
83509119 77 goto error;
662e778c
JG
78 }
79
80 ret = init_packet_context(stream_class);
12c8a1a3 81 if (ret) {
83509119 82 goto error;
12c8a1a3
JG
83 }
84
83509119 85 bt_object_init(stream_class, bt_ctf_stream_class_destroy);
11b0cdc8
JG
86 return stream_class;
87
11b0cdc8 88error:
83509119 89 BT_PUT(stream_class);
11b0cdc8
JG
90 return stream_class;
91}
92
142c5610
JG
93struct bt_ctf_trace *bt_ctf_stream_class_get_trace(
94 struct bt_ctf_stream_class *stream_class)
95{
e6a8e8e4
JG
96 return (struct bt_ctf_trace *) bt_object_get_parent(
97 stream_class);
142c5610
JG
98}
99
69dc4535
JG
100const char *bt_ctf_stream_class_get_name(
101 struct bt_ctf_stream_class *stream_class)
102{
103 const char *name = NULL;
104
105 if (!stream_class) {
106 goto end;
107 }
108
109 name = stream_class->name->str;
110end:
111 return name;
112}
113
3ea33115
JG
114int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class,
115 const char *name)
116{
117 int ret = 0;
118
119 if (!stream_class || stream_class->frozen) {
120 ret = -1;
121 goto end;
122 }
123
124 g_string_assign(stream_class->name, name);
125end:
126 return ret;
127}
128
2f100782
JG
129struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
130 struct bt_ctf_stream_class *stream_class)
131{
132 struct bt_ctf_clock *clock = NULL;
133
134 if (!stream_class || !stream_class->clock) {
135 goto end;
136 }
137
138 clock = stream_class->clock;
83509119 139 bt_get(clock);
2f100782
JG
140end:
141 return clock;
142}
143
11b0cdc8
JG
144int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
145 struct bt_ctf_clock *clock)
146{
147 int ret = 0;
eee752e5 148 struct bt_ctf_field_type *timestamp_field = NULL;
11b0cdc8
JG
149
150 if (!stream_class || !clock || stream_class->frozen) {
151 ret = -1;
152 goto end;
153 }
154
eee752e5
JG
155 /*
156 * Look for a "timestamp" field in the stream class' event header type
157 * and map the stream's clock to that field if no current mapping is
158 * currently set.
159 */
160 timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name(
161 stream_class->event_header_type, "timestamp");
162 if (timestamp_field) {
163 struct bt_ctf_clock *mapped_clock;
164
165 mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
166 timestamp_field);
167 if (mapped_clock) {
83509119 168 bt_put(mapped_clock);
eee752e5
JG
169 goto end;
170 }
171
172 ret = bt_ctf_field_type_integer_set_mapped_clock(
173 timestamp_field, clock);
174 if (ret) {
175 goto end;
176 }
177 }
178
11b0cdc8 179 if (stream_class->clock) {
83509119 180 bt_put(stream_class->clock);
11b0cdc8
JG
181 }
182
183 stream_class->clock = clock;
83509119 184 bt_get(clock);
11b0cdc8 185end:
eee752e5 186 if (timestamp_field) {
83509119 187 bt_put(timestamp_field);
eee752e5 188 }
11b0cdc8
JG
189 return ret;
190}
191
2f100782
JG
192int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class)
193{
194 int64_t ret;
195
196 if (!stream_class || !stream_class->id_set) {
197 ret = -1;
198 goto end;
199 }
200
201 ret = (int64_t) stream_class->id;
202end:
203 return ret;
204}
205
5ca83563
JG
206BT_HIDDEN
207int _bt_ctf_stream_class_set_id(
208 struct bt_ctf_stream_class *stream_class, uint32_t id)
209{
210 stream_class->id = id;
211 stream_class->id_set = 1;
212 return 0;
213}
214
29664b2a
PP
215struct event_class_set_stream_id_data {
216 uint32_t stream_id;
217 int ret;
218};
219
220static
221void event_class_set_stream_id(gpointer event_class, gpointer data)
222{
223 struct event_class_set_stream_id_data *typed_data = data;
224
225 typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class,
226 typed_data->stream_id);
227}
228
229BT_HIDDEN
230int bt_ctf_stream_class_set_id_no_check(
231 struct bt_ctf_stream_class *stream_class, uint32_t id)
2f100782
JG
232{
233 int ret = 0;
29664b2a
PP
234 struct event_class_set_stream_id_data data =
235 { .stream_id = id, .ret = 0 };
2f100782 236
29664b2a
PP
237 /*
238 * Make sure all event classes have their "stream_id" attribute
239 * set to this value.
240 */
241 g_ptr_array_foreach(stream_class->event_classes,
242 event_class_set_stream_id, &data);
243 ret = data.ret;
244 if (ret) {
2f100782
JG
245 goto end;
246 }
247
5ca83563
JG
248 ret = _bt_ctf_stream_class_set_id(stream_class, id);
249 if (ret) {
250 goto end;
251 }
2f100782
JG
252end:
253 return ret;
254}
255
29664b2a
PP
256int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
257 uint32_t id)
258{
259 int ret = 0;
260
261 if (!stream_class || stream_class->frozen) {
262 ret = -1;
263 goto end;
264 }
265
266 ret = bt_ctf_stream_class_set_id_no_check(stream_class, id);
267end:
268 return ret;
269}
270
0d23acbe
PP
271static
272void event_class_exists(gpointer element, gpointer query)
273{
274 struct bt_ctf_event_class *event_class_a = element;
275 struct search_query *search_query = query;
276 struct bt_ctf_event_class *event_class_b = search_query->value;
277 int64_t id_a, id_b;
278
279 if (search_query->value == element) {
280 search_query->found = 1;
281 goto end;
282 }
283
284 /*
285 * Two event classes cannot share the same name in a given
286 * stream class.
287 */
288 if (!strcmp(bt_ctf_event_class_get_name(event_class_a),
289 bt_ctf_event_class_get_name(event_class_b))) {
290 search_query->found = 1;
291 goto end;
292 }
293
294 /*
295 * Two event classes cannot share the same ID in a given
296 * stream class.
297 */
298 id_a = bt_ctf_event_class_get_id(event_class_a);
299 id_b = bt_ctf_event_class_get_id(event_class_b);
300
301 if (id_a < 0 || id_b < 0) {
302 /* at least one ID is not set: will be automatically set later */
303 goto end;
304 }
305
306 if (id_a == id_b) {
307 search_query->found = 1;
308 goto end;
309 }
310
311end:
312 return;
313}
314
11b0cdc8
JG
315int bt_ctf_stream_class_add_event_class(
316 struct bt_ctf_stream_class *stream_class,
317 struct bt_ctf_event_class *event_class)
318{
319 int ret = 0;
2f100782 320 int64_t event_id;
e6a8e8e4
JG
321 struct bt_ctf_trace *trace = NULL;
322 struct bt_ctf_stream_class *old_stream_class = NULL;
09840de5
PP
323 struct bt_ctf_validation_output validation_output = { 0 };
324 struct bt_ctf_field_type *packet_header_type = NULL;
325 struct bt_ctf_field_type *packet_context_type = NULL;
326 struct bt_ctf_field_type *event_header_type = NULL;
327 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
328 struct bt_ctf_field_type *event_context_type = NULL;
329 struct bt_ctf_field_type *event_payload_type = NULL;
330 const enum bt_ctf_validation_flag validation_flags =
331 BT_CTF_VALIDATION_FLAG_EVENT;
11b0cdc8
JG
332
333 if (!stream_class || !event_class) {
334 ret = -1;
335 goto end;
336 }
337
338 /* Check for duplicate event classes */
339 struct search_query query = { .value = event_class, .found = 0 };
0d23acbe
PP
340 g_ptr_array_foreach(stream_class->event_classes, event_class_exists,
341 &query);
11b0cdc8
JG
342 if (query.found) {
343 ret = -1;
344 goto end;
345 }
346
09840de5 347 old_stream_class = bt_ctf_event_class_get_stream_class(event_class);
e6a8e8e4
JG
348 if (old_stream_class) {
349 /* Event class is already associated to a stream class. */
350 ret = -1;
351 goto end;
352 }
353
09840de5 354 trace = bt_ctf_stream_class_get_trace(stream_class);
e6a8e8e4 355 if (trace) {
09840de5
PP
356 /*
357 * If the stream class is associated with a trace, then
358 * both those objects are frozen. Also, this event class
359 * is about to be frozen.
360 *
361 * Therefore the event class must be validated here.
362 * The trace and stream class should be valid at this
363 * point.
364 */
365 assert(trace->valid);
366 assert(stream_class->valid);
367 packet_header_type =
368 bt_ctf_trace_get_packet_header_type(trace);
369 packet_context_type =
370 bt_ctf_stream_class_get_packet_context_type(
371 stream_class);
372 event_header_type =
373 bt_ctf_stream_class_get_event_header_type(stream_class);
374 stream_event_ctx_type =
375 bt_ctf_stream_class_get_event_context_type(
376 stream_class);
377 event_context_type =
378 bt_ctf_event_class_get_context_type(event_class);
379 event_payload_type =
380 bt_ctf_event_class_get_payload_type(event_class);
381 ret = bt_ctf_validate_class_types(
382 trace->environment, packet_header_type,
383 packet_context_type, event_header_type,
384 stream_event_ctx_type, event_context_type,
385 event_payload_type, trace->valid,
386 stream_class->valid, event_class->valid,
387 &validation_output, validation_flags);
388 BT_PUT(packet_header_type);
389 BT_PUT(packet_context_type);
390 BT_PUT(event_header_type);
391 BT_PUT(stream_event_ctx_type);
392 BT_PUT(event_context_type);
393 BT_PUT(event_payload_type);
394
26079216 395 if (ret) {
09840de5
PP
396 /*
397 * This means something went wrong during the
398 * validation process, not that the objects are
399 * invalid.
400 */
401 goto end;
402 }
403
404 if ((validation_output.valid_flags & validation_flags) !=
405 validation_flags) {
406 /* Invalid event class */
407 ret = -1;
26079216
JG
408 goto end;
409 }
410 }
411
09840de5 412 /* Only set an event ID if none was explicitly set before */
2f100782
JG
413 event_id = bt_ctf_event_class_get_id(event_class);
414 if (event_id < 0) {
415 if (bt_ctf_event_class_set_id(event_class,
416 stream_class->next_event_id++)) {
417 ret = -1;
418 goto end;
419 }
420 }
421
29664b2a
PP
422 ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);
423 if (ret) {
424 goto end;
425 }
426
e6a8e8e4 427 bt_object_set_parent(event_class, stream_class);
09840de5
PP
428
429 if (trace) {
430 /*
431 * At this point we know that the function will be
432 * successful. Therefore we can replace the event
433 * class's field types with what's in the validation
434 * output structure and mark this event class as valid.
435 */
436 bt_ctf_validation_replace_types(NULL, NULL, event_class,
437 &validation_output, validation_flags);
438 event_class->valid = 1;
439
440 /*
441 * Put what was not moved in
442 * bt_ctf_validation_replace_types().
443 */
444 bt_ctf_validation_output_put_types(&validation_output);
445 }
446
447 /* Add to the event classes of the stream class */
11b0cdc8 448 g_ptr_array_add(stream_class->event_classes, event_class);
09840de5
PP
449
450 /* Freeze the event class */
58203827 451 bt_ctf_event_class_freeze(event_class);
5ca83563
JG
452
453 if (stream_class->byte_order) {
454 /*
455 * Only set native byte order if it has been initialized
456 * when the stream class was added to a trace.
457 *
458 * If not set here, this will be set when the stream
09840de5 459 * class is added to a trace.
5ca83563
JG
460 */
461 bt_ctf_event_class_set_native_byte_order(event_class,
462 stream_class->byte_order);
463 }
09840de5 464
9b888ff3
JG
465 /* Notifiy listeners of the trace's schema modification. */
466 if (trace) {
467 struct bt_ctf_ir_element element = { .element = event_class,
468 .type = BT_CTF_IR_TYPE_EVENT_CLASS };
469
470 (void) bt_ctf_trace_element_modification(&element, trace);
471 }
11b0cdc8 472end:
e6a8e8e4
JG
473 BT_PUT(trace);
474 BT_PUT(old_stream_class);
09840de5
PP
475 bt_ctf_validation_output_put_types(&validation_output);
476 assert(!packet_header_type);
477 assert(!packet_context_type);
478 assert(!event_header_type);
479 assert(!stream_event_ctx_type);
480 assert(!event_context_type);
481 assert(!event_payload_type);
482
11b0cdc8
JG
483 return ret;
484}
485
074ee56d 486int bt_ctf_stream_class_get_event_class_count(
69dc4535
JG
487 struct bt_ctf_stream_class *stream_class)
488{
074ee56d 489 int ret;
69dc4535
JG
490
491 if (!stream_class) {
492 ret = -1;
493 goto end;
494 }
495
074ee56d 496 ret = (int) stream_class->event_classes->len;
69dc4535
JG
497end:
498 return ret;
499}
500
501struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(
074ee56d 502 struct bt_ctf_stream_class *stream_class, int index)
69dc4535
JG
503{
504 struct bt_ctf_event_class *event_class = NULL;
505
074ee56d
JG
506 if (!stream_class || index < 0 ||
507 index >= stream_class->event_classes->len) {
69dc4535
JG
508 goto end;
509 }
510
511 event_class = g_ptr_array_index(stream_class->event_classes, index);
83509119 512 bt_get(event_class);
69dc4535
JG
513end:
514 return event_class;
515}
516
517struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(
518 struct bt_ctf_stream_class *stream_class, const char *name)
519{
520 size_t i;
69dc4535
JG
521 struct bt_ctf_event_class *event_class = NULL;
522
523 if (!stream_class || !name) {
524 goto end;
525 }
526
69dc4535 527 for (i = 0; i < stream_class->event_classes->len; i++) {
b8248cc0 528 struct bt_ctf_event_class *cur_event_class =
69dc4535 529 g_ptr_array_index(stream_class->event_classes, i);
b8248cc0
PP
530 const char *cur_event_class_name =
531 bt_ctf_event_class_get_name(cur_event_class);
69dc4535 532
b8248cc0
PP
533 if (!strcmp(name, cur_event_class_name)) {
534 event_class = cur_event_class;
83509119 535 bt_get(event_class);
69dc4535
JG
536 goto end;
537 }
538 }
539end:
540 return event_class;
541}
542
0863f950
PP
543struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id(
544 struct bt_ctf_stream_class *stream_class, uint32_t id)
545{
546 size_t i;
547 struct bt_ctf_event_class *event_class = NULL;
548
549 if (!stream_class) {
550 goto end;
551 }
552
553 for (i = 0; i < stream_class->event_classes->len; i++) {
554 struct bt_ctf_event_class *current_event_class =
555 g_ptr_array_index(stream_class->event_classes, i);
556
557 if (bt_ctf_event_class_get_id(current_event_class) == id) {
558 event_class = current_event_class;
83509119 559 bt_get(event_class);
0863f950
PP
560 goto end;
561 }
562 }
563end:
564 return event_class;
565}
566
12c8a1a3
JG
567struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
568 struct bt_ctf_stream_class *stream_class)
569{
570 struct bt_ctf_field_type *ret = NULL;
571
572 if (!stream_class) {
573 goto end;
574 }
575
576 assert(stream_class->packet_context_type);
83509119 577 bt_get(stream_class->packet_context_type);
12c8a1a3
JG
578 ret = stream_class->packet_context_type;
579end:
580 return ret;
581}
582
583int bt_ctf_stream_class_set_packet_context_type(
584 struct bt_ctf_stream_class *stream_class,
585 struct bt_ctf_field_type *packet_context_type)
586{
587 int ret = 0;
588
0a00bfa1 589 if (!stream_class || !packet_context_type || stream_class->frozen) {
12c8a1a3
JG
590 ret = -1;
591 goto end;
592 }
593
594 assert(stream_class->packet_context_type);
662e778c
JG
595 if (stream_class->packet_context_type == packet_context_type) {
596 goto end;
597 }
b34f4d90 598 if (bt_ctf_field_type_get_type_id(packet_context_type) !=
9a19a512 599 BT_CTF_TYPE_ID_STRUCT) {
12c8a1a3
JG
600 /* A packet context must be a structure */
601 ret = -1;
602 goto end;
603 }
604
83509119
JG
605 bt_put(stream_class->packet_context_type);
606 bt_get(packet_context_type);
12c8a1a3
JG
607 stream_class->packet_context_type = packet_context_type;
608end:
609 return ret;
610}
611
662e778c
JG
612struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
613 struct bt_ctf_stream_class *stream_class)
614{
615 struct bt_ctf_field_type *ret = NULL;
616
617 if (!stream_class || !stream_class->event_header_type) {
618 goto end;
619 }
620
621 assert(stream_class->event_header_type);
83509119 622 bt_get(stream_class->event_header_type);
662e778c
JG
623 ret = stream_class->event_header_type;
624end:
625 return ret;
626}
627
628int bt_ctf_stream_class_set_event_header_type(
629 struct bt_ctf_stream_class *stream_class,
630 struct bt_ctf_field_type *event_header_type)
631{
632 int ret = 0;
633
634 if (!stream_class || !event_header_type || stream_class->frozen) {
635 ret = -1;
636 goto end;
637 }
638
639 assert(stream_class->event_header_type);
640 if (stream_class->event_header_type == event_header_type) {
641 goto end;
642 }
643 if (bt_ctf_field_type_get_type_id(event_header_type) !=
9a19a512 644 BT_CTF_TYPE_ID_STRUCT) {
662e778c
JG
645 /* An event header must be a structure */
646 ret = -1;
647 goto end;
648 }
649
83509119
JG
650 bt_put(stream_class->event_header_type);
651 bt_get(event_header_type);
662e778c
JG
652 stream_class->event_header_type = event_header_type;
653end:
654 return ret;
655}
656
af181248
JG
657struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
658 struct bt_ctf_stream_class *stream_class)
659{
660 struct bt_ctf_field_type *ret = NULL;
661
662 if (!stream_class || !stream_class->event_context_type) {
663 goto end;
664 }
665
666 assert(stream_class->event_context_type);
83509119 667 bt_get(stream_class->event_context_type);
af181248
JG
668 ret = stream_class->event_context_type;
669end:
670 return ret;
671}
672
673int bt_ctf_stream_class_set_event_context_type(
674 struct bt_ctf_stream_class *stream_class,
675 struct bt_ctf_field_type *event_context_type)
676{
677 int ret = 0;
678
679 if (!stream_class || !event_context_type || stream_class->frozen) {
680 ret = -1;
681 goto end;
682 }
683
684 if (bt_ctf_field_type_get_type_id(event_context_type) !=
9a19a512 685 BT_CTF_TYPE_ID_STRUCT) {
af181248
JG
686 /* A packet context must be a structure */
687 ret = -1;
688 goto end;
689 }
690
83509119
JG
691 bt_put(stream_class->event_context_type);
692 bt_get(event_context_type);
af181248
JG
693 stream_class->event_context_type = event_context_type;
694end:
695 return ret;
696}
697
11b0cdc8
JG
698void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
699{
83509119 700 bt_get(stream_class);
11b0cdc8
JG
701}
702
703void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
704{
83509119 705 bt_put(stream_class);
11b0cdc8
JG
706}
707
8bf65fbd
JG
708static
709int get_event_class_count(void *element)
710{
711 return bt_ctf_stream_class_get_event_class_count(
712 (struct bt_ctf_stream_class *) element);
713}
714
715static
716void *get_event_class(void *element, int i)
717{
718 return bt_ctf_stream_class_get_event_class(
719 (struct bt_ctf_stream_class *) element, i);
720}
721
722static
723int visit_event_class(void *element, bt_ctf_ir_visitor visitor,void *data)
724{
725 struct bt_ctf_ir_element ir_element =
726 { .element = element,
727 .type = BT_CTF_IR_TYPE_EVENT_CLASS };
728
729 return visitor(&ir_element, data);
730}
731
732int bt_ctf_stream_class_visit(struct bt_ctf_stream_class *stream_class,
733 bt_ctf_ir_visitor visitor, void *data)
734{
735 int ret;
736 struct bt_ctf_ir_element element =
737 { .element = stream_class,
738 .type = BT_CTF_IR_TYPE_STREAM_CLASS };
739
740 if (!stream_class || !visitor) {
741 ret = -1;
742 goto end;
743 }
744
745 ret = visitor_helper(&element, get_event_class_count,
746 get_event_class,
747 visit_event_class, visitor, data);
748end:
749 return ret;
750}
751
11b0cdc8
JG
752BT_HIDDEN
753void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
754{
755 if (!stream_class) {
756 return;
757 }
758
759 stream_class->frozen = 1;
662e778c 760 bt_ctf_field_type_freeze(stream_class->event_header_type);
12c8a1a3 761 bt_ctf_field_type_freeze(stream_class->packet_context_type);
af181248 762 bt_ctf_field_type_freeze(stream_class->event_context_type);
11b0cdc8 763 bt_ctf_clock_freeze(stream_class->clock);
11b0cdc8
JG
764}
765
11b0cdc8 766BT_HIDDEN
445c3471
PP
767void bt_ctf_stream_class_set_byte_order(
768 struct bt_ctf_stream_class *stream_class, int byte_order)
11b0cdc8 769{
445c3471 770 int i;
11b0cdc8 771
445c3471
PP
772 assert(stream_class);
773 assert(byte_order == LITTLE_ENDIAN || byte_order == BIG_ENDIAN);
774 stream_class->byte_order = byte_order;
5ca83563
JG
775
776 /* Set native byte order to little or big endian */
777 bt_ctf_field_type_set_native_byte_order(
445c3471 778 stream_class->event_header_type, byte_order);
5ca83563 779 bt_ctf_field_type_set_native_byte_order(
445c3471 780 stream_class->packet_context_type, byte_order);
5ca83563 781 bt_ctf_field_type_set_native_byte_order(
445c3471 782 stream_class->event_context_type, byte_order);
5ca83563
JG
783
784 /* Set all events' native byte order */
785 for (i = 0; i < stream_class->event_classes->len; i++) {
445c3471
PP
786 struct bt_ctf_event_class *event_class =
787 g_ptr_array_index(stream_class->event_classes, i);
9849f6ff 788
9849f6ff 789 bt_ctf_event_class_set_native_byte_order(event_class,
445c3471 790 byte_order);
5ca83563 791 }
11b0cdc8
JG
792}
793
794BT_HIDDEN
795int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
796 struct metadata_context *context)
797{
2f100782 798 int64_t ret = 0;
11b0cdc8
JG
799 size_t i;
800
801 g_string_assign(context->field_name, "");
802 context->current_indentation_level = 1;
803 if (!stream_class->id_set) {
804 ret = -1;
805 goto end;
806 }
807
808 g_string_append_printf(context->string,
809 "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
810 stream_class->id);
811 ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
812 context);
813 if (ret) {
814 goto end;
815 }
816
817 g_string_append(context->string, ";\n\n\tpacket.context := ");
818 ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
819 context);
820 if (ret) {
821 goto end;
822 }
823
824 if (stream_class->event_context_type) {
825 g_string_append(context->string, ";\n\n\tevent.context := ");
826 ret = bt_ctf_field_type_serialize(
827 stream_class->event_context_type, context);
828 if (ret) {
829 goto end;
830 }
831 }
832
833 g_string_append(context->string, ";\n};\n\n");
11b0cdc8
JG
834 for (i = 0; i < stream_class->event_classes->len; i++) {
835 struct bt_ctf_event_class *event_class =
836 stream_class->event_classes->pdata[i];
837
11b0cdc8
JG
838 ret = bt_ctf_event_class_serialize(event_class, context);
839 if (ret) {
840 goto end;
841 }
842 }
843end:
844 context->current_indentation_level = 0;
845 return ret;
846}
847
848static
83509119 849void bt_ctf_stream_class_destroy(struct bt_object *obj)
11b0cdc8
JG
850{
851 struct bt_ctf_stream_class *stream_class;
852
83509119
JG
853 stream_class = container_of(obj, struct bt_ctf_stream_class, base);
854 bt_put(stream_class->clock);
11b0cdc8
JG
855
856 if (stream_class->event_classes) {
857 g_ptr_array_free(stream_class->event_classes, TRUE);
858 }
859
860 if (stream_class->name) {
861 g_string_free(stream_class->name, TRUE);
862 }
863
83509119
JG
864 bt_put(stream_class->event_header_type);
865 bt_put(stream_class->packet_context_type);
866 bt_put(stream_class->event_context_type);
11b0cdc8
JG
867 g_free(stream_class);
868}
869
870static
662e778c 871int init_event_header(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
872{
873 int ret = 0;
874 struct bt_ctf_field_type *event_header_type =
875 bt_ctf_field_type_structure_create();
876 struct bt_ctf_field_type *_uint32_t =
877 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
878 struct bt_ctf_field_type *_uint64_t =
879 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
880
881 if (!event_header_type) {
882 ret = -1;
883 goto end;
884 }
885
11b0cdc8
JG
886 ret = bt_ctf_field_type_structure_add_field(event_header_type,
887 _uint32_t, "id");
888 if (ret) {
889 goto end;
890 }
891
892 ret = bt_ctf_field_type_structure_add_field(event_header_type,
893 _uint64_t, "timestamp");
894 if (ret) {
895 goto end;
896 }
897
662e778c 898 if (stream_class->event_header_type) {
83509119 899 bt_put(stream_class->event_header_type);
de876b7f 900 }
662e778c 901 stream_class->event_header_type = event_header_type;
11b0cdc8
JG
902end:
903 if (ret) {
83509119 904 bt_put(event_header_type);
11b0cdc8
JG
905 }
906
83509119
JG
907 bt_put(_uint32_t);
908 bt_put(_uint64_t);
11b0cdc8
JG
909 return ret;
910}
911
912static
662e778c 913int init_packet_context(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
914{
915 int ret = 0;
916 struct bt_ctf_field_type *packet_context_type =
917 bt_ctf_field_type_structure_create();
918 struct bt_ctf_field_type *_uint64_t =
919 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
920
921 if (!packet_context_type) {
922 ret = -1;
923 goto end;
924 }
925
926 /*
927 * We create a stream packet context as proposed in the CTF
928 * specification.
929 */
11b0cdc8
JG
930 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
931 _uint64_t, "timestamp_begin");
932 if (ret) {
933 goto end;
934 }
935
936 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
937 _uint64_t, "timestamp_end");
938 if (ret) {
939 goto end;
940 }
941
942 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
943 _uint64_t, "content_size");
944 if (ret) {
945 goto end;
946 }
947
948 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
949 _uint64_t, "packet_size");
950 if (ret) {
951 goto end;
952 }
953
954 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
955 _uint64_t, "events_discarded");
956 if (ret) {
957 goto end;
958 }
959
83509119 960 bt_put(stream_class->packet_context_type);
11b0cdc8 961 stream_class->packet_context_type = packet_context_type;
11b0cdc8
JG
962end:
963 if (ret) {
83509119 964 bt_put(packet_context_type);
11b0cdc8
JG
965 goto end;
966 }
967
83509119 968 bt_put(_uint64_t);
11b0cdc8
JG
969 return ret;
970}
This page took 0.07288 seconds and 4 git commands to generate.