Logging: standardize logging tags
[babeltrace.git] / src / plugins / lttng-utils / debug-info / trace-ir-metadata-copy.c
CommitLineData
ca9f27f3
FD
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
350ad6c1 27#define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/TRACE-IR-META-COPY"
ca9f27f3
FD
28#include "logging.h"
29
30#include <inttypes.h>
31#include <stdint.h>
32
578e048b 33#include "common/assert.h"
ca9f27f3
FD
34
35#include "trace-ir-metadata-copy.h"
36#include "trace-ir-metadata-field-class-copy.h"
37#include "utils.h"
38
39BT_HIDDEN
40int copy_trace_class_content(const bt_trace_class *in_trace_class,
41 bt_trace_class *out_trace_class)
42{
43 int ret = 0;
44 uint64_t i, env_field_count;
45 const char *in_trace_class_name;
ca9f27f3
FD
46
47 BT_LOGD("Copying content of trace class: in-tc-addr=%p, out-tc-addr=%p",
48 in_trace_class, out_trace_class);
49
50 /* Use the same stream class ids as in the origin trace class. */
51 bt_trace_class_set_assigns_automatic_stream_class_id(out_trace_class,
52 BT_FALSE);
53
54 in_trace_class_name = bt_trace_class_get_name(in_trace_class);
55 if (in_trace_class_name) {
56 bt_trace_class_set_name(out_trace_class, in_trace_class_name);
57 }
58
2d836ebb
FD
59 /*
60 * Do not copy the trace class UUID as it may be modified and should no
61 * longer have the same UUID.
62 */
ca9f27f3
FD
63
64 /*
65 * Go over all the entries in the environment section of the trace class
66 * and copy the content to the new trace class.
67 */
68 env_field_count = bt_trace_class_get_environment_entry_count(in_trace_class);
69 for (i = 0; i < env_field_count; i++) {
70 const char *value_name;
71 const bt_value *value = NULL;
72 bt_trace_class_status trace_class_status;
73
74 bt_trace_class_borrow_environment_entry_by_index_const(
75 in_trace_class, i, &value_name, &value);
76
77 BT_LOGD("Copying trace class environnement entry: "
78 "index=%" PRId64 ", value-addr=%p, value-name=%s",
79 i, value, value_name);
80
81 BT_ASSERT(value_name);
82 BT_ASSERT(value);
83
fdd3a2da 84 if (bt_value_is_signed_integer(value)) {
ca9f27f3
FD
85 trace_class_status =
86 bt_trace_class_set_environment_entry_integer(
87 out_trace_class, value_name,
fdd3a2da
PP
88 bt_value_signed_integer_get(
89 value));
ca9f27f3
FD
90 } else if (bt_value_is_string(value)) {
91 trace_class_status =
92 bt_trace_class_set_environment_entry_string(
93 out_trace_class, value_name,
94 bt_value_string_get(value));
95 } else {
96 abort();
97 }
98
99 if (trace_class_status != BT_TRACE_CLASS_STATUS_OK) {
100 ret = -1;
101 goto error;
102 }
103 }
104
105 BT_LOGD("Copied content of trace class: in-tc-addr=%p, out-tc-addr=%p",
106 in_trace_class, out_trace_class);
107error:
108 return ret;
109}
110
111static
112int copy_clock_class_content(const bt_clock_class *in_clock_class,
113 bt_clock_class *out_clock_class)
114{
115 bt_clock_class_status status;
116 const char *clock_class_name, *clock_class_description;
117 int64_t seconds;
118 uint64_t cycles;
119 bt_uuid in_uuid;
120 int ret = 0;
121
122 BT_LOGD("Copying content of clock class: in-cc-addr=%p, out-cc-addr=%p",
123 in_clock_class, out_clock_class);
124
125 clock_class_name = bt_clock_class_get_name(in_clock_class);
126
127 if (clock_class_name) {
128 status = bt_clock_class_set_name(out_clock_class, clock_class_name);
129 if (status != BT_CLOCK_CLASS_STATUS_OK) {
130 BT_LOGE("Error setting clock class' name cc-addr=%p, name=%p",
131 out_clock_class, clock_class_name);
132 out_clock_class = NULL;
133 ret = -1;
134 goto error;
135 }
136 }
137
138 clock_class_description = bt_clock_class_get_description(in_clock_class);
139
140 if (clock_class_description) {
141 status = bt_clock_class_set_description(out_clock_class,
142 clock_class_description);
143 if (status != BT_CLOCK_CLASS_STATUS_OK) {
144 BT_LOGE("Error setting clock class' description cc-addr=%p, "
145 "name=%p", out_clock_class, clock_class_description);
146 out_clock_class = NULL;
147 ret = -1;
148 goto error;
149 }
150 }
151
152 in_uuid = bt_clock_class_get_uuid(in_clock_class);
153 if (in_uuid) {
154 bt_clock_class_set_uuid(out_clock_class, in_uuid);
155 }
156
157 bt_clock_class_set_frequency(out_clock_class,
158 bt_clock_class_get_frequency(in_clock_class));
159 bt_clock_class_set_precision(out_clock_class,
160 bt_clock_class_get_precision(in_clock_class));
161 bt_clock_class_get_offset(in_clock_class, &seconds, &cycles);
162 bt_clock_class_set_offset(out_clock_class, seconds, cycles);
163 bt_clock_class_set_origin_is_unix_epoch(out_clock_class,
164 bt_clock_class_origin_is_unix_epoch(in_clock_class));
165
166 BT_LOGD("Copied content of clock class: in-cc-addr=%p, out-cc-addr=%p",
167 in_clock_class, out_clock_class);
168
169error:
170 return ret;
171}
172
173static
174bt_clock_class *borrow_mapped_clock_class(
175 struct trace_ir_metadata_maps *md_maps,
176 const bt_clock_class *in_clock_class)
177{
178 BT_ASSERT(md_maps);
179 BT_ASSERT(in_clock_class);
180
181 return g_hash_table_lookup(md_maps->clock_class_map,
182 (gpointer) in_clock_class);
183}
184
185static
186bt_clock_class *create_new_mapped_clock_class(
187 bt_self_component *self_comp,
188 struct trace_ir_metadata_maps *md_maps,
189 const bt_clock_class *in_clock_class)
190{
191 bt_clock_class *out_clock_class;
192 int ret;
193
194 BT_LOGD("Creating new mapped clock class: in-cc-addr=%p",
195 in_clock_class);
196
197 BT_ASSERT(md_maps);
198 BT_ASSERT(in_clock_class);
199
200 BT_ASSERT(!borrow_mapped_clock_class(md_maps, in_clock_class));
201
202 out_clock_class = bt_clock_class_create(self_comp);
203 if (!out_clock_class) {
204 BT_LOGE_STR("Cannot create clock class");
205 goto end;
206 }
207 /* If not, create a new one and add it to the mapping. */
208 ret = copy_clock_class_content(in_clock_class, out_clock_class);
209 if (ret) {
210 BT_LOGE_STR("Cannot copy clock class");
211 goto end;
212 }
213
214 g_hash_table_insert(md_maps->clock_class_map,
215 (gpointer) in_clock_class, out_clock_class);
216
217 BT_LOGD("Created new mapped clock class: in-cc-addr=%p, out-cc-addr=%p",
218 in_clock_class, out_clock_class);
219end:
220 return out_clock_class;
221}
222
223BT_HIDDEN
224int copy_stream_class_content(struct trace_ir_maps *ir_maps,
225 const bt_stream_class *in_stream_class,
226 bt_stream_class *out_stream_class)
227{
228 struct trace_ir_metadata_maps *md_maps;
229 const bt_clock_class *in_clock_class;
230 bt_clock_class *out_clock_class;
231 const bt_field_class *in_packet_context_fc, *in_common_context_fc;
232 bt_field_class *out_packet_context_fc, *out_common_context_fc;
233 bt_stream_class_status status;
234 const char *in_name;
235 int ret = 0;
236
237 BT_LOGD("Copying content of stream class: in-sc-addr=%p, out-sc-addr=%p",
238 in_stream_class, out_stream_class);
239
240 md_maps = borrow_metadata_maps_from_input_stream_class(ir_maps, in_stream_class);
241 in_clock_class = bt_stream_class_borrow_default_clock_class_const(
242 in_stream_class);
243
244 if (in_clock_class) {
245 /* Copy the clock class. */
246 out_clock_class =
247 borrow_mapped_clock_class(md_maps, in_clock_class);
248 if (!out_clock_class) {
249 out_clock_class = create_new_mapped_clock_class(
250 ir_maps->self_comp, md_maps,
251 in_clock_class);
252 }
253 bt_stream_class_set_default_clock_class(out_stream_class,
254 out_clock_class);
255
256 }
257
9b24b6aa 258 bt_stream_class_set_packets_have_beginning_default_clock_snapshot(
649934d2 259 out_stream_class,
9b24b6aa 260 bt_stream_class_packets_have_beginning_default_clock_snapshot(
649934d2 261 in_stream_class));
9b24b6aa 262 bt_stream_class_set_packets_have_end_default_clock_snapshot(
649934d2 263 out_stream_class,
9b24b6aa 264 bt_stream_class_packets_have_end_default_clock_snapshot(
649934d2 265 in_stream_class));
2e90378a
PP
266 bt_stream_class_set_supports_discarded_events(
267 out_stream_class,
268 bt_stream_class_supports_discarded_events(in_stream_class),
269 bt_stream_class_discarded_events_have_default_clock_snapshots(
270 in_stream_class));
271 bt_stream_class_set_supports_discarded_packets(
272 out_stream_class,
273 bt_stream_class_supports_discarded_packets(in_stream_class),
274 bt_stream_class_discarded_packets_have_default_clock_snapshots(
275 in_stream_class));
649934d2 276
ca9f27f3
FD
277 in_name = bt_stream_class_get_name(in_stream_class);
278 if (in_name) {
279 status = bt_stream_class_set_name(out_stream_class, in_name);
280 if (status != BT_STREAM_CLASS_STATUS_OK) {
281 BT_LOGE("Error set stream class name: out-sc-addr=%p, "
282 "name=%s", out_stream_class, in_name);
283 ret = -1;
284 goto error;
285 }
286 }
287
288 bt_stream_class_set_assigns_automatic_stream_id(out_stream_class,
289 BT_FALSE);
290 bt_stream_class_set_assigns_automatic_event_class_id(out_stream_class,
291 BT_FALSE);
292
293 /*
294 * Add the input packet context field class to the context to
295 * resolution in the further steps.
296 */
297 in_packet_context_fc =
298 bt_stream_class_borrow_packet_context_field_class_const(
299 in_stream_class);
300 md_maps->fc_resolving_ctx->packet_context =
301 in_packet_context_fc;
302
303 if (in_packet_context_fc) {
304 /* Copy packet context. */
305 out_packet_context_fc = create_field_class_copy(
306 md_maps, in_packet_context_fc);
307
308 ret = copy_field_class_content(md_maps,
309 in_packet_context_fc, out_packet_context_fc);
310 if (ret) {
311 ret = -1;
312 goto error;
313 }
314
315 status = bt_stream_class_set_packet_context_field_class(
316 out_stream_class, out_packet_context_fc);
317 if (status != BT_STREAM_CLASS_STATUS_OK) {
318 BT_LOGE("Error setting stream class' packet context "
319 "field class: sc-addr=%p, packet-fc-addr=%p",
320 out_stream_class, out_packet_context_fc);
321 ret = -1;
322 goto error;
323 }
324 }
325
326 /*
327 * Add the input common context field class to the context to
328 * resolution in the further steps.
329 */
330 in_common_context_fc =
331 bt_stream_class_borrow_event_common_context_field_class_const(
332 in_stream_class);
333 md_maps->fc_resolving_ctx->event_common_context =
334 in_common_context_fc;
335
336 if (in_common_context_fc) {
337 /* Copy common context. */
338 /* TODO: I find it a bit awkward to have this special function
339 * here to add the debug-info field class. I would like to
340 * abstract that.*/
341 out_common_context_fc = create_field_class_copy(
342 md_maps, in_common_context_fc);
343
344 ret = copy_event_common_context_field_class_content(
345 md_maps, ir_maps->debug_info_field_class_name,
346 in_common_context_fc, out_common_context_fc);
347 if (ret) {
348 goto error;
349 }
350
351 status = bt_stream_class_set_event_common_context_field_class(
352 out_stream_class, out_common_context_fc);
353 if (status != BT_STREAM_CLASS_STATUS_OK) {
354 BT_LOGE("Error setting stream class' packet context "
355 "field class: sc-addr=%p, packet-fc-addr=%p",
356 out_stream_class, out_common_context_fc);
357 ret = -1;
358 goto error;
359 }
360 }
361
362 /* Set packet snapshot boolean fields. */
363 BT_LOGD("Copied content of stream class: in-sc-addr=%p, out-sc-addr=%p",
364 in_stream_class, out_stream_class);
365error:
366 return ret;
367}
368
369BT_HIDDEN
370int copy_event_class_content(struct trace_ir_maps *ir_maps,
371 const bt_event_class *in_event_class,
372 bt_event_class *out_event_class)
373{
374 struct trace_ir_metadata_maps *md_maps;
375 const char *in_event_class_name, *in_emf_uri;
376 bt_property_availability prop_avail;
377 bt_event_class_log_level log_level;
378 bt_event_class_status status;
379 bt_field_class *out_specific_context_fc, *out_payload_fc;
380 const bt_field_class *in_event_specific_context, *in_event_payload;
381 int ret = 0;
382
383 BT_LOGD("Copying content of event class: in-ec-addr=%p, out-ec-addr=%p",
384 in_event_class, out_event_class);
385
386 /* Copy event class name. */
387 in_event_class_name = bt_event_class_get_name(in_event_class);
388 if (in_event_class_name) {
389 status = bt_event_class_set_name(out_event_class, in_event_class_name);
390 if (status != BT_EVENT_CLASS_STATUS_OK) {
391 BT_LOGE("Error setting event class' name: ec-addr=%p, "
392 "name=%s", out_event_class, in_event_class_name);
393 ret = -1;
394 goto error;
395 }
396 }
397
398 /* Copy event class loglevel. */
399 prop_avail = bt_event_class_get_log_level(in_event_class, &log_level);
400 if (prop_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) {
401 bt_event_class_set_log_level(out_event_class,
402 log_level);
403 }
404
405 /* Copy event class emf uri. */
406 in_emf_uri = bt_event_class_get_emf_uri(in_event_class);
407 if (in_emf_uri) {
408 status = bt_event_class_set_emf_uri(out_event_class, in_emf_uri);
409 if (status != BT_EVENT_CLASS_STATUS_OK) {
410 BT_LOGE("Error setting event class' emf uri: ec-addr=%p, "
411 "emf uri=%s", out_event_class, in_emf_uri);
412 ret = -1;
413 goto error;
414 }
415 }
416
417 md_maps = borrow_metadata_maps_from_input_event_class(ir_maps, in_event_class);
418 /*
419 * Add the input event class' specific ctx to te
420 * context.
421 */
422 in_event_specific_context =
423 bt_event_class_borrow_specific_context_field_class_const(
424 in_event_class);
425
426 md_maps->fc_resolving_ctx->event_specific_context =
427 in_event_specific_context;
428
429 if (in_event_specific_context) {
430 /* Copy the specific context of this event class. */
431 out_specific_context_fc = create_field_class_copy(md_maps,
432 in_event_specific_context);
433
5e1abef6 434 ret = copy_field_class_content(md_maps,
ca9f27f3
FD
435 in_event_specific_context, out_specific_context_fc);
436 if (ret) {
437 goto error;
438 }
439 /*
440 * Add the output specific context to the output event
441 * class.
442 */
443 status = bt_event_class_set_specific_context_field_class(
444 out_event_class, out_specific_context_fc);
445 if (status != BT_EVENT_CLASS_STATUS_OK) {
446 BT_LOGE("Error setting event class' specific context "
447 "field class: ec-addr=%p, ctx-fc-addr=%p",
448 out_event_class, out_specific_context_fc);
449 ret = -1;
450 goto error;
451 }
452 }
453
454 /*
455 * Add the input event class' payload field class to
456 * the context.
457 */
458 in_event_payload = bt_event_class_borrow_payload_field_class_const(
459 in_event_class);
460
461 md_maps->fc_resolving_ctx->event_payload = in_event_payload;
462
463 if (in_event_payload) {
464 /* Copy the payload of this event class. */
465 out_payload_fc = create_field_class_copy(md_maps,
466 in_event_payload);
5e1abef6 467 ret = copy_field_class_content(md_maps,
ca9f27f3
FD
468 in_event_payload, out_payload_fc);
469 if (ret) {
470 goto error;
471 }
472
473 /* Add the output payload to the output event class. */
474 status = bt_event_class_set_payload_field_class(
475 out_event_class, out_payload_fc);
476 if (status != BT_EVENT_CLASS_STATUS_OK) {
477 BT_LOGE("Error setting event class' payload "
478 "field class: ec-addr=%p, payload-fc-addr=%p",
479 out_event_class, out_payload_fc);
480 ret = -1;
481 goto error;
482 }
483 }
484
485 BT_LOGD("Copied content of event class: in-ec-addr=%p, out-ec-addr=%p",
486 in_event_class, out_event_class);
487error:
488 return ret;
489}
490
491BT_HIDDEN
492int copy_event_common_context_field_class_content(
493 struct trace_ir_metadata_maps *md_maps,
494 const char *debug_info_fc_name,
495 const bt_field_class *in_field_class,
496 bt_field_class *out_field_class)
497{
498 bt_field_class_status status;
499 bt_field_class *debug_field_class = NULL, *bin_field_class = NULL,
500 *func_field_class = NULL, *src_field_class = NULL;
501 int ret = 0;
502
503 BT_LOGD("Copying content of event common context field class: "
504 "in-fc-addr=%p, out-fc-addr=%p", in_field_class, out_field_class);
505
506 /* Copy the content of the input common context. */
507 ret = copy_field_class_content(md_maps, in_field_class, out_field_class);
508 if (ret) {
509 goto error;
510 }
511
512 /*
513 * If this event common context has the necessary fields to compute the
514 * debug information append the debug-info field class to the event
515 * common context.
516 */
517 if (is_event_common_ctx_dbg_info_compatible(in_field_class, debug_info_fc_name)) {
518 /*
519 * The struct field and 3 sub-fields are not stored in the
520 * field class map because they don't have input equivalent.
521 * We need to put our reference each of these field classes
522 * once they are added to their respective containing field
523 * classes.
524 */
525 debug_field_class = bt_field_class_structure_create(
526 md_maps->output_trace_class);
527 if (!debug_field_class) {
528 BT_LOGE_STR("Failed to create debug_info structure.");
529 ret = -1;
530 goto error;
531 }
532
533 bin_field_class = bt_field_class_string_create(
534 md_maps->output_trace_class);
535 if (!bin_field_class) {
536 BT_LOGE_STR("Failed to create string for field=bin.");
537 ret = -1;
538 goto error;
539 }
540
541 func_field_class = bt_field_class_string_create(
542 md_maps->output_trace_class);
543 if (!func_field_class) {
544 BT_LOGE_STR("Failed to create string for field=func.");
545 ret = -1;
546 goto error;
547 }
548
549 src_field_class = bt_field_class_string_create(
550 md_maps->output_trace_class);
551 if (!src_field_class) {
552 BT_LOGE_STR("Failed to create string for field=src.");
553 ret = -1;
554 goto error;
555 }
556
557 status = bt_field_class_structure_append_member(
558 debug_field_class, "bin", bin_field_class);
559 if (status != BT_FIELD_CLASS_STATUS_OK) {
560 BT_LOGE_STR("Failed to add a field to debug_info "
561 "struct: field=bin.");
562 ret = -1;
563 goto error;
564 }
565 BT_FIELD_CLASS_PUT_REF_AND_RESET(bin_field_class);
566
567 status = bt_field_class_structure_append_member(
568 debug_field_class, "func", func_field_class);
569 if (status != BT_FIELD_CLASS_STATUS_OK) {
570 BT_LOGE_STR("Failed to add a field to debug_info "
571 "struct: field=func.");
572 ret = -1;
573 goto error;
574 }
575 BT_FIELD_CLASS_PUT_REF_AND_RESET(func_field_class);
576
577 status = bt_field_class_structure_append_member(
578 debug_field_class, "src", src_field_class);
579 if (status != BT_FIELD_CLASS_STATUS_OK) {
580 BT_LOGE_STR("Failed to add a field to debug_info "
581 "struct: field=src.");
582 ret = -1;
583 goto error;
584 }
585 BT_FIELD_CLASS_PUT_REF_AND_RESET(src_field_class);
586
587 /*Add the filled debug-info field class to the common context. */
588 status = bt_field_class_structure_append_member(out_field_class,
589 debug_info_fc_name,
590 debug_field_class);
591 if (status != BT_FIELD_CLASS_STATUS_OK) {
592 BT_LOGE_STR("Failed to add debug_info field to "
593 "event common context.");
594 ret = -1;
595 goto error;
596 }
597 BT_FIELD_CLASS_PUT_REF_AND_RESET(debug_field_class);
598 }
599 BT_LOGD("Copied content of event common context field class: "
600 "in-fc-addr=%p, out-fc-addr=%p", in_field_class, out_field_class);
601 goto end;
602
603error:
604 if (debug_field_class) {
605 bt_field_class_put_ref(debug_field_class);
606 }
607 if (bin_field_class) {
608 bt_field_class_put_ref(bin_field_class);
609 }
610 if (func_field_class) {
611 bt_field_class_put_ref(func_field_class);
612 }
613 if (src_field_class) {
614 bt_field_class_put_ref(src_field_class);
615 }
616end:
617 return ret;
618}
619
620BT_HIDDEN
621bt_field_class *create_field_class_copy(struct trace_ir_metadata_maps *md_maps,
622 const bt_field_class *in_field_class)
623{
624 return create_field_class_copy_internal(md_maps, in_field_class);
625}
626
627BT_HIDDEN
628int copy_field_class_content(struct trace_ir_metadata_maps *md_maps,
629 const bt_field_class *in_field_class,
630 bt_field_class *out_field_class)
631{
632 return copy_field_class_content_internal(md_maps, in_field_class,
633 out_field_class);
634}
This page took 0.050788 seconds and 4 git commands to generate.