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