Replace assert() -> BT_ASSERT() and some preconditions with BT_ASSERT_PRE()
[babeltrace.git] / lib / ctf-ir / event.c
1 /*
2 * event.c
3 *
4 * Babeltrace CTF IR - Event
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
29 #define BT_LOG_TAG "EVENT"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/ctf-ir/fields-internal.h>
33 #include <babeltrace/ctf-ir/field-types-internal.h>
34 #include <babeltrace/ctf-ir/clock-class.h>
35 #include <babeltrace/ctf-ir/clock-value.h>
36 #include <babeltrace/ctf-ir/clock-value-internal.h>
37 #include <babeltrace/ctf-ir/clock-class-internal.h>
38 #include <babeltrace/ctf-ir/event-internal.h>
39 #include <babeltrace/ctf-ir/event-class.h>
40 #include <babeltrace/ctf-ir/event-class-internal.h>
41 #include <babeltrace/ctf-ir/stream-class.h>
42 #include <babeltrace/ctf-ir/stream-class-internal.h>
43 #include <babeltrace/ctf-ir/stream-internal.h>
44 #include <babeltrace/ctf-ir/packet.h>
45 #include <babeltrace/ctf-ir/packet-internal.h>
46 #include <babeltrace/ctf-ir/trace-internal.h>
47 #include <babeltrace/ctf-ir/validation-internal.h>
48 #include <babeltrace/ctf-ir/packet-internal.h>
49 #include <babeltrace/ctf-ir/utils.h>
50 #include <babeltrace/ctf-writer/serialize-internal.h>
51 #include <babeltrace/ctf-writer/clock-internal.h>
52 #include <babeltrace/ref.h>
53 #include <babeltrace/ctf-ir/attributes-internal.h>
54 #include <babeltrace/compiler-internal.h>
55 #include <babeltrace/assert-internal.h>
56 #include <babeltrace/assert-pre-internal.h>
57 #include <inttypes.h>
58
59 #define BT_ASSERT_PRE_EVENT_HOT(_event, _name) \
60 BT_ASSERT_PRE_HOT((_event), (_name), ": +%!+e", (_event))
61
62 static
63 void bt_event_destroy(struct bt_object *obj);
64
65 struct bt_event *bt_event_create(struct bt_event_class *event_class)
66 {
67 int ret;
68 enum bt_validation_flag validation_flags =
69 BT_VALIDATION_FLAG_STREAM |
70 BT_VALIDATION_FLAG_EVENT;
71 struct bt_event *event = NULL;
72 struct bt_trace *trace = NULL;
73 struct bt_stream_class *stream_class = NULL;
74 struct bt_field_type *packet_header_type = NULL;
75 struct bt_field_type *packet_context_type = NULL;
76 struct bt_field_type *event_header_type = NULL;
77 struct bt_field_type *stream_event_ctx_type = NULL;
78 struct bt_field_type *event_context_type = NULL;
79 struct bt_field_type *event_payload_type = NULL;
80 struct bt_field *event_header = NULL;
81 struct bt_field *stream_event_context = NULL;
82 struct bt_field *event_context = NULL;
83 struct bt_field *event_payload = NULL;
84 struct bt_value *environment = NULL;
85 struct bt_validation_output validation_output = { 0 };
86 int trace_valid = 0;
87 struct bt_clock_class *expected_clock_class = NULL;
88
89 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
90 BT_LOGD("Creating event object: event-class-addr=%p, "
91 "event-class-name=\"%s\", event-class-id=%" PRId64,
92 event_class, bt_event_class_get_name(event_class),
93 bt_event_class_get_id(event_class));
94
95 stream_class = bt_event_class_get_stream_class(event_class);
96 BT_ASSERT_PRE(stream_class,
97 "Event class is not part of a stream class: %!+E", event_class);
98
99 /* The event class was frozen when added to its stream class */
100 BT_ASSERT(event_class->frozen);
101
102 if (!stream_class->frozen) {
103 if (stream_class->clock) {
104 expected_clock_class =
105 bt_get(stream_class->clock->clock_class);
106 }
107
108 /*
109 * Because this function freezes the stream class,
110 * validate that this stream class contains at most a
111 * single clock class so that we set its expected clock
112 * class for future checks.
113 */
114 ret = bt_stream_class_validate_single_clock_class(
115 stream_class, &expected_clock_class);
116 if (ret) {
117 BT_LOGW("Event class's stream class or one of its event "
118 "classes contains a field type which is not "
119 "recursively mapped to the expected "
120 "clock class: "
121 "stream-class-addr=%p, "
122 "stream-class-id=%" PRId64 ", "
123 "stream-class-name=\"%s\", "
124 "expected-clock-class-addr=%p, "
125 "expected-clock-class-name=\"%s\"",
126 stream_class, bt_stream_class_get_id(stream_class),
127 bt_stream_class_get_name(stream_class),
128 expected_clock_class,
129 expected_clock_class ?
130 bt_clock_class_get_name(expected_clock_class) :
131 NULL);
132 goto error;
133 }
134 }
135
136 /* Validate the trace (if any), the stream class, and the event class */
137 trace = bt_stream_class_get_trace(stream_class);
138 if (trace) {
139 BT_LOGD_STR("Event's class is part of a trace.");
140 packet_header_type = bt_trace_get_packet_header_type(trace);
141 trace_valid = trace->valid;
142 BT_ASSERT(trace_valid);
143 environment = trace->environment;
144 }
145
146 packet_context_type = bt_stream_class_get_packet_context_type(
147 stream_class);
148 event_header_type = bt_stream_class_get_event_header_type(
149 stream_class);
150 stream_event_ctx_type = bt_stream_class_get_event_context_type(
151 stream_class);
152 event_context_type = bt_event_class_get_context_type(event_class);
153 event_payload_type = bt_event_class_get_payload_type(event_class);
154 ret = bt_validate_class_types(environment, packet_header_type,
155 packet_context_type, event_header_type, stream_event_ctx_type,
156 event_context_type, event_payload_type, trace_valid,
157 stream_class->valid, event_class->valid,
158 &validation_output, validation_flags);
159 BT_PUT(packet_header_type);
160 BT_PUT(packet_context_type);
161 BT_PUT(event_header_type);
162 BT_PUT(stream_event_ctx_type);
163 BT_PUT(event_context_type);
164 BT_PUT(event_payload_type);
165 if (ret) {
166 /*
167 * This means something went wrong during the validation
168 * process, not that the objects are invalid.
169 */
170 BT_LOGE("Failed to validate event and parents: ret=%d", ret);
171 goto error;
172 }
173
174 if ((validation_output.valid_flags & validation_flags) !=
175 validation_flags) {
176 /* Invalid trace/stream class/event class */
177 BT_LOGW("Invalid trace, stream class, or event class: "
178 "valid-flags=0x%x", validation_output.valid_flags);
179 goto error;
180 }
181
182 /*
183 * Safe to automatically map selected fields to the stream's
184 * clock's class here because the stream class is about to be
185 * frozen.
186 */
187 if (bt_stream_class_map_clock_class(stream_class,
188 validation_output.packet_context_type,
189 validation_output.event_header_type)) {
190 BT_LOGW_STR("Cannot automatically map selected stream class's "
191 "field types to stream class's clock's class.");
192 goto error;
193 }
194
195 /*
196 * At this point we know the trace (if associated to the stream
197 * class), the stream class, and the event class, with their
198 * current types, are valid. We may proceed with creating
199 * the event.
200 */
201 event = g_new0(struct bt_event, 1);
202 if (!event) {
203 BT_LOGE_STR("Failed to allocate one event.");
204 goto error;
205 }
206
207 bt_object_init(event, bt_event_destroy);
208
209 /*
210 * event does not share a common ancestor with the event class; it has
211 * to guarantee its existence by holding a reference. This reference
212 * shall be released once the event is associated to a stream since,
213 * from that point, the event and its class will share the same
214 * lifetime.
215 */
216 event->event_class = bt_get(event_class);
217 event->clock_values = g_hash_table_new_full(g_direct_hash,
218 g_direct_equal, bt_put, bt_put);
219
220 if (validation_output.event_header_type) {
221 BT_LOGD("Creating initial event header field: ft-addr=%p",
222 validation_output.event_header_type);
223 event_header =
224 bt_field_create(validation_output.event_header_type);
225 if (!event_header) {
226 BT_LOGE_STR("Cannot create initial event header field object.");
227 goto error;
228 }
229 }
230
231 if (validation_output.stream_event_ctx_type) {
232 BT_LOGD("Creating initial stream event context field: ft-addr=%p",
233 validation_output.stream_event_ctx_type);
234 stream_event_context = bt_field_create(
235 validation_output.stream_event_ctx_type);
236 if (!stream_event_context) {
237 BT_LOGE_STR("Cannot create initial stream event context field object.");
238 goto error;
239 }
240 }
241
242 if (validation_output.event_context_type) {
243 BT_LOGD("Creating initial event context field: ft-addr=%p",
244 validation_output.event_context_type);
245 event_context = bt_field_create(
246 validation_output.event_context_type);
247 if (!event_context) {
248 BT_LOGE_STR("Cannot create initial event context field object.");
249 goto error;
250 }
251 }
252
253 if (validation_output.event_payload_type) {
254 BT_LOGD("Creating initial event payload field: ft-addr=%p",
255 validation_output.event_payload_type);
256 event_payload = bt_field_create(
257 validation_output.event_payload_type);
258 if (!event_payload) {
259 BT_LOGE_STR("Cannot create initial event payload field object.");
260 goto error;
261 }
262 }
263
264 /*
265 * At this point all the fields are created, potentially from
266 * validated copies of field types, so that the field types and
267 * fields can be replaced in the trace, stream class,
268 * event class, and created event.
269 */
270 bt_validation_replace_types(trace, stream_class,
271 event_class, &validation_output, validation_flags);
272 BT_MOVE(event->event_header, event_header);
273 BT_MOVE(event->stream_event_context, stream_event_context);
274 BT_MOVE(event->context_payload, event_context);
275 BT_MOVE(event->fields_payload, event_payload);
276
277 /*
278 * Put what was not moved in bt_validation_replace_types().
279 */
280 bt_validation_output_put_types(&validation_output);
281
282 /*
283 * Freeze the stream class since the event header must not be changed
284 * anymore.
285 */
286 bt_stream_class_freeze(stream_class);
287
288 /*
289 * It is safe to set the stream class's unique clock class
290 * now because the stream class is frozen.
291 */
292 if (expected_clock_class) {
293 BT_MOVE(stream_class->clock_class, expected_clock_class);
294 }
295
296 /*
297 * Mark stream class, and event class as valid since
298 * they're all frozen now.
299 */
300 stream_class->valid = 1;
301 event_class->valid = 1;
302
303 /* Put stuff we borrowed from the event class */
304 BT_PUT(stream_class);
305 BT_PUT(trace);
306 BT_LOGD("Created event object: addr=%p, event-class-name=\"%s\", "
307 "event-class-id=%" PRId64,
308 event, bt_event_class_get_name(event->event_class),
309 bt_event_class_get_id(event_class));
310 return event;
311
312 error:
313 bt_validation_output_put_types(&validation_output);
314 BT_PUT(event);
315 BT_PUT(stream_class);
316 BT_PUT(trace);
317 BT_PUT(event_header);
318 BT_PUT(stream_event_context);
319 BT_PUT(event_context);
320 BT_PUT(event_payload);
321 bt_put(expected_clock_class);
322 BT_ASSERT(!packet_header_type);
323 BT_ASSERT(!packet_context_type);
324 BT_ASSERT(!event_header_type);
325 BT_ASSERT(!stream_event_ctx_type);
326 BT_ASSERT(!event_context_type);
327 BT_ASSERT(!event_payload_type);
328 return event;
329 }
330
331 struct bt_event_class *bt_event_get_class(struct bt_event *event)
332 {
333 BT_ASSERT_PRE_NON_NULL(event, "Event");
334 return bt_get(bt_event_borrow_event_class(event));
335 }
336
337 BT_HIDDEN
338 struct bt_stream *bt_event_borrow_stream(struct bt_event *event)
339 {
340 struct bt_stream *stream = NULL;
341
342 BT_ASSERT(event);
343
344 /*
345 * If the event has a parent, then this is its (writer) stream.
346 * If the event has no parent, then if it has a packet, this
347 * is its (non-writer) stream.
348 */
349 if (event->base.parent) {
350 stream = (struct bt_stream *) bt_object_borrow_parent(event);
351 } else {
352 if (event->packet) {
353 stream = event->packet->stream;
354 }
355 }
356
357 return stream;
358 }
359
360 struct bt_stream *bt_event_get_stream(struct bt_event *event)
361 {
362 BT_ASSERT_PRE_NON_NULL(event, "Event");
363 return bt_get(bt_event_borrow_stream(event));
364 }
365
366 int bt_event_set_payload(struct bt_event *event, const char *name,
367 struct bt_field *payload)
368 {
369 int ret = 0;
370
371 BT_ASSERT_PRE_NON_NULL(event, "Event");
372 BT_ASSERT_PRE_NON_NULL(payload, "Payload field");
373 BT_ASSERT_PRE_EVENT_HOT(event, "Event");
374
375 if (name) {
376 ret = bt_field_structure_set_field_by_name(
377 event->fields_payload, name, payload);
378 } else {
379 BT_ASSERT_PRE(bt_field_type_compare(payload->type,
380 event->event_class->fields) == 0,
381 "Payload field's type is different from the "
382 "expected field type: %![event-]+e, %![ft-]+F, "
383 "%![expected-ft-]+F",
384 event, payload->type, event->event_class->fields);
385
386 bt_put(event->fields_payload);
387 bt_get(payload);
388 event->fields_payload = payload;
389 }
390
391 if (ret) {
392 BT_LOGW("Failed to set event's payload field: event-addr=%p, "
393 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
394 "payload-field-name=\"%s\", payload-field-addr=%p",
395 event, bt_event_class_get_name(event->event_class),
396 bt_event_class_get_id(event->event_class),
397 name, payload);
398 } else {
399 BT_LOGV("Set event's payload field: event-addr=%p, "
400 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
401 "payload-field-name=\"%s\", payload-field-addr=%p",
402 event, bt_event_class_get_name(event->event_class),
403 bt_event_class_get_id(event->event_class),
404 name, payload);
405 }
406
407 return ret;
408 }
409
410 struct bt_field *bt_event_get_event_payload(struct bt_event *event)
411 {
412 struct bt_field *payload = NULL;
413
414 BT_ASSERT_PRE_NON_NULL(event, "Event");
415
416 if (!event->fields_payload) {
417 BT_LOGV("Event has no current payload field: addr=%p, "
418 "event-class-name=\"%s\", event-class-id=%" PRId64,
419 event, bt_event_class_get_name(event->event_class),
420 bt_event_class_get_id(event->event_class));
421 goto end;
422 }
423
424 payload = event->fields_payload;
425 bt_get(payload);
426
427 end:
428 return payload;
429 }
430
431 int bt_event_set_event_payload(struct bt_event *event,
432 struct bt_field *payload)
433 {
434 return bt_event_set_payload(event, NULL, payload);
435 }
436
437 struct bt_field *bt_event_get_payload(struct bt_event *event,
438 const char *name)
439 {
440 struct bt_field *field = NULL;
441
442 BT_ASSERT_PRE_NON_NULL(event, "Event");
443
444 if (name) {
445 field = bt_field_structure_get_field_by_name(
446 event->fields_payload, name);
447 } else {
448 field = event->fields_payload;
449 bt_get(field);
450 }
451
452 return field;
453 }
454
455 struct bt_field *bt_event_get_payload_by_index(
456 struct bt_event *event, uint64_t index)
457 {
458 struct bt_field *field = NULL;
459
460 BT_ASSERT_PRE_NON_NULL(event, "Event");
461 field = bt_field_structure_get_field_by_index(event->fields_payload,
462 index);
463
464 return field;
465 }
466
467 struct bt_field *bt_event_get_header(struct bt_event *event)
468 {
469 struct bt_field *header = NULL;
470
471 BT_ASSERT_PRE_NON_NULL(event, "Event");
472
473 if (!event->event_header) {
474 BT_LOGV("Event has no current header field: addr=%p, "
475 "event-class-name=\"%s\", event-class-id=%" PRId64,
476 event, bt_event_class_get_name(event->event_class),
477 bt_event_class_get_id(event->event_class));
478 goto end;
479 }
480
481 header = event->event_header;
482 bt_get(header);
483
484 end:
485 return header;
486 }
487
488 int bt_event_set_header(struct bt_event *event, struct bt_field *header)
489 {
490 BT_ASSERT_PRE_NON_NULL(event, "Event");
491 BT_ASSERT_PRE_EVENT_HOT(event, "Event");
492
493 /*
494 * Ensure the provided header's type matches the one registered to the
495 * stream class.
496 */
497 if (header) {
498 BT_ASSERT_PRE(bt_field_type_compare(header->type,
499 bt_event_class_borrow_stream_class(event->event_class)->event_header_type) == 0,
500 "Header field's type is different from the "
501 "expected field type: %![event-]+e, %![ft-]+F, "
502 "%![expected-ft-]+F",
503 event, header->type,
504 bt_event_class_borrow_stream_class(event->event_class)->event_header_type);
505 } else {
506 BT_ASSERT_PRE(!bt_event_class_borrow_stream_class(event->event_class)->event_header_type,
507 "Setting no event header field, "
508 "but event header field type is not NULL: ",
509 "%![event-]+e, %![header-ft-]+F",
510 event,
511 bt_event_class_borrow_stream_class(event->event_class)->event_header_type);
512 }
513
514 bt_put(event->event_header);
515 event->event_header = bt_get(header);
516 BT_LOGV("Set event's header field: event-addr=%p, "
517 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
518 "header-field-addr=%p",
519 event, bt_event_class_get_name(event->event_class),
520 bt_event_class_get_id(event->event_class), header);
521 return 0;
522 }
523
524 struct bt_field *bt_event_get_event_context(struct bt_event *event)
525 {
526 struct bt_field *context = NULL;
527
528 BT_ASSERT_PRE_NON_NULL(event, "Event");
529
530 if (!event->context_payload) {
531 BT_LOGV("Event has no current context field: addr=%p, "
532 "event-class-name=\"%s\", event-class-id=%" PRId64,
533 event, bt_event_class_get_name(event->event_class),
534 bt_event_class_get_id(event->event_class));
535 goto end;
536 }
537
538 context = event->context_payload;
539 bt_get(context);
540
541 end:
542 return context;
543 }
544
545 int bt_event_set_event_context(struct bt_event *event, struct bt_field *context)
546 {
547 BT_ASSERT_PRE_NON_NULL(event, "Event");
548 BT_ASSERT_PRE_EVENT_HOT(event, "Event");
549
550 if (context) {
551 BT_ASSERT_PRE(bt_field_type_compare(context->type,
552 event->event_class->context) == 0,
553 "Context field's type is different from the "
554 "expected field type: %![event-]+e, %![ft-]+F, "
555 "%![expected-ft-]+F",
556 event, context->type, event->event_class->context);
557 } else {
558 BT_ASSERT_PRE(!event->event_class->context,
559 "Setting no event context field, "
560 "but event context field type is not NULL: ",
561 "%![event-]+e, %![context-ft-]+F",
562 event, event->event_class->context);
563 }
564
565 bt_put(event->context_payload);
566 event->context_payload = bt_get(context);
567 BT_LOGV("Set event's context field: event-addr=%p, "
568 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
569 "context-field-addr=%p",
570 event, bt_event_class_get_name(event->event_class),
571 bt_event_class_get_id(event->event_class), context);
572 return 0;
573 }
574
575 struct bt_field *bt_event_get_stream_event_context(
576 struct bt_event *event)
577 {
578 struct bt_field *stream_event_context = NULL;
579
580 BT_ASSERT_PRE_NON_NULL(event, "Event");
581
582 if (!event->stream_event_context) {
583 BT_LOGV("Event has no current stream event context field: addr=%p, "
584 "event-class-name=\"%s\", event-class-id=%" PRId64,
585 event, bt_event_class_get_name(event->event_class),
586 bt_event_class_get_id(event->event_class));
587 goto end;
588 }
589
590 stream_event_context = event->stream_event_context;
591
592 end:
593 return bt_get(stream_event_context);
594 }
595
596 int bt_event_set_stream_event_context(struct bt_event *event,
597 struct bt_field *stream_event_context)
598 {
599 BT_ASSERT_PRE_NON_NULL(event, "Event");
600 BT_ASSERT_PRE_EVENT_HOT(event, "Event");
601
602 if (stream_event_context) {
603 BT_ASSERT_PRE(bt_field_type_compare(stream_event_context->type,
604 bt_event_class_borrow_stream_class(event->event_class)->event_context_type) == 0,
605 "Stream event context field's type is different from the "
606 "expected field type: %![event-]+e, %![ft-]+F, "
607 "%![expected-ft-]+F",
608 event, stream_event_context->type,
609 bt_event_class_borrow_stream_class(event->event_class)->event_context_type);
610 } else {
611 BT_ASSERT_PRE(!bt_event_class_borrow_stream_class(event->event_class)->event_context_type,
612 "Setting no stream event context field, "
613 "but stream event context field type is not NULL: ",
614 "%![event-]+e, %![context-ft-]+F",
615 event,
616 bt_event_class_borrow_stream_class(event->event_class)->event_context_type);
617 }
618
619 bt_get(stream_event_context);
620 BT_MOVE(event->stream_event_context, stream_event_context);
621 BT_LOGV("Set event's stream event context field: event-addr=%p, "
622 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
623 "stream-event-context-field-addr=%p",
624 event, bt_event_class_get_name(event->event_class),
625 bt_event_class_get_id(event->event_class),
626 stream_event_context);
627 return 0;
628 }
629
630 /* Pre-2.0 CTF writer backward compatibility */
631 void bt_ctf_event_get(struct bt_event *event)
632 {
633 bt_get(event);
634 }
635
636 /* Pre-2.0 CTF writer backward compatibility */
637 void bt_ctf_event_put(struct bt_event *event)
638 {
639 bt_put(event);
640 }
641
642 void bt_event_destroy(struct bt_object *obj)
643 {
644 struct bt_event *event;
645
646 event = container_of(obj, struct bt_event, base);
647 BT_LOGD("Destroying event: addr=%p, "
648 "event-class-name=\"%s\", event-class-id=%" PRId64,
649 event, bt_event_class_get_name(event->event_class),
650 bt_event_class_get_id(event->event_class));
651
652 if (!event->base.parent) {
653 /*
654 * Event was keeping a reference to its class since it shared no
655 * common ancestor with it to guarantee they would both have the
656 * same lifetime.
657 */
658 bt_put(event->event_class);
659 }
660 g_hash_table_destroy(event->clock_values);
661 BT_LOGD_STR("Putting event's header field.");
662 bt_put(event->event_header);
663 BT_LOGD_STR("Putting event's stream event context field.");
664 bt_put(event->stream_event_context);
665 BT_LOGD_STR("Putting event's context field.");
666 bt_put(event->context_payload);
667 BT_LOGD_STR("Putting event's payload field.");
668 bt_put(event->fields_payload);
669 BT_LOGD_STR("Putting event's packet.");
670 bt_put(event->packet);
671 g_free(event);
672 }
673
674 struct bt_clock_value *bt_event_get_clock_value(
675 struct bt_event *event, struct bt_clock_class *clock_class)
676 {
677 struct bt_clock_value *clock_value = NULL;
678
679 BT_ASSERT_PRE_NON_NULL(event, "Event");
680 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
681 clock_value = g_hash_table_lookup(event->clock_values, clock_class);
682 if (!clock_value) {
683 BT_LOGV("No clock value associated to the given clock class: "
684 "event-addr=%p, event-class-name=\"%s\", "
685 "event-class-id=%" PRId64 ", clock-class-addr=%p, "
686 "clock-class-name=\"%s\"", event,
687 bt_event_class_get_name(event->event_class),
688 bt_event_class_get_id(event->event_class),
689 clock_class, bt_clock_class_get_name(clock_class));
690 goto end;
691 }
692
693 bt_get(clock_value);
694
695 end:
696 return clock_value;
697 }
698
699 int bt_event_set_clock_value(struct bt_event *event,
700 struct bt_clock_value *value)
701 {
702 struct bt_trace *trace;
703 struct bt_stream_class *stream_class;
704 struct bt_event_class *event_class;
705 struct bt_clock_class *clock_class = NULL;
706
707 BT_ASSERT_PRE_NON_NULL(event, "Event");
708 BT_ASSERT_PRE_NON_NULL(value, "Clock value");
709 BT_ASSERT_PRE_EVENT_HOT(event, "Event");
710 clock_class = bt_clock_value_get_class(value);
711 event_class = bt_event_borrow_event_class(event);
712 BT_ASSERT(event_class);
713 stream_class = bt_event_class_borrow_stream_class(event_class);
714 BT_ASSERT(stream_class);
715 trace = bt_stream_class_borrow_trace(stream_class);
716 BT_ASSERT(trace);
717 BT_ASSERT_PRE(bt_trace_has_clock_class(trace, clock_class),
718 "Clock class is not part of event's trace: "
719 "%![event-]+e, %![clock-class-]+K",
720 event, clock_class);
721 g_hash_table_insert(event->clock_values, clock_class, bt_get(value));
722 BT_LOGV("Set event's clock value: "
723 "event-addr=%p, event-class-name=\"%s\", "
724 "event-class-id=%" PRId64 ", clock-class-addr=%p, "
725 "clock-class-name=\"%s\", clock-value-addr=%p, "
726 "clock-value-cycles=%" PRIu64,
727 event, bt_event_class_get_name(event->event_class),
728 bt_event_class_get_id(event->event_class),
729 clock_class, bt_clock_class_get_name(clock_class),
730 value, value->value);
731 clock_class = NULL;
732 bt_put(clock_class);
733 return 0;
734 }
735
736 BT_HIDDEN
737 int _bt_event_validate(struct bt_event *event)
738 {
739 int ret = 0;
740 struct bt_stream_class *stream_class;
741
742 BT_ASSERT(event);
743 if (event->event_header) {
744 ret = bt_field_validate_recursive(event->event_header);
745 if (ret) {
746 BT_ASSERT_PRE_MSG("Invalid event's header field: "
747 "%![event-]+e, %![field-]+f",
748 event, event->event_header);
749 goto end;
750 }
751 }
752
753 stream_class = bt_event_class_borrow_stream_class(event->event_class);
754
755 /*
756 * We should not have been able to create the event without associating
757 * the event class to a stream class.
758 */
759 BT_ASSERT(stream_class);
760
761 if (stream_class->event_context_type) {
762 ret = bt_field_validate_recursive(event->stream_event_context);
763 if (ret) {
764 BT_ASSERT_PRE_MSG("Invalid event's stream event context field: "
765 "%![event-]+e, %![field-]+f",
766 event, event->stream_event_context);
767 goto end;
768 }
769 }
770
771 if (event->event_class->context) {
772 ret = bt_field_validate_recursive(event->context_payload);
773 if (ret) {
774 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
775 "%![event-]+e, %![field-]+f",
776 event, event->context_payload);
777 goto end;
778 }
779 }
780
781 ret = bt_field_validate_recursive(event->fields_payload);
782 if (ret) {
783 BT_ASSERT_PRE_MSG("Invalid event's payload field: "
784 "%![event-]+e, %![field-]+f",
785 event, event->fields_payload);
786 goto end;
787 }
788
789 end:
790 return ret;
791 }
792
793 BT_HIDDEN
794 int bt_event_serialize(struct bt_event *event, struct bt_stream_pos *pos,
795 enum bt_byte_order native_byte_order)
796 {
797 int ret = 0;
798
799 BT_ASSERT(event);
800 BT_ASSERT(pos);
801
802 BT_LOGV_STR("Serializing event's context field.");
803 if (event->context_payload) {
804 ret = bt_field_serialize_recursive(event->context_payload, pos,
805 native_byte_order);
806 if (ret) {
807 BT_LOGW("Cannot serialize event's context field: "
808 "event-addr=%p, event-class-name=\"%s\", "
809 "event-class-id=%" PRId64,
810 event,
811 bt_event_class_get_name(event->event_class),
812 bt_event_class_get_id(event->event_class));
813 goto end;
814 }
815 }
816
817 BT_LOGV_STR("Serializing event's payload field.");
818 if (event->fields_payload) {
819 ret = bt_field_serialize_recursive(event->fields_payload, pos,
820 native_byte_order);
821 if (ret) {
822 BT_LOGW("Cannot serialize event's payload field: "
823 "event-addr=%p, event-class-name=\"%s\", "
824 "event-class-id=%" PRId64,
825 event,
826 bt_event_class_get_name(event->event_class),
827 bt_event_class_get_id(event->event_class));
828 goto end;
829 }
830 }
831 end:
832 return ret;
833 }
834
835 struct bt_packet *bt_event_get_packet(struct bt_event *event)
836 {
837 struct bt_packet *packet = NULL;
838
839 BT_ASSERT_PRE_NON_NULL(event, "Event");
840 if (!event->packet) {
841 BT_LOGV("Event has no current packet: addr=%p, "
842 "event-class-name=\"%s\", event-class-id=%" PRId64,
843 event, bt_event_class_get_name(event->event_class),
844 bt_event_class_get_id(event->event_class));
845 goto end;
846 }
847
848 packet = bt_get(event->packet);
849
850 end:
851 return packet;
852 }
853
854 int bt_event_set_packet(struct bt_event *event,
855 struct bt_packet *packet)
856 {
857 BT_ASSERT_PRE_NON_NULL(event, "Event");
858 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
859 BT_ASSERT_PRE_EVENT_HOT(event, "Event");
860
861 /*
862 * Make sure the new packet was created by this event's
863 * stream, if it is set.
864 */
865 if (bt_event_borrow_stream(event)) {
866 BT_ASSERT_PRE(packet->stream == bt_event_borrow_stream(event),
867 "Packet's stream and event's stream differ: "
868 "%![event-]+e, %![packet-]+a",
869 event, packet);
870 } else {
871 BT_ASSERT_PRE(bt_event_class_borrow_stream_class(event->event_class) ==
872 packet->stream->stream_class,
873 "Packet's stream class and event's stream class differ: "
874 "%![event-]+e, %![packet-]+a",
875 event, packet);
876 }
877
878 bt_get(packet);
879 BT_MOVE(event->packet, packet);
880 BT_LOGV("Set event's packet: event-addr=%p, "
881 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
882 "packet-addr=%p",
883 event, bt_event_class_get_name(event->event_class),
884 bt_event_class_get_id(event->event_class), packet);
885 return 0;
886 }
887
888 BT_HIDDEN
889 void _bt_event_freeze(struct bt_event *event)
890 {
891 BT_ASSERT(event);
892
893 if (event->frozen) {
894 return;
895 }
896
897 BT_LOGD("Freezing event: addr=%p, "
898 "event-class-name=\"%s\", event-class-id=%" PRId64,
899 event, bt_event_class_get_name(event->event_class),
900 bt_event_class_get_id(event->event_class));
901 bt_packet_freeze(event->packet);
902 BT_LOGD_STR("Freezing event's header field.");
903 bt_field_freeze_recursive(event->event_header);
904 BT_LOGD_STR("Freezing event's stream event context field.");
905 bt_field_freeze_recursive(event->stream_event_context);
906 BT_LOGD_STR("Freezing event's context field.");
907 bt_field_freeze_recursive(event->context_payload);
908 BT_LOGD_STR("Freezing event's payload field.");
909 bt_field_freeze_recursive(event->fields_payload);
910 event->frozen = 1;
911 }
This page took 0.050855 seconds and 4 git commands to generate.