Commit | Line | Data |
---|---|---|
8deee039 PP |
1 | /* |
2 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> | |
4 | * | |
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: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
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 THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
24 | #define BT_LOG_TAG "CTF-WRITER-EVENT" | |
25 | #include <babeltrace/lib-logging-internal.h> | |
26 | ||
27 | #include <babeltrace/ctf-ir/event-internal.h> | |
28 | #include <babeltrace/ctf-writer/clock-internal.h> | |
29 | #include <babeltrace/ctf-writer/event-internal.h> | |
30 | #include <babeltrace/ctf-writer/event.h> | |
31 | #include <babeltrace/ctf-writer/fields-internal.h> | |
32 | #include <babeltrace/ctf-writer/field-types-internal.h> | |
33 | #include <babeltrace/ctf-writer/field-types.h> | |
34 | #include <babeltrace/ctf-writer/fields.h> | |
35 | #include <babeltrace/ctf-writer/stream-class-internal.h> | |
36 | #include <babeltrace/ctf-writer/stream-class.h> | |
37 | #include <babeltrace/ctf-writer/trace-internal.h> | |
38 | #include <babeltrace/ctf-writer/trace.h> | |
39 | #include <babeltrace/ref.h> | |
40 | #include <babeltrace/compiler-internal.h> | |
41 | #include <babeltrace/assert-internal.h> | |
42 | #include <babeltrace/assert-pre-internal.h> | |
43 | #include <inttypes.h> | |
44 | ||
45 | static | |
46 | void bt_ctf_event_destroy(struct bt_object *obj); | |
47 | ||
48 | static | |
49 | int map_clock_classes_func(struct bt_stream_class_common *stream_class, | |
50 | struct bt_field_type_common *packet_context_type, | |
51 | struct bt_field_type_common *event_header_type) | |
52 | { | |
53 | int ret = bt_ctf_stream_class_map_clock_class( | |
54 | BT_FROM_COMMON(stream_class), | |
55 | BT_FROM_COMMON(packet_context_type), | |
56 | BT_FROM_COMMON(event_header_type)); | |
57 | ||
58 | if (ret) { | |
59 | BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class."); | |
60 | } | |
61 | ||
62 | return ret; | |
63 | } | |
64 | ||
65 | struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) | |
66 | { | |
67 | int ret; | |
68 | struct bt_ctf_event *event = NULL; | |
69 | struct bt_clock_class *expected_clock_class = NULL; | |
70 | ||
71 | event = g_new0(struct bt_ctf_event, 1); | |
72 | if (!event) { | |
73 | BT_LOGE_STR("Failed to allocate one CTF writer event."); | |
74 | goto error; | |
75 | } | |
76 | ||
77 | if (event_class) { | |
78 | struct bt_ctf_stream_class *stream_class = | |
79 | BT_FROM_COMMON(bt_event_class_common_borrow_stream_class( | |
80 | BT_TO_COMMON(event_class))); | |
81 | ||
82 | if (stream_class && stream_class->clock) { | |
83 | expected_clock_class = BT_TO_COMMON( | |
84 | stream_class->clock->clock_class); | |
85 | } | |
86 | } | |
87 | ||
88 | ret = bt_event_common_initialize(BT_TO_COMMON(event), | |
89 | BT_TO_COMMON(event_class), expected_clock_class, | |
90 | bt_ctf_event_destroy, | |
91 | (bt_validation_flag_copy_field_type_func) | |
92 | bt_ctf_field_type_copy, | |
93 | false, map_clock_classes_func, | |
94 | (void *) bt_ctf_field_create); | |
95 | if (ret) { | |
96 | /* bt_event_common_initialize() logs errors */ | |
97 | goto error; | |
98 | } | |
99 | ||
100 | goto end; | |
101 | ||
102 | error: | |
103 | BT_PUT(event); | |
104 | ||
105 | end: | |
106 | return event; | |
107 | } | |
108 | ||
109 | struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event) | |
110 | { | |
111 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
112 | return bt_get(bt_event_common_borrow_class(BT_TO_COMMON(event))); | |
113 | } | |
114 | ||
115 | BT_HIDDEN | |
116 | struct bt_ctf_stream *bt_ctf_event_borrow_stream(struct bt_ctf_event *event) | |
117 | { | |
118 | BT_ASSERT(event); | |
119 | return (struct bt_ctf_stream *) | |
120 | bt_object_borrow_parent(BT_TO_COMMON(event)); | |
121 | } | |
122 | ||
123 | struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event) | |
124 | { | |
125 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
126 | return bt_get(bt_ctf_event_borrow_stream(event)); | |
127 | } | |
128 | ||
129 | int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, | |
130 | struct bt_ctf_field *field) | |
131 | { | |
132 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
133 | BT_ASSERT_PRE_NON_NULL(field, "Payload field"); | |
134 | BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_TO_COMMON(event), "Event"); | |
135 | return bt_ctf_field_structure_set_field_by_name( | |
136 | BT_FROM_COMMON(event->common.payload_field), name, field); | |
137 | } | |
138 | ||
139 | struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, | |
140 | const char *name) | |
141 | { | |
142 | struct bt_ctf_field *field = NULL; | |
143 | ||
144 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
145 | ||
146 | if (name) { | |
147 | field = bt_ctf_field_structure_get_field_by_name( | |
148 | BT_FROM_COMMON(event->common.payload_field), name); | |
149 | } else { | |
150 | field = BT_FROM_COMMON(event->common.payload_field); | |
151 | bt_get(field); | |
152 | } | |
153 | ||
154 | return field; | |
155 | } | |
156 | ||
157 | struct bt_ctf_field *bt_ctf_event_get_payload_field( | |
158 | struct bt_ctf_event *event) | |
159 | { | |
160 | return (void *) bt_event_common_get_payload(BT_TO_COMMON(event)); | |
161 | } | |
162 | ||
163 | int bt_ctf_event_set_payload_field(struct bt_ctf_event *event, | |
164 | struct bt_ctf_field *field) | |
165 | { | |
166 | return bt_event_common_set_payload(BT_TO_COMMON(event), (void *) field); | |
167 | } | |
168 | ||
169 | struct bt_ctf_field *bt_ctf_event_get_header(struct bt_ctf_event *event) | |
170 | { | |
171 | return BT_FROM_COMMON(bt_event_common_get_header(BT_TO_COMMON(event))); | |
172 | } | |
173 | ||
174 | int bt_ctf_event_set_header(struct bt_ctf_event *event, | |
175 | struct bt_ctf_field *field) | |
176 | { | |
177 | return bt_event_common_set_header(BT_TO_COMMON(event), (void *) field); | |
178 | } | |
179 | ||
180 | struct bt_ctf_field *bt_ctf_event_get_context(struct bt_ctf_event *event) | |
181 | { | |
182 | return BT_FROM_COMMON(bt_event_common_get_context(BT_TO_COMMON(event))); | |
183 | } | |
184 | ||
185 | int bt_ctf_event_set_context(struct bt_ctf_event *event, | |
186 | struct bt_ctf_field *field) | |
187 | { | |
188 | return bt_event_common_set_context(BT_TO_COMMON(event), (void *) field); | |
189 | } | |
190 | ||
191 | struct bt_ctf_field *bt_ctf_event_get_stream_event_context( | |
192 | struct bt_ctf_event *event) | |
193 | { | |
194 | return BT_FROM_COMMON(bt_event_common_get_stream_event_context( | |
195 | BT_TO_COMMON(event))); | |
196 | } | |
197 | ||
198 | int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event, | |
199 | struct bt_ctf_field *field) | |
200 | { | |
201 | return bt_event_common_set_stream_event_context( | |
202 | BT_TO_COMMON(event), (void *) field); | |
203 | } | |
204 | ||
205 | static | |
206 | void bt_ctf_event_destroy(struct bt_object *obj) | |
207 | { | |
208 | bt_event_common_finalize(obj); | |
209 | g_free(obj); | |
210 | } | |
211 | ||
212 | BT_HIDDEN | |
213 | int bt_ctf_event_serialize(struct bt_ctf_event *event, | |
214 | struct bt_ctf_stream_pos *pos, | |
215 | enum bt_ctf_byte_order native_byte_order) | |
216 | { | |
217 | int ret = 0; | |
218 | ||
219 | BT_ASSERT(event); | |
220 | BT_ASSERT(pos); | |
221 | ||
222 | BT_LOGV_STR("Serializing event's context field."); | |
223 | if (event->common.context_field) { | |
224 | ret = bt_ctf_field_serialize_recursive( | |
225 | (void *) event->common.context_field, pos, | |
226 | native_byte_order); | |
227 | if (ret) { | |
228 | BT_LOGW("Cannot serialize event's context field: " | |
229 | "event-addr=%p, event-class-name=\"%s\", " | |
230 | "event-class-id=%" PRId64, | |
231 | event, | |
232 | bt_event_class_common_get_name(event->common.class), | |
233 | bt_event_class_common_get_id(event->common.class)); | |
234 | goto end; | |
235 | } | |
236 | } | |
237 | ||
238 | BT_LOGV_STR("Serializing event's payload field."); | |
239 | if (event->common.payload_field) { | |
240 | ret = bt_ctf_field_serialize_recursive( | |
241 | (void *) event->common.payload_field, pos, | |
242 | native_byte_order); | |
243 | if (ret) { | |
244 | BT_LOGW("Cannot serialize event's payload field: " | |
245 | "event-addr=%p, event-class-name=\"%s\", " | |
246 | "event-class-id=%" PRId64, | |
247 | event, | |
248 | bt_event_class_common_get_name(event->common.class), | |
249 | bt_event_class_common_get_id(event->common.class)); | |
250 | goto end; | |
251 | } | |
252 | } | |
253 | ||
254 | end: | |
255 | return ret; | |
256 | } | |
257 | ||
258 | BT_HIDDEN | |
259 | void _bt_ctf_event_freeze(struct bt_ctf_event *event) | |
260 | { | |
261 | _bt_event_common_freeze(BT_TO_COMMON(event)); | |
262 | } |