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