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.
52 bt_ctf_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
53 struct bt_ctf_clock_class
*clock_class
);
56 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
57 bt_ctf_object_release_func release_func
);
60 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
);
63 const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common
*trace
)
65 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
66 return trace
->name
? trace
->name
->str
: NULL
;
70 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
);
73 const uint8_t *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common
*trace
)
75 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
76 return trace
->uuid_set
? trace
->uuid
: NULL
;
80 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
, const uint8_t *uuid
);
83 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
84 const char *name
, struct bt_ctf_private_value
*value
);
87 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
88 const char *name
, const char *value
);
91 int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common
*trace
,
92 const char *name
, int64_t value
);
95 int64_t bt_ctf_trace_common_get_environment_field_count(
96 struct bt_ctf_trace_common
*trace
)
100 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
101 ret
= bt_ctf_attributes_get_count(trace
->environment
);
102 BT_ASSERT_DBG(ret
>= 0);
108 bt_ctf_trace_common_get_environment_field_name_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_get_field_name(trace
->environment
, index
);
116 struct bt_ctf_private_value
*
117 bt_ctf_trace_common_borrow_environment_field_value_by_index(
118 struct bt_ctf_trace_common
*trace
, uint64_t index
)
120 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
121 return bt_ctf_attributes_borrow_field_value(trace
->environment
, index
);
125 struct bt_ctf_private_value
*
126 bt_ctf_trace_common_borrow_environment_field_value_by_name(
127 struct bt_ctf_trace_common
*trace
, const char *name
)
129 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
130 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Name");
131 return bt_ctf_attributes_borrow_field_value_by_name(trace
->environment
,
136 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
137 struct bt_ctf_clock_class
*clock_class
);
140 int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common
*trace
)
142 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
143 return trace
->clock_classes
->len
;
147 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_index(
148 struct bt_ctf_trace_common
*trace
, uint64_t index
)
150 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
151 BT_CTF_ASSERT_PRE(index
< trace
->clock_classes
->len
,
152 "Index is out of bounds: index=%" PRIu64
", "
154 index
, trace
->clock_classes
->len
);
155 return g_ptr_array_index(trace
->clock_classes
, index
);
159 int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common
*trace
)
161 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
162 return (int64_t) trace
->streams
->len
;
166 struct bt_ctf_stream_common
*bt_ctf_trace_common_borrow_stream_by_index(
167 struct bt_ctf_trace_common
*trace
,
170 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
171 BT_CTF_ASSERT_PRE(index
< trace
->streams
->len
,
172 "Index is out of bounds: index=%" PRIu64
", "
174 index
, trace
->streams
->len
);
175 return g_ptr_array_index(trace
->streams
, index
);
179 int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common
*trace
)
181 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
182 return (int64_t) trace
->stream_classes
->len
;
186 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_index(
187 struct bt_ctf_trace_common
*trace
, uint64_t index
)
189 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
190 BT_CTF_ASSERT_PRE(index
< trace
->stream_classes
->len
,
191 "Index is out of bounds: index=%" PRIu64
", "
193 index
, trace
->stream_classes
->len
);
194 return g_ptr_array_index(trace
->stream_classes
, index
);
198 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_id(
199 struct bt_ctf_trace_common
*trace
, uint64_t id_param
)
202 struct bt_ctf_stream_class_common
*stream_class
= NULL
;
203 int64_t id
= (int64_t) id_param
;
205 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
206 BT_CTF_ASSERT_PRE(id
>= 0,
207 "Invalid stream class ID: %" PRIu64
, id_param
);
209 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
210 struct bt_ctf_stream_class_common
*stream_class_candidate
;
212 stream_class_candidate
=
213 g_ptr_array_index(trace
->stream_classes
, i
);
215 if (bt_ctf_stream_class_common_get_id(stream_class_candidate
) ==
217 stream_class
= stream_class_candidate
;
227 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_name(
228 struct bt_ctf_trace_common
*trace
, const char *name
)
231 struct bt_ctf_clock_class
*clock_class
= NULL
;
233 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
234 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Name");
236 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
237 struct bt_ctf_clock_class
*cur_clk
=
238 g_ptr_array_index(trace
->clock_classes
, i
);
239 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
245 if (!strcmp(cur_clk_name
, name
)) {
246 clock_class
= cur_clk
;
256 enum bt_ctf_byte_order
bt_ctf_trace_common_get_native_byte_order(
257 struct bt_ctf_trace_common
*trace
)
259 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
260 return trace
->native_byte_order
;
264 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
265 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
);
268 struct bt_ctf_field_type_common
*bt_ctf_trace_common_borrow_packet_header_field_type(
269 struct bt_ctf_trace_common
*trace
)
271 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
272 return trace
->packet_header_field_type
;
276 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
277 struct bt_ctf_field_type_common
*packet_header_field_type
);
280 void bt_ctf_trace_common_freeze(struct bt_ctf_trace_common
*trace
)
288 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
289 trace
, bt_ctf_trace_common_get_name(trace
));
290 BT_LOGD_STR("Freezing packet header field type.");
291 bt_ctf_field_type_common_freeze(trace
->packet_header_field_type
);
292 BT_LOGD_STR("Freezing environment attributes.");
293 bt_ctf_attributes_freeze(trace
->environment
);
295 if (trace
->clock_classes
->len
> 0) {
296 BT_LOGD_STR("Freezing clock classes.");
299 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
300 struct bt_ctf_clock_class
*clock_class
=
301 g_ptr_array_index(trace
->clock_classes
, i
);
303 bt_ctf_clock_class_freeze(clock_class
);
310 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
311 struct bt_ctf_stream_class_common
*stream_class
,
312 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
313 struct bt_ctf_clock_class
*init_expected_clock_class
,
314 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
315 struct bt_ctf_field_type_common
*packet_context_field_type
,
316 struct bt_ctf_field_type_common
*event_header_field_type
),
317 bool check_ts_begin_end_mapped
);
319 struct bt_ctf_trace
{
320 struct bt_ctf_trace_common common
;
324 * bt_ctf_trace_get_metadata_string: get metadata string.
326 * Get the trace's TSDL metadata. The caller assumes the ownership of the
329 * @param trace Trace instance.
331 * Returns the metadata string on success, NULL on error.
334 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
);
337 struct bt_ctf_trace
*bt_ctf_trace_create(void);
340 int64_t bt_ctf_trace_get_clock_class_count(
341 struct bt_ctf_trace
*trace
);
344 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
345 struct bt_ctf_trace
*trace
, uint64_t index
);
348 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
349 struct bt_ctf_trace
*trace
, const char *name
);
352 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
353 struct bt_ctf_clock_class
*clock_class
);
356 int64_t bt_ctf_trace_get_environment_field_count(
357 struct bt_ctf_trace
*trace
);
360 const char *bt_ctf_trace_get_environment_field_name_by_index(
361 struct bt_ctf_trace
*trace
, uint64_t index
);
364 struct bt_ctf_value
*
365 bt_ctf_trace_get_environment_field_value_by_index(struct bt_ctf_trace
*trace
,
369 struct bt_ctf_value
*
370 bt_ctf_trace_get_environment_field_value_by_name(
371 struct bt_ctf_trace
*trace
, const char *name
);
374 int bt_ctf_trace_visit(struct bt_ctf_trace
*trace
,
375 bt_ctf_visitor visitor
, void *data
);
377 #endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */