1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 //*****************************************************************************
5 // File: DbgTransportPipeline.cpp
9 // Implements the native pipeline for Mac debugging.
10 //*****************************************************************************
13 #include "nativepipeline.h"
14 #include "dbgtransportsession.h"
15 #include "dbgtransportmanager.h"
18 DWORD GetProcessId(const DEBUG_EVENT * pEvent)
20 return pEvent->dwProcessId;
22 DWORD GetThreadId(const DEBUG_EVENT * pEvent)
24 return pEvent->dwThreadId;
27 // Get exception event
28 BOOL IsExceptionEvent(const DEBUG_EVENT * pEvent, BOOL * pfFirstChance, const EXCEPTION_RECORD ** ppRecord)
30 if (pEvent->dwDebugEventCode != EXCEPTION_DEBUG_EVENT)
32 *pfFirstChance = FALSE;
36 *pfFirstChance = pEvent->u.Exception.dwFirstChance;
37 *ppRecord = &(pEvent->u.Exception.ExceptionRecord);
42 //---------------------------------------------------------------------------------------
44 // INativeEventPipeline is an abstraction over the Windows native debugging pipeline. This class is an
45 // implementation which works over an SSL connection for debugging a target process on a Mac remotely.
46 // It builds on top of code:DbgTransportTarget (which is a connection to the debugger proxy on the Mac) and
47 // code:DbgTransportSession (which is a connection to the target process on the Mac). See
48 // code:IEventChannel for more information.
51 // This class is NOT thread-safe. Caller is assumed to have taken the appropriate measures for
55 class DbgTransportPipeline :
56 public INativeEventPipeline
59 DbgTransportPipeline()
63 m_pIPCEvent = reinterpret_cast<DebuggerIPCEvent * >(m_rgbIPCEventBuffer);
66 _ASSERTE(!IsTransportRunning());
69 virtual ~DbgTransportPipeline()
74 // Call to free up the pipeline.
75 virtual void Delete();
77 virtual BOOL DebugSetProcessKillOnExit(bool fKillOnExit);
80 virtual HRESULT CreateProcessUnderDebugger(
81 MachineInfo machineInfo,
82 LPCWSTR lpApplicationName,
83 LPCWSTR lpCommandLine,
84 LPSECURITY_ATTRIBUTES lpProcessAttributes,
85 LPSECURITY_ATTRIBUTES lpThreadAttributes,
87 DWORD dwCreationFlags,
89 LPCWSTR lpCurrentDirectory,
90 LPSTARTUPINFOW lpStartupInfo,
91 LPPROCESS_INFORMATION lpProcessInformation);
94 virtual HRESULT DebugActiveProcess(MachineInfo machineInfo, DWORD processId);
97 virtual HRESULT DebugActiveProcessStop(DWORD processId);
99 // Block and wait for the next debug event from the debuggee process.
100 virtual BOOL WaitForDebugEvent(DEBUG_EVENT * pEvent, DWORD dwTimeout, CordbProcess * pProcess);
102 virtual BOOL ContinueDebugEvent(
105 DWORD dwContinueStatus
108 // Return a handle which will be signaled when the debuggee process terminates.
109 virtual HANDLE GetProcessHandle();
111 // Terminate the debuggee process.
112 virtual BOOL TerminateProcess(UINT32 exitCode);
115 // Return TRUE if the transport is up and runnning
116 BOOL IsTransportRunning()
121 // clean up all resources
124 if (m_hProcess != NULL)
126 CloseHandle(m_hProcess);
132 if (m_ticket.IsValid())
134 m_pTransport->StopUsingAsDebugger(&m_ticket);
136 m_pProxy->ReleaseTransport(m_pTransport);
145 // This is actually a handle to an event. This is only valid for waiting on process termination.
148 DbgTransportTarget * m_pProxy;
149 DbgTransportSession * m_pTransport;
151 // Any buffer for storing a DebuggerIPCEvent must be at least CorDBIPC_BUFFER_SIZE big. For simplicity
152 // sake I have added an extra field member which points to the buffer.
153 DebuggerIPCEvent * m_pIPCEvent;
154 BYTE m_rgbIPCEventBuffer[CorDBIPC_BUFFER_SIZE];
155 DebugTicket m_ticket;
158 // Allocate and return a pipeline object for this platform
159 INativeEventPipeline * NewPipelineForThisPlatform()
161 return new (nothrow) DbgTransportPipeline();
164 // Call to free up the lpProcessInformationpeline.
165 void DbgTransportPipeline::Delete()
170 // set whether to kill outstanding debuggees when the debugger exits.
171 BOOL DbgTransportPipeline::DebugSetProcessKillOnExit(bool fKillOnExit)
173 // This is not supported or necessary for Mac debugging. The only reason we need this on Windows is to
174 // ask the OS not to terminate the debuggee when the debugger exits. The Mac debugging pipeline doesn't
175 // automatically kill the debuggee when the debugger exits.
179 // Create an process under the debugger.
180 HRESULT DbgTransportPipeline::CreateProcessUnderDebugger(
181 MachineInfo machineInfo,
182 LPCWSTR lpApplicationName,
183 LPCWSTR lpCommandLine,
184 LPSECURITY_ATTRIBUTES lpProcessAttributes,
185 LPSECURITY_ATTRIBUTES lpThreadAttributes,
186 BOOL bInheritHandles,
187 DWORD dwCreationFlags,
188 LPVOID lpEnvironment,
189 LPCWSTR lpCurrentDirectory,
190 LPSTARTUPINFOW lpStartupInfo,
191 LPPROCESS_INFORMATION lpProcessInformation)
193 // INativeEventPipeline has a 1:1 relationship with CordbProcess.
194 _ASSERTE(!IsTransportRunning());
196 // We don't support interop-debugging on the Mac.
197 _ASSERTE(!(dwCreationFlags & (DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS)));
199 // When we're using a transport we can't deal with creating a suspended process (we need the process to
200 // startup in order that it can start up a transport thread and reply to our messages).
201 _ASSERTE(!(dwCreationFlags & CREATE_SUSPENDED));
203 // Connect to the debugger proxy on the remote machine and ask it to create a process for us.
206 m_pProxy = g_pDbgTransportTarget;
207 hr = m_pProxy->CreateProcess(lpApplicationName,
216 lpProcessInformation);
220 // Establish a connection to the actual runtime to be debugged.
221 hr = m_pProxy->GetTransportForProcess(lpProcessInformation->dwProcessId,
226 // Wait for the connection to become useable (or time out).
227 if (!m_pTransport->WaitForSessionToOpen(10000))
229 hr = CORDBG_E_TIMEOUT;
233 if (!m_pTransport->UseAsDebugger(&m_ticket))
235 hr = CORDBG_E_DEBUGGER_ALREADY_ATTACHED;
243 _ASSERTE((m_hProcess != NULL) && (m_hProcess != INVALID_HANDLE_VALUE));
245 m_dwProcessId = lpProcessInformation->dwProcessId;
247 // For Mac remote debugging, we don't actually have a process handle to hand back to the debugger.
248 // Instead, we return a handle to an event as the "process handle". The Win32 event thread also waits
249 // on this event handle, and the event will be signaled when the proxy notifies us that the process
250 // on the remote machine is terminated. However, normally the debugger calls CloseHandle() immediately
251 // on the "process handle" after CreateProcess() returns. Doing so causes the Win32 event thread to
252 // continue waiting on a closed event handle, and so it will never wake up.
253 // (In fact, in Whidbey, we also duplicate the process handle in code:CordbProcess::Init.)
254 if (!DuplicateHandle(GetCurrentProcess(),
257 &(lpProcessInformation->hProcess),
258 0, // ignored since we are going to pass DUPLICATE_SAME_ACCESS
260 DUPLICATE_SAME_ACCESS))
262 hr = HRESULT_FROM_GetLastError();
278 // Attach the debugger to this process.
279 HRESULT DbgTransportPipeline::DebugActiveProcess(MachineInfo machineInfo, DWORD processId)
281 // INativeEventPipeline has a 1:1 relationship with CordbProcess.
282 _ASSERTE(!IsTransportRunning());
286 m_pProxy = g_pDbgTransportTarget;
288 // Establish a connection to the actual runtime to be debugged.
289 hr = m_pProxy->GetTransportForProcess(processId, &m_pTransport, &m_hProcess);
292 // TODO: Pass this timeout as a parameter all the way from debugger
293 // Wait for the connection to become useable (or time out).
294 if (!m_pTransport->WaitForSessionToOpen(10000))
296 hr = CORDBG_E_TIMEOUT;
300 if (!m_pTransport->UseAsDebugger(&m_ticket))
302 hr = CORDBG_E_DEBUGGER_ALREADY_ATTACHED;
309 m_dwProcessId = processId;
321 HRESULT DbgTransportPipeline::DebugActiveProcessStop(DWORD processId)
323 // The only way to tell the transport to detach from a process is by shutting it down.
324 // That will happen when we neuter the CordbProcess object.
328 // Block and wait for the next debug event from the debuggee process.
329 BOOL DbgTransportPipeline::WaitForDebugEvent(DEBUG_EVENT * pEvent, DWORD dwTimeout, CordbProcess * pProcess)
331 if (!IsTransportRunning())
336 // We need to wait for a debug event from the transport and the process termination event.
337 // On Windows, process termination is communicated via a debug event as well, but that's not true for
338 // the Mac debugging transport.
340 HANDLE rghWaitSet[2];
341 rghWaitSet[0] = m_pTransport->GetDebugEventReadyEvent();
342 rghWaitSet[1] = m_hProcess;
344 DWORD dwRet = ::WaitForMultipleObjectsEx(cWaitSet, rghWaitSet, FALSE, dwTimeout, FALSE);
346 if (dwRet == WAIT_OBJECT_0)
348 // The Mac debugging transport actually transmits IPC events and not debug events.
349 // We need to convert the IPC event to a debug event and pass it back to the caller.
350 m_pTransport->GetNextEvent(m_pIPCEvent, CorDBIPC_BUFFER_SIZE);
352 pEvent->dwProcessId = m_pIPCEvent->processId;
353 _ASSERTE(m_dwProcessId == m_pIPCEvent->processId);
355 // We are supposed to return a thread ID in the DEBUG_EVENT back to our caller.
356 // However, we don't actually store the thread ID in the DebuggerIPCEvent anymore. Instead,
357 // we just get a VMPTR_Thread, and so we need to find the thread ID associated with the VMPTR_Thread.
358 pEvent->dwThreadId = 0;
362 if (!m_pIPCEvent->vmThread.IsNull())
364 pEvent->dwThreadId = pProcess->GetDAC()->TryGetVolatileOSThreadID(m_pIPCEvent->vmThread);
367 EX_CATCH_HRESULT(hr);
373 // The Windows implementation stores the target address of the IPC event in the debug event.
374 // We can do that for Mac debugging, but that would require the caller to do another cross-machine
375 // ReadProcessMemory(). Since we have all the data in-proc already, we just store a local address.
377 // @dbgtodo Mac - We are using -1 as a dummy base address right now.
378 // Currently Mac remote debugging doesn't really support multi-instance.
379 InitEventForDebuggerNotification(pEvent, PTR_TO_CORDB_ADDRESS(reinterpret_cast<LPVOID>(-1)), m_pIPCEvent);
383 else if (dwRet == (WAIT_OBJECT_0 + 1))
385 // The process has been terminated.
387 // We don't have a lot of information here.
388 pEvent->dwDebugEventCode = EXIT_PROCESS_DEBUG_EVENT;
389 pEvent->dwProcessId = m_dwProcessId;
390 pEvent->dwThreadId = 0; // On Windows this is the first thread created in the process.
391 pEvent->u.ExitProcess.dwExitCode = 0; // This is not passed back to us by the transport.
393 // Once the process termination event is signaled, we cannot send or receive any events.
394 // So we mark the transport as not running anymore.
400 // We may have timed out, or the actual wait operation may have failed.
401 // Either way, we don't have an event.
406 BOOL DbgTransportPipeline::ContinueDebugEvent(
409 DWORD dwContinueStatus
412 if (!IsTransportRunning())
417 // See code:INativeEventPipeline::ContinueDebugEvent.
421 // Return a handle which will be signaled when the debuggee process terminates.
422 HANDLE DbgTransportPipeline::GetProcessHandle()
424 HANDLE hProcessTerminated;
426 if (!DuplicateHandle(GetCurrentProcess(),
430 0, // ignored since we are going to pass DUPLICATE_SAME_ACCESS
432 DUPLICATE_SAME_ACCESS))
437 // The handle returned here is only valid for waiting on process termination.
438 // See code:INativeEventPipeline::GetProcessHandle.
439 return hProcessTerminated;
442 // Terminate the debuggee process.
443 BOOL DbgTransportPipeline::TerminateProcess(UINT32 exitCode)
445 _ASSERTE(IsTransportRunning());
447 // The transport will still be running until the process termination handle is signaled.
448 m_pProxy->KillProcess(m_dwProcessId);