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 bt2
.field_class
31 import collections
.abc
35 class _StreamClassIterator(collections
.abc
.Iterator
):
36 def __init__(self
, trace_class
):
37 self
._trace
_class
= trace_class
41 if self
._at
== len(self
._trace
_class
):
44 borrow_stream_class_fn
= native_bt
.trace_class_borrow_stream_class_by_index_const
45 sc_ptr
= borrow_stream_class_fn(self
._trace
_class
._ptr
, self
._at
)
47 id = native_bt
.stream_class_get_id(sc_ptr
)
53 def _trace_class_destruction_listener_from_native(user_listener
, trace_class_ptr
):
54 trace_class
= bt2
.trace_class
._TraceClass
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
55 user_listener(trace_class
)
58 class _TraceClass(object._SharedObject
, collections
.abc
.Mapping
):
59 _get_ref
= staticmethod(native_bt
.trace_class_get_ref
)
60 _put_ref
= staticmethod(native_bt
.trace_class_put_ref
)
62 # Instantiate a trace of this class.
64 def __call__(self
, name
=None, uuid
=None, env
=None):
65 trace_ptr
= native_bt
.trace_create(self
._ptr
)
68 raise bt2
.CreationError('cannot create trace class object')
70 trace
= bt2
.trace
._Trace
._create
_from
_ptr
(trace_ptr
)
79 for key
, value
in env
.items():
80 trace
.env
[key
] = value
84 # Number of stream classes in this trace class.
87 count
= native_bt
.trace_class_get_stream_class_count(self
._ptr
)
91 # Get a stream class by stream id.
93 def __getitem__(self
, key
):
94 utils
._check
_uint
64(key
)
96 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_id_const(self
._ptr
, key
)
100 return bt2
.stream_class
._StreamClass
._create
_from
_ptr
_and
_get
_ref
(sc_ptr
)
103 for idx
in range(len(self
)):
104 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_index_const(self
._ptr
, idx
)
105 assert sc_ptr
is not None
107 id = native_bt
.stream_class_get_id(sc_ptr
)
112 def create_stream_class(self
, id=None,
114 packet_context_field_class
=None,
115 event_common_context_field_class
=None,
116 default_clock_class
=None,
117 assigns_automatic_event_class_id
=True,
118 assigns_automatic_stream_id
=True,
119 supports_packets
=False,
120 packets_have_beginning_default_clock_snapshot
=False,
121 packets_have_end_default_clock_snapshot
=False,
122 supports_discarded_events
=False,
123 discarded_events_have_default_clock_snapshots
=False,
124 supports_discarded_packets
=False,
125 discarded_packets_have_default_clock_snapshots
=False):
127 if self
.assigns_automatic_stream_class_id
:
129 raise ValueError('id provided, but trace class assigns automatic stream class ids')
131 sc_ptr
= native_bt
.stream_class_create(self
._ptr
)
134 raise ValueError('id not provided, but trace class does not assign automatic stream class ids')
136 utils
._check
_uint
64(id)
137 sc_ptr
= native_bt
.stream_class_create_with_id(self
._ptr
, id)
139 sc
= bt2
.stream_class
._StreamClass
._create
_from
_ptr
(sc_ptr
)
144 if event_common_context_field_class
is not None:
145 sc
._event
_common
_context
_field
_class
= event_common_context_field_class
147 if default_clock_class
is not None:
148 sc
._default
_clock
_class
= default_clock_class
150 # call after `sc._default_clock_class` because, if
151 # `packets_have_beginning_default_clock_snapshot` or
152 # `packets_have_end_default_clock_snapshot` is true, then this
153 # stream class needs a default clock class already.
154 sc
._set
_supports
_packets
(supports_packets
,
155 packets_have_beginning_default_clock_snapshot
,
156 packets_have_end_default_clock_snapshot
)
158 # call after sc._set_supports_packets() because, if
159 # `packet_context_field_class` is not `None`, then this stream
160 # class needs to support packets already.
161 if packet_context_field_class
is not None:
162 sc
._packet
_context
_field
_class
= packet_context_field_class
164 sc
._assigns
_automatic
_event
_class
_id
= assigns_automatic_event_class_id
165 sc
._assigns
_automatic
_stream
_id
= assigns_automatic_stream_id
166 sc
._set
_supports
_discarded
_events
(supports_discarded_events
,
167 discarded_events_have_default_clock_snapshots
)
168 sc
._set
_supports
_discarded
_packets
(supports_discarded_packets
,
169 discarded_packets_have_default_clock_snapshots
)
173 def assigns_automatic_stream_class_id(self
):
174 return native_bt
.trace_class_assigns_automatic_stream_class_id(self
._ptr
)
176 def _assigns_automatic_stream_class_id(self
, auto_id
):
177 utils
._check
_bool
(auto_id
)
178 return native_bt
.trace_class_set_assigns_automatic_stream_class_id(self
._ptr
, auto_id
)
180 _assigns_automatic_stream_class_id
= property(fset
=_assigns_automatic_stream_class_id
)
182 # Field class creation methods.
184 def _check_create_status(self
, ptr
, type_name
):
186 raise bt2
.CreationError(
187 'cannot create {} field class'.format(type_name
))
189 def _create_integer_field_class(self
, create_func
, py_cls
, type_name
, field_value_range
, preferred_display_base
):
190 field_class_ptr
= create_func(self
._ptr
)
191 self
._check
_create
_status
(field_class_ptr
, type_name
)
193 field_class
= py_cls
._create
_from
_ptr
(field_class_ptr
)
195 if field_value_range
is not None:
196 field_class
._field
_value
_range
= field_value_range
198 if preferred_display_base
is not None:
199 field_class
._preferred
_display
_base
= preferred_display_base
203 def create_signed_integer_field_class(self
, field_value_range
=None, preferred_display_base
=None):
204 return self
._create
_integer
_field
_class
(native_bt
.field_class_signed_integer_create
,
205 bt2
.field_class
._SignedIntegerFieldClass
,
206 'signed integer', field_value_range
, preferred_display_base
)
208 def create_unsigned_integer_field_class(self
, field_value_range
=None, preferred_display_base
=None):
209 return self
._create
_integer
_field
_class
(native_bt
.field_class_unsigned_integer_create
,
210 bt2
.field_class
._UnsignedIntegerFieldClass
,
211 'unsigned integer', field_value_range
, preferred_display_base
)
213 def create_signed_enumeration_field_class(self
, field_value_range
=None, preferred_display_base
=None):
214 return self
._create
_integer
_field
_class
(native_bt
.field_class_signed_enumeration_create
,
215 bt2
.field_class
._SignedEnumerationFieldClass
,
216 'signed enumeration', field_value_range
, preferred_display_base
)
218 def create_unsigned_enumeration_field_class(self
, field_value_range
=None, preferred_display_base
=None):
219 return self
._create
_integer
_field
_class
(native_bt
.field_class_unsigned_enumeration_create
,
220 bt2
.field_class
._UnsignedEnumerationFieldClass
,
221 'unsigned enumeration', field_value_range
, preferred_display_base
)
223 def create_real_field_class(self
, is_single_precision
=False):
224 field_class_ptr
= native_bt
.field_class_real_create(self
._ptr
)
225 self
._check
_create
_status
(field_class_ptr
, 'real')
227 field_class
= bt2
.field_class
._RealFieldClass
._create
_from
_ptr
(field_class_ptr
)
229 field_class
._is
_single
_precision
= is_single_precision
233 def create_structure_field_class(self
):
234 field_class_ptr
= native_bt
.field_class_structure_create(self
._ptr
)
235 self
._check
_create
_status
(field_class_ptr
, 'structure')
237 return bt2
.field_class
._StructureFieldClass
._create
_from
_ptr
(field_class_ptr
)
239 def create_string_field_class(self
):
240 field_class_ptr
= native_bt
.field_class_string_create(self
._ptr
)
241 self
._check
_create
_status
(field_class_ptr
, 'string')
243 return bt2
.field_class
._StringFieldClass
._create
_from
_ptr
(field_class_ptr
)
245 def create_static_array_field_class(self
, elem_fc
, length
):
246 utils
._check
_type
(elem_fc
, bt2
.field_class
._FieldClass
)
247 utils
._check
_uint
64(length
)
248 ptr
= native_bt
.field_class_static_array_create(self
._ptr
, elem_fc
._ptr
, length
)
249 self
._check
_create
_status
(ptr
, 'static array')
251 return bt2
.field_class
._StaticArrayFieldClass
._create
_from
_ptr
_and
_get
_ref
(ptr
)
253 def create_dynamic_array_field_class(self
, elem_fc
, length_fc
=None):
254 utils
._check
_type
(elem_fc
, bt2
.field_class
._FieldClass
)
255 ptr
= native_bt
.field_class_dynamic_array_create(self
._ptr
, elem_fc
._ptr
)
256 self
._check
_create
_status
(ptr
, 'dynamic array')
257 obj
= bt2
.field_class
._DynamicArrayFieldClass
._create
_from
_ptr
(ptr
)
259 if length_fc
is not None:
260 obj
._length
_field
_class
= length_fc
264 def create_variant_field_class(self
, selector_fc
=None):
265 selector_fc_ptr
= None
267 if selector_fc
is not None:
268 utils
._check
_type
(selector_fc
, bt2
.field_class
._IntegerFieldClass
)
269 selector_fc_ptr
= selector_fc
._ptr
271 ptr
= native_bt
.field_class_variant_create(self
._ptr
, selector_fc_ptr
)
272 self
._check
_create
_status
(ptr
, 'variant')
273 return bt2
.field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
275 # Add a listener to be called when the trace class is destroyed.
277 def add_destruction_listener(self
, listener
):
279 if not callable(listener
):
280 raise TypeError("'listener' parameter is not callable")
282 fn
= native_bt
.bt2_trace_class_add_destruction_listener
283 listener_from_native
= functools
.partial(_trace_class_destruction_listener_from_native
,
286 listener_id
= fn(self
._ptr
, listener_from_native
)
287 if listener_id
is None:
288 utils
._raise
_bt
2_error
('cannot add destruction listener to trace class object')
290 return bt2
._ListenerHandle(listener_id
, self
)