Cygwin: More profiler format + small issue fixes

Make sure to cast to ulong all DWORD values displayed with format "%lu".
More instances are fixed here than in either my earlier unused patch or
Corinna's patch. I decided to use typedef..ulong for more compact code.

Address jturney's reported small issues:
- Remove explicit external ref for cygwin_internal() as it is already
  provided by <sys/cygwin.h>.
- Leave intact ref for cygwin_dll_path[] as it is required by function(s)
  in path.cc that profiler uses. Added comment to that effect.
- Delete existing main() wrapper. Rename main2() to main(). This because
  profiler is now a Cygwin program and doesn't need to dynamically load
  cygwin1.dll.
- Documentation issues will be addressed in a separate xml patch.

(I would have linked message-ids of Corinna's and Jon's messages for
proper theading but I no longer have their original emails and the mail
archives don't show msgids any more.)
This commit is contained in:
Mark Geisert 2021-08-01 23:52:31 -07:00 committed by Corinna Vinschen
parent 99a4b087d4
commit 94ead7b76d
1 changed files with 20 additions and 40 deletions

View File

@ -14,7 +14,6 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <winternl.h> #include <winternl.h>
#define cygwin_internal cygwin_internal_dontuse
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <getopt.h> #include <getopt.h>
@ -27,14 +26,13 @@
#include <unistd.h> #include <unistd.h>
#include <sys/cygwin.h> #include <sys/cygwin.h>
#include "cygwin/version.h" #include "cygwin/version.h"
#include "cygtls_padsize.h"
#include "gcc_seh.h" #include "gcc_seh.h"
typedef unsigned long ulong;
typedef unsigned short ushort; typedef unsigned short ushort;
typedef uint16_t u_int16_t; // Non-standard sized type needed by ancient gmon.h typedef uint16_t u_int16_t; // Non-standard sized type needed by ancient gmon.h
#define NO_GLOBALS_H #define NO_GLOBALS_H
#include "gmon.h" #include "gmon.h"
#include "path.h" #include "path.h"
#undef cygwin_internal
/* Undo this #define from winsup.h. */ /* Undo this #define from winsup.h. */
#ifdef ExitThread #ifdef ExitThread
@ -201,7 +199,7 @@ bump_bucket (child *c, size_t pc)
{ {
span_list *s = c->spans; span_list *s = c->spans;
//note ("%lu %p\n", c->pid, pc); //note ("%lu %p\n", (ulong) c->pid, pc);
if (pc == 0ULL) if (pc == 0ULL)
return; return;
while (s) while (s)
@ -225,7 +223,7 @@ bump_bucket (child *c, size_t pc)
* profiling info on them will be confusing if their addresses overlap. * profiling info on them will be confusing if their addresses overlap.
*/ */
if (verbose) if (verbose)
note ("*** pc %p out of range for pid %lu\n", pc, c->pid); note ("*** pc %p out of range for pid %lu\n", pc, (ulong) c->pid);
} }
/* profiler runs on its own thread; each child has a separate profiler. */ /* profiler runs on its own thread; each child has a separate profiler. */
@ -258,7 +256,7 @@ start_profiler (child *c)
DWORD tid; DWORD tid;
if (verbose) if (verbose)
note ("*** start profiler thread on pid %lu\n", c->pid); note ("*** start profiler thread on pid %lu\n", (ulong) c->pid);
c->context = (CONTEXT *) calloc (1, sizeof (CONTEXT)); c->context = (CONTEXT *) calloc (1, sizeof (CONTEXT));
if (!c->context) if (!c->context)
error (0, "unable to allocate CONTEXT buffer\n"); error (0, "unable to allocate CONTEXT buffer\n");
@ -284,7 +282,7 @@ void
stop_profiler (child *c) stop_profiler (child *c)
{ {
if (verbose) if (verbose)
note ("*** stop profiler thread on pid %lu\n", c->pid); note ("*** stop profiler thread on pid %lu\n", (ulong) c->pid);
c->profiling = 0; c->profiling = 0;
SignalObjectAndWait (c->hquitevt, c->hprofthr, INFINITE, FALSE); SignalObjectAndWait (c->hquitevt, c->hprofthr, INFINITE, FALSE);
CloseHandle (c->hquitevt); CloseHandle (c->hquitevt);
@ -312,11 +310,10 @@ dump_profile_data (child *c)
if (s->name) if (s->name)
{ {
WCHAR *name = 1 + wcsrchr (s->name, L'\\'); WCHAR *name = 1 + wcsrchr (s->name, L'\\');
sprintf (filename, "%s.%lu.%ls", prefix, (unsigned long) c->pid, sprintf (filename, "%s.%lu.%ls", prefix, (ulong) c->pid, name);
name);
} }
else else
sprintf (filename, "%s.%lu", prefix, (unsigned long) c->pid); sprintf (filename, "%s.%lu", prefix, (ulong) c->pid);
fd = open (filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY); fd = open (filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY);
if (fd < 0) if (fd < 0)
@ -390,7 +387,7 @@ add_child (DWORD pid, WCHAR *name, LPVOID base, HANDLE hproc)
start_profiler (children.next); start_profiler (children.next);
numprocesses++; numprocesses++;
if (verbose) if (verbose)
note ("*** Windows process %lu attached\n", pid); note ("*** Windows process %lu attached\n", (ulong) pid);
} }
} }
@ -412,7 +409,7 @@ remove_child (DWORD pid)
c1->hproc = 0; c1->hproc = 0;
free (c1); free (c1);
if (verbose) if (verbose)
note ("*** Windows process %lu detached\n", pid); note ("*** Windows process %lu detached\n", (ulong) pid);
numprocesses--; numprocesses--;
return; return;
} }
@ -426,7 +423,7 @@ add_thread (DWORD pid, DWORD tid, HANDLE h, WCHAR *name)
child *c = get_child (pid); child *c = get_child (pid);
if (!c) if (!c)
error (0, "add_thread: pid %lu not found\n", pid); error (0, "add_thread: pid %lu not found\n", (ulong) pid);
thread_list *t = (thread_list *) calloc (1, sizeof (thread_list)); thread_list *t = (thread_list *) calloc (1, sizeof (thread_list));
t->tid = tid; t->tid = tid;
@ -443,7 +440,7 @@ remove_thread (DWORD pid, DWORD tid)
child *c = get_child (pid); child *c = get_child (pid);
if (!c) if (!c)
error (0, "remove_thread: pid %lu not found\n", pid); error (0, "remove_thread: pid %lu not found\n", (ulong) pid);
thread_list *t = c->threads; thread_list *t = c->threads;
while (t) while (t)
@ -462,7 +459,8 @@ remove_thread (DWORD pid, DWORD tid)
t = t->next; t = t->next;
} }
error (0, "remove_thread: pid %lu tid %lu not found\n", pid, tid); error (0, "remove_thread: pid %lu tid %lu not found\n",
(ulong) pid, (ulong) tid);
} }
void void
@ -531,7 +529,7 @@ add_span (DWORD pid, WCHAR *name, LPVOID base, HANDLE h)
child *c = get_child (pid); child *c = get_child (pid);
if (!c) if (!c)
error (0, "add_span: pid %lu not found\n", pid); error (0, "add_span: pid %lu not found\n", (ulong) pid);
IMAGE_SECTION_HEADER *sect = find_text_section (base, c->hproc); IMAGE_SECTION_HEADER *sect = find_text_section (base, c->hproc);
span_list *s = (span_list *) calloc (1, sizeof (span_list)); span_list *s = (span_list *) calloc (1, sizeof (span_list));
@ -655,11 +653,7 @@ ctrl_c (DWORD)
return TRUE; return TRUE;
} }
/* Set up interfaces to Cygwin internal funcs and path.cc helper funcs. */ WCHAR cygwin_dll_path[32768]; // required by path.cc helper funcs used herein
extern "C" {
uintptr_t cygwin_internal (int, ...);
WCHAR cygwin_dll_path[32768];
}
#define DEBUG_PROCESS_DETACH_ON_EXIT 0x00000001 #define DEBUG_PROCESS_DETACH_ON_EXIT 0x00000001
#define DEBUG_PROCESS_ONLY_THIS_PROCESS 0x00000002 #define DEBUG_PROCESS_ONLY_THIS_PROCESS 0x00000002
@ -754,7 +748,7 @@ handle_output_debug_string (DWORD pid, OUTPUT_DEBUG_STRING_INFO *ev)
child *c = get_child (pid); child *c = get_child (pid);
if (!c) if (!c)
error (0, "handle_output_debug_string: pid %lu not found\n", pid); error (0, "handle_output_debug_string: pid %lu not found\n", (ulong) pid);
read_child (buf, ev->nDebugStringLength, ev->lpDebugStringData, c->hproc); read_child (buf, ev->nDebugStringLength, ev->lpDebugStringData, c->hproc);
if (strncmp (buf, "cYg", 3)) if (strncmp (buf, "cYg", 3))
@ -805,10 +799,10 @@ cygwin_pid (DWORD winpid)
cygpid = (DWORD) cygwin_internal (CW_WINPID_TO_CYGWIN_PID, winpid); cygpid = (DWORD) cygwin_internal (CW_WINPID_TO_CYGWIN_PID, winpid);
if (cygpid >= max_cygpid) if (cygpid >= max_cygpid)
snprintf (buf, sizeof buf, "%lu", (unsigned long) winpid); snprintf (buf, sizeof (buf), "%lu", (ulong) winpid);
else else
snprintf (buf, sizeof buf, "%lu (pid: %lu)", (unsigned long) winpid, snprintf (buf, sizeof (buf), "%lu (pid: %lu)",
(unsigned long) cygpid); (ulong) winpid, (ulong) cygpid);
return buf; return buf;
} }
@ -1002,7 +996,7 @@ print_version ()
} }
int int
main2 (int argc, char **argv) main (int argc, char **argv)
{ {
int opt; int opt;
pid_t pid = 0; pid_t pid = 0;
@ -1095,17 +1089,3 @@ main2 (int argc, char **argv)
fclose (ofile); fclose (ofile);
return (ret); return (ret);
} }
int
main (int argc, char **argv)
{
/* Make sure to have room for the _cygtls area *and* to initialize it.
* This is required to make sure cygwin_internal calls into Cygwin work
* reliably. This problem has been noticed under AllocationPreference
* registry setting to 0x100000 (TOP_DOWN).
*/
char buf[CYGTLS_PADSIZE];
RtlSecureZeroMemory (buf, sizeof (buf));
exit (main2 (argc, argv));
}