Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Rewrite bplocpy_repr
@ 2026-09-11 15:47 Tom Tromey
  0 siblings, 0 replies; only message in thread
From: Tom Tromey @ 2026-09-11 15:47 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

A user pointed out that printing the repr() of a watchpoint location
will cause gdb to crash.  See the new test in py-breakpoint.exp.

Rather than try to fix this directly, it seemed better to me to have
this function call bp_location::to_string.  This call omits a bit of
useful information so the enabled state is still handled in
bplocpy_repr.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34595
---
 gdb/python/py-breakpoint.c                   | 32 +++++++++-----------
 gdb/testsuite/gdb.python/py-bp-locations.exp | 11 ++-----
 gdb/testsuite/gdb.python/py-breakpoint.exp   |  2 ++
 3 files changed, 19 insertions(+), 26 deletions(-)

diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index ecb42cee5f9..561a6c478ac 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -1752,27 +1752,23 @@ bplocpy_repr (PyObject *py_self)
       || self->owner->bp != self->bp_loc->owner)
     return gdb_py_invalid_object_repr (py_self);
 
-  const auto enabled = self->bp_loc->enabled ? "enabled" : "disabled";
-
+  /* For most things we just defer to bp_location::to_string, but that
+     doesn't include the enabled/disabled information, which is kind
+     of handy here.  */
+  const auto enabled = self->bp_loc->enabled ? "enabled " : "disabled ";
   std::string str (enabled);
 
-  str += string_printf (" address=%s",
-			paddress (self->bp_loc->owner->gdbarch,
-				  self->bp_loc->address));
-
-  if (self->bp_loc->requested_address != self->bp_loc->address)
-    str += string_printf (" requested_address=%s",
-			  paddress (self->bp_loc->owner->gdbarch,
-				    self->bp_loc->requested_address));
-  if (self->bp_loc->symtab != nullptr)
-    str += string_printf (" source=%s:%d", self->bp_loc->symtab->filename (),
-			  self->bp_loc->line_number);
-
-  const auto fn_name = self->bp_loc->function_name.get ();
-  if (fn_name != nullptr)
+  try
     {
-      str += " in ";
-      str += fn_name;
+      str += self->bp_loc->to_string ();
+    }
+  catch (const gdb_exception_error &ignore)
+    {
+      return gdb_py_invalid_object_repr (py_self);
+    }
+  catch (const gdb_exception &except)
+    {
+      return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
   return PyUnicode_FromFormat ("<%s %s>",
diff --git a/gdb/testsuite/gdb.python/py-bp-locations.exp b/gdb/testsuite/gdb.python/py-bp-locations.exp
index 879306ea0f2..29b610a3364 100644
--- a/gdb/testsuite/gdb.python/py-bp-locations.exp
+++ b/gdb/testsuite/gdb.python/py-bp-locations.exp
@@ -32,10 +32,10 @@ if {![runto_main]} {
 }
 
 # Build a regexp string that represents the __repr__ of a
-# gdb.BreakpointLocation object.  Accepts arguments -enabled, -address,
+# gdb.BreakpointLocation object.  Accepts arguments -enabled,
 # -source, -line, and -func.
 proc build_bpl_regexp { args } {
-    parse_args [list {enabled True} [list address "$::hex"] {source ".*"} \
+    parse_args [list {enabled True} {source ".*"} \
 		    [list line "$::decimal"] {func ""}]
 
     set pattern "<gdb.BreakpointLocation"
@@ -46,12 +46,7 @@ proc build_bpl_regexp { args } {
 	set pattern "$pattern disabled"
     }
 
-    set pattern "$pattern address=${address}(?: requested_address=$::hex)?"
-    set pattern "$pattern source=${source}:${line}"
-    if {$func ne ""} {
-	set pattern "$pattern in ${func}"
-    }
-    set pattern "$pattern>"
+    append pattern " in ${func} at ${source}:${line}>"
     return $pattern
 }
 
diff --git a/gdb/testsuite/gdb.python/py-breakpoint.exp b/gdb/testsuite/gdb.python/py-breakpoint.exp
index 8b19a11b1f6..49a7a6de7d9 100644
--- a/gdb/testsuite/gdb.python/py-breakpoint.exp
+++ b/gdb/testsuite/gdb.python/py-breakpoint.exp
@@ -388,6 +388,8 @@ proc_with_prefix test_watchpoints { } {
     gdb_test "continue" \
 	".*\[Ww\]atchpoint.*result.*Old value = 0.*New value = 25.*main.*" \
 	"Test watchpoint write"
+
+    gdb_test "python print(repr(wp1.locations\[0\]))"
 }
 
 proc_with_prefix test_bkpt_internal { } {

base-commit: 0855b93cfb911a4feab76a31bb1914982b3e6979
-- 
2.55.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-09-11 15:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-11 15:47 [PATCH] Rewrite bplocpy_repr Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox