| 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 | * 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 |
| 26 | */ |
| 27 | |
| 28 | #include "python-complements.h" |
| 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> |
| 32 | #include <babeltrace/ctf-ir/event.h> |
| 33 | #include <babeltrace/ctf-ir/event-class.h> |
| 34 | #include <babeltrace/ctf-ir/clock-internal.h> |
| 35 | #include <babeltrace/iterator.h> |
| 36 | #include <babeltrace/ctf/iterator.h> |
| 37 | #include <babeltrace/ctf/events-internal.h> |
| 38 | #include <glib.h> |
| 39 | |
| 40 | /* List-related functions |
| 41 | ---------------------------------------------------- |
| 42 | */ |
| 43 | |
| 44 | /* ctf-field-list */ |
| 45 | struct bt_definition **_bt_python_field_listcaller( |
| 46 | const struct bt_ctf_event *ctf_event, |
| 47 | const struct bt_definition *scope, |
| 48 | unsigned int *len) |
| 49 | { |
| 50 | struct bt_definition **list; |
| 51 | int ret; |
| 52 | |
| 53 | ret = bt_ctf_get_field_list(ctf_event, scope, |
| 54 | (const struct bt_definition * const **)&list, len); |
| 55 | |
| 56 | if (ret < 0) /* For python to know an error occured */ |
| 57 | list = NULL; |
| 58 | |
| 59 | return list; |
| 60 | } |
| 61 | |
| 62 | struct bt_definition *_bt_python_field_one_from_list( |
| 63 | struct bt_definition **list, int index) |
| 64 | { |
| 65 | return list[index]; |
| 66 | } |
| 67 | |
| 68 | /* event_decl_list */ |
| 69 | struct bt_ctf_event_decl **_bt_python_event_decl_listcaller( |
| 70 | int handle_id, |
| 71 | struct bt_context *ctx, |
| 72 | unsigned int *len) |
| 73 | { |
| 74 | struct bt_ctf_event_decl **list; |
| 75 | int ret; |
| 76 | |
| 77 | ret = bt_ctf_get_event_decl_list(handle_id, ctx, |
| 78 | (struct bt_ctf_event_decl * const **)&list, len); |
| 79 | |
| 80 | if (ret < 0) /* For python to know an error occured */ |
| 81 | list = NULL; |
| 82 | |
| 83 | return list; |
| 84 | } |
| 85 | |
| 86 | struct 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 */ |
| 93 | struct bt_ctf_field_decl **_by_python_field_decl_listcaller( |
| 94 | struct bt_ctf_event_decl *event_decl, |
| 95 | enum ctf_scope scope, |
| 96 | unsigned int *len) |
| 97 | { |
| 98 | struct bt_ctf_field_decl **list; |
| 99 | int ret; |
| 100 | |
| 101 | ret = bt_ctf_get_decl_fields(event_decl, scope, |
| 102 | (const struct bt_ctf_field_decl * const **)&list, len); |
| 103 | |
| 104 | if (ret < 0) /* For python to know an error occured */ |
| 105 | list = NULL; |
| 106 | |
| 107 | return list; |
| 108 | } |
| 109 | |
| 110 | struct 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 | } |
| 115 | |
| 116 | struct 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 | } |
| 130 | end: |
| 131 | return array; |
| 132 | } |
| 133 | |
| 134 | struct 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; |
| 146 | end: |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | struct 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; |
| 162 | end: |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | const 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; |
| 177 | end: |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | const 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; |
| 192 | end: |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | struct 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 | } |
| 206 | |
| 207 | int _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; |
| 219 | end: |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | enum 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; |
| 232 | end: |
| 233 | return type_id; |
| 234 | } |
| 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 | */ |
| 241 | const 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 | |
| 253 | const 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 | |
| 265 | const 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); |
| 280 | end: |
| 281 | return name; |
| 282 | } |
| 283 | |
| 284 | struct 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 | |
| 296 | const 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); |
| 311 | end: |
| 312 | return name; |
| 313 | } |
| 314 | |
| 315 | struct 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 | } |
| 326 | |
| 327 | const 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); |
| 342 | end: |
| 343 | return name; |
| 344 | } |
| 345 | |
| 346 | struct 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 | |
| 358 | int _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]; |
| 376 | end: |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | int _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; |
| 391 | end: |
| 392 | return ret; |
| 393 | } |
| 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 | */ |
| 401 | struct bt_iter_pos *_bt_python_create_iter_pos(void) |
| 402 | { |
| 403 | return g_new0(struct bt_iter_pos, 1); |
| 404 | } |
| 405 | |
| 406 | struct 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 | |
| 415 | int _bt_python_trace_collection_has_intersection(struct bt_context *ctx) |
| 416 | { |
| 417 | int ret; |
| 418 | int64_t begin, end; |
| 419 | |
| 420 | ret = ctf_find_tc_stream_packet_intersection_union(ctx, &begin, &end); |
| 421 | |
| 422 | return ret == 0 ? 1 : 0; |
| 423 | } |