Commit | Line | Data |
---|---|---|
09840de5 PP |
1 | /* |
2 | * resolve.c | |
3 | * | |
4 | * Babeltrace - CTF IR: Type resolving internal | |
5 | * | |
6 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * Copyright 2016 Philippe Proulx <pproulx@efficios.com> | |
8 | * | |
9 | * Authors: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
10 | * Philippe Proulx <pproulx@efficios.com> | |
11 | * | |
12 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
13 | * of this software and associated documentation files (the "Software"), to deal | |
14 | * in the Software without restriction, including without limitation the rights | |
15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
16 | * copies of the Software, and to permit persons to whom the Software is | |
17 | * furnished to do so, subject to the following conditions: | |
18 | * | |
19 | * The above copyright notice and this permission notice shall be included in | |
20 | * all copies or substantial portions of the Software. | |
21 | * | |
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
28 | * SOFTWARE. | |
29 | */ | |
30 | ||
31 | #include <babeltrace/ctf-ir/event.h> | |
32 | #include <babeltrace/ctf-ir/stream-class.h> | |
33 | #include <babeltrace/ctf-ir/resolve-internal.h> | |
34 | #include <babeltrace/ctf-ir/event-types-internal.h> | |
35 | #include <babeltrace/ctf-ir/event-internal.h> | |
36 | #include <babeltrace/ref.h> | |
37 | #include <babeltrace/babeltrace-internal.h> | |
38 | #include <babeltrace/values.h> | |
39 | #include <limits.h> | |
40 | #include <glib.h> | |
41 | ||
42 | #define _printf_error(fmt, args...) \ | |
43 | printf_verbose("[resolving] " fmt, ## args) | |
44 | ||
45 | typedef GPtrArray type_stack; | |
46 | ||
47 | /* | |
48 | * A stack frame. | |
49 | * | |
50 | * `type` contains a compound field type (structure, variant, array, | |
51 | * or sequence) and `index` indicates the index of the field type in | |
52 | * the upper frame (-1 for array and sequence field types). | |
53 | * | |
54 | * `type` is owned by the stack frame. | |
55 | */ | |
56 | struct type_stack_frame { | |
57 | struct bt_ctf_field_type *type; | |
58 | int index; | |
59 | }; | |
60 | ||
61 | /* | |
62 | * The current context of the resolving engine. | |
63 | * | |
64 | * `scopes` contain the 6 CTF scope field types (see CTF, sect. 7.3.2) | |
65 | * in the following order: | |
66 | * | |
67 | * * Packet header | |
68 | * * Packet context | |
69 | * * Event header | |
70 | * * Stream event context | |
71 | * * Event context | |
72 | * * Event payload | |
73 | */ | |
74 | struct resolve_context { | |
75 | struct bt_value *environment; | |
76 | struct bt_ctf_field_type *scopes[6]; | |
77 | ||
78 | /* Root node being visited */ | |
79 | enum bt_ctf_node root_node; | |
80 | type_stack *type_stack; | |
81 | struct bt_ctf_field_type *cur_field_type; | |
82 | }; | |
83 | ||
84 | /* TSDL dynamic scope prefixes as defined in CTF Section 7.3.2 */ | |
85 | static const char * const absolute_path_prefixes[] = { | |
86 | [CTF_NODE_ENV] = "env.", | |
87 | [CTF_NODE_TRACE_PACKET_HEADER] = "trace.packet.header.", | |
88 | [CTF_NODE_STREAM_PACKET_CONTEXT] = "stream.packet.context.", | |
89 | [CTF_NODE_STREAM_EVENT_HEADER] = "stream.event.header.", | |
90 | [CTF_NODE_STREAM_EVENT_CONTEXT] = "stream.event.context.", | |
91 | [CTF_NODE_EVENT_CONTEXT] = "event.context.", | |
92 | [CTF_NODE_EVENT_FIELDS] = "event.fields.", | |
93 | }; | |
94 | ||
95 | /* Number of path tokens used for the absolute prefixes */ | |
96 | static const int absolute_path_prefix_ptoken_counts[] = { | |
97 | [CTF_NODE_ENV] = 1, | |
98 | [CTF_NODE_TRACE_PACKET_HEADER] = 3, | |
99 | [CTF_NODE_STREAM_PACKET_CONTEXT] = 3, | |
100 | [CTF_NODE_STREAM_EVENT_HEADER] = 3, | |
101 | [CTF_NODE_STREAM_EVENT_CONTEXT] = 3, | |
102 | [CTF_NODE_EVENT_CONTEXT] = 2, | |
103 | [CTF_NODE_EVENT_FIELDS] = 2, | |
104 | }; | |
105 | ||
106 | /* | |
107 | * Destroys a type stack frame. | |
108 | */ | |
109 | static | |
110 | void type_stack_destroy_notify(gpointer data) | |
111 | { | |
112 | struct type_stack_frame *frame = data; | |
113 | ||
114 | BT_PUT(frame->type); | |
115 | g_free(frame); | |
116 | } | |
117 | ||
118 | /* | |
119 | * Creates a type stack. | |
120 | * | |
121 | * Return value is owned by the caller. | |
122 | */ | |
123 | static | |
124 | type_stack *type_stack_create(void) | |
125 | { | |
126 | return g_ptr_array_new_with_free_func(type_stack_destroy_notify); | |
127 | } | |
128 | ||
129 | /* | |
130 | * Destroys a type stack. | |
131 | */ | |
132 | static | |
133 | void type_stack_destroy(type_stack *stack) | |
134 | { | |
135 | g_ptr_array_free(stack, TRUE); | |
136 | } | |
137 | ||
138 | /* | |
139 | * Pushes a field type onto a type stack. | |
140 | * | |
141 | * `type` is owned by the caller (stack frame gets a new reference). | |
142 | */ | |
143 | static | |
144 | int type_stack_push(type_stack *stack, struct bt_ctf_field_type *type) | |
145 | { | |
146 | int ret = 0; | |
147 | struct type_stack_frame *frame = NULL; | |
148 | ||
149 | if (!stack || !type) { | |
150 | ret = -1; | |
151 | goto end; | |
152 | } | |
153 | ||
154 | frame = g_new0(struct type_stack_frame, 1); | |
155 | if (!frame) { | |
156 | ret = -1; | |
157 | goto end; | |
158 | } | |
159 | ||
160 | frame->type = bt_get(type); | |
161 | g_ptr_array_add(stack, frame); | |
162 | ||
163 | end: | |
164 | return ret; | |
165 | } | |
166 | ||
167 | /* | |
168 | * Checks whether or not `stack` is empty. | |
169 | */ | |
170 | static | |
171 | bool type_stack_empty(type_stack *stack) | |
172 | { | |
173 | return stack->len == 0; | |
174 | } | |
175 | ||
176 | /* | |
177 | * Returns the number of frames in `stack`. | |
178 | */ | |
179 | static | |
180 | size_t type_stack_size(type_stack *stack) | |
181 | { | |
182 | return stack->len; | |
183 | } | |
184 | ||
185 | /* | |
186 | * Returns the top frame of `stack`. | |
187 | * | |
188 | * Return value is owned by `stack`. | |
189 | */ | |
190 | static | |
191 | struct type_stack_frame *type_stack_peek(type_stack *stack) | |
192 | { | |
193 | struct type_stack_frame *entry = NULL; | |
194 | ||
195 | if (!stack || type_stack_empty(stack)) { | |
196 | goto end; | |
197 | } | |
198 | ||
199 | entry = g_ptr_array_index(stack, stack->len - 1); | |
200 | end: | |
201 | return entry; | |
202 | } | |
203 | ||
204 | /* | |
205 | * Returns the frame at index `index` in `stack`. | |
206 | * | |
207 | * Return value is owned by `stack`. | |
208 | */ | |
209 | static | |
210 | struct type_stack_frame *type_stack_at(type_stack *stack, | |
211 | size_t index) | |
212 | { | |
213 | struct type_stack_frame *entry = NULL; | |
214 | ||
215 | if (!stack || index >= stack->len) { | |
216 | goto end; | |
217 | } | |
218 | ||
219 | entry = g_ptr_array_index(stack, index); | |
220 | ||
221 | end: | |
222 | return entry; | |
223 | } | |
224 | ||
225 | /* | |
226 | * Removes the top frame of `stack`. | |
227 | */ | |
228 | static | |
229 | void type_stack_pop(type_stack *stack) | |
230 | { | |
231 | if (!type_stack_empty(stack)) { | |
232 | /* | |
233 | * This will call the frame's destructor and free it, as | |
234 | * well as put its contained field type. | |
235 | */ | |
236 | g_ptr_array_set_size(stack, stack->len - 1); | |
237 | } | |
238 | } | |
239 | ||
240 | /* | |
241 | * Returns the scope field type of `scope` in the context `ctx`. | |
242 | * | |
243 | * Return value is owned by `ctx` on success. | |
244 | */ | |
245 | static | |
246 | struct bt_ctf_field_type *get_type_from_ctx(struct resolve_context *ctx, | |
247 | enum bt_ctf_node node) | |
248 | { | |
249 | assert(node >= CTF_NODE_TRACE_PACKET_HEADER && | |
250 | node <= CTF_NODE_EVENT_FIELDS); | |
251 | ||
252 | return ctx->scopes[node - CTF_NODE_TRACE_PACKET_HEADER]; | |
253 | } | |
254 | ||
255 | /* | |
256 | * Returns the CTF scope from a path string. May return | |
257 | * CTF_NODE_UNKNOWN if the path is found to be relative. | |
258 | */ | |
259 | static | |
260 | enum bt_ctf_node get_root_node_from_absolute_pathstr(const char *pathstr) | |
261 | { | |
262 | enum bt_ctf_node node; | |
263 | enum bt_ctf_node ret = CTF_NODE_UNKNOWN; | |
264 | const size_t prefixes_count = sizeof(absolute_path_prefixes) / | |
265 | sizeof(*absolute_path_prefixes); | |
266 | ||
267 | for (node = CTF_NODE_ENV; node < CTF_NODE_ENV + prefixes_count; | |
268 | node++) { | |
269 | /* | |
270 | * Chech if path string starts with a known absolute | |
271 | * path prefix. | |
272 | * | |
273 | * Refer to CTF 7.3.2 STATIC AND DYNAMIC SCOPES. | |
274 | */ | |
275 | if (strncmp(pathstr, absolute_path_prefixes[node], | |
276 | strlen(absolute_path_prefixes[node]))) { | |
277 | /* Prefix does not match: try the next one */ | |
278 | continue; | |
279 | } | |
280 | ||
281 | /* Found it! */ | |
282 | ret = node; | |
283 | goto end; | |
284 | } | |
285 | ||
286 | end: | |
287 | return ret; | |
288 | } | |
289 | ||
290 | /* | |
291 | * Destroys a path token. | |
292 | */ | |
293 | static | |
294 | void ptokens_destroy_func(gpointer ptoken, gpointer data) | |
295 | { | |
296 | g_string_free(ptoken, TRUE); | |
297 | } | |
298 | ||
299 | /* | |
300 | * Destroys a path token list. | |
301 | */ | |
302 | static | |
303 | void ptokens_destroy(GList *ptokens) | |
304 | { | |
305 | if (!ptokens) { | |
306 | return; | |
307 | } | |
308 | ||
309 | g_list_foreach(ptokens, ptokens_destroy_func, NULL); | |
310 | g_list_free(ptokens); | |
311 | } | |
312 | ||
313 | /* | |
314 | * Returns the string contained in a path token. | |
315 | */ | |
316 | static | |
317 | const char *ptoken_get_string(GList *ptoken) | |
318 | { | |
319 | GString *tokenstr = (GString *) ptoken->data; | |
320 | ||
321 | return tokenstr->str; | |
322 | } | |
323 | ||
324 | /* | |
325 | * Converts a path string to a path token list, that is, splits the | |
326 | * individual words of a path string into a list of individual | |
327 | * strings. | |
328 | * | |
329 | * Return value is owned by the caller on success. | |
330 | */ | |
331 | static | |
332 | GList *pathstr_to_ptokens(const char *pathstr) | |
333 | { | |
334 | const char *at = pathstr; | |
335 | const char *last = at; | |
336 | GList *ptokens = NULL; | |
337 | ||
338 | for (;;) { | |
339 | if (*at == '.' || *at == '\0') { | |
340 | GString *tokenstr; | |
341 | ||
342 | if (at == last) { | |
343 | /* Error: empty token */ | |
344 | _printf_error("Empty token in path string at position %d\n", | |
345 | (int) (at - pathstr)); | |
346 | goto error; | |
347 | } | |
348 | ||
349 | tokenstr = g_string_new(NULL); | |
350 | g_string_append_len(tokenstr, last, at - last); | |
351 | ptokens = g_list_append(ptokens, tokenstr); | |
352 | last = at + 1; | |
353 | } | |
354 | ||
355 | if (*at == '\0') { | |
356 | break; | |
357 | } | |
358 | ||
359 | at++; | |
360 | } | |
361 | ||
362 | return ptokens; | |
363 | ||
364 | error: | |
365 | ptokens_destroy(ptokens); | |
366 | return NULL; | |
367 | } | |
368 | ||
369 | /* | |
370 | * Converts a path token list to a field path object. The path token | |
371 | * list is relative from `type`. The index of the source looking for | |
372 | * its target within `type` is indicated by `src_index`. This can be | |
373 | * `INT_MAX` if the source is contained in `type`. | |
374 | * | |
375 | * `ptokens` is owned by the caller. `field_path` is an output parameter | |
376 | * owned by the caller that must be filled here. `type` is owned by the | |
377 | * caller. | |
378 | */ | |
379 | static | |
380 | int ptokens_to_field_path(GList *ptokens, struct bt_ctf_field_path *field_path, | |
381 | struct bt_ctf_field_type *type, int src_index) | |
382 | { | |
383 | int ret = 0; | |
384 | GList *cur_ptoken = ptokens; | |
385 | bool first_level_done = false; | |
386 | ||
387 | /* Get our own reference */ | |
388 | bt_get(type); | |
389 | ||
390 | /* Locate target */ | |
391 | while (cur_ptoken) { | |
392 | int child_index; | |
393 | struct bt_ctf_field_type *child_type; | |
394 | const char *field_name = ptoken_get_string(cur_ptoken); | |
395 | enum ctf_type_id type_id = bt_ctf_field_type_get_type_id(type); | |
396 | ||
397 | /* Find to which index corresponds the current path token */ | |
398 | if (type_id == CTF_TYPE_ARRAY || type_id == CTF_TYPE_SEQUENCE) { | |
399 | child_index = -1; | |
400 | } else { | |
401 | child_index = bt_ctf_field_type_get_field_index(type, | |
402 | field_name); | |
403 | if (child_index < 0) { | |
404 | /* | |
405 | * Error: field name does not exist or | |
406 | * wrong current type. | |
407 | */ | |
408 | _printf_error("Cannot get index of field type named \"%s\"\n", | |
409 | field_name); | |
410 | ret = -1; | |
411 | goto end; | |
412 | } else if (child_index > src_index && | |
413 | !first_level_done) { | |
414 | _printf_error("Child type is located after source index (%d)\n", | |
415 | src_index); | |
416 | ret = -1; | |
417 | goto end; | |
418 | } | |
419 | ||
420 | /* Next path token */ | |
421 | cur_ptoken = g_list_next(cur_ptoken); | |
422 | first_level_done = true; | |
423 | } | |
424 | ||
425 | /* Create new field path entry */ | |
426 | g_array_append_val(field_path->path_indexes, child_index); | |
427 | ||
428 | /* Get child field type */ | |
429 | child_type = bt_ctf_field_type_get_field_at_index(type, | |
430 | child_index); | |
431 | if (!child_type) { | |
432 | _printf_error("Cannot get child type at index %d (field \"%s\")\n", | |
433 | child_index, field_name); | |
434 | ret = -1; | |
435 | goto end; | |
436 | } | |
437 | ||
438 | /* Move child type to current type */ | |
439 | BT_MOVE(type, child_type); | |
440 | } | |
441 | ||
442 | end: | |
443 | bt_put(type); | |
444 | return ret; | |
445 | } | |
446 | ||
447 | /* | |
448 | * Converts a known absolute path token list to a field path object | |
449 | * within the resolving context `ctx`. | |
450 | * | |
451 | * `ptokens` is owned by the caller. `field_path` is an output parameter | |
452 | * owned by the caller that must be filled here. | |
453 | */ | |
454 | static | |
455 | int absolute_ptokens_to_field_path(GList *ptokens, | |
456 | struct bt_ctf_field_path *field_path, | |
457 | struct resolve_context *ctx) | |
458 | { | |
459 | int ret = 0; | |
460 | GList *cur_ptoken; | |
461 | struct bt_ctf_field_type *type; | |
462 | ||
463 | /* Skip absolute path tokens */ | |
464 | cur_ptoken = g_list_nth(ptokens, | |
465 | absolute_path_prefix_ptoken_counts[field_path->root]); | |
466 | ||
467 | /* Start with root type */ | |
468 | type = get_type_from_ctx(ctx, field_path->root); | |
469 | if (!type) { | |
470 | /* Error: root type is not available */ | |
471 | _printf_error("Root type with node type %d is not available\n", | |
472 | field_path->root); | |
473 | ret = -1; | |
474 | goto end; | |
475 | } | |
476 | ||
477 | /* Locate target */ | |
478 | ret = ptokens_to_field_path(cur_ptoken, field_path, type, INT_MAX); | |
479 | ||
480 | end: | |
481 | return ret; | |
482 | } | |
483 | ||
484 | /* | |
485 | * Converts a known relative path token list to a field path object | |
486 | * within the resolving context `ctx`. | |
487 | * | |
488 | * `ptokens` is owned by the caller. `field_path` is an output parameter | |
489 | * owned by the caller that must be filled here. | |
490 | */ | |
491 | static | |
492 | int relative_ptokens_to_field_path(GList *ptokens, | |
493 | struct bt_ctf_field_path *field_path, | |
494 | struct resolve_context *ctx) | |
495 | { | |
496 | int ret = 0; | |
497 | int parent_pos_in_stack; | |
498 | struct bt_ctf_field_path *tail_field_path = bt_ctf_field_path_create(); | |
499 | ||
500 | if (!tail_field_path) { | |
501 | _printf_error("Cannot create field path\n"); | |
502 | ret = -1; | |
503 | goto end; | |
504 | } | |
505 | ||
506 | parent_pos_in_stack = type_stack_size(ctx->type_stack) - 1; | |
507 | ||
508 | while (parent_pos_in_stack >= 0) { | |
509 | struct bt_ctf_field_type *parent_type = | |
510 | type_stack_at(ctx->type_stack, | |
511 | parent_pos_in_stack)->type; | |
512 | int cur_index = type_stack_at(ctx->type_stack, | |
513 | parent_pos_in_stack)->index; | |
514 | ||
515 | /* Locate target from current parent type */ | |
516 | ret = ptokens_to_field_path(ptokens, tail_field_path, | |
517 | parent_type, cur_index); | |
518 | if (ret) { | |
519 | /* Not found... yet */ | |
520 | bt_ctf_field_path_clear(tail_field_path); | |
521 | } else { | |
522 | /* Found: stitch tail field path to head field path */ | |
523 | int i = 0; | |
524 | int tail_field_path_len = | |
525 | tail_field_path->path_indexes->len; | |
526 | ||
527 | while (true) { | |
528 | struct bt_ctf_field_type *cur_type = | |
529 | type_stack_at(ctx->type_stack, i)->type; | |
530 | int index = type_stack_at( | |
531 | ctx->type_stack, i)->index; | |
532 | ||
533 | if (cur_type == parent_type) { | |
534 | break; | |
535 | } | |
536 | ||
537 | g_array_append_val(field_path->path_indexes, | |
538 | index); | |
539 | i++; | |
540 | } | |
541 | ||
542 | for (i = 0; i < tail_field_path_len; i++) { | |
543 | int index = g_array_index( | |
544 | tail_field_path->path_indexes, | |
545 | int, i); | |
546 | ||
547 | g_array_append_val(field_path->path_indexes, | |
548 | index); | |
549 | } | |
550 | break; | |
551 | } | |
552 | ||
553 | parent_pos_in_stack--; | |
554 | } | |
555 | ||
556 | if (parent_pos_in_stack < 0) { | |
557 | /* Not found: look in previous scopes */ | |
558 | field_path->root--; | |
559 | ||
560 | while (field_path->root >= CTF_NODE_TRACE_PACKET_HEADER) { | |
561 | struct bt_ctf_field_type *root_type; | |
562 | bt_ctf_field_path_clear(field_path); | |
563 | ||
564 | root_type = get_type_from_ctx(ctx, field_path->root); | |
565 | if (!root_type) { | |
566 | field_path->root--; | |
567 | continue; | |
568 | } | |
569 | ||
570 | /* Locate target in previous scope */ | |
571 | ret = ptokens_to_field_path(ptokens, field_path, | |
572 | root_type, INT_MAX); | |
573 | if (ret) { | |
574 | /* Not found yet */ | |
575 | field_path->root--; | |
576 | continue; | |
577 | } | |
578 | ||
579 | /* Found */ | |
580 | break; | |
581 | } | |
582 | } | |
583 | ||
584 | end: | |
585 | bt_ctf_field_path_destroy(tail_field_path); | |
586 | return ret; | |
587 | } | |
588 | ||
589 | /* | |
590 | * Converts a path string to a field path object within the resolving | |
591 | * context `ctx`. | |
592 | * | |
593 | * Return value is owned by the caller on success. | |
594 | */ | |
595 | static | |
596 | struct bt_ctf_field_path *pathstr_to_field_path(const char *pathstr, | |
597 | struct resolve_context *ctx) | |
598 | { | |
599 | int ret; | |
600 | enum bt_ctf_node root_node; | |
601 | GList *ptokens = NULL; | |
602 | struct bt_ctf_field_path *field_path = NULL; | |
603 | ||
604 | /* Create field path */ | |
605 | field_path = bt_ctf_field_path_create(); | |
606 | if (!field_path) { | |
607 | _printf_error("Cannot create field path\n"); | |
608 | ret = -1; | |
609 | goto end; | |
610 | } | |
611 | ||
612 | /* Convert path string to path tokens */ | |
613 | ptokens = pathstr_to_ptokens(pathstr); | |
614 | if (!ptokens) { | |
615 | _printf_error("Cannot convert path string \"%s\" to path tokens\n", | |
616 | pathstr); | |
617 | ret = -1; | |
618 | goto end; | |
619 | } | |
620 | ||
621 | /* Absolute or relative path? */ | |
622 | root_node = get_root_node_from_absolute_pathstr(pathstr); | |
623 | ||
624 | if (root_node == CTF_NODE_UNKNOWN) { | |
625 | /* Relative path: start with current root node */ | |
626 | field_path->root = ctx->root_node; | |
627 | ret = relative_ptokens_to_field_path(ptokens, field_path, ctx); | |
628 | if (ret) { | |
629 | _printf_error("Cannot get relative field path of path string \"%s\"\n", | |
630 | pathstr); | |
631 | _printf_error(" Starting at root node %d, finished at root node %d\n", | |
632 | ctx->root_node, field_path->root); | |
633 | goto end; | |
634 | } | |
635 | } else if (root_node == CTF_NODE_ENV) { | |
636 | _printf_error("Sequence field types referring the trace environment are not supported as of this version\n"); | |
637 | ret = -1; | |
638 | goto end; | |
639 | } else { | |
640 | /* Absolute path: use found root node */ | |
641 | field_path->root = root_node; | |
642 | ret = absolute_ptokens_to_field_path(ptokens, field_path, ctx); | |
643 | if (ret) { | |
644 | _printf_error("Cannot get absolute field path of path string \"%s\"\n", | |
645 | pathstr); | |
646 | _printf_error(" Looking in root node %d\n", root_node); | |
647 | goto end; | |
648 | } | |
649 | } | |
650 | ||
651 | end: | |
652 | if (ret) { | |
653 | bt_ctf_field_path_destroy(field_path); | |
654 | field_path = NULL; | |
655 | } | |
656 | ||
657 | ptokens_destroy(ptokens); | |
658 | ||
659 | return field_path; | |
660 | } | |
661 | ||
662 | /* | |
663 | * Retrieves a field type by following the field path `field_path` in | |
664 | * the resolving context `ctx`. | |
665 | * | |
666 | * Return value is owned by the caller on success. | |
667 | */ | |
668 | static | |
669 | struct bt_ctf_field_type *field_path_to_field_type( | |
670 | struct bt_ctf_field_path *field_path, | |
671 | struct resolve_context *ctx) | |
672 | { | |
673 | int i; | |
674 | struct bt_ctf_field_type *type; | |
675 | ||
676 | /* Start with root type */ | |
677 | type = get_type_from_ctx(ctx, field_path->root); | |
678 | bt_get(type); | |
679 | if (!type) { | |
680 | /* Error: root type is not available */ | |
681 | _printf_error("Root type with node type %d is not available\n", | |
682 | field_path->root); | |
683 | goto error; | |
684 | } | |
685 | ||
686 | /* Locate target */ | |
687 | for (i = 0; i < field_path->path_indexes->len; i++) { | |
688 | struct bt_ctf_field_type *child_type; | |
689 | int child_index = | |
690 | g_array_index(field_path->path_indexes, int, i); | |
691 | ||
692 | /* Get child field type */ | |
693 | child_type = bt_ctf_field_type_get_field_at_index(type, | |
694 | child_index); | |
695 | if (!child_type) { | |
696 | _printf_error("Cannot get field type field at index %d\n", | |
697 | child_index); | |
698 | goto error; | |
699 | } | |
700 | ||
701 | /* Move child type to current type */ | |
702 | BT_MOVE(type, child_type); | |
703 | } | |
704 | ||
705 | return type; | |
706 | ||
707 | error: | |
708 | BT_PUT(type); | |
709 | return type; | |
710 | } | |
711 | ||
712 | /* | |
713 | * Returns the equivalent field path object of the context type stack. | |
714 | * | |
715 | * Return value is owned by the caller on success. | |
716 | */ | |
717 | static | |
718 | struct bt_ctf_field_path *get_ctx_stack_field_path(struct resolve_context *ctx) | |
719 | { | |
720 | int i; | |
721 | struct bt_ctf_field_path *field_path; | |
722 | ||
723 | /* Create field path */ | |
724 | field_path = bt_ctf_field_path_create(); | |
725 | if (!field_path) { | |
726 | _printf_error("Cannot create field path\n"); | |
727 | goto error; | |
728 | } | |
729 | ||
730 | field_path->root = ctx->root_node; | |
731 | ||
732 | for (i = 0; i < type_stack_size(ctx->type_stack); i++) { | |
733 | struct type_stack_frame *frame; | |
734 | ||
735 | frame = type_stack_at(ctx->type_stack, i); | |
736 | g_array_append_val(field_path->path_indexes, frame->index); | |
737 | } | |
738 | ||
739 | return field_path; | |
740 | ||
741 | error: | |
742 | bt_ctf_field_path_destroy(field_path); | |
743 | return NULL; | |
744 | } | |
745 | ||
746 | /* | |
747 | * Returns the lowest common ancestor of two field path objects | |
748 | * having the same root scope. | |
749 | * | |
750 | * `field_path1` and `field_path2` are owned by the caller. | |
751 | */ | |
752 | int get_field_paths_lca_index(struct bt_ctf_field_path *field_path1, | |
753 | struct bt_ctf_field_path *field_path2) | |
754 | { | |
755 | int lca_index = 0; | |
756 | int field_path1_len, field_path2_len; | |
757 | ||
758 | /* | |
759 | * Start from both roots and find the first mismatch. | |
760 | */ | |
761 | assert(field_path1->root == field_path2->root); | |
762 | field_path1_len = field_path1->path_indexes->len; | |
763 | field_path2_len = field_path2->path_indexes->len; | |
764 | ||
765 | while (true) { | |
766 | int target_index, ctx_index; | |
767 | ||
768 | if (lca_index == field_path2_len || | |
769 | lca_index == field_path1_len) { | |
770 | /* | |
771 | * This means that both field paths never split. | |
772 | * This is invalid because the target cannot be | |
773 | * an ancestor of the source. | |
774 | */ | |
775 | _printf_error("In source and target: one is an ancestor of the other\n"); | |
776 | lca_index = -1; | |
777 | break; | |
778 | } | |
779 | ||
780 | target_index = g_array_index(field_path1->path_indexes, int, | |
781 | lca_index); | |
782 | ctx_index = g_array_index(field_path2->path_indexes, int, | |
783 | lca_index); | |
784 | ||
785 | if (target_index != ctx_index) { | |
786 | /* LCA index is the previous */ | |
787 | break; | |
788 | } | |
789 | ||
790 | lca_index++; | |
791 | } | |
792 | ||
793 | return lca_index; | |
794 | } | |
795 | ||
796 | /* | |
797 | * Validates a target field path. | |
798 | * | |
799 | * `target_field_path` and `target_type` are owned by the caller. | |
800 | */ | |
801 | static | |
802 | int validate_target_field_path(struct bt_ctf_field_path *target_field_path, | |
803 | struct bt_ctf_field_type *target_type, | |
804 | struct resolve_context *ctx) | |
805 | { | |
806 | int ret = 0; | |
807 | struct bt_ctf_field_path *ctx_field_path; | |
808 | int target_field_path_len = target_field_path->path_indexes->len; | |
809 | int lca_index; | |
810 | int ctx_cur_field_type_id; | |
811 | int target_type_id; | |
812 | ||
813 | /* Get context field path */ | |
814 | ctx_field_path = get_ctx_stack_field_path(ctx); | |
815 | if (!ctx_field_path) { | |
816 | _printf_error("Cannot get source field path\n"); | |
817 | ret = -1; | |
818 | goto end; | |
819 | } | |
820 | ||
821 | /* | |
822 | * Make sure the target is not a root. | |
823 | */ | |
824 | if (target_field_path_len == 0) { | |
825 | _printf_error("Target field path's length is 0 (targeting the root)\n"); | |
826 | ret = -1; | |
827 | goto end; | |
828 | } | |
829 | ||
830 | /* | |
831 | * Make sure the root of the target field path is not located | |
832 | * after the context field path's root. | |
833 | */ | |
834 | if (target_field_path->root > ctx_field_path->root) { | |
835 | _printf_error("Target is located after source\n"); | |
836 | ret = -1; | |
837 | goto end; | |
838 | } | |
839 | ||
840 | if (target_field_path->root == ctx_field_path->root) { | |
841 | int target_index, ctx_index; | |
842 | ||
843 | /* | |
844 | * Find the index of the lowest common ancestor of both field | |
845 | * paths. | |
846 | */ | |
847 | lca_index = get_field_paths_lca_index(target_field_path, | |
848 | ctx_field_path); | |
849 | if (lca_index < 0) { | |
850 | _printf_error("Cannot get least common ancestor\n"); | |
851 | ret = -1; | |
852 | goto end; | |
853 | } | |
854 | ||
855 | /* | |
856 | * Make sure the target field path is located before the | |
857 | * context field path. | |
858 | */ | |
859 | target_index = g_array_index(target_field_path->path_indexes, | |
860 | int, lca_index); | |
861 | ctx_index = g_array_index(ctx_field_path->path_indexes, | |
862 | int, lca_index); | |
863 | ||
864 | if (target_index >= ctx_index) { | |
865 | _printf_error("Target index (%d) is greater or equal to source index (%d) in LCA\n", | |
866 | target_index, ctx_index); | |
867 | ret = -1; | |
868 | goto end; | |
869 | } | |
870 | } | |
871 | ||
872 | /* | |
873 | * Make sure the target type has the right type and properties. | |
874 | */ | |
875 | ctx_cur_field_type_id = bt_ctf_field_type_get_type_id( | |
876 | ctx->cur_field_type); | |
877 | target_type_id = bt_ctf_field_type_get_type_id(target_type); | |
878 | ||
879 | if (ctx_cur_field_type_id == CTF_TYPE_VARIANT) { | |
880 | if (target_type_id != CTF_TYPE_ENUM) { | |
881 | _printf_error("Variant type's tag field type is not an enumeration\n"); | |
882 | ret = -1; | |
883 | goto end; | |
884 | } | |
885 | } else if (ctx_cur_field_type_id == CTF_TYPE_SEQUENCE) { | |
886 | if (target_type_id != CTF_TYPE_INTEGER || | |
887 | bt_ctf_field_type_integer_get_signed( | |
888 | target_type)) { | |
889 | _printf_error("Sequence type's length field type is not an unsigned integer\n"); | |
890 | ret = -1; | |
891 | goto end; | |
892 | } | |
893 | } else { | |
894 | assert(false); | |
895 | } | |
896 | ||
897 | end: | |
898 | bt_ctf_field_path_destroy(ctx_field_path); | |
899 | return ret; | |
900 | } | |
901 | ||
902 | /* | |
903 | * Resolves a variant or sequence field type `type`. | |
904 | * | |
905 | * `type` is owned by the caller. | |
906 | */ | |
907 | static | |
908 | int resolve_sequence_or_variant_type(struct bt_ctf_field_type *type, | |
909 | struct resolve_context *ctx) | |
910 | { | |
911 | int ret = 0; | |
912 | const char *pathstr; | |
913 | int type_id = bt_ctf_field_type_get_type_id(type); | |
914 | struct bt_ctf_field_path *target_field_path = NULL; | |
915 | struct bt_ctf_field_type *target_type = NULL; | |
916 | ||
917 | /* Get path string */ | |
918 | switch (type_id) { | |
919 | case CTF_TYPE_SEQUENCE: | |
920 | pathstr = | |
921 | bt_ctf_field_type_sequence_get_length_field_name(type); | |
922 | break; | |
923 | case CTF_TYPE_VARIANT: | |
924 | pathstr = | |
925 | bt_ctf_field_type_variant_get_tag_name(type); | |
926 | break; | |
927 | default: | |
928 | assert(false); | |
929 | } | |
930 | ||
931 | /* Get target field path out of path string */ | |
932 | target_field_path = pathstr_to_field_path(pathstr, ctx); | |
933 | if (!target_field_path) { | |
934 | _printf_error("Cannot get target field path for path string \"%s\"\n", | |
935 | pathstr); | |
936 | ret = -1; | |
937 | goto end; | |
938 | } | |
939 | ||
940 | /* Get target field type */ | |
941 | target_type = field_path_to_field_type(target_field_path, ctx); | |
942 | if (!target_type) { | |
943 | _printf_error("Cannot get target field type for path string \"%s\"\n", | |
944 | pathstr); | |
945 | ret = -1; | |
946 | goto end; | |
947 | } | |
948 | ||
949 | ret = validate_target_field_path(target_field_path, target_type, ctx); | |
950 | if (ret) { | |
951 | _printf_error("Invalid target field path for path string \"%s\"\n", | |
952 | pathstr); | |
953 | goto end; | |
954 | } | |
955 | ||
956 | /* Set target field path and target field type */ | |
957 | if (type_id == CTF_TYPE_SEQUENCE) { | |
958 | ret = bt_ctf_field_type_sequence_set_length_field_path( | |
959 | type, target_field_path); | |
960 | if (ret) { | |
961 | _printf_error("Cannot set sequence field type's length field path\n"); | |
962 | goto end; | |
963 | } | |
964 | ||
965 | target_field_path = NULL; | |
966 | } else if (type_id == CTF_TYPE_VARIANT) { | |
967 | ret = bt_ctf_field_type_variant_set_tag_field_path( | |
968 | type, target_field_path); | |
969 | if (ret) { | |
970 | _printf_error("Cannot set variant field type's tag field path\n"); | |
971 | goto end; | |
972 | } | |
973 | ||
974 | target_field_path = NULL; | |
975 | ||
976 | ret = bt_ctf_field_type_variant_set_tag(type, target_type); | |
977 | if (ret) { | |
978 | _printf_error("Cannot set variant field type's tag field type\n"); | |
979 | goto end; | |
980 | } | |
981 | } else { | |
982 | assert(false); | |
983 | } | |
984 | ||
985 | end: | |
986 | bt_ctf_field_path_destroy(target_field_path); | |
987 | BT_PUT(target_type); | |
988 | return ret; | |
989 | } | |
990 | ||
991 | /* | |
992 | * Resolves a field type `type`. | |
993 | * | |
994 | * `type` is owned by the caller. | |
995 | */ | |
996 | static | |
997 | int resolve_type(struct bt_ctf_field_type *type, struct resolve_context *ctx) | |
998 | { | |
999 | int ret = 0; | |
1000 | int type_id; | |
1001 | ||
1002 | if (!type) { | |
1003 | /* Type is not available; still valid */ | |
1004 | goto end; | |
1005 | } | |
1006 | ||
1007 | type_id = bt_ctf_field_type_get_type_id(type); | |
1008 | ctx->cur_field_type = type; | |
1009 | ||
1010 | /* Resolve sequence/variant field type */ | |
1011 | switch (type_id) { | |
1012 | case CTF_TYPE_SEQUENCE: | |
1013 | case CTF_TYPE_VARIANT: | |
1014 | ret = resolve_sequence_or_variant_type(type, ctx); | |
1015 | if (ret) { | |
1016 | _printf_error("Cannot resolve sequence or variant field type's length/tag\n"); | |
1017 | goto end; | |
1018 | } | |
1019 | break; | |
1020 | default: | |
1021 | break; | |
1022 | } | |
1023 | ||
1024 | /* Recurse into compound types */ | |
1025 | switch (type_id) { | |
1026 | case CTF_TYPE_STRUCT: | |
1027 | case CTF_TYPE_VARIANT: | |
1028 | case CTF_TYPE_SEQUENCE: | |
1029 | case CTF_TYPE_ARRAY: | |
1030 | { | |
1031 | int field_count, f_index; | |
1032 | ||
1033 | ret = type_stack_push(ctx->type_stack, type); | |
1034 | if (ret) { | |
1035 | _printf_error("Cannot push field type on type stack\n"); | |
1036 | _printf_error(" Stack size: %zu\n", | |
1037 | type_stack_size(ctx->type_stack)); | |
1038 | goto end; | |
1039 | } | |
1040 | ||
1041 | field_count = bt_ctf_field_type_get_field_count(type); | |
1042 | if (field_count < 0) { | |
1043 | _printf_error("Cannot get field type field count\n"); | |
1044 | ret = field_count; | |
1045 | goto end; | |
1046 | } | |
1047 | ||
1048 | for (f_index = 0; f_index < field_count; f_index++) { | |
1049 | struct bt_ctf_field_type *child_type = | |
1050 | bt_ctf_field_type_get_field_at_index(type, | |
1051 | f_index); | |
1052 | ||
1053 | if (!child_type) { | |
1054 | _printf_error("Cannot get field type field at index %d/%d\n", | |
1055 | f_index, field_count); | |
1056 | ret = -1; | |
1057 | goto end; | |
1058 | } | |
1059 | ||
1060 | if (type_id == CTF_TYPE_ARRAY || | |
1061 | type_id == CTF_TYPE_SEQUENCE) { | |
1062 | type_stack_peek(ctx->type_stack)->index = -1; | |
1063 | } else { | |
1064 | type_stack_peek(ctx->type_stack)->index = | |
1065 | f_index; | |
1066 | } | |
1067 | ||
1068 | ret = resolve_type(child_type, ctx); | |
1069 | BT_PUT(child_type); | |
1070 | if (ret) { | |
1071 | goto end; | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | type_stack_pop(ctx->type_stack); | |
1076 | break; | |
1077 | } | |
1078 | default: | |
1079 | break; | |
1080 | } | |
1081 | ||
1082 | end: | |
1083 | return ret; | |
1084 | } | |
1085 | ||
1086 | /* | |
1087 | * Resolves the root field type corresponding to the scope `root_scope`. | |
1088 | */ | |
1089 | static | |
1090 | int resolve_root_type(enum ctf_type_id root_node, struct resolve_context *ctx) | |
1091 | { | |
1092 | int ret; | |
1093 | ||
1094 | assert(type_stack_size(ctx->type_stack) == 0); | |
1095 | ctx->root_node = root_node; | |
1096 | ret = resolve_type(get_type_from_ctx(ctx, root_node), ctx); | |
1097 | ctx->root_node = CTF_NODE_UNKNOWN; | |
1098 | ||
1099 | return ret; | |
1100 | } | |
1101 | ||
1102 | BT_HIDDEN | |
1103 | int bt_ctf_resolve_types( | |
1104 | struct bt_value *environment, | |
1105 | struct bt_ctf_field_type *packet_header_type, | |
1106 | struct bt_ctf_field_type *packet_context_type, | |
1107 | struct bt_ctf_field_type *event_header_type, | |
1108 | struct bt_ctf_field_type *stream_event_ctx_type, | |
1109 | struct bt_ctf_field_type *event_context_type, | |
1110 | struct bt_ctf_field_type *event_payload_type, | |
1111 | enum bt_ctf_resolve_flag flags) | |
1112 | { | |
1113 | int ret = 0; | |
1114 | struct resolve_context ctx = { | |
1115 | .environment = environment, | |
1116 | .scopes = { | |
1117 | packet_header_type, | |
1118 | packet_context_type, | |
1119 | event_header_type, | |
1120 | stream_event_ctx_type, | |
1121 | event_context_type, | |
1122 | event_payload_type, | |
1123 | }, | |
1124 | .root_node = CTF_NODE_UNKNOWN, | |
1125 | }; | |
1126 | ||
1127 | /* Initialize type stack */ | |
1128 | ctx.type_stack = type_stack_create(); | |
1129 | if (!ctx.type_stack) { | |
1130 | printf_error("Cannot create type stack\n"); | |
1131 | ret = -1; | |
1132 | goto end; | |
1133 | } | |
1134 | ||
1135 | /* Resolve packet header type */ | |
1136 | if (flags & BT_CTF_RESOLVE_FLAG_PACKET_HEADER) { | |
1137 | ret = resolve_root_type(CTF_NODE_TRACE_PACKET_HEADER, &ctx); | |
1138 | if (ret) { | |
1139 | _printf_error("Cannot resolve trace packet header type\n"); | |
1140 | goto end; | |
1141 | } | |
1142 | } | |
1143 | ||
1144 | /* Resolve packet context type */ | |
1145 | if (flags & BT_CTF_RESOLVE_FLAG_PACKET_CONTEXT) { | |
1146 | ret = resolve_root_type(CTF_NODE_STREAM_PACKET_CONTEXT, &ctx); | |
1147 | if (ret) { | |
1148 | _printf_error("Cannot resolve stream packet context type\n"); | |
1149 | goto end; | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | /* Resolve event header type */ | |
1154 | if (flags & BT_CTF_RESOLVE_FLAG_EVENT_HEADER) { | |
1155 | ret = resolve_root_type(CTF_NODE_STREAM_EVENT_HEADER, &ctx); | |
1156 | ||
1157 | if (ret) { | |
1158 | _printf_error("Cannot resolve stream event header type\n"); | |
1159 | goto end; | |
1160 | } | |
1161 | } | |
1162 | ||
1163 | /* Resolve stream event context type */ | |
1164 | if (flags & BT_CTF_RESOLVE_FLAG_STREAM_EVENT_CTX) { | |
1165 | ret = resolve_root_type(CTF_NODE_STREAM_EVENT_CONTEXT, &ctx); | |
1166 | if (ret) { | |
1167 | _printf_error("Cannot resolve stream event context type\n"); | |
1168 | goto end; | |
1169 | } | |
1170 | } | |
1171 | ||
1172 | /* Resolve event context type */ | |
1173 | if (flags & BT_CTF_RESOLVE_FLAG_EVENT_CONTEXT) { | |
1174 | ret = resolve_root_type(CTF_NODE_EVENT_CONTEXT, &ctx); | |
1175 | if (ret) { | |
1176 | _printf_error("Cannot resolve event context type\n"); | |
1177 | goto end; | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | /* Resolve event payload type */ | |
1182 | if (flags & BT_CTF_RESOLVE_FLAG_EVENT_PAYLOAD) { | |
1183 | ret = resolve_root_type(CTF_NODE_EVENT_FIELDS, &ctx); | |
1184 | if (ret) { | |
1185 | _printf_error("Cannot resolve event payload type\n"); | |
1186 | goto end; | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | end: | |
1191 | type_stack_destroy(ctx.type_stack); | |
1192 | ||
1193 | return ret; | |
1194 | } |