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