Split CTF IR and CTF writer APIs and implementations
[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
3dca2276 32#include <babeltrace/assert-pre-internal.h>
bc37ae52 33#include <babeltrace/ctf-ir/trace-internal.h>
ac0c6bdd 34#include <babeltrace/ctf-ir/clock-class-internal.h>
bc37ae52
JG
35#include <babeltrace/ctf-ir/stream-internal.h>
36#include <babeltrace/ctf-ir/stream-class-internal.h>
09840de5 37#include <babeltrace/ctf-ir/event-internal.h>
272df73e
PP
38#include <babeltrace/ctf-ir/event-class.h>
39#include <babeltrace/ctf-ir/event-class-internal.h>
bc37ae52 40#include <babeltrace/ctf-writer/functor-internal.h>
ac0c6bdd 41#include <babeltrace/ctf-writer/clock-internal.h>
2e33ac5a 42#include <babeltrace/ctf-ir/field-types-internal.h>
44e0a4f5 43#include <babeltrace/ctf-ir/attributes-internal.h>
09840de5 44#include <babeltrace/ctf-ir/validation-internal.h>
8bf65fbd 45#include <babeltrace/ctf-ir/visitor-internal.h>
654c1444 46#include <babeltrace/ctf-ir/utils.h>
726106d3 47#include <babeltrace/ctf-ir/utils-internal.h>
3d9990ac 48#include <babeltrace/compiler-internal.h>
dac5c838 49#include <babeltrace/values.h>
95c09b3a 50#include <babeltrace/values-internal.h>
83509119 51#include <babeltrace/ref.h>
c55a9f58 52#include <babeltrace/types.h>
3d9990ac 53#include <babeltrace/endian-internal.h>
f6ccaed9 54#include <babeltrace/assert-internal.h>
dc3fffef 55#include <inttypes.h>
544d0515 56#include <stdint.h>
4a32fda0 57#include <string.h>
0fbb9a9f 58#include <stdlib.h>
bc37ae52
JG
59
60#define DEFAULT_IDENTIFIER_SIZE 128
61#define DEFAULT_METADATA_STRING_SIZE 4096
62
50ad4244 63struct listener_wrapper {
50842bdc 64 bt_listener_cb listener;
2204c381
JG
65 void *data;
66};
67
50842bdc
PP
68struct bt_trace_is_static_listener_elem {
69 bt_trace_is_static_listener func;
70 bt_trace_listener_removed removed;
3602afb0
PP
71 void *data;
72};
73
bc37ae52 74static
3dca2276
PP
75void bt_trace_destroy(struct bt_object *obj)
76{
77 struct bt_trace *trace = (void *) obj;
bc37ae52 78
3dca2276
PP
79 BT_LOGD("Destroying trace object: addr=%p, name=\"%s\"",
80 trace, bt_trace_get_name(trace));
bc37ae52 81
3dca2276
PP
82 /*
83 * Call remove listeners first so that everything else still
84 * exists in the trace.
85 */
86 if (trace->is_static_listeners) {
87 size_t i;
bc37ae52 88
3dca2276
PP
89 for (i = 0; i < trace->is_static_listeners->len; i++) {
90 struct bt_trace_is_static_listener_elem elem =
91 g_array_index(trace->is_static_listeners,
92 struct bt_trace_is_static_listener_elem, i);
bc37ae52 93
3dca2276
PP
94 if (elem.removed) {
95 elem.removed(trace, elem.data);
96 }
97 }
98
99 g_array_free(trace->is_static_listeners, TRUE);
bc37ae52
JG
100 }
101
3dca2276
PP
102 if (trace->listeners) {
103 g_ptr_array_free(trace->listeners, TRUE);
104 }
105
106 bt_trace_common_finalize(BT_TO_COMMON(trace));
107 g_free(trace);
108}
109
110BT_HIDDEN
111int bt_trace_common_initialize(struct bt_trace_common *trace,
112 bt_object_release_func release_func)
113{
114 int ret = 0;
115
116 BT_LOGD_STR("Initializing common trace object.");
50842bdc 117 trace->native_byte_order = BT_BYTE_ORDER_UNSPECIFIED;
3dca2276
PP
118 bt_object_init(trace, release_func);
119 trace->clock_classes = g_ptr_array_new_with_free_func(
83509119 120 (GDestroyNotify) bt_put);
3dca2276 121 if (!trace->clock_classes) {
95c09b3a
PP
122 BT_LOGE_STR("Failed to allocate one GPtrArray.");
123 goto error;
124 }
125
bc37ae52 126 trace->streams = g_ptr_array_new_with_free_func(
e6a8e8e4 127 (GDestroyNotify) bt_object_release);
95c09b3a
PP
128 if (!trace->streams) {
129 BT_LOGE_STR("Failed to allocate one GPtrArray.");
130 goto error;
131 }
132
bc37ae52 133 trace->stream_classes = g_ptr_array_new_with_free_func(
e6a8e8e4 134 (GDestroyNotify) bt_object_release);
95c09b3a
PP
135 if (!trace->stream_classes) {
136 BT_LOGE_STR("Failed to allocate one GPtrArray.");
83509119 137 goto error;
bc37ae52
JG
138 }
139
7f800dc7 140 /* Create the environment array object */
50842bdc 141 trace->environment = bt_attributes_create();
7f800dc7 142 if (!trace->environment) {
95c09b3a 143 BT_LOGE_STR("Cannot create empty attributes object.");
83509119 144 goto error;
7f800dc7
PP
145 }
146
3dca2276
PP
147 BT_LOGD("Initialized common trace object: addr=%p", trace);
148 goto end;
149
150error:
151 ret = -1;
152
153end:
154 return ret;
155}
156
157BT_HIDDEN
158void bt_trace_common_finalize(struct bt_trace_common *trace)
159{
160 BT_LOGD("Finalizing common trace object: addr=%p, name=\"%s\"",
161 trace, bt_trace_common_get_name(trace));
162
163 if (trace->environment) {
164 BT_LOGD_STR("Destroying environment attributes.");
165 bt_attributes_destroy(trace->environment);
166 }
167
168 if (trace->name) {
169 g_string_free(trace->name, TRUE);
170 }
171
172 if (trace->clock_classes) {
173 BT_LOGD_STR("Putting clock classes.");
174 g_ptr_array_free(trace->clock_classes, TRUE);
175 }
176
177 if (trace->streams) {
178 BT_LOGD_STR("Destroying streams.");
179 g_ptr_array_free(trace->streams, TRUE);
180 }
181
182 if (trace->stream_classes) {
183 BT_LOGD_STR("Destroying stream classes.");
184 g_ptr_array_free(trace->stream_classes, TRUE);
185 }
186
187 BT_LOGD_STR("Putting packet header field type.");
188 bt_put(trace->packet_header_field_type);
189}
190
191struct bt_trace *bt_trace_create(void)
192{
193 struct bt_trace *trace = NULL;
194 int ret;
195
196 BT_LOGD_STR("Creating trace object.");
197 trace = g_new0(struct bt_trace, 1);
198 if (!trace) {
199 BT_LOGE_STR("Failed to allocate one trace.");
200 goto error;
201 }
202
203 ret = bt_trace_common_initialize(BT_TO_COMMON(trace), bt_trace_destroy);
204 if (ret) {
205 /* bt_trace_common_initialize() logs errors */
206 goto error;
207 }
208
9b888ff3
JG
209 trace->listeners = g_ptr_array_new_with_free_func(
210 (GDestroyNotify) g_free);
211 if (!trace->listeners) {
95c09b3a 212 BT_LOGE_STR("Failed to allocate one GPtrArray.");
9b888ff3
JG
213 goto error;
214 }
215
3602afb0 216 trace->is_static_listeners = g_array_new(FALSE, TRUE,
50842bdc 217 sizeof(struct bt_trace_is_static_listener_elem));
3602afb0
PP
218 if (!trace->is_static_listeners) {
219 BT_LOGE_STR("Failed to allocate one GArray.");
220 goto error;
221 }
222
95c09b3a 223 BT_LOGD("Created trace object: addr=%p", trace);
bc37ae52
JG
224 return trace;
225
bc37ae52 226error:
83509119 227 BT_PUT(trace);
bc37ae52
JG
228 return trace;
229}
230
50842bdc 231const char *bt_trace_get_name(struct bt_trace *trace)
e96045d4 232{
3dca2276 233 return bt_trace_common_get_name(BT_TO_COMMON(trace));
e96045d4
JG
234}
235
3dca2276
PP
236BT_HIDDEN
237int bt_trace_common_set_name(struct bt_trace_common *trace, const char *name)
e96045d4
JG
238{
239 int ret = 0;
240
95c09b3a
PP
241 if (!trace) {
242 BT_LOGW_STR("Invalid parameter: trace is NULL.");
243 ret = -1;
244 goto end;
245 }
246
247 if (!name) {
248 BT_LOGW_STR("Invalid parameter: name is NULL.");
249 ret = -1;
250 goto end;
251 }
252
253 if (trace->frozen) {
254 BT_LOGW("Invalid parameter: trace is frozen: "
255 "addr=%p, name=\"%s\"",
3dca2276 256 trace, bt_trace_common_get_name(trace));
e96045d4
JG
257 ret = -1;
258 goto end;
259 }
260
261 trace->name = trace->name ? g_string_assign(trace->name, name) :
262 g_string_new(name);
263 if (!trace->name) {
95c09b3a 264 BT_LOGE_STR("Failed to allocate one GString.");
e96045d4
JG
265 ret = -1;
266 goto end;
267 }
95c09b3a
PP
268
269 BT_LOGV("Set trace's name: addr=%p, name=\"%s\"", trace, name);
270
e96045d4
JG
271end:
272 return ret;
273}
274
3dca2276
PP
275int bt_trace_set_name(struct bt_trace *trace, const char *name)
276{
277 return bt_trace_common_set_name(BT_TO_COMMON(trace), name);
278}
279
50842bdc 280const unsigned char *bt_trace_get_uuid(struct bt_trace *trace)
4a32fda0 281{
3dca2276 282 return bt_trace_common_get_uuid(BT_TO_COMMON(trace));
4a32fda0
PP
283}
284
3dca2276
PP
285BT_HIDDEN
286int bt_trace_common_set_uuid(struct bt_trace_common *trace,
287 const unsigned char *uuid)
4a32fda0
PP
288{
289 int ret = 0;
290
95c09b3a
PP
291 if (!trace) {
292 BT_LOGW_STR("Invalid parameter: trace is NULL.");
293 ret = -1;
294 goto end;
295 }
296
297 if (!uuid) {
298 BT_LOGW_STR("Invalid parameter: UUID is NULL.");
299 ret = -1;
300 goto end;
301 }
302
303 if (trace->frozen) {
304 BT_LOGW("Invalid parameter: trace is frozen: "
305 "addr=%p, name=\"%s\"",
3dca2276 306 trace, bt_trace_common_get_name(trace));
4a32fda0
PP
307 ret = -1;
308 goto end;
309 }
310
20eee76e 311 memcpy(trace->uuid, uuid, BABELTRACE_UUID_LEN);
c55a9f58 312 trace->uuid_set = BT_TRUE;
95c09b3a
PP
313 BT_LOGV("Set trace's UUID: addr=%p, name=\"%s\", "
314 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
3dca2276 315 trace, bt_trace_common_get_name(trace),
95c09b3a
PP
316 (unsigned int) uuid[0],
317 (unsigned int) uuid[1],
318 (unsigned int) uuid[2],
319 (unsigned int) uuid[3],
320 (unsigned int) uuid[4],
321 (unsigned int) uuid[5],
322 (unsigned int) uuid[6],
323 (unsigned int) uuid[7],
324 (unsigned int) uuid[8],
325 (unsigned int) uuid[9],
326 (unsigned int) uuid[10],
327 (unsigned int) uuid[11],
328 (unsigned int) uuid[12],
329 (unsigned int) uuid[13],
330 (unsigned int) uuid[14],
331 (unsigned int) uuid[15]);
4a32fda0
PP
332
333end:
334 return ret;
335}
336
3dca2276
PP
337int bt_trace_set_uuid(struct bt_trace *trace,
338 const unsigned char *uuid)
bc37ae52 339{
3dca2276 340 return bt_trace_common_set_uuid(BT_TO_COMMON(trace), uuid);
bc37ae52
JG
341}
342
3dca2276
PP
343BT_HIDDEN
344int bt_trace_common_set_environment_field(struct bt_trace_common *trace,
dac5c838 345 const char *name, struct bt_value *value)
bc37ae52 346{
bc37ae52
JG
347 int ret = 0;
348
95c09b3a
PP
349 if (!trace) {
350 BT_LOGW_STR("Invalid parameter: trace is NULL.");
351 ret = -1;
352 goto end;
353 }
354
355 if (!name) {
356 BT_LOGW_STR("Invalid parameter: name is NULL.");
357 ret = -1;
358 goto end;
359 }
360
361 if (!value) {
362 BT_LOGW_STR("Invalid parameter: value is NULL.");
363 ret = -1;
364 goto end;
365 }
366
f4cc9a21 367 if (!bt_identifier_is_valid(name)) {
95c09b3a
PP
368 BT_LOGW("Invalid parameter: environment field's name is not a valid CTF identifier: "
369 "trace-addr=%p, trace-name=\"%s\", "
370 "env-name=\"%s\"",
3dca2276 371 trace, bt_trace_common_get_name(trace), name);
bc37ae52 372 ret = -1;
7f800dc7 373 goto end;
bc37ae52
JG
374 }
375
95c09b3a
PP
376 if (!bt_value_is_integer(value) && !bt_value_is_string(value)) {
377 BT_LOGW("Invalid parameter: environment field's value is not an integer or string value: "
378 "trace-addr=%p, trace-name=\"%s\", "
379 "env-name=\"%s\", env-value-type=%s",
3dca2276 380 trace, bt_trace_common_get_name(trace), name,
95c09b3a
PP
381 bt_value_type_string(bt_value_get_type(value)));
382 ret = -1;
383 goto end;
384 }
385
a0d12916
JG
386 if (trace->frozen) {
387 /*
388 * New environment fields may be added to a frozen trace,
389 * but existing fields may not be changed.
390 *
391 * The object passed is frozen like all other attributes.
392 */
dac5c838 393 struct bt_value *attribute =
50842bdc 394 bt_attributes_get_field_value_by_name(
a0d12916
JG
395 trace->environment, name);
396
397 if (attribute) {
95c09b3a
PP
398 BT_LOGW("Invalid parameter: trace is frozen and environment field already exists with this name: "
399 "trace-addr=%p, trace-name=\"%s\", "
400 "env-name=\"%s\"",
3dca2276 401 trace, bt_trace_common_get_name(trace), name);
83509119 402 BT_PUT(attribute);
a0d12916
JG
403 ret = -1;
404 goto end;
405 }
406
dac5c838 407 bt_value_freeze(value);
a0d12916
JG
408 }
409
50842bdc 410 ret = bt_attributes_set_field_value(trace->environment, name,
7f800dc7 411 value);
95c09b3a
PP
412 if (ret) {
413 BT_LOGE("Cannot set environment field's value: "
414 "trace-addr=%p, trace-name=\"%s\", "
415 "env-name=\"%s\"",
3dca2276 416 trace, bt_trace_common_get_name(trace), name);
95c09b3a
PP
417 } else {
418 BT_LOGV("Set environment field's value: "
419 "trace-addr=%p, trace-name=\"%s\", "
420 "env-name=\"%s\", value-addr=%p",
3dca2276 421 trace, bt_trace_common_get_name(trace), name, value);
95c09b3a 422 }
bc37ae52 423
7f800dc7
PP
424end:
425 return ret;
426}
bc37ae52 427
3dca2276
PP
428int bt_trace_set_environment_field(struct bt_trace *trace,
429 const char *name, struct bt_value *value)
430{
431 int ret;
432
433 if (trace->is_static) {
434 BT_LOGW("Invalid parameter: trace is static: "
435 "addr=%p, name=\"%s\"",
436 trace, bt_trace_get_name(trace));
437 ret = -1;
438 goto end;
439 }
440
441 ret = bt_trace_common_set_environment_field(BT_TO_COMMON(trace),
442 name, value);
443
444end:
445 return ret;
446}
447
448BT_HIDDEN
449int bt_trace_common_set_environment_field_string(struct bt_trace_common *trace,
7f800dc7
PP
450 const char *name, const char *value)
451{
452 int ret = 0;
dac5c838 453 struct bt_value *env_value_string_obj = NULL;
7f800dc7 454
95c09b3a
PP
455 if (!value) {
456 BT_LOGW_STR("Invalid parameter: value is NULL.");
bc37ae52 457 ret = -1;
7f800dc7 458 goto end;
bc37ae52
JG
459 }
460
dac5c838 461 env_value_string_obj = bt_value_string_create_init(value);
7f800dc7 462 if (!env_value_string_obj) {
95c09b3a 463 BT_LOGE_STR("Cannot create string value object.");
7f800dc7
PP
464 ret = -1;
465 goto end;
bc37ae52
JG
466 }
467
3dca2276
PP
468 /* bt_trace_common_set_environment_field() logs errors */
469 ret = bt_trace_common_set_environment_field(trace, name,
7f800dc7
PP
470 env_value_string_obj);
471
472end:
95c09b3a 473 bt_put(env_value_string_obj);
3487c9f3
JG
474 return ret;
475}
476
3dca2276
PP
477int bt_trace_set_environment_field_string(struct bt_trace *trace,
478 const char *name, const char *value)
479{
480 return bt_trace_common_set_environment_field_string(BT_TO_COMMON(trace),
481 name, value);
482}
483
484BT_HIDDEN
485int bt_trace_common_set_environment_field_integer(
486 struct bt_trace_common *trace, const char *name, int64_t value)
3487c9f3 487{
3487c9f3 488 int ret = 0;
dac5c838 489 struct bt_value *env_value_integer_obj = NULL;
3487c9f3 490
dac5c838 491 env_value_integer_obj = bt_value_integer_create_init(value);
7f800dc7 492 if (!env_value_integer_obj) {
95c09b3a 493 BT_LOGE_STR("Cannot create integer value object.");
3487c9f3 494 ret = -1;
7f800dc7 495 goto end;
3487c9f3
JG
496 }
497
3dca2276
PP
498 /* bt_trace_common_set_environment_field() logs errors */
499 ret = bt_trace_common_set_environment_field(trace, name,
7f800dc7 500 env_value_integer_obj);
95c09b3a 501
7f800dc7 502end:
95c09b3a 503 bt_put(env_value_integer_obj);
bc37ae52
JG
504 return ret;
505}
506
3dca2276
PP
507int bt_trace_set_environment_field_integer(
508 struct bt_trace *trace, const char *name, int64_t value)
e6fa2160 509{
3dca2276
PP
510 return bt_trace_common_set_environment_field_integer(
511 BT_TO_COMMON(trace), name, value);
512}
e6fa2160 513
3dca2276
PP
514int64_t bt_trace_get_environment_field_count(struct bt_trace *trace)
515{
516 return bt_trace_common_get_environment_field_count(BT_TO_COMMON(trace));
e6fa2160
JG
517}
518
519const char *
50842bdc 520bt_trace_get_environment_field_name_by_index(struct bt_trace *trace,
9ac68eb1 521 uint64_t index)
e6fa2160 522{
3dca2276
PP
523 return bt_trace_common_get_environment_field_name_by_index(
524 BT_TO_COMMON(trace), index);
e6fa2160
JG
525}
526
50842bdc
PP
527struct bt_value *bt_trace_get_environment_field_value_by_index(
528 struct bt_trace *trace, uint64_t index)
e6fa2160 529{
3dca2276
PP
530 return bt_trace_common_get_environment_field_value_by_index(
531 BT_TO_COMMON(trace), index);
e6fa2160
JG
532}
533
50842bdc
PP
534struct bt_value *bt_trace_get_environment_field_value_by_name(
535 struct bt_trace *trace, const char *name)
e6fa2160 536{
3dca2276
PP
537 return bt_trace_common_get_environment_field_value_by_name(
538 BT_TO_COMMON(trace), name);
e6fa2160
JG
539}
540
3dca2276
PP
541BT_HIDDEN
542int bt_trace_common_add_clock_class(struct bt_trace_common *trace,
50842bdc 543 struct bt_clock_class *clock_class)
bc37ae52
JG
544{
545 int ret = 0;
bc37ae52 546
95c09b3a
PP
547 if (!trace) {
548 BT_LOGW_STR("Invalid parameter: trace is NULL.");
549 ret = -1;
550 goto end;
551 }
552
50842bdc 553 if (!bt_clock_class_is_valid(clock_class)) {
95c09b3a
PP
554 BT_LOGW("Invalid parameter: clock class is invalid: "
555 "trace-addr=%p, trace-name=\"%s\", "
556 "clock-class-addr=%p, clock-class-name=\"%s\"",
3dca2276 557 trace, bt_trace_common_get_name(trace),
50842bdc 558 clock_class, bt_clock_class_get_name(clock_class));
bc37ae52
JG
559 ret = -1;
560 goto end;
561 }
562
ac0c6bdd 563 /* Check for duplicate clock classes */
3dca2276 564 if (bt_trace_common_has_clock_class(trace, clock_class)) {
95c09b3a
PP
565 BT_LOGW("Invalid parameter: clock class already exists in trace: "
566 "trace-addr=%p, trace-name=\"%s\", "
567 "clock-class-addr=%p, clock-class-name=\"%s\"",
3dca2276 568 trace, bt_trace_common_get_name(trace),
50842bdc 569 clock_class, bt_clock_class_get_name(clock_class));
bc37ae52
JG
570 ret = -1;
571 goto end;
572 }
573
ac0c6bdd 574 bt_get(clock_class);
3dca2276 575 g_ptr_array_add(trace->clock_classes, clock_class);
cfeb617e 576
6474e016 577 if (trace->frozen) {
95c09b3a 578 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
50842bdc 579 bt_clock_class_freeze(clock_class);
6474e016 580 }
95c09b3a
PP
581
582 BT_LOGV("Added clock class to trace: "
583 "trace-addr=%p, trace-name=\"%s\", "
584 "clock-class-addr=%p, clock-class-name=\"%s\"",
3dca2276 585 trace, bt_trace_common_get_name(trace),
50842bdc 586 clock_class, bt_clock_class_get_name(clock_class));
95c09b3a 587
bc37ae52
JG
588end:
589 return ret;
590}
591
3dca2276
PP
592int bt_trace_add_clock_class(struct bt_trace *trace,
593 struct bt_clock_class *clock_class)
594{
595 int ret;
596
597 if (trace->is_static) {
598 BT_LOGW("Invalid parameter: trace is static: "
599 "addr=%p, name=\"%s\"",
600 trace, bt_trace_get_name(trace));
601 ret = -1;
602 goto end;
603 }
604
605 ret = bt_trace_common_add_clock_class(BT_TO_COMMON(trace),
606 clock_class);
607
608end:
609 return ret;
610}
611
50842bdc 612int64_t bt_trace_get_clock_class_count(struct bt_trace *trace)
884cd6c3 613{
3dca2276 614 return bt_trace_common_get_clock_class_count(BT_TO_COMMON(trace));
884cd6c3
JG
615}
616
50842bdc
PP
617struct bt_clock_class *bt_trace_get_clock_class_by_index(
618 struct bt_trace *trace, uint64_t index)
884cd6c3 619{
3dca2276
PP
620 return bt_trace_common_get_clock_class_by_index(
621 BT_TO_COMMON(trace), index);
884cd6c3
JG
622}
623
e011d2c1 624static
3dca2276
PP
625bool packet_header_field_type_is_valid(struct bt_trace_common *trace,
626 struct bt_field_type_common *packet_header_type)
e011d2c1
PP
627{
628 int ret;
629 bool is_valid = true;
3dca2276 630 struct bt_field_type_common *field_type = NULL;
e011d2c1
PP
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 */
3dca2276 650 if (packet_header_type->id != BT_FIELD_TYPE_ID_STRUCT) {
e011d2c1
PP
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,
3dca2276 654 bt_common_field_type_id_string(packet_header_type->id));
e011d2c1
PP
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 */
3dca2276 663 field_type = bt_field_type_common_structure_get_field_type_by_name(
e011d2c1
PP
664 packet_header_type, "magic");
665 if (field_type) {
666 const char *field_name;
667
3dca2276 668 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
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,
3dca2276 672 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
673 goto invalid;
674 }
675
3dca2276 676 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
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
3dca2276 682 if (bt_field_type_common_integer_get_size(field_type) != 32) {
e011d2c1
PP
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,
3dca2276 686 bt_field_type_common_integer_get_size(field_type));
e011d2c1
PP
687 goto invalid;
688 }
689
3dca2276 690 ret = bt_field_type_common_structure_get_field_by_index(
e011d2c1 691 packet_header_type, &field_name, NULL, 0);
f6ccaed9 692 BT_ASSERT(ret == 0);
e011d2c1
PP
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 */
3dca2276 708 field_type = bt_field_type_common_structure_get_field_type_by_name(
e011d2c1
PP
709 packet_header_type, "uuid");
710 if (field_type) {
3dca2276 711 struct bt_field_type_common *elem_ft;
e011d2c1 712
3dca2276 713 if (field_type->id != BT_FIELD_TYPE_ID_ARRAY) {
e011d2c1
PP
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,
3dca2276 717 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
718 goto invalid;
719 }
720
3dca2276 721 if (bt_field_type_common_array_get_length(field_type) != 16) {
e011d2c1
PP
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,
3dca2276 725 bt_field_type_common_array_get_length(field_type));
e011d2c1
PP
726 goto invalid;
727 }
728
3dca2276 729 elem_ft = bt_field_type_common_array_get_element_field_type(field_type);
f6ccaed9 730 BT_ASSERT(elem_ft);
e011d2c1 731
3dca2276 732 if (elem_ft->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
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,
3dca2276 736 bt_common_field_type_id_string(elem_ft->id));
e011d2c1
PP
737 bt_put(elem_ft);
738 goto invalid;
739 }
740
3dca2276 741 if (bt_field_type_common_integer_is_signed(elem_ft)) {
e011d2c1
PP
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
3dca2276 748 if (bt_field_type_common_integer_get_size(elem_ft) != 8) {
e011d2c1
PP
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,
3dca2276 752 bt_field_type_common_integer_get_size(elem_ft));
e011d2c1
PP
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 */
3dca2276 765 field_type = bt_field_type_common_structure_get_field_type_by_name(
e011d2c1
PP
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) {
3dca2276 779 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
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,
3dca2276 783 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
784 goto invalid;
785 }
786
3dca2276 787 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
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
708ab211
PP
796 /*
797 * If there's a `packet_seq_num` field, it must be an unsigned
798 * integer field type.
799 */
3dca2276 800 field_type = bt_field_type_common_structure_get_field_type_by_name(
708ab211
PP
801 packet_header_type, "packet_seq_num");
802 if (field_type) {
3dca2276 803 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
708ab211
PP
804 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
805 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
806 field_type,
3dca2276 807 bt_common_field_type_id_string(field_type->id));
708ab211
PP
808 goto invalid;
809 }
810
3dca2276 811 if (bt_field_type_common_integer_is_signed(field_type)) {
708ab211
PP
812 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
813 "packet-seq-num-ft-addr=%p", field_type);
814 goto invalid;
815 }
816
817 BT_PUT(field_type);
818 }
819
e011d2c1
PP
820 goto end;
821
822invalid:
823 is_valid = false;
824
825end:
826 bt_put(field_type);
827 return is_valid;
828}
829
830static
3dca2276
PP
831bool packet_context_field_type_is_valid(struct bt_trace_common *trace,
832 struct bt_stream_class_common *stream_class,
833 struct bt_field_type_common *packet_context_type,
834 bool check_ts_begin_end_mapped)
e011d2c1
PP
835{
836 bool is_valid = true;
3dca2276 837 struct bt_field_type_common *field_type = NULL;
e011d2c1
PP
838
839 if (!packet_context_type) {
840 /* No packet context field type: valid at this point */
841 goto end;
842 }
843
844 /* Packet context field type, if it exists, must be a structure */
3dca2276 845 if (packet_context_type->id != BT_FIELD_TYPE_ID_STRUCT) {
e011d2c1
PP
846 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
847 "ft-addr=%p, ft-id=%s",
848 packet_context_type,
3dca2276 849 bt_common_field_type_id_string(packet_context_type->id));
e011d2c1
PP
850 goto invalid;
851 }
852
853 /*
854 * If there's a `packet_size` field, it must be an unsigned
855 * integer field type.
856 */
3dca2276 857 field_type = bt_field_type_common_structure_get_field_type_by_name(
e011d2c1
PP
858 packet_context_type, "packet_size");
859 if (field_type) {
3dca2276 860 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
861 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
862 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
863 field_type,
3dca2276 864 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
865 goto invalid;
866 }
867
3dca2276 868 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
869 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
870 "packet-size-ft-addr=%p", field_type);
871 goto invalid;
872 }
873
874 BT_PUT(field_type);
875 }
876
877 /*
878 * If there's a `content_size` field, it must be an unsigned
879 * integer field type.
880 */
3dca2276 881 field_type = bt_field_type_common_structure_get_field_type_by_name(
e011d2c1
PP
882 packet_context_type, "content_size");
883 if (field_type) {
3dca2276 884 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
885 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
886 "content-size-ft-addr=%p, content-size-ft-id=%s",
887 field_type,
3dca2276 888 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
889 goto invalid;
890 }
891
3dca2276 892 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
893 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
894 "content-size-ft-addr=%p", field_type);
895 goto invalid;
896 }
897
898 BT_PUT(field_type);
899 }
900
901 /*
902 * If there's a `events_discarded` field, it must be an unsigned
903 * integer field type.
904 */
3dca2276 905 field_type = bt_field_type_common_structure_get_field_type_by_name(
e011d2c1
PP
906 packet_context_type, "events_discarded");
907 if (field_type) {
3dca2276 908 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
909 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
910 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
911 field_type,
3dca2276 912 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
913 goto invalid;
914 }
915
3dca2276 916 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
917 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
918 "events-discarded-ft-addr=%p", field_type);
919 goto invalid;
920 }
921
922 BT_PUT(field_type);
923 }
924
925 /*
926 * If there's a `timestamp_begin` field, it must be an unsigned
927 * integer field type. Also, if the trace is not a CTF writer's
928 * trace, then we cannot automatically set the mapped clock
929 * class of this field, so it must have a mapped clock class.
930 */
3dca2276 931 field_type = bt_field_type_common_structure_get_field_type_by_name(
e011d2c1
PP
932 packet_context_type, "timestamp_begin");
933 if (field_type) {
3dca2276 934 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
935 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
936 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
937 field_type,
3dca2276 938 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
939 goto invalid;
940 }
941
3dca2276 942 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
943 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
944 "timestamp-begin-ft-addr=%p", field_type);
945 goto invalid;
946 }
947
3dca2276 948 if (check_ts_begin_end_mapped) {
50842bdc 949 struct bt_clock_class *clock_class =
3dca2276 950 bt_field_type_common_integer_get_mapped_clock_class(
e011d2c1
PP
951 field_type);
952
953 bt_put(clock_class);
954 if (!clock_class) {
955 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
956 "timestamp-begin-ft-addr=%p", field_type);
957 goto invalid;
958 }
959 }
960
961 BT_PUT(field_type);
962 }
963
964 /*
965 * If there's a `timestamp_end` field, it must be an unsigned
966 * integer field type. Also, if the trace is not a CTF writer's
967 * trace, then we cannot automatically set the mapped clock
968 * class of this field, so it must have a mapped clock class.
969 */
3dca2276 970 field_type = bt_field_type_common_structure_get_field_type_by_name(
e011d2c1
PP
971 packet_context_type, "timestamp_end");
972 if (field_type) {
3dca2276 973 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
974 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
975 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
976 field_type,
3dca2276 977 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
978 goto invalid;
979 }
980
3dca2276 981 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
982 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
983 "timestamp-end-ft-addr=%p", field_type);
984 goto invalid;
985 }
986
3dca2276 987 if (check_ts_begin_end_mapped) {
50842bdc 988 struct bt_clock_class *clock_class =
3dca2276 989 bt_field_type_common_integer_get_mapped_clock_class(
e011d2c1
PP
990 field_type);
991
992 bt_put(clock_class);
993 if (!clock_class) {
994 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
995 "timestamp-end-ft-addr=%p", field_type);
996 goto invalid;
997 }
998 }
999
1000 BT_PUT(field_type);
1001 }
1002
1003 goto end;
1004
1005invalid:
1006 is_valid = false;
1007
1008end:
1009 bt_put(field_type);
1010 return is_valid;
1011}
1012
1013static
3dca2276
PP
1014bool event_header_field_type_is_valid(struct bt_trace_common *trace,
1015 struct bt_stream_class_common *stream_class,
1016 struct bt_field_type_common *event_header_type)
e011d2c1
PP
1017{
1018 bool is_valid = true;
3dca2276 1019 struct bt_field_type_common *field_type = NULL;
e011d2c1
PP
1020
1021 /*
1022 * We do not validate that the `timestamp` field exists here
1023 * because CTF does not require this exact name to be mapped to
1024 * a clock class.
1025 */
1026
1027 if (!event_header_type) {
1028 /*
1029 * No event header field type: stream class must have
1030 * only one event class.
1031 */
3dca2276 1032 if (bt_stream_class_common_get_event_class_count(stream_class) > 1) {
e011d2c1
PP
1033 BT_LOGW_STR("Invalid event header field type: "
1034 "event header field type does not exist but there's more than one event class in the stream class.");
1035 goto invalid;
1036 }
1037
1038 /* No event header field type: valid at this point */
1039 goto end;
1040 }
1041
1042 /* Event header field type, if it exists, must be a structure */
3dca2276 1043 if (event_header_type->id != BT_FIELD_TYPE_ID_STRUCT) {
e011d2c1
PP
1044 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
1045 "ft-addr=%p, ft-id=%s",
1046 event_header_type,
3dca2276 1047 bt_common_field_type_id_string(event_header_type->id));
e011d2c1
PP
1048 goto invalid;
1049 }
1050
1051 /*
1052 * If there's an `id` field, it must be an unsigned integer
1053 * field type or an enumeration field type with an unsigned
1054 * integer container field type.
1055 */
3dca2276 1056 field_type = bt_field_type_common_structure_get_field_type_by_name(
e011d2c1
PP
1057 event_header_type, "id");
1058 if (field_type) {
3dca2276 1059 struct bt_field_type_common *int_ft;
e011d2c1 1060
3dca2276 1061 if (field_type->id == BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1 1062 int_ft = bt_get(field_type);
3dca2276
PP
1063 } else if (field_type->id == BT_FIELD_TYPE_ID_ENUM) {
1064 int_ft = bt_field_type_common_enumeration_get_container_field_type(
e011d2c1
PP
1065 field_type);
1066 } else {
1067 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1068 "id-ft-addr=%p, id-ft-id=%s",
1069 field_type,
3dca2276 1070 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
1071 goto invalid;
1072 }
1073
f6ccaed9 1074 BT_ASSERT(int_ft);
3dca2276 1075 if (bt_field_type_common_integer_is_signed(int_ft)) {
e011d2c1
PP
1076 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1077 "id-ft-addr=%p", int_ft);
1078 goto invalid;
1079 }
1080
1081 bt_put(int_ft);
1082 BT_PUT(field_type);
1083 }
1084
1085 goto end;
1086
1087invalid:
1088 is_valid = false;
1089
1090end:
1091 bt_put(field_type);
1092 return is_valid;
1093}
1094
726106d3 1095static
3dca2276 1096int check_packet_header_type_has_no_clock_class(struct bt_trace_common *trace)
726106d3
PP
1097{
1098 int ret = 0;
1099
3dca2276 1100 if (trace->packet_header_field_type) {
726106d3
PP
1101 struct bt_clock_class *clock_class = NULL;
1102
3dca2276
PP
1103 ret = bt_field_type_common_validate_single_clock_class(
1104 trace->packet_header_field_type,
726106d3
PP
1105 &clock_class);
1106 bt_put(clock_class);
1107 if (ret || clock_class) {
1108 BT_LOGW("Trace's packet header field type cannot "
1109 "contain a field type which is mapped to "
1110 "a clock class: "
1111 "trace-addr=%p, trace-name=\"%s\", "
1112 "clock-class-name=\"%s\"",
3dca2276 1113 trace, bt_trace_common_get_name(trace),
726106d3
PP
1114 clock_class ?
1115 bt_clock_class_get_name(clock_class) :
1116 NULL);
1117 ret = -1;
1118 }
1119 }
1120
1121 return ret;
1122}
1123
3dca2276
PP
1124BT_HIDDEN
1125int bt_trace_common_add_stream_class(struct bt_trace_common *trace,
1126 struct bt_stream_class_common *stream_class,
1127 bt_validation_flag_copy_field_type_func copy_field_type_func,
1128 struct bt_clock_class *init_expected_clock_class,
1129 int (*map_clock_classes_func)(struct bt_stream_class_common *stream_class,
1130 struct bt_field_type_common *packet_context_field_type,
1131 struct bt_field_type_common *event_header_field_type),
1132 bool check_ts_begin_end_mapped)
ef0c4a15 1133{
544d0515
PP
1134 int ret;
1135 int64_t i;
ef0c4a15 1136 int64_t stream_id;
50842bdc
PP
1137 struct bt_validation_output trace_sc_validation_output = { 0 };
1138 struct bt_validation_output *ec_validation_outputs = NULL;
1139 const enum bt_validation_flag trace_sc_validation_flags =
1140 BT_VALIDATION_FLAG_TRACE |
1141 BT_VALIDATION_FLAG_STREAM;
1142 const enum bt_validation_flag ec_validation_flags =
1143 BT_VALIDATION_FLAG_EVENT;
3dca2276
PP
1144 struct bt_field_type_common *packet_header_type = NULL;
1145 struct bt_field_type_common *packet_context_type = NULL;
1146 struct bt_field_type_common *event_header_type = NULL;
1147 struct bt_field_type_common *stream_event_ctx_type = NULL;
544d0515 1148 int64_t event_class_count;
3dca2276
PP
1149 struct bt_trace_common *current_parent_trace = NULL;
1150 struct bt_clock_class *expected_clock_class =
1151 bt_get(init_expected_clock_class);
1152
1153 BT_ASSERT(copy_field_type_func);
ef0c4a15 1154
95c09b3a
PP
1155 if (!trace) {
1156 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1157 ret = -1;
1158 goto end;
1159 }
1160
1161 if (!stream_class) {
1162 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1163 ret = -1;
1164 goto end;
1165 }
1166
95c09b3a
PP
1167 BT_LOGD("Adding stream class to trace: "
1168 "trace-addr=%p, trace-name=\"%s\", "
1169 "stream-class-addr=%p, stream-class-name=\"%s\", "
1170 "stream-class-id=%" PRId64,
3dca2276
PP
1171 trace, bt_trace_common_get_name(trace),
1172 stream_class, bt_stream_class_common_get_name(stream_class),
1173 bt_stream_class_common_get_id(stream_class));
95c09b3a 1174
3dca2276 1175 current_parent_trace = bt_stream_class_common_get_trace(stream_class);
2789e5d9
JG
1176 if (current_parent_trace) {
1177 /* Stream class is already associated to a trace, abort. */
95c09b3a
PP
1178 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1179 "stream-class-trace-addr=%p, "
1180 "stream-class-trace-name=\"%s\"",
1181 current_parent_trace,
3dca2276 1182 bt_trace_common_get_name(current_parent_trace));
2789e5d9
JG
1183 ret = -1;
1184 goto end;
1185 }
1186
09840de5 1187 event_class_count =
3dca2276 1188 bt_stream_class_common_get_event_class_count(stream_class);
f6ccaed9 1189 BT_ASSERT(event_class_count >= 0);
09840de5 1190
2a3ced3c
PP
1191 if (!stream_class->frozen) {
1192 /*
1193 * Stream class is not frozen yet. Validate that the
1194 * stream class contains at most a single clock class
1195 * because the previous
3dca2276
PP
1196 * bt_stream_class_common_add_event_class() calls did
1197 * not make this validation since the stream class's
1198 * direct field types (packet context, event header,
1199 * event context) could change afterwards. This stream
1200 * class is about to be frozen and those field types
1201 * won't be changed if this function succeeds.
2a3ced3c
PP
1202 *
1203 * At this point we're also sure that the stream class's
1204 * clock, if any, has the same class as the stream
1205 * class's expected clock class, if any. This is why, if
3dca2276 1206 * bt_stream_class_common_validate_single_clock_class()
2a3ced3c
PP
1207 * succeeds below, the call to
1208 * bt_stream_class_map_clock_class() at the end of this
1209 * function is safe because it maps to the same, single
1210 * clock class.
1211 */
3dca2276
PP
1212 ret = bt_stream_class_common_validate_single_clock_class(
1213 stream_class, &expected_clock_class);
2a3ced3c
PP
1214 if (ret) {
1215 BT_LOGW("Invalid parameter: stream class or one of its "
1216 "event classes contains a field type which is "
1217 "not recursively mapped to the expected "
1218 "clock class: "
1219 "stream-class-addr=%p, "
1220 "stream-class-id=%" PRId64 ", "
1221 "stream-class-name=\"%s\", "
1222 "expected-clock-class-addr=%p, "
1223 "expected-clock-class-name=\"%s\"",
3dca2276
PP
1224 stream_class, bt_stream_class_common_get_id(stream_class),
1225 bt_stream_class_common_get_name(stream_class),
2a3ced3c
PP
1226 expected_clock_class,
1227 expected_clock_class ?
1228 bt_clock_class_get_name(expected_clock_class) :
1229 NULL);
1230 goto end;
1231 }
fe13b5c0
PP
1232 }
1233
726106d3
PP
1234 ret = check_packet_header_type_has_no_clock_class(trace);
1235 if (ret) {
1236 /* check_packet_header_type_has_no_clock_class() logs errors */
1237 goto end;
1238 }
1239
09840de5
PP
1240 /*
1241 * We're about to freeze both the trace and the stream class.
1242 * Also, each event class contained in this stream class are
1243 * already frozen.
1244 *
1245 * This trace, this stream class, and all its event classes
1246 * should be valid at this point.
1247 *
1248 * Validate trace and stream class first, then each event
1249 * class of this stream class can be validated individually.
1250 */
1251 packet_header_type =
3dca2276 1252 bt_trace_common_get_packet_header_field_type(trace);
09840de5 1253 packet_context_type =
3dca2276 1254 bt_stream_class_common_get_packet_context_field_type(stream_class);
09840de5 1255 event_header_type =
3dca2276 1256 bt_stream_class_common_get_event_header_field_type(stream_class);
09840de5 1257 stream_event_ctx_type =
3dca2276 1258 bt_stream_class_common_get_event_context_field_type(stream_class);
95c09b3a
PP
1259
1260 BT_LOGD("Validating trace and stream class field types.");
50842bdc 1261 ret = bt_validate_class_types(trace->environment,
09840de5
PP
1262 packet_header_type, packet_context_type, event_header_type,
1263 stream_event_ctx_type, NULL, NULL, trace->valid,
1264 stream_class->valid, 1, &trace_sc_validation_output,
3dca2276 1265 trace_sc_validation_flags, copy_field_type_func);
09840de5
PP
1266 BT_PUT(packet_header_type);
1267 BT_PUT(packet_context_type);
1268 BT_PUT(event_header_type);
1269 BT_PUT(stream_event_ctx_type);
1270
2bd7f864 1271 if (ret) {
09840de5
PP
1272 /*
1273 * This means something went wrong during the validation
1274 * process, not that the objects are invalid.
1275 */
95c09b3a
PP
1276 BT_LOGE("Failed to validate trace and stream class field types: "
1277 "ret=%d", ret);
2bd7f864
JG
1278 goto end;
1279 }
1280
09840de5
PP
1281 if ((trace_sc_validation_output.valid_flags &
1282 trace_sc_validation_flags) !=
1283 trace_sc_validation_flags) {
1284 /* Invalid trace/stream class */
95c09b3a
PP
1285 BT_LOGW("Invalid trace or stream class field types: "
1286 "valid-flags=0x%x",
1287 trace_sc_validation_output.valid_flags);
09840de5
PP
1288 ret = -1;
1289 goto end;
1290 }
1291
1292 if (event_class_count > 0) {
50842bdc 1293 ec_validation_outputs = g_new0(struct bt_validation_output,
09840de5
PP
1294 event_class_count);
1295 if (!ec_validation_outputs) {
95c09b3a 1296 BT_LOGE_STR("Failed to allocate one validation output structure.");
09840de5
PP
1297 ret = -1;
1298 goto end;
1299 }
1300 }
1301
1302 /* Validate each event class individually */
6474e016 1303 for (i = 0; i < event_class_count; i++) {
3dca2276
PP
1304 struct bt_event_class_common *event_class =
1305 bt_stream_class_common_get_event_class_by_index(
9ac68eb1 1306 stream_class, i);
3dca2276
PP
1307 struct bt_field_type_common *event_context_type = NULL;
1308 struct bt_field_type_common *event_payload_type = NULL;
09840de5
PP
1309
1310 event_context_type =
3dca2276 1311 bt_event_class_common_get_context_field_type(event_class);
09840de5 1312 event_payload_type =
3dca2276 1313 bt_event_class_common_get_payload_field_type(event_class);
09840de5
PP
1314
1315 /*
1316 * It is important to use the field types returned by
1317 * the previous trace and stream class validation here
1318 * because copies could have been made.
1319 */
95c09b3a
PP
1320 BT_LOGD("Validating event class's field types: "
1321 "addr=%p, name=\"%s\", id=%" PRId64,
3dca2276
PP
1322 event_class, bt_event_class_common_get_name(event_class),
1323 bt_event_class_common_get_id(event_class));
50842bdc 1324 ret = bt_validate_class_types(trace->environment,
09840de5
PP
1325 trace_sc_validation_output.packet_header_type,
1326 trace_sc_validation_output.packet_context_type,
1327 trace_sc_validation_output.event_header_type,
1328 trace_sc_validation_output.stream_event_ctx_type,
1329 event_context_type, event_payload_type,
1330 1, 1, event_class->valid, &ec_validation_outputs[i],
3dca2276 1331 ec_validation_flags, copy_field_type_func);
09840de5
PP
1332 BT_PUT(event_context_type);
1333 BT_PUT(event_payload_type);
1334 BT_PUT(event_class);
1335
1336 if (ret) {
95c09b3a
PP
1337 BT_LOGE("Failed to validate event class field types: "
1338 "ret=%d", ret);
09840de5
PP
1339 goto end;
1340 }
1341
1342 if ((ec_validation_outputs[i].valid_flags &
1343 ec_validation_flags) != ec_validation_flags) {
1344 /* Invalid event class */
95c09b3a
PP
1345 BT_LOGW("Invalid event class field types: "
1346 "valid-flags=0x%x",
1347 ec_validation_outputs[i].valid_flags);
09840de5
PP
1348 ret = -1;
1349 goto end;
1350 }
1351 }
1352
3dca2276 1353 stream_id = bt_stream_class_common_get_id(stream_class);
ef0c4a15
JG
1354 if (stream_id < 0) {
1355 stream_id = trace->next_stream_id++;
9ac68eb1 1356 if (stream_id < 0) {
95c09b3a 1357 BT_LOGE_STR("No more stream class IDs available.");
9ac68eb1
PP
1358 ret = -1;
1359 goto end;
1360 }
ef0c4a15
JG
1361
1362 /* Try to assign a new stream id */
1363 for (i = 0; i < trace->stream_classes->len; i++) {
3dca2276 1364 if (stream_id == bt_stream_class_common_get_id(
ef0c4a15
JG
1365 trace->stream_classes->pdata[i])) {
1366 /* Duplicate stream id found */
95c09b3a
PP
1367 BT_LOGW("Duplicate stream class ID: "
1368 "id=%" PRId64, (int64_t) stream_id);
ef0c4a15
JG
1369 ret = -1;
1370 goto end;
1371 }
1372 }
1373
3dca2276 1374 if (bt_stream_class_common_set_id_no_check(stream_class,
95c09b3a 1375 stream_id)) {
ef0c4a15 1376 /* TODO Should retry with a different stream id */
95c09b3a
PP
1377 BT_LOGE("Cannot set stream class's ID: "
1378 "id=%" PRId64, (int64_t) stream_id);
ef0c4a15
JG
1379 ret = -1;
1380 goto end;
1381 }
1382 }
1383
e011d2c1
PP
1384 /*
1385 * At this point all the field types in the validation output
1386 * are valid. Validate the semantics of some scopes according to
1387 * the CTF specification.
1388 */
1389 if (!packet_header_field_type_is_valid(trace,
1390 trace_sc_validation_output.packet_header_type)) {
95c09b3a 1391 BT_LOGW_STR("Invalid trace's packet header field type.");
e011d2c1
PP
1392 ret = -1;
1393 goto end;
1394 }
1395
1396 if (!packet_context_field_type_is_valid(trace,
1397 stream_class,
3dca2276
PP
1398 trace_sc_validation_output.packet_context_type,
1399 check_ts_begin_end_mapped)) {
95c09b3a 1400 BT_LOGW_STR("Invalid stream class's packet context field type.");
e011d2c1
PP
1401 ret = -1;
1402 goto end;
1403 }
1404
1405 if (!event_header_field_type_is_valid(trace,
1406 stream_class,
1407 trace_sc_validation_output.event_header_type)) {
95c09b3a 1408 BT_LOGW_STR("Invalid steam class's event header field type.");
e011d2c1
PP
1409 ret = -1;
1410 goto end;
1411 }
1412
1413 /*
1414 * Now is the time to automatically map specific field types of
1415 * the stream class's packet context and event header field
1416 * types to the stream class's clock's class if they are not
1417 * mapped to a clock class yet. We do it here because we know
1418 * that after this point, everything is frozen so it won't be
1419 * possible for the user to modify the stream class's clock, or
1420 * to map those field types to other clock classes.
1421 */
3dca2276
PP
1422 if (map_clock_classes_func) {
1423 if (map_clock_classes_func(stream_class,
e011d2c1
PP
1424 trace_sc_validation_output.packet_context_type,
1425 trace_sc_validation_output.event_header_type)) {
3dca2276 1426 /* map_clock_classes_func() logs errors */
e011d2c1
PP
1427 ret = -1;
1428 goto end;
1429 }
1430 }
1431
e6a8e8e4 1432 bt_object_set_parent(stream_class, trace);
ef0c4a15
JG
1433 g_ptr_array_add(trace->stream_classes, stream_class);
1434
1435 /*
09840de5
PP
1436 * At this point we know that the function will be successful.
1437 * Therefore we can replace the trace and stream class field
1438 * types with what's in their validation output structure and
1439 * mark them as valid. We can also replace the field types of
1440 * all the event classes of the stream class and mark them as
1441 * valid.
1442 */
50842bdc 1443 bt_validation_replace_types(trace, stream_class, NULL,
09840de5
PP
1444 &trace_sc_validation_output, trace_sc_validation_flags);
1445 trace->valid = 1;
1446 stream_class->valid = 1;
1447
1448 /*
50842bdc 1449 * Put what was not moved in bt_validation_replace_types().
09840de5 1450 */
50842bdc 1451 bt_validation_output_put_types(&trace_sc_validation_output);
09840de5 1452
6474e016 1453 for (i = 0; i < event_class_count; i++) {
3dca2276
PP
1454 struct bt_event_class_common *event_class =
1455 bt_stream_class_common_get_event_class_by_index(
9ac68eb1 1456 stream_class, i);
09840de5 1457
50842bdc 1458 bt_validation_replace_types(NULL, NULL, event_class,
09840de5
PP
1459 &ec_validation_outputs[i], ec_validation_flags);
1460 event_class->valid = 1;
1461 BT_PUT(event_class);
1462
1463 /*
1464 * Put what was not moved in
50842bdc 1465 * bt_validation_replace_types().
09840de5 1466 */
50842bdc 1467 bt_validation_output_put_types(&ec_validation_outputs[i]);
09840de5
PP
1468 }
1469
09840de5
PP
1470 /*
1471 * Freeze the trace and the stream class.
1472 */
3dca2276
PP
1473 bt_stream_class_common_freeze(stream_class);
1474 bt_trace_common_freeze(trace);
09840de5 1475
2a3ced3c
PP
1476 /*
1477 * It is safe to set the stream class's unique clock class
1478 * now because the stream class is frozen.
1479 */
1480 if (expected_clock_class) {
1481 BT_MOVE(stream_class->clock_class, expected_clock_class);
1482 }
1483
95c09b3a
PP
1484 BT_LOGD("Added stream class to trace: "
1485 "trace-addr=%p, trace-name=\"%s\", "
1486 "stream-class-addr=%p, stream-class-name=\"%s\", "
1487 "stream-class-id=%" PRId64,
3dca2276
PP
1488 trace, bt_trace_common_get_name(trace),
1489 stream_class, bt_stream_class_common_get_name(stream_class),
1490 bt_stream_class_common_get_id(stream_class));
95c09b3a 1491
ef0c4a15 1492end:
d3814b54 1493 if (ret) {
e6a8e8e4 1494 bt_object_set_parent(stream_class, NULL);
09840de5
PP
1495
1496 if (ec_validation_outputs) {
6474e016 1497 for (i = 0; i < event_class_count; i++) {
50842bdc 1498 bt_validation_output_put_types(
09840de5
PP
1499 &ec_validation_outputs[i]);
1500 }
1501 }
d3814b54 1502 }
09840de5
PP
1503
1504 g_free(ec_validation_outputs);
50842bdc 1505 bt_validation_output_put_types(&trace_sc_validation_output);
2789e5d9 1506 bt_put(current_parent_trace);
2a3ced3c 1507 bt_put(expected_clock_class);
f6ccaed9
PP
1508 BT_ASSERT(!packet_header_type);
1509 BT_ASSERT(!packet_context_type);
1510 BT_ASSERT(!event_header_type);
1511 BT_ASSERT(!stream_event_ctx_type);
ef0c4a15
JG
1512 return ret;
1513}
1514
3dca2276
PP
1515int bt_trace_add_stream_class(struct bt_trace *trace,
1516 struct bt_stream_class *stream_class)
1517{
1518 int ret = 0;
1519
1520 if (!trace) {
1521 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1522 ret = -1;
1523 goto end;
1524 }
1525
1526 if (trace->is_static) {
1527 BT_LOGW_STR("Invalid parameter: trace is static.");
1528 ret = -1;
1529 goto end;
1530 }
1531
1532 ret = bt_trace_common_add_stream_class(BT_TO_COMMON(trace),
1533 BT_TO_COMMON(stream_class),
1534 (bt_validation_flag_copy_field_type_func) bt_field_type_copy,
1535 NULL, NULL, true);
1536 if (ret) {
1537 goto end;
1538 }
1539
1540 /* Notifiy listeners of the trace's schema modification. */
1541 bt_stream_class_common_visit(BT_TO_COMMON(stream_class),
1542 bt_trace_object_modification, trace);
1543
1544end:
1545 return ret;
1546}
1547
50842bdc 1548int64_t bt_trace_get_stream_count(struct bt_trace *trace)
c1e730fe 1549{
3dca2276 1550 return bt_trace_common_get_stream_count(BT_TO_COMMON(trace));
c1e730fe
PP
1551}
1552
50842bdc
PP
1553struct bt_stream *bt_trace_get_stream_by_index(
1554 struct bt_trace *trace,
9ac68eb1 1555 uint64_t index)
c1e730fe 1556{
3dca2276
PP
1557 return BT_FROM_COMMON(bt_trace_common_get_stream_by_index(
1558 BT_TO_COMMON(trace), index));
c1e730fe
PP
1559}
1560
50842bdc 1561int64_t bt_trace_get_stream_class_count(struct bt_trace *trace)
ef0c4a15 1562{
3dca2276 1563 return bt_trace_common_get_stream_class_count(BT_TO_COMMON(trace));
ef0c4a15
JG
1564}
1565
50842bdc
PP
1566struct bt_stream_class *bt_trace_get_stream_class_by_index(
1567 struct bt_trace *trace, uint64_t index)
ef0c4a15 1568{
3dca2276
PP
1569 return BT_FROM_COMMON(bt_trace_common_get_stream_class_by_index(
1570 BT_TO_COMMON(trace), index));
ef0c4a15
JG
1571}
1572
50842bdc 1573struct bt_stream_class *bt_trace_get_stream_class_by_id(
3dca2276 1574 struct bt_trace *trace, uint64_t id)
4841ccc1 1575{
3dca2276
PP
1576 return BT_FROM_COMMON(
1577 bt_trace_common_get_stream_class_by_id(
1578 BT_TO_COMMON(trace), id));
4841ccc1
PP
1579}
1580
50842bdc
PP
1581struct bt_clock_class *bt_trace_get_clock_class_by_name(
1582 struct bt_trace *trace, const char *name)
7e4347a3 1583{
3dca2276
PP
1584 return bt_trace_common_get_clock_class_by_name(BT_TO_COMMON(trace),
1585 name);
7e4347a3
PP
1586}
1587
c9d90a34 1588BT_HIDDEN
3dca2276 1589bt_bool bt_trace_common_has_clock_class(struct bt_trace_common *trace,
50842bdc 1590 struct bt_clock_class *clock_class)
c9d90a34
PP
1591{
1592 struct search_query query = { .value = clock_class, .found = 0 };
1593
f6ccaed9
PP
1594 BT_ASSERT(trace);
1595 BT_ASSERT(clock_class);
c9d90a34 1596
3dca2276 1597 g_ptr_array_foreach(trace->clock_classes, value_exists, &query);
c9d90a34
PP
1598 return query.found;
1599}
1600
50842bdc
PP
1601enum bt_byte_order bt_trace_get_native_byte_order(
1602 struct bt_trace *trace)
4ed90fb3 1603{
3dca2276 1604 return bt_trace_common_get_native_byte_order(BT_TO_COMMON(trace));
4ed90fb3
JG
1605}
1606
3dca2276
PP
1607BT_HIDDEN
1608int bt_trace_common_set_native_byte_order(struct bt_trace_common *trace,
1609 enum bt_byte_order byte_order, bool allow_unspecified)
bc37ae52
JG
1610{
1611 int ret = 0;
bc37ae52 1612
95c09b3a
PP
1613 if (!trace) {
1614 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1615 ret = -1;
1616 goto end;
1617 }
1618
1619 if (trace->frozen) {
1620 BT_LOGW("Invalid parameter: trace is frozen: "
1621 "addr=%p, name=\"%s\"",
3dca2276 1622 trace, bt_trace_common_get_name(trace));
bc37ae52
JG
1623 ret = -1;
1624 goto end;
1625 }
1626
3dca2276
PP
1627 if (byte_order == BT_BYTE_ORDER_UNSPECIFIED && !allow_unspecified) {
1628 BT_LOGW("Invalid parameter: BT_BYTE_ORDER_UNSPECIFIED byte order is not allowed: "
d41cff38 1629 "addr=%p, name=\"%s\"",
3dca2276 1630 trace, bt_trace_common_get_name(trace));
d41cff38
PP
1631 ret = -1;
1632 goto end;
1633 }
1634
50842bdc
PP
1635 if (byte_order != BT_BYTE_ORDER_LITTLE_ENDIAN &&
1636 byte_order != BT_BYTE_ORDER_BIG_ENDIAN &&
1637 byte_order != BT_BYTE_ORDER_NETWORK) {
95c09b3a
PP
1638 BT_LOGW("Invalid parameter: invalid byte order: "
1639 "addr=%p, name=\"%s\", bo=%s",
3dca2276
PP
1640 trace, bt_trace_common_get_name(trace),
1641 bt_common_byte_order_string(byte_order));
bc37ae52
JG
1642 ret = -1;
1643 goto end;
1644 }
1645
dc3fffef 1646 trace->native_byte_order = byte_order;
95c09b3a
PP
1647 BT_LOGV("Set trace's native byte order: "
1648 "addr=%p, name=\"%s\", bo=%s",
3dca2276
PP
1649 trace, bt_trace_common_get_name(trace),
1650 bt_common_byte_order_string(byte_order));
dc3fffef 1651
bc37ae52
JG
1652end:
1653 return ret;
1654}
1655
3dca2276
PP
1656int bt_trace_set_native_byte_order(struct bt_trace *trace,
1657 enum bt_byte_order byte_order)
1658{
1659 return bt_trace_common_set_native_byte_order(BT_TO_COMMON(trace),
1660 byte_order, true);
1661}
1662
1663struct bt_field_type *bt_trace_get_packet_header_field_type(
50842bdc 1664 struct bt_trace *trace)
d246b111 1665{
3dca2276
PP
1666 return BT_FROM_COMMON(bt_trace_common_get_packet_header_field_type(
1667 BT_TO_COMMON(trace)));
d246b111
JG
1668}
1669
3dca2276
PP
1670BT_HIDDEN
1671int bt_trace_common_set_packet_header_field_type(struct bt_trace_common *trace,
1672 struct bt_field_type_common *packet_header_type)
d246b111
JG
1673{
1674 int ret = 0;
1675
95c09b3a
PP
1676 if (!trace) {
1677 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1678 ret = -1;
1679 goto end;
1680 }
1681
1682 if (trace->frozen) {
1683 BT_LOGW("Invalid parameter: trace is frozen: "
1684 "addr=%p, name=\"%s\"",
3dca2276 1685 trace, bt_trace_common_get_name(trace));
d246b111
JG
1686 ret = -1;
1687 goto end;
1688 }
1689
835b2d10
JG
1690 /* packet_header_type must be a structure. */
1691 if (packet_header_type &&
3dca2276 1692 packet_header_type->id != BT_FIELD_TYPE_ID_STRUCT) {
95c09b3a
PP
1693 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1694 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
3dca2276 1695 trace, bt_trace_common_get_name(trace),
95c09b3a 1696 packet_header_type,
3dca2276 1697 bt_common_field_type_id_string(packet_header_type->id));
d246b111
JG
1698 ret = -1;
1699 goto end;
1700 }
1701
3dca2276
PP
1702 bt_put(trace->packet_header_field_type);
1703 trace->packet_header_field_type = bt_get(packet_header_type);
95c09b3a
PP
1704 BT_LOGV("Set trace's packet header field type: "
1705 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
3dca2276 1706 trace, bt_trace_common_get_name(trace), packet_header_type);
d246b111
JG
1707end:
1708 return ret;
1709}
1710
3dca2276
PP
1711int bt_trace_set_packet_header_field_type(struct bt_trace *trace,
1712 struct bt_field_type *packet_header_type)
1713{
1714 return bt_trace_common_set_packet_header_field_type(BT_TO_COMMON(trace),
1715 (void *) packet_header_type);
1716}
1717
8bf65fbd 1718static
544d0515 1719int64_t get_stream_class_count(void *element)
8bf65fbd 1720{
50842bdc
PP
1721 return bt_trace_get_stream_class_count(
1722 (struct bt_trace *) element);
8bf65fbd
JG
1723}
1724
1725static
1726void *get_stream_class(void *element, int i)
1727{
50842bdc
PP
1728 return bt_trace_get_stream_class_by_index(
1729 (struct bt_trace *) element, i);
8bf65fbd
JG
1730}
1731
1732static
50842bdc 1733int visit_stream_class(void *object, bt_visitor visitor,void *data)
8bf65fbd 1734{
50842bdc 1735 return bt_stream_class_visit(object, visitor, data);
8bf65fbd
JG
1736}
1737
50842bdc
PP
1738int bt_trace_visit(struct bt_trace *trace,
1739 bt_visitor visitor, void *data)
8bf65fbd
JG
1740{
1741 int ret;
50842bdc
PP
1742 struct bt_visitor_object obj = {
1743 .object = trace,
1744 .type = BT_VISITOR_OBJECT_TYPE_TRACE
1745 };
8bf65fbd 1746
95c09b3a
PP
1747 if (!trace) {
1748 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1749 ret = -1;
1750 goto end;
1751 }
1752
1753 if (!visitor) {
1754 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
8bf65fbd
JG
1755 ret = -1;
1756 goto end;
1757 }
1758
95c09b3a 1759 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
50842bdc 1760 trace, bt_trace_get_name(trace));
d9a13d86 1761 ret = visitor_helper(&obj, get_stream_class_count,
8bf65fbd
JG
1762 get_stream_class, visit_stream_class, visitor, data);
1763end:
1764 return ret;
1765}
1766
1767static
50842bdc 1768int invoke_listener(struct bt_visitor_object *object, void *data)
8bf65fbd 1769{
9b888ff3 1770 struct listener_wrapper *listener_wrapper = data;
8bf65fbd 1771
d9a13d86 1772 listener_wrapper->listener(object, listener_wrapper->data);
9b888ff3 1773 return 0;
8bf65fbd
JG
1774}
1775
95c09b3a 1776// TODO: add logging to this function once we use it internally.
50842bdc
PP
1777int bt_trace_add_listener(struct bt_trace *trace,
1778 bt_listener_cb listener, void *listener_data)
2204c381
JG
1779{
1780 int ret = 0;
50ad4244
JG
1781 struct listener_wrapper *listener_wrapper =
1782 g_new0(struct listener_wrapper, 1);
2204c381 1783
50ad4244 1784 if (!trace || !listener || !listener_wrapper) {
2204c381
JG
1785 ret = -1;
1786 goto error;
1787 }
1788
50ad4244
JG
1789 listener_wrapper->listener = listener;
1790 listener_wrapper->data = listener_data;
8bf65fbd 1791
50ad4244 1792 /* Visit the current schema. */
50842bdc 1793 ret = bt_trace_visit(trace, invoke_listener, listener_wrapper);
8bf65fbd
JG
1794 if (ret) {
1795 goto error;
1796 }
1797
50ad4244
JG
1798 /*
1799 * Add listener to the array of callbacks which will be invoked on
1800 * schema changes.
1801 */
1802 g_ptr_array_add(trace->listeners, listener_wrapper);
2204c381
JG
1803 return ret;
1804error:
50ad4244 1805 g_free(listener_wrapper);
2204c381
JG
1806 return ret;
1807}
1808
bc37ae52 1809BT_HIDDEN
50842bdc 1810int bt_trace_object_modification(struct bt_visitor_object *object,
9b888ff3
JG
1811 void *trace_ptr)
1812{
1813 size_t i;
50842bdc 1814 struct bt_trace *trace = trace_ptr;
9b888ff3 1815
f6ccaed9
PP
1816 BT_ASSERT(trace);
1817 BT_ASSERT(object);
9b888ff3
JG
1818
1819 if (trace->listeners->len == 0) {
1820 goto end;
1821 }
1822
1823 for (i = 0; i < trace->listeners->len; i++) {
1824 struct listener_wrapper *listener =
1825 g_ptr_array_index(trace->listeners, i);
1826
d9a13d86 1827 listener->listener(object, listener->data);
9b888ff3
JG
1828 }
1829end:
1830 return 0;
1831}
1832
50842bdc 1833bt_bool bt_trace_is_static(struct bt_trace *trace)
5acf2ae6 1834{
d975f66c
PP
1835 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
1836 return trace->is_static;
5acf2ae6
PP
1837}
1838
50842bdc 1839int bt_trace_set_is_static(struct bt_trace *trace)
5acf2ae6
PP
1840{
1841 int ret = 0;
3602afb0 1842 size_t i;
5acf2ae6
PP
1843
1844 if (!trace) {
95c09b3a 1845 BT_LOGW_STR("Invalid parameter: trace is NULL.");
5acf2ae6
PP
1846 ret = -1;
1847 goto end;
1848 }
1849
3dca2276 1850 ret = check_packet_header_type_has_no_clock_class(BT_TO_COMMON(trace));
726106d3
PP
1851 if (ret) {
1852 /* check_packet_header_type_has_no_clock_class() logs errors */
1853 goto end;
1854 }
1855
c55a9f58 1856 trace->is_static = BT_TRUE;
3dca2276 1857 bt_trace_common_freeze(BT_TO_COMMON(trace));
95c09b3a 1858 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
50842bdc 1859 trace, bt_trace_get_name(trace));
5acf2ae6 1860
3602afb0
PP
1861 /* Call all the "trace is static" listeners */
1862 for (i = 0; i < trace->is_static_listeners->len; i++) {
50842bdc 1863 struct bt_trace_is_static_listener_elem elem =
3602afb0 1864 g_array_index(trace->is_static_listeners,
50842bdc 1865 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
1866
1867 if (elem.func) {
1868 elem.func(trace, elem.data);
1869 }
1870 }
1871
1872end:
1873 return ret;
1874}
1875
50842bdc
PP
1876int bt_trace_add_is_static_listener(struct bt_trace *trace,
1877 bt_trace_is_static_listener listener,
1878 bt_trace_listener_removed listener_removed, void *data)
3602afb0
PP
1879{
1880 int i;
50842bdc 1881 struct bt_trace_is_static_listener_elem new_elem = {
3602afb0 1882 .func = listener,
8480c8cc 1883 .removed = listener_removed,
3602afb0
PP
1884 .data = data,
1885 };
1886
1887 if (!trace) {
1888 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1889 i = -1;
1890 goto end;
1891 }
1892
1893 if (!listener) {
1894 BT_LOGW_STR("Invalid parameter: listener is NULL.");
1895 i = -1;
1896 goto end;
1897 }
1898
1899 if (trace->is_static) {
1900 BT_LOGW("Invalid parameter: trace is already static: "
1901 "addr=%p, name=\"%s\"",
50842bdc 1902 trace, bt_trace_get_name(trace));
3602afb0
PP
1903 i = -1;
1904 goto end;
1905 }
1906
8480c8cc
PP
1907 if (trace->in_remove_listener) {
1908 BT_LOGW("Cannot call this function during the execution of a remove listener: "
1909 "addr=%p, name=\"%s\"",
50842bdc 1910 trace, bt_trace_get_name(trace));
8480c8cc
PP
1911 i = -1;
1912 goto end;
1913 }
1914
3602afb0
PP
1915 /* Find the next available spot */
1916 for (i = 0; i < trace->is_static_listeners->len; i++) {
50842bdc 1917 struct bt_trace_is_static_listener_elem elem =
3602afb0 1918 g_array_index(trace->is_static_listeners,
50842bdc 1919 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
1920
1921 if (!elem.func) {
1922 break;
1923 }
1924 }
1925
1926 if (i == trace->is_static_listeners->len) {
1927 g_array_append_val(trace->is_static_listeners, new_elem);
1928 } else {
1929 g_array_insert_val(trace->is_static_listeners, i, new_elem);
1930 }
1931
1932 BT_LOGV("Added \"trace is static\" listener: "
1933 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
1934 "data-addr=%p, listener-id=%d",
50842bdc 1935 trace, bt_trace_get_name(trace), listener, data, i);
3602afb0
PP
1936
1937end:
1938 return i;
1939}
1940
50842bdc
PP
1941int bt_trace_remove_is_static_listener(
1942 struct bt_trace *trace, int listener_id)
3602afb0
PP
1943{
1944 int ret = 0;
50842bdc 1945 struct bt_trace_is_static_listener_elem *elem;
3602afb0
PP
1946
1947 if (!trace) {
1948 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1949 ret = -1;
1950 goto end;
1951 }
1952
8480c8cc
PP
1953 if (trace->in_remove_listener) {
1954 BT_LOGW("Cannot call this function during the execution of a remove listener: "
1955 "addr=%p, name=\"%s\", listener-id=%d",
50842bdc 1956 trace, bt_trace_get_name(trace),
8480c8cc
PP
1957 listener_id);
1958 ret = -1;
1959 goto end;
1960 }
1961
3602afb0
PP
1962 if (listener_id < 0) {
1963 BT_LOGW("Invalid listener ID: must be zero or positive: "
1964 "listener-id=%d", listener_id);
1965 ret = -1;
1966 goto end;
1967 }
1968
1969 if (listener_id >= trace->is_static_listeners->len) {
1970 BT_LOGW("Invalid parameter: no listener with this listener ID: "
1971 "addr=%p, name=\"%s\", listener-id=%d",
50842bdc 1972 trace, bt_trace_get_name(trace),
3602afb0
PP
1973 listener_id);
1974 ret = -1;
1975 goto end;
1976 }
1977
1978 elem = &g_array_index(trace->is_static_listeners,
50842bdc 1979 struct bt_trace_is_static_listener_elem,
3602afb0
PP
1980 listener_id);
1981 if (!elem->func) {
1982 BT_LOGW("Invalid parameter: no listener with this listener ID: "
1983 "addr=%p, name=\"%s\", listener-id=%d",
50842bdc 1984 trace, bt_trace_get_name(trace),
3602afb0
PP
1985 listener_id);
1986 ret = -1;
1987 goto end;
1988 }
1989
8480c8cc
PP
1990 if (elem->removed) {
1991 /* Call remove listener */
1992 BT_LOGV("Calling remove listener: "
1993 "trace-addr=%p, trace-name=\"%s\", "
50842bdc 1994 "listener-id=%d", trace, bt_trace_get_name(trace),
8480c8cc
PP
1995 listener_id);
1996 trace->in_remove_listener = BT_TRUE;
1997 elem->removed(trace, elem->data);
1998 trace->in_remove_listener = BT_FALSE;
1999 }
2000
3602afb0 2001 elem->func = NULL;
8480c8cc 2002 elem->removed = NULL;
3602afb0
PP
2003 elem->data = NULL;
2004 BT_LOGV("Removed \"trace is static\" listener: "
2005 "trace-addr=%p, trace-name=\"%s\", "
50842bdc 2006 "listener-id=%d", trace, bt_trace_get_name(trace),
3602afb0
PP
2007 listener_id);
2008
5acf2ae6
PP
2009end:
2010 return ret;
2011}
This page took 0.144564 seconds and 4 git commands to generate.