Commit | Line | Data |
---|---|---|
8deee039 PP |
1 | #ifndef BABELTRACE_CTF_WRITER_EVENT_INTERNAL_H |
2 | #define BABELTRACE_CTF_WRITER_EVENT_INTERNAL_H | |
3 | ||
4 | /* | |
8deee039 PP |
5 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
6 | * | |
7 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
8 | * | |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | * | |
27 | * The Common Trace Format (CTF) Specification is available at | |
28 | * http://www.efficios.com/ctf | |
29 | */ | |
30 | ||
969c1d8a FD |
31 | #include <stdbool.h> |
32 | ||
57952005 | 33 | #include "common/assert.h" |
85e7137b | 34 | #include "common/macros.h" |
3996bc53 PP |
35 | #include <babeltrace2-ctf-writer/field-types.h> |
36 | #include <babeltrace2-ctf-writer/fields.h> | |
37 | #include <babeltrace2-ctf-writer/stream-class.h> | |
38 | #include <babeltrace2-ctf-writer/stream.h> | |
57952005 MJ |
39 | #include "ctfser/ctfser.h" |
40 | ||
41 | #include "assert-pre.h" | |
42 | #include "event-class.h" | |
43 | #include "event.h" | |
44 | #include "fields.h" | |
45 | #include "field-wrapper.h" | |
46 | #include "object.h" | |
47 | #include "stream.h" | |
48 | #include "validation.h" | |
49 | #include "values.h" | |
8deee039 PP |
50 | |
51 | struct bt_ctf_stream_class; | |
52 | struct bt_ctf_stream_pos; | |
53 | struct metadata_context; | |
54 | ||
086dc475 | 55 | struct bt_ctf_event_common { |
c2606e2f | 56 | struct bt_ctf_object base; |
086dc475 PP |
57 | struct bt_ctf_event_class_common *class; |
58 | struct bt_ctf_field_wrapper *header_field; | |
59 | struct bt_ctf_field_common *stream_event_context_field; | |
60 | struct bt_ctf_field_common *context_field; | |
61 | struct bt_ctf_field_common *payload_field; | |
62 | int frozen; | |
63 | }; | |
64 | ||
65 | BT_HIDDEN | |
66 | int _bt_ctf_event_common_validate(struct bt_ctf_event_common *event); | |
67 | ||
68 | BT_HIDDEN | |
69 | void _bt_ctf_event_common_set_is_frozen(struct bt_ctf_event_common *event, | |
70 | bool is_frozen); | |
71 | ||
72 | #ifdef BT_DEV_MODE | |
73 | # define bt_ctf_event_common_validate _bt_ctf_event_common_validate | |
74 | # define bt_ctf_event_common_set_is_frozen _bt_ctf_event_common_set_is_frozen | |
75 | #else | |
76 | # define bt_ctf_event_common_validate(_event) 0 | |
77 | # define bt_ctf_event_common_set_is_frozen(_event, _is_frozen) | |
78 | #endif | |
79 | ||
bf436657 MJ |
80 | #define BT_CTF_ASSERT_PRE_EVENT_COMMON_HOT(_event, _name) \ |
81 | BT_CTF_ASSERT_PRE_HOT((_event), (_name), ": event-addr=%p", (_event)) | |
086dc475 PP |
82 | |
83 | static inline | |
84 | struct bt_ctf_event_class_common *bt_ctf_event_common_borrow_class( | |
85 | struct bt_ctf_event_common *event) | |
86 | { | |
ec4a3354 | 87 | BT_ASSERT_DBG(event); |
086dc475 PP |
88 | return event->class; |
89 | } | |
90 | ||
4ec8d4cd SM |
91 | typedef void *(*create_field_func_type)(void *); |
92 | typedef void (*release_field_func_type)(void *); | |
93 | typedef void *(*create_header_field_func_type)(void *, void *); | |
94 | typedef void (*release_header_field_func_type)(void *, void *); | |
086dc475 PP |
95 | |
96 | BT_HIDDEN | |
97 | int bt_ctf_event_common_initialize(struct bt_ctf_event_common *event, | |
98 | struct bt_ctf_event_class_common *event_class, | |
99 | struct bt_ctf_clock_class *init_expected_clock_class, | |
c2606e2f | 100 | bool is_shared_with_parent, bt_ctf_object_release_func release_func, |
086dc475 PP |
101 | bt_ctf_validation_flag_copy_field_type_func field_type_copy_func, |
102 | bool must_be_in_trace, | |
103 | int (*map_clock_classes_func)(struct bt_ctf_stream_class_common *stream_class, | |
104 | struct bt_ctf_field_type_common *packet_context_field_type, | |
105 | struct bt_ctf_field_type_common *event_header_field_type), | |
4ec8d4cd SM |
106 | create_field_func_type create_field_func, |
107 | release_field_func_type release_field_func, | |
108 | create_header_field_func_type create_header_field_func, | |
109 | release_header_field_func_type release_header_field_func); | |
086dc475 | 110 | |
f3847c75 SM |
111 | BT_HIDDEN |
112 | int bt_ctf_event_common_set_payload(struct bt_ctf_event *event, | |
113 | struct bt_ctf_field *payload); | |
114 | ||
086dc475 PP |
115 | static inline |
116 | struct bt_ctf_field_common *bt_ctf_event_common_borrow_payload( | |
117 | struct bt_ctf_event_common *event) | |
118 | { | |
119 | struct bt_ctf_field_common *payload = NULL; | |
120 | ||
bf436657 | 121 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
086dc475 PP |
122 | |
123 | if (!event->payload_field) { | |
c9ecaa78 | 124 | BT_LOGT("Event has no current payload field: addr=%p, " |
086dc475 PP |
125 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
126 | event, bt_ctf_event_class_common_get_name(event->class), | |
127 | bt_ctf_event_class_common_get_id(event->class)); | |
128 | goto end; | |
129 | } | |
130 | ||
131 | payload = event->payload_field; | |
132 | ||
133 | end: | |
134 | return payload; | |
135 | } | |
136 | ||
137 | static inline | |
138 | struct bt_ctf_field_common *bt_ctf_event_common_borrow_header( | |
139 | struct bt_ctf_event_common *event) | |
140 | { | |
141 | struct bt_ctf_field_common *header = NULL; | |
142 | ||
bf436657 | 143 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
086dc475 PP |
144 | |
145 | if (!event->header_field) { | |
c9ecaa78 | 146 | BT_LOGT("Event has no current header field: addr=%p, " |
086dc475 PP |
147 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
148 | event, bt_ctf_event_class_common_get_name(event->class), | |
149 | bt_ctf_event_class_common_get_id(event->class)); | |
150 | goto end; | |
151 | } | |
152 | ||
153 | header = event->header_field->field; | |
154 | ||
155 | end: | |
156 | return header; | |
157 | } | |
158 | ||
159 | static inline | |
160 | struct bt_ctf_field_common *bt_ctf_event_common_borrow_context( | |
161 | struct bt_ctf_event_common *event) | |
162 | { | |
163 | struct bt_ctf_field_common *context = NULL; | |
164 | ||
bf436657 | 165 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
086dc475 PP |
166 | |
167 | if (!event->context_field) { | |
c9ecaa78 | 168 | BT_LOGT("Event has no current context field: addr=%p, " |
086dc475 PP |
169 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
170 | event, bt_ctf_event_class_common_get_name(event->class), | |
171 | bt_ctf_event_class_common_get_id(event->class)); | |
172 | goto end; | |
173 | } | |
174 | ||
175 | context = event->context_field; | |
176 | ||
177 | end: | |
178 | return context; | |
179 | } | |
180 | ||
181 | static inline | |
182 | struct bt_ctf_field_common *bt_ctf_event_common_borrow_stream_event_context( | |
183 | struct bt_ctf_event_common *event) | |
184 | { | |
185 | struct bt_ctf_field_common *stream_event_context = NULL; | |
186 | ||
bf436657 | 187 | BT_CTF_ASSERT_PRE_NON_NULL(event, "Event"); |
086dc475 PP |
188 | |
189 | if (!event->stream_event_context_field) { | |
c9ecaa78 | 190 | BT_LOGT("Event has no current stream event context field: addr=%p, " |
086dc475 PP |
191 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
192 | event, bt_ctf_event_class_common_get_name(event->class), | |
193 | bt_ctf_event_class_common_get_id(event->class)); | |
194 | goto end; | |
195 | } | |
196 | ||
197 | stream_event_context = event->stream_event_context_field; | |
198 | ||
199 | end: | |
200 | return stream_event_context; | |
201 | } | |
202 | ||
203 | static inline | |
c2606e2f | 204 | void bt_ctf_event_common_finalize(struct bt_ctf_object *obj, |
086dc475 PP |
205 | void (*field_release_func)(void *), |
206 | void (*header_field_release_func)(void *, struct bt_ctf_event_common *)) | |
207 | { | |
208 | struct bt_ctf_event_common *event = (void *) obj; | |
209 | ||
210 | BT_LOGD("Destroying event: addr=%p, " | |
211 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
212 | event, | |
213 | event->class ? bt_ctf_event_class_common_get_name(event->class) : NULL, | |
214 | event->class ? bt_ctf_event_class_common_get_id(event->class) : INT64_C(-1)); | |
215 | ||
216 | if (event->header_field) { | |
217 | BT_LOGD_STR("Releasing event's header field."); | |
218 | header_field_release_func(event->header_field, event); | |
219 | } | |
220 | ||
221 | if (event->stream_event_context_field) { | |
222 | BT_LOGD_STR("Releasing event's stream event context field."); | |
223 | field_release_func(event->stream_event_context_field); | |
224 | } | |
225 | ||
226 | if (event->context_field) { | |
227 | BT_LOGD_STR("Releasing event's context field."); | |
228 | field_release_func(event->context_field); | |
229 | } | |
230 | ||
231 | if (event->payload_field) { | |
232 | BT_LOGD_STR("Releasing event's payload field."); | |
233 | field_release_func(event->payload_field); | |
234 | } | |
235 | ||
236 | /* | |
237 | * Leave this after calling header_field_release_func() because | |
238 | * this function receives the event object and could need its | |
239 | * class to perform some cleanup. | |
240 | */ | |
241 | if (!event->base.parent) { | |
242 | /* | |
243 | * Event was keeping a reference to its class since it shared no | |
244 | * common ancestor with it to guarantee they would both have the | |
245 | * same lifetime. | |
246 | */ | |
c2606e2f | 247 | bt_ctf_object_put_ref(event->class); |
086dc475 PP |
248 | } |
249 | } | |
250 | ||
8deee039 | 251 | struct bt_ctf_event { |
086dc475 | 252 | struct bt_ctf_event_common common; |
8deee039 PP |
253 | }; |
254 | ||
255 | struct bt_ctf_event_class { | |
086dc475 | 256 | struct bt_ctf_event_class_common common; |
8deee039 PP |
257 | }; |
258 | ||
259 | BT_HIDDEN | |
260 | int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class, | |
261 | struct metadata_context *context); | |
262 | ||
263 | BT_HIDDEN | |
264 | int bt_ctf_event_serialize(struct bt_ctf_event *event, | |
6f4eefbe | 265 | struct bt_ctfser *pos, |
8deee039 PP |
266 | enum bt_ctf_byte_order native_byte_order); |
267 | ||
268 | static inline | |
269 | struct bt_ctf_stream_class *bt_ctf_event_class_borrow_stream_class( | |
270 | struct bt_ctf_event_class *event_class) | |
271 | { | |
086dc475 PP |
272 | return BT_CTF_FROM_COMMON(bt_ctf_event_class_common_borrow_stream_class( |
273 | BT_CTF_TO_COMMON(event_class))); | |
8deee039 PP |
274 | } |
275 | ||
276 | #endif /* BABELTRACE_CTF_WRITER_EVENT_INTERNAL_H */ |