b431e21e428e3a569e57a65f33b9239848f0340f
[babeltrace.git] / bindings / python / bt.py
1 # nativebt.i.in
2 #
3 # Babeltrace native interface Python module
4 #
5 # Copyright 2012-2015 EfficiOS Inc.
6 #
7 # Author: Danny Serres <danny.serres@efficios.com>
8 # Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining a copy
11 # of this software and associated documentation files (the "Software"), to deal
12 # in the Software without restriction, including without limitation the rights
13 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 # copies of the Software, and to permit persons to whom the Software is
15 # furnished to do so, subject to the following conditions:
16 #
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
19 #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 # SOFTWARE.
27
28 import babeltrace.nativebt as nbt
29 import collections
30 import os
31 from datetime import datetime
32 from uuid import UUID
33
34
35 class TraceCollection:
36 """
37 A :class:`TraceCollection` is a collection of opened traces.
38
39 In general, once a trace collection is created, you add one to many
40 independent traces to it using :meth:`add_trace` or
41 :meth:`add_traces_recursive`, and then iterate the ordered events
42 of all traces merged together using :attr:`events`.
43
44 You may use :meth:`remove_trace` to close and remove a specific
45 trace from a trace collection, although all the traces of a given
46 trace collection will be automatically removed when it is garbage
47 collected.
48 """
49
50 def __init__(self):
51 """
52 Creates an empty trace collection.
53 """
54
55 self._tc = nbt._bt_context_create()
56
57 def __del__(self):
58 nbt._bt_context_put(self._tc)
59
60 def add_trace(self, path, format_str):
61 """
62 Adds a trace to the trace collection.
63
64 The trace is located at the file system path *path*. This
65 function **does not** recurse directories to find the trace:
66 *path* must point to the exact trace location (see
67 :meth:`add_traces_recursive` for a recursive version of this
68 function).
69
70 *format_str* is a string indicating the Babeltrace type of the
71 trace to add. ``ctf`` is the only currently supported trace
72 format.
73
74 Once added, the trace is opened.
75
76 Returns the corresponding :class:`TraceHandle` instance for
77 this opened trace on success, or ``None`` on error.
78 """
79
80 ret = nbt._bt_context_add_trace(self._tc, path, format_str,
81 None, None, None)
82
83 if ret < 0:
84 return None
85
86 th = TraceHandle.__new__(TraceHandle)
87 th._id = ret
88 th._trace_collection = self
89
90 return th
91
92 def add_traces_recursive(self, path, format_str):
93 """
94 Adds traces to this trace collection by recursively searching
95 in the *path* directory.
96
97 *format_str* is a string indicating the Babeltrace type of the
98 traces to find and add. ``ctf`` is the only currently supported
99 trace format.
100
101 See also :meth:`add_trace`.
102
103 Returns a :class:`dict` object mapping full paths to trace
104 handles for each trace found, or ``None`` on error.
105 """
106
107 trace_handles = {}
108 noTrace = True
109 error = False
110
111 for fullpath, dirs, files in os.walk(path):
112 if "metadata" in files:
113 trace_handle = self.add_trace(fullpath, format_str)
114
115 if trace_handle is None:
116 error = True
117 continue
118
119 trace_handles[fullpath] = trace_handle
120 noTrace = False
121
122 if noTrace and error:
123 return None
124
125 return trace_handles
126
127 def remove_trace(self, trace_handle):
128 """
129 Removes a trace from the trace collection using its trace
130 handle *trace_handle*.
131
132 :class:`TraceHandle` objects are returned by :meth:`add_trace`
133 and :meth:`add_traces_recursive`.
134
135 The trace is closed before being removed.
136 """
137
138 try:
139 nbt._bt_context_remove_trace(self._tc, trace_handle._id)
140 except AttributeError:
141 raise TypeError("in remove_trace, argument 2 must be a TraceHandle instance")
142
143 @property
144 def events(self):
145 """
146 Generates the ordered :class:`Event` objects of all the opened
147 traces contained in this trace collection. Iterate this function
148 to iterate actual events.
149
150 Due to limitations of the native Babeltrace API, only one event
151 may be "alive" at a given time, i.e. a user **should never**
152 store a copy of the events returned by this function for
153 ulterior use. Users shall make sure to copy the information
154 they need *from* an event before accessing the next one.
155
156 Furthermore, :class:`Event` objects become invalid when the
157 generator goes out of scope as the underlying iterator will be
158 reclaimed. Using an event after the the generator has gone out
159 of scope may result in a crash or data corruption.
160 """
161
162 begin_pos_ptr = nbt._bt_iter_pos()
163 end_pos_ptr = nbt._bt_iter_pos()
164 begin_pos_ptr.type = nbt.SEEK_BEGIN
165 end_pos_ptr.type = nbt.SEEK_LAST
166
167 for event in self._events(begin_pos_ptr, end_pos_ptr):
168 yield event
169
170 def events_timestamps(self, timestamp_begin, timestamp_end):
171 """
172 Generates the ordered :class:`Event` objects of all the opened
173 traces contained in this trace collection from *timestamp_begin*
174 to *timestamp_end*.
175
176 See :attr:`events` for notes and limitations.
177 """
178
179 begin_pos_ptr = nbt._bt_iter_pos()
180 end_pos_ptr = nbt._bt_iter_pos()
181 begin_pos_ptr.type = end_pos_ptr.type = nbt.SEEK_TIME
182 begin_pos_ptr.u.seek_time = timestamp_begin
183 end_pos_ptr.u.seek_time = timestamp_end
184
185 for event in self._events(begin_pos_ptr, end_pos_ptr):
186 yield event
187
188 @property
189 def timestamp_begin(self):
190 """
191 Trace collection's begin timestamp.
192 """
193
194 pos_ptr = nbt._bt_iter_pos()
195 pos_ptr.type = nbt.SEEK_BEGIN
196
197 return self._timestamp_at_pos(pos_ptr)
198
199 @property
200 def timestamp_end(self):
201 """
202 Trace collection's end timestamp.
203 """
204
205 pos_ptr = nbt._bt_iter_pos()
206 pos_ptr.type = nbt.SEEK_LAST
207
208 return self._timestamp_at_pos(pos_ptr)
209
210 def _timestamp_at_pos(self, pos_ptr):
211 ctf_it_ptr = nbt._bt_ctf_iter_create(self._tc, pos_ptr, pos_ptr)
212
213 if ctf_it_ptr is None:
214 raise NotImplementedError("Creation of multiple iterators is unsupported.")
215
216 ev_ptr = nbt._bt_ctf_iter_read_event(ctf_it_ptr)
217 nbt._bt_ctf_iter_destroy(ctf_it_ptr)
218
219 def _events(self, begin_pos_ptr, end_pos_ptr):
220 ctf_it_ptr = nbt._bt_ctf_iter_create(self._tc, begin_pos_ptr, end_pos_ptr)
221
222 if ctf_it_ptr is None:
223 raise NotImplementedError("Creation of multiple iterators is unsupported.")
224
225 while True:
226 ev_ptr = nbt._bt_ctf_iter_read_event(ctf_it_ptr)
227
228 if ev_ptr is None:
229 break
230
231 ev = Event.__new__(Event)
232 ev._e = ev_ptr
233
234 try:
235 yield ev
236 except GeneratorExit:
237 break
238
239 ret = nbt._bt_iter_next(nbt._bt_ctf_get_iter(ctf_it_ptr))
240
241 if ret != 0:
242 break
243
244 nbt._bt_ctf_iter_destroy(ctf_it_ptr)
245
246
247 # Based on enum bt_clock_type in clock-type.h
248 class _ClockType:
249 CLOCK_CYCLES = 0
250 CLOCK_REAL = 1
251
252
253 class TraceHandle:
254 """
255 A :class:`TraceHandle` is a handle allowing the user to manipulate
256 a specific trace directly. It is a unique identifier representing a
257 trace, and is not meant to be instantiated by the user.
258 """
259
260 def __init__(self):
261 raise NotImplementedError("TraceHandle cannot be instantiated")
262
263 def __repr__(self):
264 return "Babeltrace TraceHandle: trace_id('{0}')".format(self._id)
265
266 @property
267 def id(self):
268 """
269 Trace handle's numeric ID.
270 """
271
272 return self._id
273
274 @property
275 def path(self):
276 """
277 Path of the underlying trace.
278 """
279
280 return nbt._bt_trace_handle_get_path(self._trace_collection._tc,
281 self._id)
282
283 @property
284 def timestamp_begin(self):
285 """
286 Buffers creation timestamp (nanoseconds since Epoch) of the
287 underlying trace.
288 """
289
290 return nbt._bt_trace_handle_get_timestamp_begin(self._trace_collection._tc,
291 self._id,
292 _ClockType.CLOCK_REAL)
293
294 @property
295 def timestamp_end(self):
296 """
297 Buffers destruction timestamp (nanoseconds since Epoch) of the
298 underlying trace.
299 """
300
301 return nbt._bt_trace_handle_get_timestamp_end(self._trace_collection._tc,
302 self._id,
303 _ClockType.CLOCK_REAL)
304
305 @property
306 def events(self):
307 """
308 Generates all the :class:`EventDeclaration` objects of the
309 underlying trace.
310
311 Note that this doesn't generate actual trace *events*, but
312 rather their declarations, i.e. their layouts and metadata.
313 """
314
315 ret = nbt._bt_python_event_decl_listcaller(self.id,
316 self._trace_collection._tc)
317
318 if not isinstance(ret, list):
319 return
320
321 ptr_list, count = ret
322
323 for i in range(count):
324 tmp = EventDeclaration.__new__(EventDeclaration)
325 tmp._ed = nbt._bt_python_decl_one_from_list(ptr_list, i)
326 yield tmp
327
328
329 class CTFStringEncoding:
330 """
331 CTF string encodings.
332 """
333
334 #: None
335 NONE = 0
336
337 #: UTF-8
338 UTF8 = 1
339
340 #: ASCII
341 ASCII = 2
342
343 #: Unknown
344 UNKNOWN = 3
345
346
347 # Based on the enum in ctf-writer/writer.h
348 class ByteOrder:
349 """
350 Byte orders.
351 """
352
353 #: Native byte order
354 BYTE_ORDER_NATIVE = 0
355
356 #: Little-endian
357 BYTE_ORDER_LITTLE_ENDIAN = 1
358
359 #: Big-endian
360 BYTE_ORDER_BIG_ENDIAN = 2
361
362 #: Network byte order (big-endian)
363 BYTE_ORDER_NETWORK = 3
364
365 #: Unknown byte order
366 BYTE_ORDER_UNKNOWN = 4 # Python-specific entry
367
368
369 # enum equivalent, accessible constants
370 # These are taken directly from ctf/events.h
371 # All changes to enums must also be made here
372 class CTFTypeId:
373 """
374 CTF numeric type identifiers.
375 """
376
377 #: Unknown type
378 UNKNOWN = 0
379
380 #: Integer
381 INTEGER = 1
382
383 #: Floating point number
384 FLOAT = 2
385
386 #: Enumeration
387 ENUM = 3
388
389 #: String
390 STRING = 4
391
392 #: Structure
393 STRUCT = 5
394
395 #: Untagged variant
396 UNTAGGED_VARIANT = 6
397
398 #: Variant
399 VARIANT = 7
400
401 #: Array
402 ARRAY = 8
403
404 #: Sequence
405 SEQUENCE = 9
406
407 NR_CTF_TYPES = 10
408
409 def type_name(id):
410 """
411 Returns the name of the CTF numeric type identifier *id*.
412 """
413
414 name = "UNKNOWN_TYPE"
415 constants = [
416 attr for attr in dir(CTFTypeId) if not callable(
417 getattr(
418 CTFTypeId,
419 attr)) and not attr.startswith("__")]
420
421 for attr in constants:
422 if getattr(CTFTypeId, attr) == id:
423 name = attr
424 break
425
426 return name
427
428
429 class CTFScope:
430 """
431 CTF scopes.
432 """
433
434 #: Packet header
435 TRACE_PACKET_HEADER = 0
436
437 #: Packet context
438 STREAM_PACKET_CONTEXT = 1
439
440 #: Event header
441 STREAM_EVENT_HEADER = 2
442
443 #: Stream event context
444 STREAM_EVENT_CONTEXT = 3
445
446 #: Event context
447 EVENT_CONTEXT = 4
448
449 #: Event fields
450 EVENT_FIELDS = 5
451
452 def scope_name(scope):
453 """
454 Returns the name of the CTF scope *scope*.
455 """
456
457 name = "UNKNOWN_SCOPE"
458 constants = [
459 attr for attr in dir(CTFScope) if not callable(
460 getattr(
461 CTFScope,
462 attr)) and not attr.startswith("__")]
463
464 for attr in constants:
465 if getattr(CTFScope, attr) == scope:
466 name = attr
467 break
468
469 return name
470
471
472 # Priority of the scopes when searching for event fields
473 _scopes = [
474 CTFScope.EVENT_FIELDS,
475 CTFScope.EVENT_CONTEXT,
476 CTFScope.STREAM_EVENT_CONTEXT,
477 CTFScope.STREAM_EVENT_HEADER,
478 CTFScope.STREAM_PACKET_CONTEXT,
479 CTFScope.TRACE_PACKET_HEADER
480 ]
481
482
483 class Event(collections.Mapping):
484 """
485 An :class:`Event` object represents a trace event. :class:`Event`
486 objects are returned by :attr:`TraceCollection.events` and are
487 not meant to be instantiated by the user.
488
489 :class:`Event` has a :class:`dict`-like interface for accessing
490 an event's field value by field name:
491
492 .. code-block:: python
493
494 event['my_field']
495
496 If a field name exists in multiple scopes, the value of the first
497 field found is returned. The scopes are searched in the following
498 order:
499
500 1. Event fields (:attr:`CTFScope.EVENT_FIELDS`)
501 2. Event context (:attr:`CTFScope.EVENT_CONTEXT`)
502 3. Stream event context (:attr:`CTFScope.STREAM_EVENT_CONTEXT`)
503 4. Event header (:attr:`CTFScope.STREAM_EVENT_HEADER`)
504 5. Packet context (:attr:`CTFScope.STREAM_PACKET_CONTEXT`)
505 6. Packet header (:attr:`CTFScope.TRACE_PACKET_HEADER`)
506
507 It is still possible to obtain a field's value from a specific
508 scope using :meth:`field_with_scope`.
509
510 Field values are returned as native Python types, that is:
511
512 +-----------------------+----------------------------------+
513 | Field type | Python type |
514 +=======================+==================================+
515 | Integer | :class:`int` |
516 +-----------------------+----------------------------------+
517 | Floating point number | :class:`float` |
518 +-----------------------+----------------------------------+
519 | Enumeration | :class:`str` (enumeration label) |
520 +-----------------------+----------------------------------+
521 | String | :class:`str` |
522 +-----------------------+----------------------------------+
523 | Array | :class:`list` of native Python |
524 | | objects |
525 +-----------------------+----------------------------------+
526 | Sequence | :class:`list` of native Python |
527 | | objects |
528 +-----------------------+----------------------------------+
529 | Structure | :class:`dict` mapping field |
530 | | names to native Python objects |
531 +-----------------------+----------------------------------+
532
533 For example, printing the third element of a sequence named ``seq``
534 in a structure named ``my_struct`` of the ``event``'s field named
535 ``my_field`` is done this way:
536
537 .. code-block:: python
538
539 print(event['my_field']['my_struct']['seq'][2])
540 """
541
542 def __init__(self):
543 raise NotImplementedError("Event cannot be instantiated")
544
545 @property
546 def name(self):
547 """
548 Event's name or ``None`` on error.
549 """
550
551 return nbt._bt_ctf_event_name(self._e)
552
553 @property
554 def cycles(self):
555 """
556 Event's timestamp in cycles or -1 on error.
557 """
558
559 return nbt._bt_ctf_get_cycles(self._e)
560
561 @property
562 def timestamp(self):
563 """
564 Event's timestamp (nanoseconds since Epoch) or -1 on error.
565 """
566
567 return nbt._bt_ctf_get_timestamp(self._e)
568
569 @property
570 def datetime(self):
571 """
572 Event's timestamp as a standard :class:`datetime.datetime`
573 object.
574
575 Note that the :class:`datetime.datetime` class' precision
576 is limited to microseconds, whereas :attr:`timestamp` provides
577 the event's timestamp with a nanosecond resolution.
578 """
579
580 return datetime.fromtimestamp(self.timestamp / 1E9)
581
582 def field_with_scope(self, field_name, scope):
583 """
584 Returns the value of a field named *field_name* within the
585 scope *scope*, or ``None`` if the field cannot be found.
586
587 *scope* must be one of :class:`CTFScope` constants.
588 """
589
590 if scope not in _scopes:
591 raise ValueError("Invalid scope provided")
592
593 field = self._field_with_scope(field_name, scope)
594
595 if field is not None:
596 return field.value
597
598 def field_list_with_scope(self, scope):
599 """
600 Returns a list of field names in the scope *scope*.
601 """
602
603 if scope not in _scopes:
604 raise ValueError("Invalid scope provided")
605
606 field_names = []
607
608 for field in self._field_list_with_scope(scope):
609 field_names.append(field.name)
610
611 return field_names
612
613 @property
614 def handle(self):
615 """
616 :class:`TraceHandle` object containing this event, or ``None``
617 on error.
618 """
619
620 ret = nbt._bt_ctf_event_get_handle_id(self._e)
621
622 if ret < 0:
623 return None
624
625 th = TraceHandle.__new__(TraceHandle)
626 th._id = ret
627 th._trace_collection = self.get_trace_collection()
628
629 return th
630
631 @property
632 def trace_collection(self):
633 """
634 :class:`TraceCollection` object containing this event, or
635 ``None`` on error.
636 """
637
638 trace_collection = TraceCollection()
639 trace_collection._tc = nbt._bt_ctf_event_get_context(self._e)
640
641 if trace_collection._tc is not None:
642 return trace_collection
643
644 def __getitem__(self, field_name):
645 field = self._field(field_name)
646
647 if field is not None:
648 return field.value
649
650 raise KeyError(field_name)
651
652 def __iter__(self):
653 for key in self.keys():
654 yield key
655
656 def __len__(self):
657 count = 0
658
659 for scope in _scopes:
660 scope_ptr = nbt._bt_ctf_get_top_level_scope(self._e, scope)
661 ret = nbt._bt_python_field_listcaller(self._e, scope_ptr)
662
663 if isinstance(ret, list):
664 count += ret[1]
665
666 return count
667
668 def __contains__(self, field_name):
669 return self._field(field_name) is not None
670
671 def keys(self):
672 """
673 Returns the list of field names.
674
675 Note: field names are unique within the returned list, although
676 a field name could exist in multiple scopes. Use
677 :meth:`field_list_with_scope` to obtain the list of field names
678 of a given scope.
679 """
680
681 field_names = set()
682
683 for scope in _scopes:
684 for name in self.field_list_with_scope(scope):
685 field_names.add(name)
686
687 return list(field_names)
688
689 def get(self, field_name, default=None):
690 """
691 Returns the value of the field named *field_name*, or *default*
692 when not found.
693
694 See :class:`Event` note about how fields are retrieved by
695 name when multiple fields share the same name in different
696 scopes.
697 """
698
699 field = self._field(field_name)
700
701 if field is None:
702 return default
703
704 return field.value
705
706 def items(self):
707 """
708 Generates pairs of (field name, field value).
709
710 This method iterates :meth:`keys` to find field names, which
711 means some fields could be unavailable if other fields share
712 their names in scopes with higher priorities.
713 """
714
715 for field in self.keys():
716 yield (field, self[field])
717
718 def _field_with_scope(self, field_name, scope):
719 scope_ptr = nbt._bt_ctf_get_top_level_scope(self._e, scope)
720
721 if scope_ptr is None:
722 return None
723
724 definition_ptr = nbt._bt_ctf_get_field(self._e, scope_ptr, field_name)
725
726 if definition_ptr is None:
727 return None
728
729 field = _Definition(definition_ptr, scope)
730
731 return field
732
733 def _field(self, field_name):
734 field = None
735
736 for scope in _scopes:
737 field = self._field_with_scope(field_name, scope)
738
739 if field is not None:
740 break
741
742 return field
743
744 def _field_list_with_scope(self, scope):
745 fields = []
746 scope_ptr = nbt._bt_ctf_get_top_level_scope(self._e, scope)
747
748 # Returns a list [list_ptr, count]. If list_ptr is NULL, SWIG will only
749 # provide the "count" return value
750 count = 0
751 list_ptr = None
752 ret = nbt._bt_python_field_listcaller(self._e, scope_ptr)
753
754 if isinstance(ret, list):
755 list_ptr, count = ret
756
757 for i in range(count):
758 definition_ptr = nbt._bt_python_field_one_from_list(list_ptr, i)
759
760 if definition_ptr is not None:
761 definition = _Definition(definition_ptr, scope)
762 fields.append(definition)
763
764 return fields
765
766
767 class FieldError(Exception):
768 """
769 Field error, raised when a field's value cannot be accessed.
770 """
771
772 def __init__(self, value):
773 self.value = value
774
775 def __str__(self):
776 return repr(self.value)
777
778
779 class EventDeclaration:
780 """
781 An event declaration contains the properties of a class of events,
782 that is, the common properties and fields layout of all the actual
783 recorded events associated with this declaration.
784
785 This class is not meant to be instantiated by the user. It is
786 returned by :attr:`TraceHandle.events`.
787 """
788
789 MAX_UINT64 = 0xFFFFFFFFFFFFFFFF
790
791 def __init__(self):
792 raise NotImplementedError("EventDeclaration cannot be instantiated")
793
794 @property
795 def name(self):
796 """
797 Event's name, or ``None`` on error.
798 """
799
800 return nbt._bt_ctf_get_decl_event_name(self._ed)
801
802 @property
803 def id(self):
804 """
805 Event's numeric ID, or -1 on error.
806 """
807
808 id = nbt._bt_ctf_get_decl_event_id(self._ed)
809
810 if id == self.MAX_UINT64:
811 id = -1
812
813 return id
814
815 @property
816 def fields(self):
817 """
818 Generates all the event's field declarations, going through
819 each scope in the following order:
820
821 1. Event fields (:attr:`CTFScope.EVENT_FIELDS`)
822 2. Event context (:attr:`CTFScope.EVENT_CONTEXT`)
823 3. Stream event context (:attr:`CTFScope.STREAM_EVENT_CONTEXT`)
824 4. Event header (:attr:`CTFScope.STREAM_EVENT_HEADER`)
825 5. Packet context (:attr:`CTFScope.STREAM_PACKET_CONTEXT`)
826 6. Packet header (:attr:`CTFScope.TRACE_PACKET_HEADER`)
827
828 All the generated field declarations inherit
829 :class:`FieldDeclaration`, and are among:
830
831 * :class:`IntegerFieldDeclaration`
832 * :class:`FloatFieldDeclaration`
833 * :class:`EnumerationFieldDeclaration`
834 * :class:`StringFieldDeclaration`
835 * :class:`ArrayFieldDeclaration`
836 * :class:`SequenceFieldDeclaration`
837 * :class:`StructureFieldDeclaration`
838 * :class:`VariantFieldDeclaration`
839 """
840
841 for scope in _scopes:
842 for declaration in self.fields_scope(scope):
843 yield declaration
844
845 def fields_scope(self, scope):
846 """
847 Generates all the field declarations of the event's scope
848 *scope*.
849
850 *scope* must be one of :class:`CTFScope` constants.
851
852 All the generated field declarations inherit
853 :class:`FieldDeclaration`, and are among:
854
855 * :class:`IntegerFieldDeclaration`
856 * :class:`FloatFieldDeclaration`
857 * :class:`EnumerationFieldDeclaration`
858 * :class:`StringFieldDeclaration`
859 * :class:`ArrayFieldDeclaration`
860 * :class:`SequenceFieldDeclaration`
861 * :class:`StructureFieldDeclaration`
862 * :class:`VariantFieldDeclaration`
863 """
864 ret = nbt._by_python_field_decl_listcaller(self._ed, scope)
865
866 if not isinstance(ret, list):
867 return
868
869 list_ptr, count = ret
870
871 for i in range(count):
872 field_decl_ptr = nbt._bt_python_field_decl_one_from_list(list_ptr, i)
873
874 if field_decl_ptr is not None:
875 decl_ptr = nbt._bt_ctf_get_decl_from_field_decl(field_decl_ptr)
876 name = nbt._bt_ctf_get_decl_field_name(field_decl_ptr)
877 field_declaration = _create_field_declaration(decl_ptr, name,
878 scope)
879 yield field_declaration
880
881
882 class FieldDeclaration:
883 """
884 Base class for concrete field declarations.
885
886 This class is not meant to be instantiated by the user.
887 """
888
889 def __init__(self):
890 raise NotImplementedError("FieldDeclaration cannot be instantiated")
891
892 def __repr__(self):
893 return "({0}) {1} {2}".format(CTFScope.scope_name(self.scope),
894 CTFTypeId.type_name(self.type),
895 self.name)
896
897 @property
898 def name(self):
899 """
900 Field's name, or ``None`` on error.
901 """
902
903 return self._name
904
905 @property
906 def type(self):
907 """
908 Field's type (one of :class:`CTFTypeId` constants).
909 """
910
911 return nbt._bt_ctf_field_type(self._fd)
912
913 @property
914 def scope(self):
915 """
916 Field's scope (one of :class:`CTFScope` constants).
917 """
918
919 return self._s
920
921
922 class IntegerFieldDeclaration(FieldDeclaration):
923 """Do not instantiate."""
924
925 def __init__(self):
926 raise NotImplementedError("IntegerFieldDeclaration cannot be instantiated")
927
928 @property
929 def signedness(self):
930 """
931 Return the signedness of an integer:
932 0 if unsigned; 1 if signed; -1 on error.
933 """
934
935 return nbt._bt_ctf_get_int_signedness(self._fd)
936
937 @property
938 def base(self):
939 """Return the base of an int or a negative value on error."""
940
941 return nbt._bt_ctf_get_int_base(self._fd)
942
943 @property
944 def byte_order(self):
945 """
946 Return the byte order. One of class ByteOrder's entries.
947 """
948
949 ret = nbt._bt_ctf_get_int_byte_order(self._fd)
950
951 if ret == 1234:
952 return ByteOrder.BYTE_ORDER_LITTLE_ENDIAN
953 elif ret == 4321:
954 return ByteOrder.BYTE_ORDER_BIG_ENDIAN
955 else:
956 return ByteOrder.BYTE_ORDER_UNKNOWN
957
958 @property
959 def length(self):
960 """
961 Return the size, in bits, of an int or a negative
962 value on error.
963 """
964
965 return nbt._bt_ctf_get_int_len(self._fd)
966
967 @property
968 def encoding(self):
969 """
970 Return the encoding. One of class CTFStringEncoding's entries.
971 Return a negative value on error.
972 """
973
974 return nbt._bt_ctf_get_encoding(self._fd)
975
976
977 class EnumerationFieldDeclaration(FieldDeclaration):
978 """
979 Enumeration field declaration.
980
981 .. note::
982
983 As of this version, this class is missing some properties.
984 """
985
986 def __init__(self):
987 raise NotImplementedError("EnumerationFieldDeclaration cannot be instantiated")
988
989
990 class ArrayFieldDeclaration(FieldDeclaration):
991 """
992 Static array field declaration.
993 """
994
995 def __init__(self):
996 raise NotImplementedError("ArrayFieldDeclaration cannot be instantiated")
997
998 @property
999 def length(self):
1000 """
1001 Static array's fixed length (number of contained elements), or
1002 a negative value on error.
1003 """
1004
1005 return nbt._bt_ctf_get_array_len(self._fd)
1006
1007 @property
1008 def element_declaration(self):
1009 """
1010 Underlying element's field declaration.
1011 """
1012
1013 field_decl_ptr = nbt._bt_python_get_array_element_declaration(self._fd)
1014
1015 return _create_field_declaration(field_decl_ptr, "", self.scope)
1016
1017
1018 class SequenceFieldDeclaration(FieldDeclaration):
1019 """
1020 Sequence (dynamic array) field declaration.
1021
1022 .. note::
1023
1024 As of this version, this class is missing some properties.
1025 """
1026
1027 def __init__(self):
1028 raise NotImplementedError("SequenceFieldDeclaration cannot be instantiated")
1029
1030 @property
1031 def element_declaration(self):
1032 """
1033 Underlying element's field declaration.
1034 """
1035
1036 field_decl_ptr = nbt._bt_python_get_sequence_element_declaration(self._fd)
1037
1038 return _create_field_declaration(field_decl_ptr, "", self.scope)
1039
1040
1041 class FloatFieldDeclaration(FieldDeclaration):
1042 """
1043 Floating point number field declaration.
1044
1045 .. note::
1046
1047 As of this version, this class is missing some properties.
1048 """
1049
1050 def __init__(self):
1051 raise NotImplementedError("FloatFieldDeclaration cannot be instantiated")
1052
1053
1054 class StructureFieldDeclaration(FieldDeclaration):
1055 """
1056 Structure (ordered map of field names to field declarations) field
1057 declaration.
1058
1059 .. note::
1060
1061 As of this version, this class is missing some properties.
1062 """
1063
1064 def __init__(self):
1065 raise NotImplementedError("StructureFieldDeclaration cannot be instantiated")
1066
1067
1068 class StringFieldDeclaration(FieldDeclaration):
1069 """Do not instantiate."""
1070
1071 def __init__(self):
1072 raise NotImplementedError("StringFieldDeclaration cannot be instantiated")
1073
1074
1075 class VariantFieldDeclaration(FieldDeclaration):
1076 """Do not instantiate."""
1077
1078 def __init__(self):
1079 raise NotImplementedError("VariantFieldDeclaration cannot be instantiated")
1080
1081
1082 def field_error():
1083 """
1084 Return the last error code encountered while
1085 accessing a field and reset the error flag.
1086 Return 0 if no error, a negative value otherwise.
1087 """
1088
1089 return nbt._bt_ctf_field_get_error()
1090
1091
1092 def _create_field_declaration(declaration_ptr, name, scope):
1093 """
1094 Private field declaration factory.
1095 """
1096
1097 if declaration_ptr is None:
1098 raise ValueError("declaration_ptr must be valid")
1099 if scope not in _scopes:
1100 raise ValueError("Invalid scope provided")
1101
1102 type = nbt._bt_ctf_field_type(declaration_ptr)
1103 declaration = None
1104
1105 if type == CTFTypeId.INTEGER:
1106 declaration = IntegerFieldDeclaration.__new__(IntegerFieldDeclaration)
1107 elif type == CTFTypeId.ENUM:
1108 declaration = EnumerationFieldDeclaration.__new__(EnumerationFieldDeclaration)
1109 elif type == CTFTypeId.ARRAY:
1110 declaration = ArrayFieldDeclaration.__new__(ArrayFieldDeclaration)
1111 elif type == CTFTypeId.SEQUENCE:
1112 declaration = SequenceFieldDeclaration.__new__(SequenceFieldDeclaration)
1113 elif type == CTFTypeId.FLOAT:
1114 declaration = FloatFieldDeclaration.__new__(FloatFieldDeclaration)
1115 elif type == CTFTypeId.STRUCT:
1116 declaration = StructureFieldDeclaration.__new__(StructureFieldDeclaration)
1117 elif type == CTFTypeId.STRING:
1118 declaration = StringFieldDeclaration.__new__(StringFieldDeclaration)
1119 elif type == CTFTypeId.VARIANT:
1120 declaration = VariantFieldDeclaration.__new__(VariantFieldDeclaration)
1121 else:
1122 return declaration
1123
1124 declaration._fd = declaration_ptr
1125 declaration._s = scope
1126 declaration._name = name
1127
1128 return declaration
1129
1130
1131 class _Definition:
1132 def __init__(self, definition_ptr, scope):
1133 self._d = definition_ptr
1134 self._s = scope
1135
1136 if scope not in _scopes:
1137 ValueError("Invalid scope provided")
1138
1139 @property
1140 def name(self):
1141 """Return the name of a field or None on error."""
1142
1143 return nbt._bt_ctf_field_name(self._d)
1144
1145 @property
1146 def type(self):
1147 """Return the type of a field or -1 if unknown."""
1148
1149 return nbt._bt_ctf_field_type(nbt._bt_ctf_get_decl_from_def(self._d))
1150
1151 @property
1152 def declaration(self):
1153 """Return the associated Definition object."""
1154
1155 return _create_field_declaration(
1156 nbt._bt_ctf_get_decl_from_def(self._d), self.name, self.scope)
1157
1158 def _get_enum_str(self):
1159 """
1160 Return the string matching the current enumeration.
1161 Return None on error.
1162 """
1163
1164 return nbt._bt_ctf_get_enum_str(self._d)
1165
1166 def _get_array_element_at(self, index):
1167 """
1168 Return the array's element at position index.
1169 Return None on error
1170 """
1171
1172 array_ptr = nbt._bt_python_get_array_from_def(self._d)
1173
1174 if array_ptr is None:
1175 return None
1176
1177 definition_ptr = nbt._bt_array_index(array_ptr, index)
1178
1179 if definition_ptr is None:
1180 return None
1181
1182 return _Definition(definition_ptr, self.scope)
1183
1184 def _get_sequence_len(self):
1185 """
1186 Return the len of a sequence or a negative
1187 value on error.
1188 """
1189
1190 seq = nbt._bt_python_get_sequence_from_def(self._d)
1191
1192 return nbt._bt_sequence_len(seq)
1193
1194 def _get_sequence_element_at(self, index):
1195 """
1196 Return the sequence's element at position index,
1197 otherwise return None
1198 """
1199
1200 seq = nbt._bt_python_get_sequence_from_def(self._d)
1201
1202 if seq is not None:
1203 definition_ptr = nbt._bt_sequence_index(seq, index)
1204
1205 if definition_ptr is not None:
1206 return _Definition(definition_ptr, self.scope)
1207
1208 def _get_uint64(self):
1209 """
1210 Return the value associated with the field.
1211 If the field does not exist or is not of the type requested,
1212 the value returned is undefined. To check if an error occured,
1213 use the field_error() function after accessing a field.
1214 """
1215
1216 return nbt._bt_ctf_get_uint64(self._d)
1217
1218 def _get_int64(self):
1219 """
1220 Return the value associated with the field.
1221 If the field does not exist or is not of the type requested,
1222 the value returned is undefined. To check if an error occured,
1223 use the field_error() function after accessing a field.
1224 """
1225
1226 return nbt._bt_ctf_get_int64(self._d)
1227
1228 def _get_char_array(self):
1229 """
1230 Return the value associated with the field.
1231 If the field does not exist or is not of the type requested,
1232 the value returned is undefined. To check if an error occurred,
1233 use the field_error() function after accessing a field.
1234 """
1235
1236 return nbt._bt_ctf_get_char_array(self._d)
1237
1238 def _get_str(self):
1239 """
1240 Return the value associated with the field.
1241 If the field does not exist or is not of the type requested,
1242 the value returned is undefined. To check if an error occurred,
1243 use the field_error() function after accessing a field.
1244 """
1245
1246 return nbt._bt_ctf_get_string(self._d)
1247
1248 def _get_float(self):
1249 """
1250 Return the value associated with the field.
1251 If the field does not exist or is not of the type requested,
1252 the value returned is undefined. To check if an error occurred,
1253 use the field_error() function after accessing a field.
1254 """
1255
1256 return nbt._bt_ctf_get_float(self._d)
1257
1258 def _get_variant(self):
1259 """
1260 Return the variant's selected field.
1261 If the field does not exist or is not of the type requested,
1262 the value returned is undefined. To check if an error occurred,
1263 use the field_error() function after accessing a field.
1264 """
1265
1266 return nbt._bt_ctf_get_variant(self._d)
1267
1268 def _get_struct_field_count(self):
1269 """
1270 Return the number of fields contained in the structure.
1271 If the field does not exist or is not of the type requested,
1272 the value returned is undefined.
1273 """
1274
1275 return nbt._bt_ctf_get_struct_field_count(self._d)
1276
1277 def _get_struct_field_at(self, i):
1278 """
1279 Return the structure's field at position i.
1280 If the field does not exist or is not of the type requested,
1281 the value returned is undefined. To check if an error occurred,
1282 use the field_error() function after accessing a field.
1283 """
1284
1285 return nbt._bt_ctf_get_struct_field_index(self._d, i)
1286
1287 @property
1288 def value(self):
1289 """
1290 Return the value associated with the field according to its type.
1291 Return None on error.
1292 """
1293
1294 id = self.type
1295 value = None
1296
1297 if id == CTFTypeId.STRING:
1298 value = self._get_str()
1299 elif id == CTFTypeId.ARRAY:
1300 element_decl = self.declaration.element_declaration
1301
1302 if ((element_decl.type == CTFTypeId.INTEGER
1303 and element_decl.length == 8)
1304 and (element_decl.encoding == CTFStringEncoding.ASCII or element_decl.encoding == CTFStringEncoding.UTF8)):
1305 value = nbt._bt_python_get_array_string(self._d)
1306 else:
1307 value = []
1308
1309 for i in range(self.declaration.length):
1310 element = self._get_array_element_at(i)
1311 value.append(element.value)
1312 elif id == CTFTypeId.INTEGER:
1313 if self.declaration.signedness == 0:
1314 value = self._get_uint64()
1315 else:
1316 value = self._get_int64()
1317 elif id == CTFTypeId.ENUM:
1318 value = self._get_enum_str()
1319 elif id == CTFTypeId.SEQUENCE:
1320 element_decl = self.declaration.element_declaration
1321
1322 if ((element_decl.type == CTFTypeId.INTEGER
1323 and element_decl.length == 8)
1324 and (element_decl.encoding == CTFStringEncoding.ASCII or element_decl.encoding == CTFStringEncoding.UTF8)):
1325 value = nbt._bt_python_get_sequence_string(self._d)
1326 else:
1327 seq_len = self._get_sequence_len()
1328 value = []
1329
1330 for i in range(seq_len):
1331 evDef = self._get_sequence_element_at(i)
1332 value.append(evDef.value)
1333 elif id == CTFTypeId.FLOAT:
1334 value = self._get_float()
1335 elif id == CTFTypeId.VARIANT:
1336 variant = _Definition.__new__(_Definition)
1337 variant._d = self._get_variant()
1338 value = variant.value
1339 elif id == CTFTypeId.STRUCT:
1340 value = {}
1341
1342 for i in range(self._get_struct_field_count()):
1343 member = _Definition(self._get_struct_field_at(i), self.scope)
1344 value[member.name] = member.value
1345
1346 if field_error():
1347 raise FieldError(
1348 "Error occurred while accessing field {} of type {}".format(
1349 self.name,
1350 CTFTypeId.type_name(id)))
1351
1352 return value
1353
1354 @property
1355 def scope(self):
1356 """Return the scope of a field or None on error."""
1357
1358 return self._s
1359
1360
1361 class CTFWriter:
1362 # Used to compare to -1ULL in error checks
1363 _MAX_UINT64 = 0xFFFFFFFFFFFFFFFF
1364
1365 class EnumerationMapping:
1366 """
1367 Enumeration mapping class. start and end values are inclusive.
1368 """
1369
1370 def __init__(self, name, start, end):
1371 self.name = name
1372 self.start = start
1373 self.end = end
1374
1375 class Clock:
1376 def __init__(self, name):
1377 self._c = nbt._bt_ctf_clock_create(name)
1378
1379 if self._c is None:
1380 raise ValueError("Invalid clock name.")
1381
1382 def __del__(self):
1383 nbt._bt_ctf_clock_put(self._c)
1384
1385 @property
1386 def name(self):
1387 """
1388 Get the clock's name.
1389 """
1390
1391 name = nbt._bt_ctf_clock_get_name(self._c)
1392
1393 if name is None:
1394 raise ValueError("Invalid clock instance.")
1395
1396 return name
1397
1398 @property
1399 def description(self):
1400 """
1401 Get the clock's description. None if unset.
1402 """
1403
1404 return nbt._bt_ctf_clock_get_description(self._c)
1405
1406 @description.setter
1407 def description(self, desc):
1408 """
1409 Set the clock's description. The description appears in the clock's TSDL
1410 meta-data.
1411 """
1412
1413 ret = nbt._bt_ctf_clock_set_description(self._c, str(desc))
1414
1415 if ret < 0:
1416 raise ValueError("Invalid clock description.")
1417
1418 @property
1419 def frequency(self):
1420 """
1421 Get the clock's frequency (Hz).
1422 """
1423
1424 freq = nbt._bt_ctf_clock_get_frequency(self._c)
1425
1426 if freq == CTFWriter._MAX_UINT64:
1427 raise ValueError("Invalid clock instance")
1428
1429 return freq
1430
1431 @frequency.setter
1432 def frequency(self, freq):
1433 """
1434 Set the clock's frequency (Hz).
1435 """
1436
1437 ret = nbt._bt_ctf_clock_set_frequency(self._c, freq)
1438
1439 if ret < 0:
1440 raise ValueError("Invalid frequency value.")
1441
1442 @property
1443 def precision(self):
1444 """
1445 Get the clock's precision (in clock ticks).
1446 """
1447
1448 precision = nbt._bt_ctf_clock_get_precision(self._c)
1449
1450 if precision == CTFWriter._MAX_UINT64:
1451 raise ValueError("Invalid clock instance")
1452
1453 return precision
1454
1455 @precision.setter
1456 def precision(self, precision):
1457 """
1458 Set the clock's precision (in clock ticks).
1459 """
1460
1461 ret = nbt._bt_ctf_clock_set_precision(self._c, precision)
1462
1463 @property
1464 def offset_seconds(self):
1465 """
1466 Get the clock's offset in seconds from POSIX.1 Epoch.
1467 """
1468
1469 offset_s = nbt._bt_ctf_clock_get_offset_s(self._c)
1470
1471 if offset_s == CTFWriter._MAX_UINT64:
1472 raise ValueError("Invalid clock instance")
1473
1474 return offset_s
1475
1476 @offset_seconds.setter
1477 def offset_seconds(self, offset_s):
1478 """
1479 Set the clock's offset in seconds from POSIX.1 Epoch.
1480 """
1481
1482 ret = nbt._bt_ctf_clock_set_offset_s(self._c, offset_s)
1483
1484 if ret < 0:
1485 raise ValueError("Invalid offset value.")
1486
1487 @property
1488 def offset(self):
1489 """
1490 Get the clock's offset in ticks from POSIX.1 Epoch + offset in seconds.
1491 """
1492
1493 offset = nbt._bt_ctf_clock_get_offset(self._c)
1494
1495 if offset == CTFWriter._MAX_UINT64:
1496 raise ValueError("Invalid clock instance")
1497
1498 return offset
1499
1500 @offset.setter
1501 def offset(self, offset):
1502 """
1503 Set the clock's offset in ticks from POSIX.1 Epoch + offset in seconds.
1504 """
1505
1506 ret = nbt._bt_ctf_clock_set_offset(self._c, offset)
1507
1508 if ret < 0:
1509 raise ValueError("Invalid offset value.")
1510
1511 @property
1512 def absolute(self):
1513 """
1514 Get a clock's absolute attribute. A clock is absolute if the clock
1515 is a global reference across the trace's other clocks.
1516 """
1517
1518 is_absolute = nbt._bt_ctf_clock_get_is_absolute(self._c)
1519
1520 if is_absolute == -1:
1521 raise ValueError("Invalid clock instance")
1522
1523 return False if is_absolute == 0 else True
1524
1525 @absolute.setter
1526 def absolute(self, is_absolute):
1527 """
1528 Set a clock's absolute attribute. A clock is absolute if the clock
1529 is a global reference across the trace's other clocks.
1530 """
1531
1532 ret = nbt._bt_ctf_clock_set_is_absolute(self._c, int(is_absolute))
1533
1534 if ret < 0:
1535 raise ValueError("Could not set the clock's absolute attribute.")
1536
1537 @property
1538 def uuid(self):
1539 """
1540 Get a clock's UUID (an object of type UUID).
1541 """
1542
1543 uuid_list = []
1544
1545 for i in range(16):
1546 ret, value = nbt._bt_python_ctf_clock_get_uuid_index(self._c, i)
1547
1548 if ret < 0:
1549 raise ValueError("Invalid clock instance")
1550
1551 uuid_list.append(value)
1552
1553 return UUID(bytes=bytes(uuid_list))
1554
1555 @uuid.setter
1556 def uuid(self, uuid):
1557 """
1558 Set a clock's UUID (an object of type UUID).
1559 """
1560
1561 uuid_bytes = uuid.bytes
1562
1563 if len(uuid_bytes) != 16:
1564 raise ValueError("Invalid UUID provided. UUID length must be 16 bytes")
1565
1566 for i in range(len(uuid_bytes)):
1567 ret = nbt._bt_python_ctf_clock_set_uuid_index(self._c, i,
1568 uuid_bytes[i])
1569
1570 if ret < 0:
1571 raise ValueError("Invalid clock instance")
1572
1573 @property
1574 def time(self):
1575 """
1576 Get the current time in nanoseconds since the clock's origin (offset and
1577 offset_s attributes).
1578 """
1579
1580 time = nbt._bt_ctf_clock_get_time(self._c)
1581
1582 if time == CTFWriter._MAX_UINT64:
1583 raise ValueError("Invalid clock instance")
1584
1585 return time
1586
1587 @time.setter
1588 def time(self, time):
1589 """
1590 Set the current time in nanoseconds since the clock's origin (offset and
1591 offset_s attributes). The clock's value will be sampled as events are
1592 appended to a stream.
1593 """
1594
1595 ret = nbt._bt_ctf_clock_set_time(self._c, time)
1596
1597 if ret < 0:
1598 raise ValueError("Invalid time value.")
1599
1600 class FieldDeclaration:
1601 """
1602 FieldDeclaration should not be instantiated directly. Instantiate
1603 one of the concrete FieldDeclaration classes.
1604 """
1605
1606 class IntegerBase:
1607 # These values are based on the bt_ctf_integer_base enum
1608 # declared in event-types.h.
1609 INTEGER_BASE_UNKNOWN = -1
1610 INTEGER_BASE_BINARY = 2
1611 INTEGER_BASE_OCTAL = 8
1612 INTEGER_BASE_DECIMAL = 10
1613 INTEGER_BASE_HEXADECIMAL = 16
1614
1615 def __init__(self):
1616 if self._ft is None:
1617 raise ValueError("FieldDeclaration creation failed.")
1618
1619 def __del__(self):
1620 nbt._bt_ctf_field_type_put(self._ft)
1621
1622 @staticmethod
1623 def _create_field_declaration_from_native_instance(
1624 native_field_declaration):
1625 type_dict = {
1626 CTFTypeId.INTEGER: CTFWriter.IntegerFieldDeclaration,
1627 CTFTypeId.FLOAT: CTFWriter.FloatFieldDeclaration,
1628 CTFTypeId.ENUM: CTFWriter.EnumerationFieldDeclaration,
1629 CTFTypeId.STRING: CTFWriter.StringFieldDeclaration,
1630 CTFTypeId.STRUCT: CTFWriter.StructureFieldDeclaration,
1631 CTFTypeId.VARIANT: CTFWriter.VariantFieldDeclaration,
1632 CTFTypeId.ARRAY: CTFWriter.ArrayFieldDeclaration,
1633 CTFTypeId.SEQUENCE: CTFWriter.SequenceFieldDeclaration
1634 }
1635
1636 field_type_id = nbt._bt_ctf_field_type_get_type_id(native_field_declaration)
1637
1638 if field_type_id == CTFTypeId.UNKNOWN:
1639 raise TypeError("Invalid field instance")
1640
1641 declaration = CTFWriter.Field.__new__(CTFWriter.Field)
1642 declaration._ft = native_field_declaration
1643 declaration.__class__ = type_dict[field_type_id]
1644
1645 return declaration
1646
1647 @property
1648 def alignment(self):
1649 """
1650 Get the field declaration's alignment. Returns -1 on error.
1651 """
1652
1653 return nbt._bt_ctf_field_type_get_alignment(self._ft)
1654
1655 @alignment.setter
1656 def alignment(self, alignment):
1657 """
1658 Set the field declaration's alignment. Defaults to 1 (bit-aligned). However,
1659 some types, such as structures and string, may impose other alignment
1660 constraints.
1661 """
1662
1663 ret = nbt._bt_ctf_field_type_set_alignment(self._ft, alignment)
1664
1665 if ret < 0:
1666 raise ValueError("Invalid alignment value.")
1667
1668 @property
1669 def byte_order(self):
1670 """
1671 Get the field declaration's byte order. One of the ByteOrder's constant.
1672 """
1673
1674 return nbt._bt_ctf_field_type_get_byte_order(self._ft)
1675
1676 @byte_order.setter
1677 def byte_order(self, byte_order):
1678 """
1679 Set the field declaration's byte order. Use constants defined in the ByteOrder
1680 class.
1681 """
1682
1683 ret = nbt._bt_ctf_field_type_set_byte_order(self._ft, byte_order)
1684
1685 if ret < 0:
1686 raise ValueError("Could not set byte order value.")
1687
1688 class IntegerFieldDeclaration(FieldDeclaration):
1689 def __init__(self, size):
1690 """
1691 Create a new integer field declaration of the given size.
1692 """
1693 self._ft = nbt._bt_ctf_field_type_integer_create(size)
1694 super().__init__()
1695
1696 @property
1697 def size(self):
1698 """
1699 Get an integer's size.
1700 """
1701
1702 ret = nbt._bt_ctf_field_type_integer_get_size(self._ft)
1703
1704 if ret < 0:
1705 raise ValueError("Could not get Integer's size attribute.")
1706 else:
1707 return ret
1708
1709 @property
1710 def signed(self):
1711 """
1712 Get an integer's signedness attribute.
1713 """
1714
1715 ret = nbt._bt_ctf_field_type_integer_get_signed(self._ft)
1716
1717 if ret < 0:
1718 raise ValueError("Could not get Integer's signed attribute.")
1719 elif ret > 0:
1720 return True
1721 else:
1722 return False
1723
1724 @signed.setter
1725 def signed(self, signed):
1726 """
1727 Set an integer's signedness attribute.
1728 """
1729
1730 ret = nbt._bt_ctf_field_type_integer_set_signed(self._ft, signed)
1731
1732 if ret < 0:
1733 raise ValueError("Could not set Integer's signed attribute.")
1734
1735 @property
1736 def base(self):
1737 """
1738 Get the integer's base used to pretty-print the resulting trace.
1739 Returns a constant from the FieldDeclaration.IntegerBase class.
1740 """
1741
1742 return nbt._bt_ctf_field_type_integer_get_base(self._ft)
1743
1744 @base.setter
1745 def base(self, base):
1746 """
1747 Set the integer's base used to pretty-print the resulting trace.
1748 The base must be a constant of the FieldDeclarationIntegerBase class.
1749 """
1750
1751 ret = nbt._bt_ctf_field_type_integer_set_base(self._ft, base)
1752
1753 if ret < 0:
1754 raise ValueError("Could not set Integer's base.")
1755
1756 @property
1757 def encoding(self):
1758 """
1759 Get the integer's encoding (one of the constants of the
1760 CTFStringEncoding class).
1761 Returns a constant from the CTFStringEncoding class.
1762 """
1763
1764 return nbt._bt_ctf_field_type_integer_get_encoding(self._ft)
1765
1766 @encoding.setter
1767 def encoding(self, encoding):
1768 """
1769 An integer encoding may be set to signal that the integer must be printed
1770 as a text character. Must be a constant from the CTFStringEncoding class.
1771 """
1772
1773 ret = nbt._bt_ctf_field_type_integer_set_encoding(self._ft, encoding)
1774
1775 if ret < 0:
1776 raise ValueError("Could not set Integer's encoding.")
1777
1778 class EnumerationFieldDeclaration(FieldDeclaration):
1779 def __init__(self, integer_type):
1780 """
1781 Create a new enumeration field declaration with the given underlying container type.
1782 """
1783 isinst = isinstance(integer_type, CTFWriter.IntegerFieldDeclaration)
1784
1785 if integer_type is None or not isinst:
1786 raise TypeError("Invalid integer container.")
1787
1788 self._ft = nbt._bt_ctf_field_type_enumeration_create(integer_type._ft)
1789 super().__init__()
1790
1791 @property
1792 def container(self):
1793 """
1794 Get the enumeration's underlying container type.
1795 """
1796
1797 ret = nbt._bt_ctf_field_type_enumeration_get_container_type(self._ft)
1798
1799 if ret is None:
1800 raise TypeError("Invalid enumeration declaration")
1801
1802 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
1803
1804 def add_mapping(self, name, range_start, range_end):
1805 """
1806 Add a mapping to the enumeration. The range's values are inclusive.
1807 """
1808
1809 if range_start < 0 or range_end < 0:
1810 ret = nbt._bt_ctf_field_type_enumeration_add_mapping(self._ft,
1811 str(name),
1812 range_start,
1813 range_end)
1814 else:
1815 ret = nbt._bt_ctf_field_type_enumeration_add_mapping_unsigned(self._ft,
1816 str(name),
1817 range_start,
1818 range_end)
1819
1820 if ret < 0:
1821 raise ValueError("Could not add mapping to enumeration declaration.")
1822
1823 @property
1824 def mappings(self):
1825 """
1826 Generator returning instances of EnumerationMapping.
1827 """
1828
1829 signed = self.container.signed
1830
1831 count = nbt._bt_ctf_field_type_enumeration_get_mapping_count(self._ft)
1832
1833 for i in range(count):
1834 if signed:
1835 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping(self._ft, i)
1836 else:
1837 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, i)
1838
1839 if len(ret) != 3:
1840 msg = "Could not get Enumeration mapping at index {}".format(i)
1841 raise TypeError(msg)
1842
1843 name, range_start, range_end = ret
1844 yield CTFWriter.EnumerationMapping(name, range_start, range_end)
1845
1846 def get_mapping_by_name(self, name):
1847 """
1848 Get a mapping by name (EnumerationMapping).
1849 """
1850
1851 index = nbt._bt_ctf_field_type_enumeration_get_mapping_index_by_name(self._ft, name)
1852
1853 if index < 0:
1854 return None
1855
1856 if self.container.signed:
1857 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping(self._ft, index)
1858 else:
1859 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, index)
1860
1861 if len(ret) != 3:
1862 msg = "Could not get Enumeration mapping at index {}".format(i)
1863 raise TypeError(msg)
1864
1865 name, range_start, range_end = ret
1866
1867 return CTFWriter.EnumerationMapping(name, range_start, range_end)
1868
1869 def get_mapping_by_value(self, value):
1870 """
1871 Get a mapping by value (EnumerationMapping).
1872 """
1873
1874 if value < 0:
1875 index = nbt._bt_ctf_field_type_enumeration_get_mapping_index_by_value(self._ft, value)
1876 else:
1877 index = nbt._bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(self._ft, value)
1878
1879 if index < 0:
1880 return None
1881
1882 if self.container.signed:
1883 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping(self._ft, index)
1884 else:
1885 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, index)
1886
1887 if len(ret) != 3:
1888 msg = "Could not get Enumeration mapping at index {}".format(i)
1889 raise TypeError(msg)
1890
1891 name, range_start, range_end = ret
1892
1893 return CTFWriter.EnumerationMapping(name, range_start, range_end)
1894
1895 class FloatFieldDeclaration(FieldDeclaration):
1896 FLT_EXP_DIG = 8
1897 DBL_EXP_DIG = 11
1898 FLT_MANT_DIG = 24
1899 DBL_MANT_DIG = 53
1900
1901 def __init__(self):
1902 """
1903 Create a new floating point field declaration.
1904 """
1905
1906 self._ft = nbt._bt_ctf_field_type_floating_point_create()
1907 super().__init__()
1908
1909 @property
1910 def exponent_digits(self):
1911 """
1912 Get the number of exponent digits used to store the floating point field.
1913 """
1914
1915 ret = nbt._bt_ctf_field_type_floating_point_get_exponent_digits(self._ft)
1916
1917 if ret < 0:
1918 raise TypeError(
1919 "Could not get Floating point exponent digit count")
1920
1921 return ret
1922
1923 @exponent_digits.setter
1924 def exponent_digits(self, exponent_digits):
1925 """
1926 Set the number of exponent digits to use to store the floating point field.
1927 The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG which
1928 are defined as constants of this class.
1929 """
1930
1931 ret = nbt._bt_ctf_field_type_floating_point_set_exponent_digits(self._ft,
1932 exponent_digits)
1933
1934 if ret < 0:
1935 raise ValueError("Could not set exponent digit count.")
1936
1937 @property
1938 def mantissa_digits(self):
1939 """
1940 Get the number of mantissa digits used to store the floating point field.
1941 """
1942
1943 ret = nbt._bt_ctf_field_type_floating_point_get_mantissa_digits(self._ft)
1944
1945 if ret < 0:
1946 raise TypeError("Could not get Floating point mantissa digit count")
1947
1948 return ret
1949
1950 @mantissa_digits.setter
1951 def mantissa_digits(self, mantissa_digits):
1952 """
1953 Set the number of mantissa digits to use to store the floating point field.
1954 The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG which
1955 are defined as constants of this class.
1956 """
1957
1958 ret = nbt._bt_ctf_field_type_floating_point_set_mantissa_digits(self._ft,
1959 mantissa_digits)
1960
1961 if ret < 0:
1962 raise ValueError("Could not set mantissa digit count.")
1963
1964 class FloatingPointFieldDeclaration(FloatFieldDeclaration):
1965 pass
1966
1967 class StructureFieldDeclaration(FieldDeclaration):
1968 def __init__(self):
1969 """
1970 Create a new structure field declaration.
1971 """
1972
1973 self._ft = nbt._bt_ctf_field_type_structure_create()
1974 super().__init__()
1975
1976 def add_field(self, field_type, field_name):
1977 """
1978 Add a field of type "field_type" to the structure.
1979 """
1980
1981 ret = nbt._bt_ctf_field_type_structure_add_field(self._ft,
1982 field_type._ft,
1983 str(field_name))
1984
1985 if ret < 0:
1986 raise ValueError("Could not add field to structure.")
1987
1988 @property
1989 def fields(self):
1990 """
1991 Generator returning the structure's field as tuples of (field name, field declaration).
1992 """
1993
1994 count = nbt._bt_ctf_field_type_structure_get_field_count(self._ft)
1995
1996 if count < 0:
1997 raise TypeError("Could not get Structure field count")
1998
1999 for i in range(count):
2000 field_name = nbt._bt_python_ctf_field_type_structure_get_field_name(self._ft, i)
2001
2002 if field_name is None:
2003 msg = "Could not get Structure field name at index {}".format(i)
2004 raise TypeError(msg)
2005
2006 field_type_native = nbt._bt_python_ctf_field_type_structure_get_field_type(self._ft, i)
2007
2008 if field_type_native is None:
2009 msg = "Could not get Structure field type at index {}".format(i)
2010 raise TypeError(msg)
2011
2012 field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2013 yield (field_name, field_type)
2014
2015 def get_field_by_name(self, name):
2016 """
2017 Get a field declaration by name (FieldDeclaration).
2018 """
2019
2020 field_type_native = nbt._bt_ctf_field_type_structure_get_field_type_by_name(self._ft, name)
2021
2022 if field_type_native is None:
2023 msg = "Could not find Structure field with name {}".format(name)
2024 raise TypeError(msg)
2025
2026 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2027
2028 class VariantFieldDeclaration(FieldDeclaration):
2029 def __init__(self, enum_tag, tag_name):
2030 """
2031 Create a new variant field declaration.
2032 """
2033
2034 isinst = isinstance(enum_tag, CTFWriter.EnumerationFieldDeclaration)
2035 if enum_tag is None or not isinst:
2036 raise TypeError("Invalid tag type; must be of type EnumerationFieldDeclaration.")
2037
2038 self._ft = nbt._bt_ctf_field_type_variant_create(enum_tag._ft,
2039 str(tag_name))
2040 super().__init__()
2041
2042 @property
2043 def tag_name(self):
2044 """
2045 Get the variant's tag name.
2046 """
2047
2048 ret = nbt._bt_ctf_field_type_variant_get_tag_name(self._ft)
2049
2050 if ret is None:
2051 raise TypeError("Could not get Variant tag name")
2052
2053 return ret
2054
2055 @property
2056 def tag_type(self):
2057 """
2058 Get the variant's tag type.
2059 """
2060
2061 ret = nbt._bt_ctf_field_type_variant_get_tag_type(self._ft)
2062
2063 if ret is None:
2064 raise TypeError("Could not get Variant tag type")
2065
2066 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
2067
2068 def add_field(self, field_type, field_name):
2069 """
2070 Add a field of type "field_type" to the variant.
2071 """
2072
2073 ret = nbt._bt_ctf_field_type_variant_add_field(self._ft,
2074 field_type._ft,
2075 str(field_name))
2076
2077 if ret < 0:
2078 raise ValueError("Could not add field to variant.")
2079
2080 @property
2081 def fields(self):
2082 """
2083 Generator returning the variant's field as tuples of (field name, field declaration).
2084 """
2085
2086 count = nbt._bt_ctf_field_type_variant_get_field_count(self._ft)
2087
2088 if count < 0:
2089 raise TypeError("Could not get Variant field count")
2090
2091 for i in range(count):
2092 field_name = nbt._bt_python_ctf_field_type_variant_get_field_name(self._ft, i)
2093
2094 if field_name is None:
2095 msg = "Could not get Variant field name at index {}".format(i)
2096 raise TypeError(msg)
2097
2098 field_type_native = nbt._bt_python_ctf_field_type_variant_get_field_type(self._ft, i)
2099
2100 if field_type_native is None:
2101 msg = "Could not get Variant field type at index {}".format(i)
2102 raise TypeError(msg)
2103
2104 field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2105 yield (field_name, field_type)
2106
2107 def get_field_by_name(self, name):
2108 """
2109 Get a field declaration by name (FieldDeclaration).
2110 """
2111
2112 field_type_native = nbt._bt_ctf_field_type_variant_get_field_type_by_name(self._ft,
2113 name)
2114
2115 if field_type_native is None:
2116 msg = "Could not find Variant field with name {}".format(name)
2117 raise TypeError(msg)
2118
2119 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2120
2121 def get_field_from_tag(self, tag):
2122 """
2123 Get a field declaration from tag (EnumerationField).
2124 """
2125
2126 field_type_native = nbt._bt_ctf_field_type_variant_get_field_type_from_tag(self._ft, tag._f)
2127
2128 if field_type_native is None:
2129 msg = "Could not find Variant field with tag value {}".format(tag.value)
2130 raise TypeError(msg)
2131
2132 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2133
2134 class ArrayFieldDeclaration(FieldDeclaration):
2135 def __init__(self, element_type, length):
2136 """
2137 Create a new array field declaration.
2138 """
2139
2140 self._ft = nbt._bt_ctf_field_type_array_create(element_type._ft,
2141 length)
2142 super().__init__()
2143
2144 @property
2145 def element_type(self):
2146 """
2147 Get the array's element type.
2148 """
2149
2150 ret = nbt._bt_ctf_field_type_array_get_element_type(self._ft)
2151
2152 if ret is None:
2153 raise TypeError("Could not get Array element type")
2154
2155 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
2156
2157 @property
2158 def length(self):
2159 """
2160 Get the array's length.
2161 """
2162
2163 ret = nbt._bt_ctf_field_type_array_get_length(self._ft)
2164
2165 if ret < 0:
2166 raise TypeError("Could not get Array length")
2167
2168 return ret
2169
2170 class SequenceFieldDeclaration(FieldDeclaration):
2171 def __init__(self, element_type, length_field_name):
2172 """
2173 Create a new sequence field declaration.
2174 """
2175
2176 self._ft = nbt._bt_ctf_field_type_sequence_create(element_type._ft,
2177 str(length_field_name))
2178 super().__init__()
2179
2180 @property
2181 def element_type(self):
2182 """
2183 Get the sequence's element type.
2184 """
2185
2186 ret = nbt._bt_ctf_field_type_sequence_get_element_type(self._ft)
2187
2188 if ret is None:
2189 raise TypeError("Could not get Sequence element type")
2190
2191 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
2192
2193 @property
2194 def length_field_name(self):
2195 """
2196 Get the sequence's length field name.
2197 """
2198
2199 ret = nbt._bt_ctf_field_type_sequence_get_length_field_name(self._ft)
2200
2201 if ret is None:
2202 raise TypeError("Could not get Sequence length field name")
2203
2204 return ret
2205
2206 class StringFieldDeclaration(FieldDeclaration):
2207 def __init__(self):
2208 """
2209 Create a new string field declaration.
2210 """
2211
2212 self._ft = nbt._bt_ctf_field_type_string_create()
2213 super().__init__()
2214
2215 @property
2216 def encoding(self):
2217 """
2218 Get a string declaration's encoding (a constant from the CTFStringEncoding class).
2219 """
2220
2221 return nbt._bt_ctf_field_type_string_get_encoding(self._ft)
2222
2223 @encoding.setter
2224 def encoding(self, encoding):
2225 """
2226 Set a string declaration's encoding. Must be a constant from the CTFStringEncoding class.
2227 """
2228
2229 ret = nbt._bt_ctf_field_type_string_set_encoding(self._ft, encoding)
2230 if ret < 0:
2231 raise ValueError("Could not set string encoding.")
2232
2233 @staticmethod
2234 def create_field(field_type):
2235 """
2236 Create an instance of a field.
2237 """
2238 isinst = isinstance(field_type, CTFWriter.FieldDeclaration)
2239
2240 if field_type is None or not isinst:
2241 raise TypeError("Invalid field_type. Type must be a FieldDeclaration-derived class.")
2242
2243 if isinstance(field_type, CTFWriter.IntegerFieldDeclaration):
2244 return CTFWriter.IntegerField(field_type)
2245 elif isinstance(field_type, CTFWriter.EnumerationFieldDeclaration):
2246 return CTFWriter.EnumerationField(field_type)
2247 elif isinstance(field_type, CTFWriter.FloatFieldDeclaration):
2248 return CTFWriter.FloatingPointField(field_type)
2249 elif isinstance(field_type, CTFWriter.StructureFieldDeclaration):
2250 return CTFWriter.StructureField(field_type)
2251 elif isinstance(field_type, CTFWriter.VariantFieldDeclaration):
2252 return CTFWriter.VariantField(field_type)
2253 elif isinstance(field_type, CTFWriter.ArrayFieldDeclaration):
2254 return CTFWriter.ArrayField(field_type)
2255 elif isinstance(field_type, CTFWriter.SequenceFieldDeclaration):
2256 return CTFWriter.SequenceField(field_type)
2257 elif isinstance(field_type, CTFWriter.StringFieldDeclaration):
2258 return CTFWriter.StringField(field_type)
2259
2260 class Field:
2261 """
2262 Base class, do not instantiate.
2263 """
2264
2265 def __init__(self, field_type):
2266 if not isinstance(field_type, CTFWriter.FieldDeclaration):
2267 raise TypeError("Invalid field_type argument.")
2268
2269 self._f = nbt._bt_ctf_field_create(field_type._ft)
2270
2271 if self._f is None:
2272 raise ValueError("Field creation failed.")
2273
2274 def __del__(self):
2275 nbt._bt_ctf_field_put(self._f)
2276
2277 @staticmethod
2278 def _create_field_from_native_instance(native_field_instance):
2279 type_dict = {
2280 CTFTypeId.INTEGER: CTFWriter.IntegerField,
2281 CTFTypeId.FLOAT: CTFWriter.FloatingPointField,
2282 CTFTypeId.ENUM: CTFWriter.EnumerationField,
2283 CTFTypeId.STRING: CTFWriter.StringField,
2284 CTFTypeId.STRUCT: CTFWriter.StructureField,
2285 CTFTypeId.VARIANT: CTFWriter.VariantField,
2286 CTFTypeId.ARRAY: CTFWriter.ArrayField,
2287 CTFTypeId.SEQUENCE: CTFWriter.SequenceField
2288 }
2289
2290 field_type = nbt._bt_python_get_field_type(native_field_instance)
2291
2292 if field_type == CTFTypeId.UNKNOWN:
2293 raise TypeError("Invalid field instance")
2294
2295 field = CTFWriter.Field.__new__(CTFWriter.Field)
2296 field._f = native_field_instance
2297 field.__class__ = type_dict[field_type]
2298
2299 return field
2300
2301 @property
2302 def declaration(self):
2303 native_field_type = nbt._bt_ctf_field_get_type(self._f)
2304
2305 if native_field_type is None:
2306 raise TypeError("Invalid field instance")
2307 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(
2308 native_field_type)
2309
2310 class IntegerField(Field):
2311 @property
2312 def value(self):
2313 """
2314 Get an integer field's value.
2315 """
2316
2317 signedness = nbt._bt_python_field_integer_get_signedness(self._f)
2318
2319 if signedness < 0:
2320 raise TypeError("Invalid integer instance.")
2321
2322 if signedness == 0:
2323 ret, value = nbt._bt_ctf_field_unsigned_integer_get_value(self._f)
2324 else:
2325 ret, value = nbt._bt_ctf_field_signed_integer_get_value(self._f)
2326
2327 if ret < 0:
2328 raise ValueError("Could not get integer field value.")
2329
2330 return value
2331
2332 @value.setter
2333 def value(self, value):
2334 """
2335 Set an integer field's value.
2336 """
2337
2338 if not isinstance(value, int):
2339 raise TypeError("IntegerField's value must be an int")
2340
2341 signedness = nbt._bt_python_field_integer_get_signedness(self._f)
2342 if signedness < 0:
2343 raise TypeError("Invalid integer instance.")
2344
2345 if signedness == 0:
2346 ret = nbt._bt_ctf_field_unsigned_integer_set_value(self._f, value)
2347 else:
2348 ret = nbt._bt_ctf_field_signed_integer_set_value(self._f, value)
2349
2350 if ret < 0:
2351 raise ValueError("Could not set integer field value.")
2352
2353 class EnumerationField(Field):
2354 @property
2355 def container(self):
2356 """
2357 Return the enumeration's underlying container field (an integer field).
2358 """
2359
2360 container = CTFWriter.IntegerField.__new__(CTFWriter.IntegerField)
2361 container._f = nbt._bt_ctf_field_enumeration_get_container(self._f)
2362
2363 if container._f is None:
2364 raise TypeError("Invalid enumeration field type.")
2365
2366 return container
2367
2368 @property
2369 def value(self):
2370 """
2371 Get the enumeration field's mapping name.
2372 """
2373
2374 value = nbt._bt_ctf_field_enumeration_get_mapping_name(self._f)
2375
2376 if value is None:
2377 raise ValueError("Could not get enumeration's mapping name.")
2378
2379 return value
2380
2381 @value.setter
2382 def value(self, value):
2383 """
2384 Set the enumeration field's value. Must be an integer as mapping names
2385 may be ambiguous.
2386 """
2387
2388 if not isinstance(value, int):
2389 raise TypeError("EnumerationField value must be an int")
2390
2391 self.container.value = value
2392
2393 class FloatingPointField(Field):
2394 @property
2395 def value(self):
2396 """
2397 Get a floating point field's value.
2398 """
2399
2400 ret, value = nbt._bt_ctf_field_floating_point_get_value(self._f)
2401
2402 if ret < 0:
2403 raise ValueError("Could not get floating point field value.")
2404
2405 return value
2406
2407 @value.setter
2408 def value(self, value):
2409 """
2410 Set a floating point field's value.
2411 """
2412
2413 if not isinstance(value, int) and not isinstance(value, float):
2414 raise TypeError("Value must be either a float or an int")
2415
2416 ret = nbt._bt_ctf_field_floating_point_set_value(self._f, float(value))
2417
2418 if ret < 0:
2419 raise ValueError("Could not set floating point field value.")
2420
2421 # oops!! This class is provided to ensure backward-compatibility since
2422 # a stable release publicly exposed this abomination.
2423 class FloatFieldingPoint(FloatingPointField):
2424 pass
2425
2426 class StructureField(Field):
2427 def field(self, field_name):
2428 """
2429 Get the structure's field corresponding to the provided field name.
2430 """
2431
2432 native_instance = nbt._bt_ctf_field_structure_get_field(self._f,
2433 str(field_name))
2434
2435 if native_instance is None:
2436 raise ValueError("Invalid field_name provided.")
2437
2438 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2439
2440 class VariantField(Field):
2441 def field(self, tag):
2442 """
2443 Return the variant's selected field. The "tag" field is the selector enum field.
2444 """
2445
2446 native_instance = nbt._bt_ctf_field_variant_get_field(self._f, tag._f)
2447
2448 if native_instance is None:
2449 raise ValueError("Invalid tag provided.")
2450
2451 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2452
2453 class ArrayField(Field):
2454 def field(self, index):
2455 """
2456 Return the array's field at position "index".
2457 """
2458
2459 native_instance = nbt._bt_ctf_field_array_get_field(self._f, index)
2460
2461 if native_instance is None:
2462 raise IndexError("Invalid index provided.")
2463
2464 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2465
2466 class SequenceField(Field):
2467 @property
2468 def length(self):
2469 """
2470 Get the sequence's length field (IntegerField).
2471 """
2472
2473 native_instance = nbt._bt_ctf_field_sequence_get_length(self._f)
2474
2475 if native_instance is None:
2476 length = -1
2477
2478 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2479
2480 @length.setter
2481 def length(self, length_field):
2482 """
2483 Set the sequence's length field (IntegerField).
2484 """
2485
2486 if not isinstance(length_field, CTFWriter.IntegerField):
2487 raise TypeError("Invalid length field.")
2488
2489 if length_field.declaration.signed:
2490 raise TypeError("Sequence field length must be unsigned")
2491
2492 ret = nbt._bt_ctf_field_sequence_set_length(self._f, length_field._f)
2493
2494 if ret < 0:
2495 raise ValueError("Could not set sequence length.")
2496
2497 def field(self, index):
2498 """
2499 Return the sequence's field at position "index".
2500 """
2501
2502 native_instance = nbt._bt_ctf_field_sequence_get_field(self._f, index)
2503
2504 if native_instance is None:
2505 raise ValueError("Could not get sequence element at index.")
2506
2507 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2508
2509 class StringField(Field):
2510 @property
2511 def value(self):
2512 """
2513 Get a string field's value.
2514 """
2515
2516 return nbt._bt_ctf_field_string_get_value(self._f)
2517
2518 @value.setter
2519 def value(self, value):
2520 """
2521 Set a string field's value.
2522 """
2523
2524 ret = nbt._bt_ctf_field_string_set_value(self._f, str(value))
2525
2526 if ret < 0:
2527 raise ValueError("Could not set string field value.")
2528
2529 class EventClass:
2530 def __init__(self, name):
2531 """
2532 Create a new event class of the given name.
2533 """
2534
2535 self._ec = nbt._bt_ctf_event_class_create(name)
2536
2537 if self._ec is None:
2538 raise ValueError("Event class creation failed.")
2539
2540 def __del__(self):
2541 nbt._bt_ctf_event_class_put(self._ec)
2542
2543 def add_field(self, field_type, field_name):
2544 """
2545 Add a field of type "field_type" to the event class.
2546 """
2547
2548 ret = nbt._bt_ctf_event_class_add_field(self._ec, field_type._ft,
2549 str(field_name))
2550
2551 if ret < 0:
2552 raise ValueError("Could not add field to event class.")
2553
2554 @property
2555 def name(self):
2556 """
2557 Get the event class' name.
2558 """
2559
2560 name = nbt._bt_ctf_event_class_get_name(self._ec)
2561
2562 if name is None:
2563 raise TypeError("Could not get EventClass name")
2564
2565 return name
2566
2567 @property
2568 def id(self):
2569 """
2570 Get the event class' id. Returns a negative value if unset.
2571 """
2572
2573 id = nbt._bt_ctf_event_class_get_id(self._ec)
2574
2575 if id < 0:
2576 raise TypeError("Could not get EventClass id")
2577
2578 return id
2579
2580 @id.setter
2581 def id(self, id):
2582 """
2583 Set the event class' id. Throws a TypeError if the event class
2584 is already registered to a stream class.
2585 """
2586
2587 ret = nbt._bt_ctf_event_class_set_id(self._ec, id)
2588
2589 if ret < 0:
2590 raise TypeError("Can't change an Event Class's id after it has been assigned to a stream class")
2591
2592 @property
2593 def stream_class(self):
2594 """
2595 Get the event class' stream class. Returns None if unset.
2596 """
2597 stream_class_native = nbt._bt_ctf_event_class_get_stream_class(self._ec)
2598
2599 if stream_class_native is None:
2600 return None
2601
2602 stream_class = CTFWriter.StreamClass.__new__(CTFWriter.StreamClass)
2603 stream_class._sc = stream_class_native
2604
2605 return stream_class
2606
2607 @property
2608 def fields(self):
2609 """
2610 Generator returning the event class' fields as tuples of (field name, field declaration).
2611 """
2612
2613 count = nbt._bt_ctf_event_class_get_field_count(self._ec)
2614
2615 if count < 0:
2616 raise TypeError("Could not get EventClass' field count")
2617
2618 for i in range(count):
2619 field_name = nbt._bt_python_ctf_event_class_get_field_name(self._ec, i)
2620
2621 if field_name is None:
2622 msg = "Could not get EventClass' field name at index {}".format(i)
2623 raise TypeError(msg)
2624
2625 field_type_native = nbt._bt_python_ctf_event_class_get_field_type(self._ec, i)
2626
2627 if field_type_native is None:
2628 msg = "Could not get EventClass' field type at index {}".format(i)
2629 raise TypeError(msg)
2630
2631 field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2632 yield (field_name, field_type)
2633
2634 def get_field_by_name(self, name):
2635 """
2636 Get a field declaration by name (FieldDeclaration).
2637 """
2638
2639 field_type_native = nbt._bt_ctf_event_class_get_field_by_name(self._ec, name)
2640
2641 if field_type_native is None:
2642 msg = "Could not find EventClass field with name {}".format(name)
2643 raise TypeError(msg)
2644
2645 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2646
2647 class Event:
2648 def __init__(self, event_class):
2649 """
2650 Create a new event of the given event class.
2651 """
2652
2653 if not isinstance(event_class, CTFWriter.EventClass):
2654 raise TypeError("Invalid event_class argument.")
2655
2656 self._e = nbt._bt_ctf_event_create(event_class._ec)
2657
2658 if self._e is None:
2659 raise ValueError("Event creation failed.")
2660
2661 def __del__(self):
2662 nbt._bt_ctf_event_put(self._e)
2663
2664 @property
2665 def event_class(self):
2666 """
2667 Get the event's class.
2668 """
2669
2670 event_class_native = nbt._bt_ctf_event_get_class(self._e)
2671
2672 if event_class_native is None:
2673 return None
2674
2675 event_class = CTFWriter.EventClass.__new__(CTFWriter.EventClass)
2676 event_class._ec = event_class_native
2677
2678 return event_class
2679
2680 def clock(self):
2681 """
2682 Get a clock from event. Returns None if the event's class
2683 is not registered to a stream class.
2684 """
2685
2686 clock_instance = nbt._bt_ctf_event_get_clock(self._e)
2687
2688 if clock_instance is None:
2689 return None
2690
2691 clock = CTFWriter.Clock.__new__(CTFWriter.Clock)
2692 clock._c = clock_instance
2693
2694 return clock
2695
2696 def payload(self, field_name):
2697 """
2698 Get a field from event.
2699 """
2700
2701 native_instance = nbt._bt_ctf_event_get_payload(self._e,
2702 str(field_name))
2703
2704 if native_instance is None:
2705 raise ValueError("Could not get event payload.")
2706
2707 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2708
2709 def set_payload(self, field_name, value_field):
2710 """
2711 Set a manually created field as an event's payload.
2712 """
2713
2714 if not isinstance(value, CTFWriter.Field):
2715 raise TypeError("Invalid value type.")
2716
2717 ret = nbt._bt_ctf_event_set_payload(self._e, str(field_name),
2718 value_field._f)
2719
2720 if ret < 0:
2721 raise ValueError("Could not set event field payload.")
2722
2723 class StreamClass:
2724 def __init__(self, name):
2725 """
2726 Create a new stream class of the given name.
2727 """
2728
2729 self._sc = nbt._bt_ctf_stream_class_create(name)
2730
2731 if self._sc is None:
2732 raise ValueError("Stream class creation failed.")
2733
2734 def __del__(self):
2735 nbt._bt_ctf_stream_class_put(self._sc)
2736
2737 @property
2738 def name(self):
2739 """
2740 Get a stream class' name.
2741 """
2742
2743 name = nbt._bt_ctf_stream_class_get_name(self._sc)
2744
2745 if name is None:
2746 raise TypeError("Could not get StreamClass name")
2747
2748 return name
2749
2750 @property
2751 def clock(self):
2752 """
2753 Get a stream class' clock.
2754 """
2755
2756 clock_instance = nbt._bt_ctf_stream_class_get_clock(self._sc)
2757
2758 if clock_instance is None:
2759 return None
2760
2761 clock = CTFWriter.Clock.__new__(CTFWriter.Clock)
2762 clock._c = clock_instance
2763
2764 return clock
2765
2766 @clock.setter
2767 def clock(self, clock):
2768 """
2769 Assign a clock to a stream class.
2770 """
2771
2772 if not isinstance(clock, CTFWriter.Clock):
2773 raise TypeError("Invalid clock type.")
2774
2775 ret = nbt._bt_ctf_stream_class_set_clock(self._sc, clock._c)
2776
2777 if ret < 0:
2778 raise ValueError("Could not set stream class clock.")
2779
2780 @property
2781 def id(self):
2782 """
2783 Get a stream class' id.
2784 """
2785
2786 ret = nbt._bt_ctf_stream_class_get_id(self._sc)
2787
2788 if ret < 0:
2789 raise TypeError("Could not get StreamClass id")
2790
2791 return ret
2792
2793 @id.setter
2794 def id(self, id):
2795 """
2796 Assign an id to a stream class.
2797 """
2798
2799 ret = nbt._bt_ctf_stream_class_set_id(self._sc, id)
2800
2801 if ret < 0:
2802 raise TypeError("Could not set stream class id.")
2803
2804 @property
2805 def event_classes(self):
2806 """
2807 Generator returning the stream class' event classes.
2808 """
2809
2810 count = nbt._bt_ctf_stream_class_get_event_class_count(self._sc)
2811
2812 if count < 0:
2813 raise TypeError("Could not get StreamClass' event class count")
2814
2815 for i in range(count):
2816 event_class_native = nbt._bt_ctf_stream_class_get_event_class(self._sc, i)
2817
2818 if event_class_native is None:
2819 msg = "Could not get StreamClass' event class at index {}".format(i)
2820 raise TypeError(msg)
2821
2822 event_class = CTFWriter.EventClass.__new__(CTFWriter.EventClass)
2823 event_class._ec = event_class_native
2824 yield event_class
2825
2826 def add_event_class(self, event_class):
2827 """
2828 Add an event class to a stream class. New events can be added even after a
2829 stream has been instantiated and events have been appended. However, a stream
2830 will not accept events of a class that has not been added to the stream
2831 class beforehand.
2832 """
2833
2834 if not isinstance(event_class, CTFWriter.EventClass):
2835 raise TypeError("Invalid event_class type.")
2836
2837 ret = nbt._bt_ctf_stream_class_add_event_class(self._sc,
2838 event_class._ec)
2839
2840 if ret < 0:
2841 raise ValueError("Could not add event class.")
2842
2843 @property
2844 def packet_context_type(self):
2845 """
2846 Get the StreamClass' packet context type (StructureFieldDeclaration)
2847 """
2848
2849 field_type_native = nbt._bt_ctf_stream_class_get_packet_context_type(self._sc)
2850
2851 if field_type_native is None:
2852 raise ValueError("Invalid StreamClass")
2853
2854 field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2855
2856 return field_type
2857
2858 @packet_context_type.setter
2859 def packet_context_type(self, field_type):
2860 """
2861 Set a StreamClass' packet context type. Must be of type
2862 StructureFieldDeclaration.
2863 """
2864
2865 if not isinstance(field_type, CTFWriter.StructureFieldDeclaration):
2866 raise TypeError("field_type argument must be of type StructureFieldDeclaration.")
2867
2868 ret = nbt._bt_ctf_stream_class_set_packet_context_type(self._sc,
2869 field_type._ft)
2870
2871 if ret < 0:
2872 raise ValueError("Failed to set packet context type.")
2873
2874 class Stream:
2875 def __init__(self):
2876 raise NotImplementedError("Stream cannot be instantiated; use Writer.create_stream()")
2877
2878 def __del__(self):
2879 nbt._bt_ctf_stream_put(self._s)
2880
2881 @property
2882 def discarded_events(self):
2883 """
2884 Get a stream's discarded event count.
2885 """
2886
2887 ret, count = nbt._bt_ctf_stream_get_discarded_events_count(self._s)
2888
2889 if ret < 0:
2890 raise ValueError("Could not get the stream's discarded events count")
2891
2892 return count
2893
2894 def append_discarded_events(self, event_count):
2895 """
2896 Increase the current packet's discarded event count.
2897 """
2898
2899 nbt._bt_ctf_stream_append_discarded_events(self._s, event_count)
2900
2901 def append_event(self, event):
2902 """
2903 Append "event" to the stream's current packet. The stream's associated clock
2904 will be sampled during this call. The event shall not be modified after
2905 being appended to a stream.
2906 """
2907
2908 ret = nbt._bt_ctf_stream_append_event(self._s, event._e)
2909
2910 if ret < 0:
2911 raise ValueError("Could not append event to stream.")
2912
2913 @property
2914 def packet_context(self):
2915 """
2916 Get a Stream's packet context field (a StructureField).
2917 """
2918
2919 native_field = nbt._bt_ctf_stream_get_packet_context(self._s)
2920
2921 if native_field is None:
2922 raise ValueError("Invalid Stream.")
2923
2924 return CTFWriter.Field._create_field_from_native_instance(native_field)
2925
2926 @packet_context.setter
2927 def packet_context(self, field):
2928 """
2929 Set a Stream's packet context field (must be a StructureField).
2930 """
2931
2932 if not isinstance(field, CTFWriter.StructureField):
2933 raise TypeError("Argument field must be of type StructureField")
2934
2935 ret = nbt._bt_ctf_stream_set_packet_context(self._s, field._f)
2936
2937 if ret < 0:
2938 raise ValueError("Invalid packet context field.")
2939
2940 def flush(self):
2941 """
2942 The stream's current packet's events will be flushed to disk. Events
2943 subsequently appended to the stream will be added to a new packet.
2944 """
2945
2946 ret = nbt._bt_ctf_stream_flush(self._s)
2947
2948 if ret < 0:
2949 raise ValueError("Could not flush stream.")
2950
2951 class Writer:
2952 def __init__(self, path):
2953 """
2954 Create a new writer that will produce a trace in the given path.
2955 """
2956
2957 self._w = nbt._bt_ctf_writer_create(path)
2958
2959 if self._w is None:
2960 raise ValueError("Writer creation failed.")
2961
2962 def __del__(self):
2963 nbt._bt_ctf_writer_put(self._w)
2964
2965 def create_stream(self, stream_class):
2966 """
2967 Create a new stream instance and register it to the writer.
2968 """
2969
2970 if not isinstance(stream_class, CTFWriter.StreamClass):
2971 raise TypeError("Invalid stream_class type.")
2972
2973 stream = CTFWriter.Stream.__new__(CTFWriter.Stream)
2974 stream._s = nbt._bt_ctf_writer_create_stream(self._w, stream_class._sc)
2975
2976 return stream
2977
2978 def add_environment_field(self, name, value):
2979 """
2980 Add an environment field to the trace.
2981 """
2982
2983 ret = nbt._bt_ctf_writer_add_environment_field(self._w, str(name),
2984 str(value))
2985
2986 if ret < 0:
2987 raise ValueError("Could not add environment field to trace.")
2988
2989 def add_clock(self, clock):
2990 """
2991 Add a clock to the trace. Clocks assigned to stream classes must be
2992 registered to the writer.
2993 """
2994
2995 ret = nbt._bt_ctf_writer_add_clock(self._w, clock._c)
2996
2997 if ret < 0:
2998 raise ValueError("Could not add clock to Writer.")
2999
3000 @property
3001 def metadata(self):
3002 """
3003 Get the trace's TSDL meta-data.
3004 """
3005
3006 return nbt._bt_ctf_writer_get_metadata_string(self._w)
3007
3008 def flush_metadata(self):
3009 """
3010 Flush the trace's metadata to the metadata file.
3011 """
3012
3013 nbt._bt_ctf_writer_flush_metadata(self._w)
3014
3015 @property
3016 def byte_order(self):
3017 """
3018 Get the trace's byte order. Must be a constant from the ByteOrder
3019 class.
3020 """
3021
3022 raise NotImplementedError("Getter not implemented.")
3023
3024 @byte_order.setter
3025 def byte_order(self, byte_order):
3026 """
3027 Set the trace's byte order. Must be a constant from the ByteOrder
3028 class. Defaults to the host machine's endianness
3029 """
3030
3031 ret = nbt._bt_ctf_writer_set_byte_order(self._w, byte_order)
3032
3033 if ret < 0:
3034 raise ValueError("Could not set trace's byte order.")
This page took 0.095051 seconds and 3 git commands to generate.