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