1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
, object, utils
7 from bt2
import value
as bt2_value
8 from bt2
import stream
as bt2_stream
9 from bt2
import stream_class
as bt2_stream_class
15 def _bt2_trace_class():
16 from bt2
import trace_class
as bt2_trace_class
18 return bt2_trace_class
21 class _TraceEnvironmentConst(collections
.abc
.Mapping
):
22 _create_value_from_ptr_and_get_ref
= staticmethod(
23 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
26 def __init__(self
, trace
):
29 def __getitem__(self
, key
):
32 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_value_by_name_const
33 value_ptr
= borrow_entry_fn(self
._trace
._ptr
, key
)
38 return self
._create
_value
_from
_ptr
_and
_get
_ref
(value_ptr
)
41 count
= native_bt
.trace_get_environment_entry_count(self
._trace
._ptr
)
46 trace_ptr
= self
._trace
._ptr
48 for idx
in range(len(self
)):
49 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_by_index_const
50 entry_name
, _
= borrow_entry_fn(trace_ptr
, idx
)
51 assert entry_name
is not None
55 class _TraceEnvironment(_TraceEnvironmentConst
, collections
.abc
.MutableMapping
):
56 _create_value_from_ptr_and_get_ref
= staticmethod(
57 bt2_value
._create
_from
_ptr
_and
_get
_ref
60 def __setitem__(self
, key
, value
):
61 if isinstance(value
, str):
62 set_env_entry_fn
= native_bt
.trace_set_environment_entry_string
63 elif isinstance(value
, int):
64 set_env_entry_fn
= native_bt
.trace_set_environment_entry_integer
66 raise TypeError('expected str or int, got {}'.format(type(value
)))
68 status
= set_env_entry_fn(self
._trace
._ptr
, key
, value
)
69 utils
._handle
_func
_status
(status
, "cannot set trace object's environment entry")
71 def __delitem__(self
, key
):
72 raise NotImplementedError
75 class _TraceConst(object._SharedObject
, collections
.abc
.Mapping
):
76 _get_ref
= staticmethod(native_bt
.trace_get_ref
)
77 _put_ref
= staticmethod(native_bt
.trace_put_ref
)
78 _borrow_stream_ptr_by_id
= staticmethod(native_bt
.trace_borrow_stream_by_id_const
)
79 _borrow_stream_ptr_by_index
= staticmethod(
80 native_bt
.trace_borrow_stream_by_index_const
82 _borrow_class_ptr
= staticmethod(native_bt
.trace_borrow_class_const
)
83 _borrow_user_attributes_ptr
= staticmethod(
84 native_bt
.trace_borrow_user_attributes_const
86 _create_value_from_ptr_and_get_ref
= staticmethod(
87 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
89 _stream_pycls
= property(lambda _
: bt2_stream
._StreamConst
)
90 _trace_class_pycls
= property(lambda _
: _bt2_trace_class()._TraceClassConst
)
91 _trace_env_pycls
= property(lambda _
: _TraceEnvironmentConst
)
94 count
= native_bt
.trace_get_stream_count(self
._ptr
)
98 def __getitem__(self
, id):
99 utils
._check
_uint
64(id)
101 stream_ptr
= self
._borrow
_stream
_ptr
_by
_id
(self
._ptr
, id)
103 if stream_ptr
is None:
106 return self
._stream
_pycls
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
109 for idx
in range(len(self
)):
110 stream_ptr
= self
._borrow
_stream
_ptr
_by
_index
(self
._ptr
, idx
)
111 assert stream_ptr
is not None
113 id = native_bt
.stream_get_id(stream_ptr
)
120 trace_class_ptr
= self
._borrow
_class
_ptr
(self
._ptr
)
121 assert trace_class_ptr
is not None
122 return self
._trace
_class
_pycls
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
125 def user_attributes(self
):
126 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
127 assert ptr
is not None
128 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
132 return native_bt
.trace_get_name(self
._ptr
)
136 uuid_bytes
= native_bt
.trace_get_uuid(self
._ptr
)
137 if uuid_bytes
is None:
140 return uuidp
.UUID(bytes
=uuid_bytes
)
143 def environment(self
):
144 return self
._trace
_env
_pycls
(self
)
146 def add_destruction_listener(self
, listener
):
147 '''Add a listener to be called when the trace is destroyed.'''
148 if not callable(listener
):
149 raise TypeError("'listener' parameter is not callable")
151 handle
= utils
._ListenerHandle
(self
.addr
)
153 fn
= native_bt
.bt2_trace_add_destruction_listener
154 listener_from_native
= functools
.partial(
155 _trace_destruction_listener_from_native
, listener
, handle
158 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
159 utils
._handle
_func
_status
(
160 status
, 'cannot add destruction listener to trace object'
163 handle
._set
_listener
_id
(listener_id
)
167 def remove_destruction_listener(self
, listener_handle
):
168 utils
._check
_type
(listener_handle
, utils
._ListenerHandle
)
170 if listener_handle
._addr
!= self
.addr
:
172 'This trace destruction listener does not match the trace object.'
175 if listener_handle
._listener
_id
is None:
176 raise ValueError('This trace destruction listener was already removed.')
178 status
= native_bt
.trace_remove_destruction_listener(
179 self
._ptr
, listener_handle
._listener
_id
181 utils
._handle
_func
_status
(status
)
182 listener_handle
._invalidate
()
185 class _Trace(_TraceConst
):
186 _borrow_stream_ptr_by_id
= staticmethod(native_bt
.trace_borrow_stream_by_id
)
187 _borrow_stream_ptr_by_index
= staticmethod(native_bt
.trace_borrow_stream_by_index
)
188 _borrow_class_ptr
= staticmethod(native_bt
.trace_borrow_class
)
189 _borrow_user_attributes_ptr
= staticmethod(native_bt
.trace_borrow_user_attributes
)
190 _create_value_from_ptr_and_get_ref
= staticmethod(
191 bt2_value
._create
_from
_ptr
_and
_get
_ref
193 _stream_pycls
= property(lambda _
: bt2_stream
._Stream
)
194 _trace_class_pycls
= property(lambda _
: _bt2_trace_class()._TraceClass
)
195 _trace_env_pycls
= property(lambda _
: _TraceEnvironment
)
197 def _name(self
, name
):
198 utils
._check
_str
(name
)
199 status
= native_bt
.trace_set_name(self
._ptr
, name
)
200 utils
._handle
_func
_status
(status
, "cannot set trace class object's name")
202 _name
= property(fset
=_name
)
204 def _user_attributes(self
, user_attributes
):
205 value
= bt2_value
.create_value(user_attributes
)
206 utils
._check
_type
(value
, bt2_value
.MapValue
)
207 native_bt
.trace_set_user_attributes(self
._ptr
, value
._ptr
)
209 _user_attributes
= property(fset
=_user_attributes
)
211 def _uuid(self
, uuid
):
212 utils
._check
_type
(uuid
, uuidp
.UUID
)
213 native_bt
.trace_set_uuid(self
._ptr
, uuid
.bytes
)
215 _uuid
= property(fset
=_uuid
)
217 def create_stream(self
, stream_class
, id=None, name
=None, user_attributes
=None):
218 utils
._check
_type
(stream_class
, bt2_stream_class
._StreamClass
)
220 if stream_class
.assigns_automatic_stream_id
:
223 "id provided, but stream class assigns automatic stream ids"
226 stream_ptr
= native_bt
.stream_create(stream_class
._ptr
, self
._ptr
)
230 "id not provided, but stream class does not assign automatic stream ids"
233 utils
._check
_uint
64(id)
234 stream_ptr
= native_bt
.stream_create_with_id(
235 stream_class
._ptr
, self
._ptr
, id
238 if stream_ptr
is None:
239 raise bt2
._MemoryError('cannot create stream object')
241 stream
= bt2_stream
._Stream
._create
_from
_ptr
(stream_ptr
)
246 if user_attributes
is not None:
247 stream
._user
_attributes
= user_attributes
252 def _trace_destruction_listener_from_native(user_listener
, handle
, trace_ptr
):
253 trace
= _TraceConst
._create
_from
_ptr
_and
_get
_ref
(trace_ptr
)