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" | |
adc315b8 JG |
29 | #include <babeltrace/ctf-ir/event-types-internal.h> |
30 | #include <babeltrace/ctf-ir/event-fields-internal.h> | |
b4b5e4b5 | 31 | #include <babeltrace/ctf-ir/event-types.h> |
38f28df6 | 32 | #include <babeltrace/ctf-ir/event.h> |
da64442f | 33 | #include <babeltrace/ctf-ir/clock-internal.h> |
1fa8e237 JG |
34 | #include <babeltrace/iterator.h> |
35 | #include <glib.h> | |
24a3136a DS |
36 | |
37 | /* List-related functions | |
38 | ---------------------------------------------------- | |
39 | */ | |
40 | ||
41 | /* ctf-field-list */ | |
2c0df204 | 42 | struct bt_definition **_bt_python_field_listcaller( |
24a3136a | 43 | const struct bt_ctf_event *ctf_event, |
cebae8c3 JG |
44 | const struct bt_definition *scope, |
45 | unsigned int *len) | |
24a3136a | 46 | { |
2c0df204 | 47 | struct bt_definition **list; |
24a3136a DS |
48 | int ret; |
49 | ||
50 | ret = bt_ctf_get_field_list(ctf_event, scope, | |
cebae8c3 | 51 | (const struct bt_definition * const **)&list, len); |
24a3136a DS |
52 | |
53 | if (ret < 0) /* For python to know an error occured */ | |
54 | list = NULL; | |
24a3136a DS |
55 | |
56 | return list; | |
57 | } | |
58 | ||
2c0df204 XH |
59 | struct bt_definition *_bt_python_field_one_from_list( |
60 | struct bt_definition **list, int index) | |
24a3136a DS |
61 | { |
62 | return list[index]; | |
63 | } | |
64 | ||
65 | /* event_decl_list */ | |
66 | struct bt_ctf_event_decl **_bt_python_event_decl_listcaller( | |
cebae8c3 JG |
67 | int handle_id, |
68 | struct bt_context *ctx, | |
69 | unsigned int *len) | |
24a3136a DS |
70 | { |
71 | struct bt_ctf_event_decl **list; | |
24a3136a DS |
72 | int ret; |
73 | ||
74 | ret = bt_ctf_get_event_decl_list(handle_id, ctx, | |
cebae8c3 | 75 | (struct bt_ctf_event_decl * const **)&list, len); |
24a3136a DS |
76 | |
77 | if (ret < 0) /* For python to know an error occured */ | |
78 | list = NULL; | |
24a3136a DS |
79 | |
80 | return list; | |
81 | } | |
82 | ||
83 | struct bt_ctf_event_decl *_bt_python_decl_one_from_list( | |
84 | struct bt_ctf_event_decl **list, int index) | |
85 | { | |
86 | return list[index]; | |
87 | } | |
88 | ||
89 | /* decl_fields */ | |
90 | struct bt_ctf_field_decl **_by_python_field_decl_listcaller( | |
91 | struct bt_ctf_event_decl *event_decl, | |
cebae8c3 JG |
92 | enum bt_ctf_scope scope, |
93 | unsigned int *len) | |
24a3136a DS |
94 | { |
95 | struct bt_ctf_field_decl **list; | |
24a3136a DS |
96 | int ret; |
97 | ||
98 | ret = bt_ctf_get_decl_fields(event_decl, scope, | |
cebae8c3 | 99 | (const struct bt_ctf_field_decl * const **)&list, len); |
24a3136a DS |
100 | |
101 | if (ret < 0) /* For python to know an error occured */ | |
102 | list = NULL; | |
24a3136a DS |
103 | |
104 | return list; | |
105 | } | |
106 | ||
107 | struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list( | |
108 | struct bt_ctf_field_decl **list, int index) | |
109 | { | |
110 | return list[index]; | |
111 | } | |
3c2ce778 XH |
112 | |
113 | struct definition_array *_bt_python_get_array_from_def( | |
114 | struct bt_definition *field) | |
115 | { | |
116 | const struct bt_declaration *array_decl; | |
117 | struct definition_array *array = NULL; | |
118 | ||
119 | if (!field) { | |
120 | goto end; | |
121 | } | |
122 | ||
123 | array_decl = bt_ctf_get_decl_from_def(field); | |
124 | if (bt_ctf_field_type(array_decl) == CTF_TYPE_ARRAY) { | |
125 | array = container_of(field, struct definition_array, p); | |
126 | } | |
127 | end: | |
128 | return array; | |
129 | } | |
786207e0 | 130 | |
5792eb34 JG |
131 | struct bt_declaration *_bt_python_get_array_element_declaration( |
132 | struct bt_declaration *field) | |
133 | { | |
134 | struct declaration_array *array_decl; | |
135 | struct bt_declaration *ret = NULL; | |
136 | ||
137 | if (!field) { | |
138 | goto end; | |
139 | } | |
140 | ||
141 | array_decl = container_of(field, struct declaration_array, p); | |
142 | ret = array_decl->elem; | |
143 | end: | |
144 | return ret; | |
145 | } | |
146 | ||
3866c850 JG |
147 | struct bt_declaration *_bt_python_get_sequence_element_declaration( |
148 | struct bt_declaration *field) | |
149 | { | |
150 | struct declaration_sequence *sequence_decl; | |
151 | struct bt_declaration *ret = NULL; | |
152 | ||
153 | if (!field) { | |
154 | goto end; | |
155 | } | |
156 | ||
157 | sequence_decl = container_of(field, struct declaration_sequence, p); | |
158 | ret = sequence_decl->elem; | |
159 | end: | |
160 | return ret; | |
161 | } | |
162 | ||
5792eb34 JG |
163 | const char *_bt_python_get_array_string(struct bt_definition *field) |
164 | { | |
165 | struct definition_array *array; | |
166 | const char *ret = NULL; | |
167 | ||
168 | if (!field) { | |
169 | goto end; | |
170 | } | |
171 | ||
172 | array = container_of(field, struct definition_array, p); | |
173 | ret = array->string->str; | |
174 | end: | |
175 | return ret; | |
176 | } | |
177 | ||
3866c850 JG |
178 | const char *_bt_python_get_sequence_string(struct bt_definition *field) |
179 | { | |
180 | struct definition_sequence *sequence; | |
181 | const char *ret = NULL; | |
182 | ||
183 | if (!field) { | |
184 | goto end; | |
185 | } | |
186 | ||
187 | sequence = container_of(field, struct definition_sequence, p); | |
188 | ret = sequence->string->str; | |
189 | end: | |
190 | return ret; | |
191 | } | |
192 | ||
786207e0 XH |
193 | struct definition_sequence *_bt_python_get_sequence_from_def( |
194 | struct bt_definition *field) | |
195 | { | |
196 | if (field && bt_ctf_field_type( | |
197 | bt_ctf_get_decl_from_def(field)) == CTF_TYPE_SEQUENCE) { | |
198 | return container_of(field, struct definition_sequence, p); | |
199 | } | |
200 | ||
201 | return NULL; | |
202 | } | |
ec8c88d7 JG |
203 | |
204 | int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field) | |
205 | { | |
206 | int ret; | |
207 | ||
208 | if (!field || field->type->declaration->id != CTF_TYPE_INTEGER) { | |
209 | ret = -1; | |
210 | goto end; | |
211 | } | |
212 | ||
213 | const struct bt_ctf_field_type_integer *type = container_of(field->type, | |
214 | const struct bt_ctf_field_type_integer, parent); | |
215 | ret = type->declaration.signedness; | |
216 | end: | |
217 | return ret; | |
218 | } | |
219 | ||
220 | enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field) | |
221 | { | |
222 | enum ctf_type_id type_id = CTF_TYPE_UNKNOWN; | |
223 | ||
224 | if (!field) { | |
225 | goto end; | |
226 | } | |
227 | ||
228 | type_id = field->type->declaration->id; | |
229 | end: | |
230 | return type_id; | |
231 | } | |
b4b5e4b5 JG |
232 | |
233 | /* | |
234 | * Swig doesn't handle returning pointers via output arguments properly... | |
235 | * These functions only wrap the ctf-ir functions to provide them directly | |
236 | * as regular return values. | |
237 | */ | |
238 | const char *_bt_python_ctf_field_type_enumeration_get_mapping( | |
239 | struct bt_ctf_field_type *enumeration, size_t index, | |
240 | int64_t *range_start, int64_t *range_end) | |
241 | { | |
242 | int ret; | |
243 | const char *name; | |
244 | ||
245 | ret = bt_ctf_field_type_enumeration_get_mapping(enumeration, index, | |
246 | &name, range_start, range_end); | |
247 | return !ret ? name : NULL; | |
248 | } | |
249 | ||
250 | const char *_bt_python_ctf_field_type_enumeration_get_mapping_unsigned( | |
251 | struct bt_ctf_field_type *enumeration, size_t index, | |
252 | uint64_t *range_start, uint64_t *range_end) | |
253 | { | |
254 | int ret; | |
255 | const char *name; | |
256 | ||
257 | ret = bt_ctf_field_type_enumeration_get_mapping_unsigned(enumeration, | |
258 | index, &name, range_start, range_end); | |
259 | return !ret ? name : NULL; | |
260 | } | |
261 | ||
262 | const char *_bt_python_ctf_field_type_structure_get_field_name( | |
263 | struct bt_ctf_field_type *structure, size_t index) | |
264 | { | |
265 | int ret; | |
266 | const char *name; | |
267 | struct bt_ctf_field_type *type; | |
268 | ||
269 | ret = bt_ctf_field_type_structure_get_field(structure, &name, &type, | |
270 | index); | |
271 | if (ret) { | |
272 | name = NULL; | |
273 | goto end; | |
274 | } | |
275 | ||
276 | bt_ctf_field_type_put(type); | |
277 | end: | |
278 | return name; | |
279 | } | |
280 | ||
281 | struct bt_ctf_field_type *_bt_python_ctf_field_type_structure_get_field_type( | |
282 | struct bt_ctf_field_type *structure, size_t index) | |
283 | { | |
284 | int ret; | |
285 | const char *name; | |
286 | struct bt_ctf_field_type *type; | |
287 | ||
288 | ret = bt_ctf_field_type_structure_get_field(structure, &name, &type, | |
289 | index); | |
290 | return !ret ? type : NULL; | |
291 | } | |
292 | ||
293 | const char *_bt_python_ctf_field_type_variant_get_field_name( | |
294 | struct bt_ctf_field_type *variant, size_t index) | |
295 | { | |
296 | int ret; | |
297 | const char *name; | |
298 | struct bt_ctf_field_type *type; | |
299 | ||
300 | ret = bt_ctf_field_type_variant_get_field(variant, &name, &type, | |
301 | index); | |
302 | if (ret) { | |
303 | name = NULL; | |
304 | goto end; | |
305 | } | |
306 | ||
307 | bt_ctf_field_type_put(type); | |
308 | end: | |
309 | return name; | |
310 | } | |
311 | ||
312 | struct bt_ctf_field_type *_bt_python_ctf_field_type_variant_get_field_type( | |
313 | struct bt_ctf_field_type *variant, size_t index) | |
314 | { | |
315 | int ret; | |
316 | const char *name; | |
317 | struct bt_ctf_field_type *type; | |
318 | ||
319 | ret = bt_ctf_field_type_variant_get_field(variant, &name, &type, | |
320 | index); | |
321 | return !ret ? type : NULL; | |
322 | } | |
38f28df6 JG |
323 | |
324 | const char *_bt_python_ctf_event_class_get_field_name( | |
325 | struct bt_ctf_event_class *event_class, size_t index) | |
326 | { | |
327 | int ret; | |
328 | const char *name; | |
329 | struct bt_ctf_field_type *type; | |
330 | ||
331 | ret = bt_ctf_event_class_get_field(event_class, &name, &type, | |
332 | index); | |
333 | if (ret) { | |
334 | name = NULL; | |
335 | goto end; | |
336 | } | |
337 | ||
338 | bt_ctf_field_type_put(type); | |
339 | end: | |
340 | return name; | |
341 | } | |
342 | ||
343 | struct bt_ctf_field_type *_bt_python_ctf_event_class_get_field_type( | |
344 | struct bt_ctf_event_class *event_class, size_t index) | |
345 | { | |
346 | int ret; | |
347 | const char *name; | |
348 | struct bt_ctf_field_type *type; | |
349 | ||
350 | ret = bt_ctf_event_class_get_field(event_class, &name, &type, | |
351 | index); | |
352 | return !ret ? type : NULL; | |
353 | } | |
354 | ||
da64442f JG |
355 | int _bt_python_ctf_clock_get_uuid_index(struct bt_ctf_clock *clock, |
356 | size_t index, unsigned char *value) | |
357 | { | |
358 | int ret = 0; | |
359 | const unsigned char *uuid; | |
360 | ||
361 | if (index >= 16) { | |
362 | ret = -1; | |
363 | goto end; | |
364 | } | |
365 | ||
366 | uuid = bt_ctf_clock_get_uuid(clock); | |
367 | if (!uuid) { | |
368 | ret = -1; | |
369 | goto end; | |
370 | } | |
371 | ||
372 | *value = uuid[index]; | |
373 | end: | |
374 | return ret; | |
375 | } | |
376 | ||
377 | int _bt_python_ctf_clock_set_uuid_index(struct bt_ctf_clock *clock, | |
378 | size_t index, unsigned char value) | |
379 | { | |
380 | int ret = 0; | |
381 | ||
382 | if (index >= 16) { | |
383 | ret = -1; | |
384 | goto end; | |
385 | } | |
386 | ||
387 | clock->uuid[index] = value; | |
388 | end: | |
389 | return ret; | |
390 | } | |
1fa8e237 JG |
391 | |
392 | /* | |
393 | * Python 3.5 changes the StopIteration exception clearing behaviour which | |
394 | * erroneously marks swig clean-up function as having failed. This explicit | |
395 | * allocation function is intended as a work-around so SWIG doesn't manage | |
396 | * the lifetime of a "temporary" object by itself. | |
397 | */ | |
398 | struct bt_iter_pos *_bt_python_create_iter_pos(void) | |
399 | { | |
400 | return g_new0(struct bt_iter_pos, 1); | |
401 | } |