[patch] Avoid false valgrind warnings on linux_ptrace_test_ret_to_nx

Jan Kratochvil
2013-02-19T18:42:40Z

Hi,

running "valgrind gdb ..." produces this false warning due to the Ubuntu
kernel bug detection in GDB:

==6371== Process terminating with default action of signal 11 (SIGSEGV)
==6371== Bad permissions for mapped region at address 0x4032000
==6371== at 0x4032000: ???
==6371== by 0x603210: linux_ptrace_init_warnings (linux-ptrace.c:244)
==6371== by 0x5EF295: linux_child_post_startup_inferior (linux-nat.c:602)
==6371== by 0x5EDAE2: amd64_linux_child_post_startup_inferior (amd64-linux-nat.c:573)
==6371== by 0x5E9ACE: inf_ptrace_create_inferior (inf-ptrace.c:147)
==6371== by 0x5F0E35: linux_nat_create_inferior (linux-nat.c:1584)
==6371== by 0x76A330: find_default_create_inferior (target.c:3086)
==6371== by 0x7655C5: target_create_inferior (target.c:479)
==6371== by 0x71A7BA: run_command_1 (infcmd.c:594)
==6371== by 0x71A921: start_command (infcmd.c:649)
==6371== by 0x63184A: do_cfunc (cli-decode.c:113)
==6371== by 0x634930: cmd_func (cli-decode.c:1859)
==6371== by 0x83D673: execute_command (top.c:484)
==6371== by 0x739D70: catch_command_errors (exceptions.c:573)
==6371== by 0x73D612: captured_main (main.c:1009)
==6371== by 0x739C9E: catch_errors (exceptions.c:546)
==6371== by 0x73D6CF: gdb_main (main.c:1042)
==6371== by 0x48D6B5: main (gdb.c:34)
warning: linux_ptrace_test_ret_to_nx: PC 0x3806e309 is neither near return address 0x4032000 nor is the return instruction 0x602e33!

BTW that 0x3806e309 is in /usr/lib64/valgrind/memcheck-amd64-linux:

Dump of assembler code for function do_syscall_WRK:
0x000000003806e2f0 <+0>: mov %rdi,%rax
0x000000003806e2f3 <+3>: mov %rsi,%rdi
0x000000003806e2f6 <+6>: mov %rdx,%rsi
0x000000003806e2f9 <+9>: mov %rcx,%rdx
0x000000003806e2fc <+12>: mov %r8,%r10
0x000000003806e2ff <+15>: mov %r9,%r8
0x000000003806e302 <+18>: mov 0x8(%rsp),%r9
0x000000003806e307 <+23>: syscall
---------------------------->
0x000000003806e309 <+25>: retq

"Bad permissions for mapped region at address 0x4032000" is correct, this is
a test by GDB in GDB's linux_ptrace_test_ret_to_nx.

Therefore detect it and skip the test under valgrind.

RUNNING_ON_VALGRIND is described here - as found by Pedro:
http://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.clientreq

