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 | ||
29 | #include <babeltrace/ctf-ir/event.h> | |
30 | #include <babeltrace/ctf-ir/packet.h> | |
31 | #include <babeltrace/ctf-ir/event-class.h> | |
32 | #include <babeltrace/ctf-ir/stream.h> | |
33 | #include <babeltrace/ctf-ir/stream-class.h> | |
34 | #include <babeltrace/ctf-ir/clock-class.h> | |
35 | #include <babeltrace/ctf-ir/fields.h> | |
36 | #include <babeltrace/ctf-writer/stream.h> | |
37 | #include <babeltrace/ctf-ir/field-types.h> | |
38 | #include <assert.h> | |
39 | #include <stdio.h> | |
40 | ||
41 | #include "clock-fields.h" | |
42 | ||
43 | static | |
44 | int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
45 | struct bt_ctf_clock_class *writer_clock_class); | |
46 | static | |
47 | int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
48 | struct bt_ctf_clock_class *writer_clock_class); | |
49 | static | |
50 | int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
51 | struct bt_ctf_clock_class *writer_clock_class); | |
52 | static | |
53 | int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
54 | struct bt_ctf_clock_class *writer_clock_class); | |
55 | static | |
56 | int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
57 | struct bt_ctf_clock_class *writer_clock_class); | |
58 | ||
59 | static | |
60 | int copy_find_clock_int_field(FILE *err, | |
61 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
62 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
63 | struct bt_ctf_field *copy_field); | |
64 | static | |
65 | int copy_find_clock_struct_field(FILE *err, | |
66 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
67 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
68 | struct bt_ctf_field *copy_field); | |
69 | static | |
70 | int copy_find_clock_array_field(FILE *err, | |
71 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
72 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
73 | struct bt_ctf_field *copy_field); | |
74 | static | |
75 | int copy_find_clock_sequence_field(FILE *err, | |
76 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
77 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
78 | struct bt_ctf_field *copy_field); | |
79 | static | |
80 | int copy_find_clock_variant_field(FILE *err, struct bt_ctf_event *event, | |
81 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, | |
82 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field); | |
83 | static | |
84 | int copy_find_clock_enum_field(FILE *err, struct bt_ctf_event *event, | |
85 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, | |
86 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field); | |
87 | ||
88 | static | |
89 | int update_header_clock_int_field_type(FILE *err, struct bt_ctf_field_type *type, | |
90 | struct bt_ctf_clock_class *writer_clock_class) | |
91 | { | |
92 | struct bt_ctf_clock_class *clock; | |
93 | int ret; | |
94 | ||
95 | clock = bt_ctf_field_type_integer_get_mapped_clock_class(type); | |
96 | if (!clock) { | |
97 | return 0; | |
98 | } | |
99 | bt_put(clock); | |
100 | ||
101 | ret = bt_ctf_field_type_integer_set_size(type, 64); | |
102 | if (ret) { | |
103 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
104 | __LINE__); | |
105 | goto end; | |
106 | } | |
107 | ||
108 | ret = bt_ctf_field_type_integer_set_mapped_clock_class(type, | |
109 | writer_clock_class); | |
110 | if (ret) { | |
111 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
112 | __LINE__); | |
113 | goto end; | |
114 | } | |
115 | ||
116 | end: | |
117 | return ret; | |
118 | } | |
119 | ||
120 | static | |
121 | int find_update_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
122 | struct bt_ctf_clock_class *writer_clock_class) | |
123 | { | |
124 | int ret; | |
125 | ||
126 | switch (bt_ctf_field_type_get_type_id(type)) { | |
127 | case BT_CTF_TYPE_ID_INTEGER: | |
128 | return update_header_clock_int_field_type(err, type, | |
129 | writer_clock_class); | |
130 | case BT_CTF_TYPE_ID_STRUCT: | |
131 | return find_update_struct_clock_fields(err, type, | |
132 | writer_clock_class); | |
133 | case BT_CTF_TYPE_ID_ARRAY: | |
134 | return find_update_array_clock_fields(err, type, | |
135 | writer_clock_class); | |
136 | case BT_CTF_TYPE_ID_SEQUENCE: | |
137 | return find_update_sequence_clock_fields(err, type, | |
138 | writer_clock_class); | |
139 | case BT_CTF_TYPE_ID_UNTAGGED_VARIANT: | |
140 | case BT_CTF_TYPE_ID_VARIANT: | |
141 | return find_update_variant_clock_fields(err, type, | |
142 | writer_clock_class); | |
143 | case BT_CTF_TYPE_ID_ENUM: | |
144 | return find_update_enum_clock_fields(err, type, | |
145 | writer_clock_class); | |
146 | break; | |
147 | default: | |
148 | break; | |
149 | } | |
150 | ||
151 | ret = 0; | |
152 | ||
153 | return ret; | |
154 | } | |
155 | ||
156 | static | |
157 | int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
158 | struct bt_ctf_clock_class *writer_clock_class) | |
159 | { | |
160 | int count, i, ret; | |
161 | ||
162 | count = bt_ctf_field_type_variant_get_field_count(type); | |
163 | for (i = 0; i < count; i++) { | |
164 | struct bt_ctf_field_type *entry_type; | |
165 | const char *entry_name; | |
166 | ||
167 | ret = bt_ctf_field_type_variant_get_field(type, | |
168 | &entry_name, &entry_type, i); | |
169 | if (ret) { | |
170 | fprintf(err, "[error] %s in %s:%d\n", | |
171 | __func__, __FILE__, __LINE__); | |
172 | ret = -1; | |
173 | goto end; | |
174 | } | |
175 | ret = find_update_clock_fields(err, entry_type, | |
176 | writer_clock_class); | |
177 | bt_put(entry_type); | |
178 | if (ret) { | |
179 | fprintf(err, "[error] %s in %s:%d\n", | |
180 | __func__, __FILE__, __LINE__); | |
181 | ret = -1; | |
182 | goto end; | |
183 | } | |
184 | } | |
185 | ||
186 | ret = 0; | |
187 | ||
188 | end: | |
189 | return ret; | |
190 | } | |
191 | ||
192 | static | |
193 | int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
194 | struct bt_ctf_clock_class *writer_clock_class) | |
195 | { | |
196 | int count, i, ret; | |
197 | ||
198 | count = bt_ctf_field_type_structure_get_field_count(type); | |
199 | for (i = 0; i < count; i++) { | |
200 | struct bt_ctf_field_type *entry_type; | |
201 | const char *entry_name; | |
202 | ||
203 | ret = bt_ctf_field_type_structure_get_field(type, | |
204 | &entry_name, &entry_type, i); | |
205 | if (ret) { | |
206 | fprintf(err, "[error] %s in %s:%d\n", | |
207 | __func__, __FILE__, __LINE__); | |
208 | ret = -1; | |
209 | goto end; | |
210 | } | |
211 | ret = find_update_clock_fields(err, entry_type, | |
212 | writer_clock_class); | |
213 | bt_put(entry_type); | |
214 | if (ret) { | |
215 | fprintf(err, "[error] %s in %s:%d\n", | |
216 | __func__, __FILE__, __LINE__); | |
217 | ret = -1; | |
218 | goto end; | |
219 | } | |
220 | } | |
221 | ||
222 | ret = 0; | |
223 | ||
224 | end: | |
225 | return ret; | |
226 | } | |
227 | ||
228 | static | |
229 | int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
230 | struct bt_ctf_clock_class *writer_clock_class) | |
231 | { | |
232 | int ret; | |
233 | struct bt_ctf_field_type *entry_type; | |
234 | ||
235 | entry_type = bt_ctf_field_type_sequence_get_element_type(type); | |
236 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); | |
237 | bt_put(entry_type); | |
238 | if (ret) { | |
239 | fprintf(err, "[error] %s in %s:%d\n", | |
240 | __func__, __FILE__, __LINE__); | |
241 | ret = -1; | |
242 | goto end; | |
243 | } | |
244 | ||
245 | ret = 0; | |
246 | ||
247 | end: | |
248 | return ret; | |
249 | } | |
250 | ||
251 | static | |
252 | int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
253 | struct bt_ctf_clock_class *writer_clock_class) | |
254 | { | |
255 | int ret; | |
256 | struct bt_ctf_field_type *entry_type; | |
257 | ||
258 | entry_type = bt_ctf_field_type_array_get_element_type(type); | |
259 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); | |
260 | bt_put(entry_type); | |
261 | if (ret) { | |
262 | fprintf(err, "[error] %s in %s:%d\n", | |
263 | __func__, __FILE__, __LINE__); | |
264 | ret = -1; | |
265 | goto end; | |
266 | } | |
267 | ||
268 | ret = 0; | |
269 | ||
270 | end: | |
271 | return ret; | |
272 | } | |
273 | ||
274 | static | |
275 | int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
276 | struct bt_ctf_clock_class *writer_clock_class) | |
277 | { | |
278 | int ret; | |
279 | struct bt_ctf_field_type *entry_type; | |
280 | ||
281 | entry_type = bt_ctf_field_type_enumeration_get_container_type(type); | |
282 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); | |
283 | bt_put(entry_type); | |
284 | if (ret) { | |
285 | fprintf(err, "[error] %s in %s:%d\n", | |
286 | __func__, __FILE__, __LINE__); | |
287 | ret = -1; | |
288 | goto end; | |
289 | } | |
290 | ||
291 | ret = 0; | |
292 | ||
293 | end: | |
294 | return ret; | |
295 | } | |
296 | ||
297 | BT_HIDDEN | |
298 | struct bt_ctf_field_type *override_header_type(FILE *err, | |
299 | struct bt_ctf_field_type *type, | |
300 | struct bt_ctf_trace *writer_trace) | |
301 | { | |
302 | struct bt_ctf_field_type *new_type = NULL; | |
303 | int ret; | |
304 | struct bt_ctf_clock_class *writer_clock_class; | |
305 | ||
306 | /* FIXME multi-clock? */ | |
307 | writer_clock_class = bt_ctf_trace_get_clock_class(writer_trace, 0); | |
308 | if (!writer_clock_class) { | |
309 | fprintf(err, "[error] %s in %s:%d\n", | |
310 | __func__, __FILE__, __LINE__); | |
311 | goto end; | |
312 | } | |
313 | ||
314 | new_type = bt_ctf_field_type_copy(type); | |
315 | if (!new_type) { | |
316 | fprintf(err, "[error] %s in %s:%d\n", | |
317 | __func__, __FILE__, __LINE__); | |
318 | goto end_put; | |
319 | } | |
320 | ||
321 | if (bt_ctf_field_type_get_type_id(new_type) != BT_CTF_TYPE_ID_STRUCT) { | |
322 | fprintf(err, "[error] Unexpected header field type\n"); | |
323 | goto error; | |
324 | } | |
325 | ||
326 | ret = find_update_struct_clock_fields(err, new_type, writer_clock_class); | |
327 | if (ret) { | |
328 | fprintf(err, "[error] %s in %s:%d\n", | |
329 | __func__, __FILE__, __LINE__); | |
330 | goto error; | |
331 | } | |
332 | ||
333 | goto end_put; | |
334 | ||
335 | error: | |
336 | BT_PUT(new_type); | |
337 | end_put: | |
338 | bt_put(writer_clock_class); | |
339 | end: | |
340 | return new_type; | |
341 | } | |
342 | ||
343 | static | |
344 | int copy_float_field(FILE *err, struct bt_ctf_field *field, | |
345 | struct bt_ctf_field_type *type, | |
346 | struct bt_ctf_field *copy_field) | |
347 | { | |
348 | double value; | |
349 | int ret; | |
350 | ||
351 | ret = bt_ctf_field_floating_point_get_value(field, &value); | |
352 | if (ret) { | |
353 | ret = -1; | |
354 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
355 | __FILE__, __LINE__); | |
356 | goto end; | |
357 | } | |
358 | ret = bt_ctf_field_floating_point_set_value(copy_field, value); | |
359 | if (ret) { | |
360 | ret = -1; | |
361 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
362 | __FILE__, __LINE__); | |
363 | goto end; | |
364 | } | |
365 | ||
366 | ret = 0; | |
367 | ||
368 | end: | |
369 | return ret; | |
370 | } | |
371 | ||
372 | static | |
373 | int copy_string_field(FILE *err, struct bt_ctf_field *field, | |
374 | struct bt_ctf_field_type *type, | |
375 | struct bt_ctf_field *copy_field) | |
376 | { | |
377 | const char *value; | |
378 | int ret; | |
379 | ||
380 | value = bt_ctf_field_string_get_value(field); | |
381 | if (!value) { | |
382 | ret = -1; | |
383 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
384 | __FILE__, __LINE__); | |
385 | goto end; | |
386 | } | |
387 | ret = bt_ctf_field_string_set_value(copy_field, value); | |
388 | if (ret) { | |
389 | ret = -1; | |
390 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
391 | __FILE__, __LINE__); | |
392 | goto end; | |
393 | } | |
394 | ||
395 | ret = 0; | |
396 | ||
397 | end: | |
398 | return ret; | |
399 | } | |
400 | ||
401 | BT_HIDDEN | |
402 | int copy_override_field(FILE *err, struct bt_ctf_event *event, | |
403 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, | |
404 | struct bt_ctf_field *copy_field) | |
405 | { | |
406 | struct bt_ctf_field_type *type; | |
407 | int ret; | |
408 | ||
409 | type = bt_ctf_field_get_type(field); | |
410 | if (!type) { | |
411 | ret = -1; | |
412 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
413 | __LINE__); | |
414 | goto end; | |
415 | } | |
416 | ||
417 | switch (bt_ctf_field_type_get_type_id(type)) { | |
418 | case BT_CTF_TYPE_ID_INTEGER: | |
419 | ret = copy_find_clock_int_field(err, event, writer_event, | |
420 | field, type, copy_field); | |
421 | break; | |
422 | case BT_CTF_TYPE_ID_STRUCT: | |
423 | ret = copy_find_clock_struct_field(err, event, writer_event, | |
424 | field, type, copy_field); | |
425 | break; | |
426 | case BT_CTF_TYPE_ID_FLOAT: | |
427 | ret = copy_float_field(err, field, type, copy_field); | |
428 | break; | |
429 | case BT_CTF_TYPE_ID_ENUM: | |
430 | ret = copy_find_clock_enum_field(err, event, writer_event, | |
431 | field, type, copy_field); | |
432 | break; | |
433 | case BT_CTF_TYPE_ID_STRING: | |
434 | ret = copy_string_field(err, field, type, copy_field); | |
435 | break; | |
436 | case BT_CTF_TYPE_ID_ARRAY: | |
437 | ret = copy_find_clock_array_field(err, event, writer_event, | |
438 | field, type, copy_field); | |
439 | break; | |
440 | case BT_CTF_TYPE_ID_SEQUENCE: | |
441 | ret = copy_find_clock_sequence_field(err, event, writer_event, | |
442 | field, type, copy_field); | |
443 | break; | |
444 | case BT_CTF_TYPE_ID_UNTAGGED_VARIANT: | |
445 | case BT_CTF_TYPE_ID_VARIANT: | |
446 | ret = copy_find_clock_variant_field(err, event, writer_event, | |
447 | field, type, copy_field); | |
448 | break; | |
449 | /* No default, we want to catch missing field types. */ | |
450 | case BT_CTF_TYPE_ID_UNKNOWN: | |
451 | case BT_CTF_NR_TYPE_IDS: | |
452 | break; | |
453 | } | |
454 | ||
455 | ret = 0; | |
456 | bt_put(type); | |
457 | ||
458 | end: | |
459 | return ret; | |
460 | } | |
461 | ||
462 | static | |
463 | int copy_find_clock_enum_field(FILE *err, struct bt_ctf_event *event, | |
464 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, | |
465 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field) | |
466 | { | |
467 | int ret; | |
468 | struct bt_ctf_field *container, *copy_container; | |
469 | ||
470 | container = bt_ctf_field_enumeration_get_container(field); | |
471 | if (!container) { | |
472 | ret = -1; | |
473 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
474 | __LINE__); | |
475 | goto end; | |
476 | } | |
477 | ||
478 | copy_container = bt_ctf_field_enumeration_get_container(copy_field); | |
479 | if (!copy_container) { | |
480 | ret = -1; | |
481 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
482 | __LINE__); | |
483 | goto end_put_container; | |
484 | } | |
485 | ||
486 | ret = copy_override_field(err, event, writer_event, container, | |
487 | copy_container); | |
488 | if (ret) { | |
489 | ret = -1; | |
490 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
491 | __LINE__); | |
492 | goto end_put_copy_container; | |
493 | } | |
494 | ||
495 | end_put_copy_container: | |
496 | bt_put(copy_container); | |
497 | end_put_container: | |
498 | bt_put(container); | |
499 | end: | |
500 | return ret; | |
501 | } | |
502 | ||
503 | static | |
504 | int copy_find_clock_variant_field(FILE *err, struct bt_ctf_event *event, | |
505 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, | |
506 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field) | |
507 | { | |
508 | int ret; | |
509 | struct bt_ctf_field *tag; | |
510 | struct bt_ctf_field *variant_field, *copy_variant_field; | |
511 | ||
512 | tag = bt_ctf_field_variant_get_tag(field); | |
513 | if (!tag) { | |
514 | ret = -1; | |
515 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
516 | __LINE__); | |
517 | goto end; | |
518 | } | |
519 | ||
520 | variant_field = bt_ctf_field_variant_get_field(field, tag); | |
521 | if (!variant_field) { | |
522 | ret = -1; | |
523 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
524 | __LINE__); | |
525 | goto end_put_tag; | |
526 | } | |
527 | ||
528 | copy_variant_field = bt_ctf_field_variant_get_field(copy_field, tag); | |
529 | if (!copy_variant_field) { | |
530 | bt_put(variant_field); | |
531 | ret = -1; | |
532 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
533 | __LINE__); | |
534 | goto end_put_variant_field; | |
535 | } | |
536 | ||
537 | ret = copy_override_field(err, event, writer_event, variant_field, | |
538 | copy_variant_field); | |
539 | if (ret) { | |
540 | ret = -1; | |
541 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
542 | __LINE__); | |
543 | goto end_put_copy_variand_field; | |
544 | } | |
545 | ||
546 | ret = 0; | |
547 | ||
548 | end_put_copy_variand_field: | |
549 | bt_put(copy_variant_field); | |
550 | end_put_variant_field: | |
551 | bt_put(variant_field); | |
552 | end_put_tag: | |
553 | bt_put(tag); | |
554 | end: | |
555 | return ret; | |
556 | } | |
557 | ||
558 | static | |
559 | int copy_find_clock_sequence_field(FILE *err, | |
560 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
561 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
562 | struct bt_ctf_field *copy_field) | |
563 | { | |
564 | int ret; | |
565 | uint64_t i, count; | |
566 | struct bt_ctf_field_type *entry_type; | |
567 | struct bt_ctf_field *length_field; | |
568 | ||
569 | entry_type = bt_ctf_field_type_sequence_get_element_type(type); | |
570 | if (!entry_type) { | |
571 | ret = -1; | |
572 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
573 | __LINE__); | |
574 | goto end; | |
575 | } | |
576 | ||
577 | length_field = bt_ctf_field_sequence_get_length(field); | |
578 | if (!length_field) { | |
579 | ret = -1; | |
580 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
581 | __LINE__); | |
582 | goto end; | |
583 | } | |
584 | ||
585 | ret = bt_ctf_field_unsigned_integer_get_value(length_field, &count); | |
586 | if (ret) { | |
587 | bt_put(length_field); | |
588 | ret = -1; | |
589 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
590 | __LINE__); | |
591 | goto end; | |
592 | } | |
593 | ||
594 | ret = bt_ctf_field_sequence_set_length(copy_field, length_field); | |
595 | bt_put(length_field); | |
596 | if (ret) { | |
597 | ret = -1; | |
598 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
599 | __LINE__); | |
600 | goto end; | |
601 | } | |
602 | ||
603 | for (i = 0; i < count; i++) { | |
604 | struct bt_ctf_field *entry_field, *entry_copy; | |
605 | ||
606 | entry_field = bt_ctf_field_sequence_get_field(field, i); | |
607 | if (!entry_field) { | |
608 | ret = -1; | |
609 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
610 | __LINE__); | |
611 | goto end; | |
612 | } | |
613 | ||
614 | entry_copy = bt_ctf_field_sequence_get_field(copy_field, i); | |
615 | if (!entry_copy) { | |
616 | ret = -1; | |
617 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
618 | __LINE__); | |
619 | goto end; | |
620 | } | |
621 | ||
622 | ret = copy_override_field(err, event, writer_event, entry_field, | |
623 | entry_copy); | |
624 | bt_put(entry_field); | |
625 | bt_put(entry_copy); | |
626 | if (ret) { | |
627 | ret = -1; | |
628 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
629 | __LINE__); | |
630 | goto end; | |
631 | } | |
632 | } | |
633 | ||
634 | bt_put(entry_type); | |
635 | ret = 0; | |
636 | ||
637 | end: | |
638 | return ret; | |
639 | } | |
640 | ||
641 | static | |
642 | int copy_find_clock_array_field(FILE *err, | |
643 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
644 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
645 | struct bt_ctf_field *copy_field) | |
646 | { | |
647 | int ret, count, i; | |
648 | struct bt_ctf_field_type *entry_type; | |
649 | ||
650 | count = bt_ctf_field_type_array_get_length(type); | |
651 | entry_type = bt_ctf_field_type_array_get_element_type(type); | |
652 | if (!entry_type) { | |
653 | ret = -1; | |
654 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
655 | __LINE__); | |
656 | goto end; | |
657 | } | |
658 | for (i = 0; i < count; i++) { | |
659 | struct bt_ctf_field *entry_field, *entry_copy; | |
660 | ||
661 | entry_field = bt_ctf_field_array_get_field(field, i); | |
662 | if (!entry_field) { | |
663 | ret = -1; | |
664 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
665 | __LINE__); | |
666 | goto end; | |
667 | } | |
668 | ||
669 | entry_copy = bt_ctf_field_array_get_field(copy_field, i); | |
670 | if (!entry_copy) { | |
671 | ret = -1; | |
672 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
673 | __LINE__); | |
674 | goto end; | |
675 | } | |
676 | ||
677 | ret = copy_override_field(err, event, writer_event, entry_field, | |
678 | entry_copy); | |
679 | bt_put(entry_field); | |
680 | bt_put(entry_copy); | |
681 | if (ret) { | |
682 | ret = -1; | |
683 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
684 | __LINE__); | |
685 | goto end; | |
686 | } | |
687 | } | |
688 | ||
689 | bt_put(entry_type); | |
690 | ret = 0; | |
691 | ||
692 | end: | |
693 | return ret; | |
694 | } | |
695 | ||
696 | static | |
697 | int copy_find_clock_struct_field(FILE *err, | |
698 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
699 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
700 | struct bt_ctf_field *copy_field) | |
701 | { | |
702 | int count, i, ret; | |
703 | ||
704 | count = bt_ctf_field_type_structure_get_field_count(type); | |
705 | for (i = 0; i < count; i++) { | |
706 | struct bt_ctf_field_type *entry_type; | |
707 | struct bt_ctf_field *entry_field; | |
708 | const char *entry_name; | |
709 | struct bt_ctf_field *entry_copy; | |
710 | ||
711 | entry_field = bt_ctf_field_structure_get_field_by_index(field, i); | |
712 | if (!entry_field) { | |
713 | ret = -1; | |
714 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
715 | __LINE__); | |
716 | goto end; | |
717 | } | |
718 | ||
719 | ret = bt_ctf_field_type_structure_get_field(type, &entry_name, | |
720 | &entry_type, i); | |
721 | if (ret) { | |
722 | bt_put(entry_field); | |
723 | ret = -1; | |
724 | fprintf(err, "[error] %s in %s:%d\n", | |
725 | __func__, __FILE__, __LINE__); | |
726 | goto end; | |
727 | } | |
728 | ||
729 | entry_copy = bt_ctf_field_structure_get_field_by_index(copy_field, i); | |
730 | if (!entry_copy) { | |
731 | bt_put(entry_field); | |
732 | bt_put(entry_type); | |
733 | ret = -1; | |
734 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
735 | __LINE__); | |
736 | goto end; | |
737 | } | |
738 | ||
739 | ret = copy_override_field(err, event, writer_event, entry_field, | |
740 | entry_copy); | |
741 | ||
742 | bt_put(entry_copy); | |
743 | bt_put(entry_field); | |
744 | bt_put(entry_type); | |
745 | if (ret) { | |
746 | BT_PUT(entry_copy); | |
747 | ret = -1; | |
748 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
749 | __LINE__); | |
750 | goto end; | |
751 | } | |
752 | } | |
753 | ||
754 | ret = 0; | |
755 | ||
756 | end: | |
757 | return ret; | |
758 | } | |
759 | ||
760 | static | |
761 | int set_int_value(FILE *err, struct bt_ctf_field *field, | |
762 | struct bt_ctf_field *copy_field, | |
763 | struct bt_ctf_field_type *type) | |
764 | { | |
765 | uint64_t uvalue; | |
766 | int64_t value; | |
767 | int ret; | |
768 | ||
769 | if (bt_ctf_field_type_integer_get_signed(type)) { | |
770 | ret = bt_ctf_field_signed_integer_get_value(field, &value); | |
771 | if (ret) { | |
772 | ret = -1; | |
773 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
774 | __FILE__, __LINE__); | |
775 | goto end; | |
776 | } | |
777 | printf(" - v(s) = %ld\n", value); | |
778 | ret = bt_ctf_field_signed_integer_set_value(copy_field, value); | |
779 | if (ret) { | |
780 | ret = -1; | |
781 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
782 | __FILE__, __LINE__); | |
783 | goto end; | |
784 | } | |
785 | } else { | |
786 | ret = bt_ctf_field_unsigned_integer_get_value(field, | |
787 | &uvalue); | |
788 | if (ret) { | |
789 | ret = -1; | |
790 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
791 | __FILE__, __LINE__); | |
792 | goto end; | |
793 | } | |
794 | ret = bt_ctf_field_unsigned_integer_set_value(copy_field, uvalue); | |
795 | if (ret) { | |
796 | ret = -1; | |
797 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
798 | __FILE__, __LINE__); | |
799 | goto end; | |
800 | } | |
801 | } | |
802 | ret = 0; | |
803 | ||
804 | end: | |
805 | return ret; | |
806 | } | |
807 | ||
808 | struct bt_ctf_clock_class *stream_class_get_clock_class(FILE *err, | |
809 | struct bt_ctf_stream_class *stream_class) | |
810 | { | |
811 | struct bt_ctf_trace *trace; | |
812 | struct bt_ctf_clock_class *clock_class = NULL; | |
813 | ||
814 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
815 | if (!trace) { | |
816 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
817 | __LINE__); | |
818 | goto end; | |
819 | } | |
820 | ||
821 | /* FIXME multi-clock? */ | |
822 | clock_class = bt_ctf_trace_get_clock_class(trace, 0); | |
823 | ||
824 | bt_put(trace); | |
825 | end: | |
826 | return clock_class; | |
827 | } | |
828 | ||
829 | struct bt_ctf_clock_class *event_get_clock_class(FILE *err, struct bt_ctf_event *event) | |
830 | { | |
831 | struct bt_ctf_event_class *event_class; | |
832 | struct bt_ctf_stream_class *stream_class; | |
833 | struct bt_ctf_clock_class *clock_class = NULL; | |
834 | ||
835 | event_class = bt_ctf_event_get_class(event); | |
836 | if (!event_class) { | |
837 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
838 | __LINE__); | |
839 | goto end; | |
840 | } | |
841 | ||
842 | stream_class = bt_ctf_event_class_get_stream_class(event_class); | |
843 | if (!stream_class) { | |
844 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
845 | __LINE__); | |
846 | goto end_put_event_class; | |
847 | } | |
848 | ||
849 | clock_class = stream_class_get_clock_class(err, stream_class); | |
850 | bt_put(stream_class); | |
851 | ||
852 | end_put_event_class: | |
853 | bt_put(event_class); | |
854 | end: | |
855 | return clock_class; | |
856 | } | |
857 | ||
858 | static | |
859 | int copy_find_clock_int_field(FILE *err, | |
860 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
861 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
862 | struct bt_ctf_field *copy_field) | |
863 | { | |
864 | struct bt_ctf_clock_class *clock_class, *writer_clock_class; | |
865 | struct bt_ctf_clock_value *clock_value, *writer_clock_value; | |
866 | uint64_t value; | |
867 | int ret; | |
868 | ||
869 | clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(type); | |
870 | if (!clock_class) { | |
871 | return set_int_value(err, field, copy_field, type); | |
872 | } | |
873 | ||
874 | clock_value = bt_ctf_event_get_clock_value(event, clock_class); | |
875 | bt_put(clock_class); | |
876 | if (!clock_value) { | |
877 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
878 | __LINE__); | |
879 | goto end; | |
880 | } | |
881 | ||
882 | ret = bt_ctf_clock_value_get_value(clock_value, &value); | |
883 | bt_put(clock_value); | |
884 | if (ret) { | |
885 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
886 | __LINE__); | |
887 | goto end; | |
888 | } | |
889 | ||
890 | ret = bt_ctf_field_unsigned_integer_set_value(copy_field, value); | |
891 | if (ret) { | |
892 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
893 | __LINE__); | |
894 | goto end; | |
895 | } | |
896 | ||
897 | writer_clock_class = event_get_clock_class(err, writer_event); | |
898 | if (!writer_clock_class) { | |
899 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
900 | __LINE__); | |
901 | goto end; | |
902 | } | |
903 | ||
904 | writer_clock_value = bt_ctf_clock_value_create(writer_clock_class, value); | |
905 | bt_put(writer_clock_class); | |
906 | if (!writer_clock_value) { | |
907 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
908 | __LINE__); | |
909 | goto end; | |
910 | } | |
911 | ||
912 | ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value); | |
913 | bt_put(writer_clock_value); | |
914 | if (ret) { | |
915 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
916 | __LINE__); | |
917 | goto end; | |
918 | } | |
919 | ||
920 | ret = 0; | |
921 | ||
922 | end: | |
923 | return ret; | |
924 | } | |
925 |