ir: make sure you can't add a SC to a trace with a native BO
[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;
ac0c6bdd 392 struct search_query query = { .value = clock_class, .found = 0 };
bc37ae52 393
ac0c6bdd 394 if (!trace || !bt_ctf_clock_class_is_valid(clock_class)) {
bc37ae52
JG
395 ret = -1;
396 goto end;
397 }
398
ac0c6bdd 399 /* Check for duplicate clock classes */
bc37ae52
JG
400 g_ptr_array_foreach(trace->clocks, value_exists, &query);
401 if (query.found) {
402 ret = -1;
403 goto end;
404 }
405
ac0c6bdd
PP
406 bt_get(clock_class);
407 g_ptr_array_add(trace->clocks, clock_class);
cfeb617e 408
6474e016 409 if (trace->frozen) {
ac0c6bdd 410 bt_ctf_clock_class_freeze(clock_class);
6474e016 411 }
bc37ae52
JG
412end:
413 return ret;
414}
415
ac0c6bdd 416int bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
884cd6c3
JG
417{
418 int ret = -1;
419
420 if (!trace) {
421 goto end;
422 }
423
424 ret = trace->clocks->len;
425end:
426 return ret;
427}
428
ac0c6bdd
PP
429struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class(
430 struct bt_ctf_trace *trace, int index)
884cd6c3 431{
ac0c6bdd 432 struct bt_ctf_clock_class *clock_class = NULL;
884cd6c3
JG
433
434 if (!trace || index < 0 || index >= trace->clocks->len) {
435 goto end;
436 }
437
ac0c6bdd
PP
438 clock_class = g_ptr_array_index(trace->clocks, index);
439 bt_get(clock_class);
884cd6c3 440end:
ac0c6bdd 441 return clock_class;
884cd6c3
JG
442}
443
ef0c4a15
JG
444int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
445 struct bt_ctf_stream_class *stream_class)
446{
447 int ret, i;
448 int64_t stream_id;
09840de5
PP
449 struct bt_ctf_validation_output trace_sc_validation_output = { 0 };
450 struct bt_ctf_validation_output *ec_validation_outputs = NULL;
451 const enum bt_ctf_validation_flag trace_sc_validation_flags =
452 BT_CTF_VALIDATION_FLAG_TRACE |
453 BT_CTF_VALIDATION_FLAG_STREAM;
454 const enum bt_ctf_validation_flag ec_validation_flags =
455 BT_CTF_VALIDATION_FLAG_EVENT;
456 struct bt_ctf_field_type *packet_header_type = NULL;
457 struct bt_ctf_field_type *packet_context_type = NULL;
458 struct bt_ctf_field_type *event_header_type = NULL;
459 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
460 int event_class_count;
2789e5d9 461 struct bt_ctf_trace *current_parent_trace = NULL;
ef0c4a15
JG
462
463 if (!trace || !stream_class) {
464 ret = -1;
465 goto end;
466 }
467
d490fcbe
PP
468 /*
469 * At the end of this function we freeze the trace, so its
470 * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE.
471 */
472 if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
473 ret = -1;
474 goto end;
475 }
476
2789e5d9
JG
477 current_parent_trace = bt_ctf_stream_class_get_trace(stream_class);
478 if (current_parent_trace) {
479 /* Stream class is already associated to a trace, abort. */
480 ret = -1;
481 goto end;
482 }
483
09840de5
PP
484 event_class_count =
485 bt_ctf_stream_class_get_event_class_count(stream_class);
486 assert(event_class_count >= 0);
487
488 /* Check for duplicate stream classes */
ef0c4a15
JG
489 for (i = 0; i < trace->stream_classes->len; i++) {
490 if (trace->stream_classes->pdata[i] == stream_class) {
09840de5 491 /* Stream class already registered to the trace */
ef0c4a15
JG
492 ret = -1;
493 goto end;
494 }
495 }
496
fe13b5c0 497 if (stream_class->clock) {
ac0c6bdd
PP
498 struct bt_ctf_clock_class *stream_clock_class =
499 stream_class->clock->clock_class;
500
501 if (trace->is_created_by_writer) {
502 /*
503 * Make sure this clock was also added to the
504 * trace (potentially through its CTF writer
505 * owner).
506 */
507 size_t i;
508
509 for (i = 0; i < trace->clocks->len; i++) {
510 if (trace->clocks->pdata[i] ==
511 stream_clock_class) {
512 /* Found! */
513 break;
514 }
515 }
516
517 if (i == trace->clocks->len) {
518 /* Not found */
fe13b5c0
PP
519 ret = -1;
520 goto end;
521 }
522 } else {
ac0c6bdd
PP
523 /*
524 * This trace was NOT created by a CTF writer,
525 * thus do not allow the stream class to add to
526 * have a clock at all. Those are two
527 * independent APIs (non-writer and writer
528 * APIs), and isolating them simplifies things.
529 */
530 ret = -1;
531 goto end;
fe13b5c0
PP
532 }
533 }
534
09840de5
PP
535 /*
536 * We're about to freeze both the trace and the stream class.
537 * Also, each event class contained in this stream class are
538 * already frozen.
539 *
540 * This trace, this stream class, and all its event classes
541 * should be valid at this point.
542 *
543 * Validate trace and stream class first, then each event
544 * class of this stream class can be validated individually.
545 */
546 packet_header_type =
547 bt_ctf_trace_get_packet_header_type(trace);
548 packet_context_type =
549 bt_ctf_stream_class_get_packet_context_type(stream_class);
550 event_header_type =
551 bt_ctf_stream_class_get_event_header_type(stream_class);
552 stream_event_ctx_type =
553 bt_ctf_stream_class_get_event_context_type(stream_class);
554 ret = bt_ctf_validate_class_types(trace->environment,
555 packet_header_type, packet_context_type, event_header_type,
556 stream_event_ctx_type, NULL, NULL, trace->valid,
557 stream_class->valid, 1, &trace_sc_validation_output,
558 trace_sc_validation_flags);
559 BT_PUT(packet_header_type);
560 BT_PUT(packet_context_type);
561 BT_PUT(event_header_type);
562 BT_PUT(stream_event_ctx_type);
563
2bd7f864 564 if (ret) {
09840de5
PP
565 /*
566 * This means something went wrong during the validation
567 * process, not that the objects are invalid.
568 */
2bd7f864
JG
569 goto end;
570 }
571
09840de5
PP
572 if ((trace_sc_validation_output.valid_flags &
573 trace_sc_validation_flags) !=
574 trace_sc_validation_flags) {
575 /* Invalid trace/stream class */
576 ret = -1;
577 goto end;
578 }
579
580 if (event_class_count > 0) {
581 ec_validation_outputs = g_new0(struct bt_ctf_validation_output,
582 event_class_count);
583 if (!ec_validation_outputs) {
584 ret = -1;
585 goto end;
586 }
587 }
588
589 /* Validate each event class individually */
6474e016 590 for (i = 0; i < event_class_count; i++) {
09840de5
PP
591 struct bt_ctf_event_class *event_class =
592 bt_ctf_stream_class_get_event_class(stream_class, i);
593 struct bt_ctf_field_type *event_context_type = NULL;
594 struct bt_ctf_field_type *event_payload_type = NULL;
595
596 event_context_type =
597 bt_ctf_event_class_get_context_type(event_class);
598 event_payload_type =
599 bt_ctf_event_class_get_payload_type(event_class);
600
601 /*
602 * It is important to use the field types returned by
603 * the previous trace and stream class validation here
604 * because copies could have been made.
605 */
606 ret = bt_ctf_validate_class_types(trace->environment,
607 trace_sc_validation_output.packet_header_type,
608 trace_sc_validation_output.packet_context_type,
609 trace_sc_validation_output.event_header_type,
610 trace_sc_validation_output.stream_event_ctx_type,
611 event_context_type, event_payload_type,
612 1, 1, event_class->valid, &ec_validation_outputs[i],
613 ec_validation_flags);
614 BT_PUT(event_context_type);
615 BT_PUT(event_payload_type);
616 BT_PUT(event_class);
617
618 if (ret) {
619 goto end;
620 }
621
622 if ((ec_validation_outputs[i].valid_flags &
623 ec_validation_flags) != ec_validation_flags) {
624 /* Invalid event class */
625 ret = -1;
626 goto end;
627 }
628 }
629
ef0c4a15
JG
630 stream_id = bt_ctf_stream_class_get_id(stream_class);
631 if (stream_id < 0) {
632 stream_id = trace->next_stream_id++;
633
634 /* Try to assign a new stream id */
635 for (i = 0; i < trace->stream_classes->len; i++) {
636 if (stream_id == bt_ctf_stream_class_get_id(
637 trace->stream_classes->pdata[i])) {
638 /* Duplicate stream id found */
639 ret = -1;
640 goto end;
641 }
642 }
643
0ddffae5
JG
644 if (bt_ctf_stream_class_set_id_no_check(stream_class,
645 stream_id)) {
ef0c4a15
JG
646 /* TODO Should retry with a different stream id */
647 ret = -1;
648 goto end;
649 }
650 }
651
e6a8e8e4 652 bt_object_set_parent(stream_class, trace);
ef0c4a15
JG
653 g_ptr_array_add(trace->stream_classes, stream_class);
654
655 /*
09840de5
PP
656 * At this point we know that the function will be successful.
657 * Therefore we can replace the trace and stream class field
658 * types with what's in their validation output structure and
659 * mark them as valid. We can also replace the field types of
660 * all the event classes of the stream class and mark them as
661 * valid.
662 */
663 bt_ctf_validation_replace_types(trace, stream_class, NULL,
664 &trace_sc_validation_output, trace_sc_validation_flags);
665 trace->valid = 1;
666 stream_class->valid = 1;
667
668 /*
669 * Put what was not moved in bt_ctf_validation_replace_types().
670 */
671 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
672
6474e016 673 for (i = 0; i < event_class_count; i++) {
09840de5
PP
674 struct bt_ctf_event_class *event_class =
675 bt_ctf_stream_class_get_event_class(stream_class, i);
676
677 bt_ctf_validation_replace_types(NULL, NULL, event_class,
678 &ec_validation_outputs[i], ec_validation_flags);
679 event_class->valid = 1;
680 BT_PUT(event_class);
681
682 /*
683 * Put what was not moved in
684 * bt_ctf_validation_replace_types().
685 */
686 bt_ctf_validation_output_put_types(&ec_validation_outputs[i]);
687 }
688
09840de5
PP
689 /*
690 * Freeze the trace and the stream class.
691 */
f67f3a6e 692 bt_ctf_stream_class_freeze(stream_class);
09840de5
PP
693 bt_ctf_trace_freeze(trace);
694
9b888ff3
JG
695 /* Notifiy listeners of the trace's schema modification. */
696 bt_ctf_stream_class_visit(stream_class,
d9a13d86 697 bt_ctf_trace_object_modification, trace);
ef0c4a15 698end:
d3814b54 699 if (ret) {
e6a8e8e4 700 bt_object_set_parent(stream_class, NULL);
09840de5
PP
701
702 if (ec_validation_outputs) {
6474e016 703 for (i = 0; i < event_class_count; i++) {
09840de5
PP
704 bt_ctf_validation_output_put_types(
705 &ec_validation_outputs[i]);
706 }
707 }
d3814b54 708 }
09840de5
PP
709
710 g_free(ec_validation_outputs);
711 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
2789e5d9 712 bt_put(current_parent_trace);
09840de5
PP
713 assert(!packet_header_type);
714 assert(!packet_context_type);
715 assert(!event_header_type);
716 assert(!stream_event_ctx_type);
717
ef0c4a15
JG
718 return ret;
719}
720
721int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
722{
723 int ret;
724
725 if (!trace) {
726 ret = -1;
727 goto end;
728 }
729
730 ret = trace->stream_classes->len;
731end:
732 return ret;
733}
734
735struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class(
736 struct bt_ctf_trace *trace, int index)
737{
738 struct bt_ctf_stream_class *stream_class = NULL;
739
740 if (!trace || index < 0 || index >= trace->stream_classes->len) {
741 goto end;
742 }
743
744 stream_class = g_ptr_array_index(trace->stream_classes, index);
83509119 745 bt_get(stream_class);
ef0c4a15
JG
746end:
747 return stream_class;
748}
749
4841ccc1
PP
750struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
751 struct bt_ctf_trace *trace, uint32_t id)
752{
753 int i;
754 struct bt_ctf_stream_class *stream_class = NULL;
755
756 if (!trace) {
757 goto end;
758 }
759
6474e016 760 for (i = 0; i < trace->stream_classes->len; i++) {
4841ccc1
PP
761 struct bt_ctf_stream_class *stream_class_candidate;
762
763 stream_class_candidate =
764 g_ptr_array_index(trace->stream_classes, i);
765
766 if (bt_ctf_stream_class_get_id(stream_class_candidate) ==
767 (int64_t) id) {
768 stream_class = stream_class_candidate;
83509119 769 bt_get(stream_class);
4841ccc1
PP
770 goto end;
771 }
772 }
773
774end:
775 return stream_class;
776}
777
ac0c6bdd 778struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
7e4347a3
PP
779 struct bt_ctf_trace *trace, const char *name)
780{
781 size_t i;
ac0c6bdd 782 struct bt_ctf_clock_class *clock_class = NULL;
7e4347a3
PP
783
784 if (!trace || !name) {
785 goto end;
786 }
787
6474e016 788 for (i = 0; i < trace->clocks->len; i++) {
ac0c6bdd 789 struct bt_ctf_clock_class *cur_clk =
7e4347a3 790 g_ptr_array_index(trace->clocks, i);
ac0c6bdd 791 const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk);
7e4347a3
PP
792
793 if (!cur_clk_name) {
794 goto end;
795 }
796
797 if (!strcmp(cur_clk_name, name)) {
ac0c6bdd
PP
798 clock_class = cur_clk;
799 bt_get(clock_class);
7e4347a3
PP
800 goto end;
801 }
802 }
803
804end:
ac0c6bdd 805 return clock_class;
7e4347a3
PP
806}
807
bc37ae52 808BT_HIDDEN
dc3fffef 809const char *get_byte_order_string(enum bt_ctf_byte_order byte_order)
bc37ae52
JG
810{
811 const char *string;
812
813 switch (byte_order) {
dc3fffef 814 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
bc37ae52
JG
815 string = "le";
816 break;
dc3fffef 817 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
bc37ae52
JG
818 string = "be";
819 break;
dc3fffef
PP
820 case BT_CTF_BYTE_ORDER_NATIVE:
821 string = "native";
bc37ae52 822 break;
dc3fffef
PP
823 default:
824 assert(false);
bc37ae52
JG
825 }
826
827 return string;
828}
829
830static
831int append_trace_metadata(struct bt_ctf_trace *trace,
832 struct metadata_context *context)
833{
834 unsigned char *uuid = trace->uuid;
835 int ret;
836
837 g_string_append(context->string, "trace {\n");
838
839 g_string_append(context->string, "\tmajor = 1;\n");
840 g_string_append(context->string, "\tminor = 8;\n");
dc3fffef
PP
841 assert(trace->native_byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ||
842 trace->native_byte_order == BT_CTF_BYTE_ORDER_BIG_ENDIAN ||
843 trace->native_byte_order == BT_CTF_BYTE_ORDER_NETWORK);
bc37ae52
JG
844 g_string_append_printf(context->string,
845 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
846 uuid[0], uuid[1], uuid[2], uuid[3],
847 uuid[4], uuid[5], uuid[6], uuid[7],
848 uuid[8], uuid[9], uuid[10], uuid[11],
849 uuid[12], uuid[13], uuid[14], uuid[15]);
850 g_string_append_printf(context->string, "\tbyte_order = %s;\n",
dc3fffef 851 get_byte_order_string(trace->native_byte_order));
bc37ae52
JG
852
853 g_string_append(context->string, "\tpacket.header := ");
854 context->current_indentation_level++;
855 g_string_assign(context->field_name, "");
d246b111 856 ret = bt_ctf_field_type_serialize(trace->packet_header_type,
bc37ae52
JG
857 context);
858 if (ret) {
859 goto end;
860 }
861 context->current_indentation_level--;
862
863 g_string_append(context->string, ";\n};\n\n");
864end:
865 return ret;
866}
867
bc37ae52
JG
868static
869void append_env_metadata(struct bt_ctf_trace *trace,
870 struct metadata_context *context)
871{
7f800dc7
PP
872 int i;
873 int env_size;
874
875 env_size = bt_ctf_attributes_get_count(trace->environment);
876
877 if (env_size <= 0) {
bc37ae52
JG
878 return;
879 }
880
881 g_string_append(context->string, "env {\n");
7f800dc7 882
6474e016 883 for (i = 0; i < env_size; i++) {
dac5c838 884 struct bt_value *env_field_value_obj = NULL;
7f800dc7 885 const char *entry_name;
7f800dc7
PP
886
887 entry_name = bt_ctf_attributes_get_field_name(
888 trace->environment, i);
889 env_field_value_obj = bt_ctf_attributes_get_field_value(
890 trace->environment, i);
891
892 if (!entry_name || !env_field_value_obj) {
893 goto loop_next;
894 }
895
dac5c838
PP
896 switch (bt_value_get_type(env_field_value_obj)) {
897 case BT_VALUE_TYPE_INTEGER:
b8248cc0
PP
898 {
899 int ret;
900 int64_t int_value;
901
dac5c838 902 ret = bt_value_integer_get(env_field_value_obj,
7f800dc7
PP
903 &int_value);
904
905 if (ret) {
906 goto loop_next;
907 }
908
909 g_string_append_printf(context->string,
910 "\t%s = %" PRId64 ";\n", entry_name,
911 int_value);
912 break;
b8248cc0 913 }
dac5c838 914 case BT_VALUE_TYPE_STRING:
7f800dc7
PP
915 {
916 int ret;
917 const char *str_value;
918 char *escaped_str = NULL;
919
dac5c838 920 ret = bt_value_string_get(env_field_value_obj,
7f800dc7
PP
921 &str_value);
922
923 if (ret) {
924 goto loop_next;
925 }
926
927 escaped_str = g_strescape(str_value, NULL);
928
929 if (!escaped_str) {
930 goto loop_next;
931 }
932
933 g_string_append_printf(context->string,
934 "\t%s = \"%s\";\n", entry_name, escaped_str);
935 free(escaped_str);
936 break;
937 }
938
939 default:
940 goto loop_next;
941 }
942
943loop_next:
83509119 944 BT_PUT(env_field_value_obj);
7f800dc7
PP
945 }
946
bc37ae52
JG
947 g_string_append(context->string, "};\n\n");
948}
949
950char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
951{
952 char *metadata = NULL;
953 struct metadata_context *context = NULL;
954 int err = 0;
955 size_t i;
956
957 if (!trace) {
958 goto end;
959 }
960
961 context = g_new0(struct metadata_context, 1);
962 if (!context) {
963 goto end;
964 }
965
966 context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
967 context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
968 g_string_append(context->string, "/* CTF 1.8 */\n\n");
969 if (append_trace_metadata(trace, context)) {
970 goto error;
971 }
972 append_env_metadata(trace, context);
973 g_ptr_array_foreach(trace->clocks,
ac0c6bdd 974 (GFunc)bt_ctf_clock_class_serialize, context);
bc37ae52
JG
975
976 for (i = 0; i < trace->stream_classes->len; i++) {
977 err = bt_ctf_stream_class_serialize(
978 trace->stream_classes->pdata[i], context);
979 if (err) {
980 goto error;
981 }
982 }
983
984 metadata = context->string->str;
985error:
986 g_string_free(context->string, err ? TRUE : FALSE);
987 g_string_free(context->field_name, TRUE);
988 g_free(context);
989end:
990 return metadata;
991}
992
4ed90fb3
JG
993enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace)
994{
995 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
996
997 if (!trace) {
998 goto end;
999 }
1000
dc3fffef
PP
1001 ret = trace->native_byte_order;
1002
4ed90fb3
JG
1003end:
1004 return ret;
1005}
1006
391c8f0d 1007int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace,
bc37ae52
JG
1008 enum bt_ctf_byte_order byte_order)
1009{
1010 int ret = 0;
bc37ae52
JG
1011
1012 if (!trace || trace->frozen) {
1013 ret = -1;
1014 goto end;
1015 }
1016
dc3fffef
PP
1017 if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN &&
1018 byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN &&
1019 byte_order != BT_CTF_BYTE_ORDER_NETWORK) {
bc37ae52
JG
1020 ret = -1;
1021 goto end;
1022 }
1023
dc3fffef
PP
1024 trace->native_byte_order = byte_order;
1025
bc37ae52
JG
1026end:
1027 return ret;
1028}
1029
d246b111
JG
1030struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
1031 struct bt_ctf_trace *trace)
1032{
1033 struct bt_ctf_field_type *field_type = NULL;
1034
1035 if (!trace) {
1036 goto end;
1037 }
1038
83509119 1039 bt_get(trace->packet_header_type);
d246b111
JG
1040 field_type = trace->packet_header_type;
1041end:
1042 return field_type;
1043}
1044
1045int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
1046 struct bt_ctf_field_type *packet_header_type)
1047{
1048 int ret = 0;
1049
835b2d10 1050 if (!trace || trace->frozen) {
d246b111
JG
1051 ret = -1;
1052 goto end;
1053 }
1054
835b2d10
JG
1055 /* packet_header_type must be a structure. */
1056 if (packet_header_type &&
1057 bt_ctf_field_type_get_type_id(packet_header_type) !=
1487a16a 1058 BT_CTF_FIELD_TYPE_ID_STRUCT) {
d246b111
JG
1059 ret = -1;
1060 goto end;
1061 }
1062
83509119 1063 bt_put(trace->packet_header_type);
835b2d10 1064 trace->packet_header_type = bt_get(packet_header_type);
d246b111
JG
1065end:
1066 return ret;
1067}
1068
8bf65fbd
JG
1069static
1070int get_stream_class_count(void *element)
1071{
1072 return bt_ctf_trace_get_stream_class_count(
1073 (struct bt_ctf_trace *) element);
1074}
1075
1076static
1077void *get_stream_class(void *element, int i)
1078{
1079 return bt_ctf_trace_get_stream_class(
1080 (struct bt_ctf_trace *) element, i);
1081}
1082
1083static
d9a13d86 1084int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data)
8bf65fbd 1085{
d9a13d86 1086 return bt_ctf_stream_class_visit(object, visitor, data);
8bf65fbd
JG
1087}
1088
1089int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
d9a13d86 1090 bt_ctf_visitor visitor, void *data)
8bf65fbd
JG
1091{
1092 int ret;
d9a13d86
PP
1093 struct bt_ctf_object obj =
1094 { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE };
8bf65fbd
JG
1095
1096 if (!trace || !visitor) {
1097 ret = -1;
1098 goto end;
1099 }
1100
d9a13d86 1101 ret = visitor_helper(&obj, get_stream_class_count,
8bf65fbd
JG
1102 get_stream_class, visit_stream_class, visitor, data);
1103end:
1104 return ret;
1105}
1106
1107static
d9a13d86 1108int invoke_listener(struct bt_ctf_object *object, void *data)
8bf65fbd 1109{
9b888ff3 1110 struct listener_wrapper *listener_wrapper = data;
8bf65fbd 1111
d9a13d86 1112 listener_wrapper->listener(object, listener_wrapper->data);
9b888ff3 1113 return 0;
8bf65fbd
JG
1114}
1115
50ad4244
JG
1116int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace,
1117 bt_ctf_listener_cb listener, void *listener_data)
2204c381
JG
1118{
1119 int ret = 0;
50ad4244
JG
1120 struct listener_wrapper *listener_wrapper =
1121 g_new0(struct listener_wrapper, 1);
2204c381 1122
50ad4244 1123 if (!trace || !listener || !listener_wrapper) {
2204c381
JG
1124 ret = -1;
1125 goto error;
1126 }
1127
50ad4244
JG
1128 listener_wrapper->listener = listener;
1129 listener_wrapper->data = listener_data;
8bf65fbd 1130
50ad4244 1131 /* Visit the current schema. */
9b888ff3 1132 ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper);
8bf65fbd
JG
1133 if (ret) {
1134 goto error;
1135 }
1136
50ad4244
JG
1137 /*
1138 * Add listener to the array of callbacks which will be invoked on
1139 * schema changes.
1140 */
1141 g_ptr_array_add(trace->listeners, listener_wrapper);
2204c381
JG
1142 return ret;
1143error:
50ad4244 1144 g_free(listener_wrapper);
2204c381
JG
1145 return ret;
1146}
1147
bc37ae52 1148BT_HIDDEN
d9a13d86 1149int bt_ctf_trace_object_modification(struct bt_ctf_object *object,
9b888ff3
JG
1150 void *trace_ptr)
1151{
1152 size_t i;
1153 struct bt_ctf_trace *trace = trace_ptr;
1154
1155 assert(trace);
d9a13d86 1156 assert(object);
9b888ff3
JG
1157
1158 if (trace->listeners->len == 0) {
1159 goto end;
1160 }
1161
1162 for (i = 0; i < trace->listeners->len; i++) {
1163 struct listener_wrapper *listener =
1164 g_ptr_array_index(trace->listeners, i);
1165
d9a13d86 1166 listener->listener(object, listener->data);
9b888ff3
JG
1167 }
1168end:
1169 return 0;
1170}
1171
1172BT_HIDDEN
bc37ae52
JG
1173struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
1174{
a25e0d14 1175 int ret;
bc37ae52 1176 unsigned int alignment, size;
8bdcb82e 1177 struct bt_ctf_field_type *field_type = NULL;
bc37ae52
JG
1178
1179 if (alias >= NR_FIELD_TYPE_ALIAS) {
8bdcb82e 1180 goto end;
bc37ae52
JG
1181 }
1182
1183 alignment = field_type_aliases_alignments[alias];
1184 size = field_type_aliases_sizes[alias];
1185 field_type = bt_ctf_field_type_integer_create(size);
a25e0d14
JG
1186 ret = bt_ctf_field_type_set_alignment(field_type, alignment);
1187 if (ret) {
1188 BT_PUT(field_type);
1189 }
8bdcb82e 1190end:
bc37ae52
JG
1191 return field_type;
1192}
1193
f67f3a6e 1194static
09840de5 1195void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
f67f3a6e 1196{
6474e016
PP
1197 int i;
1198
09840de5 1199 bt_ctf_field_type_freeze(trace->packet_header_type);
f67f3a6e 1200 bt_ctf_attributes_freeze(trace->environment);
6474e016
PP
1201
1202 for (i = 0; i < trace->clocks->len; i++) {
ac0c6bdd 1203 struct bt_ctf_clock_class *clock_class =
6474e016
PP
1204 g_ptr_array_index(trace->clocks, i);
1205
ac0c6bdd 1206 bt_ctf_clock_class_freeze(clock_class);
6474e016
PP
1207 }
1208
f67f3a6e
JG
1209 trace->frozen = 1;
1210}
1211
bc37ae52
JG
1212static
1213int init_trace_packet_header(struct bt_ctf_trace *trace)
1214{
bc37ae52 1215 int ret = 0;
d246b111 1216 struct bt_ctf_field *magic = NULL, *uuid_array = NULL;
bc37ae52
JG
1217 struct bt_ctf_field_type *_uint32_t =
1218 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
1219 struct bt_ctf_field_type *_uint8_t =
1220 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
1221 struct bt_ctf_field_type *trace_packet_header_type =
1222 bt_ctf_field_type_structure_create();
1223 struct bt_ctf_field_type *uuid_array_type =
1224 bt_ctf_field_type_array_create(_uint8_t, 16);
1225
1226 if (!trace_packet_header_type || !uuid_array_type) {
1227 ret = -1;
1228 goto end;
1229 }
1230
bc37ae52
JG
1231 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1232 _uint32_t, "magic");
1233 if (ret) {
1234 goto end;
1235 }
1236
1237 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1238 uuid_array_type, "uuid");
1239 if (ret) {
1240 goto end;
1241 }
1242
1243 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1244 _uint32_t, "stream_id");
1245 if (ret) {
1246 goto end;
1247 }
1248
662e778c
JG
1249 ret = bt_ctf_trace_set_packet_header_type(trace,
1250 trace_packet_header_type);
1251 if (ret) {
1252 goto end;
1253 }
bc37ae52 1254end:
83509119
JG
1255 bt_put(uuid_array_type);
1256 bt_put(_uint32_t);
1257 bt_put(_uint8_t);
1258 bt_put(magic);
1259 bt_put(uuid_array);
1260 bt_put(trace_packet_header_type);
bc37ae52
JG
1261 return ret;
1262}
This page took 0.098799 seconds and 4 git commands to generate.