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 import bt2
.stream_class
30 import collections
.abc
34 class _StreamClassIterator(collections
.abc
.Iterator
):
35 def __init__(self
, trace_class
):
36 self
._trace
_class
= trace_class
40 if self
._at
== len(self
._trace
_class
):
43 borrow_stream_class_fn
= native_bt
.trace_class_borrow_stream_class_by_index_const
44 sc_ptr
= borrow_stream_class_fn(self
._trace
_class
._ptr
, self
._at
)
46 id = native_bt
.stream_class_get_id(sc_ptr
)
52 def _trace_class_destruction_listener_from_native(user_listener
, trace_class_ptr
):
53 trace_class
= bt2
.trace_class
._TraceClass
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
54 user_listener(trace_class
)
57 class _TraceClass(object._SharedObject
, collections
.abc
.Mapping
):
58 _get_ref
= staticmethod(native_bt
.trace_class_get_ref
)
59 _put_ref
= staticmethod(native_bt
.trace_class_put_ref
)
61 # Instantiate a trace of this class.
63 def __call__(self
, name
=None, uuid
=None, env
=None):
64 trace_ptr
= native_bt
.trace_create(self
._ptr
)
67 raise bt2
.CreationError('cannot create trace class object')
69 trace
= bt2
.trace
._Trace
._create
_from
_ptr
(trace_ptr
)
78 for key
, value
in env
.items():
79 trace
.env
[key
] = value
83 # Number of stream classes in this trace class.
86 count
= native_bt
.trace_class_get_stream_class_count(self
._ptr
)
90 # Get a stream class by stream id.
92 def __getitem__(self
, key
):
93 utils
._check
_uint
64(key
)
95 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_id_const(self
._ptr
, key
)
99 return bt2
.stream_class
._StreamClass
._create
_from
_ptr
_and
_get
_ref
(sc_ptr
)
102 for idx
in range(len(self
)):
103 sc_ptr
= native_bt
.trace_class_borrow_stream_class_by_index_const(self
._ptr
, idx
)
104 assert sc_ptr
is not None
106 id = native_bt
.stream_class_get_id(sc_ptr
)
111 def create_stream_class(self
, id=None,
113 packet_context_field_class
=None,
114 event_common_context_field_class
=None,
115 default_clock_class
=None,
116 assigns_automatic_event_class_id
=True,
117 assigns_automatic_stream_id
=True,
118 packets_have_beginning_default_clock_snapshot
=False,
119 packets_have_end_default_clock_snapshot
=False,
120 supports_discarded_events
=False,
121 discarded_events_have_default_clock_snapshots
=False,
122 supports_discarded_packets
=False,
123 discarded_packets_have_default_clock_snapshots
=False):
125 if self
.assigns_automatic_stream_class_id
:
127 raise ValueError('id provided, but trace class assigns automatic stream class ids')
129 sc_ptr
= native_bt
.stream_class_create(self
._ptr
)
132 raise ValueError('id not provided, but trace class does not assign automatic stream class ids')
134 utils
._check
_uint
64(id)
135 sc_ptr
= native_bt
.stream_class_create_with_id(self
._ptr
, id)
137 sc
= bt2
.stream_class
._StreamClass
._create
_from
_ptr
(sc_ptr
)
142 if packet_context_field_class
is not None:
143 sc
._packet
_context
_field
_class
= packet_context_field_class
145 if event_common_context_field_class
is not None:
146 sc
._event
_common
_context
_field
_class
= event_common_context_field_class
148 if default_clock_class
is not None:
149 sc
._default
_clock
_class
= default_clock_class
151 sc
._assigns
_automatic
_event
_class
_id
= assigns_automatic_event_class_id
152 sc
._assigns
_automatic
_stream
_id
= assigns_automatic_stream_id
153 sc
._packets
_have
_beginning
_default
_clock
_snapshot
= packets_have_beginning_default_clock_snapshot
154 sc
._packets
_have
_end
_default
_clock
_snapshot
= packets_have_end_default_clock_snapshot
155 sc
._set
_supports
_discarded
_events
(supports_discarded_events
,
156 discarded_events_have_default_clock_snapshots
)
157 sc
._set
_supports
_discarded
_packets
(supports_discarded_packets
,
158 discarded_packets_have_default_clock_snapshots
)
162 def assigns_automatic_stream_class_id(self
):
163 return native_bt
.trace_class_assigns_automatic_stream_class_id(self
._ptr
)
165 def _assigns_automatic_stream_class_id(self
, auto_id
):
166 utils
._check
_bool
(auto_id
)
167 return native_bt
.trace_class_set_assigns_automatic_stream_class_id(self
._ptr
, auto_id
)
169 _assigns_automatic_stream_class_id
= property(fset
=_assigns_automatic_stream_class_id
)
171 # Field class creation methods.
173 def _check_create_status(self
, ptr
, type_name
):
175 raise bt2
.CreationError(
176 'cannot create {} field class'.format(type_name
))
178 def _create_integer_field_class(self
, create_func
, py_cls
, type_name
, field_value_range
, preferred_display_base
):
179 field_class_ptr
= create_func(self
._ptr
)
180 self
._check
_create
_status
(field_class_ptr
, type_name
)
182 field_class
= py_cls
._create
_from
_ptr
(field_class_ptr
)
184 if field_value_range
is not None:
185 field_class
._field
_value
_range
= field_value_range
187 if preferred_display_base
is not None:
188 field_class
._preferred
_display
_base
= preferred_display_base
192 def create_signed_integer_field_class(self
, field_value_range
=None, preferred_display_base
=None):
193 return self
._create
_integer
_field
_class
(native_bt
.field_class_signed_integer_create
,
194 bt2
.field_class
._SignedIntegerFieldClass
,
195 'signed integer', field_value_range
, preferred_display_base
)
197 def create_unsigned_integer_field_class(self
, field_value_range
=None, preferred_display_base
=None):
198 return self
._create
_integer
_field
_class
(native_bt
.field_class_unsigned_integer_create
,
199 bt2
.field_class
._UnsignedIntegerFieldClass
,
200 'unsigned integer', field_value_range
, preferred_display_base
)
202 def create_signed_enumeration_field_class(self
, field_value_range
=None, preferred_display_base
=None):
203 return self
._create
_integer
_field
_class
(native_bt
.field_class_signed_enumeration_create
,
204 bt2
.field_class
._SignedEnumerationFieldClass
,
205 'signed enumeration', field_value_range
, preferred_display_base
)
207 def create_unsigned_enumeration_field_class(self
, field_value_range
=None, preferred_display_base
=None):
208 return self
._create
_integer
_field
_class
(native_bt
.field_class_unsigned_enumeration_create
,
209 bt2
.field_class
._UnsignedEnumerationFieldClass
,
210 'unsigned enumeration', field_value_range
, preferred_display_base
)
212 def create_real_field_class(self
, is_single_precision
=False):
213 field_class_ptr
= native_bt
.field_class_real_create(self
._ptr
)
214 self
._check
_create
_status
(field_class_ptr
, 'real')
216 field_class
= bt2
.field_class
._RealFieldClass
._create
_from
_ptr
(field_class_ptr
)
218 field_class
._is
_single
_precision
= is_single_precision
222 def create_structure_field_class(self
):
223 field_class_ptr
= native_bt
.field_class_structure_create(self
._ptr
)
224 self
._check
_create
_status
(field_class_ptr
, 'structure')
226 return bt2
.field_class
._StructureFieldClass
._create
_from
_ptr
(field_class_ptr
)
228 def create_string_field_class(self
):
229 field_class_ptr
= native_bt
.field_class_string_create(self
._ptr
)
230 self
._check
_create
_status
(field_class_ptr
, 'string')
232 return bt2
.field_class
._StringFieldClass
._create
_from
_ptr
(field_class_ptr
)
234 def create_static_array_field_class(self
, elem_fc
, length
):
235 utils
._check
_type
(elem_fc
, bt2
.field_class
._FieldClass
)
236 utils
._check
_uint
64(length
)
237 ptr
= native_bt
.field_class_static_array_create(self
._ptr
, elem_fc
._ptr
, length
)
238 self
._check
_create
_status
(ptr
, 'static array')
240 return bt2
.field_class
._StaticArrayFieldClass
._create
_from
_ptr
_and
_get
_ref
(ptr
)
242 def create_dynamic_array_field_class(self
, elem_fc
, length_fc
=None):
243 utils
._check
_type
(elem_fc
, bt2
.field_class
._FieldClass
)
244 ptr
= native_bt
.field_class_dynamic_array_create(self
._ptr
, elem_fc
._ptr
)
245 self
._check
_create
_status
(ptr
, 'dynamic array')
246 obj
= bt2
.field_class
._DynamicArrayFieldClass
._create
_from
_ptr
(ptr
)
248 if length_fc
is not None:
249 obj
._length
_field
_class
= length_fc
253 def create_variant_field_class(self
, selector_fc
=None):
254 ptr
= native_bt
.field_class_variant_create(self
._ptr
)
255 self
._check
_create
_status
(ptr
, 'variant')
256 obj
= bt2
.field_class
._VariantFieldClass
._create
_from
_ptr
(ptr
)
258 if selector_fc
is not None:
259 obj
._selector
_field
_class
= selector_fc
263 # Add a listener to be called when the trace class is destroyed.
265 def add_destruction_listener(self
, listener
):
267 if not callable(listener
):
268 raise TypeError("'listener' parameter is not callable")
270 fn
= native_bt
.bt2_trace_class_add_destruction_listener
271 listener_from_native
= functools
.partial(_trace_class_destruction_listener_from_native
,
274 listener_id
= fn(self
._ptr
, listener_from_native
)
275 if listener_id
is None:
276 utils
._raise
_bt
2_error
('cannot add destruction listener to trace class object')
278 return bt2
._ListenerHandle(listener_id
, self
)