1ab1be57db83fbcffb06b7de26290d8d38b6a463
[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-class-internal.h>
36 #include <babeltrace/ctf-ir/event-internal.h>
37 #include <babeltrace/ctf-ir/event-class.h>
38 #include <babeltrace/ctf-ir/event-class-internal.h>
39 #include <babeltrace/ctf-ir/stream-class.h>
40 #include <babeltrace/ctf-ir/stream-class-internal.h>
41 #include <babeltrace/ctf-ir/stream-internal.h>
42 #include <babeltrace/ctf-ir/packet.h>
43 #include <babeltrace/ctf-ir/packet-internal.h>
44 #include <babeltrace/ctf-ir/trace-internal.h>
45 #include <babeltrace/ctf-ir/validation-internal.h>
46 #include <babeltrace/ctf-ir/packet-internal.h>
47 #include <babeltrace/ctf-ir/utils.h>
48 #include <babeltrace/ctf-writer/serialize-internal.h>
49 #include <babeltrace/ref.h>
50 #include <babeltrace/ctf-ir/attributes-internal.h>
51 #include <babeltrace/compiler-internal.h>
52 #include <inttypes.h>
53
54 static
55 void bt_ctf_event_destroy(struct bt_object *obj);
56
57 struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
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_event *event = NULL;
64 struct bt_ctf_trace *trace = NULL;
65 struct bt_ctf_stream_class *stream_class = NULL;
66 struct bt_ctf_field_type *packet_header_type = NULL;
67 struct bt_ctf_field_type *packet_context_type = NULL;
68 struct bt_ctf_field_type *event_header_type = NULL;
69 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
70 struct bt_ctf_field_type *event_context_type = NULL;
71 struct bt_ctf_field_type *event_payload_type = NULL;
72 struct bt_ctf_field *event_header = NULL;
73 struct bt_ctf_field *stream_event_context = NULL;
74 struct bt_ctf_field *event_context = NULL;
75 struct bt_ctf_field *event_payload = NULL;
76 struct bt_value *environment = NULL;
77 struct bt_ctf_validation_output validation_output = { 0 };
78 int trace_valid = 0;
79
80 BT_LOGD("Creating event object: event-class-addr=%p, "
81 "event-class-name=\"%s\", event-class-id=%" PRId64,
82 event_class, bt_ctf_event_class_get_name(event_class),
83 bt_ctf_event_class_get_id(event_class));
84
85 if (!event_class) {
86 BT_LOGW_STR("Invalid parameter: event class is NULL.");
87 goto error;
88 }
89
90 stream_class = bt_ctf_event_class_get_stream_class(event_class);
91
92 /*
93 * We disallow the creation of an event if its event class has not been
94 * associated to a stream class.
95 */
96 if (!stream_class) {
97 BT_LOGW_STR("Event class is not part of a stream class.");
98 goto error;
99 }
100
101 /* A stream class should always have an existing event header type */
102 assert(stream_class->event_header_type);
103
104 /* The event class was frozen when added to its stream class */
105 assert(event_class->frozen);
106
107 /* Validate the trace (if any), the stream class, and the event class */
108 trace = bt_ctf_stream_class_get_trace(stream_class);
109 if (trace) {
110 BT_LOGD_STR("Event's class is part of a trace.");
111 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
112 trace_valid = trace->valid;
113 assert(trace_valid);
114 environment = trace->environment;
115 }
116
117 packet_context_type = bt_ctf_stream_class_get_packet_context_type(
118 stream_class);
119 event_header_type = bt_ctf_stream_class_get_event_header_type(
120 stream_class);
121 stream_event_ctx_type = bt_ctf_stream_class_get_event_context_type(
122 stream_class);
123 event_context_type = bt_ctf_event_class_get_context_type(event_class);
124 event_payload_type = bt_ctf_event_class_get_payload_type(event_class);
125 ret = bt_ctf_validate_class_types(environment, packet_header_type,
126 packet_context_type, event_header_type, stream_event_ctx_type,
127 event_context_type, event_payload_type, trace_valid,
128 stream_class->valid, event_class->valid,
129 &validation_output, validation_flags);
130 BT_PUT(packet_header_type);
131 BT_PUT(packet_context_type);
132 BT_PUT(event_header_type);
133 BT_PUT(stream_event_ctx_type);
134 BT_PUT(event_context_type);
135 BT_PUT(event_payload_type);
136 if (ret) {
137 /*
138 * This means something went wrong during the validation
139 * process, not that the objects are invalid.
140 */
141 BT_LOGE("Failed to validate event and parents: ret=%d", ret);
142 goto error;
143 }
144
145 if ((validation_output.valid_flags & validation_flags) !=
146 validation_flags) {
147 /* Invalid trace/stream class/event class */
148 BT_LOGW("Invalid trace, stream class, or event class: "
149 "valid-flags=0x%x", validation_output.valid_flags);
150 goto error;
151 }
152
153 /*
154 * At this point we know the trace (if associated to the stream
155 * class), the stream class, and the event class, with their
156 * current types, are valid. We may proceed with creating
157 * the event.
158 */
159 event = g_new0(struct bt_ctf_event, 1);
160 if (!event) {
161 BT_LOGE_STR("Failed to allocate one event.");
162 goto error;
163 }
164
165 bt_object_init(event, bt_ctf_event_destroy);
166
167 /*
168 * event does not share a common ancestor with the event class; it has
169 * to guarantee its existence by holding a reference. This reference
170 * shall be released once the event is associated to a stream since,
171 * from that point, the event and its class will share the same
172 * lifetime.
173 */
174 event->event_class = bt_get(event_class);
175 event->clock_values = g_hash_table_new_full(g_direct_hash,
176 g_direct_equal, bt_put, bt_put);
177 event_header =
178 bt_ctf_field_create(validation_output.event_header_type);
179 if (!event_header) {
180 BT_LOGE_STR("Cannot create initial event header field object.");
181 goto error;
182 }
183
184 if (validation_output.stream_event_ctx_type) {
185 stream_event_context = bt_ctf_field_create(
186 validation_output.stream_event_ctx_type);
187 if (!stream_event_context) {
188 BT_LOGE_STR("Cannot create initial stream event context field object.");
189 goto error;
190 }
191 }
192
193 if (validation_output.event_context_type) {
194 event_context = bt_ctf_field_create(
195 validation_output.event_context_type);
196 if (!event_context) {
197 BT_LOGE_STR("Cannot create initial event context field object.");
198 goto error;
199 }
200 }
201
202 if (validation_output.event_payload_type) {
203 event_payload = bt_ctf_field_create(
204 validation_output.event_payload_type);
205 if (!event_payload) {
206 BT_LOGE_STR("Cannot create initial event payload field object.");
207 goto error;
208 }
209 }
210
211 /*
212 * At this point all the fields are created, potentially from
213 * validated copies of field types, so that the field types and
214 * fields can be replaced in the trace, stream class,
215 * event class, and created event.
216 */
217 bt_ctf_validation_replace_types(trace, stream_class,
218 event_class, &validation_output, validation_flags);
219 BT_MOVE(event->event_header, event_header);
220 BT_MOVE(event->stream_event_context, stream_event_context);
221 BT_MOVE(event->context_payload, event_context);
222 BT_MOVE(event->fields_payload, event_payload);
223
224 /*
225 * Put what was not moved in bt_ctf_validation_replace_types().
226 */
227 bt_ctf_validation_output_put_types(&validation_output);
228
229 /*
230 * Freeze the stream class since the event header must not be changed
231 * anymore.
232 */
233 bt_ctf_stream_class_freeze(stream_class);
234
235 /*
236 * Mark stream class, and event class as valid since
237 * they're all frozen now.
238 */
239 stream_class->valid = 1;
240 event_class->valid = 1;
241
242 /* Put stuff we borrowed from the event class */
243 BT_PUT(stream_class);
244 BT_PUT(trace);
245 BT_LOGD("Created event object: addr=%p, event-class-name=\"%s\", "
246 "event-class-id=%" PRId64,
247 event, bt_ctf_event_class_get_name(event->event_class),
248 bt_ctf_event_class_get_id(event_class));
249 return event;
250
251 error:
252 bt_ctf_validation_output_put_types(&validation_output);
253 BT_PUT(event);
254 BT_PUT(stream_class);
255 BT_PUT(trace);
256 BT_PUT(event_header);
257 BT_PUT(stream_event_context);
258 BT_PUT(event_context);
259 BT_PUT(event_payload);
260 assert(!packet_header_type);
261 assert(!packet_context_type);
262 assert(!event_header_type);
263 assert(!stream_event_ctx_type);
264 assert(!event_context_type);
265 assert(!event_payload_type);
266
267 return event;
268 }
269
270 struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event)
271 {
272 struct bt_ctf_event_class *event_class = NULL;
273
274 if (!event) {
275 BT_LOGW_STR("Invalid parameter: event is NULL.");
276 goto end;
277 }
278
279 event_class = event ? bt_get(bt_ctf_event_borrow_event_class(event)) :
280 NULL;
281 end:
282 return event_class;
283 }
284
285 struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event)
286 {
287 struct bt_ctf_stream *stream = NULL;
288
289 if (!event) {
290 BT_LOGW_STR("Invalid parameter: event is NULL.");
291 goto end;
292 }
293
294 /*
295 * If the event has a parent, then this is its (writer) stream.
296 * If the event has no parent, then if it has a packet, this
297 * is its (non-writer) stream.
298 */
299 if (event->base.parent) {
300 stream = (struct bt_ctf_stream *) bt_object_get_parent(event);
301 } else {
302 if (event->packet) {
303 stream = bt_get(event->packet->stream);
304 }
305 }
306
307 end:
308 return stream;
309 }
310
311 int bt_ctf_event_set_payload(struct bt_ctf_event *event,
312 const char *name,
313 struct bt_ctf_field *payload)
314 {
315 int ret = 0;
316
317 if (!event || !payload) {
318 BT_LOGW("Invalid parameter: event or payload field is NULL: "
319 "event-addr=%p, payload-field-addr=%p",
320 event, payload);
321 ret = -1;
322 goto end;
323 }
324
325 if (event->frozen) {
326 BT_LOGW("Invalid parameter: event is frozen: addr=%p, "
327 "event-class-name=\"%s\", event-class-id=%" PRId64,
328 event, bt_ctf_event_class_get_name(event->event_class),
329 bt_ctf_event_class_get_id(event->event_class));
330 ret = -1;
331 goto end;
332 }
333
334 if (name) {
335 ret = bt_ctf_field_structure_set_field(event->fields_payload,
336 name, payload);
337 } else {
338 struct bt_ctf_field_type *payload_type;
339
340 payload_type = bt_ctf_field_get_type(payload);
341
342 if (bt_ctf_field_type_compare(payload_type,
343 event->event_class->fields) == 0) {
344 bt_put(event->fields_payload);
345 bt_get(payload);
346 event->fields_payload = payload;
347 } else {
348 BT_LOGW("Invalid parameter: payload field type is different from the expected field type: "
349 "event-addr=%p, event-class-name=\"%s\", "
350 "event-class-id=%" PRId64,
351 event,
352 bt_ctf_event_class_get_name(event->event_class),
353 bt_ctf_event_class_get_id(event->event_class));
354 ret = -1;
355 }
356
357 bt_put(payload_type);
358 }
359
360 if (ret) {
361 BT_LOGW("Failed to set event's payload field: event-addr=%p, "
362 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
363 "payload-field-name=\"%s\", payload-field-addr=%p",
364 event, bt_ctf_event_class_get_name(event->event_class),
365 bt_ctf_event_class_get_id(event->event_class),
366 name, payload);
367 } else {
368 BT_LOGV("Set event's payload field: event-addr=%p, "
369 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
370 "payload-field-name=\"%s\", payload-field-addr=%p",
371 event, bt_ctf_event_class_get_name(event->event_class),
372 bt_ctf_event_class_get_id(event->event_class),
373 name, payload);
374 }
375
376 end:
377 return ret;
378 }
379
380 struct bt_ctf_field *bt_ctf_event_get_event_payload(struct bt_ctf_event *event)
381 {
382 struct bt_ctf_field *payload = NULL;
383
384 if (!event) {
385 BT_LOGW_STR("Invalid parameter: event is NULL.");
386 goto end;
387 }
388
389 if (!event->fields_payload) {
390 BT_LOGV("Event has no current payload field: addr=%p, "
391 "event-class-name=\"%s\", event-class-id=%" PRId64,
392 event, bt_ctf_event_class_get_name(event->event_class),
393 bt_ctf_event_class_get_id(event->event_class));
394 goto end;
395 }
396
397 payload = event->fields_payload;
398 bt_get(payload);
399 end:
400 return payload;
401 }
402
403 int bt_ctf_event_set_event_payload(struct bt_ctf_event *event,
404 struct bt_ctf_field *payload)
405 {
406 return bt_ctf_event_set_payload(event, NULL, payload);
407 }
408
409 struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event,
410 const char *name)
411 {
412 struct bt_ctf_field *field = NULL;
413
414 if (!event) {
415 BT_LOGW_STR("Invalid parameter: event is NULL.");
416 goto end;
417 }
418
419 if (name) {
420 field = bt_ctf_field_structure_get_field(event->fields_payload,
421 name);
422 } else {
423 field = event->fields_payload;
424 bt_get(field);
425 }
426 end:
427 return field;
428 }
429
430 struct bt_ctf_field *bt_ctf_event_get_payload_by_index(
431 struct bt_ctf_event *event, uint64_t index)
432 {
433 struct bt_ctf_field *field = NULL;
434
435 if (!event) {
436 BT_LOGW_STR("Invalid parameter: event is NULL.");
437 goto end;
438 }
439
440 field = bt_ctf_field_structure_get_field_by_index(event->fields_payload,
441 index);
442 end:
443 return field;
444 }
445
446 struct bt_ctf_field *bt_ctf_event_get_header(
447 struct bt_ctf_event *event)
448 {
449 struct bt_ctf_field *header = NULL;
450
451 if (!event) {
452 BT_LOGW_STR("Invalid parameter: event is NULL.");
453 goto end;
454 }
455
456 if (!event->event_header) {
457 BT_LOGV("Event has no current header field: addr=%p, "
458 "event-class-name=\"%s\", event-class-id=%" PRId64,
459 event, bt_ctf_event_class_get_name(event->event_class),
460 bt_ctf_event_class_get_id(event->event_class));
461 goto end;
462 }
463
464 header = event->event_header;
465 bt_get(header);
466 end:
467 return header;
468 }
469
470 int bt_ctf_event_set_header(struct bt_ctf_event *event,
471 struct bt_ctf_field *header)
472 {
473 int ret = 0;
474 struct bt_ctf_field_type *field_type = NULL;
475 struct bt_ctf_stream_class *stream_class = NULL;
476
477 if (!event) {
478 BT_LOGW_STR("Invalid parameter: event is NULL.");
479 ret = -1;
480 goto end;
481 }
482
483 if (event->frozen) {
484 BT_LOGW("Invalid parameter: event is frozen: addr=%p, "
485 "event-class-name=\"%s\", event-class-id=%" PRId64,
486 event, bt_ctf_event_class_get_name(event->event_class),
487 bt_ctf_event_class_get_id(event->event_class));
488 ret = -1;
489 goto end;
490 }
491
492 stream_class = (struct bt_ctf_stream_class *) bt_object_get_parent(
493 event->event_class);
494 /*
495 * Ensure the provided header's type matches the one registered to the
496 * stream class.
497 */
498 if (header) {
499 field_type = bt_ctf_field_get_type(header);
500 if (bt_ctf_field_type_compare(field_type,
501 stream_class->event_header_type)) {
502 BT_LOGW("Invalid parameter: header field type is different from the expected field type: "
503 "event-addr=%p, event-class-name=\"%s\", "
504 "event-class-id=%" PRId64,
505 event,
506 bt_ctf_event_class_get_name(event->event_class),
507 bt_ctf_event_class_get_id(event->event_class));
508 ret = -1;
509 goto end;
510 }
511 } else {
512 if (stream_class->event_header_type) {
513 BT_LOGW("Invalid parameter: setting no event header but event header field type is not NULL: "
514 "event-addr=%p, event-class-name=\"%s\", "
515 "event-class-id=%" PRId64 ", "
516 "event-header-ft-addr=%p",
517 event,
518 bt_ctf_event_class_get_name(event->event_class),
519 bt_ctf_event_class_get_id(event->event_class),
520 stream_class->event_header_type);
521 ret = -1;
522 goto end;
523 }
524 }
525
526 bt_put(event->event_header);
527 event->event_header = bt_get(header);
528 BT_LOGV("Set event's header field: event-addr=%p, "
529 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
530 "header-field-addr=%p",
531 event, bt_ctf_event_class_get_name(event->event_class),
532 bt_ctf_event_class_get_id(event->event_class), header);
533 end:
534 bt_put(stream_class);
535 bt_put(field_type);
536 return ret;
537 }
538
539 struct bt_ctf_field *bt_ctf_event_get_event_context(
540 struct bt_ctf_event *event)
541 {
542 struct bt_ctf_field *context = NULL;
543
544 if (!event) {
545 BT_LOGW_STR("Invalid parameter: event is NULL.");
546 goto end;
547 }
548
549 if (!event->context_payload) {
550 BT_LOGV("Event has no current context field: addr=%p, "
551 "event-class-name=\"%s\", event-class-id=%" PRId64,
552 event, bt_ctf_event_class_get_name(event->event_class),
553 bt_ctf_event_class_get_id(event->event_class));
554 goto end;
555 }
556
557 context = event->context_payload;
558 bt_get(context);
559 end:
560 return context;
561 }
562
563 int bt_ctf_event_set_event_context(struct bt_ctf_event *event,
564 struct bt_ctf_field *context)
565 {
566 int ret = 0;
567 struct bt_ctf_field_type *field_type = NULL;
568
569 if (!event) {
570 BT_LOGW_STR("Invalid parameter: event is NULL.");
571 ret = -1;
572 goto end;
573 }
574
575 if (event->frozen) {
576 BT_LOGW("Invalid parameter: event is frozen: addr=%p, "
577 "event-class-name=\"%s\", event-class-id=%" PRId64,
578 event, bt_ctf_event_class_get_name(event->event_class),
579 bt_ctf_event_class_get_id(event->event_class));
580 ret = -1;
581 goto end;
582 }
583
584 if (context) {
585 field_type = bt_ctf_field_get_type(context);
586
587 if (bt_ctf_field_type_compare(field_type,
588 event->event_class->context)) {
589 BT_LOGW("Invalid parameter: context field type is different from the expected field type: "
590 "event-addr=%p, event-class-name=\"%s\", "
591 "event-class-id=%" PRId64,
592 event,
593 bt_ctf_event_class_get_name(event->event_class),
594 bt_ctf_event_class_get_id(event->event_class));
595 ret = -1;
596 goto end;
597 }
598 } else {
599 if (event->event_class->context) {
600 BT_LOGW("Invalid parameter: setting no event context but event context field type is not NULL: "
601 "event-addr=%p, event-class-name=\"%s\", "
602 "event-class-id=%" PRId64 ", "
603 "event-context-ft-addr=%p",
604 event,
605 bt_ctf_event_class_get_name(event->event_class),
606 bt_ctf_event_class_get_id(event->event_class),
607 event->event_class->context);
608 ret = -1;
609 goto end;
610 }
611 }
612
613 bt_put(event->context_payload);
614 event->context_payload = bt_get(context);
615 BT_LOGV("Set event's context field: event-addr=%p, "
616 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
617 "context-field-addr=%p",
618 event, bt_ctf_event_class_get_name(event->event_class),
619 bt_ctf_event_class_get_id(event->event_class), context);
620 end:
621 bt_put(field_type);
622 return ret;
623 }
624
625 struct bt_ctf_field *bt_ctf_event_get_stream_event_context(
626 struct bt_ctf_event *event)
627 {
628 struct bt_ctf_field *stream_event_context = NULL;
629
630 if (!event) {
631 BT_LOGW_STR("Invalid parameter: event is NULL.");
632 goto end;
633 }
634
635 if (!event->stream_event_context) {
636 BT_LOGV("Event has no current stream event context field: addr=%p, "
637 "event-class-name=\"%s\", event-class-id=%" PRId64,
638 event, bt_ctf_event_class_get_name(event->event_class),
639 bt_ctf_event_class_get_id(event->event_class));
640 goto end;
641 }
642
643 stream_event_context = event->stream_event_context;
644 end:
645 return bt_get(stream_event_context);
646 }
647
648 int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event,
649 struct bt_ctf_field *stream_event_context)
650 {
651 int ret = 0;
652 struct bt_ctf_field_type *field_type = NULL;
653 struct bt_ctf_stream_class *stream_class = NULL;
654
655 if (!event) {
656 BT_LOGW_STR("Invalid parameter: event is NULL.");
657 ret = -1;
658 goto end;
659 }
660
661 if (event->frozen) {
662 BT_LOGW("Invalid parameter: event is frozen: addr=%p, "
663 "event-class-name=\"%s\", event-class-id=%" PRId64,
664 event, bt_ctf_event_class_get_name(event->event_class),
665 bt_ctf_event_class_get_id(event->event_class));
666 ret = -1;
667 goto end;
668 }
669
670 stream_class = bt_ctf_event_class_get_stream_class(event->event_class);
671 /*
672 * We should not have been able to create the event without associating
673 * the event class to a stream class.
674 */
675 assert(stream_class);
676
677 if (stream_event_context) {
678 field_type = bt_ctf_field_get_type(stream_event_context);
679 if (bt_ctf_field_type_compare(field_type,
680 stream_class->event_context_type)) {
681 BT_LOGW("Invalid parameter: stream event context field type is different from the expected field type: "
682 "event-addr=%p, event-class-name=\"%s\", "
683 "event-class-id=%" PRId64,
684 event,
685 bt_ctf_event_class_get_name(event->event_class),
686 bt_ctf_event_class_get_id(event->event_class));
687 ret = -1;
688 goto end;
689 }
690 } else {
691 if (stream_class->event_context_type) {
692 BT_LOGW("Invalid parameter: setting no stream event context but stream event context field type is not NULL: "
693 "event-addr=%p, event-class-name=\"%s\", "
694 "event-class-id=%" PRId64 ", "
695 "stream-event-context-ft-addr=%p",
696 event,
697 bt_ctf_event_class_get_name(event->event_class),
698 bt_ctf_event_class_get_id(event->event_class),
699 stream_class->event_context_type);
700 ret = -1;
701 goto end;
702 }
703 }
704
705 bt_get(stream_event_context);
706 BT_MOVE(event->stream_event_context, stream_event_context);
707 BT_LOGV("Set event's stream event context field: event-addr=%p, "
708 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
709 "stream-event-context-field-addr=%p",
710 event, bt_ctf_event_class_get_name(event->event_class),
711 bt_ctf_event_class_get_id(event->event_class),
712 stream_event_context);
713 end:
714 BT_PUT(stream_class);
715 bt_put(field_type);
716 return ret;
717 }
718
719 /* Pre-2.0 CTF writer backward compatibility */
720 void bt_ctf_event_get(struct bt_ctf_event *event)
721 {
722 bt_get(event);
723 }
724
725 /* Pre-2.0 CTF writer backward compatibility */
726 void bt_ctf_event_put(struct bt_ctf_event *event)
727 {
728 bt_put(event);
729 }
730
731 void bt_ctf_event_destroy(struct bt_object *obj)
732 {
733 struct bt_ctf_event *event;
734
735 event = container_of(obj, struct bt_ctf_event, base);
736 BT_LOGD("Destroying event: addr=%p, "
737 "event-class-name=\"%s\", event-class-id=%" PRId64,
738 event, bt_ctf_event_class_get_name(event->event_class),
739 bt_ctf_event_class_get_id(event->event_class));
740
741 if (!event->base.parent) {
742 /*
743 * Event was keeping a reference to its class since it shared no
744 * common ancestor with it to guarantee they would both have the
745 * same lifetime.
746 */
747 bt_put(event->event_class);
748 }
749 g_hash_table_destroy(event->clock_values);
750 BT_LOGD_STR("Putting event's header field.");
751 bt_put(event->event_header);
752 BT_LOGD_STR("Putting event's stream event context field.");
753 bt_put(event->stream_event_context);
754 BT_LOGD_STR("Putting event's context field.");
755 bt_put(event->context_payload);
756 BT_LOGD_STR("Putting event's payload field.");
757 bt_put(event->fields_payload);
758 BT_LOGD_STR("Putting event's packet.");
759 bt_put(event->packet);
760 g_free(event);
761 }
762
763 struct bt_ctf_clock_value *bt_ctf_event_get_clock_value(
764 struct bt_ctf_event *event, struct bt_ctf_clock_class *clock_class)
765 {
766 struct bt_ctf_clock_value *clock_value = NULL;
767
768 if (!event || !clock_class) {
769 BT_LOGW("Invalid parameter: event or clock class is NULL: "
770 "event-addr=%p, clock-class-addr=%p",
771 event, clock_class);
772 goto end;
773 }
774
775 clock_value = g_hash_table_lookup(event->clock_values, clock_class);
776 if (!clock_value) {
777 BT_LOGV("No clock value associated to the given clock class: "
778 "event-addr=%p, event-class-name=\"%s\", "
779 "event-class-id=%" PRId64 ", clock-class-addr=%p, "
780 "clock-class-name=\"%s\"", event,
781 bt_ctf_event_class_get_name(event->event_class),
782 bt_ctf_event_class_get_id(event->event_class),
783 clock_class, bt_ctf_clock_class_get_name(clock_class));
784 goto end;
785 }
786
787 bt_get(clock_value);
788 end:
789 return clock_value;
790 }
791
792 int bt_ctf_event_set_clock_value(struct bt_ctf_event *event,
793 struct bt_ctf_clock_value *value)
794 {
795 int ret = 0;
796 struct bt_ctf_trace *trace;
797 struct bt_ctf_stream_class *stream_class;
798 struct bt_ctf_event_class *event_class;
799 struct bt_ctf_clock_class *clock_class = NULL;
800
801 if (!event || !value) {
802 BT_LOGW("Invalid parameter: event or clock value is NULL: "
803 "event-addr=%p, clock-value-addr=%p",
804 event, value);
805 ret = -1;
806 goto end;
807 }
808
809 if (event->frozen) {
810 BT_LOGW("Invalid parameter: event is frozen: addr=%p, "
811 "event-class-name=\"%s\", event-class-id=%" PRId64,
812 event, bt_ctf_event_class_get_name(event->event_class),
813 bt_ctf_event_class_get_id(event->event_class));
814 ret = -1;
815 goto end;
816 }
817
818 clock_class = bt_ctf_clock_value_get_class(value);
819 event_class = bt_ctf_event_borrow_event_class(event);
820 assert(event_class);
821 stream_class = bt_ctf_event_class_borrow_stream_class(event_class);
822 assert(stream_class);
823 trace = bt_ctf_stream_class_borrow_trace(stream_class);
824 assert(trace);
825
826 if (!bt_ctf_trace_has_clock_class(trace, clock_class)) {
827 BT_LOGW("Invalid parameter: clock class is not part of event's trace: "
828 "event-addr=%p, event-class-name=\"%s\", "
829 "event-class-id=%" PRId64 ", clock-class-addr=%p, "
830 "clock-class-name=\"%s\"",
831 event, bt_ctf_event_class_get_name(event->event_class),
832 bt_ctf_event_class_get_id(event->event_class),
833 clock_class, bt_ctf_clock_class_get_name(clock_class));
834 ret = -1;
835 goto end;
836 }
837
838 g_hash_table_insert(event->clock_values, clock_class, bt_get(value));
839 BT_LOGV("Set event's clock value: "
840 "event-addr=%p, event-class-name=\"%s\", "
841 "event-class-id=%" PRId64 ", clock-class-addr=%p, "
842 "clock-class-name=\"%s\", clock-value-addr=%p, "
843 "clock-value-cycles=%" PRIu64,
844 event, bt_ctf_event_class_get_name(event->event_class),
845 bt_ctf_event_class_get_id(event->event_class),
846 clock_class, bt_ctf_clock_class_get_name(clock_class),
847 value, value->value);
848 clock_class = NULL;
849
850 end:
851 bt_put(clock_class);
852 return ret;
853 }
854
855 BT_HIDDEN
856 int bt_ctf_event_validate(struct bt_ctf_event *event)
857 {
858 /* Make sure each field's payload has been set */
859 int ret;
860 struct bt_ctf_stream_class *stream_class = NULL;
861
862 assert(event);
863 ret = bt_ctf_field_validate(event->event_header);
864 if (ret) {
865 BT_LOGD("Invalid event's header field: "
866 "event-addr=%p, event-class-name=\"%s\", "
867 "event-class-id=%" PRId64,
868 event, bt_ctf_event_class_get_name(event->event_class),
869 bt_ctf_event_class_get_id(event->event_class));
870 goto end;
871 }
872
873 stream_class = bt_ctf_event_class_get_stream_class(event->event_class);
874 /*
875 * We should not have been able to create the event without associating
876 * the event class to a stream class.
877 */
878 assert(stream_class);
879 if (stream_class->event_context_type) {
880 ret = bt_ctf_field_validate(event->stream_event_context);
881 if (ret) {
882 BT_LOGD("Invalid event's stream event context field: "
883 "event-addr=%p, event-class-name=\"%s\", "
884 "event-class-id=%" PRId64,
885 event,
886 bt_ctf_event_class_get_name(event->event_class),
887 bt_ctf_event_class_get_id(event->event_class));
888 goto end;
889 }
890 }
891
892 ret = bt_ctf_field_validate(event->fields_payload);
893 if (ret) {
894 BT_LOGD("Invalid event's payload field: "
895 "event-addr=%p, event-class-name=\"%s\", "
896 "event-class-id=%" PRId64,
897 event,
898 bt_ctf_event_class_get_name(event->event_class),
899 bt_ctf_event_class_get_id(event->event_class));
900 goto end;
901 }
902
903 if (event->event_class->context) {
904 BT_LOGD("Invalid event's context field: "
905 "event-addr=%p, event-class-name=\"%s\", "
906 "event-class-id=%" PRId64,
907 event,
908 bt_ctf_event_class_get_name(event->event_class),
909 bt_ctf_event_class_get_id(event->event_class));
910 ret = bt_ctf_field_validate(event->context_payload);
911 }
912 end:
913 bt_put(stream_class);
914 return ret;
915 }
916
917 BT_HIDDEN
918 int bt_ctf_event_serialize(struct bt_ctf_event *event,
919 struct bt_ctf_stream_pos *pos,
920 enum bt_ctf_byte_order native_byte_order)
921 {
922 int ret = 0;
923
924 assert(event);
925 assert(pos);
926
927 BT_LOGV_STR("Serializing event's context field.");
928 if (event->context_payload) {
929 ret = bt_ctf_field_serialize(event->context_payload, pos,
930 native_byte_order);
931 if (ret) {
932 BT_LOGW("Cannot serialize event's context field: "
933 "event-addr=%p, event-class-name=\"%s\", "
934 "event-class-id=%" PRId64,
935 event,
936 bt_ctf_event_class_get_name(event->event_class),
937 bt_ctf_event_class_get_id(event->event_class));
938 goto end;
939 }
940 }
941
942 BT_LOGV_STR("Serializing event's payload field.");
943 if (event->fields_payload) {
944 ret = bt_ctf_field_serialize(event->fields_payload, pos,
945 native_byte_order);
946 if (ret) {
947 BT_LOGW("Cannot serialize event's payload field: "
948 "event-addr=%p, event-class-name=\"%s\", "
949 "event-class-id=%" PRId64,
950 event,
951 bt_ctf_event_class_get_name(event->event_class),
952 bt_ctf_event_class_get_id(event->event_class));
953 goto end;
954 }
955 }
956 end:
957 return ret;
958 }
959
960 struct bt_ctf_packet *bt_ctf_event_get_packet(struct bt_ctf_event *event)
961 {
962 struct bt_ctf_packet *packet = NULL;
963
964 if (!event) {
965 BT_LOGW_STR("Invalid parameter: event is NULL.");
966 goto end;
967 }
968
969 if (!event->packet) {
970 BT_LOGV("Event has no current packet: addr=%p, "
971 "event-class-name=\"%s\", event-class-id=%" PRId64,
972 event, bt_ctf_event_class_get_name(event->event_class),
973 bt_ctf_event_class_get_id(event->event_class));
974 goto end;
975 }
976
977 packet = bt_get(event->packet);
978 end:
979 return packet;
980 }
981
982 int bt_ctf_event_set_packet(struct bt_ctf_event *event,
983 struct bt_ctf_packet *packet)
984 {
985 struct bt_ctf_stream_class *event_stream_class = NULL;
986 struct bt_ctf_stream_class *packet_stream_class = NULL;
987 struct bt_ctf_stream *stream = NULL;
988 int ret = 0;
989
990 if (!event || !packet) {
991 BT_LOGW("Invalid parameter: event or packet is NULL: "
992 "event-addr=%p, packet-addr=%p",
993 event, packet);
994 ret = -1;
995 goto end;
996 }
997
998 if (event->frozen) {
999 BT_LOGW("Invalid parameter: event is frozen: addr=%p, "
1000 "event-class-name=\"%s\", event-class-id=%" PRId64,
1001 event, bt_ctf_event_class_get_name(event->event_class),
1002 bt_ctf_event_class_get_id(event->event_class));
1003 ret = -1;
1004 goto end;
1005 }
1006
1007 /*
1008 * Make sure the new packet was created by this event's
1009 * stream, if it is set.
1010 */
1011 stream = bt_ctf_event_get_stream(event);
1012 if (stream) {
1013 if (packet->stream != stream) {
1014 BT_LOGW("Invalid parameter: packet's stream and event's stream differ: "
1015 "event-addr=%p, event-class-name=\"%s\", "
1016 "event-class-id=%" PRId64 ", packet-stream-addr=%p, "
1017 "event-stream-addr=%p",
1018 event, bt_ctf_event_class_get_name(event->event_class),
1019 bt_ctf_event_class_get_id(event->event_class),
1020 packet->stream, stream);
1021 ret = -1;
1022 goto end;
1023 }
1024 } else {
1025 event_stream_class =
1026 bt_ctf_event_class_get_stream_class(event->event_class);
1027 packet_stream_class =
1028 bt_ctf_stream_get_class(packet->stream);
1029
1030 assert(event_stream_class);
1031 assert(packet_stream_class);
1032
1033 if (event_stream_class != packet_stream_class) {
1034 BT_LOGW("Invalid parameter: packet's stream class and event's stream class differ: "
1035 "event-addr=%p, event-class-name=\"%s\", "
1036 "event-class-id=%" PRId64 ", packet-stream-class-addr=%p, "
1037 "event-stream-class-addr=%p",
1038 event, bt_ctf_event_class_get_name(event->event_class),
1039 bt_ctf_event_class_get_id(event->event_class),
1040 packet_stream_class, event_stream_class);
1041 ret = -1;
1042 goto end;
1043 }
1044 }
1045
1046 bt_get(packet);
1047 BT_MOVE(event->packet, packet);
1048 BT_LOGV("Set event's packet: event-addr=%p, "
1049 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1050 "packet-addr=%p",
1051 event, bt_ctf_event_class_get_name(event->event_class),
1052 bt_ctf_event_class_get_id(event->event_class), packet);
1053
1054 end:
1055 BT_PUT(stream);
1056 BT_PUT(event_stream_class);
1057 BT_PUT(packet_stream_class);
1058
1059 return ret;
1060 }
1061
1062 BT_HIDDEN
1063 void bt_ctf_event_freeze(struct bt_ctf_event *event)
1064 {
1065 assert(event);
1066
1067 if (event->frozen) {
1068 return;
1069 }
1070
1071 BT_LOGD("Freezing event: addr=%p, "
1072 "event-class-name=\"%s\", event-class-id=%" PRId64,
1073 event, bt_ctf_event_class_get_name(event->event_class),
1074 bt_ctf_event_class_get_id(event->event_class));
1075 bt_ctf_packet_freeze(event->packet);
1076 BT_LOGD_STR("Freezing event's header field.");
1077 bt_ctf_field_freeze(event->event_header);
1078 BT_LOGD_STR("Freezing event's stream event context field.");
1079 bt_ctf_field_freeze(event->stream_event_context);
1080 BT_LOGD_STR("Freezing event's context field.");
1081 bt_ctf_field_freeze(event->context_payload);
1082 BT_LOGD_STR("Freezing event's payload field.");
1083 bt_ctf_field_freeze(event->fields_payload);
1084 event->frozen = 1;
1085 }
This page took 0.049961 seconds and 3 git commands to generate.