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>
25 #include <babeltrace/ctf-ir/event.h>
27 /* List-related functions
28 ----------------------------------------------------
32 struct bt_definition
**_bt_python_field_listcaller(
33 const struct bt_ctf_event
*ctf_event
,
34 const struct bt_definition
*scope
,
37 struct bt_definition
**list
;
40 ret
= bt_ctf_get_field_list(ctf_event
, scope
,
41 (const struct bt_definition
* const **)&list
, len
);
43 if (ret
< 0) /* For python to know an error occured */
49 struct bt_definition
*_bt_python_field_one_from_list(
50 struct bt_definition
**list
, int index
)
56 struct bt_ctf_event_decl
**_bt_python_event_decl_listcaller(
58 struct bt_context
*ctx
,
61 struct bt_ctf_event_decl
**list
;
64 ret
= bt_ctf_get_event_decl_list(handle_id
, ctx
,
65 (struct bt_ctf_event_decl
* const **)&list
, len
);
67 if (ret
< 0) /* For python to know an error occured */
73 struct bt_ctf_event_decl
*_bt_python_decl_one_from_list(
74 struct bt_ctf_event_decl
**list
, int index
)
80 struct bt_ctf_field_decl
**_by_python_field_decl_listcaller(
81 struct bt_ctf_event_decl
*event_decl
,
82 enum bt_ctf_scope scope
,
85 struct bt_ctf_field_decl
**list
;
88 ret
= bt_ctf_get_decl_fields(event_decl
, scope
,
89 (const struct bt_ctf_field_decl
* const **)&list
, len
);
91 if (ret
< 0) /* For python to know an error occured */
97 struct bt_ctf_field_decl
*_bt_python_field_decl_one_from_list(
98 struct bt_ctf_field_decl
**list
, int index
)
103 struct definition_array
*_bt_python_get_array_from_def(
104 struct bt_definition
*field
)
106 const struct bt_declaration
*array_decl
;
107 struct definition_array
*array
= NULL
;
113 array_decl
= bt_ctf_get_decl_from_def(field
);
114 if (bt_ctf_field_type(array_decl
) == CTF_TYPE_ARRAY
) {
115 array
= container_of(field
, struct definition_array
, p
);
121 struct bt_declaration
*_bt_python_get_array_element_declaration(
122 struct bt_declaration
*field
)
124 struct declaration_array
*array_decl
;
125 struct bt_declaration
*ret
= NULL
;
131 array_decl
= container_of(field
, struct declaration_array
, p
);
132 ret
= array_decl
->elem
;
137 struct bt_declaration
*_bt_python_get_sequence_element_declaration(
138 struct bt_declaration
*field
)
140 struct declaration_sequence
*sequence_decl
;
141 struct bt_declaration
*ret
= NULL
;
147 sequence_decl
= container_of(field
, struct declaration_sequence
, p
);
148 ret
= sequence_decl
->elem
;
153 const char *_bt_python_get_array_string(struct bt_definition
*field
)
155 struct definition_array
*array
;
156 const char *ret
= NULL
;
162 array
= container_of(field
, struct definition_array
, p
);
163 ret
= array
->string
->str
;
168 const char *_bt_python_get_sequence_string(struct bt_definition
*field
)
170 struct definition_sequence
*sequence
;
171 const char *ret
= NULL
;
177 sequence
= container_of(field
, struct definition_sequence
, p
);
178 ret
= sequence
->string
->str
;
183 struct definition_sequence
*_bt_python_get_sequence_from_def(
184 struct bt_definition
*field
)
186 if (field
&& bt_ctf_field_type(
187 bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_SEQUENCE
) {
188 return container_of(field
, struct definition_sequence
, p
);
194 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field
*field
)
198 if (!field
|| field
->type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
203 const struct bt_ctf_field_type_integer
*type
= container_of(field
->type
,
204 const struct bt_ctf_field_type_integer
, parent
);
205 ret
= type
->declaration
.signedness
;
210 enum ctf_type_id
_bt_python_get_field_type(const struct bt_ctf_field
*field
)
212 enum ctf_type_id type_id
= CTF_TYPE_UNKNOWN
;
218 type_id
= field
->type
->declaration
->id
;
224 * Swig doesn't handle returning pointers via output arguments properly...
225 * These functions only wrap the ctf-ir functions to provide them directly
226 * as regular return values.
228 const char *_bt_python_ctf_field_type_enumeration_get_mapping(
229 struct bt_ctf_field_type
*enumeration
, size_t index
,
230 int64_t *range_start
, int64_t *range_end
)
235 ret
= bt_ctf_field_type_enumeration_get_mapping(enumeration
, index
,
236 &name
, range_start
, range_end
);
237 return !ret
? name
: NULL
;
240 const char *_bt_python_ctf_field_type_enumeration_get_mapping_unsigned(
241 struct bt_ctf_field_type
*enumeration
, size_t index
,
242 uint64_t *range_start
, uint64_t *range_end
)
247 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(enumeration
,
248 index
, &name
, range_start
, range_end
);
249 return !ret
? name
: NULL
;
252 const char *_bt_python_ctf_field_type_structure_get_field_name(
253 struct bt_ctf_field_type
*structure
, size_t index
)
257 struct bt_ctf_field_type
*type
;
259 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
266 bt_ctf_field_type_put(type
);
271 struct bt_ctf_field_type
*_bt_python_ctf_field_type_structure_get_field_type(
272 struct bt_ctf_field_type
*structure
, size_t index
)
276 struct bt_ctf_field_type
*type
;
278 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
280 return !ret
? type
: NULL
;
283 const char *_bt_python_ctf_field_type_variant_get_field_name(
284 struct bt_ctf_field_type
*variant
, size_t index
)
288 struct bt_ctf_field_type
*type
;
290 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
297 bt_ctf_field_type_put(type
);
302 struct bt_ctf_field_type
*_bt_python_ctf_field_type_variant_get_field_type(
303 struct bt_ctf_field_type
*variant
, size_t index
)
307 struct bt_ctf_field_type
*type
;
309 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
311 return !ret
? type
: NULL
;
314 const char *_bt_python_ctf_event_class_get_field_name(
315 struct bt_ctf_event_class
*event_class
, size_t index
)
319 struct bt_ctf_field_type
*type
;
321 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
328 bt_ctf_field_type_put(type
);
333 struct bt_ctf_field_type
*_bt_python_ctf_event_class_get_field_type(
334 struct bt_ctf_event_class
*event_class
, size_t index
)
338 struct bt_ctf_field_type
*type
;
340 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
342 return !ret
? type
: NULL
;