2011-06-16 23:55:20 +00:00
|
|
|
/* IPXWrapper - Stub DLL functions
|
2024-06-18 00:03:34 +01:00
|
|
|
* Copyright (C) 2008-2024 Daniel Collins <solemnwarning@solemnwarning.net>
|
2008-12-09 21:36:07 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 as published by
|
|
|
|
* the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2011-08-28 21:27:06 +00:00
|
|
|
#include "common.h"
|
2012-12-01 14:34:14 +00:00
|
|
|
#include "config.h"
|
2019-08-23 20:25:14 +01:00
|
|
|
#include "funcprof.h"
|
2011-08-28 21:27:06 +00:00
|
|
|
|
2019-08-24 16:06:41 +01:00
|
|
|
static DWORD WINAPI prof_thread_main(LPVOID lpParameter);
|
|
|
|
|
|
|
|
static HANDLE prof_thread_handle = NULL;
|
|
|
|
static HANDLE prof_thread_exit = NULL;
|
|
|
|
|
2017-06-18 02:47:51 +01:00
|
|
|
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
|
|
|
|
if(fdwReason == DLL_PROCESS_ATTACH)
|
2012-10-21 10:26:52 +00:00
|
|
|
{
|
2019-08-24 16:06:41 +01:00
|
|
|
fprof_init(stub_fstats, NUM_STUBS);
|
2019-08-23 20:25:14 +01:00
|
|
|
|
2024-06-18 00:03:34 +01:00
|
|
|
log_init();
|
2011-07-13 22:56:19 +00:00
|
|
|
|
2024-06-23 23:25:13 +01:00
|
|
|
main_config_t config = get_main_config(false);
|
2019-08-24 16:06:41 +01:00
|
|
|
|
2023-11-19 21:49:52 +00:00
|
|
|
min_log_level = config.log_level;
|
|
|
|
|
2024-11-03 12:37:56 +00:00
|
|
|
log_connect(config.log_server_addr, config.log_server_port);
|
|
|
|
|
2023-11-19 21:49:52 +00:00
|
|
|
if(config.profile)
|
2019-08-24 16:06:41 +01:00
|
|
|
{
|
2023-11-19 21:49:52 +00:00
|
|
|
stubs_enable_profile = true;
|
2019-08-24 16:06:41 +01:00
|
|
|
|
2023-11-19 21:49:52 +00:00
|
|
|
prof_thread_exit = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
if(prof_thread_exit != NULL)
|
2019-08-24 16:06:41 +01:00
|
|
|
{
|
2024-06-26 00:37:43 +01:00
|
|
|
DWORD prof_thread_id;
|
2023-11-19 21:49:52 +00:00
|
|
|
prof_thread_handle = CreateThread(
|
|
|
|
NULL, /* lpThreadAttributes */
|
|
|
|
0, /* dwStackSize */
|
|
|
|
&prof_thread_main, /* lpStartAddress */
|
|
|
|
NULL, /* lpParameter */
|
|
|
|
0, /* dwCreationFlags */
|
2024-06-26 00:37:43 +01:00
|
|
|
&prof_thread_id); /* lpThreadId */
|
2023-11-19 21:49:52 +00:00
|
|
|
|
|
|
|
if(prof_thread_handle == NULL)
|
|
|
|
{
|
|
|
|
log_printf(LOG_ERROR,
|
|
|
|
"Unable to create prof_thread_main thread: %s",
|
|
|
|
w32_error(GetLastError()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2019-08-24 16:06:41 +01:00
|
|
|
log_printf(LOG_ERROR,
|
2023-11-19 21:49:52 +00:00
|
|
|
"Unable to create prof_thread_exit event object: %s",
|
2019-08-24 16:06:41 +01:00
|
|
|
w32_error(GetLastError()));
|
|
|
|
}
|
|
|
|
}
|
2012-10-21 10:26:52 +00:00
|
|
|
}
|
2017-06-18 02:47:51 +01:00
|
|
|
else if(fdwReason == DLL_PROCESS_DETACH)
|
2012-10-21 10:26:52 +00:00
|
|
|
{
|
2017-06-18 02:47:51 +01:00
|
|
|
/* When the "lpvReserved" parameter is non-NULL, the process is terminating rather
|
|
|
|
* than the DLL being unloaded dynamically and any threads will have been terminated
|
|
|
|
* at unknown points, meaning any global data may be in an inconsistent state and we
|
|
|
|
* cannot (safely) clean up. MSDN states we should do nothing.
|
|
|
|
*/
|
|
|
|
if(lpvReserved != NULL)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-08-24 16:06:41 +01:00
|
|
|
if(prof_thread_exit != NULL)
|
|
|
|
{
|
|
|
|
SetEvent(prof_thread_exit);
|
|
|
|
|
|
|
|
if(prof_thread_handle != NULL)
|
|
|
|
{
|
|
|
|
WaitForSingleObject(prof_thread_handle, INFINITE);
|
|
|
|
|
|
|
|
CloseHandle(prof_thread_handle);
|
|
|
|
prof_thread_handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(prof_thread_exit);
|
|
|
|
prof_thread_exit = NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-11 13:28:41 +00:00
|
|
|
unload_dlls();
|
2019-08-24 16:06:41 +01:00
|
|
|
|
2023-11-19 21:49:52 +00:00
|
|
|
if(stubs_enable_profile)
|
|
|
|
{
|
|
|
|
fprof_report(STUBS_DLL_NAME, stub_fstats, NUM_STUBS);
|
|
|
|
}
|
2019-08-24 16:06:41 +01:00
|
|
|
|
2011-07-13 22:56:19 +00:00
|
|
|
log_close();
|
2019-08-23 20:25:14 +01:00
|
|
|
|
2019-08-24 16:06:41 +01:00
|
|
|
fprof_cleanup(stub_fstats, NUM_STUBS);
|
2008-12-09 21:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2019-08-24 16:06:41 +01:00
|
|
|
|
|
|
|
static DWORD WINAPI prof_thread_main(LPVOID lpParameter)
|
|
|
|
{
|
|
|
|
static const int PROF_INTERVAL_MS = 10000;
|
|
|
|
|
|
|
|
while(WaitForSingleObject(prof_thread_exit, PROF_INTERVAL_MS) == WAIT_TIMEOUT)
|
|
|
|
{
|
|
|
|
fprof_report(STUBS_DLL_NAME, stub_fstats, NUM_STUBS);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|