Add time seek interface stub
[babeltrace.git] / formats / 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
2e33ac5a
PP
29#include <babeltrace/ctf-ir/fields-internal.h>
30#include <babeltrace/ctf-ir/field-types-internal.h>
24626e8b 31#include <babeltrace/ctf-ir/clock-internal.h>
adc315b8 32#include <babeltrace/ctf-ir/event-internal.h>
272df73e
PP
33#include <babeltrace/ctf-ir/event-class.h>
34#include <babeltrace/ctf-ir/event-class-internal.h>
2f100782 35#include <babeltrace/ctf-ir/stream-class.h>
c35a1669 36#include <babeltrace/ctf-ir/stream-class-internal.h>
41ac640a 37#include <babeltrace/ctf-ir/stream-internal.h>
4ce9f9d0
PP
38#include <babeltrace/ctf-ir/packet.h>
39#include <babeltrace/ctf-ir/packet-internal.h>
bc37ae52 40#include <babeltrace/ctf-ir/trace-internal.h>
09840de5 41#include <babeltrace/ctf-ir/validation-internal.h>
5c3b707d 42#include <babeltrace/ctf-ir/packet-internal.h>
654c1444 43#include <babeltrace/ctf-ir/utils.h>
83509119 44#include <babeltrace/ref.h>
44e0a4f5 45#include <babeltrace/ctf-ir/attributes-internal.h>
273b65be
JG
46#include <babeltrace/compiler.h>
47
273b65be 48static
83509119 49void bt_ctf_event_destroy(struct bt_object *obj);
662e778c
JG
50static
51int set_integer_field_value(struct bt_ctf_field *field, uint64_t value);
273b65be 52
273b65be
JG
53struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
54{
09840de5
PP
55 int ret;
56 enum bt_ctf_validation_flag validation_flags =
57 BT_CTF_VALIDATION_FLAG_STREAM |
58 BT_CTF_VALIDATION_FLAG_EVENT;
273b65be 59 struct bt_ctf_event *event = NULL;
09840de5 60 struct bt_ctf_trace *trace = NULL;
e6a8e8e4 61 struct bt_ctf_stream_class *stream_class = NULL;
09840de5
PP
62 struct bt_ctf_field_type *packet_header_type = NULL;
63 struct bt_ctf_field_type *packet_context_type = NULL;
64 struct bt_ctf_field_type *event_header_type = NULL;
65 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
66 struct bt_ctf_field_type *event_context_type = NULL;
67 struct bt_ctf_field_type *event_payload_type = NULL;
68 struct bt_ctf_field *event_header = NULL;
5fd2e9fd 69 struct bt_ctf_field *stream_event_context = NULL;
09840de5
PP
70 struct bt_ctf_field *event_context = NULL;
71 struct bt_ctf_field *event_payload = NULL;
72 struct bt_value *environment = NULL;
73 struct bt_ctf_validation_output validation_output = { 0 };
74 int trace_valid = 0;
273b65be
JG
75
76 if (!event_class) {
e6a8e8e4 77 goto error;
273b65be
JG
78 }
79
e6a8e8e4 80 stream_class = bt_ctf_event_class_get_stream_class(event_class);
09840de5 81
662e778c 82 /*
e6a8e8e4
JG
83 * We disallow the creation of an event if its event class has not been
84 * associated to a stream class.
662e778c 85 */
e6a8e8e4
JG
86 if (!stream_class) {
87 goto error;
662e778c 88 }
09840de5
PP
89
90 /* A stream class should always have an existing event header type */
e6a8e8e4 91 assert(stream_class->event_header_type);
09840de5
PP
92
93 /* The event class was frozen when added to its stream class */
94 assert(event_class->frozen);
95
96 /* Validate the trace (if any), the stream class, and the event class */
97 trace = bt_ctf_stream_class_get_trace(stream_class);
98 if (trace) {
99 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
100 trace_valid = trace->valid;
101 assert(trace_valid);
102 environment = trace->environment;
103 }
104
105 packet_context_type = bt_ctf_stream_class_get_packet_context_type(
106 stream_class);
107 event_header_type = bt_ctf_stream_class_get_event_header_type(
108 stream_class);
109 stream_event_ctx_type = bt_ctf_stream_class_get_event_context_type(
110 stream_class);
111 event_context_type = bt_ctf_event_class_get_context_type(event_class);
112 event_payload_type = bt_ctf_event_class_get_payload_type(event_class);
113 ret = bt_ctf_validate_class_types(environment, packet_header_type,
114 packet_context_type, event_header_type, stream_event_ctx_type,
115 event_context_type, event_payload_type, trace_valid,
116 stream_class->valid, event_class->valid,
117 &validation_output, validation_flags);
118 BT_PUT(packet_header_type);
119 BT_PUT(packet_context_type);
120 BT_PUT(event_header_type);
121 BT_PUT(stream_event_ctx_type);
122 BT_PUT(event_context_type);
123 BT_PUT(event_payload_type);
09840de5
PP
124 if (ret) {
125 /*
126 * This means something went wrong during the validation
127 * process, not that the objects are invalid.
128 */
129 goto error;
130 }
131
132 if ((validation_output.valid_flags & validation_flags) !=
133 validation_flags) {
134 /* Invalid trace/stream class/event class */
135 goto error;
136 }
137
138 /*
139 * At this point we know the trace (if associated to the stream
140 * class), the stream class, and the event class, with their
141 * current types, are valid. We may proceed with creating
142 * the event.
143 */
b8248cc0
PP
144 event = g_new0(struct bt_ctf_event, 1);
145 if (!event) {
e6a8e8e4 146 goto error;
b8248cc0
PP
147 }
148
83509119 149 bt_object_init(event, bt_ctf_event_destroy);
09840de5 150
e6a8e8e4
JG
151 /*
152 * event does not share a common ancestor with the event class; it has
153 * to guarantee its existence by holding a reference. This reference
154 * shall be released once the event is associated to a stream since,
155 * from that point, the event and its class will share the same
156 * lifetime.
157 */
158 event->event_class = bt_get(event_class);
41ac640a 159 event->clock_values = g_hash_table_new_full(g_direct_hash,
1556a1af 160 g_direct_equal, bt_put, bt_put);
09840de5
PP
161 event_header =
162 bt_ctf_field_create(validation_output.event_header_type);
09840de5 163 if (!event_header) {
83509119 164 goto error;
662e778c 165 }
09840de5 166
5fd2e9fd
PP
167 if (validation_output.stream_event_ctx_type) {
168 stream_event_context = bt_ctf_field_create(
169 validation_output.stream_event_ctx_type);
170 if (!stream_event_context) {
171 goto error;
172 }
173 }
174
09840de5
PP
175 if (validation_output.event_context_type) {
176 event_context = bt_ctf_field_create(
177 validation_output.event_context_type);
178 if (!event_context) {
83509119 179 goto error;
662e778c 180 }
f655a84d 181 }
09840de5
PP
182
183 if (validation_output.event_payload_type) {
184 event_payload = bt_ctf_field_create(
185 validation_output.event_payload_type);
186 if (!event_payload) {
187 goto error;
188 }
662e778c
JG
189 }
190
09840de5
PP
191 /*
192 * At this point all the fields are created, potentially from
193 * validated copies of field types, so that the field types and
194 * fields can be replaced in the trace, stream class,
195 * event class, and created event.
196 */
197 bt_ctf_validation_replace_types(trace, stream_class,
198 event_class, &validation_output, validation_flags);
199 BT_MOVE(event->event_header, event_header);
5fd2e9fd 200 BT_MOVE(event->stream_event_context, stream_event_context);
09840de5
PP
201 BT_MOVE(event->context_payload, event_context);
202 BT_MOVE(event->fields_payload, event_payload);
203
204 /*
205 * Put what was not moved in bt_ctf_validation_replace_types().
206 */
207 bt_ctf_validation_output_put_types(&validation_output);
208
662e778c
JG
209 /*
210 * Freeze the stream class since the event header must not be changed
211 * anymore.
212 */
e6a8e8e4 213 bt_ctf_stream_class_freeze(stream_class);
09840de5
PP
214
215 /*
216 * Mark stream class, and event class as valid since
217 * they're all frozen now.
218 */
219 stream_class->valid = 1;
220 event_class->valid = 1;
221
222 /* Put stuff we borrowed from the event class */
e6a8e8e4 223 BT_PUT(stream_class);
09840de5
PP
224 BT_PUT(trace);
225
273b65be 226 return event;
09840de5 227
83509119 228error:
09840de5 229 bt_ctf_validation_output_put_types(&validation_output);
83509119 230 BT_PUT(event);
e6a8e8e4 231 BT_PUT(stream_class);
09840de5
PP
232 BT_PUT(trace);
233 BT_PUT(event_header);
5fd2e9fd 234 BT_PUT(stream_event_context);
09840de5
PP
235 BT_PUT(event_context);
236 BT_PUT(event_payload);
237 assert(!packet_header_type);
238 assert(!packet_context_type);
239 assert(!event_header_type);
240 assert(!stream_event_ctx_type);
241 assert(!event_context_type);
242 assert(!event_payload_type);
243
83509119 244 return event;
273b65be
JG
245}
246
2f100782
JG
247struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event)
248{
249 struct bt_ctf_event_class *event_class = NULL;
250
251 if (!event) {
252 goto end;
253 }
254
255 event_class = event->event_class;
83509119 256 bt_get(event_class);
2f100782
JG
257end:
258 return event_class;
259}
260
8e5003bb
JG
261struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event)
262{
cacf3147
PP
263 struct bt_ctf_stream *stream = NULL;
264
265 if (!event) {
266 goto end;
267 }
268
269 /*
270 * If the event has a parent, then this is its (writer) stream.
271 * If the event has no parent, then if it has a packet, this
272 * is its (non-writer) stream.
273 */
274 if (event->base.parent) {
275 stream = (struct bt_ctf_stream *) bt_object_get_parent(event);
276 } else {
277 if (event->packet) {
278 stream = bt_get(event->packet->stream);
279 }
280 }
281
282end:
283 return stream;
8e5003bb
JG
284}
285
273b65be
JG
286int bt_ctf_event_set_payload(struct bt_ctf_event *event,
287 const char *name,
c5a9aa19 288 struct bt_ctf_field *payload)
273b65be
JG
289{
290 int ret = 0;
291
4ce9f9d0 292 if (!event || !payload || event->frozen) {
273b65be
JG
293 ret = -1;
294 goto end;
295 }
296
c5a9aa19
JG
297 if (name) {
298 ret = bt_ctf_field_structure_set_field(event->fields_payload,
299 name, payload);
300 } else {
301 struct bt_ctf_field_type *payload_type;
302
303 payload_type = bt_ctf_field_get_type(payload);
09840de5
PP
304
305 if (bt_ctf_field_type_compare(payload_type,
306 event->event_class->fields) == 0) {
83509119
JG
307 bt_put(event->fields_payload);
308 bt_get(payload);
c5a9aa19
JG
309 event->fields_payload = payload;
310 } else {
311 ret = -1;
312 }
313
83509119 314 bt_put(payload_type);
c5a9aa19 315 }
273b65be
JG
316end:
317 return ret;
318}
319
71362d53
PP
320struct bt_ctf_field *bt_ctf_event_get_payload_field(struct bt_ctf_event *event)
321{
322 struct bt_ctf_field *payload = NULL;
323
324 if (!event || !event->fields_payload) {
325 goto end;
326 }
327
328 payload = event->fields_payload;
83509119 329 bt_get(payload);
71362d53
PP
330end:
331 return payload;
332}
273b65be 333
e5e6eb3a
JG
334int bt_ctf_event_set_payload_field(struct bt_ctf_event *event,
335 struct bt_ctf_field *payload)
336{
337 int ret = 0;
338 struct bt_ctf_field_type *payload_type = NULL;
339
4ce9f9d0 340 if (!event || !payload || event->frozen) {
e5e6eb3a
JG
341 ret = -1;
342 goto end;
343 }
344
345 payload_type = bt_ctf_field_get_type(payload);
346 if (!payload_type) {
347 ret = -1;
348 goto end;
349 }
350
9a19a512
PP
351 if (bt_ctf_field_type_get_type_id(payload_type) !=
352 BT_CTF_TYPE_ID_STRUCT) {
e5e6eb3a
JG
353 ret = -1;
354 goto end;
355 }
356
83509119
JG
357 bt_get(payload);
358 bt_put(event->fields_payload);
e5e6eb3a
JG
359 event->fields_payload = payload;
360
361end:
83509119 362 bt_put(payload_type);
e5e6eb3a
JG
363 return ret;
364}
365
273b65be
JG
366struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event,
367 const char *name)
368{
369 struct bt_ctf_field *field = NULL;
370
c5a9aa19 371 if (!event) {
273b65be
JG
372 goto end;
373 }
374
c5a9aa19
JG
375 if (name) {
376 field = bt_ctf_field_structure_get_field(event->fields_payload,
377 name);
378 } else {
379 field = event->fields_payload;
83509119 380 bt_get(field);
c5a9aa19 381 }
273b65be
JG
382end:
383 return field;
384}
385
2f100782 386struct bt_ctf_field *bt_ctf_event_get_payload_by_index(
074ee56d 387 struct bt_ctf_event *event, int index)
2f100782
JG
388{
389 struct bt_ctf_field *field = NULL;
390
074ee56d 391 if (!event || index < 0) {
2f100782
JG
392 goto end;
393 }
394
395 field = bt_ctf_field_structure_get_field_by_index(event->fields_payload,
396 index);
397end:
398 return field;
399}
400
286a2840 401struct bt_ctf_field *bt_ctf_event_get_header(
662e778c
JG
402 struct bt_ctf_event *event)
403{
404 struct bt_ctf_field *header = NULL;
405
406 if (!event || !event->event_header) {
407 goto end;
408 }
409
410 header = event->event_header;
83509119 411 bt_get(header);
662e778c
JG
412end:
413 return header;
414}
415
286a2840 416int bt_ctf_event_set_header(struct bt_ctf_event *event,
662e778c
JG
417 struct bt_ctf_field *header)
418{
419 int ret = 0;
420 struct bt_ctf_field_type *field_type = NULL;
e6a8e8e4 421 struct bt_ctf_stream_class *stream_class = NULL;
662e778c 422
4ce9f9d0 423 if (!event || !header || event->frozen) {
662e778c
JG
424 ret = -1;
425 goto end;
426 }
427
e6a8e8e4
JG
428 stream_class = (struct bt_ctf_stream_class *) bt_object_get_parent(
429 event->event_class);
662e778c
JG
430 /*
431 * Ensure the provided header's type matches the one registered to the
432 * stream class.
433 */
434 field_type = bt_ctf_field_get_type(header);
09840de5
PP
435 if (bt_ctf_field_type_compare(field_type,
436 stream_class->event_header_type)) {
662e778c
JG
437 ret = -1;
438 goto end;
439 }
440
83509119
JG
441 bt_get(header);
442 bt_put(event->event_header);
662e778c
JG
443 event->event_header = header;
444end:
e6a8e8e4 445 bt_put(stream_class);
83509119 446 bt_put(field_type);
662e778c
JG
447 return ret;
448}
449
f655a84d
JG
450struct bt_ctf_field *bt_ctf_event_get_event_context(
451 struct bt_ctf_event *event)
452{
453 struct bt_ctf_field *context = NULL;
454
455 if (!event || !event->context_payload) {
456 goto end;
457 }
458
459 context = event->context_payload;
83509119 460 bt_get(context);
f655a84d
JG
461end:
462 return context;
463}
464
465int bt_ctf_event_set_event_context(struct bt_ctf_event *event,
466 struct bt_ctf_field *context)
467{
468 int ret = 0;
469 struct bt_ctf_field_type *field_type = NULL;
470
4ce9f9d0 471 if (!event || !context || event->frozen) {
f655a84d
JG
472 ret = -1;
473 goto end;
474 }
475
476 field_type = bt_ctf_field_get_type(context);
09840de5
PP
477 if (bt_ctf_field_type_compare(field_type,
478 event->event_class->context)) {
f655a84d
JG
479 ret = -1;
480 goto end;
481 }
482
83509119
JG
483 bt_get(context);
484 bt_put(event->context_payload);
f655a84d
JG
485 event->context_payload = context;
486end:
83509119 487 bt_put(field_type);
f655a84d
JG
488 return ret;
489}
490
5fd2e9fd
PP
491struct bt_ctf_field *bt_ctf_event_get_stream_event_context(
492 struct bt_ctf_event *event)
493{
494 struct bt_ctf_field *stream_event_context = NULL;
495
496 if (!event || !event->stream_event_context) {
497 goto end;
498 }
499
500 stream_event_context = event->stream_event_context;
501end:
502 return bt_get(stream_event_context);
503}
504
505int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event,
506 struct bt_ctf_field *stream_event_context)
507{
508 int ret = 0;
509 struct bt_ctf_field_type *field_type = NULL;
510 struct bt_ctf_stream_class *stream_class = NULL;
511
4ce9f9d0 512 if (!event || !stream_event_context || event->frozen) {
5fd2e9fd
PP
513 ret = -1;
514 goto end;
515 }
516
517 stream_class = bt_ctf_event_class_get_stream_class(event->event_class);
518 /*
519 * We should not have been able to create the event without associating
520 * the event class to a stream class.
521 */
522 assert(stream_class);
523
524 field_type = bt_ctf_field_get_type(stream_event_context);
525 if (bt_ctf_field_type_compare(field_type,
526 stream_class->event_context_type)) {
527 ret = -1;
528 goto end;
529 }
530
531 bt_get(stream_event_context);
532 BT_MOVE(event->stream_event_context, stream_event_context);
533end:
534 BT_PUT(stream_class);
535 bt_put(field_type);
536 return ret;
537}
538
273b65be
JG
539void bt_ctf_event_get(struct bt_ctf_event *event)
540{
83509119 541 bt_get(event);
273b65be
JG
542}
543
544void bt_ctf_event_put(struct bt_ctf_event *event)
545{
83509119 546 bt_put(event);
273b65be
JG
547}
548
83509119 549void bt_ctf_event_destroy(struct bt_object *obj)
273b65be
JG
550{
551 struct bt_ctf_event *event;
552
83509119 553 event = container_of(obj, struct bt_ctf_event, base);
e6a8e8e4
JG
554 if (!event->base.parent) {
555 /*
556 * Event was keeping a reference to its class since it shared no
557 * common ancestor with it to guarantee they would both have the
558 * same lifetime.
559 */
560 bt_put(event->event_class);
561 }
41ac640a 562 g_hash_table_destroy(event->clock_values);
83509119 563 bt_put(event->event_header);
5fd2e9fd 564 bt_put(event->stream_event_context);
83509119
JG
565 bt_put(event->context_payload);
566 bt_put(event->fields_payload);
5c3b707d 567 bt_put(event->packet);
273b65be
JG
568 g_free(event);
569}
570
1556a1af
JG
571struct bt_ctf_clock_value *bt_ctf_event_get_clock_value(
572 struct bt_ctf_event *event, struct bt_ctf_clock *clock)
78586d8a 573{
1556a1af 574 struct bt_ctf_clock_value *clock_value = NULL;
78586d8a
JG
575
576 if (!event || !clock) {
577 goto end;
578 }
579
580 clock_value = g_hash_table_lookup(event->clock_values, clock);
581 if (!clock_value) {
582 goto end;
583 }
584
1556a1af
JG
585 bt_get(clock_value);
586end:
587 return clock_value;
588}
589
590int bt_ctf_event_set_clock_value(struct bt_ctf_event *event,
591 struct bt_ctf_clock *clock, struct bt_ctf_clock_value *value)
592{
593 int ret = 0;
594
595 if (!event || !clock || !value || event->frozen) {
596 ret = -1;
597 goto end;
598 }
78586d8a 599
1556a1af 600 g_hash_table_insert(event->clock_values, bt_get(clock), bt_get(value));
78586d8a
JG
601end:
602 return ret;
603}
604
662e778c
JG
605static
606int set_integer_field_value(struct bt_ctf_field* field, uint64_t value)
607{
608 int ret = 0;
609 struct bt_ctf_field_type *field_type = NULL;
610
611 if (!field) {
612 ret = -1;
613 goto end;
614 }
615
662e778c
JG
616 field_type = bt_ctf_field_get_type(field);
617 assert(field_type);
618
9a19a512
PP
619 if (bt_ctf_field_type_get_type_id(field_type) !=
620 BT_CTF_TYPE_ID_INTEGER) {
662e778c
JG
621 /* Not an integer and the value is unset, error. */
622 ret = -1;
623 goto end;
624 }
625
626 if (bt_ctf_field_type_integer_get_signed(field_type)) {
627 ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value);
628 if (ret) {
629 /* Value is out of range, error. */
630 goto end;
631 }
632 } else {
633 ret = bt_ctf_field_unsigned_integer_set_value(field, value);
634 if (ret) {
635 /* Value is out of range, error. */
636 goto end;
637 }
638 }
639end:
83509119 640 bt_put(field_type);
662e778c
JG
641 return ret;
642}
643
273b65be
JG
644BT_HIDDEN
645int bt_ctf_event_validate(struct bt_ctf_event *event)
646{
647 /* Make sure each field's payload has been set */
648 int ret;
5fd2e9fd 649 struct bt_ctf_stream_class *stream_class = NULL;
273b65be
JG
650
651 assert(event);
662e778c
JG
652 ret = bt_ctf_field_validate(event->event_header);
653 if (ret) {
654 goto end;
655 }
656
5fd2e9fd
PP
657 stream_class = bt_ctf_event_class_get_stream_class(event->event_class);
658 /*
659 * We should not have been able to create the event without associating
660 * the event class to a stream class.
661 */
662 assert(stream_class);
663 if (stream_class->event_context_type) {
664 ret = bt_ctf_field_validate(event->stream_event_context);
665 if (ret) {
666 goto end;
667 }
668 }
669
273b65be
JG
670 ret = bt_ctf_field_validate(event->fields_payload);
671 if (ret) {
672 goto end;
673 }
674
675 if (event->event_class->context) {
676 ret = bt_ctf_field_validate(event->context_payload);
677 }
678end:
5fd2e9fd 679 bt_put(stream_class);
273b65be
JG
680 return ret;
681}
682
683BT_HIDDEN
684int bt_ctf_event_serialize(struct bt_ctf_event *event,
685 struct ctf_stream_pos *pos)
686{
687 int ret = 0;
688
689 assert(event);
690 assert(pos);
691 if (event->context_payload) {
692 ret = bt_ctf_field_serialize(event->context_payload, pos);
693 if (ret) {
694 goto end;
695 }
696 }
697
698 if (event->fields_payload) {
699 ret = bt_ctf_field_serialize(event->fields_payload, pos);
700 if (ret) {
701 goto end;
702 }
703 }
704end:
705 return ret;
706}
707
662e778c
JG
708BT_HIDDEN
709int bt_ctf_event_populate_event_header(struct bt_ctf_event *event)
710{
711 int ret = 0;
712 struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL;
24626e8b 713 struct bt_ctf_clock *mapped_clock = NULL;
662e778c 714
4ce9f9d0 715 if (!event || event->frozen) {
662e778c
JG
716 ret = -1;
717 goto end;
718 }
719
720 id_field = bt_ctf_field_structure_get_field(event->event_header, "id");
24626e8b 721 if (id_field && !bt_ctf_field_is_set(id_field)) {
662e778c 722 ret = set_integer_field_value(id_field,
b8248cc0 723 (uint64_t) bt_ctf_event_class_get_id(
24626e8b 724 event->event_class));
662e778c
JG
725 if (ret) {
726 goto end;
727 }
728 }
729
730 timestamp_field = bt_ctf_field_structure_get_field(event->event_header,
24626e8b
JG
731 "timestamp");
732 if (timestamp_field && !bt_ctf_field_is_set(timestamp_field)) {
9a220c32
JG
733 struct bt_ctf_field_type *timestamp_field_type =
734 bt_ctf_field_get_type(timestamp_field);
9a220c32
JG
735
736 assert(timestamp_field_type);
737 mapped_clock = bt_ctf_field_type_integer_get_mapped_clock(
24626e8b 738 timestamp_field_type);
83509119 739 bt_put(timestamp_field_type);
9a220c32 740 if (mapped_clock) {
61ec14e6 741 /*
24626e8b
JG
742 * Babeltrace 2.0 introduced a notion of per-event clock
743 * values which can be queried using a "clock" instance.
744 *
745 * However, the original CTF-Writer (BT 1.x) had a
746 * notion of per-stream clock which is sampled on
747 * a call to append an event to a stream.
748 *
749 * If the event has a clock value associated with the
750 * mapped clock, we ignore the pre-2.0 behaviour and
751 * populate the timestamp field using the clock value.
752 */
753 uint64_t timestamp = 0;
754 struct bt_ctf_clock_value *clock_value;
755
756 clock_value = bt_ctf_event_get_clock_value(event,
757 mapped_clock);
758 if (clock_value) {
759 ret = bt_ctf_clock_value_get_value(clock_value,
760 &timestamp);
761 bt_put(clock_value);
762 if (ret) {
763 goto end;
764 }
765 } else {
766 ret = bt_ctf_clock_get_value(mapped_clock,
767 &timestamp);
768 if (ret) {
769 goto end;
770 }
9a220c32
JG
771 }
772
773 ret = set_integer_field_value(timestamp_field,
24626e8b 774 timestamp);
9a220c32
JG
775 if (ret) {
776 goto end;
777 }
662e778c
JG
778 }
779 }
780end:
83509119
JG
781 bt_put(id_field);
782 bt_put(timestamp_field);
24626e8b 783 bt_put(mapped_clock);
662e778c
JG
784 return ret;
785}
123fbdec 786
5c0f40f4
JG
787struct bt_ctf_packet *bt_ctf_event_get_packet(struct bt_ctf_event *event)
788{
789 struct bt_ctf_packet *packet = NULL;
790
791 if (!event || !event->packet) {
792 goto end;
793 }
794
795 packet = bt_get(event->packet);
796end:
797 return packet;
798}
799
5c3b707d
PP
800int bt_ctf_event_set_packet(struct bt_ctf_event *event,
801 struct bt_ctf_packet *packet)
802{
b2b635e9
PP
803 struct bt_ctf_stream_class *event_stream_class = NULL;
804 struct bt_ctf_stream_class *packet_stream_class = NULL;
5c3b707d
PP
805 struct bt_ctf_stream *stream = NULL;
806 int ret = 0;
807
4ce9f9d0 808 if (!event || !packet || event->frozen) {
5c3b707d
PP
809 ret = -1;
810 goto end;
811 }
812
813 /*
814 * Make sure the new packet was created by this event's
815 * stream, if it is set.
816 */
817 stream = bt_ctf_event_get_stream(event);
818 if (stream) {
819 if (packet->stream != stream) {
820 ret = -1;
821 goto end;
822 }
823 } else {
b2b635e9
PP
824 event_stream_class =
825 bt_ctf_event_class_get_stream_class(event->event_class);
826 packet_stream_class =
827 bt_ctf_stream_get_class(packet->stream);
828
829 assert(event_stream_class);
830 assert(packet_stream_class);
831
832 if (event_stream_class != packet_stream_class) {
833 ret = -1;
834 goto end;
835 }
5c3b707d
PP
836 }
837
cacf3147
PP
838 bt_get(packet);
839 BT_MOVE(event->packet, packet);
5c3b707d
PP
840
841end:
842 BT_PUT(stream);
b2b635e9
PP
843 BT_PUT(event_stream_class);
844 BT_PUT(packet_stream_class);
5c3b707d
PP
845
846 return ret;
847}
4ce9f9d0
PP
848
849BT_HIDDEN
850void bt_ctf_event_freeze(struct bt_ctf_event *event)
851{
852 assert(event);
853 bt_ctf_packet_freeze(event->packet);
854 bt_ctf_field_freeze(event->event_header);
855 bt_ctf_field_freeze(event->stream_event_context);
856 bt_ctf_field_freeze(event->context_payload);
857 bt_ctf_field_freeze(event->fields_payload);
858 event->frozen = 1;
859}
This page took 0.077352 seconds and 4 git commands to generate.