| 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 | %} |
| 52 | |
| 53 | typedef unsigned long long uint64_t; |
| 54 | typedef long long int64_t; |
| 55 | typedef int bt_intern_str; |
| 56 | |
| 57 | /* ================================================================= |
| 58 | CONTEXT.H, CONTEXT-INTERNAL.H |
| 59 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 60 | */ |
| 61 | |
| 62 | %rename("_bt_context_create") bt_context_create(void); |
| 63 | %rename("_bt_context_add_trace") bt_context_add_trace( |
| 64 | struct bt_context *ctx, const char *path, const char *format, |
| 65 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, int whence), |
| 66 | struct bt_mmap_stream_list *stream_list, FILE *metadata); |
| 67 | %rename("_bt_context_remove_trace") bt_context_remove_trace( |
| 68 | struct bt_context *ctx, int trace_id); |
| 69 | %rename("_bt_context_get") bt_context_get(struct bt_context *ctx); |
| 70 | %rename("_bt_context_put") bt_context_put(struct bt_context *ctx); |
| 71 | %rename("_bt_ctf_event_get_context") bt_ctf_event_get_context( |
| 72 | const struct bt_ctf_event *event); |
| 73 | |
| 74 | struct bt_context *bt_context_create(void); |
| 75 | int bt_context_add_trace(struct bt_context *ctx, const char *path, const char *format, |
| 76 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, int whence), |
| 77 | struct bt_mmap_stream_list *stream_list, FILE *metadata); |
| 78 | void bt_context_remove_trace(struct bt_context *ctx, int trace_id); |
| 79 | void bt_context_get(struct bt_context *ctx); |
| 80 | void bt_context_put(struct bt_context *ctx); |
| 81 | struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *event); |
| 82 | |
| 83 | // class Context to prevent direct access to struct bt_context |
| 84 | %pythoncode%{ |
| 85 | class Context: |
| 86 | """ |
| 87 | The context represents the object in which a trace_collection is |
| 88 | open. As long as this structure is allocated, the trace_collection |
| 89 | is open and the traces it contains can be read and seeked by the |
| 90 | iterators and callbacks. |
| 91 | """ |
| 92 | |
| 93 | def __init__(self): |
| 94 | self._c = _bt_context_create() |
| 95 | |
| 96 | def __del__(self): |
| 97 | _bt_context_put(self._c) |
| 98 | |
| 99 | def add_trace(self, path, format_str, |
| 100 | packet_seek=None, stream_list=None, metadata=None): |
| 101 | """ |
| 102 | Add a trace by path to the context. |
| 103 | |
| 104 | Open a trace. |
| 105 | |
| 106 | path is the path to the trace, it is not recursive. |
| 107 | If "path" is None, stream_list is used instead as a list |
| 108 | of mmap streams to open for the trace. |
| 109 | |
| 110 | format is a string containing the format name in which the trace was |
| 111 | produced. |
| 112 | |
| 113 | packet_seek is not implemented for Python. Should be left None to |
| 114 | use the default packet_seek handler provided by the trace format. |
| 115 | |
| 116 | stream_list is a linked list of streams, it is used to open a trace |
| 117 | where the trace data is located in memory mapped areas instead of |
| 118 | trace files, this argument should be None when path is not None. |
| 119 | |
| 120 | The metadata parameter acts as a metadata override when not None, |
| 121 | otherwise the format handles the metadata opening. |
| 122 | |
| 123 | Return: the corresponding TraceHandle on success or None on error. |
| 124 | """ |
| 125 | if metadata is not None: |
| 126 | metadata = metadata._file |
| 127 | |
| 128 | ret = _bt_context_add_trace(self._c, path, format_str, packet_seek, |
| 129 | stream_list, metadata) |
| 130 | if ret < 0: |
| 131 | return None |
| 132 | |
| 133 | th = TraceHandle.__new__(TraceHandle) |
| 134 | th._id = ret |
| 135 | return th |
| 136 | |
| 137 | def add_traces_recursive(self, path, format_str): |
| 138 | """ |
| 139 | Open a trace recursively. |
| 140 | |
| 141 | Find each trace present in the subdirectory starting from the given |
| 142 | path, and add them to the context. |
| 143 | |
| 144 | Return a dict of TraceHandle instances (the full path is the key). |
| 145 | Return None on error. |
| 146 | """ |
| 147 | |
| 148 | import os |
| 149 | |
| 150 | trace_handles = {} |
| 151 | |
| 152 | noTrace = True |
| 153 | error = False |
| 154 | |
| 155 | for fullpath, dirs, files in os.walk(path): |
| 156 | if "metadata" in files: |
| 157 | trace_handle = self.add_trace(fullpath, format_str) |
| 158 | if trace_handle is None: |
| 159 | error = True |
| 160 | continue |
| 161 | |
| 162 | trace_handles[fullpath] = trace_handle |
| 163 | noTrace = False |
| 164 | |
| 165 | if noTrace and error: |
| 166 | return None |
| 167 | return trace_handles |
| 168 | |
| 169 | def remove_trace(self, trace_handle): |
| 170 | """ |
| 171 | Remove a trace from the context. |
| 172 | Effectively closing the trace. |
| 173 | """ |
| 174 | try: |
| 175 | _bt_context_remove_trace(self._c, trace_handle._id) |
| 176 | except AttributeError: |
| 177 | raise TypeError("in remove_trace, " |
| 178 | "argument 2 must be a TraceHandle instance") |
| 179 | %} |
| 180 | |
| 181 | |
| 182 | |
| 183 | /* ================================================================= |
| 184 | FORMAT.H, REGISTRY |
| 185 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 186 | */ |
| 187 | |
| 188 | %rename("lookup_format") bt_lookup_format(bt_intern_str qname); |
| 189 | %rename("_bt_print_format_list") bt_fprintf_format_list(FILE *fp); |
| 190 | %rename("register_format") bt_register_format(struct format *format); |
| 191 | %rename("unregister_format") bt_unregister_format(struct bt_format *format); |
| 192 | |
| 193 | extern struct format *bt_lookup_format(bt_intern_str qname); |
| 194 | extern void bt_fprintf_format_list(FILE *fp); |
| 195 | extern int bt_register_format(struct bt_format *format); |
| 196 | extern void bt_unregister_format(struct bt_format *format); |
| 197 | |
| 198 | %pythoncode %{ |
| 199 | |
| 200 | def print_format_list(babeltrace_file): |
| 201 | """ |
| 202 | Print a list of available formats to file. |
| 203 | |
| 204 | babeltrace_file must be a File instance opened in write mode. |
| 205 | """ |
| 206 | try: |
| 207 | if babeltrace_file._file is not None: |
| 208 | _bt_print_format_list(babeltrace_file._file) |
| 209 | except AttributeError: |
| 210 | raise TypeError("in print_format_list, " |
| 211 | "argument 1 must be a File instance") |
| 212 | |
| 213 | %} |
| 214 | |
| 215 | |
| 216 | /* ================================================================= |
| 217 | ITERATOR.H, ITERATOR-INTERNAL.H |
| 218 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 219 | */ |
| 220 | |
| 221 | %rename("_bt_iter_create") bt_iter_create(struct bt_context *ctx, |
| 222 | const struct bt_iter_pos *begin_pos, const struct bt_iter_pos *end_pos); |
| 223 | %rename("_bt_iter_destroy") bt_iter_destroy(struct bt_iter *iter); |
| 224 | %rename("_bt_iter_next") bt_iter_next(struct bt_iter *iter); |
| 225 | %rename("_bt_iter_get_pos") bt_iter_get_pos(struct bt_iter *iter); |
| 226 | %rename("_bt_iter_free_pos") bt_iter_free_pos(struct bt_iter_pos *pos); |
| 227 | %rename("_bt_iter_set_pos") bt_iter_set_pos(struct bt_iter *iter, |
| 228 | const struct bt_iter_pos *pos); |
| 229 | %rename("_bt_iter_create_time_pos") bt_iter_create_time_pos(struct bt_iter *iter, |
| 230 | uint64_t timestamp); |
| 231 | |
| 232 | struct bt_iter *bt_iter_create(struct bt_context *ctx, |
| 233 | const struct bt_iter_pos *begin_pos, const struct bt_iter_pos *end_pos); |
| 234 | void bt_iter_destroy(struct bt_iter *iter); |
| 235 | int bt_iter_next(struct bt_iter *iter); |
| 236 | struct bt_iter_pos *bt_iter_get_pos(struct bt_iter *iter); |
| 237 | void bt_iter_free_pos(struct bt_iter_pos *pos); |
| 238 | int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *pos); |
| 239 | struct bt_iter_pos *bt_iter_create_time_pos(struct bt_iter *iter, uint64_t timestamp); |
| 240 | |
| 241 | %rename("_bt_iter_pos") bt_iter_pos; |
| 242 | %rename("SEEK_TIME") BT_SEEK_TIME; |
| 243 | %rename("SEEK_RESTORE") BT_SEEK_RESTORE; |
| 244 | %rename("SEEK_CUR") BT_SEEK_CUR; |
| 245 | %rename("SEEK_BEGIN") BT_SEEK_BEGIN; |
| 246 | %rename("SEEK_LAST") BT_SEEK_LAST; |
| 247 | |
| 248 | // This struct is taken from iterator.h |
| 249 | // All changes to the struct must also be made here |
| 250 | struct bt_iter_pos { |
| 251 | enum { |
| 252 | BT_SEEK_TIME, /* uses u.seek_time */ |
| 253 | BT_SEEK_RESTORE, /* uses u.restore */ |
| 254 | BT_SEEK_CUR, |
| 255 | BT_SEEK_BEGIN, |
| 256 | BT_SEEK_LAST |
| 257 | } type; |
| 258 | union { |
| 259 | uint64_t seek_time; |
| 260 | struct bt_saved_pos *restore; |
| 261 | } u; |
| 262 | }; |
| 263 | |
| 264 | |
| 265 | %pythoncode%{ |
| 266 | |
| 267 | class IterPos: |
| 268 | """This class represents the position where to set an iterator.""" |
| 269 | |
| 270 | __can_access = False |
| 271 | |
| 272 | def __init__(self, seek_type, seek_time = None): |
| 273 | """ |
| 274 | seek_type represents the type of seek to use. |
| 275 | seek_time is the timestamp to seek to when using SEEK_TIME, it |
| 276 | is expressed in nanoseconds |
| 277 | Only use SEEK_RESTORE on IterPos obtained from the get_pos function |
| 278 | in Iter class. |
| 279 | """ |
| 280 | |
| 281 | self._pos = _bt_iter_pos() |
| 282 | self._pos.type = seek_type |
| 283 | if seek_time and seek_type == SEEK_TIME: |
| 284 | self._pos.u.seek_time = seek_time |
| 285 | self.__can_access = True |
| 286 | |
| 287 | def __del__(self): |
| 288 | if not self.__can_access: |
| 289 | _bt_iter_free_pos(self._pos) |
| 290 | |
| 291 | def _get_type(self): |
| 292 | if not __can_access: |
| 293 | raise AttributeError("seek_type is not available") |
| 294 | return self._pos.type |
| 295 | |
| 296 | def _set_type(self, seek_type): |
| 297 | if not __can_access: |
| 298 | raise AttributeError("seek_type is not available") |
| 299 | self._pos.type = seek_type |
| 300 | |
| 301 | def _get_time(self): |
| 302 | if not __can_access: |
| 303 | raise AttributeError("seek_time is not available") |
| 304 | |
| 305 | elif self._pos.type is not SEEK_TIME: |
| 306 | raise TypeError("seek_type is not SEEK_TIME") |
| 307 | |
| 308 | return self._pos.u.seek_time |
| 309 | |
| 310 | def _set_time(self, time): |
| 311 | if not __can_access: |
| 312 | raise AttributeError("seek_time is not available") |
| 313 | |
| 314 | elif self._pos.type is not SEEK_TIME: |
| 315 | raise TypeError("seek_type is not SEEK_TIME") |
| 316 | |
| 317 | self._pos.u.seek_time = time |
| 318 | |
| 319 | def _get_pos(self): |
| 320 | return self._pos |
| 321 | |
| 322 | |
| 323 | seek_type = property(_get_type, _set_type) |
| 324 | seek_time = property(_get_time, _set_time) |
| 325 | |
| 326 | |
| 327 | class Iterator: |
| 328 | |
| 329 | __with_init = False |
| 330 | |
| 331 | def __init__(self, context, begin_pos = None, end_pos = None, _no_init = None): |
| 332 | """ |
| 333 | Allocate a trace collection iterator. |
| 334 | |
| 335 | begin_pos and end_pos are optional parameters to specify the |
| 336 | position at which the trace collection should be seeked upon |
| 337 | iterator creation, and the position at which iteration will |
| 338 | start returning "EOF". |
| 339 | |
| 340 | By default, if begin_pos is None, a BT_SEEK_CUR is performed at |
| 341 | creation. By default, if end_pos is None, a BT_SEEK_END (end of |
| 342 | trace) is the EOF criterion. |
| 343 | """ |
| 344 | if _no_init is None: |
| 345 | if begin_pos is None: |
| 346 | bp = None |
| 347 | else: |
| 348 | try: |
| 349 | bp = begin_pos._pos |
| 350 | except AttributeError: |
| 351 | raise TypeError("in __init__, " |
| 352 | "argument 3 must be a IterPos instance") |
| 353 | |
| 354 | if end_pos is None: |
| 355 | ep = None |
| 356 | else: |
| 357 | try: |
| 358 | ep = end_pos._pos |
| 359 | except AttributeError: |
| 360 | raise TypeError("in __init__, " |
| 361 | "argument 4 must be a IterPos instance") |
| 362 | |
| 363 | try: |
| 364 | self._bi = _bt_iter_create(context._c, bp, ep) |
| 365 | except AttributeError: |
| 366 | raise TypeError("in __init__, " |
| 367 | "argument 2 must be a Context instance") |
| 368 | |
| 369 | self.__with_init = True |
| 370 | |
| 371 | else: |
| 372 | self._bi = _no_init |
| 373 | |
| 374 | def __del__(self): |
| 375 | if self.__with_init: |
| 376 | _bt_iter_destroy(self._bi) |
| 377 | |
| 378 | def next(self): |
| 379 | """ |
| 380 | Move trace collection position to the next event. |
| 381 | Returns 0 on success, a negative value on error. |
| 382 | """ |
| 383 | return _bt_iter_next(self._bi) |
| 384 | |
| 385 | def get_pos(self): |
| 386 | """Return a IterPos class of the current iterator position.""" |
| 387 | ret = IterPos(0) |
| 388 | ret.__can_access = False |
| 389 | ret._pos = _bt_iter_get_pos(self._bi) |
| 390 | return ret |
| 391 | |
| 392 | def set_pos(self, pos): |
| 393 | """ |
| 394 | Move the iterator to a given position. |
| 395 | |
| 396 | On error, the stream_heap is reinitialized and returned empty. |
| 397 | Return 0 for success. |
| 398 | Return EOF if the position requested is after the last event of the |
| 399 | trace collection. |
| 400 | Return -EINVAL when called with invalid parameter. |
| 401 | Return -ENOMEM if the stream_heap could not be properly initialized. |
| 402 | """ |
| 403 | try: |
| 404 | return _bt_iter_set_pos(self._bi, pos._pos) |
| 405 | except AttributeError: |
| 406 | raise TypeError("in set_pos, " |
| 407 | "argument 2 must be a IterPos instance") |
| 408 | |
| 409 | def create_time_pos(self, timestamp): |
| 410 | """ |
| 411 | Create a position based on time |
| 412 | This function allocates and returns a new IterPos to be able to |
| 413 | restore an iterator position based on a timestamp. |
| 414 | """ |
| 415 | |
| 416 | if timestamp < 0: |
| 417 | raise TypeError("timestamp must be an unsigned int") |
| 418 | |
| 419 | ret = IterPos(0) |
| 420 | ret.__can_access = False |
| 421 | ret._pos = _bt_iter_create_time_pos(self._bi, timestamp) |
| 422 | return ret |
| 423 | %} |
| 424 | |
| 425 | |
| 426 | /* ================================================================= |
| 427 | CLOCK-TYPE.H |
| 428 | ¯¯¯¯¯¯¯¯¯¯¯¯ |
| 429 | *** Enum copied from clock-type.h |
| 430 | All changes must also be made here |
| 431 | */ |
| 432 | %rename("CLOCK_CYCLES") BT_CLOCK_CYCLES; |
| 433 | %rename("CLOCK_REAL") BT_CLOCK_REAL; |
| 434 | |
| 435 | enum bt_clock_type { |
| 436 | BT_CLOCK_CYCLES = 0, |
| 437 | BT_CLOCK_REAL |
| 438 | }; |
| 439 | |
| 440 | /* ================================================================= |
| 441 | TRACE-HANDLE.H, TRACE-HANDLE-INTERNAL.H |
| 442 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 443 | */ |
| 444 | |
| 445 | %rename("_bt_trace_handle_create") bt_trace_handle_create(struct bt_context *ctx); |
| 446 | %rename("_bt_trace_handle_destroy") bt_trace_handle_destroy(struct bt_trace_handle *bt); |
| 447 | struct bt_trace_handle *bt_trace_handle_create(struct bt_context *ctx); |
| 448 | void bt_trace_handle_destroy(struct bt_trace_handle *bt); |
| 449 | |
| 450 | %rename("_bt_trace_handle_get_path") bt_trace_handle_get_path(struct bt_context *ctx, |
| 451 | int handle_id); |
| 452 | %rename("_bt_trace_handle_get_timestamp_begin") bt_trace_handle_get_timestamp_begin( |
| 453 | struct bt_context *ctx, int handle_id, enum bt_clock_type type); |
| 454 | %rename("_bt_trace_handle_get_timestamp_end") bt_trace_handle_get_timestamp_end( |
| 455 | struct bt_context *ctx, int handle_id, enum bt_clock_type type); |
| 456 | const char *bt_trace_handle_get_path(struct bt_context *ctx, int handle_id); |
| 457 | uint64_t bt_trace_handle_get_timestamp_begin(struct bt_context *ctx, int handle_id, |
| 458 | enum bt_clock_type type); |
| 459 | uint64_t bt_trace_handle_get_timestamp_end(struct bt_context *ctx, int handle_id, |
| 460 | enum bt_clock_type type); |
| 461 | |
| 462 | %rename("_bt_ctf_event_get_handle_id") bt_ctf_event_get_handle_id( |
| 463 | const struct bt_ctf_event *event); |
| 464 | int bt_ctf_event_get_handle_id(const struct bt_ctf_event *event); |
| 465 | |
| 466 | |
| 467 | %pythoncode%{ |
| 468 | |
| 469 | class TraceHandle(object): |
| 470 | """ |
| 471 | The TraceHandle allows the user to manipulate a trace file directly. |
| 472 | It is a unique identifier representing a trace file. |
| 473 | Do not instantiate. |
| 474 | """ |
| 475 | |
| 476 | def __init__(self): |
| 477 | raise NotImplementedError("TraceHandle cannot be instantiated") |
| 478 | |
| 479 | def __repr__(self): |
| 480 | return "Babeltrace TraceHandle: trace_id('{0}')".format(self._id) |
| 481 | |
| 482 | def get_id(self): |
| 483 | """Return the TraceHandle id.""" |
| 484 | return self._id |
| 485 | |
| 486 | def get_path(self, context): |
| 487 | """Return the path of a TraceHandle.""" |
| 488 | try: |
| 489 | return _bt_trace_handle_get_path(context._c, self._id) |
| 490 | except AttributeError: |
| 491 | raise TypeError("in get_path, " |
| 492 | "argument 2 must be a Context instance") |
| 493 | |
| 494 | def get_timestamp_begin(self, context, clock_type): |
| 495 | """Return the creation time of the buffers of a trace.""" |
| 496 | try: |
| 497 | return _bt_trace_handle_get_timestamp_begin( |
| 498 | context._c, self._id,clock_type) |
| 499 | except AttributeError: |
| 500 | raise TypeError("in get_timestamp_begin, " |
| 501 | "argument 2 must be a Context instance") |
| 502 | |
| 503 | def get_timestamp_end(self, context, clock_type): |
| 504 | """Return the destruction timestamp of the buffers of a trace.""" |
| 505 | try: |
| 506 | return _bt_trace_handle_get_timestamp_end( |
| 507 | context._c, self._id, clock_type) |
| 508 | except AttributeError: |
| 509 | raise TypeError("in get_timestamp_end, " |
| 510 | "argument 2 must be a Context instance") |
| 511 | |
| 512 | %} |
| 513 | |
| 514 | |
| 515 | |
| 516 | // ================================================================= |
| 517 | // CTF |
| 518 | // ================================================================= |
| 519 | |
| 520 | /* ================================================================= |
| 521 | ITERATOR.H, EVENTS.H |
| 522 | ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ |
| 523 | */ |
| 524 | |
| 525 | //Iterator |
| 526 | %rename("_bt_ctf_iter_create") bt_ctf_iter_create(struct bt_context *ctx, |
| 527 | const struct bt_iter_pos *begin_pos, |
| 528 | const struct bt_iter_pos *end_pos); |
| 529 | %rename("_bt_ctf_get_iter") bt_ctf_get_iter(struct bt_ctf_iter *iter); |
| 530 | %rename("_bt_ctf_iter_destroy") bt_ctf_iter_destroy(struct bt_ctf_iter *iter); |
| 531 | %rename("_bt_ctf_iter_read_event") bt_ctf_iter_read_event(struct bt_ctf_iter *iter); |
| 532 | |
| 533 | struct bt_ctf_iter *bt_ctf_iter_create(struct bt_context *ctx, |
| 534 | const struct bt_iter_pos *begin_pos, |
| 535 | const struct bt_iter_pos *end_pos); |
| 536 | struct bt_iter *bt_ctf_get_iter(struct bt_ctf_iter *iter); |
| 537 | void bt_ctf_iter_destroy(struct bt_ctf_iter *iter); |
| 538 | struct bt_ctf_event *bt_ctf_iter_read_event(struct bt_ctf_iter *iter); |
| 539 | |
| 540 | |
| 541 | //Events |
| 542 | |
| 543 | %rename("_bt_ctf_get_top_level_scope") bt_ctf_get_top_level_scope(const struct |
| 544 | bt_ctf_event *event, enum bt_ctf_scope scope); |
| 545 | %rename("_bt_ctf_event_name") bt_ctf_event_name(const struct bt_ctf_event *ctf_event); |
| 546 | %rename("_bt_ctf_get_timestamp") bt_ctf_get_timestamp( |
| 547 | const struct bt_ctf_event *ctf_event); |
| 548 | %rename("_bt_ctf_get_cycles") bt_ctf_get_cycles( |
| 549 | const struct bt_ctf_event *ctf_event); |
| 550 | |
| 551 | %rename("_bt_ctf_get_field") bt_ctf_get_field(const struct bt_ctf_event *ctf_event, |
| 552 | const struct bt_definition *scope, const char *field); |
| 553 | %rename("_bt_ctf_get_index") bt_ctf_get_index(const struct bt_ctf_event *ctf_event, |
| 554 | const struct bt_definition *field, unsigned int index); |
| 555 | %rename("_bt_ctf_field_name") bt_ctf_field_name(const struct bt_definition *field); |
| 556 | %rename("_bt_ctf_field_type") bt_ctf_field_type(const struct bt_declaration *field); |
| 557 | %rename("_bt_ctf_get_int_signedness") bt_ctf_get_int_signedness( |
| 558 | const struct bt_declaration *field); |
| 559 | %rename("_bt_ctf_get_int_base") bt_ctf_get_int_base(const struct bt_declaration *field); |
| 560 | %rename("_bt_ctf_get_int_byte_order") bt_ctf_get_int_byte_order( |
| 561 | const struct bt_declaration *field); |
| 562 | %rename("_bt_ctf_get_int_len") bt_ctf_get_int_len(const struct bt_declaration *field); |
| 563 | %rename("_bt_ctf_get_enum_int") bt_ctf_get_enum_int(const struct bt_definition *field); |
| 564 | %rename("_bt_ctf_get_enum_str") bt_ctf_get_enum_str(const struct bt_definition *field); |
| 565 | %rename("_bt_ctf_get_encoding") bt_ctf_get_encoding(const struct bt_declaration *field); |
| 566 | %rename("_bt_ctf_get_array_len") bt_ctf_get_array_len(const struct bt_declaration *field); |
| 567 | %rename("_bt_ctf_get_uint64") bt_ctf_get_uint64(const struct bt_definition *field); |
| 568 | %rename("_bt_ctf_get_int64") bt_ctf_get_int64(const struct bt_definition *field); |
| 569 | %rename("_bt_ctf_get_char_array") bt_ctf_get_char_array(const struct bt_definition *field); |
| 570 | %rename("_bt_ctf_get_string") bt_ctf_get_string(const struct bt_definition *field); |
| 571 | %rename("_bt_ctf_field_get_error") bt_ctf_field_get_error(void); |
| 572 | %rename("_bt_ctf_get_decl_event_name") bt_ctf_get_decl_event_name(const struct |
| 573 | bt_ctf_event_decl *event); |
| 574 | %rename("_bt_ctf_get_decl_field_name") bt_ctf_get_decl_field_name( |
| 575 | const struct bt_ctf_field_decl *field); |
| 576 | %rename("_bt_ctf_get_decl_from_def") bt_ctf_get_decl_from_def( |
| 577 | const struct bt_definition *field); |
| 578 | %rename("_bt_array_index") bt_array_index(struct definition_array *array, uint64_t i); |
| 579 | %rename("_bt_sequence_len") bt_sequence_len(struct definition_sequence *sequence); |
| 580 | %rename("_bt_sequence_index") bt_sequence_index(struct definition_sequence *sequence, uint64_t i); |
| 581 | |
| 582 | const struct bt_definition *bt_ctf_get_top_level_scope(const struct bt_ctf_event *ctf_event, |
| 583 | enum bt_ctf_scope scope); |
| 584 | const char *bt_ctf_event_name(const struct bt_ctf_event *ctf_event); |
| 585 | uint64_t bt_ctf_get_timestamp(const struct bt_ctf_event *ctf_event); |
| 586 | uint64_t bt_ctf_get_cycles(const struct bt_ctf_event *ctf_event); |
| 587 | const struct bt_definition *bt_ctf_get_field(const struct bt_ctf_event *ctf_event, |
| 588 | const struct bt_definition *scope, |
| 589 | const char *field); |
| 590 | const struct bt_definition *bt_ctf_get_index(const struct bt_ctf_event *ctf_event, |
| 591 | const struct bt_definition *field, |
| 592 | unsigned int index); |
| 593 | const char *bt_ctf_field_name(const struct bt_definition *field); |
| 594 | enum ctf_type_id bt_ctf_field_type(const struct bt_declaration *field); |
| 595 | int bt_ctf_get_int_signedness(const struct bt_declaration *field); |
| 596 | int bt_ctf_get_int_base(const struct bt_declaration *field); |
| 597 | int bt_ctf_get_int_byte_order(const struct bt_declaration *field); |
| 598 | ssize_t bt_ctf_get_int_len(const struct bt_declaration *field); |
| 599 | const struct bt_definition *bt_ctf_get_enum_int(const struct bt_definition *field); |
| 600 | const char *bt_ctf_get_enum_str(const struct bt_definition *field); |
| 601 | enum ctf_string_encoding bt_ctf_get_encoding(const struct bt_declaration *field); |
| 602 | int bt_ctf_get_array_len(const struct bt_declaration *field); |
| 603 | struct bt_definition *bt_array_index(struct definition_array *array, uint64_t i); |
| 604 | uint64_t bt_ctf_get_uint64(const struct bt_definition *field); |
| 605 | int64_t bt_ctf_get_int64(const struct bt_definition *field); |
| 606 | char *bt_ctf_get_char_array(const struct bt_definition *field); |
| 607 | char *bt_ctf_get_string(const struct bt_definition *field); |
| 608 | int bt_ctf_field_get_error(void); |
| 609 | const char *bt_ctf_get_decl_event_name(const struct bt_ctf_event_decl *event); |
| 610 | const char *bt_ctf_get_decl_field_name(const struct bt_ctf_field_decl *field); |
| 611 | const struct bt_declaration *bt_ctf_get_decl_from_def(const struct bt_definition *field); |
| 612 | uint64_t bt_sequence_len(struct definition_sequence *sequence); |
| 613 | struct bt_definition *bt_sequence_index(struct definition_sequence *sequence, uint64_t i); |
| 614 | |
| 615 | %pythoncode%{ |
| 616 | |
| 617 | class ctf: |
| 618 | |
| 619 | #enum equivalent, accessible constants |
| 620 | #These are taken directly from ctf/events.h |
| 621 | #All changes to enums must also be made here |
| 622 | class type_id: |
| 623 | UNKNOWN = 0 |
| 624 | INTEGER = 1 |
| 625 | FLOAT = 2 |
| 626 | ENUM = 3 |
| 627 | STRING = 4 |
| 628 | STRUCT = 5 |
| 629 | UNTAGGED_VARIANT = 6 |
| 630 | VARIANT = 7 |
| 631 | ARRAY = 8 |
| 632 | SEQUENCE = 9 |
| 633 | NR_CTF_TYPES = 10 |
| 634 | |
| 635 | def get_type_id_name(id): |
| 636 | name = "UNKNOWN" |
| 637 | constants = [attr for attr in dir(ctf.type_id) if not callable(getattr(ctf.type_id, attr)) and not attr.startswith("__")] |
| 638 | for attr in constants: |
| 639 | if getattr(ctf.type_id, attr) == id: |
| 640 | name = attr |
| 641 | break |
| 642 | return name |
| 643 | |
| 644 | class scope: |
| 645 | TRACE_PACKET_HEADER = 0 |
| 646 | STREAM_PACKET_CONTEXT = 1 |
| 647 | STREAM_EVENT_HEADER = 2 |
| 648 | STREAM_EVENT_CONTEXT = 3 |
| 649 | EVENT_CONTEXT = 4 |
| 650 | EVENT_FIELDS = 5 |
| 651 | |
| 652 | class string_encoding: |
| 653 | NONE = 0 |
| 654 | UTF8 = 1 |
| 655 | ASCII = 2 |
| 656 | UNKNOWN = 3 |
| 657 | |
| 658 | class Iterator(Iterator, object): |
| 659 | """ |
| 660 | Allocate a CTF trace collection iterator. |
| 661 | |
| 662 | begin_pos and end_pos are optional parameters to specify the |
| 663 | position at which the trace collection should be seeked upon |
| 664 | iterator creation, and the position at which iteration will |
| 665 | start returning "EOF". |
| 666 | |
| 667 | By default, if begin_pos is None, a SEEK_CUR is performed at |
| 668 | creation. By default, if end_pos is None, a SEEK_END (end of |
| 669 | trace) is the EOF criterion. |
| 670 | |
| 671 | Only one iterator can be created against a context. If more than one |
| 672 | iterator is being created for the same context, the second creation |
| 673 | will return None. The previous iterator must be destroyed before |
| 674 | creation of the new iterator for this function to succeed. |
| 675 | """ |
| 676 | |
| 677 | def __new__(cls, context, begin_pos = None, end_pos = None): |
| 678 | # __new__ is used to control the return value |
| 679 | # as the ctf.Iterator class should return None |
| 680 | # if bt_ctf_iter_create returns NULL |
| 681 | |
| 682 | if begin_pos is None: |
| 683 | bp = None |
| 684 | else: |
| 685 | bp = begin_pos._pos |
| 686 | if end_pos is None: |
| 687 | ep = None |
| 688 | else: |
| 689 | ep = end_pos._pos |
| 690 | try: |
| 691 | it = _bt_ctf_iter_create(context._c, bp, ep) |
| 692 | except AttributeError: |
| 693 | raise TypeError("in __init__, " |
| 694 | "argument 2 must be a Context instance") |
| 695 | if it is None: |
| 696 | return None |
| 697 | |
| 698 | ret_class = super(ctf.Iterator, cls).__new__(cls) |
| 699 | ret_class._i = it |
| 700 | return ret_class |
| 701 | |
| 702 | def __init__(self, context, begin_pos = None, end_pos = None): |
| 703 | Iterator.__init__(self, None, None, None, |
| 704 | _bt_ctf_get_iter(self._i)) |
| 705 | |
| 706 | def __del__(self): |
| 707 | _bt_ctf_iter_destroy(self._i) |
| 708 | |
| 709 | def read_event(self): |
| 710 | """ |
| 711 | Read the iterator's current event data. |
| 712 | Return current event on success, None on end of trace. |
| 713 | """ |
| 714 | ret = _bt_ctf_iter_read_event(self._i) |
| 715 | if ret is None: |
| 716 | return ret |
| 717 | ev = ctf.Event.__new__(ctf.Event) |
| 718 | ev._e = ret |
| 719 | return ev |
| 720 | |
| 721 | |
| 722 | class Event(object): |
| 723 | """ |
| 724 | This class represents an event from the trace. |
| 725 | It is obtained with read_event() from ctf.Iterator. |
| 726 | Do not instantiate. |
| 727 | """ |
| 728 | |
| 729 | def __init__(self): |
| 730 | raise NotImplementedError("ctf.Event cannot be instantiated") |
| 731 | |
| 732 | def get_top_level_scope(self, scope): |
| 733 | """ |
| 734 | Return a definition of the top-level scope |
| 735 | Top-level scopes are defined in ctf.scope. |
| 736 | In order to get a field or a field list, the user needs to pass a |
| 737 | scope as argument, this scope can be a top-level scope or a scope |
| 738 | relative to an arbitrary field. This function provides the mapping |
| 739 | between the scope and the actual definition of top-level scopes. |
| 740 | On error return None. |
| 741 | """ |
| 742 | evDef = ctf.Definition.__new__(ctf.Definition) |
| 743 | evDef._d = _bt_ctf_get_top_level_scope(self._e, scope) |
| 744 | if evDef._d is None: |
| 745 | return None |
| 746 | return evDef |
| 747 | |
| 748 | def get_name(self): |
| 749 | """Return the name of the event or None on error.""" |
| 750 | return _bt_ctf_event_name(self._e) |
| 751 | |
| 752 | def get_cycles(self): |
| 753 | """ |
| 754 | Return the timestamp of the event as written in |
| 755 | the packet (in cycles) or -1ULL on error. |
| 756 | """ |
| 757 | return _bt_ctf_get_cycles(self._e) |
| 758 | |
| 759 | def get_timestamp(self): |
| 760 | """ |
| 761 | Return the timestamp of the event offsetted with the |
| 762 | system clock source or -1ULL on error. |
| 763 | """ |
| 764 | return _bt_ctf_get_timestamp(self._e) |
| 765 | |
| 766 | def get_field_with_scope(self, scope, field): |
| 767 | """ |
| 768 | Return the definition of a specific field. |
| 769 | Return None on error. |
| 770 | """ |
| 771 | evDef = ctf.Definition.__new__(ctf.Definition) |
| 772 | try: |
| 773 | evDef._d = _bt_ctf_get_field(self._e, scope._d, field) |
| 774 | except AttributeError: |
| 775 | raise TypeError("in get_field, argument 2 must be a " |
| 776 | "Definition (scope) instance") |
| 777 | if evDef._d is None: |
| 778 | return None |
| 779 | evDef._s = scope |
| 780 | return evDef |
| 781 | |
| 782 | def get_field(self, field): |
| 783 | """ |
| 784 | Return the definition of fields by a name |
| 785 | Return None on error |
| 786 | """ |
| 787 | eventScope = self.get_top_level_scope(ctf.scope.EVENT_FIELDS) |
| 788 | streamScope = self.get_top_level_scope(ctf.scope.STREAM_EVENT_CONTEXT) |
| 789 | fields_by_name = [] |
| 790 | |
| 791 | if eventScope is not None: |
| 792 | evDef = self.get_field_with_scope(eventScope, field) |
| 793 | if evDef is not None: |
| 794 | fields_by_name.append(evDef) |
| 795 | |
| 796 | if streamScope is not None: |
| 797 | evDef = self.get_field_with_scope(streamScope, field) |
| 798 | if evDef is not None: |
| 799 | fields_by_name.append(evDef); |
| 800 | |
| 801 | if not fields_by_name: |
| 802 | return None |
| 803 | return fields_by_name |
| 804 | |
| 805 | def get_field_list_with_scope(self, scope): |
| 806 | """ |
| 807 | Return a list of Definitions associated with the scope |
| 808 | Return None on error. |
| 809 | """ |
| 810 | try: |
| 811 | field_lc = _bt_python_field_listcaller(self._e, scope._d) |
| 812 | except AttributeError: |
| 813 | raise TypeError("in get_field_list, argument 2 must be a " |
| 814 | "Definition (scope) instance") |
| 815 | |
| 816 | if field_lc is None: |
| 817 | return None |
| 818 | |
| 819 | def_list = [] |
| 820 | i = 0 |
| 821 | while True: |
| 822 | tmp = ctf.Definition.__new__(ctf.Definition) |
| 823 | tmp._d = _bt_python_field_one_from_list(field_lc, i) |
| 824 | |
| 825 | if tmp._d is None: |
| 826 | #Last item of list is None, assured in |
| 827 | #_bt_python_field_listcaller |
| 828 | break |
| 829 | |
| 830 | tmp._s = scope |
| 831 | def_list.append(tmp) |
| 832 | i += 1 |
| 833 | return def_list |
| 834 | |
| 835 | def get_field_list(self): |
| 836 | """Return a list of Definitions or None on error.""" |
| 837 | eventScope = self.get_top_level_scope(ctf.scope.EVENT_FIELDS) |
| 838 | streamScope = self.get_top_level_scope(ctf.scope.STREAM_EVENT_CONTEXT) |
| 839 | |
| 840 | def_list = [] |
| 841 | if eventScope is not None: |
| 842 | event_field_list = self.get_field_list_with_scope(eventScope) |
| 843 | if event_field_list is not None: |
| 844 | def_list = event_field_list |
| 845 | |
| 846 | if streamScope is not None: |
| 847 | event_field_list = self.get_field_list_with_scope(streamScope) |
| 848 | if event_field_list is not None: |
| 849 | def_list.extend(event_field_list) |
| 850 | |
| 851 | if not def_list: |
| 852 | return None |
| 853 | return def_list |
| 854 | |
| 855 | def get_index(self, field, index): |
| 856 | """ |
| 857 | If the field is an array or a sequence, return the element |
| 858 | at position index, otherwise return None |
| 859 | """ |
| 860 | evDef = ctf.Definition.__new__(ctf.Definition) |
| 861 | try: |
| 862 | evDef._d = _bt_ctf_get_index(self._e, field._d, index) |
| 863 | except AttributeError: |
| 864 | raise TypeError("in get_index, argument 2 must be a " |
| 865 | "Definition (field) instance") |
| 866 | |
| 867 | if evDef._d is None: |
| 868 | return None |
| 869 | return evDef |
| 870 | |
| 871 | def get_handle(self): |
| 872 | """ |
| 873 | Get the TraceHandle associated with an event |
| 874 | Return None on error |
| 875 | """ |
| 876 | ret = _bt_ctf_event_get_handle_id(self._e) |
| 877 | if ret < 0: |
| 878 | return None |
| 879 | |
| 880 | th = TraceHandle.__new__(TraceHandle) |
| 881 | th._id = ret |
| 882 | return th |
| 883 | |
| 884 | def get_context(self): |
| 885 | """ |
| 886 | Get the context associated with an event. |
| 887 | Return None on error. |
| 888 | """ |
| 889 | ctx = Context() |
| 890 | ctx._c = _bt_ctf_event_get_context(self._e); |
| 891 | if ctx._c is None: |
| 892 | return None |
| 893 | else: |
| 894 | return ctx |
| 895 | |
| 896 | class FieldError(Exception): |
| 897 | def __init__(self, value): |
| 898 | self.value = value |
| 899 | |
| 900 | def __str__(self): |
| 901 | return repr(self.value) |
| 902 | |
| 903 | class Definition(object): |
| 904 | """Definition class. Do not instantiate.""" |
| 905 | |
| 906 | def __init__(self): |
| 907 | raise NotImplementedError("ctf.Definition cannot be instantiated") |
| 908 | |
| 909 | def __repr__(self): |
| 910 | return "Babeltrace Definition: name('{0}'), type({1})".format( |
| 911 | self.field_name(), self.field_type()) |
| 912 | |
| 913 | def field_name(self): |
| 914 | """Return the name of a field or None on error.""" |
| 915 | return _bt_ctf_field_name(self._d) |
| 916 | |
| 917 | def field_type(self): |
| 918 | """Return the type of a field or -1 if unknown.""" |
| 919 | return _bt_ctf_field_type(_bt_ctf_get_decl_from_def(self._d)) |
| 920 | |
| 921 | def get_int_signedness(self): |
| 922 | """ |
| 923 | Return the signedness of an integer: |
| 924 | 0 if unsigned; 1 if signed; -1 on error. |
| 925 | """ |
| 926 | return _bt_ctf_get_int_signedness(_bt_ctf_get_decl_from_def(self._d)) |
| 927 | |
| 928 | def get_int_base(self): |
| 929 | """Return the base of an int or a negative value on error.""" |
| 930 | return _bt_ctf_get_int_base(_bt_ctf_get_decl_from_def(self._d)) |
| 931 | |
| 932 | def get_int_byte_order(self): |
| 933 | """ |
| 934 | Return the byte order of an int or a negative |
| 935 | value on error. |
| 936 | """ |
| 937 | return _bt_ctf_get_int_byte_order(_bt_ctf_get_decl_from_def(self._d)) |
| 938 | |
| 939 | def get_int_len(self): |
| 940 | """ |
| 941 | Return the size, in bits, of an int or a negative |
| 942 | value on error. |
| 943 | """ |
| 944 | return _bt_ctf_get_int_len(_bt_ctf_get_decl_from_def(self._d)) |
| 945 | |
| 946 | def get_enum_str(self): |
| 947 | """ |
| 948 | Return the string matching the current enumeration. |
| 949 | Return None on error. |
| 950 | """ |
| 951 | return _bt_ctf_get_enum_str(self._d) |
| 952 | |
| 953 | def get_encoding(self): |
| 954 | """ |
| 955 | Return the encoding of an int or a string. |
| 956 | Return a negative value on error. |
| 957 | """ |
| 958 | return _bt_ctf_get_encoding(_bt_ctf_get_decl_from_def(self._d)) |
| 959 | |
| 960 | def get_array_len(self): |
| 961 | """ |
| 962 | Return the len of an array or a negative |
| 963 | value on error. |
| 964 | """ |
| 965 | return _bt_ctf_get_array_len(_bt_ctf_get_decl_from_def(self._d)) |
| 966 | |
| 967 | def get_array_element_at(self, index): |
| 968 | """ |
| 969 | Return the array's element at position index. |
| 970 | Return None on error |
| 971 | """ |
| 972 | array = _bt_python_get_array_from_def(self._d) |
| 973 | if array is None: |
| 974 | return None |
| 975 | |
| 976 | element = ctf.Definition.__new__(ctf.Definition) |
| 977 | element._d = _bt_array_index(array, index) |
| 978 | if element._d is None: |
| 979 | return None |
| 980 | return element |
| 981 | |
| 982 | def get_sequence_len(self): |
| 983 | """ |
| 984 | Return the len of a sequence or a negative |
| 985 | value on error. |
| 986 | """ |
| 987 | seq = _bt_python_get_sequence_from_def(self._d) |
| 988 | return _bt_sequence_len(seq) |
| 989 | |
| 990 | def get_sequence_element_at(self, index): |
| 991 | """ |
| 992 | Return the sequence's element at position index, |
| 993 | otherwise return None |
| 994 | """ |
| 995 | seq = _bt_python_get_sequence_from_def(self._d) |
| 996 | if seq is not None: |
| 997 | element = ctf.Definition.__new__(ctf.Definition) |
| 998 | element._d = _bt_sequence_index(seq, index) |
| 999 | if element._d is not None: |
| 1000 | return element |
| 1001 | return None |
| 1002 | |
| 1003 | def get_uint64(self): |
| 1004 | """ |
| 1005 | Return the value associated with the field. |
| 1006 | If the field does not exist or is not of the type requested, |
| 1007 | the value returned is undefined. To check if an error occured, |
| 1008 | use the ctf.field_error() function after accessing a field. |
| 1009 | """ |
| 1010 | return _bt_ctf_get_uint64(self._d) |
| 1011 | |
| 1012 | def get_int64(self): |
| 1013 | """ |
| 1014 | Return the value associated with the field. |
| 1015 | If the field does not exist or is not of the type requested, |
| 1016 | the value returned is undefined. To check if an error occured, |
| 1017 | use the ctf.field_error() function after accessing a field. |
| 1018 | """ |
| 1019 | return _bt_ctf_get_int64(self._d) |
| 1020 | |
| 1021 | def get_char_array(self): |
| 1022 | """ |
| 1023 | Return the value associated with the field. |
| 1024 | If the field does not exist or is not of the type requested, |
| 1025 | the value returned is undefined. To check if an error occured, |
| 1026 | use the ctf.field_error() function after accessing a field. |
| 1027 | """ |
| 1028 | return _bt_ctf_get_char_array(self._d) |
| 1029 | |
| 1030 | def get_str(self): |
| 1031 | """ |
| 1032 | Return the value associated with the field. |
| 1033 | If the field does not exist or is not of the type requested, |
| 1034 | the value returned is undefined. To check if an error occured, |
| 1035 | use the ctf.field_error() function after accessing a field. |
| 1036 | """ |
| 1037 | return _bt_ctf_get_string(self._d) |
| 1038 | |
| 1039 | def get_value(self): |
| 1040 | """ |
| 1041 | Return the value associated with the field according to its type. |
| 1042 | Return None on error. |
| 1043 | """ |
| 1044 | id = self.field_type() |
| 1045 | value = None |
| 1046 | if id == ctf.type_id.STRING: |
| 1047 | value = self.get_str() |
| 1048 | elif id == ctf.type_id.ARRAY: |
| 1049 | value = [] |
| 1050 | for i in range(self.get_array_len()): |
| 1051 | element = self.get_array_element_at(i) |
| 1052 | value.append(element.get_value()) |
| 1053 | elif id == ctf.type_id.INTEGER: |
| 1054 | if self.get_int_signedness() == 0: |
| 1055 | value = self.get_uint64() |
| 1056 | else: |
| 1057 | value = self.get_int64() |
| 1058 | elif id == ctf.type_id.ENUM: |
| 1059 | value = self.get_enum_str() |
| 1060 | elif id == ctf.type_id.SEQUENCE: |
| 1061 | seq_len = self.get_sequence_len() |
| 1062 | value = [] |
| 1063 | for i in range(seq_len): |
| 1064 | evDef = self.get_sequence_element_at(i) |
| 1065 | value.append(evDef.get_value()) |
| 1066 | if ctf.field_error(): |
| 1067 | raise ctf.FieldError("Error occured while accessing field {} of type {}".format(self.field_name(), ctf.type_id.get_type_id_name(self.field_type()))) |
| 1068 | return value |
| 1069 | |
| 1070 | def get_scope(self): |
| 1071 | """Return the scope of a field or None on error.""" |
| 1072 | return self._s |
| 1073 | |
| 1074 | class EventDecl(object): |
| 1075 | """Event declaration class. Do not instantiate.""" |
| 1076 | |
| 1077 | def __init__(self): |
| 1078 | raise NotImplementedError("ctf.EventDecl cannot be instantiated") |
| 1079 | |
| 1080 | def __repr__(self): |
| 1081 | return "Babeltrace EventDecl: name {0}".format(self.get_name()) |
| 1082 | |
| 1083 | def get_name(self): |
| 1084 | """Return the name of the event or None on error""" |
| 1085 | return _bt_ctf_get_decl_event_name(self._d) |
| 1086 | |
| 1087 | def get_decl_fields(self, scope): |
| 1088 | """ |
| 1089 | Return a list of ctf.FieldDecl |
| 1090 | Return None on error. |
| 1091 | """ |
| 1092 | ptr_list = _by_python_field_decl_listcaller(self._d, scope) |
| 1093 | |
| 1094 | if ptr_list is None: |
| 1095 | return None |
| 1096 | |
| 1097 | decl_list = [] |
| 1098 | i = 0 |
| 1099 | while True: |
| 1100 | tmp = ctf.FieldDecl.__new__(ctf.FieldDecl) |
| 1101 | tmp._d = _bt_python_field_decl_one_from_list( |
| 1102 | ptr_list, i) |
| 1103 | |
| 1104 | if tmp._d is None: |
| 1105 | #Last item of list is None |
| 1106 | break |
| 1107 | |
| 1108 | decl_list.append(tmp) |
| 1109 | i += 1 |
| 1110 | return decl_list |
| 1111 | |
| 1112 | |
| 1113 | class FieldDecl(object): |
| 1114 | """Field declaration class. Do not instantiate.""" |
| 1115 | |
| 1116 | def __init__(self): |
| 1117 | raise NotImplementedError("ctf.FieldDecl cannot be instantiated") |
| 1118 | |
| 1119 | def __repr__(self): |
| 1120 | return "Babeltrace FieldDecl: name {0}".format(self.get_name()) |
| 1121 | |
| 1122 | def get_name(self): |
| 1123 | """Return the name of a FieldDecl or None on error""" |
| 1124 | return _bt_ctf_get_decl_field_name(self._d) |
| 1125 | |
| 1126 | |
| 1127 | @staticmethod |
| 1128 | def field_error(): |
| 1129 | """ |
| 1130 | Return the last error code encountered while |
| 1131 | accessing a field and reset the error flag. |
| 1132 | Return 0 if no error, a negative value otherwise. |
| 1133 | """ |
| 1134 | return _bt_ctf_field_get_error() |
| 1135 | |
| 1136 | @staticmethod |
| 1137 | def get_event_decl_list(trace_handle, context): |
| 1138 | """ |
| 1139 | Return a list of ctf.EventDecl |
| 1140 | Return None on error. |
| 1141 | """ |
| 1142 | try: |
| 1143 | handle_id = trace_handle._id |
| 1144 | except AttributeError: |
| 1145 | raise TypeError("in get_event_decl_list, " |
| 1146 | "argument 1 must be a TraceHandle instance") |
| 1147 | try: |
| 1148 | ptr_list = _bt_python_event_decl_listcaller(handle_id, context._c) |
| 1149 | except AttributeError: |
| 1150 | raise TypeError("in get_event_decl_list, " |
| 1151 | "argument 2 must be a Context instance") |
| 1152 | |
| 1153 | if ptr_list is None: |
| 1154 | return None |
| 1155 | |
| 1156 | decl_list = [] |
| 1157 | i = 0 |
| 1158 | while True: |
| 1159 | tmp = ctf.EventDecl.__new__(ctf.EventDecl) |
| 1160 | tmp._d = _bt_python_decl_one_from_list(ptr_list, i) |
| 1161 | |
| 1162 | if tmp._d is None: |
| 1163 | #Last item of list is None |
| 1164 | break |
| 1165 | |
| 1166 | decl_list.append(tmp) |
| 1167 | i += 1 |
| 1168 | return decl_list |
| 1169 | |
| 1170 | %} |
| 1171 | |
| 1172 | |
| 1173 | |
| 1174 | // ================================================================= |
| 1175 | // NEW FUNCTIONS |
| 1176 | // File and list-related |
| 1177 | // python-complements.h |
| 1178 | // ================================================================= |
| 1179 | |
| 1180 | %include python-complements.c |
| 1181 | |
| 1182 | %pythoncode %{ |
| 1183 | |
| 1184 | class File(object): |
| 1185 | """ |
| 1186 | Open a file for babeltrace. |
| 1187 | |
| 1188 | file_path is a string containing the path or None to use the |
| 1189 | standard output in writing mode. |
| 1190 | |
| 1191 | The mode can be 'r', 'w' or 'a' for reading (default), writing or |
| 1192 | appending. The file will be created if it doesn't exist when |
| 1193 | opened for writing or appending; it will be truncated when opened |
| 1194 | for writing. Add a 'b' to the mode for binary files. Add a '+' |
| 1195 | to the mode to allow simultaneous reading and writing. |
| 1196 | """ |
| 1197 | |
| 1198 | def __new__(cls, file_path, mode='r'): |
| 1199 | # __new__ is used to control the return value |
| 1200 | # as the File class should return None |
| 1201 | # if _bt_file_open returns NULL |
| 1202 | |
| 1203 | # Type check |
| 1204 | if file_path is not None and type(file_path) is not str: |
| 1205 | raise TypeError("in method __init__, argument 2 of type 'str'") |
| 1206 | if type(mode) is not str: |
| 1207 | raise TypeError("in method __init__, argument 3 of type 'str'") |
| 1208 | |
| 1209 | # Opening file |
| 1210 | file_ptr = _bt_file_open(file_path, mode) |
| 1211 | if file_ptr is None: |
| 1212 | return None |
| 1213 | |
| 1214 | # Class instantiation |
| 1215 | file_inst = super(File, cls).__new__(cls) |
| 1216 | file_inst._file = file_ptr |
| 1217 | return file_inst |
| 1218 | |
| 1219 | def __init__(self, file_path, mode='r'): |
| 1220 | self._opened = True |
| 1221 | self._use_stdout = False |
| 1222 | |
| 1223 | if file_path is None: |
| 1224 | # use stdout |
| 1225 | file_path = "stdout" |
| 1226 | mode = 'w' |
| 1227 | self._use_stdout = True |
| 1228 | |
| 1229 | self._file_path = file_path |
| 1230 | self._mode = mode |
| 1231 | |
| 1232 | def __del__(self): |
| 1233 | self.close() |
| 1234 | |
| 1235 | def __repr__(self): |
| 1236 | if self._opened: |
| 1237 | stat = 'opened' |
| 1238 | else: |
| 1239 | stat = 'closed' |
| 1240 | return "{0} babeltrace File; file_path('{1}'), mode('{2}')".format( |
| 1241 | stat, self._file_path, self._mode) |
| 1242 | |
| 1243 | def close(self): |
| 1244 | """Close the file. Is also called using del.""" |
| 1245 | if self._opened and not self._use_stdout: |
| 1246 | _bt_file_close(self._file) |
| 1247 | self._opened = False |
| 1248 | %} |