src.ctf.fs: use DataLen in ctf_fs_ds_index_entry
[babeltrace.git] / src / ctf-writer / resolve.c
CommitLineData
16ca5ff0 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
16ca5ff0
PP
3 *
4 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 *
0235b0db 7 * Babeltrace - CTF writer: Type resolving internal
16ca5ff0
PP
8 */
9
350ad6c1 10#define BT_LOG_TAG "CTF-WRITER/RESOLVE"
67d2ce02 11#include "logging.h"
16ca5ff0 12
16ca5ff0
PP
13#include <glib.h>
14#include <inttypes.h>
15#include <limits.h>
16#include <stdlib.h>
17
217cf9d3
PP
18#include <babeltrace2-ctf-writer/field-types.h>
19#include <babeltrace2-ctf-writer/object.h>
20#include <babeltrace2-ctf-writer/stream-class.h>
578e048b 21
91d81473 22#include "common/macros.h"
578e048b
MJ
23#include "common/assert.h"
24
25#include "field-path.h"
26#include "resolve.h"
27#include "utils.h"
28#include "values.h"
29
16ca5ff0
PP
30typedef GPtrArray type_stack;
31
32/*
33 * A stack frame.
34 *
35 * `type` contains a compound field type (structure, variant, array,
36 * or sequence) and `index` indicates the index of the field type in
37 * the upper frame (-1 for array and sequence field types).
38 *
39 * `type` is owned by the stack frame.
40 */
41struct type_stack_frame {
42 struct bt_ctf_field_type_common *type;
43 int index;
44};
45
46/*
47 * The current context of the resolving engine.
48 *
49 * `scopes` contain the 6 CTF scope field types (see CTF, sect. 7.3.2)
50 * in the following order:
51 *
52 * * Packet header
53 * * Packet context
54 * * Event header
55 * * Stream event context
56 * * Event context
57 * * Event payload
58 */
59struct resolve_context {
e1e02a22 60 struct bt_ctf_private_value *environment;
16ca5ff0
PP
61 struct bt_ctf_field_type_common *scopes[6];
62
63 /* Root scope being visited */
64 enum bt_ctf_scope root_scope;
65 type_stack *type_stack;
66 struct bt_ctf_field_type_common *cur_field_type;
67};
68
69/* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */
70static const char * const absolute_path_prefixes[] = {
71 [BT_CTF_SCOPE_ENV] = "env.",
72 [BT_CTF_SCOPE_TRACE_PACKET_HEADER] = "trace.packet.header.",
73 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT] = "stream.packet.context.",
74 [BT_CTF_SCOPE_STREAM_EVENT_HEADER] = "stream.event.header.",
75 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT] = "stream.event.context.",
76 [BT_CTF_SCOPE_EVENT_CONTEXT] = "event.context.",
77 [BT_CTF_SCOPE_EVENT_FIELDS] = "event.fields.",
78};
79
80/* Number of path tokens used for the absolute prefixes */
81static const int absolute_path_prefix_ptoken_counts[] = {
82 [BT_CTF_SCOPE_ENV] = 1,
83 [BT_CTF_SCOPE_TRACE_PACKET_HEADER] = 3,
84 [BT_CTF_SCOPE_STREAM_PACKET_CONTEXT] = 3,
85 [BT_CTF_SCOPE_STREAM_EVENT_HEADER] = 3,
86 [BT_CTF_SCOPE_STREAM_EVENT_CONTEXT] = 3,
87 [BT_CTF_SCOPE_EVENT_CONTEXT] = 2,
88 [BT_CTF_SCOPE_EVENT_FIELDS] = 2,
89};
90
91/*
92 * Destroys a type stack frame.
93 */
94static
95void type_stack_destroy_notify(gpointer data)
96{
97 struct type_stack_frame *frame = data;
98
e1e02a22 99 BT_CTF_OBJECT_PUT_REF_AND_RESET(frame->type);
16ca5ff0
PP
100 g_free(frame);
101}
102
103/*
104 * Creates a type stack.
105 *
106 * Return value is owned by the caller.
107 */
108static
109type_stack *type_stack_create(void)
110{
111 return g_ptr_array_new_with_free_func(type_stack_destroy_notify);
112}
113
114/*
115 * Destroys a type stack.
116 */
117static
118void type_stack_destroy(type_stack *stack)
119{
120 g_ptr_array_free(stack, TRUE);
121}
122
123/*
124 * Pushes a field type onto a type stack.
125 *
126 * `type` is owned by the caller (stack frame gets a new reference).
127 */
128static
129int type_stack_push(type_stack *stack, struct bt_ctf_field_type_common *type)
130{
131 int ret = 0;
132 struct type_stack_frame *frame = NULL;
133
134 if (!stack || !type) {
135 BT_LOGW("Invalid parameter: stack or type is NULL.");
136 ret = -1;
137 goto end;
138 }
139
140 frame = g_new0(struct type_stack_frame, 1);
141 if (!frame) {
142 BT_LOGE_STR("Failed to allocate one field type stack frame.");
143 ret = -1;
144 goto end;
145 }
146
ef267d12 147 BT_LOGT("Pushing field type on context's stack: "
16ca5ff0 148 "ft-addr=%p, stack-size-before=%u", type, stack->len);
e1e02a22 149 frame->type = bt_ctf_object_get_ref(type);
16ca5ff0
PP
150 g_ptr_array_add(stack, frame);
151
152end:
153 return ret;
154}
155
156/*
157 * Checks whether or not `stack` is empty.
158 */
159static
00409097 160bt_ctf_bool type_stack_empty(type_stack *stack)
16ca5ff0
PP
161{
162 return stack->len == 0;
163}
164
165/*
166 * Returns the number of frames in `stack`.
167 */
168static
169size_t type_stack_size(type_stack *stack)
170{
171 return stack->len;
172}
173
174/*
175 * Returns the top frame of `stack`.
176 *
177 * Return value is owned by `stack`.
178 */
179static
180struct type_stack_frame *type_stack_peek(type_stack *stack)
181{
8fe10fa8
SM
182 BT_ASSERT(stack);
183 BT_ASSERT(!type_stack_empty(stack));
16ca5ff0 184
8fe10fa8 185 return g_ptr_array_index(stack, stack->len - 1);
16ca5ff0
PP
186}
187
188/*
189 * Returns the frame at index `index` in `stack`.
190 *
191 * Return value is owned by `stack`.
192 */
193static
8fe10fa8 194struct type_stack_frame *type_stack_at(type_stack *stack, size_t index)
16ca5ff0 195{
8fe10fa8
SM
196 BT_ASSERT(stack);
197 BT_ASSERT(index < stack->len);
16ca5ff0 198
8fe10fa8 199 return g_ptr_array_index(stack, index);
16ca5ff0
PP
200}
201
202/*
203 * Removes the top frame of `stack`.
204 */
205static
206void type_stack_pop(type_stack *stack)
207{
208 if (!type_stack_empty(stack)) {
209 /*
210 * This will call the frame's destructor and free it, as
211 * well as put its contained field type.
212 */
ef267d12 213 BT_LOGT("Popping context's stack: stack-size-before=%u",
16ca5ff0
PP
214 stack->len);
215 g_ptr_array_set_size(stack, stack->len - 1);
216 }
217}
218
219/*
220 * Returns the scope field type of `scope` in the context `ctx`.
221 *
222 * Return value is owned by `ctx` on success.
223 */
224static
225struct bt_ctf_field_type_common *get_type_from_ctx(struct resolve_context *ctx,
226 enum bt_ctf_scope scope)
227{
98b15851 228 BT_ASSERT_DBG(scope >= BT_CTF_SCOPE_TRACE_PACKET_HEADER &&
16ca5ff0
PP
229 scope <= BT_CTF_SCOPE_EVENT_FIELDS);
230
231 return ctx->scopes[scope - BT_CTF_SCOPE_TRACE_PACKET_HEADER];
232}
233
234/*
235 * Returns the CTF scope from a path string. May return
236 * CTF_NODE_UNKNOWN if the path is found to be relative.
237 */
238static
239enum bt_ctf_scope get_root_scope_from_absolute_pathstr(const char *pathstr)
240{
241 enum bt_ctf_scope scope;
242 enum bt_ctf_scope ret = BT_CTF_SCOPE_UNKNOWN;
243 const size_t prefixes_count = sizeof(absolute_path_prefixes) /
244 sizeof(*absolute_path_prefixes);
245
246 for (scope = BT_CTF_SCOPE_ENV; scope < BT_CTF_SCOPE_ENV +
247 prefixes_count; scope++) {
248 /*
249 * Chech if path string starts with a known absolute
250 * path prefix.
251 *
252 * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES.
253 */
254 if (strncmp(pathstr, absolute_path_prefixes[scope],
255 strlen(absolute_path_prefixes[scope]))) {
256 /* Prefix does not match: try the next one */
ef267d12 257 BT_LOGT("Prefix does not match: trying the next one: "
16ca5ff0
PP
258 "path=\"%s\", path-prefix=\"%s\", scope=%s",
259 pathstr, absolute_path_prefixes[scope],
260 bt_ctf_scope_string(scope));
261 continue;
262 }
263
264 /* Found it! */
265 ret = scope;
ef267d12 266 BT_LOGT("Found root scope from absolute path: "
16ca5ff0
PP
267 "path=\"%s\", scope=%s", pathstr,
268 bt_ctf_scope_string(scope));
269 goto end;
270 }
271
272end:
273 return ret;
274}
275
276/*
277 * Destroys a path token.
278 */
279static
ecd7492f
MJ
280void ptokens_destroy_func(gpointer ptoken,
281 gpointer data __attribute__((unused)))
16ca5ff0
PP
282{
283 g_string_free(ptoken, TRUE);
284}
285
286/*
287 * Destroys a path token list.
288 */
289static
290void 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 */
303static
304const 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 */
318static
319GList *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
351error:
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 */
366static
367int 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;
00409097 372 bt_ctf_bool first_level_done = BT_CTF_FALSE;
16ca5ff0
PP
373
374 /* Get our own reference */
e1e02a22 375 bt_ctf_object_get_ref(type);
16ca5ff0
PP
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
ef267d12 385 BT_LOGT("Current path token: token=\"%s\"", field_name);
16ca5ff0
PP
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);
00409097 415 first_level_done = BT_CTF_TRUE;
16ca5ff0
PP
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 */
e1e02a22
PP
433 bt_ctf_object_get_ref(child_type);
434 BT_CTF_OBJECT_MOVE_REF(type, child_type);
16ca5ff0
PP
435 }
436
437end:
e1e02a22 438 bt_ctf_object_put_ref(type);
16ca5ff0
PP
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 */
449static
450int 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
476end:
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 */
487static
488int 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
ef267d12 511 BT_LOGT("Locating target field type from current parent field type: "
16ca5ff0
PP
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 */
ef267d12 520 BT_LOGT_STR("Not found at this point.");
16ca5ff0
PP
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
00409097 528 while (BT_CTF_TRUE) {
16ca5ff0
PP
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++) {
d50d46f3 544 int index = bt_g_array_index(
16ca5ff0
PP
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
ef267d12 565 BT_LOGT("Looking into potential root scope: scope=%s",
16ca5ff0
PP
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 */
ef267d12 578 BT_LOGT_STR("Not found in this scope.");
16ca5ff0
PP
579 field_path->root--;
580 continue;
581 }
582
583 /* Found */
ef267d12 584 BT_LOGT_STR("Found in this scope.");
16ca5ff0
PP
585 break;
586 }
587 }
588
589end:
e1e02a22 590 BT_CTF_OBJECT_PUT_REF_AND_RESET(tail_field_path);
16ca5ff0
PP
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 */
600static
601struct 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;
ef267d12 632 BT_LOGT("Detected relative path: starting with current root scope: "
16ca5ff0
PP
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;
ef267d12 650 BT_LOGT("Detected absolute path: using root scope: "
16ca5ff0
PP
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 =
39630c3b 665 field_path_pretty ? field_path_pretty->str : "(null)";
16ca5ff0 666
ef267d12 667 BT_LOGT("Found field path: path=\"%s\", field-path=\"%s\"",
16ca5ff0
PP
668 pathstr, field_path_pretty_str);
669
670 if (field_path_pretty) {
671 g_string_free(field_path_pretty, TRUE);
672 }
673 }
674
675end:
676 if (ret) {
e1e02a22 677 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_path);
16ca5ff0
PP
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 */
690static
691struct 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);
e1e02a22 700 bt_ctf_object_get_ref(type);
16ca5ff0
PP
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 =
d50d46f3 712 bt_g_array_index(field_path->indexes, int, i);
16ca5ff0
PP
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 */
e1e02a22
PP
724 bt_ctf_object_get_ref(child_type);
725 BT_CTF_OBJECT_MOVE_REF(type, child_type);
16ca5ff0
PP
726 }
727
728 return type;
729
730error:
e1e02a22 731 BT_CTF_OBJECT_PUT_REF_AND_RESET(type);
16ca5ff0
PP
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 */
740static
741struct 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
764error:
e1e02a22 765 BT_CTF_OBJECT_PUT_REF_AND_RESET(field_path);
16ca5ff0
PP
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 */
775static
776int 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
ef267d12 782 if (BT_LOG_ON_TRACE) {
16ca5ff0
PP
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 =
39630c3b 788 field_path1_pretty ? field_path1_pretty->str : "(null)";
16ca5ff0 789 const char *field_path2_pretty_str =
39630c3b 790 field_path2_pretty ? field_path2_pretty->str : "(null)";
16ca5ff0 791
ef267d12 792 BT_LOGT("Finding lowest common ancestor (LCA) between two field paths: "
16ca5ff0
PP
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 */
98b15851 808 BT_ASSERT_DBG(field_path1->root == field_path2->root);
16ca5ff0
PP
809 field_path1_len = field_path1->indexes->len;
810 field_path2_len = field_path2->indexes->len;
811
00409097 812 while (BT_CTF_TRUE) {
16ca5ff0
PP
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
d50d46f3 830 target_index = bt_g_array_index(field_path1->indexes, int,
16ca5ff0 831 lca_index);
d50d46f3 832 ctx_index = bt_g_array_index(field_path2->indexes, int,
16ca5ff0
PP
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
ef267d12 843 BT_LOGT("Found LCA: lca-index=%d", lca_index);
16ca5ff0
PP
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 */
852static
853int 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 */
d50d46f3 913 target_index = bt_g_array_index(target_field_path->indexes,
16ca5ff0 914 int, lca_index);
d50d46f3 915 ctx_index = bt_g_array_index(ctx_field_path->indexes,
16ca5ff0
PP
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:
498e7994 957 bt_common_abort();
16ca5ff0
PP
958 }
959
960end:
e1e02a22 961 BT_CTF_OBJECT_PUT_REF_AND_RESET(ctx_field_path);
16ca5ff0
PP
962 return ret;
963}
964
965/*
966 * Resolves a variant or sequence field type `type`.
967 *
968 * `type` is owned by the caller.
969 */
970static
971int 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:
498e7994 994 bt_common_abort();
16ca5ff0
PP
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) {
e7401568 1051 BT_LOGW("Cannot set variant field type's tag field path: "
16ca5ff0
PP
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) {
e7401568 1061 BT_LOGW("Cannot set variant field type's tag field type: "
16ca5ff0
PP
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:
498e7994 1069 bt_common_abort();
16ca5ff0
PP
1070 }
1071
1072end:
1073 if (target_field_path_pretty) {
1074 g_string_free(target_field_path_pretty, TRUE);
1075 }
1076
e1e02a22
PP
1077 BT_CTF_OBJECT_PUT_REF_AND_RESET(target_field_path);
1078 BT_CTF_OBJECT_PUT_REF_AND_RESET(target_type);
16ca5ff0
PP
1079 return ret;
1080}
1081
1082/*
1083 * Resolves a field type `type`.
1084 *
1085 * `type` is owned by the caller.
1086 */
1087static
1088int 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
ef267d12 1163 BT_LOGT("Resolving field type's child field type: "
16ca5ff0
PP
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
1180end:
1181 return ret;
1182}
1183
1184/*
1185 * Resolves the root field type corresponding to the scope `root_scope`.
1186 */
1187static
1188int resolve_root_type(enum bt_ctf_scope root_scope, struct resolve_context *ctx)
1189{
1190 int ret;
1191
98b15851 1192 BT_ASSERT_DBG(type_stack_size(ctx->type_stack) == 0);
16ca5ff0
PP
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
16ca5ff0 1200int bt_ctf_resolve_types(
e1e02a22 1201 struct bt_ctf_private_value *environment,
16ca5ff0
PP
1202 struct bt_ctf_field_type_common *packet_header_type,
1203 struct bt_ctf_field_type_common *packet_context_type,
1204 struct bt_ctf_field_type_common *event_header_type,
1205 struct bt_ctf_field_type_common *stream_event_ctx_type,
1206 struct bt_ctf_field_type_common *event_context_type,
1207 struct bt_ctf_field_type_common *event_payload_type,
1208 enum bt_ctf_resolve_flag flags)
1209{
1210 int ret = 0;
1211 struct resolve_context ctx = {
1212 .environment = environment,
1213 .scopes = {
1214 packet_header_type,
1215 packet_context_type,
1216 event_header_type,
1217 stream_event_ctx_type,
1218 event_context_type,
1219 event_payload_type,
1220 },
1221 .root_scope = BT_CTF_SCOPE_UNKNOWN,
1222 };
1223
ef267d12 1224 BT_LOGT("Resolving field types: "
16ca5ff0
PP
1225 "packet-header-ft-addr=%p, "
1226 "packet-context-ft-addr=%p, "
1227 "event-header-ft-addr=%p, "
1228 "stream-event-context-ft-addr=%p, "
1229 "event-context-ft-addr=%p, "
1230 "event-payload-ft-addr=%p",
1231 packet_header_type, packet_context_type, event_header_type,
1232 stream_event_ctx_type, event_context_type, event_payload_type);
1233
1234 /* Initialize type stack */
1235 ctx.type_stack = type_stack_create();
1236 if (!ctx.type_stack) {
1237 BT_LOGE_STR("Cannot create field type stack.");
1238 ret = -1;
1239 goto end;
1240 }
1241
1242 /* Resolve packet header type */
1243 if (flags & BT_CTF_RESOLVE_FLAG_PACKET_HEADER) {
1244 ret = resolve_root_type(BT_CTF_SCOPE_TRACE_PACKET_HEADER, &ctx);
1245 if (ret) {
1246 BT_LOGW("Cannot resolve trace packet header field type: "
1247 "ret=%d", ret);
1248 goto end;
1249 }
1250 }
1251
1252 /* Resolve packet context type */
1253 if (flags & BT_CTF_RESOLVE_FLAG_PACKET_CONTEXT) {
1254 ret = resolve_root_type(BT_CTF_SCOPE_STREAM_PACKET_CONTEXT, &ctx);
1255 if (ret) {
1256 BT_LOGW("Cannot resolve stream packet context field type: "
1257 "ret=%d", ret);
1258 goto end;
1259 }
1260 }
1261
1262 /* Resolve event header type */
1263 if (flags & BT_CTF_RESOLVE_FLAG_EVENT_HEADER) {
1264 ret = resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_HEADER, &ctx);
1265 if (ret) {
1266 BT_LOGW("Cannot resolve stream event header field type: "
1267 "ret=%d", ret);
1268 goto end;
1269 }
1270 }
1271
1272 /* Resolve stream event context type */
1273 if (flags & BT_CTF_RESOLVE_FLAG_STREAM_EVENT_CTX) {
1274 ret = resolve_root_type(BT_CTF_SCOPE_STREAM_EVENT_CONTEXT, &ctx);
1275 if (ret) {
1276 BT_LOGW("Cannot resolve stream event context field type: "
1277 "ret=%d", ret);
1278 goto end;
1279 }
1280 }
1281
1282 /* Resolve event context type */
1283 if (flags & BT_CTF_RESOLVE_FLAG_EVENT_CONTEXT) {
1284 ret = resolve_root_type(BT_CTF_SCOPE_EVENT_CONTEXT, &ctx);
1285 if (ret) {
1286 BT_LOGW("Cannot resolve event context field type: "
1287 "ret=%d", ret);
1288 goto end;
1289 }
1290 }
1291
1292 /* Resolve event payload type */
1293 if (flags & BT_CTF_RESOLVE_FLAG_EVENT_PAYLOAD) {
1294 ret = resolve_root_type(BT_CTF_SCOPE_EVENT_FIELDS, &ctx);
1295 if (ret) {
1296 BT_LOGW("Cannot resolve event payload field type: "
1297 "ret=%d", ret);
1298 goto end;
1299 }
1300 }
1301
ef267d12 1302 BT_LOGT_STR("Resolved field types.");
16ca5ff0
PP
1303
1304end:
1305 type_stack_destroy(ctx.type_stack);
1306
1307 return ret;
1308}
This page took 0.18616 seconds and 4 git commands to generate.