Commit | Line | Data |
---|---|---|
b2f1f465 JD |
1 | /* |
2 | * clock-fields.c | |
3 | * | |
4 | * Babeltrace - Update clock fields to write uint64 values | |
5 | * | |
6 | * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com> | |
7 | * | |
8 | * Author: Julien Desfossez <jdesfossez@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
3e4f69d3 JD |
29 | #define BT_LOG_TAG "PLUGIN-CTFCOPYTRACE-LIB-CLOCK-FIELDS" |
30 | #include "logging.h" | |
31 | ||
9d408fca | 32 | #include <babeltrace/babeltrace.h> |
f6ccaed9 | 33 | #include <babeltrace/assert-internal.h> |
b2f1f465 JD |
34 | #include <stdio.h> |
35 | ||
36 | #include "clock-fields.h" | |
37 | ||
38 | static | |
50842bdc | 39 | int find_update_struct_clock_fields(FILE *err, struct bt_field_type *type, |
40f4ba76 | 40 | const struct bt_clock_class *writer_clock_class); |
b2f1f465 | 41 | static |
50842bdc | 42 | int find_update_array_clock_fields(FILE *err, struct bt_field_type *type, |
40f4ba76 | 43 | const struct bt_clock_class *writer_clock_class); |
b2f1f465 | 44 | static |
50842bdc | 45 | int find_update_enum_clock_fields(FILE *err, struct bt_field_type *type, |
40f4ba76 | 46 | const struct bt_clock_class *writer_clock_class); |
b2f1f465 | 47 | static |
50842bdc | 48 | int find_update_sequence_clock_fields(FILE *err, struct bt_field_type *type, |
40f4ba76 | 49 | const struct bt_clock_class *writer_clock_class); |
b2f1f465 | 50 | static |
50842bdc | 51 | int find_update_variant_clock_fields(FILE *err, struct bt_field_type *type, |
40f4ba76 | 52 | const struct bt_clock_class *writer_clock_class); |
b2f1f465 JD |
53 | |
54 | static | |
55 | int copy_find_clock_int_field(FILE *err, | |
40f4ba76 PP |
56 | const struct bt_event *event, const struct bt_event *writer_event, |
57 | const struct bt_field *field, struct bt_field_type *type, | |
58 | const struct bt_field *copy_field); | |
b2f1f465 JD |
59 | static |
60 | int copy_find_clock_struct_field(FILE *err, | |
40f4ba76 PP |
61 | const struct bt_event *event, const struct bt_event *writer_event, |
62 | const struct bt_field *field, struct bt_field_type *type, | |
63 | const struct bt_field *copy_field); | |
b2f1f465 JD |
64 | static |
65 | int copy_find_clock_array_field(FILE *err, | |
40f4ba76 PP |
66 | const struct bt_event *event, const struct bt_event *writer_event, |
67 | const struct bt_field *field, struct bt_field_type *type, | |
68 | const struct bt_field *copy_field); | |
b2f1f465 JD |
69 | static |
70 | int copy_find_clock_sequence_field(FILE *err, | |
40f4ba76 PP |
71 | const struct bt_event *event, const struct bt_event *writer_event, |
72 | const struct bt_field *field, struct bt_field_type *type, | |
73 | const struct bt_field *copy_field); | |
b2f1f465 | 74 | static |
40f4ba76 PP |
75 | int copy_find_clock_variant_field(FILE *err, const struct bt_event *event, |
76 | const struct bt_event *writer_event, const struct bt_field *field, | |
77 | struct bt_field_type *type, const struct bt_field *copy_field); | |
b2f1f465 | 78 | static |
40f4ba76 PP |
79 | int copy_find_clock_enum_field(FILE *err, const struct bt_event *event, |
80 | const struct bt_event *writer_event, const struct bt_field *field, | |
81 | struct bt_field_type *type, const struct bt_field *copy_field); | |
b2f1f465 JD |
82 | |
83 | static | |
50842bdc | 84 | int update_header_clock_int_field_type(FILE *err, struct bt_field_type *type, |
40f4ba76 | 85 | const struct bt_clock_class *writer_clock_class) |
b2f1f465 | 86 | { |
40f4ba76 | 87 | const struct bt_clock_class *clock = NULL; |
b2f1f465 JD |
88 | int ret; |
89 | ||
50842bdc | 90 | clock = bt_field_type_integer_get_mapped_clock_class(type); |
b2f1f465 JD |
91 | if (!clock) { |
92 | return 0; | |
93 | } | |
65300d60 | 94 | BT_OBJECT_PUT_REF_AND_RESET(clock); |
b2f1f465 | 95 | |
50842bdc | 96 | ret = bt_field_type_integer_set_size(type, 64); |
b2f1f465 | 97 | if (ret) { |
3e4f69d3 | 98 | BT_LOGE_STR("Failed to set integer size to 64."); |
b2f1f465 JD |
99 | goto end; |
100 | } | |
101 | ||
50842bdc | 102 | ret = bt_field_type_integer_set_mapped_clock_class(type, |
b2f1f465 JD |
103 | writer_clock_class); |
104 | if (ret) { | |
3e4f69d3 | 105 | BT_LOGE_STR("Failed to map integer to clock_class."); |
b2f1f465 JD |
106 | goto end; |
107 | } | |
108 | ||
109 | end: | |
110 | return ret; | |
111 | } | |
112 | ||
113 | static | |
50842bdc | 114 | int find_update_clock_fields(FILE *err, struct bt_field_type *type, |
40f4ba76 | 115 | const struct bt_clock_class *writer_clock_class) |
b2f1f465 JD |
116 | { |
117 | int ret; | |
118 | ||
50842bdc PP |
119 | switch (bt_field_type_get_type_id(type)) { |
120 | case BT_FIELD_TYPE_ID_INTEGER: | |
b2f1f465 JD |
121 | return update_header_clock_int_field_type(err, type, |
122 | writer_clock_class); | |
50842bdc | 123 | case BT_FIELD_TYPE_ID_STRUCT: |
b2f1f465 JD |
124 | return find_update_struct_clock_fields(err, type, |
125 | writer_clock_class); | |
50842bdc | 126 | case BT_FIELD_TYPE_ID_ARRAY: |
b2f1f465 JD |
127 | return find_update_array_clock_fields(err, type, |
128 | writer_clock_class); | |
50842bdc | 129 | case BT_FIELD_TYPE_ID_SEQUENCE: |
b2f1f465 JD |
130 | return find_update_sequence_clock_fields(err, type, |
131 | writer_clock_class); | |
50842bdc | 132 | case BT_FIELD_TYPE_ID_VARIANT: |
b2f1f465 JD |
133 | return find_update_variant_clock_fields(err, type, |
134 | writer_clock_class); | |
50842bdc | 135 | case BT_FIELD_TYPE_ID_ENUM: |
b2f1f465 JD |
136 | return find_update_enum_clock_fields(err, type, |
137 | writer_clock_class); | |
138 | break; | |
139 | default: | |
140 | break; | |
141 | } | |
142 | ||
143 | ret = 0; | |
144 | ||
145 | return ret; | |
146 | } | |
147 | ||
148 | static | |
50842bdc | 149 | int find_update_variant_clock_fields(FILE *err, struct bt_field_type *type, |
40f4ba76 | 150 | const struct bt_clock_class *writer_clock_class) |
b2f1f465 JD |
151 | { |
152 | int count, i, ret; | |
50842bdc | 153 | struct bt_field_type *entry_type = NULL; |
b2f1f465 | 154 | |
50842bdc | 155 | count = bt_field_type_variant_get_field_count(type); |
b2f1f465 | 156 | for (i = 0; i < count; i++) { |
b2f1f465 JD |
157 | const char *entry_name; |
158 | ||
50842bdc | 159 | ret = bt_field_type_variant_get_field_by_index(type, |
b2f1f465 JD |
160 | &entry_name, &entry_type, i); |
161 | if (ret) { | |
3e4f69d3 | 162 | BT_LOGE_STR("Failed to get variant field."); |
9ae49d3d | 163 | goto error; |
b2f1f465 | 164 | } |
3e4f69d3 | 165 | |
b2f1f465 JD |
166 | ret = find_update_clock_fields(err, entry_type, |
167 | writer_clock_class); | |
b2f1f465 | 168 | if (ret) { |
3e4f69d3 | 169 | BT_LOGE_STR("Failed to find clock fields."); |
9ae49d3d | 170 | goto error; |
b2f1f465 | 171 | } |
65300d60 | 172 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); |
b2f1f465 JD |
173 | } |
174 | ||
175 | ret = 0; | |
9ae49d3d | 176 | goto end; |
b2f1f465 | 177 | |
9ae49d3d JD |
178 | error: |
179 | ret = -1; | |
b2f1f465 | 180 | end: |
65300d60 | 181 | bt_object_put_ref(entry_type); |
b2f1f465 JD |
182 | return ret; |
183 | } | |
184 | ||
185 | static | |
50842bdc | 186 | int find_update_struct_clock_fields(FILE *err, struct bt_field_type *type, |
40f4ba76 | 187 | const struct bt_clock_class *writer_clock_class) |
b2f1f465 JD |
188 | { |
189 | int count, i, ret; | |
50842bdc | 190 | struct bt_field_type *entry_type = NULL; |
b2f1f465 | 191 | |
50842bdc | 192 | count = bt_field_type_structure_get_field_count(type); |
b2f1f465 | 193 | for (i = 0; i < count; i++) { |
b2f1f465 JD |
194 | const char *entry_name; |
195 | ||
50842bdc | 196 | ret = bt_field_type_structure_get_field_by_index(type, |
b2f1f465 JD |
197 | &entry_name, &entry_type, i); |
198 | if (ret) { | |
3e4f69d3 | 199 | BT_LOGE_STR("Failed to get struct field."); |
9ae49d3d | 200 | goto error; |
b2f1f465 | 201 | } |
3e4f69d3 | 202 | |
b2f1f465 JD |
203 | ret = find_update_clock_fields(err, entry_type, |
204 | writer_clock_class); | |
b2f1f465 | 205 | if (ret) { |
3e4f69d3 | 206 | BT_LOGE_STR("Failed to find clock fields."); |
9ae49d3d | 207 | goto error; |
b2f1f465 | 208 | } |
65300d60 | 209 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); |
b2f1f465 JD |
210 | } |
211 | ||
212 | ret = 0; | |
9ae49d3d | 213 | goto end; |
b2f1f465 | 214 | |
9ae49d3d | 215 | error: |
65300d60 | 216 | bt_object_put_ref(entry_type); |
b2f1f465 JD |
217 | end: |
218 | return ret; | |
219 | } | |
220 | ||
221 | static | |
50842bdc | 222 | int find_update_sequence_clock_fields(FILE *err, struct bt_field_type *type, |
40f4ba76 | 223 | const struct bt_clock_class *writer_clock_class) |
b2f1f465 JD |
224 | { |
225 | int ret; | |
50842bdc | 226 | struct bt_field_type *entry_type = NULL; |
b2f1f465 | 227 | |
50842bdc | 228 | entry_type = bt_field_type_sequence_get_element_type(type); |
f6ccaed9 | 229 | BT_ASSERT(entry_type); |
3e4f69d3 | 230 | |
b2f1f465 | 231 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
65300d60 | 232 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); |
b2f1f465 | 233 | if (ret) { |
3e4f69d3 | 234 | BT_LOGE_STR("Failed to find clock fields."); |
9ae49d3d | 235 | goto error; |
b2f1f465 JD |
236 | } |
237 | ||
238 | ret = 0; | |
9ae49d3d | 239 | goto end; |
b2f1f465 | 240 | |
9ae49d3d JD |
241 | error: |
242 | ret = -1; | |
b2f1f465 JD |
243 | end: |
244 | return ret; | |
245 | } | |
246 | ||
247 | static | |
50842bdc | 248 | int find_update_array_clock_fields(FILE *err, struct bt_field_type *type, |
40f4ba76 | 249 | const struct bt_clock_class *writer_clock_class) |
b2f1f465 | 250 | { |
3e4f69d3 | 251 | int ret = 0; |
50842bdc | 252 | struct bt_field_type *entry_type = NULL; |
b2f1f465 | 253 | |
50842bdc | 254 | entry_type = bt_field_type_array_get_element_type(type); |
f6ccaed9 | 255 | BT_ASSERT(entry_type); |
3e4f69d3 | 256 | |
b2f1f465 | 257 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
65300d60 | 258 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); |
b2f1f465 | 259 | if (ret) { |
3e4f69d3 | 260 | BT_LOGE_STR("Failed to find clock fields."); |
b2f1f465 JD |
261 | ret = -1; |
262 | goto end; | |
263 | } | |
264 | ||
b2f1f465 JD |
265 | end: |
266 | return ret; | |
267 | } | |
268 | ||
269 | static | |
50842bdc | 270 | int find_update_enum_clock_fields(FILE *err, struct bt_field_type *type, |
40f4ba76 | 271 | const struct bt_clock_class *writer_clock_class) |
b2f1f465 JD |
272 | { |
273 | int ret; | |
50842bdc | 274 | struct bt_field_type *entry_type = NULL; |
b2f1f465 | 275 | |
50842bdc | 276 | entry_type = bt_field_type_enumeration_get_container_type(type); |
f6ccaed9 | 277 | BT_ASSERT(entry_type); |
3e4f69d3 | 278 | |
b2f1f465 | 279 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
65300d60 | 280 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); |
b2f1f465 | 281 | if (ret) { |
3e4f69d3 | 282 | BT_LOGE_STR("Failed to find clock fields."); |
9ae49d3d | 283 | goto error; |
b2f1f465 JD |
284 | } |
285 | ||
286 | ret = 0; | |
9ae49d3d | 287 | goto end; |
b2f1f465 | 288 | |
9ae49d3d JD |
289 | error: |
290 | ret = -1; | |
b2f1f465 JD |
291 | end: |
292 | return ret; | |
293 | } | |
294 | ||
295 | BT_HIDDEN | |
50842bdc PP |
296 | struct bt_field_type *override_header_type(FILE *err, |
297 | struct bt_field_type *type, | |
40f4ba76 | 298 | const struct bt_trace *writer_trace) |
b2f1f465 | 299 | { |
50842bdc | 300 | struct bt_field_type *new_type = NULL; |
40f4ba76 | 301 | const struct bt_clock_class *writer_clock_class = NULL; |
b2f1f465 | 302 | int ret; |
b2f1f465 JD |
303 | |
304 | /* FIXME multi-clock? */ | |
50842bdc | 305 | writer_clock_class = bt_trace_get_clock_class_by_index(writer_trace, 0); |
f6ccaed9 | 306 | BT_ASSERT(writer_clock_class); |
b2f1f465 | 307 | |
50842bdc | 308 | new_type = bt_field_type_copy(type); |
b2f1f465 | 309 | if (!new_type) { |
3e4f69d3 | 310 | BT_LOGE_STR("Failed to copy field type."); |
9ae49d3d | 311 | goto error; |
b2f1f465 JD |
312 | } |
313 | ||
50842bdc | 314 | if (bt_field_type_get_type_id(new_type) != BT_FIELD_TYPE_ID_STRUCT) { |
3e4f69d3 | 315 | BT_LOGE("Expected header field type to be struct: type=%d", |
50842bdc | 316 | bt_field_type_get_type_id(new_type)); |
b2f1f465 JD |
317 | goto error; |
318 | } | |
319 | ||
320 | ret = find_update_struct_clock_fields(err, new_type, writer_clock_class); | |
321 | if (ret) { | |
3e4f69d3 | 322 | BT_LOGE_STR("Failed to find clock fields in struct."); |
b2f1f465 JD |
323 | goto error; |
324 | } | |
65300d60 | 325 | BT_OBJECT_PUT_REF_AND_RESET(writer_clock_class); |
b2f1f465 | 326 | |
9ae49d3d | 327 | goto end; |
b2f1f465 JD |
328 | |
329 | error: | |
65300d60 PP |
330 | bt_object_put_ref(writer_clock_class); |
331 | BT_OBJECT_PUT_REF_AND_RESET(new_type); | |
b2f1f465 JD |
332 | end: |
333 | return new_type; | |
334 | } | |
335 | ||
336 | static | |
40f4ba76 | 337 | int copy_float_field(FILE *err, const struct bt_field *field, |
50842bdc | 338 | struct bt_field_type *type, |
40f4ba76 | 339 | const struct bt_field *copy_field) |
b2f1f465 JD |
340 | { |
341 | double value; | |
342 | int ret; | |
343 | ||
50842bdc | 344 | ret = bt_field_floating_point_get_value(field, &value); |
b2f1f465 | 345 | if (ret) { |
3e4f69d3 JD |
346 | BT_LOGE_STR("Failed to get value."); |
347 | goto error; | |
b2f1f465 | 348 | } |
3e4f69d3 | 349 | |
50842bdc | 350 | ret = bt_field_floating_point_set_value(copy_field, value); |
b2f1f465 JD |
351 | if (ret) { |
352 | ret = -1; | |
3e4f69d3 | 353 | BT_LOGE_STR("Failed to set floating point value."); |
b2f1f465 JD |
354 | goto end; |
355 | } | |
356 | ||
357 | ret = 0; | |
3e4f69d3 | 358 | goto end; |
b2f1f465 | 359 | |
3e4f69d3 JD |
360 | error: |
361 | ret = -1; | |
b2f1f465 JD |
362 | end: |
363 | return ret; | |
364 | } | |
365 | ||
366 | static | |
40f4ba76 | 367 | int copy_string_field(FILE *err, const struct bt_field *field, |
50842bdc | 368 | struct bt_field_type *type, |
40f4ba76 | 369 | const struct bt_field *copy_field) |
b2f1f465 JD |
370 | { |
371 | const char *value; | |
372 | int ret; | |
373 | ||
50842bdc | 374 | value = bt_field_string_get_value(field); |
b2f1f465 | 375 | if (!value) { |
3e4f69d3 JD |
376 | BT_LOGE_STR("Failed to get value."); |
377 | goto error; | |
b2f1f465 | 378 | } |
3e4f69d3 | 379 | |
50842bdc | 380 | ret = bt_field_string_set_value(copy_field, value); |
b2f1f465 JD |
381 | if (ret) { |
382 | ret = -1; | |
3e4f69d3 | 383 | BT_LOGE_STR("Failed to set string value."); |
b2f1f465 JD |
384 | goto end; |
385 | } | |
386 | ||
387 | ret = 0; | |
3e4f69d3 | 388 | goto end; |
b2f1f465 | 389 | |
3e4f69d3 JD |
390 | error: |
391 | ret = -1; | |
b2f1f465 JD |
392 | end: |
393 | return ret; | |
394 | } | |
395 | ||
396 | BT_HIDDEN | |
40f4ba76 PP |
397 | int copy_override_field(FILE *err, const struct bt_event *event, |
398 | const struct bt_event *writer_event, const struct bt_field *field, | |
399 | const struct bt_field *copy_field) | |
b2f1f465 | 400 | { |
50842bdc | 401 | struct bt_field_type *type = NULL; |
3e4f69d3 | 402 | int ret = 0; |
b2f1f465 | 403 | |
50842bdc | 404 | type = bt_field_get_type(field); |
f6ccaed9 | 405 | BT_ASSERT(type); |
b2f1f465 | 406 | |
50842bdc PP |
407 | switch (bt_field_type_get_type_id(type)) { |
408 | case BT_FIELD_TYPE_ID_INTEGER: | |
b2f1f465 JD |
409 | ret = copy_find_clock_int_field(err, event, writer_event, |
410 | field, type, copy_field); | |
411 | break; | |
50842bdc | 412 | case BT_FIELD_TYPE_ID_STRUCT: |
b2f1f465 JD |
413 | ret = copy_find_clock_struct_field(err, event, writer_event, |
414 | field, type, copy_field); | |
415 | break; | |
50842bdc | 416 | case BT_FIELD_TYPE_ID_FLOAT: |
b2f1f465 JD |
417 | ret = copy_float_field(err, field, type, copy_field); |
418 | break; | |
50842bdc | 419 | case BT_FIELD_TYPE_ID_ENUM: |
b2f1f465 JD |
420 | ret = copy_find_clock_enum_field(err, event, writer_event, |
421 | field, type, copy_field); | |
422 | break; | |
50842bdc | 423 | case BT_FIELD_TYPE_ID_STRING: |
b2f1f465 JD |
424 | ret = copy_string_field(err, field, type, copy_field); |
425 | break; | |
50842bdc | 426 | case BT_FIELD_TYPE_ID_ARRAY: |
b2f1f465 JD |
427 | ret = copy_find_clock_array_field(err, event, writer_event, |
428 | field, type, copy_field); | |
429 | break; | |
50842bdc | 430 | case BT_FIELD_TYPE_ID_SEQUENCE: |
b2f1f465 JD |
431 | ret = copy_find_clock_sequence_field(err, event, writer_event, |
432 | field, type, copy_field); | |
433 | break; | |
50842bdc | 434 | case BT_FIELD_TYPE_ID_VARIANT: |
b2f1f465 JD |
435 | ret = copy_find_clock_variant_field(err, event, writer_event, |
436 | field, type, copy_field); | |
437 | break; | |
438 | /* No default, we want to catch missing field types. */ | |
50842bdc PP |
439 | case BT_FIELD_TYPE_ID_UNKNOWN: |
440 | case BT_FIELD_TYPE_ID_NR: | |
b2f1f465 JD |
441 | break; |
442 | } | |
443 | ||
65300d60 | 444 | BT_OBJECT_PUT_REF_AND_RESET(type); |
b2f1f465 | 445 | |
b2f1f465 JD |
446 | return ret; |
447 | } | |
448 | ||
449 | static | |
40f4ba76 PP |
450 | int copy_find_clock_enum_field(FILE *err, const struct bt_event *event, |
451 | const struct bt_event *writer_event, const struct bt_field *field, | |
452 | struct bt_field_type *type, const struct bt_field *copy_field) | |
b2f1f465 JD |
453 | { |
454 | int ret; | |
40f4ba76 | 455 | const struct bt_field *container = NULL, *copy_container = NULL; |
b2f1f465 | 456 | |
50842bdc | 457 | container = bt_field_enumeration_get_container(field); |
f6ccaed9 | 458 | BT_ASSERT(container); |
b2f1f465 | 459 | |
50842bdc | 460 | copy_container = bt_field_enumeration_get_container(copy_field); |
f6ccaed9 | 461 | BT_ASSERT(copy_container); |
b2f1f465 JD |
462 | |
463 | ret = copy_override_field(err, event, writer_event, container, | |
464 | copy_container); | |
465 | if (ret) { | |
466 | ret = -1; | |
3e4f69d3 | 467 | BT_LOGE_STR("Failed to override enum field."); |
9ae49d3d | 468 | goto error; |
b2f1f465 | 469 | } |
9ac68eb1 | 470 | |
9ae49d3d JD |
471 | ret = 0; |
472 | goto end; | |
b2f1f465 | 473 | |
9ae49d3d JD |
474 | error: |
475 | ret = -1; | |
476 | end: | |
65300d60 PP |
477 | bt_object_put_ref(copy_container); |
478 | bt_object_put_ref(container); | |
b2f1f465 JD |
479 | return ret; |
480 | } | |
481 | ||
482 | static | |
40f4ba76 PP |
483 | int copy_find_clock_variant_field(FILE *err, const struct bt_event *event, |
484 | const struct bt_event *writer_event, const struct bt_field *field, | |
485 | struct bt_field_type *type, const struct bt_field *copy_field) | |
b2f1f465 JD |
486 | { |
487 | int ret; | |
40f4ba76 PP |
488 | const struct bt_field *tag = NULL; |
489 | const struct bt_field *variant_field = NULL, *copy_variant_field = NULL; | |
b2f1f465 | 490 | |
50842bdc | 491 | tag = bt_field_variant_get_tag(field); |
f6ccaed9 | 492 | BT_ASSERT(tag); |
b2f1f465 | 493 | |
50842bdc | 494 | variant_field = bt_field_variant_get_field(field, tag); |
b2f1f465 | 495 | if (!variant_field) { |
3e4f69d3 | 496 | BT_LOGE_STR("Failed to get variant field."); |
9ae49d3d | 497 | goto error; |
b2f1f465 JD |
498 | } |
499 | ||
50842bdc | 500 | copy_variant_field = bt_field_variant_get_field(copy_field, tag); |
f6ccaed9 | 501 | BT_ASSERT(copy_variant_field); |
b2f1f465 JD |
502 | |
503 | ret = copy_override_field(err, event, writer_event, variant_field, | |
504 | copy_variant_field); | |
505 | if (ret) { | |
3e4f69d3 | 506 | BT_LOGE_STR("Failed to override variant field."); |
9ae49d3d | 507 | goto error; |
b2f1f465 JD |
508 | } |
509 | ||
510 | ret = 0; | |
9ae49d3d | 511 | goto end; |
b2f1f465 | 512 | |
9ae49d3d JD |
513 | error: |
514 | ret = -1; | |
515 | end: | |
65300d60 PP |
516 | bt_object_put_ref(copy_variant_field); |
517 | bt_object_put_ref(variant_field); | |
518 | bt_object_put_ref(tag); | |
b2f1f465 JD |
519 | return ret; |
520 | } | |
521 | ||
522 | static | |
523 | int copy_find_clock_sequence_field(FILE *err, | |
40f4ba76 PP |
524 | const struct bt_event *event, const struct bt_event *writer_event, |
525 | const struct bt_field *field, struct bt_field_type *type, | |
526 | const struct bt_field *copy_field) | |
b2f1f465 JD |
527 | { |
528 | int ret; | |
529 | uint64_t i, count; | |
40f4ba76 PP |
530 | const struct bt_field *length_field = NULL; |
531 | const struct bt_field *entry_field = NULL, *entry_copy = NULL; | |
b2f1f465 | 532 | |
50842bdc | 533 | length_field = bt_field_sequence_get_length(field); |
f6ccaed9 | 534 | BT_ASSERT(length_field); |
b2f1f465 | 535 | |
50842bdc | 536 | ret = bt_field_unsigned_integer_get_value(length_field, &count); |
b2f1f465 | 537 | if (ret) { |
3e4f69d3 | 538 | BT_LOGE("Failed to get value."); |
9ae49d3d | 539 | goto error; |
b2f1f465 JD |
540 | } |
541 | ||
50842bdc | 542 | ret = bt_field_sequence_set_length(copy_field, length_field); |
b2f1f465 | 543 | if (ret) { |
3e4f69d3 | 544 | BT_LOGE_STR("Failed to set sequence length."); |
9ae49d3d | 545 | goto error; |
b2f1f465 | 546 | } |
65300d60 | 547 | BT_OBJECT_PUT_REF_AND_RESET(length_field); |
b2f1f465 JD |
548 | |
549 | for (i = 0; i < count; i++) { | |
50842bdc | 550 | entry_field = bt_field_sequence_get_field(field, i); |
b2f1f465 | 551 | if (!entry_field) { |
3e4f69d3 | 552 | BT_LOGE_STR("Failed to get sequence field."); |
9ae49d3d | 553 | goto error; |
b2f1f465 JD |
554 | } |
555 | ||
50842bdc | 556 | entry_copy = bt_field_sequence_get_field(copy_field, i); |
f6ccaed9 | 557 | BT_ASSERT(entry_copy); |
b2f1f465 JD |
558 | |
559 | ret = copy_override_field(err, event, writer_event, entry_field, | |
560 | entry_copy); | |
b2f1f465 | 561 | if (ret) { |
3e4f69d3 | 562 | BT_LOGE_STR("Faield to override field in sequence."); |
9ae49d3d | 563 | goto error; |
b2f1f465 | 564 | } |
65300d60 PP |
565 | BT_OBJECT_PUT_REF_AND_RESET(entry_field); |
566 | BT_OBJECT_PUT_REF_AND_RESET(entry_copy); | |
b2f1f465 JD |
567 | } |
568 | ||
b2f1f465 | 569 | ret = 0; |
9ae49d3d | 570 | goto end; |
b2f1f465 | 571 | |
9ae49d3d | 572 | error: |
65300d60 PP |
573 | bt_object_put_ref(length_field); |
574 | bt_object_put_ref(entry_field); | |
575 | bt_object_put_ref(entry_copy); | |
9ae49d3d | 576 | ret = -1; |
b2f1f465 JD |
577 | end: |
578 | return ret; | |
579 | } | |
580 | ||
581 | static | |
582 | int copy_find_clock_array_field(FILE *err, | |
40f4ba76 PP |
583 | const struct bt_event *event, const struct bt_event *writer_event, |
584 | const struct bt_field *field, struct bt_field_type *type, | |
585 | const struct bt_field *copy_field) | |
b2f1f465 JD |
586 | { |
587 | int ret, count, i; | |
40f4ba76 | 588 | const struct bt_field *entry_field = NULL, *entry_copy = NULL; |
b2f1f465 | 589 | |
50842bdc | 590 | count = bt_field_type_array_get_length(type); |
b2f1f465 | 591 | for (i = 0; i < count; i++) { |
50842bdc | 592 | entry_field = bt_field_array_get_field(field, i); |
b2f1f465 | 593 | if (!entry_field) { |
f01de2ae | 594 | ret = -1; |
3e4f69d3 | 595 | BT_LOGE_STR("Failed to get array field."); |
9ae49d3d | 596 | goto error; |
b2f1f465 JD |
597 | } |
598 | ||
50842bdc | 599 | entry_copy = bt_field_array_get_field(copy_field, i); |
f6ccaed9 | 600 | BT_ASSERT(entry_copy); |
b2f1f465 JD |
601 | |
602 | ret = copy_override_field(err, event, writer_event, entry_field, | |
603 | entry_copy); | |
b2f1f465 JD |
604 | if (ret) { |
605 | ret = -1; | |
3e4f69d3 | 606 | BT_LOGE_STR("Failed to override field in array."); |
9ae49d3d | 607 | goto error; |
b2f1f465 | 608 | } |
65300d60 PP |
609 | BT_OBJECT_PUT_REF_AND_RESET(entry_field); |
610 | BT_OBJECT_PUT_REF_AND_RESET(entry_copy); | |
b2f1f465 JD |
611 | } |
612 | ||
b2f1f465 | 613 | ret = 0; |
9ae49d3d JD |
614 | goto end; |
615 | ||
616 | error: | |
65300d60 PP |
617 | bt_object_put_ref(entry_field); |
618 | bt_object_put_ref(entry_copy); | |
b2f1f465 JD |
619 | |
620 | end: | |
621 | return ret; | |
622 | } | |
623 | ||
624 | static | |
625 | int copy_find_clock_struct_field(FILE *err, | |
40f4ba76 PP |
626 | const struct bt_event *event, const struct bt_event *writer_event, |
627 | const struct bt_field *field, struct bt_field_type *type, | |
628 | const struct bt_field *copy_field) | |
b2f1f465 JD |
629 | { |
630 | int count, i, ret; | |
50842bdc | 631 | struct bt_field_type *entry_type = NULL; |
40f4ba76 | 632 | const struct bt_field *entry_field = NULL, *entry_copy = NULL; |
b2f1f465 | 633 | |
50842bdc | 634 | count = bt_field_type_structure_get_field_count(type); |
b2f1f465 | 635 | for (i = 0; i < count; i++) { |
b2f1f465 | 636 | const char *entry_name; |
b2f1f465 | 637 | |
50842bdc | 638 | entry_field = bt_field_structure_get_field_by_index(field, i); |
b2f1f465 | 639 | if (!entry_field) { |
3e4f69d3 | 640 | BT_LOGE_STR("Failed to get struct field."); |
9ae49d3d | 641 | goto error; |
b2f1f465 JD |
642 | } |
643 | ||
50842bdc | 644 | ret = bt_field_type_structure_get_field_by_index(type, &entry_name, |
b2f1f465 JD |
645 | &entry_type, i); |
646 | if (ret) { | |
3e4f69d3 | 647 | BT_LOGE_STR("Failed to get struct field."); |
9ae49d3d | 648 | goto error; |
b2f1f465 JD |
649 | } |
650 | ||
50842bdc | 651 | entry_copy = bt_field_structure_get_field_by_index(copy_field, i); |
f6ccaed9 | 652 | BT_ASSERT(entry_copy); |
b2f1f465 JD |
653 | |
654 | ret = copy_override_field(err, event, writer_event, entry_field, | |
655 | entry_copy); | |
b2f1f465 | 656 | if (ret) { |
3e4f69d3 | 657 | BT_LOGE_STR("Failed to override field in struct."); |
9ae49d3d | 658 | goto error; |
b2f1f465 | 659 | } |
65300d60 PP |
660 | BT_OBJECT_PUT_REF_AND_RESET(entry_copy); |
661 | BT_OBJECT_PUT_REF_AND_RESET(entry_field); | |
662 | BT_OBJECT_PUT_REF_AND_RESET(entry_type); | |
b2f1f465 JD |
663 | } |
664 | ||
665 | ret = 0; | |
9ae49d3d | 666 | goto end; |
b2f1f465 | 667 | |
9ae49d3d | 668 | error: |
65300d60 PP |
669 | bt_object_put_ref(entry_type); |
670 | bt_object_put_ref(entry_field); | |
671 | bt_object_put_ref(entry_copy); | |
9ae49d3d | 672 | ret = -1; |
b2f1f465 JD |
673 | end: |
674 | return ret; | |
675 | } | |
676 | ||
677 | static | |
40f4ba76 PP |
678 | int set_int_value(FILE *err, const struct bt_field *field, |
679 | const struct bt_field *copy_field, | |
50842bdc | 680 | struct bt_field_type *type) |
b2f1f465 JD |
681 | { |
682 | uint64_t uvalue; | |
683 | int64_t value; | |
684 | int ret; | |
685 | ||
50842bdc PP |
686 | if (bt_field_type_integer_is_signed(type)) { |
687 | ret = bt_field_signed_integer_get_value(field, &value); | |
b2f1f465 | 688 | if (ret) { |
3e4f69d3 JD |
689 | BT_LOGE("Failed to get value."); |
690 | goto error; | |
b2f1f465 | 691 | } |
3e4f69d3 | 692 | |
50842bdc | 693 | ret = bt_field_signed_integer_set_value(copy_field, value); |
b2f1f465 JD |
694 | if (ret) { |
695 | ret = -1; | |
3e4f69d3 | 696 | BT_LOGE_STR("Failed to set signed integer value."); |
b2f1f465 JD |
697 | goto end; |
698 | } | |
699 | } else { | |
50842bdc | 700 | ret = bt_field_unsigned_integer_get_value(field, |
b2f1f465 JD |
701 | &uvalue); |
702 | if (ret) { | |
3e4f69d3 JD |
703 | BT_LOGE("Failed to get value."); |
704 | goto error; | |
b2f1f465 | 705 | } |
3e4f69d3 | 706 | |
50842bdc | 707 | ret = bt_field_unsigned_integer_set_value(copy_field, uvalue); |
b2f1f465 JD |
708 | if (ret) { |
709 | ret = -1; | |
3e4f69d3 | 710 | BT_LOGE_STR("Failed to set unsigned integer value."); |
b2f1f465 JD |
711 | goto end; |
712 | } | |
713 | } | |
714 | ret = 0; | |
3e4f69d3 | 715 | goto end; |
b2f1f465 | 716 | |
3e4f69d3 JD |
717 | error: |
718 | ret = -1; | |
b2f1f465 JD |
719 | end: |
720 | return ret; | |
721 | } | |
722 | ||
40f4ba76 PP |
723 | const struct bt_clock_class *stream_class_get_clock_class(FILE *err, |
724 | const struct bt_stream_class *stream_class) | |
b2f1f465 | 725 | { |
40f4ba76 PP |
726 | const struct bt_trace *trace = NULL; |
727 | const struct bt_clock_class *clock_class = NULL; | |
b2f1f465 | 728 | |
50842bdc | 729 | trace = bt_stream_class_get_trace(stream_class); |
f6ccaed9 | 730 | BT_ASSERT(trace); |
b2f1f465 JD |
731 | |
732 | /* FIXME multi-clock? */ | |
50842bdc | 733 | clock_class = bt_trace_get_clock_class_by_index(trace, 0); |
b2f1f465 | 734 | |
65300d60 | 735 | bt_object_put_ref(trace); |
3e4f69d3 | 736 | |
b2f1f465 JD |
737 | return clock_class; |
738 | } | |
739 | ||
40f4ba76 | 740 | const struct bt_clock_class *event_get_clock_class(FILE *err, const struct bt_event *event) |
b2f1f465 | 741 | { |
40f4ba76 PP |
742 | const struct bt_event_class *event_class = NULL; |
743 | const struct bt_stream_class *stream_class = NULL; | |
744 | const struct bt_clock_class *clock_class = NULL; | |
b2f1f465 | 745 | |
50842bdc | 746 | event_class = bt_event_get_class(event); |
f6ccaed9 | 747 | BT_ASSERT(event_class); |
b2f1f465 | 748 | |
50842bdc | 749 | stream_class = bt_event_class_get_stream_class(event_class); |
65300d60 | 750 | BT_OBJECT_PUT_REF_AND_RESET(event_class); |
f6ccaed9 | 751 | BT_ASSERT(stream_class); |
b2f1f465 JD |
752 | |
753 | clock_class = stream_class_get_clock_class(err, stream_class); | |
65300d60 | 754 | bt_object_put_ref(stream_class); |
b2f1f465 | 755 | |
b2f1f465 JD |
756 | return clock_class; |
757 | } | |
758 | ||
759 | static | |
760 | int copy_find_clock_int_field(FILE *err, | |
40f4ba76 PP |
761 | const struct bt_event *event, const struct bt_event *writer_event, |
762 | const struct bt_field *field, struct bt_field_type *type, | |
763 | const struct bt_field *copy_field) | |
b2f1f465 | 764 | { |
40f4ba76 | 765 | const struct bt_clock_class *clock_class = NULL, *writer_clock_class = NULL; |
50842bdc | 766 | struct bt_clock_value *clock_value = NULL, *writer_clock_value = NULL; |
b2f1f465 JD |
767 | uint64_t value; |
768 | int ret; | |
769 | ||
50842bdc | 770 | clock_class = bt_field_type_integer_get_mapped_clock_class(type); |
b2f1f465 JD |
771 | if (!clock_class) { |
772 | return set_int_value(err, field, copy_field, type); | |
773 | } | |
774 | ||
50842bdc | 775 | clock_value = bt_event_get_clock_value(event, clock_class); |
65300d60 | 776 | BT_OBJECT_PUT_REF_AND_RESET(clock_class); |
f6ccaed9 | 777 | BT_ASSERT(clock_value); |
b2f1f465 | 778 | |
50842bdc | 779 | ret = bt_clock_value_get_value(clock_value, &value); |
65300d60 | 780 | BT_OBJECT_PUT_REF_AND_RESET(clock_value); |
b2f1f465 | 781 | if (ret) { |
3e4f69d3 | 782 | BT_LOGE("Failed to get clock value."); |
9ae49d3d | 783 | goto error; |
b2f1f465 JD |
784 | } |
785 | ||
50842bdc | 786 | ret = bt_field_unsigned_integer_set_value(copy_field, value); |
b2f1f465 | 787 | if (ret) { |
3e4f69d3 | 788 | BT_LOGE_STR("Failed to set unsigned integer value."); |
9ae49d3d | 789 | goto error; |
b2f1f465 JD |
790 | } |
791 | ||
792 | writer_clock_class = event_get_clock_class(err, writer_event); | |
f6ccaed9 | 793 | BT_ASSERT(writer_clock_class); |
b2f1f465 | 794 | |
50842bdc | 795 | writer_clock_value = bt_clock_value_create(writer_clock_class, value); |
65300d60 | 796 | BT_OBJECT_PUT_REF_AND_RESET(writer_clock_class); |
b2f1f465 | 797 | if (!writer_clock_value) { |
3e4f69d3 | 798 | BT_LOGE_STR("Failed to create clock value."); |
9ae49d3d | 799 | goto error; |
b2f1f465 JD |
800 | } |
801 | ||
50842bdc | 802 | ret = bt_event_set_clock_value(writer_event, writer_clock_value); |
65300d60 | 803 | BT_OBJECT_PUT_REF_AND_RESET(writer_clock_value); |
b2f1f465 | 804 | if (ret) { |
3e4f69d3 | 805 | BT_LOGE_STR("Failed to set clock value."); |
9ae49d3d | 806 | goto error; |
b2f1f465 JD |
807 | } |
808 | ||
809 | ret = 0; | |
9ae49d3d | 810 | goto end; |
b2f1f465 | 811 | |
9ae49d3d JD |
812 | error: |
813 | ret = -1; | |
b2f1f465 JD |
814 | end: |
815 | return ret; | |
816 | } | |
817 |