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