From patchwork Sun Dec 31 20:25:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 83060 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EBE3D3858437 for ; Sun, 31 Dec 2023 20:26:31 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from omta036.useast.a.cloudfilter.net (omta036.useast.a.cloudfilter.net [44.202.169.35]) by sourceware.org (Postfix) with ESMTPS id CBF683858C31 for ; Sun, 31 Dec 2023 20:25:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CBF683858C31 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CBF683858C31 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=44.202.169.35 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704054344; cv=none; b=GGqQewoTRXvVL5yP2HWPeHWzmy0bMG8ZLc/QU5P/u9dw9pctbGRS2k8iFK4IsapISzXHjcsLUPL4WUxXReCTkFbrJElgFhAIxgiVCLr8yIfk3K4vq868sy6Fh/y82FAlhVyzdrov1lkno2O13Sitg/FLupajd+3o7MPrp/L0sEc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704054344; c=relaxed/simple; bh=tlZn/uFUk2ahiGlTy0IdnbGh5M0BATLzpXYQyhYttmA=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=QIoqQK+kzbHg73+LzmioP3K728R+HpsH/rne4cjoT0o8OCGCGlbz2q6cB5MmY+PXy/QmybKy505QBNh9Mgg5dJJlhWDliSFalpr317tl47SlzA90rxqHDT+JtjFDTofDdpmN5aZfGJT8qZNSDaNOvOdBcifmsqaGKxBaI62+2p8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from eig-obgw-6010a.ext.cloudfilter.net ([10.0.30.248]) by cmsmtp with ESMTPS id JrjVrLKJKgpyEK2NQrpb0V; Sun, 31 Dec 2023 20:25:40 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id K2NPrT8uV8vT0K2NQrEycw; Sun, 31 Dec 2023 20:25:40 +0000 X-Authority-Analysis: v=2.4 cv=ffi+dmcF c=1 sm=1 tr=0 ts=6591ce44 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=OWjo9vPv0XrRhIrVQ50Ab3nP57M=:19 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IkcTkHD0fZMA:10 a=e2cXIFwxEfEA:10 a=Qbun_eYptAEA:10 a=94JAvcUx1EORx4uIDcEA:9 a=QEXdDO2ut3YA:10 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=TrI0FDHWkTweqJEGw21E00GdleGZFzR1XPchCAHiHVI=; b=Ul+VcfHtLBEVrcmj8O1PpIRaPZ wj12t3owaRx0H4kzl+r1gg/xw+6XcCa66edRXvqrt7QR74V1RKxiow04k+LjOLXdo0gNbsXwVshbD 4QdyJjuvtABOBCbVcohZF3Q5J; Received: from 71-211-161-25.hlrn.qwest.net ([71.211.161.25]:45228 helo=[192.168.0.21]) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1rK2NP-002PSB-2M for gdb-patches@sourceware.org; Sun, 31 Dec 2023 13:25:39 -0700 From: Tom Tromey Date: Sun, 31 Dec 2023 13:25:40 -0700 Subject: [PATCH 3/6] Use methods for remote fileio MIME-Version: 1.0 Message-Id: <20231231-remote-fileio-v1-3-249cc6c440d9@tromey.com> References: <20231231-remote-fileio-v1-0-249cc6c440d9@tromey.com> In-Reply-To: <20231231-remote-fileio-v1-0-249cc6c440d9@tromey.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.4 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 71.211.161.25 X-Source-L: No X-Exim-ID: 1rK2NP-002PSB-2M X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 71-211-161-25.hlrn.qwest.net ([192.168.0.21]) [71.211.161.25]:45228 X-Source-Auth: tom+tromey.com X-Email-Count: 6 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfEyGA75eYLAEx7NvC61Atr5C5KMLMEJ4r7KS+rt7x6BRPzQcSUkekBm+0/3GixDqR/rKhhu+Xl5p4K5NB1Xsps+JsH6ill6SIZR8L97AnIQ2jmJ9aOQq OV9twpcbtmKFvfxNZrcaWupNcTxpZwkVIEyHDw0NzjIspMaeOVM+U3aI1VW3345NHfzJ1ZhVGig/22l3qygbStQGzl25H41LkMc= X-Spam-Status: No, score=-3012.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, UNWANTED_LANGUAGE_BODY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org This changes various functions in remote-fileio.c to be methods on the state object. --- gdb/remote-fileio.c | 248 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 146 insertions(+), 102 deletions(-) diff --git a/gdb/remote-fileio.c b/gdb/remote-fileio.c index 4891b0c5b92..f6990989da6 100644 --- a/gdb/remote-fileio.c +++ b/gdb/remote-fileio.c @@ -37,9 +37,40 @@ #endif #include -static struct +static struct remote_fileio_data { - std::vector fd_map; +public: + + void request (remote_target *remote, + char *buf, int ctrlc_pending_p); + + void reset (); + +private: + + int fd_to_targetfd (int fd); + int map_fd (int target_fd); + void close_target_fd (int target_fd); + + void func_open (remote_target *remote, char *buf); + void func_close (remote_target *remote, char *buf); + void func_read (remote_target *remote, char *buf); + void func_write (remote_target *remote, char *buf); + void func_lseek (remote_target *remote, char *buf); + void func_rename (remote_target *remote, char *buf); + void func_unlink (remote_target *remote, char *buf); + void func_stat (remote_target *remote, char *buf); + void func_fstat (remote_target *remote, char *buf); + void func_gettimeofday (remote_target *remote, char *buf); + void func_isatty (remote_target *remote, char *buf); + void func_system (remote_target *remote, char *buf); + void do_request (remote_target *remote, char *buf); + + int init_fd_map (); + int resize_fd_map (); + int next_free_fd (); + + std::vector m_fd_map; } remote_fio_data; #define FIO_FD_INVALID -1 @@ -48,67 +79,78 @@ static struct static int remote_fio_system_call_allowed = 0; -static int -remote_fileio_init_fd_map (void) +int +remote_fileio_data::init_fd_map () { - if (remote_fio_data.fd_map.empty ()) + if (m_fd_map.empty ()) { - remote_fio_data.fd_map.resize (10); - remote_fio_data.fd_map[0] = FIO_FD_CONSOLE_IN; - remote_fio_data.fd_map[1] = FIO_FD_CONSOLE_OUT; - remote_fio_data.fd_map[2] = FIO_FD_CONSOLE_OUT; + m_fd_map.resize (10); + m_fd_map[0] = FIO_FD_CONSOLE_IN; + m_fd_map[1] = FIO_FD_CONSOLE_OUT; + m_fd_map[2] = FIO_FD_CONSOLE_OUT; for (int i = 3; i < 10; ++i) - remote_fio_data.fd_map[i] = FIO_FD_INVALID; + m_fd_map[i] = FIO_FD_INVALID; } return 3; } -static int -remote_fileio_resize_fd_map (void) +int +remote_fileio_data::resize_fd_map () { - if (remote_fio_data.fd_map.empty ()) - return remote_fileio_init_fd_map (); - - int i = remote_fio_data.fd_map.size (); - remote_fio_data.fd_map.resize (i + 10); - for (; i < remote_fio_data.fd_map.size (); i++) - remote_fio_data.fd_map[i] = FIO_FD_INVALID; - return remote_fio_data.fd_map.size () - 10; + if (m_fd_map.empty ()) + return init_fd_map (); + + int i = m_fd_map.size (); + m_fd_map.resize (i + 10); + for (; i < m_fd_map.size (); i++) + m_fd_map[i] = FIO_FD_INVALID; + return m_fd_map.size () - 10; } -static int -remote_fileio_next_free_fd (void) +int +remote_fileio_data::next_free_fd () { - for (int i = 0; i < remote_fio_data.fd_map.size (); ++i) - if (remote_fio_data.fd_map[i] == FIO_FD_INVALID) + for (int i = 0; i < m_fd_map.size (); ++i) + if (m_fd_map[i] == FIO_FD_INVALID) return i; - return remote_fileio_resize_fd_map (); + return resize_fd_map (); } -static int -remote_fileio_fd_to_targetfd (int fd) +int +remote_fileio_data::fd_to_targetfd (int fd) { - int target_fd = remote_fileio_next_free_fd (); + int target_fd = next_free_fd (); - remote_fio_data.fd_map[target_fd] = fd; + m_fd_map[target_fd] = fd; return target_fd; } -static int -remote_fileio_map_fd (int target_fd) +int +remote_fileio_data::map_fd (int target_fd) { - remote_fileio_init_fd_map (); - if (target_fd < 0 || target_fd >= remote_fio_data.fd_map.size ()) + init_fd_map (); + if (target_fd < 0 || target_fd >= m_fd_map.size ()) return FIO_FD_INVALID; - return remote_fio_data.fd_map[target_fd]; + return m_fd_map[target_fd]; } -static void -remote_fileio_close_target_fd (int target_fd) +void +remote_fileio_data::close_target_fd (int target_fd) { - remote_fileio_init_fd_map (); - if (target_fd >= 0 && target_fd < remote_fio_data.fd_map.size ()) - remote_fio_data.fd_map[target_fd] = FIO_FD_INVALID; + init_fd_map (); + if (target_fd >= 0 && target_fd < m_fd_map.size ()) + m_fd_map[target_fd] = FIO_FD_INVALID; +} + +void +remote_fileio_data::reset () +{ + for (int fd : m_fd_map) + { + if (fd >= 0) + close (fd); + } + m_fd_map.clear (); } static int @@ -359,8 +401,8 @@ remote_fileio_return_success (remote_target *remote, int retcode) remote_fileio_reply (remote, retcode, 0); } -static void -remote_fileio_func_open (remote_target *remote, char *buf) +void +remote_fileio_data::func_open (remote_target *remote, char *buf) { CORE_ADDR ptrval; int length; @@ -424,12 +466,12 @@ remote_fileio_func_open (remote_target *remote, char *buf) return; } - fd = remote_fileio_fd_to_targetfd (fd); + fd = fd_to_targetfd (fd); remote_fileio_return_success (remote, fd); } -static void -remote_fileio_func_close (remote_target *remote, char *buf) +void +remote_fileio_data::func_close (remote_target *remote, char *buf) { long num; int fd; @@ -440,7 +482,7 @@ remote_fileio_func_close (remote_target *remote, char *buf) remote_fileio_ioerror (remote); return; } - fd = remote_fileio_map_fd ((int) num); + fd = map_fd ((int) num); if (fd == FIO_FD_INVALID) { remote_fileio_badfd (remote); @@ -449,12 +491,12 @@ remote_fileio_func_close (remote_target *remote, char *buf) if (fd != FIO_FD_CONSOLE_IN && fd != FIO_FD_CONSOLE_OUT && close (fd)) remote_fileio_return_errno (remote, -1); - remote_fileio_close_target_fd ((int) num); + close_target_fd ((int) num); remote_fileio_return_success (remote, 0); } -static void -remote_fileio_func_read (remote_target *remote, char *buf) +void +remote_fileio_data::func_read (remote_target *remote, char *buf) { long target_fd, num; LONGEST lnum; @@ -470,7 +512,7 @@ remote_fileio_func_read (remote_target *remote, char *buf) remote_fileio_ioerror (remote); return; } - fd = remote_fileio_map_fd ((int) target_fd); + fd = map_fd ((int) target_fd); if (fd == FIO_FD_INVALID) { remote_fileio_badfd (remote); @@ -579,8 +621,8 @@ remote_fileio_func_read (remote_target *remote, char *buf) xfree (buffer); } -static void -remote_fileio_func_write (remote_target *remote, char *buf) +void +remote_fileio_data::func_write (remote_target *remote, char *buf) { long target_fd, num; LONGEST lnum; @@ -595,7 +637,7 @@ remote_fileio_func_write (remote_target *remote, char *buf) remote_fileio_ioerror (remote); return; } - fd = remote_fileio_map_fd ((int) target_fd); + fd = map_fd ((int) target_fd); if (fd == FIO_FD_INVALID) { remote_fileio_badfd (remote); @@ -654,8 +696,8 @@ remote_fileio_func_write (remote_target *remote, char *buf) xfree (buffer); } -static void -remote_fileio_func_lseek (remote_target *remote, char *buf) +void +remote_fileio_data::func_lseek (remote_target *remote, char *buf) { long num; LONGEST lnum; @@ -668,7 +710,7 @@ remote_fileio_func_lseek (remote_target *remote, char *buf) remote_fileio_ioerror (remote); return; } - fd = remote_fileio_map_fd ((int) num); + fd = map_fd ((int) num); if (fd == FIO_FD_INVALID) { remote_fileio_badfd (remote); @@ -707,8 +749,8 @@ remote_fileio_func_lseek (remote_target *remote, char *buf) remote_fileio_return_success (remote, ret); } -static void -remote_fileio_func_rename (remote_target *remote, char *buf) +void +remote_fileio_data::func_rename (remote_target *remote, char *buf) { CORE_ADDR old_ptr, new_ptr; int old_len, new_len; @@ -800,8 +842,8 @@ remote_fileio_func_rename (remote_target *remote, char *buf) remote_fileio_return_success (remote, ret); } -static void -remote_fileio_func_unlink (remote_target *remote, char *buf) +void +remote_fileio_data::func_unlink (remote_target *remote, char *buf) { CORE_ADDR ptrval; int length; @@ -839,8 +881,8 @@ remote_fileio_func_unlink (remote_target *remote, char *buf) remote_fileio_return_success (remote, ret); } -static void -remote_fileio_func_stat (remote_target *remote, char *buf) +void +remote_fileio_data::func_stat (remote_target *remote, char *buf) { CORE_ADDR statptr, nameptr; int ret, namelength; @@ -900,8 +942,8 @@ remote_fileio_func_stat (remote_target *remote, char *buf) remote_fileio_return_success (remote, ret); } -static void -remote_fileio_func_fstat (remote_target *remote, char *buf) +void +remote_fileio_data::func_fstat (remote_target *remote, char *buf) { CORE_ADDR ptrval; int fd, ret; @@ -917,7 +959,7 @@ remote_fileio_func_fstat (remote_target *remote, char *buf) remote_fileio_ioerror (remote); return; } - fd = remote_fileio_map_fd ((int) target_fd); + fd = map_fd ((int) target_fd); if (fd == FIO_FD_INVALID) { remote_fileio_badfd (remote); @@ -977,8 +1019,8 @@ remote_fileio_func_fstat (remote_target *remote, char *buf) remote_fileio_return_success (remote, ret); } -static void -remote_fileio_func_gettimeofday (remote_target *remote, char *buf) +void +remote_fileio_data::func_gettimeofday (remote_target *remote, char *buf) { LONGEST lnum; CORE_ADDR ptrval; @@ -1028,8 +1070,8 @@ remote_fileio_func_gettimeofday (remote_target *remote, char *buf) remote_fileio_return_success (remote, ret); } -static void -remote_fileio_func_isatty (remote_target *remote, char *buf) +void +remote_fileio_data::func_isatty (remote_target *remote, char *buf) { long target_fd; int fd; @@ -1040,13 +1082,13 @@ remote_fileio_func_isatty (remote_target *remote, char *buf) remote_fileio_ioerror (remote); return; } - fd = remote_fileio_map_fd ((int) target_fd); + fd = map_fd ((int) target_fd); int ret = fd == FIO_FD_CONSOLE_IN || fd == FIO_FD_CONSOLE_OUT ? 1 : 0; remote_fileio_return_success (remote, ret); } -static void -remote_fileio_func_system (remote_target *remote, char *buf) +void +remote_fileio_data::func_system (remote_target *remote, char *buf) { CORE_ADDR ptrval; int ret, length; @@ -1093,28 +1135,28 @@ remote_fileio_func_system (remote_target *remote, char *buf) remote_fileio_return_success (remote, WEXITSTATUS (ret)); } -static const struct { - const char *name; - void (*func)(remote_target *remote, char *); -} remote_fio_func_map[] = { - { "open", remote_fileio_func_open }, - { "close", remote_fileio_func_close }, - { "read", remote_fileio_func_read }, - { "write", remote_fileio_func_write }, - { "lseek", remote_fileio_func_lseek }, - { "rename", remote_fileio_func_rename }, - { "unlink", remote_fileio_func_unlink }, - { "stat", remote_fileio_func_stat }, - { "fstat", remote_fileio_func_fstat }, - { "gettimeofday", remote_fileio_func_gettimeofday }, - { "isatty", remote_fileio_func_isatty }, - { "system", remote_fileio_func_system }, - { NULL, NULL } -}; - -static void -do_remote_fileio_request (remote_target *remote, char *buf) +void +remote_fileio_data::do_request (remote_target *remote, char *buf) { + static const struct { + const char *name; + void (remote_fileio_data::*func)(remote_target *remote, char *); + } remote_fio_func_map[] = { + { "open", &remote_fileio_data::func_open }, + { "close", &remote_fileio_data::func_close }, + { "read", &remote_fileio_data::func_read }, + { "write", &remote_fileio_data::func_write }, + { "lseek", &remote_fileio_data::func_lseek }, + { "rename", &remote_fileio_data::func_rename }, + { "unlink", &remote_fileio_data::func_unlink }, + { "stat", &remote_fileio_data::func_stat }, + { "fstat", &remote_fileio_data::func_fstat }, + { "gettimeofday", &remote_fileio_data::func_gettimeofday }, + { "isatty", &remote_fileio_data::func_isatty }, + { "system", &remote_fileio_data::func_system }, + { nullptr, nullptr } + }; + char *c; int idx; @@ -1131,20 +1173,15 @@ do_remote_fileio_request (remote_target *remote, char *buf) if (!remote_fio_func_map[idx].name) remote_fileio_reply (remote, -1, FILEIO_ENOSYS); else - remote_fio_func_map[idx].func (remote, c); + (this->*remote_fio_func_map[idx].func) (remote, c); } /* Close any open descriptors, and reinitialize the file mapping. */ void -remote_fileio_reset (void) +remote_fileio_reset () { - for (int fd : remote_fio_data.fd_map) - { - if (fd >= 0) - close (fd); - } - remote_fio_data.fd_map.clear (); + remote_fio_data.reset (); } /* Handle a file I/O request. BUF points to the packet containing the @@ -1152,7 +1189,8 @@ remote_fileio_reset (void) acknowledged the Ctrl-C sent asynchronously earlier. */ void -remote_fileio_request (remote_target *remote, char *buf, int ctrlc_pending_p) +remote_fileio_data::request (remote_target *remote, + char *buf, int ctrlc_pending_p) { /* Save the previous quit handler, so we can restore it. No need for a cleanup since we catch all exceptions below. Note that the @@ -1172,7 +1210,7 @@ remote_fileio_request (remote_target *remote, char *buf, int ctrlc_pending_p) { try { - do_remote_fileio_request (remote, buf); + do_request (remote, buf); } catch (const gdb_exception_forced_quit &ex) { @@ -1190,6 +1228,12 @@ remote_fileio_request (remote_target *remote, char *buf, int ctrlc_pending_p) quit_handler = remote_fileio_o_quit_handler; } + +void +remote_fileio_request (remote_target *remote, char *buf, int ctrlc_pending_p) +{ + remote_fio_data.request (remote, buf, ctrlc_pending_p); +} /* Unpack an fio_uint_t. */