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