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.
21 #include "python-complements.h"
22 #include <babeltrace/ctf-ir/event-types-internal.h>
23 #include <babeltrace/ctf-ir/event-fields-internal.h>
24 #include <babeltrace/ctf-ir/event-types.h>
26 /* List-related functions
27 ----------------------------------------------------
31 struct bt_definition
**_bt_python_field_listcaller(
32 const struct bt_ctf_event
*ctf_event
,
33 const struct bt_definition
*scope
,
36 struct bt_definition
**list
;
39 ret
= bt_ctf_get_field_list(ctf_event
, scope
,
40 (const struct bt_definition
* const **)&list
, len
);
42 if (ret
< 0) /* For python to know an error occured */
48 struct bt_definition
*_bt_python_field_one_from_list(
49 struct bt_definition
**list
, int index
)
55 struct bt_ctf_event_decl
**_bt_python_event_decl_listcaller(
57 struct bt_context
*ctx
,
60 struct bt_ctf_event_decl
**list
;
63 ret
= bt_ctf_get_event_decl_list(handle_id
, ctx
,
64 (struct bt_ctf_event_decl
* const **)&list
, len
);
66 if (ret
< 0) /* For python to know an error occured */
72 struct bt_ctf_event_decl
*_bt_python_decl_one_from_list(
73 struct bt_ctf_event_decl
**list
, int index
)
79 struct bt_ctf_field_decl
**_by_python_field_decl_listcaller(
80 struct bt_ctf_event_decl
*event_decl
,
81 enum bt_ctf_scope scope
,
84 struct bt_ctf_field_decl
**list
;
87 ret
= bt_ctf_get_decl_fields(event_decl
, scope
,
88 (const struct bt_ctf_field_decl
* const **)&list
, len
);
90 if (ret
< 0) /* For python to know an error occured */
96 struct bt_ctf_field_decl
*_bt_python_field_decl_one_from_list(
97 struct bt_ctf_field_decl
**list
, int index
)
102 struct definition_array
*_bt_python_get_array_from_def(
103 struct bt_definition
*field
)
105 const struct bt_declaration
*array_decl
;
106 struct definition_array
*array
= NULL
;
112 array_decl
= bt_ctf_get_decl_from_def(field
);
113 if (bt_ctf_field_type(array_decl
) == CTF_TYPE_ARRAY
) {
114 array
= container_of(field
, struct definition_array
, p
);
120 struct bt_declaration
*_bt_python_get_array_element_declaration(
121 struct bt_declaration
*field
)
123 struct declaration_array
*array_decl
;
124 struct bt_declaration
*ret
= NULL
;
130 array_decl
= container_of(field
, struct declaration_array
, p
);
131 ret
= array_decl
->elem
;
136 struct bt_declaration
*_bt_python_get_sequence_element_declaration(
137 struct bt_declaration
*field
)
139 struct declaration_sequence
*sequence_decl
;
140 struct bt_declaration
*ret
= NULL
;
146 sequence_decl
= container_of(field
, struct declaration_sequence
, p
);
147 ret
= sequence_decl
->elem
;
152 const char *_bt_python_get_array_string(struct bt_definition
*field
)
154 struct definition_array
*array
;
155 const char *ret
= NULL
;
161 array
= container_of(field
, struct definition_array
, p
);
162 ret
= array
->string
->str
;
167 const char *_bt_python_get_sequence_string(struct bt_definition
*field
)
169 struct definition_sequence
*sequence
;
170 const char *ret
= NULL
;
176 sequence
= container_of(field
, struct definition_sequence
, p
);
177 ret
= sequence
->string
->str
;
182 struct definition_sequence
*_bt_python_get_sequence_from_def(
183 struct bt_definition
*field
)
185 if (field
&& bt_ctf_field_type(
186 bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_SEQUENCE
) {
187 return container_of(field
, struct definition_sequence
, p
);
193 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field
*field
)
197 if (!field
|| field
->type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
202 const struct bt_ctf_field_type_integer
*type
= container_of(field
->type
,
203 const struct bt_ctf_field_type_integer
, parent
);
204 ret
= type
->declaration
.signedness
;
209 enum ctf_type_id
_bt_python_get_field_type(const struct bt_ctf_field
*field
)
211 enum ctf_type_id type_id
= CTF_TYPE_UNKNOWN
;
217 type_id
= field
->type
->declaration
->id
;
223 * Swig doesn't handle returning pointers via output arguments properly...
224 * These functions only wrap the ctf-ir functions to provide them directly
225 * as regular return values.
227 const char *_bt_python_ctf_field_type_enumeration_get_mapping(
228 struct bt_ctf_field_type
*enumeration
, size_t index
,
229 int64_t *range_start
, int64_t *range_end
)
234 ret
= bt_ctf_field_type_enumeration_get_mapping(enumeration
, index
,
235 &name
, range_start
, range_end
);
236 return !ret
? name
: NULL
;
239 const char *_bt_python_ctf_field_type_enumeration_get_mapping_unsigned(
240 struct bt_ctf_field_type
*enumeration
, size_t index
,
241 uint64_t *range_start
, uint64_t *range_end
)
246 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(enumeration
,
247 index
, &name
, range_start
, range_end
);
248 return !ret
? name
: NULL
;
251 const char *_bt_python_ctf_field_type_structure_get_field_name(
252 struct bt_ctf_field_type
*structure
, size_t index
)
256 struct bt_ctf_field_type
*type
;
258 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
265 bt_ctf_field_type_put(type
);
270 struct bt_ctf_field_type
*_bt_python_ctf_field_type_structure_get_field_type(
271 struct bt_ctf_field_type
*structure
, size_t index
)
275 struct bt_ctf_field_type
*type
;
277 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
279 return !ret
? type
: NULL
;
282 const char *_bt_python_ctf_field_type_variant_get_field_name(
283 struct bt_ctf_field_type
*variant
, size_t index
)
287 struct bt_ctf_field_type
*type
;
289 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
296 bt_ctf_field_type_put(type
);
301 struct bt_ctf_field_type
*_bt_python_ctf_field_type_variant_get_field_type(
302 struct bt_ctf_field_type
*variant
, size_t index
)
306 struct bt_ctf_field_type
*type
;
308 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
310 return !ret
? type
: NULL
;