1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
9 from bt2
import error
as bt2_error
10 from bt2
import utils
as bt2_utils
11 from bt2
import value
as bt2_value
12 from bt2
import object as bt2_object
13 from bt2
import stream
as bt2_stream
14 from bt2
import native_bt
15 from bt2
import stream_class
as bt2_stream_class
18 def _bt2_trace_class():
19 from bt2
import trace_class
as bt2_trace_class
21 return bt2_trace_class
24 class _TraceEnvironmentConst(collections
.abc
.Mapping
):
25 _create_value_from_ptr_and_get_ref
= staticmethod(
26 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
29 def __init__(self
, trace
):
32 def __getitem__(self
, key
):
33 bt2_utils
._check
_str
(key
)
35 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_value_by_name_const
36 value_ptr
= borrow_entry_fn(self
._trace
._ptr
, key
)
41 return self
._create
_value
_from
_ptr
_and
_get
_ref
(value_ptr
)
44 count
= native_bt
.trace_get_environment_entry_count(self
._trace
._ptr
)
49 trace_ptr
= self
._trace
._ptr
51 for idx
in range(len(self
)):
52 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_by_index_const
53 entry_name
, _
= borrow_entry_fn(trace_ptr
, idx
)
54 assert entry_name
is not None
58 class _TraceEnvironment(_TraceEnvironmentConst
, collections
.abc
.MutableMapping
):
59 _create_value_from_ptr_and_get_ref
= staticmethod(
60 bt2_value
._create
_from
_ptr
_and
_get
_ref
63 def __setitem__(self
, key
, value
):
64 if isinstance(value
, str):
65 set_env_entry_fn
= native_bt
.trace_set_environment_entry_string
66 elif isinstance(value
, int):
67 set_env_entry_fn
= native_bt
.trace_set_environment_entry_integer
69 raise TypeError("expected str or int, got {}".format(type(value
)))
71 status
= set_env_entry_fn(self
._trace
._ptr
, key
, value
)
72 bt2_utils
._handle
_func
_status
(
73 status
, "cannot set trace object's environment entry"
76 def __delitem__(self
, key
):
77 raise NotImplementedError
80 class _TraceConst(bt2_object
._SharedObject
, collections
.abc
.Mapping
):
83 native_bt
.trace_get_ref(ptr
)
87 native_bt
.trace_put_ref(ptr
)
89 _borrow_stream_ptr_by_id
= staticmethod(native_bt
.trace_borrow_stream_by_id_const
)
90 _borrow_stream_ptr_by_index
= staticmethod(
91 native_bt
.trace_borrow_stream_by_index_const
93 _borrow_class_ptr
= staticmethod(native_bt
.trace_borrow_class_const
)
94 _borrow_user_attributes_ptr
= staticmethod(
95 native_bt
.trace_borrow_user_attributes_const
97 _create_value_from_ptr_and_get_ref
= staticmethod(
98 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
100 _stream_pycls
= property(lambda _
: bt2_stream
._StreamConst
)
101 _trace_class_pycls
= property(lambda _
: _bt2_trace_class()._TraceClassConst
)
102 _trace_env_pycls
= property(lambda _
: _TraceEnvironmentConst
)
105 count
= native_bt
.trace_get_stream_count(self
._ptr
)
109 def __getitem__(self
, id):
110 bt2_utils
._check
_uint
64(id)
112 stream_ptr
= self
._borrow
_stream
_ptr
_by
_id
(self
._ptr
, id)
114 if stream_ptr
is None:
117 return self
._stream
_pycls
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
120 for idx
in range(len(self
)):
121 stream_ptr
= self
._borrow
_stream
_ptr
_by
_index
(self
._ptr
, idx
)
122 assert stream_ptr
is not None
124 id = native_bt
.stream_get_id(stream_ptr
)
131 trace_class_ptr
= self
._borrow
_class
_ptr
(self
._ptr
)
132 assert trace_class_ptr
is not None
133 return self
._trace
_class
_pycls
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
136 def user_attributes(self
):
137 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
138 assert ptr
is not None
139 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
143 return native_bt
.trace_get_name(self
._ptr
)
147 uuid_bytes
= native_bt
.trace_get_uuid(self
._ptr
)
148 if uuid_bytes
is None:
151 return uuidp
.UUID(bytes
=uuid_bytes
)
154 def environment(self
):
155 return self
._trace
_env
_pycls
(self
)
157 def add_destruction_listener(self
, listener
):
158 """Add a listener to be called when the trace is destroyed."""
159 if not callable(listener
):
160 raise TypeError("'listener' parameter is not callable")
162 handle
= bt2_utils
._ListenerHandle
(self
.addr
)
164 fn
= native_bt
.bt2_trace_add_destruction_listener
165 listener_from_native
= functools
.partial(
166 _trace_destruction_listener_from_native
, listener
, handle
169 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
170 bt2_utils
._handle
_func
_status
(
171 status
, "cannot add destruction listener to trace object"
174 handle
._set
_listener
_id
(listener_id
)
178 def remove_destruction_listener(self
, listener_handle
):
179 bt2_utils
._check
_type
(listener_handle
, bt2_utils
._ListenerHandle
)
181 if listener_handle
._addr
!= self
.addr
:
183 "This trace destruction listener does not match the trace object."
186 if listener_handle
._listener
_id
is None:
187 raise ValueError("This trace destruction listener was already removed.")
189 status
= native_bt
.trace_remove_destruction_listener(
190 self
._ptr
, listener_handle
._listener
_id
192 bt2_utils
._handle
_func
_status
(status
)
193 listener_handle
._invalidate
()
196 class _Trace(_TraceConst
):
197 _borrow_stream_ptr_by_id
= staticmethod(native_bt
.trace_borrow_stream_by_id
)
198 _borrow_stream_ptr_by_index
= staticmethod(native_bt
.trace_borrow_stream_by_index
)
199 _borrow_class_ptr
= staticmethod(native_bt
.trace_borrow_class
)
200 _borrow_user_attributes_ptr
= staticmethod(native_bt
.trace_borrow_user_attributes
)
201 _create_value_from_ptr_and_get_ref
= staticmethod(
202 bt2_value
._create
_from
_ptr
_and
_get
_ref
204 _stream_pycls
= property(lambda _
: bt2_stream
._Stream
)
205 _trace_class_pycls
= property(lambda _
: _bt2_trace_class()._TraceClass
)
206 _trace_env_pycls
= property(lambda _
: _TraceEnvironment
)
208 def _name(self
, name
):
209 bt2_utils
._check
_str
(name
)
210 status
= native_bt
.trace_set_name(self
._ptr
, name
)
211 bt2_utils
._handle
_func
_status
(status
, "cannot set trace class object's name")
213 _name
= property(fset
=_name
)
215 def _user_attributes(self
, user_attributes
):
216 value
= bt2_value
.create_value(user_attributes
)
217 bt2_utils
._check
_type
(value
, bt2_value
.MapValue
)
218 native_bt
.trace_set_user_attributes(self
._ptr
, value
._ptr
)
220 _user_attributes
= property(fset
=_user_attributes
)
222 def _uuid(self
, uuid
):
223 bt2_utils
._check
_type
(uuid
, uuidp
.UUID
)
224 native_bt
.trace_set_uuid(self
._ptr
, uuid
.bytes
)
226 _uuid
= property(fset
=_uuid
)
228 def create_stream(self
, stream_class
, id=None, name
=None, user_attributes
=None):
229 bt2_utils
._check
_type
(stream_class
, bt2_stream_class
._StreamClass
)
231 if stream_class
.assigns_automatic_stream_id
:
234 "id provided, but stream class assigns automatic stream ids"
237 stream_ptr
= native_bt
.stream_create(stream_class
._ptr
, self
._ptr
)
241 "id not provided, but stream class does not assign automatic stream ids"
244 bt2_utils
._check
_uint
64(id)
245 stream_ptr
= native_bt
.stream_create_with_id(
246 stream_class
._ptr
, self
._ptr
, id
249 if stream_ptr
is None:
250 raise bt2_error
._MemoryError
("cannot create stream object")
252 stream
= bt2_stream
._Stream
._create
_from
_ptr
(stream_ptr
)
257 if user_attributes
is not None:
258 stream
._user
_attributes
= user_attributes
263 def _trace_destruction_listener_from_native(user_listener
, handle
, trace_ptr
):
264 trace
= _TraceConst
._create
_from
_ptr
_and
_get
_ref
(trace_ptr
)