forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdacdbiimplstackwalk.cpp
More file actions
1422 lines (1205 loc) · 51.1 KB
/
dacdbiimplstackwalk.cpp
File metadata and controls
1422 lines (1205 loc) · 51.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
// DacDbiImplStackWalk.cpp
//
//
// This file contains the implementation of the stackwalking-related functions on the DacDbiInterface.
//
// ======================================================================================
#include "stdafx.h"
#include "dacdbiinterface.h"
#include "dacdbiimpl.h"
#include "excepcpu.h"
#if defined(FEATURE_COMINTEROP)
#include "comtoclrcall.h"
#include "comcallablewrapper.h"
#endif // FEATURE_COMINTEROP
#ifdef FEATURE_INTERPRETER
#include "interpexec.h"
#endif // FEATURE_INTERPRETER
#include "exinfo.h"
typedef IDacDbiInterface::StackWalkHandle StackWalkHandle;
// Persistent data needed to do a stackwalk. This is allocated on the forDbi heap.
// It can survive across multiple DD calls.
// However, it has data structures that have raw pointers into the DAC cache, and so it must
// be re-iniatialized after each time the Dac cache is flushed.
struct StackWalkData
{
public:
StackWalkData(Thread * pThread, Frame * pFrame, ULONG32 flags) :
m_iterator(pThread, NULL, flags)
{ SUPPORTS_DAC;
}
// Unwrap a handle to get StackWalkData instance.
static StackWalkData * FromHandle(StackWalkHandle handle)
{
SUPPORTS_DAC;
_ASSERTE(handle != NULL);
return reinterpret_cast<StackWalkData *>(handle);
}
// The stackwalk iterator. This has lots of pointers into the DAC cache.
StackFrameIterator m_iterator;
// The context buffer, which can be pointed to by the RegDisplay.
T_CONTEXT m_context;
// A regdisplay used by the stackwalker.
REGDISPLAY m_regdisplay;
};
// Helper to allocate stackwalk datastructures for given parameters.
// This is allocated on the local heap (and not via the forDbi allocator on the dac-cache), and then
// freed via code:DacDbiInterfaceImpl::DeleteStackWalk
//
// Throws on error (mainly OOM).
void AllocateStackwalk(StackWalkHandle * pHandle, Thread * pThread, Frame * pFrame, ULONG32 flags)
{
SUPPORTS_DAC;
StackWalkData * p = new StackWalkData(pThread, NULL, flags); // throews
StackWalkHandle h = reinterpret_cast<StackWalkHandle>(p);
*pHandle = h;
}
void DeleteStackwalk(StackWalkHandle pHandle)
{
SUPPORTS_DAC;
StackWalkData * pBuffer = (StackWalkData *) pHandle;
_ASSERTE(pBuffer != NULL);
delete pBuffer;
}
// Helper to get the StackFrameIterator from a Stackwalker handle
StackFrameIterator * GetIteratorFromHandle(StackWalkHandle pSFIHandle)
{
SUPPORTS_DAC;
StackWalkData * pBuffer = StackWalkData::FromHandle(pSFIHandle);
return &(pBuffer->m_iterator);
}
// Helper to get a RegDisplay from a Stackwalker handle
REGDISPLAY * GetRegDisplayFromHandle(StackWalkHandle pSFIHandle)
{
SUPPORTS_DAC;
StackWalkData * pBuffer = StackWalkData::FromHandle(pSFIHandle);
return &(pBuffer->m_regdisplay);
}
// Helper to get a Context buffer from a Stackwalker handle
T_CONTEXT * GetContextBufferFromHandle(StackWalkHandle pSFIHandle)
{
SUPPORTS_DAC;
StackWalkData * pBuffer = StackWalkData::FromHandle(pSFIHandle);
return &(pBuffer->m_context);
}
// Create and return a stackwalker on the specified thread.
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::CreateStackWalk(VMPTR_Thread vmThread, DT_CONTEXT * pInternalContextBuffer, OUT StackWalkHandle * ppSFIHandle)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
_ASSERTE(ppSFIHandle != NULL);
Thread * pThread = vmThread.GetDacPtr();
// Set the stackwalk flags. We pretty much want to stop at everything.
DWORD dwFlags = (NOTIFY_ON_U2M_TRANSITIONS |
NOTIFY_ON_NO_FRAME_TRANSITIONS |
NOTIFY_ON_INITIAL_NATIVE_CONTEXT);
// allocate memory for various stackwalker buffers (StackFrameIterator, RegDisplay, Context)
AllocateStackwalk(ppSFIHandle, pThread, NULL, dwFlags);
// initialize the CONTEXT.
// SetStackWalk will initial the RegDisplay from this context.
IfFailThrow(GetContext(vmThread, pInternalContextBuffer));
// initialize the stackwalker
IfFailThrow(SetStackWalkCurrentContext(vmThread,
*ppSFIHandle,
SET_CONTEXT_FLAG_ACTIVE_FRAME,
pInternalContextBuffer));
}
EX_CATCH_HRESULT(hr);
return hr;
}
// Delete the stackwalk object allocated by code:AllocateStackwalk
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::DeleteStackWalk(StackWalkHandle ppSFIHandle)
{
HRESULT hr = S_OK;
EX_TRY
{
DeleteStackwalk(ppSFIHandle);
}
EX_CATCH_HRESULT(hr);
return hr;
}
// Get the CONTEXT of the current frame at which the stackwalker is stopped.
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::GetStackWalkCurrentContext(StackWalkHandle pSFIHandle, DT_CONTEXT * pContext)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
StackFrameIterator * pIter = GetIteratorFromHandle(pSFIHandle);
GetStackWalkCurrentContext(pIter, pContext);
}
EX_CATCH_HRESULT(hr);
return hr;
}
// Internal Worker for GetStackWalkCurrentContext().
void DacDbiInterfaceImpl::GetStackWalkCurrentContext(StackFrameIterator * pIter,
DT_CONTEXT * pContext)
{
// convert the current REGDISPLAY to a DT_CONTEXT
CrawlFrame * pCF = &(pIter->m_crawl);
T_CONTEXT tmpContext = { };
UpdateContextFromRegDisp(pCF->GetRegisterSet(), &tmpContext);
CopyMemory(pContext, &tmpContext, sizeof(*pContext));
#if defined(TARGET_X86) || defined(TARGET_AMD64)
pContext->ContextFlags &= ~(CONTEXT_XSTATE & CONTEXT_AREA_MASK);
#endif
}
// Set the stackwalker to the specified CONTEXT.
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::SetStackWalkCurrentContext(VMPTR_Thread vmThread, StackWalkHandle pSFIHandle, CorDebugSetContextFlag flag, DT_CONTEXT * pContext)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
StackFrameIterator * pIter = GetIteratorFromHandle(pSFIHandle);
REGDISPLAY * pRD = GetRegDisplayFromHandle(pSFIHandle);
#if defined(_DEBUG)
// The caller should have checked this already.
_ASSERTE(CheckContext(vmThread, pContext) == S_OK);
#endif // _DEBUG
// DD can't keep pointers back into the RS address space.
// Allocate a context in DDImpl's memory space. DDImpl can't contain raw pointers back into
// the client space since that may not marshal.
T_CONTEXT * pContext2 = GetContextBufferFromHandle(pSFIHandle);
CopyMemory(pContext2, pContext, sizeof(*pContext));
// update the REGDISPLAY with the given CONTEXT.
// Be sure that the context is in DDImpl's memory space and not the Right-sides.
FillRegDisplay(pRD, pContext2);
BOOL fSuccess = pIter->ResetRegDisp(pRD, (flag == SET_CONTEXT_FLAG_ACTIVE_FRAME));
if (!fSuccess)
{
// ResetRegDisp() may fail for the same reason Init() may fail, i.e.
// because the stackwalker tries to unwind one frame ahead of time,
// or because the stackwalker needs to filter out some frames based on the stackwalk flags.
ThrowHR(E_FAIL);
}
}
EX_CATCH_HRESULT(hr);
return hr;
}
// Unwind the stackwalker to the next frame.
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::UnwindStackWalkFrame(StackWalkHandle pSFIHandle, OUT BOOL * pResult)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
StackFrameIterator * pIter = GetIteratorFromHandle(pSFIHandle);
CrawlFrame * pCF = &(pIter->m_crawl);
// Declare variables up front so goto doesn't bypass initialization
DWORD cbStackParameterSize = 0;
BOOL fIsAtEndOfStack = TRUE;
if ((pIter->GetFrameState() == StackFrameIterator::SFITER_INITIAL_NATIVE_CONTEXT) ||
(pIter->GetFrameState() == StackFrameIterator::SFITER_NATIVE_MARKER_FRAME))
{
if (IsRuntimeUnwindableStub(GetControlPC(pCF->GetRegisterSet())))
{
// This is a native stack frame which the StackFrameIterator doesn't know how to unwind.
// Use our special unwind logic.
*pResult = UnwindRuntimeStackFrame(pIter);
goto Done;
}
}
// On x86, we need to adjust the stack pointer for the callee parameter adjustment.
// This requires us to save the number of bytes used for the stack parameters of the callee.
// Thus, let's save it here before we unwind.
if (pIter->GetFrameState() == StackFrameIterator::SFITER_FRAMELESS_METHOD)
{
cbStackParameterSize = GetStackParameterSize(pCF->GetCodeInfo());
}
// If the stackwalker is invalid to begin with, we'll just say that it is at the end of the stack.
while (pIter->IsValid())
{
StackWalkAction swa = pIter->Next();
if (swa == SWA_FAILED)
{
// The stackwalker is valid to begin with, so this must be a failure case.
ThrowHR(E_FAIL);
}
else if (swa == SWA_CONTINUE)
{
if (pIter->GetFrameState() == StackFrameIterator::SFITER_DONE)
{
// We are at the end of the stack. We will break at the end of the loop and fIsAtEndOfStack
// will be TRUE.
}
else if ((pIter->GetFrameState() == StackFrameIterator::SFITER_FRAME_FUNCTION) ||
(pIter->GetFrameState() == StackFrameIterator::SFITER_SKIPPED_FRAME_FUNCTION))
{
// If the stackwalker is stopped at an explicit frame, unwind directly to the next frame.
// The V3 stackwalker doesn't stop on explicit frames.
continue;
}
else if (pIter->GetFrameState() == StackFrameIterator::SFITER_NO_FRAME_TRANSITION)
{
// No frame transitions are not exposed in V2.
// Just continue onto the next managed stack frame.
continue;
}
else if (pIter->GetFrameState() == StackFrameIterator::SFITER_FRAMELESS_METHOD)
{
// Skip the new exception handling managed code, the debugger clients are not supposed to see them
MethodDesc *pMD = pIter->m_crawl.GetFunction();
// EH.DispatchEx, EH.RhThrowEx, EH.RhThrowHwEx, ExceptionServices.InternalCalls.SfiInit, ExceptionServices.InternalCalls.SfiNext
// and System.Runtime.StackFrameIterator.*
if (pMD->GetMethodTable() == g_pEHClass || pMD->GetMethodTable() == g_pExceptionServicesInternalCallsClass || pMD->GetMethodTable() == g_pStackFrameIteratorClass)
{
continue;
}
// Runtime-invoked UCO entrypoint method (Environment.CallEntryPoint)
if (pMD == g_pEnvironmentCallEntryPointMethodDesc)
{
continue;
}
fIsAtEndOfStack = FALSE;
}
else
{
fIsAtEndOfStack = FALSE;
}
}
else
{
UNREACHABLE();
}
// If we get here, then we want to stop at this current frame.
break;
}
if (fIsAtEndOfStack == FALSE)
{
// Currently the only case where we adjust the stack pointer is at M2U transitions.
if (pIter->GetFrameState() == StackFrameIterator::SFITER_NATIVE_MARKER_FRAME)
{
_ASSERTE(!pCF->IsActiveFrame());
AdjustRegDisplayForStackParameter(pCF->GetRegisterSet(),
cbStackParameterSize,
pCF->IsActiveFrame(),
kFromManagedToUnmanaged);
}
}
*pResult = (fIsAtEndOfStack == FALSE);
Done: ;
}
EX_CATCH_HRESULT(hr);
return hr;
}
bool g_fSkipStackCheck = false;
bool g_fSkipStackCheckInit = false;
// Check whether the specified CONTEXT is valid. The only check we perform right now is whether the
// SP in the specified CONTEXT is in the stack range of the thread.
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::CheckContext(VMPTR_Thread vmThread,
const DT_CONTEXT * pContext)
{
DD_ENTER_MAY_THROW;
// If the SP in the CONTEXT isn't valid, then there's no point in checking.
if ((pContext->ContextFlags & CONTEXT_CONTROL) == 0)
{
return S_OK;
}
if (!g_fSkipStackCheckInit)
{
g_fSkipStackCheck = (CLRConfig::GetConfigValue(CLRConfig::UNSUPPORTED_DbgSkipStackCheck) != 0);
g_fSkipStackCheckInit = true;
}
// Skip this check if the customer has set the reg key/env var. This is necessary for AutoCad. They
// enable fiber mode by calling the Win32 API ConvertThreadToFiber(), but when a managed debugger is
// attached, they don't actually call into our hosting APIs such as SwitchInLogicalThreadState(). This
// leads to the cached stack range on the Thread object being stale.
if (!g_fSkipStackCheck)
{
// We don't have the backing store boundaries stored on the thread, but this is just
// a sanity check anyway.
Thread * pThread = vmThread.GetDacPtr();
PTR_VOID sp = GetSP(reinterpret_cast<const T_CONTEXT *>(pContext));
if ((sp < pThread->GetCachedStackLimit()) || (pThread->GetCachedStackBase() <= sp))
{
return CORDBG_E_NON_MATCHING_CONTEXT;
}
}
return S_OK;
}
// Retrieve information about the current frame from the stackwalker.
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::GetStackWalkCurrentFrameInfo(StackWalkHandle pSFIHandle, OPTIONAL DebuggerIPCE_STRData * pFrameData, OUT FrameType * pRetVal)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
_ASSERTE(pSFIHandle != NULL);
StackFrameIterator * pIter = GetIteratorFromHandle(pSFIHandle);
FrameType ftResult = kInvalid;
if (pIter->GetFrameState() == StackFrameIterator::SFITER_DONE)
{
_ASSERTE(!pIter->IsValid());
ftResult = kAtEndOfStack;
}
else
{
BOOL fInitFrameData = FALSE;
switch (pIter->GetFrameState())
{
case StackFrameIterator::SFITER_UNINITIALIZED:
ftResult = kInvalid;
break;
case StackFrameIterator::SFITER_FRAMELESS_METHOD:
{
MethodDesc *pMD = pIter->m_crawl.GetFunction();
// EH.DispatchEx, EH.RhThrowEx, EH.RhThrowHwEx, ExceptionServices.InternalCalls.SfiInit, ExceptionServices.InternalCalls.SfiNext
if (pMD->GetMethodTable() == g_pEHClass || pMD->GetMethodTable() == g_pExceptionServicesInternalCallsClass)
{
ftResult = kManagedExceptionHandlingCodeFrame;
}
// Runtime-invoked UCO entrypoint method (Environment.CallEntryPoint)
else if (pMD == g_pEnvironmentCallEntryPointMethodDesc)
{
ftResult = kRuntimeEntryPointFrame;
}
else
{
ftResult = kManagedStackFrame;
fInitFrameData = TRUE;
}
}
break;
case StackFrameIterator::SFITER_FRAME_FUNCTION:
//
// fall through
//
case StackFrameIterator::SFITER_SKIPPED_FRAME_FUNCTION:
ftResult = kExplicitFrame;
fInitFrameData = TRUE;
break;
case StackFrameIterator::SFITER_NO_FRAME_TRANSITION:
// no-frame transition represents an ExInfo for a native exception on x86.
// For all intents and purposes this should be treated just like another explicit frame.
ftResult = kExplicitFrame;
fInitFrameData = TRUE;
break;
case StackFrameIterator::SFITER_NATIVE_MARKER_FRAME:
//
// fall through
//
case StackFrameIterator::SFITER_INITIAL_NATIVE_CONTEXT:
if (IsRuntimeUnwindableStub(GetControlPC(pIter->m_crawl.GetRegisterSet())))
{
ftResult = kNativeRuntimeUnwindableStackFrame;
fInitFrameData = TRUE;
}
else
{
ftResult = kNativeStackFrame;
}
break;
default:
UNREACHABLE();
}
if ((fInitFrameData == TRUE) && (pFrameData != NULL))
{
InitFrameData(pIter, ftResult, pFrameData);
}
}
*pRetVal = ftResult;
}
EX_CATCH_HRESULT(hr);
return hr;
}
//---------------------------------------------------------------------------------------
//
// Return the number of internal frames on the specified thread.
//
// Arguments:
// vmThread - the thread to be walked
//
// Return Value:
// Return the number of interesting internal frames on the thread.
//
// Notes:
// Internal frames are interesting if they are not of type STUBFRAME_NONE.
//
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::GetCountOfInternalFrames(VMPTR_Thread vmThread, OUT ULONG32 * pRetVal)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
Thread * pThread = vmThread.GetDacPtr();
Frame * pFrame = pThread->GetFrame();
// We could call EnumerateInternalFrames() here, but it would be a lot of overhead for what we need.
ULONG32 uCount = 0;
while (pFrame != FRAME_TOP)
{
if (InlinedCallFrame::FrameHasActiveCall(pFrame))
{
// Skip new exception handling helpers
InlinedCallFrame *pInlinedCallFrame = dac_cast<PTR_InlinedCallFrame>(pFrame);
PTR_PInvokeMethodDesc pMD = pInlinedCallFrame->m_Datum;
TADDR datum = dac_cast<TADDR>(pMD);
if ((datum & (TADDR)InlinedCallFrameMarker::Mask) == (TADDR)InlinedCallFrameMarker::ExceptionHandlingHelper)
{
pFrame = pFrame->Next();
continue;
}
}
#ifdef FEATURE_INTERPRETER
if (pFrame->GetFrameIdentifier() == FrameIdentifier::InterpreterFrame)
{
// Skip InterpreterFrame
pFrame = pFrame->Next();
continue;
}
#endif // FEATURE_INTERPRETER
CorDebugInternalFrameType ift = GetInternalFrameType(pFrame);
if (ift != STUBFRAME_NONE)
{
uCount++;
}
pFrame = pFrame->Next();
}
*pRetVal = uCount;
}
EX_CATCH_HRESULT(hr);
return hr;
}
//---------------------------------------------------------------------------------------
//
// Enumerate the internal frames on the specified thread and invoke the provided callback on each of them.
//
// Arguments:
// vmThread - the thread to be walked
// fpCallback - callback function to be invoked for each interesting internal frame
// pUserData - user-defined custom data to be passed to the callback
//
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::EnumerateInternalFrames(VMPTR_Thread vmThread, FP_INTERNAL_FRAME_ENUMERATION_CALLBACK fpCallback, CALLBACK_DATA pUserData)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
DebuggerIPCE_STRData frameData;
Thread * pThread = vmThread.GetDacPtr();
Frame * pFrame = pThread->GetFrame();
AppDomain * pAppDomain = AppDomain::GetCurrentDomain();
// This used to be only true for Enter-Managed chains.
// Since we don't have chains anymore, this can always be false.
frameData.quicklyUnwound = false;
frameData.eType = DebuggerIPCE_STRData::cStubFrame;
while (pFrame != FRAME_TOP)
{
if (InlinedCallFrame::FrameHasActiveCall(pFrame))
{
// Skip new exception handling helpers
InlinedCallFrame *pInlinedCallFrame = dac_cast<PTR_InlinedCallFrame>(pFrame);
PTR_PInvokeMethodDesc pMD = pInlinedCallFrame->m_Datum;
TADDR datum = dac_cast<TADDR>(pMD);
if ((datum & (TADDR)InlinedCallFrameMarker::Mask) == (TADDR)InlinedCallFrameMarker::ExceptionHandlingHelper)
{
pFrame = pFrame->Next();
continue;
}
}
#ifdef FEATURE_INTERPRETER
if (pFrame->GetFrameIdentifier() == FrameIdentifier::InterpreterFrame)
{
// Skip InterpreterFrame
pFrame = pFrame->Next();
continue;
}
#endif // FEATURE_INTERPRETER
// check if the internal frame is interesting
frameData.stubFrame.frameType = GetInternalFrameType(pFrame);
if (frameData.stubFrame.frameType != STUBFRAME_NONE)
{
frameData.fp = FramePointer::MakeFramePointer(PTR_HOST_TO_TADDR(pFrame));
frameData.vmCurrentAppDomainToken.SetHostPtr(pAppDomain);
MethodDesc * pMD = pFrame->GetFunction();
Module * pModule = (pMD ? pMD->GetModule() : NULL);
DomainAssembly * pDomainAssembly = (pModule ? pModule->GetDomainAssembly() : NULL);
if (frameData.stubFrame.frameType == STUBFRAME_FUNC_EVAL)
{
FuncEvalFrame * pFEF = dac_cast<PTR_FuncEvalFrame>(pFrame);
DebuggerEval * pDE = pFEF->GetDebuggerEval();
frameData.stubFrame.funcMetadataToken = pDE->m_methodToken;
frameData.stubFrame.vmDomainAssembly.SetHostPtr(
pDE->m_debuggerModule ? pDE->m_debuggerModule->GetDomainAssembly() : NULL);
frameData.stubFrame.vmMethodDesc = VMPTR_MethodDesc::NullPtr();
}
else
{
frameData.stubFrame.funcMetadataToken = (pMD == NULL ? mdTokenNil : pMD->GetMemberDef());
frameData.stubFrame.vmDomainAssembly.SetHostPtr(pDomainAssembly);
frameData.stubFrame.vmMethodDesc.SetHostPtr(pMD);
}
// invoke the callback
fpCallback(&frameData, pUserData);
}
// move on to the next internal frame
pFrame = pFrame->Next();
}
}
EX_CATCH_HRESULT(hr);
return hr;
}
// Given the FramePointer of the parent frame and the FramePointer of the current frame,
// check if the current frame is the parent frame.
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::IsMatchingParentFrame(FramePointer fpToCheck, FramePointer fpParent, OUT BOOL * pResult)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
StackFrame sfToCheck = StackFrame((UINT_PTR)fpToCheck.GetSPValue());
StackFrame sfParent = StackFrame((UINT_PTR)fpParent.GetSPValue());
// Ask the ExInfo to figure out the answer.
// Don't try to compare the StackFrames/FramePointers ourselves.
*pResult = ExInfo::IsUnwoundToTargetParentFrame(sfToCheck, sfParent);
}
EX_CATCH_HRESULT(hr);
return hr;
}
// Return the stack parameter size of the given method.
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::GetStackParameterSize(CORDB_ADDRESS controlPC, OUT ULONG32 * pRetVal)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
PCODE currentPC = PCODE(controlPC);
EECodeInfo codeInfo(currentPC);
*pRetVal = GetStackParameterSize(&codeInfo);
}
EX_CATCH_HRESULT(hr);
return hr;
}
// Return the FramePointer of the current frame at which the stackwalker is stopped.
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::GetFramePointer(StackWalkHandle pSFIHandle, OUT FramePointer * pRetVal)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
StackFrameIterator * pIter = GetIteratorFromHandle(pSFIHandle);
*pRetVal = GetFramePointerWorker(pIter);
}
EX_CATCH_HRESULT(hr);
return hr;
}
// Internal helper for GetFramePointer.
FramePointer DacDbiInterfaceImpl::GetFramePointerWorker(StackFrameIterator * pIter)
{
CrawlFrame * pCF = &(pIter->m_crawl);
REGDISPLAY * pRD = pCF->GetRegisterSet();
FramePointer fp;
switch (pIter->GetFrameState())
{
// For managed methods, we have the full CONTEXT. Additionally, we also have the caller CONTEXT
// on WIN64.
case StackFrameIterator::SFITER_FRAMELESS_METHOD:
fp = FramePointer::MakeFramePointer(GetRegdisplayStackMark(pRD));
break;
// In these cases, we only have the full CONTEXT, not the caller CONTEXT.
case StackFrameIterator::SFITER_NATIVE_MARKER_FRAME:
//
// fall through
//
case StackFrameIterator::SFITER_INITIAL_NATIVE_CONTEXT:
fp = FramePointer::MakeFramePointer(GetRegdisplayStackMark(pRD));
break;
// In these cases, we use the address of the explicit frame as the frame marker.
case StackFrameIterator::SFITER_FRAME_FUNCTION:
//
// fall through
//
case StackFrameIterator::SFITER_SKIPPED_FRAME_FUNCTION:
fp = FramePointer::MakeFramePointer(PTR_HOST_TO_TADDR(pCF->GetFrame()));
break;
// No-frame transition represents an ExInfo for a native exception on x86.
// For all intents and purposes this should be treated just like another explicit frame.
case StackFrameIterator::SFITER_NO_FRAME_TRANSITION:
fp = FramePointer::MakeFramePointer(pCF->GetNoFrameTransitionMarker());
break;
case StackFrameIterator::SFITER_UNINITIALIZED:
//
// fall through
//
default:
UNREACHABLE();
}
return fp;
}
// Return TRUE if the specified CONTEXT is the CONTEXT of the leaf frame.
// @dbgtodo filter CONTEXT - Currently we check for the filter CONTEXT first.
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::IsLeafFrame(VMPTR_Thread vmThread, const DT_CONTEXT * pContext, OUT BOOL * pResult)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
DT_CONTEXT ctxLeaf;
IfFailThrow(GetContext(vmThread, &ctxLeaf));
// Call a platform-specific helper to compare the two contexts.
*pResult = CompareControlRegisters(pContext, &ctxLeaf);
}
EX_CATCH_HRESULT(hr);
return hr;
}
// This is a simple helper function to convert a CONTEXT to a DebuggerREGDISPLAY. We need to do this
// inside DDI because the RS has no notion of REGDISPLAY.
HRESULT STDMETHODCALLTYPE DacDbiInterfaceImpl::ConvertContextToDebuggerRegDisplay(const DT_CONTEXT * pInContext, DebuggerREGDISPLAY * pOutDRD, BOOL fActive)
{
DD_ENTER_MAY_THROW;
HRESULT hr = S_OK;
EX_TRY
{
// This is a bit cumbersome. First we need to convert the CONTEXT into a REGDISPLAY. Then we need
// to convert the REGDISPLAY to a DebuggerREGDISPLAY.
T_CONTEXT tmpContext = { };
CopyMemory(&tmpContext, pInContext, sizeof(*pInContext));
REGDISPLAY rd;
FillRegDisplay(&rd, &tmpContext);
SetDebuggerREGDISPLAYFromREGDISPLAY(pOutDRD, &rd);
}
EX_CATCH_HRESULT(hr);
return hr;
}
//---------------------------------------------------------------------------------------
//
// Fill in the structure with information about the current frame at which the stackwalker is stopped.
//
// Arguments:
// pIter - the stackwalker
// pFrameData - the structure to be filled out
//
void DacDbiInterfaceImpl::InitFrameData(StackFrameIterator * pIter,
FrameType ft,
DebuggerIPCE_STRData * pFrameData)
{
CrawlFrame * pCF = &(pIter->m_crawl);
//
// do common initialization of DebuggerIPCE_STRData for both managed stack frames and explicit frames
//
pFrameData->fp = GetFramePointerWorker(pIter);
// This used to be only true for Enter-Managed chains.
// Since we don't have chains anymore, this can always be false.
pFrameData->quicklyUnwound = false;
pFrameData->vmCurrentAppDomainToken.SetHostPtr(AppDomain::GetCurrentDomain());
if (ft == kNativeRuntimeUnwindableStackFrame)
{
pFrameData->eType = DebuggerIPCE_STRData::cRuntimeNativeFrame;
GetStackWalkCurrentContext(pIter, &(pFrameData->ctx));
}
else if (ft == kManagedStackFrame)
{
MethodDesc * pMD = pCF->GetFunction();
Module * pModule = (pMD ? pMD->GetModule() : NULL);
// Although MiniDumpNormal tries to dump all AppDomains, it's possible
// target corruption will keep one from being present. This should mean
// we'll just fail later, but struggle on for now.
DomainAssembly *pDomainAssembly = NULL;
EX_TRY_ALLOW_DATATARGET_MISSING_MEMORY
{
pDomainAssembly = (pModule ? pModule->GetDomainAssembly() : NULL);
_ASSERTE(pDomainAssembly != NULL);
}
EX_END_CATCH_ALLOW_DATATARGET_MISSING_MEMORY
//
// This is a managed stack frame.
//
_ASSERTE(pMD != NULL);
_ASSERTE(pModule != NULL);
//
// initialize the rest of the DebuggerIPCE_STRData
//
pFrameData->eType = DebuggerIPCE_STRData::cMethodFrame;
SetDebuggerREGDISPLAYFromREGDISPLAY(&(pFrameData->rd), pCF->GetRegisterSet());
GetStackWalkCurrentContext(pIter, &(pFrameData->ctx));
//
// initialize the fields in DebuggerIPCE_STRData::v
//
// These fields will be filled in later. We don't have the sequence point mapping information here.
pFrameData->v.ILOffset = (SIZE_T)(-1);
pFrameData->v.mapping = MAPPING_NO_INFO;
// Check if this is a vararg method by getting the managed calling convention from the signature.
// Strictly speaking, we can do this in CordbJITILFrame::Init(), but it's just easier and more
// efficiently to do it here. CordbJITILFrame::Init() will initialize the other vararg-related
// fields. We don't have the native var info here to fully initialize everything.
pFrameData->v.fVarArgs = pMD->IsVarArg();
// Check if this is a NoMetadata method or if the method should be hidden.
// These methods should not be visible in the debugger both for convenience and
// because they don't have backing metadata. For more information see comments in
// MethodDesc::IsNoMetadata and MethodDesc::IsDiagnosticsHidden.
pFrameData->v.fNoMetadata = pMD->IsNoMetadata() || pMD->IsDiagnosticsHidden();
pFrameData->v.taAmbientESP = pCF->GetAmbientSPFromCrawlFrame();
if (pMD->IsSharedByGenericInstantiations())
{
// This method has a generic type token which is required to figure out the exact instantiation
// of the method. CrawlFrame::GetExactGenericArgsToken() can't always successfully retrieve
// the token because the JIT doesn't generate the required information all the time. As such,
// we need to save the variable index of the generic type token in order to do the look up later.
ALLOW_DATATARGET_MISSING_MEMORY(
pFrameData->v.exactGenericArgsToken = (GENERICS_TYPE_TOKEN)(dac_cast<TADDR>(pCF->GetExactGenericArgsToken()));
);
if (pMD->AcquiresInstMethodTableFromThis())
{
// The generic type token is the "this" object.
pFrameData->v.dwExactGenericArgsTokenIndex = 0;
}
else
{
// The generic type token is one of the secret arguments.
pFrameData->v.dwExactGenericArgsTokenIndex = (DWORD)ICorDebugInfo::TYPECTXT_ILNUM;
}
}
else
{
pFrameData->v.exactGenericArgsToken = (GENERICS_TYPE_TOKEN)NULL;
pFrameData->v.dwExactGenericArgsTokenIndex = (DWORD)ICorDebugInfo::MAX_ILNUM;
}
//
// initialize the DebuggerIPCE_FuncData and DebuggerIPCE_JITFuncData
//
DebuggerIPCE_FuncData * pFuncData = &(pFrameData->v.funcData);
DebuggerIPCE_JITFuncData * pJITFuncData = &(pFrameData->v.jitFuncData);
//
// initialize the "easy" fields of DebuggerIPCE_FuncData
//
pFuncData->funcMetadataToken = pMD->GetMemberDef();
pFuncData->vmDomainAssembly.SetHostPtr(pDomainAssembly);
// PERF: this is expensive to get so I stopped fetching it eagerly
// It is only needed if we haven't already got a cached copy
pFuncData->classMetadataToken = mdTokenNil;
//
// initialize the remaining fields of DebuggerIPCE_FuncData to the default values
//
pFuncData->ilStartAddress = NULL;
pFuncData->ilSize = 0;
pFuncData->currentEnCVersion = CorDB_DEFAULT_ENC_FUNCTION_VERSION;
pFuncData->localVarSigToken = mdSignatureNil;
//
// inititalize the fields of DebuggerIPCE_JITFuncData
//
// For MiniDumpNormal, we do not guarantee method region info for all JIT tokens
// is present in the dump.
ALLOW_DATATARGET_MISSING_MEMORY(
pJITFuncData->nativeStartAddressPtr = PCODEToPINSTR(pCF->GetCodeInfo()->GetStartAddress());
);
// PERF: this is expensive to get so I stopped fetching it eagerly
// It is only needed if we haven't already got a cached copy
pJITFuncData->nativeHotSize = 0;
pJITFuncData->nativeStartAddressColdPtr = 0;
pJITFuncData->nativeColdSize = 0;
pJITFuncData->nativeOffset = pCF->GetRelOffset();
// Here we detect (and set the appropriate flag) if the nativeOffset in the current frame points to the return address of IL_Throw()
// (or other exception related JIT helpers like IL_Throw, IL_Rethrow etc).
// Since return address point to the next(!) instruction after [call IL_Throw] this sometimes can lead to incorrect exception stacktraces
// where a next source line is spotted as an exception origin. This happens when the next instruction after [call IL_Throw] belongs to
// a sequence point and a source line different from a sequence point and a source line of [call IL_Throw].
// Later on this flag is used in order to adjust nativeOffset and make ICorDebugILFrame::GetIP return IL offset within
// the same sequence point as an actual IL throw instruction.
// Here is how we detect it:
// We can assume that nativeOffset points to an the instruction after [call IL_Throw] when these conditions are met:
// 1. pCF->IsInterrupted() - Exception has been thrown by this managed frame (frame attr FRAME_ATTR_EXCEPTION)
// 2. !pCF->HasFaulted() - It wasn't a "hardware" exception (Access violation, dev by 0, etc.)
// 3. !pCF->IsIPadjusted() - It hasn't been previously adjusted to point to [call IL_Throw]
// 4. pJITFuncData->nativeOffset != 0 - nativeOffset contains something that looks like a real return address.
pJITFuncData->justAfterILThrow = pCF->IsInterrupted()
&& !pCF->HasFaulted()
&& !pCF->IsIPadjusted()
&& pJITFuncData->nativeOffset != 0;
pJITFuncData->nativeCodeJITInfoToken.Set(NULL);
pJITFuncData->vmNativeCodeMethodDescToken.SetHostPtr(pMD);
InitParentFrameInfo(pCF, pJITFuncData);
ALLOW_DATATARGET_MISSING_MEMORY(
pJITFuncData->isInstantiatedGeneric = pMD->HasClassOrMethodInstantiation();
);
pJITFuncData->enCVersion = CorDB_DEFAULT_ENC_FUNCTION_VERSION;
// PERF: this is expensive to get so I stopped fetching it eagerly
// It is only needed if we haven't already got a cached copy
pFuncData->localVarSigToken = 0;
pFuncData->ilStartAddress = 0;
pFuncData->ilSize = 0;
// See the comment for LookupEnCVersions().
// PERF: this is expensive to get so I stopped fetching it eagerly
pFuncData->currentEnCVersion = 0;
pJITFuncData->enCVersion = 0;
}
else
{