| 1 | /* |
| 2 | * ctf-visitor-generate-ir.c |
| 3 | * |
| 4 | * Common Trace Format metadata visitor (generates CTF IR objects). |
| 5 | * |
| 6 | * Based on older ctf-visitor-generate-io-struct.c. |
| 7 | * |
| 8 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 9 | * Copyright 2015-2018 - Philippe Proulx <philippe.proulx@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 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #define BT_COMP_LOG_SELF_COMP (ctx->log_cfg.self_comp) |
| 31 | #define BT_LOG_OUTPUT_LEVEL (ctx->log_cfg.log_level) |
| 32 | #define BT_LOG_TAG "PLUGIN/CTF/META/IR-VISITOR" |
| 33 | #include "logging/comp-logging.h" |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <unistd.h> |
| 37 | #include <string.h> |
| 38 | #include <stdbool.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <ctype.h> |
| 41 | #include "common/assert.h" |
| 42 | #include <glib.h> |
| 43 | #include <inttypes.h> |
| 44 | #include <errno.h> |
| 45 | #include "common/common.h" |
| 46 | #include "common/uuid.h" |
| 47 | #include "compat/endian.h" |
| 48 | #include <babeltrace2/babeltrace.h> |
| 49 | |
| 50 | #include "logging.h" |
| 51 | #include "scanner.h" |
| 52 | #include "parser.h" |
| 53 | #include "ast.h" |
| 54 | #include "decoder.h" |
| 55 | #include "ctf-meta.h" |
| 56 | #include "ctf-meta-visitors.h" |
| 57 | |
| 58 | /* Bit value (left shift) */ |
| 59 | #define _BV(_val) (1 << (_val)) |
| 60 | |
| 61 | /* Bit is set in a set of bits */ |
| 62 | #define _IS_SET(_set, _mask) (*(_set) & (_mask)) |
| 63 | |
| 64 | /* Set bit in a set of bits */ |
| 65 | #define _SET(_set, _mask) (*(_set) |= (_mask)) |
| 66 | |
| 67 | /* Try to push scope, or go to the `error` label */ |
| 68 | #define _TRY_PUSH_SCOPE_OR_GOTO_ERROR() \ |
| 69 | do { \ |
| 70 | ret = ctx_push_scope(ctx); \ |
| 71 | if (ret) { \ |
| 72 | BT_COMP_LOGE_STR("Cannot push scope."); \ |
| 73 | goto error; \ |
| 74 | } \ |
| 75 | } while (0) |
| 76 | |
| 77 | /* Bits for verifying existing attributes in various declarations */ |
| 78 | enum { |
| 79 | _CLOCK_NAME_SET = _BV(0), |
| 80 | _CLOCK_UUID_SET = _BV(1), |
| 81 | _CLOCK_FREQ_SET = _BV(2), |
| 82 | _CLOCK_PRECISION_SET = _BV(3), |
| 83 | _CLOCK_OFFSET_S_SET = _BV(4), |
| 84 | _CLOCK_OFFSET_SET = _BV(5), |
| 85 | _CLOCK_ABSOLUTE_SET = _BV(6), |
| 86 | _CLOCK_DESCRIPTION_SET = _BV(7), |
| 87 | }; |
| 88 | |
| 89 | enum { |
| 90 | _INTEGER_ALIGN_SET = _BV(0), |
| 91 | _INTEGER_SIZE_SET = _BV(1), |
| 92 | _INTEGER_BASE_SET = _BV(2), |
| 93 | _INTEGER_ENCODING_SET = _BV(3), |
| 94 | _INTEGER_BYTE_ORDER_SET = _BV(4), |
| 95 | _INTEGER_SIGNED_SET = _BV(5), |
| 96 | _INTEGER_MAP_SET = _BV(6), |
| 97 | }; |
| 98 | |
| 99 | enum { |
| 100 | _FLOAT_ALIGN_SET = _BV(0), |
| 101 | _FLOAT_MANT_DIG_SET = _BV(1), |
| 102 | _FLOAT_EXP_DIG_SET = _BV(2), |
| 103 | _FLOAT_BYTE_ORDER_SET = _BV(3), |
| 104 | }; |
| 105 | |
| 106 | enum { |
| 107 | _STRING_ENCODING_SET = _BV(0), |
| 108 | }; |
| 109 | |
| 110 | enum { |
| 111 | _TRACE_MINOR_SET = _BV(0), |
| 112 | _TRACE_MAJOR_SET = _BV(1), |
| 113 | _TRACE_BYTE_ORDER_SET = _BV(2), |
| 114 | _TRACE_UUID_SET = _BV(3), |
| 115 | _TRACE_PACKET_HEADER_SET = _BV(4), |
| 116 | }; |
| 117 | |
| 118 | enum { |
| 119 | _STREAM_ID_SET = _BV(0), |
| 120 | _STREAM_PACKET_CONTEXT_SET = _BV(1), |
| 121 | _STREAM_EVENT_HEADER_SET = _BV(2), |
| 122 | _STREAM_EVENT_CONTEXT_SET = _BV(3), |
| 123 | }; |
| 124 | |
| 125 | enum { |
| 126 | _EVENT_NAME_SET = _BV(0), |
| 127 | _EVENT_ID_SET = _BV(1), |
| 128 | _EVENT_MODEL_EMF_URI_SET = _BV(2), |
| 129 | _EVENT_STREAM_ID_SET = _BV(3), |
| 130 | _EVENT_LOG_LEVEL_SET = _BV(4), |
| 131 | _EVENT_CONTEXT_SET = _BV(5), |
| 132 | _EVENT_FIELDS_SET = _BV(6), |
| 133 | }; |
| 134 | |
| 135 | enum loglevel { |
| 136 | LOG_LEVEL_EMERG = 0, |
| 137 | LOG_LEVEL_ALERT = 1, |
| 138 | LOG_LEVEL_CRIT = 2, |
| 139 | LOG_LEVEL_ERR = 3, |
| 140 | LOG_LEVEL_WARNING = 4, |
| 141 | LOG_LEVEL_NOTICE = 5, |
| 142 | LOG_LEVEL_INFO = 6, |
| 143 | LOG_LEVEL_DEBUG_SYSTEM = 7, |
| 144 | LOG_LEVEL_DEBUG_PROGRAM = 8, |
| 145 | LOG_LEVEL_DEBUG_PROCESS = 9, |
| 146 | LOG_LEVEL_DEBUG_MODULE = 10, |
| 147 | LOG_LEVEL_DEBUG_UNIT = 11, |
| 148 | LOG_LEVEL_DEBUG_FUNCTION = 12, |
| 149 | LOG_LEVEL_DEBUG_LINE = 13, |
| 150 | LOG_LEVEL_DEBUG = 14, |
| 151 | _NR_LOGLEVELS = 15, |
| 152 | }; |
| 153 | |
| 154 | /* Prefixes of class aliases */ |
| 155 | #define _PREFIX_ALIAS 'a' |
| 156 | #define _PREFIX_ENUM 'e' |
| 157 | #define _PREFIX_STRUCT 's' |
| 158 | #define _PREFIX_VARIANT 'v' |
| 159 | |
| 160 | /* First entry in a BT list */ |
| 161 | #define _BT_LIST_FIRST_ENTRY(_ptr, _class, _member) \ |
| 162 | bt_list_entry((_ptr)->next, _class, _member) |
| 163 | |
| 164 | #define _BT_COMP_LOGE_DUP_ATTR(_node, _attr, _entity) \ |
| 165 | _BT_COMP_LOGE_LINENO((_node)->lineno, \ |
| 166 | "Duplicate attribute in %s: attr-name=\"%s\"", \ |
| 167 | _entity, _attr) |
| 168 | |
| 169 | #define _BT_COMP_LOGE_NODE(_node, _msg, args...) \ |
| 170 | _BT_COMP_LOGE_LINENO((_node)->lineno, _msg, ## args) |
| 171 | |
| 172 | #define _BT_COMP_LOGW_NODE(_node, _msg, args...) \ |
| 173 | _BT_COMP_LOGW_LINENO((_node)->lineno, _msg, ## args) |
| 174 | |
| 175 | #define _BT_COMP_LOGT_NODE(_node, _msg, args...) \ |
| 176 | _BT_COMP_LOGT_LINENO((_node)->lineno, _msg, ## args) |
| 177 | |
| 178 | /* |
| 179 | * Declaration scope of a visitor context. This represents a TSDL |
| 180 | * lexical scope, so that aliases and named structures, variants, |
| 181 | * and enumerations may be registered and looked up hierarchically. |
| 182 | */ |
| 183 | struct ctx_decl_scope { |
| 184 | /* |
| 185 | * Alias name to field class. |
| 186 | * |
| 187 | * GQuark -> struct ctf_field_class * (owned by this) |
| 188 | */ |
| 189 | GHashTable *decl_map; |
| 190 | |
| 191 | /* Parent scope; NULL if this is the root declaration scope */ |
| 192 | struct ctx_decl_scope *parent_scope; |
| 193 | }; |
| 194 | |
| 195 | /* |
| 196 | * Visitor context (private). |
| 197 | */ |
| 198 | struct ctx { |
| 199 | struct meta_log_config log_cfg; |
| 200 | |
| 201 | /* Trace IR trace class being filled (owned by this) */ |
| 202 | bt_trace_class *trace_class; |
| 203 | |
| 204 | /* CTF meta trace being filled (owned by this) */ |
| 205 | struct ctf_trace_class *ctf_tc; |
| 206 | |
| 207 | /* Current declaration scope (top of the stack) (owned by this) */ |
| 208 | struct ctx_decl_scope *current_scope; |
| 209 | |
| 210 | /* True if trace declaration is visited */ |
| 211 | bool is_trace_visited; |
| 212 | |
| 213 | /* True if this is an LTTng trace */ |
| 214 | bool is_lttng; |
| 215 | |
| 216 | /* Config passed by the user */ |
| 217 | struct ctf_metadata_decoder_config decoder_config; |
| 218 | }; |
| 219 | |
| 220 | /* |
| 221 | * Visitor (public). |
| 222 | */ |
| 223 | struct ctf_visitor_generate_ir; |
| 224 | |
| 225 | /** |
| 226 | * Creates a new declaration scope. |
| 227 | * |
| 228 | * @param par_scope Parent scope (NULL if creating a root scope) |
| 229 | * @returns New declaration scope, or NULL on error |
| 230 | */ |
| 231 | static |
| 232 | struct ctx_decl_scope *ctx_decl_scope_create(struct ctx *ctx, |
| 233 | struct ctx_decl_scope *par_scope) |
| 234 | { |
| 235 | struct ctx_decl_scope *scope; |
| 236 | |
| 237 | scope = g_new(struct ctx_decl_scope, 1); |
| 238 | if (!scope) { |
| 239 | BT_COMP_LOGE_STR("Failed to allocate one declaration scope."); |
| 240 | goto end; |
| 241 | } |
| 242 | |
| 243 | scope->decl_map = g_hash_table_new_full(g_direct_hash, g_direct_equal, |
| 244 | NULL, (GDestroyNotify) ctf_field_class_destroy); |
| 245 | scope->parent_scope = par_scope; |
| 246 | |
| 247 | end: |
| 248 | return scope; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Destroys a declaration scope. |
| 253 | * |
| 254 | * This function does not destroy the parent scope. |
| 255 | * |
| 256 | * @param scope Scope to destroy |
| 257 | */ |
| 258 | static |
| 259 | void ctx_decl_scope_destroy(struct ctx_decl_scope *scope) |
| 260 | { |
| 261 | if (!scope) { |
| 262 | goto end; |
| 263 | } |
| 264 | |
| 265 | g_hash_table_destroy(scope->decl_map); |
| 266 | g_free(scope); |
| 267 | |
| 268 | end: |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Returns the GQuark of a prefixed alias. |
| 274 | * |
| 275 | * @param prefix Prefix character |
| 276 | * @param name Name |
| 277 | * @returns Associated GQuark, or 0 on error |
| 278 | */ |
| 279 | static |
| 280 | GQuark get_prefixed_named_quark(struct ctx *ctx, char prefix, const char *name) |
| 281 | { |
| 282 | GQuark qname = 0; |
| 283 | |
| 284 | BT_ASSERT(name); |
| 285 | |
| 286 | /* Prefix character + original string + '\0' */ |
| 287 | char *prname = g_new(char, strlen(name) + 2); |
| 288 | if (!prname) { |
| 289 | BT_COMP_LOGE_STR("Failed to allocate a string."); |
| 290 | goto end; |
| 291 | } |
| 292 | |
| 293 | sprintf(prname, "%c%s", prefix, name); |
| 294 | qname = g_quark_from_string(prname); |
| 295 | g_free(prname); |
| 296 | |
| 297 | end: |
| 298 | return qname; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Looks up a prefixed class alias within a declaration scope. |
| 303 | * |
| 304 | * @param scope Declaration scope |
| 305 | * @param prefix Prefix character |
| 306 | * @param name Alias name |
| 307 | * @param levels Number of levels to dig into (-1 means infinite) |
| 308 | * @param copy True to return a copy |
| 309 | * @returns Declaration (owned by caller if \p copy is true), |
| 310 | * or NULL if not found |
| 311 | */ |
| 312 | static |
| 313 | struct ctf_field_class *ctx_decl_scope_lookup_prefix_alias( |
| 314 | struct ctx *ctx, struct ctx_decl_scope *scope, char prefix, |
| 315 | const char *name, int levels, bool copy) |
| 316 | { |
| 317 | GQuark qname = 0; |
| 318 | int cur_levels = 0; |
| 319 | struct ctf_field_class *decl = NULL; |
| 320 | struct ctx_decl_scope *cur_scope = scope; |
| 321 | |
| 322 | BT_ASSERT(scope); |
| 323 | BT_ASSERT(name); |
| 324 | qname = get_prefixed_named_quark(ctx, prefix, name); |
| 325 | if (!qname) { |
| 326 | goto end; |
| 327 | } |
| 328 | |
| 329 | if (levels < 0) { |
| 330 | levels = INT_MAX; |
| 331 | } |
| 332 | |
| 333 | while (cur_scope && cur_levels < levels) { |
| 334 | decl = g_hash_table_lookup(cur_scope->decl_map, |
| 335 | (gconstpointer) GUINT_TO_POINTER(qname)); |
| 336 | if (decl) { |
| 337 | /* Caller's reference */ |
| 338 | if (copy) { |
| 339 | decl = ctf_field_class_copy(decl); |
| 340 | BT_ASSERT(decl); |
| 341 | } |
| 342 | |
| 343 | goto end; |
| 344 | } |
| 345 | |
| 346 | cur_scope = cur_scope->parent_scope; |
| 347 | cur_levels++; |
| 348 | } |
| 349 | |
| 350 | end: |
| 351 | return decl; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Looks up a class alias within a declaration scope. |
| 356 | * |
| 357 | * @param scope Declaration scope |
| 358 | * @param name Alias name |
| 359 | * @param levels Number of levels to dig into (-1 means infinite) |
| 360 | * @param copy True to return a copy |
| 361 | * @returns Declaration (owned by caller if \p copy is true), |
| 362 | * or NULL if not found |
| 363 | */ |
| 364 | static |
| 365 | struct ctf_field_class *ctx_decl_scope_lookup_alias(struct ctx *ctx, |
| 366 | struct ctx_decl_scope *scope, const char *name, int levels, |
| 367 | bool copy) |
| 368 | { |
| 369 | return ctx_decl_scope_lookup_prefix_alias(ctx, scope, _PREFIX_ALIAS, |
| 370 | name, levels, copy); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Looks up an enumeration within a declaration scope. |
| 375 | * |
| 376 | * @param scope Declaration scope |
| 377 | * @param name Enumeration name |
| 378 | * @param levels Number of levels to dig into (-1 means infinite) |
| 379 | * @param copy True to return a copy |
| 380 | * @returns Declaration (owned by caller if \p copy is true), |
| 381 | * or NULL if not found |
| 382 | */ |
| 383 | static |
| 384 | struct ctf_field_class_enum *ctx_decl_scope_lookup_enum(struct ctx *ctx, |
| 385 | struct ctx_decl_scope *scope, const char *name, int levels, |
| 386 | bool copy) |
| 387 | { |
| 388 | return (void *) ctx_decl_scope_lookup_prefix_alias(ctx, scope, |
| 389 | _PREFIX_ENUM, name, levels, copy); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Looks up a structure within a declaration scope. |
| 394 | * |
| 395 | * @param scope Declaration scope |
| 396 | * @param name Structure name |
| 397 | * @param levels Number of levels to dig into (-1 means infinite) |
| 398 | * @param copy True to return a copy |
| 399 | * @returns Declaration (owned by caller if \p copy is true), |
| 400 | * or NULL if not found |
| 401 | */ |
| 402 | static |
| 403 | struct ctf_field_class_struct *ctx_decl_scope_lookup_struct(struct ctx *ctx, |
| 404 | struct ctx_decl_scope *scope, const char *name, int levels, |
| 405 | bool copy) |
| 406 | { |
| 407 | return (void *) ctx_decl_scope_lookup_prefix_alias(ctx, scope, |
| 408 | _PREFIX_STRUCT, name, levels, copy); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Looks up a variant within a declaration scope. |
| 413 | * |
| 414 | * @param scope Declaration scope |
| 415 | * @param name Variant name |
| 416 | * @param levels Number of levels to dig into (-1 means infinite) |
| 417 | * @param copy True to return a copy |
| 418 | * @returns Declaration (owned by caller if \p copy is true), |
| 419 | * or NULL if not found |
| 420 | */ |
| 421 | static |
| 422 | struct ctf_field_class_variant *ctx_decl_scope_lookup_variant(struct ctx *ctx, |
| 423 | struct ctx_decl_scope *scope, const char *name, int levels, |
| 424 | bool copy) |
| 425 | { |
| 426 | return (void *) ctx_decl_scope_lookup_prefix_alias(ctx, scope, |
| 427 | _PREFIX_VARIANT, name, levels, copy); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Registers a prefixed class alias within a declaration scope. |
| 432 | * |
| 433 | * @param scope Declaration scope |
| 434 | * @param prefix Prefix character |
| 435 | * @param name Alias name (non-NULL) |
| 436 | * @param decl Field class to register (copied) |
| 437 | * @returns 0 if registration went okay, negative value otherwise |
| 438 | */ |
| 439 | static |
| 440 | int ctx_decl_scope_register_prefix_alias(struct ctx *ctx, |
| 441 | struct ctx_decl_scope *scope, char prefix, const char *name, |
| 442 | struct ctf_field_class *decl) |
| 443 | { |
| 444 | int ret = 0; |
| 445 | GQuark qname = 0; |
| 446 | |
| 447 | BT_ASSERT(scope); |
| 448 | BT_ASSERT(name); |
| 449 | BT_ASSERT(decl); |
| 450 | qname = get_prefixed_named_quark(ctx, prefix, name); |
| 451 | if (!qname) { |
| 452 | ret = -ENOMEM; |
| 453 | goto end; |
| 454 | } |
| 455 | |
| 456 | /* Make sure alias does not exist in local scope */ |
| 457 | if (ctx_decl_scope_lookup_prefix_alias(ctx, scope, prefix, name, 1, |
| 458 | false)) { |
| 459 | ret = -EEXIST; |
| 460 | goto end; |
| 461 | } |
| 462 | |
| 463 | decl = ctf_field_class_copy(decl); |
| 464 | BT_ASSERT(decl); |
| 465 | g_hash_table_insert(scope->decl_map, GUINT_TO_POINTER(qname), decl); |
| 466 | |
| 467 | end: |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Registers a class alias within a declaration scope. |
| 473 | * |
| 474 | * @param scope Declaration scope |
| 475 | * @param name Alias name (non-NULL) |
| 476 | * @param decl Field class to register (copied) |
| 477 | * @returns 0 if registration went okay, negative value otherwise |
| 478 | */ |
| 479 | static |
| 480 | int ctx_decl_scope_register_alias(struct ctx *ctx, struct ctx_decl_scope *scope, |
| 481 | const char *name, struct ctf_field_class *decl) |
| 482 | { |
| 483 | return ctx_decl_scope_register_prefix_alias(ctx, scope, _PREFIX_ALIAS, |
| 484 | name, (void *) decl); |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Registers an enumeration declaration within a declaration scope. |
| 489 | * |
| 490 | * @param scope Declaration scope |
| 491 | * @param name Enumeration name (non-NULL) |
| 492 | * @param decl Enumeration field class to register (copied) |
| 493 | * @returns 0 if registration went okay, negative value otherwise |
| 494 | */ |
| 495 | static |
| 496 | int ctx_decl_scope_register_enum(struct ctx *ctx, struct ctx_decl_scope *scope, |
| 497 | const char *name, struct ctf_field_class_enum *decl) |
| 498 | { |
| 499 | return ctx_decl_scope_register_prefix_alias(ctx, scope, _PREFIX_ENUM, |
| 500 | name, (void *) decl); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Registers a structure declaration within a declaration scope. |
| 505 | * |
| 506 | * @param scope Declaration scope |
| 507 | * @param name Structure name (non-NULL) |
| 508 | * @param decl Structure field class to register (copied) |
| 509 | * @returns 0 if registration went okay, negative value otherwise |
| 510 | */ |
| 511 | static |
| 512 | int ctx_decl_scope_register_struct(struct ctx *ctx, |
| 513 | struct ctx_decl_scope *scope, const char *name, |
| 514 | struct ctf_field_class_struct *decl) |
| 515 | { |
| 516 | return ctx_decl_scope_register_prefix_alias(ctx, scope, _PREFIX_STRUCT, |
| 517 | name, (void *) decl); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Registers a variant declaration within a declaration scope. |
| 522 | * |
| 523 | * @param scope Declaration scope |
| 524 | * @param name Variant name (non-NULL) |
| 525 | * @param decl Variant field class to register |
| 526 | * @returns 0 if registration went okay, negative value otherwise |
| 527 | */ |
| 528 | static |
| 529 | int ctx_decl_scope_register_variant(struct ctx *ctx, |
| 530 | struct ctx_decl_scope *scope, const char *name, |
| 531 | struct ctf_field_class_variant *decl) |
| 532 | { |
| 533 | return ctx_decl_scope_register_prefix_alias(ctx, scope, _PREFIX_VARIANT, |
| 534 | name, (void *) decl); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Destroys a visitor context. |
| 539 | * |
| 540 | * @param ctx Visitor context to destroy |
| 541 | */ |
| 542 | static |
| 543 | void ctx_destroy(struct ctx *ctx) |
| 544 | { |
| 545 | struct ctx_decl_scope *scope; |
| 546 | |
| 547 | if (!ctx) { |
| 548 | goto end; |
| 549 | } |
| 550 | |
| 551 | scope = ctx->current_scope; |
| 552 | |
| 553 | /* |
| 554 | * Destroy all scopes, from current one to the root scope. |
| 555 | */ |
| 556 | while (scope) { |
| 557 | struct ctx_decl_scope *parent_scope = scope->parent_scope; |
| 558 | |
| 559 | ctx_decl_scope_destroy(scope); |
| 560 | scope = parent_scope; |
| 561 | } |
| 562 | |
| 563 | bt_trace_class_put_ref(ctx->trace_class); |
| 564 | |
| 565 | if (ctx->ctf_tc) { |
| 566 | ctf_trace_class_destroy(ctx->ctf_tc); |
| 567 | } |
| 568 | |
| 569 | g_free(ctx); |
| 570 | |
| 571 | end: |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Creates a new visitor context. |
| 577 | * |
| 578 | * @param trace Associated trace |
| 579 | * @returns New visitor context, or NULL on error |
| 580 | */ |
| 581 | static |
| 582 | struct ctx *ctx_create(const struct ctf_metadata_decoder_config *decoder_config) |
| 583 | { |
| 584 | struct ctx *ctx = NULL; |
| 585 | |
| 586 | BT_ASSERT(decoder_config); |
| 587 | |
| 588 | ctx = g_new0(struct ctx, 1); |
| 589 | if (!ctx) { |
| 590 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, decoder_config->log_level, |
| 591 | decoder_config->self_comp, |
| 592 | "Failed to allocate one visitor context."); |
| 593 | goto error; |
| 594 | } |
| 595 | |
| 596 | ctx->log_cfg.log_level = decoder_config->log_level; |
| 597 | ctx->log_cfg.self_comp = decoder_config->self_comp; |
| 598 | |
| 599 | if (decoder_config->self_comp) { |
| 600 | ctx->trace_class = bt_trace_class_create( |
| 601 | decoder_config->self_comp); |
| 602 | if (!ctx->trace_class) { |
| 603 | BT_COMP_LOGE_STR("Cannot create empty trace class."); |
| 604 | goto error; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | ctx->ctf_tc = ctf_trace_class_create(); |
| 609 | if (!ctx->ctf_tc) { |
| 610 | BT_COMP_LOGE_STR("Cannot create CTF trace class."); |
| 611 | goto error; |
| 612 | } |
| 613 | |
| 614 | /* Root declaration scope */ |
| 615 | ctx->current_scope = ctx_decl_scope_create(ctx, NULL); |
| 616 | if (!ctx->current_scope) { |
| 617 | BT_COMP_LOGE_STR("Cannot create declaration scope."); |
| 618 | goto error; |
| 619 | } |
| 620 | |
| 621 | ctx->decoder_config = *decoder_config; |
| 622 | goto end; |
| 623 | |
| 624 | error: |
| 625 | ctx_destroy(ctx); |
| 626 | ctx = NULL; |
| 627 | |
| 628 | end: |
| 629 | return ctx; |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Pushes a new declaration scope on top of a visitor context's |
| 634 | * declaration scope stack. |
| 635 | * |
| 636 | * @param ctx Visitor context |
| 637 | * @returns 0 on success, or a negative value on error |
| 638 | */ |
| 639 | static |
| 640 | int ctx_push_scope(struct ctx *ctx) |
| 641 | { |
| 642 | int ret = 0; |
| 643 | struct ctx_decl_scope *new_scope; |
| 644 | |
| 645 | BT_ASSERT(ctx); |
| 646 | new_scope = ctx_decl_scope_create(ctx, ctx->current_scope); |
| 647 | if (!new_scope) { |
| 648 | BT_COMP_LOGE_STR("Cannot create declaration scope."); |
| 649 | ret = -ENOMEM; |
| 650 | goto end; |
| 651 | } |
| 652 | |
| 653 | ctx->current_scope = new_scope; |
| 654 | |
| 655 | end: |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | static |
| 660 | void ctx_pop_scope(struct ctx *ctx) |
| 661 | { |
| 662 | struct ctx_decl_scope *parent_scope = NULL; |
| 663 | |
| 664 | BT_ASSERT(ctx); |
| 665 | |
| 666 | if (!ctx->current_scope) { |
| 667 | goto end; |
| 668 | } |
| 669 | |
| 670 | parent_scope = ctx->current_scope->parent_scope; |
| 671 | ctx_decl_scope_destroy(ctx->current_scope); |
| 672 | ctx->current_scope = parent_scope; |
| 673 | |
| 674 | end: |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | static |
| 679 | int visit_field_class_specifier_list(struct ctx *ctx, struct ctf_node *ts_list, |
| 680 | struct ctf_field_class **decl); |
| 681 | |
| 682 | static |
| 683 | int is_unary_string(struct bt_list_head *head) |
| 684 | { |
| 685 | int ret = TRUE; |
| 686 | struct ctf_node *node; |
| 687 | |
| 688 | bt_list_for_each_entry(node, head, siblings) { |
| 689 | if (node->type != NODE_UNARY_EXPRESSION) { |
| 690 | ret = FALSE; |
| 691 | } |
| 692 | |
| 693 | if (node->u.unary_expression.type != UNARY_STRING) { |
| 694 | ret = FALSE; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | static |
| 702 | char *concatenate_unary_strings(struct bt_list_head *head) |
| 703 | { |
| 704 | int i = 0; |
| 705 | GString *str; |
| 706 | struct ctf_node *node; |
| 707 | |
| 708 | str = g_string_new(NULL); |
| 709 | BT_ASSERT(str); |
| 710 | |
| 711 | bt_list_for_each_entry(node, head, siblings) { |
| 712 | char *src_string; |
| 713 | |
| 714 | if ( |
| 715 | node->type != NODE_UNARY_EXPRESSION || |
| 716 | node->u.unary_expression.type != UNARY_STRING || |
| 717 | !( |
| 718 | ( |
| 719 | node->u.unary_expression.link != |
| 720 | UNARY_LINK_UNKNOWN |
| 721 | ) ^ (i == 0) |
| 722 | ) |
| 723 | ) { |
| 724 | goto error; |
| 725 | } |
| 726 | |
| 727 | switch (node->u.unary_expression.link) { |
| 728 | case UNARY_DOTLINK: |
| 729 | g_string_append(str, "."); |
| 730 | break; |
| 731 | case UNARY_ARROWLINK: |
| 732 | g_string_append(str, "->"); |
| 733 | break; |
| 734 | case UNARY_DOTDOTDOT: |
| 735 | g_string_append(str, "..."); |
| 736 | break; |
| 737 | default: |
| 738 | break; |
| 739 | } |
| 740 | |
| 741 | src_string = node->u.unary_expression.u.string; |
| 742 | g_string_append(str, src_string); |
| 743 | i++; |
| 744 | } |
| 745 | |
| 746 | /* Destroys the container, returns the underlying string */ |
| 747 | return g_string_free(str, FALSE); |
| 748 | |
| 749 | error: |
| 750 | /* This always returns NULL */ |
| 751 | return g_string_free(str, TRUE); |
| 752 | } |
| 753 | |
| 754 | static |
| 755 | const char *get_map_clock_name_value(struct bt_list_head *head) |
| 756 | { |
| 757 | int i = 0; |
| 758 | struct ctf_node *node; |
| 759 | const char *name = NULL; |
| 760 | |
| 761 | bt_list_for_each_entry(node, head, siblings) { |
| 762 | char *src_string; |
| 763 | int uexpr_type = node->u.unary_expression.type; |
| 764 | int uexpr_link = node->u.unary_expression.link; |
| 765 | int cond = node->type != NODE_UNARY_EXPRESSION || |
| 766 | uexpr_type != UNARY_STRING || |
| 767 | !((uexpr_link != UNARY_LINK_UNKNOWN) ^ (i == 0)); |
| 768 | if (cond) { |
| 769 | goto error; |
| 770 | } |
| 771 | |
| 772 | /* Needs to be chained with . */ |
| 773 | switch (node->u.unary_expression.link) { |
| 774 | case UNARY_DOTLINK: |
| 775 | break; |
| 776 | case UNARY_ARROWLINK: |
| 777 | case UNARY_DOTDOTDOT: |
| 778 | goto error; |
| 779 | default: |
| 780 | break; |
| 781 | } |
| 782 | |
| 783 | src_string = node->u.unary_expression.u.string; |
| 784 | |
| 785 | switch (i) { |
| 786 | case 0: |
| 787 | if (strcmp("clock", src_string)) { |
| 788 | goto error; |
| 789 | } |
| 790 | break; |
| 791 | case 1: |
| 792 | name = src_string; |
| 793 | break; |
| 794 | case 2: |
| 795 | if (strcmp("value", src_string)) { |
| 796 | goto error; |
| 797 | } |
| 798 | break; |
| 799 | default: |
| 800 | /* Extra identifier, unknown */ |
| 801 | goto error; |
| 802 | } |
| 803 | |
| 804 | i++; |
| 805 | } |
| 806 | |
| 807 | return name; |
| 808 | |
| 809 | error: |
| 810 | return NULL; |
| 811 | } |
| 812 | |
| 813 | static |
| 814 | int is_unary_unsigned(struct bt_list_head *head) |
| 815 | { |
| 816 | int ret = TRUE; |
| 817 | struct ctf_node *node; |
| 818 | |
| 819 | bt_list_for_each_entry(node, head, siblings) { |
| 820 | if (node->type != NODE_UNARY_EXPRESSION) { |
| 821 | ret = FALSE; |
| 822 | } |
| 823 | |
| 824 | if (node->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) { |
| 825 | ret = FALSE; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | static |
| 833 | int get_unary_unsigned(struct ctx *ctx, struct bt_list_head *head, |
| 834 | uint64_t *value) |
| 835 | { |
| 836 | int i = 0; |
| 837 | int ret = 0; |
| 838 | struct ctf_node *node; |
| 839 | |
| 840 | *value = 0; |
| 841 | |
| 842 | if (bt_list_empty(head)) { |
| 843 | ret = -1; |
| 844 | goto end; |
| 845 | } |
| 846 | |
| 847 | bt_list_for_each_entry(node, head, siblings) { |
| 848 | int uexpr_type = node->u.unary_expression.type; |
| 849 | int uexpr_link = node->u.unary_expression.link; |
| 850 | int cond = node->type != NODE_UNARY_EXPRESSION || |
| 851 | uexpr_type != UNARY_UNSIGNED_CONSTANT || |
| 852 | uexpr_link != UNARY_LINK_UNKNOWN || i != 0; |
| 853 | if (cond) { |
| 854 | _BT_COMP_LOGE_NODE(node, "Invalid constant unsigned integer."); |
| 855 | ret = -EINVAL; |
| 856 | goto end; |
| 857 | } |
| 858 | |
| 859 | *value = node->u.unary_expression.u.unsigned_constant; |
| 860 | i++; |
| 861 | } |
| 862 | |
| 863 | end: |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | static |
| 868 | int is_unary_signed(struct bt_list_head *head) |
| 869 | { |
| 870 | int ret = TRUE; |
| 871 | struct ctf_node *node; |
| 872 | |
| 873 | bt_list_for_each_entry(node, head, siblings) { |
| 874 | if (node->type != NODE_UNARY_EXPRESSION) { |
| 875 | ret = FALSE; |
| 876 | } |
| 877 | |
| 878 | if (node->u.unary_expression.type != UNARY_SIGNED_CONSTANT) { |
| 879 | ret = FALSE; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | return ret; |
| 884 | } |
| 885 | |
| 886 | static |
| 887 | int get_unary_signed(struct bt_list_head *head, int64_t *value) |
| 888 | { |
| 889 | int i = 0; |
| 890 | int ret = 0; |
| 891 | struct ctf_node *node; |
| 892 | |
| 893 | bt_list_for_each_entry(node, head, siblings) { |
| 894 | int uexpr_type = node->u.unary_expression.type; |
| 895 | int uexpr_link = node->u.unary_expression.link; |
| 896 | int cond = node->type != NODE_UNARY_EXPRESSION || |
| 897 | (uexpr_type != UNARY_UNSIGNED_CONSTANT && |
| 898 | uexpr_type != UNARY_SIGNED_CONSTANT) || |
| 899 | uexpr_link != UNARY_LINK_UNKNOWN || i != 0; |
| 900 | if (cond) { |
| 901 | ret = -EINVAL; |
| 902 | goto end; |
| 903 | } |
| 904 | |
| 905 | switch (uexpr_type) { |
| 906 | case UNARY_UNSIGNED_CONSTANT: |
| 907 | *value = (int64_t) |
| 908 | node->u.unary_expression.u.unsigned_constant; |
| 909 | break; |
| 910 | case UNARY_SIGNED_CONSTANT: |
| 911 | *value = node->u.unary_expression.u.signed_constant; |
| 912 | break; |
| 913 | default: |
| 914 | ret = -EINVAL; |
| 915 | goto end; |
| 916 | } |
| 917 | |
| 918 | i++; |
| 919 | } |
| 920 | |
| 921 | end: |
| 922 | return ret; |
| 923 | } |
| 924 | |
| 925 | static |
| 926 | int get_unary_uuid(struct ctx *ctx, struct bt_list_head *head, |
| 927 | uint8_t *uuid) |
| 928 | { |
| 929 | int i = 0; |
| 930 | int ret = 0; |
| 931 | struct ctf_node *node; |
| 932 | |
| 933 | bt_list_for_each_entry(node, head, siblings) { |
| 934 | int uexpr_type = node->u.unary_expression.type; |
| 935 | int uexpr_link = node->u.unary_expression.link; |
| 936 | const char *src_string; |
| 937 | |
| 938 | if (node->type != NODE_UNARY_EXPRESSION || |
| 939 | uexpr_type != UNARY_STRING || |
| 940 | uexpr_link != UNARY_LINK_UNKNOWN || |
| 941 | i != 0) { |
| 942 | ret = -EINVAL; |
| 943 | goto end; |
| 944 | } |
| 945 | |
| 946 | src_string = node->u.unary_expression.u.string; |
| 947 | ret = bt_uuid_from_str(src_string, uuid); |
| 948 | if (ret) { |
| 949 | _BT_COMP_LOGE_NODE(node, |
| 950 | "Cannot parse UUID: uuid=\"%s\"", src_string); |
| 951 | goto end; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | end: |
| 956 | return ret; |
| 957 | } |
| 958 | |
| 959 | static |
| 960 | int get_boolean(struct ctx *ctx, struct ctf_node *unary_expr) |
| 961 | { |
| 962 | int ret = 0; |
| 963 | |
| 964 | if (unary_expr->type != NODE_UNARY_EXPRESSION) { |
| 965 | _BT_COMP_LOGE_NODE(unary_expr, |
| 966 | "Expecting unary expression: node-type=%d", |
| 967 | unary_expr->type); |
| 968 | ret = -EINVAL; |
| 969 | goto end; |
| 970 | } |
| 971 | |
| 972 | switch (unary_expr->u.unary_expression.type) { |
| 973 | case UNARY_UNSIGNED_CONSTANT: |
| 974 | ret = (unary_expr->u.unary_expression.u.unsigned_constant != 0); |
| 975 | break; |
| 976 | case UNARY_SIGNED_CONSTANT: |
| 977 | ret = (unary_expr->u.unary_expression.u.signed_constant != 0); |
| 978 | break; |
| 979 | case UNARY_STRING: |
| 980 | { |
| 981 | const char *str = unary_expr->u.unary_expression.u.string; |
| 982 | |
| 983 | if (strcmp(str, "true") == 0 || strcmp(str, "TRUE") == 0) { |
| 984 | ret = TRUE; |
| 985 | } else if (strcmp(str, "false") == 0 || strcmp(str, "FALSE") == 0) { |
| 986 | ret = FALSE; |
| 987 | } else { |
| 988 | _BT_COMP_LOGE_NODE(unary_expr, |
| 989 | "Unexpected boolean value: value=\"%s\"", str); |
| 990 | ret = -EINVAL; |
| 991 | goto end; |
| 992 | } |
| 993 | break; |
| 994 | } |
| 995 | default: |
| 996 | _BT_COMP_LOGE_NODE(unary_expr, |
| 997 | "Unexpected unary expression type: node-type=%d", |
| 998 | unary_expr->u.unary_expression.type); |
| 999 | ret = -EINVAL; |
| 1000 | goto end; |
| 1001 | } |
| 1002 | |
| 1003 | end: |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
| 1007 | static |
| 1008 | enum ctf_byte_order byte_order_from_unary_expr(struct ctx *ctx, |
| 1009 | struct ctf_node *unary_expr) |
| 1010 | { |
| 1011 | const char *str; |
| 1012 | enum ctf_byte_order bo = CTF_BYTE_ORDER_UNKNOWN; |
| 1013 | |
| 1014 | if (unary_expr->u.unary_expression.type != UNARY_STRING) { |
| 1015 | _BT_COMP_LOGE_NODE(unary_expr, |
| 1016 | "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`."); |
| 1017 | goto end; |
| 1018 | } |
| 1019 | |
| 1020 | str = unary_expr->u.unary_expression.u.string; |
| 1021 | |
| 1022 | if (strcmp(str, "be") == 0 || strcmp(str, "network") == 0) { |
| 1023 | bo = CTF_BYTE_ORDER_BIG; |
| 1024 | } else if (strcmp(str, "le") == 0) { |
| 1025 | bo = CTF_BYTE_ORDER_LITTLE; |
| 1026 | } else if (strcmp(str, "native") == 0) { |
| 1027 | bo = CTF_BYTE_ORDER_DEFAULT; |
| 1028 | } else { |
| 1029 | _BT_COMP_LOGE_NODE(unary_expr, |
| 1030 | "Unexpected \"byte_order\" attribute value: " |
| 1031 | "expecting `be`, `le`, `network`, or `native`: value=\"%s\"", |
| 1032 | str); |
| 1033 | goto end; |
| 1034 | } |
| 1035 | |
| 1036 | end: |
| 1037 | return bo; |
| 1038 | } |
| 1039 | |
| 1040 | static |
| 1041 | enum ctf_byte_order get_real_byte_order(struct ctx *ctx, |
| 1042 | struct ctf_node *uexpr) |
| 1043 | { |
| 1044 | enum ctf_byte_order bo = byte_order_from_unary_expr(ctx, uexpr); |
| 1045 | |
| 1046 | if (bo == CTF_BYTE_ORDER_DEFAULT) { |
| 1047 | bo = ctx->ctf_tc->default_byte_order; |
| 1048 | } |
| 1049 | |
| 1050 | return bo; |
| 1051 | } |
| 1052 | |
| 1053 | static |
| 1054 | int is_align_valid(uint64_t align) |
| 1055 | { |
| 1056 | return (align != 0) && !(align & (align - UINT64_C(1))); |
| 1057 | } |
| 1058 | |
| 1059 | static |
| 1060 | int get_class_specifier_name(struct ctx *ctx, struct ctf_node *cls_specifier, |
| 1061 | GString *str) |
| 1062 | { |
| 1063 | int ret = 0; |
| 1064 | |
| 1065 | if (cls_specifier->type != NODE_TYPE_SPECIFIER) { |
| 1066 | _BT_COMP_LOGE_NODE(cls_specifier, |
| 1067 | "Unexpected node type: node-type=%d", |
| 1068 | cls_specifier->type); |
| 1069 | ret = -EINVAL; |
| 1070 | goto end; |
| 1071 | } |
| 1072 | |
| 1073 | switch (cls_specifier->u.field_class_specifier.type) { |
| 1074 | case TYPESPEC_VOID: |
| 1075 | g_string_append(str, "void"); |
| 1076 | break; |
| 1077 | case TYPESPEC_CHAR: |
| 1078 | g_string_append(str, "char"); |
| 1079 | break; |
| 1080 | case TYPESPEC_SHORT: |
| 1081 | g_string_append(str, "short"); |
| 1082 | break; |
| 1083 | case TYPESPEC_INT: |
| 1084 | g_string_append(str, "int"); |
| 1085 | break; |
| 1086 | case TYPESPEC_LONG: |
| 1087 | g_string_append(str, "long"); |
| 1088 | break; |
| 1089 | case TYPESPEC_FLOAT: |
| 1090 | g_string_append(str, "float"); |
| 1091 | break; |
| 1092 | case TYPESPEC_DOUBLE: |
| 1093 | g_string_append(str, "double"); |
| 1094 | break; |
| 1095 | case TYPESPEC_SIGNED: |
| 1096 | g_string_append(str, "signed"); |
| 1097 | break; |
| 1098 | case TYPESPEC_UNSIGNED: |
| 1099 | g_string_append(str, "unsigned"); |
| 1100 | break; |
| 1101 | case TYPESPEC_BOOL: |
| 1102 | g_string_append(str, "bool"); |
| 1103 | break; |
| 1104 | case TYPESPEC_COMPLEX: |
| 1105 | g_string_append(str, "_Complex"); |
| 1106 | break; |
| 1107 | case TYPESPEC_IMAGINARY: |
| 1108 | g_string_append(str, "_Imaginary"); |
| 1109 | break; |
| 1110 | case TYPESPEC_CONST: |
| 1111 | g_string_append(str, "const"); |
| 1112 | break; |
| 1113 | case TYPESPEC_ID_TYPE: |
| 1114 | if (cls_specifier->u.field_class_specifier.id_type) { |
| 1115 | g_string_append(str, |
| 1116 | cls_specifier->u.field_class_specifier.id_type); |
| 1117 | } |
| 1118 | break; |
| 1119 | case TYPESPEC_STRUCT: |
| 1120 | { |
| 1121 | struct ctf_node *node = cls_specifier->u.field_class_specifier.node; |
| 1122 | |
| 1123 | if (!node->u._struct.name) { |
| 1124 | _BT_COMP_LOGE_NODE(node, "Unexpected empty structure field class name."); |
| 1125 | ret = -EINVAL; |
| 1126 | goto end; |
| 1127 | } |
| 1128 | |
| 1129 | g_string_append(str, "struct "); |
| 1130 | g_string_append(str, node->u._struct.name); |
| 1131 | break; |
| 1132 | } |
| 1133 | case TYPESPEC_VARIANT: |
| 1134 | { |
| 1135 | struct ctf_node *node = cls_specifier->u.field_class_specifier.node; |
| 1136 | |
| 1137 | if (!node->u.variant.name) { |
| 1138 | _BT_COMP_LOGE_NODE(node, "Unexpected empty variant field class name."); |
| 1139 | ret = -EINVAL; |
| 1140 | goto end; |
| 1141 | } |
| 1142 | |
| 1143 | g_string_append(str, "variant "); |
| 1144 | g_string_append(str, node->u.variant.name); |
| 1145 | break; |
| 1146 | } |
| 1147 | case TYPESPEC_ENUM: |
| 1148 | { |
| 1149 | struct ctf_node *node = cls_specifier->u.field_class_specifier.node; |
| 1150 | |
| 1151 | if (!node->u._enum.enum_id) { |
| 1152 | _BT_COMP_LOGE_NODE(node, |
| 1153 | "Unexpected empty enumeration field class (`enum`) name."); |
| 1154 | ret = -EINVAL; |
| 1155 | goto end; |
| 1156 | } |
| 1157 | |
| 1158 | g_string_append(str, "enum "); |
| 1159 | g_string_append(str, node->u._enum.enum_id); |
| 1160 | break; |
| 1161 | } |
| 1162 | case TYPESPEC_FLOATING_POINT: |
| 1163 | case TYPESPEC_INTEGER: |
| 1164 | case TYPESPEC_STRING: |
| 1165 | default: |
| 1166 | _BT_COMP_LOGE_NODE(cls_specifier->u.field_class_specifier.node, |
| 1167 | "Unexpected field class specifier type: %d", |
| 1168 | cls_specifier->u.field_class_specifier.type); |
| 1169 | ret = -EINVAL; |
| 1170 | goto end; |
| 1171 | } |
| 1172 | |
| 1173 | end: |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | static |
| 1178 | int get_class_specifier_list_name(struct ctx *ctx, |
| 1179 | struct ctf_node *cls_specifier_list, GString *str) |
| 1180 | { |
| 1181 | int ret = 0; |
| 1182 | struct ctf_node *iter; |
| 1183 | int alias_item_nr = 0; |
| 1184 | struct bt_list_head *head = |
| 1185 | &cls_specifier_list->u.field_class_specifier_list.head; |
| 1186 | |
| 1187 | bt_list_for_each_entry(iter, head, siblings) { |
| 1188 | if (alias_item_nr != 0) { |
| 1189 | g_string_append(str, " "); |
| 1190 | } |
| 1191 | |
| 1192 | alias_item_nr++; |
| 1193 | ret = get_class_specifier_name(ctx, iter, str); |
| 1194 | if (ret) { |
| 1195 | goto end; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | end: |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
| 1203 | static |
| 1204 | GQuark create_class_alias_identifier(struct ctx *ctx, |
| 1205 | struct ctf_node *cls_specifier_list, |
| 1206 | struct ctf_node *node_field_class_declarator) |
| 1207 | { |
| 1208 | int ret; |
| 1209 | char *str_c; |
| 1210 | GString *str; |
| 1211 | GQuark qalias = 0; |
| 1212 | struct ctf_node *iter; |
| 1213 | struct bt_list_head *pointers = |
| 1214 | &node_field_class_declarator->u.field_class_declarator.pointers; |
| 1215 | |
| 1216 | str = g_string_new(""); |
| 1217 | ret = get_class_specifier_list_name(ctx, cls_specifier_list, str); |
| 1218 | if (ret) { |
| 1219 | g_string_free(str, TRUE); |
| 1220 | goto end; |
| 1221 | } |
| 1222 | |
| 1223 | bt_list_for_each_entry(iter, pointers, siblings) { |
| 1224 | g_string_append(str, " *"); |
| 1225 | |
| 1226 | if (iter->u.pointer.const_qualifier) { |
| 1227 | g_string_append(str, " const"); |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | str_c = g_string_free(str, FALSE); |
| 1232 | qalias = g_quark_from_string(str_c); |
| 1233 | g_free(str_c); |
| 1234 | |
| 1235 | end: |
| 1236 | return qalias; |
| 1237 | } |
| 1238 | |
| 1239 | static |
| 1240 | int visit_field_class_declarator(struct ctx *ctx, |
| 1241 | struct ctf_node *cls_specifier_list, |
| 1242 | GQuark *field_name, struct ctf_node *node_field_class_declarator, |
| 1243 | struct ctf_field_class **field_decl, |
| 1244 | struct ctf_field_class *nested_decl) |
| 1245 | { |
| 1246 | /* |
| 1247 | * During this whole function, nested_decl is always OURS, |
| 1248 | * whereas field_decl is an output which we create, but |
| 1249 | * belongs to the caller (it is moved). |
| 1250 | */ |
| 1251 | int ret = 0; |
| 1252 | *field_decl = NULL; |
| 1253 | |
| 1254 | /* Validate field class declarator node */ |
| 1255 | if (node_field_class_declarator) { |
| 1256 | if (node_field_class_declarator->u.field_class_declarator.type == |
| 1257 | TYPEDEC_UNKNOWN) { |
| 1258 | _BT_COMP_LOGE_NODE(node_field_class_declarator, |
| 1259 | "Unexpected field class declarator type: type=%d", |
| 1260 | node_field_class_declarator->u.field_class_declarator.type); |
| 1261 | ret = -EINVAL; |
| 1262 | goto error; |
| 1263 | } |
| 1264 | |
| 1265 | /* TODO: GCC bitfields not supported yet */ |
| 1266 | if (node_field_class_declarator->u.field_class_declarator.bitfield_len != |
| 1267 | NULL) { |
| 1268 | _BT_COMP_LOGE_NODE(node_field_class_declarator, |
| 1269 | "GCC bitfields are not supported as of this version."); |
| 1270 | ret = -EPERM; |
| 1271 | goto error; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | /* Find the right nested declaration if not provided */ |
| 1276 | if (!nested_decl) { |
| 1277 | struct bt_list_head *pointers = |
| 1278 | &node_field_class_declarator->u.field_class_declarator.pointers; |
| 1279 | |
| 1280 | if (node_field_class_declarator && !bt_list_empty(pointers)) { |
| 1281 | GQuark qalias; |
| 1282 | |
| 1283 | /* |
| 1284 | * If we have a pointer declarator, it HAS to |
| 1285 | * be present in the field class aliases (else |
| 1286 | * fail). |
| 1287 | */ |
| 1288 | qalias = create_class_alias_identifier(ctx, |
| 1289 | cls_specifier_list, node_field_class_declarator); |
| 1290 | nested_decl = |
| 1291 | ctx_decl_scope_lookup_alias(ctx, |
| 1292 | ctx->current_scope, |
| 1293 | g_quark_to_string(qalias), -1, true); |
| 1294 | if (!nested_decl) { |
| 1295 | _BT_COMP_LOGE_NODE(node_field_class_declarator, |
| 1296 | "Cannot find class alias: name=\"%s\"", |
| 1297 | g_quark_to_string(qalias)); |
| 1298 | ret = -EINVAL; |
| 1299 | goto error; |
| 1300 | } |
| 1301 | |
| 1302 | if (nested_decl->type == CTF_FIELD_CLASS_TYPE_INT) { |
| 1303 | /* Pointer: force integer's base to 16 */ |
| 1304 | struct ctf_field_class_int *int_fc = |
| 1305 | (void *) nested_decl; |
| 1306 | |
| 1307 | int_fc->disp_base = |
| 1308 | BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL; |
| 1309 | } |
| 1310 | } else { |
| 1311 | ret = visit_field_class_specifier_list(ctx, |
| 1312 | cls_specifier_list, &nested_decl); |
| 1313 | if (ret) { |
| 1314 | BT_ASSERT(!nested_decl); |
| 1315 | goto error; |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | BT_ASSERT(nested_decl); |
| 1321 | |
| 1322 | if (!node_field_class_declarator) { |
| 1323 | *field_decl = nested_decl; |
| 1324 | nested_decl = NULL; |
| 1325 | goto end; |
| 1326 | } |
| 1327 | |
| 1328 | if (node_field_class_declarator->u.field_class_declarator.type == TYPEDEC_ID) { |
| 1329 | if (node_field_class_declarator->u.field_class_declarator.u.id) { |
| 1330 | const char *id = |
| 1331 | node_field_class_declarator->u.field_class_declarator.u.id; |
| 1332 | |
| 1333 | *field_name = g_quark_from_string(id); |
| 1334 | } else { |
| 1335 | *field_name = 0; |
| 1336 | } |
| 1337 | |
| 1338 | *field_decl = nested_decl; |
| 1339 | nested_decl = NULL; |
| 1340 | goto end; |
| 1341 | } else { |
| 1342 | struct ctf_node *first; |
| 1343 | struct ctf_field_class *decl = NULL; |
| 1344 | struct ctf_field_class *outer_field_decl = NULL; |
| 1345 | struct bt_list_head *length = |
| 1346 | &node_field_class_declarator-> |
| 1347 | u.field_class_declarator.u.nested.length; |
| 1348 | |
| 1349 | /* Create array/sequence, pass nested_decl as child */ |
| 1350 | if (bt_list_empty(length)) { |
| 1351 | _BT_COMP_LOGE_NODE(node_field_class_declarator, |
| 1352 | "Expecting length field reference or value."); |
| 1353 | ret = -EINVAL; |
| 1354 | goto error; |
| 1355 | } |
| 1356 | |
| 1357 | first = _BT_LIST_FIRST_ENTRY(length, struct ctf_node, siblings); |
| 1358 | if (first->type != NODE_UNARY_EXPRESSION) { |
| 1359 | _BT_COMP_LOGE_NODE(first, |
| 1360 | "Unexpected node type: node-type=%d", |
| 1361 | first->type); |
| 1362 | ret = -EINVAL; |
| 1363 | goto error; |
| 1364 | } |
| 1365 | |
| 1366 | switch (first->u.unary_expression.type) { |
| 1367 | case UNARY_UNSIGNED_CONSTANT: |
| 1368 | { |
| 1369 | struct ctf_field_class_array *array_decl = NULL; |
| 1370 | |
| 1371 | array_decl = ctf_field_class_array_create(); |
| 1372 | BT_ASSERT(array_decl); |
| 1373 | array_decl->length = |
| 1374 | first->u.unary_expression.u.unsigned_constant; |
| 1375 | array_decl->base.elem_fc = nested_decl; |
| 1376 | nested_decl = NULL; |
| 1377 | decl = (void *) array_decl; |
| 1378 | break; |
| 1379 | } |
| 1380 | case UNARY_STRING: |
| 1381 | { |
| 1382 | /* Lookup unsigned integer definition, create seq. */ |
| 1383 | struct ctf_field_class_sequence *seq_decl = NULL; |
| 1384 | char *length_name = concatenate_unary_strings(length); |
| 1385 | |
| 1386 | if (!length_name) { |
| 1387 | _BT_COMP_LOGE_NODE(node_field_class_declarator, |
| 1388 | "Cannot concatenate unary strings."); |
| 1389 | ret = -EINVAL; |
| 1390 | goto error; |
| 1391 | } |
| 1392 | |
| 1393 | if (strncmp(length_name, "env.", 4) == 0) { |
| 1394 | /* This is, in fact, an array */ |
| 1395 | const char *env_entry_name = &length_name[4]; |
| 1396 | struct ctf_trace_class_env_entry *env_entry = |
| 1397 | ctf_trace_class_borrow_env_entry_by_name( |
| 1398 | ctx->ctf_tc, env_entry_name); |
| 1399 | struct ctf_field_class_array *array_decl; |
| 1400 | |
| 1401 | if (!env_entry) { |
| 1402 | _BT_COMP_LOGE_NODE(node_field_class_declarator, |
| 1403 | "Cannot find environment entry: " |
| 1404 | "name=\"%s\"", env_entry_name); |
| 1405 | ret = -EINVAL; |
| 1406 | goto error; |
| 1407 | } |
| 1408 | |
| 1409 | if (env_entry->type != CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT) { |
| 1410 | _BT_COMP_LOGE_NODE(node_field_class_declarator, |
| 1411 | "Wrong environment entry type " |
| 1412 | "(expecting integer): " |
| 1413 | "name=\"%s\"", env_entry_name); |
| 1414 | ret = -EINVAL; |
| 1415 | goto error; |
| 1416 | } |
| 1417 | |
| 1418 | if (env_entry->value.i < 0) { |
| 1419 | _BT_COMP_LOGE_NODE(node_field_class_declarator, |
| 1420 | "Invalid, negative array length: " |
| 1421 | "env-entry-name=\"%s\", " |
| 1422 | "value=%" PRId64, |
| 1423 | env_entry_name, |
| 1424 | env_entry->value.i); |
| 1425 | ret = -EINVAL; |
| 1426 | goto error; |
| 1427 | } |
| 1428 | |
| 1429 | array_decl = ctf_field_class_array_create(); |
| 1430 | BT_ASSERT(array_decl); |
| 1431 | array_decl->length = |
| 1432 | (uint64_t) env_entry->value.i; |
| 1433 | array_decl->base.elem_fc = nested_decl; |
| 1434 | nested_decl = NULL; |
| 1435 | decl = (void *) array_decl; |
| 1436 | } else { |
| 1437 | seq_decl = ctf_field_class_sequence_create(); |
| 1438 | BT_ASSERT(seq_decl); |
| 1439 | seq_decl->base.elem_fc = nested_decl; |
| 1440 | nested_decl = NULL; |
| 1441 | g_string_assign(seq_decl->length_ref, |
| 1442 | length_name); |
| 1443 | decl = (void *) seq_decl; |
| 1444 | } |
| 1445 | |
| 1446 | g_free(length_name); |
| 1447 | break; |
| 1448 | } |
| 1449 | default: |
| 1450 | ret = -EINVAL; |
| 1451 | goto error; |
| 1452 | } |
| 1453 | |
| 1454 | BT_ASSERT(!nested_decl); |
| 1455 | BT_ASSERT(decl); |
| 1456 | BT_ASSERT(!*field_decl); |
| 1457 | |
| 1458 | /* |
| 1459 | * At this point, we found the next nested declaration. |
| 1460 | * We currently own this (and lost the ownership of |
| 1461 | * nested_decl in the meantime). Pass this next |
| 1462 | * nested declaration as the content of the outer |
| 1463 | * container, MOVING its ownership. |
| 1464 | */ |
| 1465 | ret = visit_field_class_declarator(ctx, cls_specifier_list, |
| 1466 | field_name, |
| 1467 | node_field_class_declarator-> |
| 1468 | u.field_class_declarator.u.nested.field_class_declarator, |
| 1469 | &outer_field_decl, decl); |
| 1470 | decl = NULL; |
| 1471 | if (ret) { |
| 1472 | BT_ASSERT(!outer_field_decl); |
| 1473 | ret = -EINVAL; |
| 1474 | goto error; |
| 1475 | } |
| 1476 | |
| 1477 | BT_ASSERT(outer_field_decl); |
| 1478 | *field_decl = outer_field_decl; |
| 1479 | outer_field_decl = NULL; |
| 1480 | } |
| 1481 | |
| 1482 | BT_ASSERT(*field_decl); |
| 1483 | goto end; |
| 1484 | |
| 1485 | error: |
| 1486 | ctf_field_class_destroy(*field_decl); |
| 1487 | *field_decl = NULL; |
| 1488 | |
| 1489 | if (ret >= 0) { |
| 1490 | ret = -1; |
| 1491 | } |
| 1492 | |
| 1493 | end: |
| 1494 | ctf_field_class_destroy(nested_decl); |
| 1495 | nested_decl = NULL; |
| 1496 | return ret; |
| 1497 | } |
| 1498 | |
| 1499 | static |
| 1500 | int visit_struct_decl_field(struct ctx *ctx, |
| 1501 | struct ctf_field_class_struct *struct_decl, |
| 1502 | struct ctf_node *cls_specifier_list, |
| 1503 | struct bt_list_head *field_class_declarators) |
| 1504 | { |
| 1505 | int ret = 0; |
| 1506 | struct ctf_node *iter; |
| 1507 | struct ctf_field_class *field_decl = NULL; |
| 1508 | |
| 1509 | bt_list_for_each_entry(iter, field_class_declarators, siblings) { |
| 1510 | field_decl = NULL; |
| 1511 | GQuark qfield_name; |
| 1512 | const char *field_name; |
| 1513 | |
| 1514 | ret = visit_field_class_declarator(ctx, cls_specifier_list, |
| 1515 | &qfield_name, iter, &field_decl, NULL); |
| 1516 | if (ret) { |
| 1517 | BT_ASSERT(!field_decl); |
| 1518 | _BT_COMP_LOGE_NODE(cls_specifier_list, |
| 1519 | "Cannot visit field class declarator: ret=%d", ret); |
| 1520 | goto error; |
| 1521 | } |
| 1522 | |
| 1523 | BT_ASSERT(field_decl); |
| 1524 | field_name = g_quark_to_string(qfield_name); |
| 1525 | |
| 1526 | /* Check if field with same name already exists */ |
| 1527 | if (ctf_field_class_struct_borrow_member_by_name( |
| 1528 | struct_decl, field_name)) { |
| 1529 | _BT_COMP_LOGE_NODE(cls_specifier_list, |
| 1530 | "Duplicate field in structure field class: " |
| 1531 | "field-name=\"%s\"", field_name); |
| 1532 | ret = -EINVAL; |
| 1533 | goto error; |
| 1534 | } |
| 1535 | |
| 1536 | /* Add field to structure */ |
| 1537 | ctf_field_class_struct_append_member(struct_decl, |
| 1538 | field_name, field_decl); |
| 1539 | field_decl = NULL; |
| 1540 | } |
| 1541 | |
| 1542 | return 0; |
| 1543 | |
| 1544 | error: |
| 1545 | ctf_field_class_destroy(field_decl); |
| 1546 | field_decl = NULL; |
| 1547 | return ret; |
| 1548 | } |
| 1549 | |
| 1550 | static |
| 1551 | int visit_variant_decl_field(struct ctx *ctx, |
| 1552 | struct ctf_field_class_variant *variant_decl, |
| 1553 | struct ctf_node *cls_specifier_list, |
| 1554 | struct bt_list_head *field_class_declarators) |
| 1555 | { |
| 1556 | int ret = 0; |
| 1557 | struct ctf_node *iter; |
| 1558 | struct ctf_field_class *field_decl = NULL; |
| 1559 | |
| 1560 | bt_list_for_each_entry(iter, field_class_declarators, siblings) { |
| 1561 | field_decl = NULL; |
| 1562 | GQuark qfield_name; |
| 1563 | const char *field_name; |
| 1564 | |
| 1565 | ret = visit_field_class_declarator(ctx, cls_specifier_list, |
| 1566 | &qfield_name, iter, &field_decl, NULL); |
| 1567 | if (ret) { |
| 1568 | BT_ASSERT(!field_decl); |
| 1569 | _BT_COMP_LOGE_NODE(cls_specifier_list, |
| 1570 | "Cannot visit field class declarator: ret=%d", ret); |
| 1571 | goto error; |
| 1572 | } |
| 1573 | |
| 1574 | BT_ASSERT(field_decl); |
| 1575 | field_name = g_quark_to_string(qfield_name); |
| 1576 | |
| 1577 | /* Check if field with same name already exists */ |
| 1578 | if (ctf_field_class_variant_borrow_option_by_name( |
| 1579 | variant_decl, field_name)) { |
| 1580 | _BT_COMP_LOGE_NODE(cls_specifier_list, |
| 1581 | "Duplicate field in variant field class: " |
| 1582 | "field-name=\"%s\"", field_name); |
| 1583 | ret = -EINVAL; |
| 1584 | goto error; |
| 1585 | } |
| 1586 | |
| 1587 | /* Add field to structure */ |
| 1588 | ctf_field_class_variant_append_option(variant_decl, |
| 1589 | field_name, field_decl); |
| 1590 | field_decl = NULL; |
| 1591 | } |
| 1592 | |
| 1593 | return 0; |
| 1594 | |
| 1595 | error: |
| 1596 | ctf_field_class_destroy(field_decl); |
| 1597 | field_decl = NULL; |
| 1598 | return ret; |
| 1599 | } |
| 1600 | |
| 1601 | static |
| 1602 | int visit_field_class_def(struct ctx *ctx, struct ctf_node *cls_specifier_list, |
| 1603 | struct bt_list_head *field_class_declarators) |
| 1604 | { |
| 1605 | int ret = 0; |
| 1606 | GQuark qidentifier; |
| 1607 | struct ctf_node *iter; |
| 1608 | struct ctf_field_class *class_decl = NULL; |
| 1609 | |
| 1610 | bt_list_for_each_entry(iter, field_class_declarators, siblings) { |
| 1611 | ret = visit_field_class_declarator(ctx, cls_specifier_list, |
| 1612 | &qidentifier, iter, &class_decl, NULL); |
| 1613 | if (ret) { |
| 1614 | _BT_COMP_LOGE_NODE(iter, |
| 1615 | "Cannot visit field class declarator: ret=%d", ret); |
| 1616 | ret = -EINVAL; |
| 1617 | goto end; |
| 1618 | } |
| 1619 | |
| 1620 | /* Do not allow field class def and alias of untagged variants */ |
| 1621 | if (class_decl->type == CTF_FIELD_CLASS_TYPE_VARIANT) { |
| 1622 | struct ctf_field_class_variant *var_fc = |
| 1623 | (void *) class_decl; |
| 1624 | |
| 1625 | if (var_fc->tag_path.path->len == 0) { |
| 1626 | _BT_COMP_LOGE_NODE(iter, |
| 1627 | "Type definition of untagged variant field class is not allowed."); |
| 1628 | ret = -EPERM; |
| 1629 | goto end; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | ret = ctx_decl_scope_register_alias(ctx, ctx->current_scope, |
| 1634 | g_quark_to_string(qidentifier), class_decl); |
| 1635 | if (ret) { |
| 1636 | _BT_COMP_LOGE_NODE(iter, |
| 1637 | "Cannot register field class alias: name=\"%s\"", |
| 1638 | g_quark_to_string(qidentifier)); |
| 1639 | goto end; |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | end: |
| 1644 | ctf_field_class_destroy(class_decl); |
| 1645 | class_decl = NULL; |
| 1646 | return ret; |
| 1647 | } |
| 1648 | |
| 1649 | static |
| 1650 | int visit_field_class_alias(struct ctx *ctx, struct ctf_node *target, |
| 1651 | struct ctf_node *alias) |
| 1652 | { |
| 1653 | int ret = 0; |
| 1654 | GQuark qalias; |
| 1655 | struct ctf_node *node; |
| 1656 | GQuark qdummy_field_name; |
| 1657 | struct ctf_field_class *class_decl = NULL; |
| 1658 | |
| 1659 | /* Create target field class */ |
| 1660 | if (bt_list_empty(&target->u.field_class_alias_target.field_class_declarators)) { |
| 1661 | node = NULL; |
| 1662 | } else { |
| 1663 | node = _BT_LIST_FIRST_ENTRY( |
| 1664 | &target->u.field_class_alias_target.field_class_declarators, |
| 1665 | struct ctf_node, siblings); |
| 1666 | } |
| 1667 | |
| 1668 | ret = visit_field_class_declarator(ctx, |
| 1669 | target->u.field_class_alias_target.field_class_specifier_list, |
| 1670 | &qdummy_field_name, node, &class_decl, NULL); |
| 1671 | if (ret) { |
| 1672 | BT_ASSERT(!class_decl); |
| 1673 | _BT_COMP_LOGE_NODE(node, |
| 1674 | "Cannot visit field class declarator: ret=%d", ret); |
| 1675 | goto end; |
| 1676 | } |
| 1677 | |
| 1678 | /* Do not allow field class def and alias of untagged variants */ |
| 1679 | if (class_decl->type == CTF_FIELD_CLASS_TYPE_VARIANT) { |
| 1680 | struct ctf_field_class_variant *var_fc = (void *) class_decl; |
| 1681 | |
| 1682 | if (var_fc->tag_path.path->len == 0) { |
| 1683 | _BT_COMP_LOGE_NODE(target, |
| 1684 | "Type definition of untagged variant field class is not allowed."); |
| 1685 | ret = -EPERM; |
| 1686 | goto end; |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * The semantic validator does not check whether the target is |
| 1692 | * abstract or not (if it has an identifier). Check it here. |
| 1693 | */ |
| 1694 | if (qdummy_field_name != 0) { |
| 1695 | _BT_COMP_LOGE_NODE(target, |
| 1696 | "Expecting empty identifier: id=\"%s\"", |
| 1697 | g_quark_to_string(qdummy_field_name)); |
| 1698 | ret = -EINVAL; |
| 1699 | goto end; |
| 1700 | } |
| 1701 | |
| 1702 | /* Create alias identifier */ |
| 1703 | node = _BT_LIST_FIRST_ENTRY(&alias->u.field_class_alias_name.field_class_declarators, |
| 1704 | struct ctf_node, siblings); |
| 1705 | qalias = create_class_alias_identifier(ctx, |
| 1706 | alias->u.field_class_alias_name.field_class_specifier_list, node); |
| 1707 | ret = ctx_decl_scope_register_alias(ctx, ctx->current_scope, |
| 1708 | g_quark_to_string(qalias), class_decl); |
| 1709 | if (ret) { |
| 1710 | _BT_COMP_LOGE_NODE(node, |
| 1711 | "Cannot register class alias: name=\"%s\"", |
| 1712 | g_quark_to_string(qalias)); |
| 1713 | goto end; |
| 1714 | } |
| 1715 | |
| 1716 | end: |
| 1717 | ctf_field_class_destroy(class_decl); |
| 1718 | class_decl = NULL; |
| 1719 | return ret; |
| 1720 | } |
| 1721 | |
| 1722 | static |
| 1723 | int visit_struct_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, |
| 1724 | struct ctf_field_class_struct *struct_decl) |
| 1725 | { |
| 1726 | int ret = 0; |
| 1727 | |
| 1728 | switch (entry_node->type) { |
| 1729 | case NODE_TYPEDEF: |
| 1730 | ret = visit_field_class_def(ctx, |
| 1731 | entry_node->u.field_class_def.field_class_specifier_list, |
| 1732 | &entry_node->u.field_class_def.field_class_declarators); |
| 1733 | if (ret) { |
| 1734 | _BT_COMP_LOGE_NODE(entry_node, |
| 1735 | "Cannot add field class found in structure field class: ret=%d", |
| 1736 | ret); |
| 1737 | goto end; |
| 1738 | } |
| 1739 | break; |
| 1740 | case NODE_TYPEALIAS: |
| 1741 | ret = visit_field_class_alias(ctx, entry_node->u.field_class_alias.target, |
| 1742 | entry_node->u.field_class_alias.alias); |
| 1743 | if (ret) { |
| 1744 | _BT_COMP_LOGE_NODE(entry_node, |
| 1745 | "Cannot add field class alias found in structure field class: ret=%d", |
| 1746 | ret); |
| 1747 | goto end; |
| 1748 | } |
| 1749 | break; |
| 1750 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 1751 | /* Field */ |
| 1752 | ret = visit_struct_decl_field(ctx, struct_decl, |
| 1753 | entry_node->u.struct_or_variant_declaration. |
| 1754 | field_class_specifier_list, |
| 1755 | &entry_node->u.struct_or_variant_declaration. |
| 1756 | field_class_declarators); |
| 1757 | if (ret) { |
| 1758 | goto end; |
| 1759 | } |
| 1760 | break; |
| 1761 | default: |
| 1762 | _BT_COMP_LOGE_NODE(entry_node, |
| 1763 | "Unexpected node type: node-type=%d", entry_node->type); |
| 1764 | ret = -EINVAL; |
| 1765 | goto end; |
| 1766 | } |
| 1767 | |
| 1768 | end: |
| 1769 | return ret; |
| 1770 | } |
| 1771 | |
| 1772 | static |
| 1773 | int visit_variant_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, |
| 1774 | struct ctf_field_class_variant *variant_decl) |
| 1775 | { |
| 1776 | int ret = 0; |
| 1777 | |
| 1778 | switch (entry_node->type) { |
| 1779 | case NODE_TYPEDEF: |
| 1780 | ret = visit_field_class_def(ctx, |
| 1781 | entry_node->u.field_class_def.field_class_specifier_list, |
| 1782 | &entry_node->u.field_class_def.field_class_declarators); |
| 1783 | if (ret) { |
| 1784 | _BT_COMP_LOGE_NODE(entry_node, |
| 1785 | "Cannot add field class found in variant field class: ret=%d", |
| 1786 | ret); |
| 1787 | goto end; |
| 1788 | } |
| 1789 | break; |
| 1790 | case NODE_TYPEALIAS: |
| 1791 | ret = visit_field_class_alias(ctx, entry_node->u.field_class_alias.target, |
| 1792 | entry_node->u.field_class_alias.alias); |
| 1793 | if (ret) { |
| 1794 | _BT_COMP_LOGE_NODE(entry_node, |
| 1795 | "Cannot add field class alias found in variant field class: ret=%d", |
| 1796 | ret); |
| 1797 | goto end; |
| 1798 | } |
| 1799 | break; |
| 1800 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 1801 | /* Field */ |
| 1802 | ret = visit_variant_decl_field(ctx, variant_decl, |
| 1803 | entry_node->u.struct_or_variant_declaration. |
| 1804 | field_class_specifier_list, |
| 1805 | &entry_node->u.struct_or_variant_declaration. |
| 1806 | field_class_declarators); |
| 1807 | if (ret) { |
| 1808 | goto end; |
| 1809 | } |
| 1810 | break; |
| 1811 | default: |
| 1812 | _BT_COMP_LOGE_NODE(entry_node, |
| 1813 | "Unexpected node type: node-type=%d", |
| 1814 | entry_node->type); |
| 1815 | ret = -EINVAL; |
| 1816 | goto end; |
| 1817 | } |
| 1818 | |
| 1819 | end: |
| 1820 | return ret; |
| 1821 | } |
| 1822 | |
| 1823 | static |
| 1824 | int visit_struct_decl(struct ctx *ctx, const char *name, |
| 1825 | struct bt_list_head *decl_list, int has_body, |
| 1826 | struct bt_list_head *min_align, |
| 1827 | struct ctf_field_class_struct **struct_decl) |
| 1828 | { |
| 1829 | int ret = 0; |
| 1830 | |
| 1831 | BT_ASSERT(struct_decl); |
| 1832 | *struct_decl = NULL; |
| 1833 | |
| 1834 | /* For named struct (without body), lookup in declaration scope */ |
| 1835 | if (!has_body) { |
| 1836 | if (!name) { |
| 1837 | BT_COMP_LOGE_STR("Bodyless structure field class: missing name."); |
| 1838 | ret = -EPERM; |
| 1839 | goto error; |
| 1840 | } |
| 1841 | |
| 1842 | *struct_decl = ctx_decl_scope_lookup_struct(ctx, ctx->current_scope, |
| 1843 | name, -1, true); |
| 1844 | if (!*struct_decl) { |
| 1845 | BT_COMP_LOGE("Cannot find structure field class: name=\"struct %s\"", |
| 1846 | name); |
| 1847 | ret = -EINVAL; |
| 1848 | goto error; |
| 1849 | } |
| 1850 | } else { |
| 1851 | struct ctf_node *entry_node; |
| 1852 | uint64_t min_align_value = 0; |
| 1853 | |
| 1854 | if (name) { |
| 1855 | if (ctx_decl_scope_lookup_struct(ctx, |
| 1856 | ctx->current_scope, name, 1, false)) { |
| 1857 | BT_COMP_LOGE("Structure field class already declared in local scope: " |
| 1858 | "name=\"struct %s\"", name); |
| 1859 | ret = -EINVAL; |
| 1860 | goto error; |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | if (!bt_list_empty(min_align)) { |
| 1865 | ret = get_unary_unsigned(ctx, min_align, |
| 1866 | &min_align_value); |
| 1867 | if (ret) { |
| 1868 | BT_COMP_LOGE("Unexpected unary expression for structure field class's `align` attribute: " |
| 1869 | "ret=%d", ret); |
| 1870 | goto error; |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | *struct_decl = ctf_field_class_struct_create(); |
| 1875 | BT_ASSERT(*struct_decl); |
| 1876 | |
| 1877 | if (min_align_value != 0) { |
| 1878 | (*struct_decl)->base.alignment = min_align_value; |
| 1879 | } |
| 1880 | |
| 1881 | _TRY_PUSH_SCOPE_OR_GOTO_ERROR(); |
| 1882 | |
| 1883 | bt_list_for_each_entry(entry_node, decl_list, siblings) { |
| 1884 | ret = visit_struct_decl_entry(ctx, entry_node, |
| 1885 | *struct_decl); |
| 1886 | if (ret) { |
| 1887 | _BT_COMP_LOGE_NODE(entry_node, |
| 1888 | "Cannot visit structure field class entry: " |
| 1889 | "ret=%d", ret); |
| 1890 | ctx_pop_scope(ctx); |
| 1891 | goto error; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | ctx_pop_scope(ctx); |
| 1896 | |
| 1897 | if (name) { |
| 1898 | ret = ctx_decl_scope_register_struct(ctx, |
| 1899 | ctx->current_scope, name, *struct_decl); |
| 1900 | if (ret) { |
| 1901 | BT_COMP_LOGE("Cannot register structure field class in declaration scope: " |
| 1902 | "name=\"struct %s\", ret=%d", name, ret); |
| 1903 | goto error; |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | return 0; |
| 1909 | |
| 1910 | error: |
| 1911 | ctf_field_class_destroy((void *) *struct_decl); |
| 1912 | *struct_decl = NULL; |
| 1913 | return ret; |
| 1914 | } |
| 1915 | |
| 1916 | static |
| 1917 | int visit_variant_decl(struct ctx *ctx, const char *name, |
| 1918 | const char *tag, struct bt_list_head *decl_list, |
| 1919 | int has_body, struct ctf_field_class_variant **variant_decl) |
| 1920 | { |
| 1921 | int ret = 0; |
| 1922 | struct ctf_field_class_variant *untagged_variant_decl = NULL; |
| 1923 | |
| 1924 | BT_ASSERT(variant_decl); |
| 1925 | *variant_decl = NULL; |
| 1926 | |
| 1927 | /* For named variant (without body), lookup in declaration scope */ |
| 1928 | if (!has_body) { |
| 1929 | if (!name) { |
| 1930 | BT_COMP_LOGE_STR("Bodyless variant field class: missing name."); |
| 1931 | ret = -EPERM; |
| 1932 | goto error; |
| 1933 | } |
| 1934 | |
| 1935 | untagged_variant_decl = |
| 1936 | ctx_decl_scope_lookup_variant(ctx, ctx->current_scope, |
| 1937 | name, -1, true); |
| 1938 | if (!untagged_variant_decl) { |
| 1939 | BT_COMP_LOGE("Cannot find variant field class: name=\"variant %s\"", |
| 1940 | name); |
| 1941 | ret = -EINVAL; |
| 1942 | goto error; |
| 1943 | } |
| 1944 | } else { |
| 1945 | struct ctf_node *entry_node; |
| 1946 | |
| 1947 | if (name) { |
| 1948 | if (ctx_decl_scope_lookup_variant(ctx, |
| 1949 | ctx->current_scope, name, 1, false)) { |
| 1950 | BT_COMP_LOGE("Variant field class already declared in local scope: " |
| 1951 | "name=\"variant %s\"", name); |
| 1952 | ret = -EINVAL; |
| 1953 | goto error; |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | untagged_variant_decl = ctf_field_class_variant_create(); |
| 1958 | BT_ASSERT(untagged_variant_decl); |
| 1959 | _TRY_PUSH_SCOPE_OR_GOTO_ERROR(); |
| 1960 | |
| 1961 | bt_list_for_each_entry(entry_node, decl_list, siblings) { |
| 1962 | ret = visit_variant_decl_entry(ctx, entry_node, |
| 1963 | untagged_variant_decl); |
| 1964 | if (ret) { |
| 1965 | _BT_COMP_LOGE_NODE(entry_node, |
| 1966 | "Cannot visit variant field class entry: " |
| 1967 | "ret=%d", ret); |
| 1968 | ctx_pop_scope(ctx); |
| 1969 | goto error; |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | ctx_pop_scope(ctx); |
| 1974 | |
| 1975 | if (name) { |
| 1976 | ret = ctx_decl_scope_register_variant(ctx, |
| 1977 | ctx->current_scope, name, |
| 1978 | untagged_variant_decl); |
| 1979 | if (ret) { |
| 1980 | BT_COMP_LOGE("Cannot register variant field class in declaration scope: " |
| 1981 | "name=\"variant %s\", ret=%d", name, ret); |
| 1982 | goto error; |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * If tagged, create tagged variant and return; otherwise |
| 1989 | * return untagged variant. |
| 1990 | */ |
| 1991 | if (!tag) { |
| 1992 | *variant_decl = untagged_variant_decl; |
| 1993 | untagged_variant_decl = NULL; |
| 1994 | } else { |
| 1995 | /* |
| 1996 | * At this point, we have a fresh untagged variant; nobody |
| 1997 | * else owns it. Set its tag now. |
| 1998 | */ |
| 1999 | g_string_assign(untagged_variant_decl->tag_ref, tag); |
| 2000 | *variant_decl = untagged_variant_decl; |
| 2001 | untagged_variant_decl = NULL; |
| 2002 | } |
| 2003 | |
| 2004 | BT_ASSERT(!untagged_variant_decl); |
| 2005 | BT_ASSERT(*variant_decl); |
| 2006 | return 0; |
| 2007 | |
| 2008 | error: |
| 2009 | ctf_field_class_destroy((void *) untagged_variant_decl); |
| 2010 | untagged_variant_decl = NULL; |
| 2011 | ctf_field_class_destroy((void *) *variant_decl); |
| 2012 | *variant_decl = NULL; |
| 2013 | return ret; |
| 2014 | } |
| 2015 | |
| 2016 | struct uori { |
| 2017 | bool is_signed; |
| 2018 | union { |
| 2019 | uint64_t u; |
| 2020 | uint64_t i; |
| 2021 | } value; |
| 2022 | }; |
| 2023 | |
| 2024 | static |
| 2025 | int visit_enum_decl_entry(struct ctx *ctx, struct ctf_node *enumerator, |
| 2026 | struct ctf_field_class_enum *enum_decl, struct uori *last) |
| 2027 | { |
| 2028 | int ret = 0; |
| 2029 | int nr_vals = 0; |
| 2030 | struct ctf_node *iter; |
| 2031 | struct uori start = { |
| 2032 | .is_signed = false, |
| 2033 | .value.u = 0, |
| 2034 | }; |
| 2035 | struct uori end = { |
| 2036 | .is_signed = false, |
| 2037 | .value.u = 0, |
| 2038 | }; |
| 2039 | const char *label = enumerator->u.enumerator.id; |
| 2040 | struct bt_list_head *values = &enumerator->u.enumerator.values; |
| 2041 | |
| 2042 | bt_list_for_each_entry(iter, values, siblings) { |
| 2043 | struct uori *target; |
| 2044 | |
| 2045 | if (iter->type != NODE_UNARY_EXPRESSION) { |
| 2046 | _BT_COMP_LOGE_NODE(iter, |
| 2047 | "Wrong expression for enumeration field class label: " |
| 2048 | "node-type=%d, label=\"%s\"", iter->type, |
| 2049 | label); |
| 2050 | ret = -EINVAL; |
| 2051 | goto error; |
| 2052 | } |
| 2053 | |
| 2054 | if (nr_vals == 0) { |
| 2055 | target = &start; |
| 2056 | } else { |
| 2057 | target = &end; |
| 2058 | } |
| 2059 | |
| 2060 | switch (iter->u.unary_expression.type) { |
| 2061 | case UNARY_SIGNED_CONSTANT: |
| 2062 | target->is_signed = true; |
| 2063 | target->value.i = |
| 2064 | iter->u.unary_expression.u.signed_constant; |
| 2065 | break; |
| 2066 | case UNARY_UNSIGNED_CONSTANT: |
| 2067 | target->is_signed = false; |
| 2068 | target->value.u = |
| 2069 | iter->u.unary_expression.u.unsigned_constant; |
| 2070 | break; |
| 2071 | default: |
| 2072 | _BT_COMP_LOGE_NODE(iter, |
| 2073 | "Invalid enumeration field class entry: " |
| 2074 | "expecting constant signed or unsigned integer: " |
| 2075 | "node-type=%d, label=\"%s\"", |
| 2076 | iter->u.unary_expression.type, label); |
| 2077 | ret = -EINVAL; |
| 2078 | goto error; |
| 2079 | } |
| 2080 | |
| 2081 | if (nr_vals > 1) { |
| 2082 | _BT_COMP_LOGE_NODE(iter, |
| 2083 | "Invalid enumeration field class entry: label=\"%s\"", |
| 2084 | label); |
| 2085 | ret = -EINVAL; |
| 2086 | goto error; |
| 2087 | } |
| 2088 | |
| 2089 | nr_vals++; |
| 2090 | } |
| 2091 | |
| 2092 | if (nr_vals == 0) { |
| 2093 | start = *last; |
| 2094 | } |
| 2095 | |
| 2096 | if (nr_vals <= 1) { |
| 2097 | end = start; |
| 2098 | } |
| 2099 | |
| 2100 | if (end.is_signed) { |
| 2101 | last->value.i = end.value.i + 1; |
| 2102 | } else { |
| 2103 | last->value.u = end.value.u + 1; |
| 2104 | } |
| 2105 | |
| 2106 | ctf_field_class_enum_map_range(enum_decl, label, |
| 2107 | start.value.u, end.value.u); |
| 2108 | return 0; |
| 2109 | |
| 2110 | error: |
| 2111 | return ret; |
| 2112 | } |
| 2113 | |
| 2114 | static |
| 2115 | int visit_enum_decl(struct ctx *ctx, const char *name, |
| 2116 | struct ctf_node *container_cls, |
| 2117 | struct bt_list_head *enumerator_list, |
| 2118 | int has_body, struct ctf_field_class_enum **enum_decl) |
| 2119 | { |
| 2120 | int ret = 0; |
| 2121 | GQuark qdummy_id; |
| 2122 | struct ctf_field_class_int *integer_decl = NULL; |
| 2123 | |
| 2124 | BT_ASSERT(enum_decl); |
| 2125 | *enum_decl = NULL; |
| 2126 | |
| 2127 | /* For named enum (without body), lookup in declaration scope */ |
| 2128 | if (!has_body) { |
| 2129 | if (!name) { |
| 2130 | BT_COMP_LOGE_STR("Bodyless enumeration field class: missing name."); |
| 2131 | ret = -EPERM; |
| 2132 | goto error; |
| 2133 | } |
| 2134 | |
| 2135 | *enum_decl = ctx_decl_scope_lookup_enum(ctx, ctx->current_scope, |
| 2136 | name, -1, true); |
| 2137 | if (!*enum_decl) { |
| 2138 | BT_COMP_LOGE("Cannot find enumeration field class: " |
| 2139 | "name=\"enum %s\"", name); |
| 2140 | ret = -EINVAL; |
| 2141 | goto error; |
| 2142 | } |
| 2143 | } else { |
| 2144 | struct ctf_node *iter; |
| 2145 | struct uori last_value = { |
| 2146 | .is_signed = false, |
| 2147 | .value.u = 0, |
| 2148 | }; |
| 2149 | |
| 2150 | if (name) { |
| 2151 | if (ctx_decl_scope_lookup_enum(ctx, ctx->current_scope, |
| 2152 | name, 1, false)) { |
| 2153 | BT_COMP_LOGE("Enumeration field class already declared in local scope: " |
| 2154 | "name=\"enum %s\"", name); |
| 2155 | ret = -EINVAL; |
| 2156 | goto error; |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | if (!container_cls) { |
| 2161 | integer_decl = (void *) ctx_decl_scope_lookup_alias(ctx, |
| 2162 | ctx->current_scope, "int", -1, true); |
| 2163 | if (!integer_decl) { |
| 2164 | BT_COMP_LOGE_STR("Cannot find implicit `int` field class alias for enumeration field class."); |
| 2165 | ret = -EINVAL; |
| 2166 | goto error; |
| 2167 | } |
| 2168 | } else { |
| 2169 | ret = visit_field_class_declarator(ctx, container_cls, |
| 2170 | &qdummy_id, NULL, (void *) &integer_decl, |
| 2171 | NULL); |
| 2172 | if (ret) { |
| 2173 | BT_ASSERT(!integer_decl); |
| 2174 | ret = -EINVAL; |
| 2175 | goto error; |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | BT_ASSERT(integer_decl); |
| 2180 | |
| 2181 | if (integer_decl->base.base.type != CTF_FIELD_CLASS_TYPE_INT) { |
| 2182 | BT_COMP_LOGE("Container field class for enumeration field class is not an integer field class: " |
| 2183 | "fc-type=%d", integer_decl->base.base.type); |
| 2184 | ret = -EINVAL; |
| 2185 | goto error; |
| 2186 | } |
| 2187 | |
| 2188 | *enum_decl = ctf_field_class_enum_create(); |
| 2189 | BT_ASSERT(*enum_decl); |
| 2190 | (*enum_decl)->base.base.base.alignment = |
| 2191 | integer_decl->base.base.alignment; |
| 2192 | ctf_field_class_int_copy_content((void *) *enum_decl, |
| 2193 | (void *) integer_decl); |
| 2194 | last_value.is_signed = (*enum_decl)->base.is_signed; |
| 2195 | |
| 2196 | bt_list_for_each_entry(iter, enumerator_list, siblings) { |
| 2197 | ret = visit_enum_decl_entry(ctx, iter, *enum_decl, |
| 2198 | &last_value); |
| 2199 | if (ret) { |
| 2200 | _BT_COMP_LOGE_NODE(iter, |
| 2201 | "Cannot visit enumeration field class entry: " |
| 2202 | "ret=%d", ret); |
| 2203 | goto error; |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | if (name) { |
| 2208 | ret = ctx_decl_scope_register_enum(ctx, |
| 2209 | ctx->current_scope, name, *enum_decl); |
| 2210 | if (ret) { |
| 2211 | BT_COMP_LOGE("Cannot register enumeration field class in declaration scope: " |
| 2212 | "ret=%d", ret); |
| 2213 | goto error; |
| 2214 | } |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | goto end; |
| 2219 | |
| 2220 | error: |
| 2221 | ctf_field_class_destroy((void *) *enum_decl); |
| 2222 | *enum_decl = NULL; |
| 2223 | |
| 2224 | end: |
| 2225 | ctf_field_class_destroy((void *) integer_decl); |
| 2226 | integer_decl = NULL; |
| 2227 | return ret; |
| 2228 | } |
| 2229 | |
| 2230 | static |
| 2231 | int visit_field_class_specifier(struct ctx *ctx, |
| 2232 | struct ctf_node *cls_specifier_list, |
| 2233 | struct ctf_field_class **decl) |
| 2234 | { |
| 2235 | int ret = 0; |
| 2236 | GString *str = NULL; |
| 2237 | |
| 2238 | *decl = NULL; |
| 2239 | str = g_string_new(""); |
| 2240 | ret = get_class_specifier_list_name(ctx, cls_specifier_list, str); |
| 2241 | if (ret) { |
| 2242 | _BT_COMP_LOGE_NODE(cls_specifier_list, |
| 2243 | "Cannot get field class specifier list's name: ret=%d", ret); |
| 2244 | goto error; |
| 2245 | } |
| 2246 | |
| 2247 | *decl = ctx_decl_scope_lookup_alias(ctx, ctx->current_scope, str->str, |
| 2248 | -1, true); |
| 2249 | if (!*decl) { |
| 2250 | _BT_COMP_LOGE_NODE(cls_specifier_list, |
| 2251 | "Cannot find field class alias: name=\"%s\"", str->str); |
| 2252 | ret = -EINVAL; |
| 2253 | goto error; |
| 2254 | } |
| 2255 | |
| 2256 | goto end; |
| 2257 | |
| 2258 | error: |
| 2259 | ctf_field_class_destroy(*decl); |
| 2260 | *decl = NULL; |
| 2261 | |
| 2262 | end: |
| 2263 | if (str) { |
| 2264 | g_string_free(str, TRUE); |
| 2265 | } |
| 2266 | |
| 2267 | return ret; |
| 2268 | } |
| 2269 | |
| 2270 | static |
| 2271 | int visit_integer_decl(struct ctx *ctx, |
| 2272 | struct bt_list_head *expressions, |
| 2273 | struct ctf_field_class_int **integer_decl) |
| 2274 | { |
| 2275 | int set = 0; |
| 2276 | int ret = 0; |
| 2277 | int signedness = 0; |
| 2278 | struct ctf_node *expression; |
| 2279 | uint64_t alignment = 0, size = 0; |
| 2280 | struct ctf_clock_class *mapped_clock_class = NULL; |
| 2281 | enum ctf_encoding encoding = CTF_ENCODING_NONE; |
| 2282 | bt_field_class_integer_preferred_display_base base = |
| 2283 | BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; |
| 2284 | enum ctf_byte_order byte_order = ctx->ctf_tc->default_byte_order; |
| 2285 | |
| 2286 | *integer_decl = NULL; |
| 2287 | |
| 2288 | bt_list_for_each_entry(expression, expressions, siblings) { |
| 2289 | struct ctf_node *left, *right; |
| 2290 | |
| 2291 | left = _BT_LIST_FIRST_ENTRY(&expression->u.ctf_expression.left, |
| 2292 | struct ctf_node, siblings); |
| 2293 | right = _BT_LIST_FIRST_ENTRY( |
| 2294 | &expression->u.ctf_expression.right, struct ctf_node, |
| 2295 | siblings); |
| 2296 | |
| 2297 | if (left->u.unary_expression.type != UNARY_STRING) { |
| 2298 | _BT_COMP_LOGE_NODE(left, |
| 2299 | "Unexpected unary expression type: type=%d", |
| 2300 | left->u.unary_expression.type); |
| 2301 | ret = -EINVAL; |
| 2302 | goto error; |
| 2303 | } |
| 2304 | |
| 2305 | if (strcmp(left->u.unary_expression.u.string, "signed") == 0) { |
| 2306 | if (_IS_SET(&set, _INTEGER_SIGNED_SET)) { |
| 2307 | _BT_COMP_LOGE_DUP_ATTR(left, "signed", |
| 2308 | "integer field class"); |
| 2309 | ret = -EPERM; |
| 2310 | goto error; |
| 2311 | } |
| 2312 | |
| 2313 | signedness = get_boolean(ctx, right); |
| 2314 | if (signedness < 0) { |
| 2315 | _BT_COMP_LOGE_NODE(right, |
| 2316 | "Invalid boolean value for integer field class's `signed` attribute: " |
| 2317 | "ret=%d", ret); |
| 2318 | ret = -EINVAL; |
| 2319 | goto error; |
| 2320 | } |
| 2321 | |
| 2322 | _SET(&set, _INTEGER_SIGNED_SET); |
| 2323 | } else if (strcmp(left->u.unary_expression.u.string, "byte_order") == 0) { |
| 2324 | if (_IS_SET(&set, _INTEGER_BYTE_ORDER_SET)) { |
| 2325 | _BT_COMP_LOGE_DUP_ATTR(left, "byte_order", |
| 2326 | "integer field class"); |
| 2327 | ret = -EPERM; |
| 2328 | goto error; |
| 2329 | } |
| 2330 | |
| 2331 | byte_order = get_real_byte_order(ctx, right); |
| 2332 | if (byte_order == CTF_BYTE_ORDER_UNKNOWN) { |
| 2333 | _BT_COMP_LOGE_NODE(right, |
| 2334 | "Invalid `byte_order` attribute in integer field class: " |
| 2335 | "ret=%d", ret); |
| 2336 | ret = -EINVAL; |
| 2337 | goto error; |
| 2338 | } |
| 2339 | |
| 2340 | _SET(&set, _INTEGER_BYTE_ORDER_SET); |
| 2341 | } else if (strcmp(left->u.unary_expression.u.string, "size") == 0) { |
| 2342 | if (_IS_SET(&set, _INTEGER_SIZE_SET)) { |
| 2343 | _BT_COMP_LOGE_DUP_ATTR(left, "size", |
| 2344 | "integer field class"); |
| 2345 | ret = -EPERM; |
| 2346 | goto error; |
| 2347 | } |
| 2348 | |
| 2349 | if (right->u.unary_expression.type != |
| 2350 | UNARY_UNSIGNED_CONSTANT) { |
| 2351 | _BT_COMP_LOGE_NODE(right, |
| 2352 | "Invalid `size` attribute in integer field class: " |
| 2353 | "expecting unsigned constant integer: " |
| 2354 | "node-type=%d", |
| 2355 | right->u.unary_expression.type); |
| 2356 | ret = -EINVAL; |
| 2357 | goto error; |
| 2358 | } |
| 2359 | |
| 2360 | size = right->u.unary_expression.u.unsigned_constant; |
| 2361 | if (size == 0) { |
| 2362 | _BT_COMP_LOGE_NODE(right, |
| 2363 | "Invalid `size` attribute in integer field class: " |
| 2364 | "expecting positive constant integer: " |
| 2365 | "size=%" PRIu64, size); |
| 2366 | ret = -EINVAL; |
| 2367 | goto error; |
| 2368 | } else if (size > 64) { |
| 2369 | _BT_COMP_LOGE_NODE(right, |
| 2370 | "Invalid `size` attribute in integer field class: " |
| 2371 | "integer fields over 64 bits are not supported as of this version: " |
| 2372 | "size=%" PRIu64, size); |
| 2373 | ret = -EINVAL; |
| 2374 | goto error; |
| 2375 | } |
| 2376 | |
| 2377 | _SET(&set, _INTEGER_SIZE_SET); |
| 2378 | } else if (strcmp(left->u.unary_expression.u.string, "align") == 0) { |
| 2379 | if (_IS_SET(&set, _INTEGER_ALIGN_SET)) { |
| 2380 | _BT_COMP_LOGE_DUP_ATTR(left, "align", |
| 2381 | "integer field class"); |
| 2382 | ret = -EPERM; |
| 2383 | goto error; |
| 2384 | } |
| 2385 | |
| 2386 | if (right->u.unary_expression.type != |
| 2387 | UNARY_UNSIGNED_CONSTANT) { |
| 2388 | _BT_COMP_LOGE_NODE(right, |
| 2389 | "Invalid `align` attribute in integer field class: " |
| 2390 | "expecting unsigned constant integer: " |
| 2391 | "node-type=%d", |
| 2392 | right->u.unary_expression.type); |
| 2393 | ret = -EINVAL; |
| 2394 | goto error; |
| 2395 | } |
| 2396 | |
| 2397 | alignment = |
| 2398 | right->u.unary_expression.u.unsigned_constant; |
| 2399 | if (!is_align_valid(alignment)) { |
| 2400 | _BT_COMP_LOGE_NODE(right, |
| 2401 | "Invalid `align` attribute in integer field class: " |
| 2402 | "expecting power of two: " |
| 2403 | "align=%" PRIu64, alignment); |
| 2404 | ret = -EINVAL; |
| 2405 | goto error; |
| 2406 | } |
| 2407 | |
| 2408 | _SET(&set, _INTEGER_ALIGN_SET); |
| 2409 | } else if (strcmp(left->u.unary_expression.u.string, "base") == 0) { |
| 2410 | if (_IS_SET(&set, _INTEGER_BASE_SET)) { |
| 2411 | _BT_COMP_LOGE_DUP_ATTR(left, "base", |
| 2412 | "integer field class"); |
| 2413 | ret = -EPERM; |
| 2414 | goto error; |
| 2415 | } |
| 2416 | |
| 2417 | switch (right->u.unary_expression.type) { |
| 2418 | case UNARY_UNSIGNED_CONSTANT: |
| 2419 | { |
| 2420 | uint64_t constant = right->u.unary_expression. |
| 2421 | u.unsigned_constant; |
| 2422 | |
| 2423 | switch (constant) { |
| 2424 | case 2: |
| 2425 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY; |
| 2426 | break; |
| 2427 | case 8: |
| 2428 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL; |
| 2429 | break; |
| 2430 | case 10: |
| 2431 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; |
| 2432 | break; |
| 2433 | case 16: |
| 2434 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL; |
| 2435 | break; |
| 2436 | default: |
| 2437 | _BT_COMP_LOGE_NODE(right, |
| 2438 | "Invalid `base` attribute in integer field class: " |
| 2439 | "base=%" PRIu64, |
| 2440 | right->u.unary_expression.u.unsigned_constant); |
| 2441 | ret = -EINVAL; |
| 2442 | goto error; |
| 2443 | } |
| 2444 | break; |
| 2445 | } |
| 2446 | case UNARY_STRING: |
| 2447 | { |
| 2448 | char *s_right = concatenate_unary_strings( |
| 2449 | &expression->u.ctf_expression.right); |
| 2450 | if (!s_right) { |
| 2451 | _BT_COMP_LOGE_NODE(right, |
| 2452 | "Unexpected unary expression for integer field class's `base` attribute."); |
| 2453 | ret = -EINVAL; |
| 2454 | goto error; |
| 2455 | } |
| 2456 | |
| 2457 | if (strcmp(s_right, "decimal") == 0 || |
| 2458 | strcmp(s_right, "dec") == 0 || |
| 2459 | strcmp(s_right, "d") == 0 || |
| 2460 | strcmp(s_right, "i") == 0 || |
| 2461 | strcmp(s_right, "u") == 0) { |
| 2462 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; |
| 2463 | } else if (strcmp(s_right, "hexadecimal") == 0 || |
| 2464 | strcmp(s_right, "hex") == 0 || |
| 2465 | strcmp(s_right, "x") == 0 || |
| 2466 | strcmp(s_right, "X") == 0 || |
| 2467 | strcmp(s_right, "p") == 0) { |
| 2468 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL; |
| 2469 | } else if (strcmp(s_right, "octal") == 0 || |
| 2470 | strcmp(s_right, "oct") == 0 || |
| 2471 | strcmp(s_right, "o") == 0) { |
| 2472 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL; |
| 2473 | } else if (strcmp(s_right, "binary") == 0 || |
| 2474 | strcmp(s_right, "b") == 0) { |
| 2475 | base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY; |
| 2476 | } else { |
| 2477 | _BT_COMP_LOGE_NODE(right, |
| 2478 | "Unexpected unary expression for integer field class's `base` attribute: " |
| 2479 | "base=\"%s\"", s_right); |
| 2480 | g_free(s_right); |
| 2481 | ret = -EINVAL; |
| 2482 | goto error; |
| 2483 | } |
| 2484 | |
| 2485 | g_free(s_right); |
| 2486 | break; |
| 2487 | } |
| 2488 | default: |
| 2489 | _BT_COMP_LOGE_NODE(right, |
| 2490 | "Invalid `base` attribute in integer field class: " |
| 2491 | "expecting unsigned constant integer or unary string."); |
| 2492 | ret = -EINVAL; |
| 2493 | goto error; |
| 2494 | } |
| 2495 | |
| 2496 | _SET(&set, _INTEGER_BASE_SET); |
| 2497 | } else if (strcmp(left->u.unary_expression.u.string, "encoding") == 0) { |
| 2498 | char *s_right; |
| 2499 | |
| 2500 | if (_IS_SET(&set, _INTEGER_ENCODING_SET)) { |
| 2501 | _BT_COMP_LOGE_DUP_ATTR(left, "encoding", |
| 2502 | "integer field class"); |
| 2503 | ret = -EPERM; |
| 2504 | goto error; |
| 2505 | } |
| 2506 | |
| 2507 | if (right->u.unary_expression.type != UNARY_STRING) { |
| 2508 | _BT_COMP_LOGE_NODE(right, |
| 2509 | "Invalid `encoding` attribute in integer field class: " |
| 2510 | "expecting unary string."); |
| 2511 | ret = -EINVAL; |
| 2512 | goto error; |
| 2513 | } |
| 2514 | |
| 2515 | s_right = concatenate_unary_strings( |
| 2516 | &expression->u.ctf_expression.right); |
| 2517 | if (!s_right) { |
| 2518 | _BT_COMP_LOGE_NODE(right, |
| 2519 | "Unexpected unary expression for integer field class's `encoding` attribute."); |
| 2520 | ret = -EINVAL; |
| 2521 | goto error; |
| 2522 | } |
| 2523 | |
| 2524 | if (strcmp(s_right, "UTF8") == 0 || |
| 2525 | strcmp(s_right, "utf8") == 0 || |
| 2526 | strcmp(s_right, "utf-8") == 0 || |
| 2527 | strcmp(s_right, "UTF-8") == 0 || |
| 2528 | strcmp(s_right, "ASCII") == 0 || |
| 2529 | strcmp(s_right, "ascii") == 0) { |
| 2530 | encoding = CTF_ENCODING_UTF8; |
| 2531 | } else if (strcmp(s_right, "none") == 0) { |
| 2532 | encoding = CTF_ENCODING_NONE; |
| 2533 | } else { |
| 2534 | _BT_COMP_LOGE_NODE(right, |
| 2535 | "Invalid `encoding` attribute in integer field class: " |
| 2536 | "unknown encoding: encoding=\"%s\"", |
| 2537 | s_right); |
| 2538 | g_free(s_right); |
| 2539 | ret = -EINVAL; |
| 2540 | goto error; |
| 2541 | } |
| 2542 | |
| 2543 | g_free(s_right); |
| 2544 | _SET(&set, _INTEGER_ENCODING_SET); |
| 2545 | } else if (strcmp(left->u.unary_expression.u.string, "map") == 0) { |
| 2546 | const char *clock_name; |
| 2547 | |
| 2548 | if (_IS_SET(&set, _INTEGER_MAP_SET)) { |
| 2549 | _BT_COMP_LOGE_DUP_ATTR(left, "map", |
| 2550 | "integer field class"); |
| 2551 | ret = -EPERM; |
| 2552 | goto error; |
| 2553 | } |
| 2554 | |
| 2555 | if (right->u.unary_expression.type != UNARY_STRING) { |
| 2556 | _BT_COMP_LOGE_NODE(right, |
| 2557 | "Invalid `map` attribute in integer field class: " |
| 2558 | "expecting unary string."); |
| 2559 | ret = -EINVAL; |
| 2560 | goto error; |
| 2561 | } |
| 2562 | |
| 2563 | clock_name = |
| 2564 | get_map_clock_name_value( |
| 2565 | &expression->u.ctf_expression.right); |
| 2566 | if (!clock_name) { |
| 2567 | char *s_right = concatenate_unary_strings( |
| 2568 | &expression->u.ctf_expression.right); |
| 2569 | |
| 2570 | if (!s_right) { |
| 2571 | _BT_COMP_LOGE_NODE(right, |
| 2572 | "Unexpected unary expression for integer field class's `map` attribute."); |
| 2573 | ret = -EINVAL; |
| 2574 | goto error; |
| 2575 | } |
| 2576 | |
| 2577 | _BT_COMP_LOGE_NODE(right, |
| 2578 | "Invalid `map` attribute in integer field class: " |
| 2579 | "cannot find clock class at this point: name=\"%s\"", |
| 2580 | s_right); |
| 2581 | _SET(&set, _INTEGER_MAP_SET); |
| 2582 | g_free(s_right); |
| 2583 | continue; |
| 2584 | } |
| 2585 | |
| 2586 | mapped_clock_class = |
| 2587 | ctf_trace_class_borrow_clock_class_by_name( |
| 2588 | ctx->ctf_tc, clock_name); |
| 2589 | if (!mapped_clock_class) { |
| 2590 | _BT_COMP_LOGE_NODE(right, |
| 2591 | "Invalid `map` attribute in integer field class: " |
| 2592 | "cannot find clock class at this point: name=\"%s\"", |
| 2593 | clock_name); |
| 2594 | ret = -EINVAL; |
| 2595 | goto error; |
| 2596 | } |
| 2597 | |
| 2598 | _SET(&set, _INTEGER_MAP_SET); |
| 2599 | } else { |
| 2600 | _BT_COMP_LOGW_NODE(left, |
| 2601 | "Unknown attribute in integer field class: " |
| 2602 | "attr-name=\"%s\"", |
| 2603 | left->u.unary_expression.u.string); |
| 2604 | } |
| 2605 | } |
| 2606 | |
| 2607 | if (!_IS_SET(&set, _INTEGER_SIZE_SET)) { |
| 2608 | BT_COMP_LOGE_STR("Missing `size` attribute in integer field class."); |
| 2609 | ret = -EPERM; |
| 2610 | goto error; |
| 2611 | } |
| 2612 | |
| 2613 | if (!_IS_SET(&set, _INTEGER_ALIGN_SET)) { |
| 2614 | if (size % CHAR_BIT) { |
| 2615 | /* Bit-packed alignment */ |
| 2616 | alignment = 1; |
| 2617 | } else { |
| 2618 | /* Byte-packed alignment */ |
| 2619 | alignment = CHAR_BIT; |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | *integer_decl = ctf_field_class_int_create(); |
| 2624 | BT_ASSERT(*integer_decl); |
| 2625 | (*integer_decl)->base.base.alignment = alignment; |
| 2626 | (*integer_decl)->base.byte_order = byte_order; |
| 2627 | (*integer_decl)->base.size = size; |
| 2628 | (*integer_decl)->is_signed = (signedness > 0); |
| 2629 | (*integer_decl)->disp_base = base; |
| 2630 | (*integer_decl)->encoding = encoding; |
| 2631 | (*integer_decl)->mapped_clock_class = mapped_clock_class; |
| 2632 | return 0; |
| 2633 | |
| 2634 | error: |
| 2635 | ctf_field_class_destroy((void *) *integer_decl); |
| 2636 | *integer_decl = NULL; |
| 2637 | return ret; |
| 2638 | } |
| 2639 | |
| 2640 | static |
| 2641 | int visit_floating_point_number_decl(struct ctx *ctx, |
| 2642 | struct bt_list_head *expressions, |
| 2643 | struct ctf_field_class_float **float_decl) |
| 2644 | { |
| 2645 | int set = 0; |
| 2646 | int ret = 0; |
| 2647 | struct ctf_node *expression; |
| 2648 | uint64_t alignment = 1, exp_dig = 0, mant_dig = 0; |
| 2649 | enum ctf_byte_order byte_order = ctx->ctf_tc->default_byte_order; |
| 2650 | |
| 2651 | *float_decl = NULL; |
| 2652 | |
| 2653 | bt_list_for_each_entry(expression, expressions, siblings) { |
| 2654 | struct ctf_node *left, *right; |
| 2655 | |
| 2656 | left = _BT_LIST_FIRST_ENTRY(&expression->u.ctf_expression.left, |
| 2657 | struct ctf_node, siblings); |
| 2658 | right = _BT_LIST_FIRST_ENTRY( |
| 2659 | &expression->u.ctf_expression.right, struct ctf_node, |
| 2660 | siblings); |
| 2661 | |
| 2662 | if (left->u.unary_expression.type != UNARY_STRING) { |
| 2663 | _BT_COMP_LOGE_NODE(left, |
| 2664 | "Unexpected unary expression type: type=%d", |
| 2665 | left->u.unary_expression.type); |
| 2666 | ret = -EINVAL; |
| 2667 | goto error; |
| 2668 | } |
| 2669 | |
| 2670 | if (strcmp(left->u.unary_expression.u.string, "byte_order") == 0) { |
| 2671 | if (_IS_SET(&set, _FLOAT_BYTE_ORDER_SET)) { |
| 2672 | _BT_COMP_LOGE_DUP_ATTR(left, "byte_order", |
| 2673 | "floating point number field class"); |
| 2674 | ret = -EPERM; |
| 2675 | goto error; |
| 2676 | } |
| 2677 | |
| 2678 | byte_order = get_real_byte_order(ctx, right); |
| 2679 | if (byte_order == CTF_BYTE_ORDER_UNKNOWN) { |
| 2680 | _BT_COMP_LOGE_NODE(right, |
| 2681 | "Invalid `byte_order` attribute in floating point number field class: " |
| 2682 | "ret=%d", ret); |
| 2683 | ret = -EINVAL; |
| 2684 | goto error; |
| 2685 | } |
| 2686 | |
| 2687 | _SET(&set, _FLOAT_BYTE_ORDER_SET); |
| 2688 | } else if (strcmp(left->u.unary_expression.u.string, "exp_dig") == 0) { |
| 2689 | if (_IS_SET(&set, _FLOAT_EXP_DIG_SET)) { |
| 2690 | _BT_COMP_LOGE_DUP_ATTR(left, "exp_dig", |
| 2691 | "floating point number field class"); |
| 2692 | ret = -EPERM; |
| 2693 | goto error; |
| 2694 | } |
| 2695 | |
| 2696 | if (right->u.unary_expression.type != |
| 2697 | UNARY_UNSIGNED_CONSTANT) { |
| 2698 | _BT_COMP_LOGE_NODE(right, |
| 2699 | "Invalid `exp_dig` attribute in floating point number field class: " |
| 2700 | "expecting unsigned constant integer: " |
| 2701 | "node-type=%d", |
| 2702 | right->u.unary_expression.type); |
| 2703 | ret = -EINVAL; |
| 2704 | goto error; |
| 2705 | } |
| 2706 | |
| 2707 | exp_dig = right->u.unary_expression.u.unsigned_constant; |
| 2708 | _SET(&set, _FLOAT_EXP_DIG_SET); |
| 2709 | } else if (strcmp(left->u.unary_expression.u.string, "mant_dig") == 0) { |
| 2710 | if (_IS_SET(&set, _FLOAT_MANT_DIG_SET)) { |
| 2711 | _BT_COMP_LOGE_DUP_ATTR(left, "mant_dig", |
| 2712 | "floating point number field class"); |
| 2713 | ret = -EPERM; |
| 2714 | goto error; |
| 2715 | } |
| 2716 | |
| 2717 | if (right->u.unary_expression.type != |
| 2718 | UNARY_UNSIGNED_CONSTANT) { |
| 2719 | _BT_COMP_LOGE_NODE(right, |
| 2720 | "Invalid `mant_dig` attribute in floating point number field class: " |
| 2721 | "expecting unsigned constant integer: " |
| 2722 | "node-type=%d", |
| 2723 | right->u.unary_expression.type); |
| 2724 | ret = -EINVAL; |
| 2725 | goto error; |
| 2726 | } |
| 2727 | |
| 2728 | mant_dig = right->u.unary_expression.u. |
| 2729 | unsigned_constant; |
| 2730 | _SET(&set, _FLOAT_MANT_DIG_SET); |
| 2731 | } else if (strcmp(left->u.unary_expression.u.string, "align") == 0) { |
| 2732 | if (_IS_SET(&set, _FLOAT_ALIGN_SET)) { |
| 2733 | _BT_COMP_LOGE_DUP_ATTR(left, "align", |
| 2734 | "floating point number field class"); |
| 2735 | ret = -EPERM; |
| 2736 | goto error; |
| 2737 | } |
| 2738 | |
| 2739 | if (right->u.unary_expression.type != |
| 2740 | UNARY_UNSIGNED_CONSTANT) { |
| 2741 | _BT_COMP_LOGE_NODE(right, |
| 2742 | "Invalid `align` attribute in floating point number field class: " |
| 2743 | "expecting unsigned constant integer: " |
| 2744 | "node-type=%d", |
| 2745 | right->u.unary_expression.type); |
| 2746 | ret = -EINVAL; |
| 2747 | goto error; |
| 2748 | } |
| 2749 | |
| 2750 | alignment = right->u.unary_expression.u. |
| 2751 | unsigned_constant; |
| 2752 | |
| 2753 | if (!is_align_valid(alignment)) { |
| 2754 | _BT_COMP_LOGE_NODE(right, |
| 2755 | "Invalid `align` attribute in floating point number field class: " |
| 2756 | "expecting power of two: " |
| 2757 | "align=%" PRIu64, alignment); |
| 2758 | ret = -EINVAL; |
| 2759 | goto error; |
| 2760 | } |
| 2761 | |
| 2762 | _SET(&set, _FLOAT_ALIGN_SET); |
| 2763 | } else { |
| 2764 | _BT_COMP_LOGW_NODE(left, |
| 2765 | "Unknown attribute in floating point number field class: " |
| 2766 | "attr-name=\"%s\"", |
| 2767 | left->u.unary_expression.u.string); |
| 2768 | } |
| 2769 | } |
| 2770 | |
| 2771 | if (!_IS_SET(&set, _FLOAT_MANT_DIG_SET)) { |
| 2772 | BT_COMP_LOGE_STR("Missing `mant_dig` attribute in floating point number field class."); |
| 2773 | ret = -EPERM; |
| 2774 | goto error; |
| 2775 | } |
| 2776 | |
| 2777 | if (!_IS_SET(&set, _FLOAT_EXP_DIG_SET)) { |
| 2778 | BT_COMP_LOGE_STR("Missing `exp_dig` attribute in floating point number field class."); |
| 2779 | ret = -EPERM; |
| 2780 | goto error; |
| 2781 | } |
| 2782 | |
| 2783 | if (mant_dig != 24 && mant_dig != 53) { |
| 2784 | BT_COMP_LOGE_STR("`mant_dig` attribute: expecting 24 or 53."); |
| 2785 | ret = -EPERM; |
| 2786 | goto error; |
| 2787 | } |
| 2788 | |
| 2789 | if (mant_dig == 24 && exp_dig != 8) { |
| 2790 | BT_COMP_LOGE_STR("`exp_dig` attribute: expecting 8 because `mant_dig` is 24."); |
| 2791 | ret = -EPERM; |
| 2792 | goto error; |
| 2793 | } |
| 2794 | |
| 2795 | if (mant_dig == 53 && exp_dig != 11) { |
| 2796 | BT_COMP_LOGE_STR("`exp_dig` attribute: expecting 11 because `mant_dig` is 53."); |
| 2797 | ret = -EPERM; |
| 2798 | goto error; |
| 2799 | } |
| 2800 | |
| 2801 | if (!_IS_SET(&set, _INTEGER_ALIGN_SET)) { |
| 2802 | if ((mant_dig + exp_dig) % CHAR_BIT) { |
| 2803 | /* Bit-packed alignment */ |
| 2804 | alignment = 1; |
| 2805 | } else { |
| 2806 | /* Byte-packed alignment */ |
| 2807 | alignment = CHAR_BIT; |
| 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | *float_decl = ctf_field_class_float_create(); |
| 2812 | BT_ASSERT(*float_decl); |
| 2813 | (*float_decl)->base.base.alignment = alignment; |
| 2814 | (*float_decl)->base.byte_order = byte_order; |
| 2815 | (*float_decl)->base.size = mant_dig + exp_dig; |
| 2816 | return 0; |
| 2817 | |
| 2818 | error: |
| 2819 | ctf_field_class_destroy((void *) *float_decl); |
| 2820 | *float_decl = NULL; |
| 2821 | return ret; |
| 2822 | } |
| 2823 | |
| 2824 | static |
| 2825 | int visit_string_decl(struct ctx *ctx, |
| 2826 | struct bt_list_head *expressions, |
| 2827 | struct ctf_field_class_string **string_decl) |
| 2828 | { |
| 2829 | int set = 0; |
| 2830 | int ret = 0; |
| 2831 | struct ctf_node *expression; |
| 2832 | enum ctf_encoding encoding = CTF_ENCODING_UTF8; |
| 2833 | |
| 2834 | *string_decl = NULL; |
| 2835 | |
| 2836 | bt_list_for_each_entry(expression, expressions, siblings) { |
| 2837 | struct ctf_node *left, *right; |
| 2838 | |
| 2839 | left = _BT_LIST_FIRST_ENTRY(&expression->u.ctf_expression.left, |
| 2840 | struct ctf_node, siblings); |
| 2841 | right = _BT_LIST_FIRST_ENTRY( |
| 2842 | &expression->u.ctf_expression.right, struct ctf_node, |
| 2843 | siblings); |
| 2844 | |
| 2845 | if (left->u.unary_expression.type != UNARY_STRING) { |
| 2846 | _BT_COMP_LOGE_NODE(left, |
| 2847 | "Unexpected unary expression type: type=%d", |
| 2848 | left->u.unary_expression.type); |
| 2849 | ret = -EINVAL; |
| 2850 | goto error; |
| 2851 | } |
| 2852 | |
| 2853 | if (strcmp(left->u.unary_expression.u.string, "encoding") == 0) { |
| 2854 | char *s_right; |
| 2855 | |
| 2856 | if (_IS_SET(&set, _STRING_ENCODING_SET)) { |
| 2857 | _BT_COMP_LOGE_DUP_ATTR(left, "encoding", |
| 2858 | "string field class"); |
| 2859 | ret = -EPERM; |
| 2860 | goto error; |
| 2861 | } |
| 2862 | |
| 2863 | if (right->u.unary_expression.type != UNARY_STRING) { |
| 2864 | _BT_COMP_LOGE_NODE(right, |
| 2865 | "Invalid `encoding` attribute in string field class: " |
| 2866 | "expecting unary string."); |
| 2867 | ret = -EINVAL; |
| 2868 | goto error; |
| 2869 | } |
| 2870 | |
| 2871 | s_right = concatenate_unary_strings( |
| 2872 | &expression->u.ctf_expression.right); |
| 2873 | if (!s_right) { |
| 2874 | _BT_COMP_LOGE_NODE(right, |
| 2875 | "Unexpected unary expression for string field class's `encoding` attribute."); |
| 2876 | ret = -EINVAL; |
| 2877 | goto error; |
| 2878 | } |
| 2879 | |
| 2880 | if (strcmp(s_right, "UTF8") == 0 || |
| 2881 | strcmp(s_right, "utf8") == 0 || |
| 2882 | strcmp(s_right, "utf-8") == 0 || |
| 2883 | strcmp(s_right, "UTF-8") == 0 || |
| 2884 | strcmp(s_right, "ASCII") == 0 || |
| 2885 | strcmp(s_right, "ascii") == 0) { |
| 2886 | encoding = CTF_ENCODING_UTF8; |
| 2887 | } else if (strcmp(s_right, "none") == 0) { |
| 2888 | encoding = CTF_ENCODING_NONE; |
| 2889 | } else { |
| 2890 | _BT_COMP_LOGE_NODE(right, |
| 2891 | "Invalid `encoding` attribute in string field class: " |
| 2892 | "unknown encoding: encoding=\"%s\"", |
| 2893 | s_right); |
| 2894 | g_free(s_right); |
| 2895 | ret = -EINVAL; |
| 2896 | goto error; |
| 2897 | } |
| 2898 | |
| 2899 | g_free(s_right); |
| 2900 | _SET(&set, _STRING_ENCODING_SET); |
| 2901 | } else { |
| 2902 | _BT_COMP_LOGW_NODE(left, |
| 2903 | "Unknown attribute in string field class: " |
| 2904 | "attr-name=\"%s\"", |
| 2905 | left->u.unary_expression.u.string); |
| 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | *string_decl = ctf_field_class_string_create(); |
| 2910 | BT_ASSERT(*string_decl); |
| 2911 | (*string_decl)->encoding = encoding; |
| 2912 | return 0; |
| 2913 | |
| 2914 | error: |
| 2915 | ctf_field_class_destroy((void *) *string_decl); |
| 2916 | *string_decl = NULL; |
| 2917 | return ret; |
| 2918 | } |
| 2919 | |
| 2920 | static |
| 2921 | int visit_field_class_specifier_list(struct ctx *ctx, |
| 2922 | struct ctf_node *ts_list, struct ctf_field_class **decl) |
| 2923 | { |
| 2924 | int ret = 0; |
| 2925 | struct ctf_node *first, *node; |
| 2926 | |
| 2927 | *decl = NULL; |
| 2928 | |
| 2929 | if (ts_list->type != NODE_TYPE_SPECIFIER_LIST) { |
| 2930 | _BT_COMP_LOGE_NODE(ts_list, |
| 2931 | "Unexpected node type: node-type=%d", ts_list->type); |
| 2932 | ret = -EINVAL; |
| 2933 | goto error; |
| 2934 | } |
| 2935 | |
| 2936 | first = _BT_LIST_FIRST_ENTRY(&ts_list->u.field_class_specifier_list.head, |
| 2937 | struct ctf_node, siblings); |
| 2938 | if (first->type != NODE_TYPE_SPECIFIER) { |
| 2939 | _BT_COMP_LOGE_NODE(first, |
| 2940 | "Unexpected node type: node-type=%d", first->type); |
| 2941 | ret = -EINVAL; |
| 2942 | goto error; |
| 2943 | } |
| 2944 | |
| 2945 | node = first->u.field_class_specifier.node; |
| 2946 | |
| 2947 | switch (first->u.field_class_specifier.type) { |
| 2948 | case TYPESPEC_INTEGER: |
| 2949 | ret = visit_integer_decl(ctx, &node->u.integer.expressions, |
| 2950 | (void *) decl); |
| 2951 | if (ret) { |
| 2952 | BT_ASSERT(!*decl); |
| 2953 | goto error; |
| 2954 | } |
| 2955 | break; |
| 2956 | case TYPESPEC_FLOATING_POINT: |
| 2957 | ret = visit_floating_point_number_decl(ctx, |
| 2958 | &node->u.floating_point.expressions, (void *) decl); |
| 2959 | if (ret) { |
| 2960 | BT_ASSERT(!*decl); |
| 2961 | goto error; |
| 2962 | } |
| 2963 | break; |
| 2964 | case TYPESPEC_STRING: |
| 2965 | ret = visit_string_decl(ctx, |
| 2966 | &node->u.string.expressions, (void *) decl); |
| 2967 | if (ret) { |
| 2968 | BT_ASSERT(!*decl); |
| 2969 | goto error; |
| 2970 | } |
| 2971 | break; |
| 2972 | case TYPESPEC_STRUCT: |
| 2973 | ret = visit_struct_decl(ctx, node->u._struct.name, |
| 2974 | &node->u._struct.declaration_list, |
| 2975 | node->u._struct.has_body, |
| 2976 | &node->u._struct.min_align, (void *) decl); |
| 2977 | if (ret) { |
| 2978 | BT_ASSERT(!*decl); |
| 2979 | goto error; |
| 2980 | } |
| 2981 | break; |
| 2982 | case TYPESPEC_VARIANT: |
| 2983 | ret = visit_variant_decl(ctx, node->u.variant.name, |
| 2984 | node->u.variant.choice, |
| 2985 | &node->u.variant.declaration_list, |
| 2986 | node->u.variant.has_body, (void *) decl); |
| 2987 | if (ret) { |
| 2988 | BT_ASSERT(!*decl); |
| 2989 | goto error; |
| 2990 | } |
| 2991 | break; |
| 2992 | case TYPESPEC_ENUM: |
| 2993 | ret = visit_enum_decl(ctx, node->u._enum.enum_id, |
| 2994 | node->u._enum.container_field_class, |
| 2995 | &node->u._enum.enumerator_list, |
| 2996 | node->u._enum.has_body, (void *) decl); |
| 2997 | if (ret) { |
| 2998 | BT_ASSERT(!*decl); |
| 2999 | goto error; |
| 3000 | } |
| 3001 | break; |
| 3002 | case TYPESPEC_VOID: |
| 3003 | case TYPESPEC_CHAR: |
| 3004 | case TYPESPEC_SHORT: |
| 3005 | case TYPESPEC_INT: |
| 3006 | case TYPESPEC_LONG: |
| 3007 | case TYPESPEC_FLOAT: |
| 3008 | case TYPESPEC_DOUBLE: |
| 3009 | case TYPESPEC_SIGNED: |
| 3010 | case TYPESPEC_UNSIGNED: |
| 3011 | case TYPESPEC_BOOL: |
| 3012 | case TYPESPEC_COMPLEX: |
| 3013 | case TYPESPEC_IMAGINARY: |
| 3014 | case TYPESPEC_CONST: |
| 3015 | case TYPESPEC_ID_TYPE: |
| 3016 | ret = visit_field_class_specifier(ctx, ts_list, decl); |
| 3017 | if (ret) { |
| 3018 | _BT_COMP_LOGE_NODE(first, |
| 3019 | "Cannot visit field class specifier: ret=%d", |
| 3020 | ret); |
| 3021 | BT_ASSERT(!*decl); |
| 3022 | goto error; |
| 3023 | } |
| 3024 | break; |
| 3025 | default: |
| 3026 | _BT_COMP_LOGE_NODE(first, |
| 3027 | "Unexpected field class specifier type: node-type=%d", |
| 3028 | first->u.field_class_specifier.type); |
| 3029 | ret = -EINVAL; |
| 3030 | goto error; |
| 3031 | } |
| 3032 | |
| 3033 | BT_ASSERT(*decl); |
| 3034 | return 0; |
| 3035 | |
| 3036 | error: |
| 3037 | ctf_field_class_destroy((void *) *decl); |
| 3038 | *decl = NULL; |
| 3039 | return ret; |
| 3040 | } |
| 3041 | |
| 3042 | static |
| 3043 | int visit_event_decl_entry(struct ctx *ctx, struct ctf_node *node, |
| 3044 | struct ctf_event_class *event_class, uint64_t *stream_id, |
| 3045 | int *set) |
| 3046 | { |
| 3047 | int ret = 0; |
| 3048 | char *left = NULL; |
| 3049 | |
| 3050 | switch (node->type) { |
| 3051 | case NODE_TYPEDEF: |
| 3052 | ret = visit_field_class_def(ctx, node->u.field_class_def.field_class_specifier_list, |
| 3053 | &node->u.field_class_def.field_class_declarators); |
| 3054 | if (ret) { |
| 3055 | _BT_COMP_LOGE_NODE(node, |
| 3056 | "Cannot add field class found in event class."); |
| 3057 | goto error; |
| 3058 | } |
| 3059 | break; |
| 3060 | case NODE_TYPEALIAS: |
| 3061 | ret = visit_field_class_alias(ctx, node->u.field_class_alias.target, |
| 3062 | node->u.field_class_alias.alias); |
| 3063 | if (ret) { |
| 3064 | _BT_COMP_LOGE_NODE(node, |
| 3065 | "Cannot add field class alias found in event class."); |
| 3066 | goto error; |
| 3067 | } |
| 3068 | break; |
| 3069 | case NODE_CTF_EXPRESSION: |
| 3070 | { |
| 3071 | left = concatenate_unary_strings(&node->u.ctf_expression.left); |
| 3072 | if (!left) { |
| 3073 | _BT_COMP_LOGE_NODE(node, "Cannot concatenate unary strings."); |
| 3074 | ret = -EINVAL; |
| 3075 | goto error; |
| 3076 | } |
| 3077 | |
| 3078 | if (strcmp(left, "name") == 0) { |
| 3079 | /* This is already known at this stage */ |
| 3080 | if (_IS_SET(set, _EVENT_NAME_SET)) { |
| 3081 | _BT_COMP_LOGE_DUP_ATTR(node, "name", "event class"); |
| 3082 | ret = -EPERM; |
| 3083 | goto error; |
| 3084 | } |
| 3085 | |
| 3086 | _SET(set, _EVENT_NAME_SET); |
| 3087 | } else if (strcmp(left, "id") == 0) { |
| 3088 | int64_t id = -1; |
| 3089 | |
| 3090 | if (_IS_SET(set, _EVENT_ID_SET)) { |
| 3091 | _BT_COMP_LOGE_DUP_ATTR(node, "id", "event class"); |
| 3092 | ret = -EPERM; |
| 3093 | goto error; |
| 3094 | } |
| 3095 | |
| 3096 | ret = get_unary_unsigned(ctx, |
| 3097 | &node->u.ctf_expression.right, |
| 3098 | (uint64_t *) &id); |
| 3099 | /* Only read "id" if get_unary_unsigned() succeeded. */ |
| 3100 | if (ret || (!ret && id < 0)) { |
| 3101 | _BT_COMP_LOGE_NODE(node, |
| 3102 | "Unexpected unary expression for event class's `id` attribute."); |
| 3103 | ret = -EINVAL; |
| 3104 | goto error; |
| 3105 | } |
| 3106 | |
| 3107 | event_class->id = id; |
| 3108 | _SET(set, _EVENT_ID_SET); |
| 3109 | } else if (strcmp(left, "stream_id") == 0) { |
| 3110 | if (_IS_SET(set, _EVENT_STREAM_ID_SET)) { |
| 3111 | _BT_COMP_LOGE_DUP_ATTR(node, "stream_id", |
| 3112 | "event class"); |
| 3113 | ret = -EPERM; |
| 3114 | goto error; |
| 3115 | } |
| 3116 | |
| 3117 | ret = get_unary_unsigned(ctx, |
| 3118 | &node->u.ctf_expression.right, stream_id); |
| 3119 | |
| 3120 | /* |
| 3121 | * Only read "stream_id" if get_unary_unsigned() |
| 3122 | * succeeded. |
| 3123 | */ |
| 3124 | if (ret) { |
| 3125 | _BT_COMP_LOGE_NODE(node, |
| 3126 | "Unexpected unary expression for event class's `stream_id` attribute."); |
| 3127 | ret = -EINVAL; |
| 3128 | goto error; |
| 3129 | } |
| 3130 | |
| 3131 | _SET(set, _EVENT_STREAM_ID_SET); |
| 3132 | } else if (strcmp(left, "context") == 0) { |
| 3133 | if (_IS_SET(set, _EVENT_CONTEXT_SET)) { |
| 3134 | _BT_COMP_LOGE_NODE(node, |
| 3135 | "Duplicate `context` entry in event class."); |
| 3136 | ret = -EPERM; |
| 3137 | goto error; |
| 3138 | } |
| 3139 | |
| 3140 | ret = visit_field_class_specifier_list(ctx, |
| 3141 | _BT_LIST_FIRST_ENTRY( |
| 3142 | &node->u.ctf_expression.right, |
| 3143 | struct ctf_node, siblings), |
| 3144 | &event_class->spec_context_fc); |
| 3145 | if (ret) { |
| 3146 | _BT_COMP_LOGE_NODE(node, |
| 3147 | "Cannot create event class's context field class."); |
| 3148 | goto error; |
| 3149 | } |
| 3150 | |
| 3151 | BT_ASSERT(event_class->spec_context_fc); |
| 3152 | _SET(set, _EVENT_CONTEXT_SET); |
| 3153 | } else if (strcmp(left, "fields") == 0) { |
| 3154 | if (_IS_SET(set, _EVENT_FIELDS_SET)) { |
| 3155 | _BT_COMP_LOGE_NODE(node, |
| 3156 | "Duplicate `fields` entry in event class."); |
| 3157 | ret = -EPERM; |
| 3158 | goto error; |
| 3159 | } |
| 3160 | |
| 3161 | ret = visit_field_class_specifier_list(ctx, |
| 3162 | _BT_LIST_FIRST_ENTRY( |
| 3163 | &node->u.ctf_expression.right, |
| 3164 | struct ctf_node, siblings), |
| 3165 | &event_class->payload_fc); |
| 3166 | if (ret) { |
| 3167 | _BT_COMP_LOGE_NODE(node, |
| 3168 | "Cannot create event class's payload field class."); |
| 3169 | goto error; |
| 3170 | } |
| 3171 | |
| 3172 | BT_ASSERT(event_class->payload_fc); |
| 3173 | _SET(set, _EVENT_FIELDS_SET); |
| 3174 | } else if (strcmp(left, "loglevel") == 0) { |
| 3175 | uint64_t loglevel_value; |
| 3176 | bool is_log_level_known = true; |
| 3177 | bt_event_class_log_level log_level = -1; |
| 3178 | |
| 3179 | if (_IS_SET(set, _EVENT_LOG_LEVEL_SET)) { |
| 3180 | _BT_COMP_LOGE_DUP_ATTR(node, "loglevel", |
| 3181 | "event class"); |
| 3182 | ret = -EPERM; |
| 3183 | goto error; |
| 3184 | } |
| 3185 | |
| 3186 | ret = get_unary_unsigned(ctx, |
| 3187 | &node->u.ctf_expression.right, &loglevel_value); |
| 3188 | if (ret) { |
| 3189 | _BT_COMP_LOGE_NODE(node, |
| 3190 | "Unexpected unary expression for event class's `loglevel` attribute."); |
| 3191 | ret = -EINVAL; |
| 3192 | goto error; |
| 3193 | } |
| 3194 | |
| 3195 | switch (loglevel_value) { |
| 3196 | case 0: |
| 3197 | log_level = BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY; |
| 3198 | break; |
| 3199 | case 1: |
| 3200 | log_level = BT_EVENT_CLASS_LOG_LEVEL_ALERT; |
| 3201 | break; |
| 3202 | case 2: |
| 3203 | log_level = BT_EVENT_CLASS_LOG_LEVEL_CRITICAL; |
| 3204 | break; |
| 3205 | case 3: |
| 3206 | log_level = BT_EVENT_CLASS_LOG_LEVEL_ERROR; |
| 3207 | break; |
| 3208 | case 4: |
| 3209 | log_level = BT_EVENT_CLASS_LOG_LEVEL_WARNING; |
| 3210 | break; |
| 3211 | case 5: |
| 3212 | log_level = BT_EVENT_CLASS_LOG_LEVEL_NOTICE; |
| 3213 | break; |
| 3214 | case 6: |
| 3215 | log_level = BT_EVENT_CLASS_LOG_LEVEL_INFO; |
| 3216 | break; |
| 3217 | case 7: |
| 3218 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM; |
| 3219 | break; |
| 3220 | case 8: |
| 3221 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM; |
| 3222 | break; |
| 3223 | case 9: |
| 3224 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS; |
| 3225 | break; |
| 3226 | case 10: |
| 3227 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE; |
| 3228 | break; |
| 3229 | case 11: |
| 3230 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT; |
| 3231 | break; |
| 3232 | case 12: |
| 3233 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION; |
| 3234 | break; |
| 3235 | case 13: |
| 3236 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE; |
| 3237 | break; |
| 3238 | case 14: |
| 3239 | log_level = BT_EVENT_CLASS_LOG_LEVEL_DEBUG; |
| 3240 | break; |
| 3241 | default: |
| 3242 | is_log_level_known = false; |
| 3243 | _BT_COMP_LOGW_NODE(node, "Not setting event class's log level because its value is unknown: " |
| 3244 | "log-level=%" PRIu64, loglevel_value); |
| 3245 | } |
| 3246 | |
| 3247 | if (is_log_level_known) { |
| 3248 | ctf_event_class_set_log_level(event_class, log_level); |
| 3249 | } |
| 3250 | |
| 3251 | _SET(set, _EVENT_LOG_LEVEL_SET); |
| 3252 | } else if (strcmp(left, "model.emf.uri") == 0) { |
| 3253 | char *right; |
| 3254 | |
| 3255 | if (_IS_SET(set, _EVENT_MODEL_EMF_URI_SET)) { |
| 3256 | _BT_COMP_LOGE_DUP_ATTR(node, "model.emf.uri", |
| 3257 | "event class"); |
| 3258 | ret = -EPERM; |
| 3259 | goto error; |
| 3260 | } |
| 3261 | |
| 3262 | right = concatenate_unary_strings( |
| 3263 | &node->u.ctf_expression.right); |
| 3264 | if (!right) { |
| 3265 | _BT_COMP_LOGE_NODE(node, |
| 3266 | "Unexpected unary expression for event class's `model.emf.uri` attribute."); |
| 3267 | ret = -EINVAL; |
| 3268 | goto error; |
| 3269 | } |
| 3270 | |
| 3271 | if (strlen(right) == 0) { |
| 3272 | _BT_COMP_LOGW_NODE(node, |
| 3273 | "Not setting event class's EMF URI because it's empty."); |
| 3274 | } else { |
| 3275 | g_string_assign(event_class->emf_uri, |
| 3276 | right); |
| 3277 | } |
| 3278 | |
| 3279 | g_free(right); |
| 3280 | _SET(set, _EVENT_MODEL_EMF_URI_SET); |
| 3281 | } else { |
| 3282 | _BT_COMP_LOGW_NODE(node, |
| 3283 | "Unknown attribute in event class: " |
| 3284 | "attr-name=\"%s\"", left); |
| 3285 | } |
| 3286 | |
| 3287 | g_free(left); |
| 3288 | left = NULL; |
| 3289 | break; |
| 3290 | } |
| 3291 | default: |
| 3292 | ret = -EPERM; |
| 3293 | goto error; |
| 3294 | } |
| 3295 | |
| 3296 | goto end; |
| 3297 | |
| 3298 | error: |
| 3299 | g_free(left); |
| 3300 | |
| 3301 | end: |
| 3302 | return ret; |
| 3303 | } |
| 3304 | |
| 3305 | static |
| 3306 | char *get_event_decl_name(struct ctx *ctx, struct ctf_node *node) |
| 3307 | { |
| 3308 | char *left = NULL; |
| 3309 | char *name = NULL; |
| 3310 | struct ctf_node *iter; |
| 3311 | struct bt_list_head *decl_list = &node->u.event.declaration_list; |
| 3312 | |
| 3313 | bt_list_for_each_entry(iter, decl_list, siblings) { |
| 3314 | if (iter->type != NODE_CTF_EXPRESSION) { |
| 3315 | continue; |
| 3316 | } |
| 3317 | |
| 3318 | left = concatenate_unary_strings(&iter->u.ctf_expression.left); |
| 3319 | if (!left) { |
| 3320 | _BT_COMP_LOGE_NODE(iter, |
| 3321 | "Cannot concatenate unary strings."); |
| 3322 | goto error; |
| 3323 | } |
| 3324 | |
| 3325 | if (strcmp(left, "name") == 0) { |
| 3326 | name = concatenate_unary_strings( |
| 3327 | &iter->u.ctf_expression.right); |
| 3328 | if (!name) { |
| 3329 | _BT_COMP_LOGE_NODE(iter, |
| 3330 | "Unexpected unary expression for event class's `name` attribute."); |
| 3331 | goto error; |
| 3332 | } |
| 3333 | } |
| 3334 | |
| 3335 | g_free(left); |
| 3336 | left = NULL; |
| 3337 | |
| 3338 | if (name) { |
| 3339 | break; |
| 3340 | } |
| 3341 | } |
| 3342 | |
| 3343 | return name; |
| 3344 | |
| 3345 | error: |
| 3346 | g_free(left); |
| 3347 | return NULL; |
| 3348 | } |
| 3349 | |
| 3350 | static |
| 3351 | int visit_event_decl(struct ctx *ctx, struct ctf_node *node) |
| 3352 | { |
| 3353 | int ret = 0; |
| 3354 | int set = 0; |
| 3355 | struct ctf_node *iter; |
| 3356 | uint64_t stream_id = 0; |
| 3357 | char *event_name = NULL; |
| 3358 | struct ctf_event_class *event_class = NULL; |
| 3359 | struct ctf_stream_class *stream_class = NULL; |
| 3360 | struct bt_list_head *decl_list = &node->u.event.declaration_list; |
| 3361 | bool pop_scope = false; |
| 3362 | |
| 3363 | if (node->visited) { |
| 3364 | goto end; |
| 3365 | } |
| 3366 | |
| 3367 | node->visited = TRUE; |
| 3368 | event_name = get_event_decl_name(ctx, node); |
| 3369 | if (!event_name) { |
| 3370 | _BT_COMP_LOGE_NODE(node, |
| 3371 | "Missing `name` attribute in event class."); |
| 3372 | ret = -EPERM; |
| 3373 | goto error; |
| 3374 | } |
| 3375 | |
| 3376 | event_class = ctf_event_class_create(); |
| 3377 | BT_ASSERT(event_class); |
| 3378 | g_string_assign(event_class->name, event_name); |
| 3379 | _TRY_PUSH_SCOPE_OR_GOTO_ERROR(); |
| 3380 | pop_scope = true; |
| 3381 | |
| 3382 | bt_list_for_each_entry(iter, decl_list, siblings) { |
| 3383 | ret = visit_event_decl_entry(ctx, iter, event_class, |
| 3384 | &stream_id, &set); |
| 3385 | if (ret) { |
| 3386 | _BT_COMP_LOGE_NODE(iter, "Cannot visit event class's entry: " |
| 3387 | "ret=%d", ret); |
| 3388 | goto error; |
| 3389 | } |
| 3390 | } |
| 3391 | |
| 3392 | if (!_IS_SET(&set, _EVENT_STREAM_ID_SET)) { |
| 3393 | /* |
| 3394 | * Allow missing stream_id if there is only a single |
| 3395 | * stream class. |
| 3396 | */ |
| 3397 | switch (ctx->ctf_tc->stream_classes->len) { |
| 3398 | case 0: |
| 3399 | /* Create implicit stream class if there's none */ |
| 3400 | stream_id = 0; |
| 3401 | stream_class = ctf_stream_class_create(); |
| 3402 | BT_ASSERT(stream_class); |
| 3403 | stream_class->id = stream_id; |
| 3404 | g_ptr_array_add(ctx->ctf_tc->stream_classes, |
| 3405 | stream_class); |
| 3406 | break; |
| 3407 | case 1: |
| 3408 | /* Single stream class: get its ID */ |
| 3409 | stream_class = ctx->ctf_tc->stream_classes->pdata[0]; |
| 3410 | stream_id = stream_class->id; |
| 3411 | break; |
| 3412 | default: |
| 3413 | _BT_COMP_LOGE_NODE(node, |
| 3414 | "Missing `stream_id` attribute in event class."); |
| 3415 | ret = -EPERM; |
| 3416 | goto error; |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | /* We have the stream ID now; get the stream class if found */ |
| 3421 | if (!stream_class) { |
| 3422 | stream_class = ctf_trace_class_borrow_stream_class_by_id( |
| 3423 | ctx->ctf_tc, stream_id); |
| 3424 | if (!stream_class) { |
| 3425 | _BT_COMP_LOGE_NODE(node, |
| 3426 | "Cannot find stream class at this point: " |
| 3427 | "id=%" PRId64, stream_id); |
| 3428 | ret = -EINVAL; |
| 3429 | goto error; |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | BT_ASSERT(stream_class); |
| 3434 | |
| 3435 | if (!_IS_SET(&set, _EVENT_ID_SET)) { |
| 3436 | /* Allow only one event without ID per stream */ |
| 3437 | if (stream_class->event_classes->len != 0) { |
| 3438 | _BT_COMP_LOGE_NODE(node, |
| 3439 | "Missing `id` attribute in event class."); |
| 3440 | ret = -EPERM; |
| 3441 | goto error; |
| 3442 | } |
| 3443 | |
| 3444 | /* Automatic ID */ |
| 3445 | event_class->id = 0; |
| 3446 | } |
| 3447 | |
| 3448 | if (ctf_stream_class_borrow_event_class_by_id(stream_class, |
| 3449 | event_class->id)) { |
| 3450 | _BT_COMP_LOGE_NODE(node, |
| 3451 | "Duplicate event class (same ID) in the same stream class: " |
| 3452 | "id=%" PRId64, event_class->id); |
| 3453 | ret = -EEXIST; |
| 3454 | goto error; |
| 3455 | } |
| 3456 | |
| 3457 | ctf_stream_class_append_event_class(stream_class, event_class); |
| 3458 | event_class = NULL; |
| 3459 | goto end; |
| 3460 | |
| 3461 | error: |
| 3462 | ctf_event_class_destroy(event_class); |
| 3463 | event_class = NULL; |
| 3464 | |
| 3465 | if (ret >= 0) { |
| 3466 | ret = -1; |
| 3467 | } |
| 3468 | |
| 3469 | end: |
| 3470 | if (pop_scope) { |
| 3471 | ctx_pop_scope(ctx); |
| 3472 | } |
| 3473 | |
| 3474 | g_free(event_name); |
| 3475 | |
| 3476 | return ret; |
| 3477 | } |
| 3478 | |
| 3479 | static |
| 3480 | int auto_map_field_to_trace_clock_class(struct ctx *ctx, |
| 3481 | struct ctf_field_class *fc) |
| 3482 | { |
| 3483 | struct ctf_clock_class *clock_class_to_map_to = NULL; |
| 3484 | struct ctf_field_class_int *int_fc = (void *) fc; |
| 3485 | int ret = 0; |
| 3486 | uint64_t clock_class_count; |
| 3487 | |
| 3488 | if (!fc) { |
| 3489 | goto end; |
| 3490 | } |
| 3491 | |
| 3492 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 3493 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 3494 | goto end; |
| 3495 | } |
| 3496 | |
| 3497 | if (int_fc->mapped_clock_class) { |
| 3498 | /* Already mapped */ |
| 3499 | goto end; |
| 3500 | } |
| 3501 | |
| 3502 | clock_class_count = ctx->ctf_tc->clock_classes->len; |
| 3503 | |
| 3504 | switch (clock_class_count) { |
| 3505 | case 0: |
| 3506 | /* |
| 3507 | * No clock class exists in the trace at this point. Create an |
| 3508 | * implicit one at 1 GHz, named `default`, and use this clock |
| 3509 | * class. |
| 3510 | */ |
| 3511 | clock_class_to_map_to = ctf_clock_class_create(); |
| 3512 | BT_ASSERT(clock_class_to_map_to); |
| 3513 | clock_class_to_map_to->frequency = UINT64_C(1000000000); |
| 3514 | g_string_assign(clock_class_to_map_to->name, "default"); |
| 3515 | BT_ASSERT(ret == 0); |
| 3516 | g_ptr_array_add(ctx->ctf_tc->clock_classes, |
| 3517 | clock_class_to_map_to); |
| 3518 | break; |
| 3519 | case 1: |
| 3520 | /* |
| 3521 | * Only one clock class exists in the trace at this point: use |
| 3522 | * this one. |
| 3523 | */ |
| 3524 | clock_class_to_map_to = ctx->ctf_tc->clock_classes->pdata[0]; |
| 3525 | break; |
| 3526 | default: |
| 3527 | /* |
| 3528 | * Timestamp field not mapped to a clock class and there's more |
| 3529 | * than one clock class in the trace: this is an error. |
| 3530 | */ |
| 3531 | BT_COMP_LOGE_STR("Timestamp field found with no mapped clock class, " |
| 3532 | "but there's more than one clock class in the trace at this point."); |
| 3533 | ret = -1; |
| 3534 | goto end; |
| 3535 | } |
| 3536 | |
| 3537 | BT_ASSERT(clock_class_to_map_to); |
| 3538 | int_fc->mapped_clock_class = clock_class_to_map_to; |
| 3539 | |
| 3540 | end: |
| 3541 | return ret; |
| 3542 | } |
| 3543 | |
| 3544 | static |
| 3545 | int auto_map_fields_to_trace_clock_class(struct ctx *ctx, |
| 3546 | struct ctf_field_class *root_fc, const char *field_name) |
| 3547 | { |
| 3548 | int ret = 0; |
| 3549 | uint64_t i, count; |
| 3550 | struct ctf_field_class_struct *struct_fc = (void *) root_fc; |
| 3551 | struct ctf_field_class_variant *var_fc = (void *) root_fc; |
| 3552 | |
| 3553 | if (!root_fc) { |
| 3554 | goto end; |
| 3555 | } |
| 3556 | |
| 3557 | if (root_fc->type != CTF_FIELD_CLASS_TYPE_STRUCT && |
| 3558 | root_fc->type != CTF_FIELD_CLASS_TYPE_VARIANT) { |
| 3559 | goto end; |
| 3560 | } |
| 3561 | |
| 3562 | if (root_fc->type == CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 3563 | count = struct_fc->members->len; |
| 3564 | } else { |
| 3565 | count = var_fc->options->len; |
| 3566 | } |
| 3567 | |
| 3568 | for (i = 0; i < count; i++) { |
| 3569 | struct ctf_named_field_class *named_fc = NULL; |
| 3570 | |
| 3571 | if (root_fc->type == CTF_FIELD_CLASS_TYPE_STRUCT) { |
| 3572 | named_fc = ctf_field_class_struct_borrow_member_by_index( |
| 3573 | struct_fc, i); |
| 3574 | } else if (root_fc->type == CTF_FIELD_CLASS_TYPE_VARIANT) { |
| 3575 | named_fc = ctf_field_class_variant_borrow_option_by_index( |
| 3576 | var_fc, i); |
| 3577 | } |
| 3578 | |
| 3579 | if (strcmp(named_fc->name->str, field_name) == 0) { |
| 3580 | ret = auto_map_field_to_trace_clock_class(ctx, |
| 3581 | named_fc->fc); |
| 3582 | if (ret) { |
| 3583 | BT_COMP_LOGE("Cannot automatically map field to trace's clock class: " |
| 3584 | "field-name=\"%s\"", field_name); |
| 3585 | goto end; |
| 3586 | } |
| 3587 | } |
| 3588 | |
| 3589 | ret = auto_map_fields_to_trace_clock_class(ctx, named_fc->fc, |
| 3590 | field_name); |
| 3591 | if (ret) { |
| 3592 | BT_COMP_LOGE("Cannot automatically map structure or variant field class's fields to trace's clock class: " |
| 3593 | "field-name=\"%s\", root-field-name=\"%s\"", |
| 3594 | field_name, named_fc->name->str); |
| 3595 | goto end; |
| 3596 | } |
| 3597 | } |
| 3598 | |
| 3599 | end: |
| 3600 | return ret; |
| 3601 | } |
| 3602 | |
| 3603 | static |
| 3604 | int visit_stream_decl_entry(struct ctx *ctx, struct ctf_node *node, |
| 3605 | struct ctf_stream_class *stream_class, int *set) |
| 3606 | { |
| 3607 | int ret = 0; |
| 3608 | char *left = NULL; |
| 3609 | |
| 3610 | switch (node->type) { |
| 3611 | case NODE_TYPEDEF: |
| 3612 | ret = visit_field_class_def(ctx, node->u.field_class_def.field_class_specifier_list, |
| 3613 | &node->u.field_class_def.field_class_declarators); |
| 3614 | if (ret) { |
| 3615 | _BT_COMP_LOGE_NODE(node, |
| 3616 | "Cannot add field class found in stream class."); |
| 3617 | goto error; |
| 3618 | } |
| 3619 | break; |
| 3620 | case NODE_TYPEALIAS: |
| 3621 | ret = visit_field_class_alias(ctx, node->u.field_class_alias.target, |
| 3622 | node->u.field_class_alias.alias); |
| 3623 | if (ret) { |
| 3624 | _BT_COMP_LOGE_NODE(node, |
| 3625 | "Cannot add field class alias found in stream class."); |
| 3626 | goto error; |
| 3627 | } |
| 3628 | break; |
| 3629 | case NODE_CTF_EXPRESSION: |
| 3630 | { |
| 3631 | left = concatenate_unary_strings(&node->u.ctf_expression.left); |
| 3632 | if (!left) { |
| 3633 | _BT_COMP_LOGE_NODE(node, "Cannot concatenate unary strings."); |
| 3634 | ret = -EINVAL; |
| 3635 | goto error; |
| 3636 | } |
| 3637 | |
| 3638 | if (strcmp(left, "id") == 0) { |
| 3639 | int64_t id; |
| 3640 | |
| 3641 | if (_IS_SET(set, _STREAM_ID_SET)) { |
| 3642 | _BT_COMP_LOGE_DUP_ATTR(node, "id", |
| 3643 | "stream declaration"); |
| 3644 | ret = -EPERM; |
| 3645 | goto error; |
| 3646 | } |
| 3647 | |
| 3648 | ret = get_unary_unsigned(ctx, |
| 3649 | &node->u.ctf_expression.right, |
| 3650 | (uint64_t *) &id); |
| 3651 | |
| 3652 | /* Only read "id" if get_unary_unsigned() succeeded. */ |
| 3653 | if (ret || (!ret && id < 0)) { |
| 3654 | _BT_COMP_LOGE_NODE(node, |
| 3655 | "Unexpected unary expression for stream class's `id` attribute."); |
| 3656 | ret = -EINVAL; |
| 3657 | goto error; |
| 3658 | } |
| 3659 | |
| 3660 | if (ctf_trace_class_borrow_stream_class_by_id( |
| 3661 | ctx->ctf_tc, id)) { |
| 3662 | _BT_COMP_LOGE_NODE(node, |
| 3663 | "Duplicate stream class (same ID): id=%" PRId64, |
| 3664 | id); |
| 3665 | ret = -EEXIST; |
| 3666 | goto error; |
| 3667 | } |
| 3668 | |
| 3669 | stream_class->id = id; |
| 3670 | _SET(set, _STREAM_ID_SET); |
| 3671 | } else if (strcmp(left, "event.header") == 0) { |
| 3672 | if (_IS_SET(set, _STREAM_EVENT_HEADER_SET)) { |
| 3673 | _BT_COMP_LOGE_NODE(node, |
| 3674 | "Duplicate `event.header` entry in stream class."); |
| 3675 | ret = -EPERM; |
| 3676 | goto error; |
| 3677 | } |
| 3678 | |
| 3679 | ret = visit_field_class_specifier_list(ctx, |
| 3680 | _BT_LIST_FIRST_ENTRY( |
| 3681 | &node->u.ctf_expression.right, |
| 3682 | struct ctf_node, siblings), |
| 3683 | &stream_class->event_header_fc); |
| 3684 | if (ret) { |
| 3685 | _BT_COMP_LOGE_NODE(node, |
| 3686 | "Cannot create stream class's event header field class."); |
| 3687 | goto error; |
| 3688 | } |
| 3689 | |
| 3690 | BT_ASSERT(stream_class->event_header_fc); |
| 3691 | ret = auto_map_fields_to_trace_clock_class(ctx, |
| 3692 | stream_class->event_header_fc, "timestamp"); |
| 3693 | if (ret) { |
| 3694 | _BT_COMP_LOGE_NODE(node, |
| 3695 | "Cannot automatically map specific event header field class fields named `timestamp` to trace's clock class."); |
| 3696 | goto error; |
| 3697 | } |
| 3698 | |
| 3699 | _SET(set, _STREAM_EVENT_HEADER_SET); |
| 3700 | } else if (strcmp(left, "event.context") == 0) { |
| 3701 | if (_IS_SET(set, _STREAM_EVENT_CONTEXT_SET)) { |
| 3702 | _BT_COMP_LOGE_NODE(node, |
| 3703 | "Duplicate `event.context` entry in stream class."); |
| 3704 | ret = -EPERM; |
| 3705 | goto error; |
| 3706 | } |
| 3707 | |
| 3708 | ret = visit_field_class_specifier_list(ctx, |
| 3709 | _BT_LIST_FIRST_ENTRY( |
| 3710 | &node->u.ctf_expression.right, |
| 3711 | struct ctf_node, siblings), |
| 3712 | &stream_class->event_common_context_fc); |
| 3713 | if (ret) { |
| 3714 | _BT_COMP_LOGE_NODE(node, |
| 3715 | "Cannot create stream class's event context field class."); |
| 3716 | goto error; |
| 3717 | } |
| 3718 | |
| 3719 | BT_ASSERT(stream_class->event_common_context_fc); |
| 3720 | _SET(set, _STREAM_EVENT_CONTEXT_SET); |
| 3721 | } else if (strcmp(left, "packet.context") == 0) { |
| 3722 | if (_IS_SET(set, _STREAM_PACKET_CONTEXT_SET)) { |
| 3723 | _BT_COMP_LOGE_NODE(node, |
| 3724 | "Duplicate `packet.context` entry in stream class."); |
| 3725 | ret = -EPERM; |
| 3726 | goto error; |
| 3727 | } |
| 3728 | |
| 3729 | ret = visit_field_class_specifier_list(ctx, |
| 3730 | _BT_LIST_FIRST_ENTRY( |
| 3731 | &node->u.ctf_expression.right, |
| 3732 | struct ctf_node, siblings), |
| 3733 | &stream_class->packet_context_fc); |
| 3734 | if (ret) { |
| 3735 | _BT_COMP_LOGE_NODE(node, |
| 3736 | "Cannot create stream class's packet context field class."); |
| 3737 | goto error; |
| 3738 | } |
| 3739 | |
| 3740 | BT_ASSERT(stream_class->packet_context_fc); |
| 3741 | ret = auto_map_fields_to_trace_clock_class(ctx, |
| 3742 | stream_class->packet_context_fc, |
| 3743 | "timestamp_begin"); |
| 3744 | if (ret) { |
| 3745 | _BT_COMP_LOGE_NODE(node, |
| 3746 | "Cannot automatically map specific packet context field class fields named `timestamp_begin` to trace's clock class."); |
| 3747 | goto error; |
| 3748 | } |
| 3749 | |
| 3750 | ret = auto_map_fields_to_trace_clock_class(ctx, |
| 3751 | stream_class->packet_context_fc, |
| 3752 | "timestamp_end"); |
| 3753 | if (ret) { |
| 3754 | _BT_COMP_LOGE_NODE(node, |
| 3755 | "Cannot automatically map specific packet context field class fields named `timestamp_end` to trace's clock class."); |
| 3756 | goto error; |
| 3757 | } |
| 3758 | |
| 3759 | _SET(set, _STREAM_PACKET_CONTEXT_SET); |
| 3760 | } else { |
| 3761 | _BT_COMP_LOGW_NODE(node, |
| 3762 | "Unknown attribute in stream class: " |
| 3763 | "attr-name=\"%s\"", left); |
| 3764 | } |
| 3765 | |
| 3766 | g_free(left); |
| 3767 | left = NULL; |
| 3768 | break; |
| 3769 | } |
| 3770 | |
| 3771 | default: |
| 3772 | ret = -EPERM; |
| 3773 | goto error; |
| 3774 | } |
| 3775 | |
| 3776 | return 0; |
| 3777 | |
| 3778 | error: |
| 3779 | g_free(left); |
| 3780 | return ret; |
| 3781 | } |
| 3782 | |
| 3783 | static |
| 3784 | int visit_stream_decl(struct ctx *ctx, struct ctf_node *node) |
| 3785 | { |
| 3786 | int set = 0; |
| 3787 | int ret = 0; |
| 3788 | struct ctf_node *iter; |
| 3789 | struct ctf_stream_class *stream_class = NULL; |
| 3790 | struct bt_list_head *decl_list = &node->u.stream.declaration_list; |
| 3791 | |
| 3792 | if (node->visited) { |
| 3793 | goto end; |
| 3794 | } |
| 3795 | |
| 3796 | node->visited = TRUE; |
| 3797 | stream_class = ctf_stream_class_create(); |
| 3798 | BT_ASSERT(stream_class); |
| 3799 | _TRY_PUSH_SCOPE_OR_GOTO_ERROR(); |
| 3800 | |
| 3801 | bt_list_for_each_entry(iter, decl_list, siblings) { |
| 3802 | ret = visit_stream_decl_entry(ctx, iter, stream_class, &set); |
| 3803 | if (ret) { |
| 3804 | _BT_COMP_LOGE_NODE(iter, |
| 3805 | "Cannot visit stream class's entry: " |
| 3806 | "ret=%d", ret); |
| 3807 | ctx_pop_scope(ctx); |
| 3808 | goto error; |
| 3809 | } |
| 3810 | } |
| 3811 | |
| 3812 | ctx_pop_scope(ctx); |
| 3813 | |
| 3814 | if (_IS_SET(&set, _STREAM_ID_SET)) { |
| 3815 | /* Check that packet header has `stream_id` field */ |
| 3816 | struct ctf_named_field_class *named_fc = NULL; |
| 3817 | |
| 3818 | if (!ctx->ctf_tc->packet_header_fc) { |
| 3819 | _BT_COMP_LOGE_NODE(node, |
| 3820 | "Stream class has a `id` attribute, " |
| 3821 | "but trace has no packet header field class."); |
| 3822 | goto error; |
| 3823 | } |
| 3824 | |
| 3825 | named_fc = ctf_field_class_struct_borrow_member_by_name( |
| 3826 | (void *) ctx->ctf_tc->packet_header_fc, "stream_id"); |
| 3827 | if (!named_fc) { |
| 3828 | _BT_COMP_LOGE_NODE(node, |
| 3829 | "Stream class has a `id` attribute, " |
| 3830 | "but trace's packet header field class has no `stream_id` field."); |
| 3831 | goto error; |
| 3832 | } |
| 3833 | |
| 3834 | if (named_fc->fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 3835 | named_fc->fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 3836 | _BT_COMP_LOGE_NODE(node, |
| 3837 | "Stream class has a `id` attribute, " |
| 3838 | "but trace's packet header field class's `stream_id` field is not an integer field class."); |
| 3839 | goto error; |
| 3840 | } |
| 3841 | } else { |
| 3842 | /* Allow only _one_ ID-less stream */ |
| 3843 | if (ctx->ctf_tc->stream_classes->len != 0) { |
| 3844 | _BT_COMP_LOGE_NODE(node, |
| 3845 | "Missing `id` attribute in stream class as there's more than one stream class in the trace."); |
| 3846 | ret = -EPERM; |
| 3847 | goto error; |
| 3848 | } |
| 3849 | |
| 3850 | /* Automatic ID: 0 */ |
| 3851 | stream_class->id = 0; |
| 3852 | } |
| 3853 | |
| 3854 | /* |
| 3855 | * Make sure that this stream class's ID is currently unique in |
| 3856 | * the trace. |
| 3857 | */ |
| 3858 | if (ctf_trace_class_borrow_stream_class_by_id(ctx->ctf_tc, |
| 3859 | stream_class->id)) { |
| 3860 | _BT_COMP_LOGE_NODE(node, |
| 3861 | "Duplicate stream class (same ID): id=%" PRId64, |
| 3862 | stream_class->id); |
| 3863 | ret = -EINVAL; |
| 3864 | goto error; |
| 3865 | } |
| 3866 | |
| 3867 | g_ptr_array_add(ctx->ctf_tc->stream_classes, stream_class); |
| 3868 | stream_class = NULL; |
| 3869 | goto end; |
| 3870 | |
| 3871 | error: |
| 3872 | ctf_stream_class_destroy(stream_class); |
| 3873 | stream_class = NULL; |
| 3874 | |
| 3875 | end: |
| 3876 | return ret; |
| 3877 | } |
| 3878 | |
| 3879 | static |
| 3880 | int visit_trace_decl_entry(struct ctx *ctx, struct ctf_node *node, int *set) |
| 3881 | { |
| 3882 | int ret = 0; |
| 3883 | char *left = NULL; |
| 3884 | uint64_t val; |
| 3885 | |
| 3886 | switch (node->type) { |
| 3887 | case NODE_TYPEDEF: |
| 3888 | ret = visit_field_class_def(ctx, node->u.field_class_def.field_class_specifier_list, |
| 3889 | &node->u.field_class_def.field_class_declarators); |
| 3890 | if (ret) { |
| 3891 | _BT_COMP_LOGE_NODE(node, |
| 3892 | "Cannot add field class found in trace (`trace` block)."); |
| 3893 | goto error; |
| 3894 | } |
| 3895 | break; |
| 3896 | case NODE_TYPEALIAS: |
| 3897 | ret = visit_field_class_alias(ctx, node->u.field_class_alias.target, |
| 3898 | node->u.field_class_alias.alias); |
| 3899 | if (ret) { |
| 3900 | _BT_COMP_LOGE_NODE(node, |
| 3901 | "Cannot add field class alias found in trace (`trace` block)."); |
| 3902 | goto error; |
| 3903 | } |
| 3904 | break; |
| 3905 | case NODE_CTF_EXPRESSION: |
| 3906 | { |
| 3907 | left = concatenate_unary_strings(&node->u.ctf_expression.left); |
| 3908 | if (!left) { |
| 3909 | _BT_COMP_LOGE_NODE(node, "Cannot concatenate unary strings."); |
| 3910 | ret = -EINVAL; |
| 3911 | goto error; |
| 3912 | } |
| 3913 | |
| 3914 | if (strcmp(left, "major") == 0) { |
| 3915 | if (_IS_SET(set, _TRACE_MAJOR_SET)) { |
| 3916 | _BT_COMP_LOGE_DUP_ATTR(node, "major", "trace"); |
| 3917 | ret = -EPERM; |
| 3918 | goto error; |
| 3919 | } |
| 3920 | |
| 3921 | ret = get_unary_unsigned(ctx, |
| 3922 | &node->u.ctf_expression.right, &val); |
| 3923 | if (ret) { |
| 3924 | _BT_COMP_LOGE_NODE(node, |
| 3925 | "Unexpected unary expression for trace's `major` attribute."); |
| 3926 | ret = -EINVAL; |
| 3927 | goto error; |
| 3928 | } |
| 3929 | |
| 3930 | if (val != 1) { |
| 3931 | _BT_COMP_LOGE_NODE(node, |
| 3932 | "Invalid trace's `minor` attribute: expecting 1."); |
| 3933 | goto error; |
| 3934 | } |
| 3935 | |
| 3936 | ctx->ctf_tc->major = val; |
| 3937 | _SET(set, _TRACE_MAJOR_SET); |
| 3938 | } else if (strcmp(left, "minor") == 0) { |
| 3939 | if (_IS_SET(set, _TRACE_MINOR_SET)) { |
| 3940 | _BT_COMP_LOGE_DUP_ATTR(node, "minor", "trace"); |
| 3941 | ret = -EPERM; |
| 3942 | goto error; |
| 3943 | } |
| 3944 | |
| 3945 | ret = get_unary_unsigned(ctx, |
| 3946 | &node->u.ctf_expression.right, &val); |
| 3947 | if (ret) { |
| 3948 | _BT_COMP_LOGE_NODE(node, |
| 3949 | "Unexpected unary expression for trace's `minor` attribute."); |
| 3950 | ret = -EINVAL; |
| 3951 | goto error; |
| 3952 | } |
| 3953 | |
| 3954 | if (val != 8) { |
| 3955 | _BT_COMP_LOGE_NODE(node, |
| 3956 | "Invalid trace's `minor` attribute: expecting 8."); |
| 3957 | goto error; |
| 3958 | } |
| 3959 | |
| 3960 | ctx->ctf_tc->minor = val; |
| 3961 | _SET(set, _TRACE_MINOR_SET); |
| 3962 | } else if (strcmp(left, "uuid") == 0) { |
| 3963 | if (_IS_SET(set, _TRACE_UUID_SET)) { |
| 3964 | _BT_COMP_LOGE_DUP_ATTR(node, "uuid", "trace"); |
| 3965 | ret = -EPERM; |
| 3966 | goto error; |
| 3967 | } |
| 3968 | |
| 3969 | ret = get_unary_uuid(ctx, |
| 3970 | &node->u.ctf_expression.right, |
| 3971 | ctx->ctf_tc->uuid); |
| 3972 | if (ret) { |
| 3973 | _BT_COMP_LOGE_NODE(node, |
| 3974 | "Invalid trace's `uuid` attribute."); |
| 3975 | goto error; |
| 3976 | } |
| 3977 | |
| 3978 | ctx->ctf_tc->is_uuid_set = true; |
| 3979 | _SET(set, _TRACE_UUID_SET); |
| 3980 | } else if (strcmp(left, "byte_order") == 0) { |
| 3981 | /* Default byte order is already known at this stage */ |
| 3982 | if (_IS_SET(set, _TRACE_BYTE_ORDER_SET)) { |
| 3983 | _BT_COMP_LOGE_DUP_ATTR(node, "byte_order", |
| 3984 | "trace"); |
| 3985 | ret = -EPERM; |
| 3986 | goto error; |
| 3987 | } |
| 3988 | |
| 3989 | BT_ASSERT(ctx->ctf_tc->default_byte_order != CTF_BYTE_ORDER_UNKNOWN); |
| 3990 | _SET(set, _TRACE_BYTE_ORDER_SET); |
| 3991 | } else if (strcmp(left, "packet.header") == 0) { |
| 3992 | if (_IS_SET(set, _TRACE_PACKET_HEADER_SET)) { |
| 3993 | _BT_COMP_LOGE_NODE(node, |
| 3994 | "Duplicate `packet.header` entry in trace."); |
| 3995 | ret = -EPERM; |
| 3996 | goto error; |
| 3997 | } |
| 3998 | |
| 3999 | ret = visit_field_class_specifier_list(ctx, |
| 4000 | _BT_LIST_FIRST_ENTRY( |
| 4001 | &node->u.ctf_expression.right, |
| 4002 | struct ctf_node, siblings), |
| 4003 | &ctx->ctf_tc->packet_header_fc); |
| 4004 | if (ret) { |
| 4005 | _BT_COMP_LOGE_NODE(node, |
| 4006 | "Cannot create trace's packet header field class."); |
| 4007 | goto error; |
| 4008 | } |
| 4009 | |
| 4010 | BT_ASSERT(ctx->ctf_tc->packet_header_fc); |
| 4011 | _SET(set, _TRACE_PACKET_HEADER_SET); |
| 4012 | } else { |
| 4013 | _BT_COMP_LOGW_NODE(node, |
| 4014 | "Unknown attribute in stream class: " |
| 4015 | "attr-name=\"%s\"", left); |
| 4016 | } |
| 4017 | |
| 4018 | g_free(left); |
| 4019 | left = NULL; |
| 4020 | break; |
| 4021 | } |
| 4022 | default: |
| 4023 | _BT_COMP_LOGE_NODE(node, "Unknown expression in trace."); |
| 4024 | ret = -EINVAL; |
| 4025 | goto error; |
| 4026 | } |
| 4027 | |
| 4028 | return 0; |
| 4029 | |
| 4030 | error: |
| 4031 | g_free(left); |
| 4032 | return ret; |
| 4033 | } |
| 4034 | |
| 4035 | static |
| 4036 | int visit_trace_decl(struct ctx *ctx, struct ctf_node *node) |
| 4037 | { |
| 4038 | int ret = 0; |
| 4039 | int set = 0; |
| 4040 | struct ctf_node *iter; |
| 4041 | struct bt_list_head *decl_list = &node->u.trace.declaration_list; |
| 4042 | |
| 4043 | if (node->visited) { |
| 4044 | goto end; |
| 4045 | } |
| 4046 | |
| 4047 | node->visited = TRUE; |
| 4048 | |
| 4049 | if (ctx->is_trace_visited) { |
| 4050 | _BT_COMP_LOGE_NODE(node, "Duplicate trace (`trace` block)."); |
| 4051 | ret = -EEXIST; |
| 4052 | goto error; |
| 4053 | } |
| 4054 | |
| 4055 | _TRY_PUSH_SCOPE_OR_GOTO_ERROR(); |
| 4056 | |
| 4057 | bt_list_for_each_entry(iter, decl_list, siblings) { |
| 4058 | ret = visit_trace_decl_entry(ctx, iter, &set); |
| 4059 | if (ret) { |
| 4060 | _BT_COMP_LOGE_NODE(iter, "Cannot visit trace's entry (`trace` block): " |
| 4061 | "ret=%d", ret); |
| 4062 | ctx_pop_scope(ctx); |
| 4063 | goto error; |
| 4064 | } |
| 4065 | } |
| 4066 | |
| 4067 | ctx_pop_scope(ctx); |
| 4068 | |
| 4069 | if (!_IS_SET(&set, _TRACE_MAJOR_SET)) { |
| 4070 | _BT_COMP_LOGE_NODE(node, |
| 4071 | "Missing `major` attribute in trace (`trace` block)."); |
| 4072 | ret = -EPERM; |
| 4073 | goto error; |
| 4074 | } |
| 4075 | |
| 4076 | if (!_IS_SET(&set, _TRACE_MINOR_SET)) { |
| 4077 | _BT_COMP_LOGE_NODE(node, |
| 4078 | "Missing `minor` attribute in trace (`trace` block)."); |
| 4079 | ret = -EPERM; |
| 4080 | goto error; |
| 4081 | } |
| 4082 | |
| 4083 | if (!_IS_SET(&set, _TRACE_BYTE_ORDER_SET)) { |
| 4084 | _BT_COMP_LOGE_NODE(node, |
| 4085 | "Missing `byte_order` attribute in trace (`trace` block)."); |
| 4086 | ret = -EPERM; |
| 4087 | goto error; |
| 4088 | } |
| 4089 | |
| 4090 | ctx->is_trace_visited = true; |
| 4091 | |
| 4092 | end: |
| 4093 | return 0; |
| 4094 | |
| 4095 | error: |
| 4096 | return ret; |
| 4097 | } |
| 4098 | |
| 4099 | static |
| 4100 | int visit_env(struct ctx *ctx, struct ctf_node *node) |
| 4101 | { |
| 4102 | int ret = 0; |
| 4103 | char *left = NULL; |
| 4104 | struct ctf_node *entry_node; |
| 4105 | struct bt_list_head *decl_list = &node->u.env.declaration_list; |
| 4106 | |
| 4107 | if (node->visited) { |
| 4108 | goto end; |
| 4109 | } |
| 4110 | |
| 4111 | node->visited = TRUE; |
| 4112 | |
| 4113 | bt_list_for_each_entry(entry_node, decl_list, siblings) { |
| 4114 | struct bt_list_head *right_head = |
| 4115 | &entry_node->u.ctf_expression.right; |
| 4116 | |
| 4117 | if (entry_node->type != NODE_CTF_EXPRESSION) { |
| 4118 | _BT_COMP_LOGE_NODE(entry_node, |
| 4119 | "Wrong expression in environment entry: " |
| 4120 | "node-type=%d", entry_node->type); |
| 4121 | ret = -EPERM; |
| 4122 | goto error; |
| 4123 | } |
| 4124 | |
| 4125 | left = concatenate_unary_strings( |
| 4126 | &entry_node->u.ctf_expression.left); |
| 4127 | if (!left) { |
| 4128 | _BT_COMP_LOGE_NODE(entry_node, |
| 4129 | "Cannot get environment entry's name."); |
| 4130 | ret = -EINVAL; |
| 4131 | goto error; |
| 4132 | } |
| 4133 | |
| 4134 | if (is_unary_string(right_head)) { |
| 4135 | char *right = concatenate_unary_strings(right_head); |
| 4136 | |
| 4137 | if (!right) { |
| 4138 | _BT_COMP_LOGE_NODE(entry_node, |
| 4139 | "Unexpected unary expression for environment entry's value: " |
| 4140 | "name=\"%s\"", left); |
| 4141 | ret = -EINVAL; |
| 4142 | goto error; |
| 4143 | } |
| 4144 | |
| 4145 | if (strcmp(left, "tracer_name") == 0) { |
| 4146 | if (strncmp(right, "lttng", 5) == 0) { |
| 4147 | BT_COMP_LOGI("Detected LTTng trace from `%s` environment value: " |
| 4148 | "tracer-name=\"%s\"", |
| 4149 | left, right); |
| 4150 | ctx->is_lttng = true; |
| 4151 | } |
| 4152 | } |
| 4153 | |
| 4154 | ctf_trace_class_append_env_entry(ctx->ctf_tc, |
| 4155 | left, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR, |
| 4156 | right, 0); |
| 4157 | g_free(right); |
| 4158 | } else if (is_unary_unsigned(right_head) || |
| 4159 | is_unary_signed(right_head)) { |
| 4160 | int64_t v; |
| 4161 | |
| 4162 | if (is_unary_unsigned(right_head)) { |
| 4163 | ret = get_unary_unsigned(ctx, right_head, |
| 4164 | (uint64_t *) &v); |
| 4165 | } else { |
| 4166 | ret = get_unary_signed(right_head, &v); |
| 4167 | } |
| 4168 | if (ret) { |
| 4169 | _BT_COMP_LOGE_NODE(entry_node, |
| 4170 | "Unexpected unary expression for environment entry's value: " |
| 4171 | "name=\"%s\"", left); |
| 4172 | ret = -EINVAL; |
| 4173 | goto error; |
| 4174 | } |
| 4175 | |
| 4176 | ctf_trace_class_append_env_entry(ctx->ctf_tc, |
| 4177 | left, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT, |
| 4178 | NULL, v); |
| 4179 | } else { |
| 4180 | _BT_COMP_LOGW_NODE(entry_node, |
| 4181 | "Environment entry has unknown type: " |
| 4182 | "name=\"%s\"", left); |
| 4183 | } |
| 4184 | |
| 4185 | g_free(left); |
| 4186 | left = NULL; |
| 4187 | } |
| 4188 | |
| 4189 | end: |
| 4190 | return 0; |
| 4191 | |
| 4192 | error: |
| 4193 | g_free(left); |
| 4194 | return ret; |
| 4195 | } |
| 4196 | |
| 4197 | static |
| 4198 | int set_trace_byte_order(struct ctx *ctx, struct ctf_node *trace_node) |
| 4199 | { |
| 4200 | int ret = 0; |
| 4201 | int set = 0; |
| 4202 | char *left = NULL; |
| 4203 | struct ctf_node *node; |
| 4204 | struct bt_list_head *decl_list = &trace_node->u.trace.declaration_list; |
| 4205 | |
| 4206 | bt_list_for_each_entry(node, decl_list, siblings) { |
| 4207 | if (node->type == NODE_CTF_EXPRESSION) { |
| 4208 | struct ctf_node *right_node; |
| 4209 | |
| 4210 | left = concatenate_unary_strings( |
| 4211 | &node->u.ctf_expression.left); |
| 4212 | if (!left) { |
| 4213 | _BT_COMP_LOGE_NODE(node, |
| 4214 | "Cannot concatenate unary strings."); |
| 4215 | ret = -EINVAL; |
| 4216 | goto error; |
| 4217 | } |
| 4218 | |
| 4219 | if (strcmp(left, "byte_order") == 0) { |
| 4220 | enum ctf_byte_order bo; |
| 4221 | |
| 4222 | if (_IS_SET(&set, _TRACE_BYTE_ORDER_SET)) { |
| 4223 | _BT_COMP_LOGE_DUP_ATTR(node, "byte_order", |
| 4224 | "trace"); |
| 4225 | ret = -EPERM; |
| 4226 | goto error; |
| 4227 | } |
| 4228 | |
| 4229 | _SET(&set, _TRACE_BYTE_ORDER_SET); |
| 4230 | right_node = _BT_LIST_FIRST_ENTRY( |
| 4231 | &node->u.ctf_expression.right, |
| 4232 | struct ctf_node, siblings); |
| 4233 | bo = byte_order_from_unary_expr(ctx, |
| 4234 | right_node); |
| 4235 | if (bo == CTF_BYTE_ORDER_UNKNOWN) { |
| 4236 | _BT_COMP_LOGE_NODE(node, |
| 4237 | "Invalid `byte_order` attribute in trace (`trace` block): " |
| 4238 | "expecting `le`, `be`, or `network`."); |
| 4239 | ret = -EINVAL; |
| 4240 | goto error; |
| 4241 | } else if (bo == CTF_BYTE_ORDER_DEFAULT) { |
| 4242 | _BT_COMP_LOGE_NODE(node, |
| 4243 | "Invalid `byte_order` attribute in trace (`trace` block): " |
| 4244 | "cannot be set to `native` here."); |
| 4245 | ret = -EPERM; |
| 4246 | goto error; |
| 4247 | } |
| 4248 | |
| 4249 | ctx->ctf_tc->default_byte_order = bo; |
| 4250 | } |
| 4251 | |
| 4252 | g_free(left); |
| 4253 | left = NULL; |
| 4254 | } |
| 4255 | } |
| 4256 | |
| 4257 | if (!_IS_SET(&set, _TRACE_BYTE_ORDER_SET)) { |
| 4258 | _BT_COMP_LOGE_NODE(trace_node, |
| 4259 | "Missing `byte_order` attribute in trace (`trace` block)."); |
| 4260 | ret = -EINVAL; |
| 4261 | goto error; |
| 4262 | } |
| 4263 | |
| 4264 | return 0; |
| 4265 | |
| 4266 | error: |
| 4267 | g_free(left); |
| 4268 | return ret; |
| 4269 | } |
| 4270 | |
| 4271 | static |
| 4272 | int visit_clock_decl_entry(struct ctx *ctx, struct ctf_node *entry_node, |
| 4273 | struct ctf_clock_class *clock, int *set, int64_t *offset_seconds, |
| 4274 | uint64_t *offset_cycles) |
| 4275 | { |
| 4276 | int ret = 0; |
| 4277 | char *left = NULL; |
| 4278 | |
| 4279 | if (entry_node->type != NODE_CTF_EXPRESSION) { |
| 4280 | _BT_COMP_LOGE_NODE(entry_node, |
| 4281 | "Unexpected node type: node-type=%d", |
| 4282 | entry_node->type); |
| 4283 | ret = -EPERM; |
| 4284 | goto error; |
| 4285 | } |
| 4286 | |
| 4287 | left = concatenate_unary_strings(&entry_node->u.ctf_expression.left); |
| 4288 | if (!left) { |
| 4289 | _BT_COMP_LOGE_NODE(entry_node, "Cannot concatenate unary strings."); |
| 4290 | ret = -EINVAL; |
| 4291 | goto error; |
| 4292 | } |
| 4293 | |
| 4294 | if (strcmp(left, "name") == 0) { |
| 4295 | char *right; |
| 4296 | |
| 4297 | if (_IS_SET(set, _CLOCK_NAME_SET)) { |
| 4298 | _BT_COMP_LOGE_DUP_ATTR(entry_node, "name", "clock class"); |
| 4299 | ret = -EPERM; |
| 4300 | goto error; |
| 4301 | } |
| 4302 | |
| 4303 | right = concatenate_unary_strings( |
| 4304 | &entry_node->u.ctf_expression.right); |
| 4305 | if (!right) { |
| 4306 | _BT_COMP_LOGE_NODE(entry_node, |
| 4307 | "Unexpected unary expression for clock class's `name` attribute."); |
| 4308 | ret = -EINVAL; |
| 4309 | goto error; |
| 4310 | } |
| 4311 | |
| 4312 | g_string_assign(clock->name, right); |
| 4313 | g_free(right); |
| 4314 | _SET(set, _CLOCK_NAME_SET); |
| 4315 | } else if (strcmp(left, "uuid") == 0) { |
| 4316 | bt_uuid_t uuid; |
| 4317 | |
| 4318 | if (_IS_SET(set, _CLOCK_UUID_SET)) { |
| 4319 | _BT_COMP_LOGE_DUP_ATTR(entry_node, "uuid", "clock class"); |
| 4320 | ret = -EPERM; |
| 4321 | goto error; |
| 4322 | } |
| 4323 | |
| 4324 | ret = get_unary_uuid(ctx, &entry_node->u.ctf_expression.right, |
| 4325 | uuid); |
| 4326 | if (ret) { |
| 4327 | _BT_COMP_LOGE_NODE(entry_node, |
| 4328 | "Invalid clock class's `uuid` attribute."); |
| 4329 | goto error; |
| 4330 | } |
| 4331 | |
| 4332 | clock->has_uuid = true; |
| 4333 | bt_uuid_copy(clock->uuid, uuid); |
| 4334 | _SET(set, _CLOCK_UUID_SET); |
| 4335 | } else if (strcmp(left, "description") == 0) { |
| 4336 | char *right; |
| 4337 | |
| 4338 | if (_IS_SET(set, _CLOCK_DESCRIPTION_SET)) { |
| 4339 | _BT_COMP_LOGE_DUP_ATTR(entry_node, "description", |
| 4340 | "clock class"); |
| 4341 | ret = -EPERM; |
| 4342 | goto error; |
| 4343 | } |
| 4344 | |
| 4345 | right = concatenate_unary_strings( |
| 4346 | &entry_node->u.ctf_expression.right); |
| 4347 | if (!right) { |
| 4348 | _BT_COMP_LOGE_NODE(entry_node, |
| 4349 | "Unexpected unary expression for clock class's `description` attribute."); |
| 4350 | ret = -EINVAL; |
| 4351 | goto error; |
| 4352 | } |
| 4353 | |
| 4354 | g_string_assign(clock->description, right); |
| 4355 | g_free(right); |
| 4356 | _SET(set, _CLOCK_DESCRIPTION_SET); |
| 4357 | } else if (strcmp(left, "freq") == 0) { |
| 4358 | uint64_t freq = UINT64_C(-1); |
| 4359 | |
| 4360 | if (_IS_SET(set, _CLOCK_FREQ_SET)) { |
| 4361 | _BT_COMP_LOGE_DUP_ATTR(entry_node, "freq", "clock class"); |
| 4362 | ret = -EPERM; |
| 4363 | goto error; |
| 4364 | } |
| 4365 | |
| 4366 | ret = get_unary_unsigned(ctx, |
| 4367 | &entry_node->u.ctf_expression.right, &freq); |
| 4368 | if (ret) { |
| 4369 | _BT_COMP_LOGE_NODE(entry_node, |
| 4370 | "Unexpected unary expression for clock class's `freq` attribute."); |
| 4371 | ret = -EINVAL; |
| 4372 | goto error; |
| 4373 | } |
| 4374 | |
| 4375 | if (freq == UINT64_C(-1) || freq == 0) { |
| 4376 | _BT_COMP_LOGE_NODE(entry_node, |
| 4377 | "Invalid clock class frequency: freq=%" PRIu64, |
| 4378 | freq); |
| 4379 | ret = -EINVAL; |
| 4380 | goto error; |
| 4381 | } |
| 4382 | |
| 4383 | clock->frequency = freq; |
| 4384 | _SET(set, _CLOCK_FREQ_SET); |
| 4385 | } else if (strcmp(left, "precision") == 0) { |
| 4386 | uint64_t precision; |
| 4387 | |
| 4388 | if (_IS_SET(set, _CLOCK_PRECISION_SET)) { |
| 4389 | _BT_COMP_LOGE_DUP_ATTR(entry_node, "precision", |
| 4390 | "clock class"); |
| 4391 | ret = -EPERM; |
| 4392 | goto error; |
| 4393 | } |
| 4394 | |
| 4395 | ret = get_unary_unsigned(ctx, |
| 4396 | &entry_node->u.ctf_expression.right, &precision); |
| 4397 | if (ret) { |
| 4398 | _BT_COMP_LOGE_NODE(entry_node, |
| 4399 | "Unexpected unary expression for clock class's `precision` attribute."); |
| 4400 | ret = -EINVAL; |
| 4401 | goto error; |
| 4402 | } |
| 4403 | |
| 4404 | clock->precision = precision; |
| 4405 | _SET(set, _CLOCK_PRECISION_SET); |
| 4406 | } else if (strcmp(left, "offset_s") == 0) { |
| 4407 | if (_IS_SET(set, _CLOCK_OFFSET_S_SET)) { |
| 4408 | _BT_COMP_LOGE_DUP_ATTR(entry_node, "offset_s", |
| 4409 | "clock class"); |
| 4410 | ret = -EPERM; |
| 4411 | goto error; |
| 4412 | } |
| 4413 | |
| 4414 | ret = get_unary_signed( |
| 4415 | &entry_node->u.ctf_expression.right, offset_seconds); |
| 4416 | if (ret) { |
| 4417 | _BT_COMP_LOGE_NODE(entry_node, |
| 4418 | "Unexpected unary expression for clock class's `offset_s` attribute."); |
| 4419 | ret = -EINVAL; |
| 4420 | goto error; |
| 4421 | } |
| 4422 | |
| 4423 | _SET(set, _CLOCK_OFFSET_S_SET); |
| 4424 | } else if (strcmp(left, "offset") == 0) { |
| 4425 | if (_IS_SET(set, _CLOCK_OFFSET_SET)) { |
| 4426 | _BT_COMP_LOGE_DUP_ATTR(entry_node, "offset", "clock class"); |
| 4427 | ret = -EPERM; |
| 4428 | goto error; |
| 4429 | } |
| 4430 | |
| 4431 | ret = get_unary_unsigned(ctx, |
| 4432 | &entry_node->u.ctf_expression.right, offset_cycles); |
| 4433 | if (ret) { |
| 4434 | _BT_COMP_LOGE_NODE(entry_node, |
| 4435 | "Unexpected unary expression for clock class's `offset` attribute."); |
| 4436 | ret = -EINVAL; |
| 4437 | goto error; |
| 4438 | } |
| 4439 | |
| 4440 | _SET(set, _CLOCK_OFFSET_SET); |
| 4441 | } else if (strcmp(left, "absolute") == 0) { |
| 4442 | struct ctf_node *right; |
| 4443 | |
| 4444 | if (_IS_SET(set, _CLOCK_ABSOLUTE_SET)) { |
| 4445 | _BT_COMP_LOGE_DUP_ATTR(entry_node, "absolute", |
| 4446 | "clock class"); |
| 4447 | ret = -EPERM; |
| 4448 | goto error; |
| 4449 | } |
| 4450 | |
| 4451 | right = _BT_LIST_FIRST_ENTRY( |
| 4452 | &entry_node->u.ctf_expression.right, |
| 4453 | struct ctf_node, siblings); |
| 4454 | ret = get_boolean(ctx, right); |
| 4455 | if (ret < 0) { |
| 4456 | _BT_COMP_LOGE_NODE(entry_node, |
| 4457 | "Unexpected unary expression for clock class's `absolute` attribute."); |
| 4458 | ret = -EINVAL; |
| 4459 | goto error; |
| 4460 | } |
| 4461 | |
| 4462 | clock->is_absolute = ret; |
| 4463 | _SET(set, _CLOCK_ABSOLUTE_SET); |
| 4464 | } else { |
| 4465 | _BT_COMP_LOGW_NODE(entry_node, |
| 4466 | "Unknown attribute in clock class: attr-name=\"%s\"", |
| 4467 | left); |
| 4468 | } |
| 4469 | |
| 4470 | g_free(left); |
| 4471 | left = NULL; |
| 4472 | return 0; |
| 4473 | |
| 4474 | error: |
| 4475 | g_free(left); |
| 4476 | return ret; |
| 4477 | } |
| 4478 | |
| 4479 | static inline |
| 4480 | uint64_t cycles_from_ns(uint64_t frequency, uint64_t ns) |
| 4481 | { |
| 4482 | uint64_t cycles; |
| 4483 | |
| 4484 | /* 1GHz */ |
| 4485 | if (frequency == UINT64_C(1000000000)) { |
| 4486 | cycles = ns; |
| 4487 | } else { |
| 4488 | cycles = (uint64_t) (((double) ns * (double) frequency) / 1e9); |
| 4489 | } |
| 4490 | |
| 4491 | return cycles; |
| 4492 | } |
| 4493 | |
| 4494 | static |
| 4495 | void calibrate_clock_class_offsets(int64_t *offset_seconds, |
| 4496 | uint64_t *offset_cycles, uint64_t freq) |
| 4497 | { |
| 4498 | if (*offset_cycles >= freq) { |
| 4499 | const uint64_t s_in_offset_cycles = *offset_cycles / freq; |
| 4500 | |
| 4501 | *offset_seconds += (int64_t) s_in_offset_cycles; |
| 4502 | *offset_cycles -= (s_in_offset_cycles * freq); |
| 4503 | } |
| 4504 | } |
| 4505 | |
| 4506 | static |
| 4507 | void apply_clock_class_offset(struct ctx *ctx, |
| 4508 | struct ctf_clock_class *clock) |
| 4509 | { |
| 4510 | uint64_t freq; |
| 4511 | int64_t offset_s_to_apply = ctx->decoder_config.clock_class_offset_s; |
| 4512 | uint64_t offset_ns_to_apply; |
| 4513 | int64_t cur_offset_s; |
| 4514 | uint64_t cur_offset_cycles; |
| 4515 | |
| 4516 | if (ctx->decoder_config.clock_class_offset_s == 0 && |
| 4517 | ctx->decoder_config.clock_class_offset_ns == 0) { |
| 4518 | goto end; |
| 4519 | } |
| 4520 | |
| 4521 | /* Transfer nanoseconds to seconds as much as possible */ |
| 4522 | if (ctx->decoder_config.clock_class_offset_ns < 0) { |
| 4523 | const int64_t abs_ns = -ctx->decoder_config.clock_class_offset_ns; |
| 4524 | const int64_t abs_extra_s = abs_ns / INT64_C(1000000000) + 1; |
| 4525 | const int64_t extra_s = -abs_extra_s; |
| 4526 | const int64_t offset_ns = ctx->decoder_config.clock_class_offset_ns - |
| 4527 | (extra_s * INT64_C(1000000000)); |
| 4528 | |
| 4529 | BT_ASSERT(offset_ns > 0); |
| 4530 | offset_ns_to_apply = (uint64_t) offset_ns; |
| 4531 | offset_s_to_apply += extra_s; |
| 4532 | } else { |
| 4533 | const int64_t extra_s = ctx->decoder_config.clock_class_offset_ns / |
| 4534 | INT64_C(1000000000); |
| 4535 | const int64_t offset_ns = ctx->decoder_config.clock_class_offset_ns - |
| 4536 | (extra_s * INT64_C(1000000000)); |
| 4537 | |
| 4538 | BT_ASSERT(offset_ns >= 0); |
| 4539 | offset_ns_to_apply = (uint64_t) offset_ns; |
| 4540 | offset_s_to_apply += extra_s; |
| 4541 | } |
| 4542 | |
| 4543 | freq = clock->frequency; |
| 4544 | cur_offset_s = clock->offset_seconds; |
| 4545 | cur_offset_cycles = clock->offset_cycles; |
| 4546 | |
| 4547 | /* Apply offsets */ |
| 4548 | cur_offset_s += offset_s_to_apply; |
| 4549 | cur_offset_cycles += cycles_from_ns(freq, offset_ns_to_apply); |
| 4550 | |
| 4551 | /* |
| 4552 | * Recalibrate offsets because the part in cycles can be greater |
| 4553 | * than the frequency at this point. |
| 4554 | */ |
| 4555 | calibrate_clock_class_offsets(&cur_offset_s, &cur_offset_cycles, freq); |
| 4556 | |
| 4557 | /* Set final offsets */ |
| 4558 | clock->offset_seconds = cur_offset_s; |
| 4559 | clock->offset_cycles = cur_offset_cycles; |
| 4560 | |
| 4561 | end: |
| 4562 | return; |
| 4563 | } |
| 4564 | |
| 4565 | static |
| 4566 | int visit_clock_decl(struct ctx *ctx, struct ctf_node *clock_node) |
| 4567 | { |
| 4568 | int ret = 0; |
| 4569 | int set = 0; |
| 4570 | struct ctf_clock_class *clock; |
| 4571 | struct ctf_node *entry_node; |
| 4572 | struct bt_list_head *decl_list = &clock_node->u.clock.declaration_list; |
| 4573 | const char *clock_class_name; |
| 4574 | int64_t offset_seconds = 0; |
| 4575 | uint64_t offset_cycles = 0; |
| 4576 | uint64_t freq; |
| 4577 | |
| 4578 | if (clock_node->visited) { |
| 4579 | return 0; |
| 4580 | } |
| 4581 | |
| 4582 | clock_node->visited = TRUE; |
| 4583 | |
| 4584 | /* CTF 1.8's default frequency for a clock class is 1 GHz */ |
| 4585 | clock = ctf_clock_class_create(); |
| 4586 | if (!clock) { |
| 4587 | _BT_COMP_LOGE_NODE(clock_node, |
| 4588 | "Cannot create default clock class."); |
| 4589 | ret = -ENOMEM; |
| 4590 | goto end; |
| 4591 | } |
| 4592 | |
| 4593 | bt_list_for_each_entry(entry_node, decl_list, siblings) { |
| 4594 | ret = visit_clock_decl_entry(ctx, entry_node, clock, &set, |
| 4595 | &offset_seconds, &offset_cycles); |
| 4596 | if (ret) { |
| 4597 | _BT_COMP_LOGE_NODE(entry_node, |
| 4598 | "Cannot visit clock class's entry: ret=%d", |
| 4599 | ret); |
| 4600 | goto end; |
| 4601 | } |
| 4602 | } |
| 4603 | |
| 4604 | if (!_IS_SET(&set, _CLOCK_NAME_SET)) { |
| 4605 | _BT_COMP_LOGE_NODE(clock_node, |
| 4606 | "Missing `name` attribute in clock class."); |
| 4607 | ret = -EPERM; |
| 4608 | goto end; |
| 4609 | } |
| 4610 | |
| 4611 | clock_class_name = clock->name->str; |
| 4612 | BT_ASSERT(clock_class_name); |
| 4613 | if (ctx->is_lttng && strcmp(clock_class_name, "monotonic") == 0) { |
| 4614 | /* |
| 4615 | * Old versions of LTTng forgot to set its clock class |
| 4616 | * as absolute, even if it is. This is important because |
| 4617 | * it's a condition to be able to sort messages |
| 4618 | * from different sources. |
| 4619 | */ |
| 4620 | clock->is_absolute = true; |
| 4621 | } |
| 4622 | |
| 4623 | /* |
| 4624 | * Adjust offsets so that the part in cycles is less than the |
| 4625 | * frequency (move to the part in seconds). |
| 4626 | */ |
| 4627 | freq = clock->frequency; |
| 4628 | calibrate_clock_class_offsets(&offset_seconds, &offset_cycles, freq); |
| 4629 | BT_ASSERT(offset_cycles < clock->frequency); |
| 4630 | clock->offset_seconds = offset_seconds; |
| 4631 | clock->offset_cycles = offset_cycles; |
| 4632 | apply_clock_class_offset(ctx, clock); |
| 4633 | g_ptr_array_add(ctx->ctf_tc->clock_classes, clock); |
| 4634 | clock = NULL; |
| 4635 | |
| 4636 | end: |
| 4637 | if (clock) { |
| 4638 | ctf_clock_class_destroy(clock); |
| 4639 | } |
| 4640 | |
| 4641 | return ret; |
| 4642 | } |
| 4643 | |
| 4644 | static |
| 4645 | int visit_root_decl(struct ctx *ctx, struct ctf_node *root_decl_node) |
| 4646 | { |
| 4647 | int ret = 0; |
| 4648 | |
| 4649 | if (root_decl_node->visited) { |
| 4650 | goto end; |
| 4651 | } |
| 4652 | |
| 4653 | root_decl_node->visited = TRUE; |
| 4654 | |
| 4655 | switch (root_decl_node->type) { |
| 4656 | case NODE_TYPEDEF: |
| 4657 | ret = visit_field_class_def(ctx, |
| 4658 | root_decl_node->u.field_class_def.field_class_specifier_list, |
| 4659 | &root_decl_node->u.field_class_def.field_class_declarators); |
| 4660 | if (ret) { |
| 4661 | _BT_COMP_LOGE_NODE(root_decl_node, |
| 4662 | "Cannot add field class found in root scope."); |
| 4663 | goto end; |
| 4664 | } |
| 4665 | break; |
| 4666 | case NODE_TYPEALIAS: |
| 4667 | ret = visit_field_class_alias(ctx, root_decl_node->u.field_class_alias.target, |
| 4668 | root_decl_node->u.field_class_alias.alias); |
| 4669 | if (ret) { |
| 4670 | _BT_COMP_LOGE_NODE(root_decl_node, |
| 4671 | "Cannot add field class alias found in root scope."); |
| 4672 | goto end; |
| 4673 | } |
| 4674 | break; |
| 4675 | case NODE_TYPE_SPECIFIER_LIST: |
| 4676 | { |
| 4677 | struct ctf_field_class *decl = NULL; |
| 4678 | |
| 4679 | /* |
| 4680 | * Just add the field class specifier to the root |
| 4681 | * declaration scope. Put local reference. |
| 4682 | */ |
| 4683 | ret = visit_field_class_specifier_list(ctx, root_decl_node, &decl); |
| 4684 | if (ret) { |
| 4685 | _BT_COMP_LOGE_NODE(root_decl_node, |
| 4686 | "Cannot visit root scope's field class: " |
| 4687 | "ret=%d", ret); |
| 4688 | BT_ASSERT(!decl); |
| 4689 | goto end; |
| 4690 | } |
| 4691 | |
| 4692 | ctf_field_class_destroy(decl); |
| 4693 | decl = NULL; |
| 4694 | break; |
| 4695 | } |
| 4696 | default: |
| 4697 | _BT_COMP_LOGE_NODE(root_decl_node, |
| 4698 | "Unexpected node type: node-type=%d", |
| 4699 | root_decl_node->type); |
| 4700 | ret = -EPERM; |
| 4701 | goto end; |
| 4702 | } |
| 4703 | |
| 4704 | end: |
| 4705 | return ret; |
| 4706 | } |
| 4707 | |
| 4708 | BT_HIDDEN |
| 4709 | struct ctf_visitor_generate_ir *ctf_visitor_generate_ir_create( |
| 4710 | const struct ctf_metadata_decoder_config *decoder_config) |
| 4711 | { |
| 4712 | struct ctx *ctx = NULL; |
| 4713 | |
| 4714 | /* Create visitor's context */ |
| 4715 | ctx = ctx_create(decoder_config); |
| 4716 | if (!ctx) { |
| 4717 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, decoder_config->log_level, |
| 4718 | decoder_config->self_comp, |
| 4719 | "Cannot create visitor's context."); |
| 4720 | goto error; |
| 4721 | } |
| 4722 | |
| 4723 | goto end; |
| 4724 | |
| 4725 | error: |
| 4726 | ctx_destroy(ctx); |
| 4727 | ctx = NULL; |
| 4728 | |
| 4729 | end: |
| 4730 | return (void *) ctx; |
| 4731 | } |
| 4732 | |
| 4733 | BT_HIDDEN |
| 4734 | void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor) |
| 4735 | { |
| 4736 | ctx_destroy((void *) visitor); |
| 4737 | } |
| 4738 | |
| 4739 | BT_HIDDEN |
| 4740 | bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class( |
| 4741 | struct ctf_visitor_generate_ir *visitor) |
| 4742 | { |
| 4743 | struct ctx *ctx = (void *) visitor; |
| 4744 | |
| 4745 | BT_ASSERT(ctx); |
| 4746 | |
| 4747 | if (ctx->trace_class) { |
| 4748 | bt_trace_class_get_ref(ctx->trace_class); |
| 4749 | } |
| 4750 | |
| 4751 | return ctx->trace_class; |
| 4752 | } |
| 4753 | |
| 4754 | BT_HIDDEN |
| 4755 | struct ctf_trace_class *ctf_visitor_generate_ir_borrow_ctf_trace_class( |
| 4756 | struct ctf_visitor_generate_ir *visitor) |
| 4757 | { |
| 4758 | struct ctx *ctx = (void *) visitor; |
| 4759 | |
| 4760 | BT_ASSERT(ctx); |
| 4761 | BT_ASSERT(ctx->ctf_tc); |
| 4762 | return ctx->ctf_tc; |
| 4763 | } |
| 4764 | |
| 4765 | BT_HIDDEN |
| 4766 | int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor, |
| 4767 | struct ctf_node *node) |
| 4768 | { |
| 4769 | int ret = 0; |
| 4770 | struct ctx *ctx = (void *) visitor; |
| 4771 | |
| 4772 | BT_COMP_LOGI_STR("Visiting metadata's AST to generate CTF IR objects."); |
| 4773 | |
| 4774 | switch (node->type) { |
| 4775 | case NODE_ROOT: |
| 4776 | { |
| 4777 | struct ctf_node *iter; |
| 4778 | bool got_trace_decl = false; |
| 4779 | |
| 4780 | /* |
| 4781 | * The first thing we need is the native byte order of |
| 4782 | * the trace block, because early class aliases can have |
| 4783 | * a `byte_order` attribute set to `native`. If we don't |
| 4784 | * have the native byte order yet, and we don't have any |
| 4785 | * trace block yet, then fail with EINCOMPLETE. |
| 4786 | */ |
| 4787 | if (ctx->ctf_tc->default_byte_order == CTF_BYTE_ORDER_UNKNOWN) { |
| 4788 | bt_list_for_each_entry(iter, &node->u.root.trace, siblings) { |
| 4789 | if (got_trace_decl) { |
| 4790 | _BT_COMP_LOGE_NODE(node, |
| 4791 | "Duplicate trace (`trace` block)."); |
| 4792 | ret = -1; |
| 4793 | goto end; |
| 4794 | } |
| 4795 | |
| 4796 | ret = set_trace_byte_order(ctx, iter); |
| 4797 | if (ret) { |
| 4798 | _BT_COMP_LOGE_NODE(node, |
| 4799 | "Cannot set trace's native byte order: " |
| 4800 | "ret=%d", ret); |
| 4801 | goto end; |
| 4802 | } |
| 4803 | |
| 4804 | got_trace_decl = true; |
| 4805 | } |
| 4806 | |
| 4807 | if (!got_trace_decl) { |
| 4808 | BT_COMP_LOGD_STR("Incomplete AST: need trace (`trace` block)."); |
| 4809 | ret = -EINCOMPLETE; |
| 4810 | goto end; |
| 4811 | } |
| 4812 | } |
| 4813 | |
| 4814 | BT_ASSERT(ctx->ctf_tc->default_byte_order == CTF_BYTE_ORDER_LITTLE || |
| 4815 | ctx->ctf_tc->default_byte_order == CTF_BYTE_ORDER_BIG); |
| 4816 | BT_ASSERT(ctx->current_scope && |
| 4817 | !ctx->current_scope->parent_scope); |
| 4818 | |
| 4819 | /* Environment */ |
| 4820 | bt_list_for_each_entry(iter, &node->u.root.env, siblings) { |
| 4821 | ret = visit_env(ctx, iter); |
| 4822 | if (ret) { |
| 4823 | _BT_COMP_LOGE_NODE(iter, |
| 4824 | "Cannot visit trace's environment (`env` block) entry: " |
| 4825 | "ret=%d", ret); |
| 4826 | goto end; |
| 4827 | } |
| 4828 | } |
| 4829 | |
| 4830 | BT_ASSERT(ctx->current_scope && |
| 4831 | !ctx->current_scope->parent_scope); |
| 4832 | |
| 4833 | /* |
| 4834 | * Visit clock blocks. |
| 4835 | */ |
| 4836 | bt_list_for_each_entry(iter, &node->u.root.clock, siblings) { |
| 4837 | ret = visit_clock_decl(ctx, iter); |
| 4838 | if (ret) { |
| 4839 | _BT_COMP_LOGE_NODE(iter, |
| 4840 | "Cannot visit clock class: ret=%d", |
| 4841 | ret); |
| 4842 | goto end; |
| 4843 | } |
| 4844 | } |
| 4845 | |
| 4846 | BT_ASSERT(ctx->current_scope && |
| 4847 | !ctx->current_scope->parent_scope); |
| 4848 | |
| 4849 | /* |
| 4850 | * Visit root declarations next, as they can be used by any |
| 4851 | * following entity. |
| 4852 | */ |
| 4853 | bt_list_for_each_entry(iter, &node->u.root.declaration_list, |
| 4854 | siblings) { |
| 4855 | ret = visit_root_decl(ctx, iter); |
| 4856 | if (ret) { |
| 4857 | _BT_COMP_LOGE_NODE(iter, |
| 4858 | "Cannot visit root entry: ret=%d", |
| 4859 | ret); |
| 4860 | goto end; |
| 4861 | } |
| 4862 | } |
| 4863 | |
| 4864 | BT_ASSERT(ctx->current_scope && |
| 4865 | !ctx->current_scope->parent_scope); |
| 4866 | |
| 4867 | /* Callsite blocks are not supported */ |
| 4868 | bt_list_for_each_entry(iter, &node->u.root.callsite, siblings) { |
| 4869 | _BT_COMP_LOGW_NODE(iter, |
| 4870 | "\"callsite\" blocks are not supported as of this version."); |
| 4871 | } |
| 4872 | |
| 4873 | BT_ASSERT(ctx->current_scope && |
| 4874 | !ctx->current_scope->parent_scope); |
| 4875 | |
| 4876 | /* Trace */ |
| 4877 | bt_list_for_each_entry(iter, &node->u.root.trace, siblings) { |
| 4878 | ret = visit_trace_decl(ctx, iter); |
| 4879 | if (ret) { |
| 4880 | _BT_COMP_LOGE_NODE(iter, |
| 4881 | "Cannot visit trace (`trace` block): " |
| 4882 | "ret=%d", ret); |
| 4883 | goto end; |
| 4884 | } |
| 4885 | } |
| 4886 | |
| 4887 | BT_ASSERT(ctx->current_scope && |
| 4888 | !ctx->current_scope->parent_scope); |
| 4889 | |
| 4890 | /* Streams */ |
| 4891 | bt_list_for_each_entry(iter, &node->u.root.stream, siblings) { |
| 4892 | ret = visit_stream_decl(ctx, iter); |
| 4893 | if (ret) { |
| 4894 | _BT_COMP_LOGE_NODE(iter, |
| 4895 | "Cannot visit stream class: ret=%d", |
| 4896 | ret); |
| 4897 | goto end; |
| 4898 | } |
| 4899 | } |
| 4900 | |
| 4901 | BT_ASSERT(ctx->current_scope && |
| 4902 | !ctx->current_scope->parent_scope); |
| 4903 | |
| 4904 | /* Events */ |
| 4905 | bt_list_for_each_entry(iter, &node->u.root.event, siblings) { |
| 4906 | ret = visit_event_decl(ctx, iter); |
| 4907 | if (ret) { |
| 4908 | _BT_COMP_LOGE_NODE(iter, |
| 4909 | "Cannot visit event class: ret=%d", |
| 4910 | ret); |
| 4911 | goto end; |
| 4912 | } |
| 4913 | } |
| 4914 | |
| 4915 | BT_ASSERT(ctx->current_scope && |
| 4916 | !ctx->current_scope->parent_scope); |
| 4917 | break; |
| 4918 | } |
| 4919 | default: |
| 4920 | _BT_COMP_LOGE_NODE(node, |
| 4921 | "Unexpected node type: node-type=%d", |
| 4922 | node->type); |
| 4923 | ret = -EINVAL; |
| 4924 | goto end; |
| 4925 | } |
| 4926 | |
| 4927 | /* Update default clock classes */ |
| 4928 | ret = ctf_trace_class_update_default_clock_classes(ctx->ctf_tc, |
| 4929 | &ctx->log_cfg); |
| 4930 | if (ret) { |
| 4931 | ret = -EINVAL; |
| 4932 | goto end; |
| 4933 | } |
| 4934 | |
| 4935 | /* Update trace class meanings */ |
| 4936 | ret = ctf_trace_class_update_meanings(ctx->ctf_tc); |
| 4937 | if (ret) { |
| 4938 | ret = -EINVAL; |
| 4939 | goto end; |
| 4940 | } |
| 4941 | |
| 4942 | /* Update stream class configuration */ |
| 4943 | ret = ctf_trace_class_update_stream_class_config(ctx->ctf_tc); |
| 4944 | if (ret) { |
| 4945 | ret = -EINVAL; |
| 4946 | goto end; |
| 4947 | } |
| 4948 | |
| 4949 | /* Update text arrays and sequences */ |
| 4950 | ret = ctf_trace_class_update_text_array_sequence(ctx->ctf_tc); |
| 4951 | if (ret) { |
| 4952 | ret = -EINVAL; |
| 4953 | goto end; |
| 4954 | } |
| 4955 | |
| 4956 | /* Resolve sequence lengths and variant tags */ |
| 4957 | ret = ctf_trace_class_resolve_field_classes(ctx->ctf_tc, &ctx->log_cfg); |
| 4958 | if (ret) { |
| 4959 | ret = -EINVAL; |
| 4960 | goto end; |
| 4961 | } |
| 4962 | |
| 4963 | if (ctx->trace_class) { |
| 4964 | /* |
| 4965 | * Update "in IR" for field classes. |
| 4966 | * |
| 4967 | * If we have no IR trace class, then we'll have no way |
| 4968 | * to create IR fields anyway, so we leave all the |
| 4969 | * `in_ir` members false. |
| 4970 | */ |
| 4971 | ret = ctf_trace_class_update_in_ir(ctx->ctf_tc); |
| 4972 | if (ret) { |
| 4973 | ret = -EINVAL; |
| 4974 | goto end; |
| 4975 | } |
| 4976 | } |
| 4977 | |
| 4978 | /* Update saved value indexes */ |
| 4979 | ret = ctf_trace_class_update_value_storing_indexes(ctx->ctf_tc); |
| 4980 | if (ret) { |
| 4981 | ret = -EINVAL; |
| 4982 | goto end; |
| 4983 | } |
| 4984 | |
| 4985 | /* Validate what we have so far */ |
| 4986 | ret = ctf_trace_class_validate(ctx->ctf_tc, &ctx->log_cfg); |
| 4987 | if (ret) { |
| 4988 | ret = -EINVAL; |
| 4989 | goto end; |
| 4990 | } |
| 4991 | |
| 4992 | /* |
| 4993 | * If there are fields which are not related to the CTF format |
| 4994 | * itself in the packet header and in event header field |
| 4995 | * classes, warn about it because they are never translated. |
| 4996 | */ |
| 4997 | ctf_trace_class_warn_meaningless_header_fields(ctx->ctf_tc, |
| 4998 | &ctx->log_cfg); |
| 4999 | |
| 5000 | if (ctx->trace_class) { |
| 5001 | /* Copy new CTF metadata -> new IR metadata */ |
| 5002 | ret = ctf_trace_class_translate(ctx->log_cfg.self_comp, |
| 5003 | ctx->trace_class, ctx->ctf_tc); |
| 5004 | if (ret) { |
| 5005 | ret = -EINVAL; |
| 5006 | goto end; |
| 5007 | } |
| 5008 | } |
| 5009 | |
| 5010 | end: |
| 5011 | return ret; |
| 5012 | } |