Allow specifying environment variable modifications

* This works for local and remote invocations of programs, but is mostly
  useful on unix systems (Windows programs use env vars less often)
This commit is contained in:
baldurk committed 2016-08-19 17:26:33 +02:00
1 parent 71cca06683
commit b5e6f8bef2
20 files changed
+1132 -78

No files matched your search

+8 -8
View File
@@ -52,17 +52,17 @@ enum ModificationType
eEnvModification_Replace = 0,
// prepend/append options will replace if there is no existing variable
eEnvModification_Append, // append with no separators
eEnvModification_AppendColon, // append, separated by colons
eEnvModification_AppendSemiColon, // append, separated by semi-colons
eEnvModification_AppendPlatform, // append, separated by colons for linux & semi-colons for
// windows
eEnvModification_AppendSemiColon, // append, separated by semi-colons
eEnvModification_AppendColon, // append, separated by colons
eEnvModification_Append, // append with no separators
eEnvModification_Prepend, // prepend with no separators
eEnvModification_PrependColon, // prepend, separated by colons
eEnvModification_PrependSemiColon, // prepend, separated by semi-colons
eEnvModification_PrependPlatform, // prepend, separated by colons for linux & semi-colons for
// windows
eEnvModification_PrependSemiColon, // prepend, separated by semi-colons
eEnvModification_PrependColon, // prepend, separated by colons
eEnvModification_Prepend, // prepend with no separators
};
struct EnvironmentModification
{
@@ -84,8 +84,8 @@ uint32_t InjectIntoProcess(uint32_t pid, const char *logfile, const CaptureOptio
bool waitForExit);
uint32_t LaunchProcess(const char *app, const char *workingDir, const char *cmdLine);
uint32_t LaunchAndInjectIntoProcess(const char *app, const char *workingDir, const char *cmdLine,
const char *logfile, const CaptureOptions *opts,
bool waitForExit);
EnvironmentModification *env, const char *logfile,
const CaptureOptions *opts, bool waitForExit);
void *LoadModule(const char *module);
void *GetFunctionAddress(void *module, const char *function);
uint32_t GetCurrentPID();
+19 -2
View File
@@ -361,8 +361,9 @@ uint32_t Process::LaunchProcess(const char *app, const char *workingDir, const c
}
uint32_t Process::LaunchAndInjectIntoProcess(const char *app, const char *workingDir,
const char *cmdLine, const char *logfile,
const CaptureOptions *opts, bool waitForExit)
const char *cmdLine, EnvironmentModification *envList,
const char *logfile, const CaptureOptions *opts,
bool waitForExit)
{
if(app == NULL || app[0] == 0)
{
@@ -375,6 +376,22 @@ uint32_t Process::LaunchAndInjectIntoProcess(const char *app, const char *workin
map<string, string> env = EnvStringToEnvMap((const char **)currentEnvironment);
vector<EnvironmentModification> &modifications = GetEnvModifications();
if(envList)
{
for(;;)
{
EnvironmentModification e = *envList;
e.name = trim(e.name);
if(e.name == "")
break;
modifications.push_back(e);
envList++;
}
}
if(logfile == NULL)
logfile = "";
+70 -5
View File
@@ -171,6 +171,35 @@ extern "C" __declspec(dllexport) void __cdecl RENDERDOC_SetLogFile(const char *l
if(log)
RenderDoc::Inst().SetLogFile(log);
}
static Process::EnvironmentModification tempEnvMod;
extern "C" __declspec(dllexport) void __cdecl RENDERDOC_EnvModName(const char *name)
{
if(name)
tempEnvMod.name = name;
}
extern "C" __declspec(dllexport) void __cdecl RENDERDOC_EnvModValue(const char *value)
{
if(value)
tempEnvMod.value = value;
}
extern "C" __declspec(dllexport) void __cdecl RENDERDOC_EnvMod(Process::ModificationType *type)
{
if(type)
{
tempEnvMod.type = *type;
Process::RegisterEnvironmentModification(tempEnvMod);
}
}
extern "C" __declspec(dllexport) void __cdecl RENDERDOC_ApplyEnvMods(void *ignored)
{
Process::ApplyEnvironmentModification();
}
void InjectDLL(HANDLE hProcess, wstring libName)
{
wchar_t dllPath[MAX_PATH + 1] = {0};
@@ -591,8 +620,9 @@ uint32_t Process::LaunchProcess(const char *app, const char *workingDir, const c
}
uint32_t Process::LaunchAndInjectIntoProcess(const char *app, const char *workingDir,
const char *cmdLine, const char *logfile,
const CaptureOptions *opts, bool waitForExit)
const char *cmdLine, EnvironmentModification *env,
const char *logfile, const CaptureOptions *opts,
bool waitForExit)
{
void *func = GetProcAddress(GetModuleHandleA("renderdoc.dll"), "RENDERDOC_SetLogFile");
@@ -607,18 +637,53 @@ uint32_t Process::LaunchAndInjectIntoProcess(const char *app, const char *workin
if(pi.dwProcessId == 0)
return 0;
// don't need it
CloseHandle(pi.hProcess);
uint32_t ret = InjectIntoProcess(pi.dwProcessId, logfile, opts, false);
if(ret == 0)
{
ResumeThread(pi.hThread);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
if(env)
{
uintptr_t loc = FindRemoteDLL(pi.dwProcessId, L"renderdoc.dll");
if(loc == 0)
{
RDCERR("Can't locate renderdoc.dll in remote PID %d", pi.dwProcessId);
}
else
{
for(;;)
{
string name = trim(env->name);
string value = env->value;
ModificationType type = env->type;
if(name == "")
break;
InjectFunctionCall(pi.hProcess, loc, "RENDERDOC_EnvModName", (void *)name.c_str(),
name.size() + 1);
InjectFunctionCall(pi.hProcess, loc, "RENDERDOC_EnvModValue", (void *)value.c_str(),
value.size() + 1);
InjectFunctionCall(pi.hProcess, loc, "RENDERDOC_EnvMod", &type, sizeof(type));
env++;
}
// parameter is unused
InjectFunctionCall(pi.hProcess, loc, "RENDERDOC_ApplyEnvMods", env,
sizeof(EnvironmentModification));
}
}
// done with it
CloseHandle(pi.hProcess);
ResumeThread(pi.hThread);
if(waitForExit)