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/common | |
parent | b4987c956dfa44ca9fd8552f63e15f5fa094b2a4 (diff) | |
download | gdb-25e3c82c0e927398e759e2d5e35623012b8683f7.zip gdb-25e3c82c0e927398e759e2d5e35623012b8683f7.tar.gz gdb-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/common')
-rw-r--r-- | gdb/common/common-utils.c | 32 | ||||
-rw-r--r-- | gdb/common/common-utils.h | 5 | ||||
-rw-r--r-- | gdb/common/pathstuff.c | 14 | ||||
-rw-r--r-- | gdb/common/pathstuff.h | 4 |
4 files changed, 55 insertions, 0 deletions
diff --git a/gdb/common/common-utils.c b/gdb/common/common-utils.c index ae2dd9d..80de826 100644 --- a/gdb/common/common-utils.c +++ b/gdb/common/common-utils.c @@ -20,6 +20,7 @@ #include "common-defs.h" #include "common-utils.h" #include "host-defs.h" +#include <sys/stat.h> #include <ctype.h> /* The xmalloc() (libiberty.h) family of memory management routines. @@ -408,3 +409,34 @@ stringify_argv (const std::vector<char *> &args) return ret; } + +/* See common/common-utils.h. */ + +bool +is_regular_file (const char *name, int *errno_ptr) +{ + struct stat st; + const int status = stat (name, &st); + + /* Stat should never fail except when the file does not exist. + If stat fails, analyze the source of error and return true + unless the file does not exist, to avoid returning false results + on obscure systems where stat does not work as expected. */ + + if (status != 0) + { + if (errno != ENOENT) + return true; + *errno_ptr = ENOENT; + return false; + } + + if (S_ISREG (st.st_mode)) + return true; + + if (S_ISDIR (st.st_mode)) + *errno_ptr = EISDIR; + else + *errno_ptr = EINVAL; + return false; +} diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h index 2320318..5408c35 100644 --- a/gdb/common/common-utils.h +++ b/gdb/common/common-utils.h @@ -146,4 +146,9 @@ in_inclusive_range (T value, T low, T high) return value >= low && value <= high; } +/* Return true if the file NAME exists and is a regular file. + If the result is false then *ERRNO_PTR is set to a useful value assuming + we're expecting a regular file. */ +extern bool is_regular_file (const char *name, int *errno_ptr); + #endif diff --git a/gdb/common/pathstuff.c b/gdb/common/pathstuff.c index 02f6e44..fc574dc 100644 --- a/gdb/common/pathstuff.c +++ b/gdb/common/pathstuff.c @@ -140,3 +140,17 @@ gdb_abspath (const char *path) ? "" : SLASH_STRING, path, (char *) NULL)); } + +/* See common/pathstuff.h. */ + +bool +contains_dir_separator (const char *path) +{ + for (; *path != '\0'; path++) + { + if (IS_DIR_SEPARATOR (*path)) + return true; + } + + return false; +} diff --git a/gdb/common/pathstuff.h b/gdb/common/pathstuff.h index 3cb02c8..9f26127 100644 --- a/gdb/common/pathstuff.h +++ b/gdb/common/pathstuff.h @@ -46,4 +46,8 @@ extern gdb::unique_xmalloc_ptr<char> extern gdb::unique_xmalloc_ptr<char> gdb_abspath (const char *path); +/* Return whether PATH contains a directory separator character. */ + +extern bool contains_dir_separator (const char *path); + #endif /* PATHSTUFF_H */ |