flt.lttng-utils.debug-info: copy user attributes
[babeltrace.git] / src / plugins / lttng-utils / debug-info / trace-ir-metadata-copy.c
1 /*
2 * Babeltrace - Trace IR metadata object copy
3 *
4 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #define BT_COMP_LOG_SELF_COMP self_comp
28 #define BT_LOG_OUTPUT_LEVEL log_level
29 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-META-COPY"
30 #include "logging/comp-logging.h"
31
32 #include <inttypes.h>
33 #include <stdint.h>
34
35 #include "common/assert.h"
36
37 #include "trace-ir-metadata-copy.h"
38 #include "trace-ir-metadata-field-class-copy.h"
39 #include "utils.h"
40
41 BT_HIDDEN
42 int copy_trace_class_content(const bt_trace_class *in_trace_class,
43 bt_trace_class *out_trace_class, bt_logging_level log_level,
44 bt_self_component *self_comp)
45 {
46 BT_COMP_LOGD("Copying content of trace class: in-tc-addr=%p, out-tc-addr=%p",
47 in_trace_class, out_trace_class);
48
49 /*
50 * Safe to use the same value object because it's frozen at this
51 * point.
52 */
53 bt_trace_class_set_user_attributes(out_trace_class,
54 bt_trace_class_borrow_user_attributes_const(in_trace_class));
55
56 /* Use the same stream class ids as in the origin trace class. */
57 bt_trace_class_set_assigns_automatic_stream_class_id(out_trace_class,
58 BT_FALSE);
59 BT_COMP_LOGD("Copied content of trace class: in-tc-addr=%p, out-tc-addr=%p",
60 in_trace_class, out_trace_class);
61 return 0;
62 }
63
64 static
65 int copy_clock_class_content(const bt_clock_class *in_clock_class,
66 bt_clock_class *out_clock_class, bt_logging_level log_level,
67 bt_self_component *self_comp)
68 {
69 const char *clock_class_name, *clock_class_description;
70 int64_t seconds;
71 uint64_t cycles;
72 bt_uuid in_uuid;
73 int ret = 0;
74
75 BT_COMP_LOGD("Copying content of clock class: in-cc-addr=%p, out-cc-addr=%p",
76 in_clock_class, out_clock_class);
77
78 clock_class_name = bt_clock_class_get_name(in_clock_class);
79
80 if (clock_class_name) {
81 if (bt_clock_class_set_name(out_clock_class, clock_class_name)
82 != BT_CLOCK_CLASS_SET_NAME_STATUS_OK) {
83 BT_COMP_LOGE("Error setting clock class' name cc-addr=%p, name=%p",
84 out_clock_class, clock_class_name);
85 out_clock_class = NULL;
86 ret = -1;
87 goto error;
88 }
89 }
90
91 /*
92 * Safe to use the same value object because it's frozen at this
93 * point.
94 */
95 bt_clock_class_set_user_attributes(out_clock_class,
96 bt_clock_class_borrow_user_attributes_const(in_clock_class));
97
98 clock_class_description = bt_clock_class_get_description(in_clock_class);
99
100 if (clock_class_description) {
101 if (bt_clock_class_set_description(out_clock_class,
102 clock_class_description) !=
103 BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_OK) {
104 BT_COMP_LOGE("Error setting clock class' description cc-addr=%p, "
105 "name=%p", out_clock_class, clock_class_description);
106 out_clock_class = NULL;
107 ret = -1;
108 goto error;
109 }
110 }
111
112 in_uuid = bt_clock_class_get_uuid(in_clock_class);
113 if (in_uuid) {
114 bt_clock_class_set_uuid(out_clock_class, in_uuid);
115 }
116
117 bt_clock_class_set_frequency(out_clock_class,
118 bt_clock_class_get_frequency(in_clock_class));
119 bt_clock_class_set_precision(out_clock_class,
120 bt_clock_class_get_precision(in_clock_class));
121 bt_clock_class_get_offset(in_clock_class, &seconds, &cycles);
122 bt_clock_class_set_offset(out_clock_class, seconds, cycles);
123 bt_clock_class_set_origin_is_unix_epoch(out_clock_class,
124 bt_clock_class_origin_is_unix_epoch(in_clock_class));
125
126 BT_COMP_LOGD("Copied content of clock class: in-cc-addr=%p, out-cc-addr=%p",
127 in_clock_class, out_clock_class);
128
129 error:
130 return ret;
131 }
132
133 static
134 bt_clock_class *borrow_mapped_clock_class(
135 struct trace_ir_metadata_maps *md_maps,
136 const bt_clock_class *in_clock_class)
137 {
138 BT_ASSERT(md_maps);
139 BT_ASSERT(in_clock_class);
140
141 return g_hash_table_lookup(md_maps->clock_class_map,
142 (gpointer) in_clock_class);
143 }
144
145 static
146 bt_clock_class *create_new_mapped_clock_class(
147 bt_self_component *self_comp,
148 struct trace_ir_metadata_maps *md_maps,
149 const bt_clock_class *in_clock_class)
150 {
151 bt_clock_class *out_clock_class;
152 int ret;
153 bt_logging_level log_level = md_maps->log_level;
154
155 BT_COMP_LOGD("Creating new mapped clock class: in-cc-addr=%p",
156 in_clock_class);
157
158 BT_ASSERT(md_maps);
159 BT_ASSERT(in_clock_class);
160
161 BT_ASSERT(!borrow_mapped_clock_class(md_maps, in_clock_class));
162
163 out_clock_class = bt_clock_class_create(self_comp);
164 if (!out_clock_class) {
165 BT_COMP_LOGE_STR("Cannot create clock class");
166 goto end;
167 }
168 /* If not, create a new one and add it to the mapping. */
169 ret = copy_clock_class_content(in_clock_class, out_clock_class,
170 log_level, self_comp);
171 if (ret) {
172 BT_COMP_LOGE_STR("Cannot copy clock class");
173 goto end;
174 }
175
176 g_hash_table_insert(md_maps->clock_class_map,
177 (gpointer) in_clock_class, out_clock_class);
178
179 BT_COMP_LOGD("Created new mapped clock class: in-cc-addr=%p, out-cc-addr=%p",
180 in_clock_class, out_clock_class);
181 end:
182 return out_clock_class;
183 }
184
185 BT_HIDDEN
186 int copy_stream_class_content(struct trace_ir_maps *ir_maps,
187 const bt_stream_class *in_stream_class,
188 bt_stream_class *out_stream_class)
189 {
190 struct trace_ir_metadata_maps *md_maps;
191 const bt_clock_class *in_clock_class;
192 bt_clock_class *out_clock_class;
193 const bt_field_class *in_packet_context_fc, *in_common_context_fc;
194 bt_field_class *out_packet_context_fc, *out_common_context_fc;
195 const char *in_name;
196 int ret = 0;
197 bt_logging_level log_level = ir_maps->log_level;
198 bt_self_component *self_comp = ir_maps->self_comp;
199
200 BT_COMP_LOGD("Copying content of stream class: in-sc-addr=%p, out-sc-addr=%p",
201 in_stream_class, out_stream_class);
202
203 md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps, in_stream_class);
204 in_clock_class = bt_stream_class_borrow_default_clock_class_const(
205 in_stream_class);
206
207 if (in_clock_class) {
208 /* Copy the clock class. */
209 out_clock_class =
210 borrow_mapped_clock_class(md_maps, in_clock_class);
211 if (!out_clock_class) {
212 out_clock_class = create_new_mapped_clock_class(
213 ir_maps->self_comp, md_maps,
214 in_clock_class);
215 }
216 bt_stream_class_set_default_clock_class(out_stream_class,
217 out_clock_class);
218
219 }
220
221 /*
222 * Safe to use the same value object because it's frozen at this
223 * point.
224 */
225 bt_stream_class_set_user_attributes(out_stream_class,
226 bt_stream_class_borrow_user_attributes_const(in_stream_class));
227
228 bt_stream_class_set_supports_packets(
229 out_stream_class,
230 bt_stream_class_supports_packets(in_stream_class),
231 bt_stream_class_packets_have_beginning_default_clock_snapshot(
232 in_stream_class),
233 bt_stream_class_packets_have_end_default_clock_snapshot(
234 in_stream_class));
235 bt_stream_class_set_supports_discarded_events(
236 out_stream_class,
237 bt_stream_class_supports_discarded_events(in_stream_class),
238 bt_stream_class_discarded_events_have_default_clock_snapshots(
239 in_stream_class));
240 bt_stream_class_set_supports_discarded_packets(
241 out_stream_class,
242 bt_stream_class_supports_discarded_packets(in_stream_class),
243 bt_stream_class_discarded_packets_have_default_clock_snapshots(
244 in_stream_class));
245
246 in_name = bt_stream_class_get_name(in_stream_class);
247 if (in_name) {
248 if (bt_stream_class_set_name(out_stream_class, in_name) !=
249 BT_STREAM_CLASS_SET_NAME_STATUS_OK) {
250 BT_COMP_LOGE("Error set stream class name: out-sc-addr=%p, "
251 "name=%s", out_stream_class, in_name);
252 ret = -1;
253 goto error;
254 }
255 }
256
257 bt_stream_class_set_assigns_automatic_stream_id(out_stream_class,
258 BT_FALSE);
259 bt_stream_class_set_assigns_automatic_event_class_id(out_stream_class,
260 BT_FALSE);
261
262 /*
263 * Add the input packet context field class to the context to
264 * resolution in the further steps.
265 */
266 in_packet_context_fc =
267 bt_stream_class_borrow_packet_context_field_class_const(
268 in_stream_class);
269 md_maps->fc_resolving_ctx->packet_context =
270 in_packet_context_fc;
271
272 if (in_packet_context_fc) {
273 /* Copy packet context. */
274 out_packet_context_fc = create_field_class_copy(
275 md_maps, in_packet_context_fc);
276
277 ret = copy_field_class_content(md_maps,
278 in_packet_context_fc, out_packet_context_fc);
279 if (ret) {
280 ret = -1;
281 goto error;
282 }
283
284 if (bt_stream_class_set_packet_context_field_class(
285 out_stream_class, out_packet_context_fc) !=
286 BT_STREAM_CLASS_SET_FIELD_CLASS_STATUS_OK) {
287 BT_COMP_LOGE("Error setting stream class' packet context "
288 "field class: sc-addr=%p, packet-fc-addr=%p",
289 out_stream_class, out_packet_context_fc);
290 ret = -1;
291 goto error;
292 }
293 }
294
295 /*
296 * Add the input common context field class to the context to
297 * resolution in the further steps.
298 */
299 in_common_context_fc =
300 bt_stream_class_borrow_event_common_context_field_class_const(
301 in_stream_class);
302 md_maps->fc_resolving_ctx->event_common_context =
303 in_common_context_fc;
304
305 if (in_common_context_fc) {
306 /* Copy common context. */
307 /* TODO: I find it a bit awkward to have this special function
308 * here to add the debug-info field class. I would like to
309 * abstract that.*/
310 out_common_context_fc = create_field_class_copy(
311 md_maps, in_common_context_fc);
312
313 ret = copy_event_common_context_field_class_content(
314 md_maps, ir_maps->debug_info_field_class_name,
315 in_common_context_fc, out_common_context_fc);
316 if (ret) {
317 goto error;
318 }
319
320 if (bt_stream_class_set_event_common_context_field_class(
321 out_stream_class, out_common_context_fc) !=
322 BT_STREAM_CLASS_SET_FIELD_CLASS_STATUS_OK) {
323 BT_COMP_LOGE("Error setting stream class' packet context "
324 "field class: sc-addr=%p, packet-fc-addr=%p",
325 out_stream_class, out_common_context_fc);
326 ret = -1;
327 goto error;
328 }
329 }
330
331 /* Set packet snapshot boolean fields. */
332 BT_COMP_LOGD("Copied content of stream class: in-sc-addr=%p, out-sc-addr=%p",
333 in_stream_class, out_stream_class);
334 error:
335 return ret;
336 }
337
338 BT_HIDDEN
339 int copy_event_class_content(struct trace_ir_maps *ir_maps,
340 const bt_event_class *in_event_class,
341 bt_event_class *out_event_class)
342 {
343 struct trace_ir_metadata_maps *md_maps;
344 const char *in_event_class_name, *in_emf_uri;
345 bt_property_availability prop_avail;
346 bt_event_class_log_level ec_log_level;
347 bt_field_class *out_specific_context_fc, *out_payload_fc;
348 const bt_field_class *in_event_specific_context, *in_event_payload;
349 int ret = 0;
350 bt_logging_level log_level = ir_maps->log_level;
351 bt_self_component *self_comp = ir_maps->self_comp;
352
353 BT_COMP_LOGD("Copying content of event class: in-ec-addr=%p, out-ec-addr=%p",
354 in_event_class, out_event_class);
355
356 /* Copy event class name. */
357 in_event_class_name = bt_event_class_get_name(in_event_class);
358 if (in_event_class_name) {
359 if (bt_event_class_set_name(out_event_class,
360 in_event_class_name) !=
361 BT_EVENT_CLASS_SET_NAME_STATUS_OK) {
362 BT_COMP_LOGE("Error setting event class' name: ec-addr=%p, "
363 "name=%s", out_event_class, in_event_class_name);
364 ret = -1;
365 goto error;
366 }
367 }
368
369 /*
370 * Safe to use the same value object because it's frozen at this
371 * point.
372 */
373 bt_event_class_set_user_attributes(out_event_class,
374 bt_event_class_borrow_user_attributes_const(in_event_class));
375
376 /* Copy event class loglevel. */
377 prop_avail = bt_event_class_get_log_level(in_event_class,
378 &ec_log_level);
379 if (prop_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) {
380 bt_event_class_set_log_level(out_event_class,
381 ec_log_level);
382 }
383
384 /* Copy event class emf uri. */
385 in_emf_uri = bt_event_class_get_emf_uri(in_event_class);
386 if (in_emf_uri) {
387 if (bt_event_class_set_emf_uri(out_event_class, in_emf_uri) !=
388 BT_EVENT_CLASS_SET_EMF_URI_STATUS_OK) {
389 BT_COMP_LOGE("Error setting event class' emf uri: ec-addr=%p, "
390 "emf uri=%s", out_event_class, in_emf_uri);
391 ret = -1;
392 goto error;
393 }
394 }
395
396 md_maps = borrow_metadata_maps_from_input_event_class(ir_maps, in_event_class);
397 /*
398 * Add the input event class' specific ctx to te
399 * context.
400 */
401 in_event_specific_context =
402 bt_event_class_borrow_specific_context_field_class_const(
403 in_event_class);
404
405 md_maps->fc_resolving_ctx->event_specific_context =
406 in_event_specific_context;
407
408 if (in_event_specific_context) {
409 /* Copy the specific context of this event class. */
410 out_specific_context_fc = create_field_class_copy(md_maps,
411 in_event_specific_context);
412
413 ret = copy_field_class_content(md_maps,
414 in_event_specific_context, out_specific_context_fc);
415 if (ret) {
416 goto error;
417 }
418 /*
419 * Add the output specific context to the output event
420 * class.
421 */
422 if (bt_event_class_set_specific_context_field_class(
423 out_event_class, out_specific_context_fc) !=
424 BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK) {
425 BT_COMP_LOGE("Error setting event class' specific context "
426 "field class: ec-addr=%p, ctx-fc-addr=%p",
427 out_event_class, out_specific_context_fc);
428 ret = -1;
429 goto error;
430 }
431 }
432
433 /*
434 * Add the input event class' payload field class to
435 * the context.
436 */
437 in_event_payload = bt_event_class_borrow_payload_field_class_const(
438 in_event_class);
439
440 md_maps->fc_resolving_ctx->event_payload = in_event_payload;
441
442 if (in_event_payload) {
443 /* Copy the payload of this event class. */
444 out_payload_fc = create_field_class_copy(md_maps,
445 in_event_payload);
446 ret = copy_field_class_content(md_maps,
447 in_event_payload, out_payload_fc);
448 if (ret) {
449 goto error;
450 }
451
452 /* Add the output payload to the output event class. */
453 if (bt_event_class_set_payload_field_class(
454 out_event_class, out_payload_fc) !=
455 BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK) {
456 BT_COMP_LOGE("Error setting event class' payload "
457 "field class: ec-addr=%p, payload-fc-addr=%p",
458 out_event_class, out_payload_fc);
459 ret = -1;
460 goto error;
461 }
462 }
463
464 BT_COMP_LOGD("Copied content of event class: in-ec-addr=%p, out-ec-addr=%p",
465 in_event_class, out_event_class);
466 error:
467 return ret;
468 }
469
470 BT_HIDDEN
471 int copy_event_common_context_field_class_content(
472 struct trace_ir_metadata_maps *md_maps,
473 const char *debug_info_fc_name,
474 const bt_field_class *in_field_class,
475 bt_field_class *out_field_class)
476 {
477 bt_field_class *debug_field_class = NULL, *bin_field_class = NULL,
478 *func_field_class = NULL, *src_field_class = NULL;
479 int ret = 0;
480 bt_logging_level log_level = md_maps->log_level;
481 bt_self_component *self_comp = md_maps->self_comp;
482
483 BT_COMP_LOGD("Copying content of event common context field class: "
484 "in-fc-addr=%p, out-fc-addr=%p", in_field_class, out_field_class);
485
486 /* Copy the content of the input common context. */
487 ret = copy_field_class_content(md_maps, in_field_class, out_field_class);
488 if (ret) {
489 goto error;
490 }
491
492 /*
493 * If this event common context has the necessary fields to compute the
494 * debug information append the debug-info field class to the event
495 * common context.
496 */
497 if (is_event_common_ctx_dbg_info_compatible(in_field_class, debug_info_fc_name)) {
498 /*
499 * The struct field and 3 sub-fields are not stored in the
500 * field class map because they don't have input equivalent.
501 * We need to put our reference each of these field classes
502 * once they are added to their respective containing field
503 * classes.
504 */
505 debug_field_class = bt_field_class_structure_create(
506 md_maps->output_trace_class);
507 if (!debug_field_class) {
508 BT_COMP_LOGE_STR("Failed to create debug_info structure.");
509 ret = -1;
510 goto error;
511 }
512
513 bin_field_class = bt_field_class_string_create(
514 md_maps->output_trace_class);
515 if (!bin_field_class) {
516 BT_COMP_LOGE_STR("Failed to create string for field=bin.");
517 ret = -1;
518 goto error;
519 }
520
521 func_field_class = bt_field_class_string_create(
522 md_maps->output_trace_class);
523 if (!func_field_class) {
524 BT_COMP_LOGE_STR("Failed to create string for field=func.");
525 ret = -1;
526 goto error;
527 }
528
529 src_field_class = bt_field_class_string_create(
530 md_maps->output_trace_class);
531 if (!src_field_class) {
532 BT_COMP_LOGE_STR("Failed to create string for field=src.");
533 ret = -1;
534 goto error;
535 }
536
537 if (bt_field_class_structure_append_member(
538 debug_field_class, "bin", bin_field_class) !=
539 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK) {
540 BT_COMP_LOGE_STR("Failed to add a field to debug_info "
541 "struct: field=bin.");
542 ret = -1;
543 goto error;
544 }
545 BT_FIELD_CLASS_PUT_REF_AND_RESET(bin_field_class);
546
547 if (bt_field_class_structure_append_member(
548 debug_field_class, "func", func_field_class) !=
549 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK) {
550 BT_COMP_LOGE_STR("Failed to add a field to debug_info "
551 "struct: field=func.");
552 ret = -1;
553 goto error;
554 }
555 BT_FIELD_CLASS_PUT_REF_AND_RESET(func_field_class);
556
557 if (bt_field_class_structure_append_member(
558 debug_field_class, "src", src_field_class) !=
559 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK) {
560 BT_COMP_LOGE_STR("Failed to add a field to debug_info "
561 "struct: field=src.");
562 ret = -1;
563 goto error;
564 }
565 BT_FIELD_CLASS_PUT_REF_AND_RESET(src_field_class);
566
567 /*Add the filled debug-info field class to the common context. */
568 if (bt_field_class_structure_append_member(out_field_class,
569 debug_info_fc_name, debug_field_class) !=
570 BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK) {
571 BT_COMP_LOGE_STR("Failed to add debug_info field to "
572 "event common context.");
573 ret = -1;
574 goto error;
575 }
576 BT_FIELD_CLASS_PUT_REF_AND_RESET(debug_field_class);
577 }
578 BT_COMP_LOGD("Copied content of event common context field class: "
579 "in-fc-addr=%p, out-fc-addr=%p", in_field_class, out_field_class);
580 goto end;
581
582 error:
583 if (debug_field_class) {
584 bt_field_class_put_ref(debug_field_class);
585 }
586 if (bin_field_class) {
587 bt_field_class_put_ref(bin_field_class);
588 }
589 if (func_field_class) {
590 bt_field_class_put_ref(func_field_class);
591 }
592 if (src_field_class) {
593 bt_field_class_put_ref(src_field_class);
594 }
595 end:
596 return ret;
597 }
598
599 BT_HIDDEN
600 bt_field_class *create_field_class_copy(struct trace_ir_metadata_maps *md_maps,
601 const bt_field_class *in_field_class)
602 {
603 return create_field_class_copy_internal(md_maps, in_field_class);
604 }
605
606 BT_HIDDEN
607 int copy_field_class_content(struct trace_ir_metadata_maps *md_maps,
608 const bt_field_class *in_field_class,
609 bt_field_class *out_field_class)
610 {
611 return copy_field_class_content_internal(md_maps, in_field_class,
612 out_field_class);
613 }
This page took 0.043132 seconds and 5 git commands to generate.