public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Check if /proc is usable on gdbserver start
       [not found] <CGME20180704073447eucas1p1541c2021074eb8faa58e49f1abe8d733@eucas1p1.samsung.com>
@ 2018-07-04  7:34 ` Vyacheslav Barinov
  2018-07-04  9:57   ` Pedro Alves
  0 siblings, 1 reply; 4+ messages in thread
From: Vyacheslav Barinov @ 2018-07-04  7:34 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 542 bytes --]

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.
    

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Check-if-proc-is-usable-on-gdbserver-start.patch --]
[-- Type: text/x-patch, Size: 982 bytes --]

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Check if /proc is usable on gdbserver start
  2018-07-04  7:34 ` [PATCH] Check if /proc is usable on gdbserver start Vyacheslav Barinov
@ 2018-07-04  9:57   ` Pedro Alves
  2018-07-04 11:19     ` Vyacheslav Barinov
  0 siblings, 1 reply; 4+ messages in thread
From: Pedro Alves @ 2018-07-04  9:57 UTC (permalink / raw)
  To: Vyacheslav Barinov, gdb-patches

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Check if /proc is usable on gdbserver start
  2018-07-04  9:57   ` Pedro Alves
@ 2018-07-04 11:19     ` Vyacheslav Barinov
  2018-07-04 15:23       ` [pushed] Warn if /proc is not accessible (was: Re: [PATCH] Check if /proc is usable on gdbserver start) Pedro Alves
  0 siblings, 1 reply; 4+ messages in thread
From: Vyacheslav Barinov @ 2018-07-04 11:19 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [pushed] Warn if /proc is not accessible (was: Re: [PATCH] Check if /proc is usable on gdbserver start)
  2018-07-04 11:19     ` Vyacheslav Barinov
@ 2018-07-04 15:23       ` Pedro Alves
  0 siblings, 0 replies; 4+ messages in thread
From: Pedro Alves @ 2018-07-04 15:23 UTC (permalink / raw)
  To: Vyacheslav Barinov; +Cc: gdb-patches

On 07/04/2018 12:19 PM, Vyacheslav Barinov wrote:
> 
> Okay, if the gdbserver can work without /proc, then just warning for cases
> like this is enough.

Alright, here's what I've merged.

I added a new linux_ptrace_init_warnings routine to nat/linux-procfs.c
very similar in spirit to the existing linux_ptrace_init_warnings
and called it from the same places.  This makes native debugging
with GDB warn too.

From 7c65a87e84f79155a9e9f59890340f85387df8a1 Mon Sep 17 00:00:00 2001
From: Vyacheslav Barinov <v.barinov@samsung.com>
Date: Wed, 4 Jul 2018 16:13:29 +0100
Subject: [PATCH] Warn if /proc is not accessible

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.

Make discovering the problem easier by making GDB / GDBserver warn
(even without --debug) if /proc can not be accessed.

Native debugging:

 (gdb) start
 Temporary breakpoint 1 at 0x400835: file test.c, line 10.
 Starting program: /tmp/test
 warning: /proc is not accessible.

GDBserver/remote debugging:

 $ ./gdbserver :9999 ./gdbserver
 gdbserver: /proc is not accessible.

gdb/ChangeLog:
2018-07-04  Vyacheslav Barinov  <v.barinov@samsung.com>
	    Pedro Alves  <palves@redhat.com>

	* linux-nat.c (linux_init_ptrace): Rename to ...
	(linux_init_ptrace_procfs): ... this.  Call
	linux_proc_init_warnings.
	(linux_nat_target::post_attach)
	(linux_nat_target::post_startup_inferior): Adjust.
	* nat/linux-procfs.c (linux_proc_init_warnings): Define function.
	* nat/linux-procfs.h (linux_proc_init_warnings): Declare function.

gdb/gdbserver/ChangeLog:
2018-07-04  Vyacheslav Barinov  <v.barinov@samsung.com>
	    Pedro Alves  <palves@redhat.com>

	* linux-low.c (initialize_low): Call linux_proc_init_warnings.
---
 gdb/gdbserver/linux-low.c |  1 +
 gdb/linux-nat.c           | 11 ++++++-----
 gdb/nat/linux-procfs.c    | 17 +++++++++++++++++
 gdb/nat/linux-procfs.h    |  5 +++++
 4 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index 6e026f1aab8..7c396ae3662 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -7558,6 +7558,7 @@ initialize_low (void)
   set_target_ops (&linux_target_ops);
 
   linux_ptrace_init_warnings ();
+  linux_proc_init_warnings ();
 
   sigchld_action.sa_handler = sigchld_handler;
   sigemptyset (&sigchld_action.sa_mask);
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 567cb064d3a..18d9a3872a5 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -384,18 +384,19 @@ linux_nat_ptrace_options (int attached)
   return options;
 }
 
-/* Initialize ptrace warnings and check for supported ptrace
-   features given PID.
+/* Initialize ptrace and procfs warnings and check for supported
+   ptrace features given PID.
 
    ATTACHED should be nonzero iff we attached to the inferior.  */
 
 static void
-linux_init_ptrace (pid_t pid, int attached)
+linux_init_ptrace_procfs (pid_t pid, int attached)
 {
   int options = linux_nat_ptrace_options (attached);
 
   linux_enable_event_reporting (pid, options);
   linux_ptrace_init_warnings ();
+  linux_proc_init_warnings ();
 }
 
 linux_nat_target::~linux_nat_target ()
@@ -404,13 +405,13 @@ linux_nat_target::~linux_nat_target ()
 void
 linux_nat_target::post_attach (int pid)
 {
-  linux_init_ptrace (pid, 1);
+  linux_init_ptrace_procfs (pid, 1);
 }
 
 void
 linux_nat_target::post_startup_inferior (ptid_t ptid)
 {
-  linux_init_ptrace (ptid.pid (), 0);
+  linux_init_ptrace_procfs (ptid.pid (), 0);
 }
 
 /* Return the number of known LWPs in the tgid given by PID.  */
diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c
index 1c236c543d9..6c15ea5cce3 100644
--- a/gdb/nat/linux-procfs.c
+++ b/gdb/nat/linux-procfs.c
@@ -357,3 +357,20 @@ linux_proc_pid_to_exec_file (int pid)
 
   return buf;
 }
+
+/* See linux-procfs.h.  */
+
+void
+linux_proc_init_warnings ()
+{
+  static bool warned = false;
+
+  if (warned)
+    return;
+  warned = true;
+
+  struct stat st;
+
+  if (stat ("/proc/self", &st) != 0)
+    warning (_("/proc is not accessible."));
+}
diff --git a/gdb/nat/linux-procfs.h b/gdb/nat/linux-procfs.h
index efb2911cb8f..2f8932e89c7 100644
--- a/gdb/nat/linux-procfs.h
+++ b/gdb/nat/linux-procfs.h
@@ -80,4 +80,9 @@ extern int linux_proc_task_list_dir_exists (pid_t pid);
 
 extern char *linux_proc_pid_to_exec_file (int pid);
 
+/* Display possible problems on this system.  Display them only once
+   per GDB execution.  */
+
+extern void linux_proc_init_warnings ();
+
 #endif /* COMMON_LINUX_PROCFS_H */
-- 
2.14.4

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-07-04 15:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20180704073447eucas1p1541c2021074eb8faa58e49f1abe8d733@eucas1p1.samsung.com>
2018-07-04  7:34 ` [PATCH] Check if /proc is usable on gdbserver start Vyacheslav Barinov
2018-07-04  9:57   ` Pedro Alves
2018-07-04 11:19     ` Vyacheslav Barinov
2018-07-04 15:23       ` [pushed] Warn if /proc is not accessible (was: Re: [PATCH] Check if /proc is usable on gdbserver start) Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).