summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Tromey <tromey@adacore.com>2022-07-20 12:46:08 -0600
committerTom Tromey <tromey@adacore.com>2022-08-16 08:04:37 -0600
commit02d04eac24f497e34ac9869bac5bfb1e044a6b62 (patch)
tree5dbf104d58fe1abec6fe1338fb823c78a443de06
parentec29a63c80602056bfc8533d230394d0db14b982 (diff)
downloadbinutils-gdb-02d04eac24f497e34ac9869bac5bfb1e044a6b62.tar.gz
Use strwinerror in gdb/windows-nat.c
When working on windows-nat.c, it's useful to see an error message in addition to the error number given by GetLastError. This patch moves strwinerror from gdbserver to gdbsupport, and then updates windows-nat.c to use it. A couple of minor changes to strwinerror (constify the return type and use the ARRAY_SIZE macro) are also included.
-rw-r--r--gdb/nat/windows-nat.c22
-rw-r--r--gdb/windows-nat.c51
-rw-r--r--gdbserver/win32-low.cc59
-rw-r--r--gdbserver/win32-low.h11
-rw-r--r--gdbsupport/errors.cc54
-rw-r--r--gdbsupport/errors.h16
6 files changed, 119 insertions, 94 deletions
diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c
index ea6e2980f59..ba60b585e85 100644
--- a/gdb/nat/windows-nat.c
+++ b/gdb/nat/windows-nat.c
@@ -95,8 +95,8 @@ windows_thread_info::suspend ()
We can get Invalid Handle (6) if the main thread
has exited. */
if (err != ERROR_INVALID_HANDLE && err != ERROR_ACCESS_DENIED)
- warning (_("SuspendThread (tid=0x%x) failed. (winerr %u)"),
- (unsigned) tid, (unsigned) err);
+ warning (_("SuspendThread (tid=0x%x) failed. (winerr %u: %s)"),
+ (unsigned) tid, (unsigned) err, strwinerror (err));
suspended = -1;
}
else
@@ -113,8 +113,8 @@ windows_thread_info::resume ()
if (ResumeThread (h) == (DWORD) -1)
{
DWORD err = GetLastError ();
- warning (_("warning: ResumeThread (tid=0x%x) failed. (winerr %u)"),
- (unsigned) tid, (unsigned) err);
+ warning (_("warning: ResumeThread (tid=0x%x) failed. (winerr %u: %s)"),
+ (unsigned) tid, (unsigned) err, strwinerror (err));
}
}
suspended = 0;
@@ -204,8 +204,11 @@ windows_process_info::get_exec_module_filename (char *exe_name_ret,
len = GetModuleFileNameEx (handle,
dh_buf, pathbuf, exe_name_max_len);
if (len == 0)
- error (_("Error getting executable filename: %u."),
- (unsigned) GetLastError ());
+ {
+ unsigned err = (unsigned) GetLastError ();
+ error (_("Error getting executable filename (error %u): %s"),
+ err, strwinerror (err));
+ }
if (cygwin_conv_path (CCP_WIN_W_TO_POSIX, pathbuf, exe_name_ret,
exe_name_max_len) < 0)
error (_("Error converting executable filename to POSIX: %d."), errno);
@@ -214,8 +217,11 @@ windows_process_info::get_exec_module_filename (char *exe_name_ret,
len = GetModuleFileNameEx (handle,
dh_buf, exe_name_ret, exe_name_max_len);
if (len == 0)
- error (_("Error getting executable filename: %u."),
- (unsigned) GetLastError ());
+ {
+ unsigned err = (unsigned) GetLastError ();
+ error (_("Error getting executable filename (error %u): %s"),
+ err, strwinerror (err));
+ }
#endif
return 1; /* success */
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 9265ed9e632..1e310371e47 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -325,8 +325,11 @@ static void
check (BOOL ok, const char *file, int line)
{
if (!ok)
- gdb_printf ("error return %s:%d was %u\n", file, line,
- (unsigned) GetLastError ());
+ {
+ unsigned err = (unsigned) GetLastError ();
+ gdb_printf ("error return %s:%d was %u: %s\n", file, line,
+ err, strwinerror (err));
+ }
}
/* See nat/windows-nat.h. */
@@ -1160,9 +1163,12 @@ windows_continue (DWORD continue_status, int id, int killed)
res = continue_last_debug_event (continue_status, debug_events);
if (!res)
- error (_("Failed to resume program execution"
- " (ContinueDebugEvent failed, error %u)"),
- (unsigned int) GetLastError ());
+ {
+ unsigned err = (unsigned) GetLastError ();
+ error (_("Failed to resume program execution"
+ " (ContinueDebugEvent failed, error %u: %s)"),
+ err, strwinerror (err));
+ }
return res;
}
@@ -1179,8 +1185,9 @@ windows_nat_target::fake_create_process ()
windows_process.open_process_used = 1;
else
{
- error (_("OpenProcess call failed, GetLastError = %u"),
- (unsigned) GetLastError ());
+ unsigned err = (unsigned) GetLastError ();
+ error (_("OpenProcess call failed, GetLastError = %u: %s"),
+ err, strwinerror (err));
/* We can not debug anything in that case. */
}
add_thread (ptid_t (windows_process.current_event.dwProcessId, 0,
@@ -1887,8 +1894,11 @@ windows_nat_target::attach (const char *args, int from_tty)
#endif
if (!ok)
- error (_("Can't attach to process %u (error %u)"),
- (unsigned) pid, (unsigned) GetLastError ());
+ {
+ unsigned err = (unsigned) GetLastError ();
+ error (_("Can't attach to process %u (error %u: %s)"),
+ (unsigned) pid, err, strwinerror (err));
+ }
DebugSetProcessKillOnExit (FALSE);
@@ -1916,9 +1926,12 @@ windows_nat_target::detach (inferior *inf, int from_tty)
resume (ptid, 0, GDB_SIGNAL_0);
if (!DebugActiveProcessStop (windows_process.current_event.dwProcessId))
- error (_("Can't detach process %u (error %u)"),
- (unsigned) windows_process.current_event.dwProcessId,
- (unsigned) GetLastError ());
+ {
+ unsigned err = (unsigned) GetLastError ();
+ error (_("Can't detach process %u (error %u: %s)"),
+ (unsigned) windows_process.current_event.dwProcessId,
+ err, strwinerror (err));
+ }
DebugSetProcessKillOnExit (FALSE);
target_announce_detach (from_tty);
@@ -2526,8 +2539,11 @@ windows_nat_target::create_inferior (const char *exec_file,
tty = CreateFileA (inferior_tty.c_str (), GENERIC_READ | GENERIC_WRITE,
0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (tty == INVALID_HANDLE_VALUE)
- warning (_("Warning: Failed to open TTY %s, error %#x."),
- inferior_tty.c_str (), (unsigned) GetLastError ());
+ {
+ unsigned err = (unsigned) GetLastError ();
+ warning (_("Warning: Failed to open TTY %s, error %#x: %s"),
+ inferior_tty.c_str (), err, strwinerror (err));
+ }
}
if (redirected || tty != INVALID_HANDLE_VALUE)
{
@@ -2608,8 +2624,11 @@ windows_nat_target::create_inferior (const char *exec_file,
#endif /* !__CYGWIN__ */
if (!ret)
- error (_("Error creating process %s, (error %u)."),
- exec_file, (unsigned) GetLastError ());
+ {
+ unsigned err = (unsigned) GetLastError ();
+ error (_("Error creating process %s, (error %u: %s)"),
+ exec_file, err, strwinerror (err));
+ }
#ifdef __x86_64__
BOOL wow64;
diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc
index 27224077cea..5e2c028d683 100644
--- a/gdbserver/win32-low.cc
+++ b/gdbserver/win32-low.cc
@@ -61,10 +61,6 @@ gdbserver_windows_process windows_process;
#define _T(x) TEXT (x)
#endif
-#ifndef COUNTOF
-#define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
-#endif
-
int using_threads = 1;
const struct target_desc *win32_tdesc;
@@ -483,61 +479,6 @@ child_store_inferior_registers (struct regcache *regcache, int r)
(*the_low_target.store_inferior_register) (regcache, th, regno);
}
-/* Map the Windows error number in ERROR to a locale-dependent error
- message string and return a pointer to it. Typically, the values
- for ERROR come from GetLastError.
-
- The string pointed to shall not be modified by the application,
- but may be overwritten by a subsequent call to strwinerror
-
- The strwinerror function does not change the current setting
- of GetLastError. */
-
-char *
-strwinerror (DWORD error)
-{
- static char buf[1024];
- TCHAR *msgbuf;
- DWORD lasterr = GetLastError ();
- DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
- | FORMAT_MESSAGE_ALLOCATE_BUFFER,
- NULL,
- error,
- 0, /* Default language */
- (LPTSTR) &msgbuf,
- 0,
- NULL);
- if (chars != 0)
- {
- /* If there is an \r\n appended, zap it. */
- if (chars >= 2
- && msgbuf[chars - 2] == '\r'
- && msgbuf[chars - 1] == '\n')
- {
- chars -= 2;
- msgbuf[chars] = 0;
- }
-
- if (chars > ((COUNTOF (buf)) - 1))
- {
- chars = COUNTOF (buf) - 1;
- msgbuf [chars] = 0;
- }
-
-#ifdef UNICODE
- wcstombs (buf, msgbuf, chars + 1);
-#else
- strncpy (buf, msgbuf, chars + 1);
-#endif
- LocalFree (msgbuf);
- }
- else
- sprintf (buf, "unknown win32 error (%u)", (unsigned) error);
-
- SetLastError (lasterr);
- return buf;
-}
-
static BOOL
create_process (const char *program, char *args,
DWORD flags, PROCESS_INFORMATION *pi)
diff --git a/gdbserver/win32-low.h b/gdbserver/win32-low.h
index 58bb105b283..d7bb76b6a98 100644
--- a/gdbserver/win32-low.h
+++ b/gdbserver/win32-low.h
@@ -211,15 +211,4 @@ extern gdbserver_windows_process windows_process;
/* Retrieve the context for this thread, if not already retrieved. */
extern void win32_require_context (windows_nat::windows_thread_info *th);
-/* Map the Windows error number in ERROR to a locale-dependent error
- message string and return a pointer to it. Typically, the values
- for ERROR come from GetLastError.
-
- The string pointed to shall not be modified by the application,
- but may be overwritten by a subsequent call to strwinerror
-
- The strwinerror function does not change the current setting
- of GetLastError. */
-extern char * strwinerror (DWORD error);
-
#endif /* GDBSERVER_WIN32_LOW_H */
diff --git a/gdbsupport/errors.cc b/gdbsupport/errors.cc
index 703b2213756..b98d0154eb3 100644
--- a/gdbsupport/errors.cc
+++ b/gdbsupport/errors.cc
@@ -19,6 +19,9 @@
#include "common-defs.h"
#include "errors.h"
+#ifdef USE_WIN32API
+#include <windows.h>
+#endif /* USE_WIN32API */
/* See gdbsupport/errors.h. */
@@ -67,3 +70,54 @@ internal_warning (const char *file, int line, const char *fmt, ...)
internal_vwarning (file, line, fmt, ap);
va_end (ap);
}
+
+#ifdef USE_WIN32API
+
+/* See errors.h. */
+
+const char *
+strwinerror (ULONGEST error)
+{
+ static char buf[1024];
+ TCHAR *msgbuf;
+ DWORD lasterr = GetLastError ();
+ DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
+ | FORMAT_MESSAGE_ALLOCATE_BUFFER,
+ NULL,
+ error,
+ 0, /* Default language */
+ (LPTSTR) &msgbuf,
+ 0,
+ NULL);
+ if (chars != 0)
+ {
+ /* If there is an \r\n appended, zap it. */
+ if (chars >= 2
+ && msgbuf[chars - 2] == '\r'
+ && msgbuf[chars - 1] == '\n')
+ {
+ chars -= 2;
+ msgbuf[chars] = 0;
+ }
+
+ if (chars > ARRAY_SIZE (buf) - 1)
+ {
+ chars = ARRAY_SIZE (buf) - 1;
+ msgbuf [chars] = 0;
+ }
+
+#ifdef UNICODE
+ wcstombs (buf, msgbuf, chars + 1);
+#else
+ strncpy (buf, msgbuf, chars + 1);
+#endif
+ LocalFree (msgbuf);
+ }
+ else
+ sprintf (buf, "unknown win32 error (%u)", (unsigned) error);
+
+ SetLastError (lasterr);
+ return buf;
+}
+
+#endif /* USE_WIN32API */
diff --git a/gdbsupport/errors.h b/gdbsupport/errors.h
index 47965ea8a3c..9a671d3e289 100644
--- a/gdbsupport/errors.h
+++ b/gdbsupport/errors.h
@@ -91,4 +91,20 @@ extern void malloc_failure (long size) ATTRIBUTE_NORETURN;
extern void flush_streams ();
+#ifdef USE_WIN32API
+
+/* Map the Windows error number in ERROR to a locale-dependent error
+ message string and return a pointer to it. Typically, the values
+ for ERROR come from GetLastError.
+
+ The string pointed to shall not be modified by the application,
+ but may be overwritten by a subsequent call to strwinerror
+
+ The strwinerror function does not change the current setting
+ of GetLastError. */
+
+extern const char *strwinerror (ULONGEST error);
+
+#endif /* USE_WIN32API */
+
#endif /* COMMON_ERRORS_H */