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