1 #ifndef BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
2 #define BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
5 * Copyright 2014 EfficiOS Inc.
7 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * The Common Trace Format (CTF) Specification is available at
28 * http://www.efficios.com/ctf
31 #include "common/macros.h"
32 #include "common/uuid.h"
33 #include <babeltrace2-ctf-writer/field-types.h>
34 #include <babeltrace2-ctf-writer/fields.h>
35 #include <babeltrace2-ctf-writer/trace.h>
36 #include <babeltrace2/types.h>
38 #include <sys/types.h>
40 #include "assert-pre.h"
41 #include "attributes.h"
42 #include "clock-class.h"
44 #include "stream-class.h"
45 #include "validation.h"
48 struct bt_ctf_trace_common
{
49 struct bt_ctf_object base
;
54 enum bt_ctf_byte_order native_byte_order
;
55 struct bt_ctf_private_value
*environment
;
56 GPtrArray
*clock_classes
; /* Array of pointers to bt_ctf_clock_class */
57 GPtrArray
*stream_classes
; /* Array of ptrs to bt_ctf_stream_class_common */
58 GPtrArray
*streams
; /* Array of ptrs to bt_ctf_stream_common */
59 struct bt_ctf_field_type_common
*packet_header_field_type
;
60 int64_t next_stream_id
;
63 * This flag indicates if the trace is valid. A valid
64 * trace is _always_ frozen.
70 bt_ctf_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
71 struct bt_ctf_clock_class
*clock_class
);
74 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
75 bt_ctf_object_release_func release_func
);
78 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
);
81 const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common
*trace
)
83 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
84 return trace
->name
? trace
->name
->str
: NULL
;
88 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
);
91 const uint8_t *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common
*trace
)
93 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
94 return trace
->uuid_set
? trace
->uuid
: NULL
;
98 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
, const uint8_t *uuid
);
101 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
102 const char *name
, struct bt_ctf_private_value
*value
);
105 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
106 const char *name
, const char *value
);
109 int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common
*trace
,
110 const char *name
, int64_t value
);
113 int64_t bt_ctf_trace_common_get_environment_field_count(
114 struct bt_ctf_trace_common
*trace
)
118 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
119 ret
= bt_ctf_attributes_get_count(trace
->environment
);
120 BT_ASSERT_DBG(ret
>= 0);
126 bt_ctf_trace_common_get_environment_field_name_by_index(
127 struct bt_ctf_trace_common
*trace
, uint64_t index
)
129 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
130 return bt_ctf_attributes_get_field_name(trace
->environment
, index
);
134 struct bt_ctf_private_value
*
135 bt_ctf_trace_common_borrow_environment_field_value_by_index(
136 struct bt_ctf_trace_common
*trace
, uint64_t index
)
138 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
139 return bt_ctf_attributes_borrow_field_value(trace
->environment
, index
);
143 struct bt_ctf_private_value
*
144 bt_ctf_trace_common_borrow_environment_field_value_by_name(
145 struct bt_ctf_trace_common
*trace
, const char *name
)
147 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
148 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Name");
149 return bt_ctf_attributes_borrow_field_value_by_name(trace
->environment
,
154 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
155 struct bt_ctf_clock_class
*clock_class
);
158 int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common
*trace
)
160 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
161 return trace
->clock_classes
->len
;
165 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_index(
166 struct bt_ctf_trace_common
*trace
, uint64_t index
)
168 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
169 BT_CTF_ASSERT_PRE(index
< trace
->clock_classes
->len
,
170 "Index is out of bounds: index=%" PRIu64
", "
172 index
, trace
->clock_classes
->len
);
173 return g_ptr_array_index(trace
->clock_classes
, index
);
177 int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common
*trace
)
179 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
180 return (int64_t) trace
->streams
->len
;
184 struct bt_ctf_stream_common
*bt_ctf_trace_common_borrow_stream_by_index(
185 struct bt_ctf_trace_common
*trace
,
188 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
189 BT_CTF_ASSERT_PRE(index
< trace
->streams
->len
,
190 "Index is out of bounds: index=%" PRIu64
", "
192 index
, trace
->streams
->len
);
193 return g_ptr_array_index(trace
->streams
, index
);
197 int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common
*trace
)
199 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
200 return (int64_t) trace
->stream_classes
->len
;
204 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_index(
205 struct bt_ctf_trace_common
*trace
, uint64_t index
)
207 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
208 BT_CTF_ASSERT_PRE(index
< trace
->stream_classes
->len
,
209 "Index is out of bounds: index=%" PRIu64
", "
211 index
, trace
->stream_classes
->len
);
212 return g_ptr_array_index(trace
->stream_classes
, index
);
216 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_id(
217 struct bt_ctf_trace_common
*trace
, uint64_t id_param
)
220 struct bt_ctf_stream_class_common
*stream_class
= NULL
;
221 int64_t id
= (int64_t) id_param
;
223 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
224 BT_CTF_ASSERT_PRE(id
>= 0,
225 "Invalid stream class ID: %" PRIu64
, id_param
);
227 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
228 struct bt_ctf_stream_class_common
*stream_class_candidate
;
230 stream_class_candidate
=
231 g_ptr_array_index(trace
->stream_classes
, i
);
233 if (bt_ctf_stream_class_common_get_id(stream_class_candidate
) ==
235 stream_class
= stream_class_candidate
;
245 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_name(
246 struct bt_ctf_trace_common
*trace
, const char *name
)
249 struct bt_ctf_clock_class
*clock_class
= NULL
;
251 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
252 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Name");
254 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
255 struct bt_ctf_clock_class
*cur_clk
=
256 g_ptr_array_index(trace
->clock_classes
, i
);
257 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
263 if (!strcmp(cur_clk_name
, name
)) {
264 clock_class
= cur_clk
;
274 enum bt_ctf_byte_order
bt_ctf_trace_common_get_native_byte_order(
275 struct bt_ctf_trace_common
*trace
)
277 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
278 return trace
->native_byte_order
;
282 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
283 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
);
286 struct bt_ctf_field_type_common
*bt_ctf_trace_common_borrow_packet_header_field_type(
287 struct bt_ctf_trace_common
*trace
)
289 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
290 return trace
->packet_header_field_type
;
294 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
295 struct bt_ctf_field_type_common
*packet_header_field_type
);
298 void bt_ctf_trace_common_freeze(struct bt_ctf_trace_common
*trace
)
306 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
307 trace
, bt_ctf_trace_common_get_name(trace
));
308 BT_LOGD_STR("Freezing packet header field type.");
309 bt_ctf_field_type_common_freeze(trace
->packet_header_field_type
);
310 BT_LOGD_STR("Freezing environment attributes.");
311 bt_ctf_attributes_freeze(trace
->environment
);
313 if (trace
->clock_classes
->len
> 0) {
314 BT_LOGD_STR("Freezing clock classes.");
317 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
318 struct bt_ctf_clock_class
*clock_class
=
319 g_ptr_array_index(trace
->clock_classes
, i
);
321 bt_ctf_clock_class_freeze(clock_class
);
328 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
329 struct bt_ctf_stream_class_common
*stream_class
,
330 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
331 struct bt_ctf_clock_class
*init_expected_clock_class
,
332 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
333 struct bt_ctf_field_type_common
*packet_context_field_type
,
334 struct bt_ctf_field_type_common
*event_header_field_type
),
335 bool check_ts_begin_end_mapped
);
337 struct bt_ctf_trace
{
338 struct bt_ctf_trace_common common
;
342 * bt_ctf_trace_get_metadata_string: get metadata string.
344 * Get the trace's TSDL metadata. The caller assumes the ownership of the
347 * @param trace Trace instance.
349 * Returns the metadata string on success, NULL on error.
352 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
);
355 struct bt_ctf_trace
*bt_ctf_trace_create(void);
358 int64_t bt_ctf_trace_get_clock_class_count(
359 struct bt_ctf_trace
*trace
);
362 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
363 struct bt_ctf_trace
*trace
, uint64_t index
);
366 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
367 struct bt_ctf_trace
*trace
, const char *name
);
370 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
371 struct bt_ctf_clock_class
*clock_class
);
374 int64_t bt_ctf_trace_get_environment_field_count(
375 struct bt_ctf_trace
*trace
);
378 const char *bt_ctf_trace_get_environment_field_name_by_index(
379 struct bt_ctf_trace
*trace
, uint64_t index
);
382 struct bt_ctf_value
*
383 bt_ctf_trace_get_environment_field_value_by_index(struct bt_ctf_trace
*trace
,
387 struct bt_ctf_value
*
388 bt_ctf_trace_get_environment_field_value_by_name(
389 struct bt_ctf_trace
*trace
, const char *name
);
391 #endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */