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 <babeltrace2/ctf-writer/assert-pre-internal.h>
32 #include <babeltrace2/babeltrace-internal.h>
33 #include <babeltrace2/compat/uuid-internal.h>
34 #include <babeltrace2/ctf-writer/attributes-internal.h>
35 #include <babeltrace2/ctf-writer/clock-class-internal.h>
36 #include <babeltrace2/ctf-writer/field-types.h>
37 #include <babeltrace2/ctf-writer/fields.h>
38 #include <babeltrace2/ctf-writer/stream-class-internal.h>
39 #include <babeltrace2/ctf-writer/trace.h>
40 #include <babeltrace2/ctf-writer/validation-internal.h>
41 #include <babeltrace2/ctf-writer/object-internal.h>
42 #include <babeltrace2/ctf-writer/values-internal.h>
43 #include <babeltrace2/types.h>
45 #include <sys/types.h>
47 struct bt_ctf_trace_common
{
48 struct bt_ctf_object base
;
51 unsigned char uuid
[BABELTRACE_UUID_LEN
];
53 enum bt_ctf_byte_order native_byte_order
;
54 struct bt_ctf_private_value
*environment
;
55 GPtrArray
*clock_classes
; /* Array of pointers to bt_ctf_clock_class */
56 GPtrArray
*stream_classes
; /* Array of ptrs to bt_ctf_stream_class_common */
57 GPtrArray
*streams
; /* Array of ptrs to bt_ctf_stream_common */
58 struct bt_ctf_field_type_common
*packet_header_field_type
;
59 int64_t next_stream_id
;
62 * This flag indicates if the trace is valid. A valid
63 * trace is _always_ frozen.
69 bt_bool
bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common
*trace
,
70 struct bt_ctf_clock_class
*clock_class
);
73 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common
*trace
,
74 bt_ctf_object_release_func release_func
);
77 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common
*trace
);
80 const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common
*trace
)
82 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
83 return trace
->name
? trace
->name
->str
: NULL
;
87 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common
*trace
, const char *name
);
90 const unsigned char *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common
*trace
)
92 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
93 return trace
->uuid_set
? trace
->uuid
: NULL
;
97 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common
*trace
, const unsigned char *uuid
);
100 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common
*trace
,
101 const char *name
, struct bt_ctf_private_value
*value
);
104 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common
*trace
,
105 const char *name
, const char *value
);
108 int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common
*trace
,
109 const char *name
, int64_t value
);
112 int64_t bt_ctf_trace_common_get_environment_field_count(
113 struct bt_ctf_trace_common
*trace
)
117 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
118 ret
= bt_ctf_attributes_get_count(trace
->environment
);
125 bt_ctf_trace_common_get_environment_field_name_by_index(
126 struct bt_ctf_trace_common
*trace
, uint64_t index
)
128 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
129 return bt_ctf_attributes_get_field_name(trace
->environment
, index
);
133 struct bt_ctf_private_value
*
134 bt_ctf_trace_common_borrow_environment_field_value_by_index(
135 struct bt_ctf_trace_common
*trace
, uint64_t index
)
137 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
138 return bt_ctf_attributes_borrow_field_value(trace
->environment
, index
);
142 struct bt_ctf_private_value
*
143 bt_ctf_trace_common_borrow_environment_field_value_by_name(
144 struct bt_ctf_trace_common
*trace
, const char *name
)
146 BT_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
147 BT_CTF_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_CTF_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_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
168 BT_CTF_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_CTF_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_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
188 BT_CTF_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_CTF_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_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
207 BT_CTF_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_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
223 BT_CTF_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_CTF_ASSERT_PRE_NON_NULL(trace
, "Trace");
251 BT_CTF_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_CTF_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_CTF_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
);
373 int64_t bt_ctf_trace_get_environment_field_count(
374 struct bt_ctf_trace
*trace
);
377 const char *bt_ctf_trace_get_environment_field_name_by_index(
378 struct bt_ctf_trace
*trace
, uint64_t index
);
381 struct bt_ctf_value
*
382 bt_ctf_trace_get_environment_field_value_by_index(struct bt_ctf_trace
*trace
,
386 struct bt_ctf_value
*
387 bt_ctf_trace_get_environment_field_value_by_name(
388 struct bt_ctf_trace
*trace
, const char *name
);
390 #endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */