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>
19 #include <sys/types.h>
21 #include "assert-pre.h"
22 #include "attributes.h"
23 #include "clock-class.h"
25 #include "stream-class.h"
26 #include "validation.h"
29 struct bt_ctf_trace_common
{
30 struct bt_ctf_object base
;
35 enum bt_ctf_byte_order native_byte_order
;
36 struct bt_ctf_private_value
*environment
;
37 GPtrArray
*clock_classes
; /* Array of pointers to bt_ctf_clock_class */
38 GPtrArray
*stream_classes
; /* Array of ptrs to bt_ctf_stream_class_common */
39 GPtrArray
*streams
; /* Array of ptrs to bt_ctf_stream_common */
40 struct bt_ctf_field_type_common
*packet_header_field_type
;
41 int64_t next_stream_id
;
44 * This flag indicates if the trace is valid. A valid
45 * trace is _always_ frozen.
50 bt_ctf_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
51 struct bt_ctf_clock_class
*clock_class
);
53 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
54 bt_ctf_object_release_func release_func
);
56 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
);
59 const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common
*trace
)
61 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
62 return trace
->name
? trace
->name
->str
: NULL
;
65 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
);
68 const uint8_t *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common
*trace
)
70 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
71 return trace
->uuid_set
? trace
->uuid
: NULL
;
74 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
, const uint8_t *uuid
);
76 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
77 const char *name
, struct bt_ctf_private_value
*value
);
79 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
80 const char *name
, const char *value
);
82 int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common
*trace
,
83 const char *name
, int64_t value
);
86 int64_t bt_ctf_trace_common_get_environment_field_count(
87 struct bt_ctf_trace_common
*trace
)
91 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
92 ret
= bt_ctf_attributes_get_count(trace
->environment
);
93 BT_ASSERT_DBG(ret
>= 0);
99 bt_ctf_trace_common_get_environment_field_name_by_index(
100 struct bt_ctf_trace_common
*trace
, uint64_t index
)
102 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
103 return bt_ctf_attributes_get_field_name(trace
->environment
, index
);
107 struct bt_ctf_private_value
*
108 bt_ctf_trace_common_borrow_environment_field_value_by_index(
109 struct bt_ctf_trace_common
*trace
, uint64_t index
)
111 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
112 return bt_ctf_attributes_borrow_field_value(trace
->environment
, index
);
116 struct bt_ctf_private_value
*
117 bt_ctf_trace_common_borrow_environment_field_value_by_name(
118 struct bt_ctf_trace_common
*trace
, const char *name
)
120 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
121 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Name");
122 return bt_ctf_attributes_borrow_field_value_by_name(trace
->environment
,
126 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
127 struct bt_ctf_clock_class
*clock_class
);
130 int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common
*trace
)
132 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
133 return trace
->clock_classes
->len
;
137 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_index(
138 struct bt_ctf_trace_common
*trace
, uint64_t index
)
140 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
141 BT_CTF_ASSERT_PRE(index
< trace
->clock_classes
->len
,
142 "Index is out of bounds: index=%" PRIu64
", "
144 index
, trace
->clock_classes
->len
);
145 return g_ptr_array_index(trace
->clock_classes
, index
);
149 int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common
*trace
)
151 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
152 return (int64_t) trace
->streams
->len
;
156 struct bt_ctf_stream_common
*bt_ctf_trace_common_borrow_stream_by_index(
157 struct bt_ctf_trace_common
*trace
,
160 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
161 BT_CTF_ASSERT_PRE(index
< trace
->streams
->len
,
162 "Index is out of bounds: index=%" PRIu64
", "
164 index
, trace
->streams
->len
);
165 return g_ptr_array_index(trace
->streams
, index
);
169 int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common
*trace
)
171 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
172 return (int64_t) trace
->stream_classes
->len
;
176 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_index(
177 struct bt_ctf_trace_common
*trace
, uint64_t index
)
179 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
180 BT_CTF_ASSERT_PRE(index
< trace
->stream_classes
->len
,
181 "Index is out of bounds: index=%" PRIu64
", "
183 index
, trace
->stream_classes
->len
);
184 return g_ptr_array_index(trace
->stream_classes
, index
);
188 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_id(
189 struct bt_ctf_trace_common
*trace
, uint64_t id_param
)
192 struct bt_ctf_stream_class_common
*stream_class
= NULL
;
193 int64_t id
= (int64_t) id_param
;
195 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
196 BT_CTF_ASSERT_PRE(id
>= 0,
197 "Invalid stream class ID: %" PRIu64
, id_param
);
199 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
200 struct bt_ctf_stream_class_common
*stream_class_candidate
;
202 stream_class_candidate
=
203 g_ptr_array_index(trace
->stream_classes
, i
);
205 if (bt_ctf_stream_class_common_get_id(stream_class_candidate
) ==
207 stream_class
= stream_class_candidate
;
217 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_name(
218 struct bt_ctf_trace_common
*trace
, const char *name
)
221 struct bt_ctf_clock_class
*clock_class
= NULL
;
223 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
224 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Name");
226 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
227 struct bt_ctf_clock_class
*cur_clk
=
228 g_ptr_array_index(trace
->clock_classes
, i
);
229 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
235 if (!strcmp(cur_clk_name
, name
)) {
236 clock_class
= cur_clk
;
246 enum bt_ctf_byte_order
bt_ctf_trace_common_get_native_byte_order(
247 struct bt_ctf_trace_common
*trace
)
249 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
250 return trace
->native_byte_order
;
253 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
254 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
);
257 struct bt_ctf_field_type_common
*bt_ctf_trace_common_borrow_packet_header_field_type(
258 struct bt_ctf_trace_common
*trace
)
260 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
261 return trace
->packet_header_field_type
;
264 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
265 struct bt_ctf_field_type_common
*packet_header_field_type
);
268 void bt_ctf_trace_common_freeze(struct bt_ctf_trace_common
*trace
)
276 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
277 trace
, bt_ctf_trace_common_get_name(trace
));
278 BT_LOGD_STR("Freezing packet header field type.");
279 bt_ctf_field_type_common_freeze(trace
->packet_header_field_type
);
280 BT_LOGD_STR("Freezing environment attributes.");
281 bt_ctf_attributes_freeze(trace
->environment
);
283 if (trace
->clock_classes
->len
> 0) {
284 BT_LOGD_STR("Freezing clock classes.");
287 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
288 struct bt_ctf_clock_class
*clock_class
=
289 g_ptr_array_index(trace
->clock_classes
, i
);
291 bt_ctf_clock_class_freeze(clock_class
);
297 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
298 struct bt_ctf_stream_class_common
*stream_class
,
299 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
300 struct bt_ctf_clock_class
*init_expected_clock_class
,
301 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
302 struct bt_ctf_field_type_common
*packet_context_field_type
,
303 struct bt_ctf_field_type_common
*event_header_field_type
),
304 bool check_ts_begin_end_mapped
);
306 struct bt_ctf_trace
{
307 struct bt_ctf_trace_common common
;
311 * bt_ctf_trace_get_metadata_string: get metadata string.
313 * Get the trace's TSDL metadata. The caller assumes the ownership of the
316 * @param trace Trace instance.
318 * Returns the metadata string on success, NULL on error.
320 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
);
322 struct bt_ctf_trace
*bt_ctf_trace_create(void);
324 int64_t bt_ctf_trace_get_clock_class_count(
325 struct bt_ctf_trace
*trace
);
327 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
328 struct bt_ctf_trace
*trace
, uint64_t index
);
330 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
331 struct bt_ctf_trace
*trace
, const char *name
);
333 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
334 struct bt_ctf_clock_class
*clock_class
);
336 int64_t bt_ctf_trace_get_environment_field_count(
337 struct bt_ctf_trace
*trace
);
339 const char *bt_ctf_trace_get_environment_field_name_by_index(
340 struct bt_ctf_trace
*trace
, uint64_t index
);
342 struct bt_ctf_value
*
343 bt_ctf_trace_get_environment_field_value_by_index(struct bt_ctf_trace
*trace
,
346 struct bt_ctf_value
*
347 bt_ctf_trace_get_environment_field_value_by_name(
348 struct bt_ctf_trace
*trace
, const char *name
);
350 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
351 bt_ctf_visitor visitor
, void *data
);
353 #endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */