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