From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id GNvQJw3C4mJhDBwAWB0awg (envelope-from ) for ; Thu, 28 Jul 2022 13:06:21 -0400 Received: by simark.ca (Postfix, from userid 112) id 9FFED1E9ED; Thu, 28 Jul 2022 13:06:21 -0400 (EDT) Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=Rfm4qUpR; dkim-atps=neutral X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id A3ECD1E87E for ; Thu, 28 Jul 2022 13:06:20 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 369CE385780F for ; Thu, 28 Jul 2022 17:06:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 369CE385780F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1659027980; bh=CnBDpyw3k3s6DgDOB4M//KEX38jDrWISLhCVWuRByRY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Rfm4qUpR0x+96jyFDSCEql4Au8jdBzkA8mxPYXaZVm/83LVyRH0VgQYzujtufex3A G9pYIGznEca07p82vEDTHaMOSEkNWBMs8xoplATjthZlrr3ukCMpLA6uMtPmsj07Re rToidETyXuEs8kUXhkvir/mOPm6HPgddZlH89SeU= Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by sourceware.org (Postfix) with ESMTPS id A7C773858D33 for ; Thu, 28 Jul 2022 17:05:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A7C773858D33 Received: by mail-ot1-x32c.google.com with SMTP id a14-20020a0568300b8e00b0061c4e3eb52aso1577319otv.3 for ; Thu, 28 Jul 2022 10:05:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=CnBDpyw3k3s6DgDOB4M//KEX38jDrWISLhCVWuRByRY=; b=57JTao4/tNW0nuWWyKK3wYrq6/5DtQjR1SHD/4TFlWSxHqQPr8lHvV2WIVITe6NXde gwFcPPGA5LAXK6+LY1NVDqwUtTbv7AkTRMNUePEjmSV9EiiY02tJYZVbvDijMYFuDrrO 1TYf5dWfE7bm2R4aXB8Gl6w2hEnF1bIdmKmBQRx9yjGzCF/trYJdXXjxmJdkuYTBBbb3 j7wIBdQn/mJaX16me42Zr07Vex1WFt526TZa/M1xisOIP1oPevrWOCbMdeaTkKl8urUS CLm2GdLj1bZN27ru0OVo8rIYGwR9fbgqRZpAsvdre13/JYg6TTKfbTy/DhgJwso8SY1v q6Iw== X-Gm-Message-State: AJIora/eSrCVR0YD0m5oS9P1aleNNoIl9m5PlQYs452wApWtV4Paj60Y OYo8tw9NsVxfCpUbqmy/3CETys0yBmLuag== X-Google-Smtp-Source: AGRyM1v1Y6g//wFito+opH+hwpyRD9yaBRMhth85M8DolBCeFpH8bILm6E07EoN6yQpxlsUnban9Bw== X-Received: by 2002:a9d:6484:0:b0:60b:eb0b:4054 with SMTP id g4-20020a9d6484000000b0060beb0b4054mr10835222otl.159.1659027958938; Thu, 28 Jul 2022 10:05:58 -0700 (PDT) Received: from murgatroyd.Home (71-211-185-228.hlrn.qwest.net. [71.211.185.228]) by smtp.gmail.com with ESMTPSA id i18-20020a056808031200b00326cb6225f8sm433220oie.44.2022.07.28.10.05.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 10:05:58 -0700 (PDT) To: gdb-patches@sourceware.org Subject: [PATCH] Use strwinerror in gdb/windows-nat.c Date: Thu, 28 Jul 2022 11:05:55 -0600 Message-Id: <20220728170555.85143-1-tromey@adacore.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: , From: Tom Tromey via Gdb-patches Reply-To: Tom Tromey Cc: Tom Tromey Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" 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. --- gdb/nat/windows-nat.c | 22 ++++++++++------ gdb/windows-nat.c | 51 ++++++++++++++++++++++++------------ gdbserver/win32-low.cc | 59 ------------------------------------------ gdbserver/win32-low.h | 11 -------- gdbsupport/errors.cc | 54 ++++++++++++++++++++++++++++++++++++++ gdbsupport/errors.h | 16 ++++++++++++ 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 +#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 */ -- 2.34.1