Commit | Line | Data |
---|---|---|
44c440bc | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
44c440bc | 3 | * |
0235b0db | 4 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> |
44c440bc PP |
5 | */ |
6 | ||
3fadfbc0 | 7 | #include <babeltrace2/babeltrace.h> |
91d81473 | 8 | #include "common/macros.h" |
578e048b | 9 | #include "common/assert.h" |
44c440bc PP |
10 | #include <glib.h> |
11 | #include <stdint.h> | |
12 | #include <string.h> | |
13 | #include <inttypes.h> | |
14 | ||
087cd0f5 | 15 | #include "ctf-meta-visitors.hpp" |
44c440bc | 16 | |
4164020e SM |
17 | static int update_field_class_stored_value_index(struct ctf_field_class *fc, |
18 | struct ctf_trace_class *tc, | |
19 | struct ctf_stream_class *sc, | |
20 | struct ctf_event_class *ec) | |
44c440bc | 21 | { |
4164020e SM |
22 | int ret = 0; |
23 | uint64_t i; | |
24 | struct ctf_field_path *field_path = NULL; | |
25 | struct ctf_field_class_int *tgt_fc = NULL; | |
26 | uint64_t *stored_value_index = NULL; | |
27 | ||
28 | if (!fc) { | |
29 | goto end; | |
30 | } | |
31 | ||
32 | switch (fc->type) { | |
33 | case CTF_FIELD_CLASS_TYPE_VARIANT: | |
34 | { | |
35 | ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc); | |
36 | ||
37 | field_path = &var_fc->tag_path; | |
38 | stored_value_index = &var_fc->stored_tag_index; | |
39 | tgt_fc = &var_fc->tag_fc->base; | |
40 | break; | |
41 | } | |
42 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
43 | { | |
44 | struct ctf_field_class_sequence *seq_fc = ctf_field_class_as_sequence(fc); | |
45 | ||
46 | field_path = &seq_fc->length_path; | |
47 | stored_value_index = &seq_fc->stored_length_index; | |
48 | tgt_fc = seq_fc->length_fc; | |
49 | break; | |
50 | } | |
51 | default: | |
52 | break; | |
53 | } | |
54 | ||
55 | if (field_path) { | |
56 | BT_ASSERT(tgt_fc); | |
57 | BT_ASSERT(tgt_fc->base.base.type == CTF_FIELD_CLASS_TYPE_INT || | |
58 | tgt_fc->base.base.type == CTF_FIELD_CLASS_TYPE_ENUM); | |
59 | if (tgt_fc->storing_index >= 0) { | |
60 | /* Already storing its value */ | |
61 | *stored_value_index = (uint64_t) tgt_fc->storing_index; | |
62 | } else { | |
63 | /* Not storing its value: allocate new index */ | |
64 | tgt_fc->storing_index = tc->stored_value_count; | |
65 | *stored_value_index = (uint64_t) tgt_fc->storing_index; | |
66 | tc->stored_value_count++; | |
67 | } | |
68 | } | |
69 | ||
70 | switch (fc->type) { | |
71 | case CTF_FIELD_CLASS_TYPE_STRUCT: | |
72 | { | |
73 | struct ctf_field_class_struct *struct_fc = ctf_field_class_as_struct(fc); | |
74 | ||
75 | for (i = 0; i < struct_fc->members->len; i++) { | |
76 | struct ctf_named_field_class *named_fc = | |
77 | ctf_field_class_struct_borrow_member_by_index(struct_fc, i); | |
78 | ||
79 | ret = update_field_class_stored_value_index(named_fc->fc, tc, sc, ec); | |
80 | if (ret) { | |
81 | goto end; | |
82 | } | |
83 | } | |
84 | ||
85 | break; | |
86 | } | |
87 | case CTF_FIELD_CLASS_TYPE_VARIANT: | |
88 | { | |
89 | struct ctf_field_class_variant *var_fc = ctf_field_class_as_variant(fc); | |
90 | ||
91 | for (i = 0; i < var_fc->options->len; i++) { | |
92 | struct ctf_named_field_class *named_fc = | |
93 | ctf_field_class_variant_borrow_option_by_index(var_fc, i); | |
94 | ||
95 | ret = update_field_class_stored_value_index(named_fc->fc, tc, sc, ec); | |
96 | if (ret) { | |
97 | goto end; | |
98 | } | |
99 | } | |
100 | ||
101 | break; | |
102 | } | |
103 | case CTF_FIELD_CLASS_TYPE_ARRAY: | |
104 | case CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
105 | { | |
106 | struct ctf_field_class_array_base *array_fc = ctf_field_class_as_array_base(fc); | |
107 | ||
108 | ret = update_field_class_stored_value_index(array_fc->elem_fc, tc, sc, ec); | |
109 | if (ret) { | |
110 | goto end; | |
111 | } | |
112 | ||
113 | break; | |
114 | } | |
115 | default: | |
116 | break; | |
117 | } | |
44c440bc PP |
118 | |
119 | end: | |
4164020e | 120 | return ret; |
44c440bc PP |
121 | } |
122 | ||
44c440bc PP |
123 | int ctf_trace_class_update_value_storing_indexes(struct ctf_trace_class *ctf_tc) |
124 | { | |
4164020e SM |
125 | uint64_t i; |
126 | ||
127 | if (!ctf_tc->is_translated) { | |
128 | update_field_class_stored_value_index(ctf_tc->packet_header_fc, ctf_tc, NULL, NULL); | |
129 | } | |
130 | ||
131 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { | |
132 | uint64_t j; | |
133 | ctf_stream_class *sc = (ctf_stream_class *) ctf_tc->stream_classes->pdata[i]; | |
134 | ||
135 | if (!sc->is_translated) { | |
136 | update_field_class_stored_value_index(sc->packet_context_fc, ctf_tc, sc, NULL); | |
137 | update_field_class_stored_value_index(sc->event_header_fc, ctf_tc, sc, NULL); | |
138 | update_field_class_stored_value_index(sc->event_common_context_fc, ctf_tc, sc, NULL); | |
139 | } | |
140 | ||
141 | for (j = 0; j < sc->event_classes->len; j++) { | |
142 | struct ctf_event_class *ec = (ctf_event_class *) sc->event_classes->pdata[j]; | |
143 | ||
144 | if (!ec->is_translated) { | |
145 | update_field_class_stored_value_index(ec->spec_context_fc, ctf_tc, sc, ec); | |
146 | update_field_class_stored_value_index(ec->payload_fc, ctf_tc, sc, ec); | |
147 | } | |
148 | } | |
149 | } | |
150 | ||
151 | return 0; | |
44c440bc | 152 | } |