While they suggest
You are encouraged to copy the valgrind/*.h headers into your
project's include directory, so your program doesn't have
a compile-time dependency on Valgrind being installed.
I made a standard --with-X/--without-X external dependency as besides other
reasons the valgrind.h file is 280KB and its stripping also would not be
perfect as the macro depends on various arch-specific asm implementations.

No regressions on {x86_64,x86_64-m32,i686}-fedora19pre-linux-gnu.

Thanks,
Jan

gdb/
2013-02-19 Jan Kratochvil <jan.kratochvil@redhat.com>

* acinclude.m4: Include common/acinclude.m4.
* common/acinclude.m4: New file.
* common/linux-ptrace.c <HAVE_RUNNING_ON_VALGRIND>: Include
valgrind/valgrind.h.
(linux_ptrace_test_ret_to_nx) <HAVE_RUNNING_ON_VALGRIND>: Return.
* config.in: Regenerate.
* configure: Regenerate.
* configure.ac: Call GDB_AC_CHECK_VALGRIND.

gdb/gdbserver/
2013-02-19 Jan Kratochvil <jan.kratochvil@redhat.com>

* acinclude.m4: Include ../common/acinclude.m4.
* config.in: Regenerate.
* configure: Regenerate.
* configure.ac: Call GDB_AC_CHECK_VALGRIND.

diff --git a/gdb/acinclude.m4 b/gdb/acinclude.m4
index 25caddd..12da851 100644
--- a/gdb/acinclude.m4
+++ b/gdb/acinclude.m4
@@ -49,6 +49,8 @@ sinclude([../config/codeset.m4])

sinclude([../config/zlib.m4])

+sinclude([common/acinclude.m4])
+
## ----------------------------------------- ##
## ANSIfy the C compiler whenever possible. ##
## From Franc,ois Pinard ##
diff --git a/gdb/common/acinclude.m4 b/gdb/common/acinclude.m4
new file mode 100644
index 0000000..a4d937d9
--- /dev/null
+++ b/gdb/common/acinclude.m4
@@ -0,0 +1,43 @@
+# Copyright 2012 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/>.
+
+dnl GDB_AC_CHECK_VALGRIND
+dnl Check for HAVE_RUNNING_ON_VALGRIND.
+dnl If such symbol is defined <valgrind/valgrind.h> is also available.
+AC_DEFUN([GDB_AC_CHECK_VALGRIND], [
+ AC_ARG_WITH([valgrind],
+ [AS_HELP_STRING([--without-valgrind],
+ [do not include false valgrind warning message skip])],,
+ [with_valgrind=check])
+ if test "$with_valgrind" != no; then
+ AC_MSG_CHECKING([for RUNNING_ON_VALGRIND macro])
+ AC_CACHE_VAL(gdb_cv_check_running_on_valgrind, [
+ AC_LINK_IFELSE(
+ [AC_LANG_PROGRAM([[#include <valgrind/valgrind.h>]],
+ [[return RUNNING_ON_VALGRIND;]])],
+ [gdb_cv_check_running_on_valgrind=yes],
+ [gdb_cv_check_running_on_valgrind=no])])
+ AC_MSG_RESULT($gdb_cv_check_running_on_valgrind)
+ if test "$gdb_cv_check_running_on_valgrind" = yes; then
+ with_valgrind=yes
+ elif test "$with_valgrind" = yes; then
+ AC_ERROR([--with-valgrind was requested but it has not been found])
+ fi
+ fi
+ if test "$with_valgrind" = yes; then
+ AC_DEFINE(HAVE_RUNNING_ON_VALGRIND, 1,
+ [Define if you have <valgrind/valgrind.h> and RUNNING_ON_VALGRIND.])
+ fi
+])
diff --git a/gdb/common/linux-ptrace.c b/gdb/common/linux-ptrace.c
index 886be80..e1bd5eb 100644
--- a/gdb/common/linux-ptrace.c
+++ b/gdb/common/linux-ptrace.c
@@ -29,6 +29,10 @@
#include "gdb_assert.h"
#include "gdb_wait.h"

+#ifdef HAVE_RUNNING_ON_VALGRIND
+# include <valgrind/valgrind.h>
+#endif
+
/* Find all possible reasons we could fail to attach PID and append these
newline terminated reason strings to initialized BUFFER. '\0' termination
of BUFFER must be done by the caller. */
@@ -76,6 +80,11 @@ linux_ptrace_test_ret_to_nx (void)
long l;
int status;

+#ifdef HAVE_RUNNING_ON_VALGRIND
+ if (HAVE_RUNNING_ON_VALGRIND)
+ return;
+#endif
+
return_address = mmap (NULL, 2, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (return_address == MAP_FAILED)
diff --git a/gdb/configure.ac b/gdb/configure.ac
index e501766..a7bac76 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -1166,6 +1166,7 @@ AC_CHECK_FUNCS([canonicalize_file_name realpath getrusage getuid getgid \
ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
setrlimit getrlimit posix_madvise waitpid lstat])
AM_LANGINFO_CODESET
+GDB_AC_CHECK_VALGRIND

# Check the return and argument types of ptrace. No canned test for
# this, so roll our own.
diff --git a/gdb/gdbserver/acinclude.m4 b/gdb/gdbserver/acinclude.m4
index 0e0bdc8..c5a93b7 100644
--- a/gdb/gdbserver/acinclude.m4
+++ b/gdb/gdbserver/acinclude.m4
@@ -12,6 +12,8 @@ sinclude(../../config/acx.m4)
m4_include(../../config/depstand.m4)
m4_include(../../config/lead-dot.m4)

+sinclude([../common/acinclude.m4])
+
dnl Check for existence of a type $1 in libthread_db.h
dnl Based on BFD_HAVE_SYS_PROCFS_TYPE in bfd/bfd.m4.

diff --git a/gdb/gdbserver/configure.ac b/gdb/gdbserver/configure.ac
index 55fb461..d492e8b 100644
--- a/gdb/gdbserver/configure.ac
+++ b/gdb/gdbserver/configure.ac
@@ -71,6 +71,7 @@ AC_CHECK_HEADERS(sgtty.h termio.h termios.h sys/reg.h string.h dnl
linux/perf_event.h)
AC_CHECK_FUNCS(pread pwrite pread64 readlink)
AC_REPLACE_FUNCS(vasprintf vsnprintf)
+GDB_AC_CHECK_VALGRIND

# Check for UST
ustlibs=""