public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* [PATCH] Improve strace to log most Windows debug events
@ 2015-06-10 12:05 Jon TURNEY
  2015-06-10 14:11 ` Corinna Vinschen
  2015-06-10 18:44 ` Jon TURNEY
  0 siblings, 2 replies; 7+ messages in thread
From: Jon TURNEY @ 2015-06-10 12:05 UTC (permalink / raw)
  To: cygwin-patches; +Cc: Jon TURNEY

Not sure if this is wanted, but on a couple of occasions recently I have been
presented with strace output which contains an exception at an address in an
unknown module (i.e. not in the cygwin DLL or the main executable), so here is a
patch which adds some more information, including DLL load addresses, to help
interpret such straces.

2015-06-07  Jon Turney  <jon.turney@dronecode.org.uk>

	* strace.cc (proc_child): Log process and thread create and exit,
	and DLL load and unload.
	(GetFileNameFromHandle): New function.
	* Makefile.in (strace.exe): Link against psapi.dll.

Signed-off-by: Jon TURNEY <jon.turney@dronecode.org.uk>
---
 winsup/utils/ChangeLog   |  7 ++++++
 winsup/utils/Makefile.in |  2 +-
 winsup/utils/strace.cc   | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/winsup/utils/ChangeLog b/winsup/utils/ChangeLog
index bfdb42a..7561a58 100644
--- a/winsup/utils/ChangeLog
+++ b/winsup/utils/ChangeLog
@@ -1,3 +1,10 @@
+2015-06-07  Jon Turney  <jon.turney@dronecode.org.uk>
+
+	* strace.cc (proc_child): Log process and thread create and exit,
+	and DLL load and unload.
+	(GetFileNameFromHandle): New function.
+	* Makefile.in (strace.exe): Link against psapi.dll.
+
 2015-04-21  Corinna Vinschen  <corinna@vinschen.de>
 
 	* tzmap-from-unicode.org: Convert Calcutta to Kolkata.
diff --git a/winsup/utils/Makefile.in b/winsup/utils/Makefile.in
index fe81d87..4dfe349 100644
--- a/winsup/utils/Makefile.in
+++ b/winsup/utils/Makefile.in
@@ -101,7 +101,7 @@ cygcheck.exe: ${CYGCHECK_OBJS}
 cygpath.o: CXXFLAGS += -fno-threadsafe-statics
 cygpath.exe: CYGWIN_LDFLAGS += -lcygwin -luserenv -lntdll
 ps.exe: CYGWIN_LDFLAGS += -lcygwin -lpsapi -lntdll
-strace.exe: MINGW_LDFLAGS += -lntdll
+strace.exe: MINGW_LDFLAGS += -lntdll -lpsapi
 
 ldd.exe:CYGWIN_LDFLAGS += -lpsapi
 pldd.exe: CYGWIN_LDFLAGS += -lpsapi
diff --git a/winsup/utils/strace.cc b/winsup/utils/strace.cc
index 73096ab..f54cab5 100644
--- a/winsup/utils/strace.cc
+++ b/winsup/utils/strace.cc
@@ -12,6 +12,7 @@ Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
 details. */
 
 #include <windows.h>
+#include <psapi.h>
 #include <winternl.h>
 #define cygwin_internal cygwin_internal_dontuse
 #include <stdio.h>
@@ -637,6 +638,36 @@ handle_output_debug_string (DWORD id, LPVOID p, unsigned mask, FILE *ofile)
     fflush (ofile);
 }
 
