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 _stream_class_pycls
= bt2_stream_class
._StreamClassConst
51 _create_value_from_ptr_and_get_ref
= staticmethod(
52 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
56 def user_attributes(self
):
57 ptr
= native_bt
.trace_class_borrow_user_attributes(self
._ptr
)
58 assert ptr
is not None
59 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
61 # Number of stream classes in this trace class.
64 count
= native_bt
.trace_class_get_stream_class_count(self
._ptr
)
68 # Get a stream class by stream id.
70 def __getitem__(self
, key
):
71 utils
._check
_uint
64(key
)
73 sc_ptr
= self
._borrow
_stream
_class
_ptr
_by
_id
(self
._ptr
, key
)
77 return self
._stream
_class
_pycls
._create
_from
_ptr
_and
_get
_ref
(sc_ptr
)
80 for idx
in range(len(self
)):
81 sc_ptr
= self
._borrow
_stream
_class
_ptr
_by
_index
(self
._ptr
, idx
)
82 assert sc_ptr
is not None
84 id = native_bt
.stream_class_get_id(sc_ptr
)
90 def assigns_automatic_stream_class_id(self
):
91 return native_bt
.trace_class_assigns_automatic_stream_class_id(self
._ptr
)
93 # Add a listener to be called when the trace class is destroyed.
95 def add_destruction_listener(self
, listener
):
97 if not callable(listener
):
98 raise TypeError("'listener' parameter is not callable")
100 fn
= native_bt
.bt2_trace_class_add_destruction_listener
101 listener_from_native
= functools
.partial(
102 _trace_class_destruction_listener_from_native
, listener
105 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
106 utils
._handle
_func
_status
(
107 status
, 'cannot add destruction listener to trace class object'
110 return utils
._ListenerHandle
(listener_id
, self
)
113 class _TraceClass(_TraceClassConst
):
114 _borrow_stream_class_ptr_by_index
= staticmethod(
115 native_bt
.trace_class_borrow_stream_class_by_index
117 _borrow_stream_class_ptr_by_id
= staticmethod(
118 native_bt
.trace_class_borrow_stream_class_by_id
120 _stream_class_pycls
= bt2_stream_class
._StreamClass
121 _create_value_from_ptr_and_get_ref
= staticmethod(
122 bt2_value
._create
_from
_ptr
_and
_get
_ref
125 # Instantiate a trace of this class.
127 def __call__(self
, name
=None, user_attributes
=None, uuid
=None, environment
=None):
128 trace_ptr
= native_bt
.trace_create(self
._ptr
)
130 if trace_ptr
is None:
131 raise bt2
._MemoryError('cannot create trace class object')
133 trace
= bt2_trace
._Trace
._create
_from
_ptr
(trace_ptr
)
138 if user_attributes
is not None:
139 trace
._user
_attributes
= user_attributes
144 if environment
is not None:
145 for key
, value
in environment
.items():
146 trace
.environment
[key
] = value
150 def create_stream_class(
154 user_attributes
=None,
155 packet_context_field_class
=None,
156 event_common_context_field_class
=None,
157 default_clock_class
=None,
158 assigns_automatic_event_class_id
=True,
159 assigns_automatic_stream_id
=True,
160 supports_packets
=False,
161 packets_have_beginning_default_clock_snapshot
=False,
162 packets_have_end_default_clock_snapshot
=False,
163 supports_discarded_events
=False,
164 discarded_events_have_default_clock_snapshots
=False,
165 supports_discarded_packets
=False,
166 discarded_packets_have_default_clock_snapshots
=False,
169 if self
.assigns_automatic_stream_class_id
:
172 'id provided, but trace class assigns automatic stream class ids'
175 sc_ptr
= native_bt
.stream_class_create(self
._ptr
)
179 'id not provided, but trace class does not assign automatic stream class ids'
182 utils
._check
_uint
64(id)
183 sc_ptr
= native_bt
.stream_class_create_with_id(self
._ptr
, id)
185 sc
= bt2_stream_class
._StreamClass
._create
_from
_ptr
(sc_ptr
)
190 if user_attributes
is not None:
191 sc
._user
_attributes
= user_attributes
193 if event_common_context_field_class
is not None:
194 sc
._event
_common
_context
_field
_class
= event_common_context_field_class
196 if default_clock_class
is not None:
197 sc
._default
_clock
_class
= default_clock_class
199 # call after `sc._default_clock_class` because, if
200 # `packets_have_beginning_default_clock_snapshot` or
201 # `packets_have_end_default_clock_snapshot` is true, then this
202 # stream class needs a default clock class already.
203 sc
._set
_supports
_packets
(
205 packets_have_beginning_default_clock_snapshot
,
206 packets_have_end_default_clock_snapshot
,
209 # call after sc._set_supports_packets() because, if
210 # `packet_context_field_class` is not `None`, then this stream
211 # class needs to support packets already.
212 if packet_context_field_class
is not None:
213 sc
._packet
_context
_field
_class
= packet_context_field_class
215 sc
._assigns
_automatic
_event
_class
_id
= assigns_automatic_event_class_id
216 sc
._assigns
_automatic
_stream
_id
= assigns_automatic_stream_id
217 sc
._set
_supports
_discarded
_events
(
218 supports_discarded_events
, discarded_events_have_default_clock_snapshots
220 sc
._set
_supports
_discarded
_packets
(
221 supports_discarded_packets
, discarded_packets_have_default_clock_snapshots
225 def _user_attributes(self
, user_attributes
):
226 value
= bt2_value
.create_value(user_attributes
)
227 utils
._check
_type
(value
, bt2_value
.MapValue
)
228 native_bt
.trace_class_set_user_attributes(self
._ptr
, value
._ptr
)
230 _user_attributes
= property(fset
=_user_attributes
)
232 def _assigns_automatic_stream_class_id(self
, auto_id
):
233 utils
._check
_bool
(auto_id
)
234 return native_bt
.trace_class_set_assigns_automatic_stream_class_id(
238 _assigns_automatic_stream_class_id
= property(
239 fset
=_assigns_automatic_stream_class_id
242 # Field class creation methods.
244 def _check_field_class_create_status(self
, ptr
, type_name
):
246 raise bt2
._MemoryError('cannot create {} field class'.format(type_name
))
249 def _set_field_class_user_attrs(fc
, user_attributes
):
250 if user_attributes
is not None:
251 fc
._user
_attributes
= user_attributes
253 def create_bool_field_class(self
, user_attributes
=None):
254 field_class_ptr
= native_bt
.field_class_bool_create(self
._ptr
)
255 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'boolean')
256 fc
= bt2_field_class
._BoolFieldClass
._create
_from
_ptr
(field_class_ptr
)
257 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
260 def create_bit_array_field_class(self
, length
, user_attributes
=None):
261 utils
._check
_uint
64(length
)
263 if length
< 1 or length
> 64:
265 'invalid length {}: expecting a value in the [1, 64] range'.format(
270 field_class_ptr
= native_bt
.field_class_bit_array_create(self
._ptr
, length
)
271 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'bit array')
272 fc
= bt2_field_class
._BitArrayFieldClass
._create
_from
_ptr
(field_class_ptr
)
273 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
276 def _create_integer_field_class(
282 preferred_display_base
,
285 field_class_ptr
= create_func(self
._ptr
)
286 self
._check
_field
_class
_create
_status
(field_class_ptr
, type_name
)
288 field_class
= py_cls
._create
_from
_ptr
(field_class_ptr
)
290 if field_value_range
is not None:
291 field_class
._field
_value
_range
= field_value_range
293 if preferred_display_base
is not None:
294 field_class
._preferred
_display
_base
= preferred_display_base
296 self
._set
_field
_class
_user
_attrs
(field_class
, user_attributes
)
299 def create_signed_integer_field_class(
300 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
302 return self
._create
_integer
_field
_class
(
303 native_bt
.field_class_integer_signed_create
,
304 bt2_field_class
._SignedIntegerFieldClass
,
307 preferred_display_base
,
311 def create_unsigned_integer_field_class(
312 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
314 return self
._create
_integer
_field
_class
(
315 native_bt
.field_class_integer_unsigned_create
,
316 bt2_field_class
._UnsignedIntegerFieldClass
,
319 preferred_display_base
,
323 def create_signed_enumeration_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_enumeration_signed_create
,
328 bt2_field_class
._SignedEnumerationFieldClass
,
329 'signed enumeration',
331 preferred_display_base
,
335 def create_unsigned_enumeration_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_enumeration_unsigned_create
,
340 bt2_field_class
._UnsignedEnumerationFieldClass
,
341 'unsigned enumeration',
343 preferred_display_base
,
347 def create_real_field_class(self
, is_single_precision
=False, user_attributes
=None):
348 field_class_ptr
= native_bt
.field_class_real_create(self
._ptr
)
349 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'real')
351 field_class
= bt2_field_class
._RealFieldClass
._create
_from
_ptr
(field_class_ptr
)
353 field_class
._is
_single
_precision
= is_single_precision
354 self
._set
_field
_class
_user
_attrs
(field_class
, user_attributes
)
358 def create_structure_field_class(self
, user_attributes
=None):
359 field_class_ptr
= native_bt
.field_class_structure_create(self
._ptr
)
360 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'structure')
361 fc
= bt2_field_class
._StructureFieldClass
._create
_from
_ptr
(field_class_ptr
)
362 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
365 def create_string_field_class(self
, user_attributes
=None):
366 field_class_ptr
= native_bt
.field_class_string_create(self
._ptr
)
367 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'string')
368 fc
= bt2_field_class
._StringFieldClass
._create
_from
_ptr
(field_class_ptr
)
369 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
372 def create_static_array_field_class(self
, elem_fc
, length
, user_attributes
=None):
373 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
374 utils
._check
_uint
64(length
)
375 ptr
= native_bt
.field_class_array_static_create(self
._ptr
, elem_fc
._ptr
, length
)
376 self
._check
_field
_class
_create
_status
(ptr
, 'static array')
377 fc
= bt2_field_class
._StaticArrayFieldClass
._create
_from
_ptr
_and
_get
_ref
(ptr
)
378 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
381 def create_dynamic_array_field_class(
382 self
, elem_fc
, length_fc
=None, user_attributes
=None
384 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
387 if length_fc
is not None:
388 utils
._check
_type
(length_fc
, bt2_field_class
._UnsignedIntegerFieldClass
)
389 length_fc_ptr
= length_fc
._ptr
391 ptr
= native_bt
.field_class_array_dynamic_create(
392 self
._ptr
, elem_fc
._ptr
, length_fc_ptr
394 self
._check
_field
_class
_create
_status
(ptr
, 'dynamic array')
395 fc
= bt2_field_class
._DynamicArrayFieldClass
._create
_from
_ptr
(ptr
)
396 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
399 def create_option_field_class(
400 self
, content_fc
, selector_fc
=None, user_attributes
=None
402 utils
._check
_type
(content_fc
, bt2_field_class
._FieldClass
)
404 selector_fc_ptr
= None
406 if selector_fc
is not None:
407 utils
._check
_type
(selector_fc
, bt2_field_class
._BoolFieldClass
)
408 selector_fc_ptr
= selector_fc
._ptr
410 ptr
= native_bt
.field_class_option_create(
411 self
._ptr
, content_fc
._ptr
, selector_fc_ptr
413 self
._check
_field
_class
_create
_status
(ptr
, 'option')
414 fc
= bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
415 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
418 def create_variant_field_class(self
, selector_fc
=None, user_attributes
=None):
419 selector_fc_ptr
= None
421 if selector_fc
is not None:
422 utils
._check
_type
(selector_fc
, bt2_field_class
._IntegerFieldClass
)
423 selector_fc_ptr
= selector_fc
._ptr
425 ptr
= native_bt
.field_class_variant_create(self
._ptr
, selector_fc_ptr
)
426 self
._check
_field
_class
_create
_status
(ptr
, 'variant')
427 fc
= bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
428 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)