ctf plugin: notif iter: use "borrow" functions for metadata where possible
[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 =
094ff7c0 394 bt_attributes_borrow_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);
a0d12916
JG
402 ret = -1;
403 goto end;
404 }
405
dac5c838 406 bt_value_freeze(value);
a0d12916
JG
407 }
408
50842bdc 409 ret = bt_attributes_set_field_value(trace->environment, name,
7f800dc7 410 value);
95c09b3a
PP
411 if (ret) {
412 BT_LOGE("Cannot set environment field's value: "
413 "trace-addr=%p, trace-name=\"%s\", "
414 "env-name=\"%s\"",
3dca2276 415 trace, bt_trace_common_get_name(trace), name);
95c09b3a
PP
416 } else {
417 BT_LOGV("Set environment field's value: "
418 "trace-addr=%p, trace-name=\"%s\", "
419 "env-name=\"%s\", value-addr=%p",
3dca2276 420 trace, bt_trace_common_get_name(trace), name, value);
95c09b3a 421 }
bc37ae52 422
7f800dc7
PP
423end:
424 return ret;
425}
bc37ae52 426
3dca2276
PP
427int bt_trace_set_environment_field(struct bt_trace *trace,
428 const char *name, struct bt_value *value)
429{
430 int ret;
431
432 if (trace->is_static) {
433 BT_LOGW("Invalid parameter: trace is static: "
434 "addr=%p, name=\"%s\"",
435 trace, bt_trace_get_name(trace));
436 ret = -1;
437 goto end;
438 }
439
440 ret = bt_trace_common_set_environment_field(BT_TO_COMMON(trace),
441 name, value);
442
443end:
444 return ret;
445}
446
447BT_HIDDEN
448int bt_trace_common_set_environment_field_string(struct bt_trace_common *trace,
7f800dc7
PP
449 const char *name, const char *value)
450{
451 int ret = 0;
dac5c838 452 struct bt_value *env_value_string_obj = NULL;
7f800dc7 453
95c09b3a
PP
454 if (!value) {
455 BT_LOGW_STR("Invalid parameter: value is NULL.");
bc37ae52 456 ret = -1;
7f800dc7 457 goto end;
bc37ae52
JG
458 }
459
dac5c838 460 env_value_string_obj = bt_value_string_create_init(value);
7f800dc7 461 if (!env_value_string_obj) {
95c09b3a 462 BT_LOGE_STR("Cannot create string value object.");
7f800dc7
PP
463 ret = -1;
464 goto end;
bc37ae52
JG
465 }
466
3dca2276
PP
467 /* bt_trace_common_set_environment_field() logs errors */
468 ret = bt_trace_common_set_environment_field(trace, name,
7f800dc7
PP
469 env_value_string_obj);
470
471end:
95c09b3a 472 bt_put(env_value_string_obj);
3487c9f3
JG
473 return ret;
474}
475
3dca2276
PP
476int bt_trace_set_environment_field_string(struct bt_trace *trace,
477 const char *name, const char *value)
478{
479 return bt_trace_common_set_environment_field_string(BT_TO_COMMON(trace),
480 name, value);
481}
482
483BT_HIDDEN
484int bt_trace_common_set_environment_field_integer(
485 struct bt_trace_common *trace, const char *name, int64_t value)
3487c9f3 486{
3487c9f3 487 int ret = 0;
dac5c838 488 struct bt_value *env_value_integer_obj = NULL;
3487c9f3 489
dac5c838 490 env_value_integer_obj = bt_value_integer_create_init(value);
7f800dc7 491 if (!env_value_integer_obj) {
95c09b3a 492 BT_LOGE_STR("Cannot create integer value object.");
3487c9f3 493 ret = -1;
7f800dc7 494 goto end;
3487c9f3
JG
495 }
496
3dca2276
PP
497 /* bt_trace_common_set_environment_field() logs errors */
498 ret = bt_trace_common_set_environment_field(trace, name,
7f800dc7 499 env_value_integer_obj);
95c09b3a 500
7f800dc7 501end:
95c09b3a 502 bt_put(env_value_integer_obj);
bc37ae52
JG
503 return ret;
504}
505
3dca2276
PP
506int bt_trace_set_environment_field_integer(
507 struct bt_trace *trace, const char *name, int64_t value)
e6fa2160 508{
3dca2276
PP
509 return bt_trace_common_set_environment_field_integer(
510 BT_TO_COMMON(trace), name, value);
511}
e6fa2160 512
3dca2276
PP
513int64_t bt_trace_get_environment_field_count(struct bt_trace *trace)
514{
515 return bt_trace_common_get_environment_field_count(BT_TO_COMMON(trace));
e6fa2160
JG
516}
517
518const char *
50842bdc 519bt_trace_get_environment_field_name_by_index(struct bt_trace *trace,
9ac68eb1 520 uint64_t index)
e6fa2160 521{
3dca2276
PP
522 return bt_trace_common_get_environment_field_name_by_index(
523 BT_TO_COMMON(trace), index);
e6fa2160
JG
524}
525
094ff7c0 526struct bt_value *bt_trace_borrow_environment_field_value_by_index(
50842bdc 527 struct bt_trace *trace, uint64_t index)
e6fa2160 528{
094ff7c0 529 return bt_trace_common_borrow_environment_field_value_by_index(
3dca2276 530 BT_TO_COMMON(trace), index);
e6fa2160
JG
531}
532
094ff7c0 533struct bt_value *bt_trace_borrow_environment_field_value_by_name(
50842bdc 534 struct bt_trace *trace, const char *name)
e6fa2160 535{
094ff7c0 536 return bt_trace_common_borrow_environment_field_value_by_name(
3dca2276 537 BT_TO_COMMON(trace), name);
e6fa2160
JG
538}
539
3dca2276
PP
540BT_HIDDEN
541int bt_trace_common_add_clock_class(struct bt_trace_common *trace,
50842bdc 542 struct bt_clock_class *clock_class)
bc37ae52
JG
543{
544 int ret = 0;
bc37ae52 545
95c09b3a
PP
546 if (!trace) {
547 BT_LOGW_STR("Invalid parameter: trace is NULL.");
548 ret = -1;
549 goto end;
550 }
551
50842bdc 552 if (!bt_clock_class_is_valid(clock_class)) {
95c09b3a
PP
553 BT_LOGW("Invalid parameter: clock class is invalid: "
554 "trace-addr=%p, trace-name=\"%s\", "
555 "clock-class-addr=%p, clock-class-name=\"%s\"",
3dca2276 556 trace, bt_trace_common_get_name(trace),
50842bdc 557 clock_class, bt_clock_class_get_name(clock_class));
bc37ae52
JG
558 ret = -1;
559 goto end;
560 }
561
ac0c6bdd 562 /* Check for duplicate clock classes */
3dca2276 563 if (bt_trace_common_has_clock_class(trace, clock_class)) {
95c09b3a
PP
564 BT_LOGW("Invalid parameter: clock class already exists in trace: "
565 "trace-addr=%p, trace-name=\"%s\", "
566 "clock-class-addr=%p, clock-class-name=\"%s\"",
3dca2276 567 trace, bt_trace_common_get_name(trace),
50842bdc 568 clock_class, bt_clock_class_get_name(clock_class));
bc37ae52
JG
569 ret = -1;
570 goto end;
571 }
572
ac0c6bdd 573 bt_get(clock_class);
3dca2276 574 g_ptr_array_add(trace->clock_classes, clock_class);
cfeb617e 575
6474e016 576 if (trace->frozen) {
95c09b3a 577 BT_LOGV_STR("Freezing added clock class because trace is frozen.");
50842bdc 578 bt_clock_class_freeze(clock_class);
6474e016 579 }
95c09b3a
PP
580
581 BT_LOGV("Added clock class to trace: "
582 "trace-addr=%p, trace-name=\"%s\", "
583 "clock-class-addr=%p, clock-class-name=\"%s\"",
3dca2276 584 trace, bt_trace_common_get_name(trace),
50842bdc 585 clock_class, bt_clock_class_get_name(clock_class));
95c09b3a 586
bc37ae52
JG
587end:
588 return ret;
589}
590
3dca2276
PP
591int bt_trace_add_clock_class(struct bt_trace *trace,
592 struct bt_clock_class *clock_class)
593{
594 int ret;
595
596 if (trace->is_static) {
597 BT_LOGW("Invalid parameter: trace is static: "
598 "addr=%p, name=\"%s\"",
599 trace, bt_trace_get_name(trace));
600 ret = -1;
601 goto end;
602 }
603
604 ret = bt_trace_common_add_clock_class(BT_TO_COMMON(trace),
605 clock_class);
606
607end:
608 return ret;
609}
610
50842bdc 611int64_t bt_trace_get_clock_class_count(struct bt_trace *trace)
884cd6c3 612{
3dca2276 613 return bt_trace_common_get_clock_class_count(BT_TO_COMMON(trace));
884cd6c3
JG
614}
615
094ff7c0 616struct bt_clock_class *bt_trace_borrow_clock_class_by_index(
50842bdc 617 struct bt_trace *trace, uint64_t index)
884cd6c3 618{
094ff7c0 619 return bt_trace_common_borrow_clock_class_by_index(
3dca2276 620 BT_TO_COMMON(trace), index);
884cd6c3
JG
621}
622
e011d2c1 623static
3dca2276
PP
624bool packet_header_field_type_is_valid(struct bt_trace_common *trace,
625 struct bt_field_type_common *packet_header_type)
e011d2c1
PP
626{
627 int ret;
628 bool is_valid = true;
3dca2276 629 struct bt_field_type_common *field_type = NULL;
e011d2c1
PP
630
631 if (!packet_header_type) {
632 /*
633 * No packet header field type: trace must have only
634 * one stream. At this point the stream class being
635 * added is not part of the trace yet, so we validate
636 * that the trace contains no stream classes yet.
637 */
638 if (trace->stream_classes->len >= 1) {
639 BT_LOGW_STR("Invalid packet header field type: "
640 "packet header field type does not exist but there's more than one stream class in the trace.");
641 goto invalid;
642 }
643
644 /* No packet header field type: valid at this point */
645 goto end;
646 }
647
648 /* Packet header field type, if it exists, must be a structure */
3dca2276 649 if (packet_header_type->id != BT_FIELD_TYPE_ID_STRUCT) {
e011d2c1
PP
650 BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
651 "ft-addr=%p, ft-id=%s",
652 packet_header_type,
3dca2276 653 bt_common_field_type_id_string(packet_header_type->id));
e011d2c1
PP
654 goto invalid;
655 }
656
657 /*
658 * If there's a `magic` field, it must be a 32-bit unsigned
659 * integer field type. Also it must be the first field of the
660 * packet header field type.
661 */
094ff7c0 662 field_type = bt_field_type_common_structure_borrow_field_type_by_name(
e011d2c1
PP
663 packet_header_type, "magic");
664 if (field_type) {
665 const char *field_name;
666
3dca2276 667 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
668 BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
669 "magic-ft-addr=%p, magic-ft-id=%s",
670 field_type,
3dca2276 671 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
672 goto invalid;
673 }
674
3dca2276 675 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
676 BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
677 "magic-ft-addr=%p", field_type);
678 goto invalid;
679 }
680
3dca2276 681 if (bt_field_type_common_integer_get_size(field_type) != 32) {
e011d2c1
PP
682 BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
683 "magic-ft-addr=%p, magic-ft-size=%u",
684 field_type,
3dca2276 685 bt_field_type_common_integer_get_size(field_type));
e011d2c1
PP
686 goto invalid;
687 }
688
094ff7c0 689 ret = bt_field_type_common_structure_borrow_field_by_index(
e011d2c1 690 packet_header_type, &field_name, NULL, 0);
f6ccaed9 691 BT_ASSERT(ret == 0);
e011d2c1
PP
692
693 if (strcmp(field_name, "magic") != 0) {
694 BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
695 "magic-ft-addr=%p, first-field-name=\"%s\"",
696 field_type, field_name);
697 goto invalid;
698 }
e011d2c1
PP
699 }
700
701 /*
702 * If there's a `uuid` field, it must be an array field type of
703 * length 16 with an 8-bit unsigned integer element field type.
704 */
094ff7c0 705 field_type = bt_field_type_common_structure_borrow_field_type_by_name(
e011d2c1
PP
706 packet_header_type, "uuid");
707 if (field_type) {
3dca2276 708 struct bt_field_type_common *elem_ft;
e011d2c1 709
3dca2276 710 if (field_type->id != BT_FIELD_TYPE_ID_ARRAY) {
e011d2c1
PP
711 BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
712 "uuid-ft-addr=%p, uuid-ft-id=%s",
713 field_type,
3dca2276 714 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
715 goto invalid;
716 }
717
3dca2276 718 if (bt_field_type_common_array_get_length(field_type) != 16) {
e011d2c1
PP
719 BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
720 "uuid-ft-addr=%p, uuid-ft-length=%" PRId64,
721 field_type,
3dca2276 722 bt_field_type_common_array_get_length(field_type));
e011d2c1
PP
723 goto invalid;
724 }
725
094ff7c0 726 elem_ft = bt_field_type_common_array_borrow_element_field_type(field_type);
f6ccaed9 727 BT_ASSERT(elem_ft);
e011d2c1 728
3dca2276 729 if (elem_ft->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
730 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
731 "elem-ft-addr=%p, elem-ft-id=%s",
732 elem_ft,
3dca2276 733 bt_common_field_type_id_string(elem_ft->id));
e011d2c1
PP
734 goto invalid;
735 }
736
3dca2276 737 if (bt_field_type_common_integer_is_signed(elem_ft)) {
e011d2c1
PP
738 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
739 "elem-ft-addr=%p", elem_ft);
e011d2c1
PP
740 goto invalid;
741 }
742
3dca2276 743 if (bt_field_type_common_integer_get_size(elem_ft) != 8) {
e011d2c1
PP
744 BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
745 "elem-ft-addr=%p, elem-ft-size=%u",
746 elem_ft,
3dca2276 747 bt_field_type_common_integer_get_size(elem_ft));
e011d2c1
PP
748 goto invalid;
749 }
e011d2c1
PP
750 }
751
752 /*
753 * The `stream_id` field must exist if there's more than one
754 * stream classes in the trace.
755 */
094ff7c0 756 field_type = bt_field_type_common_structure_borrow_field_type_by_name(
e011d2c1
PP
757 packet_header_type, "stream_id");
758
759 if (!field_type && trace->stream_classes->len >= 1) {
760 BT_LOGW_STR("Invalid packet header field type: "
761 "`stream_id` field does not exist but there's more than one stream class in the trace.");
762 goto invalid;
763 }
764
765 /*
766 * If there's a `stream_id` field, it must be an unsigned
767 * integer field type.
768 */
769 if (field_type) {
3dca2276 770 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
771 BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
772 "stream-id-ft-addr=%p, stream-id-ft-id=%s",
773 field_type,
3dca2276 774 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
775 goto invalid;
776 }
777
3dca2276 778 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
779 BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
780 "stream-id-ft-addr=%p", field_type);
781 goto invalid;
782 }
e011d2c1
PP
783 }
784
708ab211
PP
785 /*
786 * If there's a `packet_seq_num` field, it must be an unsigned
787 * integer field type.
788 */
094ff7c0 789 field_type = bt_field_type_common_structure_borrow_field_type_by_name(
708ab211
PP
790 packet_header_type, "packet_seq_num");
791 if (field_type) {
3dca2276 792 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
708ab211
PP
793 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
794 "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
795 field_type,
3dca2276 796 bt_common_field_type_id_string(field_type->id));
708ab211
PP
797 goto invalid;
798 }
799
3dca2276 800 if (bt_field_type_common_integer_is_signed(field_type)) {
708ab211
PP
801 BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
802 "packet-seq-num-ft-addr=%p", field_type);
803 goto invalid;
804 }
708ab211
PP
805 }
806
e011d2c1
PP
807 goto end;
808
809invalid:
810 is_valid = false;
811
812end:
e011d2c1
PP
813 return is_valid;
814}
815
816static
3dca2276
PP
817bool packet_context_field_type_is_valid(struct bt_trace_common *trace,
818 struct bt_stream_class_common *stream_class,
819 struct bt_field_type_common *packet_context_type,
820 bool check_ts_begin_end_mapped)
e011d2c1
PP
821{
822 bool is_valid = true;
3dca2276 823 struct bt_field_type_common *field_type = NULL;
e011d2c1
PP
824
825 if (!packet_context_type) {
826 /* No packet context field type: valid at this point */
827 goto end;
828 }
829
830 /* Packet context field type, if it exists, must be a structure */
3dca2276 831 if (packet_context_type->id != BT_FIELD_TYPE_ID_STRUCT) {
e011d2c1
PP
832 BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: "
833 "ft-addr=%p, ft-id=%s",
834 packet_context_type,
3dca2276 835 bt_common_field_type_id_string(packet_context_type->id));
e011d2c1
PP
836 goto invalid;
837 }
838
839 /*
840 * If there's a `packet_size` field, it must be an unsigned
841 * integer field type.
842 */
094ff7c0 843 field_type = bt_field_type_common_structure_borrow_field_type_by_name(
e011d2c1
PP
844 packet_context_type, "packet_size");
845 if (field_type) {
3dca2276 846 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
847 BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: "
848 "packet-size-ft-addr=%p, packet-size-ft-id=%s",
849 field_type,
3dca2276 850 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
851 goto invalid;
852 }
853
3dca2276 854 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
855 BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: "
856 "packet-size-ft-addr=%p", field_type);
857 goto invalid;
858 }
e011d2c1
PP
859 }
860
861 /*
862 * If there's a `content_size` field, it must be an unsigned
863 * integer field type.
864 */
094ff7c0 865 field_type = bt_field_type_common_structure_borrow_field_type_by_name(
e011d2c1
PP
866 packet_context_type, "content_size");
867 if (field_type) {
3dca2276 868 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
869 BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: "
870 "content-size-ft-addr=%p, content-size-ft-id=%s",
871 field_type,
3dca2276 872 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
873 goto invalid;
874 }
875
3dca2276 876 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
877 BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: "
878 "content-size-ft-addr=%p", field_type);
879 goto invalid;
880 }
e011d2c1
PP
881 }
882
883 /*
884 * If there's a `events_discarded` field, it must be an unsigned
885 * integer field type.
886 */
094ff7c0 887 field_type = bt_field_type_common_structure_borrow_field_type_by_name(
e011d2c1
PP
888 packet_context_type, "events_discarded");
889 if (field_type) {
3dca2276 890 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
891 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: "
892 "events-discarded-ft-addr=%p, events-discarded-ft-id=%s",
893 field_type,
3dca2276 894 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
895 goto invalid;
896 }
897
3dca2276 898 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
899 BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: "
900 "events-discarded-ft-addr=%p", field_type);
901 goto invalid;
902 }
e011d2c1
PP
903 }
904
905 /*
906 * If there's a `timestamp_begin` field, it must be an unsigned
907 * integer field type. Also, if the trace is not a CTF writer's
908 * trace, then we cannot automatically set the mapped clock
909 * class of this field, so it must have a mapped clock class.
910 */
094ff7c0 911 field_type = bt_field_type_common_structure_borrow_field_type_by_name(
e011d2c1
PP
912 packet_context_type, "timestamp_begin");
913 if (field_type) {
3dca2276 914 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
915 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: "
916 "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s",
917 field_type,
3dca2276 918 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
919 goto invalid;
920 }
921
3dca2276 922 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
923 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: "
924 "timestamp-begin-ft-addr=%p", field_type);
925 goto invalid;
926 }
927
3dca2276 928 if (check_ts_begin_end_mapped) {
50842bdc 929 struct bt_clock_class *clock_class =
094ff7c0 930 bt_field_type_common_integer_borrow_mapped_clock_class(
e011d2c1
PP
931 field_type);
932
e011d2c1
PP
933 if (!clock_class) {
934 BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: "
935 "timestamp-begin-ft-addr=%p", field_type);
936 goto invalid;
937 }
938 }
e011d2c1
PP
939 }
940
941 /*
942 * If there's a `timestamp_end` 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 */
094ff7c0 947 field_type = bt_field_type_common_structure_borrow_field_type_by_name(
e011d2c1
PP
948 packet_context_type, "timestamp_end");
949 if (field_type) {
3dca2276 950 if (field_type->id != BT_FIELD_TYPE_ID_INTEGER) {
e011d2c1
PP
951 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: "
952 "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s",
953 field_type,
3dca2276 954 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
955 goto invalid;
956 }
957
3dca2276 958 if (bt_field_type_common_integer_is_signed(field_type)) {
e011d2c1
PP
959 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: "
960 "timestamp-end-ft-addr=%p", field_type);
961 goto invalid;
962 }
963
3dca2276 964 if (check_ts_begin_end_mapped) {
50842bdc 965 struct bt_clock_class *clock_class =
094ff7c0 966 bt_field_type_common_integer_borrow_mapped_clock_class(
e011d2c1
PP
967 field_type);
968
e011d2c1
PP
969 if (!clock_class) {
970 BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: "
971 "timestamp-end-ft-addr=%p", field_type);
972 goto invalid;
973 }
974 }
e011d2c1
PP
975 }
976
977 goto end;
978
979invalid:
980 is_valid = false;
981
982end:
e011d2c1
PP
983 return is_valid;
984}
985
986static
3dca2276
PP
987bool event_header_field_type_is_valid(struct bt_trace_common *trace,
988 struct bt_stream_class_common *stream_class,
989 struct bt_field_type_common *event_header_type)
e011d2c1
PP
990{
991 bool is_valid = true;
3dca2276 992 struct bt_field_type_common *field_type = NULL;
e011d2c1
PP
993
994 /*
995 * We do not validate that the `timestamp` field exists here
996 * because CTF does not require this exact name to be mapped to
997 * a clock class.
998 */
999
1000 if (!event_header_type) {
1001 /*
1002 * No event header field type: stream class must have
1003 * only one event class.
1004 */
3dca2276 1005 if (bt_stream_class_common_get_event_class_count(stream_class) > 1) {
e011d2c1
PP
1006 BT_LOGW_STR("Invalid event header field type: "
1007 "event header field type does not exist but there's more than one event class in the stream class.");
1008 goto invalid;
1009 }
1010
1011 /* No event header field type: valid at this point */
1012 goto end;
1013 }
1014
1015 /* Event header field type, if it exists, must be a structure */
3dca2276 1016 if (event_header_type->id != BT_FIELD_TYPE_ID_STRUCT) {
e011d2c1
PP
1017 BT_LOGW("Invalid event header field type: must be a structure field type if it exists: "
1018 "ft-addr=%p, ft-id=%s",
1019 event_header_type,
3dca2276 1020 bt_common_field_type_id_string(event_header_type->id));
e011d2c1
PP
1021 goto invalid;
1022 }
1023
1024 /*
1025 * If there's an `id` field, it must be an unsigned integer
1026 * field type or an enumeration field type with an unsigned
1027 * integer container field type.
1028 */
094ff7c0 1029 field_type = bt_field_type_common_structure_borrow_field_type_by_name(
e011d2c1
PP
1030 event_header_type, "id");
1031 if (field_type) {
3dca2276 1032 struct bt_field_type_common *int_ft;
e011d2c1 1033
3dca2276 1034 if (field_type->id == BT_FIELD_TYPE_ID_INTEGER) {
094ff7c0 1035 int_ft = field_type;
3dca2276 1036 } else if (field_type->id == BT_FIELD_TYPE_ID_ENUM) {
094ff7c0 1037 int_ft = bt_field_type_common_enumeration_borrow_container_field_type(
e011d2c1
PP
1038 field_type);
1039 } else {
1040 BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: "
1041 "id-ft-addr=%p, id-ft-id=%s",
1042 field_type,
3dca2276 1043 bt_common_field_type_id_string(field_type->id));
e011d2c1
PP
1044 goto invalid;
1045 }
1046
f6ccaed9 1047 BT_ASSERT(int_ft);
3dca2276 1048 if (bt_field_type_common_integer_is_signed(int_ft)) {
e011d2c1
PP
1049 BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: "
1050 "id-ft-addr=%p", int_ft);
1051 goto invalid;
1052 }
e011d2c1
PP
1053 }
1054
1055 goto end;
1056
1057invalid:
1058 is_valid = false;
1059
1060end:
e011d2c1
PP
1061 return is_valid;
1062}
1063
726106d3 1064static
3dca2276 1065int check_packet_header_type_has_no_clock_class(struct bt_trace_common *trace)
726106d3
PP
1066{
1067 int ret = 0;
1068
3dca2276 1069 if (trace->packet_header_field_type) {
726106d3
PP
1070 struct bt_clock_class *clock_class = NULL;
1071
3dca2276
PP
1072 ret = bt_field_type_common_validate_single_clock_class(
1073 trace->packet_header_field_type,
726106d3
PP
1074 &clock_class);
1075 bt_put(clock_class);
1076 if (ret || clock_class) {
1077 BT_LOGW("Trace's packet header field type cannot "
1078 "contain a field type which is mapped to "
1079 "a clock class: "
1080 "trace-addr=%p, trace-name=\"%s\", "
1081 "clock-class-name=\"%s\"",
3dca2276 1082 trace, bt_trace_common_get_name(trace),
726106d3
PP
1083 clock_class ?
1084 bt_clock_class_get_name(clock_class) :
1085 NULL);
1086 ret = -1;
1087 }
1088 }
1089
1090 return ret;
1091}
1092
3dca2276
PP
1093BT_HIDDEN
1094int bt_trace_common_add_stream_class(struct bt_trace_common *trace,
1095 struct bt_stream_class_common *stream_class,
1096 bt_validation_flag_copy_field_type_func copy_field_type_func,
1097 struct bt_clock_class *init_expected_clock_class,
1098 int (*map_clock_classes_func)(struct bt_stream_class_common *stream_class,
1099 struct bt_field_type_common *packet_context_field_type,
1100 struct bt_field_type_common *event_header_field_type),
1101 bool check_ts_begin_end_mapped)
ef0c4a15 1102{
544d0515
PP
1103 int ret;
1104 int64_t i;
ef0c4a15 1105 int64_t stream_id;
50842bdc
PP
1106 struct bt_validation_output trace_sc_validation_output = { 0 };
1107 struct bt_validation_output *ec_validation_outputs = NULL;
1108 const enum bt_validation_flag trace_sc_validation_flags =
1109 BT_VALIDATION_FLAG_TRACE |
1110 BT_VALIDATION_FLAG_STREAM;
1111 const enum bt_validation_flag ec_validation_flags =
1112 BT_VALIDATION_FLAG_EVENT;
3dca2276
PP
1113 struct bt_field_type_common *packet_header_type = NULL;
1114 struct bt_field_type_common *packet_context_type = NULL;
1115 struct bt_field_type_common *event_header_type = NULL;
1116 struct bt_field_type_common *stream_event_ctx_type = NULL;
544d0515 1117 int64_t event_class_count;
3dca2276
PP
1118 struct bt_trace_common *current_parent_trace = NULL;
1119 struct bt_clock_class *expected_clock_class =
1120 bt_get(init_expected_clock_class);
1121
1122 BT_ASSERT(copy_field_type_func);
ef0c4a15 1123
95c09b3a
PP
1124 if (!trace) {
1125 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1126 ret = -1;
1127 goto end;
1128 }
1129
1130 if (!stream_class) {
1131 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1132 ret = -1;
1133 goto end;
1134 }
1135
95c09b3a
PP
1136 BT_LOGD("Adding stream class to trace: "
1137 "trace-addr=%p, trace-name=\"%s\", "
1138 "stream-class-addr=%p, stream-class-name=\"%s\", "
1139 "stream-class-id=%" PRId64,
3dca2276
PP
1140 trace, bt_trace_common_get_name(trace),
1141 stream_class, bt_stream_class_common_get_name(stream_class),
1142 bt_stream_class_common_get_id(stream_class));
95c09b3a 1143
094ff7c0 1144 current_parent_trace = bt_stream_class_common_borrow_trace(stream_class);
2789e5d9
JG
1145 if (current_parent_trace) {
1146 /* Stream class is already associated to a trace, abort. */
95c09b3a
PP
1147 BT_LOGW("Invalid parameter: stream class is already part of a trace: "
1148 "stream-class-trace-addr=%p, "
1149 "stream-class-trace-name=\"%s\"",
1150 current_parent_trace,
3dca2276 1151 bt_trace_common_get_name(current_parent_trace));
2789e5d9
JG
1152 ret = -1;
1153 goto end;
1154 }
1155
09840de5 1156 event_class_count =
3dca2276 1157 bt_stream_class_common_get_event_class_count(stream_class);
f6ccaed9 1158 BT_ASSERT(event_class_count >= 0);
09840de5 1159
2a3ced3c
PP
1160 if (!stream_class->frozen) {
1161 /*
1162 * Stream class is not frozen yet. Validate that the
1163 * stream class contains at most a single clock class
1164 * because the previous
3dca2276
PP
1165 * bt_stream_class_common_add_event_class() calls did
1166 * not make this validation since the stream class's
1167 * direct field types (packet context, event header,
1168 * event context) could change afterwards. This stream
1169 * class is about to be frozen and those field types
1170 * won't be changed if this function succeeds.
2a3ced3c
PP
1171 *
1172 * At this point we're also sure that the stream class's
1173 * clock, if any, has the same class as the stream
1174 * class's expected clock class, if any. This is why, if
3dca2276 1175 * bt_stream_class_common_validate_single_clock_class()
2a3ced3c
PP
1176 * succeeds below, the call to
1177 * bt_stream_class_map_clock_class() at the end of this
1178 * function is safe because it maps to the same, single
1179 * clock class.
1180 */
3dca2276
PP
1181 ret = bt_stream_class_common_validate_single_clock_class(
1182 stream_class, &expected_clock_class);
2a3ced3c
PP
1183 if (ret) {
1184 BT_LOGW("Invalid parameter: stream class or one of its "
1185 "event classes contains a field type which is "
1186 "not recursively mapped to the expected "
1187 "clock class: "
1188 "stream-class-addr=%p, "
1189 "stream-class-id=%" PRId64 ", "
1190 "stream-class-name=\"%s\", "
1191 "expected-clock-class-addr=%p, "
1192 "expected-clock-class-name=\"%s\"",
3dca2276
PP
1193 stream_class, bt_stream_class_common_get_id(stream_class),
1194 bt_stream_class_common_get_name(stream_class),
2a3ced3c
PP
1195 expected_clock_class,
1196 expected_clock_class ?
1197 bt_clock_class_get_name(expected_clock_class) :
1198 NULL);
1199 goto end;
1200 }
fe13b5c0
PP
1201 }
1202
726106d3
PP
1203 ret = check_packet_header_type_has_no_clock_class(trace);
1204 if (ret) {
1205 /* check_packet_header_type_has_no_clock_class() logs errors */
1206 goto end;
1207 }
1208
09840de5
PP
1209 /*
1210 * We're about to freeze both the trace and the stream class.
1211 * Also, each event class contained in this stream class are
1212 * already frozen.
1213 *
1214 * This trace, this stream class, and all its event classes
1215 * should be valid at this point.
1216 *
1217 * Validate trace and stream class first, then each event
1218 * class of this stream class can be validated individually.
1219 */
1220 packet_header_type =
094ff7c0 1221 bt_trace_common_borrow_packet_header_field_type(trace);
09840de5 1222 packet_context_type =
094ff7c0 1223 bt_stream_class_common_borrow_packet_context_field_type(stream_class);
09840de5 1224 event_header_type =
094ff7c0 1225 bt_stream_class_common_borrow_event_header_field_type(stream_class);
09840de5 1226 stream_event_ctx_type =
094ff7c0 1227 bt_stream_class_common_borrow_event_context_field_type(stream_class);
95c09b3a
PP
1228
1229 BT_LOGD("Validating trace and stream class field types.");
50842bdc 1230 ret = bt_validate_class_types(trace->environment,
09840de5
PP
1231 packet_header_type, packet_context_type, event_header_type,
1232 stream_event_ctx_type, NULL, NULL, trace->valid,
1233 stream_class->valid, 1, &trace_sc_validation_output,
3dca2276 1234 trace_sc_validation_flags, copy_field_type_func);
09840de5 1235
2bd7f864 1236 if (ret) {
09840de5
PP
1237 /*
1238 * This means something went wrong during the validation
1239 * process, not that the objects are invalid.
1240 */
95c09b3a
PP
1241 BT_LOGE("Failed to validate trace and stream class field types: "
1242 "ret=%d", ret);
2bd7f864
JG
1243 goto end;
1244 }
1245
09840de5
PP
1246 if ((trace_sc_validation_output.valid_flags &
1247 trace_sc_validation_flags) !=
1248 trace_sc_validation_flags) {
1249 /* Invalid trace/stream class */
95c09b3a
PP
1250 BT_LOGW("Invalid trace or stream class field types: "
1251 "valid-flags=0x%x",
1252 trace_sc_validation_output.valid_flags);
09840de5
PP
1253 ret = -1;
1254 goto end;
1255 }
1256
1257 if (event_class_count > 0) {
50842bdc 1258 ec_validation_outputs = g_new0(struct bt_validation_output,
09840de5
PP
1259 event_class_count);
1260 if (!ec_validation_outputs) {
95c09b3a 1261 BT_LOGE_STR("Failed to allocate one validation output structure.");
09840de5
PP
1262 ret = -1;
1263 goto end;
1264 }
1265 }
1266
1267 /* Validate each event class individually */
6474e016 1268 for (i = 0; i < event_class_count; i++) {
3dca2276 1269 struct bt_event_class_common *event_class =
094ff7c0 1270 bt_stream_class_common_borrow_event_class_by_index(
9ac68eb1 1271 stream_class, i);
3dca2276
PP
1272 struct bt_field_type_common *event_context_type = NULL;
1273 struct bt_field_type_common *event_payload_type = NULL;
09840de5
PP
1274
1275 event_context_type =
094ff7c0
PP
1276 bt_event_class_common_borrow_context_field_type(
1277 event_class);
09840de5 1278 event_payload_type =
094ff7c0
PP
1279 bt_event_class_common_borrow_payload_field_type(
1280 event_class);
09840de5
PP
1281
1282 /*
1283 * It is important to use the field types returned by
1284 * the previous trace and stream class validation here
1285 * because copies could have been made.
1286 */
95c09b3a
PP
1287 BT_LOGD("Validating event class's field types: "
1288 "addr=%p, name=\"%s\", id=%" PRId64,
3dca2276
PP
1289 event_class, bt_event_class_common_get_name(event_class),
1290 bt_event_class_common_get_id(event_class));
50842bdc 1291 ret = bt_validate_class_types(trace->environment,
09840de5
PP
1292 trace_sc_validation_output.packet_header_type,
1293 trace_sc_validation_output.packet_context_type,
1294 trace_sc_validation_output.event_header_type,
1295 trace_sc_validation_output.stream_event_ctx_type,
1296 event_context_type, event_payload_type,
1297 1, 1, event_class->valid, &ec_validation_outputs[i],
3dca2276 1298 ec_validation_flags, copy_field_type_func);
09840de5
PP
1299
1300 if (ret) {
95c09b3a
PP
1301 BT_LOGE("Failed to validate event class field types: "
1302 "ret=%d", ret);
09840de5
PP
1303 goto end;
1304 }
1305
1306 if ((ec_validation_outputs[i].valid_flags &
1307 ec_validation_flags) != ec_validation_flags) {
1308 /* Invalid event class */
95c09b3a
PP
1309 BT_LOGW("Invalid event class field types: "
1310 "valid-flags=0x%x",
1311 ec_validation_outputs[i].valid_flags);
09840de5
PP
1312 ret = -1;
1313 goto end;
1314 }
1315 }
1316
3dca2276 1317 stream_id = bt_stream_class_common_get_id(stream_class);
ef0c4a15
JG
1318 if (stream_id < 0) {
1319 stream_id = trace->next_stream_id++;
9ac68eb1 1320 if (stream_id < 0) {
95c09b3a 1321 BT_LOGE_STR("No more stream class IDs available.");
9ac68eb1
PP
1322 ret = -1;
1323 goto end;
1324 }
ef0c4a15
JG
1325
1326 /* Try to assign a new stream id */
1327 for (i = 0; i < trace->stream_classes->len; i++) {
3dca2276 1328 if (stream_id == bt_stream_class_common_get_id(
ef0c4a15
JG
1329 trace->stream_classes->pdata[i])) {
1330 /* Duplicate stream id found */
95c09b3a
PP
1331 BT_LOGW("Duplicate stream class ID: "
1332 "id=%" PRId64, (int64_t) stream_id);
ef0c4a15
JG
1333 ret = -1;
1334 goto end;
1335 }
1336 }
1337
3dca2276 1338 if (bt_stream_class_common_set_id_no_check(stream_class,
95c09b3a 1339 stream_id)) {
ef0c4a15 1340 /* TODO Should retry with a different stream id */
95c09b3a
PP
1341 BT_LOGE("Cannot set stream class's ID: "
1342 "id=%" PRId64, (int64_t) stream_id);
ef0c4a15
JG
1343 ret = -1;
1344 goto end;
1345 }
1346 }
1347
e011d2c1
PP
1348 /*
1349 * At this point all the field types in the validation output
1350 * are valid. Validate the semantics of some scopes according to
1351 * the CTF specification.
1352 */
1353 if (!packet_header_field_type_is_valid(trace,
1354 trace_sc_validation_output.packet_header_type)) {
95c09b3a 1355 BT_LOGW_STR("Invalid trace's packet header field type.");
e011d2c1
PP
1356 ret = -1;
1357 goto end;
1358 }
1359
1360 if (!packet_context_field_type_is_valid(trace,
1361 stream_class,
3dca2276
PP
1362 trace_sc_validation_output.packet_context_type,
1363 check_ts_begin_end_mapped)) {
95c09b3a 1364 BT_LOGW_STR("Invalid stream class's packet context field type.");
e011d2c1
PP
1365 ret = -1;
1366 goto end;
1367 }
1368
1369 if (!event_header_field_type_is_valid(trace,
1370 stream_class,
1371 trace_sc_validation_output.event_header_type)) {
95c09b3a 1372 BT_LOGW_STR("Invalid steam class's event header field type.");
e011d2c1
PP
1373 ret = -1;
1374 goto end;
1375 }
1376
1377 /*
1378 * Now is the time to automatically map specific field types of
1379 * the stream class's packet context and event header field
1380 * types to the stream class's clock's class if they are not
1381 * mapped to a clock class yet. We do it here because we know
1382 * that after this point, everything is frozen so it won't be
1383 * possible for the user to modify the stream class's clock, or
1384 * to map those field types to other clock classes.
1385 */
3dca2276
PP
1386 if (map_clock_classes_func) {
1387 if (map_clock_classes_func(stream_class,
e011d2c1
PP
1388 trace_sc_validation_output.packet_context_type,
1389 trace_sc_validation_output.event_header_type)) {
3dca2276 1390 /* map_clock_classes_func() logs errors */
e011d2c1
PP
1391 ret = -1;
1392 goto end;
1393 }
1394 }
1395
e6a8e8e4 1396 bt_object_set_parent(stream_class, trace);
ef0c4a15
JG
1397 g_ptr_array_add(trace->stream_classes, stream_class);
1398
1399 /*
09840de5
PP
1400 * At this point we know that the function will be successful.
1401 * Therefore we can replace the trace and stream class field
1402 * types with what's in their validation output structure and
1403 * mark them as valid. We can also replace the field types of
1404 * all the event classes of the stream class and mark them as
1405 * valid.
1406 */
50842bdc 1407 bt_validation_replace_types(trace, stream_class, NULL,
09840de5
PP
1408 &trace_sc_validation_output, trace_sc_validation_flags);
1409 trace->valid = 1;
1410 stream_class->valid = 1;
1411
1412 /*
50842bdc 1413 * Put what was not moved in bt_validation_replace_types().
09840de5 1414 */
50842bdc 1415 bt_validation_output_put_types(&trace_sc_validation_output);
09840de5 1416
6474e016 1417 for (i = 0; i < event_class_count; i++) {
3dca2276 1418 struct bt_event_class_common *event_class =
094ff7c0 1419 bt_stream_class_common_borrow_event_class_by_index(
9ac68eb1 1420 stream_class, i);
09840de5 1421
50842bdc 1422 bt_validation_replace_types(NULL, NULL, event_class,
09840de5
PP
1423 &ec_validation_outputs[i], ec_validation_flags);
1424 event_class->valid = 1;
09840de5
PP
1425
1426 /*
1427 * Put what was not moved in
50842bdc 1428 * bt_validation_replace_types().
09840de5 1429 */
50842bdc 1430 bt_validation_output_put_types(&ec_validation_outputs[i]);
09840de5
PP
1431 }
1432
09840de5
PP
1433 /*
1434 * Freeze the trace and the stream class.
1435 */
3dca2276
PP
1436 bt_stream_class_common_freeze(stream_class);
1437 bt_trace_common_freeze(trace);
09840de5 1438
2a3ced3c
PP
1439 /*
1440 * It is safe to set the stream class's unique clock class
1441 * now because the stream class is frozen.
1442 */
1443 if (expected_clock_class) {
1444 BT_MOVE(stream_class->clock_class, expected_clock_class);
1445 }
1446
95c09b3a
PP
1447 BT_LOGD("Added stream class to trace: "
1448 "trace-addr=%p, trace-name=\"%s\", "
1449 "stream-class-addr=%p, stream-class-name=\"%s\", "
1450 "stream-class-id=%" PRId64,
3dca2276
PP
1451 trace, bt_trace_common_get_name(trace),
1452 stream_class, bt_stream_class_common_get_name(stream_class),
1453 bt_stream_class_common_get_id(stream_class));
95c09b3a 1454
ef0c4a15 1455end:
d3814b54 1456 if (ret) {
e6a8e8e4 1457 bt_object_set_parent(stream_class, NULL);
09840de5
PP
1458
1459 if (ec_validation_outputs) {
6474e016 1460 for (i = 0; i < event_class_count; i++) {
50842bdc 1461 bt_validation_output_put_types(
09840de5
PP
1462 &ec_validation_outputs[i]);
1463 }
1464 }
d3814b54 1465 }
09840de5
PP
1466
1467 g_free(ec_validation_outputs);
50842bdc 1468 bt_validation_output_put_types(&trace_sc_validation_output);
2a3ced3c 1469 bt_put(expected_clock_class);
ef0c4a15
JG
1470 return ret;
1471}
1472
3dca2276
PP
1473int bt_trace_add_stream_class(struct bt_trace *trace,
1474 struct bt_stream_class *stream_class)
1475{
1476 int ret = 0;
1477
1478 if (!trace) {
1479 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1480 ret = -1;
1481 goto end;
1482 }
1483
1484 if (trace->is_static) {
1485 BT_LOGW_STR("Invalid parameter: trace is static.");
1486 ret = -1;
1487 goto end;
1488 }
1489
1490 ret = bt_trace_common_add_stream_class(BT_TO_COMMON(trace),
1491 BT_TO_COMMON(stream_class),
1492 (bt_validation_flag_copy_field_type_func) bt_field_type_copy,
1493 NULL, NULL, true);
1494 if (ret) {
1495 goto end;
1496 }
1497
1498 /* Notifiy listeners of the trace's schema modification. */
1499 bt_stream_class_common_visit(BT_TO_COMMON(stream_class),
1500 bt_trace_object_modification, trace);
1501
1502end:
1503 return ret;
1504}
1505
50842bdc 1506int64_t bt_trace_get_stream_count(struct bt_trace *trace)
c1e730fe 1507{
3dca2276 1508 return bt_trace_common_get_stream_count(BT_TO_COMMON(trace));
c1e730fe
PP
1509}
1510
094ff7c0
PP
1511struct bt_stream *bt_trace_borrow_stream_by_index(
1512 struct bt_trace *trace, uint64_t index)
c1e730fe 1513{
094ff7c0 1514 return BT_FROM_COMMON(bt_trace_common_borrow_stream_by_index(
3dca2276 1515 BT_TO_COMMON(trace), index));
c1e730fe
PP
1516}
1517
50842bdc 1518int64_t bt_trace_get_stream_class_count(struct bt_trace *trace)
ef0c4a15 1519{
3dca2276 1520 return bt_trace_common_get_stream_class_count(BT_TO_COMMON(trace));
ef0c4a15
JG
1521}
1522
094ff7c0 1523struct bt_stream_class *bt_trace_borrow_stream_class_by_index(
50842bdc 1524 struct bt_trace *trace, uint64_t index)
ef0c4a15 1525{
094ff7c0 1526 return BT_FROM_COMMON(bt_trace_common_borrow_stream_class_by_index(
3dca2276 1527 BT_TO_COMMON(trace), index));
ef0c4a15
JG
1528}
1529
094ff7c0 1530struct bt_stream_class *bt_trace_borrow_stream_class_by_id(
3dca2276 1531 struct bt_trace *trace, uint64_t id)
4841ccc1 1532{
3dca2276 1533 return BT_FROM_COMMON(
094ff7c0 1534 bt_trace_common_borrow_stream_class_by_id(
3dca2276 1535 BT_TO_COMMON(trace), id));
4841ccc1
PP
1536}
1537
094ff7c0 1538struct bt_clock_class *bt_trace_borrow_clock_class_by_name(
50842bdc 1539 struct bt_trace *trace, const char *name)
7e4347a3 1540{
094ff7c0 1541 return bt_trace_common_borrow_clock_class_by_name(BT_TO_COMMON(trace),
3dca2276 1542 name);
7e4347a3
PP
1543}
1544
c9d90a34 1545BT_HIDDEN
3dca2276 1546bt_bool bt_trace_common_has_clock_class(struct bt_trace_common *trace,
50842bdc 1547 struct bt_clock_class *clock_class)
c9d90a34
PP
1548{
1549 struct search_query query = { .value = clock_class, .found = 0 };
1550
f6ccaed9
PP
1551 BT_ASSERT(trace);
1552 BT_ASSERT(clock_class);
c9d90a34 1553
3dca2276 1554 g_ptr_array_foreach(trace->clock_classes, value_exists, &query);
c9d90a34
PP
1555 return query.found;
1556}
1557
50842bdc
PP
1558enum bt_byte_order bt_trace_get_native_byte_order(
1559 struct bt_trace *trace)
4ed90fb3 1560{
3dca2276 1561 return bt_trace_common_get_native_byte_order(BT_TO_COMMON(trace));
4ed90fb3
JG
1562}
1563
3dca2276
PP
1564BT_HIDDEN
1565int bt_trace_common_set_native_byte_order(struct bt_trace_common *trace,
1566 enum bt_byte_order byte_order, bool allow_unspecified)
bc37ae52
JG
1567{
1568 int ret = 0;
bc37ae52 1569
95c09b3a
PP
1570 if (!trace) {
1571 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1572 ret = -1;
1573 goto end;
1574 }
1575
1576 if (trace->frozen) {
1577 BT_LOGW("Invalid parameter: trace is frozen: "
1578 "addr=%p, name=\"%s\"",
3dca2276 1579 trace, bt_trace_common_get_name(trace));
bc37ae52
JG
1580 ret = -1;
1581 goto end;
1582 }
1583
3dca2276
PP
1584 if (byte_order == BT_BYTE_ORDER_UNSPECIFIED && !allow_unspecified) {
1585 BT_LOGW("Invalid parameter: BT_BYTE_ORDER_UNSPECIFIED byte order is not allowed: "
d41cff38 1586 "addr=%p, name=\"%s\"",
3dca2276 1587 trace, bt_trace_common_get_name(trace));
d41cff38
PP
1588 ret = -1;
1589 goto end;
1590 }
1591
50842bdc
PP
1592 if (byte_order != BT_BYTE_ORDER_LITTLE_ENDIAN &&
1593 byte_order != BT_BYTE_ORDER_BIG_ENDIAN &&
1594 byte_order != BT_BYTE_ORDER_NETWORK) {
95c09b3a
PP
1595 BT_LOGW("Invalid parameter: invalid byte order: "
1596 "addr=%p, name=\"%s\", bo=%s",
3dca2276
PP
1597 trace, bt_trace_common_get_name(trace),
1598 bt_common_byte_order_string(byte_order));
bc37ae52
JG
1599 ret = -1;
1600 goto end;
1601 }
1602
dc3fffef 1603 trace->native_byte_order = byte_order;
95c09b3a
PP
1604 BT_LOGV("Set trace's native byte order: "
1605 "addr=%p, name=\"%s\", bo=%s",
3dca2276
PP
1606 trace, bt_trace_common_get_name(trace),
1607 bt_common_byte_order_string(byte_order));
dc3fffef 1608
bc37ae52
JG
1609end:
1610 return ret;
1611}
1612
3dca2276
PP
1613int bt_trace_set_native_byte_order(struct bt_trace *trace,
1614 enum bt_byte_order byte_order)
1615{
1616 return bt_trace_common_set_native_byte_order(BT_TO_COMMON(trace),
1617 byte_order, true);
1618}
1619
094ff7c0 1620struct bt_field_type *bt_trace_borrow_packet_header_field_type(
50842bdc 1621 struct bt_trace *trace)
d246b111 1622{
094ff7c0 1623 return BT_FROM_COMMON(bt_trace_common_borrow_packet_header_field_type(
3dca2276 1624 BT_TO_COMMON(trace)));
d246b111
JG
1625}
1626
3dca2276
PP
1627BT_HIDDEN
1628int bt_trace_common_set_packet_header_field_type(struct bt_trace_common *trace,
1629 struct bt_field_type_common *packet_header_type)
d246b111
JG
1630{
1631 int ret = 0;
1632
95c09b3a
PP
1633 if (!trace) {
1634 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1635 ret = -1;
1636 goto end;
1637 }
1638
1639 if (trace->frozen) {
1640 BT_LOGW("Invalid parameter: trace is frozen: "
1641 "addr=%p, name=\"%s\"",
3dca2276 1642 trace, bt_trace_common_get_name(trace));
d246b111
JG
1643 ret = -1;
1644 goto end;
1645 }
1646
835b2d10
JG
1647 /* packet_header_type must be a structure. */
1648 if (packet_header_type &&
3dca2276 1649 packet_header_type->id != BT_FIELD_TYPE_ID_STRUCT) {
95c09b3a
PP
1650 BT_LOGW("Invalid parameter: packet header field type must be a structure field type if it exists: "
1651 "addr=%p, name=\"%s\", ft-addr=%p, ft-id=%s",
3dca2276 1652 trace, bt_trace_common_get_name(trace),
95c09b3a 1653 packet_header_type,
3dca2276 1654 bt_common_field_type_id_string(packet_header_type->id));
d246b111
JG
1655 ret = -1;
1656 goto end;
1657 }
1658
3dca2276
PP
1659 bt_put(trace->packet_header_field_type);
1660 trace->packet_header_field_type = bt_get(packet_header_type);
95c09b3a
PP
1661 BT_LOGV("Set trace's packet header field type: "
1662 "addr=%p, name=\"%s\", packet-context-ft-addr=%p",
3dca2276 1663 trace, bt_trace_common_get_name(trace), packet_header_type);
d246b111
JG
1664end:
1665 return ret;
1666}
1667
3dca2276
PP
1668int bt_trace_set_packet_header_field_type(struct bt_trace *trace,
1669 struct bt_field_type *packet_header_type)
1670{
1671 return bt_trace_common_set_packet_header_field_type(BT_TO_COMMON(trace),
1672 (void *) packet_header_type);
1673}
1674
8bf65fbd 1675static
544d0515 1676int64_t get_stream_class_count(void *element)
8bf65fbd 1677{
50842bdc
PP
1678 return bt_trace_get_stream_class_count(
1679 (struct bt_trace *) element);
8bf65fbd
JG
1680}
1681
1682static
1683void *get_stream_class(void *element, int i)
1684{
50842bdc
PP
1685 return bt_trace_get_stream_class_by_index(
1686 (struct bt_trace *) element, i);
8bf65fbd
JG
1687}
1688
1689static
50842bdc 1690int visit_stream_class(void *object, bt_visitor visitor,void *data)
8bf65fbd 1691{
50842bdc 1692 return bt_stream_class_visit(object, visitor, data);
8bf65fbd
JG
1693}
1694
50842bdc
PP
1695int bt_trace_visit(struct bt_trace *trace,
1696 bt_visitor visitor, void *data)
8bf65fbd
JG
1697{
1698 int ret;
50842bdc
PP
1699 struct bt_visitor_object obj = {
1700 .object = trace,
1701 .type = BT_VISITOR_OBJECT_TYPE_TRACE
1702 };
8bf65fbd 1703
95c09b3a
PP
1704 if (!trace) {
1705 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1706 ret = -1;
1707 goto end;
1708 }
1709
1710 if (!visitor) {
1711 BT_LOGW_STR("Invalid parameter: visitor is NULL.");
8bf65fbd
JG
1712 ret = -1;
1713 goto end;
1714 }
1715
95c09b3a 1716 BT_LOGV("Visiting trace: addr=%p, name=\"%s\"",
50842bdc 1717 trace, bt_trace_get_name(trace));
d9a13d86 1718 ret = visitor_helper(&obj, get_stream_class_count,
8bf65fbd
JG
1719 get_stream_class, visit_stream_class, visitor, data);
1720end:
1721 return ret;
1722}
1723
1724static
50842bdc 1725int invoke_listener(struct bt_visitor_object *object, void *data)
8bf65fbd 1726{
9b888ff3 1727 struct listener_wrapper *listener_wrapper = data;
8bf65fbd 1728
d9a13d86 1729 listener_wrapper->listener(object, listener_wrapper->data);
9b888ff3 1730 return 0;
8bf65fbd
JG
1731}
1732
95c09b3a 1733// TODO: add logging to this function once we use it internally.
50842bdc
PP
1734int bt_trace_add_listener(struct bt_trace *trace,
1735 bt_listener_cb listener, void *listener_data)
2204c381
JG
1736{
1737 int ret = 0;
50ad4244
JG
1738 struct listener_wrapper *listener_wrapper =
1739 g_new0(struct listener_wrapper, 1);
2204c381 1740
50ad4244 1741 if (!trace || !listener || !listener_wrapper) {
2204c381
JG
1742 ret = -1;
1743 goto error;
1744 }
1745
50ad4244
JG
1746 listener_wrapper->listener = listener;
1747 listener_wrapper->data = listener_data;
8bf65fbd 1748
50ad4244 1749 /* Visit the current schema. */
50842bdc 1750 ret = bt_trace_visit(trace, invoke_listener, listener_wrapper);
8bf65fbd
JG
1751 if (ret) {
1752 goto error;
1753 }
1754
50ad4244
JG
1755 /*
1756 * Add listener to the array of callbacks which will be invoked on
1757 * schema changes.
1758 */
1759 g_ptr_array_add(trace->listeners, listener_wrapper);
2204c381
JG
1760 return ret;
1761error:
50ad4244 1762 g_free(listener_wrapper);
2204c381
JG
1763 return ret;
1764}
1765
bc37ae52 1766BT_HIDDEN
50842bdc 1767int bt_trace_object_modification(struct bt_visitor_object *object,
9b888ff3
JG
1768 void *trace_ptr)
1769{
1770 size_t i;
50842bdc 1771 struct bt_trace *trace = trace_ptr;
9b888ff3 1772
f6ccaed9
PP
1773 BT_ASSERT(trace);
1774 BT_ASSERT(object);
9b888ff3
JG
1775
1776 if (trace->listeners->len == 0) {
1777 goto end;
1778 }
1779
1780 for (i = 0; i < trace->listeners->len; i++) {
1781 struct listener_wrapper *listener =
1782 g_ptr_array_index(trace->listeners, i);
1783
d9a13d86 1784 listener->listener(object, listener->data);
9b888ff3
JG
1785 }
1786end:
1787 return 0;
1788}
1789
50842bdc 1790bt_bool bt_trace_is_static(struct bt_trace *trace)
5acf2ae6 1791{
d975f66c
PP
1792 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
1793 return trace->is_static;
5acf2ae6
PP
1794}
1795
50842bdc 1796int bt_trace_set_is_static(struct bt_trace *trace)
5acf2ae6
PP
1797{
1798 int ret = 0;
3602afb0 1799 size_t i;
5acf2ae6
PP
1800
1801 if (!trace) {
95c09b3a 1802 BT_LOGW_STR("Invalid parameter: trace is NULL.");
5acf2ae6
PP
1803 ret = -1;
1804 goto end;
1805 }
1806
3dca2276 1807 ret = check_packet_header_type_has_no_clock_class(BT_TO_COMMON(trace));
726106d3
PP
1808 if (ret) {
1809 /* check_packet_header_type_has_no_clock_class() logs errors */
1810 goto end;
1811 }
1812
c55a9f58 1813 trace->is_static = BT_TRUE;
3dca2276 1814 bt_trace_common_freeze(BT_TO_COMMON(trace));
95c09b3a 1815 BT_LOGV("Set trace static: addr=%p, name=\"%s\"",
50842bdc 1816 trace, bt_trace_get_name(trace));
5acf2ae6 1817
3602afb0
PP
1818 /* Call all the "trace is static" listeners */
1819 for (i = 0; i < trace->is_static_listeners->len; i++) {
50842bdc 1820 struct bt_trace_is_static_listener_elem elem =
3602afb0 1821 g_array_index(trace->is_static_listeners,
50842bdc 1822 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
1823
1824 if (elem.func) {
1825 elem.func(trace, elem.data);
1826 }
1827 }
1828
1829end:
1830 return ret;
1831}
1832
50842bdc
PP
1833int bt_trace_add_is_static_listener(struct bt_trace *trace,
1834 bt_trace_is_static_listener listener,
1835 bt_trace_listener_removed listener_removed, void *data)
3602afb0
PP
1836{
1837 int i;
50842bdc 1838 struct bt_trace_is_static_listener_elem new_elem = {
3602afb0 1839 .func = listener,
8480c8cc 1840 .removed = listener_removed,
3602afb0
PP
1841 .data = data,
1842 };
1843
1844 if (!trace) {
1845 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1846 i = -1;
1847 goto end;
1848 }
1849
1850 if (!listener) {
1851 BT_LOGW_STR("Invalid parameter: listener is NULL.");
1852 i = -1;
1853 goto end;
1854 }
1855
1856 if (trace->is_static) {
1857 BT_LOGW("Invalid parameter: trace is already static: "
1858 "addr=%p, name=\"%s\"",
50842bdc 1859 trace, bt_trace_get_name(trace));
3602afb0
PP
1860 i = -1;
1861 goto end;
1862 }
1863
8480c8cc
PP
1864 if (trace->in_remove_listener) {
1865 BT_LOGW("Cannot call this function during the execution of a remove listener: "
1866 "addr=%p, name=\"%s\"",
50842bdc 1867 trace, bt_trace_get_name(trace));
8480c8cc
PP
1868 i = -1;
1869 goto end;
1870 }
1871
3602afb0
PP
1872 /* Find the next available spot */
1873 for (i = 0; i < trace->is_static_listeners->len; i++) {
50842bdc 1874 struct bt_trace_is_static_listener_elem elem =
3602afb0 1875 g_array_index(trace->is_static_listeners,
50842bdc 1876 struct bt_trace_is_static_listener_elem, i);
3602afb0
PP
1877
1878 if (!elem.func) {
1879 break;
1880 }
1881 }
1882
1883 if (i == trace->is_static_listeners->len) {
1884 g_array_append_val(trace->is_static_listeners, new_elem);
1885 } else {
1886 g_array_insert_val(trace->is_static_listeners, i, new_elem);
1887 }
1888
1889 BT_LOGV("Added \"trace is static\" listener: "
1890 "trace-addr=%p, trace-name=\"%s\", func-addr=%p, "
1891 "data-addr=%p, listener-id=%d",
50842bdc 1892 trace, bt_trace_get_name(trace), listener, data, i);
3602afb0
PP
1893
1894end:
1895 return i;
1896}
1897
50842bdc
PP
1898int bt_trace_remove_is_static_listener(
1899 struct bt_trace *trace, int listener_id)
3602afb0
PP
1900{
1901 int ret = 0;
50842bdc 1902 struct bt_trace_is_static_listener_elem *elem;
3602afb0
PP
1903
1904 if (!trace) {
1905 BT_LOGW_STR("Invalid parameter: trace is NULL.");
1906 ret = -1;
1907 goto end;
1908 }
1909
8480c8cc
PP
1910 if (trace->in_remove_listener) {
1911 BT_LOGW("Cannot call this function during the execution of a remove listener: "
1912 "addr=%p, name=\"%s\", listener-id=%d",
50842bdc 1913 trace, bt_trace_get_name(trace),
8480c8cc
PP
1914 listener_id);
1915 ret = -1;
1916 goto end;
1917 }
1918
3602afb0
PP
1919 if (listener_id < 0) {
1920 BT_LOGW("Invalid listener ID: must be zero or positive: "
1921 "listener-id=%d", listener_id);
1922 ret = -1;
1923 goto end;
1924 }
1925
1926 if (listener_id >= trace->is_static_listeners->len) {
1927 BT_LOGW("Invalid parameter: no listener with this listener ID: "
1928 "addr=%p, name=\"%s\", listener-id=%d",
50842bdc 1929 trace, bt_trace_get_name(trace),
3602afb0
PP
1930 listener_id);
1931 ret = -1;
1932 goto end;
1933 }
1934
1935 elem = &g_array_index(trace->is_static_listeners,
50842bdc 1936 struct bt_trace_is_static_listener_elem,
3602afb0
PP
1937 listener_id);
1938 if (!elem->func) {
1939 BT_LOGW("Invalid parameter: no listener with this listener ID: "
1940 "addr=%p, name=\"%s\", listener-id=%d",
50842bdc 1941 trace, bt_trace_get_name(trace),
3602afb0
PP
1942 listener_id);
1943 ret = -1;
1944 goto end;
1945 }
1946
8480c8cc
PP
1947 if (elem->removed) {
1948 /* Call remove listener */
1949 BT_LOGV("Calling remove listener: "
1950 "trace-addr=%p, trace-name=\"%s\", "
50842bdc 1951 "listener-id=%d", trace, bt_trace_get_name(trace),
8480c8cc
PP
1952 listener_id);
1953 trace->in_remove_listener = BT_TRUE;
1954 elem->removed(trace, elem->data);
1955 trace->in_remove_listener = BT_FALSE;
1956 }
1957
3602afb0 1958 elem->func = NULL;
8480c8cc 1959 elem->removed = NULL;
3602afb0
PP
1960 elem->data = NULL;
1961 BT_LOGV("Removed \"trace is static\" listener: "
1962 "trace-addr=%p, trace-name=\"%s\", "
50842bdc 1963 "listener-id=%d", trace, bt_trace_get_name(trace),
3602afb0
PP
1964 listener_id);
1965
5acf2ae6
PP
1966end:
1967 return ret;
1968}
This page took 0.187532 seconds and 4 git commands to generate.