Fix: flt.lttng-utils.debug-info: Error in src line reporting
[babeltrace.git] / plugins / lttng-utils / 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_LOG_TAG "PLUGIN-LTTNG-UTILS-DEBUG-INFO-TRACE-IR-METADATA-COPY"
28 #include "logging.h"
29
30 #include <inttypes.h>
31 #include <stdint.h>
32
33 #include <babeltrace/assert-internal.h>
34
35 #include "trace-ir-metadata-copy.h"
36 #include "trace-ir-metadata-field-class-copy.h"
37 #include "utils.h"
38
39 BT_HIDDEN
40 int 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;
46 bt_uuid in_uuid;
47
48 BT_LOGD("Copying content of trace class: in-tc-addr=%p, out-tc-addr=%p",
49 in_trace_class, out_trace_class);
50
51 /* Use the same stream class ids as in the origin trace class. */
52 bt_trace_class_set_assigns_automatic_stream_class_id(out_trace_class,
53 BT_FALSE);
54
55 in_trace_class_name = bt_trace_class_get_name(in_trace_class);
56 if (in_trace_class_name) {
57 bt_trace_class_set_name(out_trace_class, in_trace_class_name);
58 }
59
60 in_uuid = bt_trace_class_get_uuid(in_trace_class);
61 if (in_uuid) {
62 bt_trace_class_set_uuid(out_trace_class, in_uuid);
63 }
64
65 /*
66 * Go over all the entries in the environment section of the trace class
67 * and copy the content to the new trace class.
68 */
69 env_field_count = bt_trace_class_get_environment_entry_count(in_trace_class);
70 for (i = 0; i < env_field_count; i++) {
71 const char *value_name;
72 const bt_value *value = NULL;
73 bt_trace_class_status trace_class_status;
74
75 bt_trace_class_borrow_environment_entry_by_index_const(
76 in_trace_class, i, &value_name, &value);
77
78 BT_LOGD("Copying trace class environnement entry: "
79 "index=%" PRId64 ", value-addr=%p, value-name=%s",
80 i, value, value_name);
81
82 BT_ASSERT(value_name);
83 BT_ASSERT(value);
84
85 if (bt_value_is_integer(value)) {
86 trace_class_status =
87 bt_trace_class_set_environment_entry_integer(
88 out_trace_class, value_name,
89 bt_value_integer_get(value));
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);
107 error:
108 return ret;
109 }
110
111 static
112 int 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
169 error:
170 return ret;
171 }
172
173 static
174 bt_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
185 static
186 bt_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);
219 end:
220 return out_clock_class;
221 }
222
223 BT_HIDDEN
224 int 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
258 in_name = bt_stream_class_get_name(in_stream_class);
259 if (in_name) {
260 status = bt_stream_class_set_name(out_stream_class, in_name);
261 if (status != BT_STREAM_CLASS_STATUS_OK) {
262 BT_LOGE("Error set stream class name: out-sc-addr=%p, "
263 "name=%s", out_stream_class, in_name);
264 ret = -1;
265 goto error;
266 }
267 }
268
269 bt_stream_class_set_assigns_automatic_stream_id(out_stream_class,
270 BT_FALSE);
271 bt_stream_class_set_assigns_automatic_event_class_id(out_stream_class,
272 BT_FALSE);
273
274 /*
275 * Add the input packet context field class to the context to
276 * resolution in the further steps.
277 */
278 in_packet_context_fc =
279 bt_stream_class_borrow_packet_context_field_class_const(
280 in_stream_class);
281 md_maps->fc_resolving_ctx->packet_context =
282 in_packet_context_fc;
283
284 if (in_packet_context_fc) {
285 /* Copy packet context. */
286 out_packet_context_fc = create_field_class_copy(
287 md_maps, in_packet_context_fc);
288
289 ret = copy_field_class_content(md_maps,
290 in_packet_context_fc, out_packet_context_fc);
291 if (ret) {
292 ret = -1;
293 goto error;
294 }
295
296 status = bt_stream_class_set_packet_context_field_class(
297 out_stream_class, out_packet_context_fc);
298 if (status != BT_STREAM_CLASS_STATUS_OK) {
299 BT_LOGE("Error setting stream class' packet context "
300 "field class: sc-addr=%p, packet-fc-addr=%p",
301 out_stream_class, out_packet_context_fc);
302 ret = -1;
303 goto error;
304 }
305 }
306
307 /*
308 * Add the input common context field class to the context to
309 * resolution in the further steps.
310 */
311 in_common_context_fc =
312 bt_stream_class_borrow_event_common_context_field_class_const(
313 in_stream_class);
314 md_maps->fc_resolving_ctx->event_common_context =
315 in_common_context_fc;
316
317 if (in_common_context_fc) {
318 /* Copy common context. */
319 /* TODO: I find it a bit awkward to have this special function
320 * here to add the debug-info field class. I would like to
321 * abstract that.*/
322 out_common_context_fc = create_field_class_copy(
323 md_maps, in_common_context_fc);
324
325 ret = copy_event_common_context_field_class_content(
326 md_maps, ir_maps->debug_info_field_class_name,
327 in_common_context_fc, out_common_context_fc);
328 if (ret) {
329 goto error;
330 }
331
332 status = bt_stream_class_set_event_common_context_field_class(
333 out_stream_class, out_common_context_fc);
334 if (status != BT_STREAM_CLASS_STATUS_OK) {
335 BT_LOGE("Error setting stream class' packet context "
336 "field class: sc-addr=%p, packet-fc-addr=%p",
337 out_stream_class, out_common_context_fc);
338 ret = -1;
339 goto error;
340 }
341 }
342
343 /* Set packet snapshot boolean fields. */
344 BT_LOGD("Copied content of stream class: in-sc-addr=%p, out-sc-addr=%p",
345 in_stream_class, out_stream_class);
346 error:
347 return ret;
348 }
349
350 BT_HIDDEN
351 int copy_event_class_content(struct trace_ir_maps *ir_maps,
352 const bt_event_class *in_event_class,
353 bt_event_class *out_event_class)
354 {
355 struct trace_ir_metadata_maps *md_maps;
356 const char *in_event_class_name, *in_emf_uri;
357 bt_property_availability prop_avail;
358 bt_event_class_log_level log_level;
359 bt_event_class_status status;
360 bt_field_class *out_specific_context_fc, *out_payload_fc;
361 const bt_field_class *in_event_specific_context, *in_event_payload;
362 int ret = 0;
363
364 BT_LOGD("Copying content of event class: in-ec-addr=%p, out-ec-addr=%p",
365 in_event_class, out_event_class);
366
367 /* Copy event class name. */
368 in_event_class_name = bt_event_class_get_name(in_event_class);
369 if (in_event_class_name) {
370 status = bt_event_class_set_name(out_event_class, in_event_class_name);
371 if (status != BT_EVENT_CLASS_STATUS_OK) {
372 BT_LOGE("Error setting event class' name: ec-addr=%p, "
373 "name=%s", out_event_class, in_event_class_name);
374 ret = -1;
375 goto error;
376 }
377 }
378
379 /* Copy event class loglevel. */
380 prop_avail = bt_event_class_get_log_level(in_event_class, &log_level);
381 if (prop_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) {
382 bt_event_class_set_log_level(out_event_class,
383 log_level);
384 }
385
386 /* Copy event class emf uri. */
387 in_emf_uri = bt_event_class_get_emf_uri(in_event_class);
388 if (in_emf_uri) {
389 status = bt_event_class_set_emf_uri(out_event_class, in_emf_uri);
390 if (status != BT_EVENT_CLASS_STATUS_OK) {
391 BT_LOGE("Error setting event class' emf uri: ec-addr=%p, "
392 "emf uri=%s", out_event_class, in_emf_uri);
393 ret = -1;
394 goto error;
395 }
396 }
397
398 md_maps = borrow_metadata_maps_from_input_event_class(ir_maps, in_event_class);
399 /*
400 * Add the input event class' specific ctx to te
401 * context.
402 */
403 in_event_specific_context =
404 bt_event_class_borrow_specific_context_field_class_const(
405 in_event_class);
406
407 md_maps->fc_resolving_ctx->event_specific_context =
408 in_event_specific_context;
409
410 if (in_event_specific_context) {
411 /* Copy the specific context of this event class. */
412 out_specific_context_fc = create_field_class_copy(md_maps,
413 in_event_specific_context);
414
415 copy_field_class_content(md_maps,
416 in_event_specific_context, out_specific_context_fc);
417 if (ret) {
418 goto error;
419 }
420 /*
421 * Add the output specific context to the output event
422 * class.
423 */
424 status = bt_event_class_set_specific_context_field_class(
425 out_event_class, out_specific_context_fc);
426 if (status != BT_EVENT_CLASS_STATUS_OK) {
427 BT_LOGE("Error setting event class' specific context "
428 "field class: ec-addr=%p, ctx-fc-addr=%p",
429 out_event_class, out_specific_context_fc);
430 ret = -1;
431 goto error;
432 }
433 }
434
435 /*
436 * Add the input event class' payload field class to
437 * the context.
438 */
439 in_event_payload = bt_event_class_borrow_payload_field_class_const(
440 in_event_class);
441
442 md_maps->fc_resolving_ctx->event_payload = in_event_payload;
443
444 if (in_event_payload) {
445 /* Copy the payload of this event class. */
446 out_payload_fc = create_field_class_copy(md_maps,
447 in_event_payload);
448 copy_field_class_content(md_maps,
449 in_event_payload, out_payload_fc);
450 if (ret) {
451 goto error;
452 }
453
454 /* Add the output payload to the output event class. */
455 status = bt_event_class_set_payload_field_class(
456 out_event_class, out_payload_fc);
457 if (status != BT_EVENT_CLASS_STATUS_OK) {
458 BT_LOGE("Error setting event class' payload "
459 "field class: ec-addr=%p, payload-fc-addr=%p",
460 out_event_class, out_payload_fc);
461 ret = -1;
462 goto error;
463 }
464 }
465
466 BT_LOGD("Copied content of event class: in-ec-addr=%p, out-ec-addr=%p",
467 in_event_class, out_event_class);
468 error:
469 return ret;
470 }
471
472 BT_HIDDEN
473 int copy_event_common_context_field_class_content(
474 struct trace_ir_metadata_maps *md_maps,
475 const char *debug_info_fc_name,
476 const bt_field_class *in_field_class,
477 bt_field_class *out_field_class)
478 {
479 bt_field_class_status status;
480 bt_field_class *debug_field_class = NULL, *bin_field_class = NULL,
481 *func_field_class = NULL, *src_field_class = NULL;
482 int ret = 0;
483
484 BT_LOGD("Copying content of event common context field class: "
485 "in-fc-addr=%p, out-fc-addr=%p", in_field_class, out_field_class);
486
487 /* Copy the content of the input common context. */
488 ret = copy_field_class_content(md_maps, in_field_class, out_field_class);
489 if (ret) {
490 goto error;
491 }
492
493 /*
494 * If this event common context has the necessary fields to compute the
495 * debug information append the debug-info field class to the event
496 * common context.
497 */
498 if (is_event_common_ctx_dbg_info_compatible(in_field_class, debug_info_fc_name)) {
499 /*
500 * The struct field and 3 sub-fields are not stored in the
501 * field class map because they don't have input equivalent.
502 * We need to put our reference each of these field classes
503 * once they are added to their respective containing field
504 * classes.
505 */
506 debug_field_class = bt_field_class_structure_create(
507 md_maps->output_trace_class);
508 if (!debug_field_class) {
509 BT_LOGE_STR("Failed to create debug_info structure.");
510 ret = -1;
511 goto error;
512 }
513
514 bin_field_class = bt_field_class_string_create(
515 md_maps->output_trace_class);
516 if (!bin_field_class) {
517 BT_LOGE_STR("Failed to create string for field=bin.");
518 ret = -1;
519 goto error;
520 }
521
522 func_field_class = bt_field_class_string_create(
523 md_maps->output_trace_class);
524 if (!func_field_class) {
525 BT_LOGE_STR("Failed to create string for field=func.");
526 ret = -1;
527 goto error;
528 }
529
530 src_field_class = bt_field_class_string_create(
531 md_maps->output_trace_class);
532 if (!src_field_class) {
533 BT_LOGE_STR("Failed to create string for field=src.");
534 ret = -1;
535 goto error;
536 }
537
538 status = bt_field_class_structure_append_member(
539 debug_field_class, "bin", bin_field_class);
540 if (status != BT_FIELD_CLASS_STATUS_OK) {
541 BT_LOGE_STR("Failed to add a field to debug_info "
542 "struct: field=bin.");
543 ret = -1;
544 goto error;
545 }
546 BT_FIELD_CLASS_PUT_REF_AND_RESET(bin_field_class);
547
548 status = bt_field_class_structure_append_member(
549 debug_field_class, "func", func_field_class);
550 if (status != BT_FIELD_CLASS_STATUS_OK) {
551 BT_LOGE_STR("Failed to add a field to debug_info "
552 "struct: field=func.");
553 ret = -1;
554 goto error;
555 }
556 BT_FIELD_CLASS_PUT_REF_AND_RESET(func_field_class);
557
558 status = bt_field_class_structure_append_member(
559 debug_field_class, "src", src_field_class);
560 if (status != BT_FIELD_CLASS_STATUS_OK) {
561 BT_LOGE_STR("Failed to add a field to debug_info "
562 "struct: field=src.");
563 ret = -1;
564 goto error;
565 }
566 BT_FIELD_CLASS_PUT_REF_AND_RESET(src_field_class);
567
568 /*Add the filled debug-info field class to the common context. */
569 status = bt_field_class_structure_append_member(out_field_class,
570 debug_info_fc_name,
571 debug_field_class);
572 if (status != BT_FIELD_CLASS_STATUS_OK) {
573 BT_LOGE_STR("Failed to add debug_info field to "
574 "event common context.");
575 ret = -1;
576 goto error;
577 }
578 BT_FIELD_CLASS_PUT_REF_AND_RESET(debug_field_class);
579 }
580 BT_LOGD("Copied content of event common context field class: "
581 "in-fc-addr=%p, out-fc-addr=%p", in_field_class, out_field_class);
582 goto end;
583
584 error:
585 if (debug_field_class) {
586 bt_field_class_put_ref(debug_field_class);
587 }
588 if (bin_field_class) {
589 bt_field_class_put_ref(bin_field_class);
590 }
591 if (func_field_class) {
592 bt_field_class_put_ref(func_field_class);
593 }
594 if (src_field_class) {
595 bt_field_class_put_ref(src_field_class);
596 }
597 end:
598 return ret;
599 }
600
601 BT_HIDDEN
602 bt_field_class *create_field_class_copy(struct trace_ir_metadata_maps *md_maps,
603 const bt_field_class *in_field_class)
604 {
605 return create_field_class_copy_internal(md_maps, in_field_class);
606 }
607
608 BT_HIDDEN
609 int copy_field_class_content(struct trace_ir_metadata_maps *md_maps,
610 const bt_field_class *in_field_class,
611 bt_field_class *out_field_class)
612 {
613 return copy_field_class_content_internal(md_maps, in_field_class,
614 out_field_class);
615 }
This page took 0.041905 seconds and 4 git commands to generate.