Move to kernel style SPDX license identifiers
[babeltrace.git] / src / ctf-writer / trace.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2014 EfficiOS Inc.
5 *
6 * The Common Trace Format (CTF) Specification is available at
7 * http://www.efficios.com/ctf
8 */
9
10 #ifndef BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
11 #define BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
12
13 #include "common/macros.h"
14 #include "common/uuid.h"
15 #include <babeltrace2-ctf-writer/field-types.h>
16 #include <babeltrace2-ctf-writer/fields.h>
17 #include <babeltrace2-ctf-writer/trace.h>
18 #include <babeltrace2/types.h>
19 #include <glib.h>
20 #include <sys/types.h>
21
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
30 struct bt_ctf_trace_common {
31 struct bt_ctf_object base;
32 GString *name;
33 int frozen;
34 bt_uuid_t uuid;
35 bt_ctf_bool uuid_set;
36 enum bt_ctf_byte_order native_byte_order;
37 struct bt_ctf_private_value *environment;
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
51 BT_HIDDEN
52 bt_ctf_bool bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common *trace,
53 struct bt_ctf_clock_class *clock_class);
54
55 BT_HIDDEN
56 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common *trace,
57 bt_ctf_object_release_func release_func);
58
59 BT_HIDDEN
60 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common *trace);
61
62 static inline
63 const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common *trace)
64 {
65 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
66 return trace->name ? trace->name->str : NULL;
67 }
68
69 BT_HIDDEN
70 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common *trace, const char *name);
71
72 static inline
73 const uint8_t *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common *trace)
74 {
75 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
76 return trace->uuid_set ? trace->uuid : NULL;
77 }
78
79 BT_HIDDEN
80 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common *trace, const uint8_t *uuid);
81
82 BT_HIDDEN
83 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common *trace,
84 const char *name, struct bt_ctf_private_value *value);
85
86 BT_HIDDEN
87 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common *trace,
88 const char *name, const char *value);
89
90 BT_HIDDEN
91 int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common *trace,
92 const char *name, int64_t value);
93
94 static inline
95 int64_t bt_ctf_trace_common_get_environment_field_count(
96 struct bt_ctf_trace_common *trace)
97 {
98 int64_t ret;
99
100 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
101 ret = bt_ctf_attributes_get_count(trace->environment);
102 BT_ASSERT_DBG(ret >= 0);
103 return ret;
104 }
105
106 static inline
107 const char *
108 bt_ctf_trace_common_get_environment_field_name_by_index(
109 struct bt_ctf_trace_common *trace, uint64_t index)
110 {
111 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
112 return bt_ctf_attributes_get_field_name(trace->environment, index);
113 }
114
115 static inline
116 struct bt_ctf_private_value *
117 bt_ctf_trace_common_borrow_environment_field_value_by_index(
118 struct bt_ctf_trace_common *trace, uint64_t index)
119 {
120 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
121 return bt_ctf_attributes_borrow_field_value(trace->environment, index);
122 }
123
124 static inline
125 struct bt_ctf_private_value *
126 bt_ctf_trace_common_borrow_environment_field_value_by_name(
127 struct bt_ctf_trace_common *trace, const char *name)
128 {
129 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
130 BT_CTF_ASSERT_PRE_NON_NULL(name, "Name");
131 return bt_ctf_attributes_borrow_field_value_by_name(trace->environment,
132 name);
133 }
134
135 BT_HIDDEN
136 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common *trace,
137 struct bt_ctf_clock_class *clock_class);
138
139 static inline
140 int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common *trace)
141 {
142 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
143 return trace->clock_classes->len;
144 }
145
146 static inline
147 struct bt_ctf_clock_class *bt_ctf_trace_common_borrow_clock_class_by_index(
148 struct bt_ctf_trace_common *trace, uint64_t index)
149 {
150 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
151 BT_CTF_ASSERT_PRE(index < trace->clock_classes->len,
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
158 static inline
159 int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common *trace)
160 {
161 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
162 return (int64_t) trace->streams->len;
163 }
164
165 static inline
166 struct bt_ctf_stream_common *bt_ctf_trace_common_borrow_stream_by_index(
167 struct bt_ctf_trace_common *trace,
168 uint64_t index)
169 {
170 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
171 BT_CTF_ASSERT_PRE(index < trace->streams->len,
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
178 static inline
179 int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common *trace)
180 {
181 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
182 return (int64_t) trace->stream_classes->len;
183 }
184
185 static inline
186 struct 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 {
189 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
190 BT_CTF_ASSERT_PRE(index < trace->stream_classes->len,
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
197 static inline
198 struct 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
205 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
206 BT_CTF_ASSERT_PRE(id >= 0,
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
222 end:
223 return stream_class;
224 }
225
226 static inline
227 struct 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
233 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
234 BT_CTF_ASSERT_PRE_NON_NULL(name, "Name");
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
251 end:
252 return clock_class;
253 }
254
255 static inline
256 enum bt_ctf_byte_order bt_ctf_trace_common_get_native_byte_order(
257 struct bt_ctf_trace_common *trace)
258 {
259 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
260 return trace->native_byte_order;
261 }
262
263 BT_HIDDEN
264 int 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
267 static inline
268 struct bt_ctf_field_type_common *bt_ctf_trace_common_borrow_packet_header_field_type(
269 struct bt_ctf_trace_common *trace)
270 {
271 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
272 return trace->packet_header_field_type;
273 }
274
275 BT_HIDDEN
276 int 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
279 static inline
280 void 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
309 BT_HIDDEN
310 int 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);
318
319 struct bt_ctf_trace {
320 struct bt_ctf_trace_common common;
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 */
333 BT_HIDDEN
334 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace);
335
336 BT_HIDDEN
337 struct bt_ctf_trace *bt_ctf_trace_create(void);
338
339 BT_HIDDEN
340 int64_t bt_ctf_trace_get_clock_class_count(
341 struct bt_ctf_trace *trace);
342
343 BT_HIDDEN
344 struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index(
345 struct bt_ctf_trace *trace, uint64_t index);
346
347 BT_HIDDEN
348 struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
349 struct bt_ctf_trace *trace, const char *name);
350
351 BT_HIDDEN
352 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
353 struct bt_ctf_clock_class *clock_class);
354
355 BT_HIDDEN
356 int64_t bt_ctf_trace_get_environment_field_count(
357 struct bt_ctf_trace *trace);
358
359 BT_HIDDEN
360 const char *bt_ctf_trace_get_environment_field_name_by_index(
361 struct bt_ctf_trace *trace, uint64_t index);
362
363 BT_HIDDEN
364 struct bt_ctf_value *
365 bt_ctf_trace_get_environment_field_value_by_index(struct bt_ctf_trace *trace,
366 uint64_t index);
367
368 BT_HIDDEN
369 struct bt_ctf_value *
370 bt_ctf_trace_get_environment_field_value_by_name(
371 struct bt_ctf_trace *trace, const char *name);
372
373 BT_HIDDEN
374 int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
375 bt_ctf_visitor visitor, void *data);
376
377 #endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */
This page took 0.035976 seconds and 4 git commands to generate.