| 1 | /* |
| 2 | * ctf/events.c |
| 3 | * |
| 4 | * Babeltrace Library |
| 5 | * |
| 6 | * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 9 | * Julien Desfossez <julien.desfossez@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | */ |
| 21 | |
| 22 | #include <babeltrace/babeltrace.h> |
| 23 | #include <babeltrace/format.h> |
| 24 | #include <babeltrace/ctf/events.h> |
| 25 | #include <babeltrace/ctf-ir/metadata.h> |
| 26 | #include <babeltrace/prio_heap.h> |
| 27 | #include <babeltrace/iterator-internal.h> |
| 28 | #include <babeltrace/ctf/events-internal.h> |
| 29 | #include <babeltrace/ctf/metadata.h> |
| 30 | #include <glib.h> |
| 31 | |
| 32 | #include "events-private.h" |
| 33 | |
| 34 | /* |
| 35 | * thread local storage to store the last error that occured |
| 36 | * while reading a field, this variable must be accessed by |
| 37 | * bt_ctf_field_error only |
| 38 | */ |
| 39 | __thread int bt_ctf_last_field_error = 0; |
| 40 | |
| 41 | const struct definition *bt_ctf_get_top_level_scope(const struct ctf_event_definition *event, |
| 42 | enum bt_ctf_scope scope) |
| 43 | { |
| 44 | struct definition *tmp = NULL; |
| 45 | |
| 46 | switch (scope) { |
| 47 | case BT_TRACE_PACKET_HEADER: |
| 48 | if (!event->stream) |
| 49 | goto error; |
| 50 | if (event->stream->trace_packet_header) |
| 51 | tmp = &event->stream->trace_packet_header->p; |
| 52 | break; |
| 53 | case BT_STREAM_PACKET_CONTEXT: |
| 54 | if (!event->stream) |
| 55 | goto error; |
| 56 | if (event->stream->stream_packet_context) |
| 57 | tmp = &event->stream->stream_packet_context->p; |
| 58 | break; |
| 59 | case BT_STREAM_EVENT_HEADER: |
| 60 | if (!event->stream) |
| 61 | goto error; |
| 62 | if (event->stream->stream_event_header) |
| 63 | tmp = &event->stream->stream_event_header->p; |
| 64 | break; |
| 65 | case BT_STREAM_EVENT_CONTEXT: |
| 66 | if (!event->stream) |
| 67 | goto error; |
| 68 | if (event->stream->stream_event_context) |
| 69 | tmp = &event->stream->stream_event_context->p; |
| 70 | break; |
| 71 | case BT_EVENT_CONTEXT: |
| 72 | if (event->event_context) |
| 73 | tmp = &event->event_context->p; |
| 74 | break; |
| 75 | case BT_EVENT_FIELDS: |
| 76 | if (event->event_fields) |
| 77 | tmp = &event->event_fields->p; |
| 78 | break; |
| 79 | } |
| 80 | return tmp; |
| 81 | |
| 82 | error: |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | const struct definition *bt_ctf_get_field(const struct ctf_event_definition *event, |
| 87 | const struct definition *scope, |
| 88 | const char *field) |
| 89 | { |
| 90 | struct definition *def; |
| 91 | char *field_underscore; |
| 92 | |
| 93 | if (scope) { |
| 94 | def = lookup_definition(scope, field); |
| 95 | /* |
| 96 | * optionally a field can have an underscore prefix, try |
| 97 | * to lookup the field with this prefix if it failed |
| 98 | */ |
| 99 | if (!def) { |
| 100 | field_underscore = g_new(char, strlen(field) + 2); |
| 101 | field_underscore[0] = '_'; |
| 102 | strcpy(&field_underscore[1], field); |
| 103 | def = lookup_definition(scope, field_underscore); |
| 104 | g_free(field_underscore); |
| 105 | } |
| 106 | if (bt_ctf_field_type(def) == CTF_TYPE_VARIANT) { |
| 107 | struct definition_variant *variant_definition; |
| 108 | variant_definition = container_of(def, |
| 109 | struct definition_variant, p); |
| 110 | return variant_definition->current_field; |
| 111 | } |
| 112 | return def; |
| 113 | } |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | const struct definition *bt_ctf_get_index(const struct ctf_event_definition *event, |
| 118 | const struct definition *field, |
| 119 | unsigned int index) |
| 120 | { |
| 121 | struct definition *ret = NULL; |
| 122 | |
| 123 | if (bt_ctf_field_type(field) == CTF_TYPE_ARRAY) { |
| 124 | struct definition_array *array_definition; |
| 125 | array_definition = container_of(field, |
| 126 | struct definition_array, p); |
| 127 | ret = array_index(array_definition, index); |
| 128 | } else if (bt_ctf_field_type(field) == CTF_TYPE_SEQUENCE) { |
| 129 | struct definition_sequence *sequence_definition; |
| 130 | sequence_definition = container_of(field, |
| 131 | struct definition_sequence, p); |
| 132 | ret = sequence_index(sequence_definition, index); |
| 133 | } |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | const char *bt_ctf_event_name(const struct ctf_event_definition *event) |
| 138 | { |
| 139 | struct ctf_event_declaration *event_class; |
| 140 | struct ctf_stream_declaration *stream_class; |
| 141 | |
| 142 | if (!event) |
| 143 | return NULL; |
| 144 | stream_class = event->stream->stream_class; |
| 145 | event_class = g_ptr_array_index(stream_class->events_by_id, |
| 146 | event->stream->event_id); |
| 147 | return g_quark_to_string(event_class->name); |
| 148 | } |
| 149 | |
| 150 | const char *bt_ctf_field_name(const struct definition *def) |
| 151 | { |
| 152 | if (def) |
| 153 | return rem_(g_quark_to_string(def->name)); |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | enum ctf_type_id bt_ctf_field_type(const struct definition *def) |
| 158 | { |
| 159 | if (def) |
| 160 | return def->declaration->id; |
| 161 | return CTF_TYPE_UNKNOWN; |
| 162 | } |
| 163 | |
| 164 | int bt_ctf_get_field_list(const struct ctf_event_definition *event, |
| 165 | const struct definition *scope, |
| 166 | struct definition const * const **list, |
| 167 | unsigned int *count) |
| 168 | { |
| 169 | switch (bt_ctf_field_type(scope)) { |
| 170 | case CTF_TYPE_INTEGER: |
| 171 | case CTF_TYPE_FLOAT: |
| 172 | case CTF_TYPE_STRING: |
| 173 | case CTF_TYPE_ENUM: |
| 174 | goto error; |
| 175 | case CTF_TYPE_STRUCT: |
| 176 | { |
| 177 | const struct definition_struct *def_struct; |
| 178 | |
| 179 | def_struct = container_of(scope, const struct definition_struct, p); |
| 180 | if (!def_struct) |
| 181 | goto error; |
| 182 | if (def_struct->fields->pdata) { |
| 183 | *list = (struct definition const* const*) def_struct->fields->pdata; |
| 184 | *count = def_struct->fields->len; |
| 185 | goto end; |
| 186 | } else { |
| 187 | goto error; |
| 188 | } |
| 189 | } |
| 190 | case CTF_TYPE_UNTAGGED_VARIANT: |
| 191 | goto error; |
| 192 | case CTF_TYPE_VARIANT: |
| 193 | { |
| 194 | const struct definition_variant *def_variant; |
| 195 | |
| 196 | def_variant = container_of(scope, const struct definition_variant, p); |
| 197 | if (!def_variant) |
| 198 | goto error; |
| 199 | if (def_variant->fields->pdata) { |
| 200 | *list = (struct definition const* const*) def_variant->fields->pdata; |
| 201 | *count = def_variant->fields->len; |
| 202 | goto end; |
| 203 | } else { |
| 204 | goto error; |
| 205 | } |
| 206 | } |
| 207 | case CTF_TYPE_ARRAY: |
| 208 | { |
| 209 | const struct definition_array *def_array; |
| 210 | |
| 211 | def_array = container_of(scope, const struct definition_array, p); |
| 212 | if (!def_array) |
| 213 | goto error; |
| 214 | if (def_array->elems->pdata) { |
| 215 | *list = (struct definition const* const*) def_array->elems->pdata; |
| 216 | *count = def_array->elems->len; |
| 217 | goto end; |
| 218 | } else { |
| 219 | goto error; |
| 220 | } |
| 221 | } |
| 222 | case CTF_TYPE_SEQUENCE: |
| 223 | { |
| 224 | const struct definition_sequence *def_sequence; |
| 225 | |
| 226 | def_sequence = container_of(scope, const struct definition_sequence, p); |
| 227 | if (!def_sequence) |
| 228 | goto error; |
| 229 | if (def_sequence->elems->pdata) { |
| 230 | *list = (struct definition const* const*) def_sequence->elems->pdata; |
| 231 | *count = def_sequence->elems->len; |
| 232 | goto end; |
| 233 | } else { |
| 234 | goto error; |
| 235 | } |
| 236 | } |
| 237 | default: |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | end: |
| 242 | return 0; |
| 243 | |
| 244 | error: |
| 245 | *list = NULL; |
| 246 | *count = 0; |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | struct bt_context *bt_ctf_event_get_context(const struct ctf_event_definition *event) |
| 251 | { |
| 252 | struct bt_context *ret = NULL; |
| 253 | struct ctf_file_stream *cfs; |
| 254 | struct ctf_trace *trace; |
| 255 | |
| 256 | cfs = container_of(event->stream, struct ctf_file_stream, |
| 257 | parent); |
| 258 | trace = cfs->parent.stream_class->trace; |
| 259 | if (trace->ctx) |
| 260 | ret = trace->ctx; |
| 261 | |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | int bt_ctf_event_get_handle_id(const struct ctf_event_definition *event) |
| 266 | { |
| 267 | int ret = -1; |
| 268 | struct ctf_file_stream *cfs; |
| 269 | struct ctf_trace *trace; |
| 270 | |
| 271 | cfs = container_of(event->stream, struct ctf_file_stream, |
| 272 | parent); |
| 273 | trace = cfs->parent.stream_class->trace; |
| 274 | if (trace->handle) |
| 275 | ret = trace->handle->id; |
| 276 | |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | uint64_t bt_ctf_get_timestamp_raw(const struct ctf_event_definition *event) |
| 281 | { |
| 282 | if (event && event->stream->has_timestamp) |
| 283 | return ctf_get_timestamp_raw(event->stream, |
| 284 | event->stream->timestamp); |
| 285 | else |
| 286 | return -1ULL; |
| 287 | } |
| 288 | |
| 289 | uint64_t bt_ctf_get_timestamp(const struct ctf_event_definition *event) |
| 290 | { |
| 291 | if (event && event->stream->has_timestamp) |
| 292 | return ctf_get_timestamp(event->stream, |
| 293 | event->stream->timestamp); |
| 294 | else |
| 295 | return -1ULL; |
| 296 | } |
| 297 | |
| 298 | static void bt_ctf_field_set_error(int error) |
| 299 | { |
| 300 | bt_ctf_last_field_error = error; |
| 301 | } |
| 302 | |
| 303 | int bt_ctf_field_get_error(void) |
| 304 | { |
| 305 | int ret; |
| 306 | ret = bt_ctf_last_field_error; |
| 307 | bt_ctf_last_field_error = 0; |
| 308 | |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | int bt_ctf_get_int_signedness(const struct definition *field) |
| 313 | { |
| 314 | int ret; |
| 315 | |
| 316 | if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) { |
| 317 | ret = get_int_signedness(field); |
| 318 | } else { |
| 319 | ret = -1; |
| 320 | bt_ctf_field_set_error(-EINVAL); |
| 321 | } |
| 322 | |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | int bt_ctf_get_int_base(const struct definition *field) |
| 327 | { |
| 328 | int ret; |
| 329 | |
| 330 | if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) { |
| 331 | ret = get_int_base(field); |
| 332 | } else { |
| 333 | ret = -1; |
| 334 | bt_ctf_field_set_error(-EINVAL); |
| 335 | } |
| 336 | |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | int bt_ctf_get_int_byte_order(const struct definition *field) |
| 341 | { |
| 342 | int ret; |
| 343 | |
| 344 | if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) { |
| 345 | ret = get_int_byte_order(field); |
| 346 | } else { |
| 347 | ret = -1; |
| 348 | bt_ctf_field_set_error(-EINVAL); |
| 349 | } |
| 350 | |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | enum ctf_string_encoding bt_ctf_get_encoding(const struct definition *field) |
| 355 | { |
| 356 | enum ctf_string_encoding ret = 0; |
| 357 | |
| 358 | if (!field) |
| 359 | goto end; |
| 360 | |
| 361 | if (bt_ctf_field_type(field) == CTF_TYPE_INTEGER) |
| 362 | ret = get_int_encoding(field); |
| 363 | else if (bt_ctf_field_type(field) == CTF_TYPE_STRING) |
| 364 | ret = get_string_encoding(field); |
| 365 | else |
| 366 | goto error; |
| 367 | |
| 368 | end: |
| 369 | return ret; |
| 370 | |
| 371 | error: |
| 372 | bt_ctf_field_set_error(-EINVAL); |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | int bt_ctf_get_array_len(const struct definition *field) |
| 377 | { |
| 378 | int ret; |
| 379 | |
| 380 | if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) { |
| 381 | ret = get_array_len(field); |
| 382 | } else { |
| 383 | ret = -1; |
| 384 | bt_ctf_field_set_error(-EINVAL); |
| 385 | } |
| 386 | |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | uint64_t bt_ctf_get_uint64(const struct definition *field) |
| 391 | { |
| 392 | unsigned int ret = 0; |
| 393 | |
| 394 | if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) |
| 395 | ret = get_unsigned_int(field); |
| 396 | else |
| 397 | bt_ctf_field_set_error(-EINVAL); |
| 398 | |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | int64_t bt_ctf_get_int64(const struct definition *field) |
| 403 | { |
| 404 | int ret = 0; |
| 405 | |
| 406 | if (field && bt_ctf_field_type(field) == CTF_TYPE_INTEGER) |
| 407 | ret = get_signed_int(field); |
| 408 | else |
| 409 | bt_ctf_field_set_error(-EINVAL); |
| 410 | |
| 411 | return ret; |
| 412 | |
| 413 | } |
| 414 | |
| 415 | char *bt_ctf_get_char_array(const struct definition *field) |
| 416 | { |
| 417 | char *ret = NULL; |
| 418 | |
| 419 | if (field && bt_ctf_field_type(field) == CTF_TYPE_ARRAY) |
| 420 | ret = get_char_array(field)->str; |
| 421 | else |
| 422 | bt_ctf_field_set_error(-EINVAL); |
| 423 | |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | char *bt_ctf_get_string(const struct definition *field) |
| 428 | { |
| 429 | char *ret = NULL; |
| 430 | |
| 431 | if (field && bt_ctf_field_type(field) == CTF_TYPE_STRING) |
| 432 | ret = get_string(field); |
| 433 | else |
| 434 | bt_ctf_field_set_error(-EINVAL); |
| 435 | |
| 436 | return ret; |
| 437 | } |