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