| 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-writer/event.h> |
| 30 | #include <babeltrace/ctf-writer/event-types.h> |
| 31 | #include <babeltrace/ctf-writer/event-fields.h> |
| 32 | #include <babeltrace/ctf-ir/event-fields-internal.h> |
| 33 | #include <babeltrace/ctf-ir/event-types-internal.h> |
| 34 | #include <babeltrace/ctf-ir/event-internal.h> |
| 35 | #include <babeltrace/ctf-ir/stream-class.h> |
| 36 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
| 37 | #include <babeltrace/ctf-ir/trace-internal.h> |
| 38 | #include <babeltrace/ctf-ir/utils.h> |
| 39 | #include <babeltrace/compiler.h> |
| 40 | |
| 41 | static |
| 42 | void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref); |
| 43 | static |
| 44 | void bt_ctf_event_destroy(struct bt_ctf_ref *ref); |
| 45 | static |
| 46 | int set_integer_field_value(struct bt_ctf_field *field, uint64_t value); |
| 47 | |
| 48 | struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name) |
| 49 | { |
| 50 | struct bt_ctf_event_class *event_class = NULL; |
| 51 | |
| 52 | if (bt_ctf_validate_identifier(name)) { |
| 53 | goto end; |
| 54 | } |
| 55 | |
| 56 | event_class = g_new0(struct bt_ctf_event_class, 1); |
| 57 | if (!event_class) { |
| 58 | goto end; |
| 59 | } |
| 60 | |
| 61 | bt_ctf_ref_init(&event_class->ref_count); |
| 62 | event_class->name = g_quark_from_string(name); |
| 63 | end: |
| 64 | return event_class; |
| 65 | } |
| 66 | |
| 67 | const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class) |
| 68 | { |
| 69 | const char *name = NULL; |
| 70 | |
| 71 | if (!event_class) { |
| 72 | goto end; |
| 73 | } |
| 74 | |
| 75 | name = g_quark_to_string(event_class->name); |
| 76 | end: |
| 77 | return name; |
| 78 | } |
| 79 | |
| 80 | int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class) |
| 81 | { |
| 82 | int64_t ret; |
| 83 | |
| 84 | if (!event_class || !event_class->id_set) { |
| 85 | ret = -1; |
| 86 | goto end; |
| 87 | } |
| 88 | |
| 89 | ret = (int64_t) event_class->id; |
| 90 | end: |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class, |
| 95 | uint32_t id) |
| 96 | { |
| 97 | int ret = 0; |
| 98 | |
| 99 | if (!event_class) { |
| 100 | ret = -1; |
| 101 | goto end; |
| 102 | } |
| 103 | |
| 104 | if (event_class->stream_class) { |
| 105 | /* |
| 106 | * We don't allow changing the id if the event class has already |
| 107 | * been added to a stream class. |
| 108 | */ |
| 109 | ret = -1; |
| 110 | goto end; |
| 111 | } |
| 112 | |
| 113 | event_class->id = id; |
| 114 | event_class->id_set = 1; |
| 115 | end: |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class( |
| 120 | struct bt_ctf_event_class *event_class) |
| 121 | { |
| 122 | struct bt_ctf_stream_class *stream_class = NULL; |
| 123 | |
| 124 | if (!event_class) { |
| 125 | goto end; |
| 126 | } |
| 127 | |
| 128 | stream_class = event_class->stream_class; |
| 129 | bt_ctf_stream_class_get(stream_class); |
| 130 | end: |
| 131 | return stream_class; |
| 132 | } |
| 133 | |
| 134 | int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, |
| 135 | struct bt_ctf_field_type *type, |
| 136 | const char *name) |
| 137 | { |
| 138 | int ret = 0; |
| 139 | |
| 140 | if (!event_class || !type || bt_ctf_validate_identifier(name) || |
| 141 | event_class->frozen) { |
| 142 | ret = -1; |
| 143 | goto end; |
| 144 | } |
| 145 | |
| 146 | if (!event_class->fields) { |
| 147 | event_class->fields = bt_ctf_field_type_structure_create(); |
| 148 | if (!event_class->fields) { |
| 149 | ret = -1; |
| 150 | goto end; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | ret = bt_ctf_field_type_structure_add_field(event_class->fields, |
| 155 | type, name); |
| 156 | end: |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | int bt_ctf_event_class_get_field_count( |
| 161 | struct bt_ctf_event_class *event_class) |
| 162 | { |
| 163 | int ret; |
| 164 | |
| 165 | if (!event_class) { |
| 166 | ret = -1; |
| 167 | goto end; |
| 168 | } |
| 169 | |
| 170 | ret = bt_ctf_field_type_structure_get_field_count(event_class->fields); |
| 171 | end: |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class, |
| 176 | const char **field_name, struct bt_ctf_field_type **field_type, |
| 177 | int index) |
| 178 | { |
| 179 | int ret; |
| 180 | |
| 181 | if (!event_class || index < 0) { |
| 182 | ret = -1; |
| 183 | goto end; |
| 184 | } |
| 185 | |
| 186 | ret = bt_ctf_field_type_structure_get_field(event_class->fields, |
| 187 | field_name, field_type, index); |
| 188 | end: |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name( |
| 193 | struct bt_ctf_event_class *event_class, const char *name) |
| 194 | { |
| 195 | GQuark name_quark; |
| 196 | struct bt_ctf_field_type *field_type = NULL; |
| 197 | |
| 198 | if (!event_class || !name) { |
| 199 | goto end; |
| 200 | } |
| 201 | |
| 202 | name_quark = g_quark_try_string(name); |
| 203 | if (!name_quark) { |
| 204 | goto end; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * No need to increment field_type's reference count since getting it |
| 209 | * from the structure already does. |
| 210 | */ |
| 211 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( |
| 212 | event_class->fields, name); |
| 213 | end: |
| 214 | return field_type; |
| 215 | } |
| 216 | |
| 217 | struct bt_ctf_field_type *bt_ctf_event_class_get_context_type( |
| 218 | struct bt_ctf_event_class *event_class) |
| 219 | { |
| 220 | struct bt_ctf_field_type *context_type = NULL; |
| 221 | |
| 222 | if (!event_class || !event_class->context) { |
| 223 | goto end; |
| 224 | } |
| 225 | |
| 226 | bt_ctf_field_type_get(event_class->context); |
| 227 | context_type = event_class->context; |
| 228 | end: |
| 229 | return context_type; |
| 230 | } |
| 231 | |
| 232 | int bt_ctf_event_class_set_context_type( |
| 233 | struct bt_ctf_event_class *event_class, |
| 234 | struct bt_ctf_field_type *context) |
| 235 | { |
| 236 | int ret = 0; |
| 237 | |
| 238 | if (!event_class || !context || event_class->frozen) { |
| 239 | ret = -1; |
| 240 | goto end; |
| 241 | } |
| 242 | |
| 243 | if (bt_ctf_field_type_get_type_id(context) != CTF_TYPE_STRUCT) { |
| 244 | ret = -1; |
| 245 | goto end; |
| 246 | } |
| 247 | |
| 248 | bt_ctf_field_type_get(context); |
| 249 | bt_ctf_field_type_put(event_class->context); |
| 250 | event_class->context = context; |
| 251 | end: |
| 252 | return ret; |
| 253 | |
| 254 | } |
| 255 | |
| 256 | void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class) |
| 257 | { |
| 258 | if (!event_class) { |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | bt_ctf_ref_get(&event_class->ref_count); |
| 263 | } |
| 264 | |
| 265 | void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class) |
| 266 | { |
| 267 | if (!event_class) { |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy); |
| 272 | } |
| 273 | |
| 274 | struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) |
| 275 | { |
| 276 | struct bt_ctf_event *event = NULL; |
| 277 | |
| 278 | if (!event_class) { |
| 279 | goto end; |
| 280 | } |
| 281 | |
| 282 | event = g_new0(struct bt_ctf_event, 1); |
| 283 | if (!event) { |
| 284 | goto end; |
| 285 | } |
| 286 | |
| 287 | bt_ctf_ref_init(&event->ref_count); |
| 288 | bt_ctf_event_class_get(event_class); |
| 289 | bt_ctf_event_class_freeze(event_class); |
| 290 | event->event_class = event_class; |
| 291 | |
| 292 | /* |
| 293 | * The event class does not keep ownership of the stream class to |
| 294 | * which it as been added. Therefore, it can't assume it has been |
| 295 | * set. However, we disallow the creation of an event if its |
| 296 | * associated stream class has been reclaimed. |
| 297 | */ |
| 298 | if (!event_class->stream_class) { |
| 299 | goto error_destroy; |
| 300 | } |
| 301 | assert(event_class->stream_class->event_header_type); |
| 302 | |
| 303 | event->event_header = bt_ctf_field_create( |
| 304 | event_class->stream_class->event_header_type); |
| 305 | if (!event->event_header) { |
| 306 | goto error_destroy; |
| 307 | } |
| 308 | if (event_class->context) { |
| 309 | event->context_payload = bt_ctf_field_create( |
| 310 | event_class->context); |
| 311 | if (!event->context_payload) { |
| 312 | goto error_destroy; |
| 313 | } |
| 314 | } |
| 315 | event->fields_payload = bt_ctf_field_create(event_class->fields); |
| 316 | if (!event->fields_payload) { |
| 317 | goto error_destroy; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Freeze the stream class since the event header must not be changed |
| 322 | * anymore. |
| 323 | */ |
| 324 | bt_ctf_stream_class_freeze(event_class->stream_class); |
| 325 | end: |
| 326 | return event; |
| 327 | error_destroy: |
| 328 | bt_ctf_event_destroy(&event->ref_count); |
| 329 | return NULL; |
| 330 | } |
| 331 | |
| 332 | struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event) |
| 333 | { |
| 334 | struct bt_ctf_event_class *event_class = NULL; |
| 335 | |
| 336 | if (!event) { |
| 337 | goto end; |
| 338 | } |
| 339 | |
| 340 | event_class = event->event_class; |
| 341 | bt_ctf_event_class_get(event_class); |
| 342 | end: |
| 343 | return event_class; |
| 344 | } |
| 345 | |
| 346 | struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event) |
| 347 | { |
| 348 | struct bt_ctf_clock *clock = NULL; |
| 349 | struct bt_ctf_event_class *event_class; |
| 350 | struct bt_ctf_stream_class *stream_class; |
| 351 | |
| 352 | if (!event) { |
| 353 | goto end; |
| 354 | } |
| 355 | |
| 356 | event_class = bt_ctf_event_get_class(event); |
| 357 | if (!event_class) { |
| 358 | goto end; |
| 359 | } |
| 360 | |
| 361 | stream_class = bt_ctf_event_class_get_stream_class(event_class); |
| 362 | if (!stream_class) { |
| 363 | goto error_put_event_class; |
| 364 | } |
| 365 | |
| 366 | clock = bt_ctf_stream_class_get_clock(stream_class); |
| 367 | if (!clock) { |
| 368 | goto error_put_stream_class; |
| 369 | } |
| 370 | |
| 371 | error_put_stream_class: |
| 372 | bt_ctf_stream_class_put(stream_class); |
| 373 | error_put_event_class: |
| 374 | bt_ctf_event_class_put(event_class); |
| 375 | end: |
| 376 | return clock; |
| 377 | } |
| 378 | |
| 379 | int bt_ctf_event_set_payload(struct bt_ctf_event *event, |
| 380 | const char *name, |
| 381 | struct bt_ctf_field *value) |
| 382 | { |
| 383 | int ret = 0; |
| 384 | |
| 385 | if (!event || !value || bt_ctf_validate_identifier(name)) { |
| 386 | ret = -1; |
| 387 | goto end; |
| 388 | } |
| 389 | |
| 390 | ret = bt_ctf_field_structure_set_field(event->fields_payload, |
| 391 | name, value); |
| 392 | end: |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, |
| 398 | const char *name) |
| 399 | { |
| 400 | struct bt_ctf_field *field = NULL; |
| 401 | |
| 402 | if (!event || !name) { |
| 403 | goto end; |
| 404 | } |
| 405 | |
| 406 | field = bt_ctf_field_structure_get_field(event->fields_payload, name); |
| 407 | end: |
| 408 | return field; |
| 409 | } |
| 410 | |
| 411 | struct bt_ctf_field *bt_ctf_event_get_payload_by_index( |
| 412 | struct bt_ctf_event *event, int index) |
| 413 | { |
| 414 | struct bt_ctf_field *field = NULL; |
| 415 | |
| 416 | if (!event || index < 0) { |
| 417 | goto end; |
| 418 | } |
| 419 | |
| 420 | field = bt_ctf_field_structure_get_field_by_index(event->fields_payload, |
| 421 | index); |
| 422 | end: |
| 423 | return field; |
| 424 | } |
| 425 | |
| 426 | struct bt_ctf_field *bt_ctf_event_get_event_header( |
| 427 | struct bt_ctf_event *event) |
| 428 | { |
| 429 | struct bt_ctf_field *header = NULL; |
| 430 | |
| 431 | if (!event || !event->event_header) { |
| 432 | goto end; |
| 433 | } |
| 434 | |
| 435 | header = event->event_header; |
| 436 | bt_ctf_field_get(header); |
| 437 | end: |
| 438 | return header; |
| 439 | } |
| 440 | |
| 441 | int bt_ctf_event_set_event_header(struct bt_ctf_event *event, |
| 442 | struct bt_ctf_field *header) |
| 443 | { |
| 444 | int ret = 0; |
| 445 | struct bt_ctf_field_type *field_type = NULL; |
| 446 | |
| 447 | if (!event || !header) { |
| 448 | ret = -1; |
| 449 | goto end; |
| 450 | } |
| 451 | |
| 452 | /* Could be NULL since an event class doesn't own a stream class */ |
| 453 | if (!event->event_class->stream_class) { |
| 454 | ret = -1; |
| 455 | goto end; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Ensure the provided header's type matches the one registered to the |
| 460 | * stream class. |
| 461 | */ |
| 462 | field_type = bt_ctf_field_get_type(header); |
| 463 | if (field_type != event->event_class->stream_class->event_header_type) { |
| 464 | ret = -1; |
| 465 | goto end; |
| 466 | } |
| 467 | |
| 468 | bt_ctf_field_get(header); |
| 469 | bt_ctf_field_put(event->event_header); |
| 470 | event->event_header = header; |
| 471 | end: |
| 472 | if (field_type) { |
| 473 | bt_ctf_field_type_put(field_type); |
| 474 | } |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | struct bt_ctf_field *bt_ctf_event_get_event_context( |
| 479 | struct bt_ctf_event *event) |
| 480 | { |
| 481 | struct bt_ctf_field *context = NULL; |
| 482 | |
| 483 | if (!event || !event->context_payload) { |
| 484 | goto end; |
| 485 | } |
| 486 | |
| 487 | context = event->context_payload; |
| 488 | bt_ctf_field_get(context); |
| 489 | end: |
| 490 | return context; |
| 491 | } |
| 492 | |
| 493 | int bt_ctf_event_set_event_context(struct bt_ctf_event *event, |
| 494 | struct bt_ctf_field *context) |
| 495 | { |
| 496 | int ret = 0; |
| 497 | struct bt_ctf_field_type *field_type = NULL; |
| 498 | |
| 499 | if (!event || !context) { |
| 500 | ret = -1; |
| 501 | goto end; |
| 502 | } |
| 503 | |
| 504 | field_type = bt_ctf_field_get_type(context); |
| 505 | if (field_type != event->event_class->context) { |
| 506 | ret = -1; |
| 507 | goto end; |
| 508 | } |
| 509 | |
| 510 | bt_ctf_field_get(context); |
| 511 | bt_ctf_field_put(event->context_payload); |
| 512 | event->context_payload = context; |
| 513 | end: |
| 514 | if (field_type) { |
| 515 | bt_ctf_field_type_put(field_type); |
| 516 | } |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | void bt_ctf_event_get(struct bt_ctf_event *event) |
| 521 | { |
| 522 | if (!event) { |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | bt_ctf_ref_get(&event->ref_count); |
| 527 | } |
| 528 | |
| 529 | void bt_ctf_event_put(struct bt_ctf_event *event) |
| 530 | { |
| 531 | if (!event) { |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | bt_ctf_ref_put(&event->ref_count, bt_ctf_event_destroy); |
| 536 | } |
| 537 | |
| 538 | static |
| 539 | void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref) |
| 540 | { |
| 541 | struct bt_ctf_event_class *event_class; |
| 542 | |
| 543 | if (!ref) { |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Don't call put() on the stream class. See comment in |
| 549 | * bt_ctf_event_class_set_stream_class for explanation. |
| 550 | */ |
| 551 | event_class = container_of(ref, struct bt_ctf_event_class, ref_count); |
| 552 | if (event_class->context) { |
| 553 | bt_ctf_field_type_put(event_class->context); |
| 554 | } |
| 555 | if (event_class->fields) { |
| 556 | bt_ctf_field_type_put(event_class->fields); |
| 557 | } |
| 558 | g_free(event_class); |
| 559 | } |
| 560 | |
| 561 | static |
| 562 | void bt_ctf_event_destroy(struct bt_ctf_ref *ref) |
| 563 | { |
| 564 | struct bt_ctf_event *event; |
| 565 | |
| 566 | if (!ref) { |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | event = container_of(ref, struct bt_ctf_event, |
| 571 | ref_count); |
| 572 | if (event->event_class) { |
| 573 | bt_ctf_event_class_put(event->event_class); |
| 574 | } |
| 575 | if (event->event_header) { |
| 576 | bt_ctf_field_put(event->event_header); |
| 577 | } |
| 578 | if (event->context_payload) { |
| 579 | bt_ctf_field_put(event->context_payload); |
| 580 | } |
| 581 | if (event->fields_payload) { |
| 582 | bt_ctf_field_put(event->fields_payload); |
| 583 | } |
| 584 | g_free(event); |
| 585 | } |
| 586 | |
| 587 | static |
| 588 | int set_integer_field_value(struct bt_ctf_field* field, uint64_t value) |
| 589 | { |
| 590 | int ret = 0; |
| 591 | struct bt_ctf_field_type *field_type = NULL; |
| 592 | |
| 593 | if (!field) { |
| 594 | ret = -1; |
| 595 | goto end; |
| 596 | } |
| 597 | |
| 598 | if (!bt_ctf_field_validate(field)) { |
| 599 | /* Payload already set, skip! (not an error) */ |
| 600 | goto end; |
| 601 | } |
| 602 | |
| 603 | field_type = bt_ctf_field_get_type(field); |
| 604 | assert(field_type); |
| 605 | |
| 606 | if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) { |
| 607 | /* Not an integer and the value is unset, error. */ |
| 608 | ret = -1; |
| 609 | goto end; |
| 610 | } |
| 611 | |
| 612 | if (bt_ctf_field_type_integer_get_signed(field_type)) { |
| 613 | ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value); |
| 614 | if (ret) { |
| 615 | /* Value is out of range, error. */ |
| 616 | goto end; |
| 617 | } |
| 618 | } else { |
| 619 | ret = bt_ctf_field_unsigned_integer_set_value(field, value); |
| 620 | if (ret) { |
| 621 | /* Value is out of range, error. */ |
| 622 | goto end; |
| 623 | } |
| 624 | } |
| 625 | end: |
| 626 | bt_ctf_field_type_put(field_type); |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | BT_HIDDEN |
| 631 | void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class) |
| 632 | { |
| 633 | assert(event_class); |
| 634 | event_class->frozen = 1; |
| 635 | bt_ctf_field_type_freeze(event_class->context); |
| 636 | bt_ctf_field_type_freeze(event_class->fields); |
| 637 | } |
| 638 | |
| 639 | BT_HIDDEN |
| 640 | int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class *event_class, |
| 641 | struct bt_ctf_stream_class *stream_class) |
| 642 | { |
| 643 | int ret = 0; |
| 644 | |
| 645 | if (!event_class) { |
| 646 | ret = -1; |
| 647 | goto end; |
| 648 | } |
| 649 | |
| 650 | /* Allow a NULL stream_class to unset the current stream_class */ |
| 651 | if (stream_class && event_class->stream_class) { |
| 652 | ret = -1; |
| 653 | goto end; |
| 654 | } |
| 655 | |
| 656 | event_class->stream_class = stream_class; |
| 657 | /* |
| 658 | * We don't get() the stream_class since doing so would introduce |
| 659 | * a circular ownership between event classes and stream classes. |
| 660 | * |
| 661 | * A stream class will always unset itself from its events before |
| 662 | * being destroyed. This ensures that a user won't get a pointer |
| 663 | * to a stale stream class instance from an event class. |
| 664 | */ |
| 665 | end: |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | BT_HIDDEN |
| 670 | int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class, |
| 671 | struct metadata_context *context) |
| 672 | { |
| 673 | int ret = 0; |
| 674 | int64_t stream_id; |
| 675 | |
| 676 | assert(event_class); |
| 677 | assert(context); |
| 678 | stream_id = bt_ctf_stream_class_get_id(event_class->stream_class); |
| 679 | if (stream_id < 0) { |
| 680 | ret = -1; |
| 681 | goto end; |
| 682 | } |
| 683 | |
| 684 | context->current_indentation_level = 1; |
| 685 | g_string_assign(context->field_name, ""); |
| 686 | g_string_append_printf(context->string, "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64 ";\n", |
| 687 | g_quark_to_string(event_class->name), |
| 688 | event_class->id, |
| 689 | stream_id); |
| 690 | |
| 691 | if (event_class->context) { |
| 692 | g_string_append(context->string, "\tcontext := "); |
| 693 | ret = bt_ctf_field_type_serialize(event_class->context, |
| 694 | context); |
| 695 | if (ret) { |
| 696 | goto end; |
| 697 | } |
| 698 | g_string_append(context->string, ";\n"); |
| 699 | } |
| 700 | |
| 701 | if (event_class->fields) { |
| 702 | g_string_append(context->string, "\tfields := "); |
| 703 | ret = bt_ctf_field_type_serialize(event_class->fields, context); |
| 704 | if (ret) { |
| 705 | goto end; |
| 706 | } |
| 707 | g_string_append(context->string, ";\n"); |
| 708 | } |
| 709 | |
| 710 | g_string_append(context->string, "};\n\n"); |
| 711 | end: |
| 712 | context->current_indentation_level = 0; |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | void bt_ctf_event_class_set_native_byte_order( |
| 717 | struct bt_ctf_event_class *event_class, |
| 718 | int byte_order) |
| 719 | { |
| 720 | if (!event_class) { |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | bt_ctf_field_type_set_native_byte_order(event_class->context, |
| 725 | byte_order); |
| 726 | bt_ctf_field_type_set_native_byte_order(event_class->fields, |
| 727 | byte_order); |
| 728 | } |
| 729 | |
| 730 | BT_HIDDEN |
| 731 | int bt_ctf_event_validate(struct bt_ctf_event *event) |
| 732 | { |
| 733 | /* Make sure each field's payload has been set */ |
| 734 | int ret; |
| 735 | |
| 736 | assert(event); |
| 737 | ret = bt_ctf_field_validate(event->event_header); |
| 738 | if (ret) { |
| 739 | goto end; |
| 740 | } |
| 741 | |
| 742 | ret = bt_ctf_field_validate(event->fields_payload); |
| 743 | if (ret) { |
| 744 | goto end; |
| 745 | } |
| 746 | |
| 747 | if (event->event_class->context) { |
| 748 | ret = bt_ctf_field_validate(event->context_payload); |
| 749 | } |
| 750 | end: |
| 751 | return ret; |
| 752 | } |
| 753 | |
| 754 | BT_HIDDEN |
| 755 | int bt_ctf_event_serialize(struct bt_ctf_event *event, |
| 756 | struct ctf_stream_pos *pos) |
| 757 | { |
| 758 | int ret = 0; |
| 759 | |
| 760 | assert(event); |
| 761 | assert(pos); |
| 762 | if (event->context_payload) { |
| 763 | ret = bt_ctf_field_serialize(event->context_payload, pos); |
| 764 | if (ret) { |
| 765 | goto end; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if (event->fields_payload) { |
| 770 | ret = bt_ctf_field_serialize(event->fields_payload, pos); |
| 771 | if (ret) { |
| 772 | goto end; |
| 773 | } |
| 774 | } |
| 775 | end: |
| 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | BT_HIDDEN |
| 780 | int bt_ctf_event_set_timestamp(struct bt_ctf_event *event, |
| 781 | uint64_t timestamp) |
| 782 | { |
| 783 | int ret = 0; |
| 784 | |
| 785 | assert(event); |
| 786 | if (event->timestamp) { |
| 787 | ret = -1; |
| 788 | goto end; |
| 789 | } |
| 790 | |
| 791 | event->timestamp = timestamp; |
| 792 | end: |
| 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | BT_HIDDEN |
| 797 | uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event *event) |
| 798 | { |
| 799 | assert(event); |
| 800 | return event->timestamp; |
| 801 | } |
| 802 | |
| 803 | BT_HIDDEN |
| 804 | int bt_ctf_event_populate_event_header(struct bt_ctf_event *event) |
| 805 | { |
| 806 | int ret = 0; |
| 807 | struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL; |
| 808 | |
| 809 | if (!event) { |
| 810 | ret = -1; |
| 811 | goto end; |
| 812 | } |
| 813 | |
| 814 | id_field = bt_ctf_field_structure_get_field(event->event_header, "id"); |
| 815 | if (id_field) { |
| 816 | ret = set_integer_field_value(id_field, |
| 817 | (uint64_t) event->event_class->id); |
| 818 | if (ret) { |
| 819 | goto end; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | timestamp_field = bt_ctf_field_structure_get_field(event->event_header, |
| 824 | "timestamp"); |
| 825 | if (timestamp_field) { |
| 826 | ret = set_integer_field_value(timestamp_field, |
| 827 | (uint64_t) event->timestamp); |
| 828 | if (ret) { |
| 829 | goto end; |
| 830 | } |
| 831 | } |
| 832 | end: |
| 833 | if (id_field) { |
| 834 | bt_ctf_field_put(id_field); |
| 835 | } |
| 836 | if (timestamp_field) { |
| 837 | bt_ctf_field_put(timestamp_field); |
| 838 | } |
| 839 | return ret; |
| 840 | } |