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