Commit | Line | Data |
---|---|---|
ca9f27f3 FD |
1 | /* |
2 | * Babeltrace - Trace IR field copy | |
3 | * | |
4 | * Copyright (c) 2015-2019 EfficiOS Inc. and Linux Foundation | |
5 | * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com> | |
6 | * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com> | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | * SOFTWARE. | |
25 | */ | |
26 | ||
91bc8451 | 27 | #define BT_COMP_LOG_SELF_COMP (md_maps->self_comp) |
3a3d15f3 | 28 | #define BT_LOG_OUTPUT_LEVEL (md_maps->log_level) |
350ad6c1 | 29 | #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-META-FC-COPY" |
91bc8451 | 30 | #include "plugins/comp-logging.h" |
ca9f27f3 | 31 | |
578e048b MJ |
32 | #include "common/assert.h" |
33 | #include "common/common.h" | |
34 | #include "compat/compiler.h" | |
3fadfbc0 | 35 | #include <babeltrace2/babeltrace.h> |
ca9f27f3 FD |
36 | |
37 | #include "trace-ir-metadata-copy.h" | |
38 | #include "trace-ir-metadata-field-class-copy.h" | |
39 | ||
40 | /* | |
41 | * This fonction walks througth the nested structures field class to resolve a | |
42 | * field path object. A field path is made of indexes inside possibly nested | |
43 | * structures ultimately leading to a field class. | |
44 | */ | |
45 | static | |
3a3d15f3 PP |
46 | const bt_field_class *walk_field_path(struct trace_ir_metadata_maps *md_maps, |
47 | const bt_field_path *fp, const bt_field_class *fc) | |
ca9f27f3 | 48 | { |
66ddcddf | 49 | uint64_t i, fp_item_count; |
ca9f27f3 FD |
50 | const bt_field_class *curr_fc; |
51 | ||
52 | BT_ASSERT(bt_field_class_get_type(fc) == BT_FIELD_CLASS_TYPE_STRUCTURE); | |
91bc8451 | 53 | BT_COMP_LOGD("Walking field path on field class: fp-addr=%p, fc-addr=%p", |
ca9f27f3 FD |
54 | fp, fc); |
55 | ||
66ddcddf | 56 | fp_item_count = bt_field_path_get_item_count(fp); |
ca9f27f3 | 57 | curr_fc = fc; |
66ddcddf | 58 | for (i = 0; i < fp_item_count; i++) { |
ca9f27f3 | 59 | bt_field_class_type fc_type = bt_field_class_get_type(curr_fc); |
66ddcddf PP |
60 | const bt_field_path_item *fp_item = |
61 | bt_field_path_borrow_item_by_index_const(fp, i); | |
ca9f27f3 FD |
62 | |
63 | switch (fc_type) { | |
64 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
1e6fd1d7 | 65 | { |
66ddcddf PP |
66 | const bt_field_class_structure_member *member; |
67 | ||
68 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == | |
69 | BT_FIELD_PATH_ITEM_TYPE_INDEX); | |
70 | member = bt_field_class_structure_borrow_member_by_index_const( | |
71 | curr_fc, | |
72 | bt_field_path_item_index_get_index(fp_item)); | |
1e6fd1d7 PP |
73 | curr_fc = bt_field_class_structure_member_borrow_field_class_const( |
74 | member); | |
ca9f27f3 | 75 | break; |
1e6fd1d7 | 76 | } |
ca9f27f3 | 77 | case BT_FIELD_CLASS_TYPE_VARIANT: |
1e6fd1d7 | 78 | { |
66ddcddf PP |
79 | const bt_field_class_variant_option *option; |
80 | ||
81 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == | |
82 | BT_FIELD_PATH_ITEM_TYPE_INDEX); | |
83 | option = bt_field_class_variant_borrow_option_by_index_const( | |
84 | curr_fc, | |
85 | bt_field_path_item_index_get_index(fp_item)); | |
1e6fd1d7 PP |
86 | curr_fc = bt_field_class_variant_option_borrow_field_class_const( |
87 | option); | |
ca9f27f3 | 88 | break; |
1e6fd1d7 | 89 | } |
66ddcddf PP |
90 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
91 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
92 | { | |
93 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == | |
94 | BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT); | |
95 | curr_fc = bt_field_class_array_borrow_element_field_class_const( | |
96 | curr_fc); | |
97 | break; | |
98 | } | |
ca9f27f3 FD |
99 | default: |
100 | abort(); | |
101 | } | |
102 | } | |
103 | ||
104 | return curr_fc; | |
105 | } | |
106 | ||
107 | static | |
108 | const bt_field_class *resolve_field_path_to_field_class(const bt_field_path *fp, | |
109 | struct trace_ir_metadata_maps *md_maps) | |
110 | { | |
111 | struct field_class_resolving_context *fc_resolving_ctx; | |
112 | const bt_field_class *fc; | |
113 | bt_scope fp_scope; | |
114 | ||
91bc8451 | 115 | BT_COMP_LOGD("Resolving field path: fp-addr=%p", fp); |
ca9f27f3 FD |
116 | |
117 | fc_resolving_ctx = md_maps->fc_resolving_ctx; | |
118 | fp_scope = bt_field_path_get_root_scope(fp); | |
119 | ||
120 | switch (fp_scope) { | |
121 | case BT_SCOPE_PACKET_CONTEXT: | |
3a3d15f3 PP |
122 | fc = walk_field_path(md_maps, fp, |
123 | fc_resolving_ctx->packet_context); | |
ca9f27f3 FD |
124 | break; |
125 | case BT_SCOPE_EVENT_COMMON_CONTEXT: | |
3a3d15f3 PP |
126 | fc = walk_field_path(md_maps, fp, |
127 | fc_resolving_ctx->event_common_context); | |
ca9f27f3 FD |
128 | break; |
129 | case BT_SCOPE_EVENT_SPECIFIC_CONTEXT: | |
3a3d15f3 PP |
130 | fc = walk_field_path(md_maps, fp, |
131 | fc_resolving_ctx->event_specific_context); | |
ca9f27f3 FD |
132 | break; |
133 | case BT_SCOPE_EVENT_PAYLOAD: | |
3a3d15f3 PP |
134 | fc = walk_field_path(md_maps, fp, |
135 | fc_resolving_ctx->event_payload); | |
ca9f27f3 FD |
136 | break; |
137 | default: | |
138 | abort(); | |
139 | } | |
140 | ||
141 | return fc; | |
142 | } | |
143 | ||
144 | static inline | |
145 | void field_class_integer_set_props(const bt_field_class *input_fc, | |
146 | bt_field_class *output_fc) | |
147 | { | |
148 | bt_field_class_integer_set_preferred_display_base(output_fc, | |
149 | bt_field_class_integer_get_preferred_display_base(input_fc)); | |
150 | bt_field_class_integer_set_field_value_range(output_fc, | |
151 | bt_field_class_integer_get_field_value_range(input_fc)); | |
152 | } | |
153 | ||
154 | static inline | |
155 | int field_class_unsigned_integer_copy( | |
156 | struct trace_ir_metadata_maps *md_maps, | |
157 | const bt_field_class *in_field_class, | |
158 | bt_field_class *out_field_class) | |
159 | { | |
91bc8451 | 160 | BT_COMP_LOGD("Copying content of unsigned integer field class: " |
ca9f27f3 FD |
161 | "in-fc-addr=%p, out-fc-addr=%p", |
162 | in_field_class, out_field_class); | |
163 | ||
164 | field_class_integer_set_props(in_field_class, out_field_class); | |
165 | ||
91bc8451 | 166 | BT_COMP_LOGD("Copied content of unsigned integer field class: " |
ca9f27f3 FD |
167 | "in-fc-addr=%p, out-fc-addr=%p", |
168 | in_field_class, out_field_class); | |
169 | return 0; | |
170 | } | |
171 | ||
172 | static inline | |
173 | int field_class_signed_integer_copy( | |
174 | struct trace_ir_metadata_maps *md_maps, | |
175 | const bt_field_class *in_field_class, | |
176 | bt_field_class *out_field_class) | |
177 | { | |
91bc8451 | 178 | BT_COMP_LOGD("Copying content of signed integer field class: " |
ca9f27f3 FD |
179 | "in-fc-addr=%p, out-fc-addr=%p", |
180 | in_field_class, out_field_class); | |
181 | ||
182 | field_class_integer_set_props(in_field_class, out_field_class); | |
183 | ||
91bc8451 | 184 | BT_COMP_LOGD("Copied content of signed integer field class: " |
ca9f27f3 FD |
185 | "in-fc-addr=%p, out-fc-addr=%p", |
186 | in_field_class, out_field_class); | |
187 | return 0; | |
188 | } | |
189 | ||
190 | BT_HIDDEN | |
191 | int field_class_unsigned_enumeration_copy( | |
192 | struct trace_ir_metadata_maps *md_maps, | |
193 | const bt_field_class *in_field_class, | |
194 | bt_field_class *out_field_class) | |
195 | { | |
196 | uint64_t i, enum_mapping_count; | |
197 | int ret = 0; | |
198 | ||
91bc8451 | 199 | BT_COMP_LOGD("Copying content of unsigned enumeration field class: " |
ca9f27f3 FD |
200 | "in-fc-addr=%p, out-fc-addr=%p", |
201 | in_field_class, out_field_class); | |
202 | ||
203 | /* Copy properties of the inner integer. */ | |
204 | field_class_integer_set_props(in_field_class, out_field_class); | |
205 | ||
206 | /* Copy all enumeration entries. */ | |
207 | enum_mapping_count = bt_field_class_enumeration_get_mapping_count(in_field_class); | |
208 | for (i = 0; i < enum_mapping_count; i++) { | |
209 | const char *label; | |
8f3ccfbc PP |
210 | const bt_field_class_unsigned_enumeration_mapping *u_mapping; |
211 | const bt_field_class_enumeration_mapping *mapping; | |
ca9f27f3 FD |
212 | uint64_t range_index, range_count; |
213 | ||
214 | /* Get the ranges and the range count. */ | |
8f3ccfbc PP |
215 | u_mapping = bt_field_class_unsigned_enumeration_borrow_mapping_by_index_const( |
216 | in_field_class, i); | |
217 | mapping = bt_field_class_unsigned_enumeration_mapping_as_mapping_const( | |
218 | u_mapping); | |
ca9f27f3 | 219 | range_count = |
8f3ccfbc PP |
220 | bt_field_class_enumeration_mapping_get_range_count( |
221 | mapping); | |
222 | label = bt_field_class_enumeration_mapping_get_label( | |
223 | mapping); | |
224 | ||
ca9f27f3 FD |
225 | /* |
226 | * Iterate over all the ranges to add them to copied field | |
227 | * class. | |
228 | */ | |
229 | for (range_index = 0; range_index < range_count; range_index++) { | |
230 | uint64_t lower, upper; | |
231 | bt_field_class_status status; | |
8f3ccfbc PP |
232 | bt_field_class_unsigned_enumeration_mapping_get_range_by_index( |
233 | u_mapping, range_index, &lower, &upper); | |
ca9f27f3 | 234 | |
91bc8451 | 235 | BT_COMP_LOGD("Copying range in enumeration field class: " |
ca9f27f3 FD |
236 | "label=%s, lower=%"PRId64", upper=%"PRId64, |
237 | label, lower, upper); | |
238 | ||
239 | /* Add the label and its range to the copy field class. */ | |
240 | status = bt_field_class_unsigned_enumeration_map_range( | |
241 | out_field_class, label, lower, upper); | |
242 | ||
243 | if (status != BT_FIELD_CLASS_STATUS_OK) { | |
91bc8451 | 244 | BT_COMP_LOGE_STR("Failed to add range to unsigned " |
ca9f27f3 FD |
245 | "enumeration."); |
246 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_field_class); | |
247 | ret = -1; | |
248 | goto error; | |
249 | } | |
250 | } | |
251 | } | |
252 | ||
91bc8451 | 253 | BT_COMP_LOGD("Copied content of unsigned enumeration field class: " |
ca9f27f3 FD |
254 | "in-fc-addr=%p, out-fc-addr=%p", |
255 | in_field_class, out_field_class); | |
256 | ||
257 | error: | |
258 | return ret; | |
259 | } | |
260 | ||
261 | static inline | |
262 | int field_class_signed_enumeration_copy( | |
263 | struct trace_ir_metadata_maps *md_maps, | |
264 | const bt_field_class *in_field_class, | |
265 | bt_field_class *out_field_class) | |
266 | { | |
267 | uint64_t i, enum_mapping_count; | |
268 | int ret = 0; | |
269 | ||
91bc8451 | 270 | BT_COMP_LOGD("Copying content of signed enumeration field class: " |
ca9f27f3 FD |
271 | "in-fc-addr=%p, out-fc-addr=%p", |
272 | in_field_class, out_field_class); | |
273 | ||
274 | /* Copy properties of the inner integer. */ | |
275 | field_class_integer_set_props(in_field_class, out_field_class); | |
276 | ||
277 | /* Copy all enumeration entries. */ | |
278 | enum_mapping_count = | |
279 | bt_field_class_enumeration_get_mapping_count(in_field_class); | |
280 | for (i = 0; i < enum_mapping_count; i++) { | |
281 | const char *label; | |
8f3ccfbc PP |
282 | const bt_field_class_signed_enumeration_mapping *i_mapping; |
283 | const bt_field_class_enumeration_mapping *mapping; | |
ca9f27f3 FD |
284 | uint64_t range_index, range_count; |
285 | ||
286 | /* Get the ranges and the range count. */ | |
8f3ccfbc PP |
287 | i_mapping = bt_field_class_signed_enumeration_borrow_mapping_by_index_const( |
288 | in_field_class, i); | |
289 | mapping = bt_field_class_signed_enumeration_mapping_as_mapping_const( | |
290 | i_mapping); | |
ca9f27f3 | 291 | range_count = |
8f3ccfbc PP |
292 | bt_field_class_enumeration_mapping_get_range_count( |
293 | mapping); | |
294 | label = bt_field_class_enumeration_mapping_get_label( | |
295 | mapping); | |
296 | ||
ca9f27f3 FD |
297 | /* |
298 | * Iterate over all the ranges to add them to copied field | |
299 | * class. | |
300 | */ | |
301 | for (range_index = 0; range_index < range_count; range_index++) { | |
302 | int64_t lower, upper; | |
303 | bt_field_class_status status; | |
8f3ccfbc PP |
304 | bt_field_class_signed_enumeration_mapping_get_range_by_index( |
305 | i_mapping, range_index, &lower, &upper); | |
ca9f27f3 | 306 | |
91bc8451 | 307 | BT_COMP_LOGD("Copying range in enumeration field class: " |
fca9fd7d | 308 | "label=%s, lower=%"PRId64", upper=%"PRId64, |
ca9f27f3 FD |
309 | label, lower, upper); |
310 | ||
311 | /* Add the label and its range to the copy field class. */ | |
312 | status = bt_field_class_signed_enumeration_map_range( | |
313 | out_field_class, label, lower, upper); | |
314 | if (status != BT_FIELD_CLASS_STATUS_OK) { | |
91bc8451 | 315 | BT_COMP_LOGE_STR("Failed to add range to signed " |
ca9f27f3 FD |
316 | "enumeration."); |
317 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_field_class); | |
318 | ret = -1; | |
319 | goto error; | |
320 | } | |
321 | } | |
322 | } | |
323 | ||
91bc8451 | 324 | BT_COMP_LOGD("Copied content of signed enumeration field class: " |
ca9f27f3 FD |
325 | "in-fc-addr=%p, out-fc-addr=%p", |
326 | in_field_class, out_field_class); | |
327 | ||
328 | error: | |
329 | return ret; | |
330 | } | |
331 | ||
332 | static inline | |
333 | int field_class_real_copy( | |
334 | struct trace_ir_metadata_maps *md_maps, | |
335 | const bt_field_class *in_field_class, | |
336 | bt_field_class *out_field_class) | |
337 | { | |
91bc8451 | 338 | BT_COMP_LOGD("Copying content of real field class: " |
ca9f27f3 FD |
339 | "in-fc-addr=%p, out-fc-addr=%p", |
340 | in_field_class, out_field_class); | |
341 | ||
342 | bt_field_class_real_set_is_single_precision(out_field_class, | |
343 | bt_field_class_real_is_single_precision(in_field_class)); | |
344 | ||
91bc8451 | 345 | BT_COMP_LOGD("Copied content real field class: in-fc-addr=%p, " |
ca9f27f3 FD |
346 | "out-fc-addr=%p", in_field_class, out_field_class); |
347 | ||
348 | return 0; | |
349 | } | |
350 | ||
351 | static inline | |
352 | int field_class_structure_copy( | |
353 | struct trace_ir_metadata_maps *md_maps, | |
354 | const bt_field_class *in_field_class, | |
355 | bt_field_class *out_field_class) | |
356 | { | |
357 | uint64_t i, struct_member_count; | |
358 | bt_field_class_status status; | |
359 | int ret = 0; | |
360 | ||
91bc8451 | 361 | BT_COMP_LOGD("Copying content of structure field class: " |
ca9f27f3 FD |
362 | "in-fc-addr=%p, out-fc-addr=%p", |
363 | in_field_class, out_field_class); | |
364 | /* Get the number of member in that struct. */ | |
365 | struct_member_count = | |
366 | bt_field_class_structure_get_member_count(in_field_class); | |
367 | ||
368 | /* Iterate over all the members of the struct. */ | |
369 | for (i = 0; i < struct_member_count; i++) { | |
1e6fd1d7 | 370 | const bt_field_class_structure_member *member; |
ca9f27f3 FD |
371 | const char *member_name; |
372 | const bt_field_class *member_fc; | |
373 | bt_field_class *out_member_field_class; | |
374 | ||
1e6fd1d7 PP |
375 | member = bt_field_class_structure_borrow_member_by_index_const( |
376 | in_field_class, i); | |
377 | member_fc = bt_field_class_structure_member_borrow_field_class_const( | |
378 | member); | |
379 | member_name = bt_field_class_structure_member_get_name(member); | |
91bc8451 | 380 | BT_COMP_LOGD("Copying structure field class's field: " |
ca9f27f3 FD |
381 | "index=%" PRId64 ", " |
382 | "member-fc-addr=%p, field-name=\"%s\"", | |
383 | i, member_fc, member_name); | |
384 | ||
385 | out_member_field_class = create_field_class_copy(md_maps, | |
386 | member_fc); | |
387 | if (!out_member_field_class) { | |
91bc8451 | 388 | BT_COMP_LOGE("Cannot copy structure field class's field: " |
ca9f27f3 FD |
389 | "index=%" PRId64 ", " |
390 | "field-fc-addr=%p, field-name=\"%s\"", | |
391 | i, member_fc, member_name); | |
392 | ret = -1; | |
393 | goto error; | |
394 | } | |
395 | ret = copy_field_class_content(md_maps, member_fc, | |
396 | out_member_field_class); | |
397 | if (ret) { | |
398 | goto error; | |
399 | } | |
400 | ||
401 | status = bt_field_class_structure_append_member(out_field_class, | |
402 | member_name, out_member_field_class); | |
403 | if (status != BT_FIELD_CLASS_STATUS_OK) { | |
91bc8451 | 404 | BT_COMP_LOGE("Cannot append structure field class's field: " |
ca9f27f3 FD |
405 | "index=%" PRId64 ", " |
406 | "field-fc-addr=%p, field-name=\"%s\"", | |
407 | i, member_fc, member_name); | |
408 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_member_field_class); | |
409 | ret = -1; | |
410 | goto error; | |
411 | } | |
412 | } | |
413 | ||
91bc8451 | 414 | BT_COMP_LOGD("Copied structure field class: original-fc-addr=%p, copy-fc-addr=%p", |
ca9f27f3 FD |
415 | in_field_class, out_field_class); |
416 | ||
417 | error: | |
418 | return ret; | |
419 | } | |
420 | ||
421 | static inline | |
422 | int field_class_variant_copy( | |
423 | struct trace_ir_metadata_maps *md_maps, | |
424 | const bt_field_class *in_field_class, | |
425 | bt_field_class *out_field_class) | |
426 | { | |
1ec009b2 | 427 | bt_field_class *out_tag_field_class = NULL; |
ca9f27f3 FD |
428 | uint64_t i, variant_option_count; |
429 | const bt_field_path *tag_fp; | |
430 | const bt_field_class *tag_fc; | |
431 | int ret = 0; | |
432 | ||
91bc8451 | 433 | BT_COMP_LOGD("Copying content of variant field class: " |
ca9f27f3 FD |
434 | "in-fc-addr=%p, out-fc-addr=%p", |
435 | in_field_class, out_field_class); | |
436 | ||
437 | tag_fp = bt_field_class_variant_borrow_selector_field_path_const( | |
438 | in_field_class); | |
439 | if (tag_fp) { | |
440 | tag_fc = resolve_field_path_to_field_class(tag_fp, | |
441 | md_maps); | |
442 | ||
443 | out_tag_field_class = g_hash_table_lookup( | |
444 | md_maps->field_class_map, tag_fc); | |
445 | if (!out_tag_field_class) { | |
91bc8451 | 446 | BT_COMP_LOGE_STR("Cannot find the tag field class."); |
ca9f27f3 FD |
447 | ret = -1; |
448 | goto error; | |
449 | } | |
450 | bt_field_class_variant_set_selector_field_class(out_field_class, | |
451 | out_tag_field_class); | |
452 | } | |
453 | ||
454 | variant_option_count = | |
455 | bt_field_class_variant_get_option_count(in_field_class); | |
456 | for (i = 0; i < variant_option_count; i++) { | |
1e6fd1d7 | 457 | const bt_field_class *option_fc; |
ca9f27f3 FD |
458 | const char *option_name; |
459 | bt_field_class *out_option_field_class; | |
460 | bt_field_class_status status; | |
1e6fd1d7 | 461 | const bt_field_class_variant_option *option; |
ca9f27f3 | 462 | |
1e6fd1d7 PP |
463 | option = bt_field_class_variant_borrow_option_by_index_const( |
464 | in_field_class, i); | |
465 | option_fc = bt_field_class_variant_option_borrow_field_class_const( | |
466 | option); | |
467 | option_name = bt_field_class_variant_option_get_name(option); | |
ca9f27f3 | 468 | out_option_field_class = create_field_class_copy_internal( |
1e6fd1d7 | 469 | md_maps, option_fc); |
ca9f27f3 | 470 | if (!out_option_field_class) { |
91bc8451 | 471 | BT_COMP_LOGE_STR("Cannot copy field class."); |
ca9f27f3 FD |
472 | ret = -1; |
473 | goto error; | |
474 | } | |
1e6fd1d7 | 475 | ret = copy_field_class_content_internal(md_maps, option_fc, |
ca9f27f3 FD |
476 | out_option_field_class); |
477 | if (ret) { | |
91bc8451 | 478 | BT_COMP_LOGE_STR("Error copying content of option variant " |
ca9f27f3 FD |
479 | "field class'"); |
480 | goto error; | |
481 | } | |
482 | ||
483 | status = bt_field_class_variant_append_option( | |
484 | out_field_class, option_name, | |
485 | out_option_field_class); | |
486 | if (status != BT_FIELD_CLASS_STATUS_OK) { | |
91bc8451 | 487 | BT_COMP_LOGE_STR("Cannot append option to variant field class'"); |
ca9f27f3 FD |
488 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_tag_field_class); |
489 | ret = -1; | |
490 | goto error; | |
491 | } | |
492 | } | |
493 | ||
91bc8451 | 494 | BT_COMP_LOGD("Copied content of variant field class: in-fc-addr=%p, " |
ca9f27f3 FD |
495 | "out-fc-addr=%p", in_field_class, out_field_class); |
496 | ||
497 | error: | |
498 | return ret; | |
499 | } | |
500 | ||
501 | static inline | |
502 | int field_class_static_array_copy( | |
503 | struct trace_ir_metadata_maps *md_maps, | |
504 | const bt_field_class *in_field_class, | |
505 | bt_field_class *out_field_class) | |
506 | { | |
91bc8451 | 507 | BT_COMP_LOGD("Copying content of static array field class: in-fc-addr=%p, " |
ca9f27f3 FD |
508 | "out-fc-addr=%p", in_field_class, out_field_class); |
509 | /* | |
510 | * There is no content to copy. Keep this function call anyway for | |
511 | * logging purposes. | |
512 | */ | |
91bc8451 | 513 | BT_COMP_LOGD("Copied content of static array field class: in-fc-addr=%p, " |
ca9f27f3 FD |
514 | "out-fc-addr=%p", in_field_class, out_field_class); |
515 | ||
516 | return 0; | |
517 | } | |
518 | ||
519 | static inline | |
520 | int field_class_dynamic_array_copy( | |
521 | struct trace_ir_metadata_maps *md_maps, | |
522 | const bt_field_class *in_field_class, | |
523 | bt_field_class *out_field_class) | |
524 | { | |
3b40fbf9 | 525 | const bt_field_class *len_fc; |
ca9f27f3 FD |
526 | const bt_field_path *len_fp; |
527 | bt_field_class_status status; | |
528 | bt_field_class *out_len_field_class; | |
529 | int ret = 0; | |
530 | ||
91bc8451 | 531 | BT_COMP_LOGD("Copying content of dynamic array field class: " |
ca9f27f3 FD |
532 | "in-fc-addr=%p, out-fc-addr=%p", |
533 | in_field_class, out_field_class); | |
534 | ||
ca9f27f3 FD |
535 | len_fp = bt_field_class_dynamic_array_borrow_length_field_path_const( |
536 | in_field_class); | |
537 | ||
538 | if (len_fp) { | |
91bc8451 | 539 | BT_COMP_LOGD("Copying dynamic array length field class using " |
ca9f27f3 FD |
540 | "field path: in-len-fp=%p", len_fp); |
541 | len_fc = resolve_field_path_to_field_class( | |
542 | len_fp, md_maps); | |
543 | out_len_field_class = g_hash_table_lookup( | |
544 | md_maps->field_class_map, len_fc); | |
545 | if (!out_len_field_class) { | |
91bc8451 | 546 | BT_COMP_LOGE_STR("Cannot find the output matching length" |
ca9f27f3 FD |
547 | "field class."); |
548 | ret = -1; | |
549 | goto error; | |
550 | } | |
551 | ||
552 | status = bt_field_class_dynamic_array_set_length_field_class( | |
553 | out_field_class, out_len_field_class); | |
554 | if (status != BT_FIELD_CLASS_STATUS_OK) { | |
91bc8451 | 555 | BT_COMP_LOGE_STR("Cannot set dynamic array field class' " |
ca9f27f3 FD |
556 | "length field class."); |
557 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_len_field_class); | |
558 | ret = -1; | |
559 | goto error; | |
560 | } | |
561 | } | |
562 | ||
91bc8451 | 563 | BT_COMP_LOGD("Copied dynamic array field class: in-fc-addr=%p, " |
ca9f27f3 FD |
564 | "out-fc-addr=%p", in_field_class, out_field_class); |
565 | ||
566 | error: | |
567 | return ret; | |
568 | } | |
569 | ||
570 | static inline | |
571 | int field_class_string_copy(struct trace_ir_metadata_maps *md_maps, | |
572 | const bt_field_class *in_field_class, | |
573 | bt_field_class *out_field_class) | |
574 | { | |
91bc8451 | 575 | BT_COMP_LOGD("Copying content of string field class: in-fc-addr=%p, " |
ca9f27f3 FD |
576 | "out-fc-addr=%p", in_field_class, out_field_class); |
577 | /* | |
578 | * There is no content to copy. Keep this function call anyway for | |
579 | * logging purposes. | |
580 | */ | |
91bc8451 | 581 | BT_COMP_LOGD("Copied content of string field class: in-fc-addr=%p, " |
ca9f27f3 FD |
582 | "out-fc-addr=%p", in_field_class, out_field_class); |
583 | ||
584 | return 0; | |
585 | } | |
586 | ||
587 | static | |
588 | bt_field_class *copy_field_class_array_element(struct trace_ir_metadata_maps *md_maps, | |
589 | const bt_field_class *in_elem_fc) | |
590 | { | |
591 | int ret; | |
592 | bt_field_class *out_elem_fc = | |
593 | create_field_class_copy_internal(md_maps, in_elem_fc); | |
594 | if (!out_elem_fc) { | |
91bc8451 | 595 | BT_COMP_LOGE("Error creating output elem field class " |
ca9f27f3 FD |
596 | "from input elem field class for static array: " |
597 | "in-fc-addr=%p", in_elem_fc); | |
598 | goto error; | |
599 | } | |
600 | ||
601 | ret = copy_field_class_content_internal(md_maps, in_elem_fc, out_elem_fc); | |
602 | if (ret) { | |
91bc8451 | 603 | BT_COMP_LOGE("Error creating output elem field class " |
ca9f27f3 FD |
604 | "from input elem field class for static array: " |
605 | "in-fc-addr=%p", in_elem_fc); | |
606 | BT_FIELD_CLASS_PUT_REF_AND_RESET(out_elem_fc); | |
607 | goto error; | |
608 | } | |
609 | ||
610 | error: | |
611 | return out_elem_fc; | |
612 | } | |
613 | ||
614 | BT_HIDDEN | |
615 | bt_field_class *create_field_class_copy_internal(struct trace_ir_metadata_maps *md_maps, | |
616 | const bt_field_class *in_field_class) | |
617 | { | |
618 | bt_field_class *out_field_class = NULL; | |
619 | ||
91bc8451 | 620 | BT_COMP_LOGD("Creating bare field class based on field class: in-fc-addr=%p", |
ca9f27f3 FD |
621 | in_field_class); |
622 | ||
623 | switch(bt_field_class_get_type(in_field_class)) { | |
624 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: | |
625 | out_field_class = bt_field_class_unsigned_integer_create( | |
626 | md_maps->output_trace_class); | |
627 | break; | |
628 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
629 | out_field_class = bt_field_class_signed_integer_create( | |
630 | md_maps->output_trace_class); | |
631 | break; | |
632 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
633 | out_field_class = bt_field_class_unsigned_enumeration_create( | |
634 | md_maps->output_trace_class); | |
635 | break; | |
636 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
637 | out_field_class = bt_field_class_signed_enumeration_create( | |
638 | md_maps->output_trace_class); | |
639 | break; | |
640 | case BT_FIELD_CLASS_TYPE_REAL: | |
641 | out_field_class = bt_field_class_real_create( | |
642 | md_maps->output_trace_class); | |
643 | break; | |
644 | case BT_FIELD_CLASS_TYPE_STRING: | |
645 | out_field_class = bt_field_class_string_create( | |
646 | md_maps->output_trace_class); | |
647 | break; | |
648 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
649 | out_field_class = bt_field_class_structure_create( | |
650 | md_maps->output_trace_class); | |
651 | break; | |
652 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
653 | { | |
654 | const bt_field_class *in_elem_fc = | |
655 | bt_field_class_array_borrow_element_field_class_const( | |
656 | in_field_class); | |
657 | uint64_t array_len = | |
658 | bt_field_class_static_array_get_length(in_field_class); | |
659 | ||
660 | bt_field_class *out_elem_fc = copy_field_class_array_element( | |
661 | md_maps, in_elem_fc); | |
662 | if (!out_elem_fc) { | |
663 | out_field_class = NULL; | |
664 | goto error; | |
665 | } | |
666 | ||
667 | out_field_class = bt_field_class_static_array_create( | |
668 | md_maps->output_trace_class, | |
669 | out_elem_fc, array_len); | |
670 | break; | |
671 | } | |
672 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
673 | { | |
674 | const bt_field_class *in_elem_fc = | |
675 | bt_field_class_array_borrow_element_field_class_const( | |
676 | in_field_class); | |
677 | ||
678 | bt_field_class *out_elem_fc = copy_field_class_array_element( | |
679 | md_maps, in_elem_fc); | |
680 | if (!out_elem_fc) { | |
681 | out_field_class = NULL; | |
682 | goto error; | |
683 | } | |
684 | ||
685 | out_field_class = bt_field_class_dynamic_array_create( | |
686 | md_maps->output_trace_class, | |
687 | out_elem_fc); | |
688 | break; | |
689 | } | |
690 | case BT_FIELD_CLASS_TYPE_VARIANT: | |
691 | out_field_class = bt_field_class_variant_create( | |
692 | md_maps->output_trace_class); | |
693 | break; | |
694 | default: | |
695 | abort(); | |
696 | } | |
697 | ||
698 | /* | |
699 | * Add mapping from in_field_class to out_field_class. This simplifies | |
700 | * the resolution of field paths in variant and dynamic array field | |
701 | * classes. | |
702 | */ | |
703 | g_hash_table_insert(md_maps->field_class_map, | |
704 | (gpointer) in_field_class, out_field_class); | |
705 | ||
706 | error: | |
707 | if(out_field_class){ | |
91bc8451 | 708 | BT_COMP_LOGD("Created bare field class based on field class: in-fc-addr=%p, " |
ca9f27f3 FD |
709 | "out-fc-addr=%p", in_field_class, out_field_class); |
710 | } else { | |
91bc8451 | 711 | BT_COMP_LOGE("Error creating output field class from input field " |
ca9f27f3 FD |
712 | "class: in-fc-addr=%p", in_field_class); |
713 | } | |
714 | ||
715 | return out_field_class; | |
716 | } | |
717 | ||
718 | BT_HIDDEN | |
719 | int copy_field_class_content_internal( | |
720 | struct trace_ir_metadata_maps *md_maps, | |
721 | const bt_field_class *in_field_class, | |
722 | bt_field_class *out_field_class) | |
723 | { | |
724 | int ret = 0; | |
725 | switch(bt_field_class_get_type(in_field_class)) { | |
726 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: | |
727 | ret = field_class_unsigned_integer_copy(md_maps, | |
728 | in_field_class, out_field_class); | |
729 | break; | |
730 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
731 | ret = field_class_signed_integer_copy(md_maps, | |
732 | in_field_class, out_field_class); | |
733 | break; | |
734 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
735 | ret = field_class_unsigned_enumeration_copy(md_maps, | |
736 | in_field_class, out_field_class); | |
737 | break; | |
738 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
739 | ret = field_class_signed_enumeration_copy(md_maps, | |
740 | in_field_class, out_field_class); | |
741 | break; | |
742 | case BT_FIELD_CLASS_TYPE_REAL: | |
743 | ret = field_class_real_copy(md_maps, | |
744 | in_field_class, out_field_class); | |
745 | break; | |
746 | case BT_FIELD_CLASS_TYPE_STRING: | |
747 | ret = field_class_string_copy(md_maps, | |
748 | in_field_class, out_field_class); | |
749 | break; | |
750 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
751 | ret = field_class_structure_copy(md_maps, | |
752 | in_field_class, out_field_class); | |
753 | break; | |
754 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
755 | ret = field_class_static_array_copy(md_maps, | |
756 | in_field_class, out_field_class); | |
757 | break; | |
758 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
759 | ret = field_class_dynamic_array_copy(md_maps, | |
760 | in_field_class, out_field_class); | |
761 | break; | |
762 | case BT_FIELD_CLASS_TYPE_VARIANT: | |
763 | ret = field_class_variant_copy(md_maps, | |
764 | in_field_class, out_field_class); | |
765 | break; | |
766 | default: | |
767 | abort(); | |
768 | } | |
769 | ||
770 | return ret; | |
771 | } |