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