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