fs-sink: fix handling static notifications
[babeltrace.git] / lib / 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
d2f71f12
PP
29#define BT_LOG_TAG "STREAM-CLASS"
30#include <babeltrace/lib-logging-internal.h>
31
11b0cdc8 32#include <babeltrace/ctf-writer/clock.h>
ac0c6bdd
PP
33#include <babeltrace/ctf-writer/clock-internal.h>
34#include <babeltrace/ctf-ir/clock-class-internal.h>
11b0cdc8 35#include <babeltrace/ctf-writer/event.h>
272df73e 36#include <babeltrace/ctf-ir/event-class-internal.h>
11b0cdc8 37#include <babeltrace/ctf-ir/event-internal.h>
2e33ac5a
PP
38#include <babeltrace/ctf-ir/field-types-internal.h>
39#include <babeltrace/ctf-ir/fields-internal.h>
11b0cdc8
JG
40#include <babeltrace/ctf-writer/stream.h>
41#include <babeltrace/ctf-ir/stream-class-internal.h>
09840de5 42#include <babeltrace/ctf-ir/validation-internal.h>
8bf65fbd 43#include <babeltrace/ctf-ir/visitor-internal.h>
11b0cdc8 44#include <babeltrace/ctf-writer/functor-internal.h>
654c1444 45#include <babeltrace/ctf-ir/utils.h>
83509119 46#include <babeltrace/ref.h>
3d9990ac
PP
47#include <babeltrace/compiler-internal.h>
48#include <babeltrace/align-internal.h>
49#include <babeltrace/endian-internal.h>
dc3fffef 50#include <inttypes.h>
544d0515 51#include <stdint.h>
e011d2c1 52#include <stdbool.h>
11b0cdc8
JG
53
54static
83509119 55void bt_ctf_stream_class_destroy(struct bt_object *obj);
11b0cdc8 56static
662e778c 57int init_event_header(struct bt_ctf_stream_class *stream_class);
11b0cdc8 58static
662e778c 59int init_packet_context(struct bt_ctf_stream_class *stream_class);
11b0cdc8
JG
60
61struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
62{
d2f71f12 63 struct bt_ctf_stream_class *stream_class;
12c8a1a3 64 int ret;
e0e2946b 65
d2f71f12
PP
66 BT_LOGD("Creating default stream class object: name=\"%s\"", name);
67 stream_class = bt_ctf_stream_class_create_empty(name);
e0e2946b 68 if (!stream_class) {
d2f71f12 69 BT_LOGD_STR("Cannot create empty stream class.");
e0e2946b
PP
70 goto error;
71 }
72
73 ret = init_event_header(stream_class);
74 if (ret) {
d2f71f12 75 BT_LOGE_STR("Cannot initialize stream class's event header field type.");
e0e2946b
PP
76 goto error;
77 }
78
79 ret = init_packet_context(stream_class);
80 if (ret) {
d2f71f12 81 BT_LOGE_STR("Cannot initialize stream class's packet context field type.");
e0e2946b
PP
82 goto error;
83 }
84
d2f71f12
PP
85 BT_LOGD("Created default stream class object: addr=%p, name=\"%s\"",
86 stream_class, name);
e0e2946b
PP
87 return stream_class;
88
89error:
90 BT_PUT(stream_class);
91 return stream_class;
92}
93
94struct bt_ctf_stream_class *bt_ctf_stream_class_create_empty(const char *name)
95{
11b0cdc8
JG
96 struct bt_ctf_stream_class *stream_class = NULL;
97
d2f71f12
PP
98 BT_LOGD("Creating empty stream class object: name=\"%s\"", name);
99
3ea33115 100 if (name && bt_ctf_validate_identifier(name)) {
d2f71f12
PP
101 BT_LOGW("Invalid parameter: stream class's name is not a valid CTF identifier: "
102 "name=\"%s\"", name);
11b0cdc8
JG
103 goto error;
104 }
105
106 stream_class = g_new0(struct bt_ctf_stream_class, 1);
107 if (!stream_class) {
d2f71f12 108 BT_LOGE_STR("Failed to allocate one stream class.");
11b0cdc8
JG
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(
e6a8e8e4 114 (GDestroyNotify) bt_object_release);
11b0cdc8 115 if (!stream_class->event_classes) {
d2f71f12 116 BT_LOGE_STR("Failed to allocate a GPtrArray.");
83509119 117 goto error;
11b0cdc8
JG
118 }
119
0b9ce69f
JG
120 stream_class->event_classes_ht = g_hash_table_new_full(g_int64_hash,
121 g_int64_equal, g_free, NULL);
d2f71f12
PP
122 if (!stream_class->event_classes_ht) {
123 BT_LOGE_STR("Failed to allocate a GHashTable.");
124 goto error;
125 }
0b9ce69f 126
83509119 127 bt_object_init(stream_class, bt_ctf_stream_class_destroy);
d2f71f12
PP
128 BT_LOGD("Created empty stream class object: addr=%p, name=\"%s\"",
129 stream_class, name);
11b0cdc8
JG
130 return stream_class;
131
11b0cdc8 132error:
e0e2946b 133 BT_PUT(stream_class);
11b0cdc8
JG
134 return stream_class;
135}
136
142c5610
JG
137struct bt_ctf_trace *bt_ctf_stream_class_get_trace(
138 struct bt_ctf_stream_class *stream_class)
139{
dc3fffef
PP
140 return stream_class ?
141 bt_get(bt_ctf_stream_class_borrow_trace(stream_class)) :
142 NULL;
142c5610
JG
143}
144
69dc4535
JG
145const 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) {
d2f71f12 151 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
69dc4535
JG
152 goto end;
153 }
154
155 name = stream_class->name->str;
156end:
157 return name;
158}
159
3ea33115
JG
160int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class,
161 const char *name)
162{
163 int ret = 0;
164
d2f71f12
PP
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));
3ea33115
JG
176 ret = -1;
177 goto end;
178 }
179
180 g_string_assign(stream_class->name, name);
d2f71f12
PP
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));
3ea33115
JG
185end:
186 return ret;
187}
188
2f100782
JG
189struct 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
d2f71f12
PP
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));
2f100782
JG
204 goto end;
205 }
206
ac0c6bdd 207 clock = bt_get(stream_class->clock);
2f100782
JG
208end:
209 return clock;
210}
211
11b0cdc8
JG
212int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
213 struct bt_ctf_clock *clock)
214{
215 int ret = 0;
eee752e5 216 struct bt_ctf_field_type *timestamp_field = NULL;
11b0cdc8 217
d2f71f12
PP
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));
11b0cdc8
JG
231 ret = -1;
232 goto end;
233 }
234
ac0c6bdd
PP
235 /* Replace the current clock of this stream class. */
236 bt_put(stream_class->clock);
237 stream_class->clock = bt_get(clock);
d2f71f12
PP
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));
11b0cdc8 245
11b0cdc8 246end:
ac0c6bdd 247 bt_put(timestamp_field);
11b0cdc8
JG
248 return ret;
249}
250
2f100782
JG
251int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class)
252{
253 int64_t ret;
254
d2f71f12
PP
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));
9ac68eb1 265 ret = (int64_t) -1;
2f100782
JG
266 goto end;
267 }
268
9ac68eb1 269 ret = stream_class->id;
2f100782
JG
270end:
271 return ret;
272}
273
5ca83563 274BT_HIDDEN
d2f71f12 275void _bt_ctf_stream_class_set_id(
9ac68eb1 276 struct bt_ctf_stream_class *stream_class, int64_t id)
5ca83563 277{
d2f71f12 278 assert(stream_class);
5ca83563
JG
279 stream_class->id = id;
280 stream_class->id_set = 1;
d2f71f12
PP
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));
5ca83563
JG
285}
286
9ac68eb1
PP
287struct event_class_set_stream_class_id_data {
288 int64_t stream_class_id;
29664b2a
PP
289 int ret;
290};
291
292static
293void event_class_set_stream_id(gpointer event_class, gpointer data)
294{
9ac68eb1 295 struct event_class_set_stream_class_id_data *typed_data = data;
29664b2a
PP
296
297 typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class,
9ac68eb1 298 typed_data->stream_class_id);
29664b2a
PP
299}
300
301BT_HIDDEN
302int bt_ctf_stream_class_set_id_no_check(
9ac68eb1 303 struct bt_ctf_stream_class *stream_class, int64_t id)
2f100782
JG
304{
305 int ret = 0;
9ac68eb1
PP
306 struct event_class_set_stream_class_id_data data =
307 { .stream_class_id = id, .ret = 0 };
2f100782 308
29664b2a
PP
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) {
d2f71f12
PP
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));
2f100782
JG
321 goto end;
322 }
323
d2f71f12 324 _bt_ctf_stream_class_set_id(stream_class, id);
2f100782
JG
325end:
326 return ret;
327}
328
29664b2a 329int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
9ac68eb1 330 uint64_t id_param)
29664b2a
PP
331{
332 int ret = 0;
9ac68eb1 333 int64_t id = (int64_t) id_param;
29664b2a 334
d2f71f12
PP
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);
29664b2a
PP
357 ret = -1;
358 goto end;
359 }
360
361 ret = bt_ctf_stream_class_set_id_no_check(stream_class, id);
d2f71f12
PP
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 }
29664b2a
PP
368end:
369 return ret;
370}
371
0d23acbe
PP
372static
373void 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
0d23acbe
PP
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) {
66871d36 398 BT_LOGW("Event class with this ID already exists in the stream class: "
d2f71f12
PP
399 "id=%" PRId64 ", name=\"%s\"",
400 id_a, bt_ctf_event_class_get_name(event_class_a));
0d23acbe
PP
401 search_query->found = 1;
402 goto end;
403 }
404
405end:
406 return;
407}
408
11b0cdc8
JG
409int 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;
0b9ce69f 414 int64_t *event_id = NULL;
e6a8e8e4
JG
415 struct bt_ctf_trace *trace = NULL;
416 struct bt_ctf_stream_class *old_stream_class = NULL;
09840de5
PP
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;
11b0cdc8 426
e011d2c1
PP
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
d2f71f12
PP
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
5acf2ae6
PP
445 trace = bt_ctf_stream_class_get_trace(stream_class);
446 if (trace && trace->is_static) {
447 ret = -1;
448 goto end;
449 }
450
0b9ce69f
JG
451 event_id = g_new(int64_t, 1);
452 if (!event_id) {
d2f71f12 453 BT_LOGE_STR("Failed to allocate one int64_t.");
0b9ce69f
JG
454 ret = -1;
455 goto end;
456 }
457
11b0cdc8
JG
458 /* Check for duplicate event classes */
459 struct search_query query = { .value = event_class, .found = 0 };
0d23acbe
PP
460 g_ptr_array_foreach(stream_class->event_classes, event_class_exists,
461 &query);
11b0cdc8 462 if (query.found) {
d2f71f12 463 BT_LOGW_STR("Another event class part of this stream class has the same ID.");
11b0cdc8
JG
464 ret = -1;
465 goto end;
466 }
467
09840de5 468 old_stream_class = bt_ctf_event_class_get_stream_class(event_class);
e6a8e8e4
JG
469 if (old_stream_class) {
470 /* Event class is already associated to a stream class. */
d2f71f12
PP
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));
e6a8e8e4
JG
478 ret = -1;
479 goto end;
480 }
481
e6a8e8e4 482 if (trace) {
09840de5
PP
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
26079216 522 if (ret) {
09840de5
PP
523 /*
524 * This means something went wrong during the
525 * validation process, not that the objects are
526 * invalid.
527 */
d2f71f12 528 BT_LOGE("Failed to validate event class: ret=%d", ret);
09840de5
PP
529 goto end;
530 }
531
532 if ((validation_output.valid_flags & validation_flags) !=
533 validation_flags) {
534 /* Invalid event class */
66871d36 535 BT_LOGW("Invalid trace, stream class, or event class: "
d2f71f12
PP
536 "valid-flags=0x%x",
537 validation_output.valid_flags);
09840de5 538 ret = -1;
26079216
JG
539 goto end;
540 }
541 }
542
09840de5 543 /* Only set an event ID if none was explicitly set before */
0b9ce69f 544 *event_id = bt_ctf_event_class_get_id(event_class);
24626e8b 545 if (*event_id < 0) {
d2f71f12
PP
546 BT_LOGV("Event class has no ID: automatically setting it: "
547 "id=%" PRId64, stream_class->next_event_id);
548
2f100782 549 if (bt_ctf_event_class_set_id(event_class,
d2f71f12
PP
550 stream_class->next_event_id)) {
551 BT_LOGE("Cannot set event class's ID: id=%" PRId64,
552 stream_class->next_event_id);
2f100782
JG
553 ret = -1;
554 goto end;
555 }
d2f71f12 556 stream_class->next_event_id++;
0b9ce69f 557 *event_id = stream_class->next_event_id;
2f100782
JG
558 }
559
29664b2a
PP
560 ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id);
561 if (ret) {
d2f71f12
PP
562 BT_LOGE("Cannot set event class's stream class ID attribute: ret=%d",
563 ret);
29664b2a
PP
564 goto end;
565 }
566
e6a8e8e4 567 bt_object_set_parent(event_class, stream_class);
09840de5
PP
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 */
11b0cdc8 588 g_ptr_array_add(stream_class->event_classes, event_class);
0b9ce69f
JG
589 g_hash_table_insert(stream_class->event_classes_ht, event_id,
590 event_class);
591 event_id = NULL;
09840de5
PP
592
593 /* Freeze the event class */
58203827 594 bt_ctf_event_class_freeze(event_class);
5ca83563 595
9b888ff3
JG
596 /* Notifiy listeners of the trace's schema modification. */
597 if (trace) {
d9a13d86
PP
598 struct bt_ctf_object obj = { .object = event_class,
599 .type = BT_CTF_OBJECT_TYPE_EVENT_CLASS };
9b888ff3 600
d9a13d86 601 (void) bt_ctf_trace_object_modification(&obj, trace);
9b888ff3 602 }
d2f71f12
PP
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
11b0cdc8 614end:
e6a8e8e4
JG
615 BT_PUT(trace);
616 BT_PUT(old_stream_class);
09840de5
PP
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);
0b9ce69f 624 g_free(event_id);
09840de5 625
11b0cdc8
JG
626 return ret;
627}
628
544d0515 629int64_t bt_ctf_stream_class_get_event_class_count(
69dc4535
JG
630 struct bt_ctf_stream_class *stream_class)
631{
544d0515 632 int64_t ret;
69dc4535
JG
633
634 if (!stream_class) {
d2f71f12 635 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
9ac68eb1 636 ret = (int64_t) -1;
69dc4535
JG
637 goto end;
638 }
639
9ac68eb1 640 ret = (int64_t) stream_class->event_classes->len;
69dc4535
JG
641end:
642 return ret;
643}
644
9ac68eb1
PP
645struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_index(
646 struct bt_ctf_stream_class *stream_class, uint64_t index)
69dc4535
JG
647{
648 struct bt_ctf_event_class *event_class = NULL;
649
d2f71f12
PP
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);
69dc4535
JG
662 goto end;
663 }
664
665 event_class = g_ptr_array_index(stream_class->event_classes, index);
83509119 666 bt_get(event_class);
69dc4535
JG
667end:
668 return event_class;
669}
670
0863f950 671struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id(
9ac68eb1 672 struct bt_ctf_stream_class *stream_class, uint64_t id)
0863f950 673{
9ac68eb1 674 int64_t id_key = (int64_t) id;
0863f950
PP
675 struct bt_ctf_event_class *event_class = NULL;
676
d2f71f12
PP
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);
0863f950
PP
689 goto end;
690 }
691
0b9ce69f
JG
692 event_class = g_hash_table_lookup(stream_class->event_classes_ht,
693 &id_key);
694 bt_get(event_class);
0863f950
PP
695end:
696 return event_class;
697}
698
12c8a1a3
JG
699struct 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) {
d2f71f12 705 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
12c8a1a3
JG
706 goto end;
707 }
708
83509119 709 bt_get(stream_class->packet_context_type);
12c8a1a3
JG
710 ret = stream_class->packet_context_type;
711end:
712 return ret;
713}
714
715int 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
d2f71f12
PP
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));
12c8a1a3
JG
732 ret = -1;
733 goto end;
734 }
735
835b2d10
JG
736 if (packet_context_type &&
737 bt_ctf_field_type_get_type_id(packet_context_type) !=
1487a16a 738 BT_CTF_FIELD_TYPE_ID_STRUCT) {
835b2d10 739 /* A packet context must be a structure. */
d2f71f12
PP
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)));
12c8a1a3
JG
748 ret = -1;
749 goto end;
750 }
751
83509119
JG
752 bt_put(stream_class->packet_context_type);
753 bt_get(packet_context_type);
12c8a1a3 754 stream_class->packet_context_type = packet_context_type;
d2f71f12
PP
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
12c8a1a3
JG
762end:
763 return ret;
764}
765
662e778c
JG
766struct 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
d2f71f12
PP
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));
662e778c
JG
781 goto end;
782 }
783
83509119 784 bt_get(stream_class->event_header_type);
662e778c
JG
785 ret = stream_class->event_header_type;
786end:
787 return ret;
788}
789
790int 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
d2f71f12
PP
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));
662e778c
JG
807 ret = -1;
808 goto end;
809 }
810
835b2d10
JG
811 if (event_header_type &&
812 bt_ctf_field_type_get_type_id(event_header_type) !=
1487a16a 813 BT_CTF_FIELD_TYPE_ID_STRUCT) {
835b2d10 814 /* An event header must be a structure. */
d2f71f12
PP
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)));
662e778c
JG
823 ret = -1;
824 goto end;
825 }
826
83509119 827 bt_put(stream_class->event_header_type);
835b2d10 828 stream_class->event_header_type = bt_get(event_header_type);
d2f71f12
PP
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);
662e778c
JG
835end:
836 return ret;
837}
838
af181248
JG
839struct 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
d2f71f12
PP
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) {
af181248
JG
850 goto end;
851 }
852
83509119 853 bt_get(stream_class->event_context_type);
af181248
JG
854 ret = stream_class->event_context_type;
855end:
856 return ret;
857}
858
859int 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
d2f71f12
PP
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));
af181248
JG
876 ret = -1;
877 goto end;
878 }
879
835b2d10
JG
880 if (event_context_type &&
881 bt_ctf_field_type_get_type_id(event_context_type) !=
1487a16a 882 BT_CTF_FIELD_TYPE_ID_STRUCT) {
835b2d10 883 /* A packet context must be a structure. */
d2f71f12
PP
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)));
af181248
JG
892 ret = -1;
893 goto end;
894 }
895
83509119 896 bt_put(stream_class->event_context_type);
835b2d10 897 stream_class->event_context_type = bt_get(event_context_type);
d2f71f12
PP
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);
af181248
JG
904end:
905 return ret;
906}
907
d2f71f12 908/* Pre-2.0 CTF writer backward compatibility */
11b0cdc8
JG
909void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
910{
83509119 911 bt_get(stream_class);
11b0cdc8
JG
912}
913
d2f71f12 914/* Pre-2.0 CTF writer backward compatibility */
11b0cdc8
JG
915void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
916{
83509119 917 bt_put(stream_class);
11b0cdc8
JG
918}
919
8bf65fbd 920static
544d0515 921int64_t get_event_class_count(void *element)
8bf65fbd
JG
922{
923 return bt_ctf_stream_class_get_event_class_count(
924 (struct bt_ctf_stream_class *) element);
925}
926
927static
928void *get_event_class(void *element, int i)
929{
9ac68eb1 930 return bt_ctf_stream_class_get_event_class_by_index(
8bf65fbd
JG
931 (struct bt_ctf_stream_class *) element, i);
932}
933
934static
d9a13d86 935int visit_event_class(void *object, bt_ctf_visitor visitor,void *data)
8bf65fbd 936{
d9a13d86
PP
937 struct bt_ctf_object obj =
938 { .object = object,
939 .type = BT_CTF_OBJECT_TYPE_EVENT_CLASS };
8bf65fbd 940
d9a13d86 941 return visitor(&obj, data);
8bf65fbd
JG
942}
943
944int bt_ctf_stream_class_visit(struct bt_ctf_stream_class *stream_class,
d9a13d86 945 bt_ctf_visitor visitor, void *data)
8bf65fbd
JG
946{
947 int ret;
d9a13d86
PP
948 struct bt_ctf_object obj =
949 { .object = stream_class,
950 .type = BT_CTF_OBJECT_TYPE_STREAM_CLASS };
8bf65fbd
JG
951
952 if (!stream_class || !visitor) {
d2f71f12
PP
953 BT_LOGW("Invalid parameter: stream class or visitor is NULL: "
954 "stream-class-addr=%p, visitor=%p",
955 stream_class, visitor);
8bf65fbd
JG
956 ret = -1;
957 goto end;
958 }
959
d9a13d86 960 ret = visitor_helper(&obj, get_event_class_count,
8bf65fbd
JG
961 get_event_class,
962 visit_event_class, visitor, data);
d2f71f12 963 BT_LOGV("visitor_helper() returned: ret=%d", ret);
8bf65fbd
JG
964end:
965 return ret;
966}
967
11b0cdc8
JG
968BT_HIDDEN
969void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
970{
d2f71f12 971 if (!stream_class || stream_class->frozen) {
11b0cdc8
JG
972 return;
973 }
974
d2f71f12
PP
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));
11b0cdc8 978 stream_class->frozen = 1;
662e778c 979 bt_ctf_field_type_freeze(stream_class->event_header_type);
12c8a1a3 980 bt_ctf_field_type_freeze(stream_class->packet_context_type);
af181248 981 bt_ctf_field_type_freeze(stream_class->event_context_type);
ac0c6bdd
PP
982
983 if (stream_class->clock) {
984 bt_ctf_clock_class_freeze(stream_class->clock->clock_class);
985 }
11b0cdc8
JG
986}
987
11b0cdc8
JG
988BT_HIDDEN
989int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
990 struct metadata_context *context)
991{
9ac68eb1 992 int ret = 0;
11b0cdc8
JG
993 size_t i;
994
d2f71f12
PP
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);
11b0cdc8
JG
1000 g_string_assign(context->field_name, "");
1001 context->current_indentation_level = 1;
1002 if (!stream_class->id_set) {
d2f71f12 1003 BT_LOGW_STR("Stream class's ID is not set.");
11b0cdc8
JG
1004 ret = -1;
1005 goto end;
1006 }
1007
1008 g_string_append_printf(context->string,
e011d2c1
PP
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, ";");
11b0cdc8
JG
1021 }
1022
e011d2c1 1023
98edd02c 1024 if (stream_class->packet_context_type) {
e011d2c1
PP
1025 BT_LOGD_STR("Serializing stream class's packet context field type's metadata.");
1026 g_string_append(context->string, "\n\n\tpacket.context := ");
98edd02c
JG
1027 ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
1028 context);
1029 if (ret) {
66871d36 1030 BT_LOGW("Cannot serialize stream class's packet context field type's metadata: "
d2f71f12 1031 "ret=%d", ret);
98edd02c
JG
1032 goto end;
1033 }
e011d2c1 1034 g_string_append(context->string, ";");
11b0cdc8
JG
1035 }
1036
1037 if (stream_class->event_context_type) {
e011d2c1
PP
1038 BT_LOGD_STR("Serializing stream class's event context field type's metadata.");
1039 g_string_append(context->string, "\n\n\tevent.context := ");
11b0cdc8
JG
1040 ret = bt_ctf_field_type_serialize(
1041 stream_class->event_context_type, context);
1042 if (ret) {
66871d36 1043 BT_LOGW("Cannot serialize stream class's event context field type's metadata: "
d2f71f12 1044 "ret=%d", ret);
11b0cdc8
JG
1045 goto end;
1046 }
e011d2c1 1047 g_string_append(context->string, ";");
11b0cdc8
JG
1048 }
1049
e011d2c1
PP
1050 g_string_append(context->string, "\n};\n\n");
1051
11b0cdc8
JG
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
11b0cdc8
JG
1056 ret = bt_ctf_event_class_serialize(event_class, context);
1057 if (ret) {
66871d36 1058 BT_LOGW("Cannot serialize event class's metadata: "
d2f71f12
PP
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));
11b0cdc8
JG
1064 goto end;
1065 }
1066 }
1067end:
1068 context->current_indentation_level = 0;
1069 return ret;
1070}
1071
1072static
83509119 1073void bt_ctf_stream_class_destroy(struct bt_object *obj)
11b0cdc8
JG
1074{
1075 struct bt_ctf_stream_class *stream_class;
1076
83509119 1077 stream_class = container_of(obj, struct bt_ctf_stream_class, base);
d2f71f12
PP
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));
83509119 1081 bt_put(stream_class->clock);
11b0cdc8 1082
0b9ce69f
JG
1083 if (stream_class->event_classes_ht) {
1084 g_hash_table_destroy(stream_class->event_classes_ht);
1085 }
11b0cdc8 1086 if (stream_class->event_classes) {
8c8cb0f2 1087 BT_LOGD_STR("Destroying event classes.");
11b0cdc8
JG
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
8c8cb0f2 1095 BT_LOGD_STR("Putting event header field type.");
83509119 1096 bt_put(stream_class->event_header_type);
8c8cb0f2 1097 BT_LOGD_STR("Putting packet context field type.");
83509119 1098 bt_put(stream_class->packet_context_type);
8c8cb0f2 1099 BT_LOGD_STR("Putting event context field type.");
83509119 1100 bt_put(stream_class->event_context_type);
11b0cdc8
JG
1101 g_free(stream_class);
1102}
1103
1104static
662e778c 1105int init_event_header(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
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) {
d2f71f12 1116 BT_LOGE_STR("Cannot create empty structure field type.");
11b0cdc8
JG
1117 ret = -1;
1118 goto end;
1119 }
1120
11b0cdc8
JG
1121 ret = bt_ctf_field_type_structure_add_field(event_header_type,
1122 _uint32_t, "id");
1123 if (ret) {
d2f71f12 1124 BT_LOGE_STR("Cannot add `id` field to event header field type.");
11b0cdc8
JG
1125 goto end;
1126 }
1127
1128 ret = bt_ctf_field_type_structure_add_field(event_header_type,
1129 _uint64_t, "timestamp");
1130 if (ret) {
d2f71f12 1131 BT_LOGE_STR("Cannot add `timestamp` field to event header field type.");
11b0cdc8
JG
1132 goto end;
1133 }
1134
e0e2946b 1135 BT_MOVE(stream_class->event_header_type, event_header_type);
11b0cdc8
JG
1136end:
1137 if (ret) {
83509119 1138 bt_put(event_header_type);
11b0cdc8
JG
1139 }
1140
83509119
JG
1141 bt_put(_uint32_t);
1142 bt_put(_uint64_t);
11b0cdc8
JG
1143 return ret;
1144}
1145
1146static
662e778c 1147int init_packet_context(struct bt_ctf_stream_class *stream_class)
11b0cdc8
JG
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);
e011d2c1 1154 struct bt_ctf_field_type *ts_begin_end_uint64_t;
11b0cdc8
JG
1155
1156 if (!packet_context_type) {
d2f71f12 1157 BT_LOGE_STR("Cannot create empty structure field type.");
11b0cdc8
JG
1158 ret = -1;
1159 goto end;
1160 }
1161
e011d2c1
PP
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
11b0cdc8
JG
1169 /*
1170 * We create a stream packet context as proposed in the CTF
1171 * specification.
1172 */
11b0cdc8 1173 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
e011d2c1 1174 ts_begin_end_uint64_t, "timestamp_begin");
11b0cdc8 1175 if (ret) {
d2f71f12 1176 BT_LOGE_STR("Cannot add `timestamp_begin` field to event header field type.");
11b0cdc8
JG
1177 goto end;
1178 }
1179
1180 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
e011d2c1 1181 ts_begin_end_uint64_t, "timestamp_end");
11b0cdc8 1182 if (ret) {
d2f71f12 1183 BT_LOGE_STR("Cannot add `timestamp_end` field to event header field type.");
11b0cdc8
JG
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) {
d2f71f12 1190 BT_LOGE_STR("Cannot add `content_size` field to event header field type.");
11b0cdc8
JG
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) {
d2f71f12 1197 BT_LOGE_STR("Cannot add `packet_size` field to event header field type.");
11b0cdc8
JG
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) {
d2f71f12 1204 BT_LOGE_STR("Cannot add `events_discarded` field to event header field type.");
11b0cdc8
JG
1205 goto end;
1206 }
1207
e0e2946b 1208 BT_MOVE(stream_class->packet_context_type, packet_context_type);
11b0cdc8
JG
1209end:
1210 if (ret) {
83509119 1211 bt_put(packet_context_type);
11b0cdc8
JG
1212 goto end;
1213 }
1214
83509119 1215 bt_put(_uint64_t);
e011d2c1
PP
1216 bt_put(ts_begin_end_uint64_t);
1217 return ret;
1218}
1219
1220static
1221int 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
1264end:
1265 bt_put(mapped_clock_class);
1266 return ret;
1267}
1268
1269BT_HIDDEN
1270int 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
1313end:
11b0cdc8
JG
1314 return ret;
1315}
This page took 0.103518 seconds and 4 git commands to generate.