ctf plugin: notif iter: use "borrow" functions for metadata where possible
[babeltrace.git] / include / babeltrace / ctf-ir / trace-internal.h
CommitLineData
daf01357
JG
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
3dca2276 30#include <babeltrace/assert-pre-internal.h>
daf01357 31#include <babeltrace/ctf-ir/trace.h>
3dca2276 32#include <babeltrace/ctf-ir/stream-class-internal.h>
2e33ac5a
PP
33#include <babeltrace/ctf-ir/field-types.h>
34#include <babeltrace/ctf-ir/fields.h>
3dca2276
PP
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>
83509119 38#include <babeltrace/object-internal.h>
daf01357 39#include <babeltrace/babeltrace-internal.h>
dac5c838 40#include <babeltrace/values.h>
c55a9f58 41#include <babeltrace/types.h>
daf01357
JG
42#include <glib.h>
43#include <sys/types.h>
20eee76e 44#include <babeltrace/compat/uuid-internal.h>
daf01357 45
3dca2276 46struct bt_trace_common {
83509119 47 struct bt_object base;
e96045d4 48 GString *name;
daf01357 49 int frozen;
20eee76e 50 unsigned char uuid[BABELTRACE_UUID_LEN];
c55a9f58 51 bt_bool uuid_set;
50842bdc 52 enum bt_byte_order native_byte_order;
dac5c838 53 struct bt_value *environment;
3dca2276
PP
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;
9ac68eb1 58 int64_t next_stream_id;
09840de5
PP
59
60 /*
61 * This flag indicates if the trace is valid. A valid
62 * trace is _always_ frozen.
63 */
64 int valid;
3dca2276
PP
65};
66
67struct bt_trace {
68 struct bt_trace_common common;
50ad4244 69 GPtrArray *listeners; /* Array of struct listener_wrapper */
3602afb0 70 GArray *is_static_listeners;
c55a9f58 71 bt_bool is_static;
8480c8cc 72 bt_bool in_remove_listener;
daf01357
JG
73};
74
9b888ff3 75BT_HIDDEN
50842bdc 76int bt_trace_object_modification(struct bt_visitor_object *object,
9b888ff3
JG
77 void *trace_ptr);
78
c9d90a34 79BT_HIDDEN
3dca2276 80bt_bool bt_trace_common_has_clock_class(struct bt_trace_common *trace,
50842bdc 81 struct bt_clock_class *clock_class);
c9d90a34 82
3230ee6b 83/**
50842bdc 84@brief User function type to use with bt_trace_add_listener().
3230ee6b
PP
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*/
50842bdc 92typedef void (*bt_listener_cb)(struct bt_visitor_object *obj, void *data);
3230ee6b
PP
93
94/**
95@brief Adds the trace class modification listener \p listener to
96 the CTF IR trace class \p trace_class.
97
98Once you add \p listener to \p trace_class, whenever \p trace_class
99is 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*/
5b876b06 111BT_HIDDEN
50842bdc
PP
112int bt_trace_add_listener(struct bt_trace *trace_class,
113 bt_listener_cb listener, void *data);
3230ee6b 114
5b876b06 115BT_HIDDEN
3dca2276
PP
116int bt_trace_common_initialize(struct bt_trace_common *trace,
117 bt_object_release_func release_func);
118
119BT_HIDDEN
120void bt_trace_common_finalize(struct bt_trace_common *trace);
121
122static inline
123const 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
129BT_HIDDEN
130int bt_trace_common_set_name(struct bt_trace_common *trace, const char *name);
131
132static inline
133const 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
139BT_HIDDEN
140int bt_trace_common_set_uuid(struct bt_trace_common *trace, const unsigned char *uuid);
141
142BT_HIDDEN
143int bt_trace_common_set_environment_field(struct bt_trace_common *trace,
144 const char *name, struct bt_value *value);
145
146BT_HIDDEN
147int bt_trace_common_set_environment_field_string(struct bt_trace_common *trace,
148 const char *name, const char *value);
149
150BT_HIDDEN
151int bt_trace_common_set_environment_field_integer(struct bt_trace_common *trace,
152 const char *name, int64_t value);
153
154static inline
155int64_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
166static inline
167const char *
168bt_trace_common_get_environment_field_name_by_index(
094ff7c0 169 struct bt_trace_common *trace, uint64_t index)
3dca2276
PP
170{
171 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
172 return bt_attributes_get_field_name(trace->environment, index);
173}
174
175static inline
094ff7c0 176struct bt_value *bt_trace_common_borrow_environment_field_value_by_index(
3dca2276
PP
177 struct bt_trace_common *trace, uint64_t index)
178{
179 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
094ff7c0 180 return bt_attributes_borrow_field_value(trace->environment, index);
3dca2276
PP
181}
182
183static inline
094ff7c0 184struct bt_value *bt_trace_common_borrow_environment_field_value_by_name(
3dca2276
PP
185 struct bt_trace_common *trace, const char *name)
186{
187 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
188 BT_ASSERT_PRE_NON_NULL(name, "Name");
094ff7c0 189 return bt_attributes_borrow_field_value_by_name(trace->environment,
3dca2276
PP
190 name);
191}
192
193BT_HIDDEN
194int bt_trace_common_add_clock_class(struct bt_trace_common *trace,
195 struct bt_clock_class *clock_class);
196
197static inline
198int64_t bt_trace_common_get_clock_class_count(struct bt_trace_common *trace)
199{
200 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
201 return trace->clock_classes->len;
202}
203
204static inline
094ff7c0 205struct bt_clock_class *bt_trace_common_borrow_clock_class_by_index(
3dca2276
PP
206 struct bt_trace_common *trace, uint64_t index)
207{
208 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
209 BT_ASSERT_PRE(index < trace->clock_classes->len,
210 "Index is out of bounds: index=%" PRIu64 ", "
211 "count=%u",
212 index, trace->clock_classes->len);
094ff7c0 213 return g_ptr_array_index(trace->clock_classes, index);
3dca2276
PP
214}
215
216static inline
217int64_t bt_trace_common_get_stream_count(struct bt_trace_common *trace)
218{
219 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
220 return (int64_t) trace->streams->len;
221}
222
223static inline
094ff7c0 224struct bt_stream_common *bt_trace_common_borrow_stream_by_index(
3dca2276
PP
225 struct bt_trace_common *trace,
226 uint64_t index)
227{
228 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
229 BT_ASSERT_PRE(index < trace->streams->len,
230 "Index is out of bounds: index=%" PRIu64 ", "
231 "count=%u",
232 index, trace->streams->len);
094ff7c0 233 return g_ptr_array_index(trace->streams, index);
3dca2276
PP
234}
235
236static inline
237int64_t bt_trace_common_get_stream_class_count(struct bt_trace_common *trace)
238{
239 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
240 return (int64_t) trace->stream_classes->len;
241}
242
243static inline
094ff7c0 244struct bt_stream_class_common *bt_trace_common_borrow_stream_class_by_index(
3dca2276
PP
245 struct bt_trace_common *trace, uint64_t index)
246{
247 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
248 BT_ASSERT_PRE(index < trace->stream_classes->len,
249 "Index is out of bounds: index=%" PRIu64 ", "
250 "count=%u",
251 index, trace->stream_classes->len);
094ff7c0 252 return g_ptr_array_index(trace->stream_classes, index);
3dca2276
PP
253}
254
255static inline
094ff7c0 256struct bt_stream_class_common *bt_trace_common_borrow_stream_class_by_id(
3dca2276
PP
257 struct bt_trace_common *trace, uint64_t id_param)
258{
259 int i;
260 struct bt_stream_class_common *stream_class = NULL;
261 int64_t id = (int64_t) id_param;
262
263 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
264 BT_ASSERT_PRE(id >= 0,
265 "Invalid stream class ID: %" PRIu64, id_param);
266
267 for (i = 0; i < trace->stream_classes->len; i++) {
268 struct bt_stream_class_common *stream_class_candidate;
269
270 stream_class_candidate =
271 g_ptr_array_index(trace->stream_classes, i);
272
273 if (bt_stream_class_common_get_id(stream_class_candidate) ==
274 (int64_t) id) {
275 stream_class = stream_class_candidate;
3dca2276
PP
276 goto end;
277 }
278 }
279
280end:
281 return stream_class;
282}
283
284static inline
094ff7c0 285struct bt_clock_class *bt_trace_common_borrow_clock_class_by_name(
3dca2276
PP
286 struct bt_trace_common *trace, const char *name)
287{
288 size_t i;
289 struct bt_clock_class *clock_class = NULL;
290
291 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
292 BT_ASSERT_PRE_NON_NULL(name, "Name");
293
294 for (i = 0; i < trace->clock_classes->len; i++) {
295 struct bt_clock_class *cur_clk =
296 g_ptr_array_index(trace->clock_classes, i);
297 const char *cur_clk_name = bt_clock_class_get_name(cur_clk);
298
299 if (!cur_clk_name) {
300 goto end;
301 }
302
303 if (!strcmp(cur_clk_name, name)) {
304 clock_class = cur_clk;
3dca2276
PP
305 goto end;
306 }
307 }
308
309end:
310 return clock_class;
311}
312
313static inline
314enum bt_byte_order bt_trace_common_get_native_byte_order(
315 struct bt_trace_common *trace)
316{
317 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
318 return trace->native_byte_order;
319}
320
321BT_HIDDEN
322int bt_trace_common_set_native_byte_order(struct bt_trace_common *trace,
323 enum bt_byte_order byte_order, bool allow_unspecified);
324
325static inline
094ff7c0 326struct bt_field_type_common *bt_trace_common_borrow_packet_header_field_type(
3dca2276
PP
327 struct bt_trace_common *trace)
328{
329 BT_ASSERT_PRE_NON_NULL(trace, "Trace");
094ff7c0 330 return trace->packet_header_field_type;
3dca2276
PP
331}
332
333BT_HIDDEN
334int bt_trace_common_set_packet_header_field_type(struct bt_trace_common *trace,
335 struct bt_field_type_common *packet_header_field_type);
336
337static inline
338void bt_trace_common_freeze(struct bt_trace_common *trace)
339{
340 int i;
341
342 if (trace->frozen) {
343 return;
344 }
345
346 BT_LOGD("Freezing trace: addr=%p, name=\"%s\"",
347 trace, bt_trace_common_get_name(trace));
348 BT_LOGD_STR("Freezing packet header field type.");
349 bt_field_type_common_freeze(trace->packet_header_field_type);
350 BT_LOGD_STR("Freezing environment attributes.");
351 bt_attributes_freeze(trace->environment);
352
353 if (trace->clock_classes->len > 0) {
354 BT_LOGD_STR("Freezing clock classes.");
355 }
356
357 for (i = 0; i < trace->clock_classes->len; i++) {
358 struct bt_clock_class *clock_class =
359 g_ptr_array_index(trace->clock_classes, i);
360
361 bt_clock_class_freeze(clock_class);
362 }
363
364 trace->frozen = 1;
365}
366
367BT_HIDDEN
368int bt_trace_common_add_stream_class(struct bt_trace_common *trace,
369 struct bt_stream_class_common *stream_class,
370 bt_validation_flag_copy_field_type_func copy_field_type_func,
371 struct bt_clock_class *init_expected_clock_class,
372 int (*map_clock_classes_func)(struct bt_stream_class_common *stream_class,
373 struct bt_field_type_common *packet_context_field_type,
374 struct bt_field_type_common *event_header_field_type),
375 bool check_ts_begin_end_mapped);
5b876b06 376
daf01357 377#endif /* BABELTRACE_CTF_IR_TRACE_INTERNAL_H */
This page took 0.057311 seconds and 4 git commands to generate.