2015-05-10 16:35:52 -05:00
|
|
|
// 10 may 2015
|
|
|
|
#include "uipriv_windows.h"
|
|
|
|
|
|
|
|
static FILE *fprof = NULL;
|
|
|
|
|
2015-05-10 22:02:16 -05:00
|
|
|
static DWORD WINAPI profilerThread(LPVOID th)
|
2015-05-10 16:35:52 -05:00
|
|
|
{
|
2015-05-10 22:02:16 -05:00
|
|
|
HANDLE thread = (HANDLE) th;
|
2015-05-10 16:35:52 -05:00
|
|
|
LARGE_INTEGER counter;
|
2015-05-10 22:02:16 -05:00
|
|
|
CONTEXT ctxt;
|
2015-05-10 16:35:52 -05:00
|
|
|
|
2015-05-10 22:02:16 -05:00
|
|
|
// TODO check for errors
|
|
|
|
if (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) == 0)
|
|
|
|
complain("error setting thread priority in profilerThread()");
|
|
|
|
for (;;) {
|
|
|
|
if (SuspendThread(thread) == (DWORD) (-1))
|
|
|
|
complain("error suspending thread in profilerThread()");
|
|
|
|
QueryPerformanceCounter(&counter);
|
|
|
|
ZeroMemory(&ctxt, sizeof (CONTEXT));
|
|
|
|
ctxt.ContextFlags = CONTEXT_CONTROL;
|
|
|
|
if (GetThreadContext(thread, &ctxt) == 0)
|
|
|
|
complain("error getting thread context in profilerThread()");
|
|
|
|
fprintf(fprof, "%I64X %I64d\n",
|
|
|
|
(DWORD64) (ctxt.Eip),
|
|
|
|
counter.QuadPart);
|
|
|
|
fflush(fprof);
|
|
|
|
if (ResumeThread(thread) == (DWORD) (-1))
|
|
|
|
complain("error resuming thread in profilerThread()");
|
|
|
|
Sleep(100);
|
|
|
|
}
|
|
|
|
return 0;
|
2015-05-10 16:35:52 -05:00
|
|
|
}
|
|
|
|
|
2015-05-10 22:02:16 -05:00
|
|
|
void initprofiler(HANDLE thread)
|
2015-05-10 16:35:52 -05:00
|
|
|
{
|
2015-05-10 22:02:16 -05:00
|
|
|
fprof = fopen("profiler.out", "w");
|
|
|
|
if (fprof == NULL) {
|
|
|
|
fprintf(stderr, "error opening profiler output file\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if (CreateThread(NULL, 0, profilerThread, thread, 0, NULL) == NULL)
|
|
|
|
complain("error creating profiler thread");
|
2015-05-10 16:35:52 -05:00
|
|
|
}
|