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 FieldDeclaration should not be instantiated directly. Instantiate
301 one of the concrete FieldDeclaration classes.
305 # These values are based on the bt_ctf_integer_base enum
306 # declared in event-types.h.
307 INTEGER_BASE_UNKNOWN
= -1
308 INTEGER_BASE_BINARY
= 2
309 INTEGER_BASE_OCTAL
= 8
310 INTEGER_BASE_DECIMAL
= 10
311 INTEGER_BASE_HEXADECIMAL
= 16
315 raise ValueError("FieldDeclaration creation failed.")
318 nbt
._bt
_ctf
_field
_type
_put
(self
._ft
)
321 def _create_field_declaration_from_native_instance(
322 native_field_declaration
):
324 common
.CTFTypeId
.INTEGER
: IntegerFieldDeclaration
,
325 common
.CTFTypeId
.FLOAT
: FloatFieldDeclaration
,
326 common
.CTFTypeId
.ENUM
: EnumerationFieldDeclaration
,
327 common
.CTFTypeId
.STRING
: StringFieldDeclaration
,
328 common
.CTFTypeId
.STRUCT
: StructureFieldDeclaration
,
329 common
.CTFTypeId
.VARIANT
: VariantFieldDeclaration
,
330 common
.CTFTypeId
.ARRAY
: ArrayFieldDeclaration
,
331 common
.CTFTypeId
.SEQUENCE
: SequenceFieldDeclaration
334 field_type_id
= nbt
._bt
_ctf
_field
_type
_get
_type
_id
(native_field_declaration
)
336 if field_type_id
== common
.CTFTypeId
.UNKNOWN
:
337 raise TypeError("Invalid field instance")
339 declaration
= Field
.__new
__(Field
)
340 declaration
._ft
= native_field_declaration
341 declaration
.__class
__ = type_dict
[field_type_id
]
348 Get the field declaration's alignment. Returns -1 on error.
351 return nbt
._bt
_ctf
_field
_type
_get
_alignment
(self
._ft
)
354 def alignment(self
, alignment
):
356 Set the field declaration's alignment. Defaults to 1 (bit-aligned). However,
357 some types, such as structures and string, may impose other alignment
361 ret
= nbt
._bt
_ctf
_field
_type
_set
_alignment
(self
._ft
, alignment
)
364 raise ValueError("Invalid alignment value.")
367 def byte_order(self
):
369 Get the field declaration's byte order. One of the ByteOrder's constant.
372 return nbt
._bt
_ctf
_field
_type
_get
_byte
_order
(self
._ft
)
375 def byte_order(self
, byte_order
):
377 Set the field declaration's byte order. Use constants defined in the ByteOrder
381 ret
= nbt
._bt
_ctf
_field
_type
_set
_byte
_order
(self
._ft
, byte_order
)
384 raise ValueError("Could not set byte order value.")
387 class IntegerFieldDeclaration(FieldDeclaration
):
388 def __init__(self
, size
):
390 Create a new integer field declaration of the given size.
392 self
._ft
= nbt
._bt
_ctf
_field
_type
_integer
_create
(size
)
398 Get an integer's size.
401 ret
= nbt
._bt
_ctf
_field
_type
_integer
_get
_size
(self
._ft
)
404 raise ValueError("Could not get Integer's size attribute.")
411 Get an integer's signedness attribute.
414 ret
= nbt
._bt
_ctf
_field
_type
_integer
_get
_signed
(self
._ft
)
417 raise ValueError("Could not get Integer's signed attribute.")
424 def signed(self
, signed
):
426 Set an integer's signedness attribute.
429 ret
= nbt
._bt
_ctf
_field
_type
_integer
_set
_signed
(self
._ft
, signed
)
432 raise ValueError("Could not set Integer's signed attribute.")
437 Get the integer's base used to pretty-print the resulting trace.
438 Returns a constant from the FieldDeclaration.IntegerBase class.
441 return nbt
._bt
_ctf
_field
_type
_integer
_get
_base
(self
._ft
)
444 def base(self
, base
):
446 Set the integer's base used to pretty-print the resulting trace.
447 The base must be a constant of the FieldDeclarationIntegerBase class.
450 ret
= nbt
._bt
_ctf
_field
_type
_integer
_set
_base
(self
._ft
, base
)
453 raise ValueError("Could not set Integer's base.")
458 Get the integer's encoding (one of the constants of the
459 CTFStringEncoding class).
460 Returns a constant from the CTFStringEncoding class.
463 return nbt
._bt
_ctf
_field
_type
_integer
_get
_encoding
(self
._ft
)
466 def encoding(self
, encoding
):
468 An integer encoding may be set to signal that the integer must be printed
469 as a text character. Must be a constant from the CTFStringEncoding class.
472 ret
= nbt
._bt
_ctf
_field
_type
_integer
_set
_encoding
(self
._ft
, encoding
)
475 raise ValueError("Could not set Integer's encoding.")
478 class EnumerationFieldDeclaration(FieldDeclaration
):
479 def __init__(self
, integer_type
):
481 Create a new enumeration field declaration with the given underlying container type.
483 isinst
= isinstance(integer_type
, IntegerFieldDeclaration
)
485 if integer_type
is None or not isinst
:
486 raise TypeError("Invalid integer container.")
488 self
._ft
= nbt
._bt
_ctf
_field
_type
_enumeration
_create
(integer_type
._ft
)
494 Get the enumeration's underlying container type.
497 ret
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_container
_type
(self
._ft
)
500 raise TypeError("Invalid enumeration declaration")
502 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
504 def add_mapping(self
, name
, range_start
, range_end
):
506 Add a mapping to the enumeration. The range's values are inclusive.
509 if range_start
< 0 or range_end
< 0:
510 ret
= nbt
._bt
_ctf
_field
_type
_enumeration
_add
_mapping
(self
._ft
,
515 ret
= nbt
._bt
_ctf
_field
_type
_enumeration
_add
_mapping
_unsigned
(self
._ft
,
521 raise ValueError("Could not add mapping to enumeration declaration.")
526 Generator returning instances of EnumerationMapping.
529 signed
= self
.container
.signed
531 count
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_count
(self
._ft
)
533 for i
in range(count
):
535 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
(self
._ft
, i
)
537 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
_unsigned
(self
._ft
, i
)
540 msg
= "Could not get Enumeration mapping at index {}".format(i
)
543 name
, range_start
, range_end
= ret
544 yield EnumerationMapping(name
, range_start
, range_end
)
546 def get_mapping_by_name(self
, name
):
548 Get a mapping by name (EnumerationMapping).
551 index
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_index
_by
_name
(self
._ft
, name
)
556 if self
.container
.signed
:
557 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
(self
._ft
, index
)
559 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
_unsigned
(self
._ft
, index
)
562 msg
= "Could not get Enumeration mapping at index {}".format(i
)
565 name
, range_start
, range_end
= ret
567 return EnumerationMapping(name
, range_start
, range_end
)
569 def get_mapping_by_value(self
, value
):
571 Get a mapping by value (EnumerationMapping).
575 index
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_index
_by
_value
(self
._ft
, value
)
577 index
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_index
_by
_unsigned
_value
(self
._ft
, value
)
582 if self
.container
.signed
:
583 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
(self
._ft
, index
)
585 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
_unsigned
(self
._ft
, index
)
588 msg
= "Could not get Enumeration mapping at index {}".format(i
)
591 name
, range_start
, range_end
= ret
593 return EnumerationMapping(name
, range_start
, range_end
)
596 class FloatFieldDeclaration(FieldDeclaration
):
604 Create a new floating point field declaration.
607 self
._ft
= nbt
._bt
_ctf
_field
_type
_floating
_point
_create
()
611 def exponent_digits(self
):
613 Get the number of exponent digits used to store the floating point field.
616 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_get
_exponent
_digits
(self
._ft
)
620 "Could not get Floating point exponent digit count")
624 @exponent_digits.setter
625 def exponent_digits(self
, exponent_digits
):
627 Set the number of exponent digits to use to store the floating point field.
628 The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG which
629 are defined as constants of this class.
632 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_set
_exponent
_digits
(self
._ft
,
636 raise ValueError("Could not set exponent digit count.")
639 def mantissa_digits(self
):
641 Get the number of mantissa digits used to store the floating point field.
644 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_get
_mantissa
_digits
(self
._ft
)
647 raise TypeError("Could not get Floating point mantissa digit count")
651 @mantissa_digits.setter
652 def mantissa_digits(self
, mantissa_digits
):
654 Set the number of mantissa digits to use to store the floating point field.
655 The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG which
656 are defined as constants of this class.
659 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_set
_mantissa
_digits
(self
._ft
,
663 raise ValueError("Could not set mantissa digit count.")
666 class FloatingPointFieldDeclaration(FloatFieldDeclaration
):
670 class StructureFieldDeclaration(FieldDeclaration
):
673 Create a new structure field declaration.
676 self
._ft
= nbt
._bt
_ctf
_field
_type
_structure
_create
()
679 def add_field(self
, field_type
, field_name
):
681 Add a field of type "field_type" to the structure.
684 ret
= nbt
._bt
_ctf
_field
_type
_structure
_add
_field
(self
._ft
,
689 raise ValueError("Could not add field to structure.")
694 Generator returning the structure's field as tuples of (field name, field declaration).
697 count
= nbt
._bt
_ctf
_field
_type
_structure
_get
_field
_count
(self
._ft
)
700 raise TypeError("Could not get Structure field count")
702 for i
in range(count
):
703 field_name
= nbt
._bt
_python
_ctf
_field
_type
_structure
_get
_field
_name
(self
._ft
, i
)
705 if field_name
is None:
706 msg
= "Could not get Structure field name at index {}".format(i
)
709 field_type_native
= nbt
._bt
_python
_ctf
_field
_type
_structure
_get
_field
_type
(self
._ft
, i
)
711 if field_type_native
is None:
712 msg
= "Could not get Structure field type at index {}".format(i
)
715 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
716 yield (field_name
, field_type
)
718 def get_field_by_name(self
, name
):
720 Get a field declaration by name (FieldDeclaration).
723 field_type_native
= nbt
._bt
_ctf
_field
_type
_structure
_get
_field
_type
_by
_name
(self
._ft
, name
)
725 if field_type_native
is None:
726 msg
= "Could not find Structure field with name {}".format(name
)
729 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
732 class VariantFieldDeclaration(FieldDeclaration
):
733 def __init__(self
, enum_tag
, tag_name
):
735 Create a new variant field declaration.
738 isinst
= isinstance(enum_tag
, EnumerationFieldDeclaration
)
739 if enum_tag
is None or not isinst
:
740 raise TypeError("Invalid tag type; must be of type EnumerationFieldDeclaration.")
742 self
._ft
= nbt
._bt
_ctf
_field
_type
_variant
_create
(enum_tag
._ft
,
749 Get the variant's tag name.
752 ret
= nbt
._bt
_ctf
_field
_type
_variant
_get
_tag
_name
(self
._ft
)
755 raise TypeError("Could not get Variant tag name")
762 Get the variant's tag type.
765 ret
= nbt
._bt
_ctf
_field
_type
_variant
_get
_tag
_type
(self
._ft
)
768 raise TypeError("Could not get Variant tag type")
770 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
772 def add_field(self
, field_type
, field_name
):
774 Add a field of type "field_type" to the variant.
777 ret
= nbt
._bt
_ctf
_field
_type
_variant
_add
_field
(self
._ft
,
782 raise ValueError("Could not add field to variant.")
787 Generator returning the variant's field as tuples of (field name, field declaration).
790 count
= nbt
._bt
_ctf
_field
_type
_variant
_get
_field
_count
(self
._ft
)
793 raise TypeError("Could not get Variant field count")
795 for i
in range(count
):
796 field_name
= nbt
._bt
_python
_ctf
_field
_type
_variant
_get
_field
_name
(self
._ft
, i
)
798 if field_name
is None:
799 msg
= "Could not get Variant field name at index {}".format(i
)
802 field_type_native
= nbt
._bt
_python
_ctf
_field
_type
_variant
_get
_field
_type
(self
._ft
, i
)
804 if field_type_native
is None:
805 msg
= "Could not get Variant field type at index {}".format(i
)
808 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
809 yield (field_name
, field_type
)
811 def get_field_by_name(self
, name
):
813 Get a field declaration by name (FieldDeclaration).
816 field_type_native
= nbt
._bt
_ctf
_field
_type
_variant
_get
_field
_type
_by
_name
(self
._ft
,
819 if field_type_native
is None:
820 msg
= "Could not find Variant field with name {}".format(name
)
823 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
825 def get_field_from_tag(self
, tag
):
827 Get a field declaration from tag (EnumerationField).
830 field_type_native
= nbt
._bt
_ctf
_field
_type
_variant
_get
_field
_type
_from
_tag
(self
._ft
, tag
._f
)
832 if field_type_native
is None:
833 msg
= "Could not find Variant field with tag value {}".format(tag
.value
)
836 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
839 class ArrayFieldDeclaration(FieldDeclaration
):
840 def __init__(self
, element_type
, length
):
842 Create a new array field declaration.
845 self
._ft
= nbt
._bt
_ctf
_field
_type
_array
_create
(element_type
._ft
,
850 def element_type(self
):
852 Get the array's element type.
855 ret
= nbt
._bt
_ctf
_field
_type
_array
_get
_element
_type
(self
._ft
)
858 raise TypeError("Could not get Array element type")
860 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
865 Get the array's length.
868 ret
= nbt
._bt
_ctf
_field
_type
_array
_get
_length
(self
._ft
)
871 raise TypeError("Could not get Array length")
876 class SequenceFieldDeclaration(FieldDeclaration
):
877 def __init__(self
, element_type
, length_field_name
):
879 Create a new sequence field declaration.
882 self
._ft
= nbt
._bt
_ctf
_field
_type
_sequence
_create
(element_type
._ft
,
883 str(length_field_name
))
887 def element_type(self
):
889 Get the sequence's element type.
892 ret
= nbt
._bt
_ctf
_field
_type
_sequence
_get
_element
_type
(self
._ft
)
895 raise TypeError("Could not get Sequence element type")
897 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
900 def length_field_name(self
):
902 Get the sequence's length field name.
905 ret
= nbt
._bt
_ctf
_field
_type
_sequence
_get
_length
_field
_name
(self
._ft
)
908 raise TypeError("Could not get Sequence length field name")
913 class StringFieldDeclaration(FieldDeclaration
):
916 Create a new string field declaration.
919 self
._ft
= nbt
._bt
_ctf
_field
_type
_string
_create
()
925 Get a string declaration's encoding (a constant from the CTFStringEncoding class).
928 return nbt
._bt
_ctf
_field
_type
_string
_get
_encoding
(self
._ft
)
931 def encoding(self
, encoding
):
933 Set a string declaration's encoding. Must be a constant from the CTFStringEncoding class.
936 ret
= nbt
._bt
_ctf
_field
_type
_string
_set
_encoding
(self
._ft
, encoding
)
938 raise ValueError("Could not set string encoding.")
942 def create_field(field_type
):
944 Create an instance of a field.
946 isinst
= isinstance(field_type
, FieldDeclaration
)
948 if field_type
is None or not isinst
:
949 raise TypeError("Invalid field_type. Type must be a FieldDeclaration-derived class.")
951 if isinstance(field_type
, IntegerFieldDeclaration
):
952 return IntegerField(field_type
)
953 elif isinstance(field_type
, EnumerationFieldDeclaration
):
954 return EnumerationField(field_type
)
955 elif isinstance(field_type
, FloatFieldDeclaration
):
956 return FloatingPointField(field_type
)
957 elif isinstance(field_type
, StructureFieldDeclaration
):
958 return StructureField(field_type
)
959 elif isinstance(field_type
, VariantFieldDeclaration
):
960 return VariantField(field_type
)
961 elif isinstance(field_type
, ArrayFieldDeclaration
):
962 return ArrayField(field_type
)
963 elif isinstance(field_type
, SequenceFieldDeclaration
):
964 return SequenceField(field_type
)
965 elif isinstance(field_type
, StringFieldDeclaration
):
966 return StringField(field_type
)
971 Base class, do not instantiate.
974 def __init__(self
, field_type
):
975 if not isinstance(field_type
, FieldDeclaration
):
976 raise TypeError("Invalid field_type argument.")
978 self
._f
= nbt
._bt
_ctf
_field
_create
(field_type
._ft
)
981 raise ValueError("Field creation failed.")
984 nbt
._bt
_ctf
_field
_put
(self
._f
)
987 def _create_field_from_native_instance(native_field_instance
):
989 common
.CTFTypeId
.INTEGER
: IntegerField
,
990 common
.CTFTypeId
.FLOAT
: FloatingPointField
,
991 common
.CTFTypeId
.ENUM
: EnumerationField
,
992 common
.CTFTypeId
.STRING
: StringField
,
993 common
.CTFTypeId
.STRUCT
: StructureField
,
994 common
.CTFTypeId
.VARIANT
: VariantField
,
995 common
.CTFTypeId
.ARRAY
: ArrayField
,
996 common
.CTFTypeId
.SEQUENCE
: SequenceField
999 field_type
= nbt
._bt
_python
_get
_field
_type
(native_field_instance
)
1001 if field_type
== common
.CTFTypeId
.UNKNOWN
:
1002 raise TypeError("Invalid field instance")
1004 field
= Field
.__new
__(Field
)
1005 field
._f
= native_field_instance
1006 field
.__class
__ = type_dict
[field_type
]
1011 def declaration(self
):
1012 native_field_type
= nbt
._bt
_ctf
_field
_get
_type
(self
._f
)
1014 if native_field_type
is None:
1015 raise TypeError("Invalid field instance")
1016 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(
1020 class IntegerField(Field
):
1024 Get an integer field's value.
1027 signedness
= nbt
._bt
_python
_field
_integer
_get
_signedness
(self
._f
)
1030 raise TypeError("Invalid integer instance.")
1033 ret
, value
= nbt
._bt
_ctf
_field
_unsigned
_integer
_get
_value
(self
._f
)
1035 ret
, value
= nbt
._bt
_ctf
_field
_signed
_integer
_get
_value
(self
._f
)
1038 raise ValueError("Could not get integer field value.")
1043 def value(self
, value
):
1045 Set an integer field's value.
1048 if not isinstance(value
, int):
1049 raise TypeError("IntegerField's value must be an int")
1051 signedness
= nbt
._bt
_python
_field
_integer
_get
_signedness
(self
._f
)
1053 raise TypeError("Invalid integer instance.")
1056 ret
= nbt
._bt
_ctf
_field
_unsigned
_integer
_set
_value
(self
._f
, value
)
1058 ret
= nbt
._bt
_ctf
_field
_signed
_integer
_set
_value
(self
._f
, value
)
1061 raise ValueError("Could not set integer field value.")
1064 class EnumerationField(Field
):
1066 def container(self
):
1068 Return the enumeration's underlying container field (an integer field).
1071 container
= IntegerField
.__new
__(IntegerField
)
1072 container
._f
= nbt
._bt
_ctf
_field
_enumeration
_get
_container
(self
._f
)
1074 if container
._f
is None:
1075 raise TypeError("Invalid enumeration field type.")
1082 Get the enumeration field's mapping name.
1085 value
= nbt
._bt
_ctf
_field
_enumeration
_get
_mapping
_name
(self
._f
)
1088 raise ValueError("Could not get enumeration's mapping name.")
1093 def value(self
, value
):
1095 Set the enumeration field's value. Must be an integer as mapping names
1099 if not isinstance(value
, int):
1100 raise TypeError("EnumerationField value must be an int")
1102 self
.container
.value
= value
1105 class FloatingPointField(Field
):
1109 Get a floating point field's value.
1112 ret
, value
= nbt
._bt
_ctf
_field
_floating
_point
_get
_value
(self
._f
)
1115 raise ValueError("Could not get floating point field value.")
1120 def value(self
, value
):
1122 Set a floating point field's value.
1125 if not isinstance(value
, int) and not isinstance(value
, float):
1126 raise TypeError("Value must be either a float or an int")
1128 ret
= nbt
._bt
_ctf
_field
_floating
_point
_set
_value
(self
._f
, float(value
))
1131 raise ValueError("Could not set floating point field value.")
1134 # oops!! This class is provided to ensure backward-compatibility since
1135 # a stable release publicly exposed this abomination.
1136 class FloatFieldingPoint(FloatingPointField
):
1140 class StructureField(Field
):
1141 def field(self
, field_name
):
1143 Get the structure's field corresponding to the provided field name.
1146 native_instance
= nbt
._bt
_ctf
_field
_structure
_get
_field
(self
._f
,
1149 if native_instance
is None:
1150 raise ValueError("Invalid field_name provided.")
1152 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1155 class VariantField(Field
):
1156 def field(self
, tag
):
1158 Return the variant's selected field. The "tag" field is the selector enum field.
1161 native_instance
= nbt
._bt
_ctf
_field
_variant
_get
_field
(self
._f
, tag
._f
)
1163 if native_instance
is None:
1164 raise ValueError("Invalid tag provided.")
1166 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1169 class ArrayField(Field
):
1170 def field(self
, index
):
1172 Return the array's field at position "index".
1175 native_instance
= nbt
._bt
_ctf
_field
_array
_get
_field
(self
._f
, index
)
1177 if native_instance
is None:
1178 raise IndexError("Invalid index provided.")
1180 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1183 class SequenceField(Field
):
1187 Get the sequence's length field (IntegerField).
1190 native_instance
= nbt
._bt
_ctf
_field
_sequence
_get
_length
(self
._f
)
1192 if native_instance
is None:
1195 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1198 def length(self
, length_field
):
1200 Set the sequence's length field (IntegerField).
1203 if not isinstance(length_field
, IntegerField
):
1204 raise TypeError("Invalid length field.")
1206 if length_field
.declaration
.signed
:
1207 raise TypeError("Sequence field length must be unsigned")
1209 ret
= nbt
._bt
_ctf
_field
_sequence
_set
_length
(self
._f
, length_field
._f
)
1212 raise ValueError("Could not set sequence length.")
1214 def field(self
, index
):
1216 Return the sequence's field at position "index".
1219 native_instance
= nbt
._bt
_ctf
_field
_sequence
_get
_field
(self
._f
, index
)
1221 if native_instance
is None:
1222 raise ValueError("Could not get sequence element at index.")
1224 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1227 class StringField(Field
):
1231 Get a string field's value.
1234 return nbt
._bt
_ctf
_field
_string
_get
_value
(self
._f
)
1237 def value(self
, value
):
1239 Set a string field's value.
1242 ret
= nbt
._bt
_ctf
_field
_string
_set
_value
(self
._f
, str(value
))
1245 raise ValueError("Could not set string field value.")
1249 def __init__(self
, name
):
1251 Create a new event class of the given name.
1254 self
._ec
= nbt
._bt
_ctf
_event
_class
_create
(name
)
1256 if self
._ec
is None:
1257 raise ValueError("Event class creation failed.")
1260 nbt
._bt
_ctf
_event
_class
_put
(self
._ec
)
1262 def add_field(self
, field_type
, field_name
):
1264 Add a field of type "field_type" to the event class.
1267 ret
= nbt
._bt
_ctf
_event
_class
_add
_field
(self
._ec
, field_type
._ft
,
1271 raise ValueError("Could not add field to event class.")
1276 Get the event class' name.
1279 name
= nbt
._bt
_ctf
_event
_class
_get
_name
(self
._ec
)
1282 raise TypeError("Could not get EventClass name")
1289 Get the event class' id. Returns a negative value if unset.
1292 id = nbt
._bt
_ctf
_event
_class
_get
_id
(self
._ec
)
1295 raise TypeError("Could not get EventClass id")
1302 Set the event class' id. Throws a TypeError if the event class
1303 is already registered to a stream class.
1306 ret
= nbt
._bt
_ctf
_event
_class
_set
_id
(self
._ec
, id)
1309 raise TypeError("Can't change an Event Class's id after it has been assigned to a stream class")
1312 def stream_class(self
):
1314 Get the event class' stream class. Returns None if unset.
1316 stream_class_native
= nbt
._bt
_ctf
_event
_class
_get
_stream
_class
(self
._ec
)
1318 if stream_class_native
is None:
1321 stream_class
= StreamClass
.__new
__(StreamClass
)
1322 stream_class
._sc
= stream_class_native
1329 Generator returning the event class' fields as tuples of (field name, field declaration).
1332 count
= nbt
._bt
_ctf
_event
_class
_get
_field
_count
(self
._ec
)
1335 raise TypeError("Could not get EventClass' field count")
1337 for i
in range(count
):
1338 field_name
= nbt
._bt
_python
_ctf
_event
_class
_get
_field
_name
(self
._ec
, i
)
1340 if field_name
is None:
1341 msg
= "Could not get EventClass' field name at index {}".format(i
)
1342 raise TypeError(msg
)
1344 field_type_native
= nbt
._bt
_python
_ctf
_event
_class
_get
_field
_type
(self
._ec
, i
)
1346 if field_type_native
is None:
1347 msg
= "Could not get EventClass' field type at index {}".format(i
)
1348 raise TypeError(msg
)
1350 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
1351 yield (field_name
, field_type
)
1353 def get_field_by_name(self
, name
):
1355 Get a field declaration by name (FieldDeclaration).
1358 field_type_native
= nbt
._bt
_ctf
_event
_class
_get
_field
_by
_name
(self
._ec
, name
)
1360 if field_type_native
is None:
1361 msg
= "Could not find EventClass field with name {}".format(name
)
1362 raise TypeError(msg
)
1364 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
1368 def __init__(self
, event_class
):
1370 Create a new event of the given event class.
1373 if not isinstance(event_class
, EventClass
):
1374 raise TypeError("Invalid event_class argument.")
1376 self
._e
= nbt
._bt
_ctf
_event
_create
(event_class
._ec
)
1379 raise ValueError("Event creation failed.")
1382 nbt
._bt
_ctf
_event
_put
(self
._e
)
1385 def event_class(self
):
1387 Get the event's class.
1390 event_class_native
= nbt
._bt
_ctf
_event
_get
_class
(self
._e
)
1392 if event_class_native
is None:
1395 event_class
= EventClass
.__new
__(EventClass
)
1396 event_class
._ec
= event_class_native
1402 Get a clock from event. Returns None if the event's class
1403 is not registered to a stream class.
1406 clock_instance
= nbt
._bt
_ctf
_event
_get
_clock
(self
._e
)
1408 if clock_instance
is None:
1411 clock
= Clock
.__new
__(Clock
)
1412 clock
._c
= clock_instance
1416 def payload(self
, field_name
):
1418 Get a field from event.
1421 native_instance
= nbt
._bt
_ctf
_event
_get
_payload
(self
._e
,
1424 if native_instance
is None:
1425 raise ValueError("Could not get event payload.")
1427 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1429 def set_payload(self
, field_name
, value_field
):
1431 Set a manually created field as an event's payload.
1434 if not isinstance(value
, Field
):
1435 raise TypeError("Invalid value type.")
1437 ret
= nbt
._bt
_ctf
_event
_set
_payload
(self
._e
, str(field_name
),
1441 raise ValueError("Could not set event field payload.")
1445 def __init__(self
, name
):
1447 Create a new stream class of the given name.
1450 self
._sc
= nbt
._bt
_ctf
_stream
_class
_create
(name
)
1452 if self
._sc
is None:
1453 raise ValueError("Stream class creation failed.")
1456 nbt
._bt
_ctf
_stream
_class
_put
(self
._sc
)
1461 Get a stream class' name.
1464 name
= nbt
._bt
_ctf
_stream
_class
_get
_name
(self
._sc
)
1467 raise TypeError("Could not get StreamClass name")
1474 Get a stream class' clock.
1477 clock_instance
= nbt
._bt
_ctf
_stream
_class
_get
_clock
(self
._sc
)
1479 if clock_instance
is None:
1482 clock
= Clock
.__new
__(Clock
)
1483 clock
._c
= clock_instance
1488 def clock(self
, clock
):
1490 Assign a clock to a stream class.
1493 if not isinstance(clock
, Clock
):
1494 raise TypeError("Invalid clock type.")
1496 ret
= nbt
._bt
_ctf
_stream
_class
_set
_clock
(self
._sc
, clock
._c
)
1499 raise ValueError("Could not set stream class clock.")
1504 Get a stream class' id.
1507 ret
= nbt
._bt
_ctf
_stream
_class
_get
_id
(self
._sc
)
1510 raise TypeError("Could not get StreamClass id")
1517 Assign an id to a stream class.
1520 ret
= nbt
._bt
_ctf
_stream
_class
_set
_id
(self
._sc
, id)
1523 raise TypeError("Could not set stream class id.")
1526 def event_classes(self
):
1528 Generator returning the stream class' event classes.
1531 count
= nbt
._bt
_ctf
_stream
_class
_get
_event
_class
_count
(self
._sc
)
1534 raise TypeError("Could not get StreamClass' event class count")
1536 for i
in range(count
):
1537 event_class_native
= nbt
._bt
_ctf
_stream
_class
_get
_event
_class
(self
._sc
, i
)
1539 if event_class_native
is None:
1540 msg
= "Could not get StreamClass' event class at index {}".format(i
)
1541 raise TypeError(msg
)
1543 event_class
= EventClass
.__new
__(EventClass
)
1544 event_class
._ec
= event_class_native
1547 def add_event_class(self
, event_class
):
1549 Add an event class to a stream class. New events can be added even after a
1550 stream has been instantiated and events have been appended. However, a stream
1551 will not accept events of a class that has not been added to the stream
1555 if not isinstance(event_class
, EventClass
):
1556 raise TypeError("Invalid event_class type.")
1558 ret
= nbt
._bt
_ctf
_stream
_class
_add
_event
_class
(self
._sc
,
1562 raise ValueError("Could not add event class.")
1565 def packet_context_type(self
):
1567 Get the StreamClass' packet context type (StructureFieldDeclaration)
1570 field_type_native
= nbt
._bt
_ctf
_stream
_class
_get
_packet
_context
_type
(self
._sc
)
1572 if field_type_native
is None:
1573 raise ValueError("Invalid StreamClass")
1575 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
1579 @packet_context_type.setter
1580 def packet_context_type(self
, field_type
):
1582 Set a StreamClass' packet context type. Must be of type
1583 StructureFieldDeclaration.
1586 if not isinstance(field_type
, StructureFieldDeclaration
):
1587 raise TypeError("field_type argument must be of type StructureFieldDeclaration.")
1589 ret
= nbt
._bt
_ctf
_stream
_class
_set
_packet
_context
_type
(self
._sc
,
1593 raise ValueError("Failed to set packet context type.")
1598 raise NotImplementedError("Stream cannot be instantiated; use Writer.create_stream()")
1601 nbt
._bt
_ctf
_stream
_put
(self
._s
)
1604 def discarded_events(self
):
1606 Get a stream's discarded event count.
1609 ret
, count
= nbt
._bt
_ctf
_stream
_get
_discarded
_events
_count
(self
._s
)
1612 raise ValueError("Could not get the stream's discarded events count")
1616 def append_discarded_events(self
, event_count
):
1618 Increase the current packet's discarded event count.
1621 nbt
._bt
_ctf
_stream
_append
_discarded
_events
(self
._s
, event_count
)
1623 def append_event(self
, event
):
1625 Append "event" to the stream's current packet. The stream's associated clock
1626 will be sampled during this call. The event shall not be modified after
1627 being appended to a stream.
1630 ret
= nbt
._bt
_ctf
_stream
_append
_event
(self
._s
, event
._e
)
1633 raise ValueError("Could not append event to stream.")
1636 def packet_context(self
):
1638 Get a Stream's packet context field (a StructureField).
1641 native_field
= nbt
._bt
_ctf
_stream
_get
_packet
_context
(self
._s
)
1643 if native_field
is None:
1644 raise ValueError("Invalid Stream.")
1646 return Field
._create
_field
_from
_native
_instance
(native_field
)
1648 @packet_context.setter
1649 def packet_context(self
, field
):
1651 Set a Stream's packet context field (must be a StructureField).
1654 if not isinstance(field
, StructureField
):
1655 raise TypeError("Argument field must be of type StructureField")
1657 ret
= nbt
._bt
_ctf
_stream
_set
_packet
_context
(self
._s
, field
._f
)
1660 raise ValueError("Invalid packet context field.")
1664 The stream's current packet's events will be flushed to disk. Events
1665 subsequently appended to the stream will be added to a new packet.
1668 ret
= nbt
._bt
_ctf
_stream
_flush
(self
._s
)
1671 raise ValueError("Could not flush stream.")
1675 def __init__(self
, path
):
1677 Create a new writer that will produce a trace in the given path.
1680 self
._w
= nbt
._bt
_ctf
_writer
_create
(path
)
1683 raise ValueError("Writer creation failed.")
1686 nbt
._bt
_ctf
_writer
_put
(self
._w
)
1688 def create_stream(self
, stream_class
):
1690 Create a new stream instance and register it to the writer.
1693 if not isinstance(stream_class
, StreamClass
):
1694 raise TypeError("Invalid stream_class type.")
1696 stream
= Stream
.__new
__(Stream
)
1697 stream
._s
= nbt
._bt
_ctf
_writer
_create
_stream
(self
._w
, stream_class
._sc
)
1701 def add_environment_field(self
, name
, value
):
1703 Add an environment field to the trace.
1706 ret
= nbt
._bt
_ctf
_writer
_add
_environment
_field
(self
._w
, str(name
),
1710 raise ValueError("Could not add environment field to trace.")
1712 def add_clock(self
, clock
):
1714 Add a clock to the trace. Clocks assigned to stream classes must be
1715 registered to the writer.
1718 ret
= nbt
._bt
_ctf
_writer
_add
_clock
(self
._w
, clock
._c
)
1721 raise ValueError("Could not add clock to Writer.")
1726 Get the trace's TSDL meta-data.
1729 return nbt
._bt
_ctf
_writer
_get
_metadata
_string
(self
._w
)
1731 def flush_metadata(self
):
1733 Flush the trace's metadata to the metadata file.
1736 nbt
._bt
_ctf
_writer
_flush
_metadata
(self
._w
)
1739 def byte_order(self
):
1741 Get the trace's byte order. Must be a constant from the ByteOrder
1745 raise NotImplementedError("Getter not implemented.")
1748 def byte_order(self
, byte_order
):
1750 Set the trace's byte order. Must be a constant from the ByteOrder
1751 class. Defaults to the host machine's endianness
1754 ret
= nbt
._bt
_ctf
_writer
_set
_byte
_order
(self
._w
, byte_order
)
1757 raise ValueError("Could not set trace's byte order.")