[2/2] Use exec_file_find to locate executable on attach (sometimes)
Commit Message
This commit updates attach_command_post_wait to use exec_file_find
to compute the full pathname of the main executable if the pathname
returned by target_pid_to_exec_file is absolute and if gdb_sysroot
is non-empty and not remote. The net effect of this is to prefix
the main executable's path with gdb_sysroot if gdb_sysroot is set;
this is useful for attaching to processes running in chroot jails
or containerized environments. This logic is skipped for remote
gdb_sysroots because the subsequent code does not support opening
the main executable remotely.
gdb/ChangeLog:
* infcmd.c (filenames.h): New include.
(remote.h): Likewise.
(solist.h): Likewise.
(attach_command_post_wait): Prefix absolute executable
paths with gdb_sysroot if set and not remote.
---
gdb/ChangeLog | 8 ++++++++
gdb/infcmd.c | 37 +++++++++++++++++++++++++++++--------
2 files changed, 37 insertions(+), 8 deletions(-)
@@ -54,6 +54,9 @@
#include "continuations.h"
#include "linespec.h"
#include "cli/cli-utils.h"
+#include "filenames.h"
+#include "remote.h"
+#include "solist.h"
/* Local functions: */
@@ -2493,16 +2496,34 @@ attach_command_post_wait (char *args, int from_tty, int async_exec)
exec_file = target_pid_to_exec_file (ptid_get_pid (inferior_ptid));
if (exec_file)
{
- /* It's possible we don't have a full path, but rather just a
- filename. Some targets, such as HP-UX, don't provide the
- full path, sigh.
+ /* If there is a gdb_sysroot and exec_file is absolute we
+ prepend gdb_sysroot to exec_file. This is currently
+ disabled for remote sysroot as the subsequent code cannot
+ yet cope with these. */
+ if (IS_ABSOLUTE_PATH (exec_file)
+ && gdb_sysroot != NULL && *gdb_sysroot != '\0'
+ && !remote_filename_p (gdb_sysroot))
+ {
+ int fd = -1;
+
+ full_exec_path = exec_file_find (exec_file, &fd);
+ if (fd >= 0)
+ close (fd);
+ }
- Attempt to qualify the filename against the source path.
- (If that fails, we'll just fall back on the original
- filename. Not much more we can do...) */
+ if (full_exec_path == NULL)
+ {
+ /* It's possible we don't have a full path, but rather
+ just a filename. Some targets, such as HP-UX, don't
+ provide the full path, sigh.
+
+ Attempt to qualify the filename against the source
+ path. (If that fails, we'll just fall back on the
+ original filename. Not much more we can do...) */
- if (!source_full_path_of (exec_file, &full_exec_path))
- full_exec_path = xstrdup (exec_file);
+ if (!source_full_path_of (exec_file, &full_exec_path))
+ full_exec_path = xstrdup (exec_file);
+ }
exec_file_attach (full_exec_path, from_tty);
symbol_file_add_main (full_exec_path, from_tty);