From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id D01A73858C2B for ; Mon, 18 Sep 2023 14:52:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D01A73858C2B 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-x12a.google.com with SMTP id e9e14a558f8ab-34fd83200efso5879665ab.0 for ; Mon, 18 Sep 2023 07:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1695048770; x=1695653570; darn=sourceware.org; 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=JBRc7N7UMM5sq8mdCBoM4oeRZNnOOyKN/O6QUWpFebA=; b=RwMfvPGiDNYmaTWoXdGCKR6s59ZaCvKLQEHlarssYKPd+zvbU4SX3H1Bm7p5eWAIVC cOiCdV8M7wMnXWF6kKStLUE5b/8Bj/wcIit47/IvnjPfUdekcd1gosDdNBgGhVRxpPZT H4PItX+3aMBNPjCVK0/SgW6jaaI9TwZ1qNrr52nmmnesBuiG85SL/ZnK/WtZwy8gAXyl n6Qz7k9GW6kx5qpXNZpGDAVTUFvwKjj7HoL4OQTyskl4Osgtvb3fk1E1QlSpc0n/J5rD jWw4Eg5Yj5lDp9Niowk9z744RFbag3QSoDY5lNfIRVgmxxN6jSDM/Q0bFStFMSBfX/75 0B/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695048770; x=1695653570; 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=JBRc7N7UMM5sq8mdCBoM4oeRZNnOOyKN/O6QUWpFebA=; b=dSDIW0ZCfgiY5ozzq/LybnyYabxGitAvH6/irgX3TTWBB34YbBDktDbn1f7uXW91r+ EZK2+li/yPYAAdmc6oZjYHejU4sM5TAbnBIbr5z27EXQHGvnJGugRwIA+zopSCi5i6x8 r0mjlom7vOVjyWtTmn2ydbaYnOoGq1QaNlQ9aK2NeATdlqD6/9xzAXYoJPkhOybOnX6+ N3fC3aFnSpTGIg73l84WeDsNpKlr3IqNqZV5n59+jFAH7gyx/TDbC9GJkFkjA/IgV8ZS z+LTTjeeH2rjRKo8nm3fcSCiHqHU4ZTlSPUfCzyRmioqd38pHQGiRJzLiqrdj/4UnMrW 1pLg== X-Gm-Message-State: AOJu0YwfIjtTg2t/hKKwlqRhScF/tzSaERNIwImRMCWiA7uIodoawTHs rtq5jTCbYfyVbYaC92/FzINm8nd7h9do4JhO2QM9mA== X-Google-Smtp-Source: AGHT+IHLixYlujFKkV8dndBtkDW/VL5me5LCN8eOAAeQCzoORohzgkCtl9q0bYEbIUkLn9LfWXjtrw== X-Received: by 2002:a05:6e02:1546:b0:34f:6f44:aec4 with SMTP id j6-20020a056e02154600b0034f6f44aec4mr14108200ilu.8.1695048770078; Mon, 18 Sep 2023 07:52:50 -0700 (PDT) Received: from localhost.localdomain (71-211-130-31.hlrn.qwest.net. [71.211.130.31]) by smtp.gmail.com with ESMTPSA id v19-20020a056e020f9300b0034f65b28335sm2359256ilo.66.2023.09.18.07.52.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 07:52:49 -0700 (PDT) From: Tom Tromey Date: Mon, 18 Sep 2023 08:52:49 -0600 Subject: [PATCH v2 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: <20230918-gdb-bfd-vec-v2-4-162c0e9a2bc9@adacore.com> References: <20230918-gdb-bfd-vec-v2-0-162c0e9a2bc9@adacore.com> In-Reply-To: <20230918-gdb-bfd-vec-v2-0-162c0e9a2bc9@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 e331703d4b1..2f489a4f210 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