2000-02-18 03:38:33 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include "sys/strace.h"
|
|
|
|
|
2000-03-19 11:53:18 +08:00
|
|
|
/* GCC runtime library's C++ EH code unfortunately pulls in stdio, and we
|
|
|
|
get undefine references to __impure_ptr, and hence the following
|
|
|
|
hack. It should be reasonably safe however as long as this file
|
|
|
|
is built using -mno-cygwin as is intended. */
|
|
|
|
int _impure_ptr;
|
|
|
|
|
2000-06-06 02:43:54 +08:00
|
|
|
/* we *know* we're being built with GCC */
|
|
|
|
#define alloca __builtin_alloca
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
static const char *pgm;
|
|
|
|
static int forkdebug = 0;
|
2000-02-28 13:08:05 +08:00
|
|
|
static int numerror = 1;
|
|
|
|
static int usecs = 1;
|
|
|
|
static int delta = 1;
|
|
|
|
static int hhmmss = 0;
|
2000-10-28 08:21:41 +08:00
|
|
|
static int bufsize = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
static BOOL close_handle (HANDLE h, DWORD ok);
|
|
|
|
|
|
|
|
#define CloseHandle(h) close_handle(h, 0)
|
|
|
|
|
|
|
|
struct child_list
|
2000-10-28 13:00:00 +08:00
|
|
|
{
|
|
|
|
DWORD id;
|
|
|
|
HANDLE hproc;
|
|
|
|
int saw_stars;
|
|
|
|
char nfields;
|
|
|
|
long long start_time;
|
|
|
|
DWORD last_usecs;
|
|
|
|
struct child_list *next;
|
|
|
|
child_list ():id (0), hproc (NULL), saw_stars (0), nfields (0),
|
|
|
|
start_time (0), last_usecs (0), next (NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
}
|
|
|
|
};
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-03-13 10:23:47 +08:00
|
|
|
child_list children;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
warn (int geterrno, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
sprintf (buf, "%s: ", pgm);
|
|
|
|
vsprintf (strchr (buf, '\0'), fmt, args);
|
|
|
|
if (geterrno)
|
|
|
|
perror (buf);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fputs (buf, stderr);
|
|
|
|
fputs ("\n", stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __attribute__ ((noreturn))
|
|
|
|
error (int geterrno, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
sprintf (buf, "%s: ", pgm);
|
|
|
|
vsprintf (strchr (buf, '\0'), fmt, args);
|
|
|
|
if (geterrno)
|
|
|
|
perror (buf);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fputs (buf, stderr);
|
|
|
|
fputs ("\n", stderr);
|
|
|
|
}
|
|
|
|
ExitProcess (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD lastid = 0;
|
|
|
|
HANDLE lasth;
|
|
|
|
|
|
|
|
#define PROCFLAGS \
|
2000-10-28 13:00:00 +08:00
|
|
|
PROCESS_ALL_ACCESS /*(PROCESS_DUP_HANDLE | PROCESS_TERMINATE | PROCESS_VM_READ | PROCESS_VM_WRITE) */
|
2000-02-18 03:38:33 +08:00
|
|
|
static void
|
|
|
|
add_child (DWORD id, HANDLE hproc)
|
|
|
|
{
|
|
|
|
child_list *c = children.next;
|
|
|
|
children.next = new (child_list);
|
|
|
|
children.next->next = c;
|
|
|
|
lastid = children.next->id = id;
|
|
|
|
HANDLE me = GetCurrentProcess ();
|
2000-10-09 11:31:10 +08:00
|
|
|
lasth = children.next->hproc = hproc;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2000-03-13 10:23:47 +08:00
|
|
|
static child_list *
|
|
|
|
get_child (DWORD id)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
child_list *c;
|
2000-10-28 13:00:00 +08:00
|
|
|
for (c = &children; (c = c->next) != NULL;)
|
2000-02-18 03:38:33 +08:00
|
|
|
if (c->id == id)
|
2000-03-13 10:23:47 +08:00
|
|
|
return c;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
error (0, "no process id %d found", id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_child (DWORD id)
|
|
|
|
{
|
|
|
|
child_list *c;
|
|
|
|
if (id == lastid)
|
|
|
|
lastid = 0;
|
|
|
|
for (c = &children; c->next != NULL; c = c->next)
|
|
|
|
if (c->next->id == id)
|
|
|
|
{
|
|
|
|
child_list *c1 = c->next;
|
|
|
|
c->next = c1->next;
|
|
|
|
delete c1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
error (0, "no process id %d found", id);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LINE_BUF_CHUNK 128
|
|
|
|
|
|
|
|
class linebuf
|
|
|
|
{
|
|
|
|
size_t alloc;
|
|
|
|
public:
|
2000-10-28 13:00:00 +08:00
|
|
|
size_t ix;
|
2000-02-18 03:38:33 +08:00
|
|
|
char *buf;
|
|
|
|
linebuf ()
|
|
|
|
{
|
|
|
|
ix = 0;
|
|
|
|
alloc = 0;
|
|
|
|
buf = NULL;
|
|
|
|
}
|
2000-10-28 13:00:00 +08:00
|
|
|
~linebuf ()
|
|
|
|
{
|
|
|
|
if (buf)
|
|
|
|
free (buf);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
void add (const char *what, int len);
|
2000-10-28 13:00:00 +08:00
|
|
|
void add (const char *what)
|
|
|
|
{
|
|
|
|
add (what, strlen (what));
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
void prepend (const char *what, int len);
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
linebuf::add (const char *what, int len)
|
|
|
|
{
|
|
|
|
size_t newix;
|
|
|
|
if ((newix = ix + len) >= alloc)
|
|
|
|
{
|
|
|
|
alloc += LINE_BUF_CHUNK + len;
|
|
|
|
buf = (char *) realloc (buf, alloc + 1);
|
|
|
|
}
|
|
|
|
memcpy (buf + ix, what, len);
|
|
|
|
ix = newix;
|
|
|
|
buf[ix] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
linebuf::prepend (const char *what, int len)
|
|
|
|
{
|
|
|
|
int buflen;
|
|
|
|
size_t newix;
|
|
|
|
if ((newix = ix + len) >= alloc)
|
|
|
|
{
|
|
|
|
alloc += LINE_BUF_CHUNK + len;
|
|
|
|
buf = (char *) realloc (buf, alloc + 1);
|
|
|
|
buf[ix] = '\0';
|
|
|
|
}
|
|
|
|
if ((buflen = strlen (buf)))
|
2000-10-28 13:00:00 +08:00
|
|
|
memmove (buf + len, buf, buflen + 1);
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
2000-10-28 13:00:00 +08:00
|
|
|
buf[newix] = '\0';
|
2000-02-18 03:38:33 +08:00
|
|
|
memcpy (buf, what, len);
|
|
|
|
ix = newix;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-10-28 13:00:00 +08:00
|
|
|
make_command_line (linebuf & one_line, char **argv)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
for (; *argv; argv++)
|
|
|
|
{
|
|
|
|
char *p = NULL;
|
|
|
|
const char *a = *argv;
|
|
|
|
|
|
|
|
int len = strlen (a);
|
|
|
|
if (len != 0 && !(p = strpbrk (a, " \t\n\r\"")))
|
|
|
|
one_line.add (a, len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
one_line.add ("\"", 1);
|
|
|
|
for (; p; a = p, p = strchr (p, '"'))
|
|
|
|
{
|
|
|
|
one_line.add (a, ++p - a);
|
|
|
|
if (p[-1] == '"')
|
|
|
|
one_line.add ("\"", 1);
|
|
|
|
}
|
|
|
|
if (*a)
|
|
|
|
one_line.add (a);
|
|
|
|
one_line.add ("\"", 1);
|
|
|
|
}
|
|
|
|
one_line.add (" ", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (one_line.ix)
|
|
|
|
one_line.buf[one_line.ix - 1] = '\0';
|
|
|
|
else
|
|
|
|
one_line.add ("", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD child_pid;
|
|
|
|
|
|
|
|
static BOOL WINAPI
|
2000-02-28 13:08:05 +08:00
|
|
|
ctrl_c (DWORD)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
static int tic = 1;
|
|
|
|
if ((tic ^= 1) && !GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0))
|
|
|
|
error (0, "couldn't send CTRL-C to child, win32 error %d\n",
|
|
|
|
GetLastError ());
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_child (char **argv)
|
|
|
|
{
|
|
|
|
linebuf one_line;
|
|
|
|
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
BOOL ret;
|
|
|
|
DWORD flags;
|
|
|
|
|
|
|
|
if (!*argv)
|
|
|
|
error (0, "no program argument specified");
|
|
|
|
|
|
|
|
memset (&si, 0, sizeof (si));
|
|
|
|
si.cb = sizeof (si);
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
/* cygwin32_conv_to_win32_path (exec_file, real_path); */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
flags = forkdebug ? 0 : DEBUG_ONLY_THIS_PROCESS;
|
2000-10-28 13:00:00 +08:00
|
|
|
flags |=
|
|
|
|
/*CREATE_NEW_PROCESS_GROUP | */ CREATE_DEFAULT_ERROR_MODE | DEBUG_PROCESS;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
make_command_line (one_line, argv);
|
|
|
|
|
|
|
|
SetConsoleCtrlHandler (NULL, 0);
|
2000-10-28 13:00:00 +08:00
|
|
|
ret = CreateProcess (0, one_line.buf, /* command line */
|
2000-02-18 03:38:33 +08:00
|
|
|
NULL, /* Security */
|
|
|
|
NULL, /* thread */
|
|
|
|
TRUE, /* inherit handles */
|
|
|
|
flags, /* start flags */
|
2000-10-28 13:00:00 +08:00
|
|
|
NULL, NULL, /* current directory */
|
|
|
|
&si, &pi);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!ret)
|
2000-10-28 13:00:00 +08:00
|
|
|
error (0, "error creating process %s, (error %d)", *argv,
|
|
|
|
GetLastError ());
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
CloseHandle (pi.hThread);
|
|
|
|
CloseHandle (pi.hProcess);
|
|
|
|
child_pid = pi.dwProcessId;
|
|
|
|
SetConsoleCtrlHandler (ctrl_c, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
output_winerror (FILE *ofile, char *s)
|
|
|
|
{
|
|
|
|
char *winerr = strstr (s, "Win32 error ");
|
|
|
|
if (!winerr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
DWORD errnum = atoi (winerr + sizeof ("Win32 error ") - 1);
|
|
|
|
if (!errnum)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: Currently there is no policy for how long the
|
|
|
|
* the buffers are, and looks like 256 is a smallest one
|
|
|
|
* (dlfcn.cc). Other than error 1395 (length 213) and
|
|
|
|
* error 1015 (length 249), the rest are all under 188
|
|
|
|
* characters, and so I'll use 189 as the buffer length.
|
|
|
|
* For those longer error messages, FormatMessage will
|
|
|
|
* return FALSE, and we'll get the old behaviour such as
|
|
|
|
* ``Win32 error 1395'' etc.
|
|
|
|
*/
|
|
|
|
char buf[4096];
|
|
|
|
if (!FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
|
|
|
|
| FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
errnum,
|
|
|
|
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
|
2000-10-28 13:00:00 +08:00
|
|
|
(LPTSTR) buf, sizeof (buf), NULL))
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Get rid the trailing CR/NL pair. */
|
|
|
|
char *p = strchr (buf, '\0');
|
|
|
|
p[-2] = '\n';
|
|
|
|
p[-1] = '\0';
|
|
|
|
|
|
|
|
*winerr = '\0';
|
|
|
|
fputs (s, ofile);
|
|
|
|
fputs (buf, ofile);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-02-28 13:08:05 +08:00
|
|
|
static SYSTEMTIME *
|
|
|
|
syst (long long t)
|
|
|
|
{
|
|
|
|
FILETIME n;
|
|
|
|
static SYSTEMTIME st;
|
|
|
|
long long now = t + ((long long) usecs * 10);
|
|
|
|
n.dwHighDateTime = now >> 32;
|
|
|
|
n.dwLowDateTime = now & 0xffffffff;
|
|
|
|
FileTimeToSystemTime (&n, &st);
|
|
|
|
return &st;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
static void __stdcall
|
|
|
|
handle_output_debug_string (DWORD id, LPVOID p, unsigned mask, FILE *ofile)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
int special;
|
|
|
|
char alen[3 + 8 + 1];
|
|
|
|
DWORD nbytes;
|
2000-03-13 10:23:47 +08:00
|
|
|
child_list *child = get_child (id);
|
|
|
|
HANDLE hchild = child->hproc;
|
2000-10-28 13:00:00 +08:00
|
|
|
#define INTROLEN (sizeof (alen) - 1)
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (id == lastid && hchild != lasth)
|
|
|
|
warn (0, "%p != %p", hchild, lasth);
|
|
|
|
|
|
|
|
alen[INTROLEN] = '\0';
|
|
|
|
if (!ReadProcessMemory (hchild, p, alen, INTROLEN, &nbytes))
|
|
|
|
#ifndef DEBUGGING
|
|
|
|
return;
|
|
|
|
#else
|
2000-10-28 13:00:00 +08:00
|
|
|
error (0,
|
|
|
|
"couldn't get message length from subprocess %d<%p>, windows error %d",
|
2000-02-18 03:38:33 +08:00
|
|
|
id, hchild, GetLastError ());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (strncmp (alen, "cYg", 3))
|
|
|
|
return;
|
|
|
|
len = (int) strtoul (alen + 3, NULL, 16);
|
|
|
|
if (!len)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
special = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
special = len;
|
|
|
|
if (special == _STRACE_INTERFACE_ACTIVATE_ADDR)
|
|
|
|
len = 17;
|
|
|
|
}
|
2000-02-28 13:08:05 +08:00
|
|
|
|
|
|
|
char *buf;
|
|
|
|
buf = (char *) alloca (len + 65) + 10;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (!ReadProcessMemory (hchild, ((char *) p) + INTROLEN, buf, len, &nbytes))
|
|
|
|
error (0, "couldn't get message from subprocess, windows error %d",
|
|
|
|
GetLastError ());
|
|
|
|
|
|
|
|
buf[len] = '\0';
|
|
|
|
char *s = strtok (buf, " ");
|
|
|
|
|
|
|
|
unsigned n = strtoul (s, NULL, 16);
|
|
|
|
|
|
|
|
s = strchr (s, '\0') + 1;
|
|
|
|
|
|
|
|
if (special == _STRACE_INTERFACE_ACTIVATE_ADDR)
|
|
|
|
{
|
|
|
|
DWORD new_flag = 1;
|
|
|
|
if (!WriteProcessMemory (hchild, (LPVOID) n, &new_flag,
|
2000-10-28 13:00:00 +08:00
|
|
|
sizeof (new_flag), &nbytes))
|
|
|
|
error (0,
|
|
|
|
"couldn't write strace flag to subprocess, windows error %d",
|
2000-02-18 03:38:33 +08:00
|
|
|
GetLastError ());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-02-28 13:08:05 +08:00
|
|
|
char *origs = s;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (mask & n)
|
2000-10-28 13:00:00 +08:00
|
|
|
/* got it */ ;
|
2000-02-18 03:38:33 +08:00
|
|
|
else if (!(mask & _STRACE_ALL) || (n & _STRACE_NOTALL))
|
2000-10-28 13:00:00 +08:00
|
|
|
return; /* This should not be included in "all" output */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-02-28 13:08:05 +08:00
|
|
|
DWORD dusecs, usecs;
|
|
|
|
char *ptusec, *ptrest;
|
|
|
|
|
|
|
|
dusecs = strtoul (s, &ptusec, 10);
|
|
|
|
char *q = ptusec;
|
|
|
|
while (*q == ' ')
|
|
|
|
q++;
|
|
|
|
if (*q != '[')
|
|
|
|
{
|
|
|
|
usecs = strtoul (q, &ptrest, 10);
|
|
|
|
while (*ptrest == ' ')
|
|
|
|
ptrest++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptrest = q;
|
|
|
|
ptusec = s;
|
|
|
|
usecs = dusecs;
|
|
|
|
}
|
|
|
|
|
2000-03-13 10:23:47 +08:00
|
|
|
if (child->saw_stars == 0)
|
2000-02-28 13:08:05 +08:00
|
|
|
{
|
|
|
|
FILETIME st;
|
|
|
|
char *news;
|
|
|
|
|
|
|
|
GetSystemTimeAsFileTime (&st);
|
|
|
|
FileTimeToLocalFileTime (&st, &st);
|
2000-03-13 10:23:47 +08:00
|
|
|
child->start_time = st.dwHighDateTime;
|
|
|
|
child->start_time <<= 32;
|
|
|
|
child->start_time |= st.dwLowDateTime;
|
2000-02-28 13:08:05 +08:00
|
|
|
if (*(news = ptrest) != '[')
|
2000-03-13 10:23:47 +08:00
|
|
|
child->saw_stars = 2;
|
2000-02-28 13:08:05 +08:00
|
|
|
else
|
|
|
|
{
|
2000-03-13 10:23:47 +08:00
|
|
|
child->saw_stars++;
|
2000-02-28 13:08:05 +08:00
|
|
|
while ((news = strchr (news, ' ')) != NULL && *++news != '*')
|
2000-03-13 10:23:47 +08:00
|
|
|
child->nfields++;
|
2000-02-28 13:08:05 +08:00
|
|
|
if (news == NULL)
|
2000-03-13 10:23:47 +08:00
|
|
|
child->saw_stars++;
|
2000-02-28 13:08:05 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
s = news;
|
2000-03-13 10:23:47 +08:00
|
|
|
child->nfields++;
|
2000-02-28 13:08:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-03-13 10:23:47 +08:00
|
|
|
else if (child->saw_stars < 2)
|
2000-02-28 13:08:05 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *news;
|
|
|
|
if (*(news = ptrest) != '[')
|
2000-03-13 10:23:47 +08:00
|
|
|
child->saw_stars = 2;
|
2000-02-28 13:08:05 +08:00
|
|
|
else
|
|
|
|
{
|
2000-03-13 10:23:47 +08:00
|
|
|
for (i = 0; i < child->nfields; i++)
|
2000-02-28 13:08:05 +08:00
|
|
|
if ((news = strchr (news, ' ')) == NULL)
|
2000-10-28 13:00:00 +08:00
|
|
|
break; // Should never happen
|
2000-02-28 13:08:05 +08:00
|
|
|
else
|
|
|
|
news++;
|
|
|
|
|
|
|
|
if (news == NULL)
|
2000-03-13 10:23:47 +08:00
|
|
|
child->saw_stars = 2;
|
2000-02-28 13:08:05 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
s = news;
|
|
|
|
if (*s == '*')
|
|
|
|
{
|
2000-03-13 10:23:47 +08:00
|
|
|
SYSTEMTIME *st = syst (child->start_time);
|
2000-10-28 13:00:00 +08:00
|
|
|
fprintf (ofile,
|
|
|
|
"Date/Time: %d-%02d-%02d %02d:%02d:%02d\n",
|
|
|
|
st->wYear, st->wMonth, st->wDay, st->wHour,
|
|
|
|
st->wMinute, st->wSecond);
|
2000-03-13 10:23:47 +08:00
|
|
|
child->saw_stars++;
|
2000-02-28 13:08:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-13 10:23:47 +08:00
|
|
|
long long d = usecs - child->last_usecs;
|
2000-02-28 13:08:05 +08:00
|
|
|
char intbuf[40];
|
|
|
|
|
2000-03-13 10:23:47 +08:00
|
|
|
if (child->saw_stars < 2 || s != origs)
|
2000-10-28 13:00:00 +08:00
|
|
|
/* Nothing */ ;
|
2000-02-28 13:08:05 +08:00
|
|
|
else if (hhmmss)
|
|
|
|
{
|
|
|
|
s = ptrest - 9;
|
2000-03-13 10:23:47 +08:00
|
|
|
SYSTEMTIME *st = syst (child->start_time + (long long) usecs * 10);
|
2000-02-28 13:08:05 +08:00
|
|
|
sprintf (s, "%02d:%02d:%02d", st->wHour, st->wMinute, st->wSecond);
|
|
|
|
*strchr (s, '\0') = ' ';
|
|
|
|
}
|
|
|
|
else if (!delta)
|
|
|
|
s = ptusec;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s = ptusec;
|
|
|
|
sprintf (intbuf, "%5d ", (int) d);
|
|
|
|
int len = strlen (intbuf);
|
|
|
|
|
|
|
|
memcpy ((s -= len), intbuf, len);
|
|
|
|
}
|
|
|
|
|
2000-03-13 10:23:47 +08:00
|
|
|
child->last_usecs = usecs;
|
2000-02-28 13:08:05 +08:00
|
|
|
if (numerror || !output_winerror (ofile, s))
|
2000-02-18 03:38:33 +08:00
|
|
|
fputs (s, ofile);
|
2000-10-28 08:21:41 +08:00
|
|
|
if (!bufsize)
|
|
|
|
fflush (ofile);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
proc_child (unsigned mask, FILE *ofile)
|
|
|
|
{
|
|
|
|
DEBUG_EVENT ev;
|
|
|
|
int processes = 0;
|
|
|
|
SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST);
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
BOOL debug_event = WaitForDebugEvent (&ev, 1000);
|
2000-10-12 14:22:30 +08:00
|
|
|
DWORD status = DBG_CONTINUE;
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!debug_event)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (ev.dwDebugEventCode)
|
|
|
|
{
|
|
|
|
case CREATE_PROCESS_DEBUG_EVENT:
|
|
|
|
if (ev.u.CreateProcessInfo.hFile)
|
|
|
|
CloseHandle (ev.u.CreateProcessInfo.hFile);
|
|
|
|
add_child (ev.dwProcessId, ev.u.CreateProcessInfo.hProcess);
|
|
|
|
processes++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CREATE_THREAD_DEBUG_EVENT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOAD_DLL_DEBUG_EVENT:
|
|
|
|
if (ev.u.LoadDll.hFile)
|
|
|
|
CloseHandle (ev.u.LoadDll.hFile);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OUTPUT_DEBUG_STRING_EVENT:
|
|
|
|
handle_output_debug_string (ev.dwProcessId,
|
|
|
|
ev.u.DebugString.lpDebugStringData,
|
|
|
|
mask, ofile);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EXIT_PROCESS_DEBUG_EVENT:
|
|
|
|
remove_child (ev.dwProcessId);
|
|
|
|
break;
|
2000-10-12 14:22:30 +08:00
|
|
|
case EXCEPTION_DEBUG_EVENT:
|
2000-10-28 13:00:00 +08:00
|
|
|
if (ev.u.Exception.ExceptionRecord.ExceptionCode !=
|
|
|
|
STATUS_BREAKPOINT)
|
2000-10-15 11:43:48 +08:00
|
|
|
{
|
|
|
|
status = DBG_EXCEPTION_NOT_HANDLED;
|
|
|
|
#if 0
|
|
|
|
fprintf (stderr, "exception %p at %p\n",
|
|
|
|
ev.u.Exception.ExceptionRecord.ExceptionCode,
|
|
|
|
ev.u.Exception.ExceptionRecord.ExceptionAddress);
|
|
|
|
#endif
|
|
|
|
}
|
2000-10-12 14:22:30 +08:00
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-10-12 14:22:30 +08:00
|
|
|
if (!ContinueDebugEvent (ev.dwProcessId, ev.dwThreadId, status))
|
2000-02-18 03:38:33 +08:00
|
|
|
error (0, "couldn't continue debug event, windows error %d",
|
|
|
|
GetLastError ());
|
|
|
|
if (ev.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT && --processes == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dostrace (unsigned mask, FILE *ofile, char **argv)
|
|
|
|
{
|
|
|
|
create_child (argv);
|
|
|
|
proc_child (mask, ofile);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-10-28 13:00:00 +08:00
|
|
|
main (int argc, char **argv)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
unsigned mask = 0;
|
|
|
|
FILE *ofile = NULL;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
if (!(pgm = strrchr (*argv, '\\')) && !(pgm = strrchr (*argv, '/')))
|
|
|
|
pgm = *argv;
|
|
|
|
else
|
|
|
|
pgm++;
|
|
|
|
|
2000-10-28 08:21:41 +08:00
|
|
|
while ((opt = getopt (argc, argv, "b:m:o:fndut")) != EOF)
|
2000-02-18 03:38:33 +08:00
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 'f':
|
|
|
|
forkdebug ^= 1;
|
|
|
|
break;
|
2000-10-28 08:21:41 +08:00
|
|
|
case 'b':
|
|
|
|
bufsize = atoi (optarg);
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
case 'm':
|
|
|
|
mask = strtoul (optarg, NULL, 16);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
if ((ofile = fopen (optarg, "w")) == NULL)
|
|
|
|
error (1, "can't open %s", optarg);
|
|
|
|
#ifdef F_SETFD
|
|
|
|
(void) fcntl (fileno (ofile), F_SETFD, 0);
|
|
|
|
#endif
|
|
|
|
break;
|
2000-02-28 13:08:05 +08:00
|
|
|
case 'n':
|
|
|
|
numerror ^= 1;
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
case 't':
|
2000-02-28 13:08:05 +08:00
|
|
|
hhmmss ^= 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
delta ^= 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
2000-02-28 13:08:05 +08:00
|
|
|
case 'u':
|
|
|
|
usecs ^= 1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mask)
|
|
|
|
mask = 1;
|
|
|
|
|
2000-10-28 08:21:41 +08:00
|
|
|
if (bufsize)
|
|
|
|
setvbuf (ofile, (char *) alloca (bufsize), _IOFBF, bufsize);
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!ofile)
|
|
|
|
ofile = stdout;
|
|
|
|
|
|
|
|
dostrace (mask, ofile, argv + optind);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef CloseHandle
|
|
|
|
|
|
|
|
static BOOL
|
|
|
|
close_handle (HANDLE h, DWORD ok)
|
|
|
|
{
|
|
|
|
child_list *c;
|
2000-10-28 13:00:00 +08:00
|
|
|
for (c = &children; (c = c->next) != NULL;)
|
2000-02-18 03:38:33 +08:00
|
|
|
if (c->hproc == h && c->id != ok)
|
|
|
|
error (0, "Closing child handle %p", h);
|
|
|
|
return CloseHandle (h);
|
|
|
|
}
|