Commit | Line | Data |
---|---|---|
24a3136a DS |
1 | /* |
2 | * python-complements.c | |
3 | * | |
4 | * Babeltrace Python module complements, required for Python bindings | |
5 | * | |
6 | * Copyright 2012 EfficiOS Inc. | |
7 | * | |
8 | * Author: Danny Serres <danny.serres@efficios.com> | |
9 | * | |
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: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
5aa9939f JD |
19 | * |
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 | |
24a3136a DS |
26 | */ |
27 | ||
28 | #include "python-complements.h" | |
2e33ac5a PP |
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> | |
38f28df6 | 32 | #include <babeltrace/ctf-ir/event.h> |
272df73e | 33 | #include <babeltrace/ctf-ir/event-class.h> |
da64442f | 34 | #include <babeltrace/ctf-ir/clock-internal.h> |
1fa8e237 JG |
35 | #include <babeltrace/iterator.h> |
36 | #include <glib.h> | |
24a3136a DS |
37 | |
38 | /* List-related functions | |
39 | ---------------------------------------------------- | |
40 | */ | |
41 | ||
42 | /* ctf-field-list */ | |
2c0df204 | 43 | struct bt_definition **_bt_python_field_listcaller( |
24a3136a | 44 | const struct bt_ctf_event *ctf_event, |
cebae8c3 JG |
45 | const struct bt_definition *scope, |
46 | unsigned int *len) | |
24a3136a | 47 | { |
2c0df204 | 48 | struct bt_definition **list; |
24a3136a DS |
49 | int ret; |
50 | ||
51 | ret = bt_ctf_get_field_list(ctf_event, scope, | |
cebae8c3 | 52 | (const struct bt_definition * const **)&list, len); |
24a3136a DS |
53 | |
54 | if (ret < 0) /* For python to know an error occured */ | |
55 | list = NULL; | |
24a3136a DS |
56 | |
57 | return list; | |
58 | } | |
59 | ||
2c0df204 XH |
60 | struct bt_definition *_bt_python_field_one_from_list( |
61 | struct bt_definition **list, int index) | |
24a3136a DS |
62 | { |
63 | return list[index]; | |
64 | } | |
65 | ||
66 | /* event_decl_list */ | |
67 | struct bt_ctf_event_decl **_bt_python_event_decl_listcaller( | |
cebae8c3 JG |
68 | int handle_id, |
69 | struct bt_context *ctx, | |
70 | unsigned int *len) | |
24a3136a DS |
71 | { |
72 | struct bt_ctf_event_decl **list; | |
24a3136a DS |
73 | int ret; |
74 | ||
75 | ret = bt_ctf_get_event_decl_list(handle_id, ctx, | |
cebae8c3 | 76 | (struct bt_ctf_event_decl * const **)&list, len); |
24a3136a DS |
77 | |
78 | if (ret < 0) /* For python to know an error occured */ | |
79 | list = NULL; | |
24a3136a DS |
80 | |
81 | return list; | |
82 | } | |
83 | ||
84 | struct bt_ctf_event_decl *_bt_python_decl_one_from_list( | |
85 | struct bt_ctf_event_decl **list, int index) | |
86 | { | |
87 | return list[index]; | |
88 | } | |
89 | ||
90 | /* decl_fields */ | |
91 | struct bt_ctf_field_decl **_by_python_field_decl_listcaller( | |
92 | struct bt_ctf_event_decl *event_decl, | |
46df6b28 | 93 | enum ctf_scope scope, |
cebae8c3 | 94 | unsigned int *len) |
24a3136a DS |
95 | { |
96 | struct bt_ctf_field_decl **list; | |
24a3136a DS |
97 | int ret; |
98 | ||
99 | ret = bt_ctf_get_decl_fields(event_decl, scope, | |
cebae8c3 | 100 | (const struct bt_ctf_field_decl * const **)&list, len); |
24a3136a DS |
101 | |
102 | if (ret < 0) /* For python to know an error occured */ | |
103 | list = NULL; | |
24a3136a DS |
104 | |
105 | return list; | |
106 | } | |
107 | ||
108 | struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list( | |
109 | struct bt_ctf_field_decl **list, int index) | |
110 | { | |
111 | return list[index]; | |
112 | } | |
3c2ce778 XH |
113 | |
114 | struct definition_array *_bt_python_get_array_from_def( | |
115 | struct bt_definition *field) | |
116 | { | |
117 | const struct bt_declaration *array_decl; | |
118 | struct definition_array *array = NULL; | |
119 | ||
120 | if (!field) { | |
121 | goto end; | |
122 | } | |
123 | ||
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); | |
127 | } | |
128 | end: | |
129 | return array; | |
130 | } | |
786207e0 | 131 | |
5792eb34 JG |
132 | struct bt_declaration *_bt_python_get_array_element_declaration( |
133 | struct bt_declaration *field) | |
134 | { | |
135 | struct declaration_array *array_decl; | |
136 | struct bt_declaration *ret = NULL; | |
137 | ||
138 | if (!field) { | |
139 | goto end; | |
140 | } | |
141 | ||
142 | array_decl = container_of(field, struct declaration_array, p); | |
143 | ret = array_decl->elem; | |
144 | end: | |
145 | return ret; | |
146 | } | |
147 | ||
3866c850 JG |
148 | struct bt_declaration *_bt_python_get_sequence_element_declaration( |
149 | struct bt_declaration *field) | |
150 | { | |
151 | struct declaration_sequence *sequence_decl; | |
152 | struct bt_declaration *ret = NULL; | |
153 | ||
154 | if (!field) { | |
155 | goto end; | |
156 | } | |
157 | ||
158 | sequence_decl = container_of(field, struct declaration_sequence, p); | |
159 | ret = sequence_decl->elem; | |
160 | end: | |
161 | return ret; | |
162 | } | |
163 | ||
5792eb34 JG |
164 | const char *_bt_python_get_array_string(struct bt_definition *field) |
165 | { | |
166 | struct definition_array *array; | |
167 | const char *ret = NULL; | |
168 | ||
169 | if (!field) { | |
170 | goto end; | |
171 | } | |
172 | ||
173 | array = container_of(field, struct definition_array, p); | |
174 | ret = array->string->str; | |
175 | end: | |
176 | return ret; | |
177 | } | |
178 | ||
3866c850 JG |
179 | const char *_bt_python_get_sequence_string(struct bt_definition *field) |
180 | { | |
181 | struct definition_sequence *sequence; | |
182 | const char *ret = NULL; | |
183 | ||
184 | if (!field) { | |
185 | goto end; | |
186 | } | |
187 | ||
188 | sequence = container_of(field, struct definition_sequence, p); | |
189 | ret = sequence->string->str; | |
190 | end: | |
191 | return ret; | |
192 | } | |
193 | ||
786207e0 XH |
194 | struct definition_sequence *_bt_python_get_sequence_from_def( |
195 | struct bt_definition *field) | |
196 | { | |
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); | |
200 | } | |
201 | ||
202 | return NULL; | |
203 | } | |
ec8c88d7 JG |
204 | |
205 | int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field) | |
206 | { | |
207 | int ret; | |
208 | ||
209 | if (!field || field->type->declaration->id != CTF_TYPE_INTEGER) { | |
210 | ret = -1; | |
211 | goto end; | |
212 | } | |
213 | ||
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; | |
217 | end: | |
218 | return ret; | |
219 | } | |
220 | ||
221 | enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field) | |
222 | { | |
223 | enum ctf_type_id type_id = CTF_TYPE_UNKNOWN; | |
224 | ||
225 | if (!field) { | |
226 | goto end; | |
227 | } | |
228 | ||
229 | type_id = field->type->declaration->id; | |
230 | end: | |
231 | return type_id; | |
232 | } | |
b4b5e4b5 JG |
233 | |
234 | /* | |
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. | |
238 | */ | |
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) | |
242 | { | |
243 | int ret; | |
244 | const char *name; | |
245 | ||
246 | ret = bt_ctf_field_type_enumeration_get_mapping(enumeration, index, | |
247 | &name, range_start, range_end); | |
248 | return !ret ? name : NULL; | |
249 | } | |
250 | ||
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) | |
254 | { | |
255 | int ret; | |
256 | const char *name; | |
257 | ||
258 | ret = bt_ctf_field_type_enumeration_get_mapping_unsigned(enumeration, | |
259 | index, &name, range_start, range_end); | |
260 | return !ret ? name : NULL; | |
261 | } | |
262 | ||
263 | const char *_bt_python_ctf_field_type_structure_get_field_name( | |
264 | struct bt_ctf_field_type *structure, size_t index) | |
265 | { | |
266 | int ret; | |
267 | const char *name; | |
268 | struct bt_ctf_field_type *type; | |
269 | ||
270 | ret = bt_ctf_field_type_structure_get_field(structure, &name, &type, | |
271 | index); | |
272 | if (ret) { | |
273 | name = NULL; | |
274 | goto end; | |
275 | } | |
276 | ||
277 | bt_ctf_field_type_put(type); | |
278 | end: | |
279 | return name; | |
280 | } | |
281 | ||
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) | |
284 | { | |
285 | int ret; | |
286 | const char *name; | |
287 | struct bt_ctf_field_type *type; | |
288 | ||
289 | ret = bt_ctf_field_type_structure_get_field(structure, &name, &type, | |
290 | index); | |
291 | return !ret ? type : NULL; | |
292 | } | |
293 | ||
294 | const char *_bt_python_ctf_field_type_variant_get_field_name( | |
295 | struct bt_ctf_field_type *variant, size_t index) | |
296 | { | |
297 | int ret; | |
298 | const char *name; | |
299 | struct bt_ctf_field_type *type; | |
300 | ||
301 | ret = bt_ctf_field_type_variant_get_field(variant, &name, &type, | |
302 | index); | |
303 | if (ret) { | |
304 | name = NULL; | |
305 | goto end; | |
306 | } | |
307 | ||
308 | bt_ctf_field_type_put(type); | |
309 | end: | |
310 | return name; | |
311 | } | |
312 | ||
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) | |
315 | { | |
316 | int ret; | |
317 | const char *name; | |
318 | struct bt_ctf_field_type *type; | |
319 | ||
320 | ret = bt_ctf_field_type_variant_get_field(variant, &name, &type, | |
321 | index); | |
322 | return !ret ? type : NULL; | |
323 | } | |
38f28df6 JG |
324 | |
325 | const char *_bt_python_ctf_event_class_get_field_name( | |
326 | struct bt_ctf_event_class *event_class, size_t index) | |
327 | { | |
328 | int ret; | |
329 | const char *name; | |
330 | struct bt_ctf_field_type *type; | |
331 | ||
332 | ret = bt_ctf_event_class_get_field(event_class, &name, &type, | |
333 | index); | |
334 | if (ret) { | |
335 | name = NULL; | |
336 | goto end; | |
337 | } | |
338 | ||
339 | bt_ctf_field_type_put(type); | |
340 | end: | |
341 | return name; | |
342 | } | |
343 | ||
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) | |
346 | { | |
347 | int ret; | |
348 | const char *name; | |
349 | struct bt_ctf_field_type *type; | |
350 | ||
351 | ret = bt_ctf_event_class_get_field(event_class, &name, &type, | |
352 | index); | |
353 | return !ret ? type : NULL; | |
354 | } | |
355 | ||
da64442f JG |
356 | int _bt_python_ctf_clock_get_uuid_index(struct bt_ctf_clock *clock, |
357 | size_t index, unsigned char *value) | |
358 | { | |
359 | int ret = 0; | |
360 | const unsigned char *uuid; | |
361 | ||
362 | if (index >= 16) { | |
363 | ret = -1; | |
364 | goto end; | |
365 | } | |
366 | ||
367 | uuid = bt_ctf_clock_get_uuid(clock); | |
368 | if (!uuid) { | |
369 | ret = -1; | |
370 | goto end; | |
371 | } | |
372 | ||
373 | *value = uuid[index]; | |
374 | end: | |
375 | return ret; | |
376 | } | |
377 | ||
378 | int _bt_python_ctf_clock_set_uuid_index(struct bt_ctf_clock *clock, | |
379 | size_t index, unsigned char value) | |
380 | { | |
381 | int ret = 0; | |
382 | ||
383 | if (index >= 16) { | |
384 | ret = -1; | |
385 | goto end; | |
386 | } | |
387 | ||
388 | clock->uuid[index] = value; | |
389 | end: | |
390 | return ret; | |
391 | } | |
1fa8e237 JG |
392 | |
393 | /* | |
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. | |
398 | */ | |
399 | struct bt_iter_pos *_bt_python_create_iter_pos(void) | |
400 | { | |
401 | return g_new0(struct bt_iter_pos, 1); | |
402 | } |