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: breakpoint.cpp
9 //*****************************************************************************
12 /* ------------------------------------------------------------------------- *
14 * ------------------------------------------------------------------------- */
16 CordbBreakpoint::CordbBreakpoint(CordbProcess * pProcess, CordbBreakpointType bpType)
17 : CordbBase(pProcess, 0, enumCordbBreakpoint),
18 m_active(false), m_pAppDomain(NULL), m_type(bpType)
22 // Neutered by CordbAppDomain
23 void CordbBreakpoint::Neuter()
25 m_pAppDomain = NULL; // clear ref
29 HRESULT CordbBreakpoint::QueryInterface(REFIID id, void **pInterface)
31 if (id == IID_ICorDebugBreakpoint)
33 *pInterface = static_cast<ICorDebugBreakpoint*>(this);
35 else if (id == IID_IUnknown)
37 *pInterface = static_cast<IUnknown *>(static_cast<ICorDebugBreakpoint*>(this));
48 HRESULT CordbBreakpoint::BaseIsActive(BOOL *pbActive)
50 *pbActive = m_active ? TRUE : FALSE;
55 /* ------------------------------------------------------------------------- *
56 * Function Breakpoint class
57 * ------------------------------------------------------------------------- */
59 CordbFunctionBreakpoint::CordbFunctionBreakpoint(CordbCode *code,
62 : CordbBreakpoint(code->GetProcess(), CBT_FUNCTION),
63 m_code(code), m_offset(offset),
64 m_offsetIsIl(offsetIsIl)
66 // Remember the app domain we came from so that breakpoints can be
67 // deactivated from within the ExitAppdomain callback.
68 m_pAppDomain = m_code->GetAppDomain();
69 _ASSERTE(m_pAppDomain != NULL);
72 CordbFunctionBreakpoint::~CordbFunctionBreakpoint()
74 // @todo- eventually get CordbFunctionBreakpoint rooted and enable this.
75 //_ASSERTE(this->IsNeutered());
76 //_ASSERTE(m_code == NULL);
79 void CordbFunctionBreakpoint::Neuter()
82 CordbBreakpoint::Neuter();
85 HRESULT CordbFunctionBreakpoint::QueryInterface(REFIID id, void **pInterface)
87 if (id == IID_ICorDebugFunctionBreakpoint)
89 *pInterface = static_cast<ICorDebugFunctionBreakpoint*>(this);
93 // Not looking for a function breakpoint? See if the base class handles
94 // this interface. (issue 143976)
95 return CordbBreakpoint::QueryInterface(id, pInterface);
102 HRESULT CordbFunctionBreakpoint::GetFunction(ICorDebugFunction **ppFunction)
104 PUBLIC_API_ENTRY(this);
105 FAIL_IF_NEUTERED(this);
106 VALIDATE_POINTER_TO_OBJECT(ppFunction, ICorDebugFunction **);
110 return CORDBG_E_PROCESS_TERMINATED;
112 if (m_code->IsNeutered())
114 return CORDBG_E_CODE_NOT_AVAILABLE;
117 *ppFunction = static_cast<ICorDebugFunction *> (m_code->GetFunction());
118 (*ppFunction)->AddRef();
123 // m_id is actually a LSPTR_BREAKPOINT. Get it as a type-safe member.
124 LSPTR_BREAKPOINT CordbFunctionBreakpoint::GetLsPtrBP()
131 HRESULT CordbFunctionBreakpoint::GetOffset(ULONG32 *pnOffset)
133 //REVISIT_TODO: is this casting correct for ia64?
134 PUBLIC_API_ENTRY(this);
135 FAIL_IF_NEUTERED(this);
136 VALIDATE_POINTER_TO_OBJECT(pnOffset, SIZE_T *);
138 *pnOffset = (ULONG32)m_offset;
143 //---------------------------------------------------------------------------------------
145 // Activates or removes a breakpoint
148 // fActivate - TRUE if to activate the breakpoint, else FALSE.
151 // S_OK if successful, else a specific error code detailing the type of failure.
153 //---------------------------------------------------------------------------------------
154 HRESULT CordbFunctionBreakpoint::Activate(BOOL fActivate)
156 PUBLIC_REENTRANT_API_ENTRY(this);
157 OK_IF_NEUTERED(this); // we'll check again later
159 if (fActivate == (m_active == true) )
164 // For backwards compat w/ everett, we let the other error codes
165 // take precedence over neutering error codes.
166 if ((m_code == NULL) || this->IsNeutered())
168 return CORDBG_E_PROCESS_TERMINATED;
172 ATT_ALLOW_LIVE_DO_STOPGO(GetProcess());
174 // For legacy, check this error condition. We must do this under the stop-go lock to ensure
175 // that the m_code object was not deleted out from underneath us.
177 // 6/23/09 - This isn't just for legacy anymore, collectible types should be able to hit this
178 // by unloading the module containing the code this breakpoint is bound to.
179 if (m_code->IsNeutered())
181 return CORDBG_E_CODE_NOT_AVAILABLE;
186 // <REVISIT_TODO>@todo: when we implement module and value breakpoints, then
187 // we'll want to factor some of this code out.</REVISIT_TODO>
189 CordbProcess * pProcess = GetProcess();
191 RSLockHolder lockHolder(pProcess->GetProcessLock());
192 pProcess->ClearPatchTable(); // if we add something, then the right side
193 // view of the patch table is no longer valid
195 DebuggerIPCEvent * pEvent = (DebuggerIPCEvent *) _alloca(CorDBIPC_BUFFER_SIZE);
197 CordbAppDomain * pAppDomain = GetAppDomain();
198 _ASSERTE (pAppDomain != NULL);
202 pProcess->InitIPCEvent(pEvent, DB_IPCE_BREAKPOINT_ADD, true, pAppDomain->GetADToken());
204 pEvent->BreakpointData.funcMetadataToken = m_code->GetMetadataToken();
205 pEvent->BreakpointData.vmDomainFile = m_code->GetModule()->GetRuntimeDomainFile();
206 pEvent->BreakpointData.encVersion = m_code->GetVersion();
208 BOOL codeIsIL = m_code->IsIL();
210 pEvent->BreakpointData.isIL = m_offsetIsIl;
211 pEvent->BreakpointData.offset = m_offset;
214 pEvent->BreakpointData.nativeCodeMethodDescToken = pEvent->BreakpointData.nativeCodeMethodDescToken.NullPtr();
218 pEvent->BreakpointData.nativeCodeMethodDescToken =
219 (m_code.GetValue()->AsNativeCode())->GetVMNativeCodeMethodDescToken().ToLsPtr();
222 // Note: we're sending a two-way event, so it blocks here
223 // until the breakpoint is really added and the reply event is
224 // copied over the event we sent.
225 lockHolder.Release();
226 hr = pProcess->SendIPCEvent(pEvent, CorDBIPC_BUFFER_SIZE);
227 lockHolder.Acquire();
229 hr = WORST_HR(hr, pEvent->hr);
237 m_id = LsPtrToCookie(pEvent->BreakpointData.breakpointToken);
239 // If we weren't able to allocate the BP, we should have set the
240 // hr on the left side.
244 pAppDomain->m_breakpoints.AddBase(this);
247 // Continue called automatically by StopContinueHolder
251 _ASSERTE (pAppDomain != NULL);
253 if (pProcess->IsSafeToSendEvents())
255 pProcess->InitIPCEvent(pEvent, DB_IPCE_BREAKPOINT_REMOVE, false, pAppDomain->GetADToken());
257 pEvent->BreakpointData.breakpointToken = GetLsPtrBP();
259 lockHolder.Release();
260 hr = pProcess->SendIPCEvent(pEvent, CorDBIPC_BUFFER_SIZE);
261 lockHolder.Acquire();
263 hr = WORST_HR(hr, pEvent->hr);
267 hr = CORDBHRFromProcessState(pProcess, pAppDomain);
270 pAppDomain->m_breakpoints.RemoveBase(LsPtrToCookie(GetLsPtrBP()));
277 void CordbFunctionBreakpoint::Disconnect()
282 /* ------------------------------------------------------------------------- *
284 * ------------------------------------------------------------------------- */
286 CordbStepper::CordbStepper(CordbThread *thread, CordbFrame *frame)
287 : CordbBase(thread->GetProcess(), 0, enumCordbStepper),
288 m_thread(thread), m_frame(frame),
289 m_stepperToken(0), m_active(false),
291 m_fIsJMCStepper(false),
292 m_rgfMappingStop(STOP_OTHER_UNMAPPED),
293 m_rgfInterceptStop(INTERCEPT_NONE)
297 HRESULT CordbStepper::QueryInterface(REFIID id, void **pInterface)
299 if (id == IID_ICorDebugStepper)
300 *pInterface = static_cast<ICorDebugStepper *>(this);
301 else if (id == IID_ICorDebugStepper2)
302 *pInterface = static_cast<ICorDebugStepper2 *>(this);
303 else if (id == IID_IUnknown)
304 *pInterface = static_cast<IUnknown *>(static_cast<ICorDebugStepper *>(this));
306 return E_NOINTERFACE;
312 HRESULT CordbStepper::SetRangeIL(BOOL bIL)
314 PUBLIC_API_ENTRY(this);
315 FAIL_IF_NEUTERED(this);
316 m_rangeIL = (bIL != FALSE);
321 HRESULT CordbStepper::SetJMC(BOOL fIsJMCStepper)
323 PUBLIC_API_ENTRY(this);
324 FAIL_IF_NEUTERED(this);
325 // Can't have JMC and stopping with anything else.
326 if (m_rgfMappingStop & STOP_ALL)
329 m_fIsJMCStepper = (fIsJMCStepper != FALSE);
333 HRESULT CordbStepper::IsActive(BOOL *pbActive)
335 PUBLIC_API_ENTRY(this);
336 FAIL_IF_NEUTERED(this);
337 VALIDATE_POINTER_TO_OBJECT(pbActive, BOOL *);
339 *pbActive = m_active;
344 // M_id is a ptr to the stepper in the LS process.
345 LSPTR_STEPPER CordbStepper::GetLsPtrStepper()
352 HRESULT CordbStepper::Deactivate()
354 PUBLIC_REENTRANT_API_ENTRY(this);
358 FAIL_IF_NEUTERED(this);
360 if (m_thread == NULL)
361 return CORDBG_E_PROCESS_TERMINATED;
364 CordbProcess *process = GetProcess();
365 ATT_ALLOW_LIVE_DO_STOPGO(process);
369 if (!m_active) // another thread may be deactivating (e.g. step complete event)
375 CordbAppDomain *pAppDomain = GetAppDomain();
376 _ASSERTE (pAppDomain != NULL);
378 DebuggerIPCEvent event;
379 process->InitIPCEvent(&event,
382 pAppDomain->GetADToken());
384 event.StepData.stepperToken = GetLsPtrStepper();
387 hr = process->SendIPCEvent(&event, sizeof(DebuggerIPCEvent));
388 hr = WORST_HR(hr, event.hr);
392 process->m_steppers.RemoveBase((ULONG_PTR)m_id);
400 HRESULT CordbStepper::SetInterceptMask(CorDebugIntercept mask)
402 PUBLIC_API_ENTRY(this);
403 FAIL_IF_NEUTERED(this);
404 m_rgfInterceptStop = mask;
408 HRESULT CordbStepper::SetUnmappedStopMask(CorDebugUnmappedStop mask)
410 PUBLIC_API_ENTRY(this);
411 FAIL_IF_NEUTERED(this);
413 // You must be Win32 attached to stop in unmanaged code.
414 if ((mask & STOP_UNMANAGED) && !GetProcess()->IsInteropDebugging())
417 // Limitations on JMC Stepping - if JMC stepping is active,
418 // all other stop masks must be disabled.
419 // The jit can't place JMC probes before the prolog, so if we're
420 // we're JMC stepping, we'll stop after the prolog.
421 // The implementation for JMC stepping also doesn't let us stop in
422 // unmanaged code. (because there are no probes there).
423 // So enforce those implementation limitations here.
430 // @todo- Ensure that we only set valid bits.
433 m_rgfMappingStop = mask;
437 HRESULT CordbStepper::Step(BOOL bStepIn)
439 PUBLIC_API_ENTRY(this);
440 FAIL_IF_NEUTERED(this);
441 ATT_REQUIRE_STOPPED_MAY_FAIL(GetProcess());
443 if (m_thread == NULL)
444 return CORDBG_E_PROCESS_TERMINATED;
446 return StepRange(bStepIn, NULL, 0);
449 //---------------------------------------------------------------------------------------
451 // Ships off a step-range command to the left-side. On the next continue the LS will
452 // step across one range at a time.
455 // fStepIn - TRUE if this stepper should execute a step-in, else FALSE
456 // rgRanges - Array of ranges that define a single step.
457 // cRanges - Count of number of elements in rgRanges.
460 // S_OK if the stepper is successfully set-up, else an appropriate error code.
462 HRESULT CordbStepper::StepRange(BOOL fStepIn,
463 COR_DEBUG_STEP_RANGE rgRanges[],
466 PUBLIC_REENTRANT_API_ENTRY(this);
467 FAIL_IF_NEUTERED(this);
468 VALIDATE_POINTER_TO_OBJECT_ARRAY_OR_NULL(rgRanges, COR_DEBUG_STEP_RANGE, cRanges, true, true);
470 ATT_REQUIRE_STOPPED_MAY_FAIL(GetProcess());
472 if (m_thread == NULL)
474 return CORDBG_E_PROCESS_TERMINATED;
482 // Deactivate the current stepping.
483 // or return an error???
493 // Validate step-ranges. Ranges are exclusive, so end offset
494 // should always be greater than start offset.
495 // Ranges don't have to be sorted.
496 // Zero ranges is ok; though they ought to just call Step() in that case.
497 for (ULONG32 i = 0; i < cRanges; i++)
499 if (rgRanges[i].startOffset >= rgRanges[i].endOffset)
501 STRESS_LOG2(LF_CORDB, LL_INFO10, "Illegal step range. 0x%x-0x%x\n", rgRanges[i].startOffset, rgRanges[i].endOffset);
502 return ErrWrapper(E_INVALIDARG);
506 CordbProcess * pProcess = GetProcess();
512 DebuggerIPCEvent * pEvent = reinterpret_cast<DebuggerIPCEvent *>(_alloca(CorDBIPC_BUFFER_SIZE));
514 pProcess->InitIPCEvent(pEvent, DB_IPCE_STEP, true, GetAppDomain()->GetADToken());
516 pEvent->StepData.vmThreadToken = m_thread->m_vmThreadToken;
517 pEvent->StepData.rgfMappingStop = m_rgfMappingStop;
518 pEvent->StepData.rgfInterceptStop = m_rgfInterceptStop;
519 pEvent->StepData.IsJMCStop = !!m_fIsJMCStepper;
524 pEvent->StepData.frameToken = LEAF_MOST_FRAME;
528 pEvent->StepData.frameToken = m_frame->GetFramePointer();
531 pEvent->StepData.stepIn = (fStepIn != 0);
532 pEvent->StepData.totalRangeCount = cRanges;
533 pEvent->StepData.rangeIL = m_rangeIL;
536 // Send ranges. We may have to send > 1 message.
539 COR_DEBUG_STEP_RANGE * pRangeStart = &(pEvent->StepData.range);
540 COR_DEBUG_STEP_RANGE * pRangeEnd = (reinterpret_cast<COR_DEBUG_STEP_RANGE *> (((BYTE *)pEvent) + CorDBIPC_BUFFER_SIZE)) - 1;
542 int cRangesToGo = cRanges;
546 while (cRangesToGo > 0)
549 // Find the number of ranges we can copy this time thru the loop
553 if (cRangesToGo < (pRangeEnd - pRangeStart))
555 cRangesToCopy = cRangesToGo;
559 cRangesToCopy = (unsigned int)(pRangeEnd - pRangeStart);
563 // Copy the ranges into the IPC block now, 1-by-1
565 int cRangesCopied = 0;
567 while (cRangesCopied != cRangesToCopy)
569 pRangeStart[cRangesCopied] = rgRanges[cRanges - cRangesToGo + cRangesCopied];
573 pEvent->StepData.rangeCount = cRangesCopied;
575 cRangesToGo -= cRangesCopied;
578 // Send step event (two-way event here...)
581 hr = pProcess->SendIPCEvent(pEvent, CorDBIPC_BUFFER_SIZE);
583 hr = WORST_HR(hr, pEvent->hr);
594 // Send step event without any ranges (two-way event here...)
597 hr = pProcess->SendIPCEvent(pEvent, CorDBIPC_BUFFER_SIZE);
599 hr = WORST_HR(hr, pEvent->hr);
607 m_id = LsPtrToCookie(pEvent->StepData.stepperToken);
609 LOG((LF_CORDB,LL_INFO10000, "CS::SR: m_id:0x%x | 0x%x \n",
611 LsPtrToCookie(pEvent->StepData.stepperToken)));
614 CordbAppDomain *pAppDomain = GetAppDomain();
616 _ASSERTE (pAppDomain != NULL);
620 pProcess->m_steppers.AddBase(this);
628 //---------------------------------------------------------------------------------------
630 // Ships off a step-out command to the left-side. On the next continue the LS will
631 // execute a step-out
634 // S_OK if the stepper is successfully set-up, else an appropriate error code.
636 HRESULT CordbStepper::StepOut()
638 PUBLIC_API_ENTRY(this);
639 FAIL_IF_NEUTERED(this);
640 ATT_REQUIRE_STOPPED_MAY_FAIL(GetProcess());
642 if (m_thread == NULL)
644 return CORDBG_E_PROCESS_TERMINATED;
652 // Deactivate the current stepping.
653 // or return an error???
664 CordbProcess * pProcess = GetProcess();
666 // We don't do native step-out.
667 if (pProcess->SupportsVersion(ver_ICorDebugProcess2))
669 if ((m_rgfMappingStop & STOP_UNMANAGED) != 0)
671 return ErrWrapper(CORDBG_E_CANT_INTEROP_STEP_OUT);
679 DebuggerIPCEvent * pEvent = (DebuggerIPCEvent *) _alloca(CorDBIPC_BUFFER_SIZE);
681 pProcess->InitIPCEvent(pEvent, DB_IPCE_STEP_OUT, true, GetAppDomain()->GetADToken());
683 pEvent->StepData.vmThreadToken = m_thread->m_vmThreadToken;
684 pEvent->StepData.rgfMappingStop = m_rgfMappingStop;
685 pEvent->StepData.rgfInterceptStop = m_rgfInterceptStop;
686 pEvent->StepData.IsJMCStop = !!m_fIsJMCStepper;
690 pEvent->StepData.frameToken = LEAF_MOST_FRAME;
694 pEvent->StepData.frameToken = m_frame->GetFramePointer();
697 pEvent->StepData.totalRangeCount = 0;
699 // Note: two-way event here...
700 hr = pProcess->SendIPCEvent(pEvent, CorDBIPC_BUFFER_SIZE);
702 hr = WORST_HR(hr, pEvent->hr);
709 m_id = LsPtrToCookie(pEvent->StepData.stepperToken);
712 CordbAppDomain * pAppDomain = GetAppDomain();
714 _ASSERTE (pAppDomain != NULL);
718 pProcess->m_steppers.AddBase(this);