Move to kernel style SPDX license identifiers
[babeltrace.git] / src / plugins / lttng-utils / debug-info / debug-info.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
6 * Copyright (c) 2015 Antoine Busque <abusque@efficios.com>
7 * Copyright (c) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
9 *
10 * Babeltrace - Debug Information State Tracker
11 */
12
13 #define BT_COMP_LOG_SELF_COMP self_comp
14 #define BT_LOG_OUTPUT_LEVEL log_level
15 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO"
16 #include "logging/comp-logging.h"
17
18 #include <stdbool.h>
19
20 #include <glib.h>
21
22 #include "common/assert.h"
23 #include "common/common.h"
24 #include "fd-cache/fd-cache.h"
25
26 #include "bin-info.h"
27 #include "debug-info.h"
28 #include "trace-ir-data-copy.h"
29 #include "trace-ir-mapping.h"
30 #include "trace-ir-metadata-copy.h"
31 #include "utils.h"
32 #include "plugins/common/param-validation/param-validation.h"
33
34 #define DEFAULT_DEBUG_INFO_FIELD_NAME "debug_info"
35 #define LTTNG_UST_STATEDUMP_PREFIX "lttng_ust"
36 #define VPID_FIELD_NAME "vpid"
37 #define IP_FIELD_NAME "ip"
38 #define BADDR_FIELD_NAME "baddr"
39 #define CRC32_FIELD_NAME "crc"
40 #define BUILD_ID_FIELD_NAME "build_id"
41 #define FILENAME_FIELD_NAME "filename"
42 #define IS_PIC_FIELD_NAME "is_pic"
43 #define MEMSZ_FIELD_NAME "memsz"
44 #define PATH_FIELD_NAME "path"
45
46 struct debug_info_component {
47 bt_logging_level log_level;
48 bt_self_component *self_comp;
49 bt_self_component_filter *self_comp_filter;
50 gchar *arg_debug_dir;
51 gchar *arg_debug_info_field_name;
52 gchar *arg_target_prefix;
53 bt_bool arg_full_path;
54 };
55
56 struct debug_info_msg_iter {
57 bt_logging_level log_level;
58 struct debug_info_component *debug_info_component;
59 bt_self_message_iterator *input_iterator;
60 bt_self_component *self_comp;
61 bt_message_iterator *msg_iter;
62
63 struct trace_ir_maps *ir_maps;
64 /* in_trace -> debug_info_mapping. */
65 GHashTable *debug_info_map;
66
67 struct bt_fd_cache fd_cache;
68 };
69
70 struct debug_info_source {
71 /* Strings are owned by debug_info_source. */
72 gchar *func;
73 /*
74 * Store the line number as a string so that the allocation and
75 * conversion to string is only done once.
76 */
77 gchar *line_no;
78 gchar *src_path;
79 /* short_src_path points inside src_path, no need to free. */
80 const gchar *short_src_path;
81 gchar *bin_path;
82 /* short_bin_path points inside bin_path, no need to free. */
83 const gchar *short_bin_path;
84 /*
85 * Location within the binary. Either absolute (@0x1234) or
86 * relative (+0x4321).
87 */
88 gchar *bin_loc;
89 };
90
91 struct proc_debug_info_sources {
92 /*
93 * Hash table: base address (pointer to uint64_t) to bin info; owned by
94 * proc_debug_info_sources.
95 */
96 GHashTable *baddr_to_bin_info;
97
98 /*
99 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
100 * owned by proc_debug_info_sources.
101 */
102 GHashTable *ip_to_debug_info_src;
103 };
104
105 struct debug_info {
106 bt_logging_level log_level;
107 bt_self_component *self_comp;
108 struct debug_info_component *comp;
109 const bt_trace *input_trace;
110 bt_listener_id destruction_listener_id;
111
112 /*
113 * Hash table of VPIDs (pointer to int64_t) to
114 * (struct proc_debug_info_sources*); owned by debug_info.
115 */
116 GHashTable *vpid_to_proc_dbg_info_src;
117 GQuark q_statedump_bin_info;
118 GQuark q_statedump_debug_link;
119 GQuark q_statedump_build_id;
120 GQuark q_statedump_start;
121 GQuark q_dl_open;
122 GQuark q_lib_load;
123 GQuark q_lib_unload;
124 struct bt_fd_cache *fd_cache; /* Weak ref. Owned by the iterator. */
125 };
126
127 static
128 int debug_info_init(struct debug_info *info)
129 {
130 info->q_statedump_bin_info = g_quark_from_string(
131 "lttng_ust_statedump:bin_info");
132 info->q_statedump_debug_link = g_quark_from_string(
133 "lttng_ust_statedump:debug_link");
134 info->q_statedump_build_id = g_quark_from_string(
135 "lttng_ust_statedump:build_id");
136 info->q_statedump_start = g_quark_from_string(
137 "lttng_ust_statedump:start");
138 info->q_dl_open = g_quark_from_string("lttng_ust_dl:dlopen");
139 info->q_lib_load = g_quark_from_string("lttng_ust_lib:load");
140 info->q_lib_unload = g_quark_from_string("lttng_ust_lib:unload");
141
142 return bin_info_init(info->log_level, info->self_comp);
143 }
144
145 static
146 void debug_info_source_destroy(struct debug_info_source *debug_info_src)
147 {
148 if (!debug_info_src) {
149 return;
150 }
151
152 g_free(debug_info_src->func);
153 g_free(debug_info_src->line_no);
154 g_free(debug_info_src->src_path);
155 g_free(debug_info_src->bin_path);
156 g_free(debug_info_src->bin_loc);
157 g_free(debug_info_src);
158 }
159
160 static
161 struct debug_info_source *debug_info_source_create_from_bin(
162 struct bin_info *bin, uint64_t ip,
163 bt_self_component *self_comp)
164 {
165 int ret;
166 struct debug_info_source *debug_info_src = NULL;
167 struct source_location *src_loc = NULL;
168 bt_logging_level log_level;
169
170 BT_ASSERT(bin);
171
172 log_level = bin->log_level;
173
174 debug_info_src = g_new0(struct debug_info_source, 1);
175
176 if (!debug_info_src) {
177 goto end;
178 }
179
180 /* Lookup function name */
181 ret = bin_info_lookup_function_name(bin, ip, &debug_info_src->func);
182 if (ret) {
183 goto error;
184 }
185
186 /* Can't retrieve src_loc from ELF, or could not find binary, skip. */
187 if (!bin->is_elf_only || !debug_info_src->func) {
188 /* Lookup source location */
189 ret = bin_info_lookup_source_location(bin, ip, &src_loc);
190 if (ret) {
191 BT_COMP_LOGI("Failed to lookup source location: ret=%d", ret);
192 }
193 }
194
195 if (src_loc) {
196 debug_info_src->line_no =
197 g_strdup_printf("%"PRId64, src_loc->line_no);
198 if (!debug_info_src->line_no) {
199 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
200 "Error occurred when setting `line_no` field.");
201 goto error;
202 }
203
204 if (src_loc->filename) {
205 debug_info_src->src_path = g_strdup(src_loc->filename);
206 if (!debug_info_src->src_path) {
207 goto error;
208 }
209
210 debug_info_src->short_src_path = get_filename_from_path(
211 debug_info_src->src_path);
212 }
213 source_location_destroy(src_loc);
214 }
215
216 if (bin->elf_path) {
217 debug_info_src->bin_path = g_strdup(bin->elf_path);
218 if (!debug_info_src->bin_path) {
219 goto error;
220 }
221
222 debug_info_src->short_bin_path = get_filename_from_path(
223 debug_info_src->bin_path);
224
225 ret = bin_info_get_bin_loc(bin, ip, &(debug_info_src->bin_loc));
226 if (ret) {
227 goto error;
228 }
229 }
230
231 end:
232 return debug_info_src;
233
234 error:
235 debug_info_source_destroy(debug_info_src);
236 return NULL;
237 }
238
239 static
240 void proc_debug_info_sources_destroy(
241 struct proc_debug_info_sources *proc_dbg_info_src)
242 {
243 if (!proc_dbg_info_src) {
244 return;
245 }
246
247 if (proc_dbg_info_src->baddr_to_bin_info) {
248 g_hash_table_destroy(proc_dbg_info_src->baddr_to_bin_info);
249 }
250
251 if (proc_dbg_info_src->ip_to_debug_info_src) {
252 g_hash_table_destroy(proc_dbg_info_src->ip_to_debug_info_src);
253 }
254
255 g_free(proc_dbg_info_src);
256 }
257
258 static
259 struct proc_debug_info_sources *proc_debug_info_sources_create(void)
260 {
261 struct proc_debug_info_sources *proc_dbg_info_src = NULL;
262
263 proc_dbg_info_src = g_new0(struct proc_debug_info_sources, 1);
264 if (!proc_dbg_info_src) {
265 goto end;
266 }
267
268 proc_dbg_info_src->baddr_to_bin_info = g_hash_table_new_full(
269 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
270 (GDestroyNotify) bin_info_destroy);
271 if (!proc_dbg_info_src->baddr_to_bin_info) {
272 goto error;
273 }
274
275 proc_dbg_info_src->ip_to_debug_info_src = g_hash_table_new_full(
276 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
277 (GDestroyNotify) debug_info_source_destroy);
278 if (!proc_dbg_info_src->ip_to_debug_info_src) {
279 goto error;
280 }
281
282 end:
283 return proc_dbg_info_src;
284
285 error:
286 proc_debug_info_sources_destroy(proc_dbg_info_src);
287 return NULL;
288 }
289
290 static
291 struct proc_debug_info_sources *proc_debug_info_sources_ht_get_entry(
292 GHashTable *ht, int64_t vpid)
293 {
294 gpointer key = g_new0(int64_t, 1);
295 struct proc_debug_info_sources *proc_dbg_info_src = NULL;
296
297 if (!key) {
298 goto end;
299 }
300
301 *((int64_t *) key) = vpid;
302
303 /* Exists? Return it */
304 proc_dbg_info_src = g_hash_table_lookup(ht, key);
305 if (proc_dbg_info_src) {
306 goto end;
307 }
308
309 /* Otherwise, create and return it */
310 proc_dbg_info_src = proc_debug_info_sources_create();
311 if (!proc_dbg_info_src) {
312 goto end;
313 }
314
315 g_hash_table_insert(ht, key, proc_dbg_info_src);
316 /* Ownership passed to ht */
317 key = NULL;
318 end:
319 g_free(key);
320 return proc_dbg_info_src;
321 }
322
323 static inline
324 const bt_field *event_borrow_payload_field(const bt_event *event,
325 const char *field_name)
326 {
327 const bt_field *event_payload, *field;
328
329 event_payload = bt_event_borrow_payload_field_const(event);
330 BT_ASSERT_DBG(event_payload);
331
332 field = bt_field_structure_borrow_member_field_by_name_const(
333 event_payload, field_name);
334 return field;
335 }
336
337 static inline
338 const bt_field *event_borrow_common_context_field(const bt_event *event,
339 const char *field_name)
340 {
341 const bt_field *event_common_ctx, *field = NULL;
342
343 event_common_ctx = bt_event_borrow_common_context_field_const(event);
344 if (!event_common_ctx) {
345 goto end;
346 }
347
348 field = bt_field_structure_borrow_member_field_by_name_const(
349 event_common_ctx, field_name);
350
351 end:
352 return field;
353 }
354
355 static inline
356 void event_get_common_context_signed_integer_field_value(
357 const bt_event *event, const char *field_name, int64_t *value)
358 {
359 *value = bt_field_integer_signed_get_value(
360 event_borrow_common_context_field(event, field_name));
361 }
362
363 static inline
364 void event_get_payload_build_id_length(const bt_event *event,
365 const char *field_name, uint64_t *build_id_len)
366 {
367 const bt_field *build_id_field;
368 const bt_field_class *build_id_field_class;
369
370 build_id_field = event_borrow_payload_field(event, field_name);
371 build_id_field_class = bt_field_borrow_class_const(build_id_field);
372
373 BT_ASSERT(bt_field_class_get_type(build_id_field_class) ==
374 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD ||
375 bt_field_class_get_type(build_id_field_class) ==
376 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD);
377 BT_ASSERT(bt_field_class_get_type(
378 bt_field_class_array_borrow_element_field_class_const(
379 build_id_field_class)) ==
380 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER);
381
382 *build_id_len = bt_field_array_get_length(build_id_field);
383 }
384
385 static inline
386 void event_get_payload_build_id_value(const bt_event *event,
387 const char *field_name, uint8_t *build_id)
388 {
389 const bt_field *curr_field, *build_id_field;
390 const bt_field_class *build_id_field_class;
391 uint64_t i, build_id_len;
392
393 build_id_field = event_borrow_payload_field(event, field_name);
394 build_id_field_class = bt_field_borrow_class_const(build_id_field);
395
396 BT_ASSERT(bt_field_class_get_type(build_id_field_class) ==
397 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD ||
398 bt_field_class_get_type(build_id_field_class) ==
399 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD);
400 BT_ASSERT(bt_field_class_get_type(
401 bt_field_class_array_borrow_element_field_class_const(
402 build_id_field_class)) ==
403 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER);
404
405 build_id_len = bt_field_array_get_length(build_id_field);
406
407 for (i = 0; i < build_id_len; i++) {
408 curr_field = bt_field_array_borrow_element_field_by_index_const(
409 build_id_field, i);
410
411 build_id[i] = bt_field_integer_unsigned_get_value(curr_field);
412 }
413 }
414
415 static
416 void event_get_payload_unsigned_integer_field_value(const bt_event *event,
417 const char *field_name, uint64_t *value)
418 {
419 *value = bt_field_integer_unsigned_get_value(
420 event_borrow_payload_field(event, field_name));
421 }
422
423 static
424 void event_get_payload_string_field_value(const bt_event *event,
425 const char *field_name, const char **value)
426 {
427 *value = bt_field_string_get_value(
428 event_borrow_payload_field(event, field_name));
429 }
430
431 static inline
432 bool event_has_payload_field(const bt_event *event,
433 const char *field_name)
434 {
435 return event_borrow_payload_field(event, field_name);
436 }
437
438 static
439 struct debug_info_source *proc_debug_info_sources_get_entry(
440 struct debug_info *debug_info,
441 struct proc_debug_info_sources *proc_dbg_info_src, uint64_t ip)
442 {
443 struct debug_info_source *debug_info_src = NULL;
444 gpointer key = g_new0(uint64_t, 1);
445 GHashTableIter iter;
446 gpointer baddr, value;
447
448 if (!key) {
449 goto end;
450 }
451
452 *((uint64_t *) key) = ip;
453
454 /* Look in IP to debug infos hash table first. */
455 debug_info_src = g_hash_table_lookup(
456 proc_dbg_info_src->ip_to_debug_info_src, key);
457 if (debug_info_src) {
458 goto end;
459 }
460
461 /* Check in all bin_infos. */
462 g_hash_table_iter_init(&iter, proc_dbg_info_src->baddr_to_bin_info);
463
464 while (g_hash_table_iter_next(&iter, &baddr, &value))
465 {
466 struct bin_info *bin = value;
467
468 if (!bin_info_has_address(value, ip)) {
469 continue;
470 }
471
472 /*
473 * Found; add it to cache.
474 *
475 * FIXME: this should be bounded in size (and implement
476 * a caching policy), and entries should be prunned when
477 * libraries are unmapped.
478 */
479 debug_info_src = debug_info_source_create_from_bin(bin, ip,
480 debug_info->self_comp);
481 if (debug_info_src) {
482 g_hash_table_insert(
483 proc_dbg_info_src->ip_to_debug_info_src, key,
484 debug_info_src);
485 /* Ownership passed to ht. */
486 key = NULL;
487 }
488 break;
489 }
490
491 end:
492 free(key);
493 return debug_info_src;
494 }
495
496 static
497 struct debug_info_source *debug_info_query(struct debug_info *debug_info,
498 int64_t vpid, uint64_t ip)
499 {
500 struct debug_info_source *dbg_info_src = NULL;
501 struct proc_debug_info_sources *proc_dbg_info_src;
502
503 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
504 debug_info->vpid_to_proc_dbg_info_src, vpid);
505 if (!proc_dbg_info_src) {
506 goto end;
507 }
508
509 dbg_info_src = proc_debug_info_sources_get_entry(debug_info,
510 proc_dbg_info_src, ip);
511
512 end:
513 return dbg_info_src;
514 }
515
516 static
517 struct debug_info *debug_info_create(struct debug_info_component *comp,
518 const bt_trace *trace, struct bt_fd_cache *fdc)
519 {
520 int ret;
521 struct debug_info *debug_info;
522
523 BT_ASSERT(comp);
524 BT_ASSERT(trace);
525 BT_ASSERT(fdc);
526
527 debug_info = g_new0(struct debug_info, 1);
528 if (!debug_info) {
529 goto end;
530 }
531
532 debug_info->log_level = comp->log_level;
533 debug_info->self_comp = comp->self_comp;
534 debug_info->vpid_to_proc_dbg_info_src = g_hash_table_new_full(
535 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
536 (GDestroyNotify) proc_debug_info_sources_destroy);
537 if (!debug_info->vpid_to_proc_dbg_info_src) {
538 goto error;
539 }
540
541 debug_info->comp = comp;
542 ret = debug_info_init(debug_info);
543 if (ret) {
544 goto error;
545 }
546
547 debug_info->input_trace = trace;
548 debug_info->fd_cache = fdc;
549
550 end:
551 return debug_info;
552 error:
553 g_free(debug_info);
554 return NULL;
555 }
556
557 static
558 void debug_info_destroy(struct debug_info *debug_info)
559 {
560 bt_logging_level log_level;
561 bt_self_component *self_comp;
562 bt_trace_remove_listener_status remove_listener_status;
563 if (!debug_info) {
564 goto end;
565 }
566
567 log_level = debug_info->log_level;
568 self_comp = debug_info->self_comp;
569
570 if (debug_info->vpid_to_proc_dbg_info_src) {
571 g_hash_table_destroy(debug_info->vpid_to_proc_dbg_info_src);
572 }
573
574 remove_listener_status = bt_trace_remove_destruction_listener(
575 debug_info->input_trace,
576 debug_info->destruction_listener_id);
577 if (remove_listener_status != BT_TRACE_REMOVE_LISTENER_STATUS_OK) {
578 BT_COMP_LOGE("Trace destruction listener removal failed.");
579 bt_current_thread_clear_error();
580 }
581
582 g_free(debug_info);
583 end:
584 return;
585 }
586
587 static
588 void handle_event_statedump_build_id(struct debug_info *debug_info,
589 const bt_event *event)
590 {
591 struct proc_debug_info_sources *proc_dbg_info_src;
592 uint64_t build_id_len, baddr;
593 uint8_t *build_id = NULL;
594 struct bin_info *bin;
595 int64_t vpid;
596 int ret = 0;
597
598 event_get_common_context_signed_integer_field_value(event,
599 VPID_FIELD_NAME, &vpid);
600 event_get_payload_unsigned_integer_field_value(event,
601 BADDR_FIELD_NAME, &baddr);
602
603 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
604 debug_info->vpid_to_proc_dbg_info_src, vpid);
605 if (!proc_dbg_info_src) {
606 goto end;
607 }
608
609 bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info,
610 (gpointer) &baddr);
611 if (!bin) {
612 /*
613 * The build_id event comes after the bin has been
614 * created. If it isn't found, just ignore this event.
615 */
616 goto end;
617 }
618
619 event_get_payload_build_id_length(event, BUILD_ID_FIELD_NAME,
620 &build_id_len);
621
622 build_id = g_new0(uint8_t, build_id_len);
623 if (!build_id) {
624 goto end;
625 }
626
627 event_get_payload_build_id_value(event, BUILD_ID_FIELD_NAME, build_id);
628
629 ret = bin_info_set_build_id(bin, build_id, build_id_len);
630 if (ret) {
631 goto end;
632 }
633
634 /*
635 * Reset the is_elf_only flag in case it had been set
636 * previously, because we might find separate debug info using
637 * the new build id information.
638 */
639 bin->is_elf_only = false;
640
641 end:
642 g_free(build_id);
643 return;
644 }
645
646 static
647 void handle_event_statedump_debug_link(struct debug_info *debug_info,
648 const bt_event *event)
649 {
650 struct proc_debug_info_sources *proc_dbg_info_src;
651 struct bin_info *bin = NULL;
652 int64_t vpid;
653 uint64_t baddr;
654 const char *filename = NULL;
655 uint32_t crc32;
656 uint64_t crc_field_value;
657
658 event_get_common_context_signed_integer_field_value(event,
659 VPID_FIELD_NAME, &vpid);
660
661 event_get_payload_unsigned_integer_field_value(event,
662 BADDR_FIELD_NAME, &baddr);
663
664 event_get_payload_unsigned_integer_field_value(event,
665 CRC32_FIELD_NAME, &crc_field_value);
666
667 crc32 = (uint32_t) crc_field_value;
668
669 event_get_payload_string_field_value(event,
670 FILENAME_FIELD_NAME, &filename);
671
672 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
673 debug_info->vpid_to_proc_dbg_info_src, vpid);
674 if (!proc_dbg_info_src) {
675 goto end;
676 }
677
678 bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info,
679 (gpointer) &baddr);
680 if (!bin) {
681 /*
682 * The debug_link event comes after the bin has been
683 * created. If it isn't found, just ignore this event.
684 */
685 goto end;
686 }
687
688 bin_info_set_debug_link(bin, filename, crc32);
689
690 end:
691 return;
692 }
693
694 static
695 void handle_bin_info_event(struct debug_info *debug_info,
696 const bt_event *event, bool has_pic_field)
697 {
698 struct proc_debug_info_sources *proc_dbg_info_src;
699 struct bin_info *bin;
700 uint64_t baddr, memsz;
701 int64_t vpid;
702 const char *path;
703 gpointer key = NULL;
704 bool is_pic;
705
706 event_get_payload_unsigned_integer_field_value(event,
707 MEMSZ_FIELD_NAME, &memsz);
708 if (memsz == 0) {
709 /* Ignore VDSO. */
710 goto end;
711 }
712
713 event_get_payload_unsigned_integer_field_value(event,
714 BADDR_FIELD_NAME, &baddr);
715
716 /*
717 * This field is not produced by the dlopen event emitted before
718 * lttng-ust 2.9.
719 */
720 if (!event_has_payload_field(event, PATH_FIELD_NAME)) {
721 goto end;
722 }
723 event_get_payload_string_field_value(event, PATH_FIELD_NAME, &path);
724
725 if (has_pic_field) {
726 uint64_t is_pic_field_value;
727
728 event_get_payload_unsigned_integer_field_value(event,
729 IS_PIC_FIELD_NAME, &is_pic_field_value);
730 is_pic = is_pic_field_value == 1;
731 } else {
732 /*
733 * dlopen has no is_pic field, because the shared
734 * object is always PIC.
735 */
736 is_pic = true;
737 }
738
739 event_get_common_context_signed_integer_field_value(event,
740 VPID_FIELD_NAME, &vpid);
741
742 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
743 debug_info->vpid_to_proc_dbg_info_src, vpid);
744 if (!proc_dbg_info_src) {
745 goto end;
746 }
747
748 key = g_new0(uint64_t, 1);
749 if (!key) {
750 goto end;
751 }
752
753 *((uint64_t *) key) = baddr;
754
755 bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info, key);
756 if (bin) {
757 goto end;
758 }
759
760 bin = bin_info_create(debug_info->fd_cache, path, baddr, memsz, is_pic,
761 debug_info->comp->arg_debug_dir,
762 debug_info->comp->arg_target_prefix,
763 debug_info->log_level, debug_info->self_comp);
764 if (!bin) {
765 goto end;
766 }
767
768 g_hash_table_insert(proc_dbg_info_src->baddr_to_bin_info, key, bin);
769 /* Ownership passed to ht. */
770 key = NULL;
771
772 end:
773 g_free(key);
774 return;
775 }
776
777 static inline
778 void handle_event_statedump_bin_info(struct debug_info *debug_info,
779 const bt_event *event)
780 {
781 handle_bin_info_event(debug_info, event, true);
782 }
783
784 static inline
785 void handle_event_lib_load(struct debug_info *debug_info,
786 const bt_event *event)
787 {
788 handle_bin_info_event(debug_info, event, false);
789 }
790
791 static
792 void handle_event_lib_unload(struct debug_info *debug_info,
793 const bt_event *event)
794 {
795 gboolean ret;
796 struct proc_debug_info_sources *proc_dbg_info_src;
797 uint64_t baddr;
798 int64_t vpid;
799
800 event_get_payload_unsigned_integer_field_value(event, BADDR_FIELD_NAME,
801 &baddr);
802
803 event_get_common_context_signed_integer_field_value(event,
804 VPID_FIELD_NAME, &vpid);
805
806 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
807 debug_info->vpid_to_proc_dbg_info_src, vpid);
808 if (!proc_dbg_info_src) {
809 /*
810 * It's an unload event for a library for which no load event
811 * was previously received.
812 */
813 goto end;
814 }
815
816 ret = g_hash_table_remove(proc_dbg_info_src->baddr_to_bin_info,
817 (gpointer) &baddr);
818 BT_ASSERT(ret);
819 end:
820 return;
821 }
822
823 static
824 void handle_event_statedump_start(struct debug_info *debug_info,
825 const bt_event *event)
826 {
827 struct proc_debug_info_sources *proc_dbg_info_src;
828 int64_t vpid;
829
830 event_get_common_context_signed_integer_field_value(
831 event, VPID_FIELD_NAME, &vpid);
832
833 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
834 debug_info->vpid_to_proc_dbg_info_src, vpid);
835 if (!proc_dbg_info_src) {
836 goto end;
837 }
838
839 g_hash_table_remove_all(proc_dbg_info_src->baddr_to_bin_info);
840 g_hash_table_remove_all(proc_dbg_info_src->ip_to_debug_info_src);
841
842 end:
843 return;
844 }
845
846 static
847 void trace_debug_info_remove_func(const bt_trace *in_trace, void *data)
848 {
849 struct debug_info_msg_iter *debug_it = data;
850 if (debug_it->debug_info_map) {
851 gboolean ret;
852 ret = g_hash_table_remove(debug_it->debug_info_map,
853 (gpointer) in_trace);
854 BT_ASSERT(ret);
855 }
856 }
857
858 static
859 void handle_event_statedump(struct debug_info_msg_iter *debug_it,
860 const bt_event *event)
861 {
862 const bt_event_class *event_class;
863 const char *event_name;
864 GQuark q_event_name;
865 const bt_trace *trace;
866 struct debug_info *debug_info;
867
868 BT_ASSERT(debug_it);
869 BT_ASSERT(event);
870
871 event_class = bt_event_borrow_class_const(event);
872
873 event_name = bt_event_class_get_name(event_class);
874
875 trace = bt_stream_borrow_trace_const(
876 bt_event_borrow_stream_const(event));
877
878 debug_info = g_hash_table_lookup(debug_it->debug_info_map, trace);
879 if (!debug_info) {
880 bt_trace_add_listener_status add_listener_status;
881
882 debug_info = debug_info_create(debug_it->debug_info_component,
883 trace, &debug_it->fd_cache);
884 g_hash_table_insert(debug_it->debug_info_map, (gpointer) trace,
885 debug_info);
886 add_listener_status = bt_trace_add_destruction_listener(
887 trace, trace_debug_info_remove_func, debug_it,
888 &debug_info->destruction_listener_id);
889 BT_ASSERT(add_listener_status == BT_TRACE_ADD_LISTENER_STATUS_OK);
890 }
891
892 q_event_name = g_quark_try_string(event_name);
893
894 if (q_event_name == debug_info->q_statedump_bin_info) {
895 /* State dump */
896 handle_event_statedump_bin_info(debug_info, event);
897 } else if (q_event_name == debug_info->q_dl_open ||
898 q_event_name == debug_info->q_lib_load) {
899 /*
900 * dl_open and lib_load events are both checked for since
901 * only dl_open was produced as of lttng-ust 2.8.
902 *
903 * lib_load, which is produced from lttng-ust 2.9+, is a lot
904 * more reliable since it will be emitted when other functions
905 * of the dlopen family are called (e.g. dlmopen) and when
906 * library are transitively loaded.
907 */
908 handle_event_lib_load(debug_info, event);
909 } else if (q_event_name == debug_info->q_statedump_start) {
910 /* Start state dump */
911 handle_event_statedump_start(debug_info, event);
912 } else if (q_event_name == debug_info->q_statedump_debug_link) {
913 /* Debug link info */
914 handle_event_statedump_debug_link(debug_info, event);
915 } else if (q_event_name == debug_info->q_statedump_build_id) {
916 /* Build ID info */
917 handle_event_statedump_build_id(debug_info, event);
918 } else if (q_event_name == debug_info-> q_lib_unload) {
919 handle_event_lib_unload(debug_info, event);
920 }
921
922 return;
923 }
924
925 static
926 void destroy_debug_info_comp(struct debug_info_component *debug_info)
927 {
928 if (!debug_info) {
929 return;
930 }
931
932 g_free(debug_info->arg_debug_dir);
933 g_free(debug_info->arg_debug_info_field_name);
934 g_free(debug_info->arg_target_prefix);
935 g_free(debug_info);
936 }
937
938 static
939 void fill_debug_info_bin_field(struct debug_info_source *dbg_info_src,
940 bool full_path, bt_field *curr_field,
941 bt_logging_level log_level, bt_self_component *self_comp)
942 {
943 bt_field_string_set_value_status set_status;
944 bt_field_string_append_status append_status;
945
946 BT_ASSERT_DBG(bt_field_get_class_type(curr_field) ==
947 BT_FIELD_CLASS_TYPE_STRING);
948
949 if (dbg_info_src) {
950 if (full_path) {
951 set_status = bt_field_string_set_value(curr_field,
952 dbg_info_src->bin_path);
953 } else {
954 set_status = bt_field_string_set_value(curr_field,
955 dbg_info_src->short_bin_path);
956 }
957 if (set_status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
958 BT_COMP_LOGE("Cannot set path component of `bin` "
959 "curr_field field's value: str-fc-addr=%p",
960 curr_field);
961 bt_current_thread_clear_error();
962 }
963
964 append_status = bt_field_string_append(curr_field,
965 dbg_info_src->bin_loc);
966 if (append_status != BT_FIELD_STRING_APPEND_STATUS_OK) {
967 BT_COMP_LOGE("Cannot set bin location component of `bin` "
968 "curr_field field's value: str-fc-addr=%p",
969 curr_field);
970 bt_current_thread_clear_error();
971 }
972 } else {
973 set_status = bt_field_string_set_value(curr_field, "");
974 if (set_status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
975 BT_COMP_LOGE("Cannot set `bin` curr_field field's value: "
976 "str-fc-addr=%p", curr_field);
977 bt_current_thread_clear_error();
978 }
979 }
980 }
981
982 static
983 void fill_debug_info_func_field(struct debug_info_source *dbg_info_src,
984 bt_field *curr_field, bt_logging_level log_level,
985 bt_self_component *self_comp)
986 {
987 bt_field_string_set_value_status status;
988
989 BT_ASSERT_DBG(bt_field_get_class_type(curr_field) ==
990 BT_FIELD_CLASS_TYPE_STRING);
991 if (dbg_info_src && dbg_info_src->func) {
992 status = bt_field_string_set_value(curr_field,
993 dbg_info_src->func);
994 } else {
995 status = bt_field_string_set_value(curr_field, "");
996 }
997 if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
998 BT_COMP_LOGE("Cannot set `func` curr_field field's value: "
999 "str-fc-addr=%p", curr_field);
1000 bt_current_thread_clear_error();
1001 }
1002 }
1003
1004 static
1005 void fill_debug_info_src_field(struct debug_info_source *dbg_info_src,
1006 bool full_path, bt_field *curr_field,
1007 bt_logging_level log_level,
1008 bt_self_component *self_comp)
1009 {
1010 bt_field_string_set_value_status set_status;
1011 bt_field_string_append_status append_status;
1012
1013 BT_ASSERT_DBG(bt_field_get_class_type(curr_field) ==
1014 BT_FIELD_CLASS_TYPE_STRING);
1015
1016 if (dbg_info_src && dbg_info_src->src_path) {
1017 if (full_path) {
1018 set_status = bt_field_string_set_value(curr_field,
1019 dbg_info_src->src_path);
1020 } else {
1021 set_status = bt_field_string_set_value(curr_field,
1022 dbg_info_src->short_src_path);
1023 }
1024 if (set_status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
1025 BT_COMP_LOGE("Cannot set path component of `src` "
1026 "curr_field field's value: str-fc-addr=%p",
1027 curr_field);
1028 bt_current_thread_clear_error();
1029 }
1030
1031 append_status = bt_field_string_append(curr_field, ":");
1032 if (append_status != BT_FIELD_STRING_APPEND_STATUS_OK) {
1033 BT_COMP_LOGE("Cannot set colon component of `src` "
1034 "curr_field field's value: str-fc-addr=%p",
1035 curr_field);
1036 bt_current_thread_clear_error();
1037 }
1038
1039 append_status = bt_field_string_append(curr_field,
1040 dbg_info_src->line_no);
1041 if (append_status != BT_FIELD_STRING_APPEND_STATUS_OK) {
1042 BT_COMP_LOGE("Cannot set line number component of `src` "
1043 "curr_field field's value: str-fc-addr=%p",
1044 curr_field);
1045 bt_current_thread_clear_error();
1046 }
1047 } else {
1048 set_status = bt_field_string_set_value(curr_field, "");
1049 if (set_status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
1050 BT_COMP_LOGE("Cannot set `src` curr_field field's value: "
1051 "str-fc-addr=%p", curr_field);
1052 bt_current_thread_clear_error();
1053 }
1054 }
1055 }
1056
1057 static
1058 void fill_debug_info_field_empty(bt_field *debug_info_field,
1059 bt_logging_level log_level, bt_self_component *self_comp)
1060 {
1061 bt_field_string_set_value_status status;
1062 bt_field *bin_field, *func_field, *src_field;
1063
1064 BT_ASSERT_DBG(bt_field_get_class_type(debug_info_field) ==
1065 BT_FIELD_CLASS_TYPE_STRUCTURE);
1066
1067 bin_field = bt_field_structure_borrow_member_field_by_name(
1068 debug_info_field, "bin");
1069 func_field = bt_field_structure_borrow_member_field_by_name(
1070 debug_info_field, "func");
1071 src_field = bt_field_structure_borrow_member_field_by_name(
1072 debug_info_field, "src");
1073
1074 BT_ASSERT_DBG(bt_field_get_class_type(bin_field) ==
1075 BT_FIELD_CLASS_TYPE_STRING);
1076 BT_ASSERT_DBG(bt_field_get_class_type(func_field) ==
1077 BT_FIELD_CLASS_TYPE_STRING);
1078 BT_ASSERT_DBG(bt_field_get_class_type(src_field) ==
1079 BT_FIELD_CLASS_TYPE_STRING);
1080
1081 status = bt_field_string_set_value(bin_field, "");
1082 if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
1083 BT_COMP_LOGE("Cannot set `bin` field's value: "
1084 "str-fc-addr=%p", bin_field);
1085 bt_current_thread_clear_error();
1086 }
1087
1088 status = bt_field_string_set_value(func_field, "");
1089 if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
1090 BT_COMP_LOGE("Cannot set `func` field's value: "
1091 "str-fc-addr=%p", func_field);
1092 bt_current_thread_clear_error();
1093 }
1094
1095 status = bt_field_string_set_value(src_field, "");
1096 if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
1097 BT_COMP_LOGE("Cannot set `src` field's value: "
1098 "str-fc-addr=%p", src_field);
1099 bt_current_thread_clear_error();
1100 }
1101 }
1102 static
1103 void fill_debug_info_field(struct debug_info *debug_info, int64_t vpid,
1104 uint64_t ip, bt_field *debug_info_field)
1105 {
1106 struct debug_info_source *dbg_info_src;
1107 const bt_field_class *debug_info_fc;
1108
1109 BT_ASSERT_DBG(bt_field_get_class_type(debug_info_field) ==
1110 BT_FIELD_CLASS_TYPE_STRUCTURE);
1111
1112 debug_info_fc = bt_field_borrow_class_const(debug_info_field);
1113
1114 BT_ASSERT_DBG(bt_field_class_structure_get_member_count(
1115 debug_info_fc) == 3);
1116
1117 dbg_info_src = debug_info_query(debug_info, vpid, ip);
1118
1119 fill_debug_info_bin_field(dbg_info_src,
1120 debug_info->comp->arg_full_path,
1121 bt_field_structure_borrow_member_field_by_name(
1122 debug_info_field, "bin"),
1123 debug_info->log_level, debug_info->self_comp);
1124 fill_debug_info_func_field(dbg_info_src,
1125 bt_field_structure_borrow_member_field_by_name(
1126 debug_info_field, "func"),
1127 debug_info->log_level, debug_info->self_comp);
1128 fill_debug_info_src_field(dbg_info_src,
1129 debug_info->comp->arg_full_path,
1130 bt_field_structure_borrow_member_field_by_name(
1131 debug_info_field, "src"),
1132 debug_info->log_level, debug_info->self_comp);
1133 }
1134
1135 static
1136 void fill_debug_info_event_if_needed(struct debug_info_msg_iter *debug_it,
1137 const bt_event *in_event, bt_event *out_event)
1138 {
1139 bt_field *out_common_ctx_field, *out_debug_info_field;
1140 const bt_field *vpid_field, *ip_field, *in_common_ctx_field;
1141 const bt_field_class *in_common_ctx_fc;
1142 struct debug_info *debug_info;
1143 uint64_t vpid;
1144 int64_t ip;
1145 gchar *debug_info_field_name =
1146 debug_it->debug_info_component->arg_debug_info_field_name;
1147 bt_logging_level log_level = debug_it->log_level;
1148 bt_self_component *self_comp = debug_it->self_comp;
1149
1150 in_common_ctx_field = bt_event_borrow_common_context_field_const(
1151 in_event);
1152 if (!in_common_ctx_field) {
1153 /*
1154 * There is no event common context so no need to add debug
1155 * info field.
1156 */
1157 goto end;
1158 }
1159
1160 in_common_ctx_fc = bt_field_borrow_class_const(in_common_ctx_field);
1161 if (!is_event_common_ctx_dbg_info_compatible(in_common_ctx_fc,
1162 debug_it->ir_maps->debug_info_field_class_name)) {
1163 /*
1164 * The input event common context does not have the necessary
1165 * fields to resolve debug information.
1166 */
1167 goto end;
1168 }
1169
1170 /* Borrow the debug-info field. */
1171 out_common_ctx_field = bt_event_borrow_common_context_field(out_event);
1172 if (!out_common_ctx_field) {
1173 goto end;
1174 }
1175
1176 out_debug_info_field = bt_field_structure_borrow_member_field_by_name(
1177 out_common_ctx_field, debug_info_field_name);
1178
1179 vpid_field = bt_field_structure_borrow_member_field_by_name_const(
1180 out_common_ctx_field, VPID_FIELD_NAME);
1181 ip_field = bt_field_structure_borrow_member_field_by_name_const(
1182 out_common_ctx_field, IP_FIELD_NAME);
1183
1184 vpid = bt_field_integer_signed_get_value(vpid_field);
1185 ip = bt_field_integer_unsigned_get_value(ip_field);
1186
1187 /*
1188 * Borrow the debug_info structure needed for the source
1189 * resolving.
1190 */
1191 debug_info = g_hash_table_lookup(debug_it->debug_info_map,
1192 bt_stream_borrow_trace_const(
1193 bt_event_borrow_stream_const(in_event)));
1194
1195 if (debug_info) {
1196 /*
1197 * Perform the debug-info resolving and set the event fields
1198 * accordingly.
1199 */
1200 fill_debug_info_field(debug_info, vpid, ip, out_debug_info_field);
1201 } else {
1202 BT_COMP_LOGD("No debug information for this trace. Setting debug "
1203 "info fields to empty strings.");
1204 fill_debug_info_field_empty(out_debug_info_field,
1205 log_level, self_comp);
1206 }
1207 end:
1208 return;
1209 }
1210
1211 static
1212 void update_event_statedump_if_needed(struct debug_info_msg_iter *debug_it,
1213 const bt_event *in_event)
1214 {
1215 const bt_field *event_common_ctx;
1216 const bt_field_class *event_common_ctx_fc;
1217 const bt_event_class *in_event_class = bt_event_borrow_class_const(in_event);
1218
1219 /*
1220 * If the event is an lttng_ust_statedump event AND has the right event
1221 * common context fields update the debug-info view for this process.
1222 */
1223 event_common_ctx = bt_event_borrow_common_context_field_const(in_event);
1224 if (!event_common_ctx) {
1225 goto end;
1226 }
1227
1228 event_common_ctx_fc = bt_field_borrow_class_const(event_common_ctx);
1229 if (is_event_common_ctx_dbg_info_compatible(event_common_ctx_fc,
1230 debug_it->ir_maps->debug_info_field_class_name)) {
1231 /* Checkout if it might be a one of lttng ust statedump events. */
1232 const char *in_event_name = bt_event_class_get_name(in_event_class);
1233 if (strncmp(in_event_name, LTTNG_UST_STATEDUMP_PREFIX,
1234 strlen(LTTNG_UST_STATEDUMP_PREFIX)) == 0) {
1235 /* Handle statedump events. */
1236 handle_event_statedump(debug_it, in_event);
1237 }
1238 }
1239 end:
1240 return;
1241 }
1242
1243 static
1244 bt_message *handle_event_message(struct debug_info_msg_iter *debug_it,
1245 const bt_message *in_message)
1246 {
1247 const bt_clock_snapshot *cs;
1248 const bt_clock_class *default_cc;
1249 const bt_packet *in_packet;
1250 const bt_stream *in_stream;
1251 const bt_stream *out_stream;
1252 bt_event_class *out_event_class;
1253 bt_packet *out_packet = NULL;
1254 bt_event *out_event;
1255 bt_logging_level log_level = debug_it->log_level;
1256 bt_self_component *self_comp = debug_it->self_comp;
1257
1258 bt_message *out_message = NULL;
1259
1260 /* Borrow the input event and its event class. */
1261 const bt_event *in_event =
1262 bt_message_event_borrow_event_const(in_message);
1263 const bt_event_class *in_event_class =
1264 bt_event_borrow_class_const(in_event);
1265
1266 update_event_statedump_if_needed(debug_it, in_event);
1267
1268 out_event_class = trace_ir_mapping_borrow_mapped_event_class(
1269 debug_it->ir_maps, in_event_class);
1270 if (!out_event_class) {
1271 out_event_class = trace_ir_mapping_create_new_mapped_event_class(
1272 debug_it->ir_maps, in_event_class);
1273 }
1274 BT_ASSERT_DBG(out_event_class);
1275
1276 /* Borrow the input stream. */
1277 in_stream = bt_event_borrow_stream_const(in_event);
1278 BT_ASSERT_DBG(in_stream);
1279 out_stream = trace_ir_mapping_borrow_mapped_stream(debug_it->ir_maps,
1280 in_stream);
1281 BT_ASSERT_DBG(in_stream);
1282
1283 /* Borrow the input and output packets. */
1284 in_packet = bt_event_borrow_packet_const(in_event);
1285 if (in_packet) {
1286 out_packet = trace_ir_mapping_borrow_mapped_packet(
1287 debug_it->ir_maps, in_packet);
1288 }
1289
1290 default_cc = bt_stream_class_borrow_default_clock_class_const(
1291 bt_event_class_borrow_stream_class_const(in_event_class));
1292 if (default_cc) {
1293 /* Borrow event clock snapshot. */
1294 cs = bt_message_event_borrow_default_clock_snapshot_const(
1295 in_message);
1296
1297 /* Create an output event message. */
1298 if (out_packet) {
1299 out_message =
1300 bt_message_event_create_with_packet_and_default_clock_snapshot(
1301 debug_it->input_iterator,
1302 out_event_class, out_packet,
1303 bt_clock_snapshot_get_value(cs));
1304 } else {
1305 out_message =
1306 bt_message_event_create_with_default_clock_snapshot(
1307 debug_it->input_iterator,
1308 out_event_class, out_stream,
1309 bt_clock_snapshot_get_value(cs));
1310 }
1311 } else {
1312 if (out_packet) {
1313 out_message = bt_message_event_create_with_packet(
1314 debug_it->input_iterator, out_event_class,
1315 out_packet);
1316 } else {
1317 out_message = bt_message_event_create(
1318 debug_it->input_iterator, out_event_class,
1319 out_stream);
1320 }
1321 }
1322
1323 if (!out_message) {
1324 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1325 "Error creating output event message.");
1326 goto error;
1327 }
1328
1329 out_event = bt_message_event_borrow_event(out_message);
1330
1331 /* Copy the original fields to the output event. */
1332 if (copy_event_content(in_event, out_event, log_level, self_comp) !=
1333 DEBUG_INFO_TRACE_IR_MAPPING_STATUS_OK) {
1334 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1335 "Error copying event message content output event message: "
1336 "in-ev-addr=%p, out-ev-addr=%p", in_event, out_event);
1337 goto error;
1338 }
1339
1340 /*
1341 * Try to set the debug-info fields based on debug information that is
1342 * gathered so far.
1343 */
1344 fill_debug_info_event_if_needed(debug_it, in_event, out_event);
1345
1346 goto end;
1347
1348 error:
1349 BT_MESSAGE_PUT_REF_AND_RESET(out_message);
1350 end:
1351 return out_message;
1352 }
1353
1354 static
1355 bt_message *handle_stream_begin_message(struct debug_info_msg_iter *debug_it,
1356 const bt_message *in_message)
1357 {
1358 const bt_stream *in_stream;
1359 bt_message *out_message;
1360 bt_stream *out_stream;
1361 bt_logging_level log_level = debug_it->log_level;
1362 bt_self_component *self_comp = debug_it->self_comp;
1363
1364 in_stream = bt_message_stream_beginning_borrow_stream_const(in_message);
1365 BT_ASSERT(in_stream);
1366
1367 /* Create a duplicated output stream. */
1368 out_stream = trace_ir_mapping_create_new_mapped_stream(
1369 debug_it->ir_maps, in_stream);
1370 if (!out_stream) {
1371 out_message = NULL;
1372 goto error;
1373 }
1374
1375 /* Create an output stream beginning message. */
1376 out_message = bt_message_stream_beginning_create(
1377 debug_it->input_iterator, out_stream);
1378 if (!out_message) {
1379 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1380 "Error creating output stream beginning message: "
1381 "out-s-addr=%p", out_stream);
1382 }
1383 error:
1384 return out_message;
1385 }
1386
1387 static
1388 bt_message *handle_stream_end_message(struct debug_info_msg_iter *debug_it,
1389 const bt_message *in_message)
1390 {
1391 const bt_stream *in_stream;
1392 bt_message *out_message = NULL;
1393 bt_stream *out_stream;
1394 bt_logging_level log_level = debug_it->log_level;
1395 bt_self_component *self_comp = debug_it->self_comp;
1396
1397 in_stream = bt_message_stream_end_borrow_stream_const(in_message);
1398 BT_ASSERT(in_stream);
1399
1400 out_stream = trace_ir_mapping_borrow_mapped_stream(
1401 debug_it->ir_maps, in_stream);
1402 BT_ASSERT(out_stream);
1403
1404 /* Create an output stream end message. */
1405 out_message = bt_message_stream_end_create(debug_it->input_iterator,
1406 out_stream);
1407 if (!out_message) {
1408 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1409 "Error creating output stream end message: "
1410 "out-s-addr=%p", out_stream);
1411 goto end;
1412 }
1413
1414 /* Remove stream from trace mapping hashtable. */
1415 trace_ir_mapping_remove_mapped_stream(debug_it->ir_maps, in_stream);
1416
1417 end:
1418 return out_message;
1419 }
1420
1421 static
1422 bt_message *handle_packet_begin_message(struct debug_info_msg_iter *debug_it,
1423 const bt_message *in_message)
1424 {
1425 bool has_default_clock_snapshot;
1426 const bt_clock_snapshot *cs;
1427 bt_message *out_message = NULL;
1428 bt_packet *out_packet;
1429 bt_logging_level log_level = debug_it->log_level;
1430 bt_self_component *self_comp = debug_it->self_comp;
1431
1432 const bt_packet *in_packet =
1433 bt_message_packet_beginning_borrow_packet_const(in_message);
1434 BT_ASSERT(in_packet);
1435
1436 /* This packet should not be already mapped. */
1437 BT_ASSERT(!trace_ir_mapping_borrow_mapped_packet(
1438 debug_it->ir_maps, in_packet));
1439
1440 out_packet = trace_ir_mapping_create_new_mapped_packet(debug_it->ir_maps,
1441 in_packet);
1442
1443 BT_ASSERT(out_packet);
1444
1445 has_default_clock_snapshot =
1446 bt_stream_class_packets_have_beginning_default_clock_snapshot(
1447 bt_stream_borrow_class_const(
1448 bt_packet_borrow_stream_const(in_packet)));
1449 if (has_default_clock_snapshot) {
1450 /* Borrow clock snapshot. */
1451 cs = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
1452 in_message);
1453
1454 /* Create an output packet beginning message. */
1455 out_message = bt_message_packet_beginning_create_with_default_clock_snapshot(
1456 debug_it->input_iterator, out_packet,
1457 bt_clock_snapshot_get_value(cs));
1458 } else {
1459 out_message = bt_message_packet_beginning_create(
1460 debug_it->input_iterator, out_packet);
1461 }
1462 if (!out_message) {
1463 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1464 "Error creating output packet beginning message: "
1465 "out-p-addr=%p", out_packet);
1466 }
1467
1468 return out_message;
1469 }
1470
1471 static
1472 bt_message *handle_packet_end_message(struct debug_info_msg_iter *debug_it,
1473 const bt_message *in_message)
1474 {
1475 bool has_default_clock_snapshot;
1476 const bt_clock_snapshot *cs;
1477 const bt_packet *in_packet;
1478 bt_message *out_message = NULL;
1479 bt_packet *out_packet;
1480 bt_logging_level log_level = debug_it->log_level;
1481 bt_self_component *self_comp = debug_it->self_comp;
1482
1483 in_packet = bt_message_packet_end_borrow_packet_const(in_message);
1484 BT_ASSERT(in_packet);
1485
1486 out_packet = trace_ir_mapping_borrow_mapped_packet(debug_it->ir_maps, in_packet);
1487 BT_ASSERT(out_packet);
1488
1489 has_default_clock_snapshot =
1490 bt_stream_class_packets_have_end_default_clock_snapshot(
1491 bt_stream_borrow_class_const(
1492 bt_packet_borrow_stream_const(in_packet)));
1493 if (has_default_clock_snapshot) {
1494 /* Borrow clock snapshot. */
1495 cs = bt_message_packet_end_borrow_default_clock_snapshot_const(
1496 in_message);
1497
1498 /* Create an outpute packet end message. */
1499 out_message = bt_message_packet_end_create_with_default_clock_snapshot(
1500 debug_it->input_iterator, out_packet,
1501 bt_clock_snapshot_get_value(cs));
1502 } else {
1503 out_message = bt_message_packet_end_create(
1504 debug_it->input_iterator, out_packet);
1505 }
1506
1507 if (!out_message) {
1508 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1509 "Error creating output packet end message: "
1510 "out-p-addr=%p", out_packet);
1511 goto end;
1512 }
1513
1514 /* Remove packet from data mapping hashtable. */
1515 trace_ir_mapping_remove_mapped_packet(debug_it->ir_maps, in_packet);
1516
1517 end:
1518 return out_message;
1519 }
1520
1521 static
1522 bt_message *handle_msg_iterator_inactivity(struct debug_info_msg_iter *debug_it,
1523 const bt_message *in_message)
1524 {
1525 /*
1526 * This message type can be forwarded directly because it does
1527 * not refer to any objects in the trace class.
1528 */
1529 bt_message_get_ref(in_message);
1530 return (bt_message*) in_message;
1531 }
1532
1533 static
1534 bt_message *handle_discarded_events_message(struct debug_info_msg_iter *debug_it,
1535 const bt_message *in_message)
1536 {
1537 const bt_clock_snapshot *begin_cs, *end_cs;
1538 const bt_stream *in_stream;
1539 bool has_default_clock_snapshots;
1540 uint64_t discarded_events, begin_cs_value, end_cs_value;
1541 bt_property_availability prop_avail;
1542 bt_message *out_message = NULL;
1543 bt_stream *out_stream;
1544 bt_logging_level log_level = debug_it->log_level;
1545 bt_self_component *self_comp = debug_it->self_comp;
1546
1547 in_stream = bt_message_discarded_events_borrow_stream_const(
1548 in_message);
1549 BT_ASSERT(in_stream);
1550
1551 out_stream = trace_ir_mapping_borrow_mapped_stream( debug_it->ir_maps,
1552 in_stream);
1553 BT_ASSERT(out_stream);
1554
1555 has_default_clock_snapshots =
1556 bt_stream_class_discarded_events_have_default_clock_snapshots(
1557 bt_stream_borrow_class_const(in_stream));
1558 if (has_default_clock_snapshots) {
1559 begin_cs = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1560 in_message);
1561 end_cs = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1562 in_message);
1563
1564 begin_cs_value = bt_clock_snapshot_get_value(begin_cs);
1565 end_cs_value = bt_clock_snapshot_get_value(end_cs);
1566
1567 out_message = bt_message_discarded_events_create_with_default_clock_snapshots(
1568 debug_it->input_iterator, out_stream,
1569 begin_cs_value, end_cs_value);
1570 } else {
1571 out_message = bt_message_discarded_events_create(
1572 debug_it->input_iterator, out_stream);
1573 }
1574 if (!out_message) {
1575 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1576 "Error creating output discarded events message: "
1577 "out-s-addr=%p", out_stream);
1578 goto error;
1579 }
1580
1581 prop_avail = bt_message_discarded_events_get_count(in_message,
1582 &discarded_events);
1583
1584 if (prop_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1585 bt_message_discarded_events_set_count(out_message,
1586 discarded_events);
1587 }
1588
1589 error:
1590 return out_message;
1591 }
1592
1593 static
1594 bt_message *handle_discarded_packets_message(struct debug_info_msg_iter *debug_it,
1595 const bt_message *in_message)
1596 {
1597 const bt_clock_snapshot *begin_cs, *end_cs;
1598 bool has_default_clock_snapshots;
1599 const bt_stream *in_stream;
1600 uint64_t discarded_packets, begin_cs_value, end_cs_value;
1601 bt_property_availability prop_avail;
1602 bt_message *out_message = NULL;
1603 bt_stream *out_stream;
1604 bt_logging_level log_level = debug_it->log_level;
1605 bt_self_component *self_comp = debug_it->self_comp;
1606
1607 in_stream = bt_message_discarded_packets_borrow_stream_const(
1608 in_message);
1609 BT_ASSERT(in_stream);
1610
1611 out_stream = trace_ir_mapping_borrow_mapped_stream( debug_it->ir_maps,
1612 in_stream);
1613 BT_ASSERT(out_stream);
1614
1615 has_default_clock_snapshots =
1616 bt_stream_class_discarded_packets_have_default_clock_snapshots(
1617 bt_stream_borrow_class_const(in_stream));
1618 if (has_default_clock_snapshots) {
1619 begin_cs = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1620 in_message);
1621
1622 end_cs = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1623 in_message);
1624
1625 begin_cs_value = bt_clock_snapshot_get_value(begin_cs);
1626 end_cs_value = bt_clock_snapshot_get_value(end_cs);
1627
1628 out_message = bt_message_discarded_packets_create_with_default_clock_snapshots(
1629 debug_it->input_iterator, out_stream,
1630 begin_cs_value, end_cs_value);
1631 } else {
1632 out_message = bt_message_discarded_packets_create(
1633 debug_it->input_iterator, out_stream);
1634 }
1635 if (!out_message) {
1636 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1637 "Error creating output discarded packet message: "
1638 "out-s-addr=%p", out_stream);
1639 goto error;
1640 }
1641
1642 prop_avail = bt_message_discarded_packets_get_count(in_message,
1643 &discarded_packets);
1644 if (prop_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1645 bt_message_discarded_packets_set_count(out_message,
1646 discarded_packets);
1647 }
1648
1649 error:
1650 return out_message;
1651 }
1652
1653 static
1654 const bt_message *handle_message(struct debug_info_msg_iter *debug_it,
1655 const bt_message *in_message)
1656 {
1657 bt_message *out_message = NULL;
1658
1659 switch (bt_message_get_type(in_message)) {
1660 case BT_MESSAGE_TYPE_EVENT:
1661 out_message = handle_event_message(debug_it, in_message);
1662 break;
1663 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1664 out_message = handle_packet_begin_message(debug_it, in_message);
1665 break;
1666 case BT_MESSAGE_TYPE_PACKET_END:
1667 out_message = handle_packet_end_message(debug_it, in_message);
1668 break;
1669 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1670 out_message = handle_stream_begin_message(debug_it, in_message);
1671 break;
1672 case BT_MESSAGE_TYPE_STREAM_END:
1673 out_message = handle_stream_end_message(debug_it, in_message);
1674 break;
1675 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
1676 out_message = handle_msg_iterator_inactivity(debug_it, in_message);
1677 break;
1678 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1679 out_message = handle_discarded_events_message(debug_it, in_message);
1680 break;
1681 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1682 out_message = handle_discarded_packets_message(debug_it, in_message);
1683 break;
1684 default:
1685 bt_common_abort();
1686 break;
1687 }
1688
1689 return out_message;
1690 }
1691
1692 static
1693 struct bt_param_validation_map_value_entry_descr debug_info_params[] = {
1694 { "debug-info-field-name", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_STRING } },
1695 { "debug-info-dir", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_STRING } },
1696 { "target-prefix", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_STRING } },
1697 { "full-path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
1698 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
1699 };
1700
1701 static
1702 bt_component_class_initialize_method_status init_from_params(
1703 struct debug_info_component *debug_info_component,
1704 const bt_value *params)
1705 {
1706 const bt_value *value;
1707 bt_component_class_initialize_method_status status;
1708 bt_logging_level log_level = debug_info_component->log_level;
1709 enum bt_param_validation_status validation_status;
1710 gchar *validate_error = NULL;
1711
1712 validation_status = bt_param_validation_validate(params,
1713 debug_info_params, &validate_error);
1714 if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
1715 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
1716 goto end;
1717 } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
1718 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
1719 BT_COMP_LOGE_APPEND_CAUSE(debug_info_component->self_comp,
1720 "%s", validate_error);
1721 goto end;
1722 }
1723
1724 BT_ASSERT(params);
1725
1726 value = bt_value_map_borrow_entry_value_const(params,
1727 "debug-info-field-name");
1728 if (value) {
1729 debug_info_component->arg_debug_info_field_name =
1730 g_strdup(bt_value_string_get(value));
1731 } else {
1732 debug_info_component->arg_debug_info_field_name =
1733 g_strdup(DEFAULT_DEBUG_INFO_FIELD_NAME);
1734 }
1735
1736 value = bt_value_map_borrow_entry_value_const(params, "debug-info-dir");
1737 if (value) {
1738 debug_info_component->arg_debug_dir =
1739 g_strdup(bt_value_string_get(value));
1740 } else {
1741 debug_info_component->arg_debug_dir = NULL;
1742 }
1743
1744
1745 value = bt_value_map_borrow_entry_value_const(params, "target-prefix");
1746 if (value) {
1747 debug_info_component->arg_target_prefix =
1748 g_strdup(bt_value_string_get(value));
1749 } else {
1750 debug_info_component->arg_target_prefix = NULL;
1751 }
1752
1753 value = bt_value_map_borrow_entry_value_const(params, "full-path");
1754 if (value) {
1755 debug_info_component->arg_full_path = bt_value_bool_get(value);
1756 } else {
1757 debug_info_component->arg_full_path = BT_FALSE;
1758 }
1759
1760 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
1761
1762 end:
1763 g_free(validate_error);
1764
1765 return status;
1766 }
1767
1768 BT_HIDDEN
1769 bt_component_class_initialize_method_status debug_info_comp_init(
1770 bt_self_component_filter *self_comp_flt,
1771 bt_self_component_filter_configuration *config,
1772 const bt_value *params, __attribute__((unused)) void *init_method_data)
1773 {
1774 struct debug_info_component *debug_info_comp;
1775 bt_component_class_initialize_method_status status =
1776 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
1777 bt_self_component_add_port_status add_port_status;
1778 bt_self_component *self_comp =
1779 bt_self_component_filter_as_self_component(self_comp_flt);
1780 bt_logging_level log_level = bt_component_get_logging_level(
1781 bt_self_component_as_component(self_comp));
1782
1783 BT_COMP_LOGI("Initializing debug_info component: "
1784 "comp-addr=%p, params-addr=%p", self_comp, params);
1785
1786 debug_info_comp = g_new0(struct debug_info_component, 1);
1787 if (!debug_info_comp) {
1788 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1789 "Failed to allocate one debug_info component.");
1790 goto error;
1791 }
1792
1793 debug_info_comp->log_level = log_level;
1794 debug_info_comp->self_comp = self_comp;
1795 debug_info_comp->self_comp_filter = self_comp_flt;
1796 bt_self_component_set_data(self_comp, debug_info_comp);
1797
1798 add_port_status = bt_self_component_filter_add_input_port(
1799 self_comp_flt, "in", NULL, NULL);
1800 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
1801 status = (int) add_port_status;
1802 goto error;
1803 }
1804
1805 add_port_status = bt_self_component_filter_add_output_port(
1806 self_comp_flt, "out", NULL, NULL);
1807 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
1808 status = (int) add_port_status;
1809 goto error;
1810 }
1811
1812 status = init_from_params(debug_info_comp, params);
1813 if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
1814 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
1815 "Cannot configure debug_info component: "
1816 "debug_info-comp-addr=%p, params-addr=%p",
1817 debug_info_comp, params);
1818 goto error;
1819 }
1820
1821 goto end;
1822
1823 error:
1824 destroy_debug_info_comp(debug_info_comp);
1825 bt_self_component_set_data(self_comp, NULL);
1826
1827 if (status == BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
1828 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
1829 }
1830 end:
1831 return status;
1832 }
1833
1834 BT_HIDDEN
1835 void debug_info_comp_finalize(bt_self_component_filter *self_comp_flt)
1836 {
1837 struct debug_info_component *debug_info =
1838 bt_self_component_get_data(
1839 bt_self_component_filter_as_self_component(
1840 self_comp_flt));
1841 bt_logging_level log_level = debug_info->log_level;
1842 bt_self_component *self_comp = debug_info->self_comp;
1843
1844 BT_COMP_LOGI("Finalizing debug_info self_component: comp-addr=%p",
1845 self_comp);
1846
1847 destroy_debug_info_comp(debug_info);
1848 }
1849
1850 BT_HIDDEN
1851 bt_message_iterator_class_next_method_status debug_info_msg_iter_next(
1852 bt_self_message_iterator *self_msg_iter,
1853 const bt_message_array_const msgs, uint64_t capacity,
1854 uint64_t *count)
1855 {
1856 bt_message_iterator *upstream_iterator = NULL;
1857 bt_message_iterator_next_status upstream_iterator_ret_status;
1858 struct debug_info_msg_iter *debug_info_msg_iter;
1859 struct debug_info_component *debug_info = NULL;
1860 bt_message_iterator_class_next_method_status status;
1861 bt_self_component *self_comp = NULL;
1862 bt_message_array_const input_msgs;
1863 const bt_message *out_message;
1864 uint64_t curr_msg_idx, i;
1865
1866 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
1867
1868 self_comp = bt_self_message_iterator_borrow_component(self_msg_iter);
1869 BT_ASSERT_DBG(self_comp);
1870
1871 debug_info = bt_self_component_get_data(self_comp);
1872 BT_ASSERT_DBG(debug_info);
1873
1874 debug_info_msg_iter = bt_self_message_iterator_get_data(self_msg_iter);
1875 BT_ASSERT_DBG(debug_info_msg_iter);
1876
1877 upstream_iterator = debug_info_msg_iter->msg_iter;
1878 BT_ASSERT_DBG(upstream_iterator);
1879
1880 upstream_iterator_ret_status =
1881 bt_message_iterator_next(
1882 upstream_iterator, &input_msgs, count);
1883 if (upstream_iterator_ret_status !=
1884 BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) {
1885 /*
1886 * No messages were returned. Not necessarily an error.
1887 * Convert the upstream message iterator status to a
1888 * self status.
1889 */
1890 status = (int) upstream_iterator_ret_status;
1891 goto end;
1892 }
1893
1894 /*
1895 * There should never be more received messages than the capacity we
1896 * provided.
1897 */
1898 BT_ASSERT_DBG(*count <= capacity);
1899
1900 for (curr_msg_idx = 0; curr_msg_idx < *count; curr_msg_idx++) {
1901 out_message = handle_message(debug_info_msg_iter,
1902 input_msgs[curr_msg_idx]);
1903 if (!out_message) {
1904 goto handle_msg_error;
1905 }
1906
1907 msgs[curr_msg_idx] = out_message;
1908 /*
1909 * Drop our reference of the input message as we are done with
1910 * it and created a output copy.
1911 */
1912 bt_message_put_ref(input_msgs[curr_msg_idx]);
1913 }
1914
1915 goto end;
1916
1917 handle_msg_error:
1918 /*
1919 * Drop references of all the output messages created before the
1920 * failure.
1921 */
1922 for (i = 0; i < curr_msg_idx; i++) {
1923 bt_message_put_ref(msgs[i]);
1924 }
1925
1926 /*
1927 * Drop references of all the input messages not dropped before the
1928 * failure.
1929 */
1930 for (i = curr_msg_idx; i < *count; i++) {
1931 bt_message_put_ref(input_msgs[i]);
1932 }
1933
1934 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
1935
1936 end:
1937 return status;
1938 }
1939
1940 static
1941 void debug_info_msg_iter_destroy(struct debug_info_msg_iter *debug_info_msg_iter)
1942 {
1943 if (!debug_info_msg_iter) {
1944 goto end;
1945 }
1946
1947 if (debug_info_msg_iter->msg_iter) {
1948 bt_message_iterator_put_ref(
1949 debug_info_msg_iter->msg_iter);
1950 }
1951
1952 if (debug_info_msg_iter->ir_maps) {
1953 trace_ir_maps_destroy(debug_info_msg_iter->ir_maps);
1954 }
1955
1956 if (debug_info_msg_iter->debug_info_map) {
1957 g_hash_table_destroy(debug_info_msg_iter->debug_info_map);
1958 }
1959
1960 bt_fd_cache_fini(&debug_info_msg_iter->fd_cache);
1961 g_free(debug_info_msg_iter);
1962
1963 end:
1964 return;
1965 }
1966
1967 BT_HIDDEN
1968 bt_message_iterator_class_initialize_method_status debug_info_msg_iter_init(
1969 bt_self_message_iterator *self_msg_iter,
1970 bt_self_message_iterator_configuration *config,
1971 bt_self_component_port_output *self_port)
1972 {
1973 bt_message_iterator_class_initialize_method_status status;
1974 bt_message_iterator_create_from_message_iterator_status
1975 msg_iter_status;
1976 struct bt_self_component_port_input *input_port = NULL;
1977 bt_message_iterator *upstream_iterator = NULL;
1978 struct debug_info_msg_iter *debug_info_msg_iter = NULL;
1979 gchar *debug_info_field_name;
1980 int ret;
1981 bt_self_component *self_comp =
1982 bt_self_message_iterator_borrow_component(self_msg_iter);
1983 bt_logging_level log_level = bt_component_get_logging_level(
1984 bt_self_component_as_component(self_comp));
1985
1986 debug_info_msg_iter = g_new0(struct debug_info_msg_iter, 1);
1987 if (!debug_info_msg_iter) {
1988 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
1989 goto error;
1990 }
1991
1992 debug_info_msg_iter->log_level = log_level;
1993 debug_info_msg_iter->self_comp = self_comp;
1994
1995 debug_info_msg_iter->debug_info_component =
1996 bt_self_component_get_data(self_comp);
1997
1998 /* Borrow the upstream input port. */
1999 input_port = bt_self_component_filter_borrow_input_port_by_name(
2000 debug_info_msg_iter->debug_info_component->self_comp_filter,
2001 "in");
2002 if (!input_port) {
2003 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
2004 goto error;
2005 }
2006
2007 /* Create an iterator on the upstream component. */
2008 msg_iter_status = bt_message_iterator_create_from_message_iterator(
2009 self_msg_iter, input_port, &upstream_iterator);
2010 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
2011 status = (int) msg_iter_status;
2012 goto error;
2013 }
2014
2015 BT_MESSAGE_ITERATOR_MOVE_REF(
2016 debug_info_msg_iter->msg_iter, upstream_iterator);
2017
2018 /* Create hashtable that will contain debug info mapping. */
2019 debug_info_msg_iter->debug_info_map = g_hash_table_new_full(
2020 g_direct_hash, g_direct_equal, (GDestroyNotify) NULL,
2021 (GDestroyNotify) debug_info_destroy);
2022 if (!debug_info_msg_iter->debug_info_map) {
2023 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
2024 goto error;
2025 }
2026
2027 debug_info_field_name =
2028 debug_info_msg_iter->debug_info_component->arg_debug_info_field_name;
2029
2030 debug_info_msg_iter->ir_maps = trace_ir_maps_create(self_comp,
2031 debug_info_field_name, log_level);
2032 if (!debug_info_msg_iter->ir_maps) {
2033 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
2034 goto error;
2035 }
2036
2037 ret = bt_fd_cache_init(&debug_info_msg_iter->fd_cache, log_level);
2038 if (ret) {
2039 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
2040 goto error;
2041 }
2042
2043 bt_self_message_iterator_configuration_set_can_seek_forward(config,
2044 bt_message_iterator_can_seek_forward(
2045 debug_info_msg_iter->msg_iter));
2046
2047 bt_self_message_iterator_set_data(self_msg_iter, debug_info_msg_iter);
2048 debug_info_msg_iter->input_iterator = self_msg_iter;
2049
2050 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
2051 goto end;
2052
2053 error:
2054 debug_info_msg_iter_destroy(debug_info_msg_iter);
2055
2056 end:
2057 return status;
2058 }
2059
2060 BT_HIDDEN
2061 bt_message_iterator_class_can_seek_beginning_method_status
2062 debug_info_msg_iter_can_seek_beginning(bt_self_message_iterator *self_msg_iter,
2063 bt_bool *can_seek)
2064 {
2065 struct debug_info_msg_iter *debug_info_msg_iter =
2066 bt_self_message_iterator_get_data(self_msg_iter);
2067 BT_ASSERT(debug_info_msg_iter);
2068
2069 return (int) bt_message_iterator_can_seek_beginning(
2070 debug_info_msg_iter->msg_iter, can_seek);
2071 }
2072
2073 BT_HIDDEN
2074 bt_message_iterator_class_seek_beginning_method_status
2075 debug_info_msg_iter_seek_beginning(bt_self_message_iterator *self_msg_iter)
2076 {
2077 struct debug_info_msg_iter *debug_info_msg_iter =
2078 bt_self_message_iterator_get_data(self_msg_iter);
2079 bt_message_iterator_class_seek_beginning_method_status status =
2080 BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK;
2081 bt_message_iterator_seek_beginning_status seek_beg_status;
2082
2083 BT_ASSERT(debug_info_msg_iter);
2084
2085 /* Ask the upstream component to seek to the beginning. */
2086 seek_beg_status = bt_message_iterator_seek_beginning(
2087 debug_info_msg_iter->msg_iter);
2088 if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) {
2089 status = (int) seek_beg_status;
2090 goto end;
2091 }
2092
2093 /* Clear this iterator data. */
2094 trace_ir_maps_clear(debug_info_msg_iter->ir_maps);
2095 g_hash_table_remove_all(debug_info_msg_iter->debug_info_map);
2096
2097 end:
2098 return status;
2099 }
2100
2101 BT_HIDDEN
2102 void debug_info_msg_iter_finalize(bt_self_message_iterator *it)
2103 {
2104 struct debug_info_msg_iter *debug_info_msg_iter;
2105
2106 debug_info_msg_iter = bt_self_message_iterator_get_data(it);
2107 BT_ASSERT(debug_info_msg_iter);
2108
2109 debug_info_msg_iter_destroy(debug_info_msg_iter);
2110 }
This page took 0.109693 seconds and 4 git commands to generate.