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 from bt2
import native_bt
, utils
, object
26 from bt2
import stream_class
as bt2_stream_class
27 from bt2
import field_class
as bt2_field_class
28 from bt2
import trace
as bt2_trace
29 from bt2
import value
as bt2_value
30 import collections
.abc
35 def _trace_class_destruction_listener_from_native(user_listener
, trace_class_ptr
):
36 trace_class
= _TraceClass
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
37 user_listener(trace_class
)
40 class _TraceClassConst(object._SharedObject
, collections
.abc
.Mapping
):
41 _get_ref
= staticmethod(native_bt
.trace_class_get_ref
)
42 _put_ref
= staticmethod(native_bt
.trace_class_put_ref
)
43 _borrow_stream_class_ptr_by_index
= staticmethod(
44 native_bt
.trace_class_borrow_stream_class_by_index_const
46 _borrow_stream_class_ptr_by_id
= staticmethod(
47 native_bt
.trace_class_borrow_stream_class_by_id_const
49 _borrow_user_attributes_ptr
= staticmethod(
50 native_bt
.trace_class_borrow_user_attributes_const
52 _stream_class_pycls
= bt2_stream_class
._StreamClassConst
53 _create_value_from_ptr_and_get_ref
= staticmethod(
54 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
58 def user_attributes(self
):
59 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
60 assert ptr
is not None
61 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
63 # Number of stream classes in this trace class.
66 count
= native_bt
.trace_class_get_stream_class_count(self
._ptr
)
70 # Get a stream class by stream id.
72 def __getitem__(self
, key
):
73 utils
._check
_uint
64(key
)
75 sc_ptr
= self
._borrow
_stream
_class
_ptr
_by
_id
(self
._ptr
, key
)
79 return self
._stream
_class
_pycls
._create
_from
_ptr
_and
_get
_ref
(sc_ptr
)
82 for idx
in range(len(self
)):
83 sc_ptr
= self
._borrow
_stream
_class
_ptr
_by
_index
(self
._ptr
, idx
)
84 assert sc_ptr
is not None
86 id = native_bt
.stream_class_get_id(sc_ptr
)
92 def assigns_automatic_stream_class_id(self
):
93 return native_bt
.trace_class_assigns_automatic_stream_class_id(self
._ptr
)
95 # Add a listener to be called when the trace class is destroyed.
97 def add_destruction_listener(self
, listener
):
99 if not callable(listener
):
100 raise TypeError("'listener' parameter is not callable")
102 fn
= native_bt
.bt2_trace_class_add_destruction_listener
103 listener_from_native
= functools
.partial(
104 _trace_class_destruction_listener_from_native
, listener
107 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
108 utils
._handle
_func
_status
(
109 status
, 'cannot add destruction listener to trace class object'
112 return utils
._ListenerHandle
(listener_id
, self
)
114 def remove_destruction_listener(self
, listener_handle
):
115 utils
._check
_type
(listener_handle
, utils
._ListenerHandle
)
117 if listener_handle
._obj
.addr
!= self
.addr
:
119 'This trace class destruction listener does not match the trace object.'
122 if listener_handle
._listener
_id
is None:
124 'This trace class destruction listener was already removed.'
127 status
= native_bt
.trace_class_remove_destruction_listener(
128 self
._ptr
, listener_handle
._listener
_id
130 utils
._handle
_func
_status
(status
)
131 listener_handle
._listener
_id
= None
134 class _TraceClass(_TraceClassConst
):
135 _borrow_stream_class_ptr_by_index
= staticmethod(
136 native_bt
.trace_class_borrow_stream_class_by_index
138 _borrow_stream_class_ptr_by_id
= staticmethod(
139 native_bt
.trace_class_borrow_stream_class_by_id
141 _borrow_user_attributes_ptr
= staticmethod(
142 native_bt
.trace_class_borrow_user_attributes
144 _stream_class_pycls
= bt2_stream_class
._StreamClass
145 _create_value_from_ptr_and_get_ref
= staticmethod(
146 bt2_value
._create
_from
_ptr
_and
_get
_ref
149 # Instantiate a trace of this class.
151 def __call__(self
, name
=None, user_attributes
=None, uuid
=None, environment
=None):
152 trace_ptr
= native_bt
.trace_create(self
._ptr
)
154 if trace_ptr
is None:
155 raise bt2
._MemoryError('cannot create trace class object')
157 trace
= bt2_trace
._Trace
._create
_from
_ptr
(trace_ptr
)
162 if user_attributes
is not None:
163 trace
._user
_attributes
= user_attributes
168 if environment
is not None:
169 for key
, value
in environment
.items():
170 trace
.environment
[key
] = value
174 def create_stream_class(
178 user_attributes
=None,
179 packet_context_field_class
=None,
180 event_common_context_field_class
=None,
181 default_clock_class
=None,
182 assigns_automatic_event_class_id
=True,
183 assigns_automatic_stream_id
=True,
184 supports_packets
=False,
185 packets_have_beginning_default_clock_snapshot
=False,
186 packets_have_end_default_clock_snapshot
=False,
187 supports_discarded_events
=False,
188 discarded_events_have_default_clock_snapshots
=False,
189 supports_discarded_packets
=False,
190 discarded_packets_have_default_clock_snapshots
=False,
193 if self
.assigns_automatic_stream_class_id
:
196 'id provided, but trace class assigns automatic stream class ids'
199 sc_ptr
= native_bt
.stream_class_create(self
._ptr
)
203 'id not provided, but trace class does not assign automatic stream class ids'
206 utils
._check
_uint
64(id)
207 sc_ptr
= native_bt
.stream_class_create_with_id(self
._ptr
, id)
209 sc
= bt2_stream_class
._StreamClass
._create
_from
_ptr
(sc_ptr
)
214 if user_attributes
is not None:
215 sc
._user
_attributes
= user_attributes
217 if event_common_context_field_class
is not None:
218 sc
._event
_common
_context
_field
_class
= event_common_context_field_class
220 if default_clock_class
is not None:
221 sc
._default
_clock
_class
= default_clock_class
223 # call after `sc._default_clock_class` because, if
224 # `packets_have_beginning_default_clock_snapshot` or
225 # `packets_have_end_default_clock_snapshot` is true, then this
226 # stream class needs a default clock class already.
227 sc
._set
_supports
_packets
(
229 packets_have_beginning_default_clock_snapshot
,
230 packets_have_end_default_clock_snapshot
,
233 # call after sc._set_supports_packets() because, if
234 # `packet_context_field_class` is not `None`, then this stream
235 # class needs to support packets already.
236 if packet_context_field_class
is not None:
237 sc
._packet
_context
_field
_class
= packet_context_field_class
239 sc
._assigns
_automatic
_event
_class
_id
= assigns_automatic_event_class_id
240 sc
._assigns
_automatic
_stream
_id
= assigns_automatic_stream_id
241 sc
._set
_supports
_discarded
_events
(
242 supports_discarded_events
, discarded_events_have_default_clock_snapshots
244 sc
._set
_supports
_discarded
_packets
(
245 supports_discarded_packets
, discarded_packets_have_default_clock_snapshots
249 def _user_attributes(self
, user_attributes
):
250 value
= bt2_value
.create_value(user_attributes
)
251 utils
._check
_type
(value
, bt2_value
.MapValue
)
252 native_bt
.trace_class_set_user_attributes(self
._ptr
, value
._ptr
)
254 _user_attributes
= property(fset
=_user_attributes
)
256 def _assigns_automatic_stream_class_id(self
, auto_id
):
257 utils
._check
_bool
(auto_id
)
258 return native_bt
.trace_class_set_assigns_automatic_stream_class_id(
262 _assigns_automatic_stream_class_id
= property(
263 fset
=_assigns_automatic_stream_class_id
266 # Field class creation methods.
268 def _check_field_class_create_status(self
, ptr
, type_name
):
270 raise bt2
._MemoryError('cannot create {} field class'.format(type_name
))
273 def _set_field_class_user_attrs(fc
, user_attributes
):
274 if user_attributes
is not None:
275 fc
._user
_attributes
= user_attributes
277 def create_bool_field_class(self
, user_attributes
=None):
278 field_class_ptr
= native_bt
.field_class_bool_create(self
._ptr
)
279 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'boolean')
280 fc
= bt2_field_class
._BoolFieldClass
._create
_from
_ptr
(field_class_ptr
)
281 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
284 def create_bit_array_field_class(self
, length
, user_attributes
=None):
285 utils
._check
_uint
64(length
)
287 if length
< 1 or length
> 64:
289 'invalid length {}: expecting a value in the [1, 64] range'.format(
294 field_class_ptr
= native_bt
.field_class_bit_array_create(self
._ptr
, length
)
295 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'bit array')
296 fc
= bt2_field_class
._BitArrayFieldClass
._create
_from
_ptr
(field_class_ptr
)
297 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
300 def _create_integer_field_class(
306 preferred_display_base
,
309 field_class_ptr
= create_func(self
._ptr
)
310 self
._check
_field
_class
_create
_status
(field_class_ptr
, type_name
)
312 field_class
= py_cls
._create
_from
_ptr
(field_class_ptr
)
314 if field_value_range
is not None:
315 field_class
._field
_value
_range
= field_value_range
317 if preferred_display_base
is not None:
318 field_class
._preferred
_display
_base
= preferred_display_base
320 self
._set
_field
_class
_user
_attrs
(field_class
, user_attributes
)
323 def create_signed_integer_field_class(
324 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
326 return self
._create
_integer
_field
_class
(
327 native_bt
.field_class_integer_signed_create
,
328 bt2_field_class
._SignedIntegerFieldClass
,
331 preferred_display_base
,
335 def create_unsigned_integer_field_class(
336 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
338 return self
._create
_integer
_field
_class
(
339 native_bt
.field_class_integer_unsigned_create
,
340 bt2_field_class
._UnsignedIntegerFieldClass
,
343 preferred_display_base
,
347 def create_signed_enumeration_field_class(
348 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
350 return self
._create
_integer
_field
_class
(
351 native_bt
.field_class_enumeration_signed_create
,
352 bt2_field_class
._SignedEnumerationFieldClass
,
353 'signed enumeration',
355 preferred_display_base
,
359 def create_unsigned_enumeration_field_class(
360 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
362 return self
._create
_integer
_field
_class
(
363 native_bt
.field_class_enumeration_unsigned_create
,
364 bt2_field_class
._UnsignedEnumerationFieldClass
,
365 'unsigned enumeration',
367 preferred_display_base
,
371 def create_single_precision_real_field_class(self
, user_attributes
=None):
372 field_class_ptr
= native_bt
.field_class_real_single_precision_create(self
._ptr
)
373 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'single-precision real')
375 field_class
= bt2_field_class
._SinglePrecisionRealFieldClass
._create
_from
_ptr
(
379 self
._set
_field
_class
_user
_attrs
(field_class
, user_attributes
)
383 def create_double_precision_real_field_class(self
, user_attributes
=None):
384 field_class_ptr
= native_bt
.field_class_real_double_precision_create(self
._ptr
)
385 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'double-precision real')
387 field_class
= bt2_field_class
._DoublePrecisionRealFieldClass
._create
_from
_ptr
(
391 self
._set
_field
_class
_user
_attrs
(field_class
, user_attributes
)
395 def create_structure_field_class(self
, user_attributes
=None):
396 field_class_ptr
= native_bt
.field_class_structure_create(self
._ptr
)
397 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'structure')
398 fc
= bt2_field_class
._StructureFieldClass
._create
_from
_ptr
(field_class_ptr
)
399 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
402 def create_string_field_class(self
, user_attributes
=None):
403 field_class_ptr
= native_bt
.field_class_string_create(self
._ptr
)
404 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'string')
405 fc
= bt2_field_class
._StringFieldClass
._create
_from
_ptr
(field_class_ptr
)
406 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
409 def create_static_array_field_class(self
, elem_fc
, length
, user_attributes
=None):
410 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
411 utils
._check
_uint
64(length
)
412 ptr
= native_bt
.field_class_array_static_create(self
._ptr
, elem_fc
._ptr
, length
)
413 self
._check
_field
_class
_create
_status
(ptr
, 'static array')
414 fc
= bt2_field_class
._StaticArrayFieldClass
._create
_from
_ptr
_and
_get
_ref
(ptr
)
415 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
418 def create_dynamic_array_field_class(
419 self
, elem_fc
, length_fc
=None, user_attributes
=None
421 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
424 if length_fc
is not None:
425 utils
._check
_type
(length_fc
, bt2_field_class
._UnsignedIntegerFieldClass
)
426 length_fc_ptr
= length_fc
._ptr
428 ptr
= native_bt
.field_class_array_dynamic_create(
429 self
._ptr
, elem_fc
._ptr
, length_fc_ptr
431 self
._check
_field
_class
_create
_status
(ptr
, 'dynamic array')
432 fc
= bt2_field_class
._DynamicArrayFieldClass
._create
_from
_ptr
(ptr
)
433 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
436 def create_option_field_class(
437 self
, content_fc
, selector_fc
=None, user_attributes
=None
439 utils
._check
_type
(content_fc
, bt2_field_class
._FieldClass
)
441 selector_fc_ptr
= None
443 if selector_fc
is not None:
444 utils
._check
_type
(selector_fc
, bt2_field_class
._BoolFieldClass
)
445 selector_fc_ptr
= selector_fc
._ptr
447 ptr
= native_bt
.field_class_option_create(
448 self
._ptr
, content_fc
._ptr
, selector_fc_ptr
450 self
._check
_field
_class
_create
_status
(ptr
, 'option')
451 fc
= bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
452 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
455 def create_variant_field_class(self
, selector_fc
=None, user_attributes
=None):
456 selector_fc_ptr
= None
458 if selector_fc
is not None:
459 utils
._check
_type
(selector_fc
, bt2_field_class
._IntegerFieldClass
)
460 selector_fc_ptr
= selector_fc
._ptr
462 ptr
= native_bt
.field_class_variant_create(self
._ptr
, selector_fc_ptr
)
463 self
._check
_field
_class
_create
_status
(ptr
, 'variant')
464 fc
= bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
465 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)