cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / ctf-writer / trace.h
CommitLineData
3dca2276 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
3dca2276 3 *
0235b0db 4 * Copyright 2014 EfficiOS Inc.
3dca2276
PP
5 *
6 * The Common Trace Format (CTF) Specification is available at
7 * http://www.efficios.com/ctf
8 */
9
0235b0db
MJ
10#ifndef BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
11#define BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
12
91d81473 13#include "common/macros.h"
6162e6b7 14#include "common/uuid.h"
217cf9d3
PP
15#include <babeltrace2-ctf-writer/field-types.h>
16#include <babeltrace2-ctf-writer/fields.h>
17#include <babeltrace2-ctf-writer/trace.h>
16ca5ff0
PP
18#include <glib.h>
19#include <sys/types.h>
20
578e048b
MJ
21#include "assert-pre.h"
22#include "attributes.h"
23#include "clock-class.h"
24#include "object.h"
25#include "stream-class.h"
26#include "validation.h"
27#include "values.h"
28
16ca5ff0 29struct bt_ctf_trace_common {
e1e02a22 30 struct bt_ctf_object base;
16ca5ff0
PP
31 GString *name;
32 int frozen;
6162e6b7 33 bt_uuid_t uuid;
00409097 34 bt_ctf_bool uuid_set;
16ca5ff0 35 enum bt_ctf_byte_order native_byte_order;
e1e02a22 36 struct bt_ctf_private_value *environment;
16ca5ff0
PP
37 GPtrArray *clock_classes; /* Array of pointers to bt_ctf_clock_class */
38 GPtrArray *stream_classes; /* Array of ptrs to bt_ctf_stream_class_common */
39 GPtrArray *streams; /* Array of ptrs to bt_ctf_stream_common */
40 struct bt_ctf_field_type_common *packet_header_field_type;
41 int64_t next_stream_id;
42
43 /*
44 * This flag indicates if the trace is valid. A valid
45 * trace is _always_ frozen.
46 */
47 int valid;
48};
49
00409097 50bt_ctf_bool bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common *trace,
16ca5ff0
PP
51 struct bt_ctf_clock_class *clock_class);
52
16ca5ff0 53int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common *trace,
e1e02a22 54 bt_ctf_object_release_func release_func);
16ca5ff0 55
16ca5ff0
PP
56void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common *trace);
57
58static inline
59const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common *trace)
60{
67d2ce02 61 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
62 return trace->name ? trace->name->str : NULL;
63}
64
16ca5ff0
PP
65int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common *trace, const char *name);
66
67static inline
6162e6b7 68const uint8_t *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common *trace)
16ca5ff0 69{
67d2ce02 70 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
71 return trace->uuid_set ? trace->uuid : NULL;
72}
73
6162e6b7 74int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common *trace, const uint8_t *uuid);
16ca5ff0 75
16ca5ff0 76int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common *trace,
e1e02a22 77 const char *name, struct bt_ctf_private_value *value);
16ca5ff0 78
16ca5ff0
PP
79int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common *trace,
80 const char *name, const char *value);
81
16ca5ff0
PP
82int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common *trace,
83 const char *name, int64_t value);
84
85static inline
86int64_t bt_ctf_trace_common_get_environment_field_count(
87 struct bt_ctf_trace_common *trace)
88{
89 int64_t ret;
90
67d2ce02 91 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0 92 ret = bt_ctf_attributes_get_count(trace->environment);
98b15851 93 BT_ASSERT_DBG(ret >= 0);
16ca5ff0
PP
94 return ret;
95}
96
97static inline
98const char *
99bt_ctf_trace_common_get_environment_field_name_by_index(
100 struct bt_ctf_trace_common *trace, uint64_t index)
101{
67d2ce02 102 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
103 return bt_ctf_attributes_get_field_name(trace->environment, index);
104}
105
106static inline
e1e02a22 107struct bt_ctf_private_value *
da91b29a 108bt_ctf_trace_common_borrow_environment_field_value_by_index(
16ca5ff0
PP
109 struct bt_ctf_trace_common *trace, uint64_t index)
110{
67d2ce02 111 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
112 return bt_ctf_attributes_borrow_field_value(trace->environment, index);
113}
114
115static inline
e1e02a22 116struct bt_ctf_private_value *
da91b29a 117bt_ctf_trace_common_borrow_environment_field_value_by_name(
16ca5ff0
PP
118 struct bt_ctf_trace_common *trace, const char *name)
119{
67d2ce02
MJ
120 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
121 BT_CTF_ASSERT_PRE_NON_NULL(name, "Name");
16ca5ff0
PP
122 return bt_ctf_attributes_borrow_field_value_by_name(trace->environment,
123 name);
124}
125
16ca5ff0
PP
126int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common *trace,
127 struct bt_ctf_clock_class *clock_class);
128
129static inline
130int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common *trace)
131{
67d2ce02 132 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
133 return trace->clock_classes->len;
134}
135
136static inline
137struct bt_ctf_clock_class *bt_ctf_trace_common_borrow_clock_class_by_index(
138 struct bt_ctf_trace_common *trace, uint64_t index)
139{
67d2ce02
MJ
140 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
141 BT_CTF_ASSERT_PRE(index < trace->clock_classes->len,
16ca5ff0
PP
142 "Index is out of bounds: index=%" PRIu64 ", "
143 "count=%u",
144 index, trace->clock_classes->len);
145 return g_ptr_array_index(trace->clock_classes, index);
146}
147
148static inline
149int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common *trace)
150{
67d2ce02 151 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
152 return (int64_t) trace->streams->len;
153}
154
155static inline
156struct bt_ctf_stream_common *bt_ctf_trace_common_borrow_stream_by_index(
157 struct bt_ctf_trace_common *trace,
158 uint64_t index)
159{
67d2ce02
MJ
160 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
161 BT_CTF_ASSERT_PRE(index < trace->streams->len,
16ca5ff0
PP
162 "Index is out of bounds: index=%" PRIu64 ", "
163 "count=%u",
164 index, trace->streams->len);
165 return g_ptr_array_index(trace->streams, index);
166}
167
168static inline
169int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common *trace)
170{
67d2ce02 171 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
172 return (int64_t) trace->stream_classes->len;
173}
174
175static inline
176struct bt_ctf_stream_class_common *bt_ctf_trace_common_borrow_stream_class_by_index(
177 struct bt_ctf_trace_common *trace, uint64_t index)
178{
67d2ce02
MJ
179 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
180 BT_CTF_ASSERT_PRE(index < trace->stream_classes->len,
16ca5ff0
PP
181 "Index is out of bounds: index=%" PRIu64 ", "
182 "count=%u",
183 index, trace->stream_classes->len);
184 return g_ptr_array_index(trace->stream_classes, index);
185}
186
187static inline
188struct bt_ctf_stream_class_common *bt_ctf_trace_common_borrow_stream_class_by_id(
189 struct bt_ctf_trace_common *trace, uint64_t id_param)
190{
191 int i;
192 struct bt_ctf_stream_class_common *stream_class = NULL;
193 int64_t id = (int64_t) id_param;
194
67d2ce02
MJ
195 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
196 BT_CTF_ASSERT_PRE(id >= 0,
16ca5ff0
PP
197 "Invalid stream class ID: %" PRIu64, id_param);
198
199 for (i = 0; i < trace->stream_classes->len; i++) {
200 struct bt_ctf_stream_class_common *stream_class_candidate;
201
202 stream_class_candidate =
203 g_ptr_array_index(trace->stream_classes, i);
204
205 if (bt_ctf_stream_class_common_get_id(stream_class_candidate) ==
206 (int64_t) id) {
207 stream_class = stream_class_candidate;
208 goto end;
209 }
210 }
211
212end:
213 return stream_class;
214}
215
216static inline
217struct bt_ctf_clock_class *bt_ctf_trace_common_borrow_clock_class_by_name(
218 struct bt_ctf_trace_common *trace, const char *name)
219{
220 size_t i;
221 struct bt_ctf_clock_class *clock_class = NULL;
222
67d2ce02
MJ
223 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
224 BT_CTF_ASSERT_PRE_NON_NULL(name, "Name");
16ca5ff0
PP
225
226 for (i = 0; i < trace->clock_classes->len; i++) {
227 struct bt_ctf_clock_class *cur_clk =
228 g_ptr_array_index(trace->clock_classes, i);
229 const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk);
230
231 if (!cur_clk_name) {
232 goto end;
233 }
234
235 if (!strcmp(cur_clk_name, name)) {
236 clock_class = cur_clk;
237 goto end;
238 }
239 }
240
241end:
242 return clock_class;
243}
244
245static inline
246enum bt_ctf_byte_order bt_ctf_trace_common_get_native_byte_order(
247 struct bt_ctf_trace_common *trace)
248{
67d2ce02 249 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
250 return trace->native_byte_order;
251}
252
16ca5ff0
PP
253int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common *trace,
254 enum bt_ctf_byte_order byte_order, bool allow_unspecified);
255
256static inline
257struct bt_ctf_field_type_common *bt_ctf_trace_common_borrow_packet_header_field_type(
258 struct bt_ctf_trace_common *trace)
259{
67d2ce02 260 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
261 return trace->packet_header_field_type;
262}
263
16ca5ff0
PP
264int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common *trace,
265 struct bt_ctf_field_type_common *packet_header_field_type);
266
267static inline
268void bt_ctf_trace_common_freeze(struct bt_ctf_trace_common *trace)
269{
270 int i;
271
272 if (trace->frozen) {
273 return;
274 }
275
276 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
277 trace, bt_ctf_trace_common_get_name(trace));
278 BT_LOGD_STR("Freezing packet header field type.");
279 bt_ctf_field_type_common_freeze(trace->packet_header_field_type);
280 BT_LOGD_STR("Freezing environment attributes.");
281 bt_ctf_attributes_freeze(trace->environment);
282
283 if (trace->clock_classes->len > 0) {
284 BT_LOGD_STR("Freezing clock classes.");
285 }
286
287 for (i = 0; i < trace->clock_classes->len; i++) {
288 struct bt_ctf_clock_class *clock_class =
289 g_ptr_array_index(trace->clock_classes, i);
290
291 bt_ctf_clock_class_freeze(clock_class);
292 }
293
294 trace->frozen = 1;
295}
296
16ca5ff0
PP
297int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common *trace,
298 struct bt_ctf_stream_class_common *stream_class,
299 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func,
300 struct bt_ctf_clock_class *init_expected_clock_class,
301 int (*map_clock_classes_func)(struct bt_ctf_stream_class_common *stream_class,
302 struct bt_ctf_field_type_common *packet_context_field_type,
303 struct bt_ctf_field_type_common *event_header_field_type),
304 bool check_ts_begin_end_mapped);
3dca2276
PP
305
306struct bt_ctf_trace {
16ca5ff0 307 struct bt_ctf_trace_common common;
3dca2276
PP
308};
309
310/*
311 * bt_ctf_trace_get_metadata_string: get metadata string.
312 *
313 * Get the trace's TSDL metadata. The caller assumes the ownership of the
314 * returned string.
315 *
316 * @param trace Trace instance.
317 *
318 * Returns the metadata string on success, NULL on error.
319 */
3dca2276
PP
320char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace);
321
3dca2276
PP
322struct bt_ctf_trace *bt_ctf_trace_create(void);
323
16ca5ff0
PP
324int64_t bt_ctf_trace_get_clock_class_count(
325 struct bt_ctf_trace *trace);
326
16ca5ff0
PP
327struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index(
328 struct bt_ctf_trace *trace, uint64_t index);
329
16ca5ff0
PP
330struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
331 struct bt_ctf_trace *trace, const char *name);
332
16ca5ff0
PP
333int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
334 struct bt_ctf_clock_class *clock_class);
335
e1e02a22
PP
336int64_t bt_ctf_trace_get_environment_field_count(
337 struct bt_ctf_trace *trace);
338
e1e02a22
PP
339const char *bt_ctf_trace_get_environment_field_name_by_index(
340 struct bt_ctf_trace *trace, uint64_t index);
341
e1e02a22
PP
342struct bt_ctf_value *
343bt_ctf_trace_get_environment_field_value_by_index(struct bt_ctf_trace *trace,
344 uint64_t index);
345
e1e02a22
PP
346struct bt_ctf_value *
347bt_ctf_trace_get_environment_field_value_by_name(
348 struct bt_ctf_trace *trace, const char *name);
349
7c7301d5
SM
350int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
351 bt_ctf_visitor visitor, void *data);
352
3dca2276 353#endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */
This page took 0.104588 seconds and 5 git commands to generate.