From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12771 invoked by alias); 9 Nov 2011 12:03:28 -0000 Received: (qmail 12756 invoked by uid 22791); 9 Nov 2011 12:03:24 -0000 X-SWARE-Spam-Status: No, hits=-1.3 required=5.0 tests=AWL,BAYES_00,FROM_12LTRDOM X-Spam-Check-By: sourceware.org Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 09 Nov 2011 12:02:59 +0000 Received: from svr-orw-fem-01.mgc.mentorg.com ([147.34.98.93]) by relay1.mentorg.com with esmtp id 1RO6rq-0002uU-Oj from Maciej_Rozycki@mentor.com for gdb-patches@sourceware.org; Wed, 09 Nov 2011 04:02:58 -0800 Received: from SVR-IES-FEM-01.mgc.mentorg.com ([137.202.0.104]) by svr-orw-fem-01.mgc.mentorg.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Wed, 9 Nov 2011 04:02:52 -0800 Received: from [172.30.5.200] (137.202.0.76) by SVR-IES-FEM-01.mgc.mentorg.com (137.202.0.104) with Microsoft SMTP Server id 14.1.289.1; Wed, 9 Nov 2011 12:02:50 +0000 Date: Wed, 09 Nov 2011 12:03:00 -0000 From: "Maciej W. Rozycki" To: Subject: [PATCH] windows-nat: Decode system error numbers Message-ID: User-Agent: Alpine 1.10 (DEB 962 2008-03-14) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2011-11/txt/msg00223.txt.bz2 Hi, A while ago I have come across a problem while debugging a MinGW program natively where the backend reported an error numerically. As quite obviously I couldn't figure out what the cause of the error was I went back to the sources and discovered that we have this piece of code in windows-nat that fails to decode Windows system errors. With the patch below I was able to track down what the problem was. Now I don't have that setup available anymore (that was something weird anyway), so to validate this change before submission I simulated an error scenario by running GDB under GDB and deliberately corrupting data. I chose windows_kill_inferior as it's easy to trigger by just killing the inferior from the debuggee GDB. So an example session looks like: (gdb) break windows_kill_inferior Breakpoint 1 at 0x519cf4: file .../gdb/windows-nat.c, line 2312. (gdb) continue Continuing. [Switching to Thread 4700.0x1bb4] Breakpoint 1, windows_kill_inferior (ops=0x953180) at .../gdb/windows-nat.c:2312 2312 CHECK (TerminateProcess (current_process_handle, 0)); (gdb) print current_process_handle $1 = (HANDLE) 0x148 (gdb) set variable current_process_handle = 0 (gdb) continue Continuing. and as it stands this is printed by the debuggee GDB: error return .../gdb/windows-nat.c:2312 was 6 but with the patch applied a proper error message is produced instead: error return .../gdb/windows-nat.c:2332 was: The handle is invalid. (6) As Windows tends to terminate its error messages with a CR+LF sequence, these characters are stripped by my code so that the result remains a single line (I decided not to strip the trailing full stop though and consequently added one to the fallback message for consistency). This code builds and works as manually verified above; these errors never trigger in the test suite so that doesn't really cover it -- my understanding is they are not meant to trigger unless GDB or the system is malfunctioning for some reason. OK to apply? 2011-11-09 Maciej W. Rozycki gdb/ * windows-nat.c (check): Decode the error number retrieved with GetLastError. Maciej gdb-windows-nat-check.patch Index: gdb-fsf-trunk-quilt/gdb/windows-nat.c =================================================================== --- gdb-fsf-trunk-quilt.orig/gdb/windows-nat.c 2011-11-09 11:38:15.000000000 +0000 +++ gdb-fsf-trunk-quilt/gdb/windows-nat.c 2011-11-09 11:48:36.765865140 +0000 @@ -274,9 +274,29 @@ windows_set_context_register_offsets (co static void check (BOOL ok, const char *file, int line) { - if (!ok) - printf_filtered ("error return %s:%d was %lu\n", file, line, - GetLastError ()); + const char *msg = "Unspecified error."; + unsigned long err; + char buf[1025]; + size_t size; + + if (ok) + return; + + err = GetLastError(); + size = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM + | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + err, + MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), + buf, (sizeof (buf) - 1) / sizeof (TCHAR), NULL); + if (size > 0 && buf[size - 1] == '\n') + buf[--size] = '\0'; + if (size > 0 && buf[size - 1] == '\r') + buf[--size] = '\0'; + if (size > 0) + msg = buf; + + printf_filtered ("error return %s:%d was: %s (%lu)\n", file, line, msg, err); } /* Find a thread record given a thread id. If GET_CONTEXT is not 0,