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