Add bt_version_get_*() functions to dynamically get the lib's version
[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
29#include <babeltrace/ctf-ir/trace-internal.h>
ac0c6bdd 30#include <babeltrace/ctf-ir/clock-class-internal.h>
bc37ae52
JG
31#include <babeltrace/ctf-ir/stream-internal.h>
32#include <babeltrace/ctf-ir/stream-class-internal.h>
09840de5 33#include <babeltrace/ctf-ir/event-internal.h>
272df73e
PP
34#include <babeltrace/ctf-ir/event-class.h>
35#include <babeltrace/ctf-ir/event-class-internal.h>
bc37ae52 36#include <babeltrace/ctf-writer/functor-internal.h>
ac0c6bdd 37#include <babeltrace/ctf-writer/clock-internal.h>
2e33ac5a 38#include <babeltrace/ctf-ir/field-types-internal.h>
44e0a4f5 39#include <babeltrace/ctf-ir/attributes-internal.h>
09840de5 40#include <babeltrace/ctf-ir/validation-internal.h>
8bf65fbd 41#include <babeltrace/ctf-ir/visitor-internal.h>
654c1444 42#include <babeltrace/ctf-ir/utils.h>
b2e0c907 43#include <babeltrace/graph/notification-schema.h>
bc37ae52 44#include <babeltrace/compiler.h>
dac5c838 45#include <babeltrace/values.h>
83509119 46#include <babeltrace/ref.h>
a0b720b2 47#include <babeltrace/endian.h>
dc3fffef 48#include <inttypes.h>
bc37ae52
JG
49
50#define DEFAULT_IDENTIFIER_SIZE 128
51#define DEFAULT_METADATA_STRING_SIZE 4096
52
50ad4244
JG
53struct listener_wrapper {
54 bt_ctf_listener_cb listener;
2204c381
JG
55 void *data;
56};
57
bc37ae52 58static
83509119 59void bt_ctf_trace_destroy(struct bt_object *obj);
bc37ae52
JG
60static
61int init_trace_packet_header(struct bt_ctf_trace *trace);
f67f3a6e 62static
09840de5 63void bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
bc37ae52 64
bc37ae52
JG
65static
66const unsigned int field_type_aliases_alignments[] = {
67 [FIELD_TYPE_ALIAS_UINT5_T] = 1,
68 [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
69 [FIELD_TYPE_ALIAS_UINT27_T] = 1,
70 [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
71};
72
73static
74const unsigned int field_type_aliases_sizes[] = {
75 [FIELD_TYPE_ALIAS_UINT5_T] = 5,
76 [FIELD_TYPE_ALIAS_UINT8_T] = 8,
77 [FIELD_TYPE_ALIAS_UINT16_T] = 16,
78 [FIELD_TYPE_ALIAS_UINT27_T] = 27,
79 [FIELD_TYPE_ALIAS_UINT32_T] = 32,
80 [FIELD_TYPE_ALIAS_UINT64_T] = 64,
81};
82
bc37ae52
JG
83struct bt_ctf_trace *bt_ctf_trace_create(void)
84{
85 struct bt_ctf_trace *trace = NULL;
86
87 trace = g_new0(struct bt_ctf_trace, 1);
88 if (!trace) {
89 goto error;
90 }
91
391c8f0d 92 trace->native_byte_order = BT_CTF_BYTE_ORDER_NATIVE;
83509119 93 bt_object_init(trace, bt_ctf_trace_destroy);
bc37ae52 94 trace->clocks = g_ptr_array_new_with_free_func(
83509119 95 (GDestroyNotify) bt_put);
bc37ae52 96 trace->streams = g_ptr_array_new_with_free_func(
e6a8e8e4 97 (GDestroyNotify) bt_object_release);
bc37ae52 98 trace->stream_classes = g_ptr_array_new_with_free_func(
e6a8e8e4 99 (GDestroyNotify) bt_object_release);
7f800dc7 100 if (!trace->clocks || !trace->stream_classes || !trace->streams) {
83509119 101 goto error;
bc37ae52
JG
102 }
103
104 /* Generate a trace UUID */
105 uuid_generate(trace->uuid);
106 if (init_trace_packet_header(trace)) {
83509119 107 goto error;
bc37ae52
JG
108 }
109
7f800dc7
PP
110 /* Create the environment array object */
111 trace->environment = bt_ctf_attributes_create();
112 if (!trace->environment) {
83509119 113 goto error;
7f800dc7
PP
114 }
115
9b888ff3
JG
116 trace->listeners = g_ptr_array_new_with_free_func(
117 (GDestroyNotify) g_free);
118 if (!trace->listeners) {
119 goto error;
120 }
121
bc37ae52
JG
122 return trace;
123
bc37ae52 124error:
83509119 125 BT_PUT(trace);
bc37ae52
JG
126 return trace;
127}
128
e96045d4
JG
129const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace)
130{
131 const char *name = NULL;
132
133 if (!trace || !trace->name) {
134 goto end;
135 }
136
137 name = trace->name->str;
138end:
139 return name;
140}
141
142int bt_ctf_trace_set_name(struct bt_ctf_trace *trace, const char *name)
143{
144 int ret = 0;
145
146 if (!trace || !name || trace->frozen) {
147 ret = -1;
148 goto end;
149 }
150
151 trace->name = trace->name ? g_string_assign(trace->name, name) :
152 g_string_new(name);
153 if (!trace->name) {
154 ret = -1;
155 goto end;
156 }
157end:
158 return ret;
159}
160
83509119 161void bt_ctf_trace_destroy(struct bt_object *obj)
bc37ae52
JG
162{
163 struct bt_ctf_trace *trace;
bc37ae52 164
83509119 165 trace = container_of(obj, struct bt_ctf_trace, base);
bc37ae52 166 if (trace->environment) {
7f800dc7 167 bt_ctf_attributes_destroy(trace->environment);
bc37ae52
JG
168 }
169
e96045d4
JG
170 if (trace->name) {
171 g_string_free(trace->name, TRUE);
172 }
173
bc37ae52
JG
174 if (trace->clocks) {
175 g_ptr_array_free(trace->clocks, TRUE);
176 }
177
178 if (trace->streams) {
179 g_ptr_array_free(trace->streams, TRUE);
180 }
181
182 if (trace->stream_classes) {
183 g_ptr_array_free(trace->stream_classes, TRUE);
184 }
185
9b888ff3
JG
186 if (trace->listeners) {
187 g_ptr_array_free(trace->listeners, TRUE);
188 }
189
83509119 190 bt_put(trace->packet_header_type);
bc37ae52
JG
191 g_free(trace);
192}
193
7f800dc7 194int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
dac5c838 195 const char *name, struct bt_value *value)
bc37ae52 196{
bc37ae52
JG
197 int ret = 0;
198
a0d12916 199 if (!trace || !name || !value ||
7f800dc7 200 bt_ctf_validate_identifier(name) ||
dac5c838 201 !(bt_value_is_integer(value) || bt_value_is_string(value))) {
bc37ae52 202 ret = -1;
7f800dc7 203 goto end;
bc37ae52
JG
204 }
205
206 if (strchr(name, ' ')) {
207 ret = -1;
7f800dc7 208 goto end;
bc37ae52
JG
209 }
210
a0d12916
JG
211 if (trace->frozen) {
212 /*
213 * New environment fields may be added to a frozen trace,
214 * but existing fields may not be changed.
215 *
216 * The object passed is frozen like all other attributes.
217 */
dac5c838 218 struct bt_value *attribute =
a0d12916
JG
219 bt_ctf_attributes_get_field_value_by_name(
220 trace->environment, name);
221
222 if (attribute) {
83509119 223 BT_PUT(attribute);
a0d12916
JG
224 ret = -1;
225 goto end;
226 }
227
dac5c838 228 bt_value_freeze(value);
a0d12916
JG
229 }
230
7f800dc7
PP
231 ret = bt_ctf_attributes_set_field_value(trace->environment, name,
232 value);
bc37ae52 233
7f800dc7
PP
234end:
235 return ret;
236}
bc37ae52 237
7f800dc7
PP
238int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
239 const char *name, const char *value)
240{
241 int ret = 0;
dac5c838 242 struct bt_value *env_value_string_obj = NULL;
7f800dc7
PP
243
244 if (!trace || !name || !value) {
bc37ae52 245 ret = -1;
7f800dc7 246 goto end;
bc37ae52
JG
247 }
248
a0d12916
JG
249 if (trace->frozen) {
250 /*
251 * New environment fields may be added to a frozen trace,
252 * but existing fields may not be changed.
253 */
dac5c838 254 struct bt_value *attribute =
a0d12916
JG
255 bt_ctf_attributes_get_field_value_by_name(
256 trace->environment, name);
257
258 if (attribute) {
83509119 259 BT_PUT(attribute);
a0d12916
JG
260 ret = -1;
261 goto end;
262 }
263 }
264
dac5c838 265 env_value_string_obj = bt_value_string_create_init(value);
bc37ae52 266
7f800dc7
PP
267 if (!env_value_string_obj) {
268 ret = -1;
269 goto end;
bc37ae52
JG
270 }
271
a0d12916 272 if (trace->frozen) {
dac5c838 273 bt_value_freeze(env_value_string_obj);
a0d12916 274 }
7f800dc7
PP
275 ret = bt_ctf_trace_set_environment_field(trace, name,
276 env_value_string_obj);
277
278end:
83509119 279 BT_PUT(env_value_string_obj);
3487c9f3
JG
280 return ret;
281}
282
7f800dc7
PP
283int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
284 const char *name, int64_t value)
3487c9f3 285{
3487c9f3 286 int ret = 0;
dac5c838 287 struct bt_value *env_value_integer_obj = NULL;
3487c9f3
JG
288
289 if (!trace || !name) {
290 ret = -1;
7f800dc7 291 goto end;
3487c9f3
JG
292 }
293
a0d12916
JG
294 if (trace->frozen) {
295 /*
296 * New environment fields may be added to a frozen trace,
297 * but existing fields may not be changed.
298 */
dac5c838 299 struct bt_value *attribute =
a0d12916
JG
300 bt_ctf_attributes_get_field_value_by_name(
301 trace->environment, name);
302
303 if (attribute) {
83509119 304 BT_PUT(attribute);
a0d12916
JG
305 ret = -1;
306 goto end;
307 }
308 }
309
dac5c838 310 env_value_integer_obj = bt_value_integer_create_init(value);
7f800dc7 311 if (!env_value_integer_obj) {
3487c9f3 312 ret = -1;
7f800dc7 313 goto end;
3487c9f3
JG
314 }
315
7f800dc7
PP
316 ret = bt_ctf_trace_set_environment_field(trace, name,
317 env_value_integer_obj);
a0d12916 318 if (trace->frozen) {
dac5c838 319 bt_value_freeze(env_value_integer_obj);
a0d12916 320 }
7f800dc7 321end:
83509119 322 BT_PUT(env_value_integer_obj);
bc37ae52
JG
323 return ret;
324}
325
e6fa2160
JG
326int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
327{
328 int ret = 0;
329
330 if (!trace) {
331 ret = -1;
332 goto end;
333 }
334
7f800dc7 335 ret = bt_ctf_attributes_get_count(trace->environment);
e6fa2160 336
e6fa2160 337end:
7f800dc7 338 return ret;
e6fa2160
JG
339}
340
341const char *
342bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace,
343 int index)
344{
e6fa2160
JG
345 const char *ret = NULL;
346
7f800dc7 347 if (!trace) {
e6fa2160
JG
348 goto end;
349 }
350
7f800dc7
PP
351 ret = bt_ctf_attributes_get_field_name(trace->environment, index);
352
e6fa2160
JG
353end:
354 return ret;
355}
356
dac5c838 357struct bt_value *bt_ctf_trace_get_environment_field_value(
7f800dc7 358 struct bt_ctf_trace *trace, int index)
e6fa2160 359{
dac5c838 360 struct bt_value *ret = NULL;
e6fa2160 361
7f800dc7 362 if (!trace) {
e6fa2160
JG
363 goto end;
364 }
365
7f800dc7
PP
366 ret = bt_ctf_attributes_get_field_value(trace->environment, index);
367
e6fa2160
JG
368end:
369 return ret;
370}
371
dac5c838 372struct bt_value *bt_ctf_trace_get_environment_field_value_by_name(
7f800dc7 373 struct bt_ctf_trace *trace, const char *name)
e6fa2160 374{
dac5c838 375 struct bt_value *ret = NULL;
e6fa2160 376
7f800dc7 377 if (!trace || !name) {
e6fa2160
JG
378 goto end;
379 }
380
7f800dc7
PP
381 ret = bt_ctf_attributes_get_field_value_by_name(trace->environment,
382 name);
383
e6fa2160
JG
384end:
385 return ret;
386}
387
ac0c6bdd
PP
388int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
389 struct bt_ctf_clock_class *clock_class)
bc37ae52
JG
390{
391 int ret = 0;
bc37ae52 392
ac0c6bdd 393 if (!trace || !bt_ctf_clock_class_is_valid(clock_class)) {
bc37ae52
JG
394 ret = -1;
395 goto end;
396 }
397
ac0c6bdd 398 /* Check for duplicate clock classes */
c9d90a34 399 if (bt_ctf_trace_has_clock_class(trace, clock_class)) {
bc37ae52
JG
400 ret = -1;
401 goto end;
402 }
403
ac0c6bdd
PP
404 bt_get(clock_class);
405 g_ptr_array_add(trace->clocks, clock_class);
cfeb617e 406
6474e016 407 if (trace->frozen) {
ac0c6bdd 408 bt_ctf_clock_class_freeze(clock_class);
6474e016 409 }
bc37ae52
JG
410end:
411 return ret;
412}
413
ac0c6bdd 414int bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
884cd6c3
JG
415{
416 int ret = -1;
417
418 if (!trace) {
419 goto end;
420 }
421
422 ret = trace->clocks->len;
423end:
424 return ret;
425}
426
ac0c6bdd
PP
427struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class(
428 struct bt_ctf_trace *trace, int index)
884cd6c3 429{
ac0c6bdd 430 struct bt_ctf_clock_class *clock_class = NULL;
884cd6c3
JG
431
432 if (!trace || index < 0 || index >= trace->clocks->len) {
433 goto end;
434 }
435
ac0c6bdd
PP
436 clock_class = g_ptr_array_index(trace->clocks, index);
437 bt_get(clock_class);
884cd6c3 438end:
ac0c6bdd 439 return clock_class;
884cd6c3
JG
440}
441
ef0c4a15
JG
442int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
443 struct bt_ctf_stream_class *stream_class)
444{
445 int ret, i;
446 int64_t stream_id;
09840de5
PP
447 struct bt_ctf_validation_output trace_sc_validation_output = { 0 };
448 struct bt_ctf_validation_output *ec_validation_outputs = NULL;
449 const enum bt_ctf_validation_flag trace_sc_validation_flags =
450 BT_CTF_VALIDATION_FLAG_TRACE |
451 BT_CTF_VALIDATION_FLAG_STREAM;
452 const enum bt_ctf_validation_flag ec_validation_flags =
453 BT_CTF_VALIDATION_FLAG_EVENT;
454 struct bt_ctf_field_type *packet_header_type = NULL;
455 struct bt_ctf_field_type *packet_context_type = NULL;
456 struct bt_ctf_field_type *event_header_type = NULL;
457 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
458 int event_class_count;
2789e5d9 459 struct bt_ctf_trace *current_parent_trace = NULL;
ef0c4a15
JG
460
461 if (!trace || !stream_class) {
462 ret = -1;
463 goto end;
464 }
465
d490fcbe
PP
466 /*
467 * At the end of this function we freeze the trace, so its
468 * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE.
469 */
470 if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
471 ret = -1;
472 goto end;
473 }
474
2789e5d9
JG
475 current_parent_trace = bt_ctf_stream_class_get_trace(stream_class);
476 if (current_parent_trace) {
477 /* Stream class is already associated to a trace, abort. */
478 ret = -1;
479 goto end;
480 }
481
09840de5
PP
482 event_class_count =
483 bt_ctf_stream_class_get_event_class_count(stream_class);
484 assert(event_class_count >= 0);
485
486 /* Check for duplicate stream classes */
ef0c4a15
JG
487 for (i = 0; i < trace->stream_classes->len; i++) {
488 if (trace->stream_classes->pdata[i] == stream_class) {
09840de5 489 /* Stream class already registered to the trace */
ef0c4a15
JG
490 ret = -1;
491 goto end;
492 }
493 }
494
fe13b5c0 495 if (stream_class->clock) {
ac0c6bdd
PP
496 struct bt_ctf_clock_class *stream_clock_class =
497 stream_class->clock->clock_class;
498
499 if (trace->is_created_by_writer) {
500 /*
501 * Make sure this clock was also added to the
502 * trace (potentially through its CTF writer
503 * owner).
504 */
505 size_t i;
506
507 for (i = 0; i < trace->clocks->len; i++) {
508 if (trace->clocks->pdata[i] ==
509 stream_clock_class) {
510 /* Found! */
511 break;
512 }
513 }
514
515 if (i == trace->clocks->len) {
516 /* Not found */
fe13b5c0
PP
517 ret = -1;
518 goto end;
519 }
520 } else {
ac0c6bdd
PP
521 /*
522 * This trace was NOT created by a CTF writer,
523 * thus do not allow the stream class to add to
524 * have a clock at all. Those are two
525 * independent APIs (non-writer and writer
526 * APIs), and isolating them simplifies things.
527 */
528 ret = -1;
529 goto end;
fe13b5c0
PP
530 }
531 }
532
09840de5
PP
533 /*
534 * We're about to freeze both the trace and the stream class.
535 * Also, each event class contained in this stream class are
536 * already frozen.
537 *
538 * This trace, this stream class, and all its event classes
539 * should be valid at this point.
540 *
541 * Validate trace and stream class first, then each event
542 * class of this stream class can be validated individually.
543 */
544 packet_header_type =
545 bt_ctf_trace_get_packet_header_type(trace);
546 packet_context_type =
547 bt_ctf_stream_class_get_packet_context_type(stream_class);
548 event_header_type =
549 bt_ctf_stream_class_get_event_header_type(stream_class);
550 stream_event_ctx_type =
551 bt_ctf_stream_class_get_event_context_type(stream_class);
552 ret = bt_ctf_validate_class_types(trace->environment,
553 packet_header_type, packet_context_type, event_header_type,
554 stream_event_ctx_type, NULL, NULL, trace->valid,
555 stream_class->valid, 1, &trace_sc_validation_output,
556 trace_sc_validation_flags);
557 BT_PUT(packet_header_type);
558 BT_PUT(packet_context_type);
559 BT_PUT(event_header_type);
560 BT_PUT(stream_event_ctx_type);
561
2bd7f864 562 if (ret) {
09840de5
PP
563 /*
564 * This means something went wrong during the validation
565 * process, not that the objects are invalid.
566 */
2bd7f864
JG
567 goto end;
568 }
569
09840de5
PP
570 if ((trace_sc_validation_output.valid_flags &
571 trace_sc_validation_flags) !=
572 trace_sc_validation_flags) {
573 /* Invalid trace/stream class */
574 ret = -1;
575 goto end;
576 }
577
578 if (event_class_count > 0) {
579 ec_validation_outputs = g_new0(struct bt_ctf_validation_output,
580 event_class_count);
581 if (!ec_validation_outputs) {
582 ret = -1;
583 goto end;
584 }
585 }
586
587 /* Validate each event class individually */
6474e016 588 for (i = 0; i < event_class_count; i++) {
09840de5
PP
589 struct bt_ctf_event_class *event_class =
590 bt_ctf_stream_class_get_event_class(stream_class, i);
591 struct bt_ctf_field_type *event_context_type = NULL;
592 struct bt_ctf_field_type *event_payload_type = NULL;
593
594 event_context_type =
595 bt_ctf_event_class_get_context_type(event_class);
596 event_payload_type =
597 bt_ctf_event_class_get_payload_type(event_class);
598
599 /*
600 * It is important to use the field types returned by
601 * the previous trace and stream class validation here
602 * because copies could have been made.
603 */
604 ret = bt_ctf_validate_class_types(trace->environment,
605 trace_sc_validation_output.packet_header_type,
606 trace_sc_validation_output.packet_context_type,
607 trace_sc_validation_output.event_header_type,
608 trace_sc_validation_output.stream_event_ctx_type,
609 event_context_type, event_payload_type,
610 1, 1, event_class->valid, &ec_validation_outputs[i],
611 ec_validation_flags);
612 BT_PUT(event_context_type);
613 BT_PUT(event_payload_type);
614 BT_PUT(event_class);
615
616 if (ret) {
617 goto end;
618 }
619
620 if ((ec_validation_outputs[i].valid_flags &
621 ec_validation_flags) != ec_validation_flags) {
622 /* Invalid event class */
623 ret = -1;
624 goto end;
625 }
626 }
627
ef0c4a15
JG
628 stream_id = bt_ctf_stream_class_get_id(stream_class);
629 if (stream_id < 0) {
630 stream_id = trace->next_stream_id++;
631
632 /* Try to assign a new stream id */
633 for (i = 0; i < trace->stream_classes->len; i++) {
634 if (stream_id == bt_ctf_stream_class_get_id(
635 trace->stream_classes->pdata[i])) {
636 /* Duplicate stream id found */
637 ret = -1;
638 goto end;
639 }
640 }
641
0ddffae5
JG
642 if (bt_ctf_stream_class_set_id_no_check(stream_class,
643 stream_id)) {
ef0c4a15
JG
644 /* TODO Should retry with a different stream id */
645 ret = -1;
646 goto end;
647 }
648 }
649
e6a8e8e4 650 bt_object_set_parent(stream_class, trace);
ef0c4a15
JG
651 g_ptr_array_add(trace->stream_classes, stream_class);
652
653 /*
09840de5
PP
654 * At this point we know that the function will be successful.
655 * Therefore we can replace the trace and stream class field
656 * types with what's in their validation output structure and
657 * mark them as valid. We can also replace the field types of
658 * all the event classes of the stream class and mark them as
659 * valid.
660 */
661 bt_ctf_validation_replace_types(trace, stream_class, NULL,
662 &trace_sc_validation_output, trace_sc_validation_flags);
663 trace->valid = 1;
664 stream_class->valid = 1;
665
666 /*
667 * Put what was not moved in bt_ctf_validation_replace_types().
668 */
669 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
670
6474e016 671 for (i = 0; i < event_class_count; i++) {
09840de5
PP
672 struct bt_ctf_event_class *event_class =
673 bt_ctf_stream_class_get_event_class(stream_class, i);
674
675 bt_ctf_validation_replace_types(NULL, NULL, event_class,
676 &ec_validation_outputs[i], ec_validation_flags);
677 event_class->valid = 1;
678 BT_PUT(event_class);
679
680 /*
681 * Put what was not moved in
682 * bt_ctf_validation_replace_types().
683 */
684 bt_ctf_validation_output_put_types(&ec_validation_outputs[i]);
685 }
686
09840de5
PP
687 /*
688 * Freeze the trace and the stream class.
689 */
f67f3a6e 690 bt_ctf_stream_class_freeze(stream_class);
09840de5
PP
691 bt_ctf_trace_freeze(trace);
692
9b888ff3
JG
693 /* Notifiy listeners of the trace's schema modification. */
694 bt_ctf_stream_class_visit(stream_class,
d9a13d86 695 bt_ctf_trace_object_modification, trace);
ef0c4a15 696end:
d3814b54 697 if (ret) {
e6a8e8e4 698 bt_object_set_parent(stream_class, NULL);
09840de5
PP
699
700 if (ec_validation_outputs) {
6474e016 701 for (i = 0; i < event_class_count; i++) {
09840de5
PP
702 bt_ctf_validation_output_put_types(
703 &ec_validation_outputs[i]);
704 }
705 }
d3814b54 706 }
09840de5
PP
707
708 g_free(ec_validation_outputs);
709 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
2789e5d9 710 bt_put(current_parent_trace);
09840de5
PP
711 assert(!packet_header_type);
712 assert(!packet_context_type);
713 assert(!event_header_type);
714 assert(!stream_event_ctx_type);
715
ef0c4a15
JG
716 return ret;
717}
718
719int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
720{
721 int ret;
722
723 if (!trace) {
724 ret = -1;
725 goto end;
726 }
727
728 ret = trace->stream_classes->len;
729end:
730 return ret;
731}
732
733struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class(
734 struct bt_ctf_trace *trace, int index)
735{
736 struct bt_ctf_stream_class *stream_class = NULL;
737
738 if (!trace || index < 0 || index >= trace->stream_classes->len) {
739 goto end;
740 }
741
742 stream_class = g_ptr_array_index(trace->stream_classes, index);
83509119 743 bt_get(stream_class);
ef0c4a15
JG
744end:
745 return stream_class;
746}
747
4841ccc1
PP
748struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
749 struct bt_ctf_trace *trace, uint32_t id)
750{
751 int i;
752 struct bt_ctf_stream_class *stream_class = NULL;
753
754 if (!trace) {
755 goto end;
756 }
757
6474e016 758 for (i = 0; i < trace->stream_classes->len; i++) {
4841ccc1
PP
759 struct bt_ctf_stream_class *stream_class_candidate;
760
761 stream_class_candidate =
762 g_ptr_array_index(trace->stream_classes, i);
763
764 if (bt_ctf_stream_class_get_id(stream_class_candidate) ==
765 (int64_t) id) {
766 stream_class = stream_class_candidate;
83509119 767 bt_get(stream_class);
4841ccc1
PP
768 goto end;
769 }
770 }
771
772end:
773 return stream_class;
774}
775
ac0c6bdd 776struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
7e4347a3
PP
777 struct bt_ctf_trace *trace, const char *name)
778{
779 size_t i;
ac0c6bdd 780 struct bt_ctf_clock_class *clock_class = NULL;
7e4347a3
PP
781
782 if (!trace || !name) {
783 goto end;
784 }
785
6474e016 786 for (i = 0; i < trace->clocks->len; i++) {
ac0c6bdd 787 struct bt_ctf_clock_class *cur_clk =
7e4347a3 788 g_ptr_array_index(trace->clocks, i);
ac0c6bdd 789 const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk);
7e4347a3
PP
790
791 if (!cur_clk_name) {
792 goto end;
793 }
794
795 if (!strcmp(cur_clk_name, name)) {
ac0c6bdd
PP
796 clock_class = cur_clk;
797 bt_get(clock_class);
7e4347a3
PP
798 goto end;
799 }
800 }
801
802end:
ac0c6bdd 803 return clock_class;
7e4347a3
PP
804}
805
c9d90a34
PP
806BT_HIDDEN
807bool bt_ctf_trace_has_clock_class(struct bt_ctf_trace *trace,
808 struct bt_ctf_clock_class *clock_class)
809{
810 struct search_query query = { .value = clock_class, .found = 0 };
811
812 assert(trace);
813 assert(clock_class);
814
815 g_ptr_array_foreach(trace->clocks, value_exists, &query);
816 return query.found;
817}
818
bc37ae52 819BT_HIDDEN
dc3fffef 820const char *get_byte_order_string(enum bt_ctf_byte_order byte_order)
bc37ae52
JG
821{
822 const char *string;
823
824 switch (byte_order) {
dc3fffef 825 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
bc37ae52
JG
826 string = "le";
827 break;
dc3fffef 828 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
bc37ae52
JG
829 string = "be";
830 break;
dc3fffef
PP
831 case BT_CTF_BYTE_ORDER_NATIVE:
832 string = "native";
bc37ae52 833 break;
dc3fffef
PP
834 default:
835 assert(false);
bc37ae52
JG
836 }
837
838 return string;
839}
840
841static
842int append_trace_metadata(struct bt_ctf_trace *trace,
843 struct metadata_context *context)
844{
845 unsigned char *uuid = trace->uuid;
846 int ret;
847
848 g_string_append(context->string, "trace {\n");
849
850 g_string_append(context->string, "\tmajor = 1;\n");
851 g_string_append(context->string, "\tminor = 8;\n");
dc3fffef
PP
852 assert(trace->native_byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ||
853 trace->native_byte_order == BT_CTF_BYTE_ORDER_BIG_ENDIAN ||
854 trace->native_byte_order == BT_CTF_BYTE_ORDER_NETWORK);
bc37ae52
JG
855 g_string_append_printf(context->string,
856 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
857 uuid[0], uuid[1], uuid[2], uuid[3],
858 uuid[4], uuid[5], uuid[6], uuid[7],
859 uuid[8], uuid[9], uuid[10], uuid[11],
860 uuid[12], uuid[13], uuid[14], uuid[15]);
861 g_string_append_printf(context->string, "\tbyte_order = %s;\n",
dc3fffef 862 get_byte_order_string(trace->native_byte_order));
bc37ae52
JG
863
864 g_string_append(context->string, "\tpacket.header := ");
865 context->current_indentation_level++;
866 g_string_assign(context->field_name, "");
d246b111 867 ret = bt_ctf_field_type_serialize(trace->packet_header_type,
bc37ae52
JG
868 context);
869 if (ret) {
870 goto end;
871 }
872 context->current_indentation_level--;
873
874 g_string_append(context->string, ";\n};\n\n");
875end:
876 return ret;
877}
878
bc37ae52
JG
879static
880void append_env_metadata(struct bt_ctf_trace *trace,
881 struct metadata_context *context)
882{
7f800dc7
PP
883 int i;
884 int env_size;
885
886 env_size = bt_ctf_attributes_get_count(trace->environment);
887
888 if (env_size <= 0) {
bc37ae52
JG
889 return;
890 }
891
892 g_string_append(context->string, "env {\n");
7f800dc7 893
6474e016 894 for (i = 0; i < env_size; i++) {
dac5c838 895 struct bt_value *env_field_value_obj = NULL;
7f800dc7 896 const char *entry_name;
7f800dc7
PP
897
898 entry_name = bt_ctf_attributes_get_field_name(
899 trace->environment, i);
900 env_field_value_obj = bt_ctf_attributes_get_field_value(
901 trace->environment, i);
902
903 if (!entry_name || !env_field_value_obj) {
904 goto loop_next;
905 }
906
dac5c838
PP
907 switch (bt_value_get_type(env_field_value_obj)) {
908 case BT_VALUE_TYPE_INTEGER:
b8248cc0
PP
909 {
910 int ret;
911 int64_t int_value;
912
dac5c838 913 ret = bt_value_integer_get(env_field_value_obj,
7f800dc7
PP
914 &int_value);
915
916 if (ret) {
917 goto loop_next;
918 }
919
920 g_string_append_printf(context->string,
921 "\t%s = %" PRId64 ";\n", entry_name,
922 int_value);
923 break;
b8248cc0 924 }
dac5c838 925 case BT_VALUE_TYPE_STRING:
7f800dc7
PP
926 {
927 int ret;
928 const char *str_value;
929 char *escaped_str = NULL;
930
dac5c838 931 ret = bt_value_string_get(env_field_value_obj,
7f800dc7
PP
932 &str_value);
933
934 if (ret) {
935 goto loop_next;
936 }
937
938 escaped_str = g_strescape(str_value, NULL);
939
940 if (!escaped_str) {
941 goto loop_next;
942 }
943
944 g_string_append_printf(context->string,
945 "\t%s = \"%s\";\n", entry_name, escaped_str);
946 free(escaped_str);
947 break;
948 }
949
950 default:
951 goto loop_next;
952 }
953
954loop_next:
83509119 955 BT_PUT(env_field_value_obj);
7f800dc7
PP
956 }
957
bc37ae52
JG
958 g_string_append(context->string, "};\n\n");
959}
960
961char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
962{
963 char *metadata = NULL;
964 struct metadata_context *context = NULL;
965 int err = 0;
966 size_t i;
967
968 if (!trace) {
969 goto end;
970 }
971
972 context = g_new0(struct metadata_context, 1);
973 if (!context) {
974 goto end;
975 }
976
977 context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
978 context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
979 g_string_append(context->string, "/* CTF 1.8 */\n\n");
980 if (append_trace_metadata(trace, context)) {
981 goto error;
982 }
983 append_env_metadata(trace, context);
984 g_ptr_array_foreach(trace->clocks,
ac0c6bdd 985 (GFunc)bt_ctf_clock_class_serialize, context);
bc37ae52
JG
986
987 for (i = 0; i < trace->stream_classes->len; i++) {
988 err = bt_ctf_stream_class_serialize(
989 trace->stream_classes->pdata[i], context);
990 if (err) {
991 goto error;
992 }
993 }
994
995 metadata = context->string->str;
996error:
997 g_string_free(context->string, err ? TRUE : FALSE);
998 g_string_free(context->field_name, TRUE);
999 g_free(context);
1000end:
1001 return metadata;
1002}
1003
4ed90fb3
JG
1004enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace)
1005{
1006 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
1007
1008 if (!trace) {
1009 goto end;
1010 }
1011
dc3fffef
PP
1012 ret = trace->native_byte_order;
1013
4ed90fb3
JG
1014end:
1015 return ret;
1016}
1017
391c8f0d 1018int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace,
bc37ae52
JG
1019 enum bt_ctf_byte_order byte_order)
1020{
1021 int ret = 0;
bc37ae52
JG
1022
1023 if (!trace || trace->frozen) {
1024 ret = -1;
1025 goto end;
1026 }
1027
dc3fffef
PP
1028 if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN &&
1029 byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN &&
1030 byte_order != BT_CTF_BYTE_ORDER_NETWORK) {
bc37ae52
JG
1031 ret = -1;
1032 goto end;
1033 }
1034
dc3fffef
PP
1035 trace->native_byte_order = byte_order;
1036
bc37ae52
JG
1037end:
1038 return ret;
1039}
1040
d246b111
JG
1041struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
1042 struct bt_ctf_trace *trace)
1043{
1044 struct bt_ctf_field_type *field_type = NULL;
1045
1046 if (!trace) {
1047 goto end;
1048 }
1049
83509119 1050 bt_get(trace->packet_header_type);
d246b111
JG
1051 field_type = trace->packet_header_type;
1052end:
1053 return field_type;
1054}
1055
1056int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
1057 struct bt_ctf_field_type *packet_header_type)
1058{
1059 int ret = 0;
1060
835b2d10 1061 if (!trace || trace->frozen) {
d246b111
JG
1062 ret = -1;
1063 goto end;
1064 }
1065
835b2d10
JG
1066 /* packet_header_type must be a structure. */
1067 if (packet_header_type &&
1068 bt_ctf_field_type_get_type_id(packet_header_type) !=
1487a16a 1069 BT_CTF_FIELD_TYPE_ID_STRUCT) {
d246b111
JG
1070 ret = -1;
1071 goto end;
1072 }
1073
83509119 1074 bt_put(trace->packet_header_type);
835b2d10 1075 trace->packet_header_type = bt_get(packet_header_type);
d246b111
JG
1076end:
1077 return ret;
1078}
1079
8bf65fbd
JG
1080static
1081int get_stream_class_count(void *element)
1082{
1083 return bt_ctf_trace_get_stream_class_count(
1084 (struct bt_ctf_trace *) element);
1085}
1086
1087static
1088void *get_stream_class(void *element, int i)
1089{
1090 return bt_ctf_trace_get_stream_class(
1091 (struct bt_ctf_trace *) element, i);
1092}
1093
1094static
d9a13d86 1095int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data)
8bf65fbd 1096{
d9a13d86 1097 return bt_ctf_stream_class_visit(object, visitor, data);
8bf65fbd
JG
1098}
1099
1100int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
d9a13d86 1101 bt_ctf_visitor visitor, void *data)
8bf65fbd
JG
1102{
1103 int ret;
d9a13d86
PP
1104 struct bt_ctf_object obj =
1105 { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE };
8bf65fbd
JG
1106
1107 if (!trace || !visitor) {
1108 ret = -1;
1109 goto end;
1110 }
1111
d9a13d86 1112 ret = visitor_helper(&obj, get_stream_class_count,
8bf65fbd
JG
1113 get_stream_class, visit_stream_class, visitor, data);
1114end:
1115 return ret;
1116}
1117
1118static
d9a13d86 1119int invoke_listener(struct bt_ctf_object *object, void *data)
8bf65fbd 1120{
9b888ff3 1121 struct listener_wrapper *listener_wrapper = data;
8bf65fbd 1122
d9a13d86 1123 listener_wrapper->listener(object, listener_wrapper->data);
9b888ff3 1124 return 0;
8bf65fbd
JG
1125}
1126
50ad4244
JG
1127int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace,
1128 bt_ctf_listener_cb listener, void *listener_data)
2204c381
JG
1129{
1130 int ret = 0;
50ad4244
JG
1131 struct listener_wrapper *listener_wrapper =
1132 g_new0(struct listener_wrapper, 1);
2204c381 1133
50ad4244 1134 if (!trace || !listener || !listener_wrapper) {
2204c381
JG
1135 ret = -1;
1136 goto error;
1137 }
1138
50ad4244
JG
1139 listener_wrapper->listener = listener;
1140 listener_wrapper->data = listener_data;
8bf65fbd 1141
50ad4244 1142 /* Visit the current schema. */
9b888ff3 1143 ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper);
8bf65fbd
JG
1144 if (ret) {
1145 goto error;
1146 }
1147
50ad4244
JG
1148 /*
1149 * Add listener to the array of callbacks which will be invoked on
1150 * schema changes.
1151 */
1152 g_ptr_array_add(trace->listeners, listener_wrapper);
2204c381
JG
1153 return ret;
1154error:
50ad4244 1155 g_free(listener_wrapper);
2204c381
JG
1156 return ret;
1157}
1158
bc37ae52 1159BT_HIDDEN
d9a13d86 1160int bt_ctf_trace_object_modification(struct bt_ctf_object *object,
9b888ff3
JG
1161 void *trace_ptr)
1162{
1163 size_t i;
1164 struct bt_ctf_trace *trace = trace_ptr;
1165
1166 assert(trace);
d9a13d86 1167 assert(object);
9b888ff3
JG
1168
1169 if (trace->listeners->len == 0) {
1170 goto end;
1171 }
1172
1173 for (i = 0; i < trace->listeners->len; i++) {
1174 struct listener_wrapper *listener =
1175 g_ptr_array_index(trace->listeners, i);
1176
d9a13d86 1177 listener->listener(object, listener->data);
9b888ff3
JG
1178 }
1179end:
1180 return 0;
1181}
1182
1183BT_HIDDEN
bc37ae52
JG
1184struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
1185{
a25e0d14 1186 int ret;
bc37ae52 1187 unsigned int alignment, size;
8bdcb82e 1188 struct bt_ctf_field_type *field_type = NULL;
bc37ae52
JG
1189
1190 if (alias >= NR_FIELD_TYPE_ALIAS) {
8bdcb82e 1191 goto end;
bc37ae52
JG
1192 }
1193
1194 alignment = field_type_aliases_alignments[alias];
1195 size = field_type_aliases_sizes[alias];
1196 field_type = bt_ctf_field_type_integer_create(size);
a25e0d14
JG
1197 ret = bt_ctf_field_type_set_alignment(field_type, alignment);
1198 if (ret) {
1199 BT_PUT(field_type);
1200 }
8bdcb82e 1201end:
bc37ae52
JG
1202 return field_type;
1203}
1204
f67f3a6e 1205static
09840de5 1206void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
f67f3a6e 1207{
6474e016
PP
1208 int i;
1209
09840de5 1210 bt_ctf_field_type_freeze(trace->packet_header_type);
f67f3a6e 1211 bt_ctf_attributes_freeze(trace->environment);
6474e016
PP
1212
1213 for (i = 0; i < trace->clocks->len; i++) {
ac0c6bdd 1214 struct bt_ctf_clock_class *clock_class =
6474e016
PP
1215 g_ptr_array_index(trace->clocks, i);
1216
ac0c6bdd 1217 bt_ctf_clock_class_freeze(clock_class);
6474e016
PP
1218 }
1219
f67f3a6e
JG
1220 trace->frozen = 1;
1221}
1222
bc37ae52
JG
1223static
1224int init_trace_packet_header(struct bt_ctf_trace *trace)
1225{
bc37ae52 1226 int ret = 0;
d246b111 1227 struct bt_ctf_field *magic = NULL, *uuid_array = NULL;
bc37ae52
JG
1228 struct bt_ctf_field_type *_uint32_t =
1229 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
1230 struct bt_ctf_field_type *_uint8_t =
1231 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
1232 struct bt_ctf_field_type *trace_packet_header_type =
1233 bt_ctf_field_type_structure_create();
1234 struct bt_ctf_field_type *uuid_array_type =
1235 bt_ctf_field_type_array_create(_uint8_t, 16);
1236
1237 if (!trace_packet_header_type || !uuid_array_type) {
1238 ret = -1;
1239 goto end;
1240 }
1241
bc37ae52
JG
1242 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1243 _uint32_t, "magic");
1244 if (ret) {
1245 goto end;
1246 }
1247
1248 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1249 uuid_array_type, "uuid");
1250 if (ret) {
1251 goto end;
1252 }
1253
1254 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1255 _uint32_t, "stream_id");
1256 if (ret) {
1257 goto end;
1258 }
1259
662e778c
JG
1260 ret = bt_ctf_trace_set_packet_header_type(trace,
1261 trace_packet_header_type);
1262 if (ret) {
1263 goto end;
1264 }
bc37ae52 1265end:
83509119
JG
1266 bt_put(uuid_array_type);
1267 bt_put(_uint32_t);
1268 bt_put(_uint8_t);
1269 bt_put(magic);
1270 bt_put(uuid_array);
1271 bt_put(trace_packet_header_type);
bc37ae52
JG
1272 return ret;
1273}
This page took 0.095414 seconds and 4 git commands to generate.