This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
util.cpp
2981 lines (2480 loc) · 87.8 KB
/
util.cpp
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.
// See the LICENSE file in the project root for more information.
// ===========================================================================
// File: UTIL.CPP
//
// ===========================================================================
#include "common.h"
#include "excep.h"
#include "corhost.h"
#include "eventtrace.h"
#include "posterror.h"
#include "eemessagebox.h"
#include <shlobj.h>
#include "dlwrap.h"
#ifndef DACCESS_COMPILE
// Helper function that encapsulates the parsing rules.
//
// Called first with *pdstout == NULL to figure out how many args there are
// and the size of the required destination buffer.
//
// Called again with a nonnull *pdstout to fill in the actual buffer.
//
// Returns the # of arguments.
static UINT ParseCommandLine(LPCWSTR psrc, __inout LPWSTR *pdstout)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
FORBID_FAULT;
}
CONTRACTL_END
UINT argcount = 1; // discovery of arg0 is unconditional, below
LPWSTR pdst = *pdstout;
BOOL fDoWrite = (pdst != NULL);
BOOL fInQuotes;
int iSlash;
/* A quoted program name is handled here. The handling is much
simpler than for other arguments. Basically, whatever lies
between the leading double-quote and next one, or a terminal null
character is simply accepted. Fancier handling is not required
because the program name must be a legal NTFS/HPFS file name.
Note that the double-quote characters are not copied, nor do they
contribute to numchars.
This "simplification" is necessary for compatibility reasons even
though it leads to mishandling of certain cases. For example,
"c:\tests\"test.exe will result in an arg0 of c:\tests\ and an
arg1 of test.exe. In any rational world this is incorrect, but
we need to preserve compatibility.
*/
LPCWSTR pStart = psrc;
BOOL skipQuote = FALSE;
if (*psrc == W('\"'))
{
// scan from just past the first double-quote through the next
// double-quote, or up to a null, whichever comes first
while ((*(++psrc) != W('\"')) && (*psrc != W('\0')))
continue;
skipQuote = TRUE;
}
else
{
/* Not a quoted program name */
while (!ISWWHITE(*psrc) && *psrc != W('\0'))
psrc++;
}
// We have now identified arg0 as pStart (or pStart+1 if we have a leading
// quote) through psrc-1 inclusive
if (skipQuote)
pStart++;
while (pStart < psrc)
{
if (fDoWrite)
*pdst = *pStart;
pStart++;
pdst++;
}
// And terminate it.
if (fDoWrite)
*pdst = W('\0');
pdst++;
// if we stopped on a double-quote when arg0 is quoted, skip over it
if (skipQuote && *psrc == W('\"'))
psrc++;
while ( *psrc != W('\0'))
{
LEADINGWHITE:
// The outofarg state.
while (ISWWHITE(*psrc))
psrc++;
if (*psrc == W('\0'))
break;
else
if (*psrc == W('#'))
{
while (*psrc != W('\0') && *psrc != W('\n'))
psrc++; // skip to end of line
goto LEADINGWHITE;
}
argcount++;
fInQuotes = FALSE;
while ((!ISWWHITE(*psrc) || fInQuotes) && *psrc != W('\0'))
{
switch (*psrc)
{
case W('\\'):
iSlash = 0;
while (*psrc == W('\\'))
{
iSlash++;
psrc++;
}
if (*psrc == W('\"'))
{
for ( ; iSlash >= 2; iSlash -= 2)
{
if (fDoWrite)
*pdst = W('\\');
pdst++;
}
if (iSlash & 1)
{
if (fDoWrite)
*pdst = *psrc;
psrc++;
pdst++;
}
else
{
fInQuotes = !fInQuotes;
psrc++;
}
}
else
for ( ; iSlash > 0; iSlash--)
{
if (fDoWrite)
*pdst = W('\\');
pdst++;
}
break;
case W('\"'):
fInQuotes = !fInQuotes;
psrc++;
break;
default:
if (fDoWrite)
*pdst = *psrc;
psrc++;
pdst++;
}
}
if (fDoWrite)
*pdst = W('\0');
pdst++;
}
_ASSERTE(*psrc == W('\0'));
*pdstout = pdst;
return argcount;
}
// Function to parse apart a command line and return the
// arguments just like argv and argc
// This function is a little funky because of the pointer work
// but it is neat because it allows the recipient of the char**
// to only have to do a single delete []
LPWSTR* CommandLineToArgvW(__in LPWSTR lpCmdLine, DWORD *pNumArgs)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
INJECT_FAULT(return NULL;);
}
CONTRACTL_END
DWORD argcount = 0;
LPWSTR retval = NULL;
LPWSTR *pslot;
// First we need to find out how many strings there are in the command line
_ASSERTE(lpCmdLine);
_ASSERTE(pNumArgs);
LPWSTR pdst = NULL;
argcount = ParseCommandLine(lpCmdLine, &pdst);
// This check is because on WinCE the Application Name is not passed in as an argument to the app!
if (argcount == 0)
{
*pNumArgs = 0;
return NULL;
}
// Now we need alloc a buffer the size of the command line + the number of strings * DWORD
retval = new (nothrow) WCHAR[(argcount*sizeof(WCHAR*))/sizeof(WCHAR) + (pdst - (LPWSTR)NULL)];
if(!retval)
return NULL;
pdst = (LPWSTR)( argcount*sizeof(LPWSTR*) + (BYTE*)retval );
ParseCommandLine(lpCmdLine, &pdst);
pdst = (LPWSTR)( argcount*sizeof(LPWSTR*) + (BYTE*)retval );
pslot = (LPWSTR*)retval;
for (DWORD i = 0; i < argcount; i++)
{
*(pslot++) = pdst;
while (*pdst != W('\0'))
{
pdst++;
}
pdst++;
}
*pNumArgs = argcount;
return (LPWSTR*)retval;
}
//************************************************************************
// CQuickHeap
//
// A fast non-multithread-safe heap for short term use.
// Destroying the heap frees all blocks allocated from the heap.
// Blocks cannot be freed individually.
//
// The heap uses COM+ exceptions to report errors.
//
// The heap does not use any internal synchronization so it is not
// multithreadsafe.
//************************************************************************
CQuickHeap::CQuickHeap()
{
LIMITED_METHOD_CONTRACT;
m_pFirstQuickBlock = NULL;
m_pFirstBigQuickBlock = NULL;
m_pNextFree = NULL;
}
CQuickHeap::~CQuickHeap()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
FORBID_FAULT;
}
CONTRACTL_END
QuickBlock *pQuickBlock = m_pFirstQuickBlock;
while (pQuickBlock) {
QuickBlock *ptmp = pQuickBlock;
pQuickBlock = pQuickBlock->m_next;
delete [] (BYTE*)ptmp;
}
pQuickBlock = m_pFirstBigQuickBlock;
while (pQuickBlock) {
QuickBlock *ptmp = pQuickBlock;
pQuickBlock = pQuickBlock->m_next;
delete [] (BYTE*)ptmp;
}
}
LPVOID CQuickHeap::Alloc(UINT sz)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
INJECT_FAULT(COMPlusThrowOM(););
} CONTRACTL_END;
sz = (sz+7) & ~7;
if ( sz > kBlockSize ) {
QuickBlock *pQuickBigBlock = (QuickBlock*) new BYTE[sz + sizeof(QuickBlock) - 1];
pQuickBigBlock->m_next = m_pFirstBigQuickBlock;
m_pFirstBigQuickBlock = pQuickBigBlock;
return pQuickBigBlock->m_bytes;
} else {
if (m_pNextFree == NULL || sz > (UINT)( &(m_pFirstQuickBlock->m_bytes[kBlockSize]) - m_pNextFree )) {
QuickBlock *pQuickBlock = (QuickBlock*) new BYTE[kBlockSize + sizeof(QuickBlock) - 1];
pQuickBlock->m_next = m_pFirstQuickBlock;
m_pFirstQuickBlock = pQuickBlock;
m_pNextFree = pQuickBlock->m_bytes;
}
LPVOID pv = m_pNextFree;
m_pNextFree += sz;
return pv;
}
}
//----------------------------------------------------------------------------
// Output functions that avoid the crt's.
//----------------------------------------------------------------------------
static
void NPrintToHandleA(HANDLE Handle, const char *pszString, size_t BytesToWrite)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
FORBID_FAULT;
}
CONTRACTL_END
if (Handle == INVALID_HANDLE_VALUE || Handle == NULL)
return;
BOOL success;
DWORD dwBytesWritten;
const size_t maxWriteFileSize = 32767; // This is somewhat arbitrary limit, but 2**16-1 doesn't work
while (BytesToWrite > 0) {
DWORD dwChunkToWrite = (DWORD) min(BytesToWrite, maxWriteFileSize);
// No CharNextExA on CoreSystem, we just assume no multi-byte characters (this code path shouldn't be
// used in the production codepath for currently supported CoreSystem based products anyway).
#ifndef FEATURE_CORESYSTEM
if (dwChunkToWrite < BytesToWrite) {
break;
// must go by char to find biggest string that will fit, taking DBCS chars into account
//dwChunkToWrite = 0;
//const char *charNext = pszString;
//while (dwChunkToWrite < maxWriteFileSize-2 && charNext) {
// charNext = CharNextExA(0, pszString+dwChunkToWrite, 0);
// dwChunkToWrite = (DWORD)(charNext - pszString);
//}
//if (dwChunkToWrite == 0)
// break;
}
#endif // !FEATURE_CORESYSTEM
// Try to write to handle. If this is not a CUI app, then this is probably
// not going to work unless the dev took special pains to set their own console
// handle during CreateProcess. So try it, but don't yell if it doesn't work in
// that case. Also, if we redirect stdout to a pipe then the pipe breaks (ie, we
// write to something like the UNIX head command), don't complain.
success = WriteFile(Handle, pszString, dwChunkToWrite, &dwBytesWritten, NULL);
if (!success)
{
#if defined(_DEBUG)
// This can happen if stdout is a closed pipe. This might not help
// much, but we'll have half a chance of seeing this.
OutputDebugStringA("CLR: Writing out an unhandled exception to stdout failed!\n");
OutputDebugStringA(pszString);
#endif //_DEBUG
break;
}
else {
_ASSERTE(dwBytesWritten == dwChunkToWrite);
}
pszString = pszString + dwChunkToWrite;
BytesToWrite -= dwChunkToWrite;
}
}
static
void PrintToHandleA(HANDLE Handle, const char *pszString)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
FORBID_FAULT;
}
CONTRACTL_END
size_t len = strlen(pszString);
NPrintToHandleA(Handle, pszString, len);
}
void PrintToStdOutA(const char *pszString) {
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
FORBID_FAULT;
}
CONTRACTL_END
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);
PrintToHandleA(Handle, pszString);
}
void PrintToStdOutW(const WCHAR *pwzString)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
INJECT_FAULT(COMPlusThrowOM(););
}
CONTRACTL_END
MAKE_MULTIBYTE_FROMWIDE_BESTFIT(pStr, pwzString, GetConsoleOutputCP());
PrintToStdOutA(pStr);
}
void PrintToStdErrA(const char *pszString) {
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
FORBID_FAULT;
}
CONTRACTL_END
HANDLE Handle = GetStdHandle(STD_ERROR_HANDLE);
PrintToHandleA(Handle, pszString);
}
void PrintToStdErrW(const WCHAR *pwzString)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
INJECT_FAULT(COMPlusThrowOM(););
}
CONTRACTL_END
MAKE_MULTIBYTE_FROMWIDE_BESTFIT(pStr, pwzString, GetConsoleOutputCP());
PrintToStdErrA(pStr);
}
void NPrintToStdOutA(const char *pszString, size_t nbytes) {
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
FORBID_FAULT;
}
CONTRACTL_END
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);
NPrintToHandleA(Handle, pszString, nbytes);
}
void NPrintToStdOutW(const WCHAR *pwzString, size_t nchars)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
INJECT_FAULT(COMPlusThrowOM(););
}
CONTRACTL_END
LPSTR pStr;
MAKE_MULTIBYTE_FROMWIDEN_BESTFIT(pStr, pwzString, (int)nchars, nbytes, GetConsoleOutputCP());
NPrintToStdOutA(pStr, nbytes);
}
void NPrintToStdErrA(const char *pszString, size_t nbytes) {
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
FORBID_FAULT;
}
CONTRACTL_END
HANDLE Handle = GetStdHandle(STD_ERROR_HANDLE);
NPrintToHandleA(Handle, pszString, nbytes);
}
void NPrintToStdErrW(const WCHAR *pwzString, size_t nchars)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
INJECT_FAULT(COMPlusThrowOM(););
}
CONTRACTL_END
LPSTR pStr;
MAKE_MULTIBYTE_FROMWIDEN_BESTFIT(pStr, pwzString, (int)nchars, nbytes, GetConsoleOutputCP());
NPrintToStdErrA(pStr, nbytes);
}
//----------------------------------------------------------------------------
//*****************************************************************************
// Compare VarLoc's
//*****************************************************************************
bool operator ==(const ICorDebugInfo::VarLoc &varLoc1,
const ICorDebugInfo::VarLoc &varLoc2)
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
if (varLoc1.vlType != varLoc2.vlType)
return false;
switch(varLoc1.vlType)
{
case ICorDebugInfo::VLT_REG:
case ICorDebugInfo::VLT_REG_BYREF:
return varLoc1.vlReg.vlrReg == varLoc2.vlReg.vlrReg;
case ICorDebugInfo::VLT_STK:
case ICorDebugInfo::VLT_STK_BYREF:
return varLoc1.vlStk.vlsBaseReg == varLoc2.vlStk.vlsBaseReg &&
varLoc1.vlStk.vlsOffset == varLoc2.vlStk.vlsOffset;
case ICorDebugInfo::VLT_REG_REG:
return varLoc1.vlRegReg.vlrrReg1 == varLoc2.vlRegReg.vlrrReg1 &&
varLoc1.vlRegReg.vlrrReg2 == varLoc2.vlRegReg.vlrrReg2;
case ICorDebugInfo::VLT_REG_STK:
return varLoc1.vlRegStk.vlrsReg == varLoc2.vlRegStk.vlrsReg &&
varLoc1.vlRegStk.vlrsStk.vlrssBaseReg == varLoc2.vlRegStk.vlrsStk.vlrssBaseReg &&
varLoc1.vlRegStk.vlrsStk.vlrssOffset == varLoc2.vlRegStk.vlrsStk.vlrssOffset;
case ICorDebugInfo::VLT_STK_REG:
return varLoc1.vlStkReg.vlsrStk.vlsrsBaseReg == varLoc2.vlStkReg.vlsrStk.vlsrsBaseReg &&
varLoc1.vlStkReg.vlsrStk.vlsrsOffset == varLoc2.vlStkReg.vlsrStk.vlsrsBaseReg &&
varLoc1.vlStkReg.vlsrReg == varLoc2.vlStkReg.vlsrReg;
case ICorDebugInfo::VLT_STK2:
return varLoc1.vlStk2.vls2BaseReg == varLoc2.vlStk2.vls2BaseReg &&
varLoc1.vlStk2.vls2Offset == varLoc2.vlStk2.vls2Offset;
case ICorDebugInfo::VLT_FPSTK:
return varLoc1.vlFPstk.vlfReg == varLoc2.vlFPstk.vlfReg;
default:
_ASSERTE(!"Bad vlType"); return false;
}
}
#endif // #ifndef DACCESS_COMPILE
//*****************************************************************************
// The following are used to read and write data given NativeVarInfo
// for primitive types. For ValueClasses, FALSE will be returned.
//*****************************************************************************
SIZE_T GetRegOffsInCONTEXT(ICorDebugInfo::RegNum regNum)
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
#ifdef _TARGET_X86_
switch(regNum)
{
case ICorDebugInfo::REGNUM_EAX: return offsetof(T_CONTEXT,Eax);
case ICorDebugInfo::REGNUM_ECX: return offsetof(T_CONTEXT,Ecx);
case ICorDebugInfo::REGNUM_EDX: return offsetof(T_CONTEXT,Edx);
case ICorDebugInfo::REGNUM_EBX: return offsetof(T_CONTEXT,Ebx);
// TODO: Fix AMBIENT_SP handling.
// AMBIENT_SP It isn't necessarily the same value as ESP. We probably shouldn't try
// and handle REGNUM_AMBIENT_SP here, and instead update our callers (eg.
// GetNativeVarVal) to handle this case explicitly. This logic should also be
// merged with the parallel (but correct in this case) logic in mscordbi.
case ICorDebugInfo::REGNUM_ESP:
case ICorDebugInfo::REGNUM_AMBIENT_SP:
return offsetof(T_CONTEXT,Esp);
case ICorDebugInfo::REGNUM_EBP: return offsetof(T_CONTEXT,Ebp);
case ICorDebugInfo::REGNUM_ESI: return offsetof(T_CONTEXT,Esi);
case ICorDebugInfo::REGNUM_EDI: return offsetof(T_CONTEXT,Edi);
default: _ASSERTE(!"Bad regNum"); return (SIZE_T) -1;
}
#elif defined(_TARGET_AMD64_)
switch(regNum)
{
case ICorDebugInfo::REGNUM_RAX: return offsetof(CONTEXT, Rax);
case ICorDebugInfo::REGNUM_RCX: return offsetof(CONTEXT, Rcx);
case ICorDebugInfo::REGNUM_RDX: return offsetof(CONTEXT, Rdx);
case ICorDebugInfo::REGNUM_RBX: return offsetof(CONTEXT, Rbx);
case ICorDebugInfo::REGNUM_RSP: return offsetof(CONTEXT, Rsp);
case ICorDebugInfo::REGNUM_RBP: return offsetof(CONTEXT, Rbp);
case ICorDebugInfo::REGNUM_RSI: return offsetof(CONTEXT, Rsi);
case ICorDebugInfo::REGNUM_RDI: return offsetof(CONTEXT, Rdi);
case ICorDebugInfo::REGNUM_R8: return offsetof(CONTEXT, R8);
case ICorDebugInfo::REGNUM_R9: return offsetof(CONTEXT, R9);
case ICorDebugInfo::REGNUM_R10: return offsetof(CONTEXT, R10);
case ICorDebugInfo::REGNUM_R11: return offsetof(CONTEXT, R11);
case ICorDebugInfo::REGNUM_R12: return offsetof(CONTEXT, R12);
case ICorDebugInfo::REGNUM_R13: return offsetof(CONTEXT, R13);
case ICorDebugInfo::REGNUM_R14: return offsetof(CONTEXT, R14);
case ICorDebugInfo::REGNUM_R15: return offsetof(CONTEXT, R15);
default: _ASSERTE(!"Bad regNum"); return (SIZE_T)(-1);
}
#elif defined(_TARGET_ARM_)
switch(regNum)
{
case ICorDebugInfo::REGNUM_R0: return offsetof(T_CONTEXT, R0);
case ICorDebugInfo::REGNUM_R1: return offsetof(T_CONTEXT, R1);
case ICorDebugInfo::REGNUM_R2: return offsetof(T_CONTEXT, R2);
case ICorDebugInfo::REGNUM_R3: return offsetof(T_CONTEXT, R3);
case ICorDebugInfo::REGNUM_R4: return offsetof(T_CONTEXT, R4);
case ICorDebugInfo::REGNUM_R5: return offsetof(T_CONTEXT, R5);
case ICorDebugInfo::REGNUM_R6: return offsetof(T_CONTEXT, R6);
case ICorDebugInfo::REGNUM_R7: return offsetof(T_CONTEXT, R7);
case ICorDebugInfo::REGNUM_R8: return offsetof(T_CONTEXT, R8);
case ICorDebugInfo::REGNUM_R9: return offsetof(T_CONTEXT, R9);
case ICorDebugInfo::REGNUM_R10: return offsetof(T_CONTEXT, R10);
case ICorDebugInfo::REGNUM_R11: return offsetof(T_CONTEXT, R11);
case ICorDebugInfo::REGNUM_R12: return offsetof(T_CONTEXT, R12);
case ICorDebugInfo::REGNUM_SP: return offsetof(T_CONTEXT, Sp);
case ICorDebugInfo::REGNUM_PC: return offsetof(T_CONTEXT, Pc);
case ICorDebugInfo::REGNUM_LR: return offsetof(T_CONTEXT, Lr);
case ICorDebugInfo::REGNUM_AMBIENT_SP: return offsetof(T_CONTEXT, Sp);
default: _ASSERTE(!"Bad regNum"); return (SIZE_T)(-1);
}
#elif defined(_TARGET_ARM64_)
switch(regNum)
{
case ICorDebugInfo::REGNUM_X0: return offsetof(T_CONTEXT, X0);
case ICorDebugInfo::REGNUM_X1: return offsetof(T_CONTEXT, X1);
case ICorDebugInfo::REGNUM_X2: return offsetof(T_CONTEXT, X2);
case ICorDebugInfo::REGNUM_X3: return offsetof(T_CONTEXT, X3);
case ICorDebugInfo::REGNUM_X4: return offsetof(T_CONTEXT, X4);
case ICorDebugInfo::REGNUM_X5: return offsetof(T_CONTEXT, X5);
case ICorDebugInfo::REGNUM_X6: return offsetof(T_CONTEXT, X6);
case ICorDebugInfo::REGNUM_X7: return offsetof(T_CONTEXT, X7);
case ICorDebugInfo::REGNUM_X8: return offsetof(T_CONTEXT, X8);
case ICorDebugInfo::REGNUM_X9: return offsetof(T_CONTEXT, X9);
case ICorDebugInfo::REGNUM_X10: return offsetof(T_CONTEXT, X10);
case ICorDebugInfo::REGNUM_X11: return offsetof(T_CONTEXT, X11);
case ICorDebugInfo::REGNUM_X12: return offsetof(T_CONTEXT, X12);
case ICorDebugInfo::REGNUM_X13: return offsetof(T_CONTEXT, X13);
case ICorDebugInfo::REGNUM_X14: return offsetof(T_CONTEXT, X14);
case ICorDebugInfo::REGNUM_X15: return offsetof(T_CONTEXT, X15);
case ICorDebugInfo::REGNUM_X16: return offsetof(T_CONTEXT, X16);
case ICorDebugInfo::REGNUM_X17: return offsetof(T_CONTEXT, X17);
case ICorDebugInfo::REGNUM_X18: return offsetof(T_CONTEXT, X18);
case ICorDebugInfo::REGNUM_X19: return offsetof(T_CONTEXT, X19);
case ICorDebugInfo::REGNUM_X20: return offsetof(T_CONTEXT, X20);
case ICorDebugInfo::REGNUM_X21: return offsetof(T_CONTEXT, X21);
case ICorDebugInfo::REGNUM_X22: return offsetof(T_CONTEXT, X22);
case ICorDebugInfo::REGNUM_X23: return offsetof(T_CONTEXT, X23);
case ICorDebugInfo::REGNUM_X24: return offsetof(T_CONTEXT, X24);
case ICorDebugInfo::REGNUM_X25: return offsetof(T_CONTEXT, X25);
case ICorDebugInfo::REGNUM_X26: return offsetof(T_CONTEXT, X26);
case ICorDebugInfo::REGNUM_X27: return offsetof(T_CONTEXT, X27);
case ICorDebugInfo::REGNUM_X28: return offsetof(T_CONTEXT, X28);
case ICorDebugInfo::REGNUM_FP: return offsetof(T_CONTEXT, Fp);
case ICorDebugInfo::REGNUM_LR: return offsetof(T_CONTEXT, Lr);
case ICorDebugInfo::REGNUM_SP: return offsetof(T_CONTEXT, Sp);
case ICorDebugInfo::REGNUM_PC: return offsetof(T_CONTEXT, Pc);
case ICorDebugInfo::REGNUM_AMBIENT_SP: return offsetof(T_CONTEXT, Sp);
default: _ASSERTE(!"Bad regNum"); return (SIZE_T)(-1);
}
#else
PORTABILITY_ASSERT("GetRegOffsInCONTEXT is not implemented on this platform.");
return (SIZE_T) -1;
#endif // _TARGET_X86_
}
SIZE_T DereferenceByRefVar(SIZE_T addr)
{
STATIC_CONTRACT_WRAPPER;
SIZE_T result = NULL;
#if defined(DACCESS_COMPILE)
HRESULT hr = DacReadAll(addr, &result, sizeof(result), false);
if (FAILED(hr))
{
result = NULL;
}
#else // !DACCESS_COMPILE
EX_TRY
{
AVInRuntimeImplOkayHolder AVOkay;
result = *(SIZE_T*)addr;
}
EX_CATCH
{
}
EX_END_CATCH(SwallowAllExceptions);
#endif // !DACCESS_COMPILE
return result;
}
// How are errors communicated to the caller?
ULONG NativeVarLocations(const ICorDebugInfo::VarLoc & varLoc,
PT_CONTEXT pCtx,
ULONG numLocs,
NativeVarLocation* locs)
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
_ASSERTE(numLocs >= MAX_NATIVE_VAR_LOCS);
bool fByRef = false;
switch(varLoc.vlType)
{
SIZE_T regOffs;
TADDR baseReg;
case ICorDebugInfo::VLT_REG_BYREF:
fByRef = true; // fall through
case ICorDebugInfo::VLT_REG:
regOffs = GetRegOffsInCONTEXT(varLoc.vlReg.vlrReg);
locs->addr = (ULONG64)(ULONG_PTR)pCtx + regOffs;
if (fByRef)
{
locs->addr = (ULONG64)DereferenceByRefVar((SIZE_T)locs->addr);
}
locs->size = sizeof(SIZE_T);
{
locs->contextReg = true;
}
return 1;
case ICorDebugInfo::VLT_STK_BYREF:
fByRef = true; // fall through
case ICorDebugInfo::VLT_STK:
regOffs = GetRegOffsInCONTEXT(varLoc.vlStk.vlsBaseReg);
baseReg = *(TADDR *)(regOffs + (BYTE*)pCtx);
locs->addr = baseReg + varLoc.vlStk.vlsOffset;
if (fByRef)
{
locs->addr = (ULONG64)DereferenceByRefVar((SIZE_T)locs->addr);
}
locs->size = sizeof(SIZE_T);
locs->contextReg = false;
return 1;
case ICorDebugInfo::VLT_REG_REG:
regOffs = GetRegOffsInCONTEXT(varLoc.vlRegReg.vlrrReg1);
locs->addr = (ULONG64)(ULONG_PTR)pCtx + regOffs;
locs->size = sizeof(SIZE_T);
locs->contextReg = true;
locs++;
regOffs = GetRegOffsInCONTEXT(varLoc.vlRegReg.vlrrReg2);
locs->addr = (ULONG64)(ULONG_PTR)pCtx + regOffs;
locs->size = sizeof(SIZE_T);
locs->contextReg = true;
return 2;
case ICorDebugInfo::VLT_REG_STK:
regOffs = GetRegOffsInCONTEXT(varLoc.vlRegStk.vlrsReg);
locs->addr = (ULONG64)(ULONG_PTR)pCtx + regOffs;
locs->size = sizeof(SIZE_T);
locs->contextReg = true;
locs++;
regOffs = GetRegOffsInCONTEXT(varLoc.vlRegStk.vlrsStk.vlrssBaseReg);
baseReg = *(TADDR *)(regOffs + (BYTE*)pCtx);
locs->addr = baseReg + varLoc.vlRegStk.vlrsStk.vlrssOffset;
locs->size = sizeof(SIZE_T);
locs->contextReg = false;
return 2;
case ICorDebugInfo::VLT_STK_REG:
regOffs = GetRegOffsInCONTEXT(varLoc.vlStkReg.vlsrStk.vlsrsBaseReg);
baseReg = *(TADDR *)(regOffs + (BYTE*)pCtx);
locs->addr = baseReg + varLoc.vlStkReg.vlsrStk.vlsrsOffset;
locs->size = sizeof(SIZE_T);
locs->contextReg = false;
locs++;
regOffs = GetRegOffsInCONTEXT(varLoc.vlStkReg.vlsrReg);
locs->addr = (ULONG64)(ULONG_PTR)pCtx + regOffs;
locs->size = sizeof(SIZE_T);
locs->contextReg = true;
return 2;
case ICorDebugInfo::VLT_STK2:
regOffs = GetRegOffsInCONTEXT(varLoc.vlStk2.vls2BaseReg);
baseReg = *(TADDR *)(regOffs + (BYTE*)pCtx);
locs->addr = baseReg + varLoc.vlStk2.vls2Offset;
locs->size = 2 * sizeof(SIZE_T);
locs->contextReg = false;
return 1;
case ICorDebugInfo::VLT_FPSTK:
_ASSERTE(!"NYI");
return 0;
default:
_ASSERTE(!"Bad locType");
return 0;
}
}
BOOL CompareFiles(HANDLE hFile1,HANDLE hFile2)
{
STATIC_CONTRACT_THROWS;
STATIC_CONTRACT_GC_NOTRIGGER;
BY_HANDLE_FILE_INFORMATION fileinfo1;
BY_HANDLE_FILE_INFORMATION fileinfo2;
if (!GetFileInformationByHandle(hFile1,&fileinfo1) ||
!GetFileInformationByHandle(hFile2,&fileinfo2))
ThrowLastError();
return fileinfo1.nFileIndexLow == fileinfo2.nFileIndexLow &&
fileinfo1.nFileIndexHigh == fileinfo2.nFileIndexHigh &&
fileinfo1.dwVolumeSerialNumber==fileinfo2.dwVolumeSerialNumber;
}
#ifndef DACCESS_COMPILE
// Returns the location at which the variable
// begins. Returns NULL for register vars. For reg-stack
// split, it'll return the addr of the stack part.
// This also works for VLT_REG (a single register).
SIZE_T *NativeVarStackAddr(const ICorDebugInfo::VarLoc & varLoc,
PCONTEXT pCtx)
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
SIZE_T *dwAddr = NULL;
bool fByRef = false;
switch(varLoc.vlType)
{
SIZE_T regOffs;
const BYTE * baseReg;
case ICorDebugInfo::VLT_REG_BYREF:
fByRef = true; // fall through
case ICorDebugInfo::VLT_REG:
regOffs = GetRegOffsInCONTEXT(varLoc.vlReg.vlrReg);
dwAddr = (SIZE_T *)(regOffs + (BYTE*)pCtx);
if (fByRef)
{
dwAddr = (SIZE_T*)(*dwAddr);
}
LOG((LF_CORDB, LL_INFO100, "NVSA: VLT_REG @ 0x%x (by ref = %d)\n", dwAddr, fByRef));
break;
case ICorDebugInfo::VLT_STK_BYREF:
fByRef = true; // fall through
case ICorDebugInfo::VLT_STK:
regOffs = GetRegOffsInCONTEXT(varLoc.vlStk.vlsBaseReg);
baseReg = (const BYTE *)*(SIZE_T *)(regOffs + (BYTE*)pCtx);
dwAddr = (SIZE_T *)(baseReg + varLoc.vlStk.vlsOffset);
if (fByRef)
{
dwAddr = (SIZE_T*)(*dwAddr);
}
LOG((LF_CORDB, LL_INFO100, "NVSA: VLT_STK @ 0x%x (by ref = %d)\n", dwAddr, fByRef));
break;
case ICorDebugInfo::VLT_STK2:
// <TODO>@TODO : VLT_STK2 is overloaded to also mean VLT_STK_n.
// return FALSE if n > 2;</TODO>
regOffs = GetRegOffsInCONTEXT(varLoc.vlStk2.vls2BaseReg);
baseReg = (const BYTE *)*(SIZE_T *)(regOffs + (BYTE*)pCtx);
dwAddr = (SIZE_T *)(baseReg + varLoc.vlStk2.vls2Offset);
LOG((LF_CORDB, LL_INFO100, "NVSA: VLT_STK_2 @ 0x%x\n",dwAddr));
break;
case ICorDebugInfo::VLT_REG_STK:
regOffs = GetRegOffsInCONTEXT(varLoc.vlRegStk.vlrsStk.vlrssBaseReg);
baseReg = (const BYTE *)*(SIZE_T *)(regOffs + (BYTE*)pCtx);
dwAddr = (SIZE_T *)(baseReg + varLoc.vlRegStk.vlrsStk.vlrssOffset);
LOG((LF_CORDB, LL_INFO100, "NVSA: REG_STK @ 0x%x\n",dwAddr));
break;
case ICorDebugInfo::VLT_STK_REG:
regOffs = GetRegOffsInCONTEXT(varLoc.vlStkReg.vlsrStk.vlsrsBaseReg);
baseReg = (const BYTE *)*(SIZE_T *)(regOffs + (BYTE*)pCtx);
dwAddr = (SIZE_T *)(baseReg + varLoc.vlStkReg.vlsrStk.vlsrsOffset);
LOG((LF_CORDB, LL_INFO100, "NVSA: STK_REG @ 0x%x\n",dwAddr));
break;
case ICorDebugInfo::VLT_REG_REG:
case ICorDebugInfo::VLT_FPSTK:
_ASSERTE(!"NYI"); break;
default:
_ASSERTE(!"Bad locType"); break;
}
return dwAddr;
}
#if defined(_WIN64)
void GetNativeVarValHelper(SIZE_T* dstAddrLow, SIZE_T* dstAddrHigh, SIZE_T* srcAddr, SIZE_T size)
{
if (size == 1)
*(BYTE*)dstAddrLow = *(BYTE*)srcAddr;
else if (size == 2)
*(USHORT*)dstAddrLow = *(USHORT*)srcAddr;
else if (size == 4)
*(ULONG*)dstAddrLow = *(ULONG*)srcAddr;
else if (size == 8)
*dstAddrLow = *srcAddr;
else if (size == 16)
{
*dstAddrLow = *srcAddr;
*dstAddrHigh = *(srcAddr+1);
}
else
{
_ASSERTE(!"util.cpp - unreachable code.\n");
UNREACHABLE();
}
}
#endif // _WIN64
bool GetNativeVarVal(const ICorDebugInfo::VarLoc & varLoc,
PCONTEXT pCtx,
SIZE_T * pVal1,
SIZE_T * pVal2
WIN64_ARG(SIZE_T cbSize))
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
switch(varLoc.vlType)
{
#if !defined(_WIN64)
SIZE_T regOffs;
case ICorDebugInfo::VLT_REG:
*pVal1 = *NativeVarStackAddr(varLoc,pCtx);