1 #ifndef BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
2 #define BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
5 * BabelTrace - CTF Writer: Trace
7 * Copyright 2014 EfficiOS Inc.
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The Common Trace Format (CTF) Specification is available at
30 * http://www.efficios.com/ctf
33 #include <babeltrace/assert-pre-internal.h>
34 #include <babeltrace/babeltrace-internal.h>
35 #include <babeltrace/compat/uuid-internal.h>
36 #include <babeltrace/ctf-writer/attributes-internal.h>
37 #include <babeltrace/ctf-writer/clock-class-internal.h>
38 #include <babeltrace/ctf-writer/field-types.h>
39 #include <babeltrace/ctf-writer/fields.h>
40 #include <babeltrace/ctf-writer/stream-class-internal.h>
41 #include <babeltrace/ctf-writer/trace.h>
42 #include <babeltrace/ctf-writer/validation-internal.h>
43 #include <babeltrace/object-internal.h>
44 #include <babeltrace/types.h>
45 #include <babeltrace/values.h>
46 #include <babeltrace/private-values.h>
48 #include <sys/types.h>
50 struct bt_ctf_trace_common
{
51 struct bt_object base
;
54 unsigned char uuid
[BABELTRACE_UUID_LEN
];
56 enum bt_ctf_byte_order native_byte_order
;
57 struct bt_private_value
*environment
;
58 GPtrArray
*clock_classes
; /* Array of pointers to bt_ctf_clock_class */
59 GPtrArray
*stream_classes
; /* Array of ptrs to bt_ctf_stream_class_common */
60 GPtrArray
*streams
; /* Array of ptrs to bt_ctf_stream_common */
61 struct bt_ctf_field_type_common
*packet_header_field_type
;
62 int64_t next_stream_id
;
65 * This flag indicates if the trace is valid. A valid
66 * trace is _always_ frozen.
72 bt_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
73 struct bt_ctf_clock_class
*clock_class
);
76 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
77 bt_object_release_func release_func
);
80 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
);
83 const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common
*trace
)
85 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
86 return trace
->name
? trace
->name
->str
: NULL
;
90 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
);
93 const unsigned char *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common
*trace
)
95 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
96 return trace
->uuid_set
? trace
->uuid
: NULL
;
100 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
, const unsigned char *uuid
);
103 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
104 const char *name
, struct bt_private_value
*value
);
107 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
108 const char *name
, const char *value
);
111 int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common
*trace
,
112 const char *name
, int64_t value
);
115 int64_t bt_ctf_trace_common_get_environment_field_count(
116 struct bt_ctf_trace_common
*trace
)
120 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
121 ret
= bt_ctf_attributes_get_count(trace
->environment
);
128 bt_ctf_trace_common_get_environment_field_name_by_index(
129 struct bt_ctf_trace_common
*trace
, uint64_t index
)
131 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
132 return bt_ctf_attributes_get_field_name(trace
->environment
, index
);
136 struct bt_private_value
*
137 bt_ctf_trace_common_borrow_environment_field_value_by_index(
138 struct bt_ctf_trace_common
*trace
, uint64_t index
)
140 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
141 return bt_ctf_attributes_borrow_field_value(trace
->environment
, index
);
145 struct bt_private_value
*
146 bt_ctf_trace_common_borrow_environment_field_value_by_name(
147 struct bt_ctf_trace_common
*trace
, const char *name
)
149 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
150 BT_ASSERT_PRE_NON_NULL(name
, "Name");
151 return bt_ctf_attributes_borrow_field_value_by_name(trace
->environment
,
156 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
157 struct bt_ctf_clock_class
*clock_class
);
160 int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common
*trace
)
162 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
163 return trace
->clock_classes
->len
;
167 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_index(
168 struct bt_ctf_trace_common
*trace
, uint64_t index
)
170 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
171 BT_ASSERT_PRE(index
< trace
->clock_classes
->len
,
172 "Index is out of bounds: index=%" PRIu64
", "
174 index
, trace
->clock_classes
->len
);
175 return g_ptr_array_index(trace
->clock_classes
, index
);
179 int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common
*trace
)
181 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
182 return (int64_t) trace
->streams
->len
;
186 struct bt_ctf_stream_common
*bt_ctf_trace_common_borrow_stream_by_index(
187 struct bt_ctf_trace_common
*trace
,
190 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
191 BT_ASSERT_PRE(index
< trace
->streams
->len
,
192 "Index is out of bounds: index=%" PRIu64
", "
194 index
, trace
->streams
->len
);
195 return g_ptr_array_index(trace
->streams
, index
);
199 int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common
*trace
)
201 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
202 return (int64_t) trace
->stream_classes
->len
;
206 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_index(
207 struct bt_ctf_trace_common
*trace
, uint64_t index
)
209 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
210 BT_ASSERT_PRE(index
< trace
->stream_classes
->len
,
211 "Index is out of bounds: index=%" PRIu64
", "
213 index
, trace
->stream_classes
->len
);
214 return g_ptr_array_index(trace
->stream_classes
, index
);
218 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_id(
219 struct bt_ctf_trace_common
*trace
, uint64_t id_param
)
222 struct bt_ctf_stream_class_common
*stream_class
= NULL
;
223 int64_t id
= (int64_t) id_param
;
225 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
226 BT_ASSERT_PRE(id
>= 0,
227 "Invalid stream class ID: %" PRIu64
, id_param
);
229 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
230 struct bt_ctf_stream_class_common
*stream_class_candidate
;
232 stream_class_candidate
=
233 g_ptr_array_index(trace
->stream_classes
, i
);
235 if (bt_ctf_stream_class_common_get_id(stream_class_candidate
) ==
237 stream_class
= stream_class_candidate
;
247 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_name(
248 struct bt_ctf_trace_common
*trace
, const char *name
)
251 struct bt_ctf_clock_class
*clock_class
= NULL
;
253 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
254 BT_ASSERT_PRE_NON_NULL(name
, "Name");
256 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
257 struct bt_ctf_clock_class
*cur_clk
=
258 g_ptr_array_index(trace
->clock_classes
, i
);
259 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
265 if (!strcmp(cur_clk_name
, name
)) {
266 clock_class
= cur_clk
;
276 enum bt_ctf_byte_order
bt_ctf_trace_common_get_native_byte_order(
277 struct bt_ctf_trace_common
*trace
)
279 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
280 return trace
->native_byte_order
;
284 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
285 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
);
288 struct bt_ctf_field_type_common
*bt_ctf_trace_common_borrow_packet_header_field_type(
289 struct bt_ctf_trace_common
*trace
)
291 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
292 return trace
->packet_header_field_type
;
296 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
297 struct bt_ctf_field_type_common
*packet_header_field_type
);
300 void bt_ctf_trace_common_freeze(struct bt_ctf_trace_common
*trace
)
308 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
309 trace
, bt_ctf_trace_common_get_name(trace
));
310 BT_LOGD_STR("Freezing packet header field type.");
311 bt_ctf_field_type_common_freeze(trace
->packet_header_field_type
);
312 BT_LOGD_STR("Freezing environment attributes.");
313 bt_ctf_attributes_freeze(trace
->environment
);
315 if (trace
->clock_classes
->len
> 0) {
316 BT_LOGD_STR("Freezing clock classes.");
319 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
320 struct bt_ctf_clock_class
*clock_class
=
321 g_ptr_array_index(trace
->clock_classes
, i
);
323 bt_ctf_clock_class_freeze(clock_class
);
330 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
331 struct bt_ctf_stream_class_common
*stream_class
,
332 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
333 struct bt_ctf_clock_class
*init_expected_clock_class
,
334 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
335 struct bt_ctf_field_type_common
*packet_context_field_type
,
336 struct bt_ctf_field_type_common
*event_header_field_type
),
337 bool check_ts_begin_end_mapped
);
339 struct bt_ctf_trace
{
340 struct bt_ctf_trace_common common
;
344 * bt_ctf_trace_get_metadata_string: get metadata string.
346 * Get the trace's TSDL metadata. The caller assumes the ownership of the
349 * @param trace Trace instance.
351 * Returns the metadata string on success, NULL on error.
354 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
);
357 struct bt_ctf_trace
*bt_ctf_trace_create(void);
360 int64_t bt_ctf_trace_get_clock_class_count(
361 struct bt_ctf_trace
*trace
);
364 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
365 struct bt_ctf_trace
*trace
, uint64_t index
);
368 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
369 struct bt_ctf_trace
*trace
, const char *name
);
372 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
373 struct bt_ctf_clock_class
*clock_class
);
375 #endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */