2 * SPDX-License-Identifier: MIT
4 * Copyright 2014 EfficiOS Inc.
6 * The Common Trace Format (CTF) Specification is available at
7 * http://www.efficios.com/ctf
10 #ifndef BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
11 #define BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
13 #include "common/macros.h"
14 #include "common/uuid.h"
15 #include <babeltrace2-ctf-writer/field-types.h>
16 #include <babeltrace2-ctf-writer/fields.h>
17 #include <babeltrace2-ctf-writer/trace.h>
18 #include <babeltrace2/types.h>
20 #include <sys/types.h>
22 #include "assert-pre.h"
23 #include "attributes.h"
24 #include "clock-class.h"
26 #include "stream-class.h"
27 #include "validation.h"
30 struct bt_ctf_trace_common
{
31 struct bt_ctf_object base
;
36 enum bt_ctf_byte_order native_byte_order
;
37 struct bt_ctf_private_value
*environment
;
38 GPtrArray
*clock_classes
; /* Array of pointers to bt_ctf_clock_class */
39 GPtrArray
*stream_classes
; /* Array of ptrs to bt_ctf_stream_class_common */
40 GPtrArray
*streams
; /* Array of ptrs to bt_ctf_stream_common */
41 struct bt_ctf_field_type_common
*packet_header_field_type
;
42 int64_t next_stream_id
;
45 * This flag indicates if the trace is valid. A valid
46 * trace is _always_ frozen.
51 bt_ctf_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
52 struct bt_ctf_clock_class
*clock_class
);
54 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
55 bt_ctf_object_release_func release_func
);
57 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
);
60 const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common
*trace
)
62 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
63 return trace
->name
? trace
->name
->str
: NULL
;
66 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
);
69 const uint8_t *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common
*trace
)
71 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
72 return trace
->uuid_set
? trace
->uuid
: NULL
;
75 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
, const uint8_t *uuid
);
77 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
78 const char *name
, struct bt_ctf_private_value
*value
);
80 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
81 const char *name
, const char *value
);
83 int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common
*trace
,
84 const char *name
, int64_t value
);
87 int64_t bt_ctf_trace_common_get_environment_field_count(
88 struct bt_ctf_trace_common
*trace
)
92 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
93 ret
= bt_ctf_attributes_get_count(trace
->environment
);
94 BT_ASSERT_DBG(ret
>= 0);
100 bt_ctf_trace_common_get_environment_field_name_by_index(
101 struct bt_ctf_trace_common
*trace
, uint64_t index
)
103 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
104 return bt_ctf_attributes_get_field_name(trace
->environment
, index
);
108 struct bt_ctf_private_value
*
109 bt_ctf_trace_common_borrow_environment_field_value_by_index(
110 struct bt_ctf_trace_common
*trace
, uint64_t index
)
112 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
113 return bt_ctf_attributes_borrow_field_value(trace
->environment
, index
);
117 struct bt_ctf_private_value
*
118 bt_ctf_trace_common_borrow_environment_field_value_by_name(
119 struct bt_ctf_trace_common
*trace
, const char *name
)
121 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
122 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Name");
123 return bt_ctf_attributes_borrow_field_value_by_name(trace
->environment
,
127 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
128 struct bt_ctf_clock_class
*clock_class
);
131 int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common
*trace
)
133 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
134 return trace
->clock_classes
->len
;
138 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_index(
139 struct bt_ctf_trace_common
*trace
, uint64_t index
)
141 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
142 BT_CTF_ASSERT_PRE(index
< trace
->clock_classes
->len
,
143 "Index is out of bounds: index=%" PRIu64
", "
145 index
, trace
->clock_classes
->len
);
146 return g_ptr_array_index(trace
->clock_classes
, index
);
150 int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common
*trace
)
152 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
153 return (int64_t) trace
->streams
->len
;
157 struct bt_ctf_stream_common
*bt_ctf_trace_common_borrow_stream_by_index(
158 struct bt_ctf_trace_common
*trace
,
161 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
162 BT_CTF_ASSERT_PRE(index
< trace
->streams
->len
,
163 "Index is out of bounds: index=%" PRIu64
", "
165 index
, trace
->streams
->len
);
166 return g_ptr_array_index(trace
->streams
, index
);
170 int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common
*trace
)
172 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
173 return (int64_t) trace
->stream_classes
->len
;
177 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_index(
178 struct bt_ctf_trace_common
*trace
, uint64_t index
)
180 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
181 BT_CTF_ASSERT_PRE(index
< trace
->stream_classes
->len
,
182 "Index is out of bounds: index=%" PRIu64
", "
184 index
, trace
->stream_classes
->len
);
185 return g_ptr_array_index(trace
->stream_classes
, index
);
189 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_id(
190 struct bt_ctf_trace_common
*trace
, uint64_t id_param
)
193 struct bt_ctf_stream_class_common
*stream_class
= NULL
;
194 int64_t id
= (int64_t) id_param
;
196 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
197 BT_CTF_ASSERT_PRE(id
>= 0,
198 "Invalid stream class ID: %" PRIu64
, id_param
);
200 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
201 struct bt_ctf_stream_class_common
*stream_class_candidate
;
203 stream_class_candidate
=
204 g_ptr_array_index(trace
->stream_classes
, i
);
206 if (bt_ctf_stream_class_common_get_id(stream_class_candidate
) ==
208 stream_class
= stream_class_candidate
;
218 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_name(
219 struct bt_ctf_trace_common
*trace
, const char *name
)
222 struct bt_ctf_clock_class
*clock_class
= NULL
;
224 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
225 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Name");
227 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
228 struct bt_ctf_clock_class
*cur_clk
=
229 g_ptr_array_index(trace
->clock_classes
, i
);
230 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
236 if (!strcmp(cur_clk_name
, name
)) {
237 clock_class
= cur_clk
;
247 enum bt_ctf_byte_order
bt_ctf_trace_common_get_native_byte_order(
248 struct bt_ctf_trace_common
*trace
)
250 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
251 return trace
->native_byte_order
;
254 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
255 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
);
258 struct bt_ctf_field_type_common
*bt_ctf_trace_common_borrow_packet_header_field_type(
259 struct bt_ctf_trace_common
*trace
)
261 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
262 return trace
->packet_header_field_type
;
265 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
266 struct bt_ctf_field_type_common
*packet_header_field_type
);
269 void bt_ctf_trace_common_freeze(struct bt_ctf_trace_common
*trace
)
277 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
278 trace
, bt_ctf_trace_common_get_name(trace
));
279 BT_LOGD_STR("Freezing packet header field type.");
280 bt_ctf_field_type_common_freeze(trace
->packet_header_field_type
);
281 BT_LOGD_STR("Freezing environment attributes.");
282 bt_ctf_attributes_freeze(trace
->environment
);
284 if (trace
->clock_classes
->len
> 0) {
285 BT_LOGD_STR("Freezing clock classes.");
288 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
289 struct bt_ctf_clock_class
*clock_class
=
290 g_ptr_array_index(trace
->clock_classes
, i
);
292 bt_ctf_clock_class_freeze(clock_class
);
298 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
299 struct bt_ctf_stream_class_common
*stream_class
,
300 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
301 struct bt_ctf_clock_class
*init_expected_clock_class
,
302 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
303 struct bt_ctf_field_type_common
*packet_context_field_type
,
304 struct bt_ctf_field_type_common
*event_header_field_type
),
305 bool check_ts_begin_end_mapped
);
307 struct bt_ctf_trace
{
308 struct bt_ctf_trace_common common
;
312 * bt_ctf_trace_get_metadata_string: get metadata string.
314 * Get the trace's TSDL metadata. The caller assumes the ownership of the
317 * @param trace Trace instance.
319 * Returns the metadata string on success, NULL on error.
321 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
);
323 struct bt_ctf_trace
*bt_ctf_trace_create(void);
325 int64_t bt_ctf_trace_get_clock_class_count(
326 struct bt_ctf_trace
*trace
);
328 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
329 struct bt_ctf_trace
*trace
, uint64_t index
);
331 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
332 struct bt_ctf_trace
*trace
, const char *name
);
334 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
335 struct bt_ctf_clock_class
*clock_class
);
337 int64_t bt_ctf_trace_get_environment_field_count(
338 struct bt_ctf_trace
*trace
);
340 const char *bt_ctf_trace_get_environment_field_name_by_index(
341 struct bt_ctf_trace
*trace
, uint64_t index
);
343 struct bt_ctf_value
*
344 bt_ctf_trace_get_environment_field_value_by_index(struct bt_ctf_trace
*trace
,
347 struct bt_ctf_value
*
348 bt_ctf_trace_get_environment_field_value_by_name(
349 struct bt_ctf_trace
*trace
, const char *name
);
351 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
352 bt_ctf_visitor visitor
, void *data
);
354 #endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */