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