+// This is a pretty tragic way to convert HANDLE -> filename, but this is the
+// the way MSDN suggest to do it if you want to be portable to Windows versions
+// prior to 6.0, where GetFinalPathNameByHandle() isn't available.
+static BOOL
+GetFileNameFromHandle(HANDLE hFile, WCHAR pszFilename[MAX_PATH+1])
+{
+  HANDLE hFileMap;
+  BOOL result = FALSE;
+
+  // Create a file mapping object.
+  hFileMap = CreateFileMapping (hFile, NULL, PAGE_READONLY, 0, 1, NULL);
+  if (hFileMap)
+    {
+      // Create a file mapping to get the file name.
+      void* pMem = MapViewOfFile (hFileMap, FILE_MAP_READ, 0, 0, 1);
+      if (pMem)
+	{
+	  if (GetMappedFileNameW (GetCurrentProcess (), pMem, pszFilename,
+				  MAX_PATH))
+	    {
+	      result = TRUE;
+	    }
+	  UnmapViewOfFile(pMem);
+	}
+    CloseHandle(hFileMap);
+  }
+
+  return result;
+}
+
 static DWORD
 proc_child (unsigned mask, FILE *ofile, pid_t pid)
 {
@@ -670,19 +701,38 @@ proc_child (unsigned mask, FILE *ofile, pid_t pid)
       switch (ev.dwDebugEventCode)
 	{
 	case CREATE_PROCESS_DEBUG_EVENT:
+	  fprintf (ofile, "--- Process %lu created\n", ev.dwProcessId);
 	  if (ev.u.CreateProcessInfo.hFile)
 	    CloseHandle (ev.u.CreateProcessInfo.hFile);
 	  add_child (ev.dwProcessId, ev.u.CreateProcessInfo.hProcess);
 	  break;
 
 	case CREATE_THREAD_DEBUG_EVENT:
+	  fprintf (ofile, "--- Process %lu thread %lu created\n",
+		   ev.dwProcessId, ev.dwThreadId);
 	  break;
 
 	case LOAD_DLL_DEBUG_EVENT:
+	  {
+	    // lpImageName is not always populated, so find the filename for
+	    // hFile instead
+	    WCHAR dllname[MAX_PATH+1];
+	    if (!GetFileNameFromHandle(ev.u.LoadDll.hFile, dllname))
+		wcscpy(dllname, L"(unknown)");
+
+	    fprintf (ofile, "--- Process %lu loaded %ls at %p\n",
+		     ev.dwProcessId, dllname, ev.u.LoadDll.lpBaseOfDll);
+	  }
+
 	  if (ev.u.LoadDll.hFile)
 	    CloseHandle (ev.u.LoadDll.hFile);
 	  break;
 
+	case UNLOAD_DLL_DEBUG_EVENT:
+	  fprintf (ofile, "--- Process %lu unloaded DLL at %p\n",
+		   ev.dwProcessId, ev.u.UnloadDll.lpBaseOfDll);
+	  break;
+
 	case OUTPUT_DEBUG_STRING_EVENT:
 	  handle_output_debug_string (ev.dwProcessId,
 				      ev.u.DebugString.lpDebugStringData,
@@ -690,9 +740,17 @@ proc_child (unsigned mask, FILE *ofile, pid_t pid)
 	  break;
 
 	case EXIT_PROCESS_DEBUG_EVENT:
+	  fprintf (ofile, "--- Process %lu exited with status 0x%lx\n",
+		   ev.dwProcessId, ev.u.ExitProcess.dwExitCode);
 	  res = ev.u.ExitProcess.dwExitCode;
 	  remove_child (ev.dwProcessId);
 	  break;
+
+	case EXIT_THREAD_DEBUG_EVENT:
+	  fprintf (ofile, "--- Process %lu thread %lu exited with status 0x%lx\n",
+		   ev.dwProcessId, ev.dwThreadId, ev.u.ExitThread.dwExitCode);
+	  break;
+
 	case EXCEPTION_DEBUG_EVENT:
 	  if (ev.u.Exception.ExceptionRecord.ExceptionCode
 	      != (DWORD) STATUS_BREAKPOINT)
-- 
2.1.4

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Improve strace to log most Windows debug events
  2015-06-10 12:05 [PATCH] Improve strace to log most Windows debug events Jon TURNEY
@ 2015-06-10 14:11 ` Corinna Vinschen
  2015-06-10 14:18   ` Corinna Vinschen
  2015-06-10 18:44 ` Jon TURNEY
  1 sibling, 1 reply; 7+ messages in thread
From: Corinna Vinschen @ 2015-06-10 14:11 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 969 bytes --]

Hi Jon,

On Jun 10 13:05, Jon TURNEY wrote:
> Not sure if this is wanted, but on a couple of occasions recently I have been
> presented with strace output which contains an exception at an address in an
> unknown module (i.e. not in the cygwin DLL or the main executable), so here is a
> patch which adds some more information, including DLL load addresses, to help
> interpret such straces.

That's a nice addition.  Two points, though:

- Do we *always* want that output or do we want a way to switch it on
  and off?  If the latter, we can simply add another _STRACE_foo option
  for it.  

- The GetFileNameFromHandle function could be much simpler.  Rather than
  opening a mapping object for ev.u.LoadDll.hFile, just use the existing
  mapping object from ev.u.LoadDll.lpBaseOfDll.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Improve strace to log most Windows debug events
  2015-06-10 14:11 ` Corinna Vinschen
@ 2015-06-10 14:18   ` Corinna Vinschen
  2015-06-10 15:44     ` Corinna Vinschen
  0 siblings, 1 reply; 7+ messages in thread
From: Corinna Vinschen @ 2015-06-10 14:18 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 1119 bytes --]

On Jun 10 16:11, Corinna Vinschen wrote:
> Hi Jon,
> 
> On Jun 10 13:05, Jon TURNEY wrote:
> > Not sure if this is wanted, but on a couple of occasions recently I have been
> > presented with strace output which contains an exception at an address in an
> > unknown module (i.e. not in the cygwin DLL or the main executable), so here is a
> > patch which adds some more information, including DLL load addresses, to help
> > interpret such straces.
> 
> That's a nice addition.  Two points, though:
> 
> - Do we *always* want that output or do we want a way to switch it on
>   and off?  If the latter, we can simply add another _STRACE_foo option
>   for it.  
> 
> - The GetFileNameFromHandle function could be much simpler.  Rather than
>   opening a mapping object for ev.u.LoadDll.hFile, just use the existing
>   mapping object from ev.u.LoadDll.lpBaseOfDll.

    ...with the process handle taken from get_child(ev.dwProcessId).


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Improve strace to log most Windows debug events
  2015-06-10 14:18   ` Corinna Vinschen
@ 2015-06-10 15:44     ` Corinna Vinschen
  2015-06-10 17:23       ` Corinna Vinschen
  0 siblings, 1 reply; 7+ messages in thread
From: Corinna Vinschen @ 2015-06-10 15:44 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 1529 bytes --]

On Jun 10 16:18, Corinna Vinschen wrote:
> On Jun 10 16:11, Corinna Vinschen wrote:
> > Hi Jon,
> > 
> > On Jun 10 13:05, Jon TURNEY wrote:
> > > Not sure if this is wanted, but on a couple of occasions recently I have been
> > > presented with strace output which contains an exception at an address in an
> > > unknown module (i.e. not in the cygwin DLL or the main executable), so here is a
> > > patch which adds some more information, including DLL load addresses, to help
> > > interpret such straces.
> > 
> > That's a nice addition.  Two points, though:
> > 
> > - Do we *always* want that output or do we want a way to switch it on
> >   and off?  If the latter, we can simply add another _STRACE_foo option
> >   for it.  
> > 
> > - The GetFileNameFromHandle function could be much simpler.  Rather than
> >   opening a mapping object for ev.u.LoadDll.hFile, just use the existing
> >   mapping object from ev.u.LoadDll.lpBaseOfDll.
> 
>     ...with the process handle taken from get_child(ev.dwProcessId).

And since I'm generally fuzzy and unclear in my first reply:

Of course, ev.u.LoadDll.lpBaseOfDll is not the mapping *object*, but the
mapping *address*.  So you neither have to call CreateFileMapping nor
MapViewOfFile.  Just call GetMappedFileNameW (get_child (ev.dwProcessId),
ev.u.LoadDll.lpBaseOfDll, ...)


Sorry,
Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Improve strace to log most Windows debug events
  2015-06-10 15:44     ` Corinna Vinschen
@ 2015-06-10 17:23       ` Corinna Vinschen
  2015-06-10 18:47         ` Jon TURNEY
  0 siblings, 1 reply; 7+ messages in thread
From: Corinna Vinschen @ 2015-06-10 17:23 UTC (permalink / raw)
  To: cygwin-patches

[-- Attachment #1: Type: text/plain, Size: 3689 bytes --]

On Jun 10 17:44, Corinna Vinschen wrote:
> On Jun 10 16:18, Corinna Vinschen wrote:
> > On Jun 10 16:11, Corinna Vinschen wrote:
> > > Hi Jon,
> > > 
> > > On Jun 10 13:05, Jon TURNEY wrote:
> > > > Not sure if this is wanted, but on a couple of occasions recently I have been
> > > > presented with strace output which contains an exception at an address in an
> > > > unknown module (i.e. not in the cygwin DLL or the main executable), so here is a
> > > > patch which adds some more information, including DLL load addresses, to help
> > > > interpret such straces.
> > > 
> > > That's a nice addition.  Two points, though:
> > > 
> > > - Do we *always* want that output or do we want a way to switch it on
> > >   and off?  If the latter, we can simply add another _STRACE_foo option
> > >   for it.  
> > > 
> > > - The GetFileNameFromHandle function could be much simpler.  Rather than
> > >   opening a mapping object for ev.u.LoadDll.hFile, just use the existing
> > >   mapping object from ev.u.LoadDll.lpBaseOfDll.
> > 
> >     ...with the process handle taken from get_child(ev.dwProcessId).
> 
> And since I'm generally fuzzy and unclear in my first reply:
> 
> Of course, ev.u.LoadDll.lpBaseOfDll is not the mapping *object*, but the
> mapping *address*.  So you neither have to call CreateFileMapping nor
> MapViewOfFile.  Just call GetMappedFileNameW (get_child (ev.dwProcessId),
> ev.u.LoadDll.lpBaseOfDll, ...)

This works for me resulting in Win32 pathnames.  These are only the
affected diff hunks, I omited the rest.  Does that work for you?

Btw., I don't like using MAX_PATH as maximum path length, but I think
DLL paths can't be longer anyway, so that should be ok...


diff --git a/winsup/utils/strace.cc b/winsup/utils/strace.cc
index 73096ab..0661e17 100644
--- a/winsup/utils/strace.cc
+++ b/winsup/utils/strace.cc
@@ -54,6 +54,8 @@ static BOOL close_handle (HANDLE h, DWORD ok);
 
 #define CloseHandle(h) close_handle(h, 0)
 
+static void *drive_map;
+
 struct child_list
 {
   DWORD id;
@@ -637,6 +639,30 @@ handle_output_debug_string (DWORD id, LPVOID p, unsigned mask, FILE *ofile)
     fflush (ofile);
 }
 
+static BOOL
+GetFileNameFromHandle(HANDLE hFile, WCHAR pszFilename[MAX_PATH+1])
+{
+  BOOL result = FALSE;
+  ULONG len = 0;
+  OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION *) alloca (65536);
+  NTSTATUS status = NtQueryObject (hFile, ObjectNameInformation,
+				   ntfn, 65536, &len);
+  if (NT_SUCCESS (status))
+    {
+      PWCHAR win32path = ntfn->Name.Buffer;
+      win32path[ntfn->Name.Length / sizeof (WCHAR)] = L'\0';
+
+      /* NtQueryObject returns a native NT path.  (Try to) convert to Win32. */
+      if (drive_map)
+	win32path = (PWCHAR) cygwin_internal (CW_MAP_DRIVE_MAP, drive_map,
+					      win32path);
+      pszFilename[0] = L'\0';
+      wcsncat (pszFilename, win32path, MAX_PATH);
+      result = TRUE;
+    }
+  return result;
+}
+
 static DWORD
 proc_child (unsigned mask, FILE *ofile, pid_t pid)
 {
@@ -1090,7 +1143,13 @@ character #%d.\n", optarg, (int) (endptr - optarg), endptr);
   if (toggle)
     dotoggle (pid);
   else
-    ExitProcess (dostrace (mask, ofile, pid, argv + optind));
+    {
+      drive_map = (void *) cygwin_internal (CW_ALLOC_DRIVE_MAP);
+      DWORD ret = dostrace (mask, ofile, pid, argv + optind);
+      if (drive_map)
+	cygwin_internal (CW_FREE_DRIVE_MAP, drive_map);
+      ExitProcess (ret);
+    }
   return 0;
 }
 

Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] Improve strace to log most Windows debug events
  2015-06-10 12:05 [PATCH] Improve strace to log most Windows debug events Jon TURNEY
  2015-06-10 14:11 ` Corinna Vinschen
@ 2015-06-10 18:44 ` Jon TURNEY
  1 sibling, 0 replies; 7+ messages in thread
From: Jon TURNEY @ 2015-06-10 18:44 UTC (permalink / raw)
  To: cygwin-patches; +Cc: Jon TURNEY

Not sure if this is wanted, but on a couple of occasions recently I have been
presented with strace output which contains an exception at an address in an
unknown module (i.e. not in the cygwin DLL or the main executable), so here is a
patch which adds some more information, including DLL load addresses, to help
interpret such straces.

v2:
Use NtQueryObject() for HANDLE -> filename conversion
Add new '-e' option to toggle this additional logging

2015-06-07  Jon Turney  <jon.turney@dronecode.org.uk>

	* strace.cc (proc_child): Log process and thread create and exit,
	and DLL load and unload.
	(GetFileNameFromHandle): New function.

Signed-off-by: Jon TURNEY <jon.turney@dronecode.org.uk>
---
 winsup/doc/utils.xml   |  1 +
 winsup/utils/ChangeLog |  6 ++++
 winsup/utils/strace.cc | 75 ++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 80 insertions(+), 2 deletions(-)

diff --git a/winsup/doc/utils.xml b/winsup/doc/utils.xml
index adafc2b..673da3b 100644
--- a/winsup/doc/utils.xml
+++ b/winsup/doc/utils.xml
@@ -2002,6 +2002,7 @@ Trace system calls and signals
 
   -b, --buffer-size=SIZE       set size of output file buffer
   -d, --no-delta               don't display the delta-t microsecond timestamp
+  -e, --events                 log all Windows DEBUG_EVENTS (toggle - default true)
   -f, --trace-children         trace child processes (toggle - default true)
   -h, --help                   output usage information and exit
   -m, --mask=MASK              set message filter mask
diff --git a/winsup/utils/ChangeLog b/winsup/utils/ChangeLog
index f9a25b2..5a68a40 100644
--- a/winsup/utils/ChangeLog
+++ b/winsup/utils/ChangeLog
@@ -1,3 +1,9 @@
+2015-06-07  Jon Turney  <jon.turney@dronecode.org.uk>
+
+	* strace.cc (proc_child): Log process and thread create and exit,
+	and DLL load and unload.
+	(GetFileNameFromHandle): New function.
+
 2015-06-10  Corinna Vinschen  <corinna@vinschen.de>
 
 	* ps.cc (main): Widen UID field in long format to accommodate longer
diff --git a/winsup/utils/strace.cc b/winsup/utils/strace.cc
index 73096ab..4b0d669 100644
--- a/winsup/utils/strace.cc
+++ b/winsup/utils/strace.cc
@@ -40,6 +40,7 @@ static int forkdebug = 1;
 static int numerror = 1;
 static int show_usecs = 1;
 static int delta = 1;
+static int events = 1;
 static int hhmmss;
 static int bufsize;
 static int new_window;
@@ -54,6 +55,8 @@ static BOOL close_handle (HANDLE h, DWORD ok);
 
 #define CloseHandle(h) close_handle(h, 0)
 
+static void *drive_map;
+
 struct child_list
 {
   DWORD id;
@@ -637,6 +640,30 @@ handle_output_debug_string (DWORD id, LPVOID p, unsigned mask, FILE *ofile)
     fflush (ofile);
 }
 
+static BOOL
+GetFileNameFromHandle(HANDLE hFile, WCHAR pszFilename[MAX_PATH+1])
+{
+  BOOL result = FALSE;
+  ULONG len = 0;
+  OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION *) alloca (65536);
+  NTSTATUS status = NtQueryObject (hFile, ObjectNameInformation,
+				   ntfn, 65536, &len);
+  if (NT_SUCCESS (status))
+    {
+      PWCHAR win32path = ntfn->Name.Buffer;
+      win32path[ntfn->Name.Length / sizeof (WCHAR)] = L'\0';
+
+      /* NtQueryObject returns a native NT path.  (Try to) convert to Win32. */
+      if (drive_map)
+	win32path = (PWCHAR) cygwin_internal (CW_MAP_DRIVE_MAP, drive_map,
+					      win32path);
+      pszFilename[0] = L'\0';
+      wcsncat (pszFilename, win32path, MAX_PATH);
+      result = TRUE;
+    }
+  return result;
+}
+
 static DWORD
 proc_child (unsigned mask, FILE *ofile, pid_t pid)
 {
@@ -670,19 +697,42 @@ proc_child (unsigned mask, FILE *ofile, pid_t pid)
       switch (ev.dwDebugEventCode)
 	{
 	case CREATE_PROCESS_DEBUG_EVENT:
+	  if (events)
+	    fprintf (ofile, "--- Process %lu created\n", ev.dwProcessId);
 	  if (ev.u.CreateProcessInfo.hFile)
 	    CloseHandle (ev.u.CreateProcessInfo.hFile);
 	  add_child (ev.dwProcessId, ev.u.CreateProcessInfo.hProcess);
 	  break;
 
 	case CREATE_THREAD_DEBUG_EVENT:
+	  if (events)
+	    fprintf (ofile, "--- Process %lu thread %lu created\n",
+		     ev.dwProcessId, ev.dwThreadId);
 	  break;
 
 	case LOAD_DLL_DEBUG_EVENT:
+	  if (events)
+	    {
+	      // lpImageName is not always populated, so find the filename for
+	      // hFile instead
+	      WCHAR dllname[MAX_PATH+1];
+	      if (!GetFileNameFromHandle(ev.u.LoadDll.hFile, dllname))
+		wcscpy(dllname, L"(unknown)");
+
+	      fprintf (ofile, "--- Process %lu loaded %ls at %p\n",
+		       ev.dwProcessId, dllname, ev.u.LoadDll.lpBaseOfDll);
+	    }
+
 	  if (ev.u.LoadDll.hFile)
 	    CloseHandle (ev.u.LoadDll.hFile);
 	  break;
 
+	case UNLOAD_DLL_DEBUG_EVENT:
+	  if (events)
+	    fprintf (ofile, "--- Process %lu unloaded DLL at %p\n",
+		     ev.dwProcessId, ev.u.UnloadDll.lpBaseOfDll);
+	  break;
+
 	case OUTPUT_DEBUG_STRING_EVENT:
 	  handle_output_debug_string (ev.dwProcessId,
 				      ev.u.DebugString.lpDebugStringData,
@@ -690,9 +740,19 @@ proc_child (unsigned mask, FILE *ofile, pid_t pid)
 	  break;
 
 	case EXIT_PROCESS_DEBUG_EVENT:
+	  if (events)
+	    fprintf (ofile, "--- Process %lu exited with status 0x%lx\n",
+		     ev.dwProcessId, ev.u.ExitProcess.dwExitCode);
 	  res = ev.u.ExitProcess.dwExitCode;
 	  remove_child (ev.dwProcessId);
 	  break;
+
+	case EXIT_THREAD_DEBUG_EVENT:
+	  if (events)
+	    fprintf (ofile, "--- Process %lu thread %lu exited with status 0x%lx\n",
+		     ev.dwProcessId, ev.dwThreadId, ev.u.ExitThread.dwExitCode);
+	  break;
+
 	case EXCEPTION_DEBUG_EVENT:
 	  if (ev.u.Exception.ExceptionRecord.ExceptionCode
 	      != (DWORD) STATUS_BREAKPOINT)
@@ -873,6 +933,7 @@ Trace system calls and signals\n\
 \n\
   -b, --buffer-size=SIZE       set size of output file buffer\n\
   -d, --no-delta               don't display the delta-t microsecond timestamp\n\
+  -e, --events                 log all Windows DEBUG_EVENTS (toggle - default true)\n\
   -f, --trace-children         trace child processes (toggle - default true)\n\
   -h, --help                   output usage information and exit\n\
   -m, --mask=MASK              set message filter mask\n\
@@ -928,6 +989,7 @@ Trace system calls and signals\n\
 
 struct option longopts[] = {
   {"buffer-size", required_argument, NULL, 'b'},
+  {"events", no_argument, NULL, 'e'},
   {"help", no_argument, NULL, 'h'},
   {"flush-period", required_argument, NULL, 'S'},
   {"hex", no_argument, NULL, 'H'},
@@ -946,7 +1008,7 @@ struct option longopts[] = {
   {NULL, 0, NULL, 0}
 };
 
-static const char *const opts = "+b:dhHfm:no:p:qS:tTuVw";
+static const char *const opts = "+b:dehHfm:no:p:qS:tTuVw";
 
 static void
 print_version ()
@@ -994,6 +1056,9 @@ main (int argc, char **argv)
       case 'd':
 	delta ^= 1;
 	break;
+      case 'e':
+	events ^= 1;
+	break;
       case 'f':
 	forkdebug ^= 1;
 	break;
@@ -1090,7 +1155,13 @@ character #%d.\n", optarg, (int) (endptr - optarg), endptr);
   if (toggle)
     dotoggle (pid);
   else
-    ExitProcess (dostrace (mask, ofile, pid, argv + optind));
+    {
+      drive_map = (void *) cygwin_internal (CW_ALLOC_DRIVE_MAP);
+      DWORD ret = dostrace (mask, ofile, pid, argv + optind);
+      if (drive_map)
+	cygwin_internal (CW_FREE_DRIVE_MAP, drive_map);
+      ExitProcess (ret);
+    }
   return 0;
 }
 
-- 
2.1.4

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Improve strace to log most Windows debug events
  2015-06-10 17:23       ` Corinna Vinschen
@ 2015-06-10 18:47         ` Jon TURNEY
  0 siblings, 0 replies; 7+ messages in thread
From: Jon TURNEY @ 2015-06-10 18:47 UTC (permalink / raw)
  To: cygwin-patches

On 10/06/2015 18:23, Corinna Vinschen wrote:
> On Jun 10 17:44, Corinna Vinschen wrote:
>> On Jun 10 16:18, Corinna Vinschen wrote:
>>> On Jun 10 16:11, Corinna Vinschen wrote:
>>>> Hi Jon,
>>>>
>>>> On Jun 10 13:05, Jon TURNEY wrote:
>>>>> Not sure if this is wanted, but on a couple of occasions recently I have been
>>>>> presented with strace output which contains an exception at an address in an
>>>>> unknown module (i.e. not in the cygwin DLL or the main executable), so here is a
>>>>> patch which adds some more information, including DLL load addresses, to help
>>>>> interpret such straces.
>>>>
>>>> That's a nice addition.  Two points, though:
>>>>
>>>> - Do we *always* want that output or do we want a way to switch it on
>>>>    and off?  If the latter, we can simply add another _STRACE_foo option
>>>>    for it.

I'm not sure it makes much sense to use a _STRACE_foo flag, since those 
form a mask applied to a set of flags emitted by the cygwin DLL.

I added a command line option to turn this additional logging off.

>>>>
>>>> - The GetFileNameFromHandle function could be much simpler.  Rather than
>>>>    opening a mapping object for ev.u.LoadDll.hFile, just use the existing
>>>>    mapping object from ev.u.LoadDll.lpBaseOfDll.
>>>
>>>      ...with the process handle taken from get_child(ev.dwProcessId).
>>
>> And since I'm generally fuzzy and unclear in my first reply:
>>
>> Of course, ev.u.LoadDll.lpBaseOfDll is not the mapping *object*, but the
>> mapping *address*.  So you neither have to call CreateFileMapping nor
>> MapViewOfFile.  Just call GetMappedFileNameW (get_child (ev.dwProcessId),
>> ev.u.LoadDll.lpBaseOfDll, ...)
>
> This works for me resulting in Win32 pathnames.  These are only the
> affected diff hunks, I omited the rest.  Does that work for you?
>
> Btw., I don't like using MAX_PATH as maximum path length, but I think
> DLL paths can't be longer anyway, so that should be ok...
>
>
> diff --git a/winsup/utils/strace.cc b/winsup/utils/strace.cc
> index 73096ab..0661e17 100644
> --- a/winsup/utils/strace.cc
> +++ b/winsup/utils/strace.cc

Yes, that seems to work.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-06-10 18:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-10 12:05 [PATCH] Improve strace to log most Windows debug events Jon TURNEY
2015-06-10 14:11 ` Corinna Vinschen
2015-06-10 14:18   ` Corinna Vinschen
2015-06-10 15:44     ` Corinna Vinschen
2015-06-10 17:23       ` Corinna Vinschen
2015-06-10 18:47         ` Jon TURNEY
2015-06-10 18:44 ` Jon TURNEY

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).