| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #define BT_COMP_LOG_SELF_COMP (ctx->self_comp) |
| 9 | #define BT_COMP_LOG_SELF_COMP_CLASS (ctx->self_comp_class) |
| 10 | #define BT_LOG_OUTPUT_LEVEL (ctx->log_level) |
| 11 | #define BT_LOG_TAG "PLUGIN/CTF/META/RESOLVE" |
| 12 | #include "logging/comp-logging.h" |
| 13 | |
| 14 | #include <babeltrace2/babeltrace.h> |
| 15 | #include "common/macros.h" |
| 16 | #include "common/assert.h" |
| 17 | #include "common/common.h" |
| 18 | #include <glib.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <stdint.h> |
| 21 | #include <string.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <limits.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <glib.h> |
| 26 | |
| 27 | #include "ctf-meta-visitors.hpp" |
| 28 | #include "logging.hpp" |
| 29 | |
| 30 | using field_class_stack_t = GPtrArray; |
| 31 | |
| 32 | /* |
| 33 | * A stack frame. |
| 34 | * |
| 35 | * `fc` contains a compound field class (structure, variant, array, |
| 36 | * or sequence) and `index` indicates the index of the field class in |
| 37 | * the upper frame (-1 for array and sequence field classes). `name` |
| 38 | * indicates the name of the field class in the upper frame (empty |
| 39 | * string for array and sequence field classes). |
| 40 | */ |
| 41 | struct field_class_stack_frame |
| 42 | { |
| 43 | struct ctf_field_class *fc; |
| 44 | int64_t index; |
| 45 | }; |
| 46 | |
| 47 | /* |
| 48 | * The current context of the resolving engine. |
| 49 | */ |
| 50 | struct resolve_context |
| 51 | { |
| 52 | bt_logging_level log_level; |
| 53 | |
| 54 | /* Weak, exactly one of these must be set */ |
| 55 | bt_self_component *self_comp; |
| 56 | bt_self_component_class *self_comp_class; |
| 57 | |
| 58 | struct ctf_trace_class *tc; |
| 59 | struct ctf_stream_class *sc; |
| 60 | struct ctf_event_class *ec; |
| 61 | |
| 62 | struct |
| 63 | { |
| 64 | struct ctf_field_class *packet_header; |
| 65 | struct ctf_field_class *packet_context; |
| 66 | struct ctf_field_class *event_header; |
| 67 | struct ctf_field_class *event_common_context; |
| 68 | struct ctf_field_class *event_spec_context; |
| 69 | struct ctf_field_class *event_payload; |
| 70 | } scopes; |
| 71 | |
| 72 | /* Root scope being visited */ |
| 73 | enum ctf_scope root_scope; |
| 74 | field_class_stack_t *field_class_stack; |
| 75 | struct ctf_field_class *cur_fc; |
| 76 | }; |
| 77 | |
| 78 | /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */ |
| 79 | static const char * const absolute_path_prefixes[] = { |
| 80 | /* CTF_SCOPE_PACKET_HEADER */ "trace.packet.header.", |
| 81 | /* CTF_SCOPE_PACKET_CONTEXT */ "stream.packet.context.", |
| 82 | /* CTF_SCOPE_EVENT_HEADER */ "stream.event.header.", |
| 83 | /* CTF_SCOPE_EVENT_COMMON_CONTEXT */ "stream.event.context.", |
| 84 | /* CTF_SCOPE_EVENT_SPECIFIC_CONTEXT */ "event.context.", |
| 85 | /* CTF_SCOPE_EVENT_PAYLOAD */ "event.fields.", |
| 86 | }; |
| 87 | |
| 88 | /* Number of path tokens used for the absolute prefixes */ |
| 89 | static const uint64_t absolute_path_prefix_ptoken_counts[] = { |
| 90 | /* CTF_SCOPE_PACKET_HEADER */ 3, |
| 91 | /* CTF_SCOPE_PACKET_CONTEXT */ 3, |
| 92 | /* CTF_SCOPE_EVENT_HEADER */ 3, |
| 93 | /* CTF_SCOPE_EVENT_COMMON_CONTEXT */ 3, |
| 94 | /* CTF_SCOPE_EVENT_SPECIFIC_CONTEXT */ 2, |
| 95 | /* CTF_SCOPE_EVENT_PAYLOAD */ 2, |
| 96 | }; |
| 97 | |
| 98 | static void destroy_field_class_stack_frame(struct field_class_stack_frame *frame) |
| 99 | { |
| 100 | if (!frame) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | g_free(frame); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Creates a class stack. |
| 109 | */ |
| 110 | static field_class_stack_t *field_class_stack_create(void) |
| 111 | { |
| 112 | return g_ptr_array_new_with_free_func((GDestroyNotify) destroy_field_class_stack_frame); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Destroys a class stack. |
| 117 | */ |
| 118 | static void field_class_stack_destroy(field_class_stack_t *stack) |
| 119 | { |
| 120 | if (stack) { |
| 121 | g_ptr_array_free(stack, TRUE); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Pushes a field class onto a class stack. |
| 127 | */ |
| 128 | static int field_class_stack_push(field_class_stack_t *stack, struct ctf_field_class *fc, |
| 129 | struct resolve_context *ctx) |
| 130 | { |
| 131 | int ret = 0; |
| 132 | struct field_class_stack_frame *frame = NULL; |
| 133 | |
| 134 | if (!stack || !fc) { |
| 135 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 136 | "Invalid parameter: stack or field class is `NULL`."); |
| 137 | ret = -1; |
| 138 | goto end; |
| 139 | } |
| 140 | |
| 141 | frame = g_new0(struct field_class_stack_frame, 1); |
| 142 | if (!frame) { |
| 143 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Failed to allocate one field class stack frame."); |
| 144 | ret = -1; |
| 145 | goto end; |
| 146 | } |
| 147 | |
| 148 | BT_COMP_LOGD("Pushing field class on context's stack: " |
| 149 | "fc-addr=%p, stack-size-before=%u", |
| 150 | fc, stack->len); |
| 151 | frame->fc = fc; |
| 152 | g_ptr_array_add(stack, frame); |
| 153 | |
| 154 | end: |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Checks whether or not `stack` is empty. |
| 160 | */ |
| 161 | static bool field_class_stack_empty(field_class_stack_t *stack) |
| 162 | { |
| 163 | return stack->len == 0; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Returns the number of frames in `stack`. |
| 168 | */ |
| 169 | static size_t field_class_stack_size(field_class_stack_t *stack) |
| 170 | { |
| 171 | return stack->len; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Returns the top frame of `stack`. |
| 176 | */ |
| 177 | static struct field_class_stack_frame *field_class_stack_peek(field_class_stack_t *stack) |
| 178 | { |
| 179 | BT_ASSERT(stack); |
| 180 | BT_ASSERT(!field_class_stack_empty(stack)); |
| 181 | |
| 182 | return (field_class_stack_frame *) g_ptr_array_index(stack, stack->len - 1); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Returns the frame at index `index` in `stack`. |
| 187 | */ |
| 188 | static struct field_class_stack_frame *field_class_stack_at(field_class_stack_t *stack, |
| 189 | size_t index) |
| 190 | { |
| 191 | BT_ASSERT(stack); |
| 192 | BT_ASSERT(index < stack->len); |
| 193 | |
| 194 | return (field_class_stack_frame *) g_ptr_array_index(stack, index); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Removes the top frame of `stack`. |
| 199 | */ |
| 200 | static void field_class_stack_pop(field_class_stack_t *stack, struct resolve_context *ctx) |
| 201 | { |
| 202 | if (!field_class_stack_empty(stack)) { |
| 203 | /* |
| 204 | * This will call the frame's destructor and free it, as |
| 205 | * well as put its contained field class. |
| 206 | */ |
| 207 | BT_COMP_LOGD("Popping context's stack: stack-size-before=%u", stack->len); |
| 208 | g_ptr_array_set_size(stack, stack->len - 1); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Returns the scope field class of `scope` in the context `ctx`. |
| 214 | */ |
| 215 | static struct ctf_field_class *borrow_class_from_ctx(struct resolve_context *ctx, |
| 216 | enum ctf_scope scope) |
| 217 | { |
| 218 | switch (scope) { |
| 219 | case CTF_SCOPE_PACKET_HEADER: |
| 220 | return ctx->scopes.packet_header; |
| 221 | case CTF_SCOPE_PACKET_CONTEXT: |
| 222 | return ctx->scopes.packet_context; |
| 223 | case CTF_SCOPE_EVENT_HEADER: |
| 224 | return ctx->scopes.event_header; |
| 225 | case CTF_SCOPE_EVENT_COMMON_CONTEXT: |
| 226 | return ctx->scopes.event_common_context; |
| 227 | case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 228 | return ctx->scopes.event_spec_context; |
| 229 | case CTF_SCOPE_EVENT_PAYLOAD: |
| 230 | return ctx->scopes.event_payload; |
| 231 | default: |
| 232 | bt_common_abort(); |
| 233 | } |
| 234 | |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Returns the CTF scope from a path string. May return -1 if the path |
| 240 | * is found to be relative. |
| 241 | */ |
| 242 | static enum ctf_scope get_root_scope_from_absolute_pathstr(const char *pathstr, |
| 243 | struct resolve_context *ctx) |
| 244 | { |
| 245 | enum ctf_scope scope; |
| 246 | enum ctf_scope ret = CTF_SCOPE_PACKET_UNKNOWN; |
| 247 | const size_t prefixes_count = sizeof(absolute_path_prefixes) / sizeof(*absolute_path_prefixes); |
| 248 | |
| 249 | for (scope = CTF_SCOPE_PACKET_HEADER; scope < CTF_SCOPE_PACKET_HEADER + prefixes_count; |
| 250 | scope = (ctf_scope) (scope + 1)) { |
| 251 | /* |
| 252 | * Check if path string starts with a known absolute |
| 253 | * path prefix. |
| 254 | * |
| 255 | * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES. |
| 256 | */ |
| 257 | if (strncmp(pathstr, absolute_path_prefixes[scope], |
| 258 | strlen(absolute_path_prefixes[scope]))) { |
| 259 | /* Prefix does not match: try the next one */ |
| 260 | BT_COMP_LOGD("Prefix does not match: trying the next one: " |
| 261 | "path=\"%s\", path-prefix=\"%s\", scope=%s", |
| 262 | pathstr, absolute_path_prefixes[scope], ctf_scope_string(scope)); |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | /* Found it! */ |
| 267 | ret = scope; |
| 268 | BT_COMP_LOGD("Found root scope from absolute path: " |
| 269 | "path=\"%s\", scope=%s", |
| 270 | pathstr, ctf_scope_string(scope)); |
| 271 | goto end; |
| 272 | } |
| 273 | |
| 274 | end: |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Destroys a path token. |
| 280 | */ |
| 281 | static void ptokens_destroy_func(gpointer ptoken, gpointer data) |
| 282 | { |
| 283 | g_string_free((GString *) ptoken, TRUE); |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Destroys a path token list. |
| 288 | */ |
| 289 | static void ptokens_destroy(GList *ptokens) |
| 290 | { |
| 291 | if (!ptokens) { |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | g_list_foreach(ptokens, ptokens_destroy_func, NULL); |
| 296 | g_list_free(ptokens); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Returns the string contained in a path token. |
| 301 | */ |
| 302 | static const char *ptoken_get_string(GList *ptoken) |
| 303 | { |
| 304 | GString *tokenstr = (GString *) ptoken->data; |
| 305 | |
| 306 | return tokenstr->str; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Converts a path string to a path token list, that is, splits the |
| 311 | * individual words of a path string into a list of individual |
| 312 | * strings. |
| 313 | */ |
| 314 | static GList *pathstr_to_ptokens(const char *pathstr, struct resolve_context *ctx) |
| 315 | { |
| 316 | const char *at = pathstr; |
| 317 | const char *last = at; |
| 318 | GList *ptokens = NULL; |
| 319 | |
| 320 | for (;;) { |
| 321 | if (*at == '.' || *at == '\0') { |
| 322 | GString *tokenstr; |
| 323 | |
| 324 | if (at == last) { |
| 325 | /* Error: empty token */ |
| 326 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Empty path token: path=\"%s\", pos=%u", |
| 327 | pathstr, (unsigned int) (at - pathstr)); |
| 328 | goto error; |
| 329 | } |
| 330 | |
| 331 | tokenstr = g_string_new(NULL); |
| 332 | g_string_append_len(tokenstr, last, at - last); |
| 333 | ptokens = g_list_append(ptokens, tokenstr); |
| 334 | last = at + 1; |
| 335 | } |
| 336 | |
| 337 | if (*at == '\0') { |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | at++; |
| 342 | } |
| 343 | |
| 344 | return ptokens; |
| 345 | |
| 346 | error: |
| 347 | ptokens_destroy(ptokens); |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Converts a path token list to a field path object. The path token |
| 353 | * list is relative from `fc`. The index of the source looking for its |
| 354 | * target within `fc` is indicated by `src_index`. This can be |
| 355 | * `INT64_MAX` if the source is contained in `fc`. |
| 356 | * |
| 357 | * `field_path` is an output parameter owned by the caller that must be |
| 358 | * filled here. |
| 359 | */ |
| 360 | static int ptokens_to_field_path(GList *ptokens, struct ctf_field_path *field_path, |
| 361 | struct ctf_field_class *fc, int64_t src_index, |
| 362 | struct resolve_context *ctx) |
| 363 | { |
| 364 | int ret = 0; |
| 365 | GList *cur_ptoken = ptokens; |
| 366 | bool first_level_done = false; |
| 367 | |
| 368 | /* Locate target */ |
| 369 | while (cur_ptoken) { |
| 370 | int64_t child_index; |
| 371 | struct ctf_field_class *child_fc; |
| 372 | const char *ft_name = ptoken_get_string(cur_ptoken); |
| 373 | |
| 374 | BT_COMP_LOGD("Current path token: token=\"%s\"", ft_name); |
| 375 | |
| 376 | /* Find to which index corresponds the current path token */ |
| 377 | if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY || fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) { |
| 378 | child_index = -1; |
| 379 | } else { |
| 380 | child_index = |
| 381 | ctf_field_class_compound_get_field_class_index_from_orig_name(fc, ft_name); |
| 382 | if (child_index < 0) { |
| 383 | /* |
| 384 | * Error: field name does not exist or |
| 385 | * wrong current class. |
| 386 | */ |
| 387 | BT_COMP_LOGD("Cannot get index of field class: " |
| 388 | "field-name=\"%s\", " |
| 389 | "src-index=%" PRId64 ", " |
| 390 | "child-index=%" PRId64 ", " |
| 391 | "first-level-done=%d", |
| 392 | ft_name, src_index, child_index, first_level_done); |
| 393 | ret = -1; |
| 394 | goto end; |
| 395 | } else if (child_index > src_index && !first_level_done) { |
| 396 | BT_COMP_LOGD("Child field class is located after source field class: " |
| 397 | "field-name=\"%s\", " |
| 398 | "src-index=%" PRId64 ", " |
| 399 | "child-index=%" PRId64 ", " |
| 400 | "first-level-done=%d", |
| 401 | ft_name, src_index, child_index, first_level_done); |
| 402 | ret = -1; |
| 403 | goto end; |
| 404 | } |
| 405 | |
| 406 | /* Next path token */ |
| 407 | cur_ptoken = g_list_next(cur_ptoken); |
| 408 | first_level_done = true; |
| 409 | } |
| 410 | |
| 411 | /* Create new field path entry */ |
| 412 | ctf_field_path_append_index(field_path, child_index); |
| 413 | |
| 414 | /* Get child field class */ |
| 415 | child_fc = ctf_field_class_compound_borrow_field_class_by_index(fc, child_index); |
| 416 | BT_ASSERT(child_fc); |
| 417 | |
| 418 | /* Move child class to current class */ |
| 419 | fc = child_fc; |
| 420 | } |
| 421 | |
| 422 | end: |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Converts a known absolute path token list to a field path object |
| 428 | * within the resolving context `ctx`. |
| 429 | * |
| 430 | * `field_path` is an output parameter owned by the caller that must be |
| 431 | * filled here. |
| 432 | */ |
| 433 | static int absolute_ptokens_to_field_path(GList *ptokens, struct ctf_field_path *field_path, |
| 434 | struct resolve_context *ctx) |
| 435 | { |
| 436 | int ret = 0; |
| 437 | GList *cur_ptoken; |
| 438 | struct ctf_field_class *fc; |
| 439 | |
| 440 | /* |
| 441 | * Make sure we're not referring to a scope within a translated |
| 442 | * object. |
| 443 | */ |
| 444 | switch (field_path->root) { |
| 445 | case CTF_SCOPE_PACKET_HEADER: |
| 446 | if (ctx->tc->is_translated) { |
| 447 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Trace class is already translated: " |
| 448 | "root-scope=%s", |
| 449 | ctf_scope_string(field_path->root)); |
| 450 | ret = -1; |
| 451 | goto end; |
| 452 | } |
| 453 | |
| 454 | break; |
| 455 | case CTF_SCOPE_PACKET_CONTEXT: |
| 456 | case CTF_SCOPE_EVENT_HEADER: |
| 457 | case CTF_SCOPE_EVENT_COMMON_CONTEXT: |
| 458 | if (!ctx->sc) { |
| 459 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("No current stream class: " |
| 460 | "root-scope=%s", |
| 461 | ctf_scope_string(field_path->root)); |
| 462 | ret = -1; |
| 463 | goto end; |
| 464 | } |
| 465 | |
| 466 | if (ctx->sc->is_translated) { |
| 467 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Stream class is already translated: " |
| 468 | "root-scope=%s", |
| 469 | ctf_scope_string(field_path->root)); |
| 470 | ret = -1; |
| 471 | goto end; |
| 472 | } |
| 473 | |
| 474 | break; |
| 475 | case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 476 | case CTF_SCOPE_EVENT_PAYLOAD: |
| 477 | if (!ctx->ec) { |
| 478 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("No current event class: " |
| 479 | "root-scope=%s", |
| 480 | ctf_scope_string(field_path->root)); |
| 481 | ret = -1; |
| 482 | goto end; |
| 483 | } |
| 484 | |
| 485 | if (ctx->ec->is_translated) { |
| 486 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Event class is already translated: " |
| 487 | "root-scope=%s", |
| 488 | ctf_scope_string(field_path->root)); |
| 489 | ret = -1; |
| 490 | goto end; |
| 491 | } |
| 492 | |
| 493 | break; |
| 494 | |
| 495 | default: |
| 496 | bt_common_abort(); |
| 497 | } |
| 498 | |
| 499 | /* Skip absolute path tokens */ |
| 500 | cur_ptoken = g_list_nth(ptokens, absolute_path_prefix_ptoken_counts[field_path->root]); |
| 501 | |
| 502 | /* Start with root class */ |
| 503 | fc = borrow_class_from_ctx(ctx, field_path->root); |
| 504 | if (!fc) { |
| 505 | /* Error: root class is not available */ |
| 506 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Root field class is not available: " |
| 507 | "root-scope=%s", |
| 508 | ctf_scope_string(field_path->root)); |
| 509 | ret = -1; |
| 510 | goto end; |
| 511 | } |
| 512 | |
| 513 | /* Locate target */ |
| 514 | ret = ptokens_to_field_path(cur_ptoken, field_path, fc, INT64_MAX, ctx); |
| 515 | |
| 516 | end: |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * Converts a known relative path token list to a field path object |
| 522 | * within the resolving context `ctx`. |
| 523 | * |
| 524 | * `field_path` is an output parameter owned by the caller that must be |
| 525 | * filled here. |
| 526 | */ |
| 527 | static int relative_ptokens_to_field_path(GList *ptokens, struct ctf_field_path *field_path, |
| 528 | struct resolve_context *ctx) |
| 529 | { |
| 530 | int ret = 0; |
| 531 | int64_t parent_pos_in_stack; |
| 532 | struct ctf_field_path tail_field_path; |
| 533 | |
| 534 | ctf_field_path_init(&tail_field_path); |
| 535 | parent_pos_in_stack = field_class_stack_size(ctx->field_class_stack) - 1; |
| 536 | |
| 537 | while (parent_pos_in_stack >= 0) { |
| 538 | struct ctf_field_class *parent_class = |
| 539 | field_class_stack_at(ctx->field_class_stack, parent_pos_in_stack)->fc; |
| 540 | int64_t cur_index = |
| 541 | field_class_stack_at(ctx->field_class_stack, parent_pos_in_stack)->index; |
| 542 | |
| 543 | BT_COMP_LOGD("Locating target field class from current parent field class: " |
| 544 | "parent-pos=%" PRId64 ", parent-fc-addr=%p, " |
| 545 | "cur-index=%" PRId64, |
| 546 | parent_pos_in_stack, parent_class, cur_index); |
| 547 | |
| 548 | /* Locate target from current parent class */ |
| 549 | ret = ptokens_to_field_path(ptokens, &tail_field_path, parent_class, cur_index, ctx); |
| 550 | if (ret) { |
| 551 | /* Not found... yet */ |
| 552 | BT_COMP_LOGD_STR("Not found at this point."); |
| 553 | ctf_field_path_clear(&tail_field_path); |
| 554 | } else { |
| 555 | /* Found: stitch tail field path to head field path */ |
| 556 | uint64_t i = 0; |
| 557 | size_t tail_field_path_len = tail_field_path.path->len; |
| 558 | |
| 559 | while (BT_TRUE) { |
| 560 | struct ctf_field_class *cur_class = |
| 561 | field_class_stack_at(ctx->field_class_stack, i)->fc; |
| 562 | int64_t index = field_class_stack_at(ctx->field_class_stack, i)->index; |
| 563 | |
| 564 | if (cur_class == parent_class) { |
| 565 | break; |
| 566 | } |
| 567 | |
| 568 | ctf_field_path_append_index(field_path, index); |
| 569 | i++; |
| 570 | } |
| 571 | |
| 572 | for (i = 0; i < tail_field_path_len; i++) { |
| 573 | int64_t index = ctf_field_path_borrow_index_by_index(&tail_field_path, i); |
| 574 | |
| 575 | ctf_field_path_append_index(field_path, (int64_t) index); |
| 576 | } |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | parent_pos_in_stack--; |
| 581 | } |
| 582 | |
| 583 | if (parent_pos_in_stack < 0) { |
| 584 | /* Not found */ |
| 585 | ret = -1; |
| 586 | } |
| 587 | |
| 588 | ctf_field_path_fini(&tail_field_path); |
| 589 | return ret; |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * Converts a path string to a field path object within the resolving |
| 594 | * context `ctx`. |
| 595 | */ |
| 596 | static int pathstr_to_field_path(const char *pathstr, struct ctf_field_path *field_path, |
| 597 | struct resolve_context *ctx) |
| 598 | { |
| 599 | int ret = 0; |
| 600 | enum ctf_scope root_scope; |
| 601 | GList *ptokens = NULL; |
| 602 | |
| 603 | /* Convert path string to path tokens */ |
| 604 | ptokens = pathstr_to_ptokens(pathstr, ctx); |
| 605 | if (!ptokens) { |
| 606 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot convert path string to path tokens: " |
| 607 | "path=\"%s\"", |
| 608 | pathstr); |
| 609 | ret = -1; |
| 610 | goto end; |
| 611 | } |
| 612 | |
| 613 | /* Absolute or relative path? */ |
| 614 | root_scope = get_root_scope_from_absolute_pathstr(pathstr, ctx); |
| 615 | |
| 616 | if (root_scope == CTF_SCOPE_PACKET_UNKNOWN) { |
| 617 | /* Relative path: start with current root scope */ |
| 618 | field_path->root = ctx->root_scope; |
| 619 | BT_COMP_LOGD("Detected relative path: starting with current root scope: " |
| 620 | "scope=%s", |
| 621 | ctf_scope_string(field_path->root)); |
| 622 | ret = relative_ptokens_to_field_path(ptokens, field_path, ctx); |
| 623 | if (ret) { |
| 624 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 625 | "Cannot get relative field path of path string: " |
| 626 | "path=\"%s\", start-scope=%s, end-scope=%s", |
| 627 | pathstr, ctf_scope_string(ctx->root_scope), ctf_scope_string(field_path->root)); |
| 628 | goto end; |
| 629 | } |
| 630 | } else { |
| 631 | /* Absolute path: use found root scope */ |
| 632 | field_path->root = root_scope; |
| 633 | BT_COMP_LOGD("Detected absolute path: using root scope: " |
| 634 | "scope=%s", |
| 635 | ctf_scope_string(field_path->root)); |
| 636 | ret = absolute_ptokens_to_field_path(ptokens, field_path, ctx); |
| 637 | if (ret) { |
| 638 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 639 | "Cannot get absolute field path of path string: " |
| 640 | "path=\"%s\", root-scope=%s", |
| 641 | pathstr, ctf_scope_string(root_scope)); |
| 642 | goto end; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | if (BT_LOG_ON_TRACE && ret == 0) { |
| 647 | GString *field_path_pretty = ctf_field_path_string(field_path); |
| 648 | const char *field_path_pretty_str = field_path_pretty ? field_path_pretty->str : "(null)"; |
| 649 | |
| 650 | BT_COMP_LOGD("Found field path: path=\"%s\", field-path=\"%s\"", pathstr, |
| 651 | field_path_pretty_str); |
| 652 | |
| 653 | if (field_path_pretty) { |
| 654 | g_string_free(field_path_pretty, TRUE); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | end: |
| 659 | ptokens_destroy(ptokens); |
| 660 | return ret; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Retrieves a field class by following the field path `field_path` in |
| 665 | * the resolving context `ctx`. |
| 666 | */ |
| 667 | static struct ctf_field_class *field_path_to_field_class(struct ctf_field_path *field_path, |
| 668 | struct resolve_context *ctx) |
| 669 | { |
| 670 | uint64_t i; |
| 671 | struct ctf_field_class *fc; |
| 672 | |
| 673 | /* Start with root class */ |
| 674 | fc = borrow_class_from_ctx(ctx, field_path->root); |
| 675 | if (!fc) { |
| 676 | /* Error: root class is not available */ |
| 677 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Root field class is not available: root-scope=%s", |
| 678 | ctf_scope_string(field_path->root)); |
| 679 | goto end; |
| 680 | } |
| 681 | |
| 682 | /* Locate target */ |
| 683 | for (i = 0; i < field_path->path->len; i++) { |
| 684 | struct ctf_field_class *child_fc; |
| 685 | int64_t child_index = ctf_field_path_borrow_index_by_index(field_path, i); |
| 686 | |
| 687 | /* Get child field class */ |
| 688 | child_fc = ctf_field_class_compound_borrow_field_class_by_index(fc, child_index); |
| 689 | BT_ASSERT(child_fc); |
| 690 | |
| 691 | /* Move child class to current class */ |
| 692 | fc = child_fc; |
| 693 | } |
| 694 | |
| 695 | end: |
| 696 | return fc; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * Fills the equivalent field path object of the context class stack. |
| 701 | */ |
| 702 | static void get_ctx_stack_field_path(struct resolve_context *ctx, struct ctf_field_path *field_path) |
| 703 | { |
| 704 | uint64_t i; |
| 705 | |
| 706 | BT_ASSERT(field_path); |
| 707 | field_path->root = ctx->root_scope; |
| 708 | ctf_field_path_clear(field_path); |
| 709 | |
| 710 | for (i = 0; i < field_class_stack_size(ctx->field_class_stack); i++) { |
| 711 | struct field_class_stack_frame *frame = field_class_stack_at(ctx->field_class_stack, i); |
| 712 | |
| 713 | ctf_field_path_append_index(field_path, frame->index); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Returns the index of the lowest common ancestor of two field path |
| 719 | * objects having the same root scope. |
| 720 | */ |
| 721 | static int64_t get_field_paths_lca_index(struct ctf_field_path *field_path1, |
| 722 | struct ctf_field_path *field_path2, |
| 723 | struct resolve_context *ctx) |
| 724 | { |
| 725 | int64_t lca_index = 0; |
| 726 | uint64_t field_path1_len, field_path2_len; |
| 727 | |
| 728 | if (BT_LOG_ON_TRACE) { |
| 729 | GString *field_path1_pretty = ctf_field_path_string(field_path1); |
| 730 | GString *field_path2_pretty = ctf_field_path_string(field_path2); |
| 731 | const char *field_path1_pretty_str = |
| 732 | field_path1_pretty ? field_path1_pretty->str : "(null)"; |
| 733 | const char *field_path2_pretty_str = |
| 734 | field_path2_pretty ? field_path2_pretty->str : "(null)"; |
| 735 | |
| 736 | BT_COMP_LOGD("Finding lowest common ancestor (LCA) between two field paths: " |
| 737 | "field-path-1=\"%s\", field-path-2=\"%s\"", |
| 738 | field_path1_pretty_str, field_path2_pretty_str); |
| 739 | |
| 740 | if (field_path1_pretty) { |
| 741 | g_string_free(field_path1_pretty, TRUE); |
| 742 | } |
| 743 | |
| 744 | if (field_path2_pretty) { |
| 745 | g_string_free(field_path2_pretty, TRUE); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Start from both roots and find the first mismatch. |
| 751 | */ |
| 752 | BT_ASSERT(field_path1->root == field_path2->root); |
| 753 | field_path1_len = field_path1->path->len; |
| 754 | field_path2_len = field_path2->path->len; |
| 755 | |
| 756 | while (true) { |
| 757 | int64_t target_index, ctx_index; |
| 758 | |
| 759 | if (lca_index == (int64_t) field_path2_len || lca_index == (int64_t) field_path1_len) { |
| 760 | /* |
| 761 | * This means that both field paths never split. |
| 762 | * This is invalid because the target cannot be |
| 763 | * an ancestor of the source. |
| 764 | */ |
| 765 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 766 | "Source field class is an ancestor of target field class or vice versa: " |
| 767 | "lca-index=%" PRId64 ", " |
| 768 | "field-path-1-len=%" PRIu64 ", " |
| 769 | "field-path-2-len=%" PRIu64, |
| 770 | lca_index, field_path1_len, field_path2_len); |
| 771 | lca_index = -1; |
| 772 | break; |
| 773 | } |
| 774 | |
| 775 | target_index = ctf_field_path_borrow_index_by_index(field_path1, lca_index); |
| 776 | ctx_index = ctf_field_path_borrow_index_by_index(field_path2, lca_index); |
| 777 | |
| 778 | if (target_index != ctx_index) { |
| 779 | /* LCA index is the previous */ |
| 780 | break; |
| 781 | } |
| 782 | |
| 783 | lca_index++; |
| 784 | } |
| 785 | |
| 786 | BT_COMP_LOGD("Found LCA: lca-index=%" PRId64, lca_index); |
| 787 | return lca_index; |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Validates a target field path. |
| 792 | */ |
| 793 | static int validate_target_field_path(struct ctf_field_path *target_field_path, |
| 794 | struct ctf_field_class *target_fc, |
| 795 | struct resolve_context *ctx) |
| 796 | { |
| 797 | int ret = 0; |
| 798 | struct ctf_field_path ctx_field_path; |
| 799 | uint64_t target_field_path_len = target_field_path->path->len; |
| 800 | int64_t lca_index; |
| 801 | |
| 802 | /* Get context field path */ |
| 803 | ctf_field_path_init(&ctx_field_path); |
| 804 | get_ctx_stack_field_path(ctx, &ctx_field_path); |
| 805 | |
| 806 | /* |
| 807 | * Make sure the target is not a root. |
| 808 | */ |
| 809 | if (target_field_path_len == 0) { |
| 810 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 811 | "Target field path's length is 0 (targeting the root)."); |
| 812 | ret = -1; |
| 813 | goto end; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * Make sure the root of the target field path is not located |
| 818 | * after the context field path's root. |
| 819 | */ |
| 820 | if (target_field_path->root > ctx_field_path.root) { |
| 821 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 822 | "Target field class is located after source field class: " |
| 823 | "target-root=%s, source-root=%s", |
| 824 | ctf_scope_string(target_field_path->root), ctf_scope_string(ctx_field_path.root)); |
| 825 | ret = -1; |
| 826 | goto end; |
| 827 | } |
| 828 | |
| 829 | if (target_field_path->root == ctx_field_path.root) { |
| 830 | int64_t target_index, ctx_index; |
| 831 | |
| 832 | /* |
| 833 | * Find the index of the lowest common ancestor of both field |
| 834 | * paths. |
| 835 | */ |
| 836 | lca_index = get_field_paths_lca_index(target_field_path, &ctx_field_path, ctx); |
| 837 | if (lca_index < 0) { |
| 838 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot get least common ancestor."); |
| 839 | ret = -1; |
| 840 | goto end; |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * Make sure the target field path is located before the |
| 845 | * context field path. |
| 846 | */ |
| 847 | target_index = |
| 848 | ctf_field_path_borrow_index_by_index(target_field_path, (uint64_t) lca_index); |
| 849 | ctx_index = ctf_field_path_borrow_index_by_index(&ctx_field_path, (uint64_t) lca_index); |
| 850 | |
| 851 | if (target_index >= ctx_index) { |
| 852 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 853 | "Target field class's index is greater than or equal to source field class's index in LCA: " |
| 854 | "lca-index=%" PRId64 ", " |
| 855 | "target-index=%" PRId64 ", " |
| 856 | "source-index=%" PRId64, |
| 857 | lca_index, target_index, ctx_index); |
| 858 | ret = -1; |
| 859 | goto end; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Make sure the target class has the right class and properties. |
| 865 | */ |
| 866 | switch (ctx->cur_fc->type) { |
| 867 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 868 | if (target_fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 869 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 870 | "Variant field class's tag field class is not an enumeration field class: " |
| 871 | "tag-fc-addr=%p, tag-fc-id=%d", |
| 872 | target_fc, target_fc->type); |
| 873 | ret = -1; |
| 874 | goto end; |
| 875 | } |
| 876 | break; |
| 877 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 878 | { |
| 879 | if (target_fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 880 | target_fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 881 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 882 | "Sequence field class's length field class is not an unsigned integer field class: " |
| 883 | "length-fc-addr=%p, length-fc-id=%d", |
| 884 | target_fc, target_fc->type); |
| 885 | ret = -1; |
| 886 | goto end; |
| 887 | } |
| 888 | |
| 889 | ctf_field_class_int *int_fc = ctf_field_class_as_int(target_fc); |
| 890 | |
| 891 | if (int_fc->is_signed) { |
| 892 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 893 | "Sequence field class's length field class is not an unsigned integer field class: " |
| 894 | "length-fc-addr=%p, length-fc-id=%d", |
| 895 | target_fc, target_fc->type); |
| 896 | ret = -1; |
| 897 | goto end; |
| 898 | } |
| 899 | break; |
| 900 | } |
| 901 | default: |
| 902 | bt_common_abort(); |
| 903 | } |
| 904 | |
| 905 | end: |
| 906 | ctf_field_path_fini(&ctx_field_path); |
| 907 | return ret; |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * Resolves a variant or sequence field class `fc`. |
| 912 | */ |
| 913 | static int resolve_sequence_or_variant_field_class(struct ctf_field_class *fc, |
| 914 | struct resolve_context *ctx) |
| 915 | { |
| 916 | int ret = 0; |
| 917 | const char *pathstr; |
| 918 | struct ctf_field_path target_field_path; |
| 919 | struct ctf_field_class *target_fc = NULL; |
| 920 | GString *target_field_path_pretty = NULL; |
| 921 | const char *target_field_path_pretty_str; |
| 922 | |
| 923 | ctf_field_path_init(&target_field_path); |
| 924 | |
| 925 | /* Get path string */ |
| 926 | switch (fc->type) { |
| 927 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 928 | { |
| 929 | struct ctf_field_class_sequence *seq_fc = ctf_field_class_as_sequence(fc); |
| 930 | pathstr = seq_fc->length_ref->str; |
| 931 | break; |
| 932 | } |
| 933 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 934 | { |
| 935 | struct ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc); |
| 936 | pathstr = var_fc->tag_ref->str; |
| 937 | break; |
| 938 | } |
| 939 | default: |
| 940 | bt_common_abort(); |
| 941 | } |
| 942 | |
| 943 | if (!pathstr) { |
| 944 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot get path string."); |
| 945 | ret = -1; |
| 946 | goto end; |
| 947 | } |
| 948 | |
| 949 | /* Get target field path out of path string */ |
| 950 | ret = pathstr_to_field_path(pathstr, &target_field_path, ctx); |
| 951 | if (ret) { |
| 952 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot get target field path for path string: " |
| 953 | "path=\"%s\"", |
| 954 | pathstr); |
| 955 | goto end; |
| 956 | } |
| 957 | |
| 958 | target_field_path_pretty = ctf_field_path_string(&target_field_path); |
| 959 | target_field_path_pretty_str = target_field_path_pretty ? target_field_path_pretty->str : NULL; |
| 960 | |
| 961 | /* Get target field class */ |
| 962 | target_fc = field_path_to_field_class(&target_field_path, ctx); |
| 963 | if (!target_fc) { |
| 964 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot get target field class for path string: " |
| 965 | "path=\"%s\", target-field-path=\"%s\"", |
| 966 | pathstr, target_field_path_pretty_str); |
| 967 | ret = -1; |
| 968 | goto end; |
| 969 | } |
| 970 | |
| 971 | ret = validate_target_field_path(&target_field_path, target_fc, ctx); |
| 972 | if (ret) { |
| 973 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Invalid target field path for path string: " |
| 974 | "path=\"%s\", target-field-path=\"%s\"", |
| 975 | pathstr, target_field_path_pretty_str); |
| 976 | goto end; |
| 977 | } |
| 978 | |
| 979 | /* Set target field path and target field class */ |
| 980 | switch (fc->type) { |
| 981 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 982 | { |
| 983 | ctf_field_class_sequence *seq_fc = ctf_field_class_as_sequence(fc); |
| 984 | |
| 985 | ctf_field_path_copy_content(&seq_fc->length_path, &target_field_path); |
| 986 | seq_fc->length_fc = ctf_field_class_as_int(target_fc); |
| 987 | break; |
| 988 | } |
| 989 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 990 | { |
| 991 | ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc); |
| 992 | |
| 993 | ctf_field_path_copy_content(&var_fc->tag_path, &target_field_path); |
| 994 | ctf_field_class_variant_set_tag_field_class(var_fc, ctf_field_class_as_enum(target_fc)); |
| 995 | break; |
| 996 | } |
| 997 | default: |
| 998 | bt_common_abort(); |
| 999 | } |
| 1000 | |
| 1001 | end: |
| 1002 | if (target_field_path_pretty) { |
| 1003 | g_string_free(target_field_path_pretty, TRUE); |
| 1004 | } |
| 1005 | |
| 1006 | ctf_field_path_fini(&target_field_path); |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Resolves a field class `fc`. |
| 1012 | */ |
| 1013 | static int resolve_field_class(struct ctf_field_class *fc, struct resolve_context *ctx) |
| 1014 | { |
| 1015 | int ret = 0; |
| 1016 | |
| 1017 | if (!fc) { |
| 1018 | /* Field class is not available; still valid */ |
| 1019 | goto end; |
| 1020 | } |
| 1021 | |
| 1022 | ctx->cur_fc = fc; |
| 1023 | |
| 1024 | /* Resolve sequence/variant field class */ |
| 1025 | switch (fc->type) { |
| 1026 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 1027 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 1028 | ret = resolve_sequence_or_variant_field_class(fc, ctx); |
| 1029 | if (ret) { |
| 1030 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 1031 | "Cannot resolve sequence field class's length or variant field class's tag: " |
| 1032 | "ret=%d, fc-addr=%p", |
| 1033 | ret, fc); |
| 1034 | goto end; |
| 1035 | } |
| 1036 | |
| 1037 | break; |
| 1038 | default: |
| 1039 | break; |
| 1040 | } |
| 1041 | |
| 1042 | /* Recurse into compound classes */ |
| 1043 | switch (fc->type) { |
| 1044 | case CTF_FIELD_CLASS_TYPE_STRUCT: |
| 1045 | case CTF_FIELD_CLASS_TYPE_VARIANT: |
| 1046 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 1047 | case CTF_FIELD_CLASS_TYPE_ARRAY: |
| 1048 | { |
| 1049 | uint64_t i; |
| 1050 | uint64_t field_count = ctf_field_class_compound_get_field_class_count(fc); |
| 1051 | |
| 1052 | ret = field_class_stack_push(ctx->field_class_stack, fc, ctx); |
| 1053 | if (ret) { |
| 1054 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot push field class on context's stack: " |
| 1055 | "fc-addr=%p", |
| 1056 | fc); |
| 1057 | goto end; |
| 1058 | } |
| 1059 | |
| 1060 | for (i = 0; i < field_count; i++) { |
| 1061 | struct ctf_field_class *child_fc = |
| 1062 | ctf_field_class_compound_borrow_field_class_by_index(fc, i); |
| 1063 | |
| 1064 | BT_ASSERT(child_fc); |
| 1065 | |
| 1066 | if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY || |
| 1067 | fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) { |
| 1068 | field_class_stack_peek(ctx->field_class_stack)->index = -1; |
| 1069 | } else { |
| 1070 | field_class_stack_peek(ctx->field_class_stack)->index = (int64_t) i; |
| 1071 | } |
| 1072 | |
| 1073 | BT_COMP_LOGD("Resolving field class's child field class: " |
| 1074 | "parent-fc-addr=%p, child-fc-addr=%p, " |
| 1075 | "index=%" PRIu64 ", count=%" PRIu64, |
| 1076 | fc, child_fc, i, field_count); |
| 1077 | ret = resolve_field_class(child_fc, ctx); |
| 1078 | if (ret) { |
| 1079 | goto end; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | field_class_stack_pop(ctx->field_class_stack, ctx); |
| 1084 | break; |
| 1085 | } |
| 1086 | default: |
| 1087 | break; |
| 1088 | } |
| 1089 | |
| 1090 | end: |
| 1091 | return ret; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * Resolves the root field class corresponding to the scope `root_scope`. |
| 1096 | */ |
| 1097 | static int resolve_root_class(enum ctf_scope root_scope, struct resolve_context *ctx) |
| 1098 | { |
| 1099 | int ret; |
| 1100 | |
| 1101 | BT_ASSERT(field_class_stack_size(ctx->field_class_stack) == 0); |
| 1102 | ctx->root_scope = root_scope; |
| 1103 | ret = resolve_field_class(borrow_class_from_ctx(ctx, root_scope), ctx); |
| 1104 | ctx->root_scope = CTF_SCOPE_PACKET_UNKNOWN; |
| 1105 | return ret; |
| 1106 | } |
| 1107 | |
| 1108 | static int resolve_event_class_field_classes(struct resolve_context *ctx, |
| 1109 | struct ctf_event_class *ec) |
| 1110 | { |
| 1111 | int ret = 0; |
| 1112 | |
| 1113 | BT_ASSERT(!ctx->scopes.event_spec_context); |
| 1114 | BT_ASSERT(!ctx->scopes.event_payload); |
| 1115 | |
| 1116 | if (ec->is_translated) { |
| 1117 | goto end; |
| 1118 | } |
| 1119 | |
| 1120 | ctx->ec = ec; |
| 1121 | ctx->scopes.event_spec_context = ec->spec_context_fc; |
| 1122 | ret = resolve_root_class(CTF_SCOPE_EVENT_COMMON_CONTEXT, ctx); |
| 1123 | if (ret) { |
| 1124 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 1125 | "Cannot resolve event specific context field class: " |
| 1126 | "ret=%d", |
| 1127 | ret); |
| 1128 | goto end; |
| 1129 | } |
| 1130 | |
| 1131 | ctx->scopes.event_payload = ec->payload_fc; |
| 1132 | ret = resolve_root_class(CTF_SCOPE_EVENT_PAYLOAD, ctx); |
| 1133 | if (ret) { |
| 1134 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve event payload field class: " |
| 1135 | "ret=%d", |
| 1136 | ret); |
| 1137 | goto end; |
| 1138 | } |
| 1139 | |
| 1140 | end: |
| 1141 | ctx->scopes.event_spec_context = NULL; |
| 1142 | ctx->scopes.event_payload = NULL; |
| 1143 | ctx->ec = NULL; |
| 1144 | return ret; |
| 1145 | } |
| 1146 | |
| 1147 | static int resolve_stream_class_field_classes(struct resolve_context *ctx, |
| 1148 | struct ctf_stream_class *sc) |
| 1149 | { |
| 1150 | int ret = 0; |
| 1151 | uint64_t i; |
| 1152 | |
| 1153 | BT_ASSERT(!ctx->scopes.packet_context); |
| 1154 | BT_ASSERT(!ctx->scopes.event_header); |
| 1155 | BT_ASSERT(!ctx->scopes.event_common_context); |
| 1156 | ctx->sc = sc; |
| 1157 | |
| 1158 | if (!sc->is_translated) { |
| 1159 | ctx->scopes.packet_context = sc->packet_context_fc; |
| 1160 | ret = resolve_root_class(CTF_SCOPE_PACKET_CONTEXT, ctx); |
| 1161 | if (ret) { |
| 1162 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve packet context field class: " |
| 1163 | "ret=%d", |
| 1164 | ret); |
| 1165 | goto end; |
| 1166 | } |
| 1167 | |
| 1168 | ctx->scopes.event_header = sc->event_header_fc; |
| 1169 | ret = resolve_root_class(CTF_SCOPE_EVENT_HEADER, ctx); |
| 1170 | if (ret) { |
| 1171 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve event header field class: " |
| 1172 | "ret=%d", |
| 1173 | ret); |
| 1174 | goto end; |
| 1175 | } |
| 1176 | |
| 1177 | ctx->scopes.event_common_context = sc->event_common_context_fc; |
| 1178 | ret = resolve_root_class(CTF_SCOPE_EVENT_COMMON_CONTEXT, ctx); |
| 1179 | if (ret) { |
| 1180 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 1181 | "Cannot resolve event common context field class: " |
| 1182 | "ret=%d", |
| 1183 | ret); |
| 1184 | goto end; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | ctx->scopes.packet_context = sc->packet_context_fc; |
| 1189 | ctx->scopes.event_header = sc->event_header_fc; |
| 1190 | ctx->scopes.event_common_context = sc->event_common_context_fc; |
| 1191 | |
| 1192 | for (i = 0; i < sc->event_classes->len; i++) { |
| 1193 | ctf_event_class *ec = (ctf_event_class *) sc->event_classes->pdata[i]; |
| 1194 | |
| 1195 | ret = resolve_event_class_field_classes(ctx, ec); |
| 1196 | if (ret) { |
| 1197 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve event class's field classes: " |
| 1198 | "ec-id=%" PRIu64 ", ec-name=\"%s\"", |
| 1199 | ec->id, ec->name->str); |
| 1200 | goto end; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | end: |
| 1205 | ctx->scopes.packet_context = NULL; |
| 1206 | ctx->scopes.event_header = NULL; |
| 1207 | ctx->scopes.event_common_context = NULL; |
| 1208 | ctx->sc = NULL; |
| 1209 | return ret; |
| 1210 | } |
| 1211 | |
| 1212 | int ctf_trace_class_resolve_field_classes(struct ctf_trace_class *tc, |
| 1213 | struct meta_log_config *log_cfg) |
| 1214 | { |
| 1215 | int ret = 0; |
| 1216 | uint64_t i; |
| 1217 | |
| 1218 | resolve_context local_ctx {}; |
| 1219 | local_ctx.log_level = log_cfg->log_level; |
| 1220 | local_ctx.self_comp = log_cfg->self_comp; |
| 1221 | local_ctx.self_comp_class = log_cfg->self_comp_class; |
| 1222 | local_ctx.tc = tc; |
| 1223 | local_ctx.scopes.packet_header = tc->packet_header_fc; |
| 1224 | local_ctx.root_scope = CTF_SCOPE_PACKET_HEADER; |
| 1225 | |
| 1226 | struct resolve_context *ctx = &local_ctx; |
| 1227 | |
| 1228 | /* Initialize class stack */ |
| 1229 | ctx->field_class_stack = field_class_stack_create(); |
| 1230 | if (!ctx->field_class_stack) { |
| 1231 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot create field class stack."); |
| 1232 | ret = -1; |
| 1233 | goto end; |
| 1234 | } |
| 1235 | |
| 1236 | if (!tc->is_translated) { |
| 1237 | ctx->scopes.packet_header = tc->packet_header_fc; |
| 1238 | ret = resolve_root_class(CTF_SCOPE_PACKET_HEADER, ctx); |
| 1239 | if (ret) { |
| 1240 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve packet header field class: " |
| 1241 | "ret=%d", |
| 1242 | ret); |
| 1243 | goto end; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | ctx->scopes.packet_header = tc->packet_header_fc; |
| 1248 | |
| 1249 | for (i = 0; i < tc->stream_classes->len; i++) { |
| 1250 | ctf_stream_class *sc = (ctf_stream_class *) tc->stream_classes->pdata[i]; |
| 1251 | |
| 1252 | ret = resolve_stream_class_field_classes(ctx, sc); |
| 1253 | if (ret) { |
| 1254 | _BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE("Cannot resolve stream class's field classes: " |
| 1255 | "sc-id=%" PRIu64, |
| 1256 | sc->id); |
| 1257 | goto end; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | end: |
| 1262 | field_class_stack_destroy(ctx->field_class_stack); |
| 1263 | return ret; |
| 1264 | } |