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