1 # The MIT License (MIT)
3 # Copyright (c) 2016-2017 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
.clock_class
34 def _create_from_ptr(ptr
):
35 # recreate the event class wrapper of this event's class (the
36 # identity could be different, but the underlying address should be
38 event_class_ptr
= native_bt
.ctf_event_get_class(ptr
)
39 utils
._handle
_ptr
(event_class_ptr
, "cannot get event object's class")
40 event_class
= bt2
.EventClass
._create
_from
_ptr
(event_class_ptr
)
41 event
= _Event
._create
_from
_ptr
(ptr
)
42 event
._event
_class
= event_class
46 class _Event(object._Object
):
48 def event_class(self
):
49 return self
._event
_class
53 return self
._event
_class
.name
57 return self
._event
_class
.id
61 packet_ptr
= native_bt
.ctf_event_get_packet(self
._ptr
)
63 if packet_ptr
is None:
66 return bt2
.packet
._Packet
._create
_from
_ptr
(packet_ptr
)
69 def packet(self
, packet
):
70 utils
._check
_type
(packet
, bt2
.packet
._Packet
)
71 ret
= native_bt
.ctf_event_set_packet(self
._ptr
, packet
._ptr
)
72 utils
._handle
_ret
(ret
, "cannot set event object's packet object")
76 stream_ptr
= native_bt
.ctf_event_get_stream(self
._ptr
)
78 if stream_ptr
is None:
81 return bt2
.stream
._Stream
._create
_from
_ptr
(stream_ptr
)
84 def header_field(self
):
85 field_ptr
= native_bt
.ctf_event_get_header(self
._ptr
)
90 return bt2
.fields
._create
_from
_ptr
(field_ptr
)
93 def header_field(self
, header_field
):
94 header_field_ptr
= None
96 if header_field
is not None:
97 utils
._check
_type
(header_field
, bt2
.fields
._Field
)
98 header_field_ptr
= header_field
._ptr
100 ret
= native_bt
.ctf_event_set_header(self
._ptr
, header_field_ptr
)
101 utils
._handle
_ret
(ret
, "cannot set event object's header field")
104 def stream_event_context_field(self
):
105 field_ptr
= native_bt
.ctf_event_get_stream_event_context(self
._ptr
)
107 if field_ptr
is None:
110 return bt2
.fields
._create
_from
_ptr
(field_ptr
)
112 @stream_event_context_field.setter
113 def stream_event_context_field(self
, stream_event_context
):
114 stream_event_context_ptr
= None
116 if stream_event_context
is not None:
117 utils
._check
_type
(stream_event_context
, bt2
.fields
._Field
)
118 stream_event_context_ptr
= stream_event_context
._ptr
120 ret
= native_bt
.ctf_event_set_stream_event_context(self
._ptr
,
121 stream_event_context_ptr
)
122 utils
._handle
_ret
(ret
, "cannot set event object's stream event context field")
125 def context_field(self
):
126 field_ptr
= native_bt
.ctf_event_get_event_context(self
._ptr
)
128 if field_ptr
is None:
131 return bt2
.fields
._create
_from
_ptr
(field_ptr
)
133 @context_field.setter
134 def context_field(self
, context
):
137 if context
is not None:
138 utils
._check
_type
(context
, bt2
.fields
._Field
)
139 context_ptr
= context
._ptr
141 ret
= native_bt
.ctf_event_set_event_context(self
._ptr
, context_ptr
)
142 utils
._handle
_ret
(ret
, "cannot set event object's context field")
145 def payload_field(self
):
146 field_ptr
= native_bt
.ctf_event_get_event_payload(self
._ptr
)
148 if field_ptr
is None:
151 return bt2
.fields
._create
_from
_ptr
(field_ptr
)
153 @payload_field.setter
154 def payload_field(self
, payload
):
157 if payload
is not None:
158 utils
._check
_type
(payload
, bt2
.fields
._Field
)
159 payload_ptr
= payload
._ptr
161 ret
= native_bt
.ctf_event_set_event_payload(self
._ptr
, payload_ptr
)
162 utils
._handle
_ret
(ret
, "cannot set event object's payload field")
164 def _get_clock_value_cycles(self
, clock_class_ptr
):
165 clock_value_ptr
= native_bt
.ctf_event_get_clock_value(self
._ptr
,
168 if clock_value_ptr
is None:
171 ret
, cycles
= native_bt
.ctf_clock_value_get_value(clock_value_ptr
)
172 native_bt
.put(clock_value_ptr
)
173 utils
._handle
_ret
(ret
, "cannot get clock value object's cycles")
176 def clock_value(self
, clock_class
):
177 utils
._check
_type
(clock_class
, bt2
.ClockClass
)
178 clock_value_ptr
= native_bt
.ctf_event_get_clock_value(self
._ptr
,
181 if clock_value_ptr
is None:
184 clock_value
= bt2
.clock_class
._create
_clock
_value
_from
_ptr
(clock_value_ptr
)
187 def add_clock_value(self
, clock_value
):
188 utils
._check
_type
(clock_value
, bt2
.clock_class
._ClockValue
)
189 ret
= native_bt
.ctf_event_set_clock_value(self
._ptr
,
191 utils
._handle
_ret
(ret
, "cannot set event object's clock value")
193 def __getitem__(self
, key
):
194 utils
._check
_str
(key
)
195 payload_field
= self
.payload_field
197 if payload_field
is not None and key
in payload_field
:
198 return payload_field
[key
]
200 context_field
= self
.context_field
202 if context_field
is not None and key
in context_field
:
203 return context_field
[key
]
205 sec_field
= self
.stream_event_context_field
207 if sec_field
is not None and key
in sec_field
:
208 return sec_field
[key
]
210 header_field
= self
.header_field
212 if header_field
is not None and key
in header_field
:
213 return header_field
[key
]
220 pkt_context_field
= packet
.context_field
222 if pkt_context_field
is not None and key
in pkt_context_field
:
223 return pkt_context_field
[key
]
225 pkt_header_field
= packet
.header_field
227 if pkt_header_field
is not None and key
in pkt_header_field
:
228 return pkt_header_field
[key
]
233 def _clock_classes(self
):
234 stream_class
= self
.event_class
.stream_class
236 if stream_class
is None:
239 trace
= stream_class
.trace
246 for clock_class
in trace
.clock_classes
.values():
247 clock_classes
.append(clock_class
)
252 def _clock_class_ptrs(self
):
253 return [cc
._ptr
for cc
in self
._clock
_classes
]
255 def __eq__(self
, other
):
256 if type(other
) is not type(self
):
259 if self
.addr
== other
.addr
:
262 self_clock_values
= {}
263 other_clock_values
= {}
265 for clock_class_ptr
in self
._clock
_class
_ptrs
:
266 self_clock_values
[int(clock_class_ptr
)] = self
._get
_clock
_value
_cycles
(clock_class_ptr
)
268 for clock_class_ptr
in other
._clock
_class
_ptrs
:
269 other_clock_values
[int(clock_class_ptr
)] = self
._get
_clock
_value
_cycles
(clock_class_ptr
)
273 self
.stream_event_context_field
,
280 other
.stream_event_context_field
,
285 return self_props
== other_props
287 def _copy(self
, copy_func
):
288 cpy
= self
.event_class()
291 cpy
.header_field
= copy_func(self
.header_field
)
292 cpy
.stream_event_context_field
= copy_func(self
.stream_event_context_field
)
293 cpy
.context_field
= copy_func(self
.context_field
)
294 cpy
.payload_field
= copy_func(self
.payload_field
)
296 # Copy known clock value references. It's not necessary to copy
297 # clock class or clock value objects because once a clock value
298 # is created from a clock class, the clock class is frozen.
299 # Thus even if we copy the clock class, the user cannot modify
300 # it, therefore it's useless to copy it.
301 for clock_class
in self
._clock
_classes
:
302 clock_value
= self
.clock_value(clock_class
)
304 if clock_value
is not None:
305 cpy
.add_clock_value(clock_value
)
310 return self
._copy
(copy
.copy
)
312 def __deepcopy__(self
, memo
):
313 cpy
= self
._copy
(copy
.deepcopy
)