From: Sergio Durigan Junior <sergiodj@redhat.com>
To: GDB Patches <gdb-patches@sourceware.org>
Cc: Simon Marchi <simon.marchi@ericsson.com>,
Sergio Durigan Junior <sergiodj@redhat.com>
Subject: [PATCH 2/2] Make gdbserver work with filename-only binaries
Date: Sat, 10 Feb 2018 01:42:00 -0000 [thread overview]
Message-ID: <20180210014241.19278-3-sergiodj@redhat.com> (raw)
In-Reply-To: <20180210014241.19278-1-sergiodj@redhat.com>
Simon mentioned on IRC that, after the startup-with-shell feature has
been implemented on gdbserver, it is not possible to specify a
filename-only binary, like:
$ gdbserver :1234 a.out
/bin/bash: line 0: exec: a.out: not found
During startup program exited with code 127.
Exiting
This happens on systems where the current directory "." is not listed
in the PATH environment variable. Although include "." in the PATH
variable is a possible workaround, this can be considered a regression
because before startup-with-shell it was possible to use only the
filename (due to reason that gdbserver used "exec*" directly).
The idea of the patch is to perform a call to "gdb_abspath" and adjust
the PROGRAM_NAME variable before the call to "create_inferior". This
adjustment will consist of tilde-expansion or prefixing PROGRAM_NAME
using the CURRENT_DIRECTORY (a variable that was specific to GDB, but
has been put into common/common-defs.h and now is set/used by
gdbserver as well), thus transforming PROGRAM_NAME in an absolute
path.
This mimicks the behaviour seen on GDB (look at "openp" and
"attach_inferior", for example). Now, we'll always execute the binary
using its full path on gdbserver.
I am also submitting a testcase which exercises the scenario described
above. Because the test requires copying (and deleting) files
locally, I decided to restrict its execution to non-remote
targets/hosts. I've also had to do a minor adjustment on
gdb.server/non-existing-program.exp's regexp in order to match the
correct error message.
Built and regtested on BuildBot, without regressions.
gdb/ChangeLog:
2018-02-09 Sergio Durigan Junior <sergiodj@redhat.com>
* common/common-def.h (current_directory): Move here...
* defs.h (current_directory): ...from here.
gdb/gdbserver/ChangeLog:
2018-02-09 Sergio Durigan Junior <sergiodj@redhat.com>
* server.c: Include "filenames.h" and "pathstuff.h".
(current_directory): New global variable.
(adjust_program_name_path): New function.
(attach_inferior): Call "adjust_program_name_path" before
"create_inferior".
(captured_main): Likewise.
(process_serial_event): Likewise.
gdb/testsuite/ChangeLog:
2018-02-09 Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.server/abspath.exp: New file.
* gdb.server/non-existing-program.exp: Adjust regex for the
"startup-with-shell enabled" case.
---
gdb/common/common-defs.h | 3 ++
gdb/defs.h | 4 --
gdb/gdbserver/server.c | 36 +++++++++++++++
gdb/testsuite/gdb.server/abspath.exp | 54 +++++++++++++++++++++++
gdb/testsuite/gdb.server/non-existing-program.exp | 2 +-
5 files changed, 94 insertions(+), 5 deletions(-)
create mode 100644 gdb/testsuite/gdb.server/abspath.exp
diff --git a/gdb/common/common-defs.h b/gdb/common/common-defs.h
index acbc32ca69..881a4eaaff 100644
--- a/gdb/common/common-defs.h
+++ b/gdb/common/common-defs.h
@@ -91,4 +91,7 @@
/* Pull in gdb::unique_xmalloc_ptr. */
#include "common/gdb_unique_ptr.h"
+/* String containing the current directory (what getwd would return). */
+extern char *current_directory;
+
#endif /* COMMON_DEFS_H */
diff --git a/gdb/defs.h b/gdb/defs.h
index 4fb2129b30..61be475858 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -423,10 +423,6 @@ enum info_proc_what
IP_ALL
};
-/* * String containing the current directory (what getwd would return). */
-
-extern char *current_directory;
-
/* * Default radixes for input and output. Only some values supported. */
extern unsigned input_radix;
extern unsigned output_radix;
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index cb02b58507..2d87886daf 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -39,6 +39,8 @@
#include "common-inferior.h"
#include "job-control.h"
#include "environ.h"
+#include "filenames.h"
+#include "pathstuff.h"
#include "common/selftest.h"
@@ -56,6 +58,10 @@
break; \
}
+/* String containing the current directory (what getwd would return). */
+
+char *current_directory;
+
/* The environment to pass to the inferior when creating it. */
static gdb_environ our_environ;
@@ -279,6 +285,23 @@ get_environ ()
return &our_environ;
}
+/* Verify if PROGRAM_NAME is an absolute path, and perform path
+ adjustment/expansion if not. */
+
+static void
+adjust_program_name_path ()
+{
+ /* Make sure we're using the absolute path of the inferior when
+ creating it. */
+ if (!IS_ABSOLUTE_PATH (program_name))
+ {
+ char *tmp_program_name = program_name;
+
+ program_name = gdb_abspath (program_name).release ();
+ xfree (tmp_program_name);
+ }
+}
+
static int
attach_inferior (int pid)
{
@@ -3012,6 +3035,8 @@ handle_v_run (char *own_buf)
program_name = new_program_name;
}
+ adjust_program_name_path ();
+
/* Free the old argv and install the new one. */
free_vector_argv (program_args);
program_args = new_argv;
@@ -3539,6 +3564,13 @@ captured_main (int argc, char *argv[])
const char *selftest_filter = NULL;
#endif
+ current_directory = getcwd (NULL, 0);
+ if (current_directory == NULL)
+ {
+ warning (_("%s: error finding working directory"),
+ safe_strerror (errno));
+ }
+
while (*next_arg != NULL && **next_arg == '-')
{
if (strcmp (*next_arg, "--version") == 0)
@@ -3759,6 +3791,8 @@ captured_main (int argc, char *argv[])
program_args.push_back (xstrdup (next_arg[i]));
program_args.push_back (NULL);
+ adjust_program_name_path ();
+
/* Wait till we are at first instruction in program. */
create_inferior (program_name, program_args);
@@ -4279,6 +4313,8 @@ process_serial_event (void)
/* Wait till we are at 1st instruction in prog. */
if (program_name != NULL)
{
+ adjust_program_name_path ();
+
create_inferior (program_name, program_args);
if (last_status.kind == TARGET_WAITKIND_STOPPED)
diff --git a/gdb/testsuite/gdb.server/abspath.exp b/gdb/testsuite/gdb.server/abspath.exp
new file mode 100644
index 0000000000..fbde5ee537
--- /dev/null
+++ b/gdb/testsuite/gdb.server/abspath.exp
@@ -0,0 +1,54 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2018 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/>.
+
+# Test that gdbserver performs path expansion/adjustment when we
+# provide just a filename (without any path specifications) to it.
+
+load_lib gdbserver-support.exp
+
+standard_testfile normal.c
+
+if { [skip_gdbserver_tests] } {
+ return 0
+}
+
+# We only test things locally, and on native-gdbserver
+if { [is_remote target] || [is_remote host] || ![use_gdb_stub] } {
+ return 0
+}
+
+if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
+ return -1
+}
+
+set target_exec [gdbserver_download_current_prog]
+set target_execname [file tail $target_exec]
+# We temporarily copy the file to our current directory
+file copy -force $target_exec [pwd]
+set res [gdbserver_start "" $target_execname]
+
+set gdbserver_protocol [lindex $res 0]
+set gdbserver_gdbport [lindex $res 1]
+gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport
+
+if { [runto_main] } {
+ pass "load filename without absolute path"
+} else {
+ fail "load filename without absolute path"
+}
+
+file delete -force "[pwd]/$target_execname"
diff --git a/gdb/testsuite/gdb.server/non-existing-program.exp b/gdb/testsuite/gdb.server/non-existing-program.exp
index a8e057d590..af5b412992 100644
--- a/gdb/testsuite/gdb.server/non-existing-program.exp
+++ b/gdb/testsuite/gdb.server/non-existing-program.exp
@@ -48,7 +48,7 @@ expect {
}
# Likewise, but with startup-with-shell enabled, which is the
# default behaviour.
- -re "stdin/stdout redirected.*exec: non-existing-program: not found\r\nDuring startup program exited with code 127\.\r\nExiting\r\n$" {
+ -re "stdin/stdout redirected.*: .*non-existing-program: No such file or directory\r\nDuring startup program exited with code 127\.\r\nExiting\r\n$" {
set saw_exiting 1
exp_continue
}
--
2.14.3
next prev parent reply other threads:[~2018-02-10 1:42 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-10 1:42 [PATCH 0/2] " Sergio Durigan Junior
2018-02-10 1:42 ` Sergio Durigan Junior [this message]
2018-02-12 4:18 ` [PATCH 2/2] " Simon Marchi
2018-02-12 19:16 ` Sergio Durigan Junior
2018-02-21 8:05 ` Joel Brobecker
2018-02-12 19:57 ` [PATCH 0/2] " Sergio Durigan Junior
2018-02-12 19:57 ` [PATCH v2 2/2] " Sergio Durigan Junior
2018-02-13 4:35 ` Simon Marchi
2018-02-22 18:37 ` Sergio Durigan Junior
2018-02-21 12:29 ` Pedro Alves
2018-02-27 0:20 ` Sergio Durigan Junior
2018-02-28 3:32 ` Sergio Durigan Junior
2018-02-12 19:57 ` [PATCH v2 1/2] Create new common/pathstuff.[ch] Sergio Durigan Junior
2018-02-28 3:27 ` [PATCH v3 0/2] Make gdbserver work with filename-only binaries Sergio Durigan Junior
2018-02-28 3:27 ` [PATCH v3 2/2] " Sergio Durigan Junior
2018-02-28 3:58 ` Sergio Durigan Junior
2018-02-28 5:33 ` Simon Marchi
2018-02-28 7:09 ` Metzger, Markus T
2018-02-28 16:30 ` Sergio Durigan Junior
2018-02-28 5:46 ` Simon Marchi
2018-02-28 16:29 ` Sergio Durigan Junior
2018-02-28 16:40 ` Sergio Durigan Junior
2018-02-28 3:27 ` [PATCH v3 1/2] Create new common/pathstuff.[ch] Sergio Durigan Junior
2018-02-28 5:02 ` Simon Marchi
2018-02-28 16:46 ` Sergio Durigan Junior
2018-02-28 16:39 ` Sergio Durigan Junior
2018-03-01 2:23 ` [PATCH v3 0/2] Make gdbserver work with filename-only binaries Sergio Durigan Junior
2018-03-01 2:55 ` Joel Brobecker
2018-03-01 13:08 ` Christophe Lyon
2018-03-01 13:18 ` Simon Marchi
2018-03-01 19:50 ` Sergio Durigan Junior
2018-03-01 20:20 ` [PATCH] Conditionally include "<windows.h>" on common/pathstuff.c (and unbreak build on mingw*) Sergio Durigan Junior
2018-03-01 20:47 ` Simon Marchi
2018-03-02 11:46 ` Christophe Lyon
2018-03-02 12:35 ` Sergio Durigan Junior
2018-03-02 11:11 ` Yao Qi
2018-03-02 12:29 ` Sergio Durigan Junior
2018-03-02 12:37 ` Sergio Durigan Junior
2018-03-05 12:07 ` Yao Qi
2018-03-02 13:32 ` Eli Zaretskii
2018-03-02 15:15 ` Simon Marchi
2018-03-02 18:20 ` Sergio Durigan Junior
2018-03-03 7:36 ` Eli Zaretskii
2018-03-01 17:37 ` [PATCH v3 0/2] Make gdbserver work with filename-only binaries Sergio Durigan Junior
2018-03-02 3:20 ` Joel Brobecker
2018-02-28 16:47 ` [obvious/pushed] Change order of error message printed when gdbserver can't find CWD Sergio Durigan Junior
2018-02-10 1:42 ` [PATCH 1/2] Create new common/pathstuff.[ch] Sergio Durigan Junior
2018-02-11 22:14 ` Simon Marchi
2018-02-12 19:01 ` Sergio Durigan Junior
2018-02-21 7:56 ` Joel Brobecker
2018-02-22 18:43 ` Sergio Durigan Junior
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=20180210014241.19278-3-sergiodj@redhat.com \
--to=sergiodj@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=simon.marchi@ericsson.com \
/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