4 * Babeltrace Python module complements, required for Python bindings
6 * Copyright 2012 EfficiOS Inc.
8 * Author: Danny Serres <danny.serres@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include "python-complements.h"
29 #include <babeltrace/ctf-ir/field-types-internal.h>
30 #include <babeltrace/ctf-ir/fields-internal.h>
31 #include <babeltrace/ctf-ir/field-types.h>
32 #include <babeltrace/ctf-ir/event.h>
33 #include <babeltrace/ctf-ir/event-class.h>
34 #include <babeltrace/ctf-ir/clock-internal.h>
35 #include <babeltrace/iterator.h>
36 #include <babeltrace/ctf/iterator.h>
37 #include <babeltrace/ctf/events-internal.h>
40 /* List-related functions
41 ----------------------------------------------------
45 struct bt_definition
**_bt_python_field_listcaller(
46 const struct bt_ctf_event
*ctf_event
,
47 const struct bt_definition
*scope
,
50 struct bt_definition
**list
;
53 ret
= bt_ctf_get_field_list(ctf_event
, scope
,
54 (const struct bt_definition
* const **)&list
, len
);
56 if (ret
< 0) /* For python to know an error occured */
62 struct bt_definition
*_bt_python_field_one_from_list(
63 struct bt_definition
**list
, int index
)
69 struct bt_ctf_event_decl
**_bt_python_event_decl_listcaller(
71 struct bt_context
*ctx
,
74 struct bt_ctf_event_decl
**list
;
77 ret
= bt_ctf_get_event_decl_list(handle_id
, ctx
,
78 (struct bt_ctf_event_decl
* const **)&list
, len
);
80 if (ret
< 0) /* For python to know an error occured */
86 struct bt_ctf_event_decl
*_bt_python_decl_one_from_list(
87 struct bt_ctf_event_decl
**list
, int index
)
93 struct bt_ctf_field_decl
**_by_python_field_decl_listcaller(
94 struct bt_ctf_event_decl
*event_decl
,
98 struct bt_ctf_field_decl
**list
;
101 ret
= bt_ctf_get_decl_fields(event_decl
, scope
,
102 (const struct bt_ctf_field_decl
* const **)&list
, len
);
104 if (ret
< 0) /* For python to know an error occured */
110 struct bt_ctf_field_decl
*_bt_python_field_decl_one_from_list(
111 struct bt_ctf_field_decl
**list
, int index
)
116 struct definition_array
*_bt_python_get_array_from_def(
117 struct bt_definition
*field
)
119 const struct bt_declaration
*array_decl
;
120 struct definition_array
*array
= NULL
;
126 array_decl
= bt_ctf_get_decl_from_def(field
);
127 if (bt_ctf_field_type(array_decl
) == CTF_TYPE_ARRAY
) {
128 array
= container_of(field
, struct definition_array
, p
);
134 struct bt_declaration
*_bt_python_get_array_element_declaration(
135 struct bt_declaration
*field
)
137 struct declaration_array
*array_decl
;
138 struct bt_declaration
*ret
= NULL
;
144 array_decl
= container_of(field
, struct declaration_array
, p
);
145 ret
= array_decl
->elem
;
150 struct bt_declaration
*_bt_python_get_sequence_element_declaration(
151 struct bt_declaration
*field
)
153 struct declaration_sequence
*sequence_decl
;
154 struct bt_declaration
*ret
= NULL
;
160 sequence_decl
= container_of(field
, struct declaration_sequence
, p
);
161 ret
= sequence_decl
->elem
;
166 const char *_bt_python_get_array_string(struct bt_definition
*field
)
168 struct definition_array
*array
;
169 const char *ret
= NULL
;
175 array
= container_of(field
, struct definition_array
, p
);
176 ret
= array
->string
->str
;
181 const char *_bt_python_get_sequence_string(struct bt_definition
*field
)
183 struct definition_sequence
*sequence
;
184 const char *ret
= NULL
;
190 sequence
= container_of(field
, struct definition_sequence
, p
);
191 ret
= sequence
->string
->str
;
196 struct definition_sequence
*_bt_python_get_sequence_from_def(
197 struct bt_definition
*field
)
199 if (field
&& bt_ctf_field_type(
200 bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_SEQUENCE
) {
201 return container_of(field
, struct definition_sequence
, p
);
207 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field
*field
)
211 if (!field
|| field
->type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
216 const struct bt_ctf_field_type_integer
*type
= container_of(field
->type
,
217 const struct bt_ctf_field_type_integer
, parent
);
218 ret
= type
->declaration
.signedness
;
223 enum ctf_type_id
_bt_python_get_field_type(const struct bt_ctf_field
*field
)
225 enum ctf_type_id type_id
= CTF_TYPE_UNKNOWN
;
231 type_id
= field
->type
->declaration
->id
;
237 * Swig doesn't handle returning pointers via output arguments properly...
238 * These functions only wrap the ctf-ir functions to provide them directly
239 * as regular return values.
241 const char *_bt_python_ctf_field_type_enumeration_get_mapping(
242 struct bt_ctf_field_type
*enumeration
, size_t index
,
243 int64_t *range_start
, int64_t *range_end
)
248 ret
= bt_ctf_field_type_enumeration_get_mapping(enumeration
, index
,
249 &name
, range_start
, range_end
);
250 return !ret
? name
: NULL
;
253 const char *_bt_python_ctf_field_type_enumeration_get_mapping_unsigned(
254 struct bt_ctf_field_type
*enumeration
, size_t index
,
255 uint64_t *range_start
, uint64_t *range_end
)
260 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(enumeration
,
261 index
, &name
, range_start
, range_end
);
262 return !ret
? name
: NULL
;
265 const char *_bt_python_ctf_field_type_structure_get_field_name(
266 struct bt_ctf_field_type
*structure
, size_t index
)
270 struct bt_ctf_field_type
*type
;
272 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
279 bt_ctf_field_type_put(type
);
284 struct bt_ctf_field_type
*_bt_python_ctf_field_type_structure_get_field_type(
285 struct bt_ctf_field_type
*structure
, size_t index
)
289 struct bt_ctf_field_type
*type
;
291 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
293 return !ret
? type
: NULL
;
296 const char *_bt_python_ctf_field_type_variant_get_field_name(
297 struct bt_ctf_field_type
*variant
, size_t index
)
301 struct bt_ctf_field_type
*type
;
303 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
310 bt_ctf_field_type_put(type
);
315 struct bt_ctf_field_type
*_bt_python_ctf_field_type_variant_get_field_type(
316 struct bt_ctf_field_type
*variant
, size_t index
)
320 struct bt_ctf_field_type
*type
;
322 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
324 return !ret
? type
: NULL
;
327 const char *_bt_python_ctf_event_class_get_field_name(
328 struct bt_ctf_event_class
*event_class
, size_t index
)
332 struct bt_ctf_field_type
*type
;
334 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
341 bt_ctf_field_type_put(type
);
346 struct bt_ctf_field_type
*_bt_python_ctf_event_class_get_field_type(
347 struct bt_ctf_event_class
*event_class
, size_t index
)
351 struct bt_ctf_field_type
*type
;
353 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
355 return !ret
? type
: NULL
;
358 int _bt_python_ctf_clock_get_uuid_index(struct bt_ctf_clock
*clock
,
359 size_t index
, unsigned char *value
)
362 const unsigned char *uuid
;
369 uuid
= bt_ctf_clock_get_uuid(clock
);
375 *value
= uuid
[index
];
380 int _bt_python_ctf_clock_set_uuid_index(struct bt_ctf_clock
*clock
,
381 size_t index
, unsigned char value
)
390 clock
->uuid
[index
] = value
;
396 * Python 3.5 changes the StopIteration exception clearing behaviour which
397 * erroneously marks swig clean-up function as having failed. This explicit
398 * allocation function is intended as a work-around so SWIG doesn't manage
399 * the lifetime of a "temporary" object by itself.
401 struct bt_iter_pos
*_bt_python_create_iter_pos(void)
403 return g_new0(struct bt_iter_pos
, 1);
406 struct bt_ctf_iter
*_bt_python_ctf_iter_create_intersect(
407 struct bt_context
*ctx
,
408 struct bt_iter_pos
*inter_begin_pos
,
409 struct bt_iter_pos
*inter_end_pos
)
411 return bt_ctf_iter_create_intersect(ctx
, &inter_begin_pos
,
415 int _bt_python_trace_collection_has_intersection(struct bt_context
*ctx
)
420 ret
= ctf_find_tc_stream_packet_intersection_union(ctx
, &begin
, &end
);
422 return ret
== 0 ? 1 : 0;