public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fixing get_builder() function in dwarf2/read.c
@ 2020-06-14 16:49 watashiwaher
  2020-06-14 16:58 ` Simon Marchi
  0 siblings, 1 reply; 6+ messages in thread
From: watashiwaher @ 2020-06-14 16:49 UTC (permalink / raw)
  To: gdb-patches

Explanation
I found that some binaries with debug symbols halt gdb. And the problem was
in the get_builder() function inside dwarf2/read.c file. The function
recursively called itself until infinity.  So I tried to fix this issue by
eliminating recursion and adding the check for cycle detection, and
returning nullptr if cycle detected.

ChangeLog
2020-06-15  Slava Mitin <watashiwaher@gmail.com>

* dwarf2/read.c (get_builder): Added check for cycles


diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index e3073fe43c..e8c6d3bec3 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -594,15 +594,51 @@ struct dwarf2_cu
   /* Get the buildsym_compunit for this CU.  */
   buildsym_compunit *get_builder ()
   {
-    /* If this CU has a builder associated with it, use that.  */
-    if (m_builder != nullptr)
-      return m_builder.get ();
+    /* Number of iterations before the updating of
+       the recursion_check_cu pointer  */
+    const int RECURSION_CHECK_UPDATE_INTERVAL = 2;

-    /* Otherwise, search ancestors for a valid builder.  */
-    if (ancestor != nullptr)
-      return ancestor->get_builder ();
+    /* Replacing recursion calls of get_builder
+       to the loop  */
+    dwarf2_cu *current_cu = this;

-    return nullptr;
+    /* Sometimes CUs can include each other recuresively,
+       so we need to stop iterating some day, if they do  */
+    dwarf2_cu *recursion_check_cu = nullptr;
+
+    /* Counter of intervals */
+    int recursion_check_counter = 1;
+
+    while (true) {
+      /* If this CU has a builder associated with it, use that.  */
+      if (current_cu->m_builder != nullptr) {
+        return current_cu->m_builder.get();
+      }
+
+      /* Otherwise, search ancestors for a valid builder.  */
+      if (current_cu->ancestor != nullptr) {
+        current_cu = current_cu->ancestor;
+      } else {
+        return nullptr;
+      }
+
+      /* If CU was seen already, return.  */
+      if (recursion_check_cu == current_cu) {
+        return nullptr;
+      }
+
+      /* Update the variable used to check the recursion  */
+      if (recursion_check_counter == RECURSION_CHECK_UPDATE_INTERVAL) {
+        if (recursion_check_cu == nullptr) {
+          recursion_check_cu = this;
+        } else {
+          recursion_check_cu = recursion_check_cu->ancestor;
+        }
+        recursion_check_counter = 1;
+      } else {
+        recursion_check_counter++;
+      }
+    }
   }
 };

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-06-15 15:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-14 16:49 [PATCH] Fixing get_builder() function in dwarf2/read.c watashiwaher
2020-06-14 16:58 ` Simon Marchi
2020-06-14 17:07   ` watashiwaher
2020-06-15 14:58     ` Simon Marchi
2020-06-15 15:06       ` watashiwaher
2020-06-15 15:13         ` watashiwaher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).