Visibility hidden by default
[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_ctf_bool bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common *trace,
52 struct bt_ctf_clock_class *clock_class);
53
54 int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common *trace,
55 bt_ctf_object_release_func release_func);
56
57 void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common *trace);
58
59 static inline
60 const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common *trace)
61 {
62 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
63 return trace->name ? trace->name->str : NULL;
64 }
65
66 int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common *trace, const char *name);
67
68 static inline
69 const uint8_t *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common *trace)
70 {
71 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
72 return trace->uuid_set ? trace->uuid : NULL;
73 }
74
75 int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common *trace, const uint8_t *uuid);
76
77 int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common *trace,
78 const char *name, struct bt_ctf_private_value *value);
79
80 int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common *trace,
81 const char *name, const char *value);
82
83 int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common *trace,
84 const char *name, int64_t value);
85
86 static inline
87 int64_t bt_ctf_trace_common_get_environment_field_count(
88 struct bt_ctf_trace_common *trace)
89 {
90 int64_t ret;
91
92 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
93 ret = bt_ctf_attributes_get_count(trace->environment);
94 BT_ASSERT_DBG(ret >= 0);
95 return ret;
96 }
97
98 static inline
99 const char *
100 bt_ctf_trace_common_get_environment_field_name_by_index(
101 struct bt_ctf_trace_common *trace, uint64_t index)
102 {
103 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
104 return bt_ctf_attributes_get_field_name(trace->environment, index);
105 }
106
107 static inline
108 struct bt_ctf_private_value *
109 bt_ctf_trace_common_borrow_environment_field_value_by_index(
110 struct bt_ctf_trace_common *trace, uint64_t index)
111 {
112 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
113 return bt_ctf_attributes_borrow_field_value(trace->environment, index);
114 }
115
116 static inline
117 struct bt_ctf_private_value *
118 bt_ctf_trace_common_borrow_environment_field_value_by_name(
119 struct bt_ctf_trace_common *trace, const char *name)
120 {
121 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
122 BT_CTF_ASSERT_PRE_NON_NULL(name, "Name");
123 return bt_ctf_attributes_borrow_field_value_by_name(trace->environment,
124 name);
125 }
126
127 int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common *trace,
128 struct bt_ctf_clock_class *clock_class);
129
130 static inline
131 int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common *trace)
132 {
133 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
134 return trace->clock_classes->len;
135 }
136
137 static inline
138 struct bt_ctf_clock_class *bt_ctf_trace_common_borrow_clock_class_by_index(
139 struct bt_ctf_trace_common *trace, uint64_t index)
140 {
141 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
142 BT_CTF_ASSERT_PRE(index < trace->clock_classes->len,
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
149 static inline
150 int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common *trace)
151 {
152 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
153 return (int64_t) trace->streams->len;
154 }
155
156 static inline
157 struct bt_ctf_stream_common *bt_ctf_trace_common_borrow_stream_by_index(
158 struct bt_ctf_trace_common *trace,
159 uint64_t index)
160 {
161 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
162 BT_CTF_ASSERT_PRE(index < trace->streams->len,
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
169 static inline
170 int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common *trace)
171 {
172 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
173 return (int64_t) trace->stream_classes->len;
174 }
175
176 static inline
177 struct 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 {
180 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
181 BT_CTF_ASSERT_PRE(index < trace->stream_classes->len,
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
188 static inline
189 struct 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
196 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
197 BT_CTF_ASSERT_PRE(id >= 0,
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
213 end:
214 return stream_class;
215 }
216
217 static inline
218 struct 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
224 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
225 BT_CTF_ASSERT_PRE_NON_NULL(name, "Name");
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
242 end:
243 return clock_class;
244 }
245
246 static inline
247 enum bt_ctf_byte_order bt_ctf_trace_common_get_native_byte_order(
248 struct bt_ctf_trace_common *trace)
249 {
250 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
251 return trace->native_byte_order;
252 }
253
254 int 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
257 static inline
258 struct bt_ctf_field_type_common *bt_ctf_trace_common_borrow_packet_header_field_type(
259 struct bt_ctf_trace_common *trace)
260 {
261 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
262 return trace->packet_header_field_type;
263 }
264
265 int 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
268 static inline
269 void 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
298 int 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);
306
307 struct bt_ctf_trace {
308 struct bt_ctf_trace_common common;
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 */
321 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace);
322
323 struct bt_ctf_trace *bt_ctf_trace_create(void);
324
325 int64_t bt_ctf_trace_get_clock_class_count(
326 struct bt_ctf_trace *trace);
327
328 struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index(
329 struct bt_ctf_trace *trace, uint64_t index);
330
331 struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
332 struct bt_ctf_trace *trace, const char *name);
333
334 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
335 struct bt_ctf_clock_class *clock_class);
336
337 int64_t bt_ctf_trace_get_environment_field_count(
338 struct bt_ctf_trace *trace);
339
340 const char *bt_ctf_trace_get_environment_field_name_by_index(
341 struct bt_ctf_trace *trace, uint64_t index);
342
343 struct bt_ctf_value *
344 bt_ctf_trace_get_environment_field_value_by_index(struct bt_ctf_trace *trace,
345 uint64_t index);
346
347 struct bt_ctf_value *
348 bt_ctf_trace_get_environment_field_value_by_name(
349 struct bt_ctf_trace *trace, const char *name);
350
351 int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
352 bt_ctf_visitor visitor, void *data);
353
354 #endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */
This page took 0.036374 seconds and 4 git commands to generate.