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