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