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