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