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>
26 #include <babeltrace/ctf-ir/clock-internal.h>
28 /* List-related functions
29 ----------------------------------------------------
33 struct bt_definition
**_bt_python_field_listcaller(
34 const struct bt_ctf_event
*ctf_event
,
35 const struct bt_definition
*scope
,
38 struct bt_definition
**list
;
41 ret
= bt_ctf_get_field_list(ctf_event
, scope
,
42 (const struct bt_definition
* const **)&list
, len
);
44 if (ret
< 0) /* For python to know an error occured */
50 struct bt_definition
*_bt_python_field_one_from_list(
51 struct bt_definition
**list
, int index
)
57 struct bt_ctf_event_decl
**_bt_python_event_decl_listcaller(
59 struct bt_context
*ctx
,
62 struct bt_ctf_event_decl
**list
;
65 ret
= bt_ctf_get_event_decl_list(handle_id
, ctx
,
66 (struct bt_ctf_event_decl
* const **)&list
, len
);
68 if (ret
< 0) /* For python to know an error occured */
74 struct bt_ctf_event_decl
*_bt_python_decl_one_from_list(
75 struct bt_ctf_event_decl
**list
, int index
)
81 struct bt_ctf_field_decl
**_by_python_field_decl_listcaller(
82 struct bt_ctf_event_decl
*event_decl
,
83 enum bt_ctf_scope scope
,
86 struct bt_ctf_field_decl
**list
;
89 ret
= bt_ctf_get_decl_fields(event_decl
, scope
,
90 (const struct bt_ctf_field_decl
* const **)&list
, len
);
92 if (ret
< 0) /* For python to know an error occured */
98 struct bt_ctf_field_decl
*_bt_python_field_decl_one_from_list(
99 struct bt_ctf_field_decl
**list
, int index
)
104 struct definition_array
*_bt_python_get_array_from_def(
105 struct bt_definition
*field
)
107 const struct bt_declaration
*array_decl
;
108 struct definition_array
*array
= NULL
;
114 array_decl
= bt_ctf_get_decl_from_def(field
);
115 if (bt_ctf_field_type(array_decl
) == CTF_TYPE_ARRAY
) {
116 array
= container_of(field
, struct definition_array
, p
);
122 struct bt_declaration
*_bt_python_get_array_element_declaration(
123 struct bt_declaration
*field
)
125 struct declaration_array
*array_decl
;
126 struct bt_declaration
*ret
= NULL
;
132 array_decl
= container_of(field
, struct declaration_array
, p
);
133 ret
= array_decl
->elem
;
138 struct bt_declaration
*_bt_python_get_sequence_element_declaration(
139 struct bt_declaration
*field
)
141 struct declaration_sequence
*sequence_decl
;
142 struct bt_declaration
*ret
= NULL
;
148 sequence_decl
= container_of(field
, struct declaration_sequence
, p
);
149 ret
= sequence_decl
->elem
;
154 const char *_bt_python_get_array_string(struct bt_definition
*field
)
156 struct definition_array
*array
;
157 const char *ret
= NULL
;
163 array
= container_of(field
, struct definition_array
, p
);
164 ret
= array
->string
->str
;
169 const char *_bt_python_get_sequence_string(struct bt_definition
*field
)
171 struct definition_sequence
*sequence
;
172 const char *ret
= NULL
;
178 sequence
= container_of(field
, struct definition_sequence
, p
);
179 ret
= sequence
->string
->str
;
184 struct definition_sequence
*_bt_python_get_sequence_from_def(
185 struct bt_definition
*field
)
187 if (field
&& bt_ctf_field_type(
188 bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_SEQUENCE
) {
189 return container_of(field
, struct definition_sequence
, p
);
195 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field
*field
)
199 if (!field
|| field
->type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
204 const struct bt_ctf_field_type_integer
*type
= container_of(field
->type
,
205 const struct bt_ctf_field_type_integer
, parent
);
206 ret
= type
->declaration
.signedness
;
211 enum ctf_type_id
_bt_python_get_field_type(const struct bt_ctf_field
*field
)
213 enum ctf_type_id type_id
= CTF_TYPE_UNKNOWN
;
219 type_id
= field
->type
->declaration
->id
;
225 * Swig doesn't handle returning pointers via output arguments properly...
226 * These functions only wrap the ctf-ir functions to provide them directly
227 * as regular return values.
229 const char *_bt_python_ctf_field_type_enumeration_get_mapping(
230 struct bt_ctf_field_type
*enumeration
, size_t index
,
231 int64_t *range_start
, int64_t *range_end
)
236 ret
= bt_ctf_field_type_enumeration_get_mapping(enumeration
, index
,
237 &name
, range_start
, range_end
);
238 return !ret
? name
: NULL
;
241 const char *_bt_python_ctf_field_type_enumeration_get_mapping_unsigned(
242 struct bt_ctf_field_type
*enumeration
, size_t index
,
243 uint64_t *range_start
, uint64_t *range_end
)
248 ret
= bt_ctf_field_type_enumeration_get_mapping_unsigned(enumeration
,
249 index
, &name
, range_start
, range_end
);
250 return !ret
? name
: NULL
;
253 const char *_bt_python_ctf_field_type_structure_get_field_name(
254 struct bt_ctf_field_type
*structure
, size_t index
)
258 struct bt_ctf_field_type
*type
;
260 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
267 bt_ctf_field_type_put(type
);
272 struct bt_ctf_field_type
*_bt_python_ctf_field_type_structure_get_field_type(
273 struct bt_ctf_field_type
*structure
, size_t index
)
277 struct bt_ctf_field_type
*type
;
279 ret
= bt_ctf_field_type_structure_get_field(structure
, &name
, &type
,
281 return !ret
? type
: NULL
;
284 const char *_bt_python_ctf_field_type_variant_get_field_name(
285 struct bt_ctf_field_type
*variant
, size_t index
)
289 struct bt_ctf_field_type
*type
;
291 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
298 bt_ctf_field_type_put(type
);
303 struct bt_ctf_field_type
*_bt_python_ctf_field_type_variant_get_field_type(
304 struct bt_ctf_field_type
*variant
, size_t index
)
308 struct bt_ctf_field_type
*type
;
310 ret
= bt_ctf_field_type_variant_get_field(variant
, &name
, &type
,
312 return !ret
? type
: NULL
;
315 const char *_bt_python_ctf_event_class_get_field_name(
316 struct bt_ctf_event_class
*event_class
, size_t index
)
320 struct bt_ctf_field_type
*type
;
322 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
329 bt_ctf_field_type_put(type
);
334 struct bt_ctf_field_type
*_bt_python_ctf_event_class_get_field_type(
335 struct bt_ctf_event_class
*event_class
, size_t index
)
339 struct bt_ctf_field_type
*type
;
341 ret
= bt_ctf_event_class_get_field(event_class
, &name
, &type
,
343 return !ret
? type
: NULL
;
346 int _bt_python_ctf_clock_get_uuid_index(struct bt_ctf_clock
*clock
,
347 size_t index
, unsigned char *value
)
350 const unsigned char *uuid
;
357 uuid
= bt_ctf_clock_get_uuid(clock
);
363 *value
= uuid
[index
];
368 int _bt_python_ctf_clock_set_uuid_index(struct bt_ctf_clock
*clock
,
369 size_t index
, unsigned char value
)
378 clock
->uuid
[index
] = value
;