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