From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x12b.google.com (mail-il1-x12b.google.com [IPv6:2607:f8b0:4864:20::12b]) by sourceware.org (Postfix) with ESMTPS id 5D01E385841C for ; Thu, 24 Aug 2023 17:12:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5D01E385841C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-il1-x12b.google.com with SMTP id e9e14a558f8ab-34ca192f8feso142255ab.3 for ; Thu, 24 Aug 2023 10:12:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1692897143; x=1693501943; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=iFqeapLXyyHGsE7Wa8+B6CAPRbKK5hc2ShCT0jISFag=; b=FNSXsUz+axQoJgQ8pG8KsBon2DEZS04Xa/IEdhuldPPmxto+2iHTG2JvEEIGN0ImgK rLmAr++8IsFj5NIRvJSCpGKkcaq2RXIPLkTuXjXMVH9cOPhusbmszC8fvmvNli2/B/qI PAL4g+FsweWegUmyY+bHhtjyYOuSaUYZ2N46RU6O8CYwxT0cW07O+eycS1lFQK6Ar5lm iQOFwub4rfv1SgAR4hoIIgA787dwJDtwPkO0htm0f6gVv/7FHr2aJfPWMD81mtS5PrFQ sW3fyhw0WJR8E4j1bkMQ2XhxC9zilHdXR3Wqu0/EdZfiserNRp7Z6Hr9L33lDRBXNLZ3 NUaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692897143; x=1693501943; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iFqeapLXyyHGsE7Wa8+B6CAPRbKK5hc2ShCT0jISFag=; b=XXhChEtN845lSrRLo95WfC9cyW/O8X3X2itUkoPc3RgySg1mFgs4NVW/Otw1WSNe5H 2nojmZnYtkSwMl+oUy/SAzYV0tAgUQLPKOOx7Afc0FwhZCiphH/6zmelrH/CXK1isCkd L0hRLtDk/RJIknC5cYGqulN6aeW1589HeJQF7TwmDUvZvQHe5RCsF+180fUrMFxld8CT u+ilioG8lpE8RPmkfGZ6Tl1O5wEx+1K9rgGDlFDyBV/WsyFGmPc2214DRZb24jg1mCPo BlxyfoIYCkCRiZTzlmTWKiRzAE+L9R+pGNxc9zEmQTFkBoXsroh4sYUb+fGy+KkviHI+ daJQ== X-Gm-Message-State: AOJu0YyYD2O7kzuPA9Q9RrwDGhhAkYdA0DDnqcYpGbDVOCu+bdAa+u6N m/n/NqByw15hSwVX1EgaPu4S1Joi+vWioDh/v/FSPw== X-Google-Smtp-Source: AGHT+IGnPKWlJEH2iHLWHX1qdm57UdI1rO6SWS129QpiYYJ0kfaPdHCtwpJT6TnWN9bJ+RPoZJnZXg== X-Received: by 2002:a05:6e02:1d8a:b0:34c:b992:58a0 with SMTP id h10-20020a056e021d8a00b0034cb99258a0mr8099234ila.10.1692897143495; Thu, 24 Aug 2023 10:12:23 -0700 (PDT) Received: from localhost.localdomain (75-166-142-177.hlrn.qwest.net. [75.166.142.177]) by smtp.gmail.com with ESMTPSA id n8-20020a92d9c8000000b0034986f6d9a9sm4722515ilq.78.2023.08.24.10.12.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Aug 2023 10:12:23 -0700 (PDT) From: Tom Tromey Date: Thu, 24 Aug 2023 11:12:21 -0600 Subject: [PATCH 4/7] Convert target fileio to new type-safe gdb_bfd_openr_iovec MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230824-gdb-bfd-vec-v1-4-850e4e907ed1@adacore.com> References: <20230824-gdb-bfd-vec-v1-0-850e4e907ed1@adacore.com> In-Reply-To: <20230824-gdb-bfd-vec-v1-0-850e4e907ed1@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This converts the target fileio BFD iovec implementation to use the new type-safe gdb_bfd_openr_iovec. --- gdb/gdb_bfd.c | 97 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c index 2f8bb33997b..c635ba007d0 100644 --- a/gdb/gdb_bfd.c +++ b/gdb/gdb_bfd.c @@ -311,30 +311,48 @@ gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, }); } -/* bfd_openr_iovec OPEN_CLOSURE data for gdb_bfd_open. */ -struct gdb_bfd_open_closure +/* An object that manages the underlying stream for a BFD, using + target file I/O. */ + +struct target_fileio_stream : public gdb_bfd_iovec_base { - inferior *inf; - bool warn_if_slow; + target_fileio_stream (bfd *nbfd, int fd) + : m_bfd (nbfd), + m_fd (fd) + { + } + + ~target_fileio_stream (); + + file_ptr read (bfd *abfd, void *buffer, file_ptr nbytes, + file_ptr offset) override; + + int stat (struct bfd *abfd, struct stat *sb) override; + +private: + + /* The BFD. Saved for the destructor. */ + bfd *m_bfd; + + /* The file descriptor. */ + int m_fd; }; -/* Wrapper for target_fileio_open suitable for passing as the - OPEN_FUNC argument to gdb_bfd_openr_iovec. */ +/* Wrapper for target_fileio_open suitable for use as a helper + function for gdb_bfd_openr_iovec. */ -static void * -gdb_bfd_iovec_fileio_open (struct bfd *abfd, void *open_closure) +static target_fileio_stream * +gdb_bfd_iovec_fileio_open (struct bfd *abfd, inferior *inf, bool warn_if_slow) { const char *filename = bfd_get_filename (abfd); int fd; fileio_error target_errno; - int *stream; - gdb_bfd_open_closure *oclosure = (gdb_bfd_open_closure *) open_closure; gdb_assert (is_target_filename (filename)); - fd = target_fileio_open (oclosure->inf, + fd = target_fileio_open (inf, filename + strlen (TARGET_SYSROOT_PREFIX), - FILEIO_O_RDONLY, 0, oclosure->warn_if_slow, + FILEIO_O_RDONLY, 0, warn_if_slow, &target_errno); if (fd == -1) { @@ -343,19 +361,15 @@ gdb_bfd_iovec_fileio_open (struct bfd *abfd, void *open_closure) return NULL; } - stream = XCNEW (int); - *stream = fd; - return stream; + return new target_fileio_stream (abfd, fd); } -/* Wrapper for target_fileio_pread suitable for passing as the - PREAD_FUNC argument to gdb_bfd_openr_iovec. */ +/* Wrapper for target_fileio_pread. */ -static file_ptr -gdb_bfd_iovec_fileio_pread (struct bfd *abfd, void *stream, void *buf, +file_ptr +target_fileio_stream::read (struct bfd *abfd, void *buf, file_ptr nbytes, file_ptr offset) { - int fd = *(int *) stream; fileio_error target_errno; file_ptr pos, bytes; @@ -364,7 +378,7 @@ gdb_bfd_iovec_fileio_pread (struct bfd *abfd, void *stream, void *buf, { QUIT; - bytes = target_fileio_pread (fd, (gdb_byte *) buf + pos, + bytes = target_fileio_pread (m_fd, (gdb_byte *) buf + pos, nbytes - pos, offset + pos, &target_errno); if (bytes == 0) @@ -392,46 +406,35 @@ gdb_bfd_close_warning (const char *name, const char *reason) warning (_("cannot close \"%s\": %s"), name, reason); } -/* Wrapper for target_fileio_close suitable for passing as the - CLOSE_FUNC argument to gdb_bfd_openr_iovec. */ +/* Wrapper for target_fileio_close. */ -static int -gdb_bfd_iovec_fileio_close (struct bfd *abfd, void *stream) +target_fileio_stream::~target_fileio_stream () { - int fd = *(int *) stream; fileio_error target_errno; - xfree (stream); - /* Ignore errors on close. These may happen with remote targets if the connection has already been torn down. */ try { - target_fileio_close (fd, &target_errno); + target_fileio_close (m_fd, &target_errno); } catch (const gdb_exception &ex) { /* Also avoid crossing exceptions over bfd. */ - gdb_bfd_close_warning (bfd_get_filename (abfd), + gdb_bfd_close_warning (bfd_get_filename (m_bfd), ex.message->c_str ()); } - - /* Zero means success. */ - return 0; } -/* Wrapper for target_fileio_fstat suitable for passing as the - STAT_FUNC argument to gdb_bfd_openr_iovec. */ +/* Wrapper for target_fileio_fstat. */ -static int -gdb_bfd_iovec_fileio_fstat (struct bfd *abfd, void *stream, - struct stat *sb) +int +target_fileio_stream::stat (struct bfd *abfd, struct stat *sb) { - int fd = *(int *) stream; fileio_error target_errno; int result; - result = target_fileio_fstat (fd, sb, &target_errno); + result = target_fileio_fstat (m_fd, sb, &target_errno); if (result == -1) { errno = fileio_error_to_host (target_errno); @@ -482,13 +485,13 @@ gdb_bfd_open (const char *name, const char *target, int fd, { gdb_assert (fd == -1); - gdb_bfd_open_closure open_closure { current_inferior (), warn_if_slow }; - return gdb_bfd_openr_iovec (name, target, - gdb_bfd_iovec_fileio_open, - &open_closure, - gdb_bfd_iovec_fileio_pread, - gdb_bfd_iovec_fileio_close, - gdb_bfd_iovec_fileio_fstat); + auto open = [&] (bfd *nbfd) -> gdb_bfd_iovec_base * + { + return gdb_bfd_iovec_fileio_open (nbfd, current_inferior (), + warn_if_slow); + }; + + return gdb_bfd_openr_iovec (name, target, open); } name += strlen (TARGET_SYSROOT_PREFIX); -- 2.40.1