From patchwork Fri Feb 10 01:59:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Merey X-Patchwork-Id: 64619 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 10D253858C2D for ; Fri, 10 Feb 2023 02:01:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 10D253858C2D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1675994474; bh=JwC6gEl9OlYHQX2NLqbvxjWHM1j2BUSHAMC7RJCFulc=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=UdDtgNKJjEWVOsSezsWCPR8+Smp3PhGrkEe0SB8tp2ZuRU2ym/XZHzSnPl/Vlqyp9 /afe/GhVkwx5yPzRiEyvnjXnPpvEgxthmpc0jK/txY+B8iODNCrzUciNaIKK+bquqV moM6Mrc5lZvqaJgOf2d0APbsNx818zPgJoKWUQS8= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4CF433858C3A for ; Fri, 10 Feb 2023 02:00:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4CF433858C3A Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-26-dlhKItGKOlKpfR22pnb1CQ-1; Thu, 09 Feb 2023 21:00:04 -0500 X-MC-Unique: dlhKItGKOlKpfR22pnb1CQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C99E329AB3FD; Fri, 10 Feb 2023 02:00:03 +0000 (UTC) Received: from localhost.localdomain (unknown [10.22.10.224]) by smtp.corp.redhat.com (Postfix) with ESMTP id 805D7C16022; Fri, 10 Feb 2023 02:00:03 +0000 (UTC) To: gdb-patches@sourceware.org Cc: tom@tromey.com, keiths@redhat.com, Aaron Merey Subject: [PATCH 1/2] Move implementation of perror_with_name to gdbsupport Date: Thu, 9 Feb 2023 20:59:41 -0500 Message-Id: <20230210015942.3180401-2-amerey@redhat.com> In-Reply-To: <20230210015942.3180401-1-amerey@redhat.com> References: <20230210015942.3180401-1-amerey@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-16.0 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Aaron Merey via Gdb-patches From: Aaron Merey Reply-To: Aaron Merey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" gdbsupport/errors.h declares perror_with_name and leaves the implementation to the clients. However gdb and gdbserver's implementations are essentially the same, resulting in unnecessary code duplication. Fix this by implementing perror_with_name in gdbsupport. Add an optional parameter for specifying the errno used to generate the error message. Also move the implementation of perror_string to gdbsupport since perror_with_name requires it. --- gdb/utils.c | 36 ------------------------------------ gdbserver/utils.cc | 22 ---------------------- gdbsupport/errors.cc | 24 ++++++++++++++++++++++++ gdbsupport/errors.h | 13 ++++++++++--- 4 files changed, 34 insertions(+), 61 deletions(-) diff --git a/gdb/utils.c b/gdb/utils.c index 95adbe58e4a..d763e91d7e1 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -614,42 +614,6 @@ add_internal_problem_command (struct internal_problem *problem) } } -/* Return a newly allocated string, containing the PREFIX followed - by the system error message for errno (separated by a colon). */ - -static std::string -perror_string (const char *prefix) -{ - const char *err = safe_strerror (errno); - return std::string (prefix) + ": " + err; -} - -/* Print the system error message for errno, and also mention STRING - as the file name for which the error was encountered. Use ERRCODE - for the thrown exception. Then return to command level. */ - -static void ATTRIBUTE_NORETURN -throw_perror_with_name (enum errors errcode, const char *string) -{ - std::string combined = perror_string (string); - - /* I understand setting these is a matter of taste. Still, some people - may clear errno but not know about bfd_error. Doing this here is not - unreasonable. */ - bfd_set_error (bfd_error_no_error); - errno = 0; - - throw_error (errcode, _("%s."), combined.c_str ()); -} - -/* See throw_perror_with_name, ERRCODE defaults here to GENERIC_ERROR. */ - -void -perror_with_name (const char *string) -{ - throw_perror_with_name (GENERIC_ERROR, string); -} - /* Same as perror_with_name except that it prints a warning instead of throwing an error. */ diff --git a/gdbserver/utils.cc b/gdbserver/utils.cc index a6f5bd7b600..511364cfed2 100644 --- a/gdbserver/utils.cc +++ b/gdbserver/utils.cc @@ -51,28 +51,6 @@ malloc_failure (long size) abort_or_exit (); } -/* Print the system error message for errno, and also mention STRING - as the file name for which the error was encountered. - Then return to command level. */ - -void -perror_with_name (const char *string) -{ - const char *err; - char *combined; - - err = safe_strerror (errno); - if (err == NULL) - err = "unknown error"; - - combined = (char *) alloca (strlen (err) + strlen (string) + 3); - strcpy (combined, string); - strcat (combined, ": "); - strcat (combined, err); - - error ("%s.", combined); -} - /* Print an error message and return to top level. */ void diff --git a/gdbsupport/errors.cc b/gdbsupport/errors.cc index 566be377e3f..b48ce10eef8 100644 --- a/gdbsupport/errors.cc +++ b/gdbsupport/errors.cc @@ -71,6 +71,30 @@ internal_warning_loc (const char *file, int line, const char *fmt, ...) va_end (ap); } +/* See errors.h. */ + +std::string +perror_string (const char *prefix, int errnum) +{ + const char *err; + + if (errnum != 0) + err = safe_strerror (errnum); + else + err = safe_strerror (errno); + return std::string (prefix) + ": " + err; +} + +/* See errors.h. */ + +void +perror_with_name (const char *string, int errnum) +{ + std::string combined = perror_string (string, errnum); + + error (_("%s."), combined.c_str ()); +} + #if defined (USE_WIN32API) || defined(__CYGWIN__) /* See errors.h. */ diff --git a/gdbsupport/errors.h b/gdbsupport/errors.h index 2304bc1e3d1..20f9152b671 100644 --- a/gdbsupport/errors.h +++ b/gdbsupport/errors.h @@ -83,11 +83,18 @@ extern void internal_vwarning (const char *file, int line, ATTRIBUTE_PRINTF (3, 0); +/* Return a newly allocated string, containing the PREFIX followed + by the system error message for errno (separated by a colon). + If ERRNUM is given, then use it in place of errno. */ + +extern std::string perror_string (const char *prefix, int errnum = 0); + /* Like "error", but the error message is constructed by combining - STRING with the system error message for errno. This function does - not return. This function must be provided by the client. */ + STRING with the system error message for errno. If ERRNUM is given, + then use it in place of errno. This function does not return. */ -extern void perror_with_name (const char *string) ATTRIBUTE_NORETURN; +extern void perror_with_name (const char *string, int errnum = 0) + ATTRIBUTE_NORETURN; /* Call this function to handle memory allocation failures. This function does not return. This function must be provided by the