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