Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH] Rewrite bplocpy_repr
Date: Fri, 11 Sep 2026 09:47:26 -0600	[thread overview]
Message-ID: <20260911154726.667576-1-tromey@adacore.com> (raw)

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


                 reply	other threads:[~2026-09-11 15:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260911154726.667576-1-tromey@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox