diff options
author | Sergio Durigan Junior <sergiodj@redhat.com> | 2018-02-09 18:54:41 -0500 |
---|---|---|
committer | Sergio Durigan Junior <sergiodj@redhat.com> | 2018-02-28 11:37:10 -0500 |
commit | 25e3c82c0e927398e759e2d5e35623012b8683f7 (patch) | |
tree | 018618bdacb202d8edba887996bb1348848b7df4 /gdb/gdbserver/server.c | |
parent | b4987c956dfa44ca9fd8552f63e15f5fa094b2a4 (diff) | |
download | binutils-25e3c82c0e927398e759e2d5e35623012b8683f7.zip binutils-25e3c82c0e927398e759e2d5e35623012b8683f7.tar.gz binutils-25e3c82c0e927398e759e2d5e35623012b8683f7.tar.bz2 |
Make gdbserver work with filename-only binaries
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 including "." 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 verify if the program path provided by the
user (or by the remote protocol) contains a directory separator
character. If it doesn't, it means we're dealing with a filename-only
binary, so we call "gdb_abspath" to properly expand it and transform
it into a full path. Otherwise, we leave the program path untouched.
This mimicks the behaviour seen on GDB (look at "openp" and
"attach_inferior", for example).
I am also submitting a testcase which exercises the scenario described
above. This test requires gdbserver to be executed in a different CWD
than the original, so I also created a helper function, "with_cwd" (on
testsuite/lib/gdb.exp), which takes care of cd'ing into and out of the
specified dir.
Built and regtested on BuildBot, without regressions.
gdb/ChangeLog:
2018-02-28 Sergio Durigan Junior <sergiodj@redhat.com>
Simon Marchi <simon.marchi@polymtl.ca>
* common/common-utils.c: Include "sys/stat.h".
(is_regular_file): Move here from "source.c"; change return
type to "bool".
* common/common-utils.h (is_regular_file): New prototype.
* common/pathstuff.c (contains_dir_separator): New function.
* common/pathstuff.h (contains_dir_separator): New prototype.
* source.c: Don't include "sys/stat.h".
(is_regular_file): Move to "common/common-utils.c".
gdb/gdbserver/ChangeLog:
2018-02-28 Sergio Durigan Junior <sergiodj@redhat.com>
* server.c: Include "filenames.h" and "pathstuff.h".
(program_name): Delete variable.
(program_path): New anonymous class.
(get_exec_wrapper): Use "program_path" instead of
"program_name".
(handle_v_run): Likewise.
(captured_main): Likewise.
(process_serial_event): Likewise.
gdb/testsuite/ChangeLog:
2018-02-28 Sergio Durigan Junior <sergiodj@redhat.com>
* gdb.server/abspath.exp: New file.
* lib/gdb.exp (with_cwd): New procedure.
Diffstat (limited to 'gdb/gdbserver/server.c')
-rw-r--r-- | gdb/gdbserver/server.c | 53 |
1 files changed, 40 insertions, 13 deletions
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c index 922d526..7745027 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" @@ -112,7 +114,35 @@ static int vCont_supported; space randomization feature before starting an inferior. */ int disable_randomization = 1; -static char *program_name = NULL; +static struct { + /* Set the PROGRAM_PATH. Here we adjust the path of the provided + binary if needed. */ + void set (gdb::unique_xmalloc_ptr<char> &&path) + { + m_path = std::move (path); + + /* Make sure we're using the absolute path of the inferior when + creating it. */ + if (!contains_dir_separator (m_path.get ())) + { + int reg_file_errno; + + /* Check if the file is in our CWD. If it is, then we prefix + its name with CURRENT_DIRECTORY. Otherwise, we leave the + name as-is because we'll try searching for it in $PATH. */ + if (is_regular_file (m_path.get (), ®_file_errno)) + m_path = gdb_abspath (m_path.get ()); + } + } + + /* Return the PROGRAM_PATH. */ + char *get () + { return m_path.get (); } + +private: + /* The program name, adjusted if needed. */ + gdb::unique_xmalloc_ptr<char> m_path; +} program_path; static std::vector<char *> program_args; static std::string wrapper_argv; @@ -269,10 +299,10 @@ get_exec_wrapper () char * get_exec_file (int err) { - if (err && program_name == NULL) + if (err && program_path.get () == NULL) error (_("No executable file specified.")); - return program_name; + return program_path.get (); } /* See server.h. */ @@ -3003,7 +3033,7 @@ handle_v_run (char *own_buf) { /* GDB didn't specify a program to run. Use the program from the last run with the new argument list. */ - if (program_name == NULL) + if (program_path.get () == NULL) { write_enn (own_buf); free_vector_argv (new_argv); @@ -3011,16 +3041,13 @@ handle_v_run (char *own_buf) } } else - { - xfree (program_name); - program_name = new_program_name; - } + program_path.set (gdb::unique_xmalloc_ptr<char> (new_program_name)); /* Free the old argv and install the new one. */ free_vector_argv (program_args); program_args = new_argv; - create_inferior (program_name, program_args); + create_inferior (program_path.get (), program_args); if (last_status.kind == TARGET_WAITKIND_STOPPED) { @@ -3765,13 +3792,13 @@ captured_main (int argc, char *argv[]) int i, n; n = argc - (next_arg - argv); - program_name = xstrdup (next_arg[0]); + program_path.set (gdb::unique_xmalloc_ptr<char> (xstrdup (next_arg[0]))); for (i = 1; i < n; i++) program_args.push_back (xstrdup (next_arg[i])); program_args.push_back (NULL); /* Wait till we are at first instruction in program. */ - create_inferior (program_name, program_args); + create_inferior (program_path.get (), program_args); /* We are now (hopefully) stopped at the first instruction of the target process. This assumes that the target process was @@ -4288,9 +4315,9 @@ process_serial_event (void) fprintf (stderr, "GDBserver restarting\n"); /* Wait till we are at 1st instruction in prog. */ - if (program_name != NULL) + if (program_path.get () != NULL) { - create_inferior (program_name, program_args); + create_inferior (program_path.get (), program_args); if (last_status.kind == TARGET_WAITKIND_STOPPED) { |