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