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