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 | ||
16 | #define BT_LOG_TAG "PLUGIN-CTF-METADATA-META-RESOLVE" | |
17 | #include "logging.h" | |
18 | ||
71c5da58 | 19 | #include <babeltrace2/babeltrace.h> |
57952005 MJ |
20 | #include "common/babeltrace.h" |
21 | #include "common/assert.h" | |
22 | #include "common/common.h" | |
7b33a0e0 PP |
23 | #include <glib.h> |
24 | #include <stdint.h> | |
25 | #include <string.h> | |
26 | #include <inttypes.h> | |
27 | #include <limits.h> | |
28 | #include <stdlib.h> | |
29 | #include <glib.h> | |
30 | ||
31 | #include "ctf-meta-visitors.h" | |
32 | ||
939190b3 | 33 | typedef GPtrArray field_class_stack; |
7b33a0e0 PP |
34 | |
35 | /* | |
36 | * A stack frame. | |
37 | * | |
939190b3 PP |
38 | * `fc` contains a compound field class (structure, variant, array, |
39 | * or sequence) and `index` indicates the index of the field class in | |
40 | * the upper frame (-1 for array and sequence field classes). `name` | |
41 | * indicates the name of the field class in the upper frame (empty | |
42 | * string for array and sequence field classes). | |
7b33a0e0 | 43 | */ |
939190b3 PP |
44 | struct field_class_stack_frame { |
45 | struct ctf_field_class *fc; | |
7b33a0e0 PP |
46 | int64_t index; |
47 | }; | |
48 | ||
49 | /* | |
50 | * The current context of the resolving engine. | |
51 | */ | |
52 | struct resolve_context { | |
53 | struct ctf_trace_class *tc; | |
54 | struct ctf_stream_class *sc; | |
55 | struct ctf_event_class *ec; | |
56 | ||
57 | struct { | |
939190b3 PP |
58 | struct ctf_field_class *packet_header; |
59 | struct ctf_field_class *packet_context; | |
60 | struct ctf_field_class *event_header; | |
61 | struct ctf_field_class *event_common_context; | |
62 | struct ctf_field_class *event_spec_context; | |
63 | struct ctf_field_class *event_payload; | |
7b33a0e0 PP |
64 | } scopes; |
65 | ||
66 | /* Root scope being visited */ | |
0476f6f7 | 67 | enum ctf_scope root_scope; |
939190b3 PP |
68 | field_class_stack *field_class_stack; |
69 | struct ctf_field_class *cur_fc; | |
7b33a0e0 PP |
70 | }; |
71 | ||
72 | /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */ | |
73 | static const char * const absolute_path_prefixes[] = { | |
0476f6f7 PP |
74 | [CTF_SCOPE_PACKET_HEADER] = "trace.packet.header.", |
75 | [CTF_SCOPE_PACKET_CONTEXT] = "stream.packet.context.", | |
76 | [CTF_SCOPE_EVENT_HEADER] = "stream.event.header.", | |
77 | [CTF_SCOPE_EVENT_COMMON_CONTEXT] = "stream.event.context.", | |
78 | [CTF_SCOPE_EVENT_SPECIFIC_CONTEXT] = "event.context.", | |
79 | [CTF_SCOPE_EVENT_PAYLOAD] = "event.fields.", | |
7b33a0e0 PP |
80 | }; |
81 | ||
82 | /* Number of path tokens used for the absolute prefixes */ | |
83 | static const uint64_t absolute_path_prefix_ptoken_counts[] = { | |
0476f6f7 PP |
84 | [CTF_SCOPE_PACKET_HEADER] = 3, |
85 | [CTF_SCOPE_PACKET_CONTEXT] = 3, | |
86 | [CTF_SCOPE_EVENT_HEADER] = 3, | |
87 | [CTF_SCOPE_EVENT_COMMON_CONTEXT] = 3, | |
88 | [CTF_SCOPE_EVENT_SPECIFIC_CONTEXT] = 2, | |
89 | [CTF_SCOPE_EVENT_PAYLOAD] = 2, | |
7b33a0e0 PP |
90 | }; |
91 | ||
92 | static | |
939190b3 | 93 | void destroy_field_class_stack_frame(struct field_class_stack_frame *frame) |
7b33a0e0 PP |
94 | { |
95 | if (!frame) { | |
96 | return; | |
97 | } | |
98 | ||
99 | g_free(frame); | |
100 | } | |
101 | ||
102 | /* | |
939190b3 | 103 | * Creates a class stack. |
7b33a0e0 PP |
104 | */ |
105 | static | |
939190b3 | 106 | field_class_stack *field_class_stack_create(void) |
7b33a0e0 PP |
107 | { |
108 | return g_ptr_array_new_with_free_func( | |
939190b3 | 109 | (GDestroyNotify) destroy_field_class_stack_frame); |
7b33a0e0 PP |
110 | } |
111 | ||
112 | /* | |
939190b3 | 113 | * Destroys a class stack. |
7b33a0e0 PP |
114 | */ |
115 | static | |
939190b3 | 116 | void field_class_stack_destroy(field_class_stack *stack) |
7b33a0e0 PP |
117 | { |
118 | if (stack) { | |
119 | g_ptr_array_free(stack, TRUE); | |
120 | } | |
121 | } | |
122 | ||
123 | /* | |
939190b3 | 124 | * Pushes a field class onto a class stack. |
7b33a0e0 PP |
125 | */ |
126 | static | |
939190b3 | 127 | int field_class_stack_push(field_class_stack *stack, struct ctf_field_class *fc) |
7b33a0e0 PP |
128 | { |
129 | int ret = 0; | |
939190b3 | 130 | struct field_class_stack_frame *frame = NULL; |
7b33a0e0 | 131 | |
939190b3 PP |
132 | if (!stack || !fc) { |
133 | BT_LOGE("Invalid parameter: stack or field class is NULL."); | |
7b33a0e0 PP |
134 | ret = -1; |
135 | goto end; | |
136 | } | |
137 | ||
939190b3 | 138 | frame = g_new0(struct field_class_stack_frame, 1); |
7b33a0e0 | 139 | if (!frame) { |
939190b3 | 140 | BT_LOGE_STR("Failed to allocate one field class stack frame."); |
7b33a0e0 PP |
141 | ret = -1; |
142 | goto end; | |
143 | } | |
144 | ||
939190b3 PP |
145 | BT_LOGV("Pushing field class on context's stack: " |
146 | "fc-addr=%p, stack-size-before=%u", fc, stack->len); | |
147 | frame->fc = fc; | |
7b33a0e0 PP |
148 | g_ptr_array_add(stack, frame); |
149 | ||
150 | end: | |
151 | return ret; | |
152 | } | |
153 | ||
154 | /* | |
155 | * Checks whether or not `stack` is empty. | |
156 | */ | |
157 | static | |
939190b3 | 158 | bool field_class_stack_empty(field_class_stack *stack) |
7b33a0e0 PP |
159 | { |
160 | return stack->len == 0; | |
161 | } | |
162 | ||
163 | /* | |
164 | * Returns the number of frames in `stack`. | |
165 | */ | |
166 | static | |
939190b3 | 167 | size_t field_class_stack_size(field_class_stack *stack) |
7b33a0e0 PP |
168 | { |
169 | return stack->len; | |
170 | } | |
171 | ||
172 | /* | |
173 | * Returns the top frame of `stack`. | |
174 | */ | |
175 | static | |
939190b3 | 176 | struct field_class_stack_frame *field_class_stack_peek(field_class_stack *stack) |
7b33a0e0 | 177 | { |
939190b3 | 178 | struct field_class_stack_frame *entry = NULL; |
7b33a0e0 | 179 | |
939190b3 | 180 | if (!stack || field_class_stack_empty(stack)) { |
7b33a0e0 PP |
181 | goto end; |
182 | } | |
183 | ||
184 | entry = g_ptr_array_index(stack, stack->len - 1); | |
185 | end: | |
186 | return entry; | |
187 | } | |
188 | ||
189 | /* | |
190 | * Returns the frame at index `index` in `stack`. | |
191 | */ | |
192 | static | |
939190b3 | 193 | struct field_class_stack_frame *field_class_stack_at(field_class_stack *stack, |
7b33a0e0 PP |
194 | size_t index) |
195 | { | |
939190b3 | 196 | struct field_class_stack_frame *entry = NULL; |
7b33a0e0 PP |
197 | |
198 | if (!stack || index >= stack->len) { | |
199 | goto end; | |
200 | } | |
201 | ||
202 | entry = g_ptr_array_index(stack, index); | |
203 | ||
204 | end: | |
205 | return entry; | |
206 | } | |
207 | ||
208 | /* | |
209 | * Removes the top frame of `stack`. | |
210 | */ | |
211 | static | |
939190b3 | 212 | void field_class_stack_pop(field_class_stack *stack) |
7b33a0e0 | 213 | { |
939190b3 | 214 | if (!field_class_stack_empty(stack)) { |
7b33a0e0 PP |
215 | /* |
216 | * This will call the frame's destructor and free it, as | |
939190b3 | 217 | * well as put its contained field class. |
7b33a0e0 PP |
218 | */ |
219 | BT_LOGV("Popping context's stack: stack-size-before=%u", | |
220 | stack->len); | |
221 | g_ptr_array_set_size(stack, stack->len - 1); | |
222 | } | |
223 | } | |
224 | ||
225 | /* | |
939190b3 | 226 | * Returns the scope field class of `scope` in the context `ctx`. |
7b33a0e0 PP |
227 | */ |
228 | static | |
939190b3 | 229 | struct ctf_field_class *borrow_class_from_ctx(struct resolve_context *ctx, |
0476f6f7 | 230 | enum ctf_scope scope) |
7b33a0e0 PP |
231 | { |
232 | switch (scope) { | |
0476f6f7 | 233 | case CTF_SCOPE_PACKET_HEADER: |
7b33a0e0 | 234 | return ctx->scopes.packet_header; |
0476f6f7 | 235 | case CTF_SCOPE_PACKET_CONTEXT: |
7b33a0e0 | 236 | return ctx->scopes.packet_context; |
0476f6f7 | 237 | case CTF_SCOPE_EVENT_HEADER: |
7b33a0e0 | 238 | return ctx->scopes.event_header; |
0476f6f7 | 239 | case CTF_SCOPE_EVENT_COMMON_CONTEXT: |
7b33a0e0 | 240 | return ctx->scopes.event_common_context; |
0476f6f7 | 241 | case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT: |
7b33a0e0 | 242 | return ctx->scopes.event_spec_context; |
0476f6f7 | 243 | case CTF_SCOPE_EVENT_PAYLOAD: |
7b33a0e0 PP |
244 | return ctx->scopes.event_payload; |
245 | default: | |
246 | abort(); | |
247 | } | |
248 | ||
249 | return NULL; | |
250 | } | |
251 | ||
252 | /* | |
253 | * Returns the CTF scope from a path string. May return -1 if the path | |
254 | * is found to be relative. | |
255 | */ | |
256 | static | |
0476f6f7 | 257 | enum ctf_scope get_root_scope_from_absolute_pathstr(const char *pathstr) |
7b33a0e0 | 258 | { |
0476f6f7 PP |
259 | enum ctf_scope scope; |
260 | enum ctf_scope ret = -1; | |
7b33a0e0 PP |
261 | const size_t prefixes_count = sizeof(absolute_path_prefixes) / |
262 | sizeof(*absolute_path_prefixes); | |
263 | ||
0476f6f7 | 264 | for (scope = CTF_SCOPE_PACKET_HEADER; scope < CTF_SCOPE_PACKET_HEADER + |
7b33a0e0 PP |
265 | prefixes_count; scope++) { |
266 | /* | |
267 | * Chech if path string starts with a known absolute | |
268 | * path prefix. | |
269 | * | |
270 | * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES. | |
271 | */ | |
272 | if (strncmp(pathstr, absolute_path_prefixes[scope], | |
273 | strlen(absolute_path_prefixes[scope]))) { | |
274 | /* Prefix does not match: try the next one */ | |
275 | BT_LOGV("Prefix does not match: trying the next one: " | |
276 | "path=\"%s\", path-prefix=\"%s\", scope=%s", | |
277 | pathstr, absolute_path_prefixes[scope], | |
c0d5e998 | 278 | ctf_scope_string(scope)); |
7b33a0e0 PP |
279 | continue; |
280 | } | |
281 | ||
282 | /* Found it! */ | |
283 | ret = scope; | |
284 | BT_LOGV("Found root scope from absolute path: " | |
285 | "path=\"%s\", scope=%s", pathstr, | |
c0d5e998 | 286 | ctf_scope_string(scope)); |
7b33a0e0 PP |
287 | goto end; |
288 | } | |
289 | ||
290 | end: | |
291 | return ret; | |
292 | } | |
293 | ||
294 | /* | |
295 | * Destroys a path token. | |
296 | */ | |
297 | static | |
298 | void ptokens_destroy_func(gpointer ptoken, gpointer data) | |
299 | { | |
300 | g_string_free(ptoken, TRUE); | |
301 | } | |
302 | ||
303 | /* | |
304 | * Destroys a path token list. | |
305 | */ | |
306 | static | |
307 | void ptokens_destroy(GList *ptokens) | |
308 | { | |
309 | if (!ptokens) { | |
310 | return; | |
311 | } | |
312 | ||
313 | g_list_foreach(ptokens, ptokens_destroy_func, NULL); | |
314 | g_list_free(ptokens); | |
315 | } | |
316 | ||
317 | /* | |
318 | * Returns the string contained in a path token. | |
319 | */ | |
320 | static | |
321 | const char *ptoken_get_string(GList *ptoken) | |
322 | { | |
323 | GString *tokenstr = (GString *) ptoken->data; | |
324 | ||
325 | return tokenstr->str; | |
326 | } | |
327 | ||
328 | /* | |
329 | * Converts a path string to a path token list, that is, splits the | |
330 | * individual words of a path string into a list of individual | |
331 | * strings. | |
332 | */ | |
333 | static | |
334 | GList *pathstr_to_ptokens(const char *pathstr) | |
335 | { | |
336 | const char *at = pathstr; | |
337 | const char *last = at; | |
338 | GList *ptokens = NULL; | |
339 | ||
340 | for (;;) { | |
341 | if (*at == '.' || *at == '\0') { | |
342 | GString *tokenstr; | |
343 | ||
344 | if (at == last) { | |
345 | /* Error: empty token */ | |
346 | BT_LOGE("Empty path token: path=\"%s\", pos=%u", | |
347 | pathstr, (unsigned int) (at - pathstr)); | |
348 | goto error; | |
349 | } | |
350 | ||
351 | tokenstr = g_string_new(NULL); | |
352 | g_string_append_len(tokenstr, last, at - last); | |
353 | ptokens = g_list_append(ptokens, tokenstr); | |
354 | last = at + 1; | |
355 | } | |
356 | ||
357 | if (*at == '\0') { | |
358 | break; | |
359 | } | |
360 | ||
361 | at++; | |
362 | } | |
363 | ||
364 | return ptokens; | |
365 | ||
366 | error: | |
367 | ptokens_destroy(ptokens); | |
368 | return NULL; | |
369 | } | |
370 | ||
371 | /* | |
372 | * Converts a path token list to a field path object. The path token | |
939190b3 PP |
373 | * list is relative from `fc`. The index of the source looking for its |
374 | * target within `fc` is indicated by `src_index`. This can be `INT64_MAX` | |
375 | * if the source is contained in `fc`. | |
7b33a0e0 PP |
376 | * |
377 | * `field_path` is an output parameter owned by the caller that must be | |
378 | * filled here. | |
379 | */ | |
380 | static | |
381 | int ptokens_to_field_path(GList *ptokens, struct ctf_field_path *field_path, | |
939190b3 | 382 | struct ctf_field_class *fc, int64_t src_index) |
7b33a0e0 PP |
383 | { |
384 | int ret = 0; | |
385 | GList *cur_ptoken = ptokens; | |
386 | bool first_level_done = false; | |
387 | ||
388 | /* Locate target */ | |
389 | while (cur_ptoken) { | |
390 | int64_t child_index; | |
939190b3 | 391 | struct ctf_field_class *child_fc; |
7b33a0e0 PP |
392 | const char *ft_name = ptoken_get_string(cur_ptoken); |
393 | ||
394 | BT_LOGV("Current path token: token=\"%s\"", ft_name); | |
395 | ||
396 | /* Find to which index corresponds the current path token */ | |
af0c18e3 PP |
397 | if (fc->type == CTF_FIELD_CLASS_TYPE_ARRAY || |
398 | fc->type == CTF_FIELD_CLASS_TYPE_SEQUENCE) { | |
7b33a0e0 PP |
399 | child_index = -1; |
400 | } else { | |
401 | child_index = | |
939190b3 PP |
402 | ctf_field_class_compound_get_field_class_index_from_name( |
403 | fc, ft_name); | |
7b33a0e0 PP |
404 | if (child_index < 0) { |
405 | /* | |
406 | * Error: field name does not exist or | |
939190b3 | 407 | * wrong current class. |
7b33a0e0 | 408 | */ |
939190b3 | 409 | BT_LOGV("Cannot get index of field class: " |
7b33a0e0 PP |
410 | "field-name=\"%s\", " |
411 | "src-index=%" PRId64 ", " | |
412 | "child-index=%" PRId64 ", " | |
413 | "first-level-done=%d", | |
414 | ft_name, src_index, child_index, | |
415 | first_level_done); | |
416 | ret = -1; | |
417 | goto end; | |
418 | } else if (child_index > src_index && | |
419 | !first_level_done) { | |
939190b3 | 420 | BT_LOGV("Child field class is located after source field class: " |
7b33a0e0 PP |
421 | "field-name=\"%s\", " |
422 | "src-index=%" PRId64 ", " | |
423 | "child-index=%" PRId64 ", " | |
424 | "first-level-done=%d", | |
425 | ft_name, src_index, child_index, | |
426 | first_level_done); | |
427 | ret = -1; | |
428 | goto end; | |
429 | } | |
430 | ||
431 | /* Next path token */ | |
432 | cur_ptoken = g_list_next(cur_ptoken); | |
433 | first_level_done = true; | |
434 | } | |
435 | ||
436 | /* Create new field path entry */ | |
437 | ctf_field_path_append_index(field_path, child_index); | |
438 | ||
939190b3 PP |
439 | /* Get child field class */ |
440 | child_fc = ctf_field_class_compound_borrow_field_class_by_index( | |
441 | fc, child_index); | |
442 | BT_ASSERT(child_fc); | |
7b33a0e0 | 443 | |
939190b3 PP |
444 | /* Move child class to current class */ |
445 | fc = child_fc; | |
7b33a0e0 PP |
446 | } |
447 | ||
448 | end: | |
449 | return ret; | |
450 | } | |
451 | ||
452 | /* | |
453 | * Converts a known absolute path token list to a field path object | |
454 | * within the resolving context `ctx`. | |
455 | * | |
456 | * `field_path` is an output parameter owned by the caller that must be | |
457 | * filled here. | |
458 | */ | |
459 | static | |
460 | int absolute_ptokens_to_field_path(GList *ptokens, | |
461 | struct ctf_field_path *field_path, | |
462 | struct resolve_context *ctx) | |
463 | { | |
464 | int ret = 0; | |
465 | GList *cur_ptoken; | |
939190b3 | 466 | struct ctf_field_class *fc; |
7b33a0e0 PP |
467 | |
468 | /* | |
469 | * Make sure we're not referring to a scope within a translated | |
470 | * object. | |
471 | */ | |
472 | switch (field_path->root) { | |
0476f6f7 | 473 | case CTF_SCOPE_PACKET_HEADER: |
7b33a0e0 PP |
474 | if (ctx->tc->is_translated) { |
475 | BT_LOGE("Trace class is already translated: " | |
476 | "root-scope=%s", | |
c0d5e998 | 477 | ctf_scope_string(field_path->root)); |
7b33a0e0 PP |
478 | ret = -1; |
479 | goto end; | |
480 | } | |
481 | ||
482 | break; | |
0476f6f7 PP |
483 | case CTF_SCOPE_PACKET_CONTEXT: |
484 | case CTF_SCOPE_EVENT_HEADER: | |
485 | case CTF_SCOPE_EVENT_COMMON_CONTEXT: | |
7b33a0e0 PP |
486 | if (!ctx->sc) { |
487 | BT_LOGE("No current stream class: " | |
488 | "root-scope=%s", | |
c0d5e998 | 489 | ctf_scope_string(field_path->root)); |
7b33a0e0 PP |
490 | ret = -1; |
491 | goto end; | |
492 | } | |
493 | ||
494 | if (ctx->sc->is_translated) { | |
495 | BT_LOGE("Stream class is already translated: " | |
496 | "root-scope=%s", | |
c0d5e998 | 497 | ctf_scope_string(field_path->root)); |
7b33a0e0 PP |
498 | ret = -1; |
499 | goto end; | |
500 | } | |
501 | ||
502 | break; | |
0476f6f7 PP |
503 | case CTF_SCOPE_EVENT_SPECIFIC_CONTEXT: |
504 | case CTF_SCOPE_EVENT_PAYLOAD: | |
7b33a0e0 PP |
505 | if (!ctx->ec) { |
506 | BT_LOGE("No current event class: " | |
507 | "root-scope=%s", | |
c0d5e998 | 508 | ctf_scope_string(field_path->root)); |
7b33a0e0 PP |
509 | ret = -1; |
510 | goto end; | |
511 | } | |
512 | ||
513 | if (ctx->ec->is_translated) { | |
514 | BT_LOGE("Event class is already translated: " | |
515 | "root-scope=%s", | |
c0d5e998 | 516 | ctf_scope_string(field_path->root)); |
7b33a0e0 PP |
517 | ret = -1; |
518 | goto end; | |
519 | } | |
520 | ||
521 | break; | |
522 | ||
523 | default: | |
524 | abort(); | |
525 | } | |
526 | ||
527 | /* Skip absolute path tokens */ | |
528 | cur_ptoken = g_list_nth(ptokens, | |
529 | absolute_path_prefix_ptoken_counts[field_path->root]); | |
530 | ||
939190b3 PP |
531 | /* Start with root class */ |
532 | fc = borrow_class_from_ctx(ctx, field_path->root); | |
533 | if (!fc) { | |
534 | /* Error: root class is not available */ | |
535 | BT_LOGE("Root field class is not available: " | |
7b33a0e0 | 536 | "root-scope=%s", |
c0d5e998 | 537 | ctf_scope_string(field_path->root)); |
7b33a0e0 PP |
538 | ret = -1; |
539 | goto end; | |
540 | } | |
541 | ||
542 | /* Locate target */ | |
939190b3 | 543 | ret = ptokens_to_field_path(cur_ptoken, field_path, fc, INT64_MAX); |
7b33a0e0 PP |
544 | |
545 | end: | |
546 | return ret; | |
547 | } | |
548 | ||
549 | /* | |
550 | * Converts a known relative path token list to a field path object | |
551 | * within the resolving context `ctx`. | |
552 | * | |
553 | * `field_path` is an output parameter owned by the caller that must be | |
554 | * filled here. | |
555 | */ | |
556 | static | |
557 | int relative_ptokens_to_field_path(GList *ptokens, | |
558 | struct ctf_field_path *field_path, struct resolve_context *ctx) | |
559 | { | |
560 | int ret = 0; | |
561 | int64_t parent_pos_in_stack; | |
562 | struct ctf_field_path tail_field_path; | |
563 | ||
564 | ctf_field_path_init(&tail_field_path); | |
939190b3 | 565 | parent_pos_in_stack = field_class_stack_size(ctx->field_class_stack) - 1; |
7b33a0e0 PP |
566 | |
567 | while (parent_pos_in_stack >= 0) { | |
939190b3 PP |
568 | struct ctf_field_class *parent_class = |
569 | field_class_stack_at(ctx->field_class_stack, | |
570 | parent_pos_in_stack)->fc; | |
571 | int64_t cur_index = field_class_stack_at(ctx->field_class_stack, | |
7b33a0e0 PP |
572 | parent_pos_in_stack)->index; |
573 | ||
939190b3 PP |
574 | BT_LOGV("Locating target field class from current parent field class: " |
575 | "parent-pos=%" PRId64 ", parent-fc-addr=%p, " | |
7b33a0e0 | 576 | "cur-index=%" PRId64, |
939190b3 | 577 | parent_pos_in_stack, parent_class, cur_index); |
7b33a0e0 | 578 | |
939190b3 | 579 | /* Locate target from current parent class */ |
7b33a0e0 | 580 | ret = ptokens_to_field_path(ptokens, &tail_field_path, |
939190b3 | 581 | parent_class, cur_index); |
7b33a0e0 PP |
582 | if (ret) { |
583 | /* Not found... yet */ | |
584 | BT_LOGV_STR("Not found at this point."); | |
585 | ctf_field_path_clear(&tail_field_path); | |
586 | } else { | |
587 | /* Found: stitch tail field path to head field path */ | |
588 | uint64_t i = 0; | |
589 | size_t tail_field_path_len = | |
590 | tail_field_path.path->len; | |
591 | ||
592 | while (BT_TRUE) { | |
939190b3 PP |
593 | struct ctf_field_class *cur_class = |
594 | field_class_stack_at( | |
595 | ctx->field_class_stack, i)->fc; | |
596 | int64_t index = field_class_stack_at( | |
597 | ctx->field_class_stack, i)->index; | |
7b33a0e0 | 598 | |
939190b3 | 599 | if (cur_class == parent_class) { |
7b33a0e0 PP |
600 | break; |
601 | } | |
602 | ||
603 | ctf_field_path_append_index(field_path, | |
604 | index); | |
605 | i++; | |
606 | } | |
607 | ||
608 | for (i = 0; i < tail_field_path_len; i++) { | |
609 | int64_t index = | |
610 | ctf_field_path_borrow_index_by_index( | |
611 | &tail_field_path, i); | |
612 | ||
613 | ctf_field_path_append_index(field_path, | |
614 | (int64_t) index); | |
615 | } | |
616 | break; | |
617 | } | |
618 | ||
619 | parent_pos_in_stack--; | |
620 | } | |
621 | ||
622 | if (parent_pos_in_stack < 0) { | |
623 | /* Not found */ | |
624 | ret = -1; | |
625 | } | |
626 | ||
627 | ctf_field_path_fini(&tail_field_path); | |
628 | return ret; | |
629 | } | |
630 | ||
631 | /* | |
632 | * Converts a path string to a field path object within the resolving | |
633 | * context `ctx`. | |
634 | */ | |
635 | static | |
636 | int pathstr_to_field_path(const char *pathstr, | |
637 | struct ctf_field_path *field_path, struct resolve_context *ctx) | |
638 | { | |
639 | int ret = 0; | |
0476f6f7 | 640 | enum ctf_scope root_scope; |
7b33a0e0 PP |
641 | GList *ptokens = NULL; |
642 | ||
643 | /* Convert path string to path tokens */ | |
644 | ptokens = pathstr_to_ptokens(pathstr); | |
645 | if (!ptokens) { | |
646 | BT_LOGE("Cannot convert path string to path tokens: " | |
647 | "path=\"%s\"", pathstr); | |
648 | ret = -1; | |
649 | goto end; | |
650 | } | |
651 | ||
652 | /* Absolute or relative path? */ | |
653 | root_scope = get_root_scope_from_absolute_pathstr(pathstr); | |
654 | ||
655 | if (root_scope == -1) { | |
656 | /* Relative path: start with current root scope */ | |
657 | field_path->root = ctx->root_scope; | |
658 | BT_LOGV("Detected relative path: starting with current root scope: " | |
c0d5e998 | 659 | "scope=%s", ctf_scope_string(field_path->root)); |
7b33a0e0 PP |
660 | ret = relative_ptokens_to_field_path(ptokens, field_path, ctx); |
661 | if (ret) { | |
662 | BT_LOGE("Cannot get relative field path of path string: " | |
663 | "path=\"%s\", start-scope=%s, end-scope=%s", | |
c0d5e998 FD |
664 | pathstr, ctf_scope_string(ctx->root_scope), |
665 | ctf_scope_string(field_path->root)); | |
7b33a0e0 PP |
666 | goto end; |
667 | } | |
668 | } else { | |
669 | /* Absolute path: use found root scope */ | |
670 | field_path->root = root_scope; | |
671 | BT_LOGV("Detected absolute path: using root scope: " | |
c0d5e998 | 672 | "scope=%s", ctf_scope_string(field_path->root)); |
7b33a0e0 PP |
673 | ret = absolute_ptokens_to_field_path(ptokens, field_path, ctx); |
674 | if (ret) { | |
675 | BT_LOGE("Cannot get absolute field path of path string: " | |
676 | "path=\"%s\", root-scope=%s", | |
c0d5e998 | 677 | pathstr, ctf_scope_string(root_scope)); |
7b33a0e0 PP |
678 | goto end; |
679 | } | |
680 | } | |
681 | ||
682 | if (BT_LOG_ON_VERBOSE && ret == 0) { | |
683 | GString *field_path_pretty = ctf_field_path_string(field_path); | |
684 | const char *field_path_pretty_str = | |
685 | field_path_pretty ? field_path_pretty->str : NULL; | |
686 | ||
687 | BT_LOGV("Found field path: path=\"%s\", field-path=\"%s\"", | |
688 | pathstr, field_path_pretty_str); | |
689 | ||
690 | if (field_path_pretty) { | |
691 | g_string_free(field_path_pretty, TRUE); | |
692 | } | |
693 | } | |
694 | ||
695 | end: | |
696 | ptokens_destroy(ptokens); | |
697 | return ret; | |
698 | } | |
699 | ||
700 | /* | |
939190b3 | 701 | * Retrieves a field class by following the field path `field_path` in |
7b33a0e0 PP |
702 | * the resolving context `ctx`. |
703 | */ | |
704 | static | |
939190b3 | 705 | struct ctf_field_class *field_path_to_field_class( |
7b33a0e0 PP |
706 | struct ctf_field_path *field_path, struct resolve_context *ctx) |
707 | { | |
708 | uint64_t i; | |
939190b3 | 709 | struct ctf_field_class *fc; |
7b33a0e0 | 710 | |
939190b3 PP |
711 | /* Start with root class */ |
712 | fc = borrow_class_from_ctx(ctx, field_path->root); | |
713 | if (!fc) { | |
714 | /* Error: root class is not available */ | |
715 | BT_LOGE("Root field class is not available: root-scope=%s", | |
c0d5e998 | 716 | ctf_scope_string(field_path->root)); |
7b33a0e0 PP |
717 | goto end; |
718 | } | |
719 | ||
720 | /* Locate target */ | |
721 | for (i = 0; i < field_path->path->len; i++) { | |
939190b3 | 722 | struct ctf_field_class *child_fc; |
7b33a0e0 PP |
723 | int64_t child_index = |
724 | ctf_field_path_borrow_index_by_index(field_path, i); | |
725 | ||
939190b3 PP |
726 | /* Get child field class */ |
727 | child_fc = ctf_field_class_compound_borrow_field_class_by_index( | |
728 | fc, child_index); | |
729 | BT_ASSERT(child_fc); | |
7b33a0e0 | 730 | |
939190b3 PP |
731 | /* Move child class to current class */ |
732 | fc = child_fc; | |
7b33a0e0 PP |
733 | } |
734 | ||
735 | end: | |
939190b3 | 736 | return fc; |
7b33a0e0 PP |
737 | } |
738 | ||
739 | /* | |
939190b3 | 740 | * Fills the equivalent field path object of the context class stack. |
7b33a0e0 PP |
741 | */ |
742 | static | |
743 | void get_ctx_stack_field_path(struct resolve_context *ctx, | |
744 | struct ctf_field_path *field_path) | |
745 | { | |
746 | uint64_t i; | |
747 | ||
748 | BT_ASSERT(field_path); | |
749 | field_path->root = ctx->root_scope; | |
750 | ctf_field_path_clear(field_path); | |
751 | ||
939190b3 PP |
752 | for (i = 0; i < field_class_stack_size(ctx->field_class_stack); i++) { |
753 | struct field_class_stack_frame *frame = | |
754 | field_class_stack_at(ctx->field_class_stack, i); | |
7b33a0e0 PP |
755 | |
756 | ctf_field_path_append_index(field_path, frame->index); | |
757 | } | |
758 | } | |
759 | ||
760 | /* | |
761 | * Returns the index of the lowest common ancestor of two field path | |
762 | * objects having the same root scope. | |
763 | */ | |
764 | int64_t get_field_paths_lca_index(struct ctf_field_path *field_path1, | |
765 | struct ctf_field_path *field_path2) | |
766 | { | |
767 | int64_t lca_index = 0; | |
768 | uint64_t field_path1_len, field_path2_len; | |
769 | ||
770 | if (BT_LOG_ON_VERBOSE) { | |
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 | ||
780 | BT_LOGV("Finding lowest common ancestor (LCA) between two field paths: " | |
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 | */ | |
939190b3 | 810 | BT_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 | ||
832 | BT_LOGV("Found LCA: lca-index=%" PRId64, lca_index); | |
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) { | |
857 | BT_LOGE_STR("Target field path's length is 0 (targeting the root)."); | |
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) { | |
939190b3 | 867 | BT_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, | |
883 | &ctx_field_path); | |
884 | if (lca_index < 0) { | |
885 | BT_LOGE_STR("Cannot get least common ancestor."); | |
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) { | |
939190b3 | 900 | BT_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) { | |
939190b3 PP |
916 | BT_LOGE("Variant field class's tag field class is not an enumeration field class: " |
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) { | |
939190b3 PP |
929 | BT_LOGE("Sequence field class's length field class is not an unsigned integer field class: " |
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 PP |
936 | if (int_fc->is_signed) { |
937 | BT_LOGE("Sequence field class's length field class is not an unsigned integer field class: " | |
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) { | |
989 | BT_LOGE_STR("Cannot get path string."); | |
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) { | |
997 | BT_LOGE("Cannot get target field path for path string: " | |
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) { | |
1010 | BT_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 PP |
1019 | if (ret) { |
1020 | BT_LOGE("Invalid target field path for path string: " | |
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) { |
939190b3 PP |
1081 | BT_LOGE("Cannot resolve sequence field class's length or variant field class's tag: " |
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 | |
939190b3 | 1102 | ret = field_class_stack_push(ctx->field_class_stack, fc); |
7b33a0e0 | 1103 | if (ret) { |
939190b3 PP |
1104 | BT_LOGE("Cannot push field class on context's stack: " |
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 | ||
939190b3 PP |
1126 | BT_LOGV("Resolving field class's child field class: " |
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 | ||
939190b3 | 1136 | field_class_stack_pop(ctx->field_class_stack); |
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) { |
939190b3 | 1179 | BT_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) { |
939190b3 | 1187 | BT_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) { |
939190b3 | 1215 | BT_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) { |
939190b3 | 1223 | BT_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) { |
939190b3 | 1231 | BT_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) { |
939190b3 | 1246 | BT_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 | |
939190b3 | 1262 | int ctf_trace_class_resolve_field_classes(struct ctf_trace_class *tc) |
7b33a0e0 PP |
1263 | { |
1264 | int ret = 0; | |
1265 | uint64_t i; | |
1266 | struct resolve_context ctx = { | |
1267 | .tc = tc, | |
1268 | .sc = NULL, | |
1269 | .ec = NULL, | |
1270 | .scopes = { | |
939190b3 | 1271 | .packet_header = tc->packet_header_fc, |
7b33a0e0 PP |
1272 | .packet_context = NULL, |
1273 | .event_header = NULL, | |
1274 | .event_common_context = NULL, | |
1275 | .event_spec_context = NULL, | |
1276 | .event_payload = NULL, | |
1277 | }, | |
0476f6f7 | 1278 | .root_scope = CTF_SCOPE_PACKET_HEADER, |
939190b3 | 1279 | .cur_fc = NULL, |
7b33a0e0 PP |
1280 | }; |
1281 | ||
939190b3 PP |
1282 | /* Initialize class stack */ |
1283 | ctx.field_class_stack = field_class_stack_create(); | |
1284 | if (!ctx.field_class_stack) { | |
1285 | BT_LOGE_STR("Cannot create field class stack."); | |
7b33a0e0 PP |
1286 | ret = -1; |
1287 | goto end; | |
1288 | } | |
1289 | ||
1290 | if (!tc->is_translated) { | |
939190b3 | 1291 | ctx.scopes.packet_header = tc->packet_header_fc; |
0476f6f7 | 1292 | ret = resolve_root_class(CTF_SCOPE_PACKET_HEADER, &ctx); |
7b33a0e0 | 1293 | if (ret) { |
939190b3 | 1294 | BT_LOGE("Cannot resolve packet header field class: " |
7b33a0e0 PP |
1295 | "ret=%d", ret); |
1296 | goto end; | |
1297 | } | |
1298 | } | |
1299 | ||
939190b3 | 1300 | ctx.scopes.packet_header = tc->packet_header_fc; |
7b33a0e0 PP |
1301 | |
1302 | for (i = 0; i < tc->stream_classes->len; i++) { | |
1303 | struct ctf_stream_class *sc = tc->stream_classes->pdata[i]; | |
1304 | ||
939190b3 | 1305 | ret = resolve_stream_class_field_classes(&ctx, sc); |
7b33a0e0 | 1306 | if (ret) { |
939190b3 | 1307 | BT_LOGE("Cannot resolve stream class's field classes: " |
7b33a0e0 PP |
1308 | "sc-id=%" PRIu64, sc->id); |
1309 | goto end; | |
1310 | } | |
1311 | } | |
1312 | ||
1313 | end: | |
939190b3 | 1314 | field_class_stack_destroy(ctx.field_class_stack); |
7b33a0e0 PP |
1315 | return ret; |
1316 | } |