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 from bt2
import stream_class
as bt2_stream_class
30 from bt2
import field_class
as bt2_field_class
31 from bt2
import trace
as bt2_trace
32 from bt2
import trace_class
as bt2_trace_class
33 import collections
.abc
37 def _trace_class_destruction_listener_from_native(user_listener
, trace_class_ptr
):
38 trace_class
= bt2_trace_class
._TraceClass
._create
_from
_ptr
_and
_get
_ref
(
41 user_listener(trace_class
)
44 class _TraceClass(object._SharedObject
, collections
.abc
.Mapping
):
45 _get_ref
= staticmethod(native_bt
.trace_class_get_ref
)
46 _put_ref
= staticmethod(native_bt
.trace_class_put_ref
)
48 # Instantiate a trace of this class.
50 def __call__(self
, name
=None, uuid
=None, env
=None):
51 trace_ptr
= native_bt
.trace_create(self
._ptr
)
54 raise bt2
._MemoryError('cannot create trace class object')
56 trace
= bt2_trace
._Trace
._create
_from
_ptr
(trace_ptr
)
65 for key
, value
in env
.items():
66 trace
.env
[key
] = value
70 # Number of stream classes in this trace class.
73 count
= native_bt
.trace_class_get_stream_class_count(self
._ptr
)
77 # Get a stream class by stream id.
79 def __getitem__(self
, key
):
80 utils
._check
_uint
64(key
)
82 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_id_const(self
._ptr
, key
)
86 return bt2_stream_class
._StreamClass
._create
_from
_ptr
_and
_get
_ref
(sc_ptr
)
89 for idx
in range(len(self
)):
90 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_index_const(
93 assert sc_ptr
is not None
95 id = native_bt
.stream_class_get_id(sc_ptr
)
100 def create_stream_class(
104 packet_context_field_class
=None,
105 event_common_context_field_class
=None,
106 default_clock_class
=None,
107 assigns_automatic_event_class_id
=True,
108 assigns_automatic_stream_id
=True,
109 supports_packets
=False,
110 packets_have_beginning_default_clock_snapshot
=False,
111 packets_have_end_default_clock_snapshot
=False,
112 supports_discarded_events
=False,
113 discarded_events_have_default_clock_snapshots
=False,
114 supports_discarded_packets
=False,
115 discarded_packets_have_default_clock_snapshots
=False,
118 if self
.assigns_automatic_stream_class_id
:
121 'id provided, but trace class assigns automatic stream class ids'
124 sc_ptr
= native_bt
.stream_class_create(self
._ptr
)
128 'id not provided, but trace class does not assign automatic stream class ids'
131 utils
._check
_uint
64(id)
132 sc_ptr
= native_bt
.stream_class_create_with_id(self
._ptr
, id)
134 sc
= bt2_stream_class
._StreamClass
._create
_from
_ptr
(sc_ptr
)
139 if event_common_context_field_class
is not None:
140 sc
._event
_common
_context
_field
_class
= event_common_context_field_class
142 if default_clock_class
is not None:
143 sc
._default
_clock
_class
= default_clock_class
145 # call after `sc._default_clock_class` because, if
146 # `packets_have_beginning_default_clock_snapshot` or
147 # `packets_have_end_default_clock_snapshot` is true, then this
148 # stream class needs a default clock class already.
149 sc
._set
_supports
_packets
(
151 packets_have_beginning_default_clock_snapshot
,
152 packets_have_end_default_clock_snapshot
,
155 # call after sc._set_supports_packets() because, if
156 # `packet_context_field_class` is not `None`, then this stream
157 # class needs to support packets already.
158 if packet_context_field_class
is not None:
159 sc
._packet
_context
_field
_class
= packet_context_field_class
161 sc
._assigns
_automatic
_event
_class
_id
= assigns_automatic_event_class_id
162 sc
._assigns
_automatic
_stream
_id
= assigns_automatic_stream_id
163 sc
._set
_supports
_discarded
_events
(
164 supports_discarded_events
, discarded_events_have_default_clock_snapshots
166 sc
._set
_supports
_discarded
_packets
(
167 supports_discarded_packets
, 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(
181 _assigns_automatic_stream_class_id
= property(
182 fset
=_assigns_automatic_stream_class_id
185 # Field class creation methods.
187 def _check_field_class_create_status(self
, ptr
, type_name
):
189 raise bt2
._MemoryError('cannot create {} field class'.format(type_name
))
191 def create_bool_field_class(self
):
192 field_class_ptr
= native_bt
.field_class_bool_create(self
._ptr
)
193 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'boolean')
195 return bt2_field_class
._BoolFieldClass
._create
_from
_ptr
(field_class_ptr
)
197 def _create_integer_field_class(
198 self
, create_func
, py_cls
, type_name
, field_value_range
, preferred_display_base
200 field_class_ptr
= create_func(self
._ptr
)
201 self
._check
_field
_class
_create
_status
(field_class_ptr
, type_name
)
203 field_class
= py_cls
._create
_from
_ptr
(field_class_ptr
)
205 if field_value_range
is not None:
206 field_class
._field
_value
_range
= field_value_range
208 if preferred_display_base
is not None:
209 field_class
._preferred
_display
_base
= preferred_display_base
213 def create_signed_integer_field_class(
214 self
, field_value_range
=None, preferred_display_base
=None
216 return self
._create
_integer
_field
_class
(
217 native_bt
.field_class_integer_signed_create
,
218 bt2_field_class
._SignedIntegerFieldClass
,
221 preferred_display_base
,
224 def create_unsigned_integer_field_class(
225 self
, field_value_range
=None, preferred_display_base
=None
227 return self
._create
_integer
_field
_class
(
228 native_bt
.field_class_integer_unsigned_create
,
229 bt2_field_class
._UnsignedIntegerFieldClass
,
232 preferred_display_base
,
235 def create_signed_enumeration_field_class(
236 self
, field_value_range
=None, preferred_display_base
=None
238 return self
._create
_integer
_field
_class
(
239 native_bt
.field_class_enumeration_signed_create
,
240 bt2_field_class
._SignedEnumerationFieldClass
,
241 'signed enumeration',
243 preferred_display_base
,
246 def create_unsigned_enumeration_field_class(
247 self
, field_value_range
=None, preferred_display_base
=None
249 return self
._create
_integer
_field
_class
(
250 native_bt
.field_class_enumeration_unsigned_create
,
251 bt2_field_class
._UnsignedEnumerationFieldClass
,
252 'unsigned enumeration',
254 preferred_display_base
,
257 def create_real_field_class(self
, is_single_precision
=False):
258 field_class_ptr
= native_bt
.field_class_real_create(self
._ptr
)
259 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'real')
261 field_class
= bt2_field_class
._RealFieldClass
._create
_from
_ptr
(field_class_ptr
)
263 field_class
._is
_single
_precision
= is_single_precision
267 def create_structure_field_class(self
):
268 field_class_ptr
= native_bt
.field_class_structure_create(self
._ptr
)
269 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'structure')
271 return bt2_field_class
._StructureFieldClass
._create
_from
_ptr
(field_class_ptr
)
273 def create_string_field_class(self
):
274 field_class_ptr
= native_bt
.field_class_string_create(self
._ptr
)
275 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'string')
277 return bt2_field_class
._StringFieldClass
._create
_from
_ptr
(field_class_ptr
)
279 def create_static_array_field_class(self
, elem_fc
, length
):
280 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
281 utils
._check
_uint
64(length
)
282 ptr
= native_bt
.field_class_array_static_create(self
._ptr
, elem_fc
._ptr
, length
)
283 self
._check
_field
_class
_create
_status
(ptr
, 'static array')
285 return bt2_field_class
._StaticArrayFieldClass
._create
_from
_ptr
_and
_get
_ref
(ptr
)
287 def create_dynamic_array_field_class(self
, elem_fc
, length_fc
=None):
288 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
291 if length_fc
is not None:
292 utils
._check
_type
(length_fc
, bt2_field_class
._UnsignedIntegerFieldClass
)
293 length_fc_ptr
= length_fc
._ptr
295 ptr
= native_bt
.field_class_array_dynamic_create(
296 self
._ptr
, elem_fc
._ptr
, length_fc_ptr
298 self
._check
_field
_class
_create
_status
(ptr
, 'dynamic array')
299 return bt2_field_class
._DynamicArrayFieldClass
._create
_from
_ptr
(ptr
)
301 def create_variant_field_class(self
, selector_fc
=None):
302 selector_fc_ptr
= None
304 if selector_fc
is not None:
305 utils
._check
_type
(selector_fc
, bt2_field_class
._IntegerFieldClass
)
306 selector_fc_ptr
= selector_fc
._ptr
308 ptr
= native_bt
.field_class_variant_create(self
._ptr
, selector_fc_ptr
)
309 self
._check
_field
_class
_create
_status
(ptr
, 'variant')
310 return bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
312 # Add a listener to be called when the trace class is destroyed.
314 def add_destruction_listener(self
, listener
):
316 if not callable(listener
):
317 raise TypeError("'listener' parameter is not callable")
319 fn
= native_bt
.bt2_trace_class_add_destruction_listener
320 listener_from_native
= functools
.partial(
321 _trace_class_destruction_listener_from_native
, listener
324 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
325 utils
._handle
_func
_status
(
326 status
, 'cannot add destruction listener to trace class object'
329 return utils
._ListenerHandle
(listener_id
, self
)