Check if /proc is usable on gdbserver start

Message ID 20180704073447eucas1p1541c2021074eb8faa58e49f1abe8d733~_GikXtils0124801248eucas1p1n@eucas1p1.samsung.com
State New, archived
Headers

Commit Message

Slava Barinov July 4, 2018, 7:34 a.m. UTC
  Hello,

Just lost a couple of hours trying to find while gdb can't connect to gdbserver
in my debug session.

There's a buildroot where I want to debug a binary, and I tried to connect to
it from outside, but got very weird errors like architecture mismatch or
protocol errors. At last, after switching on '--debug' for gdbserver I found a
message 'Can't open /proc/pid/' message and suddenly found that I forgot to
mount procfs in my buildroot.

I think it's better to check this before running gdbserver.

Best Regards,
Slava Barinov.
  

Comments

Pedro Alves July 4, 2018, 9:57 a.m. UTC | #1
On 07/04/2018 08:34 AM, Vyacheslav Barinov wrote:
> Hello,
> 
> Just lost a couple of hours trying to find while gdb can't connect to gdbserver
> in my debug session.
> 
> There's a buildroot where I want to debug a binary, and I tried to connect to
> it from outside, but got very weird errors like architecture mismatch or
> protocol errors. At last, after switching on '--debug' for gdbserver I found a
> message 'Can't open /proc/pid/' message and suddenly found that I forgot to
> mount procfs in my buildroot.
> 
> I think it's better to check this before running gdbserver.
I'm not certain that it's a good idea to absolute require a /proc mount.
Even though we gradually moved into relying on /proc more (or better said, on
libthread_db less), over the years we've also tried to be tolerant to
missing /proc.  I believe people had use cases for that (very constrained
containers or embedded systems?  I don't recall exactly though...)

I assume that you're on either Aarch64 or x86_64 and that the architecture
mismatch is because gdbserver couldn't determine whether the process
was a 64-bit process, because that is done by reading the /proc/PID/exe
file.  Maybe other 32-bit- or 64-bit-only archs still minimally work
without /proc.

I'm thinking that it may be prudent to downgrade this to a warning.
Would that work for you?

Thanks,
Pedro Alves
  
Slava Barinov July 4, 2018, 11:19 a.m. UTC | #2
Pedro Alves <palves@redhat.com> writes:
> On 07/04/2018 08:34 AM, Vyacheslav Barinov wrote:
>> Hello,
>> 
>> Just lost a couple of hours trying to find while gdb can't connect to gdbserver
>> in my debug session.
>> 
>> There's a buildroot where I want to debug a binary, and I tried to connect to
>> it from outside, but got very weird errors like architecture mismatch or
>> protocol errors. At last, after switching on '--debug' for gdbserver I found a
>> message 'Can't open /proc/pid/' message and suddenly found that I forgot to
>> mount procfs in my buildroot.
>> 
>> I think it's better to check this before running gdbserver.
> I'm not certain that it's a good idea to absolute require a /proc mount.
> Even though we gradually moved into relying on /proc more (or better said, on
> libthread_db less), over the years we've also tried to be tolerant to
> missing /proc.  I believe people had use cases for that (very constrained
> containers or embedded systems?  I don't recall exactly though...)
>
> I assume that you're on either Aarch64 or x86_64 and that the architecture
> mismatch is because gdbserver couldn't determine whether the process
> was a 64-bit process, because that is done by reading the /proc/PID/exe
> file.  Maybe other 32-bit- or 64-bit-only archs still minimally work
> without /proc.
>
> I'm thinking that it may be prudent to downgrade this to a warning.
> Would that work for you?
>
> Thanks,
> Pedro Alves
You are right, I use x86_64 architecture.

Okay, if the gdbserver can work without /proc, then just warning for cases
like this is enough.

Best Regards,
Vyacheslav Barinov
  

Patch

From 0d9bc48e145195b9ce04dedb3ca75ee93d90a32c Mon Sep 17 00:00:00 2001
From: Slava Barinov <v.barinov@samsung.com>
Date: Wed, 4 Jul 2018 10:20:42 +0300
Subject: [PATCH] Check if /proc is usable on gdbserver start

Add check if procfs is mount and can be accessed by gdbserver.

gdbserver/
	* linux-low.c (initialize_low): Add /proc check.

Signed-off-by: Slava Barinov <v.barinov@samsung.com>
---
 gdb/gdbserver/linux-low.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index 6e026f1aab..28273da1b9 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -7553,6 +7553,13 @@  void
 initialize_low (void)
 {
   struct sigaction sigchld_action;
+  struct stat st;
+
+  if (stat ("/proc/self", &st) != 0)
+    {
+      fprintf (stderr, "/proc is not accessible.\n");
+      exit (1);
+    }
 
   memset (&sigchld_action, 0, sizeof (sigchld_action));
   set_target_ops (&linux_target_ops);
-- 
2.18.0