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