assert-pre-internal.h: add BT_ASSERT_PRE_VALID_INDEX()
[babeltrace.git] / lib / ctf-ir / event.c
CommitLineData
273b65be
JG
1/*
2 * event.c
3 *
d2dc44b6 4 * Babeltrace CTF IR - Event
273b65be 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
7b010242
PP
29#define BT_LOG_TAG "EVENT"
30#include <babeltrace/lib-logging-internal.h>
31
3dca2276 32#include <babeltrace/assert-pre-internal.h>
2e33ac5a
PP
33#include <babeltrace/ctf-ir/fields-internal.h>
34#include <babeltrace/ctf-ir/field-types-internal.h>
ac0c6bdd 35#include <babeltrace/ctf-ir/clock-class.h>
c057dea0
PP
36#include <babeltrace/ctf-ir/clock-value.h>
37#include <babeltrace/ctf-ir/clock-value-internal.h>
7b010242 38#include <babeltrace/ctf-ir/clock-class-internal.h>
adc315b8 39#include <babeltrace/ctf-ir/event-internal.h>
272df73e
PP
40#include <babeltrace/ctf-ir/event-class.h>
41#include <babeltrace/ctf-ir/event-class-internal.h>
2f100782 42#include <babeltrace/ctf-ir/stream-class.h>
c35a1669 43#include <babeltrace/ctf-ir/stream-class-internal.h>
41ac640a 44#include <babeltrace/ctf-ir/stream-internal.h>
4ce9f9d0
PP
45#include <babeltrace/ctf-ir/packet.h>
46#include <babeltrace/ctf-ir/packet-internal.h>
3dca2276 47#include <babeltrace/ctf-ir/trace.h>
bc37ae52 48#include <babeltrace/ctf-ir/trace-internal.h>
09840de5 49#include <babeltrace/ctf-ir/validation-internal.h>
5c3b707d 50#include <babeltrace/ctf-ir/packet-internal.h>
654c1444 51#include <babeltrace/ctf-ir/utils.h>
83509119 52#include <babeltrace/ref.h>
44e0a4f5 53#include <babeltrace/ctf-ir/attributes-internal.h>
3d9990ac 54#include <babeltrace/compiler-internal.h>
f6ccaed9 55#include <babeltrace/assert-internal.h>
7b010242 56#include <inttypes.h>
273b65be 57
cb6f1f7d
PP
58static inline
59int bt_event_validate_types_for_create(
60 struct bt_event_class *event_class,
3dca2276
PP
61 struct bt_validation_output *validation_output,
62 bt_validation_flag_copy_field_type_func copy_field_type_func)
273b65be 63{
09840de5 64 int ret;
50842bdc
PP
65 enum bt_validation_flag validation_flags =
66 BT_VALIDATION_FLAG_STREAM |
67 BT_VALIDATION_FLAG_EVENT;
cb6f1f7d
PP
68 struct bt_trace *trace = NULL;
69 struct bt_stream_class *stream_class = NULL;
70 struct bt_field_type *packet_header_type = NULL;
71 struct bt_field_type *packet_context_type = NULL;
72 struct bt_field_type *event_header_type = NULL;
73 struct bt_field_type *stream_event_ctx_type = NULL;
74 struct bt_field_type *event_context_type = NULL;
75 struct bt_field_type *event_payload_type = NULL;
09840de5 76 int trace_valid = 0;
3dca2276 77 struct bt_value *environment = NULL;
2a3ced3c 78
cb6f1f7d 79 stream_class = bt_event_class_borrow_stream_class(event_class);
3dca2276 80 BT_ASSERT(stream_class);
cb6f1f7d 81 trace = bt_stream_class_borrow_trace(stream_class);
09840de5 82 if (trace) {
3dca2276 83 BT_LOGD_STR("Event class is part of a trace.");
094ff7c0 84 packet_header_type =
cb6f1f7d 85 bt_trace_borrow_packet_header_field_type(trace);
09840de5 86 trace_valid = trace->valid;
f6ccaed9 87 BT_ASSERT(trace_valid);
09840de5
PP
88 environment = trace->environment;
89 }
90
094ff7c0 91 packet_context_type =
cb6f1f7d 92 bt_stream_class_borrow_packet_context_field_type(
094ff7c0
PP
93 stream_class);
94 event_header_type =
cb6f1f7d 95 bt_stream_class_borrow_event_header_field_type(
094ff7c0
PP
96 stream_class);
97 stream_event_ctx_type =
cb6f1f7d 98 bt_stream_class_borrow_event_context_field_type(
094ff7c0
PP
99 stream_class);
100 event_context_type =
cb6f1f7d 101 bt_event_class_borrow_context_field_type(event_class);
094ff7c0 102 event_payload_type =
cb6f1f7d 103 bt_event_class_borrow_payload_field_type(event_class);
50842bdc 104 ret = bt_validate_class_types(environment, packet_header_type,
09840de5
PP
105 packet_context_type, event_header_type, stream_event_ctx_type,
106 event_context_type, event_payload_type, trace_valid,
107 stream_class->valid, event_class->valid,
3dca2276 108 validation_output, validation_flags, copy_field_type_func);
09840de5
PP
109 if (ret) {
110 /*
111 * This means something went wrong during the validation
112 * process, not that the objects are invalid.
113 */
e0bcd1b0 114 BT_LOGE("Failed to validate event and parents: ret=%d", ret);
09840de5
PP
115 goto error;
116 }
117
3dca2276 118 if ((validation_output->valid_flags & validation_flags) !=
09840de5
PP
119 validation_flags) {
120 /* Invalid trace/stream class/event class */
3ee09f3a 121 BT_LOGW("Invalid trace, stream class, or event class: "
3dca2276 122 "valid-flags=0x%x", validation_output->valid_flags);
09840de5
PP
123 goto error;
124 }
125
3dca2276 126 goto end;
0c1fafed 127
3dca2276
PP
128error:
129 bt_validation_output_put_types(validation_output);
130 ret = -1;
b8248cc0 131
3dca2276 132end:
3dca2276
PP
133 return ret;
134}
09840de5 135
3dca2276 136static
cb6f1f7d
PP
137int bt_event_create_fields(
138 struct bt_stream_class *stream_class,
3dca2276 139 struct bt_validation_output *validation_output,
2dd764c1
PP
140 create_field_func create_field_func,
141 release_field_func release_field_func,
142 create_header_field_func create_header_field_func,
143 release_header_field_func release_header_field_func,
312c056a 144 struct bt_field_wrapper **header_field,
cb6f1f7d
PP
145 struct bt_field **stream_event_context_field,
146 struct bt_field **context_field,
147 struct bt_field **payload_field)
3dca2276
PP
148{
149 int ret = 0;
be514b0c 150
3dca2276 151 if (validation_output->event_header_type) {
be514b0c 152 BT_LOGD("Creating initial event header field: ft-addr=%p",
3dca2276
PP
153 validation_output->event_header_type);
154 *header_field =
312c056a
PP
155 create_header_field_func(stream_class,
156 validation_output->event_header_type);
3dca2276 157 if (!*header_field) {
be514b0c
PP
158 BT_LOGE_STR("Cannot create initial event header field object.");
159 goto error;
160 }
662e778c 161 }
09840de5 162
3dca2276 163 if (validation_output->stream_event_ctx_type) {
be514b0c 164 BT_LOGD("Creating initial stream event context field: ft-addr=%p",
3dca2276
PP
165 validation_output->stream_event_ctx_type);
166 *stream_event_context_field = create_field_func(
167 validation_output->stream_event_ctx_type);
168 if (!*stream_event_context_field) {
7b010242 169 BT_LOGE_STR("Cannot create initial stream event context field object.");
5fd2e9fd
PP
170 goto error;
171 }
172 }
173
3dca2276 174 if (validation_output->event_context_type) {
be514b0c 175 BT_LOGD("Creating initial event context field: ft-addr=%p",
3dca2276
PP
176 validation_output->event_context_type);
177 *context_field = create_field_func(
178 validation_output->event_context_type);
179 if (!*context_field) {
7b010242 180 BT_LOGE_STR("Cannot create initial event context field object.");
83509119 181 goto error;
662e778c 182 }
f655a84d 183 }
09840de5 184
3dca2276 185 if (validation_output->event_payload_type) {
be514b0c 186 BT_LOGD("Creating initial event payload field: ft-addr=%p",
3dca2276
PP
187 validation_output->event_payload_type);
188 *payload_field = create_field_func(
189 validation_output->event_payload_type);
190 if (!*payload_field) {
7b010242 191 BT_LOGE_STR("Cannot create initial event payload field object.");
09840de5
PP
192 goto error;
193 }
662e778c
JG
194 }
195
3dca2276
PP
196 goto end;
197
198error:
312c056a 199 if (*header_field) {
2dd764c1 200 release_header_field_func(*header_field, stream_class);
312c056a
PP
201 }
202
203 if (*stream_event_context_field) {
204 release_field_func(*stream_event_context_field);
205 }
206
207 if (*context_field) {
208 release_field_func(*context_field);
209 }
210
211 if (*payload_field) {
212 release_field_func(*payload_field);
213 }
214
3dca2276
PP
215 ret = -1;
216
217end:
218 return ret;
219}
220
221BT_HIDDEN
cb6f1f7d 222int _bt_event_validate(struct bt_event *event)
3dca2276
PP
223{
224 int ret = 0;
cb6f1f7d 225 struct bt_stream_class *stream_class;
3dca2276
PP
226
227 BT_ASSERT(event);
228 if (event->header_field) {
cb6f1f7d 229 ret = bt_field_validate_recursive(
312c056a 230 event->header_field->field);
3dca2276
PP
231 if (ret) {
232 BT_ASSERT_PRE_MSG("Invalid event's header field: "
cb6f1f7d 233 "%![event-]+e, %![field-]+f",
312c056a 234 event, event->header_field->field);
3dca2276
PP
235 goto end;
236 }
237 }
238
cb6f1f7d 239 stream_class = bt_event_class_borrow_stream_class(event->class);
3dca2276
PP
240
241 /*
242 * We should not have been able to create the event without associating
243 * the event class to a stream class.
244 */
245 BT_ASSERT(stream_class);
246
247 if (stream_class->event_context_field_type) {
cb6f1f7d 248 ret = bt_field_validate_recursive(
3dca2276
PP
249 event->stream_event_context_field);
250 if (ret) {
251 BT_ASSERT_PRE_MSG("Invalid event's stream event context field: "
cb6f1f7d 252 "%![event-]+e, %![field-]+f",
3dca2276
PP
253 event, event->stream_event_context_field);
254 goto end;
255 }
256 }
257
258 if (event->class->context_field_type) {
cb6f1f7d 259 ret = bt_field_validate_recursive(event->context_field);
3dca2276
PP
260 if (ret) {
261 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
cb6f1f7d 262 "%![event-]+e, %![field-]+f",
3dca2276
PP
263 event, event->context_field);
264 goto end;
265 }
266 }
267
cb6f1f7d 268 ret = bt_field_validate_recursive(event->payload_field);
3dca2276
PP
269 if (ret) {
270 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
cb6f1f7d 271 "%![event-]+e, %![field-]+f",
3dca2276
PP
272 event, event->payload_field);
273 goto end;
274 }
275
276end:
277 return ret;
278}
279
280BT_HIDDEN
cb6f1f7d 281void _bt_event_set_is_frozen(struct bt_event *event,
6c677fb5 282 bool is_frozen)
3dca2276
PP
283{
284 BT_ASSERT(event);
3dca2276
PP
285 BT_LOGD("Freezing event: addr=%p, "
286 "event-class-name=\"%s\", event-class-id=%" PRId64,
cb6f1f7d
PP
287 event, bt_event_class_get_name(event->class),
288 bt_event_class_get_id(event->class));
312c056a
PP
289
290 if (event->header_field) {
291 BT_LOGD_STR("Freezing event's header field.");
cb6f1f7d 292 bt_field_set_is_frozen_recursive(
6c677fb5 293 event->header_field->field, is_frozen);
312c056a
PP
294 }
295
296 if (event->stream_event_context_field) {
297 BT_LOGD_STR("Freezing event's stream event context field.");
cb6f1f7d 298 bt_field_set_is_frozen_recursive(
6c677fb5 299 event->stream_event_context_field, is_frozen);
312c056a
PP
300 }
301
302 if (event->context_field) {
303 BT_LOGD_STR("Freezing event's context field.");
cb6f1f7d 304 bt_field_set_is_frozen_recursive(event->context_field,
6c677fb5 305 is_frozen);
312c056a
PP
306 }
307
308 if (event->payload_field) {
309 BT_LOGD_STR("Freezing event's payload field.");
cb6f1f7d 310 bt_field_set_is_frozen_recursive(event->payload_field,
6c677fb5 311 is_frozen);
312c056a
PP
312 }
313
6c677fb5 314 event->frozen = is_frozen;
cb6f1f7d
PP
315 BT_LOGD_STR("Freezing event's packet.");
316 bt_packet_set_is_frozen(event->packet, is_frozen);
3dca2276
PP
317}
318
cb6f1f7d
PP
319static inline
320int bt_event_initialize(struct bt_event *event,
321 struct bt_event_class *event_class,
3dca2276 322 bt_validation_flag_copy_field_type_func field_type_copy_func,
2dd764c1
PP
323 create_field_func create_field_func,
324 release_field_func release_field_func,
325 create_header_field_func create_header_field_func,
326 release_header_field_func release_header_field_func)
3dca2276
PP
327{
328 int ret;
cb6f1f7d
PP
329 struct bt_trace *trace = NULL;
330 struct bt_stream_class *stream_class = NULL;
312c056a 331 struct bt_field_wrapper *event_header = NULL;
cb6f1f7d
PP
332 struct bt_field *stream_event_context = NULL;
333 struct bt_field *event_context = NULL;
334 struct bt_field *event_payload = NULL;
3dca2276 335 struct bt_validation_output validation_output = { 0 };
cb6f1f7d 336 struct bt_clock_class *expected_clock_class = NULL;
3dca2276
PP
337
338 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
cb6f1f7d 339 BT_LOGD("Initializing event object: event-class-addr=%p, "
3dca2276 340 "event-class-name=\"%s\", event-class-id=%" PRId64,
cb6f1f7d
PP
341 event_class, bt_event_class_get_name(event_class),
342 bt_event_class_get_id(event_class));
3dca2276 343
cb6f1f7d 344 stream_class = bt_event_class_borrow_stream_class(event_class);
3dca2276 345 BT_ASSERT_PRE(stream_class,
cb6f1f7d 346 "Event class is not part of a stream class: %!+E", event_class);
3dca2276
PP
347
348 /* The event class was frozen when added to its stream class */
349 BT_ASSERT(event_class->frozen);
cb6f1f7d
PP
350 trace = bt_stream_class_borrow_trace(stream_class);
351 BT_ASSERT_PRE(trace,
352 "Event class's stream class is not part of a trace: "
353 "%![ec-]+E, %![ec-]+S", event_class, stream_class);
3dca2276
PP
354
355 /*
356 * This must be called before anything that can fail because on
357 * failure, the caller releases the reference to `event` to
358 * destroy it.
359 */
cb6f1f7d 360 bt_object_init_unique(&event->base);
3dca2276
PP
361
362 if (!stream_class->frozen) {
363 /*
364 * Because this function freezes the stream class,
365 * validate that this stream class contains at most a
366 * single clock class so that we set its expected clock
367 * class for future checks.
368 */
cb6f1f7d 369 ret = bt_stream_class_validate_single_clock_class(
3dca2276
PP
370 stream_class, &expected_clock_class);
371 if (ret) {
372 BT_LOGW("Event class's stream class or one of its event "
373 "classes contains a field type which is not "
374 "recursively mapped to the expected "
375 "clock class: "
376 "stream-class-addr=%p, "
377 "stream-class-id=%" PRId64 ", "
378 "stream-class-name=\"%s\", "
379 "expected-clock-class-addr=%p, "
380 "expected-clock-class-name=\"%s\"",
381 stream_class,
cb6f1f7d
PP
382 bt_stream_class_get_id(stream_class),
383 bt_stream_class_get_name(stream_class),
3dca2276
PP
384 expected_clock_class,
385 expected_clock_class ?
386 bt_clock_class_get_name(expected_clock_class) :
387 NULL);
388 goto error;
389 }
390 }
391
392 /* Validate the trace, the stream class, and the event class */
cb6f1f7d 393 ret = bt_event_validate_types_for_create(
3dca2276
PP
394 event_class, &validation_output, field_type_copy_func);
395 if (ret) {
cb6f1f7d 396 /* bt_event_validate_types_for_create() logs errors */
3dca2276
PP
397 goto error;
398 }
399
3dca2276
PP
400 /*
401 * event does not share a common ancestor with the event class; it has
402 * to guarantee its existence by holding a reference. This reference
403 * shall be released once the event is associated to a stream since,
404 * from that point, the event and its class will share the same
405 * lifetime.
cb6f1f7d
PP
406 *
407 * TODO: Is this still true now that this API and CTF writer are
408 * two different implementations?
3dca2276
PP
409 */
410 event->class = bt_get(event_class);
cb6f1f7d 411 ret = bt_event_create_fields(stream_class,
312c056a
PP
412 &validation_output,
413 create_field_func, release_field_func,
414 create_header_field_func, release_header_field_func,
415 &event_header, &stream_event_context, &event_context,
416 &event_payload);
3dca2276 417 if (ret) {
cb6f1f7d 418 /* bt_event_create_fields() logs errors */
3dca2276
PP
419 goto error;
420 }
421
09840de5
PP
422 /*
423 * At this point all the fields are created, potentially from
424 * validated copies of field types, so that the field types and
425 * fields can be replaced in the trace, stream class,
426 * event class, and created event.
427 */
3dca2276
PP
428 bt_validation_replace_types(trace, stream_class, event_class,
429 &validation_output,
430 BT_VALIDATION_FLAG_STREAM | BT_VALIDATION_FLAG_EVENT);
312c056a
PP
431 event->header_field = event_header;
432 event_header = NULL;
433 event->stream_event_context_field = stream_event_context;
434 stream_event_context = NULL;
435 event->context_field = event_context;
436 event_context = NULL;
437 event->payload_field = event_payload;
438 event_payload = NULL;
09840de5
PP
439
440 /*
50842bdc 441 * Put what was not moved in bt_validation_replace_types().
09840de5 442 */
50842bdc 443 bt_validation_output_put_types(&validation_output);
09840de5 444
662e778c
JG
445 /*
446 * Freeze the stream class since the event header must not be changed
447 * anymore.
448 */
cb6f1f7d 449 bt_stream_class_freeze(stream_class);
09840de5 450
2a3ced3c
PP
451 /*
452 * It is safe to set the stream class's unique clock class
453 * now because the stream class is frozen.
454 */
455 if (expected_clock_class) {
456 BT_MOVE(stream_class->clock_class, expected_clock_class);
457 }
458
09840de5
PP
459 /*
460 * Mark stream class, and event class as valid since
461 * they're all frozen now.
462 */
463 stream_class->valid = 1;
464 event_class->valid = 1;
465
466 /* Put stuff we borrowed from the event class */
3dca2276 467 BT_LOGD("Initialized event object: addr=%p, event-class-name=\"%s\", "
7b010242 468 "event-class-id=%" PRId64,
cb6f1f7d
PP
469 event, bt_event_class_get_name(event->class),
470 bt_event_class_get_id(event->class));
3dca2276 471 goto end;
09840de5 472
83509119 473error:
50842bdc 474 bt_validation_output_put_types(&validation_output);
2a3ced3c 475 bt_put(expected_clock_class);
312c056a
PP
476
477 if (event_header) {
2dd764c1 478 release_header_field_func(event_header, stream_class);
312c056a
PP
479 }
480
481 if (stream_event_context) {
482 release_field_func(stream_event_context);
483 }
484
485 if (event_context) {
486 release_field_func(event_context);
487 }
488
489 if (event_payload) {
490 release_field_func(event_payload);
491 }
492
3dca2276
PP
493 ret = -1;
494
495end:
496 return ret;
497}
498
312c056a
PP
499static
500void bt_event_header_field_recycle(struct bt_field_wrapper *field_wrapper,
501 struct bt_stream_class *stream_class)
502{
503 BT_ASSERT(field_wrapper);
504 BT_LIB_LOGD("Recycling event header field: "
505 "addr=%p, %![sc-]+S, %![field-]+f", field_wrapper,
506 stream_class, field_wrapper->field);
507 bt_object_pool_recycle_object(
508 &stream_class->event_header_field_pool,
509 field_wrapper);
510}
511
512static
513struct bt_field_wrapper *create_event_header_field(
514 struct bt_stream_class *stream_class,
cb6f1f7d 515 struct bt_field_type *ft)
312c056a
PP
516{
517 struct bt_field_wrapper *field_wrapper = NULL;
518
519 field_wrapper = bt_field_wrapper_create(
520 &stream_class->event_header_field_pool, (void *) ft);
521 if (!field_wrapper) {
522 goto error;
523 }
524
525 goto end;
526
527error:
528 if (field_wrapper) {
529 bt_event_header_field_recycle(field_wrapper, stream_class);
530 field_wrapper = NULL;
531 }
532
533end:
534 return field_wrapper;
535}
536
537BT_HIDDEN
538struct bt_event *bt_event_new(struct bt_event_class *event_class)
3dca2276
PP
539{
540 int ret;
541 struct bt_event *event = NULL;
312c056a 542 struct bt_stream_class *stream_class;
3dca2276
PP
543
544 event = g_new0(struct bt_event, 1);
545 if (!event) {
546 BT_LOGE_STR("Failed to allocate one event.");
547 goto error;
548 }
549
cb6f1f7d 550 ret = bt_event_initialize(event, event_class,
3dca2276 551 (bt_validation_flag_copy_field_type_func) bt_field_type_copy,
2dd764c1
PP
552 (create_field_func) bt_field_create_recursive,
553 (release_field_func) bt_field_destroy_recursive,
554 (create_header_field_func) create_event_header_field,
555 (release_header_field_func) bt_event_header_field_recycle);
3dca2276 556 if (ret) {
cb6f1f7d 557 /* bt_event_initialize() logs errors */
3dca2276
PP
558 goto error;
559 }
560
312c056a
PP
561 stream_class = bt_event_class_borrow_stream_class(event_class);
562 BT_ASSERT(stream_class);
e22b45d0
PP
563 ret = bt_clock_value_set_initialize(&event->cv_set);
564 if (ret) {
565 goto error;
312c056a
PP
566 }
567
3dca2276
PP
568 goto end;
569
570error:
312c056a
PP
571 if (event) {
572 bt_event_destroy(event);
573 event = NULL;
574 }
575
576end:
577 return event;
578}
579
094ff7c0 580struct bt_event_class *bt_event_borrow_class(struct bt_event *event)
2f100782 581{
f6ccaed9 582 BT_ASSERT_PRE_NON_NULL(event, "Event");
cb6f1f7d 583 return event->class;
2f100782
JG
584}
585
f6ccaed9 586struct bt_stream *bt_event_borrow_stream(struct bt_event *event)
f6ccaed9
PP
587{
588 BT_ASSERT_PRE_NON_NULL(event, "Event");
094ff7c0 589 return event->packet ? event->packet->stream : NULL;
f6ccaed9
PP
590}
591
094ff7c0 592struct bt_field *bt_event_borrow_payload(struct bt_event *event)
e5e6eb3a 593{
cb6f1f7d
PP
594 struct bt_field *payload = NULL;
595
596 BT_ASSERT_PRE_NON_NULL(event, "Event");
597
598 if (!event->payload_field) {
599 BT_LOGV("Event has no current payload field: addr=%p, "
600 "event-class-name=\"%s\", event-class-id=%" PRId64,
601 event, bt_event_class_get_name(event->class),
602 bt_event_class_get_id(event->class));
603 goto end;
604 }
605
606 payload = event->payload_field;
607
608end:
609 return payload;
e5e6eb3a
JG
610}
611
094ff7c0 612struct bt_field *bt_event_borrow_header(struct bt_event *event)
662e778c 613{
cb6f1f7d
PP
614 struct bt_field *header = NULL;
615
616 BT_ASSERT_PRE_NON_NULL(event, "Event");
617
618 if (!event->header_field) {
619 BT_LOGV("Event has no current header field: addr=%p, "
620 "event-class-name=\"%s\", event-class-id=%" PRId64,
621 event, bt_event_class_get_name(event->class),
622 bt_event_class_get_id(event->class));
623 goto end;
624 }
625
626 header = event->header_field->field;
627
628end:
629 return header;
662e778c
JG
630}
631
094ff7c0 632struct bt_field *bt_event_borrow_context(struct bt_event *event)
f655a84d 633{
cb6f1f7d
PP
634 struct bt_field *context = NULL;
635
636 BT_ASSERT_PRE_NON_NULL(event, "Event");
637
638 if (!event->context_field) {
639 BT_LOGV("Event has no current context field: addr=%p, "
640 "event-class-name=\"%s\", event-class-id=%" PRId64,
641 event, bt_event_class_get_name(event->class),
642 bt_event_class_get_id(event->class));
643 goto end;
644 }
645
646 context = event->context_field;
647
648end:
649 return context;
f655a84d
JG
650}
651
094ff7c0 652struct bt_field *bt_event_borrow_stream_event_context(
50842bdc 653 struct bt_event *event)
5fd2e9fd 654{
cb6f1f7d
PP
655 struct bt_field *stream_event_context = NULL;
656
657 BT_ASSERT_PRE_NON_NULL(event, "Event");
658
659 if (!event->stream_event_context_field) {
660 BT_LOGV("Event has no current stream event context field: addr=%p, "
661 "event-class-name=\"%s\", event-class-id=%" PRId64,
662 event, bt_event_class_get_name(event->class),
663 bt_event_class_get_id(event->class));
664 goto end;
665 }
666
667 stream_event_context = event->stream_event_context_field;
668
669end:
670 return stream_event_context;
5fd2e9fd
PP
671}
672
312c056a
PP
673static
674void release_event_header_field(struct bt_field_wrapper *field_wrapper,
cb6f1f7d 675 struct bt_event *event)
273b65be 676{
312c056a
PP
677 struct bt_event_class *event_class = bt_event_borrow_class(event);
678
679 if (!event_class) {
680 bt_field_wrapper_destroy(field_wrapper);
681 } else {
682 struct bt_stream_class *stream_class =
683 bt_event_class_borrow_stream_class(event_class);
684
685 BT_ASSERT(stream_class);
686 bt_event_header_field_recycle(field_wrapper, stream_class);
687 }
273b65be
JG
688}
689
cb6f1f7d
PP
690static inline
691void bt_event_finalize(struct bt_object *obj,
692 void (*field_release_func)(void *),
693 void (*header_field_release_func)(void *, struct bt_event *))
694{
695 struct bt_event *event = (void *) obj;
696
697 BT_LOGD("Destroying event: addr=%p, "
698 "event-class-name=\"%s\", event-class-id=%" PRId64,
699 event,
700 event->class ? bt_event_class_get_name(event->class) : NULL,
701 event->class ? bt_event_class_get_id(event->class) : INT64_C(-1));
702
703 if (event->header_field) {
704 BT_LOGD_STR("Releasing event's header field.");
705 header_field_release_func(event->header_field, event);
706 }
707
708 if (event->stream_event_context_field) {
709 BT_LOGD_STR("Releasing event's stream event context field.");
710 field_release_func(event->stream_event_context_field);
711 }
712
713 if (event->context_field) {
714 BT_LOGD_STR("Releasing event's context field.");
715 field_release_func(event->context_field);
716 }
717
718 if (event->payload_field) {
719 BT_LOGD_STR("Releasing event's payload field.");
720 field_release_func(event->payload_field);
721 }
722
723 /*
724 * Leave this after calling header_field_release_func() because
725 * this function receives the event object and could need its
726 * class to perform some cleanup.
727 */
728 if (!event->base.parent) {
729 /*
730 * Event was keeping a reference to its class since it shared no
731 * common ancestor with it to guarantee they would both have the
732 * same lifetime.
733 */
734 bt_put(event->class);
735 }
736}
737
312c056a
PP
738BT_HIDDEN
739void bt_event_destroy(struct bt_event *event)
273b65be 740{
312c056a 741 BT_ASSERT(event);
cb6f1f7d 742 bt_event_finalize((void *) event,
312c056a
PP
743 (void *) bt_field_destroy_recursive,
744 (void *) release_event_header_field);
e22b45d0 745 bt_clock_value_set_finalize(&event->cv_set);
03039f21 746 BT_LOGD_STR("Putting event's packet.");
5c3b707d 747 bt_put(event->packet);
273b65be
JG
748 g_free(event);
749}
750
e22b45d0
PP
751int bt_event_set_clock_value(struct bt_event *event,
752 struct bt_clock_class *clock_class, uint64_t raw_value,
753 bt_bool is_default)
754{
755 BT_ASSERT_PRE_NON_NULL(event, "Event");
756 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
cb6f1f7d 757 BT_ASSERT_PRE_HOT(event, "Event", ": %!+e", event);
e22b45d0
PP
758 BT_ASSERT_PRE(is_default,
759 "You can only set a default clock value as of this version.");
760 return bt_clock_value_set_set_clock_value(&event->cv_set, clock_class,
761 raw_value, is_default);
762}
763
764struct bt_clock_value *bt_event_borrow_default_clock_value(
765 struct bt_event *event)
78586d8a 766{
50842bdc 767 struct bt_clock_value *clock_value = NULL;
78586d8a 768
f6ccaed9 769 BT_ASSERT_PRE_NON_NULL(event, "Event");
e22b45d0 770 clock_value = event->cv_set.default_cv;
78586d8a 771 if (!clock_value) {
e22b45d0 772 BT_LIB_LOGV("No default clock value: %![event-]+e", event);
78586d8a
JG
773 }
774
1556a1af
JG
775 return clock_value;
776}
777
094ff7c0 778struct bt_packet *bt_event_borrow_packet(struct bt_event *event)
5c0f40f4 779{
50842bdc 780 struct bt_packet *packet = NULL;
5c0f40f4 781
f6ccaed9 782 BT_ASSERT_PRE_NON_NULL(event, "Event");
7b010242
PP
783 if (!event->packet) {
784 BT_LOGV("Event has no current packet: addr=%p, "
785 "event-class-name=\"%s\", event-class-id=%" PRId64,
cb6f1f7d
PP
786 event, bt_event_class_get_name(event->class),
787 bt_event_class_get_id(event->class));
5c0f40f4
JG
788 goto end;
789 }
790
094ff7c0 791 packet = event->packet;
f6ccaed9 792
5c0f40f4
JG
793end:
794 return packet;
795}
796
312c056a
PP
797int bt_event_move_header(struct bt_event *event,
798 struct bt_event_header_field *header_field)
799{
800 struct bt_stream_class *stream_class;
801 struct bt_field_wrapper *field_wrapper = (void *) header_field;
802
803 BT_ASSERT_PRE_NON_NULL(event, "Event");
804 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Header field");
cb6f1f7d 805 BT_ASSERT_PRE_HOT(event, "Event", ": %!+e", event);
312c056a
PP
806 stream_class = bt_event_class_borrow_stream_class(
807 bt_event_borrow_class(event));
cb6f1f7d 808 BT_ASSERT_PRE(stream_class->event_header_field_type,
312c056a
PP
809 "Stream class has no event header field type: %!+S",
810 stream_class);
811
812 /* Recycle current header field: always exists */
cb6f1f7d
PP
813 BT_ASSERT(event->header_field);
814 bt_event_header_field_recycle(event->header_field,
312c056a
PP
815 stream_class);
816
817 /* Move new field */
cb6f1f7d 818 event->header_field = (void *) field_wrapper;
312c056a
PP
819 return 0;
820}
This page took 0.089669 seconds and 4 git commands to generate.