From: Pedro Alves <palves@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 16/17] Disable displaced stepping if trying it fails
Date: Wed, 01 Apr 2015 23:06:00 -0000 [thread overview]
Message-ID: <1427926454-16431-17-git-send-email-palves@redhat.com> (raw)
In-Reply-To: <1427926454-16431-1-git-send-email-palves@redhat.com>
Running the testsuite with "maint set target-non-stop on" shows:
(gdb) PASS: gdb.base/valgrind-infcall.exp: continue #98 (false warning)
continue
Continuing.
dl_main (phdr=<optimized out>..., auxv=<optimized out>) at rtld.c:2302
2302 LIBC_PROBE (init_complete, 2, LM_ID_BASE, r);
Cannot access memory at address 0x400532
(gdb) PASS: gdb.base/valgrind-infcall.exp: continue #99 (false warning)
p gdb_test_infcall ()
$1 = 1
(gdb) FAIL: gdb.base/valgrind-infcall.exp: p gdb_test_infcall ()
Even though that was a native GNU/Linux test run, this test spawns
Valgrind and connects to it with "target remote". The error above is
actually orthogonal to target-non-stop. The real issue is that that
enables displaced stepping, and displaced stepping doesn't work with
Valgrind, because we can't write to the inferior memory (thus can't
copy the instruction to the scratch pad area).
I'm sure there will be other targets with the same issue, so trying to
identify Valgrind wouldn't be sufficient. The fix is to try setting
up the displaced step anyway. If we get a MEMORY_ERRORs, we disable
displaced stepping for that inferior, and fall back to doing an
in-line step-over.
Tested on x86_64 Fedora 20.
gdb/ChangeLog:
2015-04-01 Pedro Alves <palves@redhat.com>
* inferior.h (struct inferior) <displaced_stepping_failed>: New
field.
* infrun.c (use_displaced_stepping_now_p): New parameter 'inf'.
Return false if dispaced stepping failed before.
(resume): Pass the current inferior to
use_displaced_stepping_now_p. Wrap displaced_step_prepare in
TRY/CATCH. If we get a MEMORY_ERROR, set the inferior's
displaced_stepping_failed flag, and fall back to an in-line
step-over.
gdb/testsuite/ChangeLog:
2015-04-01 Pedro Alves <palves@redhat.com>
* gdb.base/valgrind-disp-step.c: New file.
* gdb.base/valgrind-disp-step.exp: New file.
---
gdb/inferior.h | 4 +
gdb/infrun.c | 66 ++++++++++++---
gdb/testsuite/gdb.base/valgrind-disp-step.c | 32 +++++++
gdb/testsuite/gdb.base/valgrind-disp-step.exp | 116 ++++++++++++++++++++++++++
4 files changed, 205 insertions(+), 13 deletions(-)
create mode 100644 gdb/testsuite/gdb.base/valgrind-disp-step.c
create mode 100644 gdb/testsuite/gdb.base/valgrind-disp-step.exp
diff --git a/gdb/inferior.h b/gdb/inferior.h
index 3cec101..d831c90 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -414,6 +414,10 @@ struct inferior
operation to get past e.g., a breakpoint. */
struct thread_info *step_over_queue_head;
+ /* True if preparing a displaced step ever failed. If so, we won't
+ try displaced stepping for this inferior again. */
+ int displaced_stepping_failed;
+
/* Per inferior data-pointers required by other GDB modules. */
REGISTRY_FIELDS;
};
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 184622a..44c0fa6 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -1619,7 +1619,7 @@ can_use_displaced_stepping_p (struct gdbarch *gdbarch)
over a breakpoint in the current thread. */
static int
-use_displaced_stepping_now_p (struct gdbarch *gdbarch,
+use_displaced_stepping_now_p (struct inferior *inf, struct gdbarch *gdbarch,
enum gdb_signal sig)
{
CORE_ADDR retval;
@@ -1628,7 +1628,8 @@ use_displaced_stepping_now_p (struct gdbarch *gdbarch,
the comments for displaced_step_prepare explain why. The
comments in the handle_inferior event for dealing with 'random
signals' explain what we do instead. */
- return (sig == GDB_SIGNAL_0
+ return (!inf->displaced_stepping_failed
+ && sig == GDB_SIGNAL_0
&& can_use_displaced_stepping_p (gdbarch)
&& entry_point_address_query (&retval));
}
@@ -1920,6 +1921,7 @@ static void keep_going_pass (struct execution_control_state *ecs);
static void prepare_to_wait (struct execution_control_state *ecs);
static int keep_going_stepped_thread (struct thread_info *tp);
static int thread_still_needs_step_over (struct thread_info *tp);
+static void stop_all_threads (void);
/* Are there any pending step-over requests for INF? If so, run one
now and return true. Otherwise, return false. */
@@ -2349,11 +2351,33 @@ resume (enum gdb_signal sig)
/* If enabled, step over breakpoints by executing a copy of the
instruction at a different address. */
- if (tp->control.trap_expected && use_displaced_stepping_now_p (gdbarch, sig))
+ if (tp->control.trap_expected
+ && use_displaced_stepping_now_p (current_inferior (), gdbarch, sig))
{
- struct displaced_step_inferior_state *displaced;
+ int prepared = -1;
- if (!displaced_step_prepare (inferior_ptid))
+ TRY
+ {
+ prepared = displaced_step_prepare (inferior_ptid);
+ }
+ CATCH (ex, RETURN_MASK_ERROR)
+ {
+ if (ex.error != MEMORY_ERROR)
+ throw_exception (ex);
+
+ if (debug_infrun)
+ {
+ fprintf_unfiltered (gdb_stdlog,
+ "infrun: disabling displaced stepping: %s\n",
+ ex.message);
+ }
+
+ /* Disable further displaced stepping attempts. */
+ current_inferior ()->displaced_stepping_failed = 1;
+ }
+ END_CATCH
+
+ if (prepared == 0)
{
if (debug_infrun)
fprintf_unfiltered (gdb_stdlog,
@@ -2364,14 +2388,29 @@ resume (enum gdb_signal sig)
discard_cleanups (old_cleanups);
return;
}
+ else if (prepared < 0)
+ {
+ /* Fallback to stepping over the breakpoint in-line. */
- /* Update pc to reflect the new address from which we will execute
- instructions due to displaced stepping. */
- pc = regcache_read_pc (get_thread_regcache (inferior_ptid));
+ if (target_is_non_stop_p ())
+ stop_all_threads ();
- displaced = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
- step = gdbarch_displaced_step_hw_singlestep (gdbarch,
- displaced->step_closure);
+ set_step_over_info (get_regcache_aspace (regcache),
+ regcache_read_pc (regcache), 0);
+ insert_breakpoints ();
+ }
+ else if (prepared > 0)
+ {
+ struct displaced_step_inferior_state *displaced;
+
+ /* Update pc to reflect the new address from which we will
+ execute instructions due to displaced stepping. */
+ pc = regcache_read_pc (get_thread_regcache (inferior_ptid));
+
+ displaced = get_displaced_stepping_state (ptid_get_pid (inferior_ptid));
+ step = gdbarch_displaced_step_hw_singlestep (gdbarch,
+ displaced->step_closure);
+ }
}
/* Do we need to do it the hard way, w/temp breakpoints? */
@@ -2500,7 +2539,7 @@ resume (enum gdb_signal sig)
if (debug_displaced
&& tp->control.trap_expected
- && use_displaced_stepping_now_p (gdbarch, sig))
+ && use_displaced_stepping_now_p (current_inferior (), gdbarch, sig))
{
struct regcache *resume_regcache = get_thread_regcache (tp->ptid);
struct gdbarch *resume_gdbarch = get_regcache_arch (resume_regcache);
@@ -7153,7 +7192,8 @@ keep_going_pass (struct execution_control_state *ecs)
remove_wps = (step_what & STEP_OVER_WATCHPOINT);
if (remove_bp
- && !use_displaced_stepping_now_p (get_regcache_arch (regcache),
+ && !use_displaced_stepping_now_p (current_inferior (),
+ get_regcache_arch (regcache),
signo))
{
set_step_over_info (get_regcache_aspace (regcache),
diff --git a/gdb/testsuite/gdb.base/valgrind-disp-step.c b/gdb/testsuite/gdb.base/valgrind-disp-step.c
new file mode 100644
index 0000000..baba74e
--- /dev/null
+++ b/gdb/testsuite/gdb.base/valgrind-disp-step.c
@@ -0,0 +1,32 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2015 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/>. */
+
+static int
+foo (void)
+{
+}
+
+int
+main (void)
+{
+ foo (); /* stop 0 */
+ foo (); /* stop 1 */
+ foo (); /* stop 2 */
+ foo (); /* stop 3 */
+ foo (); /* stop 4 */
+ return 0;
+}
diff --git a/gdb/testsuite/gdb.base/valgrind-disp-step.exp b/gdb/testsuite/gdb.base/valgrind-disp-step.exp
new file mode 100644
index 0000000..c46d331
--- /dev/null
+++ b/gdb/testsuite/gdb.base/valgrind-disp-step.exp
@@ -0,0 +1,116 @@
+# Copyright 2012-2015 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/>.
+
+# Step over breakpoints with displaced stepping off/on. We can't
+# really use displaced stepping with Valgrind, so what this really
+# tests is that GDB falls back to in-line stepping automatically
+# instead of getting stuck or crashing.
+
+if [is_remote target] {
+ # The test always runs locally.
+ return 0
+}
+
+standard_testfile .c
+if {[build_executable "failed to build" $testfile $srcfile {debug}] == -1} {
+ return -1
+}
+
+set test "spawn valgrind"
+set cmd "valgrind --vgdb-error=0 $binfile"
+set res [remote_spawn host $cmd]
+if { $res < 0 || $res == "" } {
+ verbose -log "Spawning $cmd failed."
+ unsupported $test
+ return -1
+}
+pass $test
+# Declare GDB now as running.
+set gdb_spawn_id $res
+
+# GDB started by vgdb stops already after the startup is executed, like with
+# non-extended gdbserver. It is also not correct to run/attach the inferior.
+set use_gdb_stub 1
+
+set test "valgrind started"
+# The trailing '.' differs for different memcheck versions.
+gdb_test_multiple "" $test {
+ -re "Memcheck, a memory error detector\\.?\r\n" {
+ pass $test
+ }
+ -re "valgrind: failed to start tool 'memcheck' for platform '.*': No such file or directory" {
+ unsupported $test
+ return -1
+ }
+ -re "valgrind: wrong ELF executable class" {
+ unsupported $test
+ return -1
+ }
+ -re "command not found" {
+ # The spawn succeeded, but then valgrind was not found - e.g. if
+ # we spawned SSH to a remote system.
+ unsupported $test
+ return -1
+ }
+ -re "valgrind: Bad option.*--vgdb-error=0" {
+ # valgrind is not >= 3.7.0.
+ unsupported $test
+ return -1
+ }
+}
+
+set test "vgdb prompt"
+# The trailing '.' differs for different memcheck versions.
+gdb_test_multiple "" $test {
+ -re " (target remote | \[^\r\n\]*/vgdb \[^\r\n\]*)\r\n" {
+ set vgdbcmd $expect_out(1,string)
+ pass $test
+ }
+}
+
+# Do not kill valgrind.
+set valgrind_pid [exp_pid -i [board_info host fileid]]
+unset gdb_spawn_id
+set board [host_info name]
+unset_board_info fileid
+
+clean_restart $testfile
+
+gdb_test "$vgdbcmd" " in \\.?_start .*" "target remote for vgdb"
+
+gdb_test "monitor v.set gdb_output" "valgrind output will go to gdb.*"
+
+gdb_test_no_output "set displaced-stepping off"
+gdb_breakpoint "main" "breakpoint at main"
+gdb_test "continue" " stop 0 .*" "continue to main"
+delete_breakpoints
+
+set curr_stop 0
+foreach displaced { "off" "on" } {
+ with_test_prefix "displaced $displaced" {
+
+ gdb_test_no_output "set displaced-stepping $displaced"
+
+ foreach go { "once" "twice" } {
+ with_test_prefix $go {
+ gdb_test "break" "Breakpoint .* at .*" "set breakpoint"
+ gdb_test "next" " stop [incr curr_stop] .*" "step over breakpoint"
+ }
+ }
+ }
+}
+
+# Only if valgrind got stuck.
+remote_exec host "kill -9 ${valgrind_pid}"
--
1.9.3
next prev parent reply other threads:[~2015-04-01 23:06 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-01 22:14 [PATCH 00/17] All-stop on top of non-stop Pedro Alves
2015-04-01 22:14 ` [PATCH 01/17] Fix and test "checkpoint" in non-stop mode Pedro Alves
2015-04-01 22:14 ` [PATCH 02/17] PR13858 - Can't do displaced stepping with no symbols Pedro Alves
2015-04-01 22:14 ` [PATCH 03/17] Displaced stepping debug: fetch the right regcache Pedro Alves
2015-04-07 10:47 ` Pedro Alves
2015-04-07 13:55 ` Yao Qi
2015-04-07 14:12 ` Pedro Alves
2015-04-01 22:14 ` [PATCH 09/17] Misc switch_back_to_stepped_thread cleanups Pedro Alves
2015-04-01 22:14 ` [PATCH 04/17] Change adjust_pc_after_break's prototype Pedro Alves
2015-04-01 22:14 ` [PATCH 10/17] Factor out code to re-resume stepped thread Pedro Alves
2015-04-01 22:14 ` [PATCH 13/17] Fix signal-while-stepping-over-bp-other-thread.exp on targets always in non-stop Pedro Alves
2015-04-01 22:14 ` [PATCH 08/17] Use keep_going in proceed and start_step_over too Pedro Alves
2015-04-01 22:14 ` [PATCH 17/17] native Linux: enable always non-stop by default Pedro Alves
2015-04-01 22:24 ` [PATCH 07/17] Embed the pending step-over chain in thread_info objects Pedro Alves
2015-04-01 22:40 ` [PATCH 15/17] Fix step-over-trips-on-watchpoint.exp/step-over-lands-on-breakpoint.exp race Pedro Alves
2015-04-01 22:40 ` [PATCH 06/17] Make thread_still_needs_step_over consider stepping_over_watchpoint too Pedro Alves
2015-04-01 22:41 ` [PATCH 12/17] Implement all-stop on top of a target running non-stop mode Pedro Alves
2015-04-02 14:58 ` Eli Zaretskii
2015-04-07 9:51 ` Pedro Alves
2015-04-07 10:01 ` Eli Zaretskii
2015-04-07 10:11 ` Pedro Alves
2015-04-01 23:06 ` Pedro Alves [this message]
2015-04-01 23:06 ` [PATCH 14/17] Fix interrupt-noterm.exp on targets always in non-stop Pedro Alves
2015-04-01 23:07 ` [PATCH 05/17] remote.c/all-stop: Implement TARGET_WAITKIND_NO_RESUMED and TARGET_WNOHANG Pedro Alves
2015-04-01 23:08 ` [PATCH 11/17] Teach non-stop to do in-line step-overs (stop all, step, restart) Pedro Alves
2015-04-07 12:52 ` [cancel] Re: [PATCH 00/17] All-stop on top of non-stop Pedro Alves
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=1427926454-16431-17-git-send-email-palves@redhat.com \
--to=palves@redhat.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