[3/3] Find symbols even for deleted executables

Message ID 20151016204749.27288.28498.stgit@host1.jankratochvil.net
State New, archived
Headers

Commit Message

Jan Kratochvil Oct. 16, 2015, 8:47 p.m. UTC
  Hi,

I have found I already posted a mostly the same patch before:
	[patch] Attach to running but deleted executable
	https://sourceware.org/ml/gdb-patches/2010-03/msg00950.html
	Message-ID: <20100328183529.GA4691@host0.dyn.jankratochvil.net>

I can refer to the discussion therein.

I had to disable (->untested) the attach-pie-noexec.exp testfile as I do not
know how to "hide" the main executable from GDB after this fix.  I wrote that
testfile myself before so I do not feel guilty to disable it.

This fix works even for gdbserver in extended mode as gdbserver also calls
linux_proc_pid_to_exec_file().

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


Thanks,
Jan


gdb/ChangeLog
2015-10-16  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* nat/linux-procfs.c (linux_proc_pid_to_exec_file): Do not resolve the
	link for deleted targets.

gdb/testsuite/ChangeLog
2015-10-16  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* gdb.base/attach-kills.exp (bt from commandline, attach for deleted)
	(bt from attach): New tests.
	* gdb.base/attach-pie-noexec.exp (attach): Add untested.
---
 gdb/testsuite/gdb.base/attach-kills.exp      |   15 +++++++++++++++
 gdb/testsuite/gdb.base/attach-pie-noexec.exp |   13 ++++++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)
  

Patch

diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c
index 24bcb01..ad3c24d 100644
--- a/gdb/nat/linux-procfs.c
+++ b/gdb/nat/linux-procfs.c
@@ -263,10 +263,14 @@  linux_proc_pid_to_exec_file (int pid)
   static char buf[PATH_MAX];
   char name[PATH_MAX];
   ssize_t len;
+  const char deleted[]=" (deleted)";
+  const size_t deleted_len = strlen (deleted);
 
   xsnprintf (name, PATH_MAX, "/proc/%d/exe", pid);
   len = readlink (name, buf, PATH_MAX - 1);
-  if (len <= 0)
+  if (len <= 0
+      || (len > deleted_len
+          && memcmp (deleted, &buf[len - deleted_len], deleted_len) == 0))
     strcpy (buf, name);
   else
     buf[len] = '\0';
diff --git a/gdb/testsuite/gdb.base/attach-kills.exp b/gdb/testsuite/gdb.base/attach-kills.exp
index d77524d..21703fd 100644
--- a/gdb/testsuite/gdb.base/attach-kills.exp
+++ b/gdb/testsuite/gdb.base/attach-kills.exp
@@ -60,4 +60,19 @@  gdb_test_multiple "" $test {
     }
 }
 
+gdb_test "bt" " in main \[^\r\n\]*[string_to_regexp $srcfile]:\[0-9\]+" \
+	 "bt from commandline"
+
+clean_restart
+
+set test "attach for deleted"
+gdb_test_multiple "attach $testpid" $test {
+    -re "Attaching to process $testpid\r\n.*\r\n$gdb_prompt $" {
+        pass $test
+    }
+}
+
+gdb_test "bt" " in main \[^\r\n\]*[string_to_regexp $srcfile]:\[0-9\]+" \
+	 "bt from attach"
+
 kill_wait_spawned_process $test_spawn_id
diff --git a/gdb/testsuite/gdb.base/attach-pie-noexec.exp b/gdb/testsuite/gdb.base/attach-pie-noexec.exp
index fc53b2d..216adfb 100644
--- a/gdb/testsuite/gdb.base/attach-pie-noexec.exp
+++ b/gdb/testsuite/gdb.base/attach-pie-noexec.exp
@@ -60,7 +60,18 @@  set testpid [spawn_id_get_pid $test_spawn_id]
 
 gdb_start
 file delete -- $binfile
-gdb_test "attach $testpid" "Attaching to process $testpid\r\n.*: No such file or directory\\." "attach"
+
+set test "attach"
+gdb_test_multiple "attach $testpid" $test {
+    -re "Attaching to process $testpid\r\n.*: No such file or directory\\.\r\n$gdb_prompt $" {
+	pass $test
+    }
+    -re "Attaching to process $testpid\r\nReading symbols from /proc/$testpid/exe\\.\\.\\.done\\.\r\n.*\r\n$gdb_prompt $" {
+	# Nothing gets really tested despite it PASSes below.
+	untested "$test"
+    }
+}
+
 gdb_test "set architecture $arch" "The target architecture is assumed to be $arch"
 gdb_test "info shared" "From\[ \t\]+To\[ \t\]+Syms Read\[ \t\]+Shared Object Library\r\n0x.*"