Commit | Line | Data |
---|---|---|
91b73004 JD |
1 | /* |
2 | * copytrace.c | |
3 | * | |
4 | * Babeltrace library to create a copy of a CTF trace | |
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 <assert.h> | |
38 | ||
39 | #include "ctfcopytrace.h" | |
b2f1f465 | 40 | #include "clock-fields.h" |
91b73004 JD |
41 | |
42 | struct bt_ctf_clock_class *ctf_copy_clock_class(FILE *err, | |
43 | struct bt_ctf_clock_class *clock_class) | |
44 | { | |
45 | int64_t offset, offset_s; | |
46 | int int_ret; | |
47 | uint64_t u64_ret; | |
48 | const char *name, *description; | |
49 | struct bt_ctf_clock_class *writer_clock_class = NULL; | |
50 | ||
51 | assert(err && clock_class); | |
52 | ||
53 | name = bt_ctf_clock_class_get_name(clock_class); | |
54 | if (!name) { | |
55 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
56 | __LINE__); | |
57 | goto end; | |
58 | } | |
59 | ||
60 | writer_clock_class = bt_ctf_clock_class_create(name); | |
61 | if (!writer_clock_class) { | |
62 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
63 | __LINE__); | |
64 | goto end; | |
65 | } | |
66 | ||
67 | description = bt_ctf_clock_class_get_description(clock_class); | |
68 | if (!description) { | |
69 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
70 | __LINE__); | |
71 | goto end_destroy; | |
72 | } | |
73 | ||
74 | int_ret = bt_ctf_clock_class_set_description(writer_clock_class, | |
75 | description); | |
76 | if (int_ret != 0) { | |
77 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
78 | __LINE__); | |
79 | goto end_destroy; | |
80 | } | |
81 | ||
82 | u64_ret = bt_ctf_clock_class_get_frequency(clock_class); | |
83 | if (u64_ret == -1ULL) { | |
84 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
85 | __LINE__); | |
86 | goto end_destroy; | |
87 | } | |
88 | int_ret = bt_ctf_clock_class_set_frequency(writer_clock_class, u64_ret); | |
89 | if (int_ret != 0) { | |
90 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
91 | __LINE__); | |
92 | goto end_destroy; | |
93 | } | |
94 | ||
95 | u64_ret = bt_ctf_clock_class_get_precision(clock_class); | |
96 | if (u64_ret == -1ULL) { | |
97 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
98 | __LINE__); | |
99 | goto end_destroy; | |
100 | } | |
101 | int_ret = bt_ctf_clock_class_set_precision(writer_clock_class, | |
102 | u64_ret); | |
103 | if (int_ret != 0) { | |
104 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
105 | __LINE__); | |
106 | goto end_destroy; | |
107 | } | |
108 | ||
109 | int_ret = bt_ctf_clock_class_get_offset_s(clock_class, &offset_s); | |
110 | if (int_ret != 0) { | |
111 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
112 | __LINE__); | |
113 | goto end_destroy; | |
114 | } | |
115 | ||
116 | int_ret = bt_ctf_clock_class_set_offset_s(writer_clock_class, offset_s); | |
117 | if (int_ret != 0) { | |
118 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
119 | __LINE__); | |
120 | goto end_destroy; | |
121 | } | |
122 | ||
123 | int_ret = bt_ctf_clock_class_get_offset_cycles(clock_class, &offset); | |
124 | if (int_ret != 0) { | |
125 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
126 | __LINE__); | |
127 | goto end_destroy; | |
128 | } | |
129 | ||
130 | int_ret = bt_ctf_clock_class_set_offset_cycles(writer_clock_class, offset); | |
131 | if (int_ret != 0) { | |
132 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
133 | __LINE__); | |
134 | goto end_destroy; | |
135 | } | |
136 | ||
137 | int_ret = bt_ctf_clock_class_get_is_absolute(clock_class); | |
138 | if (int_ret == -1) { | |
139 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
140 | __LINE__); | |
141 | goto end_destroy; | |
142 | } | |
143 | ||
144 | int_ret = bt_ctf_clock_class_set_is_absolute(writer_clock_class, int_ret); | |
145 | if (int_ret != 0) { | |
146 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
147 | __LINE__); | |
148 | goto end_destroy; | |
149 | } | |
150 | ||
151 | goto end; | |
152 | ||
153 | end_destroy: | |
154 | BT_PUT(writer_clock_class); | |
155 | end: | |
156 | return writer_clock_class; | |
157 | } | |
158 | ||
159 | enum bt_component_status ctf_copy_clock_classes(FILE *err, | |
160 | struct bt_ctf_trace *writer_trace, | |
161 | struct bt_ctf_stream_class *writer_stream_class, | |
162 | struct bt_ctf_trace *trace) | |
163 | { | |
164 | enum bt_component_status ret; | |
91b73004 JD |
165 | int int_ret, clock_class_count, i; |
166 | ||
167 | clock_class_count = bt_ctf_trace_get_clock_class_count(trace); | |
168 | ||
169 | for (i = 0; i < clock_class_count; i++) { | |
170 | struct bt_ctf_clock_class *writer_clock_class; | |
171 | struct bt_ctf_clock_class *clock_class = | |
172 | bt_ctf_trace_get_clock_class(trace, i); | |
173 | ||
174 | if (!clock_class) { | |
175 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
176 | __LINE__); | |
177 | ret = BT_COMPONENT_STATUS_ERROR; | |
178 | goto end; | |
179 | } | |
180 | ||
181 | writer_clock_class = ctf_copy_clock_class(err, clock_class); | |
182 | bt_put(clock_class); | |
183 | if (!writer_clock_class) { | |
184 | fprintf(err, "Failed to copy clock class"); | |
185 | ret = BT_COMPONENT_STATUS_ERROR; | |
186 | goto end; | |
187 | } | |
188 | ||
189 | int_ret = bt_ctf_trace_add_clock_class(writer_trace, writer_clock_class); | |
190 | if (int_ret != 0) { | |
191 | BT_PUT(writer_clock_class); | |
192 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
193 | __LINE__); | |
194 | ret = BT_COMPONENT_STATUS_ERROR; | |
195 | goto end; | |
196 | } | |
197 | ||
198 | /* | |
b2f1f465 | 199 | * Ownership transferred to the trace. |
91b73004 JD |
200 | */ |
201 | bt_put(writer_clock_class); | |
202 | } | |
203 | ||
204 | ret = BT_COMPONENT_STATUS_OK; | |
205 | ||
206 | end: | |
207 | return ret; | |
208 | } | |
209 | ||
210 | struct bt_ctf_event_class *ctf_copy_event_class(FILE *err, | |
211 | struct bt_ctf_event_class *event_class) | |
212 | { | |
213 | struct bt_ctf_event_class *writer_event_class = NULL; | |
214 | const char *name; | |
215 | int count, i; | |
216 | ||
217 | name = bt_ctf_event_class_get_name(event_class); | |
218 | if (!name) { | |
219 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
220 | __FILE__, __LINE__); | |
221 | goto end; | |
222 | } | |
223 | ||
224 | writer_event_class = bt_ctf_event_class_create(name); | |
225 | if (!writer_event_class) { | |
226 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
227 | __FILE__, __LINE__); | |
228 | goto end; | |
229 | } | |
230 | ||
231 | count = bt_ctf_event_class_get_attribute_count(event_class); | |
232 | for (i = 0; i < count; i++) { | |
233 | const char *attr_name; | |
234 | struct bt_value *attr_value; | |
235 | int ret; | |
236 | ||
237 | attr_name = bt_ctf_event_class_get_attribute_name(event_class, i); | |
238 | if (!attr_name) { | |
239 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
240 | __FILE__, __LINE__); | |
9ae49d3d | 241 | goto error; |
91b73004 JD |
242 | } |
243 | attr_value = bt_ctf_event_class_get_attribute_value(event_class, i); | |
244 | if (!attr_value) { | |
245 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
246 | __FILE__, __LINE__); | |
9ae49d3d | 247 | goto error; |
91b73004 JD |
248 | } |
249 | ||
250 | ret = bt_ctf_event_class_set_attribute(writer_event_class, | |
251 | attr_name, attr_value); | |
9ae49d3d | 252 | BT_PUT(attr_value); |
91b73004 JD |
253 | if (ret < 0) { |
254 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
255 | __FILE__, __LINE__); | |
9ae49d3d | 256 | goto error; |
91b73004 JD |
257 | } |
258 | } | |
259 | ||
260 | count = bt_ctf_event_class_get_field_count(event_class); | |
261 | for (i = 0; i < count; i++) { | |
262 | const char *field_name; | |
263 | struct bt_ctf_field_type *field_type; | |
264 | int ret; | |
265 | ||
266 | ret = bt_ctf_event_class_get_field(event_class, &field_name, | |
267 | &field_type, i); | |
268 | if (ret < 0) { | |
269 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); | |
9ae49d3d | 270 | goto error; |
91b73004 JD |
271 | } |
272 | ||
273 | ret = bt_ctf_event_class_add_field(writer_event_class, field_type, | |
274 | field_name); | |
9ae49d3d | 275 | BT_PUT(field_type); |
91b73004 JD |
276 | if (ret < 0) { |
277 | fprintf(err, "[error] Cannot add field %s\n", field_name); | |
278 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); | |
9ae49d3d | 279 | goto error; |
91b73004 | 280 | } |
91b73004 JD |
281 | } |
282 | ||
9ae49d3d JD |
283 | goto end; |
284 | ||
285 | error: | |
286 | BT_PUT(writer_event_class); | |
91b73004 JD |
287 | end: |
288 | return writer_event_class; | |
289 | } | |
290 | ||
291 | enum bt_component_status ctf_copy_event_classes(FILE *err, | |
292 | struct bt_ctf_stream_class *stream_class, | |
293 | struct bt_ctf_stream_class *writer_stream_class) | |
294 | { | |
295 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
9ae49d3d | 296 | struct bt_ctf_event_class *event_class = NULL, *writer_event_class = NULL; |
91b73004 JD |
297 | int count, i; |
298 | ||
299 | count = bt_ctf_stream_class_get_event_class_count(stream_class); | |
300 | if (count < 0) { | |
301 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
302 | __FILE__, __LINE__); | |
303 | goto end; | |
304 | } | |
305 | ||
306 | for (i = 0; i < count; i++) { | |
91b73004 JD |
307 | struct bt_ctf_field_type *context; |
308 | int int_ret; | |
309 | ||
310 | event_class = bt_ctf_stream_class_get_event_class( | |
311 | stream_class, i); | |
312 | if (!event_class) { | |
313 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
314 | __FILE__, __LINE__); | |
315 | ret = BT_COMPONENT_STATUS_ERROR; | |
9ae49d3d | 316 | goto error; |
91b73004 JD |
317 | } |
318 | writer_event_class = ctf_copy_event_class(err, event_class); | |
319 | if (!writer_event_class) { | |
320 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
321 | __FILE__, __LINE__); | |
322 | ret = BT_COMPONENT_STATUS_ERROR; | |
9ae49d3d | 323 | goto error; |
91b73004 JD |
324 | } |
325 | ||
326 | context = bt_ctf_event_class_get_context_type(event_class); | |
9ae49d3d JD |
327 | if (!context) { |
328 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
329 | __FILE__, __LINE__); | |
330 | ret = BT_COMPONENT_STATUS_ERROR; | |
331 | goto error; | |
332 | } | |
91b73004 | 333 | ret = bt_ctf_event_class_set_context_type(writer_event_class, context); |
9ae49d3d | 334 | BT_PUT(context); |
91b73004 JD |
335 | if (ret < 0) { |
336 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
337 | __LINE__); | |
9ae49d3d | 338 | goto error; |
91b73004 JD |
339 | } |
340 | ||
341 | int_ret = bt_ctf_stream_class_add_event_class(writer_stream_class, | |
342 | writer_event_class); | |
343 | if (int_ret < 0) { | |
344 | fprintf(err, "[error] Failed to add event class\n"); | |
345 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
346 | __FILE__, __LINE__); | |
347 | ret = BT_COMPONENT_STATUS_ERROR; | |
9ae49d3d | 348 | goto error; |
91b73004 | 349 | } |
9ae49d3d JD |
350 | BT_PUT(writer_event_class); |
351 | BT_PUT(event_class); | |
91b73004 JD |
352 | } |
353 | ||
9ae49d3d JD |
354 | goto end; |
355 | ||
356 | error: | |
357 | bt_put(event_class); | |
358 | bt_put(writer_event_class); | |
91b73004 JD |
359 | end: |
360 | return ret; | |
361 | } | |
362 | ||
363 | struct bt_ctf_stream_class *ctf_copy_stream_class(FILE *err, | |
b2f1f465 JD |
364 | struct bt_ctf_stream_class *stream_class, |
365 | struct bt_ctf_trace *writer_trace, | |
366 | bool override_ts64) | |
91b73004 | 367 | { |
9ae49d3d JD |
368 | struct bt_ctf_field_type *type = NULL; |
369 | struct bt_ctf_stream_class *writer_stream_class = NULL; | |
91b73004 JD |
370 | int ret_int; |
371 | const char *name = bt_ctf_stream_class_get_name(stream_class); | |
372 | ||
373 | if (strlen(name) == 0) { | |
374 | name = NULL; | |
375 | } | |
376 | ||
377 | writer_stream_class = bt_ctf_stream_class_create(name); | |
378 | if (!writer_stream_class) { | |
379 | fprintf(err, "[error] %s in %s:%d\n", | |
380 | __func__, __FILE__, __LINE__); | |
381 | goto end; | |
382 | } | |
383 | ||
384 | type = bt_ctf_stream_class_get_packet_context_type(stream_class); | |
385 | if (!type) { | |
386 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
387 | __LINE__); | |
388 | goto error; | |
389 | } | |
390 | ||
391 | ret_int = bt_ctf_stream_class_set_packet_context_type( | |
392 | writer_stream_class, type); | |
91b73004 JD |
393 | if (ret_int < 0) { |
394 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
395 | __LINE__); | |
396 | goto error; | |
397 | } | |
9ae49d3d | 398 | BT_PUT(type); |
91b73004 JD |
399 | |
400 | type = bt_ctf_stream_class_get_event_header_type(stream_class); | |
401 | if (!type) { | |
402 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
403 | __LINE__); | |
404 | goto error; | |
405 | } | |
406 | ||
b2f1f465 | 407 | if (override_ts64) { |
9ae49d3d JD |
408 | struct bt_ctf_field_type *new_event_header_type; |
409 | ||
b2f1f465 JD |
410 | new_event_header_type = override_header_type(err, type, |
411 | writer_trace); | |
b2f1f465 JD |
412 | if (!new_event_header_type) { |
413 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
414 | __LINE__); | |
415 | goto error; | |
416 | } | |
0f29db56 JD |
417 | ret_int = bt_ctf_stream_class_set_event_header_type( |
418 | writer_stream_class, new_event_header_type); | |
9ae49d3d | 419 | BT_PUT(new_event_header_type); |
0f29db56 JD |
420 | if (ret_int < 0) { |
421 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
422 | __LINE__); | |
423 | goto error; | |
424 | } | |
b2f1f465 | 425 | } else { |
0f29db56 JD |
426 | ret_int = bt_ctf_stream_class_set_event_header_type( |
427 | writer_stream_class, type); | |
0f29db56 JD |
428 | if (ret_int < 0) { |
429 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
430 | __LINE__); | |
431 | goto error; | |
432 | } | |
91b73004 | 433 | } |
9ae49d3d | 434 | BT_PUT(type); |
91b73004 JD |
435 | |
436 | type = bt_ctf_stream_class_get_event_context_type(stream_class); | |
437 | if (type) { | |
438 | ret_int = bt_ctf_stream_class_set_event_context_type( | |
439 | writer_stream_class, type); | |
91b73004 JD |
440 | if (ret_int < 0) { |
441 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
442 | __LINE__); | |
443 | goto error; | |
444 | } | |
445 | } | |
9ae49d3d | 446 | BT_PUT(type); |
91b73004 JD |
447 | |
448 | goto end; | |
449 | ||
450 | error: | |
451 | BT_PUT(writer_stream_class); | |
452 | end: | |
9ae49d3d | 453 | bt_put(type); |
91b73004 JD |
454 | return writer_stream_class; |
455 | } | |
456 | ||
457 | enum bt_component_status ctf_copy_packet_context_field(FILE *err, | |
458 | struct bt_ctf_field *field, const char *field_name, | |
459 | struct bt_ctf_field *writer_packet_context, | |
460 | struct bt_ctf_field_type *writer_packet_context_type) | |
461 | { | |
462 | enum bt_component_status ret; | |
9ae49d3d JD |
463 | struct bt_ctf_field *writer_field = NULL; |
464 | struct bt_ctf_field_type *field_type = NULL; | |
91b73004 JD |
465 | int int_ret; |
466 | uint64_t value; | |
467 | ||
468 | field_type = bt_ctf_field_get_type(field); | |
469 | if (!field_type) { | |
470 | ret = BT_COMPONENT_STATUS_ERROR; | |
471 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
472 | __LINE__); | |
473 | goto end; | |
474 | } | |
475 | /* | |
476 | * Only support for integers for now. | |
477 | */ | |
1487a16a | 478 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
91b73004 | 479 | fprintf(err, "[error] Unsupported packet context field type\n"); |
91b73004 | 480 | ret = BT_COMPONENT_STATUS_ERROR; |
9ae49d3d | 481 | goto error; |
91b73004 | 482 | } |
9ae49d3d | 483 | BT_PUT(field_type); |
91b73004 | 484 | |
91b73004 JD |
485 | writer_field = bt_ctf_field_structure_get_field(writer_packet_context, |
486 | field_name); | |
487 | if (!writer_field) { | |
488 | ret = BT_COMPONENT_STATUS_ERROR; | |
489 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
490 | __LINE__); | |
491 | goto end; | |
492 | } | |
493 | ||
91b73004 JD |
494 | int_ret = bt_ctf_field_unsigned_integer_get_value(field, &value); |
495 | if (int_ret < 0) { | |
496 | fprintf(err, "[error] Wrong packet_context field type\n"); | |
497 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
498 | __LINE__); | |
499 | ret = BT_COMPONENT_STATUS_ERROR; | |
9ae49d3d | 500 | goto end; |
91b73004 JD |
501 | } |
502 | ||
503 | int_ret = bt_ctf_field_unsigned_integer_set_value(writer_field, value); | |
504 | if (int_ret < 0) { | |
505 | ret = BT_COMPONENT_STATUS_ERROR; | |
506 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
507 | __LINE__); | |
9ae49d3d | 508 | goto end; |
91b73004 JD |
509 | } |
510 | ||
511 | ret = BT_COMPONENT_STATUS_OK; | |
512 | ||
9ae49d3d JD |
513 | goto end; |
514 | ||
515 | error: | |
516 | bt_put(field_type); | |
91b73004 | 517 | end: |
9ae49d3d | 518 | bt_put(writer_field); |
91b73004 JD |
519 | return ret; |
520 | } | |
521 | ||
b2f1f465 | 522 | struct bt_ctf_field *ctf_copy_packet_context(FILE *err, |
91b73004 JD |
523 | struct bt_ctf_packet *packet, |
524 | struct bt_ctf_stream *writer_stream) | |
525 | { | |
526 | enum bt_component_status ret; | |
9ae49d3d JD |
527 | struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL; |
528 | struct bt_ctf_field_type *struct_type = NULL, *writer_packet_context_type = NULL; | |
529 | struct bt_ctf_stream_class *writer_stream_class = NULL; | |
f6f999a3 | 530 | struct bt_ctf_field *field = NULL; |
9ae49d3d | 531 | struct bt_ctf_field_type *field_type; |
b2f1f465 | 532 | int nr_fields, i; |
91b73004 JD |
533 | |
534 | packet_context = bt_ctf_packet_get_context(packet); | |
535 | if (!packet_context) { | |
91b73004 JD |
536 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
537 | __LINE__); | |
9ae49d3d | 538 | goto error; |
91b73004 JD |
539 | } |
540 | ||
541 | writer_stream_class = bt_ctf_stream_get_class(writer_stream); | |
542 | if (!writer_stream_class) { | |
91b73004 JD |
543 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
544 | __LINE__); | |
9ae49d3d | 545 | goto error; |
91b73004 JD |
546 | } |
547 | ||
548 | writer_packet_context_type = bt_ctf_stream_class_get_packet_context_type( | |
549 | writer_stream_class); | |
9ae49d3d | 550 | BT_PUT(writer_stream_class); |
91b73004 | 551 | if (!writer_packet_context_type) { |
91b73004 JD |
552 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
553 | __LINE__); | |
9ae49d3d | 554 | goto error; |
91b73004 JD |
555 | } |
556 | ||
557 | struct_type = bt_ctf_field_get_type(packet_context); | |
558 | if (!struct_type) { | |
91b73004 JD |
559 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
560 | __LINE__); | |
9ae49d3d | 561 | goto error; |
91b73004 JD |
562 | } |
563 | ||
564 | writer_packet_context = bt_ctf_field_create(writer_packet_context_type); | |
565 | if (!writer_packet_context) { | |
91b73004 JD |
566 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
567 | __LINE__); | |
9ae49d3d | 568 | goto error; |
91b73004 JD |
569 | } |
570 | ||
571 | nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type); | |
572 | for (i = 0; i < nr_fields; i++) { | |
91b73004 JD |
573 | const char *field_name; |
574 | ||
0f29db56 | 575 | field = bt_ctf_field_structure_get_field_by_index( |
91b73004 JD |
576 | packet_context, i); |
577 | if (!field) { | |
91b73004 JD |
578 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
579 | __FILE__, __LINE__); | |
9ae49d3d | 580 | goto error; |
91b73004 JD |
581 | } |
582 | if (bt_ctf_field_type_structure_get_field(struct_type, | |
583 | &field_name, &field_type, i) < 0) { | |
91b73004 JD |
584 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
585 | __FILE__, __LINE__); | |
9ae49d3d | 586 | goto error; |
91b73004 JD |
587 | } |
588 | if (!strncmp(field_name, "content_size", strlen("content_size")) || | |
589 | !strncmp(field_name, "packet_size", | |
590 | strlen("packet_size"))) { | |
9ae49d3d JD |
591 | BT_PUT(field_type); |
592 | BT_PUT(field); | |
91b73004 JD |
593 | continue; |
594 | } | |
595 | ||
1487a16a | 596 | if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) { |
91b73004 | 597 | fprintf(err, "[error] Unexpected packet context field type\n"); |
9ae49d3d | 598 | goto error; |
91b73004 JD |
599 | } |
600 | ||
601 | ret = ctf_copy_packet_context_field(err, field, field_name, | |
602 | writer_packet_context, writer_packet_context_type); | |
9ae49d3d JD |
603 | BT_PUT(field_type); |
604 | BT_PUT(field); | |
91b73004 JD |
605 | if (ret != BT_COMPONENT_STATUS_OK) { |
606 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
607 | __FILE__, __LINE__); | |
9ae49d3d | 608 | goto error; |
91b73004 JD |
609 | } |
610 | } | |
611 | ||
9ae49d3d JD |
612 | goto end; |
613 | ||
614 | error: | |
615 | BT_PUT(writer_packet_context); | |
616 | end: | |
617 | bt_put(field); | |
618 | bt_put(field_type); | |
91b73004 | 619 | bt_put(struct_type); |
91b73004 | 620 | bt_put(writer_packet_context_type); |
91b73004 | 621 | bt_put(writer_stream_class); |
91b73004 | 622 | bt_put(packet_context); |
b2f1f465 JD |
623 | return writer_packet_context; |
624 | } | |
625 | ||
0f29db56 | 626 | int ctf_copy_event_header(FILE *err, struct bt_ctf_event *event, |
b2f1f465 JD |
627 | struct bt_ctf_event_class *writer_event_class, |
628 | struct bt_ctf_event *writer_event, | |
629 | struct bt_ctf_field *event_header) | |
630 | { | |
9ae49d3d JD |
631 | struct bt_ctf_clock_class *clock_class = NULL, *writer_clock_class = NULL; |
632 | struct bt_ctf_clock_value *clock_value = NULL, *writer_clock_value = NULL; | |
b2f1f465 JD |
633 | |
634 | int ret; | |
0f29db56 JD |
635 | struct bt_ctf_field *writer_event_header = NULL; |
636 | uint64_t value; | |
b2f1f465 | 637 | |
0f29db56 JD |
638 | clock_class = event_get_clock_class(err, event); |
639 | if (!clock_class) { | |
640 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
641 | __LINE__); | |
642 | goto error; | |
643 | } | |
644 | ||
645 | clock_value = bt_ctf_event_get_clock_value(event, clock_class); | |
9ae49d3d | 646 | BT_PUT(clock_class); |
0f29db56 JD |
647 | if (!clock_value) { |
648 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
649 | __LINE__); | |
650 | goto error; | |
651 | } | |
652 | ||
653 | ret = bt_ctf_clock_value_get_value(clock_value, &value); | |
9ae49d3d | 654 | BT_PUT(clock_value); |
0f29db56 JD |
655 | if (ret) { |
656 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
657 | __LINE__); | |
658 | goto error; | |
b2f1f465 JD |
659 | } |
660 | ||
661 | writer_clock_class = event_get_clock_class(err, writer_event); | |
662 | if (!writer_clock_class) { | |
663 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
664 | __LINE__); | |
665 | goto error; | |
666 | } | |
667 | ||
0f29db56 | 668 | writer_clock_value = bt_ctf_clock_value_create(writer_clock_class, value); |
9ae49d3d | 669 | BT_PUT(writer_clock_class); |
0f29db56 | 670 | if (!writer_clock_value) { |
b2f1f465 JD |
671 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
672 | __LINE__); | |
9ae49d3d | 673 | goto error; |
b2f1f465 JD |
674 | } |
675 | ||
0f29db56 | 676 | ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value); |
9ae49d3d | 677 | BT_PUT(writer_clock_value); |
b2f1f465 JD |
678 | if (ret) { |
679 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
680 | __LINE__); | |
681 | goto error; | |
682 | } | |
683 | ||
0f29db56 JD |
684 | writer_event_header = bt_ctf_field_copy(event_header); |
685 | if (!writer_event_header) { | |
686 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
687 | __FILE__, __LINE__); | |
0f29db56 JD |
688 | goto end; |
689 | } | |
690 | ||
b2f1f465 | 691 | ret = bt_ctf_event_set_header(writer_event, writer_event_header); |
9ae49d3d | 692 | BT_PUT(writer_event_header); |
b2f1f465 JD |
693 | if (ret < 0) { |
694 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
695 | __FILE__, __LINE__); | |
696 | goto error; | |
697 | } | |
b2f1f465 JD |
698 | |
699 | ret = 0; | |
700 | ||
701 | goto end; | |
702 | ||
703 | error: | |
b2f1f465 | 704 | ret = -1; |
91b73004 JD |
705 | end: |
706 | return ret; | |
707 | } | |
708 | ||
709 | struct bt_ctf_event *ctf_copy_event(FILE *err, struct bt_ctf_event *event, | |
b2f1f465 JD |
710 | struct bt_ctf_event_class *writer_event_class, |
711 | bool override_ts64) | |
91b73004 | 712 | { |
9ae49d3d JD |
713 | struct bt_ctf_event *writer_event = NULL; |
714 | struct bt_ctf_field *field = NULL, *copy_field = NULL; | |
91b73004 JD |
715 | int ret; |
716 | ||
717 | writer_event = bt_ctf_event_create(writer_event_class); | |
718 | if (!writer_event) { | |
719 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
720 | __LINE__); | |
721 | goto end; | |
722 | } | |
723 | ||
724 | field = bt_ctf_event_get_header(event); | |
725 | if (!field) { | |
91b73004 JD |
726 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
727 | __FILE__, __LINE__); | |
9ae49d3d | 728 | goto error; |
91b73004 JD |
729 | } |
730 | ||
b2f1f465 JD |
731 | /* |
732 | * If override_ts64, we override all integer fields mapped to a clock | |
733 | * to a uint64_t field type, otherwise, we just copy it as is. | |
734 | */ | |
735 | if (override_ts64) { | |
736 | copy_field = bt_ctf_event_get_header(writer_event); | |
737 | if (!copy_field) { | |
91b73004 JD |
738 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
739 | __FILE__, __LINE__); | |
9ae49d3d | 740 | goto error; |
b2f1f465 JD |
741 | } |
742 | ||
743 | ret = copy_override_field(err, event, writer_event, field, | |
744 | copy_field); | |
b2f1f465 | 745 | if (ret) { |
b2f1f465 JD |
746 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
747 | __FILE__, __LINE__); | |
9ae49d3d | 748 | goto error; |
b2f1f465 | 749 | } |
9ae49d3d | 750 | BT_PUT(copy_field); |
b2f1f465 | 751 | } else { |
0f29db56 | 752 | ret = ctf_copy_event_header(err, event, writer_event_class, |
b2f1f465 JD |
753 | writer_event, field); |
754 | if (ret) { | |
b2f1f465 JD |
755 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
756 | __FILE__, __LINE__); | |
9ae49d3d | 757 | goto error; |
91b73004 | 758 | } |
91b73004 | 759 | } |
9ae49d3d | 760 | BT_PUT(field); |
91b73004 JD |
761 | |
762 | /* Optional field, so it can fail silently. */ | |
763 | field = bt_ctf_event_get_stream_event_context(event); | |
764 | copy_field = bt_ctf_field_copy(field); | |
91b73004 JD |
765 | if (copy_field) { |
766 | ret = bt_ctf_event_set_stream_event_context(writer_event, | |
767 | copy_field); | |
91b73004 JD |
768 | if (ret < 0) { |
769 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
770 | __FILE__, __LINE__); | |
771 | goto error; | |
772 | } | |
773 | } | |
9ae49d3d JD |
774 | BT_PUT(field); |
775 | BT_PUT(copy_field); | |
91b73004 JD |
776 | |
777 | /* Optional field, so it can fail silently. */ | |
778 | field = bt_ctf_event_get_event_context(event); | |
779 | copy_field = bt_ctf_field_copy(field); | |
91b73004 JD |
780 | if (copy_field) { |
781 | ret = bt_ctf_event_set_event_context(writer_event, copy_field); | |
91b73004 JD |
782 | if (ret < 0) { |
783 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
784 | __FILE__, __LINE__); | |
785 | goto error; | |
786 | } | |
787 | } | |
9ae49d3d JD |
788 | BT_PUT(field); |
789 | BT_PUT(copy_field); | |
91b73004 JD |
790 | |
791 | field = bt_ctf_event_get_payload_field(event); | |
792 | if (!field) { | |
91b73004 JD |
793 | fprintf(err, "[error] %s in %s:%d\n", __func__, |
794 | __FILE__, __LINE__); | |
9ae49d3d | 795 | goto error; |
91b73004 JD |
796 | } |
797 | copy_field = bt_ctf_field_copy(field); | |
91b73004 JD |
798 | if (copy_field) { |
799 | ret = bt_ctf_event_set_payload_field(writer_event, copy_field); | |
91b73004 JD |
800 | if (ret < 0) { |
801 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
802 | __FILE__, __LINE__); | |
803 | goto error; | |
804 | } | |
805 | } | |
9ae49d3d JD |
806 | BT_PUT(field); |
807 | BT_PUT(copy_field); | |
808 | ||
91b73004 JD |
809 | goto end; |
810 | ||
811 | error: | |
812 | BT_PUT(writer_event); | |
813 | end: | |
9ae49d3d JD |
814 | bt_put(field); |
815 | bt_put(copy_field); | |
91b73004 JD |
816 | return writer_event; |
817 | } | |
818 | ||
819 | enum bt_component_status ctf_copy_trace(FILE *err, struct bt_ctf_trace *trace, | |
820 | struct bt_ctf_trace *writer_trace) | |
821 | { | |
822 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
823 | int field_count, i, int_ret; | |
9ae49d3d | 824 | struct bt_ctf_field_type *header_type = NULL; |
91b73004 JD |
825 | |
826 | field_count = bt_ctf_trace_get_environment_field_count(trace); | |
827 | for (i = 0; i < field_count; i++) { | |
828 | int ret_int; | |
829 | const char *name; | |
9ae49d3d | 830 | struct bt_value *value = NULL; |
91b73004 JD |
831 | |
832 | name = bt_ctf_trace_get_environment_field_name(trace, i); | |
833 | if (!name) { | |
834 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
835 | __LINE__); | |
836 | ret = BT_COMPONENT_STATUS_ERROR; | |
9ae49d3d | 837 | goto end; |
91b73004 JD |
838 | } |
839 | value = bt_ctf_trace_get_environment_field_value(trace, i); | |
840 | if (!value) { | |
841 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
842 | __LINE__); | |
843 | ret = BT_COMPONENT_STATUS_ERROR; | |
9ae49d3d | 844 | goto end; |
91b73004 JD |
845 | } |
846 | ||
847 | ret_int = bt_ctf_trace_set_environment_field(writer_trace, | |
848 | name, value); | |
9ae49d3d | 849 | BT_PUT(value); |
91b73004 JD |
850 | if (ret_int < 0) { |
851 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
852 | __LINE__); | |
853 | fprintf(err, "[error] Unable to set environment field %s\n", | |
854 | name); | |
855 | ret = BT_COMPONENT_STATUS_ERROR; | |
9ae49d3d | 856 | goto end; |
91b73004 JD |
857 | } |
858 | } | |
859 | ||
860 | header_type = bt_ctf_trace_get_packet_header_type(writer_trace); | |
861 | if (!header_type) { | |
862 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); | |
863 | ret = BT_COMPONENT_STATUS_ERROR; | |
9ae49d3d | 864 | goto end; |
91b73004 JD |
865 | } |
866 | ||
867 | int_ret = bt_ctf_trace_set_packet_header_type(writer_trace, header_type); | |
9ae49d3d | 868 | BT_PUT(header_type); |
91b73004 JD |
869 | if (int_ret < 0) { |
870 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__); | |
871 | ret = BT_COMPONENT_STATUS_ERROR; | |
9ae49d3d | 872 | goto end; |
91b73004 JD |
873 | } |
874 | ||
9ae49d3d | 875 | end: |
91b73004 JD |
876 | return ret; |
877 | } |