configure: re-enable '-Wunused-parameter'
[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,
282 gpointer data __attribute__((unused)))
283 {
284 g_string_free(ptoken, TRUE);
285 }
286
287 /*
288 * Destroys a path token list.
289 */
290 static
291 void ptokens_destroy(GList *ptokens)
292 {
293 if (!ptokens) {
294 return;
295 }
296
297 g_list_foreach(ptokens, ptokens_destroy_func, NULL);
298 g_list_free(ptokens);
299 }
300
301 /*
302 * Returns the string contained in a path token.
303 */
304 static
305 const char *ptoken_get_string(GList *ptoken)
306 {
307 GString *tokenstr = (GString *) ptoken->data;
308
309 return tokenstr->str;
310 }
311
312 /*
313 * Converts a path string to a path token list, that is, splits the
314 * individual words of a path string into a list of individual
315 * strings.
316 *
317 * Return value is owned by the caller on success.
318 */
319 static
320 GList *pathstr_to_ptokens(const char *pathstr)
321 {
322 const char *at = pathstr;
323 const char *last = at;
324 GList *ptokens = NULL;
325
326 for (;;) {
327 if (*at == '.' || *at == '\0') {
328 GString *tokenstr;
329
330 if (at == last) {
331 /* Error: empty token */
332 BT_LOGW("Empty path token: path=\"%s\", pos=%u",
333 pathstr, (int) (at - pathstr));
334 goto error;
335 }
336
337 tokenstr = g_string_new(NULL);
338 g_string_append_len(tokenstr, last, at - last);
339 ptokens = g_list_append(ptokens, tokenstr);
340 last = at + 1;
341 }
342
343 if (*at == '\0') {
344 break;
345 }
346
347 at++;
348 }
349
350 return ptokens;
351
352 error:
353 ptokens_destroy(ptokens);
354 return NULL;
355 }
356
357 /*
358 * Converts a path token list to a field path object. The path token
359 * list is relative from `type`. The index of the source looking for
360 * its target within `type` is indicated by `src_index`. This can be
361 * `INT_MAX` if the source is contained in `type`.
362 *
363 * `ptokens` is owned by the caller. `field_path` is an output parameter
364 * owned by the caller that must be filled here. `type` is owned by the
365 * caller.
366 */
367 static
368 int ptokens_to_field_path(GList *ptokens, struct bt_ctf_field_path *field_path,
369 struct bt_ctf_field_type_common *type, int src_index)
370 {
371 int ret = 0;
372 GList *cur_ptoken = ptokens;
373 bt_ctf_bool first_level_done = BT_CTF_FALSE;
374
375 /* Get our own reference */
376 bt_ctf_object_get_ref(type);
377
378 /* Locate target */
379 while (cur_ptoken) {
380 int child_index;
381 struct bt_ctf_field_type_common *child_type;
382 const char *field_name = ptoken_get_string(cur_ptoken);
383 enum bt_ctf_field_type_id type_id =
384 bt_ctf_field_type_common_get_type_id(type);
385
386 BT_LOGT("Current path token: token=\"%s\"", field_name);
387
388 /* Find to which index corresponds the current path token */
389 if (type_id == BT_CTF_FIELD_TYPE_ID_ARRAY ||
390 type_id == BT_CTF_FIELD_TYPE_ID_SEQUENCE) {
391 child_index = -1;
392 } else {
393 child_index = bt_ctf_field_type_common_get_field_index(type,
394 field_name);
395 if (child_index < 0) {
396 /*
397 * Error: field name does not exist or
398 * wrong current type.
399 */
400 BT_LOGW("Cannot get index of field type: "
401 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
402 field_name, src_index, child_index, first_level_done);
403 ret = -1;
404 goto end;
405 } else if (child_index > src_index &&
406 !first_level_done) {
407 BT_LOGW("Child field type is located after source field type: "
408 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
409 field_name, src_index, child_index, first_level_done);
410 ret = -1;
411 goto end;
412 }
413
414 /* Next path token */
415 cur_ptoken = g_list_next(cur_ptoken);
416 first_level_done = BT_CTF_TRUE;
417 }
418
419 /* Create new field path entry */
420 g_array_append_val(field_path->indexes, child_index);
421
422 /* Get child field type */
423 child_type = bt_ctf_field_type_common_borrow_field_at_index(type,
424 child_index);
425 if (!child_type) {
426 BT_LOGW("Cannot get child field type: "
427 "field-name=\"%s\", src-index=%d, child-index=%d, first-level-done=%d",
428 field_name, src_index, child_index, first_level_done);
429 ret = -1;
430 goto end;
431 }
432
433 /* Move child type to current type */
434 bt_ctf_object_get_ref(child_type);
435 BT_CTF_OBJECT_MOVE_REF(type, child_type);
436 }
437
438 end:
439 bt_ctf_object_put_ref(type);
440 return ret;
441 }
442
443 /*
444 * Converts a known absolute path token list to a field path object
445 * within the resolving context `ctx`.
446 *
447 * `ptokens` is owned by the caller. `field_path` is an output parameter
448 * owned by the caller that must be filled here.
449 */
450 static
451 int absolute_ptokens_to_field_path(GList *ptokens,
452 struct bt_ctf_field_path *field_path,
453 struct resolve_context *ctx)
454 {
455 int ret = 0;
456 GList *cur_ptoken;
457 struct bt_ctf_field_type_common *type;
458
459 /* Skip absolute path tokens */
460 cur_ptoken = g_list_nth(ptokens,
461 absolute_path_prefix_ptoken_counts[field_path->root]);
462
463 /* Start with root type */
464 type = get_type_from_ctx(ctx, field_path->root);
465 if (!type) {
466 /* Error: root type is not available */
467 BT_LOGW("Root field type is not available: "
468 "root-scope=%s",
469 bt_ctf_scope_string(field_path->root));
470 ret = -1;
471 goto end;
472 }
473
474 /* Locate target */
475 ret = ptokens_to_field_path(cur_ptoken, field_path, type, INT_MAX);
476
477 end:
478 return ret;
479 }
480
481 /*
482 * Converts a known relative path token list to a field path object
483 * within the resolving context `ctx`.
484 *
485 * `ptokens` is owned by the caller. `field_path` is an output parameter
486 * owned by the caller that must be filled here.
487 */
488 static
489 int relative_ptokens_to_field_path(GList *ptokens,
490 struct bt_ctf_field_path *field_path,
491 struct resolve_context *ctx)
492 {
493 int ret = 0;
494 int parent_pos_in_stack;
495 struct bt_ctf_field_path *tail_field_path = bt_ctf_field_path_create();
496
497 if (!tail_field_path) {
498 BT_LOGE_STR("Cannot create empty field path.");
499 ret = -1;
500 goto end;
501 }
502
503 parent_pos_in_stack = type_stack_size(ctx->type_stack) - 1;
504
505 while (parent_pos_in_stack >= 0) {
506 struct bt_ctf_field_type_common *parent_type =
507 type_stack_at(ctx->type_stack,
508 parent_pos_in_stack)->type;
509 int cur_index = type_stack_at(ctx->type_stack,
510 parent_pos_in_stack)->index;
511
512 BT_LOGT("Locating target field type from current parent field type: "
513 "parent-pos=%d, parent-ft-addr=%p, cur-index=%d",
514 parent_pos_in_stack, parent_type, cur_index);
515
516 /* Locate target from current parent type */
517 ret = ptokens_to_field_path(ptokens, tail_field_path,
518 parent_type, cur_index);
519 if (ret) {
520 /* Not found... yet */
521 BT_LOGT_STR("Not found at this point.");
522 bt_ctf_field_path_clear(tail_field_path);
523 } else {
524 /* Found: stitch tail field path to head field path */
525 int i = 0;
526 int tail_field_path_len =
527 tail_field_path->indexes->len;
528
529 while (BT_CTF_TRUE) {
530 struct bt_ctf_field_type_common *cur_type =
531 type_stack_at(ctx->type_stack, i)->type;
532 int index = type_stack_at(
533 ctx->type_stack, i)->index;
534
535 if (cur_type == parent_type) {
536 break;
537 }
538
539 g_array_append_val(field_path->indexes,
540 index);
541 i++;
542 }
543
544 for (i = 0; i < tail_field_path_len; i++) {
545 int index = bt_g_array_index(
546 tail_field_path->indexes,
547 int, i);
548
549 g_array_append_val(field_path->indexes,
550 index);
551 }
552 break;
553 }
554
555 parent_pos_in_stack--;
556 }
557
558 if (parent_pos_in_stack < 0) {
559 /* Not found: look in previous scopes */
560 field_path->root--;
561
562 while (field_path->root >= BT_CTF_SCOPE_TRACE_PACKET_HEADER) {
563 struct bt_ctf_field_type_common *root_type;
564 bt_ctf_field_path_clear(field_path);
565
566 BT_LOGT("Looking into potential root scope: scope=%s",
567 bt_ctf_scope_string(field_path->root));
568 root_type = get_type_from_ctx(ctx, field_path->root);
569 if (!root_type) {
570 field_path->root--;
571 continue;
572 }
573
574 /* Locate target in previous scope */
575 ret = ptokens_to_field_path(ptokens, field_path,
576 root_type, INT_MAX);
577 if (ret) {
578 /* Not found yet */
579 BT_LOGT_STR("Not found in this scope.");
580 field_path->root--;
581 continue;
582 }
583
584 /* Found */
585 BT_LOGT_STR("Found in this scope.");
586 break;
587 }
588 }
589
590 end:
591 BT_CTF_OBJECT_PUT_REF_AND_RESET(tail_field_path);
592 return ret;
593 }
594
595 /*
596 * Converts a path string to a field path object within the resolving
597 * context `ctx`.
598 *
599 * Return value is owned by the caller on success.
600 */
601 static
602 struct bt_ctf_field_path *pathstr_to_field_path(const char *pathstr,
603 struct resolve_context *ctx)
604 {
605 int ret;
606 enum bt_ctf_scope root_scope;
607 GList *ptokens = NULL;
608 struct bt_ctf_field_path *field_path = NULL;
609
610 /* Create field path */
611 field_path = bt_ctf_field_path_create();
612 if (!field_path) {
613 BT_LOGE_STR("Cannot create empty field path.");
614 ret = -1;
615 goto end;
616 }
617
618 /* Convert path string to path tokens */
619 ptokens = pathstr_to_ptokens(pathstr);
620 if (!ptokens) {
621 BT_LOGW("Cannot convert path string to path tokens: "
622 "path=\"%s\"", pathstr);
623 ret = -1;
624 goto end;
625 }
626
627 /* Absolute or relative path? */
628 root_scope = get_root_scope_from_absolute_pathstr(pathstr);
629
630 if (root_scope == BT_CTF_SCOPE_UNKNOWN) {
631 /* Relative path: start with current root scope */
632 field_path->root = ctx->root_scope;
633 BT_LOGT("Detected relative path: starting with current root scope: "
634 "scope=%s", bt_ctf_scope_string(field_path->root));
635 ret = relative_ptokens_to_field_path(ptokens, field_path, ctx);
636 if (ret) {
637 BT_LOGW("Cannot get relative field path of path string: "
638 "path=\"%s\", start-scope=%s, end-scope=%s",
639 pathstr, bt_ctf_scope_string(ctx->root_scope),
640 bt_ctf_scope_string(field_path->root));
641 goto end;
642 }
643 } else if (root_scope == BT_CTF_SCOPE_ENV) {
644 BT_LOGW("Sequence field types referring the trace environment are not supported as of this version: "
645 "path=\"%s\"", pathstr);
646 ret = -1;
647 goto end;
648 } else {
649 /* Absolute path: use found root scope */
650 field_path->root = root_scope;
651 BT_LOGT("Detected absolute path: using root scope: "
652 "scope=%s", bt_ctf_scope_string(field_path->root));
653 ret = absolute_ptokens_to_field_path(ptokens, field_path, ctx);
654 if (ret) {
655 BT_LOGW("Cannot get absolute field path of path string: "
656 "path=\"%s\", root-scope=%s",
657 pathstr, bt_ctf_scope_string(root_scope));
658 goto end;
659 }
660 }
661
662 if (ret == 0) {
663 GString *field_path_pretty =
664 bt_ctf_field_path_string(field_path);
665 const char *field_path_pretty_str =
666 field_path_pretty ? field_path_pretty->str : "(null)";
667
668 BT_LOGT("Found field path: path=\"%s\", field-path=\"%s\"",
669 pathstr, field_path_pretty_str);
670
671 if (field_path_pretty) {
672 g_string_free(field_path_pretty, TRUE);
673 }
674 }
675
676 end:
677 if (ret) {
678 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_path);
679 }
680
681 ptokens_destroy(ptokens);
682 return field_path;
683 }
684
685 /*
686 * Retrieves a field type by following the field path `field_path` in
687 * the resolving context `ctx`.
688 *
689 * Return value is owned by the caller on success.
690 */
691 static
692 struct bt_ctf_field_type_common *field_path_to_field_type(
693 struct bt_ctf_field_path *field_path,
694 struct resolve_context *ctx)
695 {
696 int i;
697 struct bt_ctf_field_type_common *type;
698
699 /* Start with root type */
700 type = get_type_from_ctx(ctx, field_path->root);
701 bt_ctf_object_get_ref(type);
702 if (!type) {
703 /* Error: root type is not available */
704 BT_LOGW("Root field type is not available: root-scope=%s",
705 bt_ctf_scope_string(field_path->root));
706 goto error;
707 }
708
709 /* Locate target */
710 for (i = 0; i < field_path->indexes->len; i++) {
711 struct bt_ctf_field_type_common *child_type;
712 int child_index =
713 bt_g_array_index(field_path->indexes, int, i);
714
715 /* Get child field type */
716 child_type = bt_ctf_field_type_common_borrow_field_at_index(type,
717 child_index);
718 if (!child_type) {
719 BT_LOGW("Cannot get field type: "
720 "parent-ft-addr=%p, index=%d", type, i);
721 goto error;
722 }
723
724 /* Move child type to current type */
725 bt_ctf_object_get_ref(child_type);
726 BT_CTF_OBJECT_MOVE_REF(type, child_type);
727 }
728
729 return type;
730
731 error:
732 BT_CTF_OBJECT_PUT_REF_AND_RESET(type);
733 return type;
734 }
735
736 /*
737 * Returns the equivalent field path object of the context type stack.
738 *
739 * Return value is owned by the caller on success.
740 */
741 static
742 struct bt_ctf_field_path *get_ctx_stack_field_path(struct resolve_context *ctx)
743 {
744 int i;
745 struct bt_ctf_field_path *field_path;
746
747 /* Create field path */
748 field_path = bt_ctf_field_path_create();
749 if (!field_path) {
750 BT_LOGE_STR("Cannot create empty field path.");
751 goto error;
752 }
753
754 field_path->root = ctx->root_scope;
755
756 for (i = 0; i < type_stack_size(ctx->type_stack); i++) {
757 struct type_stack_frame *frame;
758
759 frame = type_stack_at(ctx->type_stack, i);
760 g_array_append_val(field_path->indexes, frame->index);
761 }
762
763 return field_path;
764
765 error:
766 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_path);
767 return field_path;
768 }
769
770 /*
771 * Returns the lowest common ancestor of two field path objects
772 * having the same root scope.
773 *
774 * `field_path1` and `field_path2` are owned by the caller.
775 */
776 static
777 int get_field_paths_lca_index(struct bt_ctf_field_path *field_path1,
778 struct bt_ctf_field_path *field_path2)
779 {
780 int lca_index = 0;
781 int field_path1_len, field_path2_len;
782
783 if (BT_LOG_ON_TRACE) {
784 GString *field_path1_pretty =
785 bt_ctf_field_path_string(field_path1);
786 GString *field_path2_pretty =
787 bt_ctf_field_path_string(field_path2);
788 const char *field_path1_pretty_str =
789 field_path1_pretty ? field_path1_pretty->str : "(null)";
790 const char *field_path2_pretty_str =
791 field_path2_pretty ? field_path2_pretty->str : "(null)";
792
793 BT_LOGT("Finding lowest common ancestor (LCA) between two field paths: "
794 "field-path-1=\"%s\", field-path-2=\"%s\"",
795 field_path1_pretty_str, field_path2_pretty_str);
796
797 if (field_path1_pretty) {
798 g_string_free(field_path1_pretty, TRUE);
799 }
800
801 if (field_path2_pretty) {
802 g_string_free(field_path2_pretty, TRUE);
803 }
804 }
805
806 /*
807 * Start from both roots and find the first mismatch.
808 */
809 BT_ASSERT_DBG(field_path1->root == field_path2->root);
810 field_path1_len = field_path1->indexes->len;
811 field_path2_len = field_path2->indexes->len;
812
813 while (BT_CTF_TRUE) {
814 int target_index, ctx_index;
815
816 if (lca_index == field_path2_len ||
817 lca_index == field_path1_len) {
818 /*
819 * This means that both field paths never split.
820 * This is invalid because the target cannot be
821 * an ancestor of the source.
822 */
823 BT_LOGW("Source field type is an ancestor of target field type or vice versa: "
824 "lca-index=%d, field-path-1-len=%d, "
825 "field-path-2-len=%d",
826 lca_index, field_path1_len, field_path2_len);
827 lca_index = -1;
828 break;
829 }
830
831 target_index = bt_g_array_index(field_path1->indexes, int,
832 lca_index);
833 ctx_index = bt_g_array_index(field_path2->indexes, int,
834 lca_index);
835
836 if (target_index != ctx_index) {
837 /* LCA index is the previous */
838 break;
839 }
840
841 lca_index++;
842 }
843
844 BT_LOGT("Found LCA: lca-index=%d", lca_index);
845 return lca_index;
846 }
847
848 /*
849 * Validates a target field path.
850 *
851 * `target_field_path` and `target_type` are owned by the caller.
852 */
853 static
854 int validate_target_field_path(struct bt_ctf_field_path *target_field_path,
855 struct bt_ctf_field_type_common *target_type,
856 struct resolve_context *ctx)
857 {
858 int ret = 0;
859 struct bt_ctf_field_path *ctx_field_path;
860 int target_field_path_len = target_field_path->indexes->len;
861 int lca_index;
862 enum bt_ctf_field_type_id ctx_cur_field_type_id;
863 enum bt_ctf_field_type_id target_type_id;
864
865 /* Get context field path */
866 ctx_field_path = get_ctx_stack_field_path(ctx);
867 if (!ctx_field_path) {
868 BT_LOGW_STR("Cannot get field path from context's stack.");
869 ret = -1;
870 goto end;
871 }
872
873 /*
874 * Make sure the target is not a root.
875 */
876 if (target_field_path_len == 0) {
877 BT_LOGW_STR("Target field path's length is 0 (targeting the root).");
878 ret = -1;
879 goto end;
880 }
881
882 /*
883 * Make sure the root of the target field path is not located
884 * after the context field path's root.
885 */
886 if (target_field_path->root > ctx_field_path->root) {
887 BT_LOGW("Target field type is located after source field type: "
888 "target-root=%s, source-root=%s",
889 bt_ctf_scope_string(target_field_path->root),
890 bt_ctf_scope_string(ctx_field_path->root));
891 ret = -1;
892 goto end;
893 }
894
895 if (target_field_path->root == ctx_field_path->root) {
896 int target_index, ctx_index;
897
898 /*
899 * Find the index of the lowest common ancestor of both field
900 * paths.
901 */
902 lca_index = get_field_paths_lca_index(target_field_path,
903 ctx_field_path);
904 if (lca_index < 0) {
905 BT_LOGW_STR("Cannot get least common ancestor.");
906 ret = -1;
907 goto end;
908 }
909
910 /*
911 * Make sure the target field path is located before the
912 * context field path.
913 */
914 target_index = bt_g_array_index(target_field_path->indexes,
915 int, lca_index);
916 ctx_index = bt_g_array_index(ctx_field_path->indexes,
917 int, lca_index);
918
919 if (target_index >= ctx_index) {
920 BT_LOGW("Target field type's index is greater than or equal to source field type's index in LCA: "
921 "lca-index=%d, target-index=%d, source-index=%d",
922 lca_index, target_index, ctx_index);
923 ret = -1;
924 goto end;
925 }
926 }
927
928 /*
929 * Make sure the target type has the right type and properties.
930 */
931 ctx_cur_field_type_id = bt_ctf_field_type_common_get_type_id(
932 ctx->cur_field_type);
933 target_type_id = bt_ctf_field_type_common_get_type_id(target_type);
934
935 switch (ctx_cur_field_type_id) {
936 case BT_CTF_FIELD_TYPE_ID_VARIANT:
937 if (target_type_id != BT_CTF_FIELD_TYPE_ID_ENUM) {
938 BT_LOGW("Variant field type's tag field type is not an enumeration field type: "
939 "tag-ft-addr=%p, tag-ft-id=%s",
940 target_type,
941 bt_ctf_field_type_id_string(target_type_id));
942 ret = -1;
943 goto end;
944 }
945 break;
946 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
947 if (target_type_id != BT_CTF_FIELD_TYPE_ID_INTEGER ||
948 bt_ctf_field_type_common_integer_is_signed(target_type)) {
949 BT_LOGW("Sequence field type's length field type is not an unsigned integer field type: "
950 "length-ft-addr=%p, length-ft-id=%s",
951 target_type,
952 bt_ctf_field_type_id_string(target_type_id));
953 ret = -1;
954 goto end;
955 }
956 break;
957 default:
958 bt_common_abort();
959 }
960
961 end:
962 BT_CTF_OBJECT_PUT_REF_AND_RESET(ctx_field_path);
963 return ret;
964 }
965
966 /*
967 * Resolves a variant or sequence field type `type`.
968 *
969 * `type` is owned by the caller.
970 */
971 static
972 int resolve_sequence_or_variant_type(struct bt_ctf_field_type_common *type,
973 struct resolve_context *ctx)
974 {
975 int ret = 0;
976 const char *pathstr;
977 enum bt_ctf_field_type_id type_id = bt_ctf_field_type_common_get_type_id(type);
978 struct bt_ctf_field_path *target_field_path = NULL;
979 struct bt_ctf_field_type_common *target_type = NULL;
980 GString *target_field_path_pretty = NULL;
981 const char *target_field_path_pretty_str;
982
983
984 /* Get path string */
985 switch (type_id) {
986 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
987 pathstr =
988 bt_ctf_field_type_common_sequence_get_length_field_name(type);
989 break;
990 case BT_CTF_FIELD_TYPE_ID_VARIANT:
991 pathstr =
992 bt_ctf_field_type_common_variant_get_tag_name(type);
993 break;
994 default:
995 bt_common_abort();
996 }
997
998 if (!pathstr) {
999 BT_LOGW_STR("Cannot get path string.");
1000 ret = -1;
1001 goto end;
1002 }
1003
1004 /* Get target field path out of path string */
1005 target_field_path = pathstr_to_field_path(pathstr, ctx);
1006 if (!target_field_path) {
1007 BT_LOGW("Cannot get target field path for path string: "
1008 "path=\"%s\"", pathstr);
1009 ret = -1;
1010 goto end;
1011 }
1012
1013 target_field_path_pretty = bt_ctf_field_path_string(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 type */
1018 target_type = field_path_to_field_type(target_field_path, ctx);
1019 if (!target_type) {
1020 BT_LOGW("Cannot get target field type 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, target_type, ctx);
1028 if (ret) {
1029 BT_LOGW("Invalid target field path for path string: "
1030 "path=\"%s\", target-field-path=\"%s\"",
1031 pathstr, target_field_path_pretty_str);
1032 goto end;
1033 }
1034
1035 /* Set target field path and target field type */
1036 switch (type_id) {
1037 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
1038 ret = bt_ctf_field_type_common_sequence_set_length_field_path(
1039 type, target_field_path);
1040 if (ret) {
1041 BT_LOGW("Cannot set sequence field type's length field path: "
1042 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1043 ret, type, pathstr,
1044 target_field_path_pretty_str);
1045 goto end;
1046 }
1047 break;
1048 case BT_CTF_FIELD_TYPE_ID_VARIANT:
1049 ret = bt_ctf_field_type_common_variant_set_tag_field_path(
1050 type, target_field_path);
1051 if (ret) {
1052 BT_LOGW("Cannot set varaint field type's tag field path: "
1053 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1054 ret, type, pathstr,
1055 target_field_path_pretty_str);
1056 goto end;
1057 }
1058
1059 ret = bt_ctf_field_type_common_variant_set_tag_field_type(
1060 type, target_type);
1061 if (ret) {
1062 BT_LOGW("Cannot set varaint field type's tag field type: "
1063 "ret=%d, ft-addr=%p, path=\"%s\", target-field-path=\"%s\"",
1064 ret, type, pathstr,
1065 target_field_path_pretty_str);
1066 goto end;
1067 }
1068 break;
1069 default:
1070 bt_common_abort();
1071 }
1072
1073 end:
1074 if (target_field_path_pretty) {
1075 g_string_free(target_field_path_pretty, TRUE);
1076 }
1077
1078 BT_CTF_OBJECT_PUT_REF_AND_RESET(target_field_path);
1079 BT_CTF_OBJECT_PUT_REF_AND_RESET(target_type);
1080 return ret;
1081 }
1082
1083 /*
1084 * Resolves a field type `type`.
1085 *
1086 * `type` is owned by the caller.
1087 */
1088 static
1089 int resolve_type(struct bt_ctf_field_type_common *type, struct resolve_context *ctx)
1090 {
1091 int ret = 0;
1092 enum bt_ctf_field_type_id type_id;
1093
1094 if (!type) {
1095 /* Type is not available; still valid */
1096 goto end;
1097 }
1098
1099 type_id = bt_ctf_field_type_common_get_type_id(type);
1100 ctx->cur_field_type = type;
1101
1102 /* Resolve sequence/variant field type */
1103 switch (type_id) {
1104 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
1105 case BT_CTF_FIELD_TYPE_ID_VARIANT:
1106 ret = resolve_sequence_or_variant_type(type, ctx);
1107 if (ret) {
1108 BT_LOGW("Cannot resolve sequence field type's length or variant field type's tag: "
1109 "ret=%d, ft-addr=%p", ret, type);
1110 goto end;
1111 }
1112 break;
1113 default:
1114 break;
1115 }
1116
1117 /* Recurse into compound types */
1118 switch (type_id) {
1119 case BT_CTF_FIELD_TYPE_ID_STRUCT:
1120 case BT_CTF_FIELD_TYPE_ID_VARIANT:
1121 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
1122 case BT_CTF_FIELD_TYPE_ID_ARRAY:
1123 {
1124 int64_t field_count, f_index;
1125
1126 ret = type_stack_push(ctx->type_stack, type);
1127 if (ret) {
1128 BT_LOGW("Cannot push field type on context's stack: "
1129 "ft-addr=%p", type);
1130 goto end;
1131 }
1132
1133 field_count = bt_ctf_field_type_common_get_field_count(type);
1134 if (field_count < 0) {
1135 BT_LOGW("Cannot get field type's field count: "
1136 "ret=%" PRId64 ", ft-addr=%p",
1137 field_count, type);
1138 ret = field_count;
1139 goto end;
1140 }
1141
1142 for (f_index = 0; f_index < field_count; f_index++) {
1143 struct bt_ctf_field_type_common *child_type =
1144 bt_ctf_field_type_common_borrow_field_at_index(type,
1145 f_index);
1146
1147 if (!child_type) {
1148 BT_LOGW("Cannot get field type's child field: "
1149 "ft-addr=%p, index=%" PRId64 ", "
1150 "count=%" PRId64, type, f_index,
1151 field_count);
1152 ret = -1;
1153 goto end;
1154 }
1155
1156 if (type_id == BT_CTF_FIELD_TYPE_ID_ARRAY||
1157 type_id == BT_CTF_FIELD_TYPE_ID_SEQUENCE) {
1158 type_stack_peek(ctx->type_stack)->index = -1;
1159 } else {
1160 type_stack_peek(ctx->type_stack)->index =
1161 f_index;
1162 }
1163
1164 BT_LOGT("Resolving field type's child field type: "
1165 "parent-ft-addr=%p, child-ft-addr=%p, "
1166 "index=%" PRId64 ", count=%" PRId64,
1167 type, child_type, f_index, field_count);
1168 ret = resolve_type(child_type, ctx);
1169 if (ret) {
1170 goto end;
1171 }
1172 }
1173
1174 type_stack_pop(ctx->type_stack);
1175 break;
1176 }
1177 default:
1178 break;
1179 }
1180
1181 end:
1182 return ret;
1183 }
1184
1185 /*
1186 * Resolves the root field type corresponding to the scope `root_scope`.
1187 */
1188 static
1189 int resolve_root_type(enum bt_ctf_scope root_scope, struct resolve_context *ctx)
1190 {
1191 int ret;
1192
1193 BT_ASSERT_DBG(type_stack_size(ctx->type_stack) == 0);
1194 ctx->root_scope = root_scope;
1195 ret = resolve_type(get_type_from_ctx(ctx, root_scope), ctx);
1196 ctx->root_scope = BT_CTF_SCOPE_UNKNOWN;
1197
1198 return ret;
1199 }
1200
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.061069 seconds and 4 git commands to generate.