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. | |
19 | */ | |
20 | ||
21 | #include "python-complements.h" | |
adc315b8 JG |
22 | #include <babeltrace/ctf-ir/event-types-internal.h> |
23 | #include <babeltrace/ctf-ir/event-fields-internal.h> | |
b4b5e4b5 | 24 | #include <babeltrace/ctf-ir/event-types.h> |
38f28df6 | 25 | #include <babeltrace/ctf-ir/event.h> |
24a3136a DS |
26 | |
27 | /* List-related functions | |
28 | ---------------------------------------------------- | |
29 | */ | |
30 | ||
31 | /* ctf-field-list */ | |
2c0df204 | 32 | struct bt_definition **_bt_python_field_listcaller( |
24a3136a | 33 | const struct bt_ctf_event *ctf_event, |
cebae8c3 JG |
34 | const struct bt_definition *scope, |
35 | unsigned int *len) | |
24a3136a | 36 | { |
2c0df204 | 37 | struct bt_definition **list; |
24a3136a DS |
38 | int ret; |
39 | ||
40 | ret = bt_ctf_get_field_list(ctf_event, scope, | |
cebae8c3 | 41 | (const struct bt_definition * const **)&list, len); |
24a3136a DS |
42 | |
43 | if (ret < 0) /* For python to know an error occured */ | |
44 | list = NULL; | |
24a3136a DS |
45 | |
46 | return list; | |
47 | } | |
48 | ||
2c0df204 XH |
49 | struct bt_definition *_bt_python_field_one_from_list( |
50 | struct bt_definition **list, int index) | |
24a3136a DS |
51 | { |
52 | return list[index]; | |
53 | } | |
54 | ||
55 | /* event_decl_list */ | |
56 | struct bt_ctf_event_decl **_bt_python_event_decl_listcaller( | |
cebae8c3 JG |
57 | int handle_id, |
58 | struct bt_context *ctx, | |
59 | unsigned int *len) | |
24a3136a DS |
60 | { |
61 | struct bt_ctf_event_decl **list; | |
24a3136a DS |
62 | int ret; |
63 | ||
64 | ret = bt_ctf_get_event_decl_list(handle_id, ctx, | |
cebae8c3 | 65 | (struct bt_ctf_event_decl * const **)&list, len); |
24a3136a DS |
66 | |
67 | if (ret < 0) /* For python to know an error occured */ | |
68 | list = NULL; | |
24a3136a DS |
69 | |
70 | return list; | |
71 | } | |
72 | ||
73 | struct bt_ctf_event_decl *_bt_python_decl_one_from_list( | |
74 | struct bt_ctf_event_decl **list, int index) | |
75 | { | |
76 | return list[index]; | |
77 | } | |
78 | ||
79 | /* decl_fields */ | |
80 | struct bt_ctf_field_decl **_by_python_field_decl_listcaller( | |
81 | struct bt_ctf_event_decl *event_decl, | |
cebae8c3 JG |
82 | enum bt_ctf_scope scope, |
83 | unsigned int *len) | |
24a3136a DS |
84 | { |
85 | struct bt_ctf_field_decl **list; | |
24a3136a DS |
86 | int ret; |
87 | ||
88 | ret = bt_ctf_get_decl_fields(event_decl, scope, | |
cebae8c3 | 89 | (const struct bt_ctf_field_decl * const **)&list, len); |
24a3136a DS |
90 | |
91 | if (ret < 0) /* For python to know an error occured */ | |
92 | list = NULL; | |
24a3136a DS |
93 | |
94 | return list; | |
95 | } | |
96 | ||
97 | struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list( | |
98 | struct bt_ctf_field_decl **list, int index) | |
99 | { | |
100 | return list[index]; | |
101 | } | |
3c2ce778 XH |
102 | |
103 | struct definition_array *_bt_python_get_array_from_def( | |
104 | struct bt_definition *field) | |
105 | { | |
106 | const struct bt_declaration *array_decl; | |
107 | struct definition_array *array = NULL; | |
108 | ||
109 | if (!field) { | |
110 | goto end; | |
111 | } | |
112 | ||
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); | |
116 | } | |
117 | end: | |
118 | return array; | |
119 | } | |
786207e0 | 120 | |
5792eb34 JG |
121 | struct bt_declaration *_bt_python_get_array_element_declaration( |
122 | struct bt_declaration *field) | |
123 | { | |
124 | struct declaration_array *array_decl; | |
125 | struct bt_declaration *ret = NULL; | |
126 | ||
127 | if (!field) { | |
128 | goto end; | |
129 | } | |
130 | ||
131 | array_decl = container_of(field, struct declaration_array, p); | |
132 | ret = array_decl->elem; | |
133 | end: | |
134 | return ret; | |
135 | } | |
136 | ||
3866c850 JG |
137 | struct bt_declaration *_bt_python_get_sequence_element_declaration( |
138 | struct bt_declaration *field) | |
139 | { | |
140 | struct declaration_sequence *sequence_decl; | |
141 | struct bt_declaration *ret = NULL; | |
142 | ||
143 | if (!field) { | |
144 | goto end; | |
145 | } | |
146 | ||
147 | sequence_decl = container_of(field, struct declaration_sequence, p); | |
148 | ret = sequence_decl->elem; | |
149 | end: | |
150 | return ret; | |
151 | } | |
152 | ||
5792eb34 JG |
153 | const char *_bt_python_get_array_string(struct bt_definition *field) |
154 | { | |
155 | struct definition_array *array; | |
156 | const char *ret = NULL; | |
157 | ||
158 | if (!field) { | |
159 | goto end; | |
160 | } | |
161 | ||
162 | array = container_of(field, struct definition_array, p); | |
163 | ret = array->string->str; | |
164 | end: | |
165 | return ret; | |
166 | } | |
167 | ||
3866c850 JG |
168 | const char *_bt_python_get_sequence_string(struct bt_definition *field) |
169 | { | |
170 | struct definition_sequence *sequence; | |
171 | const char *ret = NULL; | |
172 | ||
173 | if (!field) { | |
174 | goto end; | |
175 | } | |
176 | ||
177 | sequence = container_of(field, struct definition_sequence, p); | |
178 | ret = sequence->string->str; | |
179 | end: | |
180 | return ret; | |
181 | } | |
182 | ||
786207e0 XH |
183 | struct definition_sequence *_bt_python_get_sequence_from_def( |
184 | struct bt_definition *field) | |
185 | { | |
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); | |
189 | } | |
190 | ||
191 | return NULL; | |
192 | } | |
ec8c88d7 JG |
193 | |
194 | int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field) | |
195 | { | |
196 | int ret; | |
197 | ||
198 | if (!field || field->type->declaration->id != CTF_TYPE_INTEGER) { | |
199 | ret = -1; | |
200 | goto end; | |
201 | } | |
202 | ||
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; | |
206 | end: | |
207 | return ret; | |
208 | } | |
209 | ||
210 | enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field) | |
211 | { | |
212 | enum ctf_type_id type_id = CTF_TYPE_UNKNOWN; | |
213 | ||
214 | if (!field) { | |
215 | goto end; | |
216 | } | |
217 | ||
218 | type_id = field->type->declaration->id; | |
219 | end: | |
220 | return type_id; | |
221 | } | |
b4b5e4b5 JG |
222 | |
223 | /* | |
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. | |
227 | */ | |
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) | |
231 | { | |
232 | int ret; | |
233 | const char *name; | |
234 | ||
235 | ret = bt_ctf_field_type_enumeration_get_mapping(enumeration, index, | |
236 | &name, range_start, range_end); | |
237 | return !ret ? name : NULL; | |
238 | } | |
239 | ||
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) | |
243 | { | |
244 | int ret; | |
245 | const char *name; | |
246 | ||
247 | ret = bt_ctf_field_type_enumeration_get_mapping_unsigned(enumeration, | |
248 | index, &name, range_start, range_end); | |
249 | return !ret ? name : NULL; | |
250 | } | |
251 | ||
252 | const char *_bt_python_ctf_field_type_structure_get_field_name( | |
253 | struct bt_ctf_field_type *structure, size_t index) | |
254 | { | |
255 | int ret; | |
256 | const char *name; | |
257 | struct bt_ctf_field_type *type; | |
258 | ||
259 | ret = bt_ctf_field_type_structure_get_field(structure, &name, &type, | |
260 | index); | |
261 | if (ret) { | |
262 | name = NULL; | |
263 | goto end; | |
264 | } | |
265 | ||
266 | bt_ctf_field_type_put(type); | |
267 | end: | |
268 | return name; | |
269 | } | |
270 | ||
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) | |
273 | { | |
274 | int ret; | |
275 | const char *name; | |
276 | struct bt_ctf_field_type *type; | |
277 | ||
278 | ret = bt_ctf_field_type_structure_get_field(structure, &name, &type, | |
279 | index); | |
280 | return !ret ? type : NULL; | |
281 | } | |
282 | ||
283 | const char *_bt_python_ctf_field_type_variant_get_field_name( | |
284 | struct bt_ctf_field_type *variant, size_t index) | |
285 | { | |
286 | int ret; | |
287 | const char *name; | |
288 | struct bt_ctf_field_type *type; | |
289 | ||
290 | ret = bt_ctf_field_type_variant_get_field(variant, &name, &type, | |
291 | index); | |
292 | if (ret) { | |
293 | name = NULL; | |
294 | goto end; | |
295 | } | |
296 | ||
297 | bt_ctf_field_type_put(type); | |
298 | end: | |
299 | return name; | |
300 | } | |
301 | ||
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) | |
304 | { | |
305 | int ret; | |
306 | const char *name; | |
307 | struct bt_ctf_field_type *type; | |
308 | ||
309 | ret = bt_ctf_field_type_variant_get_field(variant, &name, &type, | |
310 | index); | |
311 | return !ret ? type : NULL; | |
312 | } | |
38f28df6 JG |
313 | |
314 | const char *_bt_python_ctf_event_class_get_field_name( | |
315 | struct bt_ctf_event_class *event_class, size_t index) | |
316 | { | |
317 | int ret; | |
318 | const char *name; | |
319 | struct bt_ctf_field_type *type; | |
320 | ||
321 | ret = bt_ctf_event_class_get_field(event_class, &name, &type, | |
322 | index); | |
323 | if (ret) { | |
324 | name = NULL; | |
325 | goto end; | |
326 | } | |
327 | ||
328 | bt_ctf_field_type_put(type); | |
329 | end: | |
330 | return name; | |
331 | } | |
332 | ||
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) | |
335 | { | |
336 | int ret; | |
337 | const char *name; | |
338 | struct bt_ctf_field_type *type; | |
339 | ||
340 | ret = bt_ctf_event_class_get_field(event_class, &name, &type, | |
341 | index); | |
342 | return !ret ? type : NULL; | |
343 | } | |
344 |