3 # Babeltrace writer interface Python module
5 # Copyright 2012-2015 EfficiOS Inc.
7 # Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 # Permission is hereby granted, free of charge, to any person obtaining a copy
10 # of this software and associated documentation files (the "Software"), to deal
11 # in the Software without restriction, including without limitation the rights
12 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 # copies of the Software, and to permit persons to whom the Software is
14 # furnished to do so, subject to the following conditions:
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 import babeltrace
.nativebt
as nbt
28 import babeltrace
.common
as common
32 # Used to compare to -1ULL in error checks
33 _MAX_UINT64
= 0xFFFFFFFFFFFFFFFF
36 class EnumerationMapping
:
38 Mapping from an enumeration label to a range of integers.
41 def __init__(self
, name
, start
, end
):
43 Creates an enumeration mapping, where label *name* is mapped to
44 the [*start*, *end*] range of integers (*end* is included).
46 Set *start* and *end* to the same value to create an enumeration
47 mapping to a single value.
57 A CTF clock allows the description of the system's clock topology, as
58 well as the definition of each clock's parameters.
60 :class:`Clock` objects must be registered to a :class:`Writer`
61 object (see :meth:`Writer.add_clock`), as well as be registered to
62 a :class:`StreamClass` object (see :attr:`StreamClass.clock`).
65 def __init__(self
, name
):
67 Creates a default CTF clock named *name*.
69 :exc:`ValueError` is raised on error.
72 self
._c
= nbt
._bt
_ctf
_clock
_create
(name
)
75 raise ValueError("Invalid clock name.")
78 nbt
._bt
_ctf
_clock
_put
(self
._c
)
85 Set this attribute to change the clock's name.
87 :exc:`ValueError` is raised on error.
90 name
= nbt
._bt
_ctf
_clock
_get
_name
(self
._c
)
93 raise ValueError("Invalid clock instance.")
98 def description(self
):
100 Clock description (string).
102 Set this attribute to change the clock's description.
104 :exc:`ValueError` is raised on error.
107 return nbt
._bt
_ctf
_clock
_get
_description
(self
._c
)
110 def description(self
, desc
):
111 ret
= nbt
._bt
_ctf
_clock
_set
_description
(self
._c
, str(desc
))
114 raise ValueError("Invalid clock description.")
119 Clock frequency in Hz (integer).
121 Set this attribute to change the clock's frequency.
123 :exc:`ValueError` is raised on error.
126 freq
= nbt
._bt
_ctf
_clock
_get
_frequency
(self
._c
)
128 if freq
== _MAX_UINT64
:
129 raise ValueError("Invalid clock instance")
134 def frequency(self
, freq
):
135 ret
= nbt
._bt
_ctf
_clock
_set
_frequency
(self
._c
, freq
)
138 raise ValueError("Invalid frequency value.")
143 Clock precision in clock ticks (integer).
145 Set this attribute to change the clock's precision.
147 :exc:`ValueError` is raised on error.
150 precision
= nbt
._bt
_ctf
_clock
_get
_precision
(self
._c
)
152 if precision
== _MAX_UINT64
:
153 raise ValueError("Invalid clock instance")
158 def precision(self
, precision
):
159 ret
= nbt
._bt
_ctf
_clock
_set
_precision
(self
._c
, precision
)
162 def offset_seconds(self
):
164 Clock offset in seconds since POSIX.1 Epoch (integer).
166 Set this attribute to change the clock's offset in seconds.
168 :exc:`ValueError` is raised on error.
171 offset_s
= nbt
._bt
_ctf
_clock
_get
_offset
_s
(self
._c
)
173 if offset_s
== _MAX_UINT64
:
174 raise ValueError("Invalid clock instance")
178 @offset_seconds.setter
179 def offset_seconds(self
, offset_s
):
180 ret
= nbt
._bt
_ctf
_clock
_set
_offset
_s
(self
._c
, offset_s
)
183 raise ValueError("Invalid offset value.")
188 Clock offset in ticks since (POSIX.1 Epoch +
189 :attr:`offset_seconds`).
191 Set this attribute to change the clock's offset.
193 :exc:`ValueError` is raised on error.
196 offset
= nbt
._bt
_ctf
_clock
_get
_offset
(self
._c
)
198 if offset
== _MAX_UINT64
:
199 raise ValueError("Invalid clock instance")
204 def offset(self
, offset
):
205 ret
= nbt
._bt
_ctf
_clock
_set
_offset
(self
._c
, offset
)
208 raise ValueError("Invalid offset value.")
213 ``True`` if this clock is absolute, i.e. if the clock is a
214 global reference across the other clocks of the trace.
216 Set this attribute to change the clock's absolute state
219 :exc:`ValueError` is raised on error.
222 is_absolute
= nbt
._bt
_ctf
_clock
_get
_is
_absolute
(self
._c
)
224 if is_absolute
== -1:
225 raise ValueError("Invalid clock instance")
227 return False if is_absolute
== 0 else True
230 def absolute(self
, is_absolute
):
231 ret
= nbt
._bt
_ctf
_clock
_set
_is
_absolute
(self
._c
, int(is_absolute
))
234 raise ValueError("Could not set the clock absolute attribute.")
239 Clock UUID (an :class:`uuid.UUID` object).
241 Set this attribute to change the clock's UUID.
243 :exc:`ValueError` is raised on error.
249 ret
, value
= nbt
._bt
_python
_ctf
_clock
_get
_uuid
_index
(self
._c
, i
)
252 raise ValueError("Invalid clock instance")
254 uuid_list
.append(value
)
256 return UUID(bytes
=bytes(uuid_list
))
259 def uuid(self
, uuid
):
260 uuid_bytes
= uuid
.bytes
262 if len(uuid_bytes
) != 16:
263 raise ValueError("Invalid UUID provided. UUID length must be 16 bytes")
265 for i
in range(len(uuid_bytes
)):
266 ret
= nbt
._bt
_python
_ctf
_clock
_set
_uuid
_index
(self
._c
, i
,
270 raise ValueError("Invalid clock instance")
275 Clock current time; nanoseconds (integer) since clock origin
276 (POSIX.1 Epoch + :attr:`offset_seconds` + :attr:`offset`).
278 Set this attribute to change the clock's current time.
280 :exc:`ValueError` is raised on error.
283 time
= nbt
._bt
_ctf
_clock
_get
_time
(self
._c
)
285 if time
== _MAX_UINT64
:
286 raise ValueError("Invalid clock instance")
291 def time(self
, time
):
292 ret
= nbt
._bt
_ctf
_clock
_set
_time
(self
._c
, time
)
295 raise ValueError("Invalid time value.")
298 class FieldDeclaration
:
300 Base class of all field declarations. This class is not meant to
301 be instantiated by the user; use one of the concrete field
302 declaration subclasses instead.
306 # These values are based on the bt_ctf_integer_base enum
307 # declared in event-types.h.
308 INTEGER_BASE_UNKNOWN
= -1
309 INTEGER_BASE_BINARY
= 2
310 INTEGER_BASE_OCTAL
= 8
311 INTEGER_BASE_DECIMAL
= 10
312 INTEGER_BASE_HEXADECIMAL
= 16
316 raise ValueError("FieldDeclaration creation failed.")
319 nbt
._bt
_ctf
_field
_type
_put
(self
._ft
)
322 def _create_field_declaration_from_native_instance(
323 native_field_declaration
):
325 common
.CTFTypeId
.INTEGER
: IntegerFieldDeclaration
,
326 common
.CTFTypeId
.FLOAT
: FloatFieldDeclaration
,
327 common
.CTFTypeId
.ENUM
: EnumerationFieldDeclaration
,
328 common
.CTFTypeId
.STRING
: StringFieldDeclaration
,
329 common
.CTFTypeId
.STRUCT
: StructureFieldDeclaration
,
330 common
.CTFTypeId
.VARIANT
: VariantFieldDeclaration
,
331 common
.CTFTypeId
.ARRAY
: ArrayFieldDeclaration
,
332 common
.CTFTypeId
.SEQUENCE
: SequenceFieldDeclaration
335 field_type_id
= nbt
._bt
_ctf
_field
_type
_get
_type
_id
(native_field_declaration
)
337 if field_type_id
== common
.CTFTypeId
.UNKNOWN
:
338 raise TypeError("Invalid field instance")
340 declaration
= Field
.__new
__(Field
)
341 declaration
._ft
= native_field_declaration
342 declaration
.__class
__ = type_dict
[field_type_id
]
349 Field alignment in bits (integer).
351 Set this attribute to change this field's alignment.
353 :exc:`ValueError` is raised on error.
356 return nbt
._bt
_ctf
_field
_type
_get
_alignment
(self
._ft
)
359 def alignment(self
, alignment
):
360 ret
= nbt
._bt
_ctf
_field
_type
_set
_alignment
(self
._ft
, alignment
)
363 raise ValueError("Invalid alignment value.")
366 def byte_order(self
):
368 Field byte order (one of :class:`babeltrace.common.ByteOrder`
371 Set this attribute to change this field's byte order.
373 :exc:`ValueError` is raised on error.
376 return nbt
._bt
_ctf
_field
_type
_get
_byte
_order
(self
._ft
)
379 def byte_order(self
, byte_order
):
380 ret
= nbt
._bt
_ctf
_field
_type
_set
_byte
_order
(self
._ft
, byte_order
)
383 raise ValueError("Could not set byte order value.")
386 class IntegerFieldDeclaration(FieldDeclaration
):
387 def __init__(self
, size
):
389 Create a new integer field declaration of the given size.
391 self
._ft
= nbt
._bt
_ctf
_field
_type
_integer
_create
(size
)
397 Get an integer's size.
400 ret
= nbt
._bt
_ctf
_field
_type
_integer
_get
_size
(self
._ft
)
403 raise ValueError("Could not get Integer's size attribute.")
410 Get an integer's signedness attribute.
413 ret
= nbt
._bt
_ctf
_field
_type
_integer
_get
_signed
(self
._ft
)
416 raise ValueError("Could not get Integer's signed attribute.")
423 def signed(self
, signed
):
425 Set an integer's signedness attribute.
428 ret
= nbt
._bt
_ctf
_field
_type
_integer
_set
_signed
(self
._ft
, signed
)
431 raise ValueError("Could not set Integer's signed attribute.")
436 Get the integer's base used to pretty-print the resulting trace.
437 Returns a constant from the FieldDeclaration.IntegerBase class.
440 return nbt
._bt
_ctf
_field
_type
_integer
_get
_base
(self
._ft
)
443 def base(self
, base
):
445 Set the integer's base used to pretty-print the resulting trace.
446 The base must be a constant of the FieldDeclarationIntegerBase class.
449 ret
= nbt
._bt
_ctf
_field
_type
_integer
_set
_base
(self
._ft
, base
)
452 raise ValueError("Could not set Integer's base.")
457 Get the integer's encoding (one of the constants of the
458 CTFStringEncoding class).
459 Returns a constant from the CTFStringEncoding class.
462 return nbt
._bt
_ctf
_field
_type
_integer
_get
_encoding
(self
._ft
)
465 def encoding(self
, encoding
):
467 An integer encoding may be set to signal that the integer must be printed
468 as a text character. Must be a constant from the CTFStringEncoding class.
471 ret
= nbt
._bt
_ctf
_field
_type
_integer
_set
_encoding
(self
._ft
, encoding
)
474 raise ValueError("Could not set Integer's encoding.")
477 class EnumerationFieldDeclaration(FieldDeclaration
):
478 def __init__(self
, integer_type
):
480 Create a new enumeration field declaration with the given underlying container type.
482 isinst
= isinstance(integer_type
, IntegerFieldDeclaration
)
484 if integer_type
is None or not isinst
:
485 raise TypeError("Invalid integer container.")
487 self
._ft
= nbt
._bt
_ctf
_field
_type
_enumeration
_create
(integer_type
._ft
)
493 Get the enumeration's underlying container type.
496 ret
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_container
_type
(self
._ft
)
499 raise TypeError("Invalid enumeration declaration")
501 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
503 def add_mapping(self
, name
, range_start
, range_end
):
505 Add a mapping to the enumeration. The range's values are inclusive.
508 if range_start
< 0 or range_end
< 0:
509 ret
= nbt
._bt
_ctf
_field
_type
_enumeration
_add
_mapping
(self
._ft
,
514 ret
= nbt
._bt
_ctf
_field
_type
_enumeration
_add
_mapping
_unsigned
(self
._ft
,
520 raise ValueError("Could not add mapping to enumeration declaration.")
525 Generator returning instances of EnumerationMapping.
528 signed
= self
.container
.signed
530 count
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_count
(self
._ft
)
532 for i
in range(count
):
534 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
(self
._ft
, i
)
536 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
_unsigned
(self
._ft
, i
)
539 msg
= "Could not get Enumeration mapping at index {}".format(i
)
542 name
, range_start
, range_end
= ret
543 yield EnumerationMapping(name
, range_start
, range_end
)
545 def get_mapping_by_name(self
, name
):
547 Get a mapping by name (EnumerationMapping).
550 index
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_index
_by
_name
(self
._ft
, name
)
555 if self
.container
.signed
:
556 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
(self
._ft
, index
)
558 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
_unsigned
(self
._ft
, index
)
561 msg
= "Could not get Enumeration mapping at index {}".format(i
)
564 name
, range_start
, range_end
= ret
566 return EnumerationMapping(name
, range_start
, range_end
)
568 def get_mapping_by_value(self
, value
):
570 Get a mapping by value (EnumerationMapping).
574 index
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_index
_by
_value
(self
._ft
, value
)
576 index
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_index
_by
_unsigned
_value
(self
._ft
, value
)
581 if self
.container
.signed
:
582 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
(self
._ft
, index
)
584 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
_unsigned
(self
._ft
, index
)
587 msg
= "Could not get Enumeration mapping at index {}".format(i
)
590 name
, range_start
, range_end
= ret
592 return EnumerationMapping(name
, range_start
, range_end
)
595 class FloatFieldDeclaration(FieldDeclaration
):
603 Create a new floating point field declaration.
606 self
._ft
= nbt
._bt
_ctf
_field
_type
_floating
_point
_create
()
610 def exponent_digits(self
):
612 Get the number of exponent digits used to store the floating point field.
615 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_get
_exponent
_digits
(self
._ft
)
619 "Could not get Floating point exponent digit count")
623 @exponent_digits.setter
624 def exponent_digits(self
, exponent_digits
):
626 Set the number of exponent digits to use to store the floating point field.
627 The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG which
628 are defined as constants of this class.
631 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_set
_exponent
_digits
(self
._ft
,
635 raise ValueError("Could not set exponent digit count.")
638 def mantissa_digits(self
):
640 Get the number of mantissa digits used to store the floating point field.
643 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_get
_mantissa
_digits
(self
._ft
)
646 raise TypeError("Could not get Floating point mantissa digit count")
650 @mantissa_digits.setter
651 def mantissa_digits(self
, mantissa_digits
):
653 Set the number of mantissa digits to use to store the floating point field.
654 The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG which
655 are defined as constants of this class.
658 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_set
_mantissa
_digits
(self
._ft
,
662 raise ValueError("Could not set mantissa digit count.")
665 class FloatingPointFieldDeclaration(FloatFieldDeclaration
):
669 class StructureFieldDeclaration(FieldDeclaration
):
672 Create a new structure field declaration.
675 self
._ft
= nbt
._bt
_ctf
_field
_type
_structure
_create
()
678 def add_field(self
, field_type
, field_name
):
680 Add a field of type "field_type" to the structure.
683 ret
= nbt
._bt
_ctf
_field
_type
_structure
_add
_field
(self
._ft
,
688 raise ValueError("Could not add field to structure.")
693 Generator returning the structure's field as tuples of (field name, field declaration).
696 count
= nbt
._bt
_ctf
_field
_type
_structure
_get
_field
_count
(self
._ft
)
699 raise TypeError("Could not get Structure field count")
701 for i
in range(count
):
702 field_name
= nbt
._bt
_python
_ctf
_field
_type
_structure
_get
_field
_name
(self
._ft
, i
)
704 if field_name
is None:
705 msg
= "Could not get Structure field name at index {}".format(i
)
708 field_type_native
= nbt
._bt
_python
_ctf
_field
_type
_structure
_get
_field
_type
(self
._ft
, i
)
710 if field_type_native
is None:
711 msg
= "Could not get Structure field type at index {}".format(i
)
714 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
715 yield (field_name
, field_type
)
717 def get_field_by_name(self
, name
):
719 Get a field declaration by name (FieldDeclaration).
722 field_type_native
= nbt
._bt
_ctf
_field
_type
_structure
_get
_field
_type
_by
_name
(self
._ft
, name
)
724 if field_type_native
is None:
725 msg
= "Could not find Structure field with name {}".format(name
)
728 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
731 class VariantFieldDeclaration(FieldDeclaration
):
732 def __init__(self
, enum_tag
, tag_name
):
734 Create a new variant field declaration.
737 isinst
= isinstance(enum_tag
, EnumerationFieldDeclaration
)
738 if enum_tag
is None or not isinst
:
739 raise TypeError("Invalid tag type; must be of type EnumerationFieldDeclaration.")
741 self
._ft
= nbt
._bt
_ctf
_field
_type
_variant
_create
(enum_tag
._ft
,
748 Get the variant's tag name.
751 ret
= nbt
._bt
_ctf
_field
_type
_variant
_get
_tag
_name
(self
._ft
)
754 raise TypeError("Could not get Variant tag name")
761 Get the variant's tag type.
764 ret
= nbt
._bt
_ctf
_field
_type
_variant
_get
_tag
_type
(self
._ft
)
767 raise TypeError("Could not get Variant tag type")
769 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
771 def add_field(self
, field_type
, field_name
):
773 Add a field of type "field_type" to the variant.
776 ret
= nbt
._bt
_ctf
_field
_type
_variant
_add
_field
(self
._ft
,
781 raise ValueError("Could not add field to variant.")
786 Generator returning the variant's field as tuples of (field name, field declaration).
789 count
= nbt
._bt
_ctf
_field
_type
_variant
_get
_field
_count
(self
._ft
)
792 raise TypeError("Could not get Variant field count")
794 for i
in range(count
):
795 field_name
= nbt
._bt
_python
_ctf
_field
_type
_variant
_get
_field
_name
(self
._ft
, i
)
797 if field_name
is None:
798 msg
= "Could not get Variant field name at index {}".format(i
)
801 field_type_native
= nbt
._bt
_python
_ctf
_field
_type
_variant
_get
_field
_type
(self
._ft
, i
)
803 if field_type_native
is None:
804 msg
= "Could not get Variant field type at index {}".format(i
)
807 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
808 yield (field_name
, field_type
)
810 def get_field_by_name(self
, name
):
812 Get a field declaration by name (FieldDeclaration).
815 field_type_native
= nbt
._bt
_ctf
_field
_type
_variant
_get
_field
_type
_by
_name
(self
._ft
,
818 if field_type_native
is None:
819 msg
= "Could not find Variant field with name {}".format(name
)
822 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
824 def get_field_from_tag(self
, tag
):
826 Get a field declaration from tag (EnumerationField).
829 field_type_native
= nbt
._bt
_ctf
_field
_type
_variant
_get
_field
_type
_from
_tag
(self
._ft
, tag
._f
)
831 if field_type_native
is None:
832 msg
= "Could not find Variant field with tag value {}".format(tag
.value
)
835 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
838 class ArrayFieldDeclaration(FieldDeclaration
):
839 def __init__(self
, element_type
, length
):
841 Create a new array field declaration.
844 self
._ft
= nbt
._bt
_ctf
_field
_type
_array
_create
(element_type
._ft
,
849 def element_type(self
):
851 Get the array's element type.
854 ret
= nbt
._bt
_ctf
_field
_type
_array
_get
_element
_type
(self
._ft
)
857 raise TypeError("Could not get Array element type")
859 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
864 Get the array's length.
867 ret
= nbt
._bt
_ctf
_field
_type
_array
_get
_length
(self
._ft
)
870 raise TypeError("Could not get Array length")
875 class SequenceFieldDeclaration(FieldDeclaration
):
876 def __init__(self
, element_type
, length_field_name
):
878 Create a new sequence field declaration.
881 self
._ft
= nbt
._bt
_ctf
_field
_type
_sequence
_create
(element_type
._ft
,
882 str(length_field_name
))
886 def element_type(self
):
888 Get the sequence's element type.
891 ret
= nbt
._bt
_ctf
_field
_type
_sequence
_get
_element
_type
(self
._ft
)
894 raise TypeError("Could not get Sequence element type")
896 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
899 def length_field_name(self
):
901 Get the sequence's length field name.
904 ret
= nbt
._bt
_ctf
_field
_type
_sequence
_get
_length
_field
_name
(self
._ft
)
907 raise TypeError("Could not get Sequence length field name")
912 class StringFieldDeclaration(FieldDeclaration
):
915 Create a new string field declaration.
918 self
._ft
= nbt
._bt
_ctf
_field
_type
_string
_create
()
924 Get a string declaration's encoding (a constant from the CTFStringEncoding class).
927 return nbt
._bt
_ctf
_field
_type
_string
_get
_encoding
(self
._ft
)
930 def encoding(self
, encoding
):
932 Set a string declaration's encoding. Must be a constant from the CTFStringEncoding class.
935 ret
= nbt
._bt
_ctf
_field
_type
_string
_set
_encoding
(self
._ft
, encoding
)
937 raise ValueError("Could not set string encoding.")
941 def create_field(field_type
):
943 Create an instance of a field.
945 isinst
= isinstance(field_type
, FieldDeclaration
)
947 if field_type
is None or not isinst
:
948 raise TypeError("Invalid field_type. Type must be a FieldDeclaration-derived class.")
950 if isinstance(field_type
, IntegerFieldDeclaration
):
951 return IntegerField(field_type
)
952 elif isinstance(field_type
, EnumerationFieldDeclaration
):
953 return EnumerationField(field_type
)
954 elif isinstance(field_type
, FloatFieldDeclaration
):
955 return FloatingPointField(field_type
)
956 elif isinstance(field_type
, StructureFieldDeclaration
):
957 return StructureField(field_type
)
958 elif isinstance(field_type
, VariantFieldDeclaration
):
959 return VariantField(field_type
)
960 elif isinstance(field_type
, ArrayFieldDeclaration
):
961 return ArrayField(field_type
)
962 elif isinstance(field_type
, SequenceFieldDeclaration
):
963 return SequenceField(field_type
)
964 elif isinstance(field_type
, StringFieldDeclaration
):
965 return StringField(field_type
)
970 Base class, do not instantiate.
973 def __init__(self
, field_type
):
974 if not isinstance(field_type
, FieldDeclaration
):
975 raise TypeError("Invalid field_type argument.")
977 self
._f
= nbt
._bt
_ctf
_field
_create
(field_type
._ft
)
980 raise ValueError("Field creation failed.")
983 nbt
._bt
_ctf
_field
_put
(self
._f
)
986 def _create_field_from_native_instance(native_field_instance
):
988 common
.CTFTypeId
.INTEGER
: IntegerField
,
989 common
.CTFTypeId
.FLOAT
: FloatingPointField
,
990 common
.CTFTypeId
.ENUM
: EnumerationField
,
991 common
.CTFTypeId
.STRING
: StringField
,
992 common
.CTFTypeId
.STRUCT
: StructureField
,
993 common
.CTFTypeId
.VARIANT
: VariantField
,
994 common
.CTFTypeId
.ARRAY
: ArrayField
,
995 common
.CTFTypeId
.SEQUENCE
: SequenceField
998 field_type
= nbt
._bt
_python
_get
_field
_type
(native_field_instance
)
1000 if field_type
== common
.CTFTypeId
.UNKNOWN
:
1001 raise TypeError("Invalid field instance")
1003 field
= Field
.__new
__(Field
)
1004 field
._f
= native_field_instance
1005 field
.__class
__ = type_dict
[field_type
]
1010 def declaration(self
):
1011 native_field_type
= nbt
._bt
_ctf
_field
_get
_type
(self
._f
)
1013 if native_field_type
is None:
1014 raise TypeError("Invalid field instance")
1015 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(
1019 class IntegerField(Field
):
1023 Get an integer field's value.
1026 signedness
= nbt
._bt
_python
_field
_integer
_get
_signedness
(self
._f
)
1029 raise TypeError("Invalid integer instance.")
1032 ret
, value
= nbt
._bt
_ctf
_field
_unsigned
_integer
_get
_value
(self
._f
)
1034 ret
, value
= nbt
._bt
_ctf
_field
_signed
_integer
_get
_value
(self
._f
)
1037 raise ValueError("Could not get integer field value.")
1042 def value(self
, value
):
1044 Set an integer field's value.
1047 if not isinstance(value
, int):
1048 raise TypeError("IntegerField's value must be an int")
1050 signedness
= nbt
._bt
_python
_field
_integer
_get
_signedness
(self
._f
)
1052 raise TypeError("Invalid integer instance.")
1055 ret
= nbt
._bt
_ctf
_field
_unsigned
_integer
_set
_value
(self
._f
, value
)
1057 ret
= nbt
._bt
_ctf
_field
_signed
_integer
_set
_value
(self
._f
, value
)
1060 raise ValueError("Could not set integer field value.")
1063 class EnumerationField(Field
):
1065 def container(self
):
1067 Return the enumeration's underlying container field (an integer field).
1070 container
= IntegerField
.__new
__(IntegerField
)
1071 container
._f
= nbt
._bt
_ctf
_field
_enumeration
_get
_container
(self
._f
)
1073 if container
._f
is None:
1074 raise TypeError("Invalid enumeration field type.")
1081 Get the enumeration field's mapping name.
1084 value
= nbt
._bt
_ctf
_field
_enumeration
_get
_mapping
_name
(self
._f
)
1087 raise ValueError("Could not get enumeration's mapping name.")
1092 def value(self
, value
):
1094 Set the enumeration field's value. Must be an integer as mapping names
1098 if not isinstance(value
, int):
1099 raise TypeError("EnumerationField value must be an int")
1101 self
.container
.value
= value
1104 class FloatingPointField(Field
):
1108 Get a floating point field's value.
1111 ret
, value
= nbt
._bt
_ctf
_field
_floating
_point
_get
_value
(self
._f
)
1114 raise ValueError("Could not get floating point field value.")
1119 def value(self
, value
):
1121 Set a floating point field's value.
1124 if not isinstance(value
, int) and not isinstance(value
, float):
1125 raise TypeError("Value must be either a float or an int")
1127 ret
= nbt
._bt
_ctf
_field
_floating
_point
_set
_value
(self
._f
, float(value
))
1130 raise ValueError("Could not set floating point field value.")
1133 # oops!! This class is provided to ensure backward-compatibility since
1134 # a stable release publicly exposed this abomination.
1135 class FloatFieldingPoint(FloatingPointField
):
1139 class StructureField(Field
):
1140 def field(self
, field_name
):
1142 Get the structure's field corresponding to the provided field name.
1145 native_instance
= nbt
._bt
_ctf
_field
_structure
_get
_field
(self
._f
,
1148 if native_instance
is None:
1149 raise ValueError("Invalid field_name provided.")
1151 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1154 class VariantField(Field
):
1155 def field(self
, tag
):
1157 Return the variant's selected field. The "tag" field is the selector enum field.
1160 native_instance
= nbt
._bt
_ctf
_field
_variant
_get
_field
(self
._f
, tag
._f
)
1162 if native_instance
is None:
1163 raise ValueError("Invalid tag provided.")
1165 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1168 class ArrayField(Field
):
1169 def field(self
, index
):
1171 Return the array's field at position "index".
1174 native_instance
= nbt
._bt
_ctf
_field
_array
_get
_field
(self
._f
, index
)
1176 if native_instance
is None:
1177 raise IndexError("Invalid index provided.")
1179 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1182 class SequenceField(Field
):
1186 Get the sequence's length field (IntegerField).
1189 native_instance
= nbt
._bt
_ctf
_field
_sequence
_get
_length
(self
._f
)
1191 if native_instance
is None:
1194 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1197 def length(self
, length_field
):
1199 Set the sequence's length field (IntegerField).
1202 if not isinstance(length_field
, IntegerField
):
1203 raise TypeError("Invalid length field.")
1205 if length_field
.declaration
.signed
:
1206 raise TypeError("Sequence field length must be unsigned")
1208 ret
= nbt
._bt
_ctf
_field
_sequence
_set
_length
(self
._f
, length_field
._f
)
1211 raise ValueError("Could not set sequence length.")
1213 def field(self
, index
):
1215 Return the sequence's field at position "index".
1218 native_instance
= nbt
._bt
_ctf
_field
_sequence
_get
_field
(self
._f
, index
)
1220 if native_instance
is None:
1221 raise ValueError("Could not get sequence element at index.")
1223 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1226 class StringField(Field
):
1230 Get a string field's value.
1233 return nbt
._bt
_ctf
_field
_string
_get
_value
(self
._f
)
1236 def value(self
, value
):
1238 Set a string field's value.
1241 ret
= nbt
._bt
_ctf
_field
_string
_set
_value
(self
._f
, str(value
))
1244 raise ValueError("Could not set string field value.")
1248 def __init__(self
, name
):
1250 Create a new event class of the given name.
1253 self
._ec
= nbt
._bt
_ctf
_event
_class
_create
(name
)
1255 if self
._ec
is None:
1256 raise ValueError("Event class creation failed.")
1259 nbt
._bt
_ctf
_event
_class
_put
(self
._ec
)
1261 def add_field(self
, field_type
, field_name
):
1263 Add a field of type "field_type" to the event class.
1266 ret
= nbt
._bt
_ctf
_event
_class
_add
_field
(self
._ec
, field_type
._ft
,
1270 raise ValueError("Could not add field to event class.")
1275 Get the event class' name.
1278 name
= nbt
._bt
_ctf
_event
_class
_get
_name
(self
._ec
)
1281 raise TypeError("Could not get EventClass name")
1288 Get the event class' id. Returns a negative value if unset.
1291 id = nbt
._bt
_ctf
_event
_class
_get
_id
(self
._ec
)
1294 raise TypeError("Could not get EventClass id")
1301 Set the event class' id. Throws a TypeError if the event class
1302 is already registered to a stream class.
1305 ret
= nbt
._bt
_ctf
_event
_class
_set
_id
(self
._ec
, id)
1308 raise TypeError("Can't change an Event Class's id after it has been assigned to a stream class")
1311 def stream_class(self
):
1313 Get the event class' stream class. Returns None if unset.
1315 stream_class_native
= nbt
._bt
_ctf
_event
_class
_get
_stream
_class
(self
._ec
)
1317 if stream_class_native
is None:
1320 stream_class
= StreamClass
.__new
__(StreamClass
)
1321 stream_class
._sc
= stream_class_native
1328 Generator returning the event class' fields as tuples of (field name, field declaration).
1331 count
= nbt
._bt
_ctf
_event
_class
_get
_field
_count
(self
._ec
)
1334 raise TypeError("Could not get EventClass' field count")
1336 for i
in range(count
):
1337 field_name
= nbt
._bt
_python
_ctf
_event
_class
_get
_field
_name
(self
._ec
, i
)
1339 if field_name
is None:
1340 msg
= "Could not get EventClass' field name at index {}".format(i
)
1341 raise TypeError(msg
)
1343 field_type_native
= nbt
._bt
_python
_ctf
_event
_class
_get
_field
_type
(self
._ec
, i
)
1345 if field_type_native
is None:
1346 msg
= "Could not get EventClass' field type at index {}".format(i
)
1347 raise TypeError(msg
)
1349 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
1350 yield (field_name
, field_type
)
1352 def get_field_by_name(self
, name
):
1354 Get a field declaration by name (FieldDeclaration).
1357 field_type_native
= nbt
._bt
_ctf
_event
_class
_get
_field
_by
_name
(self
._ec
, name
)
1359 if field_type_native
is None:
1360 msg
= "Could not find EventClass field with name {}".format(name
)
1361 raise TypeError(msg
)
1363 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
1367 def __init__(self
, event_class
):
1369 Create a new event of the given event class.
1372 if not isinstance(event_class
, EventClass
):
1373 raise TypeError("Invalid event_class argument.")
1375 self
._e
= nbt
._bt
_ctf
_event
_create
(event_class
._ec
)
1378 raise ValueError("Event creation failed.")
1381 nbt
._bt
_ctf
_event
_put
(self
._e
)
1384 def event_class(self
):
1386 Get the event's class.
1389 event_class_native
= nbt
._bt
_ctf
_event
_get
_class
(self
._e
)
1391 if event_class_native
is None:
1394 event_class
= EventClass
.__new
__(EventClass
)
1395 event_class
._ec
= event_class_native
1401 Get a clock from event. Returns None if the event's class
1402 is not registered to a stream class.
1405 clock_instance
= nbt
._bt
_ctf
_event
_get
_clock
(self
._e
)
1407 if clock_instance
is None:
1410 clock
= Clock
.__new
__(Clock
)
1411 clock
._c
= clock_instance
1415 def payload(self
, field_name
):
1417 Get a field from event.
1420 native_instance
= nbt
._bt
_ctf
_event
_get
_payload
(self
._e
,
1423 if native_instance
is None:
1424 raise ValueError("Could not get event payload.")
1426 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1428 def set_payload(self
, field_name
, value_field
):
1430 Set a manually created field as an event's payload.
1433 if not isinstance(value
, Field
):
1434 raise TypeError("Invalid value type.")
1436 ret
= nbt
._bt
_ctf
_event
_set
_payload
(self
._e
, str(field_name
),
1440 raise ValueError("Could not set event field payload.")
1444 def __init__(self
, name
):
1446 Create a new stream class of the given name.
1449 self
._sc
= nbt
._bt
_ctf
_stream
_class
_create
(name
)
1451 if self
._sc
is None:
1452 raise ValueError("Stream class creation failed.")
1455 nbt
._bt
_ctf
_stream
_class
_put
(self
._sc
)
1460 Get a stream class' name.
1463 name
= nbt
._bt
_ctf
_stream
_class
_get
_name
(self
._sc
)
1466 raise TypeError("Could not get StreamClass name")
1473 Get a stream class' clock.
1476 clock_instance
= nbt
._bt
_ctf
_stream
_class
_get
_clock
(self
._sc
)
1478 if clock_instance
is None:
1481 clock
= Clock
.__new
__(Clock
)
1482 clock
._c
= clock_instance
1487 def clock(self
, clock
):
1489 Assign a clock to a stream class.
1492 if not isinstance(clock
, Clock
):
1493 raise TypeError("Invalid clock type.")
1495 ret
= nbt
._bt
_ctf
_stream
_class
_set
_clock
(self
._sc
, clock
._c
)
1498 raise ValueError("Could not set stream class clock.")
1503 Get a stream class' id.
1506 ret
= nbt
._bt
_ctf
_stream
_class
_get
_id
(self
._sc
)
1509 raise TypeError("Could not get StreamClass id")
1516 Assign an id to a stream class.
1519 ret
= nbt
._bt
_ctf
_stream
_class
_set
_id
(self
._sc
, id)
1522 raise TypeError("Could not set stream class id.")
1525 def event_classes(self
):
1527 Generator returning the stream class' event classes.
1530 count
= nbt
._bt
_ctf
_stream
_class
_get
_event
_class
_count
(self
._sc
)
1533 raise TypeError("Could not get StreamClass' event class count")
1535 for i
in range(count
):
1536 event_class_native
= nbt
._bt
_ctf
_stream
_class
_get
_event
_class
(self
._sc
, i
)
1538 if event_class_native
is None:
1539 msg
= "Could not get StreamClass' event class at index {}".format(i
)
1540 raise TypeError(msg
)
1542 event_class
= EventClass
.__new
__(EventClass
)
1543 event_class
._ec
= event_class_native
1546 def add_event_class(self
, event_class
):
1548 Add an event class to a stream class. New events can be added even after a
1549 stream has been instantiated and events have been appended. However, a stream
1550 will not accept events of a class that has not been added to the stream
1554 if not isinstance(event_class
, EventClass
):
1555 raise TypeError("Invalid event_class type.")
1557 ret
= nbt
._bt
_ctf
_stream
_class
_add
_event
_class
(self
._sc
,
1561 raise ValueError("Could not add event class.")
1564 def packet_context_type(self
):
1566 Get the StreamClass' packet context type (StructureFieldDeclaration)
1569 field_type_native
= nbt
._bt
_ctf
_stream
_class
_get
_packet
_context
_type
(self
._sc
)
1571 if field_type_native
is None:
1572 raise ValueError("Invalid StreamClass")
1574 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
1578 @packet_context_type.setter
1579 def packet_context_type(self
, field_type
):
1581 Set a StreamClass' packet context type. Must be of type
1582 StructureFieldDeclaration.
1585 if not isinstance(field_type
, StructureFieldDeclaration
):
1586 raise TypeError("field_type argument must be of type StructureFieldDeclaration.")
1588 ret
= nbt
._bt
_ctf
_stream
_class
_set
_packet
_context
_type
(self
._sc
,
1592 raise ValueError("Failed to set packet context type.")
1597 raise NotImplementedError("Stream cannot be instantiated; use Writer.create_stream()")
1600 nbt
._bt
_ctf
_stream
_put
(self
._s
)
1603 def discarded_events(self
):
1605 Get a stream's discarded event count.
1608 ret
, count
= nbt
._bt
_ctf
_stream
_get
_discarded
_events
_count
(self
._s
)
1611 raise ValueError("Could not get the stream's discarded events count")
1615 def append_discarded_events(self
, event_count
):
1617 Increase the current packet's discarded event count.
1620 nbt
._bt
_ctf
_stream
_append
_discarded
_events
(self
._s
, event_count
)
1622 def append_event(self
, event
):
1624 Append "event" to the stream's current packet. The stream's associated clock
1625 will be sampled during this call. The event shall not be modified after
1626 being appended to a stream.
1629 ret
= nbt
._bt
_ctf
_stream
_append
_event
(self
._s
, event
._e
)
1632 raise ValueError("Could not append event to stream.")
1635 def packet_context(self
):
1637 Get a Stream's packet context field (a StructureField).
1640 native_field
= nbt
._bt
_ctf
_stream
_get
_packet
_context
(self
._s
)
1642 if native_field
is None:
1643 raise ValueError("Invalid Stream.")
1645 return Field
._create
_field
_from
_native
_instance
(native_field
)
1647 @packet_context.setter
1648 def packet_context(self
, field
):
1650 Set a Stream's packet context field (must be a StructureField).
1653 if not isinstance(field
, StructureField
):
1654 raise TypeError("Argument field must be of type StructureField")
1656 ret
= nbt
._bt
_ctf
_stream
_set
_packet
_context
(self
._s
, field
._f
)
1659 raise ValueError("Invalid packet context field.")
1663 The stream's current packet's events will be flushed to disk. Events
1664 subsequently appended to the stream will be added to a new packet.
1667 ret
= nbt
._bt
_ctf
_stream
_flush
(self
._s
)
1670 raise ValueError("Could not flush stream.")
1674 def __init__(self
, path
):
1676 Create a new writer that will produce a trace in the given path.
1679 self
._w
= nbt
._bt
_ctf
_writer
_create
(path
)
1682 raise ValueError("Writer creation failed.")
1685 nbt
._bt
_ctf
_writer
_put
(self
._w
)
1687 def create_stream(self
, stream_class
):
1689 Create a new stream instance and register it to the writer.
1692 if not isinstance(stream_class
, StreamClass
):
1693 raise TypeError("Invalid stream_class type.")
1695 stream
= Stream
.__new
__(Stream
)
1696 stream
._s
= nbt
._bt
_ctf
_writer
_create
_stream
(self
._w
, stream_class
._sc
)
1700 def add_environment_field(self
, name
, value
):
1702 Add an environment field to the trace.
1705 ret
= nbt
._bt
_ctf
_writer
_add
_environment
_field
(self
._w
, str(name
),
1709 raise ValueError("Could not add environment field to trace.")
1711 def add_clock(self
, clock
):
1713 Add a clock to the trace. Clocks assigned to stream classes must be
1714 registered to the writer.
1717 ret
= nbt
._bt
_ctf
_writer
_add
_clock
(self
._w
, clock
._c
)
1720 raise ValueError("Could not add clock to Writer.")
1725 Get the trace's TSDL meta-data.
1728 return nbt
._bt
_ctf
_writer
_get
_metadata
_string
(self
._w
)
1730 def flush_metadata(self
):
1732 Flush the trace's metadata to the metadata file.
1735 nbt
._bt
_ctf
_writer
_flush
_metadata
(self
._w
)
1738 def byte_order(self
):
1740 Get the trace's byte order. Must be a constant from the ByteOrder
1744 raise NotImplementedError("Getter not implemented.")
1747 def byte_order(self
, byte_order
):
1749 Set the trace's byte order. Must be a constant from the ByteOrder
1750 class. Defaults to the host machine's endianness
1753 ret
= nbt
._bt
_ctf
_writer
_set
_byte
_order
(self
._w
, byte_order
)
1756 raise ValueError("Could not set trace's byte order.")