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