https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=ae3370bb9d1bf1832b19cd6da005f4ec567c57e3

commit ae3370bb9d1bf1832b19cd6da005f4ec567c57e3
Author: Corinna Vinschen <cori...@vinschen.de>
Date:   Sat Mar 23 17:50:00 2019 +0100

    Cygwin: strace: print windows and cygwin pid in event output
    
    strace only printed the Windows PID in event output so far.
    
    Especially now that Windows and Cygwin PID are decoupled, the
    strace user might like to see the Cygwin pid in event output as
    well.  However, at process startup, the process might not have
    a Cygwin PID yet.
    
    To mitigate this, always print the Windows PID and only add the
    Cygwin pid if it exists.
    
    Signed-off-by: Corinna Vinschen <cori...@vinschen.de>

Diff:
---
 winsup/cygwin/external.cc          |  4 ++++
 winsup/cygwin/include/sys/cygwin.h |  2 ++
 winsup/utils/strace.cc             | 49 ++++++++++++++++++++++++++++----------
 3 files changed, 42 insertions(+), 13 deletions(-)

diff --git a/winsup/cygwin/external.cc b/winsup/cygwin/external.cc
index fbb901f..94431bb 100644
--- a/winsup/cygwin/external.cc
+++ b/winsup/cygwin/external.cc
@@ -331,6 +331,10 @@ cygwin_internal (cygwin_getinfo_types t, ...)
        }
        break;
 
+      case CW_MAX_CYGWIN_PID:
+       res = MAX_PID;
+       break;
+
       case CW_EXTRACT_DOMAIN_AND_USER:
        {
          WCHAR nt_domain[MAX_DOMAIN_NAME_LEN + 1];
diff --git a/winsup/cygwin/include/sys/cygwin.h 
b/winsup/cygwin/include/sys/cygwin.h
index afc193f..fc91d04 100644
--- a/winsup/cygwin/include/sys/cygwin.h
+++ b/winsup/cygwin/include/sys/cygwin.h
@@ -159,6 +159,7 @@ typedef enum
     CW_EXCEPTION_RECORD_FROM_SIGINFO_T,
     CW_CYGHEAP_PROFTHR_ALL,
     CW_WINPID_TO_CYGWIN_PID,
+    CW_MAX_CYGWIN_PID,
   } cygwin_getinfo_types;
 
 #define CW_LOCK_PINFO CW_LOCK_PINFO
@@ -222,6 +223,7 @@ typedef enum
 #define CW_EXCEPTION_RECORD_FROM_SIGINFO_T CW_EXCEPTION_RECORD_FROM_SIGINFO_T
 #define CW_CYGHEAP_PROFTHR_ALL CW_CYGHEAP_PROFTHR_ALL
 #define CW_WINPID_TO_CYGWIN_PID CW_WINPID_TO_CYGWIN_PID
+#define CW_MAX_CYGWIN_PID CW_MAX_CYGWIN_PID
 
 /* Token type for CW_SET_EXTERNAL_TOKEN */
 enum
diff --git a/winsup/utils/strace.cc b/winsup/utils/strace.cc
index 21c0835..c8b2e2c 100644
--- a/winsup/utils/strace.cc
+++ b/winsup/utils/strace.cc
@@ -672,6 +672,25 @@ GetFileNameFromHandle(HANDLE hFile, WCHAR 
pszFilename[MAX_PATH+1])
   return result;
 }
 
+static char *
+cygwin_pid (DWORD winpid)
+{
+  static char buf[48];
+  static DWORD max_cygpid = 0;
+  DWORD cygpid;
+
+  if (!max_cygpid)
+    max_cygpid = (DWORD) cygwin_internal (CW_MAX_CYGWIN_PID);
+
+  cygpid = (DWORD) cygwin_internal (CW_WINPID_TO_CYGWIN_PID, winpid);
+
+  if (cygpid >= max_cygpid)
+    snprintf (buf, sizeof buf, "%lu", winpid);
+  else
+    snprintf (buf, sizeof buf, "%lu (pid: %lu)", winpid, cygpid);
+  return buf;
+}
+
 static DWORD
 proc_child (unsigned mask, FILE *ofile, pid_t pid)
 {
@@ -706,7 +725,8 @@ proc_child (unsigned mask, FILE *ofile, pid_t pid)
        {
        case CREATE_PROCESS_DEBUG_EVENT:
          if (events)
-           fprintf (ofile, "--- Process %lu created\n", ev.dwProcessId);
+           fprintf (ofile, "--- Process %s created\n",
+                    cygwin_pid (ev.dwProcessId));
          if (ev.u.CreateProcessInfo.hFile)
            CloseHandle (ev.u.CreateProcessInfo.hFile);
          add_child (ev.dwProcessId, ev.u.CreateProcessInfo.hProcess);
@@ -714,8 +734,8 @@ proc_child (unsigned mask, FILE *ofile, pid_t pid)
 
        case CREATE_THREAD_DEBUG_EVENT:
          if (events)
-           fprintf (ofile, "--- Process %lu thread %lu created\n",
-                    ev.dwProcessId, ev.dwThreadId);
+           fprintf (ofile, "--- Process %s thread %lu created\n",
+                    cygwin_pid (ev.dwProcessId), ev.dwThreadId);
          break;
 
        case LOAD_DLL_DEBUG_EVENT:
@@ -727,8 +747,9 @@ proc_child (unsigned mask, FILE *ofile, pid_t pid)
              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);
+             fprintf (ofile, "--- Process %s loaded %ls at %p\n",
+                      cygwin_pid (ev.dwProcessId), dllname,
+                      ev.u.LoadDll.lpBaseOfDll);
            }
 
          if (ev.u.LoadDll.hFile)
@@ -737,8 +758,8 @@ proc_child (unsigned mask, FILE *ofile, pid_t pid)
 
        case UNLOAD_DLL_DEBUG_EVENT:
          if (events)
-           fprintf (ofile, "--- Process %lu unloaded DLL at %p\n",
-                    ev.dwProcessId, ev.u.UnloadDll.lpBaseOfDll);
+           fprintf (ofile, "--- Process %s unloaded DLL at %p\n",
+                    cygwin_pid (ev.dwProcessId), ev.u.UnloadDll.lpBaseOfDll);
          break;
 
        case OUTPUT_DEBUG_STRING_EVENT:
@@ -749,16 +770,18 @@ proc_child (unsigned mask, FILE *ofile, pid_t pid)
 
        case EXIT_PROCESS_DEBUG_EVENT:
          if (events)
-           fprintf (ofile, "--- Process %lu exited with status 0x%lx\n",
-                    ev.dwProcessId, ev.u.ExitProcess.dwExitCode);
+           fprintf (ofile, "--- Process %s exited with status 0x%lx\n",
+                    cygwin_pid (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);
+           fprintf (ofile, "--- Process %s thread %lu exited with "
+                           "status 0x%lx\n",
+                    cygwin_pid (ev.dwProcessId), ev.dwThreadId,
+                    ev.u.ExitThread.dwExitCode);
          break;
 
        case EXCEPTION_DEBUG_EVENT:
@@ -770,8 +793,8 @@ proc_child (unsigned mask, FILE *ofile, pid_t pid)
            default:
              status = DBG_EXCEPTION_NOT_HANDLED;
              if (ev.u.Exception.dwFirstChance)
-               fprintf (ofile, "--- Process %lu, exception %08lx at %p\n",
-                        ev.dwProcessId,
+               fprintf (ofile, "--- Process %s, exception %08lx at %p\n",
+                        cygwin_pid (ev.dwProcessId),
                         ev.u.Exception.ExceptionRecord.ExceptionCode,
                         ev.u.Exception.ExceptionRecord.ExceptionAddress);
              break;

Reply via email to