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