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