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