Commit | Line | Data |
---|---|---|
3dca2276 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 | ||
16ca5ff0 PP |
27 | #include <babeltrace/assert-pre-internal.h> |
28 | #include <babeltrace/assert-internal.h> | |
29 | #include <babeltrace/compiler-internal.h> | |
30 | #include <babeltrace/ctf-writer/attributes-internal.h> | |
31 | #include <babeltrace/ctf-writer/clock-class-internal.h> | |
3dca2276 | 32 | #include <babeltrace/ctf-writer/clock-internal.h> |
16ca5ff0 | 33 | #include <babeltrace/ctf-writer/event-class-internal.h> |
3dca2276 PP |
34 | #include <babeltrace/ctf-writer/event-internal.h> |
35 | #include <babeltrace/ctf-writer/event.h> | |
3dca2276 PP |
36 | #include <babeltrace/ctf-writer/field-types-internal.h> |
37 | #include <babeltrace/ctf-writer/field-types.h> | |
16ca5ff0 | 38 | #include <babeltrace/ctf-writer/fields-internal.h> |
3dca2276 PP |
39 | #include <babeltrace/ctf-writer/fields.h> |
40 | #include <babeltrace/ctf-writer/stream-class-internal.h> | |
41 | #include <babeltrace/ctf-writer/stream-class.h> | |
16ca5ff0 | 42 | #include <babeltrace/ctf-writer/stream-internal.h> |
3dca2276 PP |
43 | #include <babeltrace/ctf-writer/trace-internal.h> |
44 | #include <babeltrace/ctf-writer/trace.h> | |
16ca5ff0 PP |
45 | #include <babeltrace/ctf-writer/utils.h> |
46 | #include <babeltrace/ctf-writer/validation-internal.h> | |
e1e02a22 | 47 | #include <babeltrace/ctf-writer/object.h> |
3dca2276 PP |
48 | #include <inttypes.h> |
49 | ||
50 | static | |
16ca5ff0 PP |
51 | int bt_ctf_event_common_validate_types_for_create( |
52 | struct bt_ctf_event_class_common *event_class, | |
53 | struct bt_ctf_validation_output *validation_output, | |
54 | bt_ctf_validation_flag_copy_field_type_func copy_field_type_func) | |
55 | { | |
56 | int ret; | |
57 | enum bt_ctf_validation_flag validation_flags = | |
58 | BT_CTF_VALIDATION_FLAG_STREAM | | |
59 | BT_CTF_VALIDATION_FLAG_EVENT; | |
60 | struct bt_ctf_trace_common *trace = NULL; | |
61 | struct bt_ctf_stream_class_common *stream_class = NULL; | |
62 | struct bt_ctf_field_type_common *packet_header_type = NULL; | |
63 | struct bt_ctf_field_type_common *packet_context_type = NULL; | |
64 | struct bt_ctf_field_type_common *event_header_type = NULL; | |
65 | struct bt_ctf_field_type_common *stream_event_ctx_type = NULL; | |
66 | struct bt_ctf_field_type_common *event_context_type = NULL; | |
67 | struct bt_ctf_field_type_common *event_payload_type = NULL; | |
68 | int trace_valid = 0; | |
e1e02a22 | 69 | struct bt_ctf_private_value *environment = NULL; |
16ca5ff0 PP |
70 | |
71 | stream_class = bt_ctf_event_class_common_borrow_stream_class(event_class); | |
72 | BT_ASSERT(stream_class); | |
73 | trace = bt_ctf_stream_class_common_borrow_trace(stream_class); | |
74 | if (trace) { | |
75 | BT_LOGD_STR("Event class is part of a trace."); | |
76 | packet_header_type = | |
77 | bt_ctf_trace_common_borrow_packet_header_field_type(trace); | |
78 | trace_valid = trace->valid; | |
79 | BT_ASSERT(trace_valid); | |
80 | environment = trace->environment; | |
81 | } | |
82 | ||
83 | packet_context_type = | |
84 | bt_ctf_stream_class_common_borrow_packet_context_field_type( | |
85 | stream_class); | |
86 | event_header_type = | |
87 | bt_ctf_stream_class_common_borrow_event_header_field_type( | |
88 | stream_class); | |
89 | stream_event_ctx_type = | |
90 | bt_ctf_stream_class_common_borrow_event_context_field_type( | |
91 | stream_class); | |
92 | event_context_type = | |
93 | bt_ctf_event_class_common_borrow_context_field_type(event_class); | |
94 | event_payload_type = | |
95 | bt_ctf_event_class_common_borrow_payload_field_type(event_class); | |
96 | ret = bt_ctf_validate_class_types(environment, packet_header_type, | |
97 | packet_context_type, event_header_type, stream_event_ctx_type, | |
98 | event_context_type, event_payload_type, trace_valid, | |
99 | stream_class->valid, event_class->valid, | |
100 | validation_output, validation_flags, copy_field_type_func); | |
101 | if (ret) { | |
102 | /* | |
103 | * This means something went wrong during the validation | |
104 | * process, not that the objects are invalid. | |
105 | */ | |
106 | BT_LOGE("Failed to validate event and parents: ret=%d", ret); | |
107 | goto error; | |
108 | } | |
109 | ||
110 | if ((validation_output->valid_flags & validation_flags) != | |
111 | validation_flags) { | |
112 | /* Invalid trace/stream class/event class */ | |
113 | BT_LOGW("Invalid trace, stream class, or event class: " | |
114 | "valid-flags=0x%x", validation_output->valid_flags); | |
115 | goto error; | |
116 | } | |
117 | ||
118 | goto end; | |
119 | ||
120 | error: | |
121 | bt_ctf_validation_output_put_types(validation_output); | |
122 | ret = -1; | |
123 | ||
124 | end: | |
125 | return ret; | |
126 | } | |
3dca2276 PP |
127 | |
128 | static | |
16ca5ff0 PP |
129 | int bt_ctf_event_common_create_fields( |
130 | struct bt_ctf_stream_class_common *stream_class, | |
131 | struct bt_ctf_validation_output *validation_output, | |
132 | create_field_func create_field_func, | |
133 | release_field_func release_field_func, | |
134 | create_header_field_func create_header_field_func, | |
135 | release_header_field_func release_header_field_func, | |
136 | struct bt_ctf_field_wrapper **header_field, | |
137 | struct bt_ctf_field_common **stream_event_context_field, | |
138 | struct bt_ctf_field_common **context_field, | |
139 | struct bt_ctf_field_common **payload_field) | |
140 | { | |
141 | int ret = 0; | |
142 | ||
143 | if (validation_output->event_header_type) { | |
144 | BT_LOGD("Creating initial event header field: ft-addr=%p", | |
145 | validation_output->event_header_type); | |
146 | *header_field = | |
147 | create_header_field_func(stream_class, | |
148 | validation_output->event_header_type); | |
149 | if (!*header_field) { | |
150 | BT_LOGE_STR("Cannot create initial event header field object."); | |
151 | goto error; | |
152 | } | |
153 | } | |
154 | ||
155 | if (validation_output->stream_event_ctx_type) { | |
156 | BT_LOGD("Creating initial stream event context field: ft-addr=%p", | |
157 | validation_output->stream_event_ctx_type); | |
158 | *stream_event_context_field = create_field_func( | |
159 | validation_output->stream_event_ctx_type); | |
160 | if (!*stream_event_context_field) { | |
161 | BT_LOGE_STR("Cannot create initial stream event context field object."); | |
162 | goto error; | |
163 | } | |
164 | } | |
165 | ||
166 | if (validation_output->event_context_type) { | |
167 | BT_LOGD("Creating initial event context field: ft-addr=%p", | |
168 | validation_output->event_context_type); | |
169 | *context_field = create_field_func( | |
170 | validation_output->event_context_type); | |
171 | if (!*context_field) { | |
172 | BT_LOGE_STR("Cannot create initial event context field object."); | |
173 | goto error; | |
174 | } | |
175 | } | |
176 | ||
177 | if (validation_output->event_payload_type) { | |
178 | BT_LOGD("Creating initial event payload field: ft-addr=%p", | |
179 | validation_output->event_payload_type); | |
180 | *payload_field = create_field_func( | |
181 | validation_output->event_payload_type); | |
182 | if (!*payload_field) { | |
183 | BT_LOGE_STR("Cannot create initial event payload field object."); | |
184 | goto error; | |
185 | } | |
186 | } | |
187 | ||
188 | goto end; | |
189 | ||
190 | error: | |
191 | if (*header_field) { | |
192 | release_header_field_func(*header_field, stream_class); | |
193 | } | |
194 | ||
195 | if (*stream_event_context_field) { | |
196 | release_field_func(*stream_event_context_field); | |
197 | } | |
198 | ||
199 | if (*context_field) { | |
200 | release_field_func(*context_field); | |
201 | } | |
202 | ||
203 | if (*payload_field) { | |
204 | release_field_func(*payload_field); | |
205 | } | |
206 | ||
207 | ret = -1; | |
208 | ||
209 | end: | |
210 | return ret; | |
211 | } | |
212 | ||
213 | BT_HIDDEN | |
214 | int _bt_ctf_event_common_validate(struct bt_ctf_event_common *event) | |
215 | { | |
216 | int ret = 0; | |
217 | struct bt_ctf_stream_class_common *stream_class; | |
218 | ||
219 | BT_ASSERT(event); | |
220 | if (event->header_field) { | |
221 | ret = bt_ctf_field_common_validate_recursive( | |
222 | event->header_field->field); | |
223 | if (ret) { | |
224 | BT_ASSERT_PRE_MSG("Invalid event's header field: " | |
225 | "event-addr=%p, field-addr=%p", | |
226 | event, event->header_field->field); | |
227 | goto end; | |
228 | } | |
229 | } | |
230 | ||
231 | stream_class = bt_ctf_event_class_common_borrow_stream_class(event->class); | |
232 | ||
233 | /* | |
234 | * We should not have been able to create the event without associating | |
235 | * the event class to a stream class. | |
236 | */ | |
237 | BT_ASSERT(stream_class); | |
238 | ||
239 | if (stream_class->event_context_field_type) { | |
240 | ret = bt_ctf_field_common_validate_recursive( | |
241 | event->stream_event_context_field); | |
242 | if (ret) { | |
243 | BT_ASSERT_PRE_MSG("Invalid event's stream event context field: " | |
244 | "event-addr=%p, field-addr=%p", | |
245 | event, event->stream_event_context_field); | |
246 | goto end; | |
247 | } | |
248 | } | |
249 | ||
250 | if (event->class->context_field_type) { | |
251 | ret = bt_ctf_field_common_validate_recursive(event->context_field); | |
252 | if (ret) { | |
253 | BT_ASSERT_PRE_MSG("Invalid event's payload field: " | |
254 | "event-addr=%p, field-addr=%p", | |
255 | event, event->context_field); | |
256 | goto end; | |
257 | } | |
258 | } | |
259 | ||
260 | ret = bt_ctf_field_common_validate_recursive(event->payload_field); | |
261 | if (ret) { | |
262 | BT_ASSERT_PRE_MSG("Invalid event's payload field: " | |
263 | "event-addr=%p, field-addr=%p", | |
264 | event, event->payload_field); | |
265 | goto end; | |
266 | } | |
267 | ||
268 | end: | |
269 | return ret; | |
270 | } | |
271 | ||
272 | BT_HIDDEN | |
273 | void _bt_ctf_event_common_set_is_frozen(struct bt_ctf_event_common *event, | |
274 | bool is_frozen) | |
275 | { | |
276 | BT_ASSERT(event); | |
277 | BT_LOGD("Freezing event: addr=%p, " | |
278 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
279 | event, bt_ctf_event_class_common_get_name(event->class), | |
280 | bt_ctf_event_class_common_get_id(event->class)); | |
281 | ||
282 | if (event->header_field) { | |
283 | BT_LOGD_STR("Freezing event's header field."); | |
284 | bt_ctf_field_common_set_is_frozen_recursive( | |
285 | event->header_field->field, is_frozen); | |
286 | } | |
287 | ||
288 | if (event->stream_event_context_field) { | |
289 | BT_LOGD_STR("Freezing event's stream event context field."); | |
290 | bt_ctf_field_common_set_is_frozen_recursive( | |
291 | event->stream_event_context_field, is_frozen); | |
292 | } | |
293 | ||
294 | if (event->context_field) { | |
295 | BT_LOGD_STR("Freezing event's context field."); | |
296 | bt_ctf_field_common_set_is_frozen_recursive(event->context_field, | |
297 | is_frozen); | |
298 | } | |
299 | ||
300 | if (event->payload_field) { | |
301 | BT_LOGD_STR("Freezing event's payload field."); | |
302 | bt_ctf_field_common_set_is_frozen_recursive(event->payload_field, | |
303 | is_frozen); | |
304 | } | |
305 | ||
306 | event->frozen = is_frozen; | |
307 | } | |
308 | ||
309 | BT_HIDDEN | |
310 | int bt_ctf_event_common_initialize(struct bt_ctf_event_common *event, | |
311 | struct bt_ctf_event_class_common *event_class, | |
312 | struct bt_ctf_clock_class *init_expected_clock_class, | |
e1e02a22 | 313 | bool is_shared_with_parent, bt_ctf_object_release_func release_func, |
16ca5ff0 PP |
314 | bt_ctf_validation_flag_copy_field_type_func field_type_copy_func, |
315 | bool must_be_in_trace, | |
316 | int (*map_clock_classes_func)(struct bt_ctf_stream_class_common *stream_class, | |
317 | struct bt_ctf_field_type_common *packet_context_field_type, | |
318 | struct bt_ctf_field_type_common *event_header_field_type), | |
319 | create_field_func create_field_func, | |
320 | release_field_func release_field_func, | |
321 | create_header_field_func create_header_field_func, | |
322 | release_header_field_func release_header_field_func) | |
323 | { | |
324 | int ret; | |
325 | struct bt_ctf_trace_common *trace = NULL; | |
326 | struct bt_ctf_stream_class_common *stream_class = NULL; | |
327 | struct bt_ctf_field_wrapper *event_header = NULL; | |
328 | struct bt_ctf_field_common *stream_event_context = NULL; | |
329 | struct bt_ctf_field_common *event_context = NULL; | |
330 | struct bt_ctf_field_common *event_payload = NULL; | |
331 | struct bt_ctf_validation_output validation_output = { 0 }; | |
332 | struct bt_ctf_clock_class *expected_clock_class = | |
e1e02a22 | 333 | init_expected_clock_class ? bt_ctf_object_get_ref(init_expected_clock_class) : |
16ca5ff0 PP |
334 | NULL; |
335 | ||
336 | BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); | |
337 | BT_LOGD("Initializing common event object: event-class-addr=%p, " | |
338 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
339 | event_class, bt_ctf_event_class_common_get_name(event_class), | |
340 | bt_ctf_event_class_common_get_id(event_class)); | |
341 | ||
342 | stream_class = bt_ctf_event_class_common_borrow_stream_class(event_class); | |
343 | BT_ASSERT_PRE(stream_class, | |
344 | "Event class is not part of a stream class: event-class-addr=%p", | |
345 | event_class); | |
346 | ||
347 | /* The event class was frozen when added to its stream class */ | |
348 | BT_ASSERT(event_class->frozen); | |
349 | trace = bt_ctf_stream_class_common_borrow_trace(stream_class); | |
350 | ||
351 | if (must_be_in_trace) { | |
352 | BT_ASSERT_PRE(trace, | |
353 | "Event class's stream class is not part of a trace: " | |
354 | "ec-addr=%p, sc-addr=%p", event_class, stream_class); | |
355 | } | |
356 | ||
357 | /* | |
358 | * This must be called before anything that can fail because on | |
359 | * failure, the caller releases the reference to `event` to | |
360 | * destroy it. | |
361 | */ | |
362 | if (is_shared_with_parent) { | |
e1e02a22 | 363 | bt_ctf_object_init_shared_with_parent(&event->base, release_func); |
16ca5ff0 | 364 | } else { |
e1e02a22 | 365 | bt_ctf_object_init_unique(&event->base); |
16ca5ff0 PP |
366 | } |
367 | ||
368 | if (!stream_class->frozen) { | |
369 | /* | |
370 | * Because this function freezes the stream class, | |
371 | * validate that this stream class contains at most a | |
372 | * single clock class so that we set its expected clock | |
373 | * class for future checks. | |
374 | */ | |
375 | ret = bt_ctf_stream_class_common_validate_single_clock_class( | |
376 | stream_class, &expected_clock_class); | |
377 | if (ret) { | |
378 | BT_LOGW("Event class's stream class or one of its event " | |
379 | "classes contains a field type which is not " | |
380 | "recursively mapped to the expected " | |
381 | "clock class: " | |
382 | "stream-class-addr=%p, " | |
383 | "stream-class-id=%" PRId64 ", " | |
384 | "stream-class-name=\"%s\", " | |
385 | "expected-clock-class-addr=%p, " | |
386 | "expected-clock-class-name=\"%s\"", | |
387 | stream_class, | |
388 | bt_ctf_stream_class_common_get_id(stream_class), | |
389 | bt_ctf_stream_class_common_get_name(stream_class), | |
390 | expected_clock_class, | |
391 | expected_clock_class ? | |
392 | bt_ctf_clock_class_get_name(expected_clock_class) : | |
393 | NULL); | |
394 | goto error; | |
395 | } | |
396 | } | |
397 | ||
398 | /* Validate the trace, the stream class, and the event class */ | |
399 | ret = bt_ctf_event_common_validate_types_for_create( | |
400 | event_class, &validation_output, field_type_copy_func); | |
401 | if (ret) { | |
402 | /* bt_ctf_event_common_validate_types_for_create() logs errors */ | |
403 | goto error; | |
404 | } | |
405 | ||
406 | if (map_clock_classes_func) { | |
407 | /* | |
408 | * Safe to automatically map selected fields to the | |
409 | * stream's clock's class here because the stream class | |
410 | * is about to be frozen. | |
411 | */ | |
412 | if (map_clock_classes_func(stream_class, | |
413 | validation_output.packet_context_type, | |
414 | validation_output.event_header_type)) { | |
415 | BT_LOGW_STR("Cannot automatically map selected stream class's " | |
416 | "field types to stream class's clock's class."); | |
417 | goto error; | |
418 | } | |
419 | } | |
420 | ||
421 | /* | |
422 | * event does not share a common ancestor with the event class; it has | |
423 | * to guarantee its existence by holding a reference. This reference | |
424 | * shall be released once the event is associated to a stream since, | |
425 | * from that point, the event and its class will share the same | |
426 | * lifetime. | |
427 | */ | |
e1e02a22 | 428 | event->class = bt_ctf_object_get_ref(event_class); |
16ca5ff0 PP |
429 | |
430 | ret = bt_ctf_event_common_create_fields(stream_class, | |
431 | &validation_output, | |
432 | create_field_func, release_field_func, | |
433 | create_header_field_func, release_header_field_func, | |
434 | &event_header, &stream_event_context, &event_context, | |
435 | &event_payload); | |
436 | if (ret) { | |
437 | /* bt_ctf_event_common_create_fields() logs errors */ | |
438 | goto error; | |
439 | } | |
440 | ||
441 | /* | |
442 | * At this point all the fields are created, potentially from | |
443 | * validated copies of field types, so that the field types and | |
444 | * fields can be replaced in the trace, stream class, | |
445 | * event class, and created event. | |
446 | */ | |
447 | bt_ctf_validation_replace_types(trace, stream_class, event_class, | |
448 | &validation_output, | |
449 | BT_CTF_VALIDATION_FLAG_STREAM | BT_CTF_VALIDATION_FLAG_EVENT); | |
450 | event->header_field = event_header; | |
451 | event_header = NULL; | |
452 | event->stream_event_context_field = stream_event_context; | |
453 | stream_event_context = NULL; | |
454 | event->context_field = event_context; | |
455 | event_context = NULL; | |
456 | event->payload_field = event_payload; | |
457 | event_payload = NULL; | |
458 | ||
459 | /* | |
460 | * Put what was not moved in bt_ctf_validation_replace_types(). | |
461 | */ | |
462 | bt_ctf_validation_output_put_types(&validation_output); | |
463 | ||
464 | /* | |
465 | * Freeze the stream class since the event header must not be changed | |
466 | * anymore. | |
467 | */ | |
468 | bt_ctf_stream_class_common_freeze(stream_class); | |
469 | ||
470 | /* | |
471 | * It is safe to set the stream class's unique clock class | |
472 | * now because the stream class is frozen. | |
473 | */ | |
474 | if (expected_clock_class) { | |
e1e02a22 | 475 | BT_CTF_OBJECT_MOVE_REF(stream_class->clock_class, expected_clock_class); |
16ca5ff0 PP |
476 | } |
477 | ||
478 | /* | |
479 | * Mark stream class, and event class as valid since | |
480 | * they're all frozen now. | |
481 | */ | |
482 | stream_class->valid = 1; | |
483 | event_class->valid = 1; | |
484 | ||
485 | /* Put stuff we borrowed from the event class */ | |
486 | BT_LOGD("Initialized event object: addr=%p, event-class-name=\"%s\", " | |
487 | "event-class-id=%" PRId64, | |
488 | event, bt_ctf_event_class_common_get_name(event->class), | |
489 | bt_ctf_event_class_common_get_id(event->class)); | |
490 | goto end; | |
491 | ||
492 | error: | |
493 | bt_ctf_validation_output_put_types(&validation_output); | |
e1e02a22 | 494 | bt_ctf_object_put_ref(expected_clock_class); |
16ca5ff0 PP |
495 | |
496 | if (event_header) { | |
497 | release_header_field_func(event_header, stream_class); | |
498 | } | |
499 | ||
500 | if (stream_event_context) { | |
501 | release_field_func(stream_event_context); | |
502 | } | |
503 | ||
504 | if (event_context) { | |
505 | release_field_func(event_context); | |
506 | } | |
507 | ||
508 | if (event_payload) { | |
509 | release_field_func(event_payload); | |
510 | } | |
511 | ||
512 | ret = -1; | |
513 | ||
514 | end: | |
515 | return ret; | |
516 | } | |
517 | ||
518 | int map_clock_classes_func(struct bt_ctf_stream_class_common *stream_class, | |
519 | struct bt_ctf_field_type_common *packet_context_type, | |
520 | struct bt_ctf_field_type_common *event_header_type) | |
3dca2276 PP |
521 | { |
522 | int ret = bt_ctf_stream_class_map_clock_class( | |
16ca5ff0 PP |
523 | BT_CTF_FROM_COMMON(stream_class), |
524 | BT_CTF_FROM_COMMON(packet_context_type), | |
525 | BT_CTF_FROM_COMMON(event_header_type)); | |
3dca2276 PP |
526 | |
527 | if (ret) { | |
528 | BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class."); | |
529 | } | |
530 | ||
531 | return ret; | |
532 | } | |
533 | ||
312c056a | 534 | static |
16ca5ff0 | 535 | void destroy_event_header_field(struct bt_ctf_field_wrapper *field_wrapper) |
312c056a PP |
536 | { |
537 | BT_ASSERT(field_wrapper); | |
e1e02a22 | 538 | bt_ctf_object_put_ref(field_wrapper->field); |
16ca5ff0 | 539 | bt_ctf_field_wrapper_destroy(field_wrapper); |
312c056a PP |
540 | } |
541 | ||
542 | static | |
16ca5ff0 PP |
543 | struct bt_ctf_field_wrapper *create_event_header_field( |
544 | struct bt_ctf_stream_class *stream_class, | |
545 | struct bt_ctf_field_type_common *ft) | |
312c056a | 546 | { |
16ca5ff0 | 547 | struct bt_ctf_field_wrapper *field_wrapper = NULL; |
312c056a PP |
548 | struct bt_ctf_field *field = bt_ctf_field_create((void *) ft); |
549 | ||
550 | if (!field) { | |
551 | goto error; | |
552 | } | |
553 | ||
16ca5ff0 | 554 | field_wrapper = bt_ctf_field_wrapper_new(NULL); |
312c056a PP |
555 | if (!field_wrapper) { |
556 | goto error; | |
557 | } | |
558 | ||
559 | field_wrapper->field = (void *) field; | |
560 | field = NULL; | |
561 | goto end; | |
562 | ||
563 | error: | |
e1e02a22 | 564 | bt_ctf_object_put_ref(field); |
312c056a PP |
565 | |
566 | if (field_wrapper) { | |
567 | destroy_event_header_field(field_wrapper); | |
568 | field_wrapper = NULL; | |
569 | } | |
570 | ||
571 | end: | |
572 | return field_wrapper; | |
573 | } | |
574 | ||
16ca5ff0 PP |
575 | static |
576 | void release_event_header_field(struct bt_ctf_field_wrapper *field_wrapper, | |
577 | struct bt_ctf_event_common *event_common) | |
578 | { | |
579 | BT_ASSERT(field_wrapper); | |
e1e02a22 | 580 | BT_CTF_OBJECT_PUT_REF_AND_RESET(field_wrapper->field); |
16ca5ff0 PP |
581 | bt_ctf_field_wrapper_destroy(field_wrapper); |
582 | } | |
583 | ||
584 | static | |
e1e02a22 | 585 | void bt_ctf_event_destroy(struct bt_ctf_object *obj) |
16ca5ff0 | 586 | { |
e1e02a22 | 587 | bt_ctf_event_common_finalize(obj, (void *) bt_ctf_object_put_ref, |
16ca5ff0 PP |
588 | (void *) release_event_header_field); |
589 | g_free(obj); | |
590 | } | |
591 | ||
3dca2276 PP |
592 | struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) |
593 | { | |
594 | int ret; | |
595 | struct bt_ctf_event *event = NULL; | |
16ca5ff0 | 596 | struct bt_ctf_clock_class *expected_clock_class = NULL; |
3dca2276 PP |
597 | |
598 | event = g_new0(struct bt_ctf_event, 1); | |
599 | if (!event) { | |
600 | BT_LOGE_STR("Failed to allocate one CTF writer event."); | |
601 | goto error; | |
602 | } | |
603 | ||
604 | if (event_class) { | |
605 | struct bt_ctf_stream_class *stream_class = | |
16ca5ff0 PP |
606 | BT_CTF_FROM_COMMON(bt_ctf_event_class_common_borrow_stream_class( |
607 | BT_CTF_TO_COMMON(event_class))); | |
3dca2276 PP |
608 | |
609 | if (stream_class && stream_class->clock) { | |
16ca5ff0 | 610 | expected_clock_class = stream_class->clock->clock_class; |
3dca2276 PP |
611 | } |
612 | } | |
613 | ||
16ca5ff0 PP |
614 | ret = bt_ctf_event_common_initialize(BT_CTF_TO_COMMON(event), |
615 | BT_CTF_TO_COMMON(event_class), expected_clock_class, | |
3fea54f6 | 616 | true, bt_ctf_event_destroy, |
16ca5ff0 | 617 | (bt_ctf_validation_flag_copy_field_type_func) |
3dca2276 PP |
618 | bt_ctf_field_type_copy, |
619 | false, map_clock_classes_func, | |
2dd764c1 | 620 | (create_field_func) bt_ctf_field_create, |
e1e02a22 | 621 | (release_field_func) bt_ctf_object_put_ref, |
2dd764c1 PP |
622 | (create_header_field_func) create_event_header_field, |
623 | (release_header_field_func) destroy_event_header_field); | |
3dca2276 | 624 | if (ret) { |
16ca5ff0 | 625 | /* bt_ctf_event_common_initialize() logs errors */ |
3dca2276 PP |
626 | goto error; |
627 | } | |
628 | ||
629 | goto end; | |
630 | ||
631 | error: | |
e1e02a22 | 632 | BT_CTF_OBJECT_PUT_REF_AND_RESET(event); |
3dca2276 PP |
633 | |
634 | end: | |
635 | return event; | |
636 | } | |
637 | ||
638 | struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event) | |
639 | { | |
640 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
e1e02a22 | 641 | return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event))); |
3dca2276 PP |
642 | } |
643 | ||
644 | BT_HIDDEN | |
645 | struct bt_ctf_stream *bt_ctf_event_borrow_stream(struct bt_ctf_event *event) | |
646 | { | |
647 | BT_ASSERT(event); | |
648 | return (struct bt_ctf_stream *) | |
e1e02a22 | 649 | bt_ctf_object_borrow_parent(&BT_CTF_TO_COMMON(event)->base); |
3dca2276 PP |
650 | } |
651 | ||
652 | struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event) | |
653 | { | |
654 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
e1e02a22 | 655 | return bt_ctf_object_get_ref(bt_ctf_event_borrow_stream(event)); |
3dca2276 PP |
656 | } |
657 | ||
658 | int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, | |
659 | struct bt_ctf_field *field) | |
660 | { | |
661 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
662 | BT_ASSERT_PRE_NON_NULL(field, "Payload field"); | |
16ca5ff0 | 663 | BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event"); |
3dca2276 | 664 | return bt_ctf_field_structure_set_field_by_name( |
312c056a | 665 | (void *) event->common.payload_field, name, field); |
3dca2276 PP |
666 | } |
667 | ||
668 | struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, | |
669 | const char *name) | |
670 | { | |
671 | struct bt_ctf_field *field = NULL; | |
672 | ||
673 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
674 | ||
675 | if (name) { | |
676 | field = bt_ctf_field_structure_get_field_by_name( | |
16ca5ff0 | 677 | BT_CTF_FROM_COMMON(event->common.payload_field), name); |
3dca2276 | 678 | } else { |
16ca5ff0 | 679 | field = BT_CTF_FROM_COMMON(event->common.payload_field); |
e1e02a22 | 680 | bt_ctf_object_get_ref(field); |
3dca2276 PP |
681 | } |
682 | ||
683 | return field; | |
684 | } | |
685 | ||
686 | struct bt_ctf_field *bt_ctf_event_get_payload_field( | |
687 | struct bt_ctf_event *event) | |
688 | { | |
e1e02a22 | 689 | return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_payload(BT_CTF_TO_COMMON(event))); |
3dca2276 PP |
690 | } |
691 | ||
3dca2276 PP |
692 | struct bt_ctf_field *bt_ctf_event_get_header(struct bt_ctf_event *event) |
693 | { | |
e1e02a22 | 694 | return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_header(BT_CTF_TO_COMMON(event))); |
3dca2276 PP |
695 | } |
696 | ||
3dca2276 PP |
697 | struct bt_ctf_field *bt_ctf_event_get_context(struct bt_ctf_event *event) |
698 | { | |
e1e02a22 | 699 | return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_context(BT_CTF_TO_COMMON(event))); |
3dca2276 PP |
700 | } |
701 | ||
3dca2276 PP |
702 | struct bt_ctf_field *bt_ctf_event_get_stream_event_context( |
703 | struct bt_ctf_event *event) | |
704 | { | |
e1e02a22 | 705 | return bt_ctf_object_get_ref(bt_ctf_event_common_borrow_stream_event_context( |
16ca5ff0 | 706 | BT_CTF_TO_COMMON(event))); |
3dca2276 PP |
707 | } |
708 | ||
709 | BT_HIDDEN | |
710 | int bt_ctf_event_serialize(struct bt_ctf_event *event, | |
711 | struct bt_ctf_stream_pos *pos, | |
712 | enum bt_ctf_byte_order native_byte_order) | |
713 | { | |
714 | int ret = 0; | |
715 | ||
716 | BT_ASSERT(event); | |
717 | BT_ASSERT(pos); | |
718 | ||
719 | BT_LOGV_STR("Serializing event's context field."); | |
720 | if (event->common.context_field) { | |
721 | ret = bt_ctf_field_serialize_recursive( | |
722 | (void *) event->common.context_field, pos, | |
723 | native_byte_order); | |
724 | if (ret) { | |
725 | BT_LOGW("Cannot serialize event's context field: " | |
726 | "event-addr=%p, event-class-name=\"%s\", " | |
727 | "event-class-id=%" PRId64, | |
728 | event, | |
16ca5ff0 PP |
729 | bt_ctf_event_class_common_get_name(event->common.class), |
730 | bt_ctf_event_class_common_get_id(event->common.class)); | |
3dca2276 PP |
731 | goto end; |
732 | } | |
733 | } | |
734 | ||
735 | BT_LOGV_STR("Serializing event's payload field."); | |
736 | if (event->common.payload_field) { | |
737 | ret = bt_ctf_field_serialize_recursive( | |
738 | (void *) event->common.payload_field, pos, | |
739 | native_byte_order); | |
740 | if (ret) { | |
741 | BT_LOGW("Cannot serialize event's payload field: " | |
742 | "event-addr=%p, event-class-name=\"%s\", " | |
743 | "event-class-id=%" PRId64, | |
744 | event, | |
16ca5ff0 PP |
745 | bt_ctf_event_class_common_get_name(event->common.class), |
746 | bt_ctf_event_class_common_get_id(event->common.class)); | |
3dca2276 PP |
747 | goto end; |
748 | } | |
749 | } | |
750 | ||
751 | end: | |
752 | return ret; | |
753 | } | |
754 | ||
755 | BT_HIDDEN | |
756 | void _bt_ctf_event_freeze(struct bt_ctf_event *event) | |
757 | { | |
16ca5ff0 | 758 | _bt_ctf_event_common_set_is_frozen(BT_CTF_TO_COMMON(event), true); |
3dca2276 | 759 | } |
312c056a PP |
760 | |
761 | int bt_ctf_event_set_header(struct bt_ctf_event *event, | |
762 | struct bt_ctf_field *header) | |
763 | { | |
764 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
16ca5ff0 | 765 | BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event"); |
312c056a PP |
766 | |
767 | /* | |
768 | * Ensure the provided header's type matches the one registered to the | |
769 | * stream class. | |
770 | */ | |
771 | if (header) { | |
16ca5ff0 PP |
772 | BT_ASSERT_PRE(bt_ctf_field_type_common_compare( |
773 | ((struct bt_ctf_field_common *) header)->type, | |
774 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type) == 0, | |
312c056a | 775 | "Header field's type is different from the " |
16ca5ff0 PP |
776 | "expected field type: event-addr=%p, ft-addr=%p, " |
777 | "expected-ft-addr=%p", | |
778 | event, ((struct bt_ctf_field_common *) header)->type, | |
779 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type); | |
312c056a | 780 | } else { |
16ca5ff0 | 781 | BT_ASSERT_PRE(!bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type, |
312c056a PP |
782 | "Setting no event header field, " |
783 | "but event header field type is not NULL: ", | |
16ca5ff0 | 784 | "event-addr=%p, header-ft-addr=%p", |
312c056a | 785 | event, |
16ca5ff0 | 786 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_header_field_type); |
312c056a PP |
787 | } |
788 | ||
e1e02a22 PP |
789 | bt_ctf_object_put_ref(event->common.header_field->field); |
790 | event->common.header_field->field = bt_ctf_object_get_ref(header); | |
312c056a PP |
791 | BT_LOGV("Set event's header field: event-addr=%p, " |
792 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " | |
793 | "header-field-addr=%p", | |
16ca5ff0 PP |
794 | event, bt_ctf_event_class_common_get_name(event->common.class), |
795 | bt_ctf_event_class_common_get_id(event->common.class), header); | |
312c056a PP |
796 | return 0; |
797 | } | |
798 | ||
799 | int bt_ctf_event_common_set_payload(struct bt_ctf_event *event, | |
800 | struct bt_ctf_field *payload) | |
801 | { | |
802 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
16ca5ff0 | 803 | BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event"); |
312c056a PP |
804 | |
805 | if (payload) { | |
16ca5ff0 PP |
806 | BT_ASSERT_PRE(bt_ctf_field_type_common_compare( |
807 | ((struct bt_ctf_field_common *) payload)->type, | |
312c056a PP |
808 | event->common.class->payload_field_type) == 0, |
809 | "Payload field's type is different from the " | |
16ca5ff0 PP |
810 | "expected field type: event-addr=%p, ft-addr=%p, " |
811 | "expected-ft-addr=%p", | |
312c056a | 812 | event, |
16ca5ff0 | 813 | ((struct bt_ctf_field_common *) payload)->type, |
312c056a PP |
814 | event->common.class->payload_field_type); |
815 | } else { | |
816 | BT_ASSERT_PRE(!event->common.class->payload_field_type, | |
817 | "Setting no event payload field, " | |
818 | "but event payload field type is not NULL: ", | |
16ca5ff0 | 819 | "event-addr=%p, payload-ft-addr=%p", |
312c056a PP |
820 | event, event->common.class->payload_field_type); |
821 | } | |
822 | ||
e1e02a22 PP |
823 | bt_ctf_object_put_ref(event->common.payload_field); |
824 | event->common.payload_field = bt_ctf_object_get_ref(payload); | |
312c056a PP |
825 | BT_LOGV("Set event's payload field: event-addr=%p, " |
826 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " | |
827 | "payload-field-addr=%p", | |
16ca5ff0 PP |
828 | event, bt_ctf_event_class_common_get_name(event->common.class), |
829 | bt_ctf_event_class_common_get_id(event->common.class), payload); | |
312c056a PP |
830 | return 0; |
831 | } | |
832 | ||
833 | int bt_ctf_event_set_context(struct bt_ctf_event *event, | |
834 | struct bt_ctf_field *context) | |
835 | { | |
836 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
16ca5ff0 | 837 | BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event"); |
312c056a PP |
838 | |
839 | if (context) { | |
16ca5ff0 PP |
840 | BT_ASSERT_PRE(bt_ctf_field_type_common_compare( |
841 | ((struct bt_ctf_field_common *) context)->type, | |
312c056a PP |
842 | event->common.class->context_field_type) == 0, |
843 | "Context field's type is different from the " | |
16ca5ff0 PP |
844 | "expected field type: event-addr=%p, ft-addr=%p, " |
845 | "expected-ft-addr=%p", | |
846 | event, ((struct bt_ctf_field_common *) context)->type, | |
312c056a PP |
847 | event->common.class->context_field_type); |
848 | } else { | |
849 | BT_ASSERT_PRE(!event->common.class->context_field_type, | |
850 | "Setting no event context field, " | |
851 | "but event context field type is not NULL: ", | |
16ca5ff0 | 852 | "event-addr=%p, context-ft-addr=%p", |
312c056a PP |
853 | event, event->common.class->context_field_type); |
854 | } | |
855 | ||
e1e02a22 PP |
856 | bt_ctf_object_put_ref(event->common.context_field); |
857 | event->common.context_field = bt_ctf_object_get_ref(context); | |
312c056a PP |
858 | BT_LOGV("Set event's context field: event-addr=%p, " |
859 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " | |
860 | "context-field-addr=%p", | |
16ca5ff0 PP |
861 | event, bt_ctf_event_class_common_get_name(event->common.class), |
862 | bt_ctf_event_class_common_get_id(event->common.class), context); | |
312c056a PP |
863 | return 0; |
864 | } | |
865 | ||
866 | int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event, | |
867 | struct bt_ctf_field *stream_event_context) | |
868 | { | |
869 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
16ca5ff0 | 870 | BT_ASSERT_PRE_EVENT_COMMON_HOT(BT_CTF_TO_COMMON(event), "Event"); |
312c056a PP |
871 | |
872 | if (stream_event_context) { | |
16ca5ff0 PP |
873 | BT_ASSERT_PRE(bt_ctf_field_type_common_compare( |
874 | ((struct bt_ctf_field_common *) stream_event_context)->type, | |
875 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type) == 0, | |
312c056a | 876 | "Stream event context field's type is different from the " |
16ca5ff0 PP |
877 | "expected field type: event-addr=%p, ft-addr=%p, " |
878 | "expected-ft-addr=%p", | |
312c056a | 879 | event, |
16ca5ff0 PP |
880 | ((struct bt_ctf_field_common *) stream_event_context)->type, |
881 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type); | |
312c056a | 882 | } else { |
16ca5ff0 | 883 | BT_ASSERT_PRE(!bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type, |
312c056a PP |
884 | "Setting no stream event context field, " |
885 | "but stream event context field type is not NULL: ", | |
16ca5ff0 | 886 | "event-addr=%p, context-ft-addr=%p", |
312c056a | 887 | event, |
16ca5ff0 | 888 | bt_ctf_event_class_common_borrow_stream_class(event->common.class)->event_context_field_type); |
312c056a PP |
889 | } |
890 | ||
e1e02a22 PP |
891 | bt_ctf_object_put_ref(event->common.stream_event_context_field); |
892 | event->common.stream_event_context_field = bt_ctf_object_get_ref(stream_event_context); | |
312c056a PP |
893 | BT_LOGV("Set event's stream event context field: event-addr=%p, " |
894 | "event-class-name=\"%s\", event-class-id=%" PRId64 ", " | |
895 | "stream-event-context-field-addr=%p", | |
16ca5ff0 PP |
896 | event, bt_ctf_event_class_common_get_name(event->common.class), |
897 | bt_ctf_event_class_common_get_id(event->common.class), | |
312c056a PP |
898 | stream_event_context); |
899 | return 0; | |
900 | } |