Skip to content

Commit

Permalink
Add support for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Feb 26, 2024
1 parent d3a3e17 commit 16c7074
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
33 changes: 32 additions & 1 deletion gum/backend-windows/gumprocess-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ typedef void (WINAPI * GumGetCurrentThreadStackLimitsFunc) (
PULONG_PTR low_limit, PULONG_PTR high_limit);
typedef struct _GumEnumerateSymbolsContext GumEnumerateSymbolsContext;
typedef struct _GumFindExportContext GumFindExportContext;
typedef BOOL (WINAPI * GetThreadTimesFunc) (HANDLE ThreadHandle,
LPFILETIME lpCreationTime, LPFILETIME lpExitTime, LPFILETIME lpKernelTime,
LPFILETIME lpUserTime);

struct _GumEnumerateSymbolsContext
{
Expand Down Expand Up @@ -640,7 +643,35 @@ gum_thread_resume (GumThreadId thread_id,
guint64
gum_thead_get_user_time (void)
{
return 0;
static gboolean initialized = false;
static GetThreadTimesFunc get_thread_times = NULL;

HMODULE mod;
FILETIME userTime;
guint64 result;

if (!initialized)
{
initialized = TRUE;

mod = GetModuleHandle (_T ("kernel32.dll"));
if (mod == NULL)
return 0;

get_thread_times =(GetThreadTimesFunc) GetProcAddress (mod,
"GetThreadTimes");
}

if (get_thread_times == NULL)
return 0;

if (!get_thread_times (GetCurrentThread (), NULL, NULL, NULL, &userTime))
return 0;

result = ((guint64) userTime.dwHighDateTime) << 32 + userTime.dwLowDateTime;

/* Timings on Windows are to 100-nanosecond granularity. Convert to u-secs */
return result / 10;
}

gboolean
Expand Down
9 changes: 6 additions & 3 deletions tests/core/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ TESTCASE (process_threads_get_user_time)

do_work ();
user_time_b = gum_thead_get_user_time ();
#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD)
#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD) \
|| defined (HAVE_WINDOWS)
g_assert_cmpuint (user_time_a, !=, 0);
g_assert_cmpuint (user_time_b, >, user_time_a);
#else
Expand All @@ -331,7 +332,8 @@ TESTCASE (process_threads_get_user_time_by_id_self)

do_work ();
user_time_b = gum_thead_get_user_time_by_id (tid);
#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD)
#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD) \
|| defined (HAVE_WINDOWS)
g_assert_cmpuint (user_time_a, !=, 0);
g_assert_cmpuint (user_time_b, >, user_time_a);
#else
Expand Down Expand Up @@ -360,7 +362,8 @@ TESTCASE (process_threads_get_user_time_by_id_other)
g_usleep (250000);
user_time_b = gum_thead_get_user_time_by_id (d.id);

#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD)
#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD) \
|| defined (HAVE_WINDOWS)
g_assert_cmpuint (user_time_a, !=, 0);
g_assert_cmpuint (user_time_b, >, user_time_a);
#else
Expand Down
9 changes: 6 additions & 3 deletions tests/gumjs/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -5183,7 +5183,8 @@ TESTCASE (process_threads_get_user_time)
);
EXPECT_NO_MESSAGES ();

#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD)
#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD) \
|| defined (HAVE_WINDOWS)
g_assert_true (user_time_a != 0);
g_assert_true (user_time_b > user_time_a);
#else
Expand Down Expand Up @@ -5251,7 +5252,8 @@ TESTCASE (process_threads_get_user_time_other_thread)
g_async_queue_unref (ctx.sleeper_messages);
g_async_queue_unref (ctx.controller_messages);

#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD)
#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD) \
|| defined (HAVE_WINDOWS)
g_assert_true (user_time_a != 0);
g_assert_true (user_time_b > user_time_a);
#else
Expand Down Expand Up @@ -5361,7 +5363,8 @@ TESTCASE (process_threads_find_busy_thread)
g_async_queue_unref (ctx[i].controller_messages);
}

#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD)
#if defined (HAVE_LINUX) || defined (HAVE_DARWIN) || defined (HAVE_FREEBSD) \
|| defined (HAVE_WINDOWS)
EXPECT_SEND_MESSAGE_WITH ("%" G_GSIZE_MODIFIER "u", ctx[rand].id);
#endif
}
Expand Down

0 comments on commit 16c7074

Please sign in to comment.