Rename bt_ctf_X -> bt_X, maintain backward compat. for pre-2.0 CTF writer
[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 60struct listener_wrapper {
50842bdc 61 bt_listener_cb listener;
2204c381
JG
62 void *data;
63};
64
50842bdc
PP
65struct bt_trace_is_static_listener_elem {
66 bt_trace_is_static_listener func;
67 bt_trace_listener_removed removed;
3602afb0
PP
68 void *data;
69};
70
bc37ae52 71static
50842bdc 72void bt_trace_destroy(struct bt_object *obj);
bc37ae52 73static
50842bdc 74void bt_trace_freeze(struct bt_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
50842bdc 94struct bt_trace *bt_trace_create(void)
bc37ae52 95{
50842bdc 96 struct bt_trace *trace = NULL;
bc37ae52 97
50842bdc 98 trace = g_new0(struct bt_trace, 1);
bc37ae52 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.");
50842bdc
PP
105 trace->native_byte_order = BT_BYTE_ORDER_UNSPECIFIED;
106 bt_object_init(trace, bt_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 128 /* Create the environment array object */
50842bdc 129 trace->environment = bt_attributes_create();
7f800dc7 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 142 trace->is_static_listeners = g_array_new(FALSE, TRUE,
50842bdc 143 sizeof(struct bt_trace_is_static_listener_elem));
3602afb0
PP
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
50842bdc 157const char *bt_trace_get_name(struct bt_trace *trace)
e96045d4
JG
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
50842bdc 175int bt_trace_set_name(struct bt_trace *trace, const char *name)
e96045d4
JG
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\"",
50842bdc 194 trace, bt_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
50842bdc 213const unsigned char *bt_trace_get_uuid(struct bt_trace *trace)
4a32fda0
PP
214{
215 return trace && trace->uuid_set ? trace->uuid : NULL;
216}
217
50842bdc 218int bt_trace_set_uuid(struct bt_trace *trace, const unsigned char *uuid)
4a32fda0
PP
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\"",
50842bdc 237 trace, bt_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\"",
50842bdc 246 trace, bt_trace_get_name(trace),
95c09b3a
PP
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
50842bdc 268void bt_trace_destroy(struct bt_object *obj)
bc37ae52 269{
50842bdc 270 struct bt_trace *trace;
bc37ae52 271
50842bdc 272 trace = container_of(obj, struct bt_trace, base);
95c09b3a
PP
273
274 BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
50842bdc 275 trace, bt_trace_get_name(trace));
95c09b3a 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++) {
50842bdc 285 struct bt_trace_is_static_listener_elem elem =
8480c8cc 286 g_array_index(trace->is_static_listeners,
50842bdc 287 struct bt_trace_is_static_listener_elem, i);
8480c8cc
PP
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.");
50842bdc 303 bt_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
50842bdc 330int bt_trace_set_environment_field(struct bt_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
50842bdc 353 if (bt_identifier_is_valid(name)) {
95c09b3a
PP
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\"",
50842bdc 357 trace, bt_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",
50842bdc 366 trace, bt_trace_get_name(trace), name,
95c09b3a
PP
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\"",
50842bdc 375 trace, bt_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 =
50842bdc 388 bt_attributes_get_field_value_by_name(
a0d12916
JG
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\"",
50842bdc 395 trace, bt_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
50842bdc 404 ret = bt_attributes_set_field_value(trace->environment, name,
7f800dc7 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\"",
50842bdc 410 trace, bt_trace_get_name(trace), name);
95c09b3a
PP
411 } else {
412 BT_LOGV("Set environment field's value: "
413 "trace-addr=%p, trace-name=\"%s\", "
414 "env-name=\"%s\", value-addr=%p",
50842bdc 415 trace, bt_trace_get_name(trace), name, value);
95c09b3a 416 }
bc37ae52 417
7f800dc7
PP
418end:
419 return ret;
420}
bc37ae52 421
50842bdc 422int bt_trace_set_environment_field_string(struct bt_trace *trace,
7f800dc7
PP
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
50842bdc
PP
441 /* bt_trace_set_environment_field() logs errors */
442 ret = bt_trace_set_environment_field(trace, name,
7f800dc7
PP
443 env_value_string_obj);
444
445end:
95c09b3a 446 bt_put(env_value_string_obj);
3487c9f3
JG
447 return ret;
448}
449
50842bdc 450int bt_trace_set_environment_field_integer(struct bt_trace *trace,
7f800dc7 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
50842bdc
PP
463 /* bt_trace_set_environment_field() logs errors */
464 ret = bt_trace_set_environment_field(trace, name,
7f800dc7 465 env_value_integer_obj);
95c09b3a 466
7f800dc7 467end:
95c09b3a 468 bt_put(env_value_integer_obj);
bc37ae52
JG
469 return ret;
470}
471
50842bdc 472int64_t bt_trace_get_environment_field_count(struct bt_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
50842bdc 482 ret = bt_attributes_get_count(trace->environment);
95c09b3a 483 assert(ret >= 0);
e6fa2160 484
e6fa2160 485end:
7f800dc7 486 return ret;
e6fa2160
JG
487}
488
489const char *
50842bdc 490bt_trace_get_environment_field_name_by_index(struct bt_trace *trace,
9ac68eb1 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
50842bdc 500 ret = bt_attributes_get_field_name(trace->environment, index);
7f800dc7 501
e6fa2160
JG
502end:
503 return ret;
504}
505
50842bdc
PP
506struct bt_value *bt_trace_get_environment_field_value_by_index(
507 struct bt_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
50842bdc 516 ret = bt_attributes_get_field_value(trace->environment, index);
7f800dc7 517
e6fa2160
JG
518end:
519 return ret;
520}
521
50842bdc
PP
522struct bt_value *bt_trace_get_environment_field_value_by_name(
523 struct bt_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
50842bdc 537 ret = bt_attributes_get_field_value_by_name(trace->environment,
7f800dc7
PP
538 name);
539
e6fa2160
JG
540end:
541 return ret;
542}
543
50842bdc
PP
544int bt_trace_add_clock_class(struct bt_trace *trace,
545 struct bt_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\"",
50842bdc 558 trace, bt_trace_get_name(trace));
95c09b3a
PP
559 ret = -1;
560 goto end;
561 }
562
50842bdc 563 if (!bt_clock_class_is_valid(clock_class)) {
95c09b3a
PP
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\"",
50842bdc
PP
567 trace, bt_trace_get_name(trace),
568 clock_class, bt_clock_class_get_name(clock_class));
bc37ae52
JG
569 ret = -1;
570 goto end;
571 }
572
ac0c6bdd 573 /* Check for duplicate clock classes */
50842bdc 574 if (bt_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\"",
50842bdc
PP
578 trace, bt_trace_get_name(trace),
579 clock_class, bt_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.");
50842bdc 589 bt_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\"",
50842bdc
PP
595 trace, bt_trace_get_name(trace),
596 clock_class, bt_clock_class_get_name(clock_class));
95c09b3a 597
bc37ae52
JG
598end:
599 return ret;
600}
601
50842bdc 602int64_t bt_trace_get_clock_class_count(struct bt_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
50842bdc
PP
616struct bt_clock_class *bt_trace_get_clock_class_by_index(
617 struct bt_trace *trace, uint64_t index)
884cd6c3 618{
50842bdc 619 struct bt_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",
50842bdc 630 trace, bt_trace_get_name(trace),
95c09b3a 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 641static
50842bdc
PP
642bool packet_header_field_type_is_valid(struct bt_trace *trace,
643 struct bt_field_type *packet_header_type)
e011d2c1
PP
644{
645 int ret;
646 bool is_valid = true;
50842bdc 647 struct bt_field_type *field_type = NULL;
e011d2c1
PP
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 */
50842bdc 667 if (!bt_field_type_is_structure(packet_header_type)) {
e011d2c1
PP
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,
50842bdc 671 bt_field_type_id_string(packet_header_type->id));
e011d2c1
PP
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 */
50842bdc 680 field_type = bt_field_type_structure_get_field_type_by_name(
e011d2c1
PP
681 packet_header_type, "magic");
682 if (field_type) {
683 const char *field_name;
684
50842bdc 685 if (!bt_field_type_is_integer(field_type)) {
e011d2c1
PP
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,
50842bdc 689 bt_field_type_id_string(field_type->id));
e011d2c1
PP
690 goto invalid;
691 }
692
50842bdc 693 if (bt_field_type_integer_is_signed(field_type)) {
e011d2c1
PP
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
50842bdc 699 if (bt_field_type_integer_get_size(field_type) != 32) {
e011d2c1
PP
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,
50842bdc 703 bt_field_type_integer_get_size(field_type));
e011d2c1
PP
704 goto invalid;
705 }
706
50842bdc 707 ret = bt_field_type_structure_get_field_by_index(
e011d2c1
PP
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 */
50842bdc 725 field_type = bt_field_type_structure_get_field_type_by_name(
e011d2c1
PP
726 packet_header_type, "uuid");
727 if (field_type) {
50842bdc 728 struct bt_field_type *elem_ft;
e011d2c1 729
50842bdc 730 if (!bt_field_type_is_array(field_type)) {
e011d2c1
PP
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,
50842bdc 734 bt_field_type_id_string(field_type->id));
e011d2c1
PP
735 goto invalid;
736 }
737
50842bdc 738 if (bt_field_type_array_get_length(field_type) != 16) {
e011d2c1
PP
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,
50842bdc 742 bt_field_type_array_get_length(field_type));
e011d2c1
PP
743 goto invalid;
744 }
745
50842bdc 746 elem_ft = bt_field_type_array_get_element_type(field_type);
e011d2c1
PP
747 assert(elem_ft);
748
50842bdc 749 if (!bt_field_type_is_integer(elem_ft)) {
e011d2c1
PP
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,
50842bdc 753 bt_field_type_id_string(elem_ft->id));
e011d2c1
PP
754 bt_put(elem_ft);
755 goto invalid;
756 }
757
50842bdc 758 if (bt_field_type_integer_is_signed(elem_ft)) {
e011d2c1
PP
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
50842bdc 765 if (bt_field_type_integer_get_size(elem_ft) != 8) {
e011d2c1
PP
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,
50842bdc 769 bt_field_type_integer_get_size(elem_ft));
e011d2c1
PP
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 */
50842bdc 782 field_type = bt_field_type_structure_get_field_type_by_name(
e011d2c1
PP
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) {
50842bdc 796 if (!bt_field_type_is_integer(field_type)) {
e011d2c1
PP
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,
50842bdc 800 bt_field_type_id_string(field_type->id));
e011d2c1
PP
801 goto invalid;
802 }
803
50842bdc 804 if (bt_field_type_integer_is_signed(field_type)) {
e011d2c1
PP
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 */
50842bdc 817 field_type = bt_field_type_structure_get_field_type_by_name(
708ab211
PP
818 packet_header_type, "packet_seq_num");
819 if (field_type) {
50842bdc 820 if (!bt_field_type_is_integer(field_type)) {
708ab211
PP
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,
50842bdc 824 bt_field_type_id_string(field_type->id));
708ab211
PP
825 goto invalid;
826 }
827
50842bdc 828 if (bt_field_type_integer_is_signed(field_type)) {
708ab211
PP
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
50842bdc
PP
848bool packet_context_field_type_is_valid(struct bt_trace *trace,
849 struct bt_stream_class *stream_class,
850 struct bt_field_type *packet_context_type)
e011d2c1
PP
851{
852 bool is_valid = true;
50842bdc 853 struct bt_field_type *field_type = NULL;
e011d2c1
PP
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 */
50842bdc 861 if (!bt_field_type_is_structure(packet_context_type)) {
e011d2c1
PP
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,
50842bdc 865 bt_field_type_id_string(packet_context_type->id));
e011d2c1
PP
866 goto invalid;
867 }
868
869 /*
870 * If there's a `packet_size` field, it must be an unsigned
871 * integer field type.
872 */
50842bdc 873 field_type = bt_field_type_structure_get_field_type_by_name(
e011d2c1
PP
874 packet_context_type, "packet_size");
875 if (field_type) {
50842bdc 876 if (!bt_field_type_is_integer(field_type)) {
e011d2c1
PP
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,
50842bdc 880 bt_field_type_id_string(field_type->id));
e011d2c1
PP
881 goto invalid;
882 }
883
50842bdc 884 if (bt_field_type_integer_is_signed(field_type)) {
e011d2c1
PP
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 */
50842bdc 897 field_type = bt_field_type_structure_get_field_type_by_name(
e011d2c1
PP
898 packet_context_type, "content_size");
899 if (field_type) {
50842bdc 900 if (!bt_field_type_is_integer(field_type)) {
e011d2c1
PP
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,
50842bdc 904 bt_field_type_id_string(field_type->id));
e011d2c1
PP
905 goto invalid;
906 }
907
50842bdc 908 if (bt_field_type_integer_is_signed(field_type)) {
e011d2c1
PP
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 */
50842bdc 921 field_type = bt_field_type_structure_get_field_type_by_name(
e011d2c1
PP
922 packet_context_type, "events_discarded");
923 if (field_type) {
50842bdc 924 if (!bt_field_type_is_integer(field_type)) {
e011d2c1
PP
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,
50842bdc 928 bt_field_type_id_string(field_type->id));
e011d2c1
PP
929 goto invalid;
930 }
931
50842bdc 932 if (bt_field_type_integer_is_signed(field_type)) {
e011d2c1
PP
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 */
50842bdc 947 field_type = bt_field_type_structure_get_field_type_by_name(
e011d2c1
PP
948 packet_context_type, "timestamp_begin");
949 if (field_type) {
50842bdc 950 if (!bt_field_type_is_integer(field_type)) {
e011d2c1
PP
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,
50842bdc 954 bt_field_type_id_string(field_type->id));
e011d2c1
PP
955 goto invalid;
956 }
957
50842bdc 958 if (bt_field_type_integer_is_signed(field_type)) {
e011d2c1
PP
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) {
50842bdc
PP
965 struct bt_clock_class *clock_class =
966 bt_field_type_integer_get_mapped_clock_class(
e011d2c1
PP
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 */
50842bdc 986 field_type = bt_field_type_structure_get_field_type_by_name(
e011d2c1
PP
987 packet_context_type, "timestamp_end");
988 if (field_type) {
50842bdc 989 if (!bt_field_type_is_integer(field_type)) {
e011d2c1
PP
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,
50842bdc 993 bt_field_type_id_string(field_type->id));
e011d2c1
PP
994 goto invalid;
995 }
996
50842bdc 997 if (bt_field_type_integer_is_signed(field_type)) {
e011d2c1
PP
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) {
50842bdc
PP
1004 struct bt_clock_class *clock_class =
1005 bt_field_type_integer_get_mapped_clock_class(
e011d2c1
PP
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
50842bdc
PP
1030bool event_header_field_type_is_valid(struct bt_trace *trace,
1031 struct bt_stream_class *stream_class,
1032 struct bt_field_type *event_header_type)
e011d2c1
PP
1033{
1034 bool is_valid = true;
50842bdc 1035 struct bt_field_type *field_type = NULL;
e011d2c1
PP
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 */
50842bdc 1048 if (bt_stream_class_get_event_class_count(stream_class) > 1) {
e011d2c1
PP
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 */
50842bdc 1059 if (!bt_field_type_is_structure(event_header_type)) {
e011d2c1
PP
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,
50842bdc 1063 bt_field_type_id_string(event_header_type->id));
e011d2c1
PP
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 */
50842bdc 1072 field_type = bt_field_type_structure_get_field_type_by_name(
e011d2c1
PP
1073 event_header_type, "id");
1074 if (field_type) {
50842bdc 1075 struct bt_field_type *int_ft;
e011d2c1 1076
50842bdc 1077 if (bt_field_type_is_integer(field_type)) {
e011d2c1 1078 int_ft = bt_get(field_type);
50842bdc
PP
1079 } else if (bt_field_type_is_enumeration(field_type)) {
1080 int_ft = bt_field_type_enumeration_get_container_type(
e011d2c1
PP
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,
50842bdc 1086 bt_field_type_id_string(field_type->id));
e011d2c1
PP
1087 goto invalid;
1088 }
1089
1090 assert(int_ft);
50842bdc 1091 if (bt_field_type_integer_is_signed(int_ft)) {
e011d2c1
PP
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
50842bdc
PP
1111int bt_trace_add_stream_class(struct bt_trace *trace,
1112 struct bt_stream_class *stream_class)
ef0c4a15 1113{
544d0515
PP
1114 int ret;
1115 int64_t i;
ef0c4a15 1116 int64_t stream_id;
50842bdc
PP
1117 struct bt_validation_output trace_sc_validation_output = { 0 };
1118 struct bt_validation_output *ec_validation_outputs = NULL;
1119 const enum bt_validation_flag trace_sc_validation_flags =
1120 BT_VALIDATION_FLAG_TRACE |
1121 BT_VALIDATION_FLAG_STREAM;
1122 const enum bt_validation_flag ec_validation_flags =
1123 BT_VALIDATION_FLAG_EVENT;
1124 struct bt_field_type *packet_header_type = NULL;
1125 struct bt_field_type *packet_context_type = NULL;
1126 struct bt_field_type *event_header_type = NULL;
1127 struct bt_field_type *stream_event_ctx_type = NULL;
544d0515 1128 int64_t event_class_count;
50842bdc 1129 struct bt_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,
50842bdc
PP
1153 trace, bt_trace_get_name(trace),
1154 stream_class, bt_stream_class_get_name(stream_class),
1155 bt_stream_class_get_id(stream_class));
95c09b3a 1156
50842bdc 1157 current_parent_trace = bt_stream_class_get_trace(stream_class);
2789e5d9
JG
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,
50842bdc 1164 bt_trace_get_name(current_parent_trace));
2789e5d9
JG
1165 ret = -1;
1166 goto end;
1167 }
1168
09840de5 1169 event_class_count =
50842bdc 1170 bt_stream_class_get_event_class_count(stream_class);
09840de5
PP
1171 assert(event_class_count >= 0);
1172
fe13b5c0 1173 if (stream_class->clock) {
50842bdc 1174 struct bt_clock_class *stream_clock_class =
ac0c6bdd
PP
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,
50842bdc 1198 bt_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,
50842bdc 1213 bt_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 =
50842bdc 1231 bt_trace_get_packet_header_type(trace);
09840de5 1232 packet_context_type =
50842bdc 1233 bt_stream_class_get_packet_context_type(stream_class);
09840de5 1234 event_header_type =
50842bdc 1235 bt_stream_class_get_event_header_type(stream_class);
09840de5 1236 stream_event_ctx_type =
50842bdc 1237 bt_stream_class_get_event_context_type(stream_class);
95c09b3a
PP
1238
1239 BT_LOGD("Validating trace and stream class field types.");
50842bdc 1240 ret = bt_validate_class_types(trace->environment,
09840de5
PP
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) {
50842bdc 1272 ec_validation_outputs = g_new0(struct bt_validation_output,
09840de5
PP
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++) {
50842bdc
PP
1283 struct bt_event_class *event_class =
1284 bt_stream_class_get_event_class_by_index(
9ac68eb1 1285 stream_class, i);
50842bdc
PP
1286 struct bt_field_type *event_context_type = NULL;
1287 struct bt_field_type *event_payload_type = NULL;
09840de5
PP
1288
1289 event_context_type =
50842bdc 1290 bt_event_class_get_context_type(event_class);
09840de5 1291 event_payload_type =
50842bdc 1292 bt_event_class_get_payload_type(event_class);
09840de5
PP
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,
50842bdc
PP
1301 event_class, bt_event_class_get_name(event_class),
1302 bt_event_class_get_id(event_class));
1303 ret = bt_validate_class_types(trace->environment,
09840de5
PP
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
50842bdc 1332 stream_id = bt_stream_class_get_id(stream_class);
ef0c4a15
JG
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++) {
50842bdc 1343 if (stream_id == bt_stream_class_get_id(
ef0c4a15
JG
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
50842bdc 1353 if (bt_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) {
50842bdc 1401 if (bt_stream_class_map_clock_class(stream_class,
e011d2c1
PP
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 */
50842bdc 1421 bt_validation_replace_types(trace, stream_class, NULL,
09840de5
PP
1422 &trace_sc_validation_output, trace_sc_validation_flags);
1423 trace->valid = 1;
1424 stream_class->valid = 1;
1425
1426 /*
50842bdc 1427 * Put what was not moved in bt_validation_replace_types().
09840de5 1428 */
50842bdc 1429 bt_validation_output_put_types(&trace_sc_validation_output);
09840de5 1430
6474e016 1431 for (i = 0; i < event_class_count; i++) {
50842bdc
PP
1432 struct bt_event_class *event_class =
1433 bt_stream_class_get_event_class_by_index(
9ac68eb1 1434 stream_class, i);
09840de5 1435
50842bdc 1436 bt_validation_replace_types(NULL, NULL, event_class,
09840de5
PP
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
50842bdc 1443 * bt_validation_replace_types().
09840de5 1444 */
50842bdc 1445 bt_validation_output_put_types(&ec_validation_outputs[i]);
09840de5
PP
1446 }
1447
09840de5
PP
1448 /*
1449 * Freeze the trace and the stream class.
1450 */
50842bdc
PP
1451 bt_stream_class_freeze(stream_class);
1452 bt_trace_freeze(trace);
09840de5 1453
9b888ff3 1454 /* Notifiy listeners of the trace's schema modification. */
50842bdc
PP
1455 bt_stream_class_visit(stream_class,
1456 bt_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,
50842bdc
PP
1461 trace, bt_trace_get_name(trace),
1462 stream_class, bt_stream_class_get_name(stream_class),
1463 bt_stream_class_get_id(stream_class));
95c09b3a 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++) {
50842bdc 1471 bt_validation_output_put_types(
09840de5
PP
1472 &ec_validation_outputs[i]);
1473 }
1474 }
d3814b54 1475 }
09840de5
PP
1476
1477 g_free(ec_validation_outputs);
50842bdc 1478 bt_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
50842bdc 1487int64_t bt_trace_get_stream_count(struct bt_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
50842bdc
PP
1503struct bt_stream *bt_trace_get_stream_by_index(
1504 struct bt_trace *trace,
9ac68eb1 1505 uint64_t index)
c1e730fe 1506{
50842bdc 1507 struct bt_stream *stream = NULL;
c1e730fe 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",
50842bdc 1518 trace, bt_trace_get_name(trace),
95c09b3a 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
50842bdc 1529int64_t bt_trace_get_stream_class_count(struct bt_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
50842bdc
PP
1544struct bt_stream_class *bt_trace_get_stream_class_by_index(
1545 struct bt_trace *trace, uint64_t index)
ef0c4a15 1546{
50842bdc 1547 struct bt_stream_class *stream_class = NULL;
ef0c4a15 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",
50842bdc 1558 trace, bt_trace_get_name(trace),
95c09b3a 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
50842bdc
PP
1569struct bt_stream_class *bt_trace_get_stream_class_by_id(
1570 struct bt_trace *trace, uint64_t id_param)
4841ccc1
PP
1571{
1572 int i;
50842bdc 1573 struct bt_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,
50842bdc 1584 trace, bt_trace_get_name(trace), id_param);
4841ccc1
PP
1585 goto end;
1586 }
1587
6474e016 1588 for (i = 0; i < trace->stream_classes->len; i++) {
50842bdc 1589 struct bt_stream_class *stream_class_candidate;
4841ccc1
PP
1590
1591 stream_class_candidate =
1592 g_ptr_array_index(trace->stream_classes, i);
1593
50842bdc 1594 if (bt_stream_class_get_id(stream_class_candidate) ==
4841ccc1
PP
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
50842bdc
PP
1606struct bt_clock_class *bt_trace_get_clock_class_by_name(
1607 struct bt_trace *trace, const char *name)
7e4347a3
PP
1608{
1609 size_t i;
50842bdc 1610 struct bt_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++) {
50842bdc 1623 struct bt_clock_class *cur_clk =
7e4347a3 1624 g_ptr_array_index(trace->clocks, i);
50842bdc 1625 const char *cur_clk_name = bt_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
50842bdc
PP
1643bt_bool bt_trace_has_clock_class(struct bt_trace *trace,
1644 struct bt_clock_class *clock_class)
c9d90a34
PP
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
50842bdc 1656const char *get_byte_order_string(enum bt_byte_order byte_order)
bc37ae52
JG
1657{
1658 const char *string;
1659
1660 switch (byte_order) {
50842bdc 1661 case BT_BYTE_ORDER_LITTLE_ENDIAN:
bc37ae52
JG
1662 string = "le";
1663 break;
50842bdc 1664 case BT_BYTE_ORDER_BIG_ENDIAN:
bc37ae52
JG
1665 string = "be";
1666 break;
50842bdc 1667 case BT_BYTE_ORDER_NATIVE:
dc3fffef 1668 string = "native";
bc37ae52 1669 break;
dc3fffef 1670 default:
0fbb9a9f 1671 abort();
bc37ae52
JG
1672 }
1673
1674 return string;
1675}
1676
1677static
50842bdc 1678int append_trace_metadata(struct bt_trace *trace,
bc37ae52
JG
1679 struct metadata_context *context)
1680{
1681 unsigned char *uuid = trace->uuid;
e011d2c1 1682 int ret = 0;
bc37ae52 1683
50842bdc
PP
1684 if (trace->native_byte_order == BT_BYTE_ORDER_NATIVE ||
1685 trace->native_byte_order == BT_BYTE_ORDER_UNSPECIFIED) {
1686 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_BYTE_ORDER_NATIVE or BT_BYTE_ORDER_UNSPECIFIED at this point; "
1687 "set it with bt_trace_set_native_byte_order(): "
95c09b3a 1688 "addr=%p, name=\"%s\"",
50842bdc 1689 trace, bt_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");
50842bdc
PP
1697 assert(trace->native_byte_order == BT_BYTE_ORDER_LITTLE_ENDIAN ||
1698 trace->native_byte_order == BT_BYTE_ORDER_BIG_ENDIAN ||
1699 trace->native_byte_order == BT_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.");
50842bdc 1718 ret = bt_field_type_serialize(trace->packet_header_type,
e011d2c1
PP
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 1731static
50842bdc 1732void append_env_metadata(struct bt_trace *trace,
bc37ae52
JG
1733 struct metadata_context *context)
1734{
544d0515
PP
1735 int64_t i;
1736 int64_t env_size;
7f800dc7 1737
50842bdc 1738 env_size = bt_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 1748
50842bdc 1749 entry_name = bt_attributes_get_field_name(
7f800dc7 1750 trace->environment, i);
50842bdc 1751 env_field_value_obj = bt_attributes_get_field_value(
7f800dc7
PP
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
50842bdc 1803char *bt_trace_get_metadata_string(struct bt_trace *trace)
bc37ae52
JG
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,
50842bdc 1830 (GFunc)bt_clock_class_serialize, context);
bc37ae52
JG
1831
1832 for (i = 0; i < trace->stream_classes->len; i++) {
50842bdc
PP
1833 /* bt_stream_class_serialize() logs details */
1834 err = bt_stream_class_serialize(
bc37ae52
JG
1835 trace->stream_classes->pdata[i], context);
1836 if (err) {
50842bdc 1837 /* bt_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
50842bdc
PP
1853enum bt_byte_order bt_trace_get_native_byte_order(
1854 struct bt_trace *trace)
4ed90fb3 1855{
50842bdc 1856 enum bt_byte_order ret = BT_BYTE_ORDER_UNKNOWN;
4ed90fb3
JG
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
50842bdc
PP
1869int bt_trace_set_native_byte_order(struct bt_trace *trace,
1870 enum bt_byte_order byte_order)
bc37ae52
JG
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\"",
50842bdc 1883 trace, bt_trace_get_name(trace));
bc37ae52
JG
1884 ret = -1;
1885 goto end;
1886 }
1887
d41cff38 1888 if (trace->is_created_by_writer &&
50842bdc
PP
1889 byte_order == BT_BYTE_ORDER_UNSPECIFIED) {
1890 BT_LOGW("Invalid parameter: BT_BYTE_ORDER_UNSPECIFIED byte order is not allowed for a CTF writer trace: "
d41cff38 1891 "addr=%p, name=\"%s\"",
50842bdc 1892 trace, bt_trace_get_name(trace));
d41cff38
PP
1893 ret = -1;
1894 goto end;
1895 }
1896
50842bdc
PP
1897 if (byte_order != BT_BYTE_ORDER_LITTLE_ENDIAN &&
1898 byte_order != BT_BYTE_ORDER_BIG_ENDIAN &&
1899 byte_order != BT_BYTE_ORDER_NETWORK) {
95c09b3a
PP
1900 BT_LOGW("Invalid parameter: invalid byte order: "
1901 "addr=%p, name=\"%s\", bo=%s",
50842bdc
PP
1902 trace, bt_trace_get_name(trace),
1903 bt_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",
50842bdc
PP
1911 trace, bt_trace_get_name(trace),
1912 bt_byte_order_string(byte_order));
dc3fffef 1913
bc37ae52
JG
1914end:
1915 return ret;
1916}
1917
50842bdc
PP
1918struct bt_field_type *bt_trace_get_packet_header_type(
1919 struct bt_trace *trace)
d246b111 1920{
50842bdc 1921 struct bt_field_type *field_type = NULL;
d246b111
JG
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
50842bdc
PP
1934int bt_trace_set_packet_header_type(struct bt_trace *trace,
1935 struct bt_field_type *packet_header_type)
d246b111
JG
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\"",
50842bdc 1948 trace, bt_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 &&
50842bdc 1955 !bt_field_type_is_structure(packet_header_type)) {
95c09b3a
PP
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",
50842bdc 1958 trace, bt_trace_get_name(trace),
95c09b3a 1959 packet_header_type,
50842bdc 1960 bt_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",
50842bdc 1969 trace, bt_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 1976{
50842bdc
PP
1977 return bt_trace_get_stream_class_count(
1978 (struct bt_trace *) element);
8bf65fbd
JG
1979}
1980
1981static
1982void *get_stream_class(void *element, int i)
1983{
50842bdc
PP
1984 return bt_trace_get_stream_class_by_index(
1985 (struct bt_trace *) element, i);
8bf65fbd
JG
1986}
1987
1988static
50842bdc 1989int visit_stream_class(void *object, bt_visitor visitor,void *data)
8bf65fbd 1990{
50842bdc 1991 return bt_stream_class_visit(object, visitor, data);
8bf65fbd
JG
1992}
1993
50842bdc
PP
1994int bt_trace_visit(struct bt_trace *trace,
1995 bt_visitor visitor, void *data)
8bf65fbd
JG
1996{
1997 int ret;
50842bdc
PP
1998 struct bt_visitor_object obj = {
1999 .object = trace,
2000 .type = BT_VISITOR_OBJECT_TYPE_TRACE
2001 };
8bf65fbd 2002
95c09b3a
PP
2003 if (!trace) {
2004 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2005 ret = -1;
2006 goto end;
2007 }
2008
2009 if (!visitor) {
2010 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
8bf65fbd
JG
2011 ret = -1;
2012 goto end;
2013 }
2014
95c09b3a 2015 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
50842bdc 2016 trace, bt_trace_get_name(trace));
d9a13d86 2017 ret = visitor_helper(&obj, get_stream_class_count,
8bf65fbd
JG
2018 get_stream_class, visit_stream_class, visitor, data);
2019end:
2020 return ret;
2021}
2022
2023static
50842bdc 2024int invoke_listener(struct bt_visitor_object *object, void *data)
8bf65fbd 2025{
9b888ff3 2026 struct listener_wrapper *listener_wrapper = data;
8bf65fbd 2027
d9a13d86 2028 listener_wrapper->listener(object, listener_wrapper->data);
9b888ff3 2029 return 0;
8bf65fbd
JG
2030}
2031
95c09b3a 2032// TODO: add logging to this function once we use it internally.
50842bdc
PP
2033int bt_trace_add_listener(struct bt_trace *trace,
2034 bt_listener_cb listener, void *listener_data)
2204c381
JG
2035{
2036 int ret = 0;
50ad4244
JG
2037 struct listener_wrapper *listener_wrapper =
2038 g_new0(struct listener_wrapper, 1);
2204c381 2039
50ad4244 2040 if (!trace || !listener || !listener_wrapper) {
2204c381
JG
2041 ret = -1;
2042 goto error;
2043 }
2044
50ad4244
JG
2045 listener_wrapper->listener = listener;
2046 listener_wrapper->data = listener_data;
8bf65fbd 2047
50ad4244 2048 /* Visit the current schema. */
50842bdc 2049 ret = bt_trace_visit(trace, invoke_listener, listener_wrapper);
8bf65fbd
JG
2050 if (ret) {
2051 goto error;
2052 }
2053
50ad4244
JG
2054 /*
2055 * Add listener to the array of callbacks which will be invoked on
2056 * schema changes.
2057 */
2058 g_ptr_array_add(trace->listeners, listener_wrapper);
2204c381
JG
2059 return ret;
2060error:
50ad4244 2061 g_free(listener_wrapper);
2204c381
JG
2062 return ret;
2063}
2064
bc37ae52 2065BT_HIDDEN
50842bdc 2066int bt_trace_object_modification(struct bt_visitor_object *object,
9b888ff3
JG
2067 void *trace_ptr)
2068{
2069 size_t i;
50842bdc 2070 struct bt_trace *trace = trace_ptr;
9b888ff3
JG
2071
2072 assert(trace);
d9a13d86 2073 assert(object);
9b888ff3
JG
2074
2075 if (trace->listeners->len == 0) {
2076 goto end;
2077 }
2078
2079 for (i = 0; i < trace->listeners->len; i++) {
2080 struct listener_wrapper *listener =
2081 g_ptr_array_index(trace->listeners, i);
2082
d9a13d86 2083 listener->listener(object, listener->data);
9b888ff3
JG
2084 }
2085end:
2086 return 0;
2087}
2088
2089BT_HIDDEN
50842bdc 2090struct bt_field_type *get_field_type(enum field_type_alias alias)
bc37ae52 2091{
a25e0d14 2092 int ret;
bc37ae52 2093 unsigned int alignment, size;
50842bdc 2094 struct bt_field_type *field_type = NULL;
bc37ae52
JG
2095
2096 if (alias >= NR_FIELD_TYPE_ALIAS) {
8bdcb82e 2097 goto end;
bc37ae52
JG
2098 }
2099
2100 alignment = field_type_aliases_alignments[alias];
2101 size = field_type_aliases_sizes[alias];
50842bdc
PP
2102 field_type = bt_field_type_integer_create(size);
2103 ret = bt_field_type_set_alignment(field_type, alignment);
a25e0d14
JG
2104 if (ret) {
2105 BT_PUT(field_type);
2106 }
8bdcb82e 2107end:
bc37ae52
JG
2108 return field_type;
2109}
2110
f67f3a6e 2111static
50842bdc 2112void bt_trace_freeze(struct bt_trace *trace)
f67f3a6e 2113{
6474e016
PP
2114 int i;
2115
95c09b3a
PP
2116 if (trace->frozen) {
2117 return;
2118 }
2119
2120 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
50842bdc 2121 trace, bt_trace_get_name(trace));
95c09b3a 2122 BT_LOGD_STR("Freezing packet header field type.");
50842bdc 2123 bt_field_type_freeze(trace->packet_header_type);
95c09b3a 2124 BT_LOGD_STR("Freezing environment attributes.");
50842bdc 2125 bt_attributes_freeze(trace->environment);
6474e016 2126
95c09b3a
PP
2127 if (trace->clocks->len > 0) {
2128 BT_LOGD_STR("Freezing clock classes.");
2129 }
2130
6474e016 2131 for (i = 0; i < trace->clocks->len; i++) {
50842bdc 2132 struct bt_clock_class *clock_class =
6474e016
PP
2133 g_ptr_array_index(trace->clocks, i);
2134
50842bdc 2135 bt_clock_class_freeze(clock_class);
6474e016
PP
2136 }
2137
f67f3a6e
JG
2138 trace->frozen = 1;
2139}
2140
50842bdc 2141bt_bool bt_trace_is_static(struct bt_trace *trace)
5acf2ae6 2142{
c55a9f58 2143 bt_bool is_static = BT_FALSE;
5acf2ae6
PP
2144
2145 if (!trace) {
95c09b3a 2146 BT_LOGW_STR("Invalid parameter: trace is NULL.");
5acf2ae6
PP
2147 goto end;
2148 }
2149
2150 is_static = trace->is_static;
2151
2152end:
2153 return is_static;
2154}
2155
50842bdc 2156int bt_trace_set_is_static(struct bt_trace *trace)
5acf2ae6
PP
2157{
2158 int ret = 0;
3602afb0 2159 size_t i;
5acf2ae6
PP
2160
2161 if (!trace) {
95c09b3a 2162 BT_LOGW_STR("Invalid parameter: trace is NULL.");
5acf2ae6
PP
2163 ret = -1;
2164 goto end;
2165 }
2166
c55a9f58 2167 trace->is_static = BT_TRUE;
50842bdc 2168 bt_trace_freeze(trace);
95c09b3a 2169 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
50842bdc 2170 trace, bt_trace_get_name(trace));
5acf2ae6 2171
3602afb0
PP
2172 /* Call all the "trace is static" listeners */
2173 for (i = 0; i < trace->is_static_listeners->len; i++) {
50842bdc 2174 struct bt_trace_is_static_listener_elem elem =
3602afb0 2175 g_array_index(trace->is_static_listeners,
50842bdc 2176 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
2177
2178 if (elem.func) {
2179 elem.func(trace, elem.data);
2180 }
2181 }
2182
2183end:
2184 return ret;
2185}
2186
50842bdc
PP
2187int bt_trace_add_is_static_listener(struct bt_trace *trace,
2188 bt_trace_is_static_listener listener,
2189 bt_trace_listener_removed listener_removed, void *data)
3602afb0
PP
2190{
2191 int i;
50842bdc 2192 struct bt_trace_is_static_listener_elem new_elem = {
3602afb0 2193 .func = listener,
8480c8cc 2194 .removed = listener_removed,
3602afb0
PP
2195 .data = data,
2196 };
2197
2198 if (!trace) {
2199 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2200 i = -1;
2201 goto end;
2202 }
2203
2204 if (!listener) {
2205 BT_LOGW_STR("Invalid parameter: listener is NULL.");
2206 i = -1;
2207 goto end;
2208 }
2209
2210 if (trace->is_static) {
2211 BT_LOGW("Invalid parameter: trace is already static: "
2212 "addr=%p, name=\"%s\"",
50842bdc 2213 trace, bt_trace_get_name(trace));
3602afb0
PP
2214 i = -1;
2215 goto end;
2216 }
2217
8480c8cc
PP
2218 if (trace->in_remove_listener) {
2219 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2220 "addr=%p, name=\"%s\"",
50842bdc 2221 trace, bt_trace_get_name(trace));
8480c8cc
PP
2222 i = -1;
2223 goto end;
2224 }
2225
3602afb0
PP
2226 /* Find the next available spot */
2227 for (i = 0; i < trace->is_static_listeners->len; i++) {
50842bdc 2228 struct bt_trace_is_static_listener_elem elem =
3602afb0 2229 g_array_index(trace->is_static_listeners,
50842bdc 2230 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
2231
2232 if (!elem.func) {
2233 break;
2234 }
2235 }
2236
2237 if (i == trace->is_static_listeners->len) {
2238 g_array_append_val(trace->is_static_listeners, new_elem);
2239 } else {
2240 g_array_insert_val(trace->is_static_listeners, i, new_elem);
2241 }
2242
2243 BT_LOGV("Added \"trace is static\" listener: "
2244 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
2245 "data-addr=%p, listener-id=%d",
50842bdc 2246 trace, bt_trace_get_name(trace), listener, data, i);
3602afb0
PP
2247
2248end:
2249 return i;
2250}
2251
50842bdc
PP
2252int bt_trace_remove_is_static_listener(
2253 struct bt_trace *trace, int listener_id)
3602afb0
PP
2254{
2255 int ret = 0;
50842bdc 2256 struct bt_trace_is_static_listener_elem *elem;
3602afb0
PP
2257
2258 if (!trace) {
2259 BT_LOGW_STR("Invalid parameter: trace is NULL.");
2260 ret = -1;
2261 goto end;
2262 }
2263
8480c8cc
PP
2264 if (trace->in_remove_listener) {
2265 BT_LOGW("Cannot call this function during the execution of a remove listener: "
2266 "addr=%p, name=\"%s\", listener-id=%d",
50842bdc 2267 trace, bt_trace_get_name(trace),
8480c8cc
PP
2268 listener_id);
2269 ret = -1;
2270 goto end;
2271 }
2272
3602afb0
PP
2273 if (listener_id < 0) {
2274 BT_LOGW("Invalid listener ID: must be zero or positive: "
2275 "listener-id=%d", listener_id);
2276 ret = -1;
2277 goto end;
2278 }
2279
2280 if (listener_id >= trace->is_static_listeners->len) {
2281 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2282 "addr=%p, name=\"%s\", listener-id=%d",
50842bdc 2283 trace, bt_trace_get_name(trace),
3602afb0
PP
2284 listener_id);
2285 ret = -1;
2286 goto end;
2287 }
2288
2289 elem = &g_array_index(trace->is_static_listeners,
50842bdc 2290 struct bt_trace_is_static_listener_elem,
3602afb0
PP
2291 listener_id);
2292 if (!elem->func) {
2293 BT_LOGW("Invalid parameter: no listener with this listener ID: "
2294 "addr=%p, name=\"%s\", listener-id=%d",
50842bdc 2295 trace, bt_trace_get_name(trace),
3602afb0
PP
2296 listener_id);
2297 ret = -1;
2298 goto end;
2299 }
2300
8480c8cc
PP
2301 if (elem->removed) {
2302 /* Call remove listener */
2303 BT_LOGV("Calling remove listener: "
2304 "trace-addr=%p, trace-name=\"%s\", "
50842bdc 2305 "listener-id=%d", trace, bt_trace_get_name(trace),
8480c8cc
PP
2306 listener_id);
2307 trace->in_remove_listener = BT_TRUE;
2308 elem->removed(trace, elem->data);
2309 trace->in_remove_listener = BT_FALSE;
2310 }
2311
3602afb0 2312 elem->func = NULL;
8480c8cc 2313 elem->removed = NULL;
3602afb0
PP
2314 elem->data = NULL;
2315 BT_LOGV("Removed \"trace is static\" listener: "
2316 "trace-addr=%p, trace-name=\"%s\", "
50842bdc 2317 "listener-id=%d", trace, bt_trace_get_name(trace),
3602afb0
PP
2318 listener_id);
2319
5acf2ae6
PP
2320end:
2321 return ret;
2322}
This page took 0.156998 seconds and 4 git commands to generate.