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>
47 #include <sys/types.h>
49 struct bt_ctf_trace_common
{
50 struct bt_object base
;
53 unsigned char uuid
[BABELTRACE_UUID_LEN
];
55 enum bt_ctf_byte_order native_byte_order
;
56 struct bt_value
*environment
;
57 GPtrArray
*clock_classes
; /* Array of pointers to bt_ctf_clock_class */
58 GPtrArray
*stream_classes
; /* Array of ptrs to bt_ctf_stream_class_common */
59 GPtrArray
*streams
; /* Array of ptrs to bt_ctf_stream_common */
60 struct bt_ctf_field_type_common
*packet_header_field_type
;
61 int64_t next_stream_id
;
64 * This flag indicates if the trace is valid. A valid
65 * trace is _always_ frozen.
71 bt_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
72 struct bt_ctf_clock_class
*clock_class
);
75 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
76 bt_object_release_func release_func
);
79 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
);
82 const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common
*trace
)
84 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
85 return trace
->name
? trace
->name
->str
: NULL
;
89 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
);
92 const unsigned char *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common
*trace
)
94 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
95 return trace
->uuid_set
? trace
->uuid
: NULL
;
99 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
, const unsigned char *uuid
);
102 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
103 const char *name
, struct bt_value
*value
);
106 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
107 const char *name
, const char *value
);
110 int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common
*trace
,
111 const char *name
, int64_t value
);
114 int64_t bt_ctf_trace_common_get_environment_field_count(
115 struct bt_ctf_trace_common
*trace
)
119 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
120 ret
= bt_ctf_attributes_get_count(trace
->environment
);
127 bt_ctf_trace_common_get_environment_field_name_by_index(
128 struct bt_ctf_trace_common
*trace
, uint64_t index
)
130 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
131 return bt_ctf_attributes_get_field_name(trace
->environment
, index
);
135 struct bt_value
*bt_ctf_trace_common_borrow_environment_field_value_by_index(
136 struct bt_ctf_trace_common
*trace
, uint64_t index
)
138 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
139 return bt_ctf_attributes_borrow_field_value(trace
->environment
, index
);
143 struct bt_value
*bt_ctf_trace_common_borrow_environment_field_value_by_name(
144 struct bt_ctf_trace_common
*trace
, const char *name
)
146 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
147 BT_ASSERT_PRE_NON_NULL(name
, "Name");
148 return bt_ctf_attributes_borrow_field_value_by_name(trace
->environment
,
153 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common
*trace
,
154 struct bt_ctf_clock_class
*clock_class
);
157 int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common
*trace
)
159 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
160 return trace
->clock_classes
->len
;
164 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_index(
165 struct bt_ctf_trace_common
*trace
, uint64_t index
)
167 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
168 BT_ASSERT_PRE(index
< trace
->clock_classes
->len
,
169 "Index is out of bounds: index=%" PRIu64
", "
171 index
, trace
->clock_classes
->len
);
172 return g_ptr_array_index(trace
->clock_classes
, index
);
176 int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common
*trace
)
178 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
179 return (int64_t) trace
->streams
->len
;
183 struct bt_ctf_stream_common
*bt_ctf_trace_common_borrow_stream_by_index(
184 struct bt_ctf_trace_common
*trace
,
187 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
188 BT_ASSERT_PRE(index
< trace
->streams
->len
,
189 "Index is out of bounds: index=%" PRIu64
", "
191 index
, trace
->streams
->len
);
192 return g_ptr_array_index(trace
->streams
, index
);
196 int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common
*trace
)
198 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
199 return (int64_t) trace
->stream_classes
->len
;
203 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_index(
204 struct bt_ctf_trace_common
*trace
, uint64_t index
)
206 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
207 BT_ASSERT_PRE(index
< trace
->stream_classes
->len
,
208 "Index is out of bounds: index=%" PRIu64
", "
210 index
, trace
->stream_classes
->len
);
211 return g_ptr_array_index(trace
->stream_classes
, index
);
215 struct bt_ctf_stream_class_common
*bt_ctf_trace_common_borrow_stream_class_by_id(
216 struct bt_ctf_trace_common
*trace
, uint64_t id_param
)
219 struct bt_ctf_stream_class_common
*stream_class
= NULL
;
220 int64_t id
= (int64_t) id_param
;
222 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
223 BT_ASSERT_PRE(id
>= 0,
224 "Invalid stream class ID: %" PRIu64
, id_param
);
226 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
227 struct bt_ctf_stream_class_common
*stream_class_candidate
;
229 stream_class_candidate
=
230 g_ptr_array_index(trace
->stream_classes
, i
);
232 if (bt_ctf_stream_class_common_get_id(stream_class_candidate
) ==
234 stream_class
= stream_class_candidate
;
244 struct bt_ctf_clock_class
*bt_ctf_trace_common_borrow_clock_class_by_name(
245 struct bt_ctf_trace_common
*trace
, const char *name
)
248 struct bt_ctf_clock_class
*clock_class
= NULL
;
250 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
251 BT_ASSERT_PRE_NON_NULL(name
, "Name");
253 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
254 struct bt_ctf_clock_class
*cur_clk
=
255 g_ptr_array_index(trace
->clock_classes
, i
);
256 const char *cur_clk_name
= bt_ctf_clock_class_get_name(cur_clk
);
262 if (!strcmp(cur_clk_name
, name
)) {
263 clock_class
= cur_clk
;
273 enum bt_ctf_byte_order
bt_ctf_trace_common_get_native_byte_order(
274 struct bt_ctf_trace_common
*trace
)
276 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
277 return trace
->native_byte_order
;
281 int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common
*trace
,
282 enum bt_ctf_byte_order byte_order
, bool allow_unspecified
);
285 struct bt_ctf_field_type_common
*bt_ctf_trace_common_borrow_packet_header_field_type(
286 struct bt_ctf_trace_common
*trace
)
288 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
289 return trace
->packet_header_field_type
;
293 int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common
*trace
,
294 struct bt_ctf_field_type_common
*packet_header_field_type
);
297 void bt_ctf_trace_common_freeze(struct bt_ctf_trace_common
*trace
)
305 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
306 trace
, bt_ctf_trace_common_get_name(trace
));
307 BT_LOGD_STR("Freezing packet header field type.");
308 bt_ctf_field_type_common_freeze(trace
->packet_header_field_type
);
309 BT_LOGD_STR("Freezing environment attributes.");
310 bt_ctf_attributes_freeze(trace
->environment
);
312 if (trace
->clock_classes
->len
> 0) {
313 BT_LOGD_STR("Freezing clock classes.");
316 for (i
= 0; i
< trace
->clock_classes
->len
; i
++) {
317 struct bt_ctf_clock_class
*clock_class
=
318 g_ptr_array_index(trace
->clock_classes
, i
);
320 bt_ctf_clock_class_freeze(clock_class
);
327 int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common
*trace
,
328 struct bt_ctf_stream_class_common
*stream_class
,
329 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func
,
330 struct bt_ctf_clock_class
*init_expected_clock_class
,
331 int (*map_clock_classes_func
)(struct bt_ctf_stream_class_common
*stream_class
,
332 struct bt_ctf_field_type_common
*packet_context_field_type
,
333 struct bt_ctf_field_type_common
*event_header_field_type
),
334 bool check_ts_begin_end_mapped
);
336 struct bt_ctf_trace
{
337 struct bt_ctf_trace_common common
;
341 * bt_ctf_trace_get_metadata_string: get metadata string.
343 * Get the trace's TSDL metadata. The caller assumes the ownership of the
346 * @param trace Trace instance.
348 * Returns the metadata string on success, NULL on error.
351 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
);
354 struct bt_ctf_trace
*bt_ctf_trace_create(void);
357 int64_t bt_ctf_trace_get_clock_class_count(
358 struct bt_ctf_trace
*trace
);
361 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
362 struct bt_ctf_trace
*trace
, uint64_t index
);
365 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
366 struct bt_ctf_trace
*trace
, const char *name
);
369 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
370 struct bt_ctf_clock_class
*clock_class
);
372 #endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */