public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Greatly speed up rbreak
@ 2024-06-13 15:41 Tom Tromey
  2024-06-13 15:47 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2024-06-13 15:41 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

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 38.
---
 gdb/symtab.c | 46 +++++++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 15 deletions(-)

diff --git a/gdb/symtab.c b/gdb/symtab.c
index 5e65b893d6b..33fe18f92c7 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"
@@ -73,6 +74,7 @@
 #include "gdbsupport/pathstuff.h"
 #include "gdbsupport/common-utils.h"
 #include <optional>
+#include <unordered_set>
 
 /* Forward declarations for local functions.  */
 
@@ -5556,7 +5558,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)
@@ -5584,28 +5585,43 @@ 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;
   for (const symbol_search &p : symbols)
     {
-      if (p.msymbol.minsym == NULL)
+      std::string name;
+      if (p.msymbol.minsym == 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);
+	  if (file_name != nullptr)
+	    {
+	      struct symtab *symtab = p.symbol->symtab ();
+	      const char *fullname = symtab_to_fullname (symtab);
+	      name = string_printf ("%s:'%s'", fullname,
+				    p.symbol->linkage_name ());
+	    }
+	  else
+	    name = p.symbol->linkage_name ();
 	}
       else
-	{
-	  string = string_printf ("'%s'",
-				  p.msymbol.minsym->linkage_name ());
+	name = p.msymbol.minsym->linkage_name ();
+
+      if (!seen_names.insert (name).second)
+	continue;
 
-	  break_command (&string[0], from_tty);
-	  gdb_printf ("<function, no debug info> %s;\n",
-		      p.msymbol.minsym->print_name ());
+      try
+	{
+	  break_command (name.c_str (), from_tty);
+	}
+      catch (const gdb_exception_error &ex)
+	{
+	  exception_print (gdb_stderr, ex);
+	  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 ());
     }
 }
 \f
-- 
2.44.0


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

* Re: [PATCH] Greatly speed up rbreak
  2024-06-13 15:41 [PATCH] Greatly speed up rbreak Tom Tromey
@ 2024-06-13 15:47 ` Tom Tromey
  2024-06-14 19:32   ` Pedro Alves
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2024-06-13 15:47 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

>>>>> "Tom" == Tom Tromey <tromey@adacore.com> writes:

Tom> Regression tested on x86-64 Fedora 38.

I forgot to mention - this is based on my earlier patch to catch
exceptions in rbreak.  So, while this applies cleanly, it does also
include that change.

Tom

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

* Re: [PATCH] Greatly speed up rbreak
  2024-06-13 15:47 ` Tom Tromey
@ 2024-06-14 19:32   ` Pedro Alves
  0 siblings, 0 replies; 3+ messages in thread
From: Pedro Alves @ 2024-06-14 19:32 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 2024-06-13 16:47, Tom Tromey wrote:
>>>>>> "Tom" == Tom Tromey <tromey@adacore.com> writes:
> 
> Tom> Regression tested on x86-64 Fedora 38.
> 
> I forgot to mention - this is based on my earlier patch to catch
> exceptions in rbreak.  So, while this applies cleanly, it does also
> include that change.

I commented on that other patch.

For the speed up change:

  Approved-By: Pedro Alves <pedro@palves.net>


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

end of thread, other threads:[~2024-06-14 19:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-13 15:41 [PATCH] Greatly speed up rbreak Tom Tromey
2024-06-13 15:47 ` Tom Tromey
2024-06-14 19:32   ` Pedro Alves

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).