1 # The MIT License (MIT)
3 # Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 from bt2
import native_bt
, object, utils
24 import bt2
.field_types
25 import collections
.abc
31 class _StreamClassIterator(collections
.abc
.Iterator
):
32 def __init__(self
, trace
):
37 if self
._at
== len(self
._trace
):
40 sc_ptr
= native_bt
.ctf_trace_get_stream_class(self
._trace
._ptr
,
42 utils
._handle
_ptr
(sc_ptr
, "cannot get trace class object's stream class object")
43 id = native_bt
.ctf_stream_class_get_id(sc_ptr
)
46 if utils
._is
_m
1ull(id):
47 raise bt2
.Error("cannot get stream class object's ID")
53 class _TraceClockClassesIterator(collections
.abc
.Iterator
):
54 def __init__(self
, trace_clock_classes
):
55 self
._trace
_clock
_classes
= trace_clock_classes
59 if self
._at
== len(self
._trace
_clock
_classes
):
62 trace_ptr
= self
._trace
_clock
_classes
._trace
._ptr
63 cc_ptr
= native_bt
.ctf_trace_get_clock_class(trace_ptr
, self
._at
)
64 utils
._handle
_ptr
(cc_ptr
, "cannot get trace class object's clock class")
65 name
= native_bt
.ctf_clock_class_get_name(cc_ptr
)
67 utils
._handle
_ptr
(name
, "cannot get clock class object's name")
72 class _TraceClockClasses(collections
.abc
.Mapping
):
73 def __init__(self
, trace
):
76 def __getitem__(self
, key
):
78 cc_ptr
= native_bt
.ctf_trace_get_clock_class_by_name(self
._trace
._ptr
,
84 return bt2
.ClockClass
._create
_from
_ptr
(cc_ptr
)
87 count
= native_bt
.ctf_trace_get_clock_class_count(self
._trace
._ptr
)
88 utils
._handle
_ret
(count
, "cannot get trace class object's clock class count")
92 return _TraceClockClassesIterator(self
)
95 class _TraceEnvIterator(collections
.abc
.Iterator
):
96 def __init__(self
, trace_env
):
97 self
._trace
_env
= trace_env
101 if self
._at
== len(self
._trace
_env
):
104 trace_ptr
= self
._trace
_env
._trace
._ptr
105 entry_name
= native_bt
.ctf_trace_get_environment_field_name(trace_ptr
,
107 utils
._handle
_ptr
(entry_name
, "cannot get trace class object's environment entry name")
112 class _TraceEnv(collections
.abc
.MutableMapping
):
113 def __init__(self
, trace
):
116 def __getitem__(self
, key
):
117 utils
._check
_str
(key
)
118 value_ptr
= native_bt
.ctf_trace_get_environment_field_value_by_name(self
._trace
._ptr
,
121 if value_ptr
is None:
124 return bt2
.values
._create
_from
_ptr
(value_ptr
)
126 def __setitem__(self
, key
, value
):
127 utils
._check
_str
(key
)
128 value
= bt2
.create_value(value
)
129 ret
= native_bt
.ctf_trace_set_environment_field(self
._trace
._ptr
,
131 utils
._handle
_ret
(ret
, "cannot set trace class object's environment entry")
133 def __delitem__(self
, key
):
134 raise NotImplementedError
137 count
= native_bt
.ctf_trace_get_environment_field_count(self
._trace
._ptr
)
138 utils
._handle
_ret
(count
, "cannot get trace class object's environment entry count")
142 return _TraceEnvIterator(self
)
145 class Trace(object._Object
, collections
.abc
.Mapping
):
146 def __init__(self
, name
=None, native_byte_order
=None, env
=None,
147 packet_header_field_type
=None, clock_classes
=None,
148 stream_classes
=None):
149 ptr
= native_bt
.ctf_trace_create()
152 raise bt2
.CreationError('cannot create trace class object')
154 super().__init
__(ptr
)
159 if native_byte_order
is not None:
160 self
.native_byte_order
= native_byte_order
162 if packet_header_field_type
is not None:
163 self
.packet_header_field_type
= packet_header_field_type
166 for key
, value
in env
.items():
167 self
.env
[key
] = value
169 if clock_classes
is not None:
170 for clock_class
in clock_classes
:
171 self
.add_clock_class(clock_class
)
173 if stream_classes
is not None:
174 for stream_class
in stream_classes
:
175 self
.add_stream_class(stream_class
)
177 def __getitem__(self
, key
):
178 utils
._check
_int
64(key
)
179 sc_ptr
= native_bt
.ctf_trace_get_stream_class_by_id(self
._ptr
, key
)
184 return bt2
.StreamClass
._create
_from
_ptr
(sc_ptr
)
187 count
= native_bt
.ctf_trace_get_stream_class_count(self
._ptr
)
188 utils
._handle
_ret
(count
, "cannot get trace class object's stream class count")
192 return _StreamClassIterator(self
)
194 def add_stream_class(self
, stream_class
):
195 utils
._check
_type
(stream_class
, bt2
.StreamClass
)
196 ret
= native_bt
.ctf_trace_add_stream_class(self
._ptr
, stream_class
._ptr
)
197 utils
._handle
_ret
(ret
, "cannot add stream class object to trace class object")
201 return native_bt
.ctf_trace_get_name(self
._ptr
)
204 def name(self
, name
):
205 utils
._check
_str
(name
)
206 ret
= native_bt
.ctf_trace_set_name(self
._ptr
, name
)
207 utils
._handle
_ret
(ret
, "cannot set trace class object's name")
210 def native_byte_order(self
):
211 bo
= native_bt
.ctf_trace_get_byte_order(self
._ptr
)
212 utils
._handle
_ret
(bo
, "cannot get trace class object's native byte order")
215 @native_byte_order.setter
216 def native_byte_order(self
, native_byte_order
):
217 utils
._check
_int
(native_byte_order
)
218 ret
= native_bt
.ctf_trace_set_byte_order(self
._ptr
, native_byte_order
)
219 utils
._handle
_ret
(ret
, "cannot set trace class object's native byte order")
223 return _TraceEnv(self
)
226 def clock_classes(self
):
227 return _TraceClockClasses(self
)
229 def add_clock_class(self
, clock_class
):
230 utils
._check
_type
(clock_class
, bt2
.ClockClass
)
231 ret
= native_bt
.ctf_trace_add_clock_class(self
._ptr
, clock_class
._ptr
)
232 utils
._handle
_ret
(ret
, "cannot add clock class object to trace class object")
235 def packet_header_field_type(self
):
236 ft_ptr
= native_bt
.ctf_trace_get_packet_header_type(self
._ptr
)
241 return bt2
.field_types
._create
_from
_ptr
(ft_ptr
)
243 @packet_header_field_type.setter
244 def packet_header_field_type(self
, packet_header_field_type
):
245 packet_header_field_type_ptr
= None
247 if packet_header_field_type
is not None:
248 utils
._check
_type
(packet_header_field_type
, bt2
.field_types
._FieldType
)
249 packet_header_field_type_ptr
= packet_header_field_type
._ptr
251 ret
= native_bt
.ctf_trace_set_packet_header_type(self
._ptr
,
252 packet_header_field_type_ptr
)
253 utils
._handle
_ret
(ret
, "cannot set trace class object's packet header field type")
255 def __eq__(self
, other
):
256 if type(other
) is not type(self
):
257 # not comparing apples to apples
260 if self
.addr
== other
.addr
:
263 self_stream_classes
= list(self
.values())
264 self_clock_classes
= list(self
.clock_classes
.values())
265 self_env
= {key
: val
for key
, val
in self
.env
.items()}
266 other_stream_classes
= list(other
.values())
267 other_clock_classes
= list(other
.clock_classes
.values())
268 other_env
= {key
: val
for key
, val
in other
.env
.items()}
274 self
.native_byte_order
,
275 self
.packet_header_field_type
,
278 other_stream_classes
,
282 other
.native_byte_order
,
283 other
.packet_header_field_type
,
285 return self_props
== other_props
287 def _copy(self
, gen_copy_func
, sc_copy_func
):
290 if self
.name
is not None:
293 cpy
.packet_header_field_type
= gen_copy_func(self
.packet_header_field_type
)
295 for key
, val
in self
.env
.items():
296 cpy
.env
[key
] = gen_copy_func(val
)
298 for clock_class
in self
.clock_classes
.values():
299 cpy
.add_clock_class(gen_copy_func(clock_class
))
301 for stream_class
in self
.values():
302 cpy
.add_stream_class(sc_copy_func(stream_class
))
307 return self
._copy
(lambda obj
: obj
, copy
.copy
)
309 def __deepcopy__(self
, memo
):
310 cpy
= self
._copy
(copy
.deepcopy
, copy
.deepcopy
)