Add support of standard streams redirection in dbgshim
Currently it's impossible to redirect input/output streams of the process created with CreateProcessForLaunch(). Such feature can be useful for debugger implementation for Unix.
However PAL version of CreateProcessW supports stream redirection.
I propose to extend the API of the dbgshim library with something like this:
EXTERN_C HRESULT
CreateProcessForLaunchEx(
__in LPWSTR lpCommandLine,
__in BOOL bSuspendProcess,
__in LPVOID lpEnvironment,
__in LPCWSTR lpCurrentDirectory,
__out PDWORD pProcessId,
__out HANDLE *pResumeHandle,
__out HANDLE *pStdInHandle,
__out HANDLE *pStdOutHandle,
__out HANDLE *pStdErrHandle);
Also a special portable API for HANDLE reading and writing will be added:
typedef VOID (*PREADHANDLE_CALLBACK)(PVOID pBuffer, DWORD dwBufferSizeInBytes, PVOID parameter);
EXTERN_C HRESULT
RegisterForStandardOutput(
__in HANDLE hStdOutHandle,
__in PREADHANDLE_CALLBACK pfnOutCallback,
__in HANDLE hStdErrHandle,
__in PREADHANDLE_CALLBACK pfnErrCallback,
__in PVOID parameter,
__out PVOID *ppListenerToken);
EXTERN_C HRESULT
WriteStandardHandle(
__in HANDLE hStdInHandle,
__in LPCVOID lpBuffer,
__in DWORD dwNumberOfBytesToWrite,
__out DWORD *pdwNumberOfBytesWritten
);
EXTERN_C HRESULT
CloseStandardHandle(
__in HANDLE hStandardHandle);
In such case debugger can handle input/output of the newly created process in a portable way.
@dotnet/dotnet-diag
@mikem8361 - PTAL
If you don't mind, I can create a PR with such feature.
Sorry for the delay; I just got back from vacation. The reason CreateProcessForLaunch doesn't support redirection handles it because "handles" are Windows concepts and are not portable across all our platforms. On Linux, the PAL can create fake handles to emulate various Windows APIs but these handles do not work across different "instances" of the PAL. In other words, libdbgshim.so has a different PAL instance than libmsdaccore.so, libcoreclr.so, etc. or any debugger using libdbgshim.so APIs. A handle created in one PAL instance will not work in another.
I'll think about a way to do this in a platform independent way and get back to this issue. We don't want to add any new APIs that won't work on Linux (or MacOS).
@mikem8361, thank you! Indeed there are some inconveniences with handles from static PAL instances linked with each dynamic library. But stream handles are not supposed to migrate somewhere out of the debugger and the libdbgshim linked with it. Actually I have already some WIP code. So, maybe you can have a look at it, I'll appreciate your comments and advices.
There are more than just inconveniences. It will not work because the debugger and libdbgshim don't use the same static PAL. libdbgshim.so doesn't export the PAL functions needed to create a handle, etc.
Can anyone think of a workaround? Is there any way to read the output of the debuggee (suppose I'm Windows only)?
I would hope that you could use windows CreateProcess API with the CREATE_SUSPENDED flag, then use RegisterForRuntimeStartup(...) then ResumeThread(). @mikem8361 might know if there are issues with that approach?
On Windows that would work just fine. It is on Linux/MacOS that it gets more complicated. You would have to emulate, the CreateProcess CREATE_SUSPENDED, call RegisterForRuntimeStartup, and then "resume" the target process. Internally, that is what dbgshim's CreateProcessForLaunch/ResumeProcess does.
There are "CreateProcessW" and "ResumeThread" APIs exported by the DAC module (libmscordaccore.so/libmscordaccore.dylib) that could be used on Linux/MacOS, but these exports are considered internal, are not really documented and could change in future releases. The CREATE_SUSPENDED flag will work. CREATE_NEW_CONSOLE is ignored. Because of the fake handle issue I point out above, only the CreateFile* APIs export by the DAC can be use to supply the redirection handles.
Thanks! Is CreateProcessForLaunch open source? I'm guessing I could take that implementation and since I only need it on Windows I could tweak to create the process myself (suspended), then RegisterForRuntimeStartup, then resume.
Yes: https://github.com/dotnet/runtime/blob/48cd4789819d4903e11c786155211a97e1746a08/src/coreclr/src/dlls/dbgshim/dbgshim.cpp#L81