-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
divalue.cpp
4796 lines (4087 loc) · 167 KB
/
divalue.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.
//*****************************************************************************
// File: DIValue.cpp
//
//
//*****************************************************************************
#include "stdafx.h"
#include "primitives.h"
// copy from a MemoryRange to dest
// Arguments:
// input: source - MemoryRange describing the start address and size of the source buffer
// output: dest - address of the buffer to which the source buffer is copied
// Note: the buffer for dest must be allocated by the caller and must be large enough to hold the
// bytes from the source buffer.
void localCopy(void * dest, MemoryRange source)
{
_ASSERTE(dest != NULL);
_ASSERTE(source.StartAddress() != NULL);
memcpy(dest, source.StartAddress(), source.Size());
}
// for an inheritance graph of the ICDValue types, // See file:./ICorDebugValueTypes.vsd for a diagram of the types.
/* ------------------------------------------------------------------------- *
* CordbValue class
* ------------------------------------------------------------------------- */
CordbValue::CordbValue(CordbAppDomain * appdomain,
CordbType * type,
CORDB_ADDRESS id,
bool isLiteral,
NeuterList * pList)
: CordbBase(
((appdomain != NULL) ? (appdomain->GetProcess()) : (type->GetProcess())),
(UINT_PTR)id, enumCordbValue),
m_appdomain(appdomain),
m_type(type), // implicit InternalAddRef
//m_sigCopied(false),
m_size(0),
m_isLiteral(isLiteral)
{
HRESULT hr = S_OK;
_ASSERTE(GetProcess() != NULL);
// Add to a neuter list. If none is provided, use the ExitProcess list as a default.
// The main neuter lists of interest here are:
// - CordbProcess::GetContinueNeuterList() - Shortest. Neuter when the process continues.
// - CordbAppDomain::GetExitNeuterList() - Middle. Neuter when the AD exits. Since most Values (except globals) are in
// a specific AD, this almost catches all; and keeps us safe in AD-unload scenarios.
// - CordbProcess::GetExitNeuterList() - Worst. Doesn't neuter until the process exits (or we detach).
// This could be a long time.
if (pList == NULL)
{
pList = GetProcess()->GetExitNeuterList();
}
EX_TRY
{
pList->Add(GetProcess(), this);
}
EX_CATCH_HRESULT(hr);
SetUnrecoverableIfFailed(GetProcess(), hr);
} // CordbValue::CordbValue
CordbValue::~CordbValue()
{
DTOR_ENTRY(this);
_ASSERTE(this->IsNeutered());
_ASSERTE(m_type == NULL);
} // CordbValue::~CordbValue
void CordbValue::Neuter()
{
m_appdomain = NULL;
m_type.Clear();
ValueHome * pValueHome = GetValueHome();
if (pValueHome != NULL)
{
pValueHome->Clear();
}
CordbBase::Neuter();
} // CordbValue::Neuter
// Helper for code:CordbValue::CreateValueByType. Create a new instance of CordbGenericValue
// Arguments:
// input: pAppdomain - appdomain to which the value belongs
// pType - type of the value
// remoteValue - remote address and size of the value
// localValue - local address and size of the value
// ppRemoteRegAddr - register address of the value
// output: ppValue - the newly created instance of an ICDValue
// Notes:
// - only one of the three locations will be non-NULL
// - Throws
/* static */
void CordbValue::CreateGenericValue(CordbAppDomain * pAppdomain,
CordbType * pType,
TargetBuffer remoteValue,
MemoryRange localValue,
EnregisteredValueHomeHolder * ppRemoteRegAddr,
ICorDebugValue** ppValue)
{
LOG((LF_CORDB,LL_INFO100000,"CV::CreateValueByType CreateGenericValue\n"));
RSSmartPtr<CordbGenericValue> pGenValue;
// A generic value
// By using a RSSmartPtr we ensure that in both success and failure cases,
// this object is cleaned up properly (deleted or not depending on ref counts).
// Specifically, the object has probably been placed on a neuter list so we
// can't delete it (but this is a detail we shouldn't rely on)
pGenValue.Assign(new CordbGenericValue(pAppdomain,
pType,
remoteValue,
ppRemoteRegAddr));
pGenValue->Init(localValue); // throws
pGenValue->AddRef();
*ppValue = (ICorDebugValue *)(ICorDebugGenericValue *)pGenValue;
} // CordbValue::CreateGenericValue
// create a new instance of CordbVCObjectValue or CordbReferenceValue
// Arguments:
// input: pAppdomain - appdomain to which the value belongs
// pType - type of the value
// boxed - indicates whether the value is boxed
// remoteValue - remote address and size of the value
// localValue - local address and size of the value
// ppRemoteRegAddr - register address of the value
// output: ppValue - the newly created instance of an ICDValue
// Notes:
// - only one of the three locations will be non-NULL
// - Throws error codes from reading process memory
/* static */
void CordbValue::CreateVCObjOrRefValue(CordbAppDomain * pAppdomain,
CordbType * pType,
bool boxed,
TargetBuffer remoteValue,
MemoryRange localValue,
EnregisteredValueHomeHolder * ppRemoteRegAddr,
ICorDebugValue** ppValue)
{
HRESULT hr = S_OK;
LOG((LF_CORDB,LL_INFO1000000,"CV::CreateValueByType Creating ReferenceValue\n"));
// We either have a boxed or unboxed value type, or we have a value that's not a value type.
// For an unboxed value type, we'll create an instance of CordbVCObjectValue. Otherwise, we'll
// create an instance of CordbReferenceValue.
// do we have a value type?
bool isVCObject = pType->IsValueType(); // throws
if (!boxed && isVCObject)
{
RSSmartPtr<CordbVCObjectValue> pVCValue(new CordbVCObjectValue(pAppdomain,
pType,
remoteValue,
ppRemoteRegAddr));
IfFailThrow(pVCValue->Init(localValue));
pVCValue->AddRef();
*ppValue = (ICorDebugValue*)(ICorDebugObjectValue*)pVCValue;
}
else
{
// either the value is boxed or it's not a value type
RSSmartPtr<CordbReferenceValue> pRef;
hr = CordbReferenceValue::Build(pAppdomain,
pType,
remoteValue,
localValue,
VMPTR_OBJECTHANDLE::NullPtr(),
ppRemoteRegAddr, // Home
&pRef);
IfFailThrow(hr);
hr = pRef->QueryInterface(__uuidof(ICorDebugValue), (void**)ppValue);
_ASSERTE(SUCCEEDED(hr));
}
} // CordbValue::CreateVCObjOrRefValue
//
// Create the proper ICDValue instance based on the given element type.
// Arguments:
// input: pAppdomain - appdomain to which the value belongs
// pType - type of the value
// boxed - indicates whether the value is boxed
// remoteValue - remote address and size of the value
// localValue - local address and size of the value
// ppRemoteRegAddr - register address of the value
// output: ppValue - the newly created instance of an ICDValue
// Notes:
// - Only one of the three locations, remoteValue, localValue or ppRemoteRegAddr, will be non-NULL.
// - Throws.
/*static*/ void CordbValue::CreateValueByType(CordbAppDomain * pAppdomain,
CordbType * pType,
bool boxed,
TargetBuffer remoteValue,
MemoryRange localValue,
EnregisteredValueHomeHolder * ppRemoteRegAddr,
ICorDebugValue** ppValue)
{
INTERNAL_SYNC_API_ENTRY(pAppdomain->GetProcess()); //
// We'd really hope that our callers give us a valid appdomain, but in case
// they don't, we'll fail gracefully.
if ((pAppdomain != NULL) && pAppdomain->IsNeutered())
{
STRESS_LOG1(LF_CORDB, LL_EVERYTHING, "CVBT using neutered AP, %p\n", pAppdomain);
ThrowHR(E_INVALIDARG);
}
LOG((LF_CORDB,LL_INFO100000,"CV::CreateValueByType\n"));
*ppValue = NULL;
switch(pType->m_elementType)
{
case ELEMENT_TYPE_BOOLEAN:
case ELEMENT_TYPE_CHAR:
case ELEMENT_TYPE_I1:
case ELEMENT_TYPE_U1:
case ELEMENT_TYPE_I2:
case ELEMENT_TYPE_U2:
case ELEMENT_TYPE_I4:
case ELEMENT_TYPE_U4:
case ELEMENT_TYPE_R4:
case ELEMENT_TYPE_I8:
case ELEMENT_TYPE_U8:
case ELEMENT_TYPE_R8:
case ELEMENT_TYPE_I:
case ELEMENT_TYPE_U:
{
CreateGenericValue(pAppdomain, pType, remoteValue, localValue, ppRemoteRegAddr, ppValue); // throws
break;
}
case ELEMENT_TYPE_CLASS:
case ELEMENT_TYPE_OBJECT:
case ELEMENT_TYPE_STRING:
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_BYREF:
case ELEMENT_TYPE_TYPEDBYREF:
case ELEMENT_TYPE_ARRAY:
case ELEMENT_TYPE_SZARRAY:
case ELEMENT_TYPE_FNPTR:
{
CreateVCObjOrRefValue(pAppdomain, pType, boxed, remoteValue, localValue, ppRemoteRegAddr, ppValue); // throws
break;
}
default:
_ASSERTE(!"Bad value type!");
ThrowHR(E_FAIL);
}
} // CordbValue::CreateValueByType
// Create the proper ICDValue instance based on the given remote heap object
// Arguments:
// pAppDomain - the app domain the remote object is in
// vmObj - the remote object to get an ICDValue for
ICorDebugValue* CordbValue::CreateHeapValue(CordbAppDomain* pAppDomain, VMPTR_Object vmObj)
{
// Create a temporary reference and dereference it to construct the heap value we want.
RSSmartPtr<CordbReferenceValue> pRefValue(CordbValue::CreateHeapReferenceValue(pAppDomain, vmObj));
ICorDebugValue* pExtValue;
IfFailThrow(pRefValue->Dereference(&pExtValue));
return pExtValue;
}
CordbReferenceValue* CordbValue::CreateHeapReferenceValue(CordbAppDomain* pAppDomain, VMPTR_Object vmObj)
{
IDacDbiInterface* pDac = pAppDomain->GetProcess()->GetDAC();
TargetBuffer objBuffer = pDac->GetObjectContents(vmObj);
VOID* pRemoteAddr = CORDB_ADDRESS_TO_PTR(objBuffer.pAddress);
// This creates a local reference that has a remote address in it. Ie &pRemoteAddr is an address
// in the host address space and pRemoteAddr is an address in the target.
MemoryRange localReferenceDescription(&pRemoteAddr, sizeof(pRemoteAddr));
RSSmartPtr<CordbReferenceValue> pRefValue;
IfFailThrow(CordbReferenceValue::Build(pAppDomain,
NULL,
EMPTY_BUFFER,
localReferenceDescription,
VMPTR_OBJECTHANDLE::NullPtr(),
NULL,
&pRefValue));
return pRefValue;
}
// Gets the size om bytes of a value from its type. If the value is complex, we assume it is represented as
// a reference, since this is called for values that have been found on the stack, as an element of an
// array (represented as CordbArrayValue) or field of an object (CordbObjectValue) or the result of a
// func eval. For unboxed value types, we get the size of the entire value (it is not represented as a
// reference).
// Examples:
// - int on the stack
// => sizeof(int)
// - int as a field in an object on the heap
// =>sizeof(int)
// - Boxed int on the heap
// => size of a pointer
// - Class Point { int x; int y}; // class will have a method table / object header which may increase size.
// => size of a pointer
// - Struct Point {int x; int y; }; // unboxed struct may not necessarily have the object header.
// => 2 * sizeof(int)
// - List<int>
// => size of a pointer
// Arguments: pType - the type of the value
// boxing - indicates whether the value is boxed or not
// Return Value: the size of the value
// Notes: Throws
// In general, this returns the unboxed size of the value, but if we have a type
// that represents a non-generic and it's not an unboxed value type, we know that
// it will be represented as a reference, so we return the size of a pointer instead.
/* static */
ULONG32 CordbValue::GetSizeForType(CordbType * pType, BoxedValue boxing)
{
ULONG32 size = 0;
switch(pType->m_elementType)
{
case ELEMENT_TYPE_BOOLEAN:
case ELEMENT_TYPE_CHAR:
case ELEMENT_TYPE_I1:
case ELEMENT_TYPE_U1:
case ELEMENT_TYPE_I2:
case ELEMENT_TYPE_U2:
case ELEMENT_TYPE_I4:
case ELEMENT_TYPE_U4:
case ELEMENT_TYPE_R4:
case ELEMENT_TYPE_I8:
case ELEMENT_TYPE_U8:
case ELEMENT_TYPE_R8:
case ELEMENT_TYPE_I:
case ELEMENT_TYPE_U: pType->GetUnboxedObjectSize(&size); break;
case ELEMENT_TYPE_CLASS:
case ELEMENT_TYPE_OBJECT:
case ELEMENT_TYPE_STRING:
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_BYREF:
case ELEMENT_TYPE_TYPEDBYREF:
case ELEMENT_TYPE_ARRAY:
case ELEMENT_TYPE_SZARRAY:
case ELEMENT_TYPE_FNPTR: {
bool isUnboxedVCObject = false;
if (boxing == kUnboxed)
{
isUnboxedVCObject = pType->IsValueType(); // throws
}
if (!isUnboxedVCObject)
{
// if it's not an unboxed value type (we're in the case
// for compound types), then it's a reference
// and we just want to return the size of a pointer
size = sizeof(void *);
}
else
{
pType->GetUnboxedObjectSize(&size);
}
} break;
default:
_ASSERTE(!"Bad value type!");
}
return size;
} // CordbValue::GetSizeForType
HRESULT CordbValue::CreateBreakpoint(ICorDebugValueBreakpoint **ppBreakpoint)
{
VALIDATE_POINTER_TO_OBJECT(ppBreakpoint, ICorDebugValueBreakpoint **);
return E_NOTIMPL;
} // CordbValue::CreateBreakpoint
// gets the exact type of a value
// Arguments:
// input: none (uses m_type field)
// output: ppType - an instance of ICDType representing the exact type of the value
// Return Value:
HRESULT CordbValue::GetExactType(ICorDebugType **ppType)
{
PUBLIC_REENTRANT_API_ENTRY(this);
VALIDATE_POINTER_TO_OBJECT(ppType, ICorDebugType **);
FAIL_IF_NEUTERED(this);
ATT_REQUIRE_STOPPED_MAY_FAIL(GetProcess());
*ppType = static_cast<ICorDebugType*> (m_type);
if (*ppType != NULL)
(*ppType)->AddRef();
return S_OK;
} // CordbValue::GetExactType
// CreateHandle for a heap object.
// @todo: How to prevent this being called by non-heap object?
// Arguments:
// input: handleType - type of the handle to be created
// output: ppHandle - on success, the newly created handle
// Return Value: S_OK on success or E_INVALIDARG, E_OUTOFMEMORY, or CORDB_E_HELPER_MAY_DEADLOCK
HRESULT CordbValue::InternalCreateHandle(CorDebugHandleType handleType,
ICorDebugHandleValue ** ppHandle)
{
INTERNAL_SYNC_API_ENTRY(GetProcess());
LOG((LF_CORDB,LL_INFO1000,"CV::CreateHandle\n"));
DebuggerIPCEvent event;
CordbProcess *process;
// @dbgtodo- , as part of inspection, convert this path to throwing.
if (ppHandle == NULL)
{
return E_INVALIDARG;
}
*ppHandle = NULL;
switch (handleType)
{
case HANDLE_STRONG:
case HANDLE_WEAK_TRACK_RESURRECTION:
case HANDLE_PINNED:
break;
default:
return E_INVALIDARG;
}
// Create the ICorDebugHandleValue object
RSInitHolder<CordbHandleValue> pHandle(new (nothrow) CordbHandleValue(m_appdomain, m_type, handleType) );
if (pHandle == NULL)
{
return E_OUTOFMEMORY;
}
// Send the event to create the handle.
process = m_appdomain->GetProcess();
_ASSERTE(process != NULL);
process->InitIPCEvent(&event,
DB_IPCE_CREATE_HANDLE,
true,
m_appdomain->GetADToken());
CORDB_ADDRESS addr = GetValueHome() != NULL ? GetValueHome()->GetAddress() : NULL;
event.CreateHandle.objectToken = CORDB_ADDRESS_TO_PTR(addr);
event.CreateHandle.handleType = handleType;
// Note: two-way event here...
HRESULT hr = process->SendIPCEvent(&event, sizeof(DebuggerIPCEvent));
hr = WORST_HR(hr, event.hr);
if (SUCCEEDED(hr))
{
_ASSERTE(event.type == DB_IPCE_CREATE_HANDLE_RESULT);
// Initialize the handle value object.
hr = pHandle->Init(event.CreateHandleResult.vmObjectHandle);
}
if (!SUCCEEDED(hr))
{
// Free the handle from the left-side.
pHandle->Dispose();
// The RSInitHolder will neuter and delete it.
return hr;
}
// Pass out the new handle value object.
pHandle.TransferOwnershipExternal(ppHandle);
return S_OK;
} // CordbValue::InternalCreateHandle
/* ------------------------------------------------------------------------- *
* Generic Value class
* ------------------------------------------------------------------------- */
//
// CordbGenericValue constructor that builds a generic value from
// a remote address or register.
// Arguments:
// input: pAppdomain - the app domain to which the value belongs
// pType - the type of the value
// remoteValue - buffer (and size) of the remote location where
// the value resides. This may be NULL if the value
// is enregistered.
// ppRemoteRegAddr - information describing the register in which the
// value resides. This may be NULL--only one of
// ppRemoteRegAddr and remoteValue will be non-NULL,
// depending on whether the value is in a register or
// memory.
CordbGenericValue::CordbGenericValue(CordbAppDomain * pAppdomain,
CordbType * pType,
TargetBuffer remoteValue,
EnregisteredValueHomeHolder * ppRemoteRegAddr)
: CordbValue(pAppdomain, pType, remoteValue.pAddress, false),
m_pValueHome(NULL)
{
_ASSERTE(pType->m_elementType != ELEMENT_TYPE_END);
_ASSERTE(pType->m_elementType != ELEMENT_TYPE_VOID);
_ASSERTE(pType->m_elementType < ELEMENT_TYPE_MAX);
// We can fill in the size now for generic values.
ULONG32 size = 0;
HRESULT hr;
hr = pType->GetUnboxedObjectSize(&size);
_ASSERTE (!FAILED(hr));
m_size = size;
// now instantiate the value home
NewHolder<ValueHome> pHome(NULL);
if (remoteValue.IsEmpty())
{
pHome = (new RegisterValueHome(pAppdomain->GetProcess(), ppRemoteRegAddr));
}
else
{
pHome = (new RemoteValueHome(pAppdomain->GetProcess(), remoteValue));
}
m_pValueHome = pHome.GetValue(); // throws
pHome.SuppressRelease();
} // CordbGenericValue::CordbGenericValue
//
// CordbGenericValue constructor that builds an empty generic value
// from just an element type. Used for literal values for func evals
// only.
// Arguments:
// input: pType - the type of the value
CordbGenericValue::CordbGenericValue(CordbType * pType)
: CordbValue(NULL, pType, NULL, true),
m_pValueHome(NULL)
{
// The only purpose of a literal value is to hold a RS literal value.
ULONG32 size = 0;
HRESULT hr;
hr = pType->GetUnboxedObjectSize(&size);
_ASSERTE (!FAILED(hr));
m_size = size;
memset(m_pCopyOfData, 0, m_size);
// there is no value home for a literal so we leave it as NULL
} // CordbGenericValue::CordbGenericValue
// destructor
CordbGenericValue::~CordbGenericValue()
{
if (m_pValueHome != NULL)
{
delete m_pValueHome;
m_pValueHome = NULL;
}
} // CordbGenericValue::~CordbGenericValue
HRESULT CordbGenericValue::QueryInterface(REFIID id, void **pInterface)
{
if (id == IID_ICorDebugValue)
{
*pInterface = static_cast<ICorDebugValue*>(static_cast<ICorDebugGenericValue*>(this));
}
else if (id == IID_ICorDebugValue2)
{
*pInterface = static_cast<ICorDebugValue2*>(this);
}
else if (id == IID_ICorDebugValue3)
{
*pInterface = static_cast<ICorDebugValue3*>(this);
}
else if (id == IID_ICorDebugGenericValue)
{
*pInterface = static_cast<ICorDebugGenericValue*>(this);
}
else if (id == IID_IUnknown)
{
*pInterface = static_cast<IUnknown*>(static_cast<ICorDebugGenericValue*>(this));
}
else
{
*pInterface = NULL;
return E_NOINTERFACE;
}
ExternalAddRef();
return S_OK;
} // CordbGenericValue::QueryInterface
//
// initialize a generic value by copying the necessary data, either
// from the remote process or from another value in this process.
// Argument:
// input: localValue - RS location of value to be copied. This could be NULL or it
// could be a field from the cached copy of a CordbVCObjectValue or CordbObjectValue
// instance or an element from the cached copy of a CordbArrayValue instance
// Note: Throws error codes from reading process memory
void CordbGenericValue::Init(MemoryRange localValue)
{
INTERNAL_SYNC_API_ENTRY(this->GetProcess());
if(!m_isLiteral)
{
// If neither localValue.StartAddress nor m_remoteValue.pAddress are set, then all that means
// is that we've got a pre-initialized 64-bit value.
if (localValue.StartAddress() != NULL)
{
// Copy the data out of the local address space.
localCopy(m_pCopyOfData, localValue);
}
else
{
m_pValueHome->GetValue(MemoryRange(m_pCopyOfData, m_size)); // throws
}
}
} // CordbGenericValue::Init
// gets the value (i.e., number, boolean or pointer value) for this instance of CordbGenericValue
// Arguments:
// output: pTo - the starting address of a buffer in which the value will be written. This buffer must
// be guaranteed by the caller to be large enough to hold the value. There is no way for
// us to check here if it is. This must be non-NULL.
// Return Value: S_OK on success or E_INVALIDARG if the pTo is NULL
HRESULT CordbGenericValue::GetValue(void *pTo)
{
PUBLIC_REENTRANT_API_ENTRY(this);
FAIL_IF_NEUTERED(this);
VALIDATE_POINTER_TO_OBJECT_ARRAY(pTo, BYTE, m_size, false, true);
_ASSERTE(m_pCopyOfData != NULL);
// Copy out the value
memcpy(pTo, m_pCopyOfData, m_size);
return S_OK;
} // CordbGenericValue::GetValue
// Sets the value of this instance of CordbGenericValue
// Arguments:
// input: pFrom - pointer to a buffer holding the new value. We assume this is the same size as the
// original value; we have no way to check. This must be non-NULL.
// Return Value: S_OK on success or E_INVALIDARG if the pFrom is NULL
HRESULT CordbGenericValue::SetValue(void *pFrom)
{
HRESULT hr = S_OK;
PUBLIC_REENTRANT_API_ENTRY(this);
FAIL_IF_NEUTERED(this);
VALIDATE_POINTER_TO_OBJECT_ARRAY(pFrom, BYTE, m_size, true, false);
ATT_REQUIRE_STOPPED_MAY_FAIL(GetProcess());
// We only need to send to the left side to update values that are
// object references. For generic values, we can simply do a write
// memory.
EX_TRY
{
if(!m_isLiteral)
{
m_pValueHome->SetValue(MemoryRange(pFrom, m_size), m_type); // throws
}
}
EX_CATCH_HRESULT(hr);
IfFailRet(hr);
// That worked, so update the copy of the value we have in
// m_copyOfData.
memcpy(m_pCopyOfData, pFrom, m_size);
return hr;
} // CordbGenericValue::SetValue
// copies the value from this instance of CordbGenericValue iff the value represents a literal
// Arguments:
// output: pBuffer - pointer to the beginning of a caller-allocated buffer.This buffer must
// be guaranteed by the caller to be large enough to hol
// d the value. There is no way for
// us to check here if it is. This must be non-NULL.
// Return Value: true iff this is a literal value and pBuffer is a valid writeable address
bool CordbGenericValue::CopyLiteralData(BYTE *pBuffer)
{
INTERNAL_SYNC_API_ENTRY(this->GetProcess());
_ASSERTE(pBuffer != NULL);
// If this is a RS fabrication, copy the literal data into the
// given buffer and return true.
if (m_isLiteral)
{
_ASSERTE(m_size <= 8);
memcpy(pBuffer, m_pCopyOfData, m_size);
return true;
}
else
return false;
} // CordbGenericValue::CopyLiteralData
/* ------------------------------------------------------------------------- *
* Reference Value class
* ------------------------------------------------------------------------- */
// constructor
// Arguments:
// input: pAppdomain - appdomain to which the value belongs
// pType - the type of the referent (the object pointed to)
// localValue - the RS address and size of the buffer from which the reference
// will be copied. This will be NULL if either remoteValue,
// ppRemoteRegAddr or vmObjectHandle is non-NULL. Otherwise, it will
// point into the local cached copy of another instance of ICDValue
// remoteValue - the LS address and size of the buffer from which the reference
// will be copied. This will be NULL if either localValue,
// ppRemoteRegAddr, or vmObjectHandle is non-NULL.
// ppRemoteRegAddr - information about the register location of the buffer from which
// the reference will be copied. This will be NULL if either localValue,
// remoteValue, or vmObjectHandle is non-NULL.
// vmObjectHandle - a LS object handle holding the reference. This will be NULL if either
// localValue, remoteValue, or ppRemoteRegAddr is non-NULL.
// Note: this may throw OOM
CordbReferenceValue::CordbReferenceValue(CordbAppDomain * pAppdomain,
CordbType * pType,
MemoryRange localValue,
TargetBuffer remoteValue,
EnregisteredValueHomeHolder * ppRemoteRegAddr,
VMPTR_OBJECTHANDLE vmObjectHandle)
: CordbValue(pAppdomain, pType, remoteValue.pAddress, false,
// We'd like to change this to be a ContinueList so it gets neutered earlier,
// but it may be a breaking change
pAppdomain->GetSweepableExitNeuterList()),
m_realTypeOfTypedByref(NULL)
{
memset(&m_info, 0, sizeof(m_info));
LOG((LF_CORDB,LL_EVERYTHING,"CRV::CRV: this:0x%x\n",this));
m_size = sizeof(void *);
// now instantiate the value home
NewHolder<ValueHome> pHome(NULL);
if (!vmObjectHandle.IsNull())
{
pHome = (new HandleValueHome(pAppdomain->GetProcess(), vmObjectHandle));
m_valueHome.SetObjHandleFlag(false);
}
else if (remoteValue.IsEmpty())
{
pHome = (new RegisterValueHome(pAppdomain->GetProcess(), ppRemoteRegAddr));
m_valueHome.SetObjHandleFlag(true);
}
else
{
pHome = (new RefRemoteValueHome(pAppdomain->GetProcess(), remoteValue));
}
m_valueHome.m_pHome = pHome.GetValue(); // throws
pHome.SuppressRelease();
} // CordbReferenceValue::CordbReferenceValue
// CordbReferenceValue constructor that builds an empty reference value
// from just an element type. Used for literal values for func evals
// only.
// Arguments:
// input: pType - the type of the value
CordbReferenceValue::CordbReferenceValue(CordbType * pType)
: CordbValue(NULL, pType, NULL, true, pType->GetAppDomain()->GetSweepableExitNeuterList())
{
memset(&m_info, 0, sizeof(m_info));
// The only purpose of a literal value is to hold a RS literal value.
m_size = sizeof(void*);
// there is no value home for a literal
m_valueHome.m_pHome = NULL;
} // CordbReferenceValue::CordbReferenceValue
// copies the value from this instance of CordbReferenceValue iff the value represents a literal
// Arguments:
// output: pBuffer - pointer to the beginning of a caller-allocated buffer.This buffer must
// be guaranteed by the caller to be large enough to hold the value.
// There is no way for us to check here if it is. This must be non-NULL.
// Return Value: true iff this is a literal value and pBuffer is a valid writeable address
bool CordbReferenceValue::CopyLiteralData(BYTE *pBuffer)
{
_ASSERTE(pBuffer != NULL);
// If this is a RS fabrication, then its a null reference.
if (m_isLiteral)
{
void *n = NULL;
memcpy(pBuffer, &n, sizeof(n));
return true;
}
else
return false;
} // CordbReferenceValue::CopyLiteralData
// destructor
CordbReferenceValue::~CordbReferenceValue()
{
DTOR_ENTRY(this);
LOG((LF_CORDB,LL_EVERYTHING,"CRV::~CRV: this:0x%x\n",this));
_ASSERTE(IsNeutered());
} // CordbReferenceValue::~CordbReferenceValue
void CordbReferenceValue::Neuter()
{
if (m_valueHome.m_pHome != NULL)
{
m_valueHome.m_pHome->Clear();
delete m_valueHome.m_pHome;
m_valueHome.m_pHome = NULL;
}
m_realTypeOfTypedByref = NULL;
CordbValue::Neuter();
} // CordbReferenceValue::Neuter
HRESULT CordbReferenceValue::QueryInterface(REFIID id, void **pInterface)
{
if (id == IID_ICorDebugValue)
{
*pInterface = static_cast<ICorDebugValue*>(static_cast<ICorDebugReferenceValue*>(this));
}
else if (id == IID_ICorDebugValue2)
{
*pInterface = static_cast<ICorDebugValue2*>(this);
}
else if (id == IID_ICorDebugValue3)
{
*pInterface = static_cast<ICorDebugValue3*>(this);
}
else if (id == IID_ICorDebugReferenceValue)
{
*pInterface = static_cast<ICorDebugReferenceValue*>(this);
}
else if (id == IID_IUnknown)
{
*pInterface = static_cast<IUnknown*>(static_cast<ICorDebugReferenceValue*>(this));
}
else
{
*pInterface = NULL;
return E_NOINTERFACE;
}
ExternalAddRef();
return S_OK;
} // CordbReferenceValue::QueryInterface
// gets the type of the referent of the object ref
// Arguments:
// output: pType - the type of the value. The caller must guarantee that pType is non-null.
// Return Value: S_OK on success, E_INVALIDARG on failure
HRESULT CordbReferenceValue::GetType(CorElementType *pType)
{
LIMITED_METHOD_CONTRACT;
FAIL_IF_NEUTERED(this);
VALIDATE_POINTER_TO_OBJECT(pType, CorElementType *);
if( m_type == NULL )
{
// We may not have a CordbType if we were created from a GC handle to NULL
_ASSERTE( m_info.objTypeData.elementType == ELEMENT_TYPE_CLASS );
_ASSERTE(!m_valueHome.ObjHandleIsNull());
_ASSERTE( m_info.objRef == NULL );
*pType = m_info.objTypeData.elementType;
}
else
{
// The element type stored in both places should match
_ASSERTE( m_info.objTypeData.elementType == m_type->m_elementType );
*pType = m_type->m_elementType;
}
return S_OK;
} // CordbReferenceValue::GetType
// gets the remote (LS) address of the reference. This may return NULL if the
// reference is a literal or resides in a register.
// Arguments:
// output: pAddress - the LS location of the reference. The caller must guarantee pAddress is non-null,
// but the contents may be null after the call if the reference is enregistered or is
// the value of a field or element of some other Cordb*Value instance.
// Return Value: S_OK on success or E_INVALIDARG if pAddress is null
HRESULT CordbReferenceValue::GetAddress(CORDB_ADDRESS *pAddress)
{
PUBLIC_REENTRANT_API_ENTRY(this);
VALIDATE_POINTER_TO_OBJECT(pAddress, CORDB_ADDRESS *);
*pAddress = m_valueHome.m_pHome ? m_valueHome.m_pHome->GetAddress() : NULL;
return (S_OK);
}
// Determines whether the reference is null
// Arguments:
// output - pfIsNull - pointer to a BOOL that will be set to true iff this represents a
// null reference
// Return Value: S_OK on success or E_INVALIDARG if pfIsNull is null
HRESULT CordbReferenceValue::IsNull(BOOL * pfIsNull)
{
PUBLIC_REENTRANT_API_ENTRY(this);
FAIL_IF_NEUTERED(this);
VALIDATE_POINTER_TO_OBJECT(pfIsNull, BOOL *);
if (m_isLiteral || (m_info.objRef == NULL))
*pfIsNull = TRUE;
else
*pfIsNull = FALSE;
return S_OK;
}
// gets the value (object address) of this CordbReferenceValue
// Arguments:
// output: pTo - reference value
// Return Value: S_OK on success or E_INVALIDARG if pAddress is null
HRESULT CordbReferenceValue::GetValue(CORDB_ADDRESS *pAddress)
{
PUBLIC_REENTRANT_API_ENTRY(this);
VALIDATE_POINTER_TO_OBJECT(pAddress, CORDB_ADDRESS *);
FAIL_IF_NEUTERED(this);
// Copy out the value, which is simply the value the object reference.
if (m_isLiteral)
*pAddress = NULL;
else
*pAddress = PTR_TO_CORDB_ADDRESS(m_info.objRef);
return S_OK;
}
// sets the value of the reference
// Arguments:
// input: address - the new reference--this must be a LS address
// Return Value: S_OK on success or E_INVALIDARG or write process memory errors
// Note: We make no effort to ensure that the new reference is of the same type as the old one.
// We simply assume it is. As long as this assumption is correct, we only need to update information about
// the referent if it's a string (its length can change).
// @dbgtodo Microsoft inspection: consider whether it's worthwhile to verify that the type of the new referent is
// the same as the type of the existing one. We'd have to do most of the work for a call to InitRef to do
// this, since we need to know the type of the new referent.
HRESULT CordbReferenceValue::SetValue(CORDB_ADDRESS address)
{
PUBLIC_API_ENTRY(this);
FAIL_IF_NEUTERED(this);
ATT_REQUIRE_STOPPED_MAY_FAIL(GetProcess());
HRESULT hr = S_OK;
// If this is a heap object, ideally we'd prevent violations of AppDomain isolation
// here. However, we have no reliable way of determining what AppDomain the address is in.
// Can't change literal refs.
if (m_isLiteral)
{
return E_INVALIDARG;
}
// Either we know the type, or it's a handle to a null value
_ASSERTE((m_type != NULL) ||
(!m_valueHome.ObjHandleIsNull() && (m_info.objRef == NULL)));
EX_TRY
{
m_valueHome.m_pHome->SetValue(MemoryRange(&address, sizeof(void *)), m_type); // throws
}
EX_CATCH_HRESULT(hr);
if (SUCCEEDED(hr))
{
// That worked, so update the copy of the value we have in
// our local cache.
m_info.objRef = CORDB_ADDRESS_TO_PTR(address);
if (m_info.objTypeData.elementType == ELEMENT_TYPE_STRING)
{
// update information about the string
InitRef(MemoryRange(&m_info.objRef, sizeof (void *)));
}
// All other data in m_info is no longer valid, and we may have invalidated other