lib/ctf-ir/field-types.c: logging: log struct/var FT field destruction
[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
PP
100
101 /* A stream class should always have an existing event header type */
e6a8e8e4 102 assert(stream_class->event_header_type);
09840de5
PP
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) {
e0bcd1b0 110 BT_LOGD_STR("Event's class is part of a trace.");
09840de5
PP
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);
09840de5
PP
136 if (ret) {
137 /*
138 * This means something went wrong during the validation
139 * process, not that the objects are invalid.
140 */
e0bcd1b0 141 BT_LOGE("Failed to validate event and parents: ret=%d", ret);
09840de5
PP
142 goto error;
143 }
144
145 if ((validation_output.valid_flags & validation_flags) !=
146 validation_flags) {
147 /* Invalid trace/stream class/event class */
7b010242 148 BT_LOGE("Invalid trace, stream class, or event class: "
e0bcd1b0 149 "valid-flags=0x%x", validation_output.valid_flags);
09840de5
PP
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 */
b8248cc0
PP
159 event = g_new0(struct bt_ctf_event, 1);
160 if (!event) {
7b010242 161 BT_LOGE_STR("Failed to allocate one event.");
e6a8e8e4 162 goto error;
b8248cc0
PP
163 }
164
83509119 165 bt_object_init(event, bt_ctf_event_destroy);
09840de5 166
e6a8e8e4
JG
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);
41ac640a 175 event->clock_values = g_hash_table_new_full(g_direct_hash,
1556a1af 176 g_direct_equal, bt_put, bt_put);
09840de5
PP
177 event_header =
178 bt_ctf_field_create(validation_output.event_header_type);
09840de5 179 if (!event_header) {
7b010242 180 BT_LOGE_STR("Cannot create initial event header field object.");
83509119 181 goto error;
662e778c 182 }
09840de5 183
5fd2e9fd
PP
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) {
7b010242 188 BT_LOGE_STR("Cannot create initial stream event context field object.");
5fd2e9fd
PP
189 goto error;
190 }
191 }
192
09840de5
PP
193 if (validation_output.event_context_type) {
194 event_context = bt_ctf_field_create(
195 validation_output.event_context_type);
196 if (!event_context) {
7b010242 197 BT_LOGE_STR("Cannot create initial event context field object.");
83509119 198 goto error;
662e778c 199 }
f655a84d 200 }
09840de5
PP
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) {
7b010242 206 BT_LOGE_STR("Cannot create initial event payload field object.");
09840de5
PP
207 goto error;
208 }
662e778c
JG
209 }
210
09840de5
PP
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);
5fd2e9fd 220 BT_MOVE(event->stream_event_context, stream_event_context);
09840de5
PP
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
662e778c
JG
229 /*
230 * Freeze the stream class since the event header must not be changed
231 * anymore.
232 */
e6a8e8e4 233 bt_ctf_stream_class_freeze(stream_class);
09840de5
PP
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 */
e6a8e8e4 243 BT_PUT(stream_class);
09840de5 244 BT_PUT(trace);
7b010242
PP
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));
273b65be 249 return event;
09840de5 250
83509119 251error:
09840de5 252 bt_ctf_validation_output_put_types(&validation_output);
83509119 253 BT_PUT(event);
e6a8e8e4 254 BT_PUT(stream_class);
09840de5
PP
255 BT_PUT(trace);
256 BT_PUT(event_header);
5fd2e9fd 257 BT_PUT(stream_event_context);
09840de5
PP
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
83509119 267 return event;
273b65be
JG
268}
269
2f100782
JG
270struct 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) {
7b010242 275 BT_LOGW_STR("Invalid parameter: event is NULL.");
2f100782
JG
276 goto end;
277 }
278
dc3fffef
PP
279 event_class = event ? bt_get(bt_ctf_event_borrow_event_class(event)) :
280 NULL;
2f100782
JG
281end:
282 return event_class;
283}
284
8e5003bb
JG
285struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event)
286{
cacf3147
PP
287 struct bt_ctf_stream *stream = NULL;
288
289 if (!event) {
7b010242 290 BT_LOGW_STR("Invalid parameter: event is NULL.");
cacf3147
PP
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
307end:
308 return stream;
8e5003bb
JG
309}
310
273b65be
JG
311int bt_ctf_event_set_payload(struct bt_ctf_event *event,
312 const char *name,
c5a9aa19 313 struct bt_ctf_field *payload)
273b65be
JG
314{
315 int ret = 0;
316
7b010242
PP
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));
273b65be
JG
330 ret = -1;
331 goto end;
332 }
333
c5a9aa19
JG
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);
09840de5
PP
341
342 if (bt_ctf_field_type_compare(payload_type,
343 event->event_class->fields) == 0) {
83509119
JG
344 bt_put(event->fields_payload);
345 bt_get(payload);
c5a9aa19
JG
346 event->fields_payload = payload;
347 } else {
7b010242
PP
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));
c5a9aa19
JG
354 ret = -1;
355 }
356
83509119 357 bt_put(payload_type);
c5a9aa19 358 }
7b010242
PP
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
273b65be
JG
376end:
377 return ret;
378}
379
9ac68eb1 380struct bt_ctf_field *bt_ctf_event_get_event_payload(struct bt_ctf_event *event)
71362d53
PP
381{
382 struct bt_ctf_field *payload = NULL;
383
7b010242
PP
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));
71362d53
PP
394 goto end;
395 }
396
397 payload = event->fields_payload;
83509119 398 bt_get(payload);
71362d53
PP
399end:
400 return payload;
401}
273b65be 402
9ac68eb1 403int bt_ctf_event_set_event_payload(struct bt_ctf_event *event,
e5e6eb3a
JG
404 struct bt_ctf_field *payload)
405{
7b010242 406 return bt_ctf_event_set_payload(event, NULL, payload);
e5e6eb3a
JG
407}
408
273b65be
JG
409struct 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
c5a9aa19 414 if (!event) {
7b010242 415 BT_LOGW_STR("Invalid parameter: event is NULL.");
273b65be
JG
416 goto end;
417 }
418
c5a9aa19
JG
419 if (name) {
420 field = bt_ctf_field_structure_get_field(event->fields_payload,
421 name);
422 } else {
423 field = event->fields_payload;
83509119 424 bt_get(field);
c5a9aa19 425 }
273b65be
JG
426end:
427 return field;
428}
429
2f100782 430struct bt_ctf_field *bt_ctf_event_get_payload_by_index(
9ac68eb1 431 struct bt_ctf_event *event, uint64_t index)
2f100782
JG
432{
433 struct bt_ctf_field *field = NULL;
434
9ac68eb1 435 if (!event) {
7b010242 436 BT_LOGW_STR("Invalid parameter: event is NULL.");
2f100782
JG
437 goto end;
438 }
439
440 field = bt_ctf_field_structure_get_field_by_index(event->fields_payload,
441 index);
442end:
443 return field;
444}
445
286a2840 446struct bt_ctf_field *bt_ctf_event_get_header(
662e778c
JG
447 struct bt_ctf_event *event)
448{
449 struct bt_ctf_field *header = NULL;
450
7b010242
PP
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));
662e778c
JG
461 goto end;
462 }
463
464 header = event->event_header;
83509119 465 bt_get(header);
662e778c
JG
466end:
467 return header;
468}
469
286a2840 470int bt_ctf_event_set_header(struct bt_ctf_event *event,
662e778c
JG
471 struct bt_ctf_field *header)
472{
473 int ret = 0;
474 struct bt_ctf_field_type *field_type = NULL;
e6a8e8e4 475 struct bt_ctf_stream_class *stream_class = NULL;
662e778c 476
7b010242
PP
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));
662e778c
JG
488 ret = -1;
489 goto end;
490 }
491
e6a8e8e4 492 stream_class = (struct bt_ctf_stream_class *) bt_object_get_parent(
835b2d10 493 event->event_class);
662e778c
JG
494 /*
495 * Ensure the provided header's type matches the one registered to the
496 * stream class.
497 */
498 field_type = bt_ctf_field_get_type(header);
09840de5
PP
499 if (bt_ctf_field_type_compare(field_type,
500 stream_class->event_header_type)) {
7b010242
PP
501 BT_LOGW("Invalid parameter: header field type is different from the expected field type: "
502 "event-addr=%p, event-class-name=\"%s\", "
503 "event-class-id=%" PRId64,
504 event,
505 bt_ctf_event_class_get_name(event->event_class),
506 bt_ctf_event_class_get_id(event->event_class));
662e778c
JG
507 ret = -1;
508 goto end;
509 }
510
83509119 511 bt_put(event->event_header);
835b2d10 512 event->event_header = bt_get(header);
7b010242
PP
513 BT_LOGV("Set event's header field: event-addr=%p, "
514 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
515 "header-field-addr=%p",
516 event, bt_ctf_event_class_get_name(event->event_class),
517 bt_ctf_event_class_get_id(event->event_class), header);
662e778c 518end:
e6a8e8e4 519 bt_put(stream_class);
83509119 520 bt_put(field_type);
662e778c
JG
521 return ret;
522}
523
f655a84d
JG
524struct bt_ctf_field *bt_ctf_event_get_event_context(
525 struct bt_ctf_event *event)
526{
527 struct bt_ctf_field *context = NULL;
528
7b010242
PP
529 if (!event) {
530 BT_LOGW_STR("Invalid parameter: event is NULL.");
531 goto end;
532 }
533
534 if (!event->context_payload) {
535 BT_LOGV("Event has no current context field: addr=%p, "
536 "event-class-name=\"%s\", event-class-id=%" PRId64,
537 event, bt_ctf_event_class_get_name(event->event_class),
538 bt_ctf_event_class_get_id(event->event_class));
f655a84d
JG
539 goto end;
540 }
541
542 context = event->context_payload;
83509119 543 bt_get(context);
f655a84d
JG
544end:
545 return context;
546}
547
548int bt_ctf_event_set_event_context(struct bt_ctf_event *event,
549 struct bt_ctf_field *context)
550{
551 int ret = 0;
552 struct bt_ctf_field_type *field_type = NULL;
553
7b010242
PP
554 if (!event) {
555 BT_LOGW_STR("Invalid parameter: event is NULL.");
556 ret = -1;
557 goto end;
558 }
559
560 if (event->frozen) {
561 BT_LOGW("Invalid parameter: event is frozen: addr=%p, "
562 "event-class-name=\"%s\", event-class-id=%" PRId64,
563 event, bt_ctf_event_class_get_name(event->event_class),
564 bt_ctf_event_class_get_id(event->event_class));
f655a84d
JG
565 ret = -1;
566 goto end;
567 }
568
569 field_type = bt_ctf_field_get_type(context);
09840de5
PP
570 if (bt_ctf_field_type_compare(field_type,
571 event->event_class->context)) {
7b010242
PP
572 BT_LOGW("Invalid parameter: context field type is different from the expected field type: "
573 "event-addr=%p, event-class-name=\"%s\", "
574 "event-class-id=%" PRId64,
575 event,
576 bt_ctf_event_class_get_name(event->event_class),
577 bt_ctf_event_class_get_id(event->event_class));
f655a84d
JG
578 ret = -1;
579 goto end;
580 }
581
83509119 582 bt_put(event->context_payload);
835b2d10 583 event->context_payload = bt_get(context);
7b010242
PP
584 BT_LOGV("Set event's context field: event-addr=%p, "
585 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
586 "context-field-addr=%p",
587 event, bt_ctf_event_class_get_name(event->event_class),
588 bt_ctf_event_class_get_id(event->event_class), context);
f655a84d 589end:
83509119 590 bt_put(field_type);
f655a84d
JG
591 return ret;
592}
593
5fd2e9fd
PP
594struct bt_ctf_field *bt_ctf_event_get_stream_event_context(
595 struct bt_ctf_event *event)
596{
597 struct bt_ctf_field *stream_event_context = NULL;
598
7b010242
PP
599 if (!event) {
600 BT_LOGW_STR("Invalid parameter: event is NULL.");
601 goto end;
602 }
603
604 if (!event->stream_event_context) {
605 BT_LOGV("Event has no current stream event context field: addr=%p, "
606 "event-class-name=\"%s\", event-class-id=%" PRId64,
607 event, bt_ctf_event_class_get_name(event->event_class),
608 bt_ctf_event_class_get_id(event->event_class));
5fd2e9fd
PP
609 goto end;
610 }
611
612 stream_event_context = event->stream_event_context;
613end:
614 return bt_get(stream_event_context);
615}
616
617int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event,
618 struct bt_ctf_field *stream_event_context)
619{
620 int ret = 0;
621 struct bt_ctf_field_type *field_type = NULL;
622 struct bt_ctf_stream_class *stream_class = NULL;
623
7b010242
PP
624 if (!event) {
625 BT_LOGW_STR("Invalid parameter: event is NULL.");
626 ret = -1;
627 goto end;
628 }
629
630 if (event->frozen) {
631 BT_LOGW("Invalid parameter: event is frozen: addr=%p, "
632 "event-class-name=\"%s\", event-class-id=%" PRId64,
633 event, bt_ctf_event_class_get_name(event->event_class),
634 bt_ctf_event_class_get_id(event->event_class));
5fd2e9fd
PP
635 ret = -1;
636 goto end;
637 }
638
639 stream_class = bt_ctf_event_class_get_stream_class(event->event_class);
640 /*
641 * We should not have been able to create the event without associating
642 * the event class to a stream class.
643 */
644 assert(stream_class);
645
646 field_type = bt_ctf_field_get_type(stream_event_context);
647 if (bt_ctf_field_type_compare(field_type,
648 stream_class->event_context_type)) {
7b010242
PP
649 BT_LOGW("Invalid parameter: stream event context field type is different from the expected field type: "
650 "event-addr=%p, event-class-name=\"%s\", "
651 "event-class-id=%" PRId64,
652 event,
653 bt_ctf_event_class_get_name(event->event_class),
654 bt_ctf_event_class_get_id(event->event_class));
5fd2e9fd
PP
655 ret = -1;
656 goto end;
657 }
658
659 bt_get(stream_event_context);
660 BT_MOVE(event->stream_event_context, stream_event_context);
7b010242
PP
661 BT_LOGV("Set event's stream event context field: event-addr=%p, "
662 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
663 "stream-event-context-field-addr=%p",
664 event, bt_ctf_event_class_get_name(event->event_class),
665 bt_ctf_event_class_get_id(event->event_class),
666 stream_event_context);
5fd2e9fd
PP
667end:
668 BT_PUT(stream_class);
669 bt_put(field_type);
670 return ret;
671}
672
7b010242 673/* Pre-2.0 CTF writer backward compatibility */
273b65be
JG
674void bt_ctf_event_get(struct bt_ctf_event *event)
675{
83509119 676 bt_get(event);
273b65be
JG
677}
678
7b010242 679/* Pre-2.0 CTF writer backward compatibility */
273b65be
JG
680void bt_ctf_event_put(struct bt_ctf_event *event)
681{
83509119 682 bt_put(event);
273b65be
JG
683}
684
83509119 685void bt_ctf_event_destroy(struct bt_object *obj)
273b65be
JG
686{
687 struct bt_ctf_event *event;
688
83509119 689 event = container_of(obj, struct bt_ctf_event, base);
7b010242
PP
690 BT_LOGD("Destroying event: addr=%p, "
691 "event-class-name=\"%s\", event-class-id=%" PRId64,
692 event, bt_ctf_event_class_get_name(event->event_class),
693 bt_ctf_event_class_get_id(event->event_class));
694
e6a8e8e4
JG
695 if (!event->base.parent) {
696 /*
697 * Event was keeping a reference to its class since it shared no
698 * common ancestor with it to guarantee they would both have the
699 * same lifetime.
700 */
701 bt_put(event->event_class);
702 }
41ac640a 703 g_hash_table_destroy(event->clock_values);
83509119 704 bt_put(event->event_header);
5fd2e9fd 705 bt_put(event->stream_event_context);
83509119
JG
706 bt_put(event->context_payload);
707 bt_put(event->fields_payload);
5c3b707d 708 bt_put(event->packet);
273b65be
JG
709 g_free(event);
710}
711
1556a1af 712struct bt_ctf_clock_value *bt_ctf_event_get_clock_value(
ac0c6bdd 713 struct bt_ctf_event *event, struct bt_ctf_clock_class *clock_class)
78586d8a 714{
1556a1af 715 struct bt_ctf_clock_value *clock_value = NULL;
78586d8a 716
ac0c6bdd 717 if (!event || !clock_class) {
7b010242
PP
718 BT_LOGW("Invalid parameter: event or clock class is NULL: "
719 "event-addr=%p, clock-class-addr=%p",
720 event, clock_class);
78586d8a
JG
721 goto end;
722 }
723
ac0c6bdd 724 clock_value = g_hash_table_lookup(event->clock_values, clock_class);
78586d8a 725 if (!clock_value) {
7b010242
PP
726 BT_LOGV("No clock value associated to the given clock class: "
727 "event-addr=%p, event-class-name=\"%s\", "
728 "event-class-id=%" PRId64 ", clock-class-addr=%p, "
729 "clock-class-name=\"%s\"", event,
730 bt_ctf_event_class_get_name(event->event_class),
731 bt_ctf_event_class_get_id(event->event_class),
732 clock_class, bt_ctf_clock_class_get_name(clock_class));
78586d8a
JG
733 goto end;
734 }
735
1556a1af
JG
736 bt_get(clock_value);
737end:
738 return clock_value;
739}
740
741int bt_ctf_event_set_clock_value(struct bt_ctf_event *event,
ac0c6bdd 742 struct bt_ctf_clock_value *value)
662e778c
JG
743{
744 int ret = 0;
c9d90a34
PP
745 struct bt_ctf_trace *trace;
746 struct bt_ctf_stream_class *stream_class;
747 struct bt_ctf_event_class *event_class;
748 struct bt_ctf_clock_class *clock_class = NULL;
662e778c 749
7b010242
PP
750 if (!event || !value) {
751 BT_LOGW("Invalid parameter: event or clock value is NULL: "
752 "event-addr=%p, clock-value-addr=%p",
753 event, value);
754 ret = -1;
755 goto end;
756 }
757
758 if (event->frozen) {
759 BT_LOGW("Invalid parameter: event is frozen: addr=%p, "
760 "event-class-name=\"%s\", event-class-id=%" PRId64,
761 event, bt_ctf_event_class_get_name(event->event_class),
762 bt_ctf_event_class_get_id(event->event_class));
662e778c
JG
763 ret = -1;
764 goto end;
765 }
766
c9d90a34
PP
767 clock_class = bt_ctf_clock_value_get_class(value);
768 event_class = bt_ctf_event_borrow_event_class(event);
769 assert(event_class);
770 stream_class = bt_ctf_event_class_borrow_stream_class(event_class);
771 assert(stream_class);
772 trace = bt_ctf_stream_class_borrow_trace(stream_class);
773 assert(trace);
774
775 if (!bt_ctf_trace_has_clock_class(trace, clock_class)) {
7b010242
PP
776 BT_LOGW("Invalid parameter: clock class is not part of event's trace: "
777 "event-addr=%p, event-class-name=\"%s\", "
778 "event-class-id=%" PRId64 ", clock-class-addr=%p, "
779 "clock-class-name=\"%s\"",
780 event, bt_ctf_event_class_get_name(event->event_class),
781 bt_ctf_event_class_get_id(event->event_class),
782 clock_class, bt_ctf_clock_class_get_name(clock_class));
c9d90a34
PP
783 ret = -1;
784 goto end;
785 }
786
787 g_hash_table_insert(event->clock_values, clock_class, bt_get(value));
7b010242
PP
788 BT_LOGV("Set event's clock value: "
789 "event-addr=%p, event-class-name=\"%s\", "
790 "event-class-id=%" PRId64 ", clock-class-addr=%p, "
791 "clock-class-name=\"%s\", clock-value-addr=%p, "
792 "clock-value-cycles=%" PRIu64,
793 event, bt_ctf_event_class_get_name(event->event_class),
794 bt_ctf_event_class_get_id(event->event_class),
795 clock_class, bt_ctf_clock_class_get_name(clock_class),
796 value, value->value);
c9d90a34
PP
797 clock_class = NULL;
798
662e778c 799end:
c9d90a34 800 bt_put(clock_class);
662e778c
JG
801 return ret;
802}
803
273b65be
JG
804BT_HIDDEN
805int bt_ctf_event_validate(struct bt_ctf_event *event)
806{
807 /* Make sure each field's payload has been set */
808 int ret;
5fd2e9fd 809 struct bt_ctf_stream_class *stream_class = NULL;
273b65be
JG
810
811 assert(event);
662e778c
JG
812 ret = bt_ctf_field_validate(event->event_header);
813 if (ret) {
7b010242
PP
814 BT_LOGD("Invalid event's header field: "
815 "event-addr=%p, event-class-name=\"%s\", "
816 "event-class-id=%" PRId64,
817 event, bt_ctf_event_class_get_name(event->event_class),
818 bt_ctf_event_class_get_id(event->event_class));
662e778c
JG
819 goto end;
820 }
821
5fd2e9fd
PP
822 stream_class = bt_ctf_event_class_get_stream_class(event->event_class);
823 /*
824 * We should not have been able to create the event without associating
825 * the event class to a stream class.
826 */
827 assert(stream_class);
828 if (stream_class->event_context_type) {
829 ret = bt_ctf_field_validate(event->stream_event_context);
830 if (ret) {
7b010242
PP
831 BT_LOGD("Invalid event's stream event context field: "
832 "event-addr=%p, event-class-name=\"%s\", "
833 "event-class-id=%" PRId64,
834 event,
835 bt_ctf_event_class_get_name(event->event_class),
836 bt_ctf_event_class_get_id(event->event_class));
5fd2e9fd
PP
837 goto end;
838 }
839 }
840
273b65be
JG
841 ret = bt_ctf_field_validate(event->fields_payload);
842 if (ret) {
7b010242
PP
843 BT_LOGD("Invalid event's payload field: "
844 "event-addr=%p, event-class-name=\"%s\", "
845 "event-class-id=%" PRId64,
846 event,
847 bt_ctf_event_class_get_name(event->event_class),
848 bt_ctf_event_class_get_id(event->event_class));
273b65be
JG
849 goto end;
850 }
851
852 if (event->event_class->context) {
7b010242
PP
853 BT_LOGD("Invalid event's context field: "
854 "event-addr=%p, event-class-name=\"%s\", "
855 "event-class-id=%" PRId64,
856 event,
857 bt_ctf_event_class_get_name(event->event_class),
858 bt_ctf_event_class_get_id(event->event_class));
273b65be
JG
859 ret = bt_ctf_field_validate(event->context_payload);
860 }
861end:
5fd2e9fd 862 bt_put(stream_class);
273b65be
JG
863 return ret;
864}
865
866BT_HIDDEN
867int bt_ctf_event_serialize(struct bt_ctf_event *event,
dc3fffef
PP
868 struct bt_ctf_stream_pos *pos,
869 enum bt_ctf_byte_order native_byte_order)
273b65be
JG
870{
871 int ret = 0;
872
873 assert(event);
874 assert(pos);
dc3fffef 875
7b010242
PP
876 BT_LOGD("Serializing event: "
877 "event-addr=%p, event-class-name=\"%s\", "
878 "event-class-id=%" PRId64 ", pos-addr=%p, "
879 "native-bo=%s",
880 event, bt_ctf_event_class_get_name(event->event_class),
881 bt_ctf_event_class_get_id(event->event_class),
4e8304f7 882 pos, bt_ctf_byte_order_string(native_byte_order));
7b010242 883
273b65be 884 if (event->context_payload) {
dc3fffef
PP
885 ret = bt_ctf_field_serialize(event->context_payload, pos,
886 native_byte_order);
273b65be 887 if (ret) {
7b010242
PP
888 BT_LOGE("Cannot serialize event's context field: "
889 "event-addr=%p, event-class-name=\"%s\", "
890 "event-class-id=%" PRId64,
891 event,
892 bt_ctf_event_class_get_name(event->event_class),
893 bt_ctf_event_class_get_id(event->event_class));
273b65be
JG
894 goto end;
895 }
896 }
897
898 if (event->fields_payload) {
dc3fffef
PP
899 ret = bt_ctf_field_serialize(event->fields_payload, pos,
900 native_byte_order);
273b65be 901 if (ret) {
7b010242
PP
902 BT_LOGE("Cannot serialize event's payload field: "
903 "event-addr=%p, event-class-name=\"%s\", "
904 "event-class-id=%" PRId64,
905 event,
906 bt_ctf_event_class_get_name(event->event_class),
907 bt_ctf_event_class_get_id(event->event_class));
273b65be
JG
908 goto end;
909 }
910 }
911end:
912 return ret;
913}
914
5c0f40f4
JG
915struct bt_ctf_packet *bt_ctf_event_get_packet(struct bt_ctf_event *event)
916{
917 struct bt_ctf_packet *packet = NULL;
918
7b010242
PP
919 if (!event) {
920 BT_LOGW_STR("Invalid parameter: event is NULL.");
921 goto end;
922 }
923
924 if (!event->packet) {
925 BT_LOGV("Event has no current packet: addr=%p, "
926 "event-class-name=\"%s\", event-class-id=%" PRId64,
927 event, bt_ctf_event_class_get_name(event->event_class),
928 bt_ctf_event_class_get_id(event->event_class));
5c0f40f4
JG
929 goto end;
930 }
931
932 packet = bt_get(event->packet);
933end:
934 return packet;
935}
936
5c3b707d
PP
937int bt_ctf_event_set_packet(struct bt_ctf_event *event,
938 struct bt_ctf_packet *packet)
939{
b2b635e9
PP
940 struct bt_ctf_stream_class *event_stream_class = NULL;
941 struct bt_ctf_stream_class *packet_stream_class = NULL;
5c3b707d
PP
942 struct bt_ctf_stream *stream = NULL;
943 int ret = 0;
944
7b010242
PP
945 if (!event || !packet) {
946 BT_LOGW("Invalid parameter: event or packet is NULL: "
947 "event-addr=%p, packet-addr=%p",
948 event, packet);
949 ret = -1;
950 goto end;
951 }
952
953 if (event->frozen) {
954 BT_LOGW("Invalid parameter: event is frozen: addr=%p, "
955 "event-class-name=\"%s\", event-class-id=%" PRId64,
956 event, bt_ctf_event_class_get_name(event->event_class),
957 bt_ctf_event_class_get_id(event->event_class));
5c3b707d
PP
958 ret = -1;
959 goto end;
960 }
961
962 /*
963 * Make sure the new packet was created by this event's
964 * stream, if it is set.
965 */
966 stream = bt_ctf_event_get_stream(event);
967 if (stream) {
968 if (packet->stream != stream) {
7b010242
PP
969 BT_LOGW("Invalid parameter: packet's stream and event's stream differ: "
970 "event-addr=%p, event-class-name=\"%s\", "
971 "event-class-id=%" PRId64 ", packet-stream-addr=%p, "
972 "event-stream-addr=%p",
973 event, bt_ctf_event_class_get_name(event->event_class),
974 bt_ctf_event_class_get_id(event->event_class),
975 packet->stream, stream);
5c3b707d
PP
976 ret = -1;
977 goto end;
978 }
979 } else {
b2b635e9
PP
980 event_stream_class =
981 bt_ctf_event_class_get_stream_class(event->event_class);
982 packet_stream_class =
983 bt_ctf_stream_get_class(packet->stream);
984
985 assert(event_stream_class);
986 assert(packet_stream_class);
987
988 if (event_stream_class != packet_stream_class) {
7b010242
PP
989 BT_LOGW("Invalid parameter: packet's stream class and event's stream class differ: "
990 "event-addr=%p, event-class-name=\"%s\", "
991 "event-class-id=%" PRId64 ", packet-stream-class-addr=%p, "
992 "event-stream-class-addr=%p",
993 event, bt_ctf_event_class_get_name(event->event_class),
994 bt_ctf_event_class_get_id(event->event_class),
995 packet_stream_class, event_stream_class);
b2b635e9
PP
996 ret = -1;
997 goto end;
998 }
5c3b707d
PP
999 }
1000
cacf3147
PP
1001 bt_get(packet);
1002 BT_MOVE(event->packet, packet);
7b010242
PP
1003 BT_LOGV("Set event's packet: event-addr=%p, "
1004 "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
1005 "packet-addr=%p",
1006 event, bt_ctf_event_class_get_name(event->event_class),
1007 bt_ctf_event_class_get_id(event->event_class), packet);
5c3b707d
PP
1008
1009end:
1010 BT_PUT(stream);
b2b635e9
PP
1011 BT_PUT(event_stream_class);
1012 BT_PUT(packet_stream_class);
5c3b707d
PP
1013
1014 return ret;
1015}
4ce9f9d0
PP
1016
1017BT_HIDDEN
1018void bt_ctf_event_freeze(struct bt_ctf_event *event)
1019{
1020 assert(event);
7b010242
PP
1021
1022 if (event->frozen) {
1023 return;
1024 }
1025
1026 BT_LOGD("Freezing event: addr=%p, "
1027 "event-class-name=\"%s\", event-class-id=%" PRId64,
1028 event, bt_ctf_event_class_get_name(event->event_class),
1029 bt_ctf_event_class_get_id(event->event_class));
4ce9f9d0
PP
1030 bt_ctf_packet_freeze(event->packet);
1031 bt_ctf_field_freeze(event->event_header);
1032 bt_ctf_field_freeze(event->stream_event_context);
1033 bt_ctf_field_freeze(event->context_payload);
1034 bt_ctf_field_freeze(event->fields_payload);
1035 event->frozen = 1;
1036}
This page took 0.095078 seconds and 4 git commands to generate.