Files
UnrealEngineUWP/Engine/Source/Developer/Windows/LiveCodingServer/Private/External/LC_CodeCave.cpp
Ben Marsh e1fe0cc030 Integrating live coding feature (aka Live++) into UE4.
Allows fast iteration of C++ changes without restarting the application. To use, select the "Live Coding (Experimental)" mode from the drop down menu next to the editor's compile button, or type "LiveCoding" into the console for a monolithic build. Press Ctrl+Alt+F11 to find changes and compile.

Changes vs standalone Live++ version:

* UBT is used to execute builds. This allows standard UE4 adaptive unity mode, allows us to reuse object files when we do regular builds, supports using any build executor allowed by UBT (XGE, SNDBS, etc..).
* Adding new source files is supported.
* Custom visualizer for FNames is supported via a weakly linked symbol in a static library (Engine/Extras/NatvisHelpers).
* Settings are exposed in the editor's project settings dialog.
* Standalone application has been rewritten as a Slate app ("LiveCodingConsole"). There is an additional option to start the program as hidden, where it will not be visible until Ctrl+Alt+F11 is hit. Similarly, closing the window will hide it instead of closing the application.
* Does not require a standalone licensed version of Live++.

Known issues:

* Does not currently support class layout changes / object reinstancing

#rb none
#fyi Marc.Audy, Stefan.Boberg, Nick.Penwarden
#jira

[CL 5304722 by Ben Marsh in 4.22 branch]
2019-03-05 15:54:02 -05:00

101 lines
3.2 KiB
C++

// Copyright 2011-2019 Molecular Matters GmbH, all rights reserved.
#include "LC_CodeCave.h"
#include "LC_VirtualMemory.h"
#include "LC_Patch.h"
#include "LC_Thread.h"
CodeCave::CodeCave(process::Handle processHandle, unsigned int processId, unsigned int commandThreadId)
: m_processHandle(processHandle)
, m_processId(processId)
, m_commandThreadId(commandThreadId)
, m_cave(nullptr)
, m_perThreadData()
{
m_perThreadData.reserve(128u);
}
void CodeCave::Install(void)
{
process::Suspend(m_processHandle);
// prepare jump-to-self code cave
const uint32_t pageSize = virtualMemory::GetPageSize();
m_cave = virtualMemory::Allocate(m_processHandle, pageSize, virtualMemory::PageType::EXECUTE_READ_WRITE);
patch::InstallJumpToSelf(m_processHandle, m_cave);
// enumerate all threads of the process now that it's suspended
const std::vector<unsigned int>& threadIds = process::EnumerateThreads(m_processId);
const size_t threadCount = threadIds.size();
m_perThreadData.resize(threadCount);
// set all threads' instruction pointers into the code cave.
// additionally, we set the threads' priority to IDLE so that they don't burn CPU cycles,
// which could totally starve all CPUs and the OS, depending on how many threads
// are currently running.
for (size_t i = 0u; i < threadCount; ++i)
{
const unsigned id = threadIds[i];
m_perThreadData[i].id = id;
if (id == m_commandThreadId)
{
// this is the Live++ command thread, don't put it into the cave
continue;
}
thread::Handle threadHandle = thread::Open(id);
thread::Context context = thread::GetContext(threadHandle);
m_perThreadData[i].priority = thread::GetPriority(threadHandle);
m_perThreadData[i].originalIp = thread::ReadInstructionPointer(context);
thread::SetPriority(threadHandle, THREAD_PRIORITY_IDLE);
thread::WriteInstructionPointer(context, m_cave);
thread::SetContext(threadHandle, context);
thread::Close(threadHandle);
}
// let the process resume. all threads except the Live++ thread will be held in the code cave
process::Resume(m_processHandle);
}
void CodeCave::Uninstall(void)
{
process::Suspend(m_processHandle);
// restore original thread instruction pointers
const size_t threadCount = m_perThreadData.size();
for (size_t i = 0u; i < threadCount; ++i)
{
const unsigned id = m_perThreadData[i].id;
if (id == m_commandThreadId)
{
// this is the Live++ command thread
continue;
}
thread::Handle threadHandle = thread::Open(id);
thread::Context context = thread::GetContext(threadHandle);
const void* currentIp = thread::ReadInstructionPointer(context);
// only set the original instruction pointer if the thread is really being held in the cave.
// in certain situations (e.g. after an exception), the debugger/OS already restored the context
// of all threads, and it would be fatal to interfere with this.
if (currentIp == m_cave)
{
thread::SetPriority(threadHandle, m_perThreadData[i].priority);
thread::WriteInstructionPointer(context, m_perThreadData[i].originalIp);
thread::SetContext(threadHandle, context);
}
thread::Close(threadHandle);
}
// get rid of the code cave
virtualMemory::Free(m_processHandle, m_cave);
process::Resume(m_processHandle);
}