[binutils-gdb] Greatly speed up rbreak

Tom Tromey tromey@sourceware.org
Mon Dec 16 18:18:35 GMT 2024


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3d6744493634e704b6977924a0cc8082886523f5

commit 3d6744493634e704b6977924a0cc8082886523f5
Author: Tom Tromey <tromey@adacore.com>
Date:   Thu Jun 6 09:42:15 2024 -0600

    Greatly speed up rbreak
    
    While debugging another issue, I noticed that 'rbreak' on a large
    program was very slow.  In particular, with 'maint time 1':
    
    (gdb) with pagination off -- rbreak ^command_display
    [...]
    Command execution time: 1940.646332 (cpu), 1960.771517 (wall)
    
    "ps" also reported that, after this command, gdb's VSZ was 4619360.
    
    Looking into this, I found something strange.  When 'rbreak' found a
    function "f" in file "file.adb", it would try to set the breakpoint
    using "break 'file.adb':'f'".
    
    This then interacted somewhat poorly with linespec.  linespec first
    expands all the symtabs matching "file.adb", but in this program this
    results in thousands of CU expansions (probably due to inlining, but I
    did not investigate).
    
    There is probably a linespec bug here.  It would make more sense for
    it to combine the file- and symbol- lookups, as this is more
    efficient.  I plan to file a bug about this at least.
    
    I tracked this "file:function" style of linespec to the earliest days
    of gdb.  Back then, "break function" would only break on the first
    "function" that was found -- it wasn't until much later that we
    changed gdb to break on all matching functions.  So, I think that
    rbreak was written this way to try to work around this limitation, and
    it seems to me that this change obsoleted the need for rbreak to
    mention the file at all.
    
    That is, "break file:function" is redundant now, because plain
    "break function" will redo that same work -- just more efficiently.
    (The only exception to this is the case where a file is given
    to rbreak -- here the extra filtering is still needed.)
    
    This patch implements this.  On the aforementioned large program, with
    this patch, rbreak still sets all the desired breakpoints (879 of
    them) but is now much faster:
    
    (gdb) with pagination off -- rbreak ^command_display
    [...]
    Command execution time: 91.702648 (cpu), 92.770430 (wall)
    
    It also reduces the VSZ number to 2539216.
    
    Regression tested on x86-64 Fedora 40.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=14340
    Approved-By: Pedro Alves <pedro@palves.net>

Diff:
---
 gdb/symtab.c | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/gdb/symtab.c b/gdb/symtab.c
index 72a18dce2b7..106e540ca48 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -18,6 +18,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "dwarf2/call-site.h"
+#include "exceptions.h"
 #include "symtab.h"
 #include "event-top.h"
 #include "gdbtypes.h"
@@ -74,6 +75,7 @@
 #include "gdbsupport/pathstuff.h"
 #include "gdbsupport/common-utils.h"
 #include <optional>
+#include <unordered_set>
 
 /* Forward declarations for local functions.  */
 
@@ -5587,7 +5589,6 @@ info_main_command (const char *args, int from_tty)
 static void
 rbreak_command (const char *regexp, int from_tty)
 {
-  std::string string;
   gdb::unique_xmalloc_ptr<char> file_name;
 
   if (regexp != nullptr)
@@ -5615,38 +5616,46 @@ rbreak_command (const char *regexp, int from_tty)
     spec.add_filename (std::move (file_name));
   std::vector<symbol_search> symbols = spec.search ();
 
+  std::unordered_set<std::string> seen_names;
   scoped_rbreak_breakpoints finalize;
   int err_count = 0;
 
   for (const symbol_search &p : symbols)
     {
-      try
+      std::string name;
+      if (p.msymbol.minsym == nullptr)
 	{
-	  if (p.msymbol.minsym == NULL)
+	  if (file_name != nullptr)
 	    {
 	      struct symtab *symtab = p.symbol->symtab ();
 	      const char *fullname = symtab_to_fullname (symtab);
-
-	      string = string_printf ("%s:'%s'", fullname,
-				      p.symbol->linkage_name ());
-	      break_command (&string[0], from_tty);
-	      print_symbol_info (p.symbol, p.block, nullptr);
+	      name = string_printf ("%s:'%s'", fullname,
+				    p.symbol->linkage_name ());
 	    }
 	  else
-	    {
-	      string = string_printf ("'%s'",
-				      p.msymbol.minsym->linkage_name ());
+	    name = p.symbol->linkage_name ();
+	}
+      else
+	name = p.msymbol.minsym->linkage_name ();
 
-	      break_command (&string[0], from_tty);
-	      gdb_printf ("<function, no debug info> %s;\n",
-			  p.msymbol.minsym->print_name ());
-	    }
+      if (!seen_names.insert (name).second)
+	continue;
+
+      try
+	{
+	  break_command (name.c_str (), from_tty);
 	}
       catch (const gdb_exception_error &ex)
 	{
 	  exception_print (gdb_stderr, ex);
 	  ++err_count;
+	  continue;
 	}
+
+      if (p.msymbol.minsym == nullptr)
+	print_symbol_info (p.symbol, p.block, nullptr);
+      else
+	gdb_printf ("<function, no debug info> %s;\n", name.c_str ());
     }
 
   int first_bp = finalize.first_breakpoint ();


More information about the Gdb-cvs mailing list