aboutsummaryrefslogtreecommitdiff
path: root/gdbsupport
diff options
context:
space:
mode:
authorAaron Merey <amerey@redhat.com>2023-02-09 20:28:20 -0500
committerAaron Merey <amerey@redhat.com>2023-02-10 21:04:45 -0500
commit40dfb28b56fe55a370a35495e0f1eb6c95110f35 (patch)
treee5a51168f2bf2aef021346f20844ac505dcdd924 /gdbsupport
parentbad727e2d2d1d7f86e1bcdb8eb9ce778182b5926 (diff)
downloadgdb-40dfb28b56fe55a370a35495e0f1eb6c95110f35.zip
gdb-40dfb28b56fe55a370a35495e0f1eb6c95110f35.tar.gz
gdb-40dfb28b56fe55a370a35495e0f1eb6c95110f35.tar.bz2
Move implementation of perror_with_name to gdbsupport
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. Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdbsupport')
-rw-r--r--gdbsupport/errors.cc24
-rw-r--r--gdbsupport/errors.h13
2 files changed, 34 insertions, 3 deletions
diff --git a/gdbsupport/errors.cc b/gdbsupport/errors.cc
index 566be37..b48ce10 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 2304bc1..20f9152 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