Split CTF IR and CTF writer APIs and implementations
[babeltrace.git] / include / babeltrace / ctf-ir / trace-internal.h
1 #ifndef BABELTRACE_CTF_IR_TRACE_INTERNAL_H
2 #define BABELTRACE_CTF_IR_TRACE_INTERNAL_H
3
4 /*
5 * BabelTrace - CTF IR: Trace internal
6 *
7 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/ctf-ir/trace.h>
32 #include <babeltrace/ctf-ir/stream-class-internal.h>
33 #include <babeltrace/ctf-ir/field-types.h>
34 #include <babeltrace/ctf-ir/fields.h>
35 #include <babeltrace/ctf-ir/validation-internal.h>
36 #include <babeltrace/ctf-ir/attributes-internal.h>
37 #include <babeltrace/ctf-ir/clock-class-internal.h>
38 #include <babeltrace/object-internal.h>
39 #include <babeltrace/babeltrace-internal.h>
40 #include <babeltrace/values.h>
41 #include <babeltrace/types.h>
42 #include <glib.h>
43 #include <sys/types.h>
44 #include <babeltrace/compat/uuid-internal.h>
45
46 struct bt_trace_common {
47 struct bt_object base;
48 GString *name;
49 int frozen;
50 unsigned char uuid[BABELTRACE_UUID_LEN];
51 bt_bool uuid_set;
52 enum bt_byte_order native_byte_order;
53 struct bt_value *environment;
54 GPtrArray *clock_classes; /* Array of pointers to bt_clock_class */
55 GPtrArray *stream_classes; /* Array of ptrs to bt_stream_class_common */
56 GPtrArray *streams; /* Array of ptrs to bt_stream_common */
57 struct bt_field_type_common *packet_header_field_type;
58 int64_t next_stream_id;
59
60 /*
61 * This flag indicates if the trace is valid. A valid
62 * trace is _always_ frozen.
63 */
64 int valid;
65 };
66
67 struct bt_trace {
68 struct bt_trace_common common;
69 GPtrArray *listeners; /* Array of struct listener_wrapper */
70 GArray *is_static_listeners;
71 bt_bool is_static;
72 bt_bool in_remove_listener;
73 };
74
75 BT_HIDDEN
76 int bt_trace_object_modification(struct bt_visitor_object *object,
77 void *trace_ptr);
78
79 BT_HIDDEN
80 bt_bool bt_trace_common_has_clock_class(struct bt_trace_common *trace,
81 struct bt_clock_class *clock_class);
82
83 /**
84 @brief User function type to use with bt_trace_add_listener().
85
86 @param[in] obj New CTF IR object which is part of the trace
87 class hierarchy.
88 @param[in] data User data.
89
90 @prenotnull{obj}
91 */
92 typedef void (*bt_listener_cb)(struct bt_visitor_object *obj, void *data);
93
94 /**
95 @brief Adds the trace class modification listener \p listener to
96 the CTF IR trace class \p trace_class.
97
98 Once you add \p listener to \p trace_class, whenever \p trace_class
99 is modified, \p listener is called with the new element and with
100 \p data (user data).
101
102 @param[in] trace_class Trace class to which to add \p listener.
103 @param[in] listener Modification listener function.
104 @param[in] data User data.
105 @returns 0 on success, or a negative value on error.
106
107 @prenotnull{trace_class}
108 @prenotnull{listener}
109 @postrefcountsame{trace_class}
110 */
111 BT_HIDDEN
112 int bt_trace_add_listener(struct bt_trace *trace_class,
113 bt_listener_cb listener, void *data);
114
115 BT_HIDDEN
116 int bt_trace_common_initialize(struct bt_trace_common *trace,
117 bt_object_release_func release_func);
118
119 BT_HIDDEN
120 void bt_trace_common_finalize(struct bt_trace_common *trace);
121
122 static inline
123 const char *bt_trace_common_get_name(struct bt_trace_common *trace)
124 {
125 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
126 return trace->name ? trace->name->str : NULL;
127 }
128
129 BT_HIDDEN
130 int bt_trace_common_set_name(struct bt_trace_common *trace, const char *name);
131
132 static inline
133 const unsigned char *bt_trace_common_get_uuid(struct bt_trace_common *trace)
134 {
135 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
136 return trace->uuid_set ? trace->uuid : NULL;
137 }
138
139 BT_HIDDEN
140 int bt_trace_common_set_uuid(struct bt_trace_common *trace, const unsigned char *uuid);
141
142 BT_HIDDEN
143 int bt_trace_common_set_environment_field(struct bt_trace_common *trace,
144 const char *name, struct bt_value *value);
145
146 BT_HIDDEN
147 int bt_trace_common_set_environment_field_string(struct bt_trace_common *trace,
148 const char *name, const char *value);
149
150 BT_HIDDEN
151 int bt_trace_common_set_environment_field_integer(struct bt_trace_common *trace,
152 const char *name, int64_t value);
153
154 static inline
155 int64_t bt_trace_common_get_environment_field_count(
156 struct bt_trace_common *trace)
157 {
158 int64_t ret;
159
160 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
161 ret = bt_attributes_get_count(trace->environment);
162 BT_ASSERT(ret >= 0);
163 return ret;
164 }
165
166 static inline
167 const char *
168 bt_trace_common_get_environment_field_name_by_index(
169 struct bt_trace_common *trace,
170 uint64_t index)
171 {
172 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
173 return bt_attributes_get_field_name(trace->environment, index);
174 }
175
176 static inline
177 struct bt_value *bt_trace_common_get_environment_field_value_by_index(
178 struct bt_trace_common *trace, uint64_t index)
179 {
180 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
181 return bt_attributes_get_field_value(trace->environment, index);
182 }
183
184 static inline
185 struct bt_value *bt_trace_common_get_environment_field_value_by_name(
186 struct bt_trace_common *trace, const char *name)
187 {
188 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
189 BT_ASSERT_PRE_NON_NULL(name, "Name");
190 return bt_attributes_get_field_value_by_name(trace->environment,
191 name);
192 }
193
194 BT_HIDDEN
195 int bt_trace_common_add_clock_class(struct bt_trace_common *trace,
196 struct bt_clock_class *clock_class);
197
198 static inline
199 int64_t bt_trace_common_get_clock_class_count(struct bt_trace_common *trace)
200 {
201 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
202 return trace->clock_classes->len;
203 }
204
205 static inline
206 struct bt_clock_class *bt_trace_common_get_clock_class_by_index(
207 struct bt_trace_common *trace, uint64_t index)
208 {
209 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
210 BT_ASSERT_PRE(index < trace->clock_classes->len,
211 "Index is out of bounds: index=%" PRIu64 ", "
212 "count=%u",
213 index, trace->clock_classes->len);
214 return bt_get(g_ptr_array_index(trace->clock_classes, index));
215 }
216
217 static inline
218 int64_t bt_trace_common_get_stream_count(struct bt_trace_common *trace)
219 {
220 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
221 return (int64_t) trace->streams->len;
222 }
223
224 static inline
225 struct bt_stream_common *bt_trace_common_get_stream_by_index(
226 struct bt_trace_common *trace,
227 uint64_t index)
228 {
229 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
230 BT_ASSERT_PRE(index < trace->streams->len,
231 "Index is out of bounds: index=%" PRIu64 ", "
232 "count=%u",
233 index, trace->streams->len);
234 return bt_get(g_ptr_array_index(trace->streams, index));
235 }
236
237 static inline
238 int64_t bt_trace_common_get_stream_class_count(struct bt_trace_common *trace)
239 {
240 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
241 return (int64_t) trace->stream_classes->len;
242 }
243
244 static inline
245 struct bt_stream_class_common *bt_trace_common_get_stream_class_by_index(
246 struct bt_trace_common *trace, uint64_t index)
247 {
248 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
249 BT_ASSERT_PRE(index < trace->stream_classes->len,
250 "Index is out of bounds: index=%" PRIu64 ", "
251 "count=%u",
252 index, trace->stream_classes->len);
253 return bt_get(g_ptr_array_index(trace->stream_classes, index));
254 }
255
256 static inline
257 struct bt_stream_class_common *bt_trace_common_get_stream_class_by_id(
258 struct bt_trace_common *trace, uint64_t id_param)
259 {
260 int i;
261 struct bt_stream_class_common *stream_class = NULL;
262 int64_t id = (int64_t) id_param;
263
264 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
265 BT_ASSERT_PRE(id >= 0,
266 "Invalid stream class ID: %" PRIu64, id_param);
267
268 for (i = 0; i < trace->stream_classes->len; i++) {
269 struct bt_stream_class_common *stream_class_candidate;
270
271 stream_class_candidate =
272 g_ptr_array_index(trace->stream_classes, i);
273
274 if (bt_stream_class_common_get_id(stream_class_candidate) ==
275 (int64_t) id) {
276 stream_class = stream_class_candidate;
277 bt_get(stream_class);
278 goto end;
279 }
280 }
281
282 end:
283 return stream_class;
284 }
285
286 static inline
287 struct bt_clock_class *bt_trace_common_get_clock_class_by_name(
288 struct bt_trace_common *trace, const char *name)
289 {
290 size_t i;
291 struct bt_clock_class *clock_class = NULL;
292
293 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
294 BT_ASSERT_PRE_NON_NULL(name, "Name");
295
296 for (i = 0; i < trace->clock_classes->len; i++) {
297 struct bt_clock_class *cur_clk =
298 g_ptr_array_index(trace->clock_classes, i);
299 const char *cur_clk_name = bt_clock_class_get_name(cur_clk);
300
301 if (!cur_clk_name) {
302 goto end;
303 }
304
305 if (!strcmp(cur_clk_name, name)) {
306 clock_class = cur_clk;
307 bt_get(clock_class);
308 goto end;
309 }
310 }
311
312 end:
313 return clock_class;
314 }
315
316 static inline
317 enum bt_byte_order bt_trace_common_get_native_byte_order(
318 struct bt_trace_common *trace)
319 {
320 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
321 return trace->native_byte_order;
322 }
323
324 BT_HIDDEN
325 int bt_trace_common_set_native_byte_order(struct bt_trace_common *trace,
326 enum bt_byte_order byte_order, bool allow_unspecified);
327
328 static inline
329 struct bt_field_type_common *bt_trace_common_get_packet_header_field_type(
330 struct bt_trace_common *trace)
331 {
332 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
333 return bt_get(trace->packet_header_field_type);
334 }
335
336 BT_HIDDEN
337 int bt_trace_common_set_packet_header_field_type(struct bt_trace_common *trace,
338 struct bt_field_type_common *packet_header_field_type);
339
340 static inline
341 void bt_trace_common_freeze(struct bt_trace_common *trace)
342 {
343 int i;
344
345 if (trace->frozen) {
346 return;
347 }
348
349 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
350 trace, bt_trace_common_get_name(trace));
351 BT_LOGD_STR("Freezing packet header field type.");
352 bt_field_type_common_freeze(trace->packet_header_field_type);
353 BT_LOGD_STR("Freezing environment attributes.");
354 bt_attributes_freeze(trace->environment);
355
356 if (trace->clock_classes->len > 0) {
357 BT_LOGD_STR("Freezing clock classes.");
358 }
359
360 for (i = 0; i < trace->clock_classes->len; i++) {
361 struct bt_clock_class *clock_class =
362 g_ptr_array_index(trace->clock_classes, i);
363
364 bt_clock_class_freeze(clock_class);
365 }
366
367 trace->frozen = 1;
368 }
369
370 BT_HIDDEN
371 int bt_trace_common_add_stream_class(struct bt_trace_common *trace,
372 struct bt_stream_class_common *stream_class,
373 bt_validation_flag_copy_field_type_func copy_field_type_func,
374 struct bt_clock_class *init_expected_clock_class,
375 int (*map_clock_classes_func)(struct bt_stream_class_common *stream_class,
376 struct bt_field_type_common *packet_context_field_type,
377 struct bt_field_type_common *event_header_field_type),
378 bool check_ts_begin_end_mapped);
379
380 #endif /* BABELTRACE_CTF_IR_TRACE_INTERNAL_H */
This page took 0.046127 seconds and 5 git commands to generate.