1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4 # Copyright (c) 2018 Francis Deslauriers <francis.deslauriers@efficios.com>
5 # Copyright (c) 2019 Simon Marchi <simon.marchi@efficios.com>
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 __all__
= ['_TraceClass']
28 from bt2
import native_bt
, utils
, object
29 import bt2
.stream_class
30 import collections
.abc
34 class _StreamClassIterator(collections
.abc
.Iterator
):
35 def __init__(self
, trace_class
):
36 self
._trace
_class
= trace_class
40 if self
._at
== len(self
._trace
_class
):
43 borrow_stream_class_fn
= native_bt
.trace_class_borrow_stream_class_by_index_const
44 sc_ptr
= borrow_stream_class_fn(self
._trace
_class
._ptr
, self
._at
)
46 id = native_bt
.stream_class_get_id(sc_ptr
)
52 def _trace_class_destruction_listener_from_native(user_listener
, trace_class_ptr
):
53 trace_class
= bt2
.trace_class
._TraceClass
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
54 user_listener(trace_class
)
57 class _TraceClass(object._SharedObject
, collections
.abc
.Mapping
):
58 _get_ref
= staticmethod(native_bt
.trace_class_get_ref
)
59 _put_ref
= staticmethod(native_bt
.trace_class_put_ref
)
61 # Instantiate a trace of this class.
63 def __call__(self
, name
=None, uuid
=None, env
=None):
64 trace_ptr
= native_bt
.trace_create(self
._ptr
)
67 raise bt2
.CreationError('cannot create trace class object')
69 trace
= bt2
.trace
._Trace
._create
_from
_ptr
(trace_ptr
)
78 for key
, value
in env
.items():
79 trace
.env
[key
] = value
83 # Number of stream classes in this trace class.
86 count
= native_bt
.trace_class_get_stream_class_count(self
._ptr
)
90 # Get a stream class by stream id.
92 def __getitem__(self
, key
):
93 utils
._check
_uint
64(key
)
95 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_id_const(self
._ptr
, key
)
99 return bt2
.stream_class
._StreamClass
._create
_from
_ptr
_and
_get
_ref
(sc_ptr
)
102 for idx
in range(len(self
)):
103 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_index_const(self
._ptr
, idx
)
104 assert sc_ptr
is not None
106 id = native_bt
.stream_class_get_id(sc_ptr
)
111 def create_stream_class(self
, id=None,
113 packet_context_field_class
=None,
114 event_common_context_field_class
=None,
115 default_clock_class
=None,
116 assigns_automatic_event_class_id
=True,
117 assigns_automatic_stream_id
=True,
118 supports_packets
=False,
119 packets_have_beginning_default_clock_snapshot
=False,
120 packets_have_end_default_clock_snapshot
=False,
121 supports_discarded_events
=False,
122 discarded_events_have_default_clock_snapshots
=False,
123 supports_discarded_packets
=False,
124 discarded_packets_have_default_clock_snapshots
=False):
126 if self
.assigns_automatic_stream_class_id
:
128 raise ValueError('id provided, but trace class assigns automatic stream class ids')
130 sc_ptr
= native_bt
.stream_class_create(self
._ptr
)
133 raise ValueError('id not provided, but trace class does not assign automatic stream class ids')
135 utils
._check
_uint
64(id)
136 sc_ptr
= native_bt
.stream_class_create_with_id(self
._ptr
, id)
138 sc
= bt2
.stream_class
._StreamClass
._create
_from
_ptr
(sc_ptr
)
143 if event_common_context_field_class
is not None:
144 sc
._event
_common
_context
_field
_class
= event_common_context_field_class
146 if default_clock_class
is not None:
147 sc
._default
_clock
_class
= default_clock_class
149 # call after `sc._default_clock_class` because, if
150 # `packets_have_beginning_default_clock_snapshot` or
151 # `packets_have_end_default_clock_snapshot` is true, then this
152 # stream class needs a default clock class already.
153 sc
._set
_supports
_packets
(supports_packets
,
154 packets_have_beginning_default_clock_snapshot
,
155 packets_have_end_default_clock_snapshot
)
157 # call after sc._set_supports_packets() because, if
158 # `packet_context_field_class` is not `None`, then this stream
159 # class needs to support packets already.
160 if packet_context_field_class
is not None:
161 sc
._packet
_context
_field
_class
= packet_context_field_class
163 sc
._assigns
_automatic
_event
_class
_id
= assigns_automatic_event_class_id
164 sc
._assigns
_automatic
_stream
_id
= assigns_automatic_stream_id
165 sc
._set
_supports
_discarded
_events
(supports_discarded_events
,
166 discarded_events_have_default_clock_snapshots
)
167 sc
._set
_supports
_discarded
_packets
(supports_discarded_packets
,
168 discarded_packets_have_default_clock_snapshots
)
172 def assigns_automatic_stream_class_id(self
):
173 return native_bt
.trace_class_assigns_automatic_stream_class_id(self
._ptr
)
175 def _assigns_automatic_stream_class_id(self
, auto_id
):
176 utils
._check
_bool
(auto_id
)
177 return native_bt
.trace_class_set_assigns_automatic_stream_class_id(self
._ptr
, auto_id
)
179 _assigns_automatic_stream_class_id
= property(fset
=_assigns_automatic_stream_class_id
)
181 # Field class creation methods.
183 def _check_create_status(self
, ptr
, type_name
):
185 raise bt2
.CreationError(
186 'cannot create {} field class'.format(type_name
))
188 def _create_integer_field_class(self
, create_func
, py_cls
, type_name
, field_value_range
, preferred_display_base
):
189 field_class_ptr
= create_func(self
._ptr
)
190 self
._check
_create
_status
(field_class_ptr
, type_name
)
192 field_class
= py_cls
._create
_from
_ptr
(field_class_ptr
)
194 if field_value_range
is not None:
195 field_class
._field
_value
_range
= field_value_range
197 if preferred_display_base
is not None:
198 field_class
._preferred
_display
_base
= preferred_display_base
202 def create_signed_integer_field_class(self
, field_value_range
=None, preferred_display_base
=None):
203 return self
._create
_integer
_field
_class
(native_bt
.field_class_signed_integer_create
,
204 bt2
.field_class
._SignedIntegerFieldClass
,
205 'signed integer', field_value_range
, preferred_display_base
)
207 def create_unsigned_integer_field_class(self
, field_value_range
=None, preferred_display_base
=None):
208 return self
._create
_integer
_field
_class
(native_bt
.field_class_unsigned_integer_create
,
209 bt2
.field_class
._UnsignedIntegerFieldClass
,
210 'unsigned integer', field_value_range
, preferred_display_base
)
212 def create_signed_enumeration_field_class(self
, field_value_range
=None, preferred_display_base
=None):
213 return self
._create
_integer
_field
_class
(native_bt
.field_class_signed_enumeration_create
,
214 bt2
.field_class
._SignedEnumerationFieldClass
,
215 'signed enumeration', field_value_range
, preferred_display_base
)
217 def create_unsigned_enumeration_field_class(self
, field_value_range
=None, preferred_display_base
=None):
218 return self
._create
_integer
_field
_class
(native_bt
.field_class_unsigned_enumeration_create
,
219 bt2
.field_class
._UnsignedEnumerationFieldClass
,
220 'unsigned enumeration', field_value_range
, preferred_display_base
)
222 def create_real_field_class(self
, is_single_precision
=False):
223 field_class_ptr
= native_bt
.field_class_real_create(self
._ptr
)
224 self
._check
_create
_status
(field_class_ptr
, 'real')
226 field_class
= bt2
.field_class
._RealFieldClass
._create
_from
_ptr
(field_class_ptr
)
228 field_class
._is
_single
_precision
= is_single_precision
232 def create_structure_field_class(self
):
233 field_class_ptr
= native_bt
.field_class_structure_create(self
._ptr
)
234 self
._check
_create
_status
(field_class_ptr
, 'structure')
236 return bt2
.field_class
._StructureFieldClass
._create
_from
_ptr
(field_class_ptr
)
238 def create_string_field_class(self
):
239 field_class_ptr
= native_bt
.field_class_string_create(self
._ptr
)
240 self
._check
_create
_status
(field_class_ptr
, 'string')
242 return bt2
.field_class
._StringFieldClass
._create
_from
_ptr
(field_class_ptr
)
244 def create_static_array_field_class(self
, elem_fc
, length
):
245 utils
._check
_type
(elem_fc
, bt2
.field_class
._FieldClass
)
246 utils
._check
_uint
64(length
)
247 ptr
= native_bt
.field_class_static_array_create(self
._ptr
, elem_fc
._ptr
, length
)
248 self
._check
_create
_status
(ptr
, 'static array')
250 return bt2
.field_class
._StaticArrayFieldClass
._create
_from
_ptr
_and
_get
_ref
(ptr
)
252 def create_dynamic_array_field_class(self
, elem_fc
, length_fc
=None):
253 utils
._check
_type
(elem_fc
, bt2
.field_class
._FieldClass
)
254 ptr
= native_bt
.field_class_dynamic_array_create(self
._ptr
, elem_fc
._ptr
)
255 self
._check
_create
_status
(ptr
, 'dynamic array')
256 obj
= bt2
.field_class
._DynamicArrayFieldClass
._create
_from
_ptr
(ptr
)
258 if length_fc
is not None:
259 obj
._length
_field
_class
= length_fc
263 def create_variant_field_class(self
, selector_fc
=None):
264 ptr
= native_bt
.field_class_variant_create(self
._ptr
)
265 self
._check
_create
_status
(ptr
, 'variant')
266 obj
= bt2
.field_class
._VariantFieldClass
._create
_from
_ptr
(ptr
)
268 if selector_fc
is not None:
269 obj
._selector
_field
_class
= selector_fc
273 # Add a listener to be called when the trace class is destroyed.
275 def add_destruction_listener(self
, listener
):
277 if not callable(listener
):
278 raise TypeError("'listener' parameter is not callable")
280 fn
= native_bt
.bt2_trace_class_add_destruction_listener
281 listener_from_native
= functools
.partial(_trace_class_destruction_listener_from_native
,
284 listener_id
= fn(self
._ptr
, listener_from_native
)
285 if listener_id
is None:
286 utils
._raise
_bt
2_error
('cannot add destruction listener to trace class object')
288 return bt2
._ListenerHandle(listener_id
, self
)