Fix: variable declaration shadows previously declared variable
[babeltrace.git] / lib / ctf-ir / trace.c
CommitLineData
bc37ae52
JG
1/*
2 * trace.c
3 *
4 * Babeltrace CTF IR - Trace
5 *
6 * Copyright 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
e011d2c1
PP
29#define BT_LOG_TAG "TRACE"
30#include <babeltrace/lib-logging-internal.h>
31
bc37ae52 32#include <babeltrace/ctf-ir/trace-internal.h>
ac0c6bdd 33#include <babeltrace/ctf-ir/clock-class-internal.h>
bc37ae52
JG
34#include <babeltrace/ctf-ir/stream-internal.h>
35#include <babeltrace/ctf-ir/stream-class-internal.h>
09840de5 36#include <babeltrace/ctf-ir/event-internal.h>
272df73e
PP
37#include <babeltrace/ctf-ir/event-class.h>
38#include <babeltrace/ctf-ir/event-class-internal.h>
bc37ae52 39#include <babeltrace/ctf-writer/functor-internal.h>
ac0c6bdd 40#include <babeltrace/ctf-writer/clock-internal.h>
2e33ac5a 41#include <babeltrace/ctf-ir/field-types-internal.h>
44e0a4f5 42#include <babeltrace/ctf-ir/attributes-internal.h>
09840de5 43#include <babeltrace/ctf-ir/validation-internal.h>
8bf65fbd 44#include <babeltrace/ctf-ir/visitor-internal.h>
654c1444 45#include <babeltrace/ctf-ir/utils.h>
3d9990ac 46#include <babeltrace/compiler-internal.h>
dac5c838 47#include <babeltrace/values.h>
95c09b3a 48#include <babeltrace/values-internal.h>
83509119 49#include <babeltrace/ref.h>
c55a9f58 50#include <babeltrace/types.h>
3d9990ac 51#include <babeltrace/endian-internal.h>
dc3fffef 52#include <inttypes.h>
544d0515 53#include <stdint.h>
4a32fda0 54#include <string.h>
0fbb9a9f 55#include <stdlib.h>
bc37ae52
JG
56
57#define DEFAULT_IDENTIFIER_SIZE 128
58#define DEFAULT_METADATA_STRING_SIZE 4096
59
50ad4244
JG
60struct listener_wrapper {
61 bt_ctf_listener_cb listener;
2204c381
JG
62 void *data;
63};
64
3602afb0
PP
65struct bt_ctf_trace_is_static_listener_elem {
66 bt_ctf_trace_is_static_listener func;
8480c8cc 67 bt_ctf_trace_listener_removed removed;
3602afb0
PP
68 void *data;
69};
70
bc37ae52 71static
83509119 72void bt_ctf_trace_destroy(struct bt_object *obj);
bc37ae52 73static
09840de5 74void bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
bc37ae52 75
bc37ae52
JG
76static
77const unsigned int field_type_aliases_alignments[] = {
78 [FIELD_TYPE_ALIAS_UINT5_T] = 1,
79 [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
80 [FIELD_TYPE_ALIAS_UINT27_T] = 1,
81 [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
82};
83
84static
85const unsigned int field_type_aliases_sizes[] = {
86 [FIELD_TYPE_ALIAS_UINT5_T] = 5,
87 [FIELD_TYPE_ALIAS_UINT8_T] = 8,
88 [FIELD_TYPE_ALIAS_UINT16_T] = 16,
89 [FIELD_TYPE_ALIAS_UINT27_T] = 27,
90 [FIELD_TYPE_ALIAS_UINT32_T] = 32,
91 [FIELD_TYPE_ALIAS_UINT64_T] = 64,
92};
93
bc37ae52
JG
94struct bt_ctf_trace *bt_ctf_trace_create(void)
95{
96 struct bt_ctf_trace *trace = NULL;
97
98 trace = g_new0(struct bt_ctf_trace, 1);
99 if (!trace) {
95c09b3a 100 BT_LOGE_STR("Failed to allocate one trace.");
bc37ae52
JG
101 goto error;
102 }
103
95c09b3a 104 BT_LOGD_STR("Creating trace object.");
2f4b93cd 105 trace->native_byte_order = BT_CTF_BYTE_ORDER_UNSPECIFIED;
83509119 106 bt_object_init(trace, bt_ctf_trace_destroy);
bc37ae52 107 trace->clocks = g_ptr_array_new_with_free_func(
83509119 108 (GDestroyNotify) bt_put);
95c09b3a
PP
109 if (!trace->clocks) {
110 BT_LOGE_STR("Failed to allocate one GPtrArray.");
111 goto error;
112 }
113
bc37ae52 114 trace->streams = g_ptr_array_new_with_free_func(
e6a8e8e4 115 (GDestroyNotify) bt_object_release);
95c09b3a
PP
116 if (!trace->streams) {
117 BT_LOGE_STR("Failed to allocate one GPtrArray.");
118 goto error;
119 }
120
bc37ae52 121 trace->stream_classes = g_ptr_array_new_with_free_func(
e6a8e8e4 122 (GDestroyNotify) bt_object_release);
95c09b3a
PP
123 if (!trace->stream_classes) {
124 BT_LOGE_STR("Failed to allocate one GPtrArray.");
83509119 125 goto error;
bc37ae52
JG
126 }
127
7f800dc7
PP
128 /* Create the environment array object */
129 trace->environment = bt_ctf_attributes_create();
130 if (!trace->environment) {
95c09b3a 131 BT_LOGE_STR("Cannot create empty attributes object.");
83509119 132 goto error;
7f800dc7
PP
133 }
134
9b888ff3
JG
135 trace->listeners = g_ptr_array_new_with_free_func(
136 (GDestroyNotify) g_free);
137 if (!trace->listeners) {
95c09b3a 138 BT_LOGE_STR("Failed to allocate one GPtrArray.");
9b888ff3
JG
139 goto error;
140 }
141
3602afb0
PP
142 trace->is_static_listeners = g_array_new(FALSE, TRUE,
143 sizeof(struct bt_ctf_trace_is_static_listener_elem));
144 if (!trace->is_static_listeners) {
145 BT_LOGE_STR("Failed to allocate one GArray.");
146 goto error;
147 }
148
95c09b3a 149 BT_LOGD("Created trace object: addr=%p", trace);
bc37ae52
JG
150 return trace;
151
bc37ae52 152error:
83509119 153 BT_PUT(trace);
bc37ae52
JG
154 return trace;
155}
156
e96045d4
JG
157const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace)
158{
159 const char *name = NULL;
160
95c09b3a
PP
161 if (!trace) {
162 BT_LOGW_STR("Invalid parameter: trace is NULL.");
163 goto end;
164 }
165
166 if (!trace->name) {
e96045d4
JG
167 goto end;
168 }
169
170 name = trace->name->str;
171end:
172 return name;
173}
174
175int bt_ctf_trace_set_name(struct bt_ctf_trace *trace, const char *name)
176{
177 int ret = 0;
178
95c09b3a
PP
179 if (!trace) {
180 BT_LOGW_STR("Invalid parameter: trace is NULL.");
181 ret = -1;
182 goto end;
183 }
184
185 if (!name) {
186 BT_LOGW_STR("Invalid parameter: name is NULL.");
187 ret = -1;
188 goto end;
189 }
190
191 if (trace->frozen) {
192 BT_LOGW("Invalid parameter: trace is frozen: "
193 "addr=%p, name=\"%s\"",
194 trace, bt_ctf_trace_get_name(trace));
e96045d4
JG
195 ret = -1;
196 goto end;
197 }
198
199 trace->name = trace->name ? g_string_assign(trace->name, name) :
200 g_string_new(name);
201 if (!trace->name) {
95c09b3a 202 BT_LOGE_STR("Failed to allocate one GString.");
e96045d4
JG
203 ret = -1;
204 goto end;
205 }
95c09b3a
PP
206
207 BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace, name);
208
e96045d4
JG
209end:
210 return ret;
211}
212
4a32fda0
PP
213const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace *trace)
214{
215 return trace && trace->uuid_set ? trace->uuid : NULL;
216}
217
218int bt_ctf_trace_set_uuid(struct bt_ctf_trace *trace, const unsigned char *uuid)
219{
220 int ret = 0;
221
95c09b3a
PP
222 if (!trace) {
223 BT_LOGW_STR("Invalid parameter: trace is NULL.");
224 ret = -1;
225 goto end;
226 }
227
228 if (!uuid) {
229 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
230 ret = -1;
231 goto end;
232 }
233
234 if (trace->frozen) {
235 BT_LOGW("Invalid parameter: trace is frozen: "
236 "addr=%p, name=\"%s\"",
237 trace, bt_ctf_trace_get_name(trace));
4a32fda0
PP
238 ret = -1;
239 goto end;
240 }
241
20eee76e 242 memcpy(trace->uuid, uuid, BABELTRACE_UUID_LEN);
c55a9f58 243 trace->uuid_set = BT_TRUE;
95c09b3a
PP
244 BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", "
245 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
246 trace, bt_ctf_trace_get_name(trace),
247 (unsigned int) uuid[0],
248 (unsigned int) uuid[1],
249 (unsigned int) uuid[2],
250 (unsigned int) uuid[3],
251 (unsigned int) uuid[4],
252 (unsigned int) uuid[5],
253 (unsigned int) uuid[6],
254 (unsigned int) uuid[7],
255 (unsigned int) uuid[8],
256 (unsigned int) uuid[9],
257 (unsigned int) uuid[10],
258 (unsigned int) uuid[11],
259 (unsigned int) uuid[12],
260 (unsigned int) uuid[13],
261 (unsigned int) uuid[14],
262 (unsigned int) uuid[15]);
4a32fda0
PP
263
264end:
265 return ret;
266}
267
83509119 268void bt_ctf_trace_destroy(struct bt_object *obj)
bc37ae52
JG
269{
270 struct bt_ctf_trace *trace;
bc37ae52 271
83509119 272 trace = container_of(obj, struct bt_ctf_trace, base);
95c09b3a
PP
273
274 BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
275 trace, bt_ctf_trace_get_name(trace));
276
8480c8cc
PP
277 /*
278 * Call remove listeners first so that everything else still
279 * exists in the trace.
280 */
281 if (trace->is_static_listeners) {
282 size_t i;
283
284 for (i = 0; i < trace->is_static_listeners->len; i++) {
285 struct bt_ctf_trace_is_static_listener_elem elem =
286 g_array_index(trace->is_static_listeners,
287 struct bt_ctf_trace_is_static_listener_elem, i);
288
289 if (elem.removed) {
290 elem.removed(trace, elem.data);
291 }
292 }
293
294 g_array_free(trace->is_static_listeners, TRUE);
295 }
296
297 if (trace->listeners) {
298 g_ptr_array_free(trace->listeners, TRUE);
299 }
300
bc37ae52 301 if (trace->environment) {
95c09b3a 302 BT_LOGD_STR("Destroying environment attributes.");
7f800dc7 303 bt_ctf_attributes_destroy(trace->environment);
bc37ae52
JG
304 }
305
e96045d4
JG
306 if (trace->name) {
307 g_string_free(trace->name, TRUE);
308 }
309
bc37ae52 310 if (trace->clocks) {
95c09b3a 311 BT_LOGD_STR("Putting clock classes.");
bc37ae52
JG
312 g_ptr_array_free(trace->clocks, TRUE);
313 }
314
315 if (trace->streams) {
607ff9b8 316 BT_LOGD_STR("Destroying streams.");
bc37ae52
JG
317 g_ptr_array_free(trace->streams, TRUE);
318 }
319
320 if (trace->stream_classes) {
607ff9b8 321 BT_LOGD_STR("Destroying stream classes.");
bc37ae52
JG
322 g_ptr_array_free(trace->stream_classes, TRUE);
323 }
324
95c09b3a 325 BT_LOGD_STR("Putting packet header field type.");
83509119 326 bt_put(trace->packet_header_type);
bc37ae52
JG
327 g_free(trace);
328}
329
7f800dc7 330int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
dac5c838 331 const char *name, struct bt_value *value)
bc37ae52 332{
bc37ae52
JG
333 int ret = 0;
334
95c09b3a
PP
335 if (!trace) {
336 BT_LOGW_STR("Invalid parameter: trace is NULL.");
337 ret = -1;
338 goto end;
339 }
340
341 if (!name) {
342 BT_LOGW_STR("Invalid parameter: name is NULL.");
343 ret = -1;
344 goto end;
345 }
346
347 if (!value) {
348 BT_LOGW_STR("Invalid parameter: value is NULL.");
349 ret = -1;
350 goto end;
351 }
352
353 if (bt_ctf_validate_identifier(name)) {
354 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
355 "trace-addr=%p, trace-name=\"%s\", "
356 "env-name=\"%s\"",
357 trace, bt_ctf_trace_get_name(trace), name);
bc37ae52 358 ret = -1;
7f800dc7 359 goto end;
bc37ae52
JG
360 }
361
95c09b3a
PP
362 if (!bt_value_is_integer(value) && !bt_value_is_string(value)) {
363 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
364 "trace-addr=%p, trace-name=\"%s\", "
365 "env-name=\"%s\", env-value-type=%s",
366 trace, bt_ctf_trace_get_name(trace), name,
367 bt_value_type_string(bt_value_get_type(value)));
368 ret = -1;
369 goto end;
370 }
371
372 if (trace->is_static) {
373 BT_LOGW("Invalid parameter: trace is static: "
374 "addr=%p, name=\"%s\"",
375 trace, bt_ctf_trace_get_name(trace));
bc37ae52 376 ret = -1;
7f800dc7 377 goto end;
bc37ae52
JG
378 }
379
a0d12916
JG
380 if (trace->frozen) {
381 /*
382 * New environment fields may be added to a frozen trace,
383 * but existing fields may not be changed.
384 *
385 * The object passed is frozen like all other attributes.
386 */
dac5c838 387 struct bt_value *attribute =
a0d12916
JG
388 bt_ctf_attributes_get_field_value_by_name(
389 trace->environment, name);
390
391 if (attribute) {
95c09b3a
PP
392 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
393 "trace-addr=%p, trace-name=\"%s\", "
394 "env-name=\"%s\"",
395 trace, bt_ctf_trace_get_name(trace), name);
83509119 396 BT_PUT(attribute);
a0d12916
JG
397 ret = -1;
398 goto end;
399 }
400
dac5c838 401 bt_value_freeze(value);
a0d12916
JG
402 }
403
7f800dc7
PP
404 ret = bt_ctf_attributes_set_field_value(trace->environment, name,
405 value);
95c09b3a
PP
406 if (ret) {
407 BT_LOGE("Cannot set environment field's value: "
408 "trace-addr=%p, trace-name=\"%s\", "
409 "env-name=\"%s\"",
410 trace, bt_ctf_trace_get_name(trace), name);
411 } else {
412 BT_LOGV("Set environment field's value: "
413 "trace-addr=%p, trace-name=\"%s\", "
414 "env-name=\"%s\", value-addr=%p",
415 trace, bt_ctf_trace_get_name(trace), name, value);
416 }
bc37ae52 417
7f800dc7
PP
418end:
419 return ret;
420}
bc37ae52 421
7f800dc7
PP
422int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
423 const char *name, const char *value)
424{
425 int ret = 0;
dac5c838 426 struct bt_value *env_value_string_obj = NULL;
7f800dc7 427
95c09b3a
PP
428 if (!value) {
429 BT_LOGW_STR("Invalid parameter: value is NULL.");
bc37ae52 430 ret = -1;
7f800dc7 431 goto end;
bc37ae52
JG
432 }
433
dac5c838 434 env_value_string_obj = bt_value_string_create_init(value);
7f800dc7 435 if (!env_value_string_obj) {
95c09b3a 436 BT_LOGE_STR("Cannot create string value object.");
7f800dc7
PP
437 ret = -1;
438 goto end;
bc37ae52
JG
439 }
440
95c09b3a 441 /* bt_ctf_trace_set_environment_field() logs errors */
7f800dc7
PP
442 ret = bt_ctf_trace_set_environment_field(trace, name,
443 env_value_string_obj);
444
445end:
95c09b3a 446 bt_put(env_value_string_obj);
3487c9f3
JG
447 return ret;
448}
449
7f800dc7
PP
450int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
451 const char *name, int64_t value)
3487c9f3 452{
3487c9f3 453 int ret = 0;
dac5c838 454 struct bt_value *env_value_integer_obj = NULL;
3487c9f3 455
dac5c838 456 env_value_integer_obj = bt_value_integer_create_init(value);
7f800dc7 457 if (!env_value_integer_obj) {
95c09b3a 458 BT_LOGE_STR("Cannot create integer value object.");
3487c9f3 459 ret = -1;
7f800dc7 460 goto end;
3487c9f3
JG
461 }
462
95c09b3a 463 /* bt_ctf_trace_set_environment_field() logs errors */
7f800dc7
PP
464 ret = bt_ctf_trace_set_environment_field(trace, name,
465 env_value_integer_obj);
95c09b3a 466
7f800dc7 467end:
95c09b3a 468 bt_put(env_value_integer_obj);
bc37ae52
JG
469 return ret;
470}
471
544d0515 472int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
e6fa2160 473{
544d0515 474 int64_t ret = 0;
e6fa2160
JG
475
476 if (!trace) {
95c09b3a 477 BT_LOGW_STR("Invalid parameter: trace is NULL.");
9ac68eb1 478 ret = (int64_t) -1;
e6fa2160
JG
479 goto end;
480 }
481
7f800dc7 482 ret = bt_ctf_attributes_get_count(trace->environment);
95c09b3a 483 assert(ret >= 0);
e6fa2160 484
e6fa2160 485end:
7f800dc7 486 return ret;
e6fa2160
JG
487}
488
489const char *
9ac68eb1
PP
490bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace *trace,
491 uint64_t index)
e6fa2160 492{
e6fa2160
JG
493 const char *ret = NULL;
494
7f800dc7 495 if (!trace) {
95c09b3a 496 BT_LOGW_STR("Invalid parameter: trace is NULL.");
e6fa2160
JG
497 goto end;
498 }
499
7f800dc7
PP
500 ret = bt_ctf_attributes_get_field_name(trace->environment, index);
501
e6fa2160
JG
502end:
503 return ret;
504}
505
9ac68eb1
PP
506struct bt_value *bt_ctf_trace_get_environment_field_value_by_index(
507 struct bt_ctf_trace *trace, uint64_t index)
e6fa2160 508{
dac5c838 509 struct bt_value *ret = NULL;
e6fa2160 510
7f800dc7 511 if (!trace) {
95c09b3a 512 BT_LOGW_STR("Invalid parameter: trace is NULL.");
e6fa2160
JG
513 goto end;
514 }
515
7f800dc7
PP
516 ret = bt_ctf_attributes_get_field_value(trace->environment, index);
517
e6fa2160
JG
518end:
519 return ret;
520}
521
dac5c838 522struct bt_value *bt_ctf_trace_get_environment_field_value_by_name(
7f800dc7 523 struct bt_ctf_trace *trace, const char *name)
e6fa2160 524{
dac5c838 525 struct bt_value *ret = NULL;
e6fa2160 526
95c09b3a
PP
527 if (!trace) {
528 BT_LOGW_STR("Invalid parameter: trace is NULL.");
529 goto end;
530 }
531
532 if (!name) {
533 BT_LOGW_STR("Invalid parameter: name is NULL.");
e6fa2160
JG
534 goto end;
535 }
536
7f800dc7
PP
537 ret = bt_ctf_attributes_get_field_value_by_name(trace->environment,
538 name);
539
e6fa2160
JG
540end:
541 return ret;
542}
543
ac0c6bdd
PP
544int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
545 struct bt_ctf_clock_class *clock_class)
bc37ae52
JG
546{
547 int ret = 0;
bc37ae52 548
95c09b3a
PP
549 if (!trace) {
550 BT_LOGW_STR("Invalid parameter: trace is NULL.");
551 ret = -1;
552 goto end;
553 }
554
555 if (trace->is_static) {
556 BT_LOGW("Invalid parameter: trace is static: "
557 "addr=%p, name=\"%s\"",
558 trace, bt_ctf_trace_get_name(trace));
559 ret = -1;
560 goto end;
561 }
562
563 if (!bt_ctf_clock_class_is_valid(clock_class)) {
564 BT_LOGW("Invalid parameter: clock class is invalid: "
565 "trace-addr=%p, trace-name=\"%s\", "
566 "clock-class-addr=%p, clock-class-name=\"%s\"",
567 trace, bt_ctf_trace_get_name(trace),
568 clock_class, bt_ctf_clock_class_get_name(clock_class));
bc37ae52
JG
569 ret = -1;
570 goto end;
571 }
572
ac0c6bdd 573 /* Check for duplicate clock classes */
c9d90a34 574 if (bt_ctf_trace_has_clock_class(trace, clock_class)) {
95c09b3a
PP
575 BT_LOGW("Invalid parameter: clock class already exists in trace: "
576 "trace-addr=%p, trace-name=\"%s\", "
577 "clock-class-addr=%p, clock-class-name=\"%s\"",
578 trace, bt_ctf_trace_get_name(trace),
579 clock_class, bt_ctf_clock_class_get_name(clock_class));
bc37ae52
JG
580 ret = -1;
581 goto end;
582 }
583
ac0c6bdd
PP
584 bt_get(clock_class);
585 g_ptr_array_add(trace->clocks, clock_class);
cfeb617e 586
6474e016 587 if (trace->frozen) {
95c09b3a 588 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
ac0c6bdd 589 bt_ctf_clock_class_freeze(clock_class);
6474e016 590 }
95c09b3a
PP
591
592 BT_LOGV("Added clock class to trace: "
593 "trace-addr=%p, trace-name=\"%s\", "
594 "clock-class-addr=%p, clock-class-name=\"%s\"",
595 trace, bt_ctf_trace_get_name(trace),
596 clock_class, bt_ctf_clock_class_get_name(clock_class));
597
bc37ae52
JG
598end:
599 return ret;
600}
601
544d0515 602int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
884cd6c3 603{
9ac68eb1 604 int64_t ret = (int64_t) -1;
884cd6c3
JG
605
606 if (!trace) {
95c09b3a 607 BT_LOGW_STR("Invalid parameter: trace is NULL.");
884cd6c3
JG
608 goto end;
609 }
610
611 ret = trace->clocks->len;
612end:
613 return ret;
614}
615
9ac68eb1
PP
616struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index(
617 struct bt_ctf_trace *trace, uint64_t index)
884cd6c3 618{
ac0c6bdd 619 struct bt_ctf_clock_class *clock_class = NULL;
884cd6c3 620
95c09b3a
PP
621 if (!trace) {
622 BT_LOGW_STR("Invalid parameter: trace is NULL.");
623 goto end;
624 }
625
626 if (index >= trace->clocks->len) {
627 BT_LOGW("Invalid parameter: index is out of bounds: "
628 "addr=%p, name=\"%s\", "
629 "index=%" PRIu64 ", count=%u",
630 trace, bt_ctf_trace_get_name(trace),
631 index, trace->clocks->len);
884cd6c3
JG
632 goto end;
633 }
634
ac0c6bdd
PP
635 clock_class = g_ptr_array_index(trace->clocks, index);
636 bt_get(clock_class);
884cd6c3 637end:
ac0c6bdd 638 return clock_class;
884cd6c3
JG
639}
640
e011d2c1
PP
641static
642bool packet_header_field_type_is_valid(struct bt_ctf_trace *trace,
643 struct bt_ctf_field_type *packet_header_type)
644{
645 int ret;
646 bool is_valid = true;
647 struct bt_ctf_field_type *field_type = NULL;
648
649 if (!packet_header_type) {
650 /*
651 * No packet header field type: trace must have only
652 * one stream. At this point the stream class being
653 * added is not part of the trace yet, so we validate
654 * that the trace contains no stream classes yet.
655 */
656 if (trace->stream_classes->len >= 1) {
657 BT_LOGW_STR("Invalid packet header field type: "
658 "packet header field type does not exist but there's more than one stream class in the trace.");
659 goto invalid;
660 }
661
662 /* No packet header field type: valid at this point */
663 goto end;
664 }
665
666 /* Packet header field type, if it exists, must be a structure */
667 if (!bt_ctf_field_type_is_structure(packet_header_type)) {
668 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
669 "ft-addr=%p, ft-id=%s",
670 packet_header_type,
671 bt_ctf_field_type_id_string(packet_header_type->id));
672 goto invalid;
673 }
674
675 /*
676 * If there's a `magic` field, it must be a 32-bit unsigned
677 * integer field type. Also it must be the first field of the
678 * packet header field type.
679 */
680 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
681 packet_header_type, "magic");
682 if (field_type) {
683 const char *field_name;
684
685 if (!bt_ctf_field_type_is_integer(field_type)) {
686 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
687 "magic-ft-addr=%p, magic-ft-id=%s",
688 field_type,
689 bt_ctf_field_type_id_string(field_type->id));
690 goto invalid;
691 }
692
693 if (bt_ctf_field_type_integer_is_signed(field_type)) {
694 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
695 "magic-ft-addr=%p", field_type);
696 goto invalid;
697 }
698
699 if (bt_ctf_field_type_integer_get_size(field_type) != 32) {
700 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
701 "magic-ft-addr=%p, magic-ft-size=%u",
702 field_type,
703 bt_ctf_field_type_integer_get_size(field_type));
704 goto invalid;
705 }
706
707 ret = bt_ctf_field_type_structure_get_field_by_index(
708 packet_header_type, &field_name, NULL, 0);
709 assert(ret == 0);
710
711 if (strcmp(field_name, "magic") != 0) {
712 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
713 "magic-ft-addr=%p, first-field-name=\"%s\"",
714 field_type, field_name);
715 goto invalid;
716 }
717
718 BT_PUT(field_type);
719 }
720
721 /*
722 * If there's a `uuid` field, it must be an array field type of
723 * length 16 with an 8-bit unsigned integer element field type.
724 */
725 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
726 packet_header_type, "uuid");
727 if (field_type) {
728 struct bt_ctf_field_type *elem_ft;
729
730 if (!bt_ctf_field_type_is_array(field_type)) {
731 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
732 "uuid-ft-addr=%p, uuid-ft-id=%s",
733 field_type,
734 bt_ctf_field_type_id_string(field_type->id));
735 goto invalid;
736 }
737
738 if (bt_ctf_field_type_array_get_length(field_type) != 16) {
739 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
740 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64,
741 field_type,
742 bt_ctf_field_type_array_get_length(field_type));
743 goto invalid;
744 }
745
746 elem_ft = bt_ctf_field_type_array_get_element_type(field_type);
747 assert(elem_ft);
748
749 if (!bt_ctf_field_type_is_integer(elem_ft)) {
750 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
751 "elem-ft-addr=%p, elem-ft-id=%s",
752 elem_ft,
753 bt_ctf_field_type_id_string(elem_ft->id));
754 bt_put(elem_ft);
755 goto invalid;
756 }
757
758 if (bt_ctf_field_type_integer_is_signed(elem_ft)) {
759 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
760 "elem-ft-addr=%p", elem_ft);
761 bt_put(elem_ft);
762 goto invalid;
763 }
764
765 if (bt_ctf_field_type_integer_get_size(elem_ft) != 8) {
766 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
767 "elem-ft-addr=%p, elem-ft-size=%u",
768 elem_ft,
769 bt_ctf_field_type_integer_get_size(elem_ft));
770 bt_put(elem_ft);
771 goto invalid;
772 }
773
774 bt_put(elem_ft);
775 BT_PUT(field_type);
776 }
777
778 /*
779 * The `stream_id` field must exist if there's more than one
780 * stream classes in the trace.
781 */
782 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
783 packet_header_type, "stream_id");
784
785 if (!field_type && trace->stream_classes->len >= 1) {
786 BT_LOGW_STR("Invalid packet header field type: "
787 "`stream_id` field does not exist but there's more than one stream class in the trace.");
788 goto invalid;
789 }
790
791 /*
792 * If there's a `stream_id` field, it must be an unsigned
793 * integer field type.
794 */
795 if (field_type) {
796 if (!bt_ctf_field_type_is_integer(field_type)) {
797 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
798 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
799 field_type,
800 bt_ctf_field_type_id_string(field_type->id));
801 goto invalid;
802 }
803
804 if (bt_ctf_field_type_integer_is_signed(field_type)) {
805 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
806 "stream-id-ft-addr=%p", field_type);
807 goto invalid;
808 }
809
810 BT_PUT(field_type);
811 }
812
708ab211
PP
813 /*
814 * If there's a `packet_seq_num` field, it must be an unsigned
815 * integer field type.
816 */
817 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
818 packet_header_type, "packet_seq_num");
819 if (field_type) {
820 if (!bt_ctf_field_type_is_integer(field_type)) {
821 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
822 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
823 field_type,
824 bt_ctf_field_type_id_string(field_type->id));
825 goto invalid;
826 }
827
828 if (bt_ctf_field_type_integer_is_signed(field_type)) {
829 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
830 "packet-seq-num-ft-addr=%p", field_type);
831 goto invalid;
832 }
833
834 BT_PUT(field_type);
835 }
836
e011d2c1
PP
837 goto end;
838
839invalid:
840 is_valid = false;
841
842end:
843 bt_put(field_type);
844 return is_valid;
845}
846
847static
848bool packet_context_field_type_is_valid(struct bt_ctf_trace *trace,
849 struct bt_ctf_stream_class *stream_class,
850 struct bt_ctf_field_type *packet_context_type)
851{
852 bool is_valid = true;
853 struct bt_ctf_field_type *field_type = NULL;
854
855 if (!packet_context_type) {
856 /* No packet context field type: valid at this point */
857 goto end;
858 }
859
860 /* Packet context field type, if it exists, must be a structure */
861 if (!bt_ctf_field_type_is_structure(packet_context_type)) {
862 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
863 "ft-addr=%p, ft-id=%s",
864 packet_context_type,
865 bt_ctf_field_type_id_string(packet_context_type->id));
866 goto invalid;
867 }
868
869 /*
870 * If there's a `packet_size` field, it must be an unsigned
871 * integer field type.
872 */
873 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
874 packet_context_type, "packet_size");
875 if (field_type) {
876 if (!bt_ctf_field_type_is_integer(field_type)) {
877 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
878 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
879 field_type,
880 bt_ctf_field_type_id_string(field_type->id));
881 goto invalid;
882 }
883
884 if (bt_ctf_field_type_integer_is_signed(field_type)) {
885 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
886 "packet-size-ft-addr=%p", field_type);
887 goto invalid;
888 }
889
890 BT_PUT(field_type);
891 }
892
893 /*
894 * If there's a `content_size` field, it must be an unsigned
895 * integer field type.
896 */
897 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
898 packet_context_type, "content_size");
899 if (field_type) {
900 if (!bt_ctf_field_type_is_integer(field_type)) {
901 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
902 "content-size-ft-addr=%p, content-size-ft-id=%s",
903 field_type,
904 bt_ctf_field_type_id_string(field_type->id));
905 goto invalid;
906 }
907
908 if (bt_ctf_field_type_integer_is_signed(field_type)) {
909 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
910 "content-size-ft-addr=%p", field_type);
911 goto invalid;
912 }
913
914 BT_PUT(field_type);
915 }
916
917 /*
918 * If there's a `events_discarded` field, it must be an unsigned
919 * integer field type.
920 */
921 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
922 packet_context_type, "events_discarded");
923 if (field_type) {
924 if (!bt_ctf_field_type_is_integer(field_type)) {
925 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
926 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
927 field_type,
928 bt_ctf_field_type_id_string(field_type->id));
929 goto invalid;
930 }
931
932 if (bt_ctf_field_type_integer_is_signed(field_type)) {
933 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
934 "events-discarded-ft-addr=%p", field_type);
935 goto invalid;
936 }
937
938 BT_PUT(field_type);
939 }
940
941 /*
942 * If there's a `timestamp_begin` field, it must be an unsigned
943 * integer field type. Also, if the trace is not a CTF writer's
944 * trace, then we cannot automatically set the mapped clock
945 * class of this field, so it must have a mapped clock class.
946 */
947 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
948 packet_context_type, "timestamp_begin");
949 if (field_type) {
950 if (!bt_ctf_field_type_is_integer(field_type)) {
951 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
952 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
953 field_type,
954 bt_ctf_field_type_id_string(field_type->id));
955 goto invalid;
956 }
957
958 if (bt_ctf_field_type_integer_is_signed(field_type)) {
959 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
960 "timestamp-begin-ft-addr=%p", field_type);
961 goto invalid;
962 }
963
964 if (!trace->is_created_by_writer) {
965 struct bt_ctf_clock_class *clock_class =
966 bt_ctf_field_type_integer_get_mapped_clock_class(
967 field_type);
968
969 bt_put(clock_class);
970 if (!clock_class) {
971 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
972 "timestamp-begin-ft-addr=%p", field_type);
973 goto invalid;
974 }
975 }
976
977 BT_PUT(field_type);
978 }
979
980 /*
981 * If there's a `timestamp_end` field, it must be an unsigned
982 * integer field type. Also, if the trace is not a CTF writer's
983 * trace, then we cannot automatically set the mapped clock
984 * class of this field, so it must have a mapped clock class.
985 */
986 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
987 packet_context_type, "timestamp_end");
988 if (field_type) {
989 if (!bt_ctf_field_type_is_integer(field_type)) {
990 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
991 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
992 field_type,
993 bt_ctf_field_type_id_string(field_type->id));
994 goto invalid;
995 }
996
997 if (bt_ctf_field_type_integer_is_signed(field_type)) {
998 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
999 "timestamp-end-ft-addr=%p", field_type);
1000 goto invalid;
1001 }
1002
1003 if (!trace->is_created_by_writer) {
1004 struct bt_ctf_clock_class *clock_class =
1005 bt_ctf_field_type_integer_get_mapped_clock_class(
1006 field_type);
1007
1008 bt_put(clock_class);
1009 if (!clock_class) {
1010 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
1011 "timestamp-end-ft-addr=%p", field_type);
1012 goto invalid;
1013 }
1014 }
1015
1016 BT_PUT(field_type);
1017 }
1018
1019 goto end;
1020
1021invalid:
1022 is_valid = false;
1023
1024end:
1025 bt_put(field_type);
1026 return is_valid;
1027}
1028
1029static
1030bool event_header_field_type_is_valid(struct bt_ctf_trace *trace,
1031 struct bt_ctf_stream_class *stream_class,
1032 struct bt_ctf_field_type *event_header_type)
1033{
1034 bool is_valid = true;
1035 struct bt_ctf_field_type *field_type = NULL;
1036
1037 /*
1038 * We do not validate that the `timestamp` field exists here
1039 * because CTF does not require this exact name to be mapped to
1040 * a clock class.
1041 */
1042
1043 if (!event_header_type) {
1044 /*
1045 * No event header field type: stream class must have
1046 * only one event class.
1047 */
1048 if (bt_ctf_stream_class_get_event_class_count(stream_class) > 1) {
1049 BT_LOGW_STR("Invalid event header field type: "
1050 "event header field type does not exist but there's more than one event class in the stream class.");
1051 goto invalid;
1052 }
1053
1054 /* No event header field type: valid at this point */
1055 goto end;
1056 }
1057
1058 /* Event header field type, if it exists, must be a structure */
1059 if (!bt_ctf_field_type_is_structure(event_header_type)) {
1060 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
1061 "ft-addr=%p, ft-id=%s",
1062 event_header_type,
1063 bt_ctf_field_type_id_string(event_header_type->id));
1064 goto invalid;
1065 }
1066
1067 /*
1068 * If there's an `id` field, it must be an unsigned integer
1069 * field type or an enumeration field type with an unsigned
1070 * integer container field type.
1071 */
1072 field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1073 event_header_type, "id");
1074 if (field_type) {
1075 struct bt_ctf_field_type *int_ft;
1076
1077 if (bt_ctf_field_type_is_integer(field_type)) {
1078 int_ft = bt_get(field_type);
1079 } else if (bt_ctf_field_type_is_enumeration(field_type)) {
1080 int_ft = bt_ctf_field_type_enumeration_get_container_type(
1081 field_type);
1082 } else {
1083 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1084 "id-ft-addr=%p, id-ft-id=%s",
1085 field_type,
1086 bt_ctf_field_type_id_string(field_type->id));
1087 goto invalid;
1088 }
1089
1090 assert(int_ft);
1091 if (bt_ctf_field_type_integer_is_signed(int_ft)) {
1092 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1093 "id-ft-addr=%p", int_ft);
1094 goto invalid;
1095 }
1096
1097 bt_put(int_ft);
1098 BT_PUT(field_type);
1099 }
1100
1101 goto end;
1102
1103invalid:
1104 is_valid = false;
1105
1106end:
1107 bt_put(field_type);
1108 return is_valid;
1109}
1110
ef0c4a15
JG
1111int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
1112 struct bt_ctf_stream_class *stream_class)
1113{
544d0515
PP
1114 int ret;
1115 int64_t i;
ef0c4a15 1116 int64_t stream_id;
09840de5
PP
1117 struct bt_ctf_validation_output trace_sc_validation_output = { 0 };
1118 struct bt_ctf_validation_output *ec_validation_outputs = NULL;
1119 const enum bt_ctf_validation_flag trace_sc_validation_flags =
1120 BT_CTF_VALIDATION_FLAG_TRACE |
1121 BT_CTF_VALIDATION_FLAG_STREAM;
1122 const enum bt_ctf_validation_flag ec_validation_flags =
1123 BT_CTF_VALIDATION_FLAG_EVENT;
1124 struct bt_ctf_field_type *packet_header_type = NULL;
1125 struct bt_ctf_field_type *packet_context_type = NULL;
1126 struct bt_ctf_field_type *event_header_type = NULL;
1127 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
544d0515 1128 int64_t event_class_count;
2789e5d9 1129 struct bt_ctf_trace *current_parent_trace = NULL;
ef0c4a15 1130
95c09b3a
PP
1131 if (!trace) {
1132 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1133 ret = -1;
1134 goto end;
1135 }
1136
1137 if (!stream_class) {
1138 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1139 ret = -1;
1140 goto end;
1141 }
1142
1143 if (trace->is_static) {
1144 BT_LOGW_STR("Invalid parameter: trace is static.");
ef0c4a15
JG
1145 ret = -1;
1146 goto end;
1147 }
1148
95c09b3a
PP
1149 BT_LOGD("Adding stream class to trace: "
1150 "trace-addr=%p, trace-name=\"%s\", "
1151 "stream-class-addr=%p, stream-class-name=\"%s\", "
1152 "stream-class-id=%" PRId64,
1153 trace, bt_ctf_trace_get_name(trace),
1154 stream_class, bt_ctf_stream_class_get_name(stream_class),
1155 bt_ctf_stream_class_get_id(stream_class));
1156
2789e5d9
JG
1157 current_parent_trace = bt_ctf_stream_class_get_trace(stream_class);
1158 if (current_parent_trace) {
1159 /* Stream class is already associated to a trace, abort. */
95c09b3a
PP
1160 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1161 "stream-class-trace-addr=%p, "
1162 "stream-class-trace-name=\"%s\"",
1163 current_parent_trace,
1164 bt_ctf_trace_get_name(current_parent_trace));
2789e5d9
JG
1165 ret = -1;
1166 goto end;
1167 }
1168
09840de5
PP
1169 event_class_count =
1170 bt_ctf_stream_class_get_event_class_count(stream_class);
1171 assert(event_class_count >= 0);
1172
fe13b5c0 1173 if (stream_class->clock) {
ac0c6bdd
PP
1174 struct bt_ctf_clock_class *stream_clock_class =
1175 stream_class->clock->clock_class;
1176
1177 if (trace->is_created_by_writer) {
1178 /*
1179 * Make sure this clock was also added to the
1180 * trace (potentially through its CTF writer
1181 * owner).
1182 */
1183 size_t i;
1184
1185 for (i = 0; i < trace->clocks->len; i++) {
1186 if (trace->clocks->pdata[i] ==
1187 stream_clock_class) {
1188 /* Found! */
1189 break;
1190 }
1191 }
1192
1193 if (i == trace->clocks->len) {
1194 /* Not found */
95c09b3a
PP
1195 BT_LOGW("Stream class's clock's class is not part of the trace: "
1196 "clock-class-addr=%p, clock-class-name=\"%s\"",
1197 stream_clock_class,
1198 bt_ctf_clock_class_get_name(stream_clock_class));
fe13b5c0
PP
1199 ret = -1;
1200 goto end;
1201 }
1202 } else {
ac0c6bdd
PP
1203 /*
1204 * This trace was NOT created by a CTF writer,
1205 * thus do not allow the stream class to add to
1206 * have a clock at all. Those are two
1207 * independent APIs (non-writer and writer
1208 * APIs), and isolating them simplifies things.
1209 */
95c09b3a
PP
1210 BT_LOGW("Cannot add stream class with a clock to a trace which was not created by a CTF writer object: "
1211 "clock-class-addr=%p, clock-class-name=\"%s\"",
1212 stream_clock_class,
1213 bt_ctf_clock_class_get_name(stream_clock_class));
ac0c6bdd
PP
1214 ret = -1;
1215 goto end;
fe13b5c0
PP
1216 }
1217 }
1218
09840de5
PP
1219 /*
1220 * We're about to freeze both the trace and the stream class.
1221 * Also, each event class contained in this stream class are
1222 * already frozen.
1223 *
1224 * This trace, this stream class, and all its event classes
1225 * should be valid at this point.
1226 *
1227 * Validate trace and stream class first, then each event
1228 * class of this stream class can be validated individually.
1229 */
1230 packet_header_type =
1231 bt_ctf_trace_get_packet_header_type(trace);
1232 packet_context_type =
1233 bt_ctf_stream_class_get_packet_context_type(stream_class);
1234 event_header_type =
1235 bt_ctf_stream_class_get_event_header_type(stream_class);
1236 stream_event_ctx_type =
1237 bt_ctf_stream_class_get_event_context_type(stream_class);
95c09b3a
PP
1238
1239 BT_LOGD("Validating trace and stream class field types.");
09840de5
PP
1240 ret = bt_ctf_validate_class_types(trace->environment,
1241 packet_header_type, packet_context_type, event_header_type,
1242 stream_event_ctx_type, NULL, NULL, trace->valid,
1243 stream_class->valid, 1, &trace_sc_validation_output,
1244 trace_sc_validation_flags);
1245 BT_PUT(packet_header_type);
1246 BT_PUT(packet_context_type);
1247 BT_PUT(event_header_type);
1248 BT_PUT(stream_event_ctx_type);
1249
2bd7f864 1250 if (ret) {
09840de5
PP
1251 /*
1252 * This means something went wrong during the validation
1253 * process, not that the objects are invalid.
1254 */
95c09b3a
PP
1255 BT_LOGE("Failed to validate trace and stream class field types: "
1256 "ret=%d", ret);
2bd7f864
JG
1257 goto end;
1258 }
1259
09840de5
PP
1260 if ((trace_sc_validation_output.valid_flags &
1261 trace_sc_validation_flags) !=
1262 trace_sc_validation_flags) {
1263 /* Invalid trace/stream class */
95c09b3a
PP
1264 BT_LOGW("Invalid trace or stream class field types: "
1265 "valid-flags=0x%x",
1266 trace_sc_validation_output.valid_flags);
09840de5
PP
1267 ret = -1;
1268 goto end;
1269 }
1270
1271 if (event_class_count > 0) {
1272 ec_validation_outputs = g_new0(struct bt_ctf_validation_output,
1273 event_class_count);
1274 if (!ec_validation_outputs) {
95c09b3a 1275 BT_LOGE_STR("Failed to allocate one validation output structure.");
09840de5
PP
1276 ret = -1;
1277 goto end;
1278 }
1279 }
1280
1281 /* Validate each event class individually */
6474e016 1282 for (i = 0; i < event_class_count; i++) {
09840de5 1283 struct bt_ctf_event_class *event_class =
9ac68eb1
PP
1284 bt_ctf_stream_class_get_event_class_by_index(
1285 stream_class, i);
09840de5
PP
1286 struct bt_ctf_field_type *event_context_type = NULL;
1287 struct bt_ctf_field_type *event_payload_type = NULL;
1288
1289 event_context_type =
1290 bt_ctf_event_class_get_context_type(event_class);
1291 event_payload_type =
1292 bt_ctf_event_class_get_payload_type(event_class);
1293
1294 /*
1295 * It is important to use the field types returned by
1296 * the previous trace and stream class validation here
1297 * because copies could have been made.
1298 */
95c09b3a
PP
1299 BT_LOGD("Validating event class's field types: "
1300 "addr=%p, name=\"%s\", id=%" PRId64,
1301 event_class, bt_ctf_event_class_get_name(event_class),
1302 bt_ctf_event_class_get_id(event_class));
09840de5
PP
1303 ret = bt_ctf_validate_class_types(trace->environment,
1304 trace_sc_validation_output.packet_header_type,
1305 trace_sc_validation_output.packet_context_type,
1306 trace_sc_validation_output.event_header_type,
1307 trace_sc_validation_output.stream_event_ctx_type,
1308 event_context_type, event_payload_type,
1309 1, 1, event_class->valid, &ec_validation_outputs[i],
1310 ec_validation_flags);
1311 BT_PUT(event_context_type);
1312 BT_PUT(event_payload_type);
1313 BT_PUT(event_class);
1314
1315 if (ret) {
95c09b3a
PP
1316 BT_LOGE("Failed to validate event class field types: "
1317 "ret=%d", ret);
09840de5
PP
1318 goto end;
1319 }
1320
1321 if ((ec_validation_outputs[i].valid_flags &
1322 ec_validation_flags) != ec_validation_flags) {
1323 /* Invalid event class */
95c09b3a
PP
1324 BT_LOGW("Invalid event class field types: "
1325 "valid-flags=0x%x",
1326 ec_validation_outputs[i].valid_flags);
09840de5
PP
1327 ret = -1;
1328 goto end;
1329 }
1330 }
1331
ef0c4a15
JG
1332 stream_id = bt_ctf_stream_class_get_id(stream_class);
1333 if (stream_id < 0) {
1334 stream_id = trace->next_stream_id++;
9ac68eb1 1335 if (stream_id < 0) {
95c09b3a 1336 BT_LOGE_STR("No more stream class IDs available.");
9ac68eb1
PP
1337 ret = -1;
1338 goto end;
1339 }
ef0c4a15
JG
1340
1341 /* Try to assign a new stream id */
1342 for (i = 0; i < trace->stream_classes->len; i++) {
1343 if (stream_id == bt_ctf_stream_class_get_id(
1344 trace->stream_classes->pdata[i])) {
1345 /* Duplicate stream id found */
95c09b3a
PP
1346 BT_LOGW("Duplicate stream class ID: "
1347 "id=%" PRId64, (int64_t) stream_id);
ef0c4a15
JG
1348 ret = -1;
1349 goto end;
1350 }
1351 }
1352
0ddffae5 1353 if (bt_ctf_stream_class_set_id_no_check(stream_class,
95c09b3a 1354 stream_id)) {
ef0c4a15 1355 /* TODO Should retry with a different stream id */
95c09b3a
PP
1356 BT_LOGE("Cannot set stream class's ID: "
1357 "id=%" PRId64, (int64_t) stream_id);
ef0c4a15
JG
1358 ret = -1;
1359 goto end;
1360 }
1361 }
1362
e011d2c1
PP
1363 /*
1364 * At this point all the field types in the validation output
1365 * are valid. Validate the semantics of some scopes according to
1366 * the CTF specification.
1367 */
1368 if (!packet_header_field_type_is_valid(trace,
1369 trace_sc_validation_output.packet_header_type)) {
95c09b3a 1370 BT_LOGW_STR("Invalid trace's packet header field type.");
e011d2c1
PP
1371 ret = -1;
1372 goto end;
1373 }
1374
1375 if (!packet_context_field_type_is_valid(trace,
1376 stream_class,
1377 trace_sc_validation_output.packet_context_type)) {
95c09b3a 1378 BT_LOGW_STR("Invalid stream class's packet context field type.");
e011d2c1
PP
1379 ret = -1;
1380 goto end;
1381 }
1382
1383 if (!event_header_field_type_is_valid(trace,
1384 stream_class,
1385 trace_sc_validation_output.event_header_type)) {
95c09b3a 1386 BT_LOGW_STR("Invalid steam class's event header field type.");
e011d2c1
PP
1387 ret = -1;
1388 goto end;
1389 }
1390
1391 /*
1392 * Now is the time to automatically map specific field types of
1393 * the stream class's packet context and event header field
1394 * types to the stream class's clock's class if they are not
1395 * mapped to a clock class yet. We do it here because we know
1396 * that after this point, everything is frozen so it won't be
1397 * possible for the user to modify the stream class's clock, or
1398 * to map those field types to other clock classes.
1399 */
1400 if (trace->is_created_by_writer) {
1401 if (bt_ctf_stream_class_map_clock_class(stream_class,
1402 trace_sc_validation_output.packet_context_type,
1403 trace_sc_validation_output.event_header_type)) {
95c09b3a 1404 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
e011d2c1
PP
1405 ret = -1;
1406 goto end;
1407 }
1408 }
1409
e6a8e8e4 1410 bt_object_set_parent(stream_class, trace);
ef0c4a15
JG
1411 g_ptr_array_add(trace->stream_classes, stream_class);
1412
1413 /*
09840de5
PP
1414 * At this point we know that the function will be successful.
1415 * Therefore we can replace the trace and stream class field
1416 * types with what's in their validation output structure and
1417 * mark them as valid. We can also replace the field types of
1418 * all the event classes of the stream class and mark them as
1419 * valid.
1420 */
1421 bt_ctf_validation_replace_types(trace, stream_class, NULL,
1422 &trace_sc_validation_output, trace_sc_validation_flags);
1423 trace->valid = 1;
1424 stream_class->valid = 1;
1425
1426 /*
1427 * Put what was not moved in bt_ctf_validation_replace_types().
1428 */
1429 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
1430
6474e016 1431 for (i = 0; i < event_class_count; i++) {
09840de5 1432 struct bt_ctf_event_class *event_class =
9ac68eb1
PP
1433 bt_ctf_stream_class_get_event_class_by_index(
1434 stream_class, i);
09840de5
PP
1435
1436 bt_ctf_validation_replace_types(NULL, NULL, event_class,
1437 &ec_validation_outputs[i], ec_validation_flags);
1438 event_class->valid = 1;
1439 BT_PUT(event_class);
1440
1441 /*
1442 * Put what was not moved in
1443 * bt_ctf_validation_replace_types().
1444 */
1445 bt_ctf_validation_output_put_types(&ec_validation_outputs[i]);
1446 }
1447
09840de5
PP
1448 /*
1449 * Freeze the trace and the stream class.
1450 */
f67f3a6e 1451 bt_ctf_stream_class_freeze(stream_class);
09840de5
PP
1452 bt_ctf_trace_freeze(trace);
1453
9b888ff3
JG
1454 /* Notifiy listeners of the trace's schema modification. */
1455 bt_ctf_stream_class_visit(stream_class,
d9a13d86 1456 bt_ctf_trace_object_modification, trace);
95c09b3a
PP
1457 BT_LOGD("Added stream class to trace: "
1458 "trace-addr=%p, trace-name=\"%s\", "
1459 "stream-class-addr=%p, stream-class-name=\"%s\", "
1460 "stream-class-id=%" PRId64,
1461 trace, bt_ctf_trace_get_name(trace),
1462 stream_class, bt_ctf_stream_class_get_name(stream_class),
1463 bt_ctf_stream_class_get_id(stream_class));
1464
ef0c4a15 1465end:
d3814b54 1466 if (ret) {
e6a8e8e4 1467 bt_object_set_parent(stream_class, NULL);
09840de5
PP
1468
1469 if (ec_validation_outputs) {
6474e016 1470 for (i = 0; i < event_class_count; i++) {
09840de5
PP
1471 bt_ctf_validation_output_put_types(
1472 &ec_validation_outputs[i]);
1473 }
1474 }
d3814b54 1475 }
09840de5
PP
1476
1477 g_free(ec_validation_outputs);
1478 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
2789e5d9 1479 bt_put(current_parent_trace);
09840de5
PP
1480 assert(!packet_header_type);
1481 assert(!packet_context_type);
1482 assert(!event_header_type);
1483 assert(!stream_event_ctx_type);
ef0c4a15
JG
1484 return ret;
1485}
1486
544d0515 1487int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace)
c1e730fe 1488{
544d0515 1489 int64_t ret;
c1e730fe
PP
1490
1491 if (!trace) {
95c09b3a 1492 BT_LOGW_STR("Invalid parameter: trace is NULL.");
9ac68eb1 1493 ret = (int64_t) -1;
c1e730fe
PP
1494 goto end;
1495 }
1496
9ac68eb1 1497 ret = (int64_t) trace->streams->len;
c1e730fe
PP
1498
1499end:
1500 return ret;
1501}
1502
9ac68eb1
PP
1503struct bt_ctf_stream *bt_ctf_trace_get_stream_by_index(
1504 struct bt_ctf_trace *trace,
1505 uint64_t index)
c1e730fe
PP
1506{
1507 struct bt_ctf_stream *stream = NULL;
1508
95c09b3a
PP
1509 if (!trace) {
1510 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1511 goto end;
1512 }
1513
1514 if (index >= trace->streams->len) {
1515 BT_LOGW("Invalid parameter: index is out of bounds: "
1516 "addr=%p, name=\"%s\", "
1517 "index=%" PRIu64 ", count=%u",
1518 trace, bt_ctf_trace_get_name(trace),
1519 index, trace->streams->len);
c1e730fe
PP
1520 goto end;
1521 }
1522
1523 stream = bt_get(g_ptr_array_index(trace->streams, index));
1524
1525end:
1526 return stream;
1527}
1528
544d0515 1529int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
ef0c4a15 1530{
544d0515 1531 int64_t ret;
ef0c4a15
JG
1532
1533 if (!trace) {
95c09b3a 1534 BT_LOGW_STR("Invalid parameter: trace is NULL.");
9ac68eb1 1535 ret = (int64_t) -1;
ef0c4a15
JG
1536 goto end;
1537 }
1538
9ac68eb1 1539 ret = (int64_t) trace->stream_classes->len;
ef0c4a15
JG
1540end:
1541 return ret;
1542}
1543
9ac68eb1
PP
1544struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_index(
1545 struct bt_ctf_trace *trace, uint64_t index)
ef0c4a15
JG
1546{
1547 struct bt_ctf_stream_class *stream_class = NULL;
1548
95c09b3a
PP
1549 if (!trace) {
1550 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1551 goto end;
1552 }
1553
1554 if (index >= trace->stream_classes->len) {
1555 BT_LOGW("Invalid parameter: index is out of bounds: "
1556 "addr=%p, name=\"%s\", "
1557 "index=%" PRIu64 ", count=%u",
1558 trace, bt_ctf_trace_get_name(trace),
1559 index, trace->stream_classes->len);
ef0c4a15
JG
1560 goto end;
1561 }
1562
1563 stream_class = g_ptr_array_index(trace->stream_classes, index);
83509119 1564 bt_get(stream_class);
ef0c4a15
JG
1565end:
1566 return stream_class;
1567}
1568
4841ccc1 1569struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
9ac68eb1 1570 struct bt_ctf_trace *trace, uint64_t id_param)
4841ccc1
PP
1571{
1572 int i;
1573 struct bt_ctf_stream_class *stream_class = NULL;
9ac68eb1 1574 int64_t id = (int64_t) id_param;
4841ccc1 1575
95c09b3a
PP
1576 if (!trace) {
1577 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1578 goto end;
1579 }
1580
1581 if (id < 0) {
1582 BT_LOGW("Invalid parameter: invalid stream class's ID: "
1583 "trace-addr=%p, trace-name=\"%s\", id=%" PRIu64,
1584 trace, bt_ctf_trace_get_name(trace), id_param);
4841ccc1
PP
1585 goto end;
1586 }
1587
6474e016 1588 for (i = 0; i < trace->stream_classes->len; i++) {
4841ccc1
PP
1589 struct bt_ctf_stream_class *stream_class_candidate;
1590
1591 stream_class_candidate =
1592 g_ptr_array_index(trace->stream_classes, i);
1593
1594 if (bt_ctf_stream_class_get_id(stream_class_candidate) ==
1595 (int64_t) id) {
1596 stream_class = stream_class_candidate;
83509119 1597 bt_get(stream_class);
4841ccc1
PP
1598 goto end;
1599 }
1600 }
1601
1602end:
1603 return stream_class;
1604}
1605
ac0c6bdd 1606struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
7e4347a3
PP
1607 struct bt_ctf_trace *trace, const char *name)
1608{
1609 size_t i;
ac0c6bdd 1610 struct bt_ctf_clock_class *clock_class = NULL;
7e4347a3 1611
95c09b3a
PP
1612 if (!trace) {
1613 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1614 goto end;
1615 }
1616
1617 if (!name) {
1618 BT_LOGW_STR("Invalid parameter: name is NULL.");
7e4347a3
PP
1619 goto end;
1620 }
1621
6474e016 1622 for (i = 0; i < trace->clocks->len; i++) {
ac0c6bdd 1623 struct bt_ctf_clock_class *cur_clk =
7e4347a3 1624 g_ptr_array_index(trace->clocks, i);
ac0c6bdd 1625 const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk);
7e4347a3
PP
1626
1627 if (!cur_clk_name) {
1628 goto end;
1629 }
1630
1631 if (!strcmp(cur_clk_name, name)) {
ac0c6bdd
PP
1632 clock_class = cur_clk;
1633 bt_get(clock_class);
7e4347a3
PP
1634 goto end;
1635 }
1636 }
1637
1638end:
ac0c6bdd 1639 return clock_class;
7e4347a3
PP
1640}
1641
c9d90a34 1642BT_HIDDEN
c55a9f58 1643bt_bool bt_ctf_trace_has_clock_class(struct bt_ctf_trace *trace,
c9d90a34
PP
1644 struct bt_ctf_clock_class *clock_class)
1645{
1646 struct search_query query = { .value = clock_class, .found = 0 };
1647
1648 assert(trace);
1649 assert(clock_class);
1650
1651 g_ptr_array_foreach(trace->clocks, value_exists, &query);
1652 return query.found;
1653}
1654
bc37ae52 1655BT_HIDDEN
dc3fffef 1656const char *get_byte_order_string(enum bt_ctf_byte_order byte_order)
bc37ae52
JG
1657{
1658 const char *string;
1659
1660 switch (byte_order) {
dc3fffef 1661 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
bc37ae52
JG
1662 string = "le";
1663 break;
dc3fffef 1664 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
bc37ae52
JG
1665 string = "be";
1666 break;
dc3fffef
PP
1667 case BT_CTF_BYTE_ORDER_NATIVE:
1668 string = "native";
bc37ae52 1669 break;
dc3fffef 1670 default:
0fbb9a9f 1671 abort();
bc37ae52
JG
1672 }
1673
1674 return string;
1675}
1676
1677static
1678int append_trace_metadata(struct bt_ctf_trace *trace,
1679 struct metadata_context *context)
1680{
1681 unsigned char *uuid = trace->uuid;
e011d2c1 1682 int ret = 0;
bc37ae52 1683
d41cff38 1684 if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE ||
2f4b93cd
PP
1685 trace->native_byte_order == BT_CTF_BYTE_ORDER_UNSPECIFIED) {
1686 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
95c09b3a
PP
1687 "set it with bt_ctf_trace_set_native_byte_order(): "
1688 "addr=%p, name=\"%s\"",
1689 trace, bt_ctf_trace_get_name(trace));
4a32fda0
PP
1690 ret = -1;
1691 goto end;
1692 }
bc37ae52 1693
4a32fda0 1694 g_string_append(context->string, "trace {\n");
bc37ae52
JG
1695 g_string_append(context->string, "\tmajor = 1;\n");
1696 g_string_append(context->string, "\tminor = 8;\n");
dc3fffef
PP
1697 assert(trace->native_byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ||
1698 trace->native_byte_order == BT_CTF_BYTE_ORDER_BIG_ENDIAN ||
1699 trace->native_byte_order == BT_CTF_BYTE_ORDER_NETWORK);
4a32fda0
PP
1700
1701 if (trace->uuid_set) {
1702 g_string_append_printf(context->string,
1703 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
1704 uuid[0], uuid[1], uuid[2], uuid[3],
1705 uuid[4], uuid[5], uuid[6], uuid[7],
1706 uuid[8], uuid[9], uuid[10], uuid[11],
1707 uuid[12], uuid[13], uuid[14], uuid[15]);
1708 }
1709
bc37ae52 1710 g_string_append_printf(context->string, "\tbyte_order = %s;\n",
dc3fffef 1711 get_byte_order_string(trace->native_byte_order));
bc37ae52 1712
e011d2c1
PP
1713 if (trace->packet_header_type) {
1714 g_string_append(context->string, "\tpacket.header := ");
1715 context->current_indentation_level++;
1716 g_string_assign(context->field_name, "");
95c09b3a 1717 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
e011d2c1
PP
1718 ret = bt_ctf_field_type_serialize(trace->packet_header_type,
1719 context);
1720 if (ret) {
1721 goto end;
1722 }
1723 context->current_indentation_level--;
bc37ae52 1724 }
bc37ae52
JG
1725
1726 g_string_append(context->string, ";\n};\n\n");
1727end:
1728 return ret;
1729}
1730
bc37ae52
JG
1731static
1732void append_env_metadata(struct bt_ctf_trace *trace,
1733 struct metadata_context *context)
1734{
544d0515
PP
1735 int64_t i;
1736 int64_t env_size;
7f800dc7
PP
1737
1738 env_size = bt_ctf_attributes_get_count(trace->environment);
7f800dc7 1739 if (env_size <= 0) {
bc37ae52
JG
1740 return;
1741 }
1742
1743 g_string_append(context->string, "env {\n");
7f800dc7 1744
6474e016 1745 for (i = 0; i < env_size; i++) {
dac5c838 1746 struct bt_value *env_field_value_obj = NULL;
7f800dc7 1747 const char *entry_name;
7f800dc7
PP
1748
1749 entry_name = bt_ctf_attributes_get_field_name(
1750 trace->environment, i);
1751 env_field_value_obj = bt_ctf_attributes_get_field_value(
1752 trace->environment, i);
1753
95c09b3a
PP
1754 assert(entry_name);
1755 assert(env_field_value_obj);
7f800dc7 1756
dac5c838
PP
1757 switch (bt_value_get_type(env_field_value_obj)) {
1758 case BT_VALUE_TYPE_INTEGER:
b8248cc0
PP
1759 {
1760 int ret;
1761 int64_t int_value;
1762
dac5c838 1763 ret = bt_value_integer_get(env_field_value_obj,
7f800dc7 1764 &int_value);
95c09b3a 1765 assert(ret == 0);
7f800dc7
PP
1766 g_string_append_printf(context->string,
1767 "\t%s = %" PRId64 ";\n", entry_name,
1768 int_value);
1769 break;
b8248cc0 1770 }
dac5c838 1771 case BT_VALUE_TYPE_STRING:
7f800dc7
PP
1772 {
1773 int ret;
1774 const char *str_value;
1775 char *escaped_str = NULL;
1776
dac5c838 1777 ret = bt_value_string_get(env_field_value_obj,
7f800dc7 1778 &str_value);
95c09b3a 1779 assert(ret == 0);
7f800dc7 1780 escaped_str = g_strescape(str_value, NULL);
7f800dc7 1781 if (!escaped_str) {
95c09b3a
PP
1782 BT_LOGE("Cannot escape string: string=\"%s\"",
1783 str_value);
7f800dc7
PP
1784 goto loop_next;
1785 }
1786
1787 g_string_append_printf(context->string,
1788 "\t%s = \"%s\";\n", entry_name, escaped_str);
1789 free(escaped_str);
1790 break;
1791 }
7f800dc7
PP
1792 default:
1793 goto loop_next;
1794 }
1795
1796loop_next:
83509119 1797 BT_PUT(env_field_value_obj);
7f800dc7
PP
1798 }
1799
bc37ae52
JG
1800 g_string_append(context->string, "};\n\n");
1801}
1802
1803char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
1804{
1805 char *metadata = NULL;
1806 struct metadata_context *context = NULL;
1807 int err = 0;
1808 size_t i;
1809
1810 if (!trace) {
95c09b3a 1811 BT_LOGW_STR("Invalid parameter: trace is NULL.");
bc37ae52
JG
1812 goto end;
1813 }
1814
1815 context = g_new0(struct metadata_context, 1);
1816 if (!context) {
95c09b3a 1817 BT_LOGE_STR("Failed to allocate one metadata context.");
bc37ae52
JG
1818 goto end;
1819 }
1820
1821 context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
1822 context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
1823 g_string_append(context->string, "/* CTF 1.8 */\n\n");
1824 if (append_trace_metadata(trace, context)) {
95c09b3a 1825 /* append_trace_metadata() logs errors */
bc37ae52
JG
1826 goto error;
1827 }
1828 append_env_metadata(trace, context);
1829 g_ptr_array_foreach(trace->clocks,
ac0c6bdd 1830 (GFunc)bt_ctf_clock_class_serialize, context);
bc37ae52
JG
1831
1832 for (i = 0; i < trace->stream_classes->len; i++) {
95c09b3a 1833 /* bt_ctf_stream_class_serialize() logs details */
bc37ae52
JG
1834 err = bt_ctf_stream_class_serialize(
1835 trace->stream_classes->pdata[i], context);
1836 if (err) {
95c09b3a 1837 /* bt_ctf_stream_class_serialize() logs errors */
bc37ae52
JG
1838 goto error;
1839 }
1840 }
1841
1842 metadata = context->string->str;
95c09b3a 1843
bc37ae52
JG
1844error:
1845 g_string_free(context->string, err ? TRUE : FALSE);
1846 g_string_free(context->field_name, TRUE);
1847 g_free(context);
95c09b3a 1848
bc37ae52
JG
1849end:
1850 return metadata;
1851}
1852
8a716c8d
PP
1853enum bt_ctf_byte_order bt_ctf_trace_get_native_byte_order(
1854 struct bt_ctf_trace *trace)
4ed90fb3
JG
1855{
1856 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
1857
1858 if (!trace) {
95c09b3a 1859 BT_LOGW_STR("Invalid parameter: trace is NULL.");
4ed90fb3
JG
1860 goto end;
1861 }
1862
dc3fffef
PP
1863 ret = trace->native_byte_order;
1864
4ed90fb3
JG
1865end:
1866 return ret;
1867}
1868
391c8f0d 1869int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace,
bc37ae52
JG
1870 enum bt_ctf_byte_order byte_order)
1871{
1872 int ret = 0;
bc37ae52 1873
95c09b3a
PP
1874 if (!trace) {
1875 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1876 ret = -1;
1877 goto end;
1878 }
1879
1880 if (trace->frozen) {
1881 BT_LOGW("Invalid parameter: trace is frozen: "
1882 "addr=%p, name=\"%s\"",
1883 trace, bt_ctf_trace_get_name(trace));
bc37ae52
JG
1884 ret = -1;
1885 goto end;
1886 }
1887
d41cff38 1888 if (trace->is_created_by_writer &&
2f4b93cd
PP
1889 byte_order == BT_CTF_BYTE_ORDER_UNSPECIFIED) {
1890 BT_LOGW("Invalid parameter: BT_CTF_BYTE_ORDER_UNSPECIFIED byte order is not allowed for a CTF writer trace: "
d41cff38
PP
1891 "addr=%p, name=\"%s\"",
1892 trace, bt_ctf_trace_get_name(trace));
1893 ret = -1;
1894 goto end;
1895 }
1896
dc3fffef
PP
1897 if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN &&
1898 byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN &&
1899 byte_order != BT_CTF_BYTE_ORDER_NETWORK) {
95c09b3a
PP
1900 BT_LOGW("Invalid parameter: invalid byte order: "
1901 "addr=%p, name=\"%s\", bo=%s",
1902 trace, bt_ctf_trace_get_name(trace),
1903 bt_ctf_byte_order_string(byte_order));
bc37ae52
JG
1904 ret = -1;
1905 goto end;
1906 }
1907
dc3fffef 1908 trace->native_byte_order = byte_order;
95c09b3a
PP
1909 BT_LOGV("Set trace's native byte order: "
1910 "addr=%p, name=\"%s\", bo=%s",
1911 trace, bt_ctf_trace_get_name(trace),
1912 bt_ctf_byte_order_string(byte_order));
dc3fffef 1913
bc37ae52
JG
1914end:
1915 return ret;
1916}
1917
d246b111
JG
1918struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
1919 struct bt_ctf_trace *trace)
1920{
1921 struct bt_ctf_field_type *field_type = NULL;
1922
1923 if (!trace) {
95c09b3a 1924 BT_LOGW_STR("Invalid parameter: trace is NULL.");
d246b111
JG
1925 goto end;
1926 }
1927
83509119 1928 bt_get(trace->packet_header_type);
d246b111
JG
1929 field_type = trace->packet_header_type;
1930end:
1931 return field_type;
1932}
1933
1934int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
1935 struct bt_ctf_field_type *packet_header_type)
1936{
1937 int ret = 0;
1938
95c09b3a
PP
1939 if (!trace) {
1940 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1941 ret = -1;
1942 goto end;
1943 }
1944
1945 if (trace->frozen) {
1946 BT_LOGW("Invalid parameter: trace is frozen: "
1947 "addr=%p, name=\"%s\"",
1948 trace, bt_ctf_trace_get_name(trace));
d246b111
JG
1949 ret = -1;
1950 goto end;
1951 }
1952
835b2d10
JG
1953 /* packet_header_type must be a structure. */
1954 if (packet_header_type &&
95c09b3a
PP
1955 !bt_ctf_field_type_is_structure(packet_header_type)) {
1956 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1957 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
1958 trace, bt_ctf_trace_get_name(trace),
1959 packet_header_type,
1960 bt_ctf_field_type_id_string(packet_header_type->id));
d246b111
JG
1961 ret = -1;
1962 goto end;
1963 }
1964
83509119 1965 bt_put(trace->packet_header_type);
835b2d10 1966 trace->packet_header_type = bt_get(packet_header_type);
95c09b3a
PP
1967 BT_LOGV("Set trace's packet header field type: "
1968 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
1969 trace, bt_ctf_trace_get_name(trace), packet_header_type);
d246b111
JG
1970end:
1971 return ret;
1972}
1973
8bf65fbd 1974static
544d0515 1975int64_t get_stream_class_count(void *element)
8bf65fbd
JG
1976{
1977 return bt_ctf_trace_get_stream_class_count(
1978 (struct bt_ctf_trace *) element);
1979}
1980
1981static
1982void *get_stream_class(void *element, int i)
1983{
9ac68eb1 1984 return bt_ctf_trace_get_stream_class_by_index(
8bf65fbd
JG
1985 (struct bt_ctf_trace *) element, i);
1986}
1987
1988static
d9a13d86 1989int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data)
8bf65fbd 1990{
d9a13d86 1991 return bt_ctf_stream_class_visit(object, visitor, data);
8bf65fbd
JG
1992}
1993
1994int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
d9a13d86 1995 bt_ctf_visitor visitor, void *data)
8bf65fbd
JG
1996{
1997 int ret;
d9a13d86
PP
1998 struct bt_ctf_object obj =
1999 { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE };
8bf65fbd 2000
95c09b3a
PP
2001 if (!trace) {
2002 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2003 ret = -1;
2004 goto end;
2005 }
2006
2007 if (!visitor) {
2008 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
8bf65fbd
JG
2009 ret = -1;
2010 goto end;
2011 }
2012
95c09b3a
PP
2013 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
2014 trace, bt_ctf_trace_get_name(trace));
d9a13d86 2015 ret = visitor_helper(&obj, get_stream_class_count,
8bf65fbd
JG
2016 get_stream_class, visit_stream_class, visitor, data);
2017end:
2018 return ret;
2019}
2020
2021static
d9a13d86 2022int invoke_listener(struct bt_ctf_object *object, void *data)
8bf65fbd 2023{
9b888ff3 2024 struct listener_wrapper *listener_wrapper = data;
8bf65fbd 2025
d9a13d86 2026 listener_wrapper->listener(object, listener_wrapper->data);
9b888ff3 2027 return 0;
8bf65fbd
JG
2028}
2029
95c09b3a 2030// TODO: add logging to this function once we use it internally.
50ad4244
JG
2031int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace,
2032 bt_ctf_listener_cb listener, void *listener_data)
2204c381
JG
2033{
2034 int ret = 0;
50ad4244
JG
2035 struct listener_wrapper *listener_wrapper =
2036 g_new0(struct listener_wrapper, 1);
2204c381 2037
50ad4244 2038 if (!trace || !listener || !listener_wrapper) {
2204c381
JG
2039 ret = -1;
2040 goto error;
2041 }
2042
50ad4244
JG
2043 listener_wrapper->listener = listener;
2044 listener_wrapper->data = listener_data;
8bf65fbd 2045
50ad4244 2046 /* Visit the current schema. */
9b888ff3 2047 ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper);
8bf65fbd
JG
2048 if (ret) {
2049 goto error;
2050 }
2051
50ad4244
JG
2052 /*
2053 * Add listener to the array of callbacks which will be invoked on
2054 * schema changes.
2055 */
2056 g_ptr_array_add(trace->listeners, listener_wrapper);
2204c381
JG
2057 return ret;
2058error:
50ad4244 2059 g_free(listener_wrapper);
2204c381
JG
2060 return ret;
2061}
2062
bc37ae52 2063BT_HIDDEN
d9a13d86 2064int bt_ctf_trace_object_modification(struct bt_ctf_object *object,
9b888ff3
JG
2065 void *trace_ptr)
2066{
2067 size_t i;
2068 struct bt_ctf_trace *trace = trace_ptr;
2069
2070 assert(trace);
d9a13d86 2071 assert(object);
9b888ff3
JG
2072
2073 if (trace->listeners->len == 0) {
2074 goto end;
2075 }
2076
2077 for (i = 0; i < trace->listeners->len; i++) {
2078 struct listener_wrapper *listener =
2079 g_ptr_array_index(trace->listeners, i);
2080
d9a13d86 2081 listener->listener(object, listener->data);
9b888ff3
JG
2082 }
2083end:
2084 return 0;
2085}
2086
2087BT_HIDDEN
bc37ae52
JG
2088struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
2089{
a25e0d14 2090 int ret;
bc37ae52 2091 unsigned int alignment, size;
8bdcb82e 2092 struct bt_ctf_field_type *field_type = NULL;
bc37ae52
JG
2093
2094 if (alias >= NR_FIELD_TYPE_ALIAS) {
8bdcb82e 2095 goto end;
bc37ae52
JG
2096 }
2097
2098 alignment = field_type_aliases_alignments[alias];
2099 size = field_type_aliases_sizes[alias];
2100 field_type = bt_ctf_field_type_integer_create(size);
a25e0d14
JG
2101 ret = bt_ctf_field_type_set_alignment(field_type, alignment);
2102 if (ret) {
2103 BT_PUT(field_type);
2104 }
8bdcb82e 2105end:
bc37ae52
JG
2106 return field_type;
2107}
2108
f67f3a6e 2109static
09840de5 2110void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
f67f3a6e 2111{
6474e016
PP
2112 int i;
2113
95c09b3a
PP
2114 if (trace->frozen) {
2115 return;
2116 }
2117
2118 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
2119 trace, bt_ctf_trace_get_name(trace));
2120 BT_LOGD_STR("Freezing packet header field type.");
09840de5 2121 bt_ctf_field_type_freeze(trace->packet_header_type);
95c09b3a 2122 BT_LOGD_STR("Freezing environment attributes.");
f67f3a6e 2123 bt_ctf_attributes_freeze(trace->environment);
6474e016 2124
95c09b3a
PP
2125 if (trace->clocks->len > 0) {
2126 BT_LOGD_STR("Freezing clock classes.");
2127 }
2128
6474e016 2129 for (i = 0; i < trace->clocks->len; i++) {
ac0c6bdd 2130 struct bt_ctf_clock_class *clock_class =
6474e016
PP
2131 g_ptr_array_index(trace->clocks, i);
2132
ac0c6bdd 2133 bt_ctf_clock_class_freeze(clock_class);
6474e016
PP
2134 }
2135
f67f3a6e
JG
2136 trace->frozen = 1;
2137}
2138
c55a9f58 2139bt_bool bt_ctf_trace_is_static(struct bt_ctf_trace *trace)
5acf2ae6 2140{
c55a9f58 2141 bt_bool is_static = BT_FALSE;
5acf2ae6
PP
2142
2143 if (!trace) {
95c09b3a 2144 BT_LOGW_STR("Invalid parameter: trace is NULL.");
5acf2ae6
PP
2145 goto end;
2146 }
2147
2148 is_static = trace->is_static;
2149
2150end:
2151 return is_static;
2152}
2153
2154int bt_ctf_trace_set_is_static(struct bt_ctf_trace *trace)
2155{
2156 int ret = 0;
3602afb0 2157 size_t i;
5acf2ae6
PP
2158
2159 if (!trace) {
95c09b3a 2160 BT_LOGW_STR("Invalid parameter: trace is NULL.");
5acf2ae6
PP
2161 ret = -1;
2162 goto end;
2163 }
2164
c55a9f58 2165 trace->is_static = BT_TRUE;
5acf2ae6 2166 bt_ctf_trace_freeze(trace);
95c09b3a
PP
2167 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
2168 trace, bt_ctf_trace_get_name(trace));
5acf2ae6 2169
3602afb0
PP
2170 /* Call all the "trace is static" listeners */
2171 for (i = 0; i < trace->is_static_listeners->len; i++) {
2172 struct bt_ctf_trace_is_static_listener_elem elem =
2173 g_array_index(trace->is_static_listeners,
2174 struct bt_ctf_trace_is_static_listener_elem, i);
2175
2176 if (elem.func) {
2177 elem.func(trace, elem.data);
2178 }
2179 }
2180
2181end:
2182 return ret;
2183}
2184
2185int bt_ctf_trace_add_is_static_listener(struct bt_ctf_trace *trace,
8480c8cc
PP
2186 bt_ctf_trace_is_static_listener listener,
2187 bt_ctf_trace_listener_removed listener_removed, void *data)
3602afb0
PP
2188{
2189 int i;
2190 struct bt_ctf_trace_is_static_listener_elem new_elem = {
2191 .func = listener,
8480c8cc 2192 .removed = listener_removed,
3602afb0
PP
2193 .data = data,
2194 };
2195
2196 if (!trace) {
2197 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2198 i = -1;
2199 goto end;
2200 }
2201
2202 if (!listener) {
2203 BT_LOGW_STR("Invalid parameter: listener is NULL.");
2204 i = -1;
2205 goto end;
2206 }
2207
2208 if (trace->is_static) {
2209 BT_LOGW("Invalid parameter: trace is already static: "
2210 "addr=%p, name=\"%s\"",
2211 trace, bt_ctf_trace_get_name(trace));
2212 i = -1;
2213 goto end;
2214 }
2215
8480c8cc
PP
2216 if (trace->in_remove_listener) {
2217 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2218 "addr=%p, name=\"%s\"",
2219 trace, bt_ctf_trace_get_name(trace));
2220 i = -1;
2221 goto end;
2222 }
2223
3602afb0
PP
2224 /* Find the next available spot */
2225 for (i = 0; i < trace->is_static_listeners->len; i++) {
2226 struct bt_ctf_trace_is_static_listener_elem elem =
2227 g_array_index(trace->is_static_listeners,
2228 struct bt_ctf_trace_is_static_listener_elem, i);
2229
2230 if (!elem.func) {
2231 break;
2232 }
2233 }
2234
2235 if (i == trace->is_static_listeners->len) {
2236 g_array_append_val(trace->is_static_listeners, new_elem);
2237 } else {
2238 g_array_insert_val(trace->is_static_listeners, i, new_elem);
2239 }
2240
2241 BT_LOGV("Added \"trace is static\" listener: "
2242 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
2243 "data-addr=%p, listener-id=%d",
2244 trace, bt_ctf_trace_get_name(trace), listener, data, i);
2245
2246end:
2247 return i;
2248}
2249
2250int bt_ctf_trace_remove_is_static_listener(
2251 struct bt_ctf_trace *trace, int listener_id)
2252{
2253 int ret = 0;
2254 struct bt_ctf_trace_is_static_listener_elem *elem;
2255
2256 if (!trace) {
2257 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2258 ret = -1;
2259 goto end;
2260 }
2261
8480c8cc
PP
2262 if (trace->in_remove_listener) {
2263 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2264 "addr=%p, name=\"%s\", listener-id=%d",
2265 trace, bt_ctf_trace_get_name(trace),
2266 listener_id);
2267 ret = -1;
2268 goto end;
2269 }
2270
3602afb0
PP
2271 if (listener_id < 0) {
2272 BT_LOGW("Invalid listener ID: must be zero or positive: "
2273 "listener-id=%d", listener_id);
2274 ret = -1;
2275 goto end;
2276 }
2277
2278 if (listener_id >= trace->is_static_listeners->len) {
2279 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2280 "addr=%p, name=\"%s\", listener-id=%d",
2281 trace, bt_ctf_trace_get_name(trace),
2282 listener_id);
2283 ret = -1;
2284 goto end;
2285 }
2286
2287 elem = &g_array_index(trace->is_static_listeners,
2288 struct bt_ctf_trace_is_static_listener_elem,
2289 listener_id);
2290 if (!elem->func) {
2291 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2292 "addr=%p, name=\"%s\", listener-id=%d",
2293 trace, bt_ctf_trace_get_name(trace),
2294 listener_id);
2295 ret = -1;
2296 goto end;
2297 }
2298
8480c8cc
PP
2299 if (elem->removed) {
2300 /* Call remove listener */
2301 BT_LOGV("Calling remove listener: "
2302 "trace-addr=%p, trace-name=\"%s\", "
2303 "listener-id=%d", trace, bt_ctf_trace_get_name(trace),
2304 listener_id);
2305 trace->in_remove_listener = BT_TRUE;
2306 elem->removed(trace, elem->data);
2307 trace->in_remove_listener = BT_FALSE;
2308 }
2309
3602afb0 2310 elem->func = NULL;
8480c8cc 2311 elem->removed = NULL;
3602afb0
PP
2312 elem->data = NULL;
2313 BT_LOGV("Removed \"trace is static\" listener: "
2314 "trace-addr=%p, trace-name=\"%s\", "
2315 "listener-id=%d", trace, bt_ctf_trace_get_name(trace),
2316 listener_id);
2317
5acf2ae6
PP
2318end:
2319 return ret;
2320}
This page took 0.149906 seconds and 4 git commands to generate.