4 * Babeltrace Python module complements, required for Python bindings
6 * Copyright 2012 EfficiOS Inc.
8 * Author: Danny Serres <danny.serres@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
21 #include "python-complements.h"
22 #include <babeltrace/ctf-writer/event-types-internal.h>
23 #include <babeltrace/ctf-writer/event-fields-internal.h>
24 #include <babeltrace/iterator.h>
25 #include <babeltrace/ctf/iterator.h>
26 #include <babeltrace/ctf/events-internal.h>
30 ----------------------------------------------------
33 FILE *_bt_file_open(char *file_path
, char *mode
)
36 if (file_path
!= NULL
)
37 fp
= fopen(file_path
, mode
);
41 void _bt_file_close(FILE *fp
)
48 /* List-related functions
49 ----------------------------------------------------
53 struct bt_definition
**_bt_python_field_listcaller(
54 const struct bt_ctf_event
*ctf_event
,
55 const struct bt_definition
*scope
,
58 struct bt_definition
**list
;
61 ret
= bt_ctf_get_field_list(ctf_event
, scope
,
62 (const struct bt_definition
* const **)&list
, len
);
64 if (ret
< 0) /* For python to know an error occured */
70 struct bt_definition
*_bt_python_field_one_from_list(
71 struct bt_definition
**list
, int index
)
77 struct bt_ctf_event_decl
**_bt_python_event_decl_listcaller(
79 struct bt_context
*ctx
,
82 struct bt_ctf_event_decl
**list
;
85 ret
= bt_ctf_get_event_decl_list(handle_id
, ctx
,
86 (struct bt_ctf_event_decl
* const **)&list
, len
);
88 if (ret
< 0) /* For python to know an error occured */
94 struct bt_ctf_event_decl
*_bt_python_decl_one_from_list(
95 struct bt_ctf_event_decl
**list
, int index
)
101 struct bt_ctf_field_decl
**_by_python_field_decl_listcaller(
102 struct bt_ctf_event_decl
*event_decl
,
103 enum bt_ctf_scope scope
,
106 struct bt_ctf_field_decl
**list
;
109 ret
= bt_ctf_get_decl_fields(event_decl
, scope
,
110 (const struct bt_ctf_field_decl
* const **)&list
, len
);
112 if (ret
< 0) /* For python to know an error occured */
118 struct bt_ctf_field_decl
*_bt_python_field_decl_one_from_list(
119 struct bt_ctf_field_decl
**list
, int index
)
124 struct definition_array
*_bt_python_get_array_from_def(
125 struct bt_definition
*field
)
127 const struct bt_declaration
*array_decl
;
128 struct definition_array
*array
= NULL
;
134 array_decl
= bt_ctf_get_decl_from_def(field
);
135 if (bt_ctf_field_type(array_decl
) == CTF_TYPE_ARRAY
) {
136 array
= container_of(field
, struct definition_array
, p
);
142 struct bt_declaration
*_bt_python_get_array_element_declaration(
143 struct bt_declaration
*field
)
145 struct declaration_array
*array_decl
;
146 struct bt_declaration
*ret
= NULL
;
152 array_decl
= container_of(field
, struct declaration_array
, p
);
153 ret
= array_decl
->elem
;
158 struct bt_declaration
*_bt_python_get_sequence_element_declaration(
159 struct bt_declaration
*field
)
161 struct declaration_sequence
*sequence_decl
;
162 struct bt_declaration
*ret
= NULL
;
168 sequence_decl
= container_of(field
, struct declaration_sequence
, p
);
169 ret
= sequence_decl
->elem
;
174 const char *_bt_python_get_array_string(struct bt_definition
*field
)
176 struct definition_array
*array
;
177 const char *ret
= NULL
;
183 array
= container_of(field
, struct definition_array
, p
);
184 ret
= array
->string
->str
;
189 const char *_bt_python_get_sequence_string(struct bt_definition
*field
)
191 struct definition_sequence
*sequence
;
192 const char *ret
= NULL
;
198 sequence
= container_of(field
, struct definition_sequence
, p
);
199 ret
= sequence
->string
->str
;
204 struct definition_sequence
*_bt_python_get_sequence_from_def(
205 struct bt_definition
*field
)
207 if (field
&& bt_ctf_field_type(
208 bt_ctf_get_decl_from_def(field
)) == CTF_TYPE_SEQUENCE
) {
209 return container_of(field
, struct definition_sequence
, p
);
215 int _bt_python_field_integer_get_signedness(const struct bt_ctf_field
*field
)
219 if (!field
|| field
->type
->declaration
->id
!= CTF_TYPE_INTEGER
) {
224 const struct bt_ctf_field_type_integer
*type
= container_of(field
->type
,
225 const struct bt_ctf_field_type_integer
, parent
);
226 ret
= type
->declaration
.signedness
;
231 enum ctf_type_id
_bt_python_get_field_type(const struct bt_ctf_field
*field
)
233 enum ctf_type_id type_id
= CTF_TYPE_UNKNOWN
;
239 type_id
= field
->type
->declaration
->id
;
245 * Python 3.5 changes the StopIteration exception clearing behaviour which
246 * erroneously marks swig clean-up function as having failed. This explicit
247 * allocation function is intended as a work-around so SWIG doesn't manage
248 * the lifetime of a "temporary" object by itself.
250 struct bt_iter_pos
*_bt_python_create_iter_pos(void)
252 return g_new0(struct bt_iter_pos
, 1);
255 struct bt_ctf_iter
*_bt_python_ctf_iter_create_intersect(
256 struct bt_context
*ctx
,
257 struct bt_iter_pos
*inter_begin_pos
,
258 struct bt_iter_pos
*inter_end_pos
)
260 return bt_ctf_iter_create_intersect(ctx
, &inter_begin_pos
,
264 int _bt_python_trace_collection_has_intersection(struct bt_context
*ctx
)
269 ret
= ctf_find_tc_stream_packet_intersection_union(ctx
, &begin
, &end
);
271 return ret
== 0 ? 1 : 0;