2001-03-06 01:55:23 +08:00
|
|
|
/* strace.cc
|
|
|
|
|
|
|
|
Written by Chris Faylor <cgf@redhat.com>
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2012-07-12 00:41:51 +08:00
|
|
|
#include <windows.h>
|
2012-11-26 21:13:25 +08:00
|
|
|
#include <winternl.h>
|
2002-01-29 10:03:32 +08:00
|
|
|
#define cygwin_internal cygwin_internal_dontuse
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
2017-02-25 03:55:14 +08:00
|
|
|
#include <io.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2001-11-04 20:57:55 +08:00
|
|
|
#include <time.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <signal.h>
|
2002-01-29 10:03:32 +08:00
|
|
|
#include <errno.h>
|
2012-10-24 18:15:27 +08:00
|
|
|
#include "../cygwin/include/sys/strace.h"
|
|
|
|
#include "../cygwin/include/sys/cygwin.h"
|
|
|
|
#include "../cygwin/include/cygwin/version.h"
|
2015-07-29 19:32:29 +08:00
|
|
|
#include "../cygwin/cygtls_padsize.h"
|
2003-03-04 13:30:50 +08:00
|
|
|
#include "path.h"
|
2002-01-29 10:03:32 +08:00
|
|
|
#undef cygwin_internal
|
2010-08-28 19:22:37 +08:00
|
|
|
#include "loadlib.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-06-06 02:43:54 +08:00
|
|
|
/* we *know* we're being built with GCC */
|
2012-07-12 00:41:51 +08:00
|
|
|
#ifndef alloca
|
2000-06-06 02:43:54 +08:00
|
|
|
#define alloca __builtin_alloca
|
2012-07-12 00:41:51 +08:00
|
|
|
#endif
|
2000-06-06 02:43:54 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
static const char *pgm;
|
2002-06-03 01:46:38 +08:00
|
|
|
static int forkdebug = 1;
|
2000-02-28 13:08:05 +08:00
|
|
|
static int numerror = 1;
|
2005-12-06 00:39:06 +08:00
|
|
|
static int show_usecs = 1;
|
2000-02-28 13:08:05 +08:00
|
|
|
static int delta = 1;
|
2015-05-16 22:19:59 +08:00
|
|
|
static int events = 1;
|
2005-12-30 04:46:34 +08:00
|
|
|
static int hhmmss;
|
|
|
|
static int bufsize;
|
|
|
|
static int new_window;
|
|
|
|
static long flush_period;
|
|
|
|
static int include_hex;
|
|
|
|
static int quiet = -1;
|
|
|
|
|
|
|
|
static unsigned char strace_active = 1;
|
|
|
|
static int processes;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
static BOOL close_handle (HANDLE h, DWORD ok);
|
|
|
|
|
|
|
|
#define CloseHandle(h) close_handle(h, 0)
|
|
|
|
|
2015-05-16 22:19:59 +08:00
|
|
|
static void *drive_map;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
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);
|
|
|
|
}
|
2016-10-23 22:38:48 +08:00
|
|
|
va_end (args);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2002-06-07 09:36:17 +08:00
|
|
|
exit (1);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD lastid = 0;
|
|
|
|
HANDLE lasth;
|
|
|
|
|
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
|
|
|
|
2005-12-30 04:46:34 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_child (DWORD id, HANDLE hproc)
|
|
|
|
{
|
|
|
|
if (!get_child (id))
|
|
|
|
{
|
|
|
|
child_list *c = children.next;
|
|
|
|
children.next = (child_list *) calloc (1, sizeof (child_list));
|
|
|
|
children.next->next = c;
|
|
|
|
lastid = children.next->id = id;
|
|
|
|
lasth = children.next->hproc = hproc;
|
|
|
|
processes++;
|
|
|
|
if (!quiet)
|
2008-09-13 06:43:10 +08:00
|
|
|
fprintf (stderr, "Windows process %lu attached\n", id);
|
2005-12-30 04:46:34 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-10-12 02:39:02 +08:00
|
|
|
remove_child (DWORD id)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
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;
|
2002-05-19 05:05:10 +08:00
|
|
|
free (c1);
|
2005-12-30 04:46:34 +08:00
|
|
|
if (!quiet)
|
2008-09-13 06:43:10 +08:00
|
|
|
fprintf (stderr, "Windows process %lu detached\n", id);
|
2005-12-30 04:46:34 +08:00
|
|
|
processes--;
|
2000-02-18 03:38:33 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-05-17 09:26:16 +08:00
|
|
|
extern "C" {
|
2013-04-23 17:44:36 +08:00
|
|
|
uintptr_t (*cygwin_internal) (int, ...);
|
2009-11-05 01:00:45 +08:00
|
|
|
WCHAR cygwin_dll_path[32768];
|
2005-05-17 09:26:16 +08:00
|
|
|
};
|
2002-01-29 10:03:32 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
load_cygwin ()
|
|
|
|
{
|
|
|
|
static HMODULE h;
|
|
|
|
|
|
|
|
if (cygwin_internal)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (h)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!(h = LoadLibrary ("cygwin1.dll")))
|
|
|
|
{
|
|
|
|
errno = ENOENT;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-05 01:00:45 +08:00
|
|
|
GetModuleFileNameW (h, cygwin_dll_path, 32768);
|
2013-04-23 17:44:36 +08:00
|
|
|
if (!(cygwin_internal = (uintptr_t (*) (int, ...)) GetProcAddress (h, "cygwin_internal")))
|
2002-01-29 10:03:32 +08:00
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-16 00:49:51 +08:00
|
|
|
#define DEBUG_PROCESS_DETACH_ON_EXIT 0x00000001
|
|
|
|
#define DEBUG_PROCESS_ONLY_THIS_PROCESS 0x00000002
|
|
|
|
|
2002-01-29 10:03:32 +08:00
|
|
|
static void
|
|
|
|
attach_process (pid_t pid)
|
|
|
|
{
|
|
|
|
child_pid = (DWORD) cygwin_internal (CW_CYGWIN_PID_TO_WINPID, pid);
|
|
|
|
if (!child_pid)
|
2005-12-30 04:46:34 +08:00
|
|
|
child_pid = pid;
|
2002-01-29 10:03:32 +08:00
|
|
|
|
|
|
|
if (!DebugActiveProcess (child_pid))
|
2005-12-30 04:46:34 +08:00
|
|
|
error (0, "couldn't attach to pid %d for debugging", child_pid);
|
2002-01-29 10:03:32 +08:00
|
|
|
|
2011-09-16 00:49:51 +08:00
|
|
|
if (forkdebug)
|
|
|
|
{
|
|
|
|
HANDLE h = OpenProcess(PROCESS_ALL_ACCESS, FALSE, child_pid);
|
|
|
|
|
|
|
|
if (h)
|
2011-12-18 07:39:47 +08:00
|
|
|
{
|
|
|
|
/* Try to turn off DEBUG_ONLY_THIS_PROCESS so we can follow forks */
|
|
|
|
ULONG DebugFlags = DEBUG_PROCESS_DETACH_ON_EXIT;
|
|
|
|
NTSTATUS status = NtSetInformationProcess (h, ProcessDebugFlags, &DebugFlags, sizeof (DebugFlags));
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
warn (0, "Could not clear DEBUG_ONLY_THIS_PROCESS (%x), will not trace child processes", status);
|
|
|
|
|
|
|
|
CloseHandle(h);
|
|
|
|
}
|
2011-09-16 00:49:51 +08:00
|
|
|
}
|
|
|
|
|
2002-01-29 10:03:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
static void
|
|
|
|
create_child (char **argv)
|
|
|
|
{
|
|
|
|
linebuf one_line;
|
|
|
|
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
BOOL ret;
|
|
|
|
DWORD flags;
|
|
|
|
|
2007-07-24 05:03:59 +08:00
|
|
|
if (strchr (*argv, '/'))
|
|
|
|
*argv = cygpath (*argv, NULL);
|
2000-02-18 03:38:33 +08:00
|
|
|
memset (&si, 0, sizeof (si));
|
|
|
|
si.cb = sizeof (si);
|
|
|
|
|
2002-06-03 01:46:38 +08:00
|
|
|
flags = CREATE_DEFAULT_ERROR_MODE
|
2002-06-07 09:36:17 +08:00
|
|
|
| (forkdebug ? DEBUG_PROCESS : DEBUG_ONLY_THIS_PROCESS);
|
2002-06-03 01:46:38 +08:00
|
|
|
if (new_window)
|
|
|
|
flags |= CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
make_command_line (one_line, argv);
|
|
|
|
|
|
|
|
SetConsoleCtrlHandler (NULL, 0);
|
2016-10-23 22:38:48 +08:00
|
|
|
|
2009-04-23 03:08:53 +08:00
|
|
|
const char *cygwin_env = getenv ("CYGWIN");
|
|
|
|
const char *space;
|
2016-10-23 22:38:48 +08:00
|
|
|
|
|
|
|
if (cygwin_env && strlen (cygwin_env) <= 256) /* sanity check */
|
2009-04-23 03:08:53 +08:00
|
|
|
space = " ";
|
|
|
|
else
|
|
|
|
space = cygwin_env = "";
|
2016-10-23 22:38:48 +08:00
|
|
|
char *newenv = (char *) malloc (sizeof ("CYGWIN=noglob")
|
|
|
|
+ strlen (space) + strlen (cygwin_env));
|
2009-04-23 03:08:53 +08:00
|
|
|
sprintf (newenv, "CYGWIN=noglob%s%s", space, cygwin_env);
|
|
|
|
_putenv (newenv);
|
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 */
|
2009-04-23 03:08:53 +08:00
|
|
|
NULL, /* default environment */
|
|
|
|
NULL, /* current directory */
|
2000-10-28 13:00:00 +08:00
|
|
|
&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;
|
2005-12-06 00:39:06 +08:00
|
|
|
long long now = t /*+ ((long long) usecs * 10)*/;
|
2000-02-28 13:08:05 +08:00
|
|
|
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];
|
2013-04-23 17:44:36 +08:00
|
|
|
SIZE_T nbytes;
|
2000-03-13 10:23:47 +08:00
|
|
|
child_list *child = get_child (id);
|
2005-12-30 04:46:34 +08:00
|
|
|
if (!child)
|
|
|
|
error (0, "no process id %d found", id);
|
2000-03-13 10:23:47 +08:00
|
|
|
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;
|
2005-12-30 04:46:34 +08:00
|
|
|
if (special == _STRACE_INTERFACE_ACTIVATE_ADDR || special == _STRACE_CHILD_PID)
|
2000-02-18 03:38:33 +08:00
|
|
|
len = 17;
|
|
|
|
}
|
2000-02-28 13:08:05 +08:00
|
|
|
|
2016-10-23 04:39:31 +08:00
|
|
|
/* Note that the following code deliberately points buf 20 bytes into the
|
|
|
|
allocated area. The subsequent code then overwrites the usecs value
|
|
|
|
given in the application's debug string, which potentially prepends
|
|
|
|
characters to the string. If that sounds confusing and dangerous, well...
|
|
|
|
|
|
|
|
TODO: This needs a cleanup. */
|
2000-02-28 13:08:05 +08:00
|
|
|
char *buf;
|
2005-01-17 01:13:51 +08:00
|
|
|
buf = (char *) alloca (len + 85) + 20;
|
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, " ");
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
#ifdef __x86_64__
|
|
|
|
unsigned long long n = strtoull (s, NULL, 16);
|
|
|
|
#else
|
2005-03-07 18:24:23 +08:00
|
|
|
unsigned long n = strtoul (s, NULL, 16);
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
s = strchr (s, '\0') + 1;
|
|
|
|
|
2005-12-30 04:46:34 +08:00
|
|
|
if (special == _STRACE_CHILD_PID)
|
|
|
|
{
|
2011-11-25 05:54:41 +08:00
|
|
|
DebugActiveProcess (n);
|
2005-12-30 04:46:34 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (special == _STRACE_INTERFACE_ACTIVATE_ADDR)
|
|
|
|
{
|
2011-11-25 05:54:41 +08:00
|
|
|
s = strtok (NULL, " ");
|
|
|
|
if (*s && *s == '1' && !forkdebug)
|
|
|
|
/* don't activate since we are not following forks */;
|
|
|
|
else if (!WriteProcessMemory (hchild, (LPVOID) n, &strace_active,
|
2005-12-30 04:46:34 +08:00
|
|
|
sizeof (strace_active), &nbytes))
|
|
|
|
error (0, "couldn't write strace flag to subprocess at %p, "
|
|
|
|
"windows error %d", n, GetLastError ());
|
2000-02-18 03:38:33 +08:00
|
|
|
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;
|
2005-12-06 00:39:06 +08:00
|
|
|
ptusec = show_usecs ? s : ptrest;
|
2000-02-28 13:08:05 +08:00
|
|
|
usecs = dusecs;
|
|
|
|
}
|
|
|
|
|
2011-11-15 01:31:20 +08:00
|
|
|
#if 1
|
|
|
|
child->saw_stars = 2;
|
|
|
|
#else
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-15 01:31:20 +08:00
|
|
|
#endif
|
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);
|
|
|
|
}
|
|
|
|
|
2005-01-17 01:13:51 +08:00
|
|
|
if (include_hex)
|
|
|
|
{
|
|
|
|
s -= 8;
|
2013-04-23 17:44:36 +08:00
|
|
|
#ifdef __x86_64__
|
|
|
|
sprintf (s, "%012I64x", n);
|
|
|
|
#else
|
|
|
|
sprintf (s, "%08lx", n);
|
|
|
|
#endif
|
2005-01-17 01:13:51 +08:00
|
|
|
strchr (s, '\0')[0] = ' ';
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-05-16 22:19:59 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-05-25 00:50:50 +08:00
|
|
|
static DWORD
|
2002-05-27 09:49:08 +08:00
|
|
|
proc_child (unsigned mask, FILE *ofile, pid_t pid)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2006-05-25 00:50:50 +08:00
|
|
|
DWORD res = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
DEBUG_EVENT ev;
|
2001-11-04 20:57:55 +08:00
|
|
|
time_t cur_time, last_time;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST);
|
2001-11-04 20:57:55 +08:00
|
|
|
last_time = time (NULL);
|
2000-02-18 03:38:33 +08:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
BOOL debug_event = WaitForDebugEvent (&ev, 1000);
|
2000-10-12 14:22:30 +08:00
|
|
|
DWORD status = DBG_CONTINUE;
|
2001-11-04 20:57:55 +08:00
|
|
|
|
|
|
|
if (bufsize && flush_period > 0 &&
|
|
|
|
(cur_time = time (NULL)) >= last_time + flush_period)
|
|
|
|
{
|
|
|
|
last_time = cur_time;
|
|
|
|
fflush (ofile);
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!debug_event)
|
|
|
|
continue;
|
|
|
|
|
2002-05-27 09:49:08 +08:00
|
|
|
if (pid)
|
|
|
|
{
|
|
|
|
(void) cygwin_internal (CW_STRACE_TOGGLE, pid);
|
|
|
|
pid = 0;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
switch (ev.dwDebugEventCode)
|
|
|
|
{
|
|
|
|
case CREATE_PROCESS_DEBUG_EVENT:
|
2015-05-16 22:19:59 +08:00
|
|
|
if (events)
|
|
|
|
fprintf (ofile, "--- Process %lu created\n", ev.dwProcessId);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (ev.u.CreateProcessInfo.hFile)
|
|
|
|
CloseHandle (ev.u.CreateProcessInfo.hFile);
|
|
|
|
add_child (ev.dwProcessId, ev.u.CreateProcessInfo.hProcess);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CREATE_THREAD_DEBUG_EVENT:
|
2015-05-16 22:19:59 +08:00
|
|
|
if (events)
|
|
|
|
fprintf (ofile, "--- Process %lu thread %lu created\n",
|
|
|
|
ev.dwProcessId, ev.dwThreadId);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LOAD_DLL_DEBUG_EVENT:
|
2015-05-16 22:19:59 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (ev.u.LoadDll.hFile)
|
|
|
|
CloseHandle (ev.u.LoadDll.hFile);
|
|
|
|
break;
|
|
|
|
|
2015-05-16 22:19:59 +08:00
|
|
|
case UNLOAD_DLL_DEBUG_EVENT:
|
|
|
|
if (events)
|
|
|
|
fprintf (ofile, "--- Process %lu unloaded DLL at %p\n",
|
|
|
|
ev.dwProcessId, ev.u.UnloadDll.lpBaseOfDll);
|
|
|
|
break;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
case OUTPUT_DEBUG_STRING_EVENT:
|
|
|
|
handle_output_debug_string (ev.dwProcessId,
|
|
|
|
ev.u.DebugString.lpDebugStringData,
|
|
|
|
mask, ofile);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EXIT_PROCESS_DEBUG_EVENT:
|
2015-05-16 22:19:59 +08:00
|
|
|
if (events)
|
|
|
|
fprintf (ofile, "--- Process %lu exited with status 0x%lx\n",
|
|
|
|
ev.dwProcessId, ev.u.ExitProcess.dwExitCode);
|
2012-02-01 02:06:51 +08:00
|
|
|
res = ev.u.ExitProcess.dwExitCode;
|
2005-10-12 02:39:02 +08:00
|
|
|
remove_child (ev.dwProcessId);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
2015-05-16 22:19:59 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2000-10-12 14:22:30 +08:00
|
|
|
case EXCEPTION_DEBUG_EVENT:
|
2016-08-31 18:15:29 +08:00
|
|
|
switch (ev.u.Exception.ExceptionRecord.ExceptionCode)
|
2000-10-15 11:43:48 +08:00
|
|
|
{
|
2016-08-31 18:15:29 +08:00
|
|
|
case STATUS_BREAKPOINT:
|
|
|
|
case 0x406d1388: /* SetThreadName exception. */
|
|
|
|
break;
|
|
|
|
default:
|
2000-10-15 11:43:48 +08:00
|
|
|
status = DBG_EXCEPTION_NOT_HANDLED;
|
2005-12-30 04:46:34 +08:00
|
|
|
if (ev.u.Exception.dwFirstChance)
|
2013-04-23 17:44:36 +08:00
|
|
|
fprintf (ofile, "--- Process %lu, exception %08lx at %p\n",
|
|
|
|
ev.dwProcessId,
|
|
|
|
ev.u.Exception.ExceptionRecord.ExceptionCode,
|
2005-12-30 04:46:34 +08:00
|
|
|
ev.u.Exception.ExceptionRecord.ExceptionAddress);
|
2016-08-31 18:15:29 +08:00
|
|
|
break;
|
2000-10-15 11:43:48 +08:00
|
|
|
}
|
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 ());
|
2005-12-30 04:46:34 +08:00
|
|
|
if (!processes)
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
}
|
2006-05-25 00:50:50 +08:00
|
|
|
|
|
|
|
return res;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-06-07 09:36:17 +08:00
|
|
|
static void
|
|
|
|
dotoggle (pid_t pid)
|
|
|
|
{
|
|
|
|
child_pid = (DWORD) cygwin_internal (CW_CYGWIN_PID_TO_WINPID, pid);
|
|
|
|
if (!child_pid)
|
|
|
|
{
|
|
|
|
warn (0, "no such cygwin pid - %d", pid);
|
|
|
|
child_pid = pid;
|
|
|
|
}
|
|
|
|
if (cygwin_internal (CW_STRACE_TOGGLE, child_pid))
|
|
|
|
error (0, "failed to toggle tracing for process %d<%d>", pid, child_pid);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-05-25 00:50:50 +08:00
|
|
|
static DWORD
|
2002-01-29 10:03:32 +08:00
|
|
|
dostrace (unsigned mask, FILE *ofile, pid_t pid, char **argv)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-01-29 10:03:32 +08:00
|
|
|
if (!pid)
|
|
|
|
create_child (argv);
|
|
|
|
else
|
|
|
|
attach_process (pid);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-05-25 00:50:50 +08:00
|
|
|
return proc_child (mask, ofile, pid);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-11-17 17:48:09 +08:00
|
|
|
typedef struct tag_mask_mnemonic
|
|
|
|
{
|
|
|
|
unsigned long val;
|
|
|
|
const char *text;
|
|
|
|
}
|
|
|
|
mask_mnemonic;
|
|
|
|
|
|
|
|
static const mask_mnemonic mnemonic_table[] = {
|
|
|
|
{_STRACE_ALL, "all"},
|
|
|
|
{_STRACE_FLUSH, "flush"},
|
|
|
|
{_STRACE_INHERIT, "inherit"},
|
|
|
|
{_STRACE_UHOH, "uhoh"},
|
|
|
|
{_STRACE_SYSCALL, "syscall"},
|
|
|
|
{_STRACE_STARTUP, "startup"},
|
|
|
|
{_STRACE_DEBUG, "debug"},
|
|
|
|
{_STRACE_PARANOID, "paranoid"},
|
|
|
|
{_STRACE_TERMIOS, "termios"},
|
|
|
|
{_STRACE_SELECT, "select"},
|
|
|
|
{_STRACE_WM, "wm"},
|
|
|
|
{_STRACE_SIGP, "sigp"},
|
|
|
|
{_STRACE_MINIMAL, "minimal"},
|
|
|
|
{_STRACE_EXITDUMP, "exitdump"},
|
|
|
|
{_STRACE_SYSTEM, "system"},
|
|
|
|
{_STRACE_NOMUTEX, "nomutex"},
|
|
|
|
{_STRACE_MALLOC, "malloc"},
|
|
|
|
{_STRACE_THREAD, "thread"},
|
2010-02-20 01:55:27 +08:00
|
|
|
{_STRACE_PTHREAD, "pthread"},
|
2010-02-23 04:39:18 +08:00
|
|
|
{_STRACE_SPECIAL, "special"},
|
2001-11-17 17:48:09 +08:00
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
mnemonic2ul (const char *nptr, char **endptr)
|
|
|
|
{
|
|
|
|
// Look up mnemonic in table, return value.
|
|
|
|
// *endptr = ptr to char that breaks match.
|
|
|
|
const mask_mnemonic *mnp = mnemonic_table;
|
|
|
|
|
|
|
|
while (mnp->text != NULL)
|
|
|
|
{
|
|
|
|
if (strcmp (mnp->text, nptr) == 0)
|
|
|
|
{
|
|
|
|
// Found a match.
|
|
|
|
if (endptr != NULL)
|
|
|
|
{
|
|
|
|
*endptr = ((char *) nptr) + strlen (mnp->text);
|
|
|
|
}
|
|
|
|
return mnp->val;
|
|
|
|
}
|
|
|
|
mnp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't find it.
|
|
|
|
if (endptr != NULL)
|
|
|
|
{
|
|
|
|
*endptr = (char *) nptr;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
parse_mask (const char *ms, char **endptr)
|
|
|
|
{
|
|
|
|
const char *p = ms;
|
|
|
|
char *newp;
|
|
|
|
unsigned long retval = 0, thisval;
|
|
|
|
const size_t bufsize = 16;
|
|
|
|
char buffer[bufsize];
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
while (*p != '\0')
|
|
|
|
{
|
|
|
|
// First extract the term, terminate it, and lowercase it.
|
|
|
|
strncpy (buffer, p, bufsize);
|
|
|
|
buffer[bufsize - 1] = '\0';
|
|
|
|
len = strcspn (buffer, "+,\0");
|
|
|
|
buffer[len] = '\0';
|
|
|
|
strlwr (buffer);
|
|
|
|
|
|
|
|
// Check if this is a mnemonic. We have to do this first or strtoul()
|
|
|
|
// will false-trigger on anything starting with "a" through "f".
|
|
|
|
thisval = mnemonic2ul (buffer, &newp);
|
|
|
|
if (buffer == newp)
|
|
|
|
{
|
|
|
|
// This term isn't mnemonic, check if it's hex.
|
|
|
|
thisval = strtoul (buffer, &newp, 16);
|
|
|
|
if (newp != buffer + len)
|
|
|
|
{
|
|
|
|
// Not hex either, syntax error.
|
|
|
|
*endptr = (char *) p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p += len;
|
|
|
|
retval += thisval;
|
|
|
|
|
|
|
|
// Handle operators
|
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
|
|
|
if ((*p == '+') || (*p == ','))
|
|
|
|
{
|
|
|
|
// For now these both equate to addition/ORing. Until we get
|
|
|
|
// fancy and add things like "all-<something>", all we need do is
|
|
|
|
// continue the looping.
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Syntax error
|
|
|
|
*endptr = (char *) p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*endptr = (char *) p;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-06-06 10:55:10 +08:00
|
|
|
usage (FILE *where = stderr)
|
2001-11-17 17:48:09 +08:00
|
|
|
{
|
2002-06-06 10:55:10 +08:00
|
|
|
fprintf (where, "\
|
|
|
|
Usage: %s [OPTIONS] <command-line>\n\
|
2002-06-07 09:36:17 +08:00
|
|
|
Usage: %s [OPTIONS] -p <pid>\n\
|
2011-10-10 22:57:48 +08:00
|
|
|
\n\
|
2003-04-27 05:52:03 +08:00
|
|
|
Trace system calls and signals\n\
|
|
|
|
\n\
|
2001-12-04 12:12:39 +08:00
|
|
|
-b, --buffer-size=SIZE set size of output file buffer\n\
|
|
|
|
-d, --no-delta don't display the delta-t microsecond timestamp\n\
|
2015-05-16 22:19:59 +08:00
|
|
|
-e, --events log all Windows DEBUG_EVENTS (toggle - default true)\n\
|
2002-06-03 01:46:38 +08:00
|
|
|
-f, --trace-children trace child processes (toggle - default true)\n\
|
2002-06-06 10:55:10 +08:00
|
|
|
-h, --help output usage information and exit\n\
|
2001-12-04 12:12:39 +08:00
|
|
|
-m, --mask=MASK set message filter mask\n\
|
|
|
|
-n, --crack-error-numbers output descriptive text instead of error\n\
|
2011-12-18 07:39:47 +08:00
|
|
|
numbers for Windows errors\n\
|
2002-06-07 09:36:17 +08:00
|
|
|
-o, --output=FILENAME set output file to FILENAME\n\
|
|
|
|
-p, --pid=n attach to executing program with cygwin pid n\n\
|
2007-07-10 08:57:30 +08:00
|
|
|
-q, --quiet suppress messages about attaching, detaching, etc.\n\
|
2001-12-04 12:12:39 +08:00
|
|
|
-S, --flush-period=PERIOD flush buffered strace output every PERIOD secs\n\
|
2002-06-06 10:55:10 +08:00
|
|
|
-t, --timestamp use an absolute hh:mm:ss timestamp insted of \n\
|
2011-12-18 07:39:47 +08:00
|
|
|
the default microsecond timestamp. Implies -d\n\
|
2002-06-07 09:36:17 +08:00
|
|
|
-T, --toggle toggle tracing in a process already being\n\
|
2011-12-18 07:39:47 +08:00
|
|
|
traced. Requires -p <pid>\n\
|
2005-12-06 00:39:06 +08:00
|
|
|
-u, --usecs toggle printing of microseconds timestamp\n\
|
2011-10-10 22:57:48 +08:00
|
|
|
-V, --version output version information and exit\n\
|
2001-12-04 12:12:39 +08:00
|
|
|
-w, --new-window spawn program under test in a new window\n\
|
2002-06-07 09:36:17 +08:00
|
|
|
\n", pgm, pgm);
|
2002-06-06 10:55:10 +08:00
|
|
|
if ( where == stdout)
|
|
|
|
fprintf (stdout, "\
|
2001-11-17 17:48:09 +08:00
|
|
|
MASK can be any combination of the following mnemonics and/or hex values\n\
|
|
|
|
(0x is optional). Combine masks with '+' or ',' like so:\n\
|
|
|
|
\n\
|
2011-12-18 07:39:47 +08:00
|
|
|
--mask=wm+system,malloc+0x00800\n\
|
2001-11-17 17:48:09 +08:00
|
|
|
\n\
|
|
|
|
Mnemonic Hex Corresponding Def Description\n\
|
|
|
|
=========================================================================\n\
|
2010-02-23 04:39:18 +08:00
|
|
|
all 0x000001 (_STRACE_ALL) All strace messages.\n\
|
|
|
|
flush 0x000002 (_STRACE_FLUSH) Flush output buffer after each message.\n\
|
|
|
|
inherit 0x000004 (_STRACE_INHERIT) Children inherit mask from parent.\n\
|
|
|
|
uhoh 0x000008 (_STRACE_UHOH) Unusual or weird phenomenon.\n\
|
|
|
|
syscall 0x000010 (_STRACE_SYSCALL) System calls.\n\
|
|
|
|
startup 0x000020 (_STRACE_STARTUP) argc/envp printout at startup.\n\
|
|
|
|
debug 0x000040 (_STRACE_DEBUG) Info to help debugging. \n\
|
|
|
|
paranoid 0x000080 (_STRACE_PARANOID) Paranoid info.\n\
|
|
|
|
termios 0x000100 (_STRACE_TERMIOS) Info for debugging termios stuff.\n\
|
|
|
|
select 0x000200 (_STRACE_SELECT) Info on ugly select internals.\n\
|
|
|
|
wm 0x000400 (_STRACE_WM) Trace Windows msgs (enable _strace_wm).\n\
|
|
|
|
sigp 0x000800 (_STRACE_SIGP) Trace signal and process handling.\n\
|
|
|
|
minimal 0x001000 (_STRACE_MINIMAL) Very minimal strace output.\n\
|
|
|
|
pthread 0x002000 (_STRACE_PTHREAD) Pthread calls.\n\
|
|
|
|
exitdump 0x004000 (_STRACE_EXITDUMP) Dump strace cache on exit.\n\
|
|
|
|
system 0x008000 (_STRACE_SYSTEM) Serious error; goes to console and log.\n\
|
|
|
|
nomutex 0x010000 (_STRACE_NOMUTEX) Don't use mutex for synchronization.\n\
|
|
|
|
malloc 0x020000 (_STRACE_MALLOC) Trace malloc calls.\n\
|
|
|
|
thread 0x040000 (_STRACE_THREAD) Thread-locking calls.\n\
|
|
|
|
special 0x100000 (_STRACE_SPECIAL) Special debugging printfs for\n\
|
2011-12-18 07:39:47 +08:00
|
|
|
non-checked-in code\n\
|
2001-11-17 17:48:09 +08:00
|
|
|
");
|
2002-06-06 10:55:10 +08:00
|
|
|
if (where == stderr)
|
2011-10-10 22:57:48 +08:00
|
|
|
fprintf (stderr, "Try `%s --help' for more information.\n", pgm);
|
2002-06-06 10:55:10 +08:00
|
|
|
exit (where == stderr ? 1 : 0 );
|
2001-11-17 17:48:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct option longopts[] = {
|
|
|
|
{"buffer-size", required_argument, NULL, 'b'},
|
2015-05-16 22:19:59 +08:00
|
|
|
{"events", no_argument, NULL, 'e'},
|
2002-01-29 10:03:32 +08:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"flush-period", required_argument, NULL, 'S'},
|
2005-01-17 01:13:51 +08:00
|
|
|
{"hex", no_argument, NULL, 'H'},
|
2001-11-17 17:48:09 +08:00
|
|
|
{"mask", required_argument, NULL, 'm'},
|
2002-01-29 10:03:32 +08:00
|
|
|
{"new-window", no_argument, NULL, 'w'},
|
2001-11-17 17:48:09 +08:00
|
|
|
{"output", required_argument, NULL, 'o'},
|
|
|
|
{"no-delta", no_argument, NULL, 'd'},
|
2002-01-29 10:03:32 +08:00
|
|
|
{"pid", required_argument, NULL, 'p'},
|
2005-12-30 04:46:34 +08:00
|
|
|
{"quiet", no_argument, NULL, 'q'},
|
2001-11-17 17:48:09 +08:00
|
|
|
{"timestamp", no_argument, NULL, 't'},
|
2002-06-07 09:36:17 +08:00
|
|
|
{"toggle", no_argument, NULL, 'T'},
|
2002-01-29 10:03:32 +08:00
|
|
|
{"trace-children", no_argument, NULL, 'f'},
|
|
|
|
{"translate-error-numbers", no_argument, NULL, 'n'},
|
|
|
|
{"usecs", no_argument, NULL, 'u'},
|
2011-10-10 22:57:48 +08:00
|
|
|
{"version", no_argument, NULL, 'V'},
|
2001-11-17 17:48:09 +08:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2015-05-16 22:19:59 +08:00
|
|
|
static const char *const opts = "+b:dehHfm:no:p:qS:tTuVw";
|
2001-11-17 17:48:09 +08:00
|
|
|
|
2002-06-06 10:55:10 +08:00
|
|
|
static void
|
|
|
|
print_version ()
|
|
|
|
{
|
2011-10-10 22:57:48 +08:00
|
|
|
printf ("strace (cygwin) %d.%d.%d\n"
|
2011-12-18 07:39:47 +08:00
|
|
|
"System Trace\n"
|
2016-05-24 17:16:39 +08:00
|
|
|
"Copyright (C) 2000 - %s Cygwin Authors\n"
|
2011-12-18 07:39:47 +08:00
|
|
|
"This is free software; see the source for copying conditions. There is NO\n"
|
2011-10-10 22:57:48 +08:00
|
|
|
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
|
2011-12-18 07:39:47 +08:00
|
|
|
CYGWIN_VERSION_DLL_MAJOR / 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MAJOR % 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MINOR,
|
|
|
|
strrchr (__DATE__, ' ') + 1);
|
2002-06-06 10:55:10 +08:00
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
2015-07-29 19:32:29 +08:00
|
|
|
main2 (int argc, char **argv)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
unsigned mask = 0;
|
|
|
|
FILE *ofile = NULL;
|
2002-06-07 09:36:17 +08:00
|
|
|
pid_t pid = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
int opt;
|
2002-06-07 09:37:20 +08:00
|
|
|
int toggle = 0;
|
2005-12-30 04:46:34 +08:00
|
|
|
int sawquiet = -1;
|
2015-08-03 18:02:17 +08:00
|
|
|
DWORD ret = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-05-09 09:21:43 +08:00
|
|
|
if (load_cygwin ())
|
|
|
|
{
|
|
|
|
char **av = (char **) cygwin_internal (CW_ARGV);
|
2013-04-23 17:44:36 +08:00
|
|
|
if (av && (uintptr_t) av != (uintptr_t) -1)
|
2005-05-09 09:21:43 +08:00
|
|
|
for (argc = 0, argv = av; *av; av++)
|
|
|
|
argc++;
|
|
|
|
}
|
|
|
|
|
2017-02-25 03:55:14 +08:00
|
|
|
_setmode (1, _O_BINARY);
|
|
|
|
_setmode (2, _O_BINARY);
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!(pgm = strrchr (*argv, '\\')) && !(pgm = strrchr (*argv, '/')))
|
|
|
|
pgm = *argv;
|
|
|
|
else
|
|
|
|
pgm++;
|
|
|
|
|
2001-11-17 17:48:09 +08:00
|
|
|
while ((opt = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
|
2000-02-18 03:38:33 +08:00
|
|
|
switch (opt)
|
|
|
|
{
|
2002-01-29 10:03:32 +08:00
|
|
|
case 'b':
|
|
|
|
bufsize = atoi (optarg);
|
2001-11-17 17:48:09 +08:00
|
|
|
break;
|
2002-01-29 10:03:32 +08:00
|
|
|
case 'd':
|
|
|
|
delta ^= 1;
|
2001-11-17 17:48:09 +08:00
|
|
|
break;
|
2015-05-16 22:19:59 +08:00
|
|
|
case 'e':
|
|
|
|
events ^= 1;
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
case 'f':
|
|
|
|
forkdebug ^= 1;
|
|
|
|
break;
|
2002-01-29 10:03:32 +08:00
|
|
|
case 'h':
|
|
|
|
// Print help and exit
|
2002-06-06 10:55:10 +08:00
|
|
|
usage (stdout);
|
2000-10-28 08:21:41 +08:00
|
|
|
break;
|
2005-01-17 01:13:51 +08:00
|
|
|
case 'H':
|
|
|
|
include_hex ^= 1;
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
case 'm':
|
2001-11-17 17:48:09 +08:00
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
mask = parse_mask (optarg, &endptr);
|
|
|
|
if (*endptr != '\0')
|
|
|
|
{
|
|
|
|
// Bad mask expression.
|
|
|
|
error (0, "syntax error in mask expression \"%s\" near \
|
|
|
|
character #%d.\n", optarg, (int) (endptr - optarg), endptr);
|
|
|
|
}
|
2002-06-07 09:36:17 +08:00
|
|
|
break;
|
2001-11-17 17:48:09 +08:00
|
|
|
}
|
2002-01-29 10:03:32 +08:00
|
|
|
case 'n':
|
|
|
|
numerror ^= 1;
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
case 'o':
|
2009-07-15 23:18:51 +08:00
|
|
|
if ((ofile = fopen (cygpath (optarg, NULL), "wb")) == NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
error (1, "can't open %s", optarg);
|
|
|
|
#ifdef F_SETFD
|
|
|
|
(void) fcntl (fileno (ofile), F_SETFD, 0);
|
|
|
|
#endif
|
|
|
|
break;
|
2002-01-29 10:03:32 +08:00
|
|
|
case 'p':
|
2002-10-19 19:41:31 +08:00
|
|
|
pid = strtoul (optarg, NULL, 10);
|
2005-12-30 04:46:34 +08:00
|
|
|
strace_active |= 2;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
if (sawquiet < 0)
|
|
|
|
sawquiet = 1;
|
|
|
|
else
|
|
|
|
sawquiet ^= 1;
|
2002-01-29 10:03:32 +08:00
|
|
|
break;
|
|
|
|
case 'S':
|
2002-10-19 19:41:31 +08:00
|
|
|
flush_period = strtoul (optarg, NULL, 10);
|
2000-02-28 13:08:05 +08:00
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
case 't':
|
2000-02-28 13:08:05 +08:00
|
|
|
hhmmss ^= 1;
|
|
|
|
break;
|
2002-06-07 09:36:17 +08:00
|
|
|
case 'T':
|
|
|
|
toggle ^= 1;
|
|
|
|
break;
|
2000-02-28 13:08:05 +08:00
|
|
|
case 'u':
|
2002-01-29 10:03:32 +08:00
|
|
|
// FIXME: currently unimplemented
|
2005-12-06 00:39:06 +08:00
|
|
|
show_usecs ^= 1;
|
|
|
|
delta ^= 1;
|
2001-11-04 20:57:55 +08:00
|
|
|
break;
|
2011-10-10 22:57:48 +08:00
|
|
|
case 'V':
|
2002-01-29 10:03:32 +08:00
|
|
|
// Print version info and exit
|
2002-06-06 10:55:10 +08:00
|
|
|
print_version ();
|
|
|
|
return 0;
|
2001-11-04 20:57:55 +08:00
|
|
|
case 'w':
|
|
|
|
new_window ^= 1;
|
|
|
|
break;
|
2011-10-10 22:57:48 +08:00
|
|
|
default:
|
|
|
|
fprintf (stderr, "Try `%s --help' for more information.\n", pgm);
|
2002-06-07 09:36:17 +08:00
|
|
|
exit (1);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-06-07 09:36:17 +08:00
|
|
|
if (pid && argv[optind])
|
|
|
|
error (0, "cannot provide both a command line and a process id");
|
|
|
|
|
|
|
|
if (!pid && !argv[optind])
|
|
|
|
error (0, "must provide either a command line or a process id");
|
|
|
|
|
|
|
|
if (toggle && !pid)
|
|
|
|
error (0, "must provide a process id to toggle tracing");
|
2002-06-06 10:55:10 +08:00
|
|
|
|
2005-12-30 04:46:34 +08:00
|
|
|
if (!pid)
|
|
|
|
quiet = sawquiet < 0 || !sawquiet;
|
|
|
|
else if (sawquiet < 0)
|
|
|
|
quiet = 0;
|
|
|
|
else
|
|
|
|
quiet = sawquiet;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!mask)
|
2004-02-21 12:51:15 +08:00
|
|
|
mask = _STRACE_ALL;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
if (!ofile)
|
|
|
|
ofile = stdout;
|
|
|
|
|
2015-08-03 18:02:17 +08:00
|
|
|
if (bufsize)
|
|
|
|
setvbuf (ofile, (char *) alloca (bufsize), _IOFBF, bufsize);
|
|
|
|
|
2002-06-07 09:36:17 +08:00
|
|
|
if (toggle)
|
|
|
|
dotoggle (pid);
|
|
|
|
else
|
2015-05-16 22:19:59 +08:00
|
|
|
{
|
|
|
|
drive_map = (void *) cygwin_internal (CW_ALLOC_DRIVE_MAP);
|
2015-08-03 18:02:17 +08:00
|
|
|
ret = dostrace (mask, ofile, pid, argv + optind);
|
2015-05-16 22:19:59 +08:00
|
|
|
if (drive_map)
|
|
|
|
cygwin_internal (CW_FREE_DRIVE_MAP, drive_map);
|
|
|
|
}
|
2015-08-03 18:02:17 +08:00
|
|
|
if (ofile && ofile != stdout)
|
|
|
|
fclose (ofile);
|
|
|
|
ExitProcess (ret);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:32:29 +08:00
|
|
|
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];
|
|
|
|
|
2017-04-20 00:06:02 +08:00
|
|
|
RtlSecureZeroMemory (buf, sizeof (buf));
|
2015-07-29 19:32:29 +08:00
|
|
|
exit (main2 (argc, argv));
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
#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);
|
|
|
|
}
|