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 trace_class
as bt2_trace_class
30 from bt2
import value
as bt2_value
31 import collections
.abc
36 def _trace_class_destruction_listener_from_native(user_listener
, trace_class_ptr
):
37 trace_class
= _TraceClass
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
38 user_listener(trace_class
)
41 class _TraceClassConst(object._SharedObject
, collections
.abc
.Mapping
):
42 _get_ref
= staticmethod(native_bt
.trace_class_get_ref
)
43 _put_ref
= staticmethod(native_bt
.trace_class_put_ref
)
44 _borrow_stream_class_ptr_by_index
= staticmethod(
45 native_bt
.trace_class_borrow_stream_class_by_index_const
47 _borrow_stream_class_ptr_by_id
= staticmethod(
48 native_bt
.trace_class_borrow_stream_class_by_id_const
50 _borrow_user_attributes_ptr
= staticmethod(
51 native_bt
.trace_class_borrow_user_attributes_const
53 _stream_class_pycls
= bt2_stream_class
._StreamClassConst
54 _create_value_from_ptr_and_get_ref
= staticmethod(
55 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
59 def user_attributes(self
):
60 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
61 assert ptr
is not None
62 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
64 # Number of stream classes in this trace class.
67 count
= native_bt
.trace_class_get_stream_class_count(self
._ptr
)
71 # Get a stream class by stream id.
73 def __getitem__(self
, key
):
74 utils
._check
_uint
64(key
)
76 sc_ptr
= self
._borrow
_stream
_class
_ptr
_by
_id
(self
._ptr
, key
)
80 return self
._stream
_class
_pycls
._create
_from
_ptr
_and
_get
_ref
(sc_ptr
)
83 for idx
in range(len(self
)):
84 sc_ptr
= self
._borrow
_stream
_class
_ptr
_by
_index
(self
._ptr
, idx
)
85 assert sc_ptr
is not None
87 id = native_bt
.stream_class_get_id(sc_ptr
)
93 def assigns_automatic_stream_class_id(self
):
94 return native_bt
.trace_class_assigns_automatic_stream_class_id(self
._ptr
)
96 # Add a listener to be called when the trace class is destroyed.
98 def add_destruction_listener(self
, listener
):
100 if not callable(listener
):
101 raise TypeError("'listener' parameter is not callable")
103 fn
= native_bt
.bt2_trace_class_add_destruction_listener
104 listener_from_native
= functools
.partial(
105 _trace_class_destruction_listener_from_native
, listener
108 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
109 utils
._handle
_func
_status
(
110 status
, 'cannot add destruction listener to trace class object'
113 return utils
._ListenerHandle
(listener_id
, self
)
116 class _TraceClass(_TraceClassConst
):
117 _borrow_stream_class_ptr_by_index
= staticmethod(
118 native_bt
.trace_class_borrow_stream_class_by_index
120 _borrow_stream_class_ptr_by_id
= staticmethod(
121 native_bt
.trace_class_borrow_stream_class_by_id
123 _borrow_user_attributes_ptr
= staticmethod(
124 native_bt
.trace_class_borrow_user_attributes
126 _stream_class_pycls
= bt2_stream_class
._StreamClass
127 _create_value_from_ptr_and_get_ref
= staticmethod(
128 bt2_value
._create
_from
_ptr
_and
_get
_ref
131 # Instantiate a trace of this class.
133 def __call__(self
, name
=None, user_attributes
=None, uuid
=None, environment
=None):
134 trace_ptr
= native_bt
.trace_create(self
._ptr
)
136 if trace_ptr
is None:
137 raise bt2
._MemoryError('cannot create trace class object')
139 trace
= bt2_trace
._Trace
._create
_from
_ptr
(trace_ptr
)
144 if user_attributes
is not None:
145 trace
._user
_attributes
= user_attributes
150 if environment
is not None:
151 for key
, value
in environment
.items():
152 trace
.environment
[key
] = value
156 def create_stream_class(
160 user_attributes
=None,
161 packet_context_field_class
=None,
162 event_common_context_field_class
=None,
163 default_clock_class
=None,
164 assigns_automatic_event_class_id
=True,
165 assigns_automatic_stream_id
=True,
166 supports_packets
=False,
167 packets_have_beginning_default_clock_snapshot
=False,
168 packets_have_end_default_clock_snapshot
=False,
169 supports_discarded_events
=False,
170 discarded_events_have_default_clock_snapshots
=False,
171 supports_discarded_packets
=False,
172 discarded_packets_have_default_clock_snapshots
=False,
175 if self
.assigns_automatic_stream_class_id
:
178 'id provided, but trace class assigns automatic stream class ids'
181 sc_ptr
= native_bt
.stream_class_create(self
._ptr
)
185 'id not provided, but trace class does not assign automatic stream class ids'
188 utils
._check
_uint
64(id)
189 sc_ptr
= native_bt
.stream_class_create_with_id(self
._ptr
, id)
191 sc
= bt2_stream_class
._StreamClass
._create
_from
_ptr
(sc_ptr
)
196 if user_attributes
is not None:
197 sc
._user
_attributes
= user_attributes
199 if event_common_context_field_class
is not None:
200 sc
._event
_common
_context
_field
_class
= event_common_context_field_class
202 if default_clock_class
is not None:
203 sc
._default
_clock
_class
= default_clock_class
205 # call after `sc._default_clock_class` because, if
206 # `packets_have_beginning_default_clock_snapshot` or
207 # `packets_have_end_default_clock_snapshot` is true, then this
208 # stream class needs a default clock class already.
209 sc
._set
_supports
_packets
(
211 packets_have_beginning_default_clock_snapshot
,
212 packets_have_end_default_clock_snapshot
,
215 # call after sc._set_supports_packets() because, if
216 # `packet_context_field_class` is not `None`, then this stream
217 # class needs to support packets already.
218 if packet_context_field_class
is not None:
219 sc
._packet
_context
_field
_class
= packet_context_field_class
221 sc
._assigns
_automatic
_event
_class
_id
= assigns_automatic_event_class_id
222 sc
._assigns
_automatic
_stream
_id
= assigns_automatic_stream_id
223 sc
._set
_supports
_discarded
_events
(
224 supports_discarded_events
, discarded_events_have_default_clock_snapshots
226 sc
._set
_supports
_discarded
_packets
(
227 supports_discarded_packets
, discarded_packets_have_default_clock_snapshots
231 def _user_attributes(self
, user_attributes
):
232 value
= bt2_value
.create_value(user_attributes
)
233 utils
._check
_type
(value
, bt2_value
.MapValue
)
234 native_bt
.trace_class_set_user_attributes(self
._ptr
, value
._ptr
)
236 _user_attributes
= property(fset
=_user_attributes
)
238 def _assigns_automatic_stream_class_id(self
, auto_id
):
239 utils
._check
_bool
(auto_id
)
240 return native_bt
.trace_class_set_assigns_automatic_stream_class_id(
244 _assigns_automatic_stream_class_id
= property(
245 fset
=_assigns_automatic_stream_class_id
248 # Field class creation methods.
250 def _check_field_class_create_status(self
, ptr
, type_name
):
252 raise bt2
._MemoryError('cannot create {} field class'.format(type_name
))
255 def _set_field_class_user_attrs(fc
, user_attributes
):
256 if user_attributes
is not None:
257 fc
._user
_attributes
= user_attributes
259 def create_bool_field_class(self
, user_attributes
=None):
260 field_class_ptr
= native_bt
.field_class_bool_create(self
._ptr
)
261 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'boolean')
262 fc
= bt2_field_class
._BoolFieldClass
._create
_from
_ptr
(field_class_ptr
)
263 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
266 def create_bit_array_field_class(self
, length
, user_attributes
=None):
267 utils
._check
_uint
64(length
)
269 if length
< 1 or length
> 64:
271 'invalid length {}: expecting a value in the [1, 64] range'.format(
276 field_class_ptr
= native_bt
.field_class_bit_array_create(self
._ptr
, length
)
277 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'bit array')
278 fc
= bt2_field_class
._BitArrayFieldClass
._create
_from
_ptr
(field_class_ptr
)
279 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
282 def _create_integer_field_class(
288 preferred_display_base
,
291 field_class_ptr
= create_func(self
._ptr
)
292 self
._check
_field
_class
_create
_status
(field_class_ptr
, type_name
)
294 field_class
= py_cls
._create
_from
_ptr
(field_class_ptr
)
296 if field_value_range
is not None:
297 field_class
._field
_value
_range
= field_value_range
299 if preferred_display_base
is not None:
300 field_class
._preferred
_display
_base
= preferred_display_base
302 self
._set
_field
_class
_user
_attrs
(field_class
, user_attributes
)
305 def create_signed_integer_field_class(
306 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
308 return self
._create
_integer
_field
_class
(
309 native_bt
.field_class_integer_signed_create
,
310 bt2_field_class
._SignedIntegerFieldClass
,
313 preferred_display_base
,
317 def create_unsigned_integer_field_class(
318 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
320 return self
._create
_integer
_field
_class
(
321 native_bt
.field_class_integer_unsigned_create
,
322 bt2_field_class
._UnsignedIntegerFieldClass
,
325 preferred_display_base
,
329 def create_signed_enumeration_field_class(
330 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
332 return self
._create
_integer
_field
_class
(
333 native_bt
.field_class_enumeration_signed_create
,
334 bt2_field_class
._SignedEnumerationFieldClass
,
335 'signed enumeration',
337 preferred_display_base
,
341 def create_unsigned_enumeration_field_class(
342 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
344 return self
._create
_integer
_field
_class
(
345 native_bt
.field_class_enumeration_unsigned_create
,
346 bt2_field_class
._UnsignedEnumerationFieldClass
,
347 'unsigned enumeration',
349 preferred_display_base
,
353 def create_real_field_class(self
, is_single_precision
=False, user_attributes
=None):
354 field_class_ptr
= native_bt
.field_class_real_create(self
._ptr
)
355 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'real')
357 field_class
= bt2_field_class
._RealFieldClass
._create
_from
_ptr
(field_class_ptr
)
359 field_class
._is
_single
_precision
= is_single_precision
360 self
._set
_field
_class
_user
_attrs
(field_class
, user_attributes
)
364 def create_structure_field_class(self
, user_attributes
=None):
365 field_class_ptr
= native_bt
.field_class_structure_create(self
._ptr
)
366 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'structure')
367 fc
= bt2_field_class
._StructureFieldClass
._create
_from
_ptr
(field_class_ptr
)
368 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
371 def create_string_field_class(self
, user_attributes
=None):
372 field_class_ptr
= native_bt
.field_class_string_create(self
._ptr
)
373 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'string')
374 fc
= bt2_field_class
._StringFieldClass
._create
_from
_ptr
(field_class_ptr
)
375 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
378 def create_static_array_field_class(self
, elem_fc
, length
, user_attributes
=None):
379 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
380 utils
._check
_uint
64(length
)
381 ptr
= native_bt
.field_class_array_static_create(self
._ptr
, elem_fc
._ptr
, length
)
382 self
._check
_field
_class
_create
_status
(ptr
, 'static array')
383 fc
= bt2_field_class
._StaticArrayFieldClass
._create
_from
_ptr
_and
_get
_ref
(ptr
)
384 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
387 def create_dynamic_array_field_class(
388 self
, elem_fc
, length_fc
=None, user_attributes
=None
390 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
393 if length_fc
is not None:
394 utils
._check
_type
(length_fc
, bt2_field_class
._UnsignedIntegerFieldClass
)
395 length_fc_ptr
= length_fc
._ptr
397 ptr
= native_bt
.field_class_array_dynamic_create(
398 self
._ptr
, elem_fc
._ptr
, length_fc_ptr
400 self
._check
_field
_class
_create
_status
(ptr
, 'dynamic array')
401 fc
= bt2_field_class
._DynamicArrayFieldClass
._create
_from
_ptr
(ptr
)
402 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
405 def create_option_field_class(
406 self
, content_fc
, selector_fc
=None, user_attributes
=None
408 utils
._check
_type
(content_fc
, bt2_field_class
._FieldClass
)
410 selector_fc_ptr
= None
412 if selector_fc
is not None:
413 utils
._check
_type
(selector_fc
, bt2_field_class
._BoolFieldClass
)
414 selector_fc_ptr
= selector_fc
._ptr
416 ptr
= native_bt
.field_class_option_create(
417 self
._ptr
, content_fc
._ptr
, selector_fc_ptr
419 self
._check
_field
_class
_create
_status
(ptr
, 'option')
420 fc
= bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
421 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
424 def create_variant_field_class(self
, selector_fc
=None, user_attributes
=None):
425 selector_fc_ptr
= None
427 if selector_fc
is not None:
428 utils
._check
_type
(selector_fc
, bt2_field_class
._IntegerFieldClass
)
429 selector_fc_ptr
= selector_fc
._ptr
431 ptr
= native_bt
.field_class_variant_create(self
._ptr
, selector_fc_ptr
)
432 self
._check
_field
_class
_create
_status
(ptr
, 'variant')
433 fc
= bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
434 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)