Rename: bt_put(), bt_get() -> bt_object_put_ref(), bt_object_get_ref()
[babeltrace.git] / include / babeltrace / 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/*
5 * BabelTrace - CTF Writer: Trace
6 *
7 * Copyright 2014 EfficiOS Inc.
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 *
29 * The Common Trace Format (CTF) Specification is available at
30 * http://www.efficios.com/ctf
31 */
32
16ca5ff0
PP
33#include <babeltrace/assert-pre-internal.h>
34#include <babeltrace/babeltrace-internal.h>
35#include <babeltrace/compat/uuid-internal.h>
36#include <babeltrace/ctf-writer/attributes-internal.h>
37#include <babeltrace/ctf-writer/clock-class-internal.h>
38#include <babeltrace/ctf-writer/field-types.h>
39#include <babeltrace/ctf-writer/fields.h>
40#include <babeltrace/ctf-writer/stream-class-internal.h>
41#include <babeltrace/ctf-writer/trace.h>
42#include <babeltrace/ctf-writer/validation-internal.h>
43#include <babeltrace/object-internal.h>
44#include <babeltrace/types.h>
45#include <babeltrace/values.h>
46#include <glib.h>
47#include <sys/types.h>
48
49struct bt_ctf_trace_common {
50 struct bt_object base;
51 GString *name;
52 int frozen;
53 unsigned char uuid[BABELTRACE_UUID_LEN];
54 bt_bool uuid_set;
55 enum bt_ctf_byte_order native_byte_order;
56 struct bt_value *environment;
57 GPtrArray *clock_classes; /* Array of pointers to bt_ctf_clock_class */
58 GPtrArray *stream_classes; /* Array of ptrs to bt_ctf_stream_class_common */
59 GPtrArray *streams; /* Array of ptrs to bt_ctf_stream_common */
60 struct bt_ctf_field_type_common *packet_header_field_type;
61 int64_t next_stream_id;
62
63 /*
64 * This flag indicates if the trace is valid. A valid
65 * trace is _always_ frozen.
66 */
67 int valid;
68};
69
70BT_HIDDEN
71bt_bool bt_ctf_trace_common_has_clock_class(struct bt_ctf_trace_common *trace,
72 struct bt_ctf_clock_class *clock_class);
73
74BT_HIDDEN
75int bt_ctf_trace_common_initialize(struct bt_ctf_trace_common *trace,
76 bt_object_release_func release_func);
77
78BT_HIDDEN
79void bt_ctf_trace_common_finalize(struct bt_ctf_trace_common *trace);
80
81static inline
82const char *bt_ctf_trace_common_get_name(struct bt_ctf_trace_common *trace)
83{
84 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
85 return trace->name ? trace->name->str : NULL;
86}
87
88BT_HIDDEN
89int bt_ctf_trace_common_set_name(struct bt_ctf_trace_common *trace, const char *name);
90
91static inline
92const unsigned char *bt_ctf_trace_common_get_uuid(struct bt_ctf_trace_common *trace)
93{
94 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
95 return trace->uuid_set ? trace->uuid : NULL;
96}
97
98BT_HIDDEN
99int bt_ctf_trace_common_set_uuid(struct bt_ctf_trace_common *trace, const unsigned char *uuid);
100
101BT_HIDDEN
102int bt_ctf_trace_common_set_environment_field(struct bt_ctf_trace_common *trace,
103 const char *name, struct bt_value *value);
104
105BT_HIDDEN
106int bt_ctf_trace_common_set_environment_field_string(struct bt_ctf_trace_common *trace,
107 const char *name, const char *value);
108
109BT_HIDDEN
110int bt_ctf_trace_common_set_environment_field_integer(struct bt_ctf_trace_common *trace,
111 const char *name, int64_t value);
112
113static inline
114int64_t bt_ctf_trace_common_get_environment_field_count(
115 struct bt_ctf_trace_common *trace)
116{
117 int64_t ret;
118
119 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
120 ret = bt_ctf_attributes_get_count(trace->environment);
121 BT_ASSERT(ret >= 0);
122 return ret;
123}
124
125static inline
126const char *
127bt_ctf_trace_common_get_environment_field_name_by_index(
128 struct bt_ctf_trace_common *trace, uint64_t index)
129{
130 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
131 return bt_ctf_attributes_get_field_name(trace->environment, index);
132}
133
134static inline
135struct bt_value *bt_ctf_trace_common_borrow_environment_field_value_by_index(
136 struct bt_ctf_trace_common *trace, uint64_t index)
137{
138 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
139 return bt_ctf_attributes_borrow_field_value(trace->environment, index);
140}
141
142static inline
143struct bt_value *bt_ctf_trace_common_borrow_environment_field_value_by_name(
144 struct bt_ctf_trace_common *trace, const char *name)
145{
146 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
147 BT_ASSERT_PRE_NON_NULL(name, "Name");
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{
159 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
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{
167 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
168 BT_ASSERT_PRE(index < trace->clock_classes->len,
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{
178 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
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{
187 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
188 BT_ASSERT_PRE(index < trace->streams->len,
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{
198 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
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{
206 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
207 BT_ASSERT_PRE(index < trace->stream_classes->len,
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
222 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
223 BT_ASSERT_PRE(id >= 0,
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
250 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
251 BT_ASSERT_PRE_NON_NULL(name, "Name");
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{
276 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
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{
288 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
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
3dca2276 372#endif /* BABELTRACE_CTF_WRITER_TRACE_INTERNAL_H */
This page took 0.036574 seconds and 4 git commands to generate.