1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
6 from bt2
import object as bt2_object
7 from bt2
import utils
as bt2_utils
9 from bt2
import value
as bt2_value
10 from bt2
import stream
as bt2_stream
11 from bt2
import stream_class
as bt2_stream_class
17 def _bt2_trace_class():
18 from bt2
import trace_class
as bt2_trace_class
20 return bt2_trace_class
23 class _TraceEnvironmentConst(collections
.abc
.Mapping
):
24 _create_value_from_ptr_and_get_ref
= staticmethod(
25 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
28 def __init__(self
, trace
):
31 def __getitem__(self
, key
):
32 bt2_utils
._check
_str
(key
)
34 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_value_by_name_const
35 value_ptr
= borrow_entry_fn(self
._trace
._ptr
, key
)
40 return self
._create
_value
_from
_ptr
_and
_get
_ref
(value_ptr
)
43 count
= native_bt
.trace_get_environment_entry_count(self
._trace
._ptr
)
48 trace_ptr
= self
._trace
._ptr
50 for idx
in range(len(self
)):
51 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_by_index_const
52 entry_name
, _
= borrow_entry_fn(trace_ptr
, idx
)
53 assert entry_name
is not None
57 class _TraceEnvironment(_TraceEnvironmentConst
, collections
.abc
.MutableMapping
):
58 _create_value_from_ptr_and_get_ref
= staticmethod(
59 bt2_value
._create
_from
_ptr
_and
_get
_ref
62 def __setitem__(self
, key
, value
):
63 if isinstance(value
, str):
64 set_env_entry_fn
= native_bt
.trace_set_environment_entry_string
65 elif isinstance(value
, int):
66 set_env_entry_fn
= native_bt
.trace_set_environment_entry_integer
68 raise TypeError("expected str or int, got {}".format(type(value
)))
70 status
= set_env_entry_fn(self
._trace
._ptr
, key
, value
)
71 bt2_utils
._handle
_func
_status
(
72 status
, "cannot set trace object's environment entry"
75 def __delitem__(self
, key
):
76 raise NotImplementedError
79 class _TraceConst(bt2_object
._SharedObject
, collections
.abc
.Mapping
):
82 native_bt
.trace_get_ref(ptr
)
86 native_bt
.trace_put_ref(ptr
)
88 _borrow_stream_ptr_by_id
= staticmethod(native_bt
.trace_borrow_stream_by_id_const
)
89 _borrow_stream_ptr_by_index
= staticmethod(
90 native_bt
.trace_borrow_stream_by_index_const
92 _borrow_class_ptr
= staticmethod(native_bt
.trace_borrow_class_const
)
93 _borrow_user_attributes_ptr
= staticmethod(
94 native_bt
.trace_borrow_user_attributes_const
96 _create_value_from_ptr_and_get_ref
= staticmethod(
97 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
99 _stream_pycls
= property(lambda _
: bt2_stream
._StreamConst
)
100 _trace_class_pycls
= property(lambda _
: _bt2_trace_class()._TraceClassConst
)
101 _trace_env_pycls
= property(lambda _
: _TraceEnvironmentConst
)
104 count
= native_bt
.trace_get_stream_count(self
._ptr
)
108 def __getitem__(self
, id):
109 bt2_utils
._check
_uint
64(id)
111 stream_ptr
= self
._borrow
_stream
_ptr
_by
_id
(self
._ptr
, id)
113 if stream_ptr
is None:
116 return self
._stream
_pycls
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
119 for idx
in range(len(self
)):
120 stream_ptr
= self
._borrow
_stream
_ptr
_by
_index
(self
._ptr
, idx
)
121 assert stream_ptr
is not None
123 id = native_bt
.stream_get_id(stream_ptr
)
130 trace_class_ptr
= self
._borrow
_class
_ptr
(self
._ptr
)
131 assert trace_class_ptr
is not None
132 return self
._trace
_class
_pycls
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
135 def user_attributes(self
):
136 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
137 assert ptr
is not None
138 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
142 return native_bt
.trace_get_name(self
._ptr
)
146 uuid_bytes
= native_bt
.trace_get_uuid(self
._ptr
)
147 if uuid_bytes
is None:
150 return uuidp
.UUID(bytes
=uuid_bytes
)
153 def environment(self
):
154 return self
._trace
_env
_pycls
(self
)
156 def add_destruction_listener(self
, listener
):
157 """Add a listener to be called when the trace is destroyed."""
158 if not callable(listener
):
159 raise TypeError("'listener' parameter is not callable")
161 handle
= bt2_utils
._ListenerHandle
(self
.addr
)
163 fn
= native_bt
.bt2_trace_add_destruction_listener
164 listener_from_native
= functools
.partial(
165 _trace_destruction_listener_from_native
, listener
, handle
168 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
169 bt2_utils
._handle
_func
_status
(
170 status
, "cannot add destruction listener to trace object"
173 handle
._set
_listener
_id
(listener_id
)
177 def remove_destruction_listener(self
, listener_handle
):
178 bt2_utils
._check
_type
(listener_handle
, bt2_utils
._ListenerHandle
)
180 if listener_handle
._addr
!= self
.addr
:
182 "This trace destruction listener does not match the trace object."
185 if listener_handle
._listener
_id
is None:
186 raise ValueError("This trace destruction listener was already removed.")
188 status
= native_bt
.trace_remove_destruction_listener(
189 self
._ptr
, listener_handle
._listener
_id
191 bt2_utils
._handle
_func
_status
(status
)
192 listener_handle
._invalidate
()
195 class _Trace(_TraceConst
):
196 _borrow_stream_ptr_by_id
= staticmethod(native_bt
.trace_borrow_stream_by_id
)
197 _borrow_stream_ptr_by_index
= staticmethod(native_bt
.trace_borrow_stream_by_index
)
198 _borrow_class_ptr
= staticmethod(native_bt
.trace_borrow_class
)
199 _borrow_user_attributes_ptr
= staticmethod(native_bt
.trace_borrow_user_attributes
)
200 _create_value_from_ptr_and_get_ref
= staticmethod(
201 bt2_value
._create
_from
_ptr
_and
_get
_ref
203 _stream_pycls
= property(lambda _
: bt2_stream
._Stream
)
204 _trace_class_pycls
= property(lambda _
: _bt2_trace_class()._TraceClass
)
205 _trace_env_pycls
= property(lambda _
: _TraceEnvironment
)
207 def _name(self
, name
):
208 bt2_utils
._check
_str
(name
)
209 status
= native_bt
.trace_set_name(self
._ptr
, name
)
210 bt2_utils
._handle
_func
_status
(status
, "cannot set trace class object's name")
212 _name
= property(fset
=_name
)
214 def _user_attributes(self
, user_attributes
):
215 value
= bt2_value
.create_value(user_attributes
)
216 bt2_utils
._check
_type
(value
, bt2_value
.MapValue
)
217 native_bt
.trace_set_user_attributes(self
._ptr
, value
._ptr
)
219 _user_attributes
= property(fset
=_user_attributes
)
221 def _uuid(self
, uuid
):
222 bt2_utils
._check
_type
(uuid
, uuidp
.UUID
)
223 native_bt
.trace_set_uuid(self
._ptr
, uuid
.bytes
)
225 _uuid
= property(fset
=_uuid
)
227 def create_stream(self
, stream_class
, id=None, name
=None, user_attributes
=None):
228 bt2_utils
._check
_type
(stream_class
, bt2_stream_class
._StreamClass
)
230 if stream_class
.assigns_automatic_stream_id
:
233 "id provided, but stream class assigns automatic stream ids"
236 stream_ptr
= native_bt
.stream_create(stream_class
._ptr
, self
._ptr
)
240 "id not provided, but stream class does not assign automatic stream ids"
243 bt2_utils
._check
_uint
64(id)
244 stream_ptr
= native_bt
.stream_create_with_id(
245 stream_class
._ptr
, self
._ptr
, id
248 if stream_ptr
is None:
249 raise bt2
._MemoryError("cannot create stream object")
251 stream
= bt2_stream
._Stream
._create
_from
_ptr
(stream_ptr
)
256 if user_attributes
is not None:
257 stream
._user
_attributes
= user_attributes
262 def _trace_destruction_listener_from_native(user_listener
, handle
, trace_ptr
):
263 trace
= _TraceConst
._create
_from
_ptr
_and
_get
_ref
(trace_ptr
)