| 1 | /* |
| 2 | * babeltrace.i.in |
| 3 | * |
| 4 | * Babeltrace Python Module interface file |
| 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 | |
| 22 | %define DOCSTRING |
| 23 | "BABELTRACE_VERSION_STR |
| 24 | |
| 25 | Babeltrace is a trace viewer and converter reading and writing the |
| 26 | Common Trace Format (CTF). Its main use is to pretty-print CTF |
| 27 | traces into a human-readable text output. |
| 28 | |
| 29 | To use this module, the first step is to create a Context and add a |
| 30 | trace to it." |
| 31 | %enddef |
| 32 | |
| 33 | %module(docstring=DOCSTRING) babeltrace |
| 34 | |
| 35 | %include "typemaps.i" |
| 36 | %{ |
| 37 | #define SWIG_FILE_WITH_INIT |
| 38 | #include <babeltrace/babeltrace.h> |
| 39 | #include <babeltrace/babeltrace-internal.h> |
| 40 | #include <babeltrace/trace-handle.h> |
| 41 | #include <babeltrace/trace-handle-internal.h> |
| 42 | #include <babeltrace/context.h> |
| 43 | #include <babeltrace/context-internal.h> |
| 44 | #include <babeltrace/iterator.h> |
| 45 | #include <babeltrace/iterator-internal.h> |
| 46 | #include <babeltrace/format.h> |
| 47 | #include <babeltrace/list.h> |
| 48 | #include <babeltrace/types.h> |
| 49 | #include <babeltrace/ctf/iterator.h> |
| 50 | #include "python-complements.h" |
| 51 | #include <babeltrace/ctf-writer/clock.h> |
| 52 | #include <babeltrace/ctf-writer/event-fields.h> |
| 53 | #include <babeltrace/ctf-writer/event-types.h> |
| 54 | #include <babeltrace/ctf-writer/event.h> |
| 55 | #include <babeltrace/ctf-writer/stream.h> |
| 56 | #include <babeltrace/ctf-writer/writer.h> |
| 57 | %} |
| 58 | |
| 59 | typedef unsigned long long uint64_t; |
| 60 | typedef long long int64_t; |
| 61 | typedef int bt_intern_str; |
| 62 | |
| 63 | /* ================================================================= |
| 64 | PYTHON-COMPLEMENTS.H |
| 65 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 66 | */ |
| 67 | |
| 68 | FILE *_bt_file_open(char *file_path, char *mode); |
| 69 | void _bt_file_close(FILE *fp); |
| 70 | struct bt_definition **_bt_python_field_listcaller( |
| 71 | const struct bt_ctf_event *ctf_event, |
| 72 | const struct bt_definition *scope, |
| 73 | unsigned int *OUTPUT); |
| 74 | struct bt_definition *_bt_python_field_one_from_list( |
| 75 | struct bt_definition **list, int index); |
| 76 | struct bt_ctf_event_decl **_bt_python_event_decl_listcaller( |
| 77 | int handle_id, |
| 78 | struct bt_context *ctx, |
| 79 | unsigned int *OUTPUT); |
| 80 | struct bt_ctf_event_decl *_bt_python_decl_one_from_list( |
| 81 | struct bt_ctf_event_decl **list, int index); |
| 82 | struct bt_ctf_field_decl **_by_python_field_decl_listcaller( |
| 83 | struct bt_ctf_event_decl *event_decl, |
| 84 | enum bt_ctf_scope scope, |
| 85 | unsigned int *OUTPUT); |
| 86 | struct bt_ctf_field_decl *_bt_python_field_decl_one_from_list( |
| 87 | struct bt_ctf_field_decl **list, int index); |
| 88 | struct definition_array *_bt_python_get_array_from_def( |
| 89 | struct bt_definition *field); |
| 90 | struct definition_sequence *_bt_python_get_sequence_from_def( |
| 91 | struct bt_definition *field); |
| 92 | int _bt_python_field_integer_get_signedness(const struct bt_ctf_field *field); |
| 93 | enum ctf_type_id _bt_python_get_field_type(const struct bt_ctf_field *field); |
| 94 | |
| 95 | /* ================================================================= |
| 96 | CONTEXT.H, CONTEXT-INTERNAL.H |
| 97 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 98 | */ |
| 99 | |
| 100 | %rename("_bt_context_create") bt_context_create(void); |
| 101 | %rename("_bt_context_add_trace") bt_context_add_trace( |
| 102 | struct bt_context *ctx, const char *path, const char *format, |
| 103 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, int whence), |
| 104 | struct bt_mmap_stream_list *stream_list, FILE *metadata); |
| 105 | %rename("_bt_context_remove_trace") bt_context_remove_trace( |
| 106 | struct bt_context *ctx, int trace_id); |
| 107 | %rename("_bt_context_get") bt_context_get(struct bt_context *ctx); |
| 108 | %rename("_bt_context_put") bt_context_put(struct bt_context *ctx); |
| 109 | %rename("_bt_ctf_event_get_context") bt_ctf_event_get_context( |
| 110 | const struct bt_ctf_event *event); |
| 111 | |
| 112 | struct bt_context *bt_context_create(void); |
| 113 | int bt_context_add_trace(struct bt_context *ctx, const char *path, const char *format, |
| 114 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, int whence), |
| 115 | struct bt_mmap_stream_list *stream_list, FILE *metadata); |
| 116 | void bt_context_remove_trace(struct bt_context *ctx, int trace_id); |
| 117 | void bt_context_get(struct bt_context *ctx); |
| 118 | void bt_context_put(struct bt_context *ctx); |
| 119 | struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *event); |
| 120 | |
| 121 | // class Context to prevent direct access to struct bt_context |
| 122 | %pythoncode%{ |
| 123 | class Context: |
| 124 | """ |
| 125 | The context represents the object in which a trace_collection is |
| 126 | open. As long as this structure is allocated, the trace_collection |
| 127 | is open and the traces it contains can be read and seeked by the |
| 128 | iterators and callbacks. |
| 129 | """ |
| 130 | |
| 131 | def __init__(self): |
| 132 | self._c = _bt_context_create() |
| 133 | |
| 134 | def __del__(self): |
| 135 | _bt_context_put(self._c) |
| 136 | |
| 137 | def add_trace(self, path, format_str, |
| 138 | packet_seek=None, stream_list=None, metadata=None): |
| 139 | """ |
| 140 | Add a trace by path to the context. |
| 141 | |
| 142 | Open a trace. |
| 143 | |
| 144 | path is the path to the trace, it is not recursive. |
| 145 | If "path" is None, stream_list is used instead as a list |
| 146 | of mmap streams to open for the trace. |
| 147 | |
| 148 | format is a string containing the format name in which the trace was |
| 149 | produced. |
| 150 | |
| 151 | packet_seek is not implemented for Python. Should be left None to |
| 152 | use the default packet_seek handler provided by the trace format. |
| 153 | |
| 154 | stream_list is a linked list of streams, it is used to open a trace |
| 155 | where the trace data is located in memory mapped areas instead of |
| 156 | trace files, this argument should be None when path is not None. |
| 157 | |
| 158 | The metadata parameter acts as a metadata override when not None, |
| 159 | otherwise the format handles the metadata opening. |
| 160 | |
| 161 | Return: the corresponding TraceHandle on success or None on error. |
| 162 | """ |
| 163 | if metadata is not None: |
| 164 | metadata = metadata._file |
| 165 | |
| 166 | ret = _bt_context_add_trace(self._c, path, format_str, packet_seek, |
| 167 | stream_list, metadata) |
| 168 | if ret < 0: |
| 169 | return None |
| 170 | |
| 171 | th = TraceHandle.__new__(TraceHandle) |
| 172 | th._id = ret |
| 173 | return th |
| 174 | |
| 175 | def add_traces_recursive(self, path, format_str): |
| 176 | """ |
| 177 | Open a trace recursively. |
| 178 | |
| 179 | Find each trace present in the subdirectory starting from the given |
| 180 | path, and add them to the context. |
| 181 | |
| 182 | Return a dict of TraceHandle instances (the full path is the key). |
| 183 | Return None on error. |
| 184 | """ |
| 185 | |
| 186 | import os |
| 187 | |
| 188 | trace_handles = {} |
| 189 | |
| 190 | noTrace = True |
| 191 | error = False |
| 192 | |
| 193 | for fullpath, dirs, files in os.walk(path): |
| 194 | if "metadata" in files: |
| 195 | trace_handle = self.add_trace(fullpath, format_str) |
| 196 | if trace_handle is None: |
| 197 | error = True |
| 198 | continue |
| 199 | |
| 200 | trace_handles[fullpath] = trace_handle |
| 201 | noTrace = False |
| 202 | |
| 203 | if noTrace and error: |
| 204 | return None |
| 205 | return trace_handles |
| 206 | |
| 207 | def remove_trace(self, trace_handle): |
| 208 | """ |
| 209 | Remove a trace from the context. |
| 210 | Effectively closing the trace. |
| 211 | """ |
| 212 | try: |
| 213 | _bt_context_remove_trace(self._c, trace_handle._id) |
| 214 | except AttributeError: |
| 215 | raise TypeError("in remove_trace, " |
| 216 | "argument 2 must be a TraceHandle instance") |
| 217 | %} |
| 218 | |
| 219 | |
| 220 | |
| 221 | /* ================================================================= |
| 222 | FORMAT.H, REGISTRY |
| 223 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 224 | */ |
| 225 | |
| 226 | %rename("lookup_format") bt_lookup_format(bt_intern_str qname); |
| 227 | %rename("_bt_print_format_list") bt_fprintf_format_list(FILE *fp); |
| 228 | %rename("register_format") bt_register_format(struct format *format); |
| 229 | %rename("unregister_format") bt_unregister_format(struct bt_format *format); |
| 230 | |
| 231 | extern struct format *bt_lookup_format(bt_intern_str qname); |
| 232 | extern void bt_fprintf_format_list(FILE *fp); |
| 233 | extern int bt_register_format(struct bt_format *format); |
| 234 | extern void bt_unregister_format(struct bt_format *format); |
| 235 | |
| 236 | %pythoncode %{ |
| 237 | |
| 238 | def print_format_list(babeltrace_file): |
| 239 | """ |
| 240 | Print a list of available formats to file. |
| 241 | |
| 242 | babeltrace_file must be a File instance opened in write mode. |
| 243 | """ |
| 244 | try: |
| 245 | if babeltrace_file._file is not None: |
| 246 | _bt_print_format_list(babeltrace_file._file) |
| 247 | except AttributeError: |
| 248 | raise TypeError("in print_format_list, " |
| 249 | "argument 1 must be a File instance") |
| 250 | |
| 251 | %} |
| 252 | |
| 253 | |
| 254 | /* ================================================================= |
| 255 | ITERATOR.H, ITERATOR-INTERNAL.H |
| 256 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 257 | */ |
| 258 | |
| 259 | %rename("_bt_iter_create") bt_iter_create(struct bt_context *ctx, |
| 260 | const struct bt_iter_pos *begin_pos, const struct bt_iter_pos *end_pos); |
| 261 | %rename("_bt_iter_destroy") bt_iter_destroy(struct bt_iter *iter); |
| 262 | %rename("_bt_iter_next") bt_iter_next(struct bt_iter *iter); |
| 263 | %rename("_bt_iter_get_pos") bt_iter_get_pos(struct bt_iter *iter); |
| 264 | %rename("_bt_iter_free_pos") bt_iter_free_pos(struct bt_iter_pos *pos); |
| 265 | %rename("_bt_iter_set_pos") bt_iter_set_pos(struct bt_iter *iter, |
| 266 | const struct bt_iter_pos *pos); |
| 267 | %rename("_bt_iter_create_time_pos") bt_iter_create_time_pos(struct bt_iter *iter, |
| 268 | uint64_t timestamp); |
| 269 | |
| 270 | struct bt_iter *bt_iter_create(struct bt_context *ctx, |
| 271 | const struct bt_iter_pos *begin_pos, const struct bt_iter_pos *end_pos); |
| 272 | void bt_iter_destroy(struct bt_iter *iter); |
| 273 | int bt_iter_next(struct bt_iter *iter); |
| 274 | struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter); |
| 275 | void bt_iter_free_pos(struct bt_iter_pos *pos); |
| 276 | int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *pos); |
| 277 | struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter, uint64_t timestamp); |
| 278 | |
| 279 | %rename("_bt_iter_pos") bt_iter_pos; |
| 280 | %rename("SEEK_TIME") BT_SEEK_TIME; |
| 281 | %rename("SEEK_RESTORE") BT_SEEK_RESTORE; |
| 282 | %rename("SEEK_CUR") BT_SEEK_CUR; |
| 283 | %rename("SEEK_BEGIN") BT_SEEK_BEGIN; |
| 284 | %rename("SEEK_LAST") BT_SEEK_LAST; |
| 285 | |
| 286 | // This struct is taken from iterator.h |
| 287 | // All changes to the struct must also be made here |
| 288 | struct bt_iter_pos { |
| 289 | enum { |
| 290 | BT_SEEK_TIME, /* uses u.seek_time */ |
| 291 | BT_SEEK_RESTORE, /* uses u.restore */ |
| 292 | BT_SEEK_CUR, |
| 293 | BT_SEEK_BEGIN, |
| 294 | BT_SEEK_LAST |
| 295 | } type; |
| 296 | union { |
| 297 | uint64_t seek_time; |
| 298 | struct bt_saved_pos *restore; |
| 299 | } u; |
| 300 | }; |
| 301 | |
| 302 | |
| 303 | %pythoncode%{ |
| 304 | |
| 305 | class IterPos: |
| 306 | """This class represents the position where to set an iterator.""" |
| 307 | |
| 308 | __can_access = False |
| 309 | |
| 310 | def __init__(self, seek_type, seek_time = None): |
| 311 | """ |
| 312 | seek_type represents the type of seek to use. |
| 313 | seek_time is the timestamp to seek to when using SEEK_TIME, it |
| 314 | is expressed in nanoseconds |
| 315 | Only use SEEK_RESTORE on IterPos obtained from the get_pos function |
| 316 | in Iter class. |
| 317 | """ |
| 318 | |
| 319 | self._pos = _bt_iter_pos() |
| 320 | self._pos.type = seek_type |
| 321 | if seek_time and seek_type == SEEK_TIME: |
| 322 | self._pos.u.seek_time = seek_time |
| 323 | self.__can_access = True |
| 324 | |
| 325 | def __del__(self): |
| 326 | if not self.__can_access: |
| 327 | _bt_iter_free_pos(self._pos) |
| 328 | |
| 329 | def _get_type(self): |
| 330 | if not __can_access: |
| 331 | raise AttributeError("seek_type is not available") |
| 332 | return self._pos.type |
| 333 | |
| 334 | def _set_type(self, seek_type): |
| 335 | if not __can_access: |
| 336 | raise AttributeError("seek_type is not available") |
| 337 | self._pos.type = seek_type |
| 338 | |
| 339 | def _get_time(self): |
| 340 | if not __can_access: |
| 341 | raise AttributeError("seek_time is not available") |
| 342 | |
| 343 | elif self._pos.type is not SEEK_TIME: |
| 344 | raise TypeError("seek_type is not SEEK_TIME") |
| 345 | |
| 346 | return self._pos.u.seek_time |
| 347 | |
| 348 | def _set_time(self, time): |
| 349 | if not __can_access: |
| 350 | raise AttributeError("seek_time is not available") |
| 351 | |
| 352 | elif self._pos.type is not SEEK_TIME: |
| 353 | raise TypeError("seek_type is not SEEK_TIME") |
| 354 | |
| 355 | self._pos.u.seek_time = time |
| 356 | |
| 357 | def _get_pos(self): |
| 358 | return self._pos |
| 359 | |
| 360 | |
| 361 | seek_type = property(_get_type, _set_type) |
| 362 | seek_time = property(_get_time, _set_time) |
| 363 | |
| 364 | |
| 365 | class Iterator: |
| 366 | |
| 367 | __with_init = False |
| 368 | |
| 369 | def __init__(self, context, begin_pos = None, end_pos = None, _no_init = None): |
| 370 | """ |
| 371 | Allocate a trace collection iterator. |
| 372 | |
| 373 | begin_pos and end_pos are optional parameters to specify the |
| 374 | position at which the trace collection should be seeked upon |
| 375 | iterator creation, and the position at which iteration will |
| 376 | start returning "EOF". |
| 377 | |
| 378 | By default, if begin_pos is None, a BT_SEEK_CUR is performed at |
| 379 | creation. By default, if end_pos is None, a BT_SEEK_END (end of |
| 380 | trace) is the EOF criterion. |
| 381 | """ |
| 382 | if _no_init is None: |
| 383 | if begin_pos is None: |
| 384 | bp = None |
| 385 | else: |
| 386 | try: |
| 387 | bp = begin_pos._pos |
| 388 | except AttributeError: |
| 389 | raise TypeError("in __init__, " |
| 390 | "argument 3 must be a IterPos instance") |
| 391 | |
| 392 | if end_pos is None: |
| 393 | ep = None |
| 394 | else: |
| 395 | try: |
| 396 | ep = end_pos._pos |
| 397 | except AttributeError: |
| 398 | raise TypeError("in __init__, " |
| 399 | "argument 4 must be a IterPos instance") |
| 400 | |
| 401 | try: |
| 402 | self._bi = _bt_iter_create(context._c, bp, ep) |
| 403 | except AttributeError: |
| 404 | raise TypeError("in __init__, " |
| 405 | "argument 2 must be a Context instance") |
| 406 | |
| 407 | self.__with_init = True |
| 408 | |
| 409 | else: |
| 410 | self._bi = _no_init |
| 411 | |
| 412 | def __del__(self): |
| 413 | if self.__with_init: |
| 414 | _bt_iter_destroy(self._bi) |
| 415 | |
| 416 | def next(self): |
| 417 | """ |
| 418 | Move trace collection position to the next event. |
| 419 | Returns 0 on success, a negative value on error. |
| 420 | """ |
| 421 | return _bt_iter_next(self._bi) |
| 422 | |
| 423 | def get_pos(self): |
| 424 | """Return a IterPos class of the current iterator position.""" |
| 425 | ret = IterPos(0) |
| 426 | ret.__can_access = False |
| 427 | ret._pos = _bt_iter_get_pos(self._bi) |
| 428 | return ret |
| 429 | |
| 430 | def set_pos(self, pos): |
| 431 | """ |
| 432 | Move the iterator to a given position. |
| 433 | |
| 434 | On error, the stream_heap is reinitialized and returned empty. |
| 435 | Return 0 for success. |
| 436 | Return EOF if the position requested is after the last event of the |
| 437 | trace collection. |
| 438 | Return -EINVAL when called with invalid parameter. |
| 439 | Return -ENOMEM if the stream_heap could not be properly initialized. |
| 440 | """ |
| 441 | try: |
| 442 | return _bt_iter_set_pos(self._bi, pos._pos) |
| 443 | except AttributeError: |
| 444 | raise TypeError("in set_pos, " |
| 445 | "argument 2 must be a IterPos instance") |
| 446 | |
| 447 | def create_time_pos(self, timestamp): |
| 448 | """ |
| 449 | Create a position based on time |
| 450 | This function allocates and returns a new IterPos to be able to |
| 451 | restore an iterator position based on a timestamp. |
| 452 | """ |
| 453 | |
| 454 | if timestamp < 0: |
| 455 | raise TypeError("timestamp must be an unsigned int") |
| 456 | |
| 457 | ret = IterPos(0) |
| 458 | ret.__can_access = False |
| 459 | ret._pos = _bt_iter_create_time_pos(self._bi, timestamp) |
| 460 | return ret |
| 461 | %} |
| 462 | |
| 463 | |
| 464 | /* ================================================================= |
| 465 | CLOCK-TYPE.H |
| 466 | ¯¯¯¯¯¯¯¯¯¯¯¯ |
| 467 | *** Enum copied from clock-type.h |
| 468 | All changes must also be made here |
| 469 | */ |
| 470 | %rename("CLOCK_CYCLES") BT_CLOCK_CYCLES; |
| 471 | %rename("CLOCK_REAL") BT_CLOCK_REAL; |
| 472 | |
| 473 | enum bt_clock_type { |
| 474 | BT_CLOCK_CYCLES = 0, |
| 475 | BT_CLOCK_REAL |
| 476 | }; |
| 477 | |
| 478 | /* ================================================================= |
| 479 | TRACE-HANDLE.H, TRACE-HANDLE-INTERNAL.H |
| 480 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 481 | */ |
| 482 | |
| 483 | %rename("_bt_trace_handle_create") bt_trace_handle_create(struct bt_context *ctx); |
| 484 | %rename("_bt_trace_handle_destroy") bt_trace_handle_destroy(struct bt_trace_handle *bt); |
| 485 | struct bt_trace_handle *bt_trace_handle_create(struct bt_context *ctx); |
| 486 | void bt_trace_handle_destroy(struct bt_trace_handle *bt); |
| 487 | |
| 488 | %rename("_bt_trace_handle_get_path") bt_trace_handle_get_path(struct bt_context *ctx, |
| 489 | int handle_id); |
| 490 | %rename("_bt_trace_handle_get_timestamp_begin") bt_trace_handle_get_timestamp_begin( |
| 491 | struct bt_context *ctx, int handle_id, enum bt_clock_type type); |
| 492 | %rename("_bt_trace_handle_get_timestamp_end") bt_trace_handle_get_timestamp_end( |
| 493 | struct bt_context *ctx, int handle_id, enum bt_clock_type type); |
| 494 | const char *bt_trace_handle_get_path(struct bt_context *ctx, int handle_id); |
| 495 | uint64_t bt_trace_handle_get_timestamp_begin(struct bt_context *ctx, int handle_id, |
| 496 | enum bt_clock_type type); |
| 497 | uint64_t bt_trace_handle_get_timestamp_end(struct bt_context *ctx, int handle_id, |
| 498 | enum bt_clock_type type); |
| 499 | |
| 500 | %rename("_bt_ctf_event_get_handle_id") bt_ctf_event_get_handle_id( |
| 501 | const struct bt_ctf_event *event); |
| 502 | int bt_ctf_event_get_handle_id(const struct bt_ctf_event *event); |
| 503 | |
| 504 | |
| 505 | %pythoncode%{ |
| 506 | |
| 507 | class TraceHandle(object): |
| 508 | """ |
| 509 | The TraceHandle allows the user to manipulate a trace file directly. |
| 510 | It is a unique identifier representing a trace file. |
| 511 | Do not instantiate. |
| 512 | """ |
| 513 | |
| 514 | def __init__(self): |
| 515 | raise NotImplementedError("TraceHandle cannot be instantiated") |
| 516 | |
| 517 | def __repr__(self): |
| 518 | return "Babeltrace TraceHandle: trace_id('{0}')".format(self._id) |
| 519 | |
| 520 | def get_id(self): |
| 521 | """Return the TraceHandle id.""" |
| 522 | return self._id |
| 523 | |
| 524 | def get_path(self, context): |
| 525 | """Return the path of a TraceHandle.""" |
| 526 | try: |
| 527 | return _bt_trace_handle_get_path(context._c, self._id) |
| 528 | except AttributeError: |
| 529 | raise TypeError("in get_path, " |
| 530 | "argument 2 must be a Context instance") |
| 531 | |
| 532 | def get_timestamp_begin(self, context, clock_type): |
| 533 | """Return the creation time of the buffers of a trace.""" |
| 534 | try: |
| 535 | return _bt_trace_handle_get_timestamp_begin( |
| 536 | context._c, self._id,clock_type) |
| 537 | except AttributeError: |
| 538 | raise TypeError("in get_timestamp_begin, " |
| 539 | "argument 2 must be a Context instance") |
| 540 | |
| 541 | def get_timestamp_end(self, context, clock_type): |
| 542 | """Return the destruction timestamp of the buffers of a trace.""" |
| 543 | try: |
| 544 | return _bt_trace_handle_get_timestamp_end( |
| 545 | context._c, self._id, clock_type) |
| 546 | except AttributeError: |
| 547 | raise TypeError("in get_timestamp_end, " |
| 548 | "argument 2 must be a Context instance") |
| 549 | |
| 550 | %} |
| 551 | |
| 552 | |
| 553 | |
| 554 | // ================================================================= |
| 555 | // CTF |
| 556 | // ================================================================= |
| 557 | |
| 558 | /* ================================================================= |
| 559 | ITERATOR.H, EVENTS.H |
| 560 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 561 | */ |
| 562 | |
| 563 | //Iterator |
| 564 | %rename("_bt_ctf_iter_create") bt_ctf_iter_create(struct bt_context *ctx, |
| 565 | const struct bt_iter_pos *begin_pos, |
| 566 | const struct bt_iter_pos *end_pos); |
| 567 | %rename("_bt_ctf_get_iter") bt_ctf_get_iter(struct bt_ctf_iter *iter); |
| 568 | %rename("_bt_ctf_iter_destroy") bt_ctf_iter_destroy(struct bt_ctf_iter *iter); |
| 569 | %rename("_bt_ctf_iter_read_event") bt_ctf_iter_read_event(struct bt_ctf_iter *iter); |
| 570 | |
| 571 | struct bt_ctf_iter *bt_ctf_iter_create(struct bt_context *ctx, |
| 572 | const struct bt_iter_pos *begin_pos, |
| 573 | const struct bt_iter_pos *end_pos); |
| 574 | struct bt_iter *bt_ctf_get_iter(struct bt_ctf_iter *iter); |
| 575 | void bt_ctf_iter_destroy(struct bt_ctf_iter *iter); |
| 576 | struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter); |
| 577 | |
| 578 | |
| 579 | //Events |
| 580 | |
| 581 | %rename("_bt_ctf_get_top_level_scope") bt_ctf_get_top_level_scope(const struct |
| 582 | bt_ctf_event *event, enum bt_ctf_scope scope); |
| 583 | %rename("_bt_ctf_event_name") bt_ctf_event_name(const struct bt_ctf_event *ctf_event); |
| 584 | %rename("_bt_ctf_get_timestamp") bt_ctf_get_timestamp( |
| 585 | const struct bt_ctf_event *ctf_event); |
| 586 | %rename("_bt_ctf_get_cycles") bt_ctf_get_cycles( |
| 587 | const struct bt_ctf_event *ctf_event); |
| 588 | |
| 589 | %rename("_bt_ctf_get_field") bt_ctf_get_field(const struct bt_ctf_event *ctf_event, |
| 590 | const struct bt_definition *scope, const char *field); |
| 591 | %rename("_bt_ctf_get_index") bt_ctf_get_index(const struct bt_ctf_event *ctf_event, |
| 592 | const struct bt_definition *field, unsigned int index); |
| 593 | %rename("_bt_ctf_field_name") bt_ctf_field_name(const struct bt_definition *field); |
| 594 | %rename("_bt_ctf_field_type") bt_ctf_field_type(const struct bt_declaration *field); |
| 595 | %rename("_bt_ctf_get_int_signedness") bt_ctf_get_int_signedness( |
| 596 | const struct bt_declaration *field); |
| 597 | %rename("_bt_ctf_get_int_base") bt_ctf_get_int_base(const struct bt_declaration *field); |
| 598 | %rename("_bt_ctf_get_int_byte_order") bt_ctf_get_int_byte_order( |
| 599 | const struct bt_declaration *field); |
| 600 | %rename("_bt_ctf_get_int_len") bt_ctf_get_int_len(const struct bt_declaration *field); |
| 601 | %rename("_bt_ctf_get_enum_int") bt_ctf_get_enum_int(const struct bt_definition *field); |
| 602 | %rename("_bt_ctf_get_enum_str") bt_ctf_get_enum_str(const struct bt_definition *field); |
| 603 | %rename("_bt_ctf_get_encoding") bt_ctf_get_encoding(const struct bt_declaration *field); |
| 604 | %rename("_bt_ctf_get_array_len") bt_ctf_get_array_len(const struct bt_declaration *field); |
| 605 | %rename("_bt_ctf_get_uint64") bt_ctf_get_uint64(const struct bt_definition *field); |
| 606 | %rename("_bt_ctf_get_int64") bt_ctf_get_int64(const struct bt_definition *field); |
| 607 | %rename("_bt_ctf_get_char_array") bt_ctf_get_char_array(const struct bt_definition *field); |
| 608 | %rename("_bt_ctf_get_string") bt_ctf_get_string(const struct bt_definition *field); |
| 609 | %rename("_bt_ctf_get_float") bt_ctf_get_float(const struct bt_definition *field); |
| 610 | %rename("_bt_ctf_get_variant") bt_ctf_get_variant(const struct bt_definition *field); |
| 611 | %rename("_bt_ctf_field_get_error") bt_ctf_field_get_error(void); |
| 612 | %rename("_bt_ctf_get_decl_event_name") bt_ctf_get_decl_event_name(const struct |
| 613 | bt_ctf_event_decl *event); |
| 614 | %rename("_bt_ctf_get_decl_field_name") bt_ctf_get_decl_field_name( |
| 615 | const struct bt_ctf_field_decl *field); |
| 616 | %rename("_bt_ctf_get_decl_from_def") bt_ctf_get_decl_from_def( |
| 617 | const struct bt_definition *field); |
| 618 | %rename("_bt_array_index") bt_array_index(struct definition_array *array, uint64_t i); |
| 619 | %rename("_bt_sequence_len") bt_sequence_len(struct definition_sequence *sequence); |
| 620 | %rename("_bt_sequence_index") bt_sequence_index(struct definition_sequence *sequence, uint64_t i); |
| 621 | %rename("_bt_ctf_get_struct_field_count") bt_ctf_get_struct_field_count(const struct bt_definition *structure); |
| 622 | %rename("_bt_ctf_get_struct_field_index") bt_ctf_get_struct_field_index(const struct bt_definition *structure, uint64_t i); |
| 623 | |
| 624 | const struct bt_definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event, |
| 625 | enum bt_ctf_scope scope); |
| 626 | const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event); |
| 627 | uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event); |
| 628 | uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event); |
| 629 | const struct bt_definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event, |
| 630 | const struct bt_definition *scope, |
| 631 | const char *field); |
| 632 | const struct bt_definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event, |
| 633 | const struct bt_definition *field, |
| 634 | unsigned int index); |
| 635 | const char *bt_ctf_field_name(const struct bt_definition *field); |
| 636 | enum ctf_type_id bt_ctf_field_type(const struct bt_declaration *field); |
| 637 | int bt_ctf_get_int_signedness(const struct bt_declaration *field); |
| 638 | int bt_ctf_get_int_base(const struct bt_declaration *field); |
| 639 | int bt_ctf_get_int_byte_order(const struct bt_declaration *field); |
| 640 | ssize_t bt_ctf_get_int_len(const struct bt_declaration *field); |
| 641 | const struct bt_definition *bt_ctf_get_enum_int(const struct bt_definition *field); |
| 642 | const char *bt_ctf_get_enum_str(const struct bt_definition *field); |
| 643 | enum ctf_string_encoding bt_ctf_get_encoding(const struct bt_declaration *field); |
| 644 | int bt_ctf_get_array_len(const struct bt_declaration *field); |
| 645 | struct bt_definition *bt_array_index(struct definition_array *array, uint64_t i); |
| 646 | uint64_t bt_ctf_get_uint64(const struct bt_definition *field); |
| 647 | int64_t bt_ctf_get_int64(const struct bt_definition *field); |
| 648 | char *bt_ctf_get_char_array(const struct bt_definition *field); |
| 649 | char *bt_ctf_get_string(const struct bt_definition *field); |
| 650 | double bt_ctf_get_float(const struct bt_definition *field); |
| 651 | const struct bt_definition *bt_ctf_get_variant(const struct bt_definition *field); |
| 652 | int bt_ctf_field_get_error(void); |
| 653 | const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event); |
| 654 | const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field); |
| 655 | const struct bt_declaration *bt_ctf_get_decl_from_def(const struct bt_definition *field); |
| 656 | uint64_t bt_sequence_len(struct definition_sequence *sequence); |
| 657 | struct bt_definition *bt_sequence_index(struct definition_sequence *sequence, uint64_t i); |
| 658 | uint64_t bt_ctf_get_struct_field_count(const struct bt_definition *structure); |
| 659 | const struct bt_definition *bt_ctf_get_struct_field_index(const struct bt_definition *structure, uint64_t i); |
| 660 | |
| 661 | %pythoncode%{ |
| 662 | |
| 663 | class CTFStringEncoding: |
| 664 | NONE = 0 |
| 665 | UTF8 = 1 |
| 666 | ASCII = 2 |
| 667 | UNKNOWN = 3 |
| 668 | |
| 669 | #enum equivalent, accessible constants |
| 670 | #These are taken directly from ctf/events.h |
| 671 | #All changes to enums must also be made here |
| 672 | class CTFTypeId: |
| 673 | UNKNOWN = 0 |
| 674 | INTEGER = 1 |
| 675 | FLOAT = 2 |
| 676 | ENUM = 3 |
| 677 | STRING = 4 |
| 678 | STRUCT = 5 |
| 679 | UNTAGGED_VARIANT = 6 |
| 680 | VARIANT = 7 |
| 681 | ARRAY = 8 |
| 682 | SEQUENCE = 9 |
| 683 | NR_CTF_TYPES = 10 |
| 684 | |
| 685 | def get_type_name(id): |
| 686 | name = "UNKNOWN" |
| 687 | constants = [attr for attr in dir(CTFTypeId) if not callable(getattr(CTFTypeId, attr)) and not attr.startswith("__")] |
| 688 | for attr in constants: |
| 689 | if getattr(CTFTypeId, attr) == id: |
| 690 | name = attr |
| 691 | break |
| 692 | return name |
| 693 | |
| 694 | class CTFReader: |
| 695 | class scope: |
| 696 | TRACE_PACKET_HEADER = 0 |
| 697 | STREAM_PACKET_CONTEXT = 1 |
| 698 | STREAM_EVENT_HEADER = 2 |
| 699 | STREAM_EVENT_CONTEXT = 3 |
| 700 | EVENT_CONTEXT = 4 |
| 701 | EVENT_FIELDS = 5 |
| 702 | |
| 703 | class Iterator(Iterator, object): |
| 704 | """ |
| 705 | Allocate a CTF trace collection iterator. |
| 706 | |
| 707 | begin_pos and end_pos are optional parameters to specify the |
| 708 | position at which the trace collection should be seeked upon |
| 709 | iterator creation, and the position at which iteration will |
| 710 | start returning "EOF". |
| 711 | |
| 712 | By default, if begin_pos is None, a SEEK_CUR is performed at |
| 713 | creation. By default, if end_pos is None, a SEEK_END (end of |
| 714 | trace) is the EOF criterion. |
| 715 | |
| 716 | Only one iterator can be created against a context. If more than one |
| 717 | iterator is being created for the same context, the second creation |
| 718 | will return None. The previous iterator must be destroyed before |
| 719 | creation of the new iterator for this function to succeed. |
| 720 | """ |
| 721 | |
| 722 | def __new__(cls, context, begin_pos = None, end_pos = None): |
| 723 | # __new__ is used to control the return value |
| 724 | # as the CTFReader.Iterator class should return None |
| 725 | # if bt_ctf_iter_create returns NULL |
| 726 | |
| 727 | if begin_pos is None: |
| 728 | bp = None |
| 729 | else: |
| 730 | bp = begin_pos._pos |
| 731 | if end_pos is None: |
| 732 | ep = None |
| 733 | else: |
| 734 | ep = end_pos._pos |
| 735 | try: |
| 736 | it = _bt_ctf_iter_create(context._c, bp, ep) |
| 737 | except AttributeError: |
| 738 | raise TypeError("in __init__, " |
| 739 | "argument 2 must be a Context instance") |
| 740 | if it is None: |
| 741 | return None |
| 742 | |
| 743 | ret_class = super(CTFReader.Iterator, cls).__new__(cls) |
| 744 | ret_class._i = it |
| 745 | return ret_class |
| 746 | |
| 747 | def __init__(self, context, begin_pos = None, end_pos = None): |
| 748 | Iterator.__init__(self, None, None, None, |
| 749 | _bt_ctf_get_iter(self._i)) |
| 750 | |
| 751 | def __del__(self): |
| 752 | _bt_ctf_iter_destroy(self._i) |
| 753 | |
| 754 | def read_event(self): |
| 755 | """ |
| 756 | Read the iterator's current event data. |
| 757 | Return current event on success, None on end of trace. |
| 758 | """ |
| 759 | ret = _bt_ctf_iter_read_event(self._i) |
| 760 | if ret is None: |
| 761 | return ret |
| 762 | ev = CTFReader.Event.__new__(CTFReader.Event) |
| 763 | ev._e = ret |
| 764 | return ev |
| 765 | |
| 766 | |
| 767 | class Event(object): |
| 768 | """ |
| 769 | This class represents an event from the trace. |
| 770 | It is obtained with read_event() from CTFReader.Iterator. |
| 771 | Do not instantiate. |
| 772 | """ |
| 773 | |
| 774 | def __init__(self): |
| 775 | raise NotImplementedError("CTFReader.Event cannot be instantiated") |
| 776 | |
| 777 | def get_top_level_scope(self, scope): |
| 778 | """ |
| 779 | Return a definition of the top-level scope |
| 780 | Top-level scopes are defined in CTFReader.scope. |
| 781 | In order to get a field or a field list, the user needs to pass a |
| 782 | scope as argument, this scope can be a top-level scope or a scope |
| 783 | relative to an arbitrary field. This function provides the mapping |
| 784 | between the scope and the actual definition of top-level scopes. |
| 785 | On error return None. |
| 786 | """ |
| 787 | evDef = CTFReader.Definition.__new__(CTFReader.Definition) |
| 788 | evDef._d = _bt_ctf_get_top_level_scope(self._e, scope) |
| 789 | if evDef._d is None: |
| 790 | return None |
| 791 | return evDef |
| 792 | |
| 793 | def get_name(self): |
| 794 | """Return the name of the event or None on error.""" |
| 795 | return _bt_ctf_event_name(self._e) |
| 796 | |
| 797 | def get_cycles(self): |
| 798 | """ |
| 799 | Return the timestamp of the event as written in |
| 800 | the packet (in cycles) or -1ULL on error. |
| 801 | """ |
| 802 | return _bt_ctf_get_cycles(self._e) |
| 803 | |
| 804 | def get_timestamp(self): |
| 805 | """ |
| 806 | Return the timestamp of the event offsetted with the |
| 807 | system clock source or -1ULL on error. |
| 808 | """ |
| 809 | return _bt_ctf_get_timestamp(self._e) |
| 810 | |
| 811 | def get_field_with_scope(self, scope, field): |
| 812 | """ |
| 813 | Return the definition of a specific field. |
| 814 | Return None on error. |
| 815 | """ |
| 816 | evDef = CTFReader.Definition.__new__(CTFReader.Definition) |
| 817 | try: |
| 818 | evDef._d = _bt_ctf_get_field(self._e, scope._d, field) |
| 819 | except AttributeError: |
| 820 | raise TypeError("in get_field, argument 2 must be a " |
| 821 | "Definition (scope) instance") |
| 822 | if evDef._d is None: |
| 823 | return None |
| 824 | evDef._s = scope |
| 825 | return evDef |
| 826 | |
| 827 | def get_field(self, field): |
| 828 | """ |
| 829 | Return the definition of fields by a name |
| 830 | Return None on error |
| 831 | """ |
| 832 | eventScope = self.get_top_level_scope(CTFReader.scope.EVENT_FIELDS) |
| 833 | streamScope = self.get_top_level_scope(CTFReader.scope.STREAM_EVENT_CONTEXT) |
| 834 | fields_by_name = [] |
| 835 | |
| 836 | if eventScope is not None: |
| 837 | evDef = self.get_field_with_scope(eventScope, field) |
| 838 | if evDef is not None: |
| 839 | fields_by_name.append(evDef) |
| 840 | |
| 841 | if streamScope is not None: |
| 842 | evDef = self.get_field_with_scope(streamScope, field) |
| 843 | if evDef is not None: |
| 844 | fields_by_name.append(evDef); |
| 845 | |
| 846 | if not fields_by_name: |
| 847 | return None |
| 848 | return fields_by_name |
| 849 | |
| 850 | def get_field_list_with_scope(self, scope): |
| 851 | """ |
| 852 | Return a list of Definitions associated with the scope |
| 853 | Return None on error. |
| 854 | """ |
| 855 | try: |
| 856 | field_lc, count = _bt_python_field_listcaller(self._e, scope._d) |
| 857 | except AttributeError: |
| 858 | raise TypeError("in get_field_list, argument 2 must be a " |
| 859 | "Definition (scope) instance") |
| 860 | |
| 861 | if field_lc is None: |
| 862 | return None |
| 863 | |
| 864 | def_list = [] |
| 865 | for i in range(count): |
| 866 | tmp = CTFReader.Definition.__new__(CTFReader.Definition) |
| 867 | tmp._d = _bt_python_field_one_from_list(field_lc, i) |
| 868 | tmp._s = scope |
| 869 | def_list.append(tmp) |
| 870 | |
| 871 | return def_list |
| 872 | |
| 873 | def get_field_list(self): |
| 874 | """Return a list of Definitions or None on error.""" |
| 875 | eventScope = self.get_top_level_scope(CTFReader.scope.EVENT_FIELDS) |
| 876 | streamScope = self.get_top_level_scope(CTFReader.scope.STREAM_EVENT_CONTEXT) |
| 877 | |
| 878 | def_list = [] |
| 879 | if eventScope is not None: |
| 880 | event_field_list = self.get_field_list_with_scope(eventScope) |
| 881 | if event_field_list is not None: |
| 882 | def_list = event_field_list |
| 883 | |
| 884 | if streamScope is not None: |
| 885 | event_field_list = self.get_field_list_with_scope(streamScope) |
| 886 | if event_field_list is not None: |
| 887 | def_list.extend(event_field_list) |
| 888 | |
| 889 | if not def_list: |
| 890 | return None |
| 891 | return def_list |
| 892 | |
| 893 | def get_index(self, field, index): |
| 894 | """ |
| 895 | If the field is an array or a sequence, return the element |
| 896 | at position index, otherwise return None |
| 897 | """ |
| 898 | evDef = CTFReader.Definition.__new__(CTFReader.Definition) |
| 899 | try: |
| 900 | evDef._d = _bt_ctf_get_index(self._e, field._d, index) |
| 901 | except AttributeError: |
| 902 | raise TypeError("in get_index, argument 2 must be a " |
| 903 | "Definition (field) instance") |
| 904 | |
| 905 | if evDef._d is None: |
| 906 | return None |
| 907 | return evDef |
| 908 | |
| 909 | def get_handle(self): |
| 910 | """ |
| 911 | Get the TraceHandle associated with an event |
| 912 | Return None on error |
| 913 | """ |
| 914 | ret = _bt_ctf_event_get_handle_id(self._e) |
| 915 | if ret < 0: |
| 916 | return None |
| 917 | |
| 918 | th = TraceHandle.__new__(TraceHandle) |
| 919 | th._id = ret |
| 920 | return th |
| 921 | |
| 922 | def get_context(self): |
| 923 | """ |
| 924 | Get the context associated with an event. |
| 925 | Return None on error. |
| 926 | """ |
| 927 | ctx = Context() |
| 928 | ctx._c = _bt_ctf_event_get_context(self._e); |
| 929 | if ctx._c is None: |
| 930 | return None |
| 931 | else: |
| 932 | return ctx |
| 933 | |
| 934 | class FieldError(Exception): |
| 935 | def __init__(self, value): |
| 936 | self.value = value |
| 937 | |
| 938 | def __str__(self): |
| 939 | return repr(self.value) |
| 940 | |
| 941 | class Definition(object): |
| 942 | """Definition class. Do not instantiate.""" |
| 943 | |
| 944 | def __init__(self): |
| 945 | raise NotImplementedError("CTFReader.Definition cannot be instantiated") |
| 946 | |
| 947 | def __repr__(self): |
| 948 | return "Babeltrace Definition: name('{0}'), type({1})".format( |
| 949 | self.field_name(), self.field_type()) |
| 950 | |
| 951 | def field_name(self): |
| 952 | """Return the name of a field or None on error.""" |
| 953 | return _bt_ctf_field_name(self._d) |
| 954 | |
| 955 | def field_type(self): |
| 956 | """Return the type of a field or -1 if unknown.""" |
| 957 | return _bt_ctf_field_type(_bt_ctf_get_decl_from_def(self._d)) |
| 958 | |
| 959 | def get_int_signedness(self): |
| 960 | """ |
| 961 | Return the signedness of an integer: |
| 962 | 0 if unsigned; 1 if signed; -1 on error. |
| 963 | """ |
| 964 | return _bt_ctf_get_int_signedness(_bt_ctf_get_decl_from_def(self._d)) |
| 965 | |
| 966 | def get_int_base(self): |
| 967 | """Return the base of an int or a negative value on error.""" |
| 968 | return _bt_ctf_get_int_base(_bt_ctf_get_decl_from_def(self._d)) |
| 969 | |
| 970 | def get_int_byte_order(self): |
| 971 | """ |
| 972 | Return the byte order of an int or a negative |
| 973 | value on error. |
| 974 | """ |
| 975 | return _bt_ctf_get_int_byte_order(_bt_ctf_get_decl_from_def(self._d)) |
| 976 | |
| 977 | def get_int_len(self): |
| 978 | """ |
| 979 | Return the size, in bits, of an int or a negative |
| 980 | value on error. |
| 981 | """ |
| 982 | return _bt_ctf_get_int_len(_bt_ctf_get_decl_from_def(self._d)) |
| 983 | |
| 984 | def get_enum_str(self): |
| 985 | """ |
| 986 | Return the string matching the current enumeration. |
| 987 | Return None on error. |
| 988 | """ |
| 989 | return _bt_ctf_get_enum_str(self._d) |
| 990 | |
| 991 | def get_encoding(self): |
| 992 | """ |
| 993 | Return the encoding of an int or a string. |
| 994 | Return a negative value on error. |
| 995 | """ |
| 996 | return _bt_ctf_get_encoding(_bt_ctf_get_decl_from_def(self._d)) |
| 997 | |
| 998 | def get_array_len(self): |
| 999 | """ |
| 1000 | Return the len of an array or a negative |
| 1001 | value on error. |
| 1002 | """ |
| 1003 | return _bt_ctf_get_array_len(_bt_ctf_get_decl_from_def(self._d)) |
| 1004 | |
| 1005 | def get_array_element_at(self, index): |
| 1006 | """ |
| 1007 | Return the array's element at position index. |
| 1008 | Return None on error |
| 1009 | """ |
| 1010 | array = _bt_python_get_array_from_def(self._d) |
| 1011 | if array is None: |
| 1012 | return None |
| 1013 | |
| 1014 | element = CTFReader.Definition.__new__(CTFReader.Definition) |
| 1015 | element._d = _bt_array_index(array, index) |
| 1016 | if element._d is None: |
| 1017 | return None |
| 1018 | return element |
| 1019 | |
| 1020 | def get_sequence_len(self): |
| 1021 | """ |
| 1022 | Return the len of a sequence or a negative |
| 1023 | value on error. |
| 1024 | """ |
| 1025 | seq = _bt_python_get_sequence_from_def(self._d) |
| 1026 | return _bt_sequence_len(seq) |
| 1027 | |
| 1028 | def get_sequence_element_at(self, index): |
| 1029 | """ |
| 1030 | Return the sequence's element at position index, |
| 1031 | otherwise return None |
| 1032 | """ |
| 1033 | seq = _bt_python_get_sequence_from_def(self._d) |
| 1034 | if seq is not None: |
| 1035 | element = CTFReader.Definition.__new__(CTFReader.Definition) |
| 1036 | element._d = _bt_sequence_index(seq, index) |
| 1037 | if element._d is not None: |
| 1038 | return element |
| 1039 | return None |
| 1040 | |
| 1041 | def get_uint64(self): |
| 1042 | """ |
| 1043 | Return the value associated with the field. |
| 1044 | If the field does not exist or is not of the type requested, |
| 1045 | the value returned is undefined. To check if an error occured, |
| 1046 | use the CTFReader.field_error() function after accessing a field. |
| 1047 | """ |
| 1048 | return _bt_ctf_get_uint64(self._d) |
| 1049 | |
| 1050 | def get_int64(self): |
| 1051 | """ |
| 1052 | Return the value associated with the field. |
| 1053 | If the field does not exist or is not of the type requested, |
| 1054 | the value returned is undefined. To check if an error occured, |
| 1055 | use the CTFReader.field_error() function after accessing a field. |
| 1056 | """ |
| 1057 | return _bt_ctf_get_int64(self._d) |
| 1058 | |
| 1059 | def get_char_array(self): |
| 1060 | """ |
| 1061 | Return the value associated with the field. |
| 1062 | If the field does not exist or is not of the type requested, |
| 1063 | the value returned is undefined. To check if an error occured, |
| 1064 | use the CTFReader.field_error() function after accessing a field. |
| 1065 | """ |
| 1066 | return _bt_ctf_get_char_array(self._d) |
| 1067 | |
| 1068 | def get_str(self): |
| 1069 | """ |
| 1070 | Return the value associated with the field. |
| 1071 | If the field does not exist or is not of the type requested, |
| 1072 | the value returned is undefined. To check if an error occured, |
| 1073 | use the CTFReader.field_error() function after accessing a field. |
| 1074 | """ |
| 1075 | return _bt_ctf_get_string(self._d) |
| 1076 | |
| 1077 | def get_float(self): |
| 1078 | """ |
| 1079 | Return the value associated with the field. |
| 1080 | If the field does not exist or is not of the type requested, |
| 1081 | the value returned is undefined. To check if an error occured, |
| 1082 | use the CTFReader.field_error() function after accessing a field. |
| 1083 | """ |
| 1084 | return _bt_ctf_get_float(self._d) |
| 1085 | |
| 1086 | def get_variant(self): |
| 1087 | """ |
| 1088 | Return the variant's selected field. |
| 1089 | If the field does not exist or is not of the type requested, |
| 1090 | the value returned is undefined. To check if an error occured, |
| 1091 | use the CTFReader.field_error() function after accessing a field. |
| 1092 | """ |
| 1093 | return _bt_ctf_get_variant(self._d) |
| 1094 | |
| 1095 | def get_struct_field_count(self): |
| 1096 | """ |
| 1097 | Return the number of fields contained in the structure. |
| 1098 | If the field does not exist or is not of the type requested, |
| 1099 | the value returned is undefined. |
| 1100 | """ |
| 1101 | return _bt_ctf_get_struct_field_count(self._d) |
| 1102 | |
| 1103 | def get_struct_field_at(self, i): |
| 1104 | """ |
| 1105 | Return the structure's field at position i. |
| 1106 | If the field does not exist or is not of the type requested, |
| 1107 | the value returned is undefined. To check if an error occured, |
| 1108 | use the CTFReader.field_error() function after accessing a field. |
| 1109 | """ |
| 1110 | return _bt_ctf_get_struct_field_index(self._d, i) |
| 1111 | |
| 1112 | def get_value(self): |
| 1113 | """ |
| 1114 | Return the value associated with the field according to its type. |
| 1115 | Return None on error. |
| 1116 | """ |
| 1117 | id = self.field_type() |
| 1118 | value = None |
| 1119 | if id == CTFTypeId.STRING: |
| 1120 | value = self.get_str() |
| 1121 | elif id == CTFTypeId.ARRAY: |
| 1122 | value = [] |
| 1123 | for i in range(self.get_array_len()): |
| 1124 | element = self.get_array_element_at(i) |
| 1125 | value.append(element.get_value()) |
| 1126 | elif id == CTFTypeId.INTEGER: |
| 1127 | if self.get_int_signedness() == 0: |
| 1128 | value = self.get_uint64() |
| 1129 | else: |
| 1130 | value = self.get_int64() |
| 1131 | elif id == CTFTypeId.ENUM: |
| 1132 | value = self.get_enum_str() |
| 1133 | elif id == CTFTypeId.SEQUENCE: |
| 1134 | seq_len = self.get_sequence_len() |
| 1135 | value = [] |
| 1136 | for i in range(seq_len): |
| 1137 | evDef = self.get_sequence_element_at(i) |
| 1138 | value.append(evDef.get_value()) |
| 1139 | elif id == CTFTypeId.FLOAT: |
| 1140 | value = self.get_float() |
| 1141 | elif id == CTFTypeId.VARIANT: |
| 1142 | variant = CTFReader.Definition.__new__(CTFReader.Definition) |
| 1143 | variant._d = self.get_variant(); |
| 1144 | value = variant.get_value() |
| 1145 | elif id == CTFTypeId.STRUCT: |
| 1146 | value = {} |
| 1147 | for i in range(self.get_struct_field_count()): |
| 1148 | member = CTFReader.Definition.__new__(CTFReader.Definition) |
| 1149 | member._d = self.get_struct_field_at(i); |
| 1150 | value[member.field_name()] = member.get_value() |
| 1151 | |
| 1152 | if CTFReader.field_error(): |
| 1153 | raise CTFReader.FieldError("Error occured while accessing field {} of type {}".format(self.field_name(), CTFTypeId.get_type_name(self.field_type()))) |
| 1154 | return value |
| 1155 | |
| 1156 | def get_scope(self): |
| 1157 | """Return the scope of a field or None on error.""" |
| 1158 | return self._s |
| 1159 | |
| 1160 | class EventDecl(object): |
| 1161 | """Event declaration class. Do not instantiate.""" |
| 1162 | |
| 1163 | def __init__(self): |
| 1164 | raise NotImplementedError("CTFReader.EventDecl cannot be instantiated") |
| 1165 | |
| 1166 | def __repr__(self): |
| 1167 | return "Babeltrace EventDecl: name {0}".format(self.get_name()) |
| 1168 | |
| 1169 | def get_name(self): |
| 1170 | """Return the name of the event or None on error""" |
| 1171 | return _bt_ctf_get_decl_event_name(self._d) |
| 1172 | |
| 1173 | def get_decl_fields(self, scope): |
| 1174 | """ |
| 1175 | Return a list of CTFReader.FieldDecl |
| 1176 | Return None on error. |
| 1177 | """ |
| 1178 | ptr_list = _by_python_field_decl_listcaller(self._d, scope) |
| 1179 | |
| 1180 | if ptr_list is None: |
| 1181 | return None |
| 1182 | |
| 1183 | decl_list = [] |
| 1184 | i = 0 |
| 1185 | while True: |
| 1186 | tmp = CTFReader.FieldDecl.__new__(CTFReader.FieldDecl) |
| 1187 | tmp._d = _bt_python_field_decl_one_from_list( |
| 1188 | ptr_list, i) |
| 1189 | |
| 1190 | if tmp._d is None: |
| 1191 | #Last item of list is None |
| 1192 | break |
| 1193 | |
| 1194 | decl_list.append(tmp) |
| 1195 | i += 1 |
| 1196 | return decl_list |
| 1197 | |
| 1198 | |
| 1199 | class FieldDecl(object): |
| 1200 | """Field declaration class. Do not instantiate.""" |
| 1201 | |
| 1202 | def __init__(self): |
| 1203 | raise NotImplementedError("CTFReader.FieldDecl cannot be instantiated") |
| 1204 | |
| 1205 | def __repr__(self): |
| 1206 | return "Babeltrace FieldDecl: name {0}".format(self.get_name()) |
| 1207 | |
| 1208 | def get_name(self): |
| 1209 | """Return the name of a FieldDecl or None on error""" |
| 1210 | return _bt_ctf_get_decl_field_name(self._d) |
| 1211 | |
| 1212 | |
| 1213 | @staticmethod |
| 1214 | def field_error(): |
| 1215 | """ |
| 1216 | Return the last error code encountered while |
| 1217 | accessing a field and reset the error flag. |
| 1218 | Return 0 if no error, a negative value otherwise. |
| 1219 | """ |
| 1220 | return _bt_ctf_field_get_error() |
| 1221 | |
| 1222 | @staticmethod |
| 1223 | def get_event_decl_list(trace_handle, context): |
| 1224 | """ |
| 1225 | Return a list of CTFReader.EventDecl |
| 1226 | Return None on error. |
| 1227 | """ |
| 1228 | try: |
| 1229 | handle_id = trace_handle._id |
| 1230 | except AttributeError: |
| 1231 | raise TypeError("in get_event_decl_list, " |
| 1232 | "argument 1 must be a TraceHandle instance") |
| 1233 | try: |
| 1234 | ptr_list, count = _bt_python_event_decl_listcaller(handle_id, context._c) |
| 1235 | except AttributeError: |
| 1236 | raise TypeError("in get_event_decl_list, " |
| 1237 | "argument 2 must be a Context instance") |
| 1238 | |
| 1239 | if ptr_list is None: |
| 1240 | return None |
| 1241 | |
| 1242 | decl_list = [] |
| 1243 | for i in range(count): |
| 1244 | tmp = CTFReader.EventDecl.__new__(CTFReader.EventDecl) |
| 1245 | tmp._d = _bt_python_decl_one_from_list(ptr_list, i) |
| 1246 | decl_list.append(tmp) |
| 1247 | |
| 1248 | return decl_list |
| 1249 | |
| 1250 | %} |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | // ================================================================= |
| 1255 | // NEW FUNCTIONS |
| 1256 | // File and list-related |
| 1257 | // python-complements.h |
| 1258 | // ================================================================= |
| 1259 | |
| 1260 | %pythoncode %{ |
| 1261 | |
| 1262 | class File(object): |
| 1263 | """ |
| 1264 | Open a file for babeltrace. |
| 1265 | |
| 1266 | file_path is a string containing the path or None to use the |
| 1267 | standard output in writing mode. |
| 1268 | |
| 1269 | The mode can be 'r', 'w' or 'a' for reading (default), writing or |
| 1270 | appending. The file will be created if it doesn't exist when |
| 1271 | opened for writing or appending; it will be truncated when opened |
| 1272 | for writing. Add a 'b' to the mode for binary files. Add a '+' |
| 1273 | to the mode to allow simultaneous reading and writing. |
| 1274 | """ |
| 1275 | |
| 1276 | def __new__(cls, file_path, mode='r'): |
| 1277 | # __new__ is used to control the return value |
| 1278 | # as the File class should return None |
| 1279 | # if _bt_file_open returns NULL |
| 1280 | |
| 1281 | # Type check |
| 1282 | if file_path is not None and type(file_path) is not str: |
| 1283 | raise TypeError("in method __init__, argument 2 of type 'str'") |
| 1284 | if type(mode) is not str: |
| 1285 | raise TypeError("in method __init__, argument 3 of type 'str'") |
| 1286 | |
| 1287 | # Opening file |
| 1288 | file_ptr = _bt_file_open(file_path, mode) |
| 1289 | if file_ptr is None: |
| 1290 | return None |
| 1291 | |
| 1292 | # Class instantiation |
| 1293 | file_inst = super(File, cls).__new__(cls) |
| 1294 | file_inst._file = file_ptr |
| 1295 | return file_inst |
| 1296 | |
| 1297 | def __init__(self, file_path, mode='r'): |
| 1298 | self._opened = True |
| 1299 | self._use_stdout = False |
| 1300 | |
| 1301 | if file_path is None: |
| 1302 | # use stdout |
| 1303 | file_path = "stdout" |
| 1304 | mode = 'w' |
| 1305 | self._use_stdout = True |
| 1306 | |
| 1307 | self._file_path = file_path |
| 1308 | self._mode = mode |
| 1309 | |
| 1310 | def __del__(self): |
| 1311 | self.close() |
| 1312 | |
| 1313 | def __repr__(self): |
| 1314 | if self._opened: |
| 1315 | stat = 'opened' |
| 1316 | else: |
| 1317 | stat = 'closed' |
| 1318 | return "{0} babeltrace File; file_path('{1}'), mode('{2}')".format( |
| 1319 | stat, self._file_path, self._mode) |
| 1320 | |
| 1321 | def close(self): |
| 1322 | """Close the file. Is also called using del.""" |
| 1323 | if self._opened and not self._use_stdout: |
| 1324 | _bt_file_close(self._file) |
| 1325 | self._opened = False |
| 1326 | %} |
| 1327 | |
| 1328 | // ================================================================= |
| 1329 | // CTF Writer |
| 1330 | // ================================================================= |
| 1331 | |
| 1332 | /* ================================================================= |
| 1333 | CLOCK.H |
| 1334 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 1335 | */ |
| 1336 | %rename("_bt_ctf_clock_create") bt_ctf_clock_create(const char *name); |
| 1337 | %rename("_bt_ctf_clock_set_description") bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc); |
| 1338 | %rename("_bt_ctf_clock_set_frequency") bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq); |
| 1339 | %rename("_bt_ctf_clock_set_precision") bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision); |
| 1340 | %rename("_bt_ctf_clock_set_offset_s") bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s); |
| 1341 | %rename("_bt_ctf_clock_set_offset") bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset); |
| 1342 | %rename("_bt_ctf_clock_set_is_absolute") bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute); |
| 1343 | %rename("_bt_ctf_clock_set_time") bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time); |
| 1344 | %rename("_bt_ctf_clock_get") bt_ctf_clock_get(struct bt_ctf_clock *clock); |
| 1345 | %rename("_bt_ctf_clock_put") bt_ctf_clock_put(struct bt_ctf_clock *clock); |
| 1346 | |
| 1347 | struct bt_ctf_clock *bt_ctf_clock_create(const char *name); |
| 1348 | int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc); |
| 1349 | int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq); |
| 1350 | int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision); |
| 1351 | int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s); |
| 1352 | int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset); |
| 1353 | int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute); |
| 1354 | int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time); |
| 1355 | void bt_ctf_clock_get(struct bt_ctf_clock *clock); |
| 1356 | void bt_ctf_clock_put(struct bt_ctf_clock *clock); |
| 1357 | |
| 1358 | /* ================================================================= |
| 1359 | EVENT-TYPES.H |
| 1360 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 1361 | */ |
| 1362 | %rename("_bt_ctf_field_type_integer_create") bt_ctf_field_type_integer_create(unsigned int size); |
| 1363 | %rename("_bt_ctf_field_type_integer_set_signed") bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *integer, int is_signed); |
| 1364 | %rename("_bt_ctf_field_type_integer_set_base") bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *integer, enum bt_ctf_integer_base base); |
| 1365 | %rename("_bt_ctf_field_type_integer_set_encoding") bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *integer, enum ctf_string_encoding encoding); |
| 1366 | %rename("_bt_ctf_field_type_enumeration_create") bt_ctf_field_type_enumeration_create(struct bt_ctf_field_type *integer_container_type); |
| 1367 | %rename("_bt_ctf_field_type_enumeration_add_mapping") bt_ctf_field_type_enumeration_add_mapping(struct bt_ctf_field_type *enumeration, const char *string, int64_t range_start, int64_t range_end); |
| 1368 | %rename("_bt_ctf_field_type_floating_point_create") bt_ctf_field_type_floating_point_create(void); |
| 1369 | %rename("_bt_ctf_field_type_floating_point_set_exponent_digits") bt_ctf_field_type_floating_point_set_exponent_digits(struct bt_ctf_field_type *floating_point, unsigned int exponent_digits); |
| 1370 | %rename("_bt_ctf_field_type_floating_point_set_mantissa_digits") bt_ctf_field_type_floating_point_set_mantissa_digits(struct bt_ctf_field_type *floating_point, unsigned int mantissa_digits); |
| 1371 | %rename("_bt_ctf_field_type_structure_create") bt_ctf_field_type_structure_create(void); |
| 1372 | %rename("_bt_ctf_field_type_structure_add_field") bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *structure, struct bt_ctf_field_type *field_type, const char *field_name); |
| 1373 | %rename("_bt_ctf_field_type_variant_create") bt_ctf_field_type_variant_create(struct bt_ctf_field_type *enum_tag, const char *tag_name); |
| 1374 | %rename("_bt_ctf_field_type_variant_add_field") bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *variant, struct bt_ctf_field_type *field_type, const char *field_name); |
| 1375 | %rename("_bt_ctf_field_type_array_create") bt_ctf_field_type_array_create(struct bt_ctf_field_type *element_type, unsigned int length); |
| 1376 | %rename("_bt_ctf_field_type_sequence_create") bt_ctf_field_type_sequence_create(struct bt_ctf_field_type *element_type, const char *length_field_name); |
| 1377 | %rename("_bt_ctf_field_type_string_create") bt_ctf_field_type_string_create(void); |
| 1378 | %rename("_bt_ctf_field_type_string_set_encoding") bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *string, enum ctf_string_encoding encoding); |
| 1379 | %rename("_bt_ctf_field_type_set_alignment") bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, unsigned int alignment); |
| 1380 | %rename("_bt_ctf_field_type_set_byte_order") bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, enum bt_ctf_byte_order byte_order); |
| 1381 | %rename("_bt_ctf_field_type_get") bt_ctf_field_type_get(struct bt_ctf_field_type *type); |
| 1382 | %rename("_bt_ctf_field_type_put") bt_ctf_field_type_put(struct bt_ctf_field_type *type); |
| 1383 | |
| 1384 | struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size); |
| 1385 | int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *integer, int is_signed); |
| 1386 | int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *integer, enum bt_ctf_integer_base base); |
| 1387 | int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *integer, enum ctf_string_encoding encoding); |
| 1388 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create(struct bt_ctf_field_type *integer_container_type); |
| 1389 | int bt_ctf_field_type_enumeration_add_mapping(struct bt_ctf_field_type *enumeration, const char *string, int64_t range_start, int64_t range_end); |
| 1390 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void); |
| 1391 | int bt_ctf_field_type_floating_point_set_exponent_digits(struct bt_ctf_field_type *floating_point, unsigned int exponent_digits); |
| 1392 | int bt_ctf_field_type_floating_point_set_mantissa_digits(struct bt_ctf_field_type *floating_point, unsigned int mantissa_digits); |
| 1393 | struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void); |
| 1394 | int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *structure, struct bt_ctf_field_type *field_type, const char *field_name); |
| 1395 | struct bt_ctf_field_type *bt_ctf_field_type_variant_create(struct bt_ctf_field_type *enum_tag, const char *tag_name); |
| 1396 | int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *variant, struct bt_ctf_field_type *field_type, const char *field_name); |
| 1397 | struct bt_ctf_field_type *bt_ctf_field_type_array_create(struct bt_ctf_field_type *element_type, unsigned int length); |
| 1398 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_create(struct bt_ctf_field_type *element_type, const char *length_field_name); |
| 1399 | struct bt_ctf_field_type *bt_ctf_field_type_string_create(void); |
| 1400 | int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *string, enum ctf_string_encoding encoding); |
| 1401 | int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, unsigned int alignment); |
| 1402 | int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, enum bt_ctf_byte_order byte_order); |
| 1403 | void bt_ctf_field_type_get(struct bt_ctf_field_type *type); |
| 1404 | void bt_ctf_field_type_put(struct bt_ctf_field_type *type); |
| 1405 | |
| 1406 | /* ================================================================= |
| 1407 | EVENT-FIELDS.H |
| 1408 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 1409 | */ |
| 1410 | %rename("_bt_ctf_field_create") bt_ctf_field_create(struct bt_ctf_field_type *type); |
| 1411 | %rename("_bt_ctf_field_structure_get_field") bt_ctf_field_structure_get_field(struct bt_ctf_field *structure, const char *name); |
| 1412 | %rename("_bt_ctf_field_array_get_field") bt_ctf_field_array_get_field(struct bt_ctf_field *array, uint64_t index); |
| 1413 | %rename("_bt_ctf_field_sequence_set_length") bt_ctf_field_sequence_set_length(struct bt_ctf_field *sequence, struct bt_ctf_field *length_field); |
| 1414 | %rename("_bt_ctf_field_sequence_get_field") bt_ctf_field_sequence_get_field(struct bt_ctf_field *sequence, uint64_t index); |
| 1415 | %rename("_bt_ctf_field_variant_get_field") bt_ctf_field_variant_get_field(struct bt_ctf_field *variant, struct bt_ctf_field *tag); |
| 1416 | %rename("_bt_ctf_field_enumeration_get_container") bt_ctf_field_enumeration_get_container(struct bt_ctf_field *enumeration); |
| 1417 | %rename("_bt_ctf_field_signed_integer_set_value") bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *integer, int64_t value); |
| 1418 | %rename("_bt_ctf_field_unsigned_integer_set_value") bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *integer, uint64_t value); |
| 1419 | %rename("_bt_ctf_field_floating_point_set_value") bt_ctf_field_floating_point_set_value(struct bt_ctf_field *floating_point, double value); |
| 1420 | %rename("_bt_ctf_field_string_set_value") bt_ctf_field_string_set_value(struct bt_ctf_field *string, const char *value); |
| 1421 | %rename("_bt_ctf_field_get") bt_ctf_field_get(struct bt_ctf_field *field); |
| 1422 | %rename("_bt_ctf_field_put") bt_ctf_field_put(struct bt_ctf_field *field); |
| 1423 | |
| 1424 | struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type); |
| 1425 | struct bt_ctf_field *bt_ctf_field_structure_get_field(struct bt_ctf_field *structure, const char *name); |
| 1426 | struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *array, uint64_t index); |
| 1427 | int bt_ctf_field_sequence_set_length(struct bt_ctf_field *sequence, struct bt_ctf_field *length_field); |
| 1428 | struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *sequence, uint64_t index); |
| 1429 | struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *variant, struct bt_ctf_field *tag); |
| 1430 | struct bt_ctf_field *bt_ctf_field_enumeration_get_container(struct bt_ctf_field *enumeration); |
| 1431 | int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *integer, int64_t value); |
| 1432 | int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *integer, uint64_t value); |
| 1433 | int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *floating_point, double value); |
| 1434 | int bt_ctf_field_string_set_value(struct bt_ctf_field *string, const char *value); |
| 1435 | void bt_ctf_field_get(struct bt_ctf_field *field); |
| 1436 | void bt_ctf_field_put(struct bt_ctf_field *field); |
| 1437 | |
| 1438 | /* ================================================================= |
| 1439 | EVENT.H |
| 1440 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 1441 | */ |
| 1442 | %rename("_bt_ctf_event_class_create") bt_ctf_event_class_create(const char *name); |
| 1443 | %rename("_bt_ctf_event_class_add_field") bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, struct bt_ctf_field_type *type, const char *name); |
| 1444 | %rename("_bt_ctf_event_class_get") bt_ctf_event_class_get(struct bt_ctf_event_class *event_class); |
| 1445 | %rename("_bt_ctf_event_class_put") bt_ctf_event_class_put(struct bt_ctf_event_class *event_class); |
| 1446 | %rename("_bt_ctf_event_create") bt_ctf_event_create(struct bt_ctf_event_class *event_class); |
| 1447 | %rename("_bt_ctf_event_set_payload") bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struct bt_ctf_field *value); |
| 1448 | %rename("_bt_ctf_event_get_payload") bt_ctf_event_get_payload(struct bt_ctf_event *event, const char *name); |
| 1449 | %rename("_bt_ctf_event_get") bt_ctf_event_get(struct bt_ctf_event *event); |
| 1450 | %rename("_bt_ctf_event_put") bt_ctf_event_put(struct bt_ctf_event *event); |
| 1451 | |
| 1452 | struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name); |
| 1453 | int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, struct bt_ctf_field_type *type, const char *name); |
| 1454 | void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class); |
| 1455 | void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class); |
| 1456 | struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class); |
| 1457 | int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struct bt_ctf_field *value); |
| 1458 | struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, const char *name); |
| 1459 | void bt_ctf_event_get(struct bt_ctf_event *event); |
| 1460 | void bt_ctf_event_put(struct bt_ctf_event *event); |
| 1461 | |
| 1462 | /* ================================================================= |
| 1463 | STREAM.H |
| 1464 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 1465 | */ |
| 1466 | %rename("_bt_ctf_stream_class_create") bt_ctf_stream_class_create(const char *name); |
| 1467 | %rename("_bt_ctf_stream_class_set_clock") bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, struct bt_ctf_clock *clock); |
| 1468 | %rename("_bt_ctf_stream_class_add_event_class") bt_ctf_stream_class_add_event_class(struct bt_ctf_stream_class *stream_class, struct bt_ctf_event_class *event_class); |
| 1469 | %rename("_bt_ctf_stream_class_get") bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class); |
| 1470 | %rename("_bt_ctf_stream_class_put") bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class); |
| 1471 | %rename("_bt_ctf_stream_append_discarded_events") bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, uint64_t event_count); |
| 1472 | %rename("_bt_ctf_stream_append_event") bt_ctf_stream_append_event(struct bt_ctf_stream *stream, struct bt_ctf_event *event); |
| 1473 | %rename("_bt_ctf_stream_flush") bt_ctf_stream_flush(struct bt_ctf_stream *stream); |
| 1474 | %rename("_bt_ctf_stream_get") bt_ctf_stream_get(struct bt_ctf_stream *stream); |
| 1475 | %rename("_bt_ctf_stream_put") bt_ctf_stream_put(struct bt_ctf_stream *stream); |
| 1476 | |
| 1477 | struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name); |
| 1478 | int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, struct bt_ctf_clock *clock); |
| 1479 | int bt_ctf_stream_class_add_event_class(struct bt_ctf_stream_class *stream_class, struct bt_ctf_event_class *event_class); |
| 1480 | void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class); |
| 1481 | void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class); |
| 1482 | void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream, uint64_t event_count); |
| 1483 | int bt_ctf_stream_append_event(struct bt_ctf_stream *stream, struct bt_ctf_event *event); |
| 1484 | int bt_ctf_stream_flush(struct bt_ctf_stream *stream); |
| 1485 | void bt_ctf_stream_get(struct bt_ctf_stream *stream); |
| 1486 | void bt_ctf_stream_put(struct bt_ctf_stream *stream); |
| 1487 | |
| 1488 | /* ================================================================= |
| 1489 | WRITER.H |
| 1490 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 1491 | */ |
| 1492 | %rename("_bt_ctf_writer_create") bt_ctf_writer_create(const char *path); |
| 1493 | %rename("_bt_ctf_writer_create_stream") bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class); |
| 1494 | %rename("_bt_ctf_writer_add_environment_field") bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value); |
| 1495 | %rename("_bt_ctf_writer_add_clock") bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock); |
| 1496 | %newobject bt_ctf_writer_get_metadata_string; |
| 1497 | %rename("_bt_ctf_writer_get_metadata_string") bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer); |
| 1498 | %rename("_bt_ctf_writer_flush_metadata") bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer); |
| 1499 | %rename("_bt_ctf_writer_set_byte_order") bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, enum bt_ctf_byte_order byte_order); |
| 1500 | %rename("_bt_ctf_writer_get") bt_ctf_writer_get(struct bt_ctf_writer *writer); |
| 1501 | %rename("_bt_ctf_writer_put") bt_ctf_writer_put(struct bt_ctf_writer *writer); |
| 1502 | |
| 1503 | struct bt_ctf_writer *bt_ctf_writer_create(const char *path); |
| 1504 | struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class); |
| 1505 | int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value); |
| 1506 | int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock); |
| 1507 | char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer); |
| 1508 | void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer); |
| 1509 | int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer, enum bt_ctf_byte_order byte_order); |
| 1510 | void bt_ctf_writer_get(struct bt_ctf_writer *writer); |
| 1511 | void bt_ctf_writer_put(struct bt_ctf_writer *writer); |
| 1512 | |
| 1513 | %pythoncode %{ |
| 1514 | |
| 1515 | class CTFWriter: |
| 1516 | class ByteOrder: |
| 1517 | BYTE_ORDER_NATIVE = 0 |
| 1518 | BYTE_ORDER_LITTLE_ENDIAN = 1 |
| 1519 | BYTE_ORDER_BIG_ENDIAN = 2 |
| 1520 | BYTE_ORDER_NETWORK = 3 |
| 1521 | |
| 1522 | class Clock: |
| 1523 | def __init__(self, name): |
| 1524 | self._c = _bt_ctf_clock_create(name) |
| 1525 | if self._c is None: |
| 1526 | raise ValueError("Invalid clock name.") |
| 1527 | |
| 1528 | def __del__(self): |
| 1529 | _bt_ctf_clock_put(self._c) |
| 1530 | |
| 1531 | """ |
| 1532 | Set the clock's description. The description appears in the clock's TSDL |
| 1533 | meta-data. |
| 1534 | """ |
| 1535 | def set_description(self, desc): |
| 1536 | ret = _bt_ctf_clock_set_description(self._c, desc) |
| 1537 | if ret < 0: |
| 1538 | raise ValueError("Invalid clock description.") |
| 1539 | |
| 1540 | """ |
| 1541 | Set the clock's frequency (Hz). |
| 1542 | """ |
| 1543 | def set_frequency(self, freq): |
| 1544 | ret = _bt_ctf_clock_set_frequency(self._c, freq) |
| 1545 | if ret < 0: |
| 1546 | raise ValueError("Invalid frequency value.") |
| 1547 | |
| 1548 | """ |
| 1549 | Set the clock's precision (in clock ticks). |
| 1550 | """ |
| 1551 | def set_precision(self, precision): |
| 1552 | ret = _bt_ctf_clock_set_precision(self._c, precision) |
| 1553 | |
| 1554 | """ |
| 1555 | Set the clock's offset in seconds from POSIX.1 Epoch. |
| 1556 | """ |
| 1557 | def set_offset_seconds(self, offset_s): |
| 1558 | ret = _bt_ctf_clock_set_offset_s(self._c, offset_s) |
| 1559 | if ret < 0: |
| 1560 | raise ValueError("Invalid offset value.") |
| 1561 | |
| 1562 | """ |
| 1563 | Set the clock's offset in ticks from POSIX.1 Epoch + offset in seconds. |
| 1564 | """ |
| 1565 | def set_offset_seconds(self, offset): |
| 1566 | ret = _bt_ctf_clock_set_offset(self._c, offset) |
| 1567 | if ret < 0: |
| 1568 | raise ValueError("Invalid offset value.") |
| 1569 | |
| 1570 | """ |
| 1571 | Set a clock's absolute attribute. A clock is absolute if the clock |
| 1572 | is a global reference across the trace's other clocks. |
| 1573 | """ |
| 1574 | def set_is_absolute(self, is_absolute): |
| 1575 | ret = _bt_ctf_clock_set_is_absolute(self._c, is_absolute) |
| 1576 | if ret < 0: |
| 1577 | raise ValueError("Could not set the clock's absolute attribute.") |
| 1578 | |
| 1579 | """ |
| 1580 | Set the current time in nanoseconds since the clock's origin (offset and |
| 1581 | offset_s attributes). The clock's value will be sampled as events are |
| 1582 | appended to a stream. |
| 1583 | """ |
| 1584 | def set_time(self, time): |
| 1585 | ret = _bt_ctf_clock_set_time(self._c, time) |
| 1586 | if ret < 0: |
| 1587 | raise ValueError("Invalid time value.") |
| 1588 | |
| 1589 | class FieldType: |
| 1590 | """ |
| 1591 | FieldType should not be instantiated directly. Please instantiate |
| 1592 | one of the concrete FieldType classes. |
| 1593 | """ |
| 1594 | class IntegerBase: |
| 1595 | # These values are based on the bt_ctf_integer_base enum |
| 1596 | # declared in event-types.h. |
| 1597 | INTEGER_BASE_UNKNOWN = -1 |
| 1598 | INTEGER_BASE_BINARY = 2 |
| 1599 | INTEGER_BASE_OCTAL = 8 |
| 1600 | INTEGER_BASE_DECIMAL = 10 |
| 1601 | INTEGER_BASE_HEXADECIMAL = 16 |
| 1602 | |
| 1603 | def __init__(self): |
| 1604 | if self._ft is None: |
| 1605 | raise ValueError("FieldType creation failed.") |
| 1606 | |
| 1607 | def __del__(self): |
| 1608 | _bt_ctf_field_type_put(self._ft) |
| 1609 | |
| 1610 | """ |
| 1611 | Set the field type's alignment. Defaults to 1 (bit-aligned). However, |
| 1612 | some types, such as structures and string, may impose other alignment |
| 1613 | constraints. |
| 1614 | """ |
| 1615 | def set_alignment(self, alignment): |
| 1616 | ret = _bt_ctf_field_type_set_alignment(self._ft, alignment) |
| 1617 | if ret < 0: |
| 1618 | raise ValueError("Invalid alignment value.") |
| 1619 | |
| 1620 | """ |
| 1621 | Set the field type's byte order. Use constants defined in the ByteOrder |
| 1622 | class. |
| 1623 | """ |
| 1624 | def set_byte_order(self, byte_order): |
| 1625 | ret = _bt_ctf_field_type_set_byte_order(self._ft, byte_order) |
| 1626 | if ret < 0: |
| 1627 | raise ValueError("Could not set byte order value.") |
| 1628 | |
| 1629 | class FieldTypeInteger(FieldType): |
| 1630 | """ |
| 1631 | Create a new integer field type of the given size. |
| 1632 | """ |
| 1633 | def __init__(self, size): |
| 1634 | self._ft = _bt_ctf_field_type_integer_create(size) |
| 1635 | super().__init__() |
| 1636 | |
| 1637 | """ |
| 1638 | Set an integer type's signedness attribute. |
| 1639 | """ |
| 1640 | def set_signed(self, signed): |
| 1641 | ret = _bt_ctf_field_type_integer_set_signed(self._ft, signed) |
| 1642 | if ret < 0: |
| 1643 | raise ValueError("Could not set signed attribute.") |
| 1644 | |
| 1645 | """ |
| 1646 | Set the integer type's base used to pretty-print the resulting trace. |
| 1647 | The base must be a constant of the IntegerBase class. |
| 1648 | """ |
| 1649 | def set_base(self, base): |
| 1650 | ret = _bt_ctf_field_type_integer_set_base(self._ft, base) |
| 1651 | if ret < 0: |
| 1652 | raise ValueError("Could not set base value.") |
| 1653 | |
| 1654 | """ |
| 1655 | An integer encoding may be set to signal that the integer must be printed |
| 1656 | as a text character. Must be a constant from the CTFStringEncoding class. |
| 1657 | """ |
| 1658 | def set_encoding(self, encoding): |
| 1659 | ret = _bt_ctf_field_type_integer_set_encoding(self._ft, encoding) |
| 1660 | if ret < 0: |
| 1661 | raise ValueError("Could not set integer encoding.") |
| 1662 | |
| 1663 | class FieldTypeEnumeration(FieldType): |
| 1664 | """ |
| 1665 | Create a new enumeration field type with the given underlying type. |
| 1666 | """ |
| 1667 | def __init__(self, integer_type): |
| 1668 | if integer_type is None or not isinstance(integer_type, CTFWriter.FieldTypeInteger): |
| 1669 | raise TypeError("Invalid integer container.") |
| 1670 | |
| 1671 | self._ft = _bt_ctf_field_type_enumeration_create(integer_type._ft) |
| 1672 | super().__init__() |
| 1673 | |
| 1674 | """ |
| 1675 | Add a mapping to the enumeration. The range's values are inclusive. |
| 1676 | """ |
| 1677 | def add_mapping(self, name, range_start, range_end): |
| 1678 | ret = _bt_ctf_field_type_enumeration_add_mapping(self._ft, name, range_start, range_end) |
| 1679 | if ret < 0: |
| 1680 | raise ValueError("Could not add mapping to enumeration type.") |
| 1681 | |
| 1682 | class FieldTypeFloatingPoint(FieldType): |
| 1683 | FLT_EXP_DIG = 8 |
| 1684 | DBL_EXP_DIG = 11 |
| 1685 | FLT_MANT_DIG = 24 |
| 1686 | DBL_MANT_DIG = 53 |
| 1687 | |
| 1688 | """ |
| 1689 | Create a new floating point field type. |
| 1690 | """ |
| 1691 | def __init__(self): |
| 1692 | self._ft = _bt_ctf_field_type_floating_point_create() |
| 1693 | super().__init__() |
| 1694 | |
| 1695 | """ |
| 1696 | Set the number of exponent digits to use to store the floatingpoint field. |
| 1697 | The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG which |
| 1698 | are defined as constants of this class. |
| 1699 | """ |
| 1700 | def set_exponent_digits(self, exponent_digits): |
| 1701 | ret = _bt_ctf_field_type_floating_point_set_exponent_digits(self._ft, exponent_digits) |
| 1702 | if ret < 0: |
| 1703 | raise ValueError("Could not set exponent digit count.") |
| 1704 | |
| 1705 | """ |
| 1706 | Set the numberof mantissa digits to use to store the floatingpoint field. |
| 1707 | The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG which |
| 1708 | are defined as constants of this class. |
| 1709 | """ |
| 1710 | def set_mantissa_digits(self, mantissa_digits): |
| 1711 | ret = _bt_ctf_field_type_floating_point_set_mantissa_digits(self._ft, mantissa_digits) |
| 1712 | if ret < 0: |
| 1713 | raise ValueError("Could not set mantissa digit count.") |
| 1714 | |
| 1715 | class FieldTypeStructure(FieldType): |
| 1716 | """ |
| 1717 | Create a new structure field type. |
| 1718 | """ |
| 1719 | def __init__(self): |
| 1720 | self._ft = _bt_ctf_field_type_structure_create() |
| 1721 | super().__init__() |
| 1722 | |
| 1723 | """ |
| 1724 | Add a field of type "field_type" to the structure. |
| 1725 | """ |
| 1726 | def add_field(self, field_type, field_name): |
| 1727 | ret = _bt_ctf_field_type_structure_add_field(self._ft, field_type._ft, field_name) |
| 1728 | if ret < 0: |
| 1729 | raise ValueError("Could not add field to structure.") |
| 1730 | |
| 1731 | class FieldTypeVariant(FieldType): |
| 1732 | """ |
| 1733 | Create a new variant field type. |
| 1734 | """ |
| 1735 | def __init__(self, enum_tag, tag_name): |
| 1736 | if enum_tag is None or not isinstance(enum_tag, CTFWriter.FieldTypeEnumeration): |
| 1737 | raise TypeError("Invalid tag type; must be of type FieldTypeEnumeration.") |
| 1738 | |
| 1739 | self._ft = _bt_ctf_field_type_variant_create(enum_tag._ft, tag_name) |
| 1740 | super().__init__() |
| 1741 | |
| 1742 | """ |
| 1743 | Add a field of type "field_type" to the variant. |
| 1744 | """ |
| 1745 | def add_field(self, field_type, field_name): |
| 1746 | ret = _bt_ctf_field_type_variant_add_field(self._ft, field_type._ft, field_name) |
| 1747 | if ret < 0: |
| 1748 | raise ValueError("Could not add field to variant.") |
| 1749 | |
| 1750 | class FieldTypeArray(FieldType): |
| 1751 | """ |
| 1752 | Create a new array field type. |
| 1753 | """ |
| 1754 | def __init__(self, element_type, length): |
| 1755 | self._ft = _bt_ctf_field_type_array_create(element_type._ft, length) |
| 1756 | super().__init__() |
| 1757 | |
| 1758 | class FieldTypeSequence(FieldType): |
| 1759 | """ |
| 1760 | Create a new sequence field type. |
| 1761 | """ |
| 1762 | def __init__(self, element_type, length_field_name): |
| 1763 | self._ft = _bt_ctf_field_type_sequence_create(element_type._ft, length_field_name) |
| 1764 | super().__init__() |
| 1765 | |
| 1766 | class FieldTypeString(FieldType): |
| 1767 | """ |
| 1768 | Create a new string field type. |
| 1769 | """ |
| 1770 | def __init__(self): |
| 1771 | self._ft = _bt_ctf_field_type_string_create() |
| 1772 | super().__init__() |
| 1773 | |
| 1774 | """ |
| 1775 | Set a string type's encoding. Must be a constant from the CTFStringEncoding class. |
| 1776 | """ |
| 1777 | def set_encoding(self, encoding): |
| 1778 | ret = _bt_ctf_field_type_string_set_encoding(self._ft, encoding) |
| 1779 | if ret < 0: |
| 1780 | raise ValueError("Could not set string encoding.") |
| 1781 | |
| 1782 | """ |
| 1783 | Create an instance of a field. |
| 1784 | """ |
| 1785 | @staticmethod |
| 1786 | def create_field(self, field_type): |
| 1787 | if field_type is None or not isinstance(field_type, CTFWriter.FieldType): |
| 1788 | raise TypeError("Invalid field_type. Type must be a FieldType-derived class.") |
| 1789 | |
| 1790 | if isinstance(field_type, CTFWriter.FieldTypeInteger): |
| 1791 | return CTFWriter.FieldInteger(field_type) |
| 1792 | elif isinstance(field_type, CTFWriter.FieldTypeEnumeration): |
| 1793 | return CTFWriter.FieldEnumeration(field_type) |
| 1794 | elif isinstance(field_type, CTFWriter.FieldTypeFloatingPoint): |
| 1795 | return CTFWriter.FieldFloatingPoint(field_type) |
| 1796 | elif isinstance(field_type, CTFWriter.FieldTypeStructure): |
| 1797 | return CTFWriter.FieldStructure(field_type) |
| 1798 | elif isinstance(field_type, CTFWriter.FieldTypeVariant): |
| 1799 | return CTFWriter.FieldVariant(field_type) |
| 1800 | elif isinstance(field_type, CTFWriter.FieldTypeArray): |
| 1801 | return CTFWriter.FieldArray(field_type) |
| 1802 | elif isinstance(field_type, CTFWriter.FieldTypeSequence): |
| 1803 | return CTFWriter.FieldSequence(field_type) |
| 1804 | elif isinstance(field_type, CTFWriter.FieldTypeString): |
| 1805 | return CTFWriter.FieldString(field_type) |
| 1806 | |
| 1807 | class Field: |
| 1808 | """ |
| 1809 | Base class, do not instantiate. |
| 1810 | """ |
| 1811 | def __init__(self, field_type): |
| 1812 | if not isinstance(field_type, CTFWriter.FieldType): |
| 1813 | raise TypeError("Invalid field_type argument.") |
| 1814 | |
| 1815 | self._f = _bt_ctf_field_create(field_type._ft) |
| 1816 | if self._f is None: |
| 1817 | raise ValueError("Field creation failed.") |
| 1818 | |
| 1819 | def __del__(self): |
| 1820 | _bt_ctf_field_put(self._f) |
| 1821 | |
| 1822 | @staticmethod |
| 1823 | def _create_field_from_native_instance(native_field_instance): |
| 1824 | type_dict = { |
| 1825 | CTFTypeId.INTEGER : CTFWriter.FieldInteger, |
| 1826 | CTFTypeId.FLOAT : CTFWriter.FieldFloatingPoint, |
| 1827 | CTFTypeId.ENUM : CTFWriter.FieldEnumeration, |
| 1828 | CTFTypeId.STRING : CTFWriter.FieldString, |
| 1829 | CTFTypeId.STRUCT : CTFWriter.FieldStructure, |
| 1830 | CTFTypeId.VARIANT : CTFWriter.FieldVariant, |
| 1831 | CTFTypeId.ARRAY : CTFWriter.FieldArray, |
| 1832 | CTFTypeId.SEQUENCE : CTFWriter.FieldSequence |
| 1833 | } |
| 1834 | |
| 1835 | field_type = _bt_python_get_field_type(native_field_instance) |
| 1836 | if field_type == CTFTypeId.UNKNOWN: |
| 1837 | raise TypeError("Invalid field instance") |
| 1838 | |
| 1839 | field = CTFWriter.Field.__new__(CTFWriter.Field) |
| 1840 | field._f = native_field_instance |
| 1841 | field.__class__ = type_dict[field_type] |
| 1842 | return field |
| 1843 | |
| 1844 | class FieldInteger(Field): |
| 1845 | """ |
| 1846 | Set an integer field's value. |
| 1847 | """ |
| 1848 | def set_value(self, value): |
| 1849 | signedness = _bt_python_field_integer_get_signedness(self._f) |
| 1850 | if signedness < 0: |
| 1851 | raise TypeError("Invalid integer instance.") |
| 1852 | |
| 1853 | if signedness == 0: |
| 1854 | ret = _bt_ctf_field_unsigned_integer_set_value(self._f, value) |
| 1855 | else: |
| 1856 | ret = _bt_ctf_field_signed_integer_set_value(self._f, value) |
| 1857 | |
| 1858 | if ret < 0: |
| 1859 | raise ValueError("Could not set integer field value.") |
| 1860 | |
| 1861 | class FieldEnumeration(Field): |
| 1862 | """ |
| 1863 | Return the enumeration's underlying container field (an integer field). |
| 1864 | """ |
| 1865 | def get_container(self): |
| 1866 | container = CTFWriter.FieldInteger.__new__(CTFWriter.FieldInteger) |
| 1867 | container._f = _bt_ctf_field_enumeration_get_container(self._f) |
| 1868 | if container._f is None: |
| 1869 | raise TypeError("Invalid enumeration field type.") |
| 1870 | return container |
| 1871 | |
| 1872 | class FieldFloatingPoint(Field): |
| 1873 | """ |
| 1874 | Set a floating point field's value. |
| 1875 | """ |
| 1876 | def set_value(self, value): |
| 1877 | ret = _bt_ctf_field_floating_point_set_value(self._f, value) |
| 1878 | if ret < 0: |
| 1879 | raise ValueError("Could not set floating point field value.") |
| 1880 | |
| 1881 | class FieldStructure(Field): |
| 1882 | """ |
| 1883 | Get the structure's field corresponding to the provided field name. |
| 1884 | """ |
| 1885 | def get_field(self, field_name): |
| 1886 | native_instance = _bt_ctf_field_structure_get_field(self._f, field_name) |
| 1887 | if native_instance is None: |
| 1888 | raise ValueError("Invalid field_name provided.") |
| 1889 | return CTFWriter.Field._create_field_from_native_instance(native_instance) |
| 1890 | |
| 1891 | class FieldVariant(Field): |
| 1892 | """ |
| 1893 | Return the variant's selected field. The "tag" field is the selector enum field. |
| 1894 | """ |
| 1895 | def get_field(self, tag): |
| 1896 | native_instance = _bt_ctf_field_variant_get_field(self._f, tag._f) |
| 1897 | if native_instance is None: |
| 1898 | raise ValueError("Invalid tag provided.") |
| 1899 | return CTFWriter.Field._create_field_from_native_instance(native_instance) |
| 1900 | |
| 1901 | class FieldArray(Field): |
| 1902 | """ |
| 1903 | Return the array's field at position "index". |
| 1904 | """ |
| 1905 | def get_field(self, index): |
| 1906 | native_instance = _bt_ctf_field_array_get_field(self._f, index) |
| 1907 | if native_instance is None: |
| 1908 | raise IndexError("Invalid index provided.") |
| 1909 | return CTFWriter.Field._create_field_from_native_instance(native_instance) |
| 1910 | |
| 1911 | class FieldSequence(Field): |
| 1912 | """ |
| 1913 | Set the sequence's length field (IntegerField). |
| 1914 | """ |
| 1915 | def set_length(self, length): |
| 1916 | if not isinstance(length, CTFWriter.FieldInteger): |
| 1917 | raise TypeError("Invalid length field.") |
| 1918 | ret = _bt_ctf_field_sequence_set_length(self._f, length._f) |
| 1919 | if ret < 0: |
| 1920 | raise ValueError("Could not set sequence length.") |
| 1921 | |
| 1922 | """ |
| 1923 | Return the sequence's field at position "index". |
| 1924 | """ |
| 1925 | def get_field(self, index): |
| 1926 | native_instance = _bt_ctf_field_sequence_get_field(self._f, index) |
| 1927 | if native_instance is None: |
| 1928 | raise ValueError("Could not get sequence element at index.") |
| 1929 | return CTFWriter.Field._create_field_from_native_instance(native_instance) |
| 1930 | |
| 1931 | class FieldString(Field): |
| 1932 | """ |
| 1933 | Set a string field's value. |
| 1934 | """ |
| 1935 | def set_value(self, value): |
| 1936 | ret = _bt_ctf_field_string_set_value(self._f, value) |
| 1937 | if ret < 0: |
| 1938 | raise ValueError("Could not set string field value.") |
| 1939 | |
| 1940 | class EventClass: |
| 1941 | """ |
| 1942 | Create a new event class of the given name. |
| 1943 | """ |
| 1944 | def __init__(self, name): |
| 1945 | self._ec = _bt_ctf_event_class_create(name) |
| 1946 | if self._ec is None: |
| 1947 | raise ValueError("Event class creation failed.") |
| 1948 | |
| 1949 | def __del__(self): |
| 1950 | _bt_ctf_event_class_put(self._ec) |
| 1951 | |
| 1952 | """ |
| 1953 | Add a field of type "field_type" to the event class. |
| 1954 | """ |
| 1955 | def add_field(self, field_type, field_name): |
| 1956 | ret = _bt_ctf_event_class_add_field(self._ec, field_type._ft, field_name) |
| 1957 | if ret < 0: |
| 1958 | raise ValueError("Could not add field to event class.") |
| 1959 | |
| 1960 | class Event: |
| 1961 | """ |
| 1962 | Create a new event of the given event class. |
| 1963 | """ |
| 1964 | def __init__(self, event_class): |
| 1965 | if not isinstance(event_class, CTFWriter.EventClass): |
| 1966 | raise TypeError("Invalid event_class argument.") |
| 1967 | |
| 1968 | self._e = _bt_ctf_event_create(event_class._ec) |
| 1969 | if self._e is None: |
| 1970 | raise ValueError("Event creation failed.") |
| 1971 | |
| 1972 | def __del__(self): |
| 1973 | _bt_ctf_event_put(self._e) |
| 1974 | |
| 1975 | """ |
| 1976 | Set a manually created field as an event's payload. |
| 1977 | """ |
| 1978 | def set_payload(self, field_name, value): |
| 1979 | if not isinstance(value, CTFWriter.Field): |
| 1980 | raise TypeError("Invalid value type.") |
| 1981 | ret = _bt_ctf_event_set_payload(self._e, field_name, value._f) |
| 1982 | if ret < 0: |
| 1983 | raise ValueError("Could not set event field payload.") |
| 1984 | |
| 1985 | """ |
| 1986 | Set a manually created field as an event's payload. |
| 1987 | """ |
| 1988 | def get_payload(self, field_name): |
| 1989 | native_instance = _bt_ctf_event_get_payload(self._e, field_name) |
| 1990 | if native_instance is None: |
| 1991 | raise ValueError("Could not get event payload.") |
| 1992 | return CTFWriter.Field._create_field_from_native_instance(native_instance) |
| 1993 | |
| 1994 | class StreamClass: |
| 1995 | """ |
| 1996 | Create a new stream class of the given name. |
| 1997 | """ |
| 1998 | def __init__(self, name): |
| 1999 | self._sc = _bt_ctf_stream_class_create(name) |
| 2000 | if self._sc is None: |
| 2001 | raise ValueError("Stream class creation failed.") |
| 2002 | |
| 2003 | def __del__(self): |
| 2004 | _bt_ctf_stream_class_put(self._sc) |
| 2005 | |
| 2006 | """ |
| 2007 | Assign a clock to a stream class. |
| 2008 | """ |
| 2009 | def set_clock(self, clock): |
| 2010 | if not isinstance(clock, CTFWriter.Clock): |
| 2011 | raise TypeError("Invalid clock type.") |
| 2012 | |
| 2013 | ret = _bt_ctf_stream_class_set_clock(self._sc, clock._c) |
| 2014 | if ret < 0: |
| 2015 | raise ValueError("Could not set stream class clock.") |
| 2016 | |
| 2017 | """ |
| 2018 | Add an event class to a stream class. New events can be added even after a |
| 2019 | stream has beem instanciated and events have been appended. However, a stream |
| 2020 | will not accept events of a class that has not been registered beforehand. |
| 2021 | """ |
| 2022 | def add_event_class(self, event_class): |
| 2023 | if not isinstance(event_class, CTFWriter.EventClass): |
| 2024 | raise TypeError("Invalid event_class type.") |
| 2025 | |
| 2026 | ret = _bt_ctf_stream_class_add_event_class(self._sc, event_class._ec) |
| 2027 | if ret < 0: |
| 2028 | raise ValueError("Could not add event class.") |
| 2029 | |
| 2030 | class Stream: |
| 2031 | """ |
| 2032 | Create a stream of the given class. |
| 2033 | """ |
| 2034 | def __init__(self, stream_class): |
| 2035 | if not isinstance(stream_class, CTFWriter.StreamClass): |
| 2036 | raise TypeError("Invalid stream_class type.") |
| 2037 | |
| 2038 | self._s = _bt_ctf_stream_create(stream_class._sc) |
| 2039 | if self._s is None: |
| 2040 | raise ValueError("Stream creation failed.") |
| 2041 | |
| 2042 | def __del__(self): |
| 2043 | _bt_ctf_stream_put(self._s) |
| 2044 | |
| 2045 | """ |
| 2046 | Increase the current packet's discarded event count. |
| 2047 | """ |
| 2048 | def append_discarded_events(self, event_count): |
| 2049 | ret = _bt_ctf_stream_append_discarded_events(self._s, event_count) |
| 2050 | if ret < 0: |
| 2051 | raise ValueError("Could not append discarded events.") |
| 2052 | |
| 2053 | """ |
| 2054 | Append "event" to the stream's current packet. The stream's associated clock |
| 2055 | will be sampled during this call. The event shall not be modified after |
| 2056 | being appended to a stream. |
| 2057 | """ |
| 2058 | def append_event(self, event): |
| 2059 | ret = _bt_ctf_stream_append_event(self._s, event._e) |
| 2060 | if ret < 0: |
| 2061 | raise ValueError("Could not append event to stream.") |
| 2062 | |
| 2063 | """ |
| 2064 | The stream's current packet's events will be flushed to disk. Events |
| 2065 | subsequently appended to the stream will be added to a new packet. |
| 2066 | """ |
| 2067 | def flush(self): |
| 2068 | ret = _bt_ctf_stream_flush(self._s) |
| 2069 | if ret < 0: |
| 2070 | raise ValueError("Could not flush stream.") |
| 2071 | |
| 2072 | class Writer: |
| 2073 | """ |
| 2074 | Create a new writer that will produce a trace in the given path. |
| 2075 | """ |
| 2076 | def __init__(self, path): |
| 2077 | self._w = _bt_ctf_writer_create(path) |
| 2078 | if self._w is None: |
| 2079 | raise ValueError("Writer creation failed.") |
| 2080 | |
| 2081 | def __del__(self): |
| 2082 | _bt_ctf_writer_put(self._w) |
| 2083 | |
| 2084 | """ |
| 2085 | Create a new stream instance and register it to the writer. |
| 2086 | """ |
| 2087 | def create_stream(self, stream_class): |
| 2088 | if not isinstance(stream_class, CTFWriter.StreamClass): |
| 2089 | raise TypeError("Invalid stream_class type.") |
| 2090 | |
| 2091 | stream = CTFWriter.Stream.__new__(CTFWriter.Stream) |
| 2092 | stream._s = _bt_ctf_writer_create_stream(self._w, stream_class._sc) |
| 2093 | return stream |
| 2094 | |
| 2095 | """ |
| 2096 | Add an environment field to the trace. |
| 2097 | """ |
| 2098 | def add_environment_field(self, name, value): |
| 2099 | ret = _bt_ctf_writer_add_environment_field(self._w, name, value) |
| 2100 | if ret < 0: |
| 2101 | raise ValueError("Could not add environment field to trace.") |
| 2102 | |
| 2103 | """ |
| 2104 | Add a clock to the trace. Clocks assigned to stream classes must be |
| 2105 | registered to the writer. |
| 2106 | """ |
| 2107 | def add_clock(self, clock): |
| 2108 | ret = _bt_ctf_writer_add_clock(self._w, clock._c) |
| 2109 | if ret < 0: |
| 2110 | raise ValueError("Could not add clock to Writer.") |
| 2111 | |
| 2112 | """ |
| 2113 | Get the trace's TSDL meta-data. |
| 2114 | """ |
| 2115 | def get_metadata(self): |
| 2116 | return _bt_ctf_writer_get_metadata_string(self._w) |
| 2117 | |
| 2118 | """ |
| 2119 | Flush the trace's metadata to the metadata file. |
| 2120 | """ |
| 2121 | def flush_metadata(self): |
| 2122 | _bt_ctf_writer_flush_metadata(self._w) |
| 2123 | |
| 2124 | """ |
| 2125 | Set the trace's byte order. Must be a constant from the ByteOrder |
| 2126 | class. Defaults to BYTE_ORDER_NATIVE, the host machine's endianness. |
| 2127 | """ |
| 2128 | def set_byte_order(self, byte_order): |
| 2129 | ret = _bt_ctf_writer_set_byte_order(self._w, byte_order) |
| 2130 | if ret < 0: |
| 2131 | raise ValueError("Could not set trace's byte order.") |
| 2132 | |
| 2133 | %} |