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