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/event-types-internal.h>
30 #include <babeltrace/ctf-ir/event-fields-internal.h>
31 #include <babeltrace/ctf-ir/event-types.h>
32 #include <babeltrace/ctf-ir/event.h>
33 #include <babeltrace/ctf-ir/clock-internal.h>
35 /* List-related functions
36 ----------------------------------------------------
40 struct bt_definition
**_bt_python_field_listcaller(
41 const struct bt_ctf_event
*ctf_event
,
42 const struct bt_definition
*scope
,
45 struct bt_definition
**list
;
48 ret
= bt_ctf_get_field_list(ctf_event
, scope
,
49 (const struct bt_definition
* const **)&list
, len
);
51 if (ret
< 0) /* For python to know an error occured */
57 struct bt_definition
*_bt_python_field_one_from_list(
58 struct bt_definition
**list
, int index
)
64 struct bt_ctf_event_decl
**_bt_python_event_decl_listcaller(
66 struct bt_context
*ctx
,
69 struct bt_ctf_event_decl
**list
;
72 ret
= bt_ctf_get_event_decl_list(handle_id
, ctx
,
73 (struct bt_ctf_event_decl
* const **)&list
, len
);
75 if (ret
< 0) /* For python to know an error occured */
81 struct bt_ctf_event_decl
*_bt_python_decl_one_from_list(
82 struct bt_ctf_event_decl
**list
, int index
)
88 struct bt_ctf_field_decl
**_by_python_field_decl_listcaller(
89 struct bt_ctf_event_decl
*event_decl
,
90 enum bt_ctf_scope scope
,
93 struct bt_ctf_field_decl
**list
;
96 ret
= bt_ctf_get_decl_fields(event_decl
, scope
,
97 (const struct bt_ctf_field_decl
* const **)&list
, len
);
99 if (ret
< 0) /* For python to know an error occured */
105 struct bt_ctf_field_decl
*_bt_python_field_decl_one_from_list(
106 struct bt_ctf_field_decl
**list
, int index
)
111 struct definition_array
*_bt_python_get_array_from_def(
112 struct bt_definition
*field
)
114 const struct bt_declaration
*array_decl
;
115 struct definition_array
*array
= NULL
;
121 array_decl
= bt_ctf_get_decl_from_def(field
);
122 if (bt_ctf_field_type(array_decl
) == CTF_TYPE_ARRAY
) {
123 array
= container_of(field
, struct definition_array
, p
);
129 struct bt_declaration
*_bt_python_get_array_element_declaration(
130 struct bt_declaration
*field
)
132 struct declaration_array
*array_decl
;
133 struct bt_declaration
*ret
= NULL
;
139 array_decl
= container_of(field
, struct declaration_array
, p
);
140 ret
= array_decl
->elem
;
145 struct bt_declaration
*_bt_python_get_sequence_element_declaration(
146 struct bt_declaration
*field
)
148 struct declaration_sequence
*sequence_decl
;
149 struct bt_declaration
*ret
= NULL
;
155 sequence_decl
= container_of(field
, struct declaration_sequence
, p
);
156 ret
= sequence_decl
->elem
;
161 const char *_bt_python_get_array_string(struct bt_definition
*field
)
163 struct definition_array
*array
;
164 const char *ret
= NULL
;
170 array
= container_of(field
, struct definition_array
, p
);
171 ret
= array
->string
->str
;
176 const char *_bt_python_get_sequence_string(struct bt_definition
*field
)
178 struct definition_sequence
*sequence
;
179 const char *ret
= NULL
;
185 sequence
= container_of(field
, struct definition_sequence
, p
);
186 ret
= sequence
->string
->str
;
191 struct definition_sequence
*_bt_python_get_sequence_from_def(
192 struct bt_definition
*field
)
194 if (field
&& bt_ctf_field_type(
195 bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_SEQUENCE
) {
196 return container_of(field
, struct definition_sequence
, p
);
202 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field
*field
)
206 if (!field
|| field
->type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
211 const struct bt_ctf_field_type_integer
*type
= container_of(field
->type
,
212 const struct bt_ctf_field_type_integer
, parent
);
213 ret
= type
->declaration
.signedness
;
218 enum ctf_type_id
_bt_python_get_field_type(const struct bt_ctf_field
*field
)
220 enum ctf_type_id type_id
= CTF_TYPE_UNKNOWN
;
226 type_id
= field
->type
->declaration
->id
;
232 * Swig doesn't handle returning pointers via output arguments properly...
233 * These functions only wrap the ctf-ir functions to provide them directly
234 * as regular return values.
236 const char *_bt_python_ctf_field_type_enumeration_get_mapping(
237 struct bt_ctf_field_type
*enumeration
, size_t index
,
238 int64_t *range_start
, int64_t *range_end
)
243 ret
= bt_ctf_field_type_enumeration_get_mapping(enumeration
, index
,
244 &name
, range_start
, range_end
);
245 return !ret
? name
: NULL
;
248 const char *_bt_python_ctf_field_type_enumeration_get_mapping_unsigned(
249 struct bt_ctf_field_type
*enumeration
, size_t index
,
250 uint64_t *range_start
, uint64_t *range_end
)
255 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(enumeration
,
256 index
, &name
, range_start
, range_end
);
257 return !ret
? name
: NULL
;
260 const char *_bt_python_ctf_field_type_structure_get_field_name(
261 struct bt_ctf_field_type
*structure
, size_t index
)
265 struct bt_ctf_field_type
*type
;
267 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
274 bt_ctf_field_type_put(type
);
279 struct bt_ctf_field_type
*_bt_python_ctf_field_type_structure_get_field_type(
280 struct bt_ctf_field_type
*structure
, size_t index
)
284 struct bt_ctf_field_type
*type
;
286 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
288 return !ret
? type
: NULL
;
291 const char *_bt_python_ctf_field_type_variant_get_field_name(
292 struct bt_ctf_field_type
*variant
, size_t index
)
296 struct bt_ctf_field_type
*type
;
298 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
305 bt_ctf_field_type_put(type
);
310 struct bt_ctf_field_type
*_bt_python_ctf_field_type_variant_get_field_type(
311 struct bt_ctf_field_type
*variant
, size_t index
)
315 struct bt_ctf_field_type
*type
;
317 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
319 return !ret
? type
: NULL
;
322 const char *_bt_python_ctf_event_class_get_field_name(
323 struct bt_ctf_event_class
*event_class
, size_t index
)
327 struct bt_ctf_field_type
*type
;
329 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
336 bt_ctf_field_type_put(type
);
341 struct bt_ctf_field_type
*_bt_python_ctf_event_class_get_field_type(
342 struct bt_ctf_event_class
*event_class
, size_t index
)
346 struct bt_ctf_field_type
*type
;
348 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
350 return !ret
? type
: NULL
;
353 int _bt_python_ctf_clock_get_uuid_index(struct bt_ctf_clock
*clock
,
354 size_t index
, unsigned char *value
)
357 const unsigned char *uuid
;
364 uuid
= bt_ctf_clock_get_uuid(clock
);
370 *value
= uuid
[index
];
375 int _bt_python_ctf_clock_set_uuid_index(struct bt_ctf_clock
*clock
,
376 size_t index
, unsigned char value
)
385 clock
->uuid
[index
] = value
;