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