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