[1/2] Move implementation of perror_with_name to gdbsupport

Message ID 20230210015942.3180401-2-amerey@redhat.com
State Committed
Commit 40dfb28b56fe55a370a35495e0f1eb6c95110f35
Headers
Series gdb: Fix open_source_file error handling |

Commit Message

Aaron Merey Feb. 10, 2023, 1:59 a.m. UTC
  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(-)
  

Comments

Tom Tromey Feb. 10, 2023, 2:39 p.m. UTC | #1
>>>>> "Aaron" == Aaron Merey via Gdb-patches <gdb-patches@sourceware.org> writes:

Aaron> gdbsupport/errors.h declares perror_with_name and leaves the
Aaron> implementation to the clients.

Aaron> However gdb and gdbserver's implementations are essentially the
Aaron> same, resulting in unnecessary code duplication.

Aaron> Fix this by implementing perror_with_name in gdbsupport.  Add an
Aaron> optional parameter for specifying the errno used to generate the
Aaron> error message.

Aaron> Also move the implementation of perror_string to gdbsupport since
Aaron> perror_with_name requires it.

Thanks, this looks great.

Approved-By: Tom Tromey <tom@tromey.com>

Tom
  
Aaron Merey Feb. 11, 2023, 2:13 a.m. UTC | #2
On Fri, Feb 10, 2023 at 9:39 AM Tom Tromey <tom@tromey.com> wrote:
>
> >>>>> "Aaron" == Aaron Merey via Gdb-patches <gdb-patches@sourceware.org> writes:
>
> Aaron> gdbsupport/errors.h declares perror_with_name and leaves the
> Aaron> implementation to the clients.
>
> Aaron> However gdb and gdbserver's implementations are essentially the
> Aaron> same, resulting in unnecessary code duplication.
>
> Aaron> Fix this by implementing perror_with_name in gdbsupport.  Add an
> Aaron> optional parameter for specifying the errno used to generate the
> Aaron> error message.
>
> Aaron> Also move the implementation of perror_string to gdbsupport since
> Aaron> perror_with_name requires it.
>
> Thanks, this looks great.

Thanks, pushed as commit 40dfb28b56fe55a370a

Aaron
  

Patch

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