Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Stephan Rohr <stephan.rohr@intel.com>
To: gdb-patches@sourceware.org
Subject: [PATCH] gdb: fix location number after a breakpoint is re-set
Date: Wed, 25 Nov 2026 13:16:34 +0000	[thread overview]
Message-ID: <20261125131634.117460-1-stephan.rohr@intel.com> (raw)

If a thread stops at a breakpoint location and the breakpoint is then
re-set, e.g., because of a shared library load event, its locations are
replaced.  The bpstat of the stop still refers to the old location, and
bpstat_locno does not find it.  GDB then outputs a warning:

  warning: location number not found for breakpoint [..]

Also, the convenience variable $_hit_locno is set to 1, which may not be
the location the thread stopped at.

Fix this by looking up the new location based on the program space and
address of the old location.
---
 gdb/breakpoint.c                              | 18 +++++
 .../gdb.base/breakpoint-reset-location.exp    | 81 +++++++++++++++++++
 2 files changed, 99 insertions(+)
 create mode 100644 gdb/testsuite/gdb.base/breakpoint-reset-location.exp

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index ca8ae98a7b7..a3f69037b8d 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -5102,6 +5102,24 @@ bpstat_locno (const bpstat *bs)
 	  ++locno;
 	}
 
+      /* Breakpoint B may be re-set after BS is created, e.g., if symbols are
+	 loaded while the program is stopped at BL.  In this case, BL is no
+	 longer a location of B and the check above does not work.  Instead,
+	 use the location that replaced it, if available.  */
+      if (bl->owner != b)
+	{
+	  locno = 1;
+	  for (bp_location &loc : b->locations ())
+	    {
+	      if (loc.pspace == bl->pspace && loc.address == bl->address)
+		return locno;
+
+	      ++locno;
+	    }
+
+	  return 0;
+	}
+
       warning (_("location number not found for breakpoint %d address %s."),
 	       b->number, paddress (bl->gdbarch, bl->address));
     }
diff --git a/gdb/testsuite/gdb.base/breakpoint-reset-location.exp b/gdb/testsuite/gdb.base/breakpoint-reset-location.exp
new file mode 100644
index 00000000000..a44958b38bb
--- /dev/null
+++ b/gdb/testsuite/gdb.base/breakpoint-reset-location.exp
@@ -0,0 +1,81 @@
+# Copyright 2026 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Re-set a breakpoint with two locations while the program is stopped
+# at one of them, e.g., because symbols are loaded.  Test that GDB still finds
+# the number of that location and does not warn that it cannot find it.
+
+require allow_shlib_tests
+
+standard_testfile ctxobj-m.c ctxobj-f.c ctxobj-v.c
+
+# Include ctxobj-f.c in both the executable and the shared library to get
+# two breakpoint locations.
+set libsrc [list "${srcdir}/${subdir}/${srcfile3}" \
+		 "${srcdir}/${subdir}/${srcfile2}"]
+set libobj [standard_output_file libctxobj.so]
+set libobj_opts { debug
+		  additional_flags=-DVERSION=203
+		  additional_flags=-DGET_VERSION=get_version_2 }
+
+if { [gdb_compile_shlib $libsrc $libobj $libobj_opts] != "" } {
+    untested "failed to compile shared library"
+    return
+}
+
+if { [build_executable "failed to build" $testfile \
+	  [list $srcfile $srcfile2 $srcfile3] \
+	  [list debug shlib=$libobj \
+	       additional_flags=-DVERSION=104 \
+	       additional_flags=-DGET_VERSION=get_version_1]] } {
+    return
+}
+
+clean_restart $testfile
+gdb_load_shlib $libobj
+
+if {![runto_main]} {
+    return
+}
+
+set bp_line [gdb_get_line_number "STOP" "ctxobj-f.c"]
+gdb_test "break ctxobj-f.c:$bp_line" \
+    "Breakpoint $decimal at $hex: ctxobj-f.c:$bp_line. \\(2 locations\\)"
+
+# Continue to the breakpoint location in the executable.  The order is not
+# fixed, so record the location's number.
+set hit "unknown"
+gdb_test_multiple "continue" "continue to get_version_1" {
+    -re -wrap "Breakpoint ($decimal\\.$decimal), get_version_1 \\(\\).*" {
+	set hit $expect_out(1,string)
+	pass $gdb_test_name
+    }
+}
+
+# Reloading the symbols of the shared libraries re-sets the breakpoints.
+gdb_test_no_output "nosharedlibrary"
+gdb_test "sharedlibrary" ".*" "reload the shared libraries"
+
+# Test that the breakpoint location is still found.  GDB used to warn
+# about the old location after every command and set $_hit_locno to 1.
+gdb_test_multiple "printf \"%d.%d\\n\", \$_hit_bpnum, \$_hit_locno" \
+    "\$_hit_bpnum.\$_hit_locno after re-set" {
+    -re -wrap "warning: location number not found.*" {
+	fail $gdb_test_name
+    }
+    -re -wrap "\r\n[string_to_regexp $hit]" {
+	pass $gdb_test_name
+    }
+}
-- 
2.43.0

________________________________________
Intel Deutschland GmbH 

Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany 

Tel: +49 (89) 99143-0 

www.intel.de 

Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman

Chairperson of the Supervisory Board: Sonja Pierer

Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


                 reply	other threads:[~2026-11-25 13:17 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=20261125131634.117460-1-stephan.rohr@intel.com \
    --to=stephan.rohr@intel.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