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
)
115 class _TraceClass(_TraceClassConst
):
116 _borrow_stream_class_ptr_by_index
= staticmethod(
117 native_bt
.trace_class_borrow_stream_class_by_index
119 _borrow_stream_class_ptr_by_id
= staticmethod(
120 native_bt
.trace_class_borrow_stream_class_by_id
122 _borrow_user_attributes_ptr
= staticmethod(
123 native_bt
.trace_class_borrow_user_attributes
125 _stream_class_pycls
= bt2_stream_class
._StreamClass
126 _create_value_from_ptr_and_get_ref
= staticmethod(
127 bt2_value
._create
_from
_ptr
_and
_get
_ref
130 # Instantiate a trace of this class.
132 def __call__(self
, name
=None, user_attributes
=None, uuid
=None, environment
=None):
133 trace_ptr
= native_bt
.trace_create(self
._ptr
)
135 if trace_ptr
is None:
136 raise bt2
._MemoryError('cannot create trace class object')
138 trace
= bt2_trace
._Trace
._create
_from
_ptr
(trace_ptr
)
143 if user_attributes
is not None:
144 trace
._user
_attributes
= user_attributes
149 if environment
is not None:
150 for key
, value
in environment
.items():
151 trace
.environment
[key
] = value
155 def create_stream_class(
159 user_attributes
=None,
160 packet_context_field_class
=None,
161 event_common_context_field_class
=None,
162 default_clock_class
=None,
163 assigns_automatic_event_class_id
=True,
164 assigns_automatic_stream_id
=True,
165 supports_packets
=False,
166 packets_have_beginning_default_clock_snapshot
=False,
167 packets_have_end_default_clock_snapshot
=False,
168 supports_discarded_events
=False,
169 discarded_events_have_default_clock_snapshots
=False,
170 supports_discarded_packets
=False,
171 discarded_packets_have_default_clock_snapshots
=False,
174 if self
.assigns_automatic_stream_class_id
:
177 'id provided, but trace class assigns automatic stream class ids'
180 sc_ptr
= native_bt
.stream_class_create(self
._ptr
)
184 'id not provided, but trace class does not assign automatic stream class ids'
187 utils
._check
_uint
64(id)
188 sc_ptr
= native_bt
.stream_class_create_with_id(self
._ptr
, id)
190 sc
= bt2_stream_class
._StreamClass
._create
_from
_ptr
(sc_ptr
)
195 if user_attributes
is not None:
196 sc
._user
_attributes
= user_attributes
198 if event_common_context_field_class
is not None:
199 sc
._event
_common
_context
_field
_class
= event_common_context_field_class
201 if default_clock_class
is not None:
202 sc
._default
_clock
_class
= default_clock_class
204 # call after `sc._default_clock_class` because, if
205 # `packets_have_beginning_default_clock_snapshot` or
206 # `packets_have_end_default_clock_snapshot` is true, then this
207 # stream class needs a default clock class already.
208 sc
._set
_supports
_packets
(
210 packets_have_beginning_default_clock_snapshot
,
211 packets_have_end_default_clock_snapshot
,
214 # call after sc._set_supports_packets() because, if
215 # `packet_context_field_class` is not `None`, then this stream
216 # class needs to support packets already.
217 if packet_context_field_class
is not None:
218 sc
._packet
_context
_field
_class
= packet_context_field_class
220 sc
._assigns
_automatic
_event
_class
_id
= assigns_automatic_event_class_id
221 sc
._assigns
_automatic
_stream
_id
= assigns_automatic_stream_id
222 sc
._set
_supports
_discarded
_events
(
223 supports_discarded_events
, discarded_events_have_default_clock_snapshots
225 sc
._set
_supports
_discarded
_packets
(
226 supports_discarded_packets
, discarded_packets_have_default_clock_snapshots
230 def _user_attributes(self
, user_attributes
):
231 value
= bt2_value
.create_value(user_attributes
)
232 utils
._check
_type
(value
, bt2_value
.MapValue
)
233 native_bt
.trace_class_set_user_attributes(self
._ptr
, value
._ptr
)
235 _user_attributes
= property(fset
=_user_attributes
)
237 def _assigns_automatic_stream_class_id(self
, auto_id
):
238 utils
._check
_bool
(auto_id
)
239 return native_bt
.trace_class_set_assigns_automatic_stream_class_id(
243 _assigns_automatic_stream_class_id
= property(
244 fset
=_assigns_automatic_stream_class_id
247 # Field class creation methods.
249 def _check_field_class_create_status(self
, ptr
, type_name
):
251 raise bt2
._MemoryError('cannot create {} field class'.format(type_name
))
254 def _set_field_class_user_attrs(fc
, user_attributes
):
255 if user_attributes
is not None:
256 fc
._user
_attributes
= user_attributes
258 def create_bool_field_class(self
, user_attributes
=None):
259 field_class_ptr
= native_bt
.field_class_bool_create(self
._ptr
)
260 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'boolean')
261 fc
= bt2_field_class
._BoolFieldClass
._create
_from
_ptr
(field_class_ptr
)
262 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
265 def create_bit_array_field_class(self
, length
, user_attributes
=None):
266 utils
._check
_uint
64(length
)
268 if length
< 1 or length
> 64:
270 'invalid length {}: expecting a value in the [1, 64] range'.format(
275 field_class_ptr
= native_bt
.field_class_bit_array_create(self
._ptr
, length
)
276 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'bit array')
277 fc
= bt2_field_class
._BitArrayFieldClass
._create
_from
_ptr
(field_class_ptr
)
278 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
281 def _create_integer_field_class(
287 preferred_display_base
,
290 field_class_ptr
= create_func(self
._ptr
)
291 self
._check
_field
_class
_create
_status
(field_class_ptr
, type_name
)
293 field_class
= py_cls
._create
_from
_ptr
(field_class_ptr
)
295 if field_value_range
is not None:
296 field_class
._field
_value
_range
= field_value_range
298 if preferred_display_base
is not None:
299 field_class
._preferred
_display
_base
= preferred_display_base
301 self
._set
_field
_class
_user
_attrs
(field_class
, user_attributes
)
304 def create_signed_integer_field_class(
305 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
307 return self
._create
_integer
_field
_class
(
308 native_bt
.field_class_integer_signed_create
,
309 bt2_field_class
._SignedIntegerFieldClass
,
312 preferred_display_base
,
316 def create_unsigned_integer_field_class(
317 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
319 return self
._create
_integer
_field
_class
(
320 native_bt
.field_class_integer_unsigned_create
,
321 bt2_field_class
._UnsignedIntegerFieldClass
,
324 preferred_display_base
,
328 def create_signed_enumeration_field_class(
329 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
331 return self
._create
_integer
_field
_class
(
332 native_bt
.field_class_enumeration_signed_create
,
333 bt2_field_class
._SignedEnumerationFieldClass
,
334 'signed enumeration',
336 preferred_display_base
,
340 def create_unsigned_enumeration_field_class(
341 self
, field_value_range
=None, preferred_display_base
=None, user_attributes
=None
343 return self
._create
_integer
_field
_class
(
344 native_bt
.field_class_enumeration_unsigned_create
,
345 bt2_field_class
._UnsignedEnumerationFieldClass
,
346 'unsigned enumeration',
348 preferred_display_base
,
352 def create_real_field_class(self
, is_single_precision
=False, user_attributes
=None):
353 field_class_ptr
= native_bt
.field_class_real_create(self
._ptr
)
354 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'real')
356 field_class
= bt2_field_class
._RealFieldClass
._create
_from
_ptr
(field_class_ptr
)
358 field_class
._is
_single
_precision
= is_single_precision
359 self
._set
_field
_class
_user
_attrs
(field_class
, user_attributes
)
363 def create_structure_field_class(self
, user_attributes
=None):
364 field_class_ptr
= native_bt
.field_class_structure_create(self
._ptr
)
365 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'structure')
366 fc
= bt2_field_class
._StructureFieldClass
._create
_from
_ptr
(field_class_ptr
)
367 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
370 def create_string_field_class(self
, user_attributes
=None):
371 field_class_ptr
= native_bt
.field_class_string_create(self
._ptr
)
372 self
._check
_field
_class
_create
_status
(field_class_ptr
, 'string')
373 fc
= bt2_field_class
._StringFieldClass
._create
_from
_ptr
(field_class_ptr
)
374 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
377 def create_static_array_field_class(self
, elem_fc
, length
, user_attributes
=None):
378 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
379 utils
._check
_uint
64(length
)
380 ptr
= native_bt
.field_class_array_static_create(self
._ptr
, elem_fc
._ptr
, length
)
381 self
._check
_field
_class
_create
_status
(ptr
, 'static array')
382 fc
= bt2_field_class
._StaticArrayFieldClass
._create
_from
_ptr
_and
_get
_ref
(ptr
)
383 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
386 def create_dynamic_array_field_class(
387 self
, elem_fc
, length_fc
=None, user_attributes
=None
389 utils
._check
_type
(elem_fc
, bt2_field_class
._FieldClass
)
392 if length_fc
is not None:
393 utils
._check
_type
(length_fc
, bt2_field_class
._UnsignedIntegerFieldClass
)
394 length_fc_ptr
= length_fc
._ptr
396 ptr
= native_bt
.field_class_array_dynamic_create(
397 self
._ptr
, elem_fc
._ptr
, length_fc_ptr
399 self
._check
_field
_class
_create
_status
(ptr
, 'dynamic array')
400 fc
= bt2_field_class
._DynamicArrayFieldClass
._create
_from
_ptr
(ptr
)
401 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
404 def create_option_field_class(
405 self
, content_fc
, selector_fc
=None, user_attributes
=None
407 utils
._check
_type
(content_fc
, bt2_field_class
._FieldClass
)
409 selector_fc_ptr
= None
411 if selector_fc
is not None:
412 utils
._check
_type
(selector_fc
, bt2_field_class
._BoolFieldClass
)
413 selector_fc_ptr
= selector_fc
._ptr
415 ptr
= native_bt
.field_class_option_create(
416 self
._ptr
, content_fc
._ptr
, selector_fc_ptr
418 self
._check
_field
_class
_create
_status
(ptr
, 'option')
419 fc
= bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
420 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)
423 def create_variant_field_class(self
, selector_fc
=None, user_attributes
=None):
424 selector_fc_ptr
= None
426 if selector_fc
is not None:
427 utils
._check
_type
(selector_fc
, bt2_field_class
._IntegerFieldClass
)
428 selector_fc_ptr
= selector_fc
._ptr
430 ptr
= native_bt
.field_class_variant_create(self
._ptr
, selector_fc_ptr
)
431 self
._check
_field
_class
_create
_status
(ptr
, 'variant')
432 fc
= bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(ptr
)
433 self
._set
_field
_class
_user
_attrs
(fc
, user_attributes
)