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.
This commit is contained in:
Tom Tromey
2022-07-20 12:46:08 -06:00
parent ec29a63c80
commit 02d04eac24
6 changed files with 119 additions and 94 deletions

View File

@ -95,8 +95,8 @@ windows_thread_info::suspend ()
We can get Invalid Handle (6) if the main thread We can get Invalid Handle (6) if the main thread
has exited. */ has exited. */
if (err != ERROR_INVALID_HANDLE && err != ERROR_ACCESS_DENIED) if (err != ERROR_INVALID_HANDLE && err != ERROR_ACCESS_DENIED)
warning (_("SuspendThread (tid=0x%x) failed. (winerr %u)"), warning (_("SuspendThread (tid=0x%x) failed. (winerr %u: %s)"),
(unsigned) tid, (unsigned) err); (unsigned) tid, (unsigned) err, strwinerror (err));
suspended = -1; suspended = -1;
} }
else else
@ -113,8 +113,8 @@ windows_thread_info::resume ()
if (ResumeThread (h) == (DWORD) -1) if (ResumeThread (h) == (DWORD) -1)
{ {
DWORD err = GetLastError (); DWORD err = GetLastError ();
warning (_("warning: ResumeThread (tid=0x%x) failed. (winerr %u)"), warning (_("warning: ResumeThread (tid=0x%x) failed. (winerr %u: %s)"),
(unsigned) tid, (unsigned) err); (unsigned) tid, (unsigned) err, strwinerror (err));
} }
} }
suspended = 0; suspended = 0;
@ -204,8 +204,11 @@ windows_process_info::get_exec_module_filename (char *exe_name_ret,
len = GetModuleFileNameEx (handle, len = GetModuleFileNameEx (handle,
dh_buf, pathbuf, exe_name_max_len); dh_buf, pathbuf, exe_name_max_len);
if (len == 0) 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, if (cygwin_conv_path (CCP_WIN_W_TO_POSIX, pathbuf, exe_name_ret,
exe_name_max_len) < 0) exe_name_max_len) < 0)
error (_("Error converting executable filename to POSIX: %d."), errno); 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, len = GetModuleFileNameEx (handle,
dh_buf, exe_name_ret, exe_name_max_len); dh_buf, exe_name_ret, exe_name_max_len);
if (len == 0) 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 #endif
return 1; /* success */ return 1; /* success */

View File

@ -325,8 +325,11 @@ static void
check (BOOL ok, const char *file, int line) check (BOOL ok, const char *file, int line)
{ {
if (!ok) 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. */ /* 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); res = continue_last_debug_event (continue_status, debug_events);
if (!res) if (!res)
error (_("Failed to resume program execution" {
" (ContinueDebugEvent failed, error %u)"), unsigned err = (unsigned) GetLastError ();
(unsigned int) GetLastError ()); error (_("Failed to resume program execution"
" (ContinueDebugEvent failed, error %u: %s)"),
err, strwinerror (err));
}
return res; return res;
} }
@ -1179,8 +1185,9 @@ windows_nat_target::fake_create_process ()
windows_process.open_process_used = 1; windows_process.open_process_used = 1;
else else
{ {
error (_("OpenProcess call failed, GetLastError = %u"), unsigned err = (unsigned) GetLastError ();
(unsigned) GetLastError ()); error (_("OpenProcess call failed, GetLastError = %u: %s"),
err, strwinerror (err));
/* We can not debug anything in that case. */ /* We can not debug anything in that case. */
} }
add_thread (ptid_t (windows_process.current_event.dwProcessId, 0, 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 #endif
if (!ok) 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); DebugSetProcessKillOnExit (FALSE);
@ -1916,9 +1926,12 @@ windows_nat_target::detach (inferior *inf, int from_tty)
resume (ptid, 0, GDB_SIGNAL_0); resume (ptid, 0, GDB_SIGNAL_0);
if (!DebugActiveProcessStop (windows_process.current_event.dwProcessId)) if (!DebugActiveProcessStop (windows_process.current_event.dwProcessId))
error (_("Can't detach process %u (error %u)"), {
(unsigned) windows_process.current_event.dwProcessId, unsigned err = (unsigned) GetLastError ();
(unsigned) GetLastError ()); error (_("Can't detach process %u (error %u: %s)"),
(unsigned) windows_process.current_event.dwProcessId,
err, strwinerror (err));
}
DebugSetProcessKillOnExit (FALSE); DebugSetProcessKillOnExit (FALSE);
target_announce_detach (from_tty); 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, tty = CreateFileA (inferior_tty.c_str (), GENERIC_READ | GENERIC_WRITE,
0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); 0, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (tty == INVALID_HANDLE_VALUE) 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) if (redirected || tty != INVALID_HANDLE_VALUE)
{ {
@ -2608,8 +2624,11 @@ windows_nat_target::create_inferior (const char *exec_file,
#endif /* !__CYGWIN__ */ #endif /* !__CYGWIN__ */
if (!ret) 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__ #ifdef __x86_64__
BOOL wow64; BOOL wow64;

View File

@ -61,10 +61,6 @@ gdbserver_windows_process windows_process;
#define _T(x) TEXT (x) #define _T(x) TEXT (x)
#endif #endif
#ifndef COUNTOF
#define COUNTOF(STR) (sizeof (STR) / sizeof ((STR)[0]))
#endif
int using_threads = 1; int using_threads = 1;
const struct target_desc *win32_tdesc; 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); (*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 static BOOL
create_process (const char *program, char *args, create_process (const char *program, char *args,
DWORD flags, PROCESS_INFORMATION *pi) DWORD flags, PROCESS_INFORMATION *pi)

View File

@ -211,15 +211,4 @@ extern gdbserver_windows_process windows_process;
/* Retrieve the context for this thread, if not already retrieved. */ /* Retrieve the context for this thread, if not already retrieved. */
extern void win32_require_context (windows_nat::windows_thread_info *th); 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 */ #endif /* GDBSERVER_WIN32_LOW_H */

View File

@ -19,6 +19,9 @@
#include "common-defs.h" #include "common-defs.h"
#include "errors.h" #include "errors.h"
#ifdef USE_WIN32API
#include <windows.h>
#endif /* USE_WIN32API */
/* See gdbsupport/errors.h. */ /* See gdbsupport/errors.h. */
@ -67,3 +70,54 @@ internal_warning (const char *file, int line, const char *fmt, ...)
internal_vwarning (file, line, fmt, ap); internal_vwarning (file, line, fmt, ap);
va_end (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 */

View File

@ -91,4 +91,20 @@ extern void malloc_failure (long size) ATTRIBUTE_NORETURN;
extern void flush_streams (); 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 */ #endif /* COMMON_ERRORS_H */