From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 79571 invoked by alias); 16 Apr 2015 13:06:04 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 79561 invoked by uid 89); 16 Apr 2015 13:06:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.7 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_PASS,T_RP_MATCHES_RCVD,UNWANTED_LANGUAGE_BODY autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 16 Apr 2015 13:05:59 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t3GCJuUJ027496 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Thu, 16 Apr 2015 08:19:56 -0400 Received: from blade.nx (ovpn-116-95.ams2.redhat.com [10.36.116.95]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t3GCJtOL015953 for ; Thu, 16 Apr 2015 08:19:56 -0400 Received: from blade.nx (localhost [127.0.0.1]) by blade.nx (Postfix) with ESMTP id 374B6264112 for ; Thu, 16 Apr 2015 13:19:54 +0100 (BST) From: Gary Benson To: gdb-patches@sourceware.org Subject: [PATCH 5/7] Implement multiple-filesystem support for Linux targets Date: Thu, 16 Apr 2015 13:06:00 -0000 Message-Id: <1429186791-6867-6-git-send-email-gbenson@redhat.com> In-Reply-To: <1429186791-6867-1-git-send-email-gbenson@redhat.com> References: <1429186791-6867-1-git-send-email-gbenson@redhat.com> X-IsSubscribed: yes X-SW-Source: 2015-04/txt/msg00616.txt.bz2 This commit allows GDB to access executables and shared libraries on native Linux targets where GDB and the inferior have different filesystem namespaces. gdb/ChangeLog: * linux-nat.c (fileio.h): New include. (nat/linux-namespaces.h): Likewise. (super_fileio_open): New static variable. (super_fileio_unlink): Likewise. (super_fileio_readlink): Likewise. (fs_ns_pid): Likewise. (linux_nat_fileio_set_fs): New function. (linux_nat_filesystem_is_local): Likewise. (linux_nat_enter_fs): Likewise. (open_closure): New structure. (linux_nat_fileio_open_1): New function. (linux_nat_fileio_open): Likewise. (unlink_closure): New structure. (linux_nat_fileio_unlink_1): New function. (linux_nat_fileio_unlink): Likewise. (linux_nat_add_target): Install new target methods. (readlink_closure): New structure. (linux_nat_fileio_readlink_1): New function. (linux_nat_fileio_readlink): Likewise. --- gdb/ChangeLog | 22 ++++++ gdb/linux-nat.c | 193 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 215 insertions(+), 0 deletions(-) diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index b04aa68..a54c704 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -66,6 +66,8 @@ #include "target-descriptions.h" #include "filestuff.h" #include "objfiles.h" +#include "fileio.h" +#include "nat/linux-namespaces.h" #ifndef SPUFS_MAGIC #define SPUFS_MAGIC 0x23c9b64e @@ -196,6 +198,24 @@ static target_xfer_partial_ftype *super_xfer_partial; Called by our to_close. */ static void (*super_close) (struct target_ops *); +/* The saved to_fileio_open method, inherited from inf-child.c. + Called by our to_fileio_open. */ + +static int (*super_fileio_open) (struct target_ops *, + const char *, int, int, int *); + +/* The saved to_fileio_unlink method, inherited from inf-child.c. + Called by our to_fileio_unlink. */ + +static int (*super_fileio_unlink) (struct target_ops *, + const char *, int *); + +/* The saved to_fileio_readlink method, inherited from inf-child.c. + Called by our to_fileio_readlink. */ + +static char *(*super_fileio_readlink) (struct target_ops *, + const char *, int *); + static unsigned int debug_linux_nat; static void show_debug_linux_nat (struct ui_file *file, int from_tty, @@ -4842,6 +4862,169 @@ linux_nat_core_of_thread (struct target_ops *ops, ptid_t ptid) return -1; } +/* Process ID of inferior whose filesystem namespace the various + target_fileio functions will use. Zero means to use our own. */ + +static int fs_ns_pid = 0; + +/* Implementation of to_fileio_set_fs. */ + +static void +linux_nat_fileio_set_fs (struct target_ops *ops, int pid) +{ + fs_ns_pid = pid; +} + +/* Implementation of to_filesystem_is_local. */ + +static int +linux_nat_filesystem_is_local (struct target_ops *ops) +{ + return fs_ns_pid == 0 + || linux_ns_same (getpid (), fs_ns_pid, LINUX_NS_MNT); +} + +/* Enter the filesystem namespace of the process specified by + target_fileio_set_fs and call FUNC with the argument ARG, + restoring the original filesystem namespace afterwards. + Set TARGET_ERRNO if FUNC is not called. */ + +static void +linux_nat_enter_fs (void (*func) (void *), void *arg, int *target_errno) +{ + if (fs_ns_pid == 0) + func (arg); + else if (!linux_ns_enter (fs_ns_pid, LINUX_NS_MNT, func, arg)) + *target_errno = host_to_fileio_error (errno); +} + +/* Arguments and return value of to_fileio_open. */ + +struct open_closure +{ + struct target_ops *ops; + const char *filename; + int flags; + int mode; + int *target_errno; + int return_value; +}; + +/* Helper for linux_nat_fileio_open. */ + +static void +linux_nat_fileio_open_1 (void *arg) +{ + struct open_closure *clo = (struct open_closure *) arg; + + clo->return_value = super_fileio_open (clo->ops, clo->filename, + clo->flags, clo->mode, + clo->target_errno); +} + +/* Implementation of to_fileio_open. */ + +static int +linux_nat_fileio_open (struct target_ops *ops, + const char *filename, int flags, int mode, + int *target_errno) +{ + struct open_closure clo = + { + /* Arguments. */ + ops, filename, flags, mode, target_errno, + + /* Failure return value. */ + -1 + }; + + linux_nat_enter_fs (linux_nat_fileio_open_1, &clo, target_errno); + + return clo.return_value; +} + +/* Arguments and return value of to_fileio_unlink. */ + +struct unlink_closure +{ + struct target_ops *ops; + const char *filename; + int *target_errno; + int return_value; +}; + +/* Helper for linux_nat_fileio_unlink. */ + +static void +linux_nat_fileio_unlink_1 (void *arg) +{ + struct unlink_closure *clo = (struct unlink_closure *) arg; + + clo->return_value = super_fileio_unlink (clo->ops, clo->filename, + clo->target_errno); +} + +/* Implementation of to_fileio_unlink. */ + +static int +linux_nat_fileio_unlink (struct target_ops *ops, + const char *filename, int *target_errno) +{ + struct unlink_closure clo = + { + /* Arguments. */ + ops, filename, target_errno, + + /* Failure return value. */ + -1 + }; + + linux_nat_enter_fs (linux_nat_fileio_unlink_1, &clo, target_errno); + + return clo.return_value; +} + +/* Arguments and return value of to_fileio_readlink. */ + +struct readlink_closure +{ + struct target_ops *ops; + const char *filename; + int *target_errno; + char *return_value; +}; + +/* Helper for linux_nat_fileio_readlink. */ + +static void +linux_nat_fileio_readlink_1 (void *arg) +{ + struct readlink_closure *clo = (struct readlink_closure *) arg; + + clo->return_value = super_fileio_readlink (clo->ops, clo->filename, + clo->target_errno); +} + +/* Implementation of to_fileio_readlink. */ + +static char * +linux_nat_fileio_readlink (struct target_ops *ops, + const char *filename, int *target_errno) +{ + struct readlink_closure clo = + { + /* Arguments. */ + ops, filename, target_errno, + + /* Failure return value. */ + NULL + }; + + linux_nat_enter_fs (linux_nat_fileio_readlink_1, &clo, target_errno); + + return clo.return_value; +} + void linux_nat_add_target (struct target_ops *t) { @@ -4895,6 +5078,16 @@ linux_nat_add_target (struct target_ops *t) t->to_core_of_thread = linux_nat_core_of_thread; + /* Target File-I/O functions. */ + t->to_fileio_set_fs = linux_nat_fileio_set_fs; + t->to_filesystem_is_local = linux_nat_filesystem_is_local; + super_fileio_open = t->to_fileio_open; + t->to_fileio_open = linux_nat_fileio_open; + super_fileio_unlink = t->to_fileio_unlink; + t->to_fileio_unlink = linux_nat_fileio_unlink; + super_fileio_readlink = t->to_fileio_readlink; + t->to_fileio_readlink = linux_nat_fileio_readlink; + /* We don't change the stratum; this target will sit at process_stratum and thread_db will set at thread_stratum. This is a little strange, since this is a multi-threaded-capable -- 1.7.1