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