ir: do not create empty structure for optional scope field types
[babeltrace.git] / lib / ctf-ir / stream-class.c
1 /*
2 * stream-class.c
3 *
4 * Babeltrace CTF IR - Stream 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 #define BT_LOG_TAG "STREAM-CLASS"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/ctf-writer/clock.h>
33 #include <babeltrace/ctf-writer/clock-internal.h>
34 #include <babeltrace/ctf-ir/clock-class-internal.h>
35 #include <babeltrace/ctf-writer/event.h>
36 #include <babeltrace/ctf-ir/event-class-internal.h>
37 #include <babeltrace/ctf-ir/event-internal.h>
38 #include <babeltrace/ctf-ir/field-types-internal.h>
39 #include <babeltrace/ctf-ir/fields-internal.h>
40 #include <babeltrace/ctf-writer/stream.h>
41 #include <babeltrace/ctf-ir/stream-class-internal.h>
42 #include <babeltrace/ctf-ir/validation-internal.h>
43 #include <babeltrace/ctf-ir/visitor-internal.h>
44 #include <babeltrace/ctf-writer/functor-internal.h>
45 #include <babeltrace/ctf-ir/utils.h>
46 #include <babeltrace/ref.h>
47 #include <babeltrace/compiler-internal.h>
48 #include <babeltrace/align-internal.h>
49 #include <babeltrace/endian-internal.h>
50 #include <inttypes.h>
51 #include <stdint.h>
52 #include <stdbool.h>
53
54 static
55 void bt_ctf_stream_class_destroy(struct bt_object *obj);
56 static
57 int init_event_header(struct bt_ctf_stream_class *stream_class);
58 static
59 int init_packet_context(struct bt_ctf_stream_class *stream_class);
60
61 struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
62 {
63 struct bt_ctf_stream_class *stream_class;
64 int ret;
65
66 BT_LOGD("Creating default stream class object: name=\"%s\"", name);
67 stream_class = bt_ctf_stream_class_create_empty(name);
68 if (!stream_class) {
69 BT_LOGD_STR("Cannot create empty stream class.");
70 goto error;
71 }
72
73 ret = init_event_header(stream_class);
74 if (ret) {
75 BT_LOGE_STR("Cannot initialize stream class's event header field type.");
76 goto error;
77 }
78
79 ret = init_packet_context(stream_class);
80 if (ret) {
81 BT_LOGE_STR("Cannot initialize stream class's packet context field type.");
82 goto error;
83 }
84
85 BT_LOGD("Created default stream class object: addr=%p, name=\"%s\"",
86 stream_class, name);
87 return stream_class;
88
89 error:
90 BT_PUT(stream_class);
91 return stream_class;
92 }
93
94 struct bt_ctf_stream_class *bt_ctf_stream_class_create_empty(const char *name)
95 {
96 struct bt_ctf_stream_class *stream_class = NULL;
97
98 BT_LOGD("Creating empty stream class object: name=\"%s\"", name);
99
100 if (name && bt_ctf_validate_identifier(name)) {
101 BT_LOGW("Invalid parameter: stream class's name is not a valid CTF identifier: "
102 "name=\"%s\"", name);
103 goto error;
104 }
105
106 stream_class = g_new0(struct bt_ctf_stream_class, 1);
107 if (!stream_class) {
108 BT_LOGE_STR("Failed to allocate one stream class.");
109 goto error;
110 }
111
112 stream_class->name = g_string_new(name);
113 stream_class->event_classes = g_ptr_array_new_with_free_func(
114 (GDestroyNotify) bt_object_release);
115 if (!stream_class->event_classes) {
116 BT_LOGE_STR("Failed to allocate a GPtrArray.");
117 goto error;
118 }
119
120 stream_class->event_classes_ht = g_hash_table_new_full(g_int64_hash,
121 g_int64_equal, g_free, NULL);
122 if (!stream_class->event_classes_ht) {
123 BT_LOGE_STR("Failed to allocate a GHashTable.");
124 goto error;
125 }
126
127 bt_object_init(stream_class, bt_ctf_stream_class_destroy);
128 BT_LOGD("Created empty stream class object: addr=%p, name=\"%s\"",
129 stream_class, name);
130 return stream_class;
131
132 error:
133 BT_PUT(stream_class);
134 return stream_class;
135 }
136
137 struct bt_ctf_trace *bt_ctf_stream_class_get_trace(
138 struct bt_ctf_stream_class *stream_class)
139 {
140 return stream_class ?
141 bt_get(bt_ctf_stream_class_borrow_trace(stream_class)) :
142 NULL;
143 }
144
145 const char *bt_ctf_stream_class_get_name(
146 struct bt_ctf_stream_class *stream_class)
147 {
148 const char *name = NULL;
149
150 if (!stream_class) {
151 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
152 goto end;
153 }
154
155 name = stream_class->name->str;
156 end:
157 return name;
158 }
159
160 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class,
161 const char *name)
162 {
163 int ret = 0;
164
165 if (!stream_class) {
166 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
167 ret = -1;
168 goto end;
169 }
170
171 if (stream_class->frozen) {
172 BT_LOGW("Invalid parameter: stream class is frozen: "
173 "addr=%p, name=\"%s\", id=%" PRId64,
174 stream_class, bt_ctf_stream_class_get_name(stream_class),
175 bt_ctf_stream_class_get_id(stream_class));
176 ret = -1;
177 goto end;
178 }
179
180 g_string_assign(stream_class->name, name);
181 BT_LOGV("Set stream class's name: "
182 "addr=%p, name=\"%s\", id=%" PRId64,
183 stream_class, bt_ctf_stream_class_get_name(stream_class),
184 bt_ctf_stream_class_get_id(stream_class));
185 end:
186 return ret;
187 }
188
189 struct bt_ctf_clock *bt_ctf_stream_class_get_clock(
190 struct bt_ctf_stream_class *stream_class)
191 {
192 struct bt_ctf_clock *clock = NULL;
193
194 if (!stream_class) {
195 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
196 goto end;
197 }
198
199 if (!stream_class->clock) {
200 BT_LOGV("Stream class has no clock: "
201 "addr=%p, name=\"%s\", id=%" PRId64,
202 stream_class, bt_ctf_stream_class_get_name(stream_class),
203 bt_ctf_stream_class_get_id(stream_class));
204 goto end;
205 }
206
207 clock = bt_get(stream_class->clock);
208 end:
209 return clock;
210 }
211
212 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
213 struct bt_ctf_clock *clock)
214 {
215 int ret = 0;
216 struct bt_ctf_field_type *timestamp_field = NULL;
217
218 if (!stream_class || !clock) {
219 BT_LOGW("Invalid parameter: stream class or clock is NULL: "
220 "stream-class-addr=%p, clock-addr=%p",
221 stream_class, clock);
222 ret = -1;
223 goto end;
224 }
225
226 if (stream_class->frozen) {
227 BT_LOGW("Invalid parameter: stream class is frozen: "
228 "addr=%p, name=\"%s\", id=%" PRId64,
229 stream_class, bt_ctf_stream_class_get_name(stream_class),
230 bt_ctf_stream_class_get_id(stream_class));
231 ret = -1;
232 goto end;
233 }
234
235 /* Replace the current clock of this stream class. */
236 bt_put(stream_class->clock);
237 stream_class->clock = bt_get(clock);
238 BT_LOGV("Set stream class's clock: "
239 "addr=%p, name=\"%s\", id=%" PRId64 ", "
240 "clock-addr=%p, clock-name=\"%s\"",
241 stream_class, bt_ctf_stream_class_get_name(stream_class),
242 bt_ctf_stream_class_get_id(stream_class),
243 stream_class->clock,
244 bt_ctf_clock_get_name(stream_class->clock));
245
246 end:
247 bt_put(timestamp_field);
248 return ret;
249 }
250
251 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class)
252 {
253 int64_t ret;
254
255 if (!stream_class) {
256 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
257 ret = (int64_t) -1;
258 goto end;
259 }
260
261 if (!stream_class->id_set) {
262 BT_LOGV("Stream class's ID is not set: addr=%p, name=\"%s\"",
263 stream_class,
264 bt_ctf_stream_class_get_name(stream_class));
265 ret = (int64_t) -1;
266 goto end;
267 }
268
269 ret = stream_class->id;
270 end:
271 return ret;
272 }
273
274 BT_HIDDEN
275 void _bt_ctf_stream_class_set_id(
276 struct bt_ctf_stream_class *stream_class, int64_t id)
277 {
278 assert(stream_class);
279 stream_class->id = id;
280 stream_class->id_set = 1;
281 BT_LOGV("Set stream class's ID (internal): "
282 "addr=%p, name=\"%s\", id=%" PRId64,
283 stream_class, bt_ctf_stream_class_get_name(stream_class),
284 bt_ctf_stream_class_get_id(stream_class));
285 }
286
287 struct event_class_set_stream_class_id_data {
288 int64_t stream_class_id;
289 int ret;
290 };
291
292 static
293 void event_class_set_stream_id(gpointer event_class, gpointer data)
294 {
295 struct event_class_set_stream_class_id_data *typed_data = data;
296
297 typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class,
298 typed_data->stream_class_id);
299 }
300
301 BT_HIDDEN
302 int bt_ctf_stream_class_set_id_no_check(
303 struct bt_ctf_stream_class *stream_class, int64_t id)
304 {
305 int ret = 0;
306 struct event_class_set_stream_class_id_data data =
307 { .stream_class_id = id, .ret = 0 };
308
309 /*
310 * Make sure all event classes have their "stream_id" attribute
311 * set to this value.
312 */
313 g_ptr_array_foreach(stream_class->event_classes,
314 event_class_set_stream_id, &data);
315 ret = data.ret;
316 if (ret) {
317 BT_LOGE("Cannot set the IDs of all stream class's event classes: "
318 "addr=%p, name=\"%s\", id=%" PRId64,
319 stream_class, bt_ctf_stream_class_get_name(stream_class),
320 bt_ctf_stream_class_get_id(stream_class));
321 goto end;
322 }
323
324 _bt_ctf_stream_class_set_id(stream_class, id);
325 end:
326 return ret;
327 }
328
329 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
330 uint64_t id_param)
331 {
332 int ret = 0;
333 int64_t id = (int64_t) id_param;
334
335 if (!stream_class) {
336 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
337 ret = -1;
338 goto end;
339 }
340
341 if (stream_class->frozen) {
342 BT_LOGW("Invalid parameter: stream class is frozen: "
343 "addr=%p, name=\"%s\", id=%" PRId64,
344 stream_class, bt_ctf_stream_class_get_name(stream_class),
345 bt_ctf_stream_class_get_id(stream_class));
346 ret = -1;
347 goto end;
348 }
349
350 if (id < 0) {
351 BT_LOGW("Invalid parameter: invalid stream class's ID: "
352 "stream-class-addr=%p, stream-class-name=\"%s\", "
353 "stream-class-id=%" PRId64 ", id=%" PRIu64,
354 stream_class, bt_ctf_stream_class_get_name(stream_class),
355 bt_ctf_stream_class_get_id(stream_class),
356 id_param);
357 ret = -1;
358 goto end;
359 }
360
361 ret = bt_ctf_stream_class_set_id_no_check(stream_class, id);
362 if (ret == 0) {
363 BT_LOGV("Set stream class's ID: "
364 "addr=%p, name=\"%s\", id=%" PRId64,
365 stream_class, bt_ctf_stream_class_get_name(stream_class),
366 bt_ctf_stream_class_get_id(stream_class));
367 }
368 end:
369 return ret;
370 }
371
372 static
373 void event_class_exists(gpointer element, gpointer query)
374 {
375 struct bt_ctf_event_class *event_class_a = element;
376 struct search_query *search_query = query;
377 struct bt_ctf_event_class *event_class_b = search_query->value;
378 int64_t id_a, id_b;
379
380 if (search_query->value == element) {
381 search_query->found = 1;
382 goto end;
383 }
384
385 /*
386 * Two event classes cannot share the same ID in a given
387 * stream class.
388 */
389 id_a = bt_ctf_event_class_get_id(event_class_a);
390 id_b = bt_ctf_event_class_get_id(event_class_b);
391
392 if (id_a < 0 || id_b < 0) {
393 /* at least one ID is not set: will be automatically set later */
394 goto end;
395 }
396
397 if (id_a == id_b) {
398 BT_LOGW("Event class with this ID already exists in the stream class: "
399 "id=%" PRId64 ", name=\"%s\"",
400 id_a, bt_ctf_event_class_get_name(event_class_a));
401 search_query->found = 1;
402 goto end;
403 }
404
405 end:
406 return;
407 }
408
409 int bt_ctf_stream_class_add_event_class(
410 struct bt_ctf_stream_class *stream_class,
411 struct bt_ctf_event_class *event_class)
412 {
413 int ret = 0;
414 int64_t *event_id = NULL;
415 struct bt_ctf_trace *trace = NULL;
416 struct bt_ctf_stream_class *old_stream_class = NULL;
417 struct bt_ctf_validation_output validation_output = { 0 };
418 struct bt_ctf_field_type *packet_header_type = NULL;
419 struct bt_ctf_field_type *packet_context_type = NULL;
420 struct bt_ctf_field_type *event_header_type = NULL;
421 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
422 struct bt_ctf_field_type *event_context_type = NULL;
423 struct bt_ctf_field_type *event_payload_type = NULL;
424 const enum bt_ctf_validation_flag validation_flags =
425 BT_CTF_VALIDATION_FLAG_EVENT;
426
427 if (!stream_class || !event_class) {
428 BT_LOGW("Invalid parameter: stream class or event class is NULL: "
429 "stream-class-addr=%p, event-class-addr=%p",
430 stream_class, event_class);
431 ret = -1;
432 goto end;
433 }
434
435 BT_LOGD("Adding event class to stream class: "
436 "stream-class-addr=%p, stream-class-name=\"%s\", "
437 "stream-class-id=%" PRId64 ", event-class-addr=%p, "
438 "event-class-name=\"%s\", event-class-id=%" PRId64,
439 stream_class, bt_ctf_stream_class_get_name(stream_class),
440 bt_ctf_stream_class_get_id(stream_class),
441 event_class,
442 bt_ctf_event_class_get_name(event_class),
443 bt_ctf_event_class_get_id(event_class));
444
445 trace = bt_ctf_stream_class_get_trace(stream_class);
446 if (trace && trace->is_static) {
447 ret = -1;
448 goto end;
449 }
450
451 event_id = g_new(int64_t, 1);
452 if (!event_id) {
453 BT_LOGE_STR("Failed to allocate one int64_t.");
454 ret = -1;
455 goto end;
456 }
457
458 /* Check for duplicate event classes */
459 struct search_query query = { .value = event_class, .found = 0 };
460 g_ptr_array_foreach(stream_class->event_classes, event_class_exists,
461 &query);
462 if (query.found) {
463 BT_LOGW_STR("Another event class part of this stream class has the same ID.");
464 ret = -1;
465 goto end;
466 }
467
468 old_stream_class = bt_ctf_event_class_get_stream_class(event_class);
469 if (old_stream_class) {
470 /* Event class is already associated to a stream class. */
471 BT_LOGW("Event class is already part of another stream class: "
472 "event-class-stream-class-addr=%p, "
473 "event-class-stream-class-name=\"%s\", "
474 "event-class-stream-class-id=%" PRId64,
475 old_stream_class,
476 bt_ctf_stream_class_get_name(old_stream_class),
477 bt_ctf_stream_class_get_id(old_stream_class));
478 ret = -1;
479 goto end;
480 }
481
482 if (trace) {
483 /*
484 * If the stream class is associated with a trace, then
485 * both those objects are frozen. Also, this event class
486 * is about to be frozen.
487 *
488 * Therefore the event class must be validated here.
489 * The trace and stream class should be valid at this
490 * point.
491 */
492 assert(trace->valid);
493 assert(stream_class->valid);
494 packet_header_type =
495 bt_ctf_trace_get_packet_header_type(trace);
496 packet_context_type =
497 bt_ctf_stream_class_get_packet_context_type(
498 stream_class);
499 event_header_type =
500 bt_ctf_stream_class_get_event_header_type(stream_class);
501 stream_event_ctx_type =
502 bt_ctf_stream_class_get_event_context_type(
503 stream_class);
504 event_context_type =
505 bt_ctf_event_class_get_context_type(event_class);
506 event_payload_type =
507 bt_ctf_event_class_get_payload_type(event_class);
508 ret = bt_ctf_validate_class_types(
509 trace->environment, packet_header_type,
510 packet_context_type, event_header_type,
511 stream_event_ctx_type, event_context_type,
512 event_payload_type, trace->valid,
513 stream_class->valid, event_class->valid,
514 &validation_output, validation_flags);
515 BT_PUT(packet_header_type);
516 BT_PUT(packet_context_type);
517 BT_PUT(event_header_type);
518 BT_PUT(stream_event_ctx_type);
519 BT_PUT(event_context_type);
520 BT_PUT(event_payload_type);
521
522 if (ret) {
523 /*
524 * This means something went wrong during the
525 * validation process, not that the objects are
526 * invalid.
527 */
528 BT_LOGE("Failed to validate event class: ret=%d", ret);
529 goto end;
530 }
531
532 if ((validation_output.valid_flags & validation_flags) !=
533 validation_flags) {
534 /* Invalid event class */
535 BT_LOGW("Invalid trace, stream class, or event class: "
536 "valid-flags=0x%x",
537 validation_output.valid_flags);
538 ret = -1;
539 goto end;
540 }
541 }
542
543 /* Only set an event ID if none was explicitly set before */
544 *event_id = bt_ctf_event_class_get_id(event_class);
545 if (*event_id < 0) {
546 BT_LOGV("Event class has no ID: automatically setting it: "
547 "id=%" PRId64, stream_class->next_event_id);
548
549 if (bt_ctf_event_class_set_id(event_class,
550 stream_class->next_event_id)) {
551 BT_LOGE("Cannot set event class's ID: id=%" PRId64,
552 stream_class->next_event_id);
553 ret = -1;
554 goto end;
555 }
556 stream_class->next_event_id++;
557 *event_id = stream_class->next_event_id;
558 }
559
560 ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);
561 if (ret) {
562 BT_LOGE("Cannot set event class's stream class ID attribute: ret=%d",
563 ret);
564 goto end;
565 }
566
567 bt_object_set_parent(event_class, stream_class);
568
569 if (trace) {
570 /*
571 * At this point we know that the function will be
572 * successful. Therefore we can replace the event
573 * class's field types with what's in the validation
574 * output structure and mark this event class as valid.
575 */
576 bt_ctf_validation_replace_types(NULL, NULL, event_class,
577 &validation_output, validation_flags);
578 event_class->valid = 1;
579
580 /*
581 * Put what was not moved in
582 * bt_ctf_validation_replace_types().
583 */
584 bt_ctf_validation_output_put_types(&validation_output);
585 }
586
587 /* Add to the event classes of the stream class */
588 g_ptr_array_add(stream_class->event_classes, event_class);
589 g_hash_table_insert(stream_class->event_classes_ht, event_id,
590 event_class);
591 event_id = NULL;
592
593 /* Freeze the event class */
594 bt_ctf_event_class_freeze(event_class);
595
596 /* Notifiy listeners of the trace's schema modification. */
597 if (trace) {
598 struct bt_ctf_object obj = { .object = event_class,
599 .type = BT_CTF_OBJECT_TYPE_EVENT_CLASS };
600
601 (void) bt_ctf_trace_object_modification(&obj, trace);
602 }
603
604 BT_LOGD("Added event class to stream class: "
605 "stream-class-addr=%p, stream-class-name=\"%s\", "
606 "stream-class-id=%" PRId64 ", event-class-addr=%p, "
607 "event-class-name=\"%s\", event-class-id=%" PRId64,
608 stream_class, bt_ctf_stream_class_get_name(stream_class),
609 bt_ctf_stream_class_get_id(stream_class),
610 event_class,
611 bt_ctf_event_class_get_name(event_class),
612 bt_ctf_event_class_get_id(event_class));
613
614 end:
615 BT_PUT(trace);
616 BT_PUT(old_stream_class);
617 bt_ctf_validation_output_put_types(&validation_output);
618 assert(!packet_header_type);
619 assert(!packet_context_type);
620 assert(!event_header_type);
621 assert(!stream_event_ctx_type);
622 assert(!event_context_type);
623 assert(!event_payload_type);
624 g_free(event_id);
625
626 return ret;
627 }
628
629 int64_t bt_ctf_stream_class_get_event_class_count(
630 struct bt_ctf_stream_class *stream_class)
631 {
632 int64_t ret;
633
634 if (!stream_class) {
635 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
636 ret = (int64_t) -1;
637 goto end;
638 }
639
640 ret = (int64_t) stream_class->event_classes->len;
641 end:
642 return ret;
643 }
644
645 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_index(
646 struct bt_ctf_stream_class *stream_class, uint64_t index)
647 {
648 struct bt_ctf_event_class *event_class = NULL;
649
650 if (!stream_class) {
651 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
652 goto end;
653 }
654
655 if (index >= stream_class->event_classes->len) {
656 BT_LOGW("Invalid parameter: index is out of bounds: "
657 "addr=%p, name=\"%s\", id=%" PRId64 ", "
658 "index=%" PRIu64 ", count=%u",
659 stream_class, bt_ctf_stream_class_get_name(stream_class),
660 bt_ctf_stream_class_get_id(stream_class),
661 index, stream_class->event_classes->len);
662 goto end;
663 }
664
665 event_class = g_ptr_array_index(stream_class->event_classes, index);
666 bt_get(event_class);
667 end:
668 return event_class;
669 }
670
671 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id(
672 struct bt_ctf_stream_class *stream_class, uint64_t id)
673 {
674 int64_t id_key = (int64_t) id;
675 struct bt_ctf_event_class *event_class = NULL;
676
677 if (!stream_class) {
678 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
679 goto end;
680 }
681
682 if (id_key < 0) {
683 BT_LOGW("Invalid parameter: invalid event class's ID: "
684 "stream-class-addr=%p, stream-class-name=\"%s\", "
685 "stream-class-id=%" PRId64 ", event-class-id=%" PRIu64,
686 stream_class,
687 bt_ctf_stream_class_get_name(stream_class),
688 bt_ctf_stream_class_get_id(stream_class), id);
689 goto end;
690 }
691
692 event_class = g_hash_table_lookup(stream_class->event_classes_ht,
693 &id_key);
694 bt_get(event_class);
695 end:
696 return event_class;
697 }
698
699 struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(
700 struct bt_ctf_stream_class *stream_class)
701 {
702 struct bt_ctf_field_type *ret = NULL;
703
704 if (!stream_class) {
705 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
706 goto end;
707 }
708
709 bt_get(stream_class->packet_context_type);
710 ret = stream_class->packet_context_type;
711 end:
712 return ret;
713 }
714
715 int bt_ctf_stream_class_set_packet_context_type(
716 struct bt_ctf_stream_class *stream_class,
717 struct bt_ctf_field_type *packet_context_type)
718 {
719 int ret = 0;
720
721 if (!stream_class) {
722 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
723 ret = -1;
724 goto end;
725 }
726
727 if (stream_class->frozen) {
728 BT_LOGW("Invalid parameter: stream class is frozen: "
729 "addr=%p, name=\"%s\", id=%" PRId64,
730 stream_class, bt_ctf_stream_class_get_name(stream_class),
731 bt_ctf_stream_class_get_id(stream_class));
732 ret = -1;
733 goto end;
734 }
735
736 if (packet_context_type &&
737 bt_ctf_field_type_get_type_id(packet_context_type) !=
738 BT_CTF_FIELD_TYPE_ID_STRUCT) {
739 /* A packet context must be a structure. */
740 BT_LOGW("Invalid parameter: stream class's packet context field type must be a structure: "
741 "addr=%p, name=\"%s\", id=%" PRId64 ", "
742 "packet-context-ft-addr=%p, packet-context-ft-id=%s",
743 stream_class, bt_ctf_stream_class_get_name(stream_class),
744 bt_ctf_stream_class_get_id(stream_class),
745 packet_context_type,
746 bt_ctf_field_type_id_string(
747 bt_ctf_field_type_get_type_id(packet_context_type)));
748 ret = -1;
749 goto end;
750 }
751
752 bt_put(stream_class->packet_context_type);
753 bt_get(packet_context_type);
754 stream_class->packet_context_type = packet_context_type;
755 BT_LOGV("Set stream class's packet context field type: "
756 "addr=%p, name=\"%s\", id=%" PRId64 ", "
757 "packet-context-ft-addr=%p",
758 stream_class, bt_ctf_stream_class_get_name(stream_class),
759 bt_ctf_stream_class_get_id(stream_class),
760 packet_context_type);
761
762 end:
763 return ret;
764 }
765
766 struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type(
767 struct bt_ctf_stream_class *stream_class)
768 {
769 struct bt_ctf_field_type *ret = NULL;
770
771 if (!stream_class) {
772 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
773 goto end;
774 }
775
776 if (!stream_class->event_header_type) {
777 BT_LOGV("Stream class has no event header field type: "
778 "addr=%p, name=\"%s\", id=%" PRId64,
779 stream_class, bt_ctf_stream_class_get_name(stream_class),
780 bt_ctf_stream_class_get_id(stream_class));
781 goto end;
782 }
783
784 bt_get(stream_class->event_header_type);
785 ret = stream_class->event_header_type;
786 end:
787 return ret;
788 }
789
790 int bt_ctf_stream_class_set_event_header_type(
791 struct bt_ctf_stream_class *stream_class,
792 struct bt_ctf_field_type *event_header_type)
793 {
794 int ret = 0;
795
796 if (!stream_class) {
797 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
798 ret = -1;
799 goto end;
800 }
801
802 if (stream_class->frozen) {
803 BT_LOGW("Invalid parameter: stream class is frozen: "
804 "addr=%p, name=\"%s\", id=%" PRId64,
805 stream_class, bt_ctf_stream_class_get_name(stream_class),
806 bt_ctf_stream_class_get_id(stream_class));
807 ret = -1;
808 goto end;
809 }
810
811 if (event_header_type &&
812 bt_ctf_field_type_get_type_id(event_header_type) !=
813 BT_CTF_FIELD_TYPE_ID_STRUCT) {
814 /* An event header must be a structure. */
815 BT_LOGW("Invalid parameter: stream class's event header field type must be a structure: "
816 "addr=%p, name=\"%s\", id=%" PRId64 ", "
817 "event-header-ft-addr=%p, event-header-ft-id=%s",
818 stream_class, bt_ctf_stream_class_get_name(stream_class),
819 bt_ctf_stream_class_get_id(stream_class),
820 event_header_type,
821 bt_ctf_field_type_id_string(
822 bt_ctf_field_type_get_type_id(event_header_type)));
823 ret = -1;
824 goto end;
825 }
826
827 bt_put(stream_class->event_header_type);
828 stream_class->event_header_type = bt_get(event_header_type);
829 BT_LOGV("Set stream class's event header field type: "
830 "addr=%p, name=\"%s\", id=%" PRId64 ", "
831 "event-header-ft-addr=%p",
832 stream_class, bt_ctf_stream_class_get_name(stream_class),
833 bt_ctf_stream_class_get_id(stream_class),
834 event_header_type);
835 end:
836 return ret;
837 }
838
839 struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(
840 struct bt_ctf_stream_class *stream_class)
841 {
842 struct bt_ctf_field_type *ret = NULL;
843
844 if (!stream_class) {
845 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
846 goto end;
847 }
848
849 if (!stream_class->event_context_type) {
850 goto end;
851 }
852
853 bt_get(stream_class->event_context_type);
854 ret = stream_class->event_context_type;
855 end:
856 return ret;
857 }
858
859 int bt_ctf_stream_class_set_event_context_type(
860 struct bt_ctf_stream_class *stream_class,
861 struct bt_ctf_field_type *event_context_type)
862 {
863 int ret = 0;
864
865 if (!stream_class) {
866 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
867 ret = -1;
868 goto end;
869 }
870
871 if (stream_class->frozen) {
872 BT_LOGW("Invalid parameter: stream class is frozen: "
873 "addr=%p, name=\"%s\", id=%" PRId64,
874 stream_class, bt_ctf_stream_class_get_name(stream_class),
875 bt_ctf_stream_class_get_id(stream_class));
876 ret = -1;
877 goto end;
878 }
879
880 if (event_context_type &&
881 bt_ctf_field_type_get_type_id(event_context_type) !=
882 BT_CTF_FIELD_TYPE_ID_STRUCT) {
883 /* A packet context must be a structure. */
884 BT_LOGW("Invalid parameter: stream class's event context field type must be a structure: "
885 "addr=%p, name=\"%s\", id=%" PRId64 ", "
886 "event-context-ft-addr=%p, event-context-ft-id=%s",
887 stream_class, bt_ctf_stream_class_get_name(stream_class),
888 bt_ctf_stream_class_get_id(stream_class),
889 event_context_type,
890 bt_ctf_field_type_id_string(
891 bt_ctf_field_type_get_type_id(event_context_type)));
892 ret = -1;
893 goto end;
894 }
895
896 bt_put(stream_class->event_context_type);
897 stream_class->event_context_type = bt_get(event_context_type);
898 BT_LOGV("Set stream class's event context field type: "
899 "addr=%p, name=\"%s\", id=%" PRId64 ", "
900 "event-context-ft-addr=%p",
901 stream_class, bt_ctf_stream_class_get_name(stream_class),
902 bt_ctf_stream_class_get_id(stream_class),
903 event_context_type);
904 end:
905 return ret;
906 }
907
908 /* Pre-2.0 CTF writer backward compatibility */
909 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
910 {
911 bt_get(stream_class);
912 }
913
914 /* Pre-2.0 CTF writer backward compatibility */
915 void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
916 {
917 bt_put(stream_class);
918 }
919
920 static
921 int64_t get_event_class_count(void *element)
922 {
923 return bt_ctf_stream_class_get_event_class_count(
924 (struct bt_ctf_stream_class *) element);
925 }
926
927 static
928 void *get_event_class(void *element, int i)
929 {
930 return bt_ctf_stream_class_get_event_class_by_index(
931 (struct bt_ctf_stream_class *) element, i);
932 }
933
934 static
935 int visit_event_class(void *object, bt_ctf_visitor visitor,void *data)
936 {
937 struct bt_ctf_object obj =
938 { .object = object,
939 .type = BT_CTF_OBJECT_TYPE_EVENT_CLASS };
940
941 return visitor(&obj, data);
942 }
943
944 int bt_ctf_stream_class_visit(struct bt_ctf_stream_class *stream_class,
945 bt_ctf_visitor visitor, void *data)
946 {
947 int ret;
948 struct bt_ctf_object obj =
949 { .object = stream_class,
950 .type = BT_CTF_OBJECT_TYPE_STREAM_CLASS };
951
952 if (!stream_class || !visitor) {
953 BT_LOGW("Invalid parameter: stream class or visitor is NULL: "
954 "stream-class-addr=%p, visitor=%p",
955 stream_class, visitor);
956 ret = -1;
957 goto end;
958 }
959
960 ret = visitor_helper(&obj, get_event_class_count,
961 get_event_class,
962 visit_event_class, visitor, data);
963 BT_LOGV("visitor_helper() returned: ret=%d", ret);
964 end:
965 return ret;
966 }
967
968 BT_HIDDEN
969 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
970 {
971 if (!stream_class || stream_class->frozen) {
972 return;
973 }
974
975 BT_LOGD("Freezing stream class: addr=%p, name=\"%s\", id=%" PRId64,
976 stream_class, bt_ctf_stream_class_get_name(stream_class),
977 bt_ctf_stream_class_get_id(stream_class));
978 stream_class->frozen = 1;
979 bt_ctf_field_type_freeze(stream_class->event_header_type);
980 bt_ctf_field_type_freeze(stream_class->packet_context_type);
981 bt_ctf_field_type_freeze(stream_class->event_context_type);
982
983 if (stream_class->clock) {
984 bt_ctf_clock_class_freeze(stream_class->clock->clock_class);
985 }
986 }
987
988 BT_HIDDEN
989 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
990 struct metadata_context *context)
991 {
992 int ret = 0;
993 size_t i;
994
995 BT_LOGD("Serializing stream class's metadata: "
996 "stream-class-addr=%p, stream-class-name=\"%s\", "
997 "stream-class-id=%" PRId64 ", metadata-context-addr=%p",
998 stream_class, bt_ctf_stream_class_get_name(stream_class),
999 bt_ctf_stream_class_get_id(stream_class), context);
1000 g_string_assign(context->field_name, "");
1001 context->current_indentation_level = 1;
1002 if (!stream_class->id_set) {
1003 BT_LOGW_STR("Stream class's ID is not set.");
1004 ret = -1;
1005 goto end;
1006 }
1007
1008 g_string_append_printf(context->string,
1009 "stream {\n\tid = %" PRId64 ";\n", stream_class->id);
1010 if (stream_class->event_header_type) {
1011 BT_LOGD_STR("Serializing stream class's event header field type's metadata.");
1012 g_string_append(context->string, "\tevent.header := ");
1013 ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
1014 context);
1015 if (ret) {
1016 BT_LOGW("Cannot serialize stream class's event header field type's metadata: "
1017 "ret=%d", ret);
1018 goto end;
1019 }
1020 g_string_append(context->string, ";");
1021 }
1022
1023
1024 if (stream_class->packet_context_type) {
1025 BT_LOGD_STR("Serializing stream class's packet context field type's metadata.");
1026 g_string_append(context->string, "\n\n\tpacket.context := ");
1027 ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
1028 context);
1029 if (ret) {
1030 BT_LOGW("Cannot serialize stream class's packet context field type's metadata: "
1031 "ret=%d", ret);
1032 goto end;
1033 }
1034 g_string_append(context->string, ";");
1035 }
1036
1037 if (stream_class->event_context_type) {
1038 BT_LOGD_STR("Serializing stream class's event context field type's metadata.");
1039 g_string_append(context->string, "\n\n\tevent.context := ");
1040 ret = bt_ctf_field_type_serialize(
1041 stream_class->event_context_type, context);
1042 if (ret) {
1043 BT_LOGW("Cannot serialize stream class's event context field type's metadata: "
1044 "ret=%d", ret);
1045 goto end;
1046 }
1047 g_string_append(context->string, ";");
1048 }
1049
1050 g_string_append(context->string, "\n};\n\n");
1051
1052 for (i = 0; i < stream_class->event_classes->len; i++) {
1053 struct bt_ctf_event_class *event_class =
1054 stream_class->event_classes->pdata[i];
1055
1056 ret = bt_ctf_event_class_serialize(event_class, context);
1057 if (ret) {
1058 BT_LOGW("Cannot serialize event class's metadata: "
1059 "event-class-addr=%p, event-class-name=\"%s\", "
1060 "event-class-id=%" PRId64,
1061 event_class,
1062 bt_ctf_event_class_get_name(event_class),
1063 bt_ctf_event_class_get_id(event_class));
1064 goto end;
1065 }
1066 }
1067 end:
1068 context->current_indentation_level = 0;
1069 return ret;
1070 }
1071
1072 static
1073 void bt_ctf_stream_class_destroy(struct bt_object *obj)
1074 {
1075 struct bt_ctf_stream_class *stream_class;
1076
1077 stream_class = container_of(obj, struct bt_ctf_stream_class, base);
1078 BT_LOGD("Destroying stream class: addr=%p, name=\"%s\", id=%" PRId64,
1079 stream_class, bt_ctf_stream_class_get_name(stream_class),
1080 bt_ctf_stream_class_get_id(stream_class));
1081 bt_put(stream_class->clock);
1082
1083 if (stream_class->event_classes_ht) {
1084 g_hash_table_destroy(stream_class->event_classes_ht);
1085 }
1086 if (stream_class->event_classes) {
1087 BT_LOGD_STR("Destroying event classes.");
1088 g_ptr_array_free(stream_class->event_classes, TRUE);
1089 }
1090
1091 if (stream_class->name) {
1092 g_string_free(stream_class->name, TRUE);
1093 }
1094
1095 BT_LOGD_STR("Putting event header field type.");
1096 bt_put(stream_class->event_header_type);
1097 BT_LOGD_STR("Putting packet context field type.");
1098 bt_put(stream_class->packet_context_type);
1099 BT_LOGD_STR("Putting event context field type.");
1100 bt_put(stream_class->event_context_type);
1101 g_free(stream_class);
1102 }
1103
1104 static
1105 int init_event_header(struct bt_ctf_stream_class *stream_class)
1106 {
1107 int ret = 0;
1108 struct bt_ctf_field_type *event_header_type =
1109 bt_ctf_field_type_structure_create();
1110 struct bt_ctf_field_type *_uint32_t =
1111 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
1112 struct bt_ctf_field_type *_uint64_t =
1113 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
1114
1115 if (!event_header_type) {
1116 BT_LOGE_STR("Cannot create empty structure field type.");
1117 ret = -1;
1118 goto end;
1119 }
1120
1121 ret = bt_ctf_field_type_structure_add_field(event_header_type,
1122 _uint32_t, "id");
1123 if (ret) {
1124 BT_LOGE_STR("Cannot add `id` field to event header field type.");
1125 goto end;
1126 }
1127
1128 ret = bt_ctf_field_type_structure_add_field(event_header_type,
1129 _uint64_t, "timestamp");
1130 if (ret) {
1131 BT_LOGE_STR("Cannot add `timestamp` field to event header field type.");
1132 goto end;
1133 }
1134
1135 BT_MOVE(stream_class->event_header_type, event_header_type);
1136 end:
1137 if (ret) {
1138 bt_put(event_header_type);
1139 }
1140
1141 bt_put(_uint32_t);
1142 bt_put(_uint64_t);
1143 return ret;
1144 }
1145
1146 static
1147 int init_packet_context(struct bt_ctf_stream_class *stream_class)
1148 {
1149 int ret = 0;
1150 struct bt_ctf_field_type *packet_context_type =
1151 bt_ctf_field_type_structure_create();
1152 struct bt_ctf_field_type *_uint64_t =
1153 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
1154 struct bt_ctf_field_type *ts_begin_end_uint64_t;
1155
1156 if (!packet_context_type) {
1157 BT_LOGE_STR("Cannot create empty structure field type.");
1158 ret = -1;
1159 goto end;
1160 }
1161
1162 ts_begin_end_uint64_t = bt_ctf_field_type_copy(_uint64_t);
1163 if (!ts_begin_end_uint64_t) {
1164 BT_LOGE_STR("Cannot copy integer field type for `timestamp_begin` and `timestamp_end` fields.");
1165 ret = -1;
1166 goto end;
1167 }
1168
1169 /*
1170 * We create a stream packet context as proposed in the CTF
1171 * specification.
1172 */
1173 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1174 ts_begin_end_uint64_t, "timestamp_begin");
1175 if (ret) {
1176 BT_LOGE_STR("Cannot add `timestamp_begin` field to event header field type.");
1177 goto end;
1178 }
1179
1180 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1181 ts_begin_end_uint64_t, "timestamp_end");
1182 if (ret) {
1183 BT_LOGE_STR("Cannot add `timestamp_end` field to event header field type.");
1184 goto end;
1185 }
1186
1187 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1188 _uint64_t, "content_size");
1189 if (ret) {
1190 BT_LOGE_STR("Cannot add `content_size` field to event header field type.");
1191 goto end;
1192 }
1193
1194 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1195 _uint64_t, "packet_size");
1196 if (ret) {
1197 BT_LOGE_STR("Cannot add `packet_size` field to event header field type.");
1198 goto end;
1199 }
1200
1201 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1202 _uint64_t, "events_discarded");
1203 if (ret) {
1204 BT_LOGE_STR("Cannot add `events_discarded` field to event header field type.");
1205 goto end;
1206 }
1207
1208 BT_MOVE(stream_class->packet_context_type, packet_context_type);
1209 end:
1210 if (ret) {
1211 bt_put(packet_context_type);
1212 goto end;
1213 }
1214
1215 bt_put(_uint64_t);
1216 bt_put(ts_begin_end_uint64_t);
1217 return ret;
1218 }
1219
1220 static
1221 int try_map_clock_class(struct bt_ctf_stream_class *stream_class,
1222 struct bt_ctf_field_type *ft)
1223 {
1224 struct bt_ctf_clock_class *mapped_clock_class = NULL;
1225 int ret = 0;
1226
1227 if (!ft) {
1228 /* Field does not exist: not an error */
1229 goto end;
1230 }
1231
1232 assert(bt_ctf_field_type_is_integer(ft));
1233 mapped_clock_class =
1234 bt_ctf_field_type_integer_get_mapped_clock_class(ft);
1235 if (!mapped_clock_class) {
1236 if (!stream_class->clock) {
1237 BT_LOGW("Cannot automatically set field's type mapped clock class: stream class's clock is not set: "
1238 "stream-class-addr=%p, stream-class-name=\"%s\", "
1239 "stream-class-id=%" PRId64 ", ft-addr=%p",
1240 stream_class, bt_ctf_stream_class_get_name(stream_class),
1241 bt_ctf_stream_class_get_id(stream_class), ft);
1242 ret = -1;
1243 goto end;
1244 }
1245
1246 ret = bt_ctf_field_type_integer_set_mapped_clock_class_no_check(
1247 ft, stream_class->clock->clock_class);
1248 if (ret) {
1249 BT_LOGW("Cannot set field type's mapped clock class: "
1250 "stream-class-addr=%p, stream-class-name=\"%s\", "
1251 "stream-class-id=%" PRId64 ", ft-addr=%p",
1252 stream_class, bt_ctf_stream_class_get_name(stream_class),
1253 bt_ctf_stream_class_get_id(stream_class), ft);
1254 goto end;
1255 }
1256
1257 BT_LOGV("Automatically mapped field type to stream class's clock class: "
1258 "stream-class-addr=%p, stream-class-name=\"%s\", "
1259 "stream-class-id=%" PRId64 ", ft-addr=%p",
1260 stream_class, bt_ctf_stream_class_get_name(stream_class),
1261 bt_ctf_stream_class_get_id(stream_class), ft);
1262 }
1263
1264 end:
1265 bt_put(mapped_clock_class);
1266 return ret;
1267 }
1268
1269 BT_HIDDEN
1270 int bt_ctf_stream_class_map_clock_class(
1271 struct bt_ctf_stream_class *stream_class,
1272 struct bt_ctf_field_type *packet_context_type,
1273 struct bt_ctf_field_type *event_header_type)
1274 {
1275 struct bt_ctf_field_type *ft = NULL;
1276 int ret = 0;
1277
1278 assert(stream_class);
1279
1280 if (packet_context_type) {
1281 ft = bt_ctf_field_type_structure_get_field_type_by_name(
1282 packet_context_type, "timestamp_begin");
1283 if (try_map_clock_class(stream_class, ft)) {
1284 BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_begin` field's mapped clock class.");
1285 ret = -1;
1286 goto end;
1287 }
1288
1289 bt_put(ft);
1290 ft = bt_ctf_field_type_structure_get_field_type_by_name(
1291 packet_context_type, "timestamp_end");
1292 if (try_map_clock_class(stream_class, ft)) {
1293 BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_end` field's mapped clock class.");
1294 ret = -1;
1295 goto end;
1296 }
1297
1298 BT_PUT(ft);
1299 }
1300
1301 if (event_header_type) {
1302 ft = bt_ctf_field_type_structure_get_field_type_by_name(
1303 event_header_type, "timestamp");
1304 if (try_map_clock_class(stream_class, ft)) {
1305 BT_LOGE_STR("Cannot automatically set stream class's event header field type's `timestamp` field's mapped clock class.");
1306 ret = -1;
1307 goto end;
1308 }
1309
1310 BT_PUT(ft);
1311 }
1312
1313 end:
1314 return ret;
1315 }
This page took 0.061054 seconds and 5 git commands to generate.