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