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>
38 /* List-related functions
39 ----------------------------------------------------
43 struct bt_definition
**_bt_python_field_listcaller(
44 const struct bt_ctf_event
*ctf_event
,
45 const struct bt_definition
*scope
,
48 struct bt_definition
**list
;
51 ret
= bt_ctf_get_field_list(ctf_event
, scope
,
52 (const struct bt_definition
* const **)&list
, len
);
54 if (ret
< 0) /* For python to know an error occured */
60 struct bt_definition
*_bt_python_field_one_from_list(
61 struct bt_definition
**list
, int index
)
67 struct bt_ctf_event_decl
**_bt_python_event_decl_listcaller(
69 struct bt_context
*ctx
,
72 struct bt_ctf_event_decl
**list
;
75 ret
= bt_ctf_get_event_decl_list(handle_id
, ctx
,
76 (struct bt_ctf_event_decl
* const **)&list
, len
);
78 if (ret
< 0) /* For python to know an error occured */
84 struct bt_ctf_event_decl
*_bt_python_decl_one_from_list(
85 struct bt_ctf_event_decl
**list
, int index
)
91 struct bt_ctf_field_decl
**_by_python_field_decl_listcaller(
92 struct bt_ctf_event_decl
*event_decl
,
96 struct bt_ctf_field_decl
**list
;
99 ret
= bt_ctf_get_decl_fields(event_decl
, scope
,
100 (const struct bt_ctf_field_decl
* const **)&list
, len
);
102 if (ret
< 0) /* For python to know an error occured */
108 struct bt_ctf_field_decl
*_bt_python_field_decl_one_from_list(
109 struct bt_ctf_field_decl
**list
, int index
)
114 struct definition_array
*_bt_python_get_array_from_def(
115 struct bt_definition
*field
)
117 const struct bt_declaration
*array_decl
;
118 struct definition_array
*array
= NULL
;
124 array_decl
= bt_ctf_get_decl_from_def(field
);
125 if (bt_ctf_field_type(array_decl
) == CTF_TYPE_ARRAY
) {
126 array
= container_of(field
, struct definition_array
, p
);
132 struct bt_declaration
*_bt_python_get_array_element_declaration(
133 struct bt_declaration
*field
)
135 struct declaration_array
*array_decl
;
136 struct bt_declaration
*ret
= NULL
;
142 array_decl
= container_of(field
, struct declaration_array
, p
);
143 ret
= array_decl
->elem
;
148 struct bt_declaration
*_bt_python_get_sequence_element_declaration(
149 struct bt_declaration
*field
)
151 struct declaration_sequence
*sequence_decl
;
152 struct bt_declaration
*ret
= NULL
;
158 sequence_decl
= container_of(field
, struct declaration_sequence
, p
);
159 ret
= sequence_decl
->elem
;
164 const char *_bt_python_get_array_string(struct bt_definition
*field
)
166 struct definition_array
*array
;
167 const char *ret
= NULL
;
173 array
= container_of(field
, struct definition_array
, p
);
174 ret
= array
->string
->str
;
179 const char *_bt_python_get_sequence_string(struct bt_definition
*field
)
181 struct definition_sequence
*sequence
;
182 const char *ret
= NULL
;
188 sequence
= container_of(field
, struct definition_sequence
, p
);
189 ret
= sequence
->string
->str
;
194 struct definition_sequence
*_bt_python_get_sequence_from_def(
195 struct bt_definition
*field
)
197 if (field
&& bt_ctf_field_type(
198 bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_SEQUENCE
) {
199 return container_of(field
, struct definition_sequence
, p
);
205 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field
*field
)
209 if (!field
|| field
->type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
214 const struct bt_ctf_field_type_integer
*type
= container_of(field
->type
,
215 const struct bt_ctf_field_type_integer
, parent
);
216 ret
= type
->declaration
.signedness
;
221 enum ctf_type_id
_bt_python_get_field_type(const struct bt_ctf_field
*field
)
223 enum ctf_type_id type_id
= CTF_TYPE_UNKNOWN
;
229 type_id
= field
->type
->declaration
->id
;
235 * Swig doesn't handle returning pointers via output arguments properly...
236 * These functions only wrap the ctf-ir functions to provide them directly
237 * as regular return values.
239 const char *_bt_python_ctf_field_type_enumeration_get_mapping(
240 struct bt_ctf_field_type
*enumeration
, size_t index
,
241 int64_t *range_start
, int64_t *range_end
)
246 ret
= bt_ctf_field_type_enumeration_get_mapping(enumeration
, index
,
247 &name
, range_start
, range_end
);
248 return !ret
? name
: NULL
;
251 const char *_bt_python_ctf_field_type_enumeration_get_mapping_unsigned(
252 struct bt_ctf_field_type
*enumeration
, size_t index
,
253 uint64_t *range_start
, uint64_t *range_end
)
258 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(enumeration
,
259 index
, &name
, range_start
, range_end
);
260 return !ret
? name
: NULL
;
263 const char *_bt_python_ctf_field_type_structure_get_field_name(
264 struct bt_ctf_field_type
*structure
, size_t index
)
268 struct bt_ctf_field_type
*type
;
270 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
277 bt_ctf_field_type_put(type
);
282 struct bt_ctf_field_type
*_bt_python_ctf_field_type_structure_get_field_type(
283 struct bt_ctf_field_type
*structure
, size_t index
)
287 struct bt_ctf_field_type
*type
;
289 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
291 return !ret
? type
: NULL
;
294 const char *_bt_python_ctf_field_type_variant_get_field_name(
295 struct bt_ctf_field_type
*variant
, size_t index
)
299 struct bt_ctf_field_type
*type
;
301 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
308 bt_ctf_field_type_put(type
);
313 struct bt_ctf_field_type
*_bt_python_ctf_field_type_variant_get_field_type(
314 struct bt_ctf_field_type
*variant
, size_t index
)
318 struct bt_ctf_field_type
*type
;
320 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
322 return !ret
? type
: NULL
;
325 const char *_bt_python_ctf_event_class_get_field_name(
326 struct bt_ctf_event_class
*event_class
, size_t index
)
330 struct bt_ctf_field_type
*type
;
332 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
339 bt_ctf_field_type_put(type
);
344 struct bt_ctf_field_type
*_bt_python_ctf_event_class_get_field_type(
345 struct bt_ctf_event_class
*event_class
, size_t index
)
349 struct bt_ctf_field_type
*type
;
351 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
353 return !ret
? type
: NULL
;
356 int _bt_python_ctf_clock_get_uuid_index(struct bt_ctf_clock
*clock
,
357 size_t index
, unsigned char *value
)
360 const unsigned char *uuid
;
367 uuid
= bt_ctf_clock_get_uuid(clock
);
373 *value
= uuid
[index
];
378 int _bt_python_ctf_clock_set_uuid_index(struct bt_ctf_clock
*clock
,
379 size_t index
, unsigned char value
)
388 clock
->uuid
[index
] = value
;
394 * Python 3.5 changes the StopIteration exception clearing behaviour which
395 * erroneously marks swig clean-up function as having failed. This explicit
396 * allocation function is intended as a work-around so SWIG doesn't manage
397 * the lifetime of a "temporary" object by itself.
399 struct bt_iter_pos
*_bt_python_create_iter_pos(void)
401 return g_new0(struct bt_iter_pos
, 1);