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