tap-driver.sh: flush stdout after each test result
[babeltrace.git] / include / babeltrace2 / ctf-writer / trace-internal.h
CommitLineData
3dca2276
PP
1#ifndef BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
2#define BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H
3
4/*
3dca2276
PP
5 * Copyright 2014 EfficiOS Inc.
6 *
7 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 *
27 * The Common Trace Format (CTF) Specification is available at
28 * http://www.efficios.com/ctf
29 */
30
67d2ce02 31#include <babeltrace2/ctf-writer/assert-pre-internal.h>
3fadfbc0
MJ
32#include <babeltrace2/babeltrace-internal.h>
33#include <babeltrace2/compat/uuid-internal.h>
34#include <babeltrace2/ctf-writer/attributes-internal.h>
35#include <babeltrace2/ctf-writer/clock-class-internal.h>
36#include <babeltrace2/ctf-writer/field-types.h>
37#include <babeltrace2/ctf-writer/fields.h>
38#include <babeltrace2/ctf-writer/stream-class-internal.h>
39#include <babeltrace2/ctf-writer/trace.h>
40#include <babeltrace2/ctf-writer/validation-internal.h>
41#include <babeltrace2/ctf-writer/object-internal.h>
42#include <babeltrace2/ctf-writer/values-internal.h>
43#include <babeltrace2/types.h>
16ca5ff0
PP
44#include <glib.h>
45#include <sys/types.h>
46
47struct bt_ctf_trace_common {
e1e02a22 48 struct bt_ctf_object base;
16ca5ff0
PP
49 GString *name;
50 int frozen;
51 unsigned char uuid[BABELTRACE_UUID_LEN];
52 bt_bool uuid_set;
53 enum bt_ctf_byte_order native_byte_order;
e1e02a22 54 struct bt_ctf_private_value *environment;
16ca5ff0
PP
55 GPtrArray *clock_classes; /* Array of pointers to bt_ctf_clock_class */
56 GPtrArray *stream_classes; /* Array of ptrs to bt_ctf_stream_class_common */
57 GPtrArray *streams; /* Array of ptrs to bt_ctf_stream_common */
58 struct bt_ctf_field_type_common *packet_header_field_type;
59 int64_t next_stream_id;
60
61 /*
62 * This flag indicates if the trace is valid. A valid
63 * trace is _always_ frozen.
64 */
65 int valid;
66};
67
68BT_HIDDEN
69bt_bool bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common *trace,
70 struct bt_ctf_clock_class *clock_class);
71
72BT_HIDDEN
73int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common *trace,
e1e02a22 74 bt_ctf_object_release_func release_func);
16ca5ff0
PP
75
76BT_HIDDEN
77void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common *trace);
78
79static inline
80const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common *trace)
81{
67d2ce02 82 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
83 return trace->name ? trace->name->str : NULL;
84}
85
86BT_HIDDEN
87int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common *trace, const char *name);
88
89static inline
90const unsigned char *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common *trace)
91{
67d2ce02 92 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
93 return trace->uuid_set ? trace->uuid : NULL;
94}
95
96BT_HIDDEN
97int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common *trace, const unsigned char *uuid);
98
99BT_HIDDEN
100int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common *trace,
e1e02a22 101 const char *name, struct bt_ctf_private_value *value);
16ca5ff0
PP
102
103BT_HIDDEN
104int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common *trace,
105 const char *name, const char *value);
106
107BT_HIDDEN
108int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common *trace,
109 const char *name, int64_t value);
110
111static inline
112int64_t bt_ctf_trace_common_get_environment_field_count(
113 struct bt_ctf_trace_common *trace)
114{
115 int64_t ret;
116
67d2ce02 117 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
118 ret = bt_ctf_attributes_get_count(trace->environment);
119 BT_ASSERT(ret >= 0);
120 return ret;
121}
122
123static inline
124const char *
125bt_ctf_trace_common_get_environment_field_name_by_index(
126 struct bt_ctf_trace_common *trace, uint64_t index)
127{
67d2ce02 128 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
129 return bt_ctf_attributes_get_field_name(trace->environment, index);
130}
131
132static inline
e1e02a22 133struct bt_ctf_private_value *
da91b29a 134bt_ctf_trace_common_borrow_environment_field_value_by_index(
16ca5ff0
PP
135 struct bt_ctf_trace_common *trace, uint64_t index)
136{
67d2ce02 137 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
138 return bt_ctf_attributes_borrow_field_value(trace->environment, index);
139}
140
141static inline
e1e02a22 142struct bt_ctf_private_value *
da91b29a 143bt_ctf_trace_common_borrow_environment_field_value_by_name(
16ca5ff0
PP
144 struct bt_ctf_trace_common *trace, const char *name)
145{
67d2ce02
MJ
146 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
147 BT_CTF_ASSERT_PRE_NON_NULL(name, "Name");
16ca5ff0
PP
148 return bt_ctf_attributes_borrow_field_value_by_name(trace->environment,
149 name);
150}
151
152BT_HIDDEN
153int bt_ctf_trace_common_add_clock_class(struct bt_ctf_trace_common *trace,
154 struct bt_ctf_clock_class *clock_class);
155
156static inline
157int64_t bt_ctf_trace_common_get_clock_class_count(struct bt_ctf_trace_common *trace)
158{
67d2ce02 159 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
160 return trace->clock_classes->len;
161}
162
163static inline
164struct bt_ctf_clock_class *bt_ctf_trace_common_borrow_clock_class_by_index(
165 struct bt_ctf_trace_common *trace, uint64_t index)
166{
67d2ce02
MJ
167 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
168 BT_CTF_ASSERT_PRE(index < trace->clock_classes->len,
16ca5ff0
PP
169 "Index is out of bounds: index=%" PRIu64 ", "
170 "count=%u",
171 index, trace->clock_classes->len);
172 return g_ptr_array_index(trace->clock_classes, index);
173}
174
175static inline
176int64_t bt_ctf_trace_common_get_stream_count(struct bt_ctf_trace_common *trace)
177{
67d2ce02 178 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
179 return (int64_t) trace->streams->len;
180}
181
182static inline
183struct bt_ctf_stream_common *bt_ctf_trace_common_borrow_stream_by_index(
184 struct bt_ctf_trace_common *trace,
185 uint64_t index)
186{
67d2ce02
MJ
187 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
188 BT_CTF_ASSERT_PRE(index < trace->streams->len,
16ca5ff0
PP
189 "Index is out of bounds: index=%" PRIu64 ", "
190 "count=%u",
191 index, trace->streams->len);
192 return g_ptr_array_index(trace->streams, index);
193}
194
195static inline
196int64_t bt_ctf_trace_common_get_stream_class_count(struct bt_ctf_trace_common *trace)
197{
67d2ce02 198 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
199 return (int64_t) trace->stream_classes->len;
200}
201
202static inline
203struct bt_ctf_stream_class_common *bt_ctf_trace_common_borrow_stream_class_by_index(
204 struct bt_ctf_trace_common *trace, uint64_t index)
205{
67d2ce02
MJ
206 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
207 BT_CTF_ASSERT_PRE(index < trace->stream_classes->len,
16ca5ff0
PP
208 "Index is out of bounds: index=%" PRIu64 ", "
209 "count=%u",
210 index, trace->stream_classes->len);
211 return g_ptr_array_index(trace->stream_classes, index);
212}
213
214static inline
215struct bt_ctf_stream_class_common *bt_ctf_trace_common_borrow_stream_class_by_id(
216 struct bt_ctf_trace_common *trace, uint64_t id_param)
217{
218 int i;
219 struct bt_ctf_stream_class_common *stream_class = NULL;
220 int64_t id = (int64_t) id_param;
221
67d2ce02
MJ
222 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
223 BT_CTF_ASSERT_PRE(id >= 0,
16ca5ff0
PP
224 "Invalid stream class ID: %" PRIu64, id_param);
225
226 for (i = 0; i < trace->stream_classes->len; i++) {
227 struct bt_ctf_stream_class_common *stream_class_candidate;
228
229 stream_class_candidate =
230 g_ptr_array_index(trace->stream_classes, i);
231
232 if (bt_ctf_stream_class_common_get_id(stream_class_candidate) ==
233 (int64_t) id) {
234 stream_class = stream_class_candidate;
235 goto end;
236 }
237 }
238
239end:
240 return stream_class;
241}
242
243static inline
244struct bt_ctf_clock_class *bt_ctf_trace_common_borrow_clock_class_by_name(
245 struct bt_ctf_trace_common *trace, const char *name)
246{
247 size_t i;
248 struct bt_ctf_clock_class *clock_class = NULL;
249
67d2ce02
MJ
250 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
251 BT_CTF_ASSERT_PRE_NON_NULL(name, "Name");
16ca5ff0
PP
252
253 for (i = 0; i < trace->clock_classes->len; i++) {
254 struct bt_ctf_clock_class *cur_clk =
255 g_ptr_array_index(trace->clock_classes, i);
256 const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk);
257
258 if (!cur_clk_name) {
259 goto end;
260 }
261
262 if (!strcmp(cur_clk_name, name)) {
263 clock_class = cur_clk;
264 goto end;
265 }
266 }
267
268end:
269 return clock_class;
270}
271
272static inline
273enum bt_ctf_byte_order bt_ctf_trace_common_get_native_byte_order(
274 struct bt_ctf_trace_common *trace)
275{
67d2ce02 276 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
277 return trace->native_byte_order;
278}
279
280BT_HIDDEN
281int bt_ctf_trace_common_set_native_byte_order(struct bt_ctf_trace_common *trace,
282 enum bt_ctf_byte_order byte_order, bool allow_unspecified);
283
284static inline
285struct bt_ctf_field_type_common *bt_ctf_trace_common_borrow_packet_header_field_type(
286 struct bt_ctf_trace_common *trace)
287{
67d2ce02 288 BT_CTF_ASSERT_PRE_NON_NULL(trace, "Trace");
16ca5ff0
PP
289 return trace->packet_header_field_type;
290}
291
292BT_HIDDEN
293int bt_ctf_trace_common_set_packet_header_field_type(struct bt_ctf_trace_common *trace,
294 struct bt_ctf_field_type_common *packet_header_field_type);
295
296static inline
297void bt_ctf_trace_common_freeze(struct bt_ctf_trace_common *trace)
298{
299 int i;
300
301 if (trace->frozen) {
302 return;
303 }
304
305 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
306 trace, bt_ctf_trace_common_get_name(trace));
307 BT_LOGD_STR("Freezing packet header field type.");
308 bt_ctf_field_type_common_freeze(trace->packet_header_field_type);
309 BT_LOGD_STR("Freezing environment attributes.");
310 bt_ctf_attributes_freeze(trace->environment);
311
312 if (trace->clock_classes->len > 0) {
313 BT_LOGD_STR("Freezing clock classes.");
314 }
315
316 for (i = 0; i < trace->clock_classes->len; i++) {
317 struct bt_ctf_clock_class *clock_class =
318 g_ptr_array_index(trace->clock_classes, i);
319
320 bt_ctf_clock_class_freeze(clock_class);
321 }
322
323 trace->frozen = 1;
324}
325
326BT_HIDDEN
327int bt_ctf_trace_common_add_stream_class(struct bt_ctf_trace_common *trace,
328 struct bt_ctf_stream_class_common *stream_class,
329 bt_ctf_validation_flag_copy_field_type_func copy_field_type_func,
330 struct bt_ctf_clock_class *init_expected_clock_class,
331 int (*map_clock_classes_func)(struct bt_ctf_stream_class_common *stream_class,
332 struct bt_ctf_field_type_common *packet_context_field_type,
333 struct bt_ctf_field_type_common *event_header_field_type),
334 bool check_ts_begin_end_mapped);
3dca2276
PP
335
336struct bt_ctf_trace {
16ca5ff0 337 struct bt_ctf_trace_common common;
3dca2276
PP
338};
339
340/*
341 * bt_ctf_trace_get_metadata_string: get metadata string.
342 *
343 * Get the trace's TSDL metadata. The caller assumes the ownership of the
344 * returned string.
345 *
346 * @param trace Trace instance.
347 *
348 * Returns the metadata string on success, NULL on error.
349 */
350BT_HIDDEN
351char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace);
352
353BT_HIDDEN
354struct bt_ctf_trace *bt_ctf_trace_create(void);
355
16ca5ff0
PP
356BT_HIDDEN
357int64_t bt_ctf_trace_get_clock_class_count(
358 struct bt_ctf_trace *trace);
359
360BT_HIDDEN
361struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index(
362 struct bt_ctf_trace *trace, uint64_t index);
363
364BT_HIDDEN
365struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
366 struct bt_ctf_trace *trace, const char *name);
367
368BT_HIDDEN
369int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
370 struct bt_ctf_clock_class *clock_class);
371
e1e02a22
PP
372BT_HIDDEN
373int64_t bt_ctf_trace_get_environment_field_count(
374 struct bt_ctf_trace *trace);
375
376BT_HIDDEN
377const char *bt_ctf_trace_get_environment_field_name_by_index(
378 struct bt_ctf_trace *trace, uint64_t index);
379
380BT_HIDDEN
381struct bt_ctf_value *
382bt_ctf_trace_get_environment_field_value_by_index(struct bt_ctf_trace *trace,
383 uint64_t index);
384
385BT_HIDDEN
386struct bt_ctf_value *
387bt_ctf_trace_get_environment_field_value_by_name(
388 struct bt_ctf_trace *trace, const char *name);
389
3dca2276 390#endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */
This page took 0.045565 seconds and 4 git commands to generate.