bc303c15c171b30163707de354ec85fa0351e3f8
[babeltrace.git] / src / plugins / ctf / common / metadata / ctf-meta-resolve.c
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
16 #define BT_COMP_LOG_SELF_COMP (ctx->self_comp)
17 #define BT_LOG_OUTPUT_LEVEL (ctx->log_level)
18 #define BT_LOG_TAG "PLUGIN/CTF/META/RESOLVE"
19 #include "logging/comp-logging.h"
20
21 #include <babeltrace2/babeltrace.h>
22 #include "common/macros.h"
23 #include "common/assert.h"
24 #include "common/common.h"
25 #include <glib.h>
26 #include <stdbool.h>
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"
35 #include "logging.h"
36
37 typedef GPtrArray field_class_stack;
38
39 /*
40 * A stack frame.
41 *
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).
47 */
48 struct field_class_stack_frame {
49 struct ctf_field_class *fc;
50 int64_t index;
51 };
52
53 /*
54 * The current context of the resolving engine.
55 */
56 struct resolve_context {
57 bt_logging_level log_level;
58 bt_self_component *self_comp;
59 struct ctf_trace_class *tc;
60 struct ctf_stream_class *sc;
61 struct ctf_event_class *ec;
62
63 struct {
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 *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
99 void destroy_field_class_stack_frame(struct field_class_stack_frame *frame)
100 {
101 if (!frame) {
102 return;
103 }
104
105 g_free(frame);
106 }
107
108 /*
109 * Creates a class stack.
110 */
111 static
112 field_class_stack *field_class_stack_create(void)
113 {
114 return g_ptr_array_new_with_free_func(
115 (GDestroyNotify) destroy_field_class_stack_frame);
116 }
117
118 /*
119 * Destroys a class stack.
120 */
121 static
122 void field_class_stack_destroy(field_class_stack *stack)
123 {
124 if (stack) {
125 g_ptr_array_free(stack, TRUE);
126 }
127 }
128
129 /*
130 * Pushes a field class onto a class stack.
131 */
132 static
133 int field_class_stack_push(field_class_stack *stack, struct ctf_field_class *fc,
134 struct resolve_context *ctx)
135 {
136 int ret = 0;
137 struct field_class_stack_frame *frame = NULL;
138
139 if (!stack || !fc) {
140 BT_COMP_LOGE("Invalid parameter: stack or field class is NULL.");
141 ret = -1;
142 goto end;
143 }
144
145 frame = g_new0(struct field_class_stack_frame, 1);
146 if (!frame) {
147 BT_COMP_LOGE_STR("Failed to allocate one field class stack frame.");
148 ret = -1;
149 goto end;
150 }
151
152 BT_COMP_LOGD("Pushing field class on context's stack: "
153 "fc-addr=%p, stack-size-before=%u", fc, stack->len);
154 frame->fc = fc;
155 g_ptr_array_add(stack, frame);
156
157 end:
158 return ret;
159 }
160
161 /*
162 * Checks whether or not `stack` is empty.
163 */
164 static
165 bool field_class_stack_empty(field_class_stack *stack)
166 {
167 return stack->len == 0;
168 }
169
170 /*
171 * Returns the number of frames in `stack`.
172 */
173 static
174 size_t field_class_stack_size(field_class_stack *stack)
175 {
176 return stack->len;
177 }
178
179 /*
180 * Returns the top frame of `stack`.
181 */
182 static
183 struct field_class_stack_frame *field_class_stack_peek(field_class_stack *stack)
184 {
185 BT_ASSERT(stack);
186 BT_ASSERT(!field_class_stack_empty(stack));
187
188 return g_ptr_array_index(stack, stack->len - 1);
189 }
190
191 /*
192 * Returns the frame at index `index` in `stack`.
193 */
194 static
195 struct field_class_stack_frame *field_class_stack_at(field_class_stack *stack,
196 size_t index)
197 {
198 BT_ASSERT(stack);
199 BT_ASSERT(index < stack->len);
200
201 return g_ptr_array_index(stack, index);
202 }
203
204 /*
205 * Removes the top frame of `stack`.
206 */
207 static
208 void field_class_stack_pop(field_class_stack *stack,
209 struct resolve_context *ctx)
210 {
211 if (!field_class_stack_empty(stack)) {
212 /*
213 * This will call the frame's destructor and free it, as
214 * well as put its contained field class.
215 */
216 BT_COMP_LOGD("Popping context's stack: stack-size-before=%u",
217 stack->len);
218 g_ptr_array_set_size(stack, stack->len - 1);
219 }
220 }
221
222 /*
223 * Returns the scope field class of `scope` in the context `ctx`.
224 */
225 static
226 struct ctf_field_class *borrow_class_from_ctx(struct resolve_context *ctx,
227 enum ctf_scope scope)
228 {
229 switch (scope) {
230 case CTF_SCOPE_PACKET_HEADER:
231 return ctx->scopes.packet_header;
232 case CTF_SCOPE_PACKET_CONTEXT:
233 return ctx->scopes.packet_context;
234 case CTF_SCOPE_EVENT_HEADER:
235 return ctx->scopes.event_header;
236 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
237 return ctx->scopes.event_common_context;
238 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
239 return ctx->scopes.event_spec_context;
240 case CTF_SCOPE_EVENT_PAYLOAD:
241 return ctx->scopes.event_payload;
242 default:
243 bt_common_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 */
253 static
254 enum ctf_scope get_root_scope_from_absolute_pathstr(const char *pathstr,
255 struct resolve_context *ctx)
256 {
257 enum ctf_scope scope;
258 enum ctf_scope ret = CTF_SCOPE_PACKET_UNKNOWN;
259 const size_t prefixes_count = sizeof(absolute_path_prefixes) /
260 sizeof(*absolute_path_prefixes);
261
262 for (scope = CTF_SCOPE_PACKET_HEADER; scope < CTF_SCOPE_PACKET_HEADER +
263 prefixes_count; scope++) {
264 /*
265 * Check if path string starts with a known absolute
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 */
273 BT_COMP_LOGD("Prefix does not match: trying the next one: "
274 "path=\"%s\", path-prefix=\"%s\", scope=%s",
275 pathstr, absolute_path_prefixes[scope],
276 ctf_scope_string(scope));
277 continue;
278 }
279
280 /* Found it! */
281 ret = scope;
282 BT_COMP_LOGD("Found root scope from absolute path: "
283 "path=\"%s\", scope=%s", pathstr,
284 ctf_scope_string(scope));
285 goto end;
286 }
287
288 end:
289 return ret;
290 }
291
292 /*
293 * Destroys a path token.
294 */
295 static
296 void ptokens_destroy_func(gpointer ptoken, gpointer data)
297 {
298 g_string_free(ptoken, TRUE);
299 }
300
301 /*
302 * Destroys a path token list.
303 */
304 static
305 void 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 */
318 static
319 const 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 */
331 static
332 GList *pathstr_to_ptokens(const char *pathstr, struct resolve_context *ctx)
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 */
344 BT_COMP_LOGE("Empty path token: path=\"%s\", pos=%u",
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
364 error:
365 ptokens_destroy(ptokens);
366 return NULL;
367 }
368
369 /*
370 * Converts a path token list to a field path object. The path token
371 * list is relative from `fc`. The index of the source looking for its
372 * target within `fc` is indicated by `src_index`. This can be
373 * `INT64_MAX` if the source is contained in `fc`.
374 *
375 * `field_path` is an output parameter owned by the caller that must be
376 * filled here.
377 */
378 static
379 int ptokens_to_field_path(GList *ptokens, struct ctf_field_path *field_path,
380 struct ctf_field_class *fc, int64_t src_index,
381 struct resolve_context *ctx)
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;
390 struct ctf_field_class *child_fc;
391 const char *ft_name = ptoken_get_string(cur_ptoken);
392
393 BT_COMP_LOGD("Current path token: token=\"%s\"", ft_name);
394
395 /* Find to which index corresponds the current path token */
396 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY ||
397 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
398 child_index = -1;
399 } else {
400 child_index =
401 ctf_field_class_compound_get_field_class_index_from_orig_name(
402 fc, ft_name);
403 if (child_index < 0) {
404 /*
405 * Error: field name does not exist or
406 * wrong current class.
407 */
408 BT_COMP_LOGD("Cannot get index of field class: "
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) {
419 BT_COMP_LOGD("Child field class is located after source field class: "
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
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);
442
443 /* Move child class to current class */
444 fc = child_fc;
445 }
446
447 end:
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 */
458 static
459 int 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;
465 struct ctf_field_class *fc;
466
467 /*
468 * Make sure we're not referring to a scope within a translated
469 * object.
470 */
471 switch (field_path->root) {
472 case CTF_SCOPE_PACKET_HEADER:
473 if (ctx->tc->is_translated) {
474 BT_COMP_LOGE("Trace class is already translated: "
475 "root-scope=%s",
476 ctf_scope_string(field_path->root));
477 ret = -1;
478 goto end;
479 }
480
481 break;
482 case CTF_SCOPE_PACKET_CONTEXT:
483 case CTF_SCOPE_EVENT_HEADER:
484 case CTF_SCOPE_EVENT_COMMON_CONTEXT:
485 if (!ctx->sc) {
486 BT_COMP_LOGE("No current stream class: "
487 "root-scope=%s",
488 ctf_scope_string(field_path->root));
489 ret = -1;
490 goto end;
491 }
492
493 if (ctx->sc->is_translated) {
494 BT_COMP_LOGE("Stream class is already translated: "
495 "root-scope=%s",
496 ctf_scope_string(field_path->root));
497 ret = -1;
498 goto end;
499 }
500
501 break;
502 case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT:
503 case CTF_SCOPE_EVENT_PAYLOAD:
504 if (!ctx->ec) {
505 BT_COMP_LOGE("No current event class: "
506 "root-scope=%s",
507 ctf_scope_string(field_path->root));
508 ret = -1;
509 goto end;
510 }
511
512 if (ctx->ec->is_translated) {
513 BT_COMP_LOGE("Event class is already translated: "
514 "root-scope=%s",
515 ctf_scope_string(field_path->root));
516 ret = -1;
517 goto end;
518 }
519
520 break;
521
522 default:
523 bt_common_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
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 */
534 BT_COMP_LOGE("Root field class is not available: "
535 "root-scope=%s",
536 ctf_scope_string(field_path->root));
537 ret = -1;
538 goto end;
539 }
540
541 /* Locate target */
542 ret = ptokens_to_field_path(cur_ptoken, field_path, fc, INT64_MAX, ctx);
543
544 end:
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 */
555 static
556 int 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);
564 parent_pos_in_stack = field_class_stack_size(ctx->field_class_stack) - 1;
565
566 while (parent_pos_in_stack >= 0) {
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,
571 parent_pos_in_stack)->index;
572
573 BT_COMP_LOGD("Locating target field class from current parent field class: "
574 "parent-pos=%" PRId64 ", parent-fc-addr=%p, "
575 "cur-index=%" PRId64,
576 parent_pos_in_stack, parent_class, cur_index);
577
578 /* Locate target from current parent class */
579 ret = ptokens_to_field_path(ptokens, &tail_field_path,
580 parent_class, cur_index, ctx);
581 if (ret) {
582 /* Not found... yet */
583 BT_COMP_LOGD_STR("Not found at this point.");
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) {
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;
597
598 if (cur_class == parent_class) {
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 */
634 static
635 int pathstr_to_field_path(const char *pathstr,
636 struct ctf_field_path *field_path, struct resolve_context *ctx)
637 {
638 int ret = 0;
639 enum ctf_scope root_scope;
640 GList *ptokens = NULL;
641
642 /* Convert path string to path tokens */
643 ptokens = pathstr_to_ptokens(pathstr, ctx);
644 if (!ptokens) {
645 BT_COMP_LOGE("Cannot convert path string to path tokens: "
646 "path=\"%s\"", pathstr);
647 ret = -1;
648 goto end;
649 }
650
651 /* Absolute or relative path? */
652 root_scope = get_root_scope_from_absolute_pathstr(pathstr, ctx);
653
654 if (root_scope == CTF_SCOPE_PACKET_UNKNOWN) {
655 /* Relative path: start with current root scope */
656 field_path->root = ctx->root_scope;
657 BT_COMP_LOGD("Detected relative path: starting with current root scope: "
658 "scope=%s", ctf_scope_string(field_path->root));
659 ret = relative_ptokens_to_field_path(ptokens, field_path, ctx);
660 if (ret) {
661 BT_COMP_LOGE("Cannot get relative field path of path string: "
662 "path=\"%s\", start-scope=%s, end-scope=%s",
663 pathstr, ctf_scope_string(ctx->root_scope),
664 ctf_scope_string(field_path->root));
665 goto end;
666 }
667 } else {
668 /* Absolute path: use found root scope */
669 field_path->root = root_scope;
670 BT_COMP_LOGD("Detected absolute path: using root scope: "
671 "scope=%s", ctf_scope_string(field_path->root));
672 ret = absolute_ptokens_to_field_path(ptokens, field_path, ctx);
673 if (ret) {
674 BT_COMP_LOGE("Cannot get absolute field path of path string: "
675 "path=\"%s\", root-scope=%s",
676 pathstr, ctf_scope_string(root_scope));
677 goto end;
678 }
679 }
680
681 if (BT_LOG_ON_TRACE && ret == 0) {
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
686 BT_COMP_LOGD("Found field path: path=\"%s\", field-path=\"%s\"",
687 pathstr, field_path_pretty_str);
688
689 if (field_path_pretty) {
690 g_string_free(field_path_pretty, TRUE);
691 }
692 }
693
694 end:
695 ptokens_destroy(ptokens);
696 return ret;
697 }
698
699 /*
700 * Retrieves a field class by following the field path `field_path` in
701 * the resolving context `ctx`.
702 */
703 static
704 struct ctf_field_class *field_path_to_field_class(
705 struct ctf_field_path *field_path, struct resolve_context *ctx)
706 {
707 uint64_t i;
708 struct ctf_field_class *fc;
709
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 */
714 BT_COMP_LOGE("Root field class is not available: root-scope=%s",
715 ctf_scope_string(field_path->root));
716 goto end;
717 }
718
719 /* Locate target */
720 for (i = 0; i < field_path->path->len; i++) {
721 struct ctf_field_class *child_fc;
722 int64_t child_index =
723 ctf_field_path_borrow_index_by_index(field_path, i);
724
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);
729
730 /* Move child class to current class */
731 fc = child_fc;
732 }
733
734 end:
735 return fc;
736 }
737
738 /*
739 * Fills the equivalent field path object of the context class stack.
740 */
741 static
742 void 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
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);
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 */
763 static
764 int64_t get_field_paths_lca_index(struct ctf_field_path *field_path1,
765 struct ctf_field_path *field_path2,
766 struct resolve_context *ctx)
767 {
768 int64_t lca_index = 0;
769 uint64_t field_path1_len, field_path2_len;
770
771 if (BT_LOG_ON_TRACE) {
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
781 BT_COMP_LOGD("Finding lowest common ancestor (LCA) between two field paths: "
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 */
811 BT_COMP_LOGE("Source field class is an ancestor of target field class or vice versa: "
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
833 BT_COMP_LOGD("Found LCA: lca-index=%" PRId64, lca_index);
834 return lca_index;
835 }
836
837 /*
838 * Validates a target field path.
839 */
840 static
841 int validate_target_field_path(struct ctf_field_path *target_field_path,
842 struct ctf_field_class *target_fc,
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) {
858 BT_COMP_LOGE_STR("Target field path's length is 0 (targeting the root).");
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) {
868 BT_COMP_LOGE("Target field class is located after source field class: "
869 "target-root=%s, source-root=%s",
870 ctf_scope_string(target_field_path->root),
871 ctf_scope_string(ctx_field_path.root));
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,
884 &ctx_field_path, ctx);
885 if (lca_index < 0) {
886 BT_COMP_LOGE_STR("Cannot get least common ancestor.");
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) {
901 BT_COMP_LOGE("Target field class's index is greater than or equal to source field class's index in LCA: "
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 /*
912 * Make sure the target class has the right class and properties.
913 */
914 switch (ctx->cur_fc->type) {
915 case CTF_FIELD_CLASS_TYPE_VARIANT:
916 if (target_fc->type != CTF_FIELD_CLASS_TYPE_ENUM) {
917 BT_COMP_LOGE("Variant field class's tag field class is not an enumeration field class: "
918 "tag-fc-addr=%p, tag-fc-id=%d",
919 target_fc, target_fc->type);
920 ret = -1;
921 goto end;
922 }
923 break;
924 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
925 {
926 struct ctf_field_class_int *int_fc = (void *) target_fc;
927
928 if (target_fc->type != CTF_FIELD_CLASS_TYPE_INT &&
929 target_fc->type != CTF_FIELD_CLASS_TYPE_ENUM) {
930 BT_COMP_LOGE("Sequence field class's length field class is not an unsigned integer field class: "
931 "length-fc-addr=%p, length-fc-id=%d",
932 target_fc, target_fc->type);
933 ret = -1;
934 goto end;
935 }
936
937 if (int_fc->is_signed) {
938 BT_COMP_LOGE("Sequence field class's length field class is not an unsigned integer field class: "
939 "length-fc-addr=%p, length-fc-id=%d",
940 target_fc, target_fc->type);
941 ret = -1;
942 goto end;
943 }
944 break;
945 }
946 default:
947 bt_common_abort();
948 }
949
950 end:
951 ctf_field_path_fini(&ctx_field_path);
952 return ret;
953 }
954
955 /*
956 * Resolves a variant or sequence field class `fc`.
957 */
958 static
959 int resolve_sequence_or_variant_field_class(struct ctf_field_class *fc,
960 struct resolve_context *ctx)
961 {
962 int ret = 0;
963 const char *pathstr;
964 struct ctf_field_path target_field_path;
965 struct ctf_field_class *target_fc = NULL;
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 */
972 switch (fc->type) {
973 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
974 {
975 struct ctf_field_class_sequence *seq_fc = (void *) fc;
976 pathstr = seq_fc->length_ref->str;
977 break;
978 }
979 case CTF_FIELD_CLASS_TYPE_VARIANT:
980 {
981 struct ctf_field_class_variant *var_fc = (void *) fc;
982 pathstr = var_fc->tag_ref->str;
983 break;
984 }
985 default:
986 bt_common_abort();
987 }
988
989 if (!pathstr) {
990 BT_COMP_LOGE_STR("Cannot get path string.");
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) {
998 BT_COMP_LOGE("Cannot get target field path for path string: "
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
1008 /* Get target field class */
1009 target_fc = field_path_to_field_class(&target_field_path, ctx);
1010 if (!target_fc) {
1011 BT_COMP_LOGE("Cannot get target field class for path string: "
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,
1019 target_fc, ctx);
1020 if (ret) {
1021 BT_COMP_LOGE("Invalid target field path for path string: "
1022 "path=\"%s\", target-field-path=\"%s\"",
1023 pathstr, target_field_path_pretty_str);
1024 goto end;
1025 }
1026
1027 /* Set target field path and target field class */
1028 switch (fc->type) {
1029 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1030 {
1031 struct ctf_field_class_sequence *seq_fc = (void *) fc;
1032
1033 ctf_field_path_copy_content(&seq_fc->length_path,
1034 &target_field_path);
1035 seq_fc->length_fc = (void *) target_fc;
1036 break;
1037 }
1038 case CTF_FIELD_CLASS_TYPE_VARIANT:
1039 {
1040 struct ctf_field_class_variant *var_fc = (void *) fc;
1041
1042 ctf_field_path_copy_content(&var_fc->tag_path,
1043 &target_field_path);
1044 ctf_field_class_variant_set_tag_field_class(var_fc,
1045 (void *) target_fc);
1046 break;
1047 }
1048 default:
1049 bt_common_abort();
1050 }
1051
1052 end:
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 /*
1062 * Resolves a field class `fc`.
1063 */
1064 static
1065 int resolve_field_class(struct ctf_field_class *fc, struct resolve_context *ctx)
1066 {
1067 int ret = 0;
1068
1069 if (!fc) {
1070 /* Field class is not available; still valid */
1071 goto end;
1072 }
1073
1074 ctx->cur_fc = fc;
1075
1076 /* Resolve sequence/variant field class */
1077 switch (fc->type) {
1078 case CTF_FIELD_CLASS_TYPE_SEQUENCE:
1079 case CTF_FIELD_CLASS_TYPE_VARIANT:
1080 ret = resolve_sequence_or_variant_field_class(fc, ctx);
1081 if (ret) {
1082 BT_COMP_LOGE("Cannot resolve sequence field class's length or variant field class's tag: "
1083 "ret=%d, fc-addr=%p", ret, fc);
1084 goto end;
1085 }
1086
1087 break;
1088 default:
1089 break;
1090 }
1091
1092 /* Recurse into compound classes */
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:
1098 {
1099 uint64_t i;
1100 uint64_t field_count =
1101 ctf_field_class_compound_get_field_class_count(fc);
1102
1103 ret = field_class_stack_push(ctx->field_class_stack, fc, ctx);
1104 if (ret) {
1105 BT_COMP_LOGE("Cannot push field class on context's stack: "
1106 "fc-addr=%p", fc);
1107 goto end;
1108 }
1109
1110 for (i = 0; i < field_count; i++) {
1111 struct ctf_field_class *child_fc =
1112 ctf_field_class_compound_borrow_field_class_by_index(
1113 fc, i);
1114
1115 BT_ASSERT(child_fc);
1116
1117 if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY||
1118 fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) {
1119 field_class_stack_peek(
1120 ctx->field_class_stack)->index = -1;
1121 } else {
1122 field_class_stack_peek(
1123 ctx->field_class_stack)->index =
1124 (int64_t) i;
1125 }
1126
1127 BT_COMP_LOGD("Resolving field class's child field class: "
1128 "parent-fc-addr=%p, child-fc-addr=%p, "
1129 "index=%" PRIu64 ", count=%" PRIu64,
1130 fc, child_fc, i, field_count);
1131 ret = resolve_field_class(child_fc, ctx);
1132 if (ret) {
1133 goto end;
1134 }
1135 }
1136
1137 field_class_stack_pop(ctx->field_class_stack, ctx);
1138 break;
1139 }
1140 default:
1141 break;
1142 }
1143
1144 end:
1145 return ret;
1146 }
1147
1148 /*
1149 * Resolves the root field class corresponding to the scope `root_scope`.
1150 */
1151 static
1152 int resolve_root_class(enum ctf_scope root_scope, struct resolve_context *ctx)
1153 {
1154 int ret;
1155
1156 BT_ASSERT(field_class_stack_size(ctx->field_class_stack) == 0);
1157 ctx->root_scope = root_scope;
1158 ret = resolve_field_class(borrow_class_from_ctx(ctx, root_scope), ctx);
1159 ctx->root_scope = -1;
1160 return ret;
1161 }
1162
1163 static
1164 int resolve_event_class_field_classes(struct resolve_context *ctx,
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;
1177 ctx->scopes.event_spec_context = ec->spec_context_fc;
1178 ret = resolve_root_class(CTF_SCOPE_EVENT_COMMON_CONTEXT, ctx);
1179 if (ret) {
1180 BT_COMP_LOGE("Cannot resolve event specific context field class: "
1181 "ret=%d", ret);
1182 goto end;
1183 }
1184
1185 ctx->scopes.event_payload = ec->payload_fc;
1186 ret = resolve_root_class(CTF_SCOPE_EVENT_PAYLOAD, ctx);
1187 if (ret) {
1188 BT_COMP_LOGE("Cannot resolve event payload field class: "
1189 "ret=%d", ret);
1190 goto end;
1191 }
1192
1193 end:
1194 ctx->scopes.event_spec_context = NULL;
1195 ctx->scopes.event_payload = NULL;
1196 ctx->ec = NULL;
1197 return ret;
1198 }
1199
1200 static
1201 int resolve_stream_class_field_classes(struct resolve_context *ctx,
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) {
1213 ctx->scopes.packet_context = sc->packet_context_fc;
1214 ret = resolve_root_class(CTF_SCOPE_PACKET_CONTEXT, ctx);
1215 if (ret) {
1216 BT_COMP_LOGE("Cannot resolve packet context field class: "
1217 "ret=%d", ret);
1218 goto end;
1219 }
1220
1221 ctx->scopes.event_header = sc->event_header_fc;
1222 ret = resolve_root_class(CTF_SCOPE_EVENT_HEADER, ctx);
1223 if (ret) {
1224 BT_COMP_LOGE("Cannot resolve event header field class: "
1225 "ret=%d", ret);
1226 goto end;
1227 }
1228
1229 ctx->scopes.event_common_context = sc->event_common_context_fc;
1230 ret = resolve_root_class(CTF_SCOPE_EVENT_SPECIFIC_CONTEXT, ctx);
1231 if (ret) {
1232 BT_COMP_LOGE("Cannot resolve event common context field class: "
1233 "ret=%d", ret);
1234 goto end;
1235 }
1236 }
1237
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;
1241
1242 for (i = 0; i < sc->event_classes->len; i++) {
1243 struct ctf_event_class *ec = sc->event_classes->pdata[i];
1244
1245 ret = resolve_event_class_field_classes(ctx, ec);
1246 if (ret) {
1247 BT_COMP_LOGE("Cannot resolve event class's field classes: "
1248 "ec-id=%" PRIu64 ", ec-name=\"%s\"",
1249 ec->id, ec->name->str);
1250 goto end;
1251 }
1252 }
1253
1254 end:
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
1262 BT_HIDDEN
1263 int ctf_trace_class_resolve_field_classes(struct ctf_trace_class *tc,
1264 struct meta_log_config *log_cfg)
1265 {
1266 int ret = 0;
1267 uint64_t i;
1268 struct resolve_context local_ctx = {
1269 .log_level = log_cfg->log_level,
1270 .self_comp = log_cfg->self_comp,
1271 .tc = tc,
1272 .sc = NULL,
1273 .ec = NULL,
1274 .scopes = {
1275 .packet_header = tc->packet_header_fc,
1276 .packet_context = NULL,
1277 .event_header = NULL,
1278 .event_common_context = NULL,
1279 .event_spec_context = NULL,
1280 .event_payload = NULL,
1281 },
1282 .root_scope = CTF_SCOPE_PACKET_HEADER,
1283 .cur_fc = NULL,
1284 };
1285 struct resolve_context *ctx = &local_ctx;
1286
1287 /* Initialize class stack */
1288 ctx->field_class_stack = field_class_stack_create();
1289 if (!ctx->field_class_stack) {
1290 BT_COMP_LOGE_STR("Cannot create field class stack.");
1291 ret = -1;
1292 goto end;
1293 }
1294
1295 if (!tc->is_translated) {
1296 ctx->scopes.packet_header = tc->packet_header_fc;
1297 ret = resolve_root_class(CTF_SCOPE_PACKET_HEADER, ctx);
1298 if (ret) {
1299 BT_COMP_LOGE("Cannot resolve packet header field class: "
1300 "ret=%d", ret);
1301 goto end;
1302 }
1303 }
1304
1305 ctx->scopes.packet_header = tc->packet_header_fc;
1306
1307 for (i = 0; i < tc->stream_classes->len; i++) {
1308 struct ctf_stream_class *sc = tc->stream_classes->pdata[i];
1309
1310 ret = resolve_stream_class_field_classes(ctx, sc);
1311 if (ret) {
1312 BT_COMP_LOGE("Cannot resolve stream class's field classes: "
1313 "sc-id=%" PRIu64, sc->id);
1314 goto end;
1315 }
1316 }
1317
1318 end:
1319 field_class_stack_destroy(ctx->field_class_stack);
1320 return ret;
1321 }
This page took 0.071367 seconds and 3 git commands to generate.