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