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.")
300 Display base of an integer.
304 INTEGER_BASE_UNKNOWN
= -1
307 INTEGER_BASE_BINARY
= 2
310 INTEGER_BASE_OCTAL
= 8
313 INTEGER_BASE_DECIMAL
= 10
316 INTEGER_BASE_HEXADECIMAL
= 16
319 class FieldDeclaration
:
321 Base class of all field declarations. This class is not meant to
322 be instantiated by the user; use one of the concrete field
323 declaration subclasses instead.
326 class IntegerBase(IntegerBase
):
331 raise ValueError("FieldDeclaration creation failed.")
334 nbt
._bt
_ctf
_field
_type
_put
(self
._ft
)
337 def _create_field_declaration_from_native_instance(
338 native_field_declaration
):
340 common
.CTFTypeId
.INTEGER
: IntegerFieldDeclaration
,
341 common
.CTFTypeId
.FLOAT
: FloatFieldDeclaration
,
342 common
.CTFTypeId
.ENUM
: EnumerationFieldDeclaration
,
343 common
.CTFTypeId
.STRING
: StringFieldDeclaration
,
344 common
.CTFTypeId
.STRUCT
: StructureFieldDeclaration
,
345 common
.CTFTypeId
.VARIANT
: VariantFieldDeclaration
,
346 common
.CTFTypeId
.ARRAY
: ArrayFieldDeclaration
,
347 common
.CTFTypeId
.SEQUENCE
: SequenceFieldDeclaration
350 field_type_id
= nbt
._bt
_ctf
_field
_type
_get
_type
_id
(native_field_declaration
)
352 if field_type_id
== common
.CTFTypeId
.UNKNOWN
:
353 raise TypeError("Invalid field instance")
355 declaration
= Field
.__new
__(Field
)
356 declaration
._ft
= native_field_declaration
357 declaration
.__class
__ = type_dict
[field_type_id
]
364 Field alignment in bits (integer).
366 Set this attribute to change this field's alignment.
368 :exc:`ValueError` is raised on error.
371 return nbt
._bt
_ctf
_field
_type
_get
_alignment
(self
._ft
)
374 def alignment(self
, alignment
):
375 ret
= nbt
._bt
_ctf
_field
_type
_set
_alignment
(self
._ft
, alignment
)
378 raise ValueError("Invalid alignment value.")
381 def byte_order(self
):
383 Field byte order (one of :class:`babeltrace.common.ByteOrder`
386 Set this attribute to change this field's byte order.
388 :exc:`ValueError` is raised on error.
391 return nbt
._bt
_ctf
_field
_type
_get
_byte
_order
(self
._ft
)
394 def byte_order(self
, byte_order
):
395 ret
= nbt
._bt
_ctf
_field
_type
_set
_byte
_order
(self
._ft
, byte_order
)
398 raise ValueError("Could not set byte order value.")
401 class IntegerFieldDeclaration(FieldDeclaration
):
403 Integer field declaration.
406 def __init__(self
, size
):
408 Creates an integer field declaration of size *size* bits.
410 :exc:`ValueError` is raised on error.
413 self
._ft
= nbt
._bt
_ctf
_field
_type
_integer
_create
(size
)
419 Integer size in bits (integer).
421 Set this attribute to change this integer's size.
423 :exc:`ValueError` is raised on error.
426 ret
= nbt
._bt
_ctf
_field
_type
_integer
_get
_size
(self
._ft
)
429 raise ValueError("Could not get Integer size attribute.")
436 ``True`` if this integer is signed.
438 Set this attribute to change this integer's signedness
441 :exc:`ValueError` is raised on error.
444 ret
= nbt
._bt
_ctf
_field
_type
_integer
_get
_signed
(self
._ft
)
447 raise ValueError("Could not get Integer signed attribute.")
454 def signed(self
, signed
):
455 ret
= nbt
._bt
_ctf
_field
_type
_integer
_set
_signed
(self
._ft
, signed
)
458 raise ValueError("Could not set Integer signed attribute.")
463 Integer display base (one of :class:`IntegerBase` constants).
465 Set this attribute to change this integer's display base.
467 :exc:`ValueError` is raised on error.
470 return nbt
._bt
_ctf
_field
_type
_integer
_get
_base
(self
._ft
)
473 def base(self
, base
):
474 ret
= nbt
._bt
_ctf
_field
_type
_integer
_set
_base
(self
._ft
, base
)
477 raise ValueError("Could not set Integer base.")
482 Integer encoding (one of
483 :class:`babeltrace.common.CTFStringEncoding` constants).
485 Set this attribute to change this integer's encoding.
487 :exc:`ValueError` is raised on error.
490 return nbt
._bt
_ctf
_field
_type
_integer
_get
_encoding
(self
._ft
)
493 def encoding(self
, encoding
):
494 ret
= nbt
._bt
_ctf
_field
_type
_integer
_set
_encoding
(self
._ft
, encoding
)
497 raise ValueError("Could not set Integer encoding.")
500 class EnumerationFieldDeclaration(FieldDeclaration
):
501 def __init__(self
, integer_type
):
503 Create a new enumeration field declaration with the given underlying container type.
505 isinst
= isinstance(integer_type
, IntegerFieldDeclaration
)
507 if integer_type
is None or not isinst
:
508 raise TypeError("Invalid integer container.")
510 self
._ft
= nbt
._bt
_ctf
_field
_type
_enumeration
_create
(integer_type
._ft
)
516 Get the enumeration's underlying container type.
519 ret
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_container
_type
(self
._ft
)
522 raise TypeError("Invalid enumeration declaration")
524 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
526 def add_mapping(self
, name
, range_start
, range_end
):
528 Add a mapping to the enumeration. The range's values are inclusive.
531 if range_start
< 0 or range_end
< 0:
532 ret
= nbt
._bt
_ctf
_field
_type
_enumeration
_add
_mapping
(self
._ft
,
537 ret
= nbt
._bt
_ctf
_field
_type
_enumeration
_add
_mapping
_unsigned
(self
._ft
,
543 raise ValueError("Could not add mapping to enumeration declaration.")
548 Generator returning instances of EnumerationMapping.
551 signed
= self
.container
.signed
553 count
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_count
(self
._ft
)
555 for i
in range(count
):
557 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
(self
._ft
, i
)
559 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
_unsigned
(self
._ft
, i
)
562 msg
= "Could not get Enumeration mapping at index {}".format(i
)
565 name
, range_start
, range_end
= ret
566 yield EnumerationMapping(name
, range_start
, range_end
)
568 def get_mapping_by_name(self
, name
):
570 Get a mapping by name (EnumerationMapping).
573 index
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_index
_by
_name
(self
._ft
, name
)
578 if self
.container
.signed
:
579 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
(self
._ft
, index
)
581 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
_unsigned
(self
._ft
, index
)
584 msg
= "Could not get Enumeration mapping at index {}".format(i
)
587 name
, range_start
, range_end
= ret
589 return EnumerationMapping(name
, range_start
, range_end
)
591 def get_mapping_by_value(self
, value
):
593 Get a mapping by value (EnumerationMapping).
597 index
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_index
_by
_value
(self
._ft
, value
)
599 index
= nbt
._bt
_ctf
_field
_type
_enumeration
_get
_mapping
_index
_by
_unsigned
_value
(self
._ft
, value
)
604 if self
.container
.signed
:
605 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
(self
._ft
, index
)
607 ret
= nbt
._bt
_python
_ctf
_field
_type
_enumeration
_get
_mapping
_unsigned
(self
._ft
, index
)
610 msg
= "Could not get Enumeration mapping at index {}".format(i
)
613 name
, range_start
, range_end
= ret
615 return EnumerationMapping(name
, range_start
, range_end
)
618 class FloatFieldDeclaration(FieldDeclaration
):
626 Create a new floating point field declaration.
629 self
._ft
= nbt
._bt
_ctf
_field
_type
_floating
_point
_create
()
633 def exponent_digits(self
):
635 Get the number of exponent digits used to store the floating point field.
638 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_get
_exponent
_digits
(self
._ft
)
642 "Could not get Floating point exponent digit count")
646 @exponent_digits.setter
647 def exponent_digits(self
, exponent_digits
):
649 Set the number of exponent digits to use to store the floating point field.
650 The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG which
651 are defined as constants of this class.
654 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_set
_exponent
_digits
(self
._ft
,
658 raise ValueError("Could not set exponent digit count.")
661 def mantissa_digits(self
):
663 Get the number of mantissa digits used to store the floating point field.
666 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_get
_mantissa
_digits
(self
._ft
)
669 raise TypeError("Could not get Floating point mantissa digit count")
673 @mantissa_digits.setter
674 def mantissa_digits(self
, mantissa_digits
):
676 Set the number of mantissa digits to use to store the floating point field.
677 The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG which
678 are defined as constants of this class.
681 ret
= nbt
._bt
_ctf
_field
_type
_floating
_point
_set
_mantissa
_digits
(self
._ft
,
685 raise ValueError("Could not set mantissa digit count.")
688 class FloatingPointFieldDeclaration(FloatFieldDeclaration
):
692 class StructureFieldDeclaration(FieldDeclaration
):
695 Create a new structure field declaration.
698 self
._ft
= nbt
._bt
_ctf
_field
_type
_structure
_create
()
701 def add_field(self
, field_type
, field_name
):
703 Add a field of type "field_type" to the structure.
706 ret
= nbt
._bt
_ctf
_field
_type
_structure
_add
_field
(self
._ft
,
711 raise ValueError("Could not add field to structure.")
716 Generator returning the structure's field as tuples of (field name, field declaration).
719 count
= nbt
._bt
_ctf
_field
_type
_structure
_get
_field
_count
(self
._ft
)
722 raise TypeError("Could not get Structure field count")
724 for i
in range(count
):
725 field_name
= nbt
._bt
_python
_ctf
_field
_type
_structure
_get
_field
_name
(self
._ft
, i
)
727 if field_name
is None:
728 msg
= "Could not get Structure field name at index {}".format(i
)
731 field_type_native
= nbt
._bt
_python
_ctf
_field
_type
_structure
_get
_field
_type
(self
._ft
, i
)
733 if field_type_native
is None:
734 msg
= "Could not get Structure field type at index {}".format(i
)
737 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
738 yield (field_name
, field_type
)
740 def get_field_by_name(self
, name
):
742 Get a field declaration by name (FieldDeclaration).
745 field_type_native
= nbt
._bt
_ctf
_field
_type
_structure
_get
_field
_type
_by
_name
(self
._ft
, name
)
747 if field_type_native
is None:
748 msg
= "Could not find Structure field with name {}".format(name
)
751 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
754 class VariantFieldDeclaration(FieldDeclaration
):
755 def __init__(self
, enum_tag
, tag_name
):
757 Create a new variant field declaration.
760 isinst
= isinstance(enum_tag
, EnumerationFieldDeclaration
)
761 if enum_tag
is None or not isinst
:
762 raise TypeError("Invalid tag type; must be of type EnumerationFieldDeclaration.")
764 self
._ft
= nbt
._bt
_ctf
_field
_type
_variant
_create
(enum_tag
._ft
,
771 Get the variant's tag name.
774 ret
= nbt
._bt
_ctf
_field
_type
_variant
_get
_tag
_name
(self
._ft
)
777 raise TypeError("Could not get Variant tag name")
784 Get the variant's tag type.
787 ret
= nbt
._bt
_ctf
_field
_type
_variant
_get
_tag
_type
(self
._ft
)
790 raise TypeError("Could not get Variant tag type")
792 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
794 def add_field(self
, field_type
, field_name
):
796 Add a field of type "field_type" to the variant.
799 ret
= nbt
._bt
_ctf
_field
_type
_variant
_add
_field
(self
._ft
,
804 raise ValueError("Could not add field to variant.")
809 Generator returning the variant's field as tuples of (field name, field declaration).
812 count
= nbt
._bt
_ctf
_field
_type
_variant
_get
_field
_count
(self
._ft
)
815 raise TypeError("Could not get Variant field count")
817 for i
in range(count
):
818 field_name
= nbt
._bt
_python
_ctf
_field
_type
_variant
_get
_field
_name
(self
._ft
, i
)
820 if field_name
is None:
821 msg
= "Could not get Variant field name at index {}".format(i
)
824 field_type_native
= nbt
._bt
_python
_ctf
_field
_type
_variant
_get
_field
_type
(self
._ft
, i
)
826 if field_type_native
is None:
827 msg
= "Could not get Variant field type at index {}".format(i
)
830 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
831 yield (field_name
, field_type
)
833 def get_field_by_name(self
, name
):
835 Get a field declaration by name (FieldDeclaration).
838 field_type_native
= nbt
._bt
_ctf
_field
_type
_variant
_get
_field
_type
_by
_name
(self
._ft
,
841 if field_type_native
is None:
842 msg
= "Could not find Variant field with name {}".format(name
)
845 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
847 def get_field_from_tag(self
, tag
):
849 Get a field declaration from tag (EnumerationField).
852 field_type_native
= nbt
._bt
_ctf
_field
_type
_variant
_get
_field
_type
_from
_tag
(self
._ft
, tag
._f
)
854 if field_type_native
is None:
855 msg
= "Could not find Variant field with tag value {}".format(tag
.value
)
858 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
861 class ArrayFieldDeclaration(FieldDeclaration
):
862 def __init__(self
, element_type
, length
):
864 Create a new array field declaration.
867 self
._ft
= nbt
._bt
_ctf
_field
_type
_array
_create
(element_type
._ft
,
872 def element_type(self
):
874 Get the array's element type.
877 ret
= nbt
._bt
_ctf
_field
_type
_array
_get
_element
_type
(self
._ft
)
880 raise TypeError("Could not get Array element type")
882 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
887 Get the array's length.
890 ret
= nbt
._bt
_ctf
_field
_type
_array
_get
_length
(self
._ft
)
893 raise TypeError("Could not get Array length")
898 class SequenceFieldDeclaration(FieldDeclaration
):
899 def __init__(self
, element_type
, length_field_name
):
901 Create a new sequence field declaration.
904 self
._ft
= nbt
._bt
_ctf
_field
_type
_sequence
_create
(element_type
._ft
,
905 str(length_field_name
))
909 def element_type(self
):
911 Get the sequence's element type.
914 ret
= nbt
._bt
_ctf
_field
_type
_sequence
_get
_element
_type
(self
._ft
)
917 raise TypeError("Could not get Sequence element type")
919 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(ret
)
922 def length_field_name(self
):
924 Get the sequence's length field name.
927 ret
= nbt
._bt
_ctf
_field
_type
_sequence
_get
_length
_field
_name
(self
._ft
)
930 raise TypeError("Could not get Sequence length field name")
935 class StringFieldDeclaration(FieldDeclaration
):
938 Create a new string field declaration.
941 self
._ft
= nbt
._bt
_ctf
_field
_type
_string
_create
()
947 Get a string declaration's encoding (a constant from the CTFStringEncoding class).
950 return nbt
._bt
_ctf
_field
_type
_string
_get
_encoding
(self
._ft
)
953 def encoding(self
, encoding
):
955 Set a string declaration's encoding. Must be a constant from the CTFStringEncoding class.
958 ret
= nbt
._bt
_ctf
_field
_type
_string
_set
_encoding
(self
._ft
, encoding
)
960 raise ValueError("Could not set string encoding.")
964 def create_field(field_type
):
966 Create an instance of a field.
968 isinst
= isinstance(field_type
, FieldDeclaration
)
970 if field_type
is None or not isinst
:
971 raise TypeError("Invalid field_type. Type must be a FieldDeclaration-derived class.")
973 if isinstance(field_type
, IntegerFieldDeclaration
):
974 return IntegerField(field_type
)
975 elif isinstance(field_type
, EnumerationFieldDeclaration
):
976 return EnumerationField(field_type
)
977 elif isinstance(field_type
, FloatFieldDeclaration
):
978 return FloatingPointField(field_type
)
979 elif isinstance(field_type
, StructureFieldDeclaration
):
980 return StructureField(field_type
)
981 elif isinstance(field_type
, VariantFieldDeclaration
):
982 return VariantField(field_type
)
983 elif isinstance(field_type
, ArrayFieldDeclaration
):
984 return ArrayField(field_type
)
985 elif isinstance(field_type
, SequenceFieldDeclaration
):
986 return SequenceField(field_type
)
987 elif isinstance(field_type
, StringFieldDeclaration
):
988 return StringField(field_type
)
993 Base class, do not instantiate.
996 def __init__(self
, field_type
):
997 if not isinstance(field_type
, FieldDeclaration
):
998 raise TypeError("Invalid field_type argument.")
1000 self
._f
= nbt
._bt
_ctf
_field
_create
(field_type
._ft
)
1003 raise ValueError("Field creation failed.")
1006 nbt
._bt
_ctf
_field
_put
(self
._f
)
1009 def _create_field_from_native_instance(native_field_instance
):
1011 common
.CTFTypeId
.INTEGER
: IntegerField
,
1012 common
.CTFTypeId
.FLOAT
: FloatingPointField
,
1013 common
.CTFTypeId
.ENUM
: EnumerationField
,
1014 common
.CTFTypeId
.STRING
: StringField
,
1015 common
.CTFTypeId
.STRUCT
: StructureField
,
1016 common
.CTFTypeId
.VARIANT
: VariantField
,
1017 common
.CTFTypeId
.ARRAY
: ArrayField
,
1018 common
.CTFTypeId
.SEQUENCE
: SequenceField
1021 field_type
= nbt
._bt
_python
_get
_field
_type
(native_field_instance
)
1023 if field_type
== common
.CTFTypeId
.UNKNOWN
:
1024 raise TypeError("Invalid field instance")
1026 field
= Field
.__new
__(Field
)
1027 field
._f
= native_field_instance
1028 field
.__class
__ = type_dict
[field_type
]
1033 def declaration(self
):
1034 native_field_type
= nbt
._bt
_ctf
_field
_get
_type
(self
._f
)
1036 if native_field_type
is None:
1037 raise TypeError("Invalid field instance")
1038 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(
1042 class IntegerField(Field
):
1046 Get an integer field's value.
1049 signedness
= nbt
._bt
_python
_field
_integer
_get
_signedness
(self
._f
)
1052 raise TypeError("Invalid integer instance.")
1055 ret
, value
= nbt
._bt
_ctf
_field
_unsigned
_integer
_get
_value
(self
._f
)
1057 ret
, value
= nbt
._bt
_ctf
_field
_signed
_integer
_get
_value
(self
._f
)
1060 raise ValueError("Could not get integer field value.")
1065 def value(self
, value
):
1067 Set an integer field's value.
1070 if not isinstance(value
, int):
1071 raise TypeError("IntegerField's value must be an int")
1073 signedness
= nbt
._bt
_python
_field
_integer
_get
_signedness
(self
._f
)
1075 raise TypeError("Invalid integer instance.")
1078 ret
= nbt
._bt
_ctf
_field
_unsigned
_integer
_set
_value
(self
._f
, value
)
1080 ret
= nbt
._bt
_ctf
_field
_signed
_integer
_set
_value
(self
._f
, value
)
1083 raise ValueError("Could not set integer field value.")
1086 class EnumerationField(Field
):
1088 def container(self
):
1090 Return the enumeration's underlying container field (an integer field).
1093 container
= IntegerField
.__new
__(IntegerField
)
1094 container
._f
= nbt
._bt
_ctf
_field
_enumeration
_get
_container
(self
._f
)
1096 if container
._f
is None:
1097 raise TypeError("Invalid enumeration field type.")
1104 Get the enumeration field's mapping name.
1107 value
= nbt
._bt
_ctf
_field
_enumeration
_get
_mapping
_name
(self
._f
)
1110 raise ValueError("Could not get enumeration's mapping name.")
1115 def value(self
, value
):
1117 Set the enumeration field's value. Must be an integer as mapping names
1121 if not isinstance(value
, int):
1122 raise TypeError("EnumerationField value must be an int")
1124 self
.container
.value
= value
1127 class FloatingPointField(Field
):
1131 Get a floating point field's value.
1134 ret
, value
= nbt
._bt
_ctf
_field
_floating
_point
_get
_value
(self
._f
)
1137 raise ValueError("Could not get floating point field value.")
1142 def value(self
, value
):
1144 Set a floating point field's value.
1147 if not isinstance(value
, int) and not isinstance(value
, float):
1148 raise TypeError("Value must be either a float or an int")
1150 ret
= nbt
._bt
_ctf
_field
_floating
_point
_set
_value
(self
._f
, float(value
))
1153 raise ValueError("Could not set floating point field value.")
1156 # oops!! This class is provided to ensure backward-compatibility since
1157 # a stable release publicly exposed this abomination.
1158 class FloatFieldingPoint(FloatingPointField
):
1162 class StructureField(Field
):
1163 def field(self
, field_name
):
1165 Get the structure's field corresponding to the provided field name.
1168 native_instance
= nbt
._bt
_ctf
_field
_structure
_get
_field
(self
._f
,
1171 if native_instance
is None:
1172 raise ValueError("Invalid field_name provided.")
1174 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1177 class VariantField(Field
):
1178 def field(self
, tag
):
1180 Return the variant's selected field. The "tag" field is the selector enum field.
1183 native_instance
= nbt
._bt
_ctf
_field
_variant
_get
_field
(self
._f
, tag
._f
)
1185 if native_instance
is None:
1186 raise ValueError("Invalid tag provided.")
1188 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1191 class ArrayField(Field
):
1192 def field(self
, index
):
1194 Return the array's field at position "index".
1197 native_instance
= nbt
._bt
_ctf
_field
_array
_get
_field
(self
._f
, index
)
1199 if native_instance
is None:
1200 raise IndexError("Invalid index provided.")
1202 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1205 class SequenceField(Field
):
1209 Get the sequence's length field (IntegerField).
1212 native_instance
= nbt
._bt
_ctf
_field
_sequence
_get
_length
(self
._f
)
1214 if native_instance
is None:
1217 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1220 def length(self
, length_field
):
1222 Set the sequence's length field (IntegerField).
1225 if not isinstance(length_field
, IntegerField
):
1226 raise TypeError("Invalid length field.")
1228 if length_field
.declaration
.signed
:
1229 raise TypeError("Sequence field length must be unsigned")
1231 ret
= nbt
._bt
_ctf
_field
_sequence
_set
_length
(self
._f
, length_field
._f
)
1234 raise ValueError("Could not set sequence length.")
1236 def field(self
, index
):
1238 Return the sequence's field at position "index".
1241 native_instance
= nbt
._bt
_ctf
_field
_sequence
_get
_field
(self
._f
, index
)
1243 if native_instance
is None:
1244 raise ValueError("Could not get sequence element at index.")
1246 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1249 class StringField(Field
):
1253 Get a string field's value.
1256 return nbt
._bt
_ctf
_field
_string
_get
_value
(self
._f
)
1259 def value(self
, value
):
1261 Set a string field's value.
1264 ret
= nbt
._bt
_ctf
_field
_string
_set
_value
(self
._f
, str(value
))
1267 raise ValueError("Could not set string field value.")
1271 def __init__(self
, name
):
1273 Create a new event class of the given name.
1276 self
._ec
= nbt
._bt
_ctf
_event
_class
_create
(name
)
1278 if self
._ec
is None:
1279 raise ValueError("Event class creation failed.")
1282 nbt
._bt
_ctf
_event
_class
_put
(self
._ec
)
1284 def add_field(self
, field_type
, field_name
):
1286 Add a field of type "field_type" to the event class.
1289 ret
= nbt
._bt
_ctf
_event
_class
_add
_field
(self
._ec
, field_type
._ft
,
1293 raise ValueError("Could not add field to event class.")
1298 Get the event class' name.
1301 name
= nbt
._bt
_ctf
_event
_class
_get
_name
(self
._ec
)
1304 raise TypeError("Could not get EventClass name")
1311 Get the event class' id. Returns a negative value if unset.
1314 id = nbt
._bt
_ctf
_event
_class
_get
_id
(self
._ec
)
1317 raise TypeError("Could not get EventClass id")
1324 Set the event class' id. Throws a TypeError if the event class
1325 is already registered to a stream class.
1328 ret
= nbt
._bt
_ctf
_event
_class
_set
_id
(self
._ec
, id)
1331 raise TypeError("Can't change an Event Class's id after it has been assigned to a stream class")
1334 def stream_class(self
):
1336 Get the event class' stream class. Returns None if unset.
1338 stream_class_native
= nbt
._bt
_ctf
_event
_class
_get
_stream
_class
(self
._ec
)
1340 if stream_class_native
is None:
1343 stream_class
= StreamClass
.__new
__(StreamClass
)
1344 stream_class
._sc
= stream_class_native
1351 Generator returning the event class' fields as tuples of (field name, field declaration).
1354 count
= nbt
._bt
_ctf
_event
_class
_get
_field
_count
(self
._ec
)
1357 raise TypeError("Could not get EventClass' field count")
1359 for i
in range(count
):
1360 field_name
= nbt
._bt
_python
_ctf
_event
_class
_get
_field
_name
(self
._ec
, i
)
1362 if field_name
is None:
1363 msg
= "Could not get EventClass' field name at index {}".format(i
)
1364 raise TypeError(msg
)
1366 field_type_native
= nbt
._bt
_python
_ctf
_event
_class
_get
_field
_type
(self
._ec
, i
)
1368 if field_type_native
is None:
1369 msg
= "Could not get EventClass' field type at index {}".format(i
)
1370 raise TypeError(msg
)
1372 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
1373 yield (field_name
, field_type
)
1375 def get_field_by_name(self
, name
):
1377 Get a field declaration by name (FieldDeclaration).
1380 field_type_native
= nbt
._bt
_ctf
_event
_class
_get
_field
_by
_name
(self
._ec
, name
)
1382 if field_type_native
is None:
1383 msg
= "Could not find EventClass field with name {}".format(name
)
1384 raise TypeError(msg
)
1386 return FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
1390 def __init__(self
, event_class
):
1392 Create a new event of the given event class.
1395 if not isinstance(event_class
, EventClass
):
1396 raise TypeError("Invalid event_class argument.")
1398 self
._e
= nbt
._bt
_ctf
_event
_create
(event_class
._ec
)
1401 raise ValueError("Event creation failed.")
1404 nbt
._bt
_ctf
_event
_put
(self
._e
)
1407 def event_class(self
):
1409 Get the event's class.
1412 event_class_native
= nbt
._bt
_ctf
_event
_get
_class
(self
._e
)
1414 if event_class_native
is None:
1417 event_class
= EventClass
.__new
__(EventClass
)
1418 event_class
._ec
= event_class_native
1424 Get a clock from event. Returns None if the event's class
1425 is not registered to a stream class.
1428 clock_instance
= nbt
._bt
_ctf
_event
_get
_clock
(self
._e
)
1430 if clock_instance
is None:
1433 clock
= Clock
.__new
__(Clock
)
1434 clock
._c
= clock_instance
1438 def payload(self
, field_name
):
1440 Get a field from event.
1443 native_instance
= nbt
._bt
_ctf
_event
_get
_payload
(self
._e
,
1446 if native_instance
is None:
1447 raise ValueError("Could not get event payload.")
1449 return Field
._create
_field
_from
_native
_instance
(native_instance
)
1451 def set_payload(self
, field_name
, value_field
):
1453 Set a manually created field as an event's payload.
1456 if not isinstance(value
, Field
):
1457 raise TypeError("Invalid value type.")
1459 ret
= nbt
._bt
_ctf
_event
_set
_payload
(self
._e
, str(field_name
),
1463 raise ValueError("Could not set event field payload.")
1467 def __init__(self
, name
):
1469 Create a new stream class of the given name.
1472 self
._sc
= nbt
._bt
_ctf
_stream
_class
_create
(name
)
1474 if self
._sc
is None:
1475 raise ValueError("Stream class creation failed.")
1478 nbt
._bt
_ctf
_stream
_class
_put
(self
._sc
)
1483 Get a stream class' name.
1486 name
= nbt
._bt
_ctf
_stream
_class
_get
_name
(self
._sc
)
1489 raise TypeError("Could not get StreamClass name")
1496 Get a stream class' clock.
1499 clock_instance
= nbt
._bt
_ctf
_stream
_class
_get
_clock
(self
._sc
)
1501 if clock_instance
is None:
1504 clock
= Clock
.__new
__(Clock
)
1505 clock
._c
= clock_instance
1510 def clock(self
, clock
):
1512 Assign a clock to a stream class.
1515 if not isinstance(clock
, Clock
):
1516 raise TypeError("Invalid clock type.")
1518 ret
= nbt
._bt
_ctf
_stream
_class
_set
_clock
(self
._sc
, clock
._c
)
1521 raise ValueError("Could not set stream class clock.")
1526 Get a stream class' id.
1529 ret
= nbt
._bt
_ctf
_stream
_class
_get
_id
(self
._sc
)
1532 raise TypeError("Could not get StreamClass id")
1539 Assign an id to a stream class.
1542 ret
= nbt
._bt
_ctf
_stream
_class
_set
_id
(self
._sc
, id)
1545 raise TypeError("Could not set stream class id.")
1548 def event_classes(self
):
1550 Generator returning the stream class' event classes.
1553 count
= nbt
._bt
_ctf
_stream
_class
_get
_event
_class
_count
(self
._sc
)
1556 raise TypeError("Could not get StreamClass' event class count")
1558 for i
in range(count
):
1559 event_class_native
= nbt
._bt
_ctf
_stream
_class
_get
_event
_class
(self
._sc
, i
)
1561 if event_class_native
is None:
1562 msg
= "Could not get StreamClass' event class at index {}".format(i
)
1563 raise TypeError(msg
)
1565 event_class
= EventClass
.__new
__(EventClass
)
1566 event_class
._ec
= event_class_native
1569 def add_event_class(self
, event_class
):
1571 Add an event class to a stream class. New events can be added even after a
1572 stream has been instantiated and events have been appended. However, a stream
1573 will not accept events of a class that has not been added to the stream
1577 if not isinstance(event_class
, EventClass
):
1578 raise TypeError("Invalid event_class type.")
1580 ret
= nbt
._bt
_ctf
_stream
_class
_add
_event
_class
(self
._sc
,
1584 raise ValueError("Could not add event class.")
1587 def packet_context_type(self
):
1589 Get the StreamClass' packet context type (StructureFieldDeclaration)
1592 field_type_native
= nbt
._bt
_ctf
_stream
_class
_get
_packet
_context
_type
(self
._sc
)
1594 if field_type_native
is None:
1595 raise ValueError("Invalid StreamClass")
1597 field_type
= FieldDeclaration
._create
_field
_declaration
_from
_native
_instance
(field_type_native
)
1601 @packet_context_type.setter
1602 def packet_context_type(self
, field_type
):
1604 Set a StreamClass' packet context type. Must be of type
1605 StructureFieldDeclaration.
1608 if not isinstance(field_type
, StructureFieldDeclaration
):
1609 raise TypeError("field_type argument must be of type StructureFieldDeclaration.")
1611 ret
= nbt
._bt
_ctf
_stream
_class
_set
_packet
_context
_type
(self
._sc
,
1615 raise ValueError("Failed to set packet context type.")
1620 raise NotImplementedError("Stream cannot be instantiated; use Writer.create_stream()")
1623 nbt
._bt
_ctf
_stream
_put
(self
._s
)
1626 def discarded_events(self
):
1628 Get a stream's discarded event count.
1631 ret
, count
= nbt
._bt
_ctf
_stream
_get
_discarded
_events
_count
(self
._s
)
1634 raise ValueError("Could not get the stream's discarded events count")
1638 def append_discarded_events(self
, event_count
):
1640 Increase the current packet's discarded event count.
1643 nbt
._bt
_ctf
_stream
_append
_discarded
_events
(self
._s
, event_count
)
1645 def append_event(self
, event
):
1647 Append "event" to the stream's current packet. The stream's associated clock
1648 will be sampled during this call. The event shall not be modified after
1649 being appended to a stream.
1652 ret
= nbt
._bt
_ctf
_stream
_append
_event
(self
._s
, event
._e
)
1655 raise ValueError("Could not append event to stream.")
1658 def packet_context(self
):
1660 Get a Stream's packet context field (a StructureField).
1663 native_field
= nbt
._bt
_ctf
_stream
_get
_packet
_context
(self
._s
)
1665 if native_field
is None:
1666 raise ValueError("Invalid Stream.")
1668 return Field
._create
_field
_from
_native
_instance
(native_field
)
1670 @packet_context.setter
1671 def packet_context(self
, field
):
1673 Set a Stream's packet context field (must be a StructureField).
1676 if not isinstance(field
, StructureField
):
1677 raise TypeError("Argument field must be of type StructureField")
1679 ret
= nbt
._bt
_ctf
_stream
_set
_packet
_context
(self
._s
, field
._f
)
1682 raise ValueError("Invalid packet context field.")
1686 The stream's current packet's events will be flushed to disk. Events
1687 subsequently appended to the stream will be added to a new packet.
1690 ret
= nbt
._bt
_ctf
_stream
_flush
(self
._s
)
1693 raise ValueError("Could not flush stream.")
1697 def __init__(self
, path
):
1699 Create a new writer that will produce a trace in the given path.
1702 self
._w
= nbt
._bt
_ctf
_writer
_create
(path
)
1705 raise ValueError("Writer creation failed.")
1708 nbt
._bt
_ctf
_writer
_put
(self
._w
)
1710 def create_stream(self
, stream_class
):
1712 Create a new stream instance and register it to the writer.
1715 if not isinstance(stream_class
, StreamClass
):
1716 raise TypeError("Invalid stream_class type.")
1718 stream
= Stream
.__new
__(Stream
)
1719 stream
._s
= nbt
._bt
_ctf
_writer
_create
_stream
(self
._w
, stream_class
._sc
)
1723 def add_environment_field(self
, name
, value
):
1725 Add an environment field to the trace.
1728 ret
= nbt
._bt
_ctf
_writer
_add
_environment
_field
(self
._w
, str(name
),
1732 raise ValueError("Could not add environment field to trace.")
1734 def add_clock(self
, clock
):
1736 Add a clock to the trace. Clocks assigned to stream classes must be
1737 registered to the writer.
1740 ret
= nbt
._bt
_ctf
_writer
_add
_clock
(self
._w
, clock
._c
)
1743 raise ValueError("Could not add clock to Writer.")
1748 Get the trace's TSDL meta-data.
1751 return nbt
._bt
_ctf
_writer
_get
_metadata
_string
(self
._w
)
1753 def flush_metadata(self
):
1755 Flush the trace's metadata to the metadata file.
1758 nbt
._bt
_ctf
_writer
_flush
_metadata
(self
._w
)
1761 def byte_order(self
):
1763 Get the trace's byte order. Must be a constant from the ByteOrder
1767 raise NotImplementedError("Getter not implemented.")
1770 def byte_order(self
, byte_order
):
1772 Set the trace's byte order. Must be a constant from the ByteOrder
1773 class. Defaults to the host machine's endianness
1776 ret
= nbt
._bt
_ctf
_writer
_set
_byte
_order
(self
._w
, byte_order
)
1779 raise ValueError("Could not set trace's byte order.")