]> sourceware.org Git - newlib-cygwin.git/commitdiff
Cygwin: strace: print windows and cygwin pid in event output
authorCorinna Vinschen <corinna@vinschen.de>
Sat, 23 Mar 2019 16:50:00 +0000 (17:50 +0100)
committerCorinna Vinschen <corinna@vinschen.de>
Sat, 23 Mar 2019 16:50:00 +0000 (17:50 +0100)
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 <corinna@vinschen.de>
winsup/cygwin/external.cc
winsup/cygwin/include/sys/cygwin.h
winsup/utils/strace.cc

index fbb901f3ae432ca8e026bc2d05ee9642c5425242..94431bb5292df3341650d823333572c84a6dfa11 100644 (file)
@@ -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];
index afc193fb7249d0363426da18ac78705b609aecd5..fc91d04ac415679ee0ba7cadaff2af97eb8807a5 100644 (file)
@@ -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
index 21c0835d4c45c3be1028209c30d612341775c3b5..c8b2e2cf502d54506bed5d2fa2a051c6a153e1ed 100644 (file)
@@ -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;
This page took 0.041301 seconds and 5 git commands to generate.