|
8 | 8 | using System.Runtime.InteropServices; |
9 | 9 | using System.Runtime.InteropServices.Marshalling; |
10 | 10 | using Microsoft.Diagnostics.DataContractReader.Contracts; |
| 11 | +using DACF = Microsoft.Diagnostics.DataContractReader.Contracts.DebuggerAssemblyControlFlags; |
11 | 12 |
|
12 | 13 | namespace Microsoft.Diagnostics.DataContractReader.Legacy; |
13 | 14 |
|
@@ -37,6 +38,17 @@ private int StringHolderAssignCopy(nint stringHolder, string str) |
37 | 38 | } |
38 | 39 | } |
39 | 40 |
|
| 41 | + private bool CORProfilerPresent() |
| 42 | + { |
| 43 | + if (!_target.TryReadGlobalPointer(Constants.Globals.ProfilerControlBlock, out TargetPointer? profControlBlockAddress)) |
| 44 | + return false; |
| 45 | + |
| 46 | + Target.TypeInfo type = _target.GetTypeInfo(DataType.ProfControlBlock); |
| 47 | + TargetPointer mainProfInterface = _target.ReadPointerField(profControlBlockAddress.Value, type, "MainProfilerProfInterface"); |
| 48 | + int notificationCount = _target.ReadField<int>(profControlBlockAddress.Value, type, "NotificationProfilerCount"); |
| 49 | + return mainProfInterface != TargetPointer.Null || notificationCount > 0; |
| 50 | + } |
| 51 | + |
40 | 52 | public DacDbiImpl(Target target, object? legacyObj) |
41 | 53 | { |
42 | 54 | _target = target; |
@@ -315,7 +327,51 @@ public int GetCompilerFlags(ulong vmAssembly, Interop.BOOL* pfAllowJITOpts, Inte |
315 | 327 | } |
316 | 328 |
|
317 | 329 | public int SetCompilerFlags(ulong vmAssembly, Interop.BOOL fAllowJitOpts, Interop.BOOL fEnableEnC) |
318 | | - => LegacyFallbackHelper.CanFallback() && _legacy is not null ? _legacy.SetCompilerFlags(vmAssembly, fAllowJitOpts, fEnableEnC) : HResults.E_NOTIMPL; |
| 330 | + { |
| 331 | + int hr = HResults.S_OK; |
| 332 | + try |
| 333 | + { |
| 334 | + Contracts.ILoader loader = _target.Contracts.Loader; |
| 335 | + Contracts.ModuleHandle handle = loader.GetModuleHandleFromAssemblyPtr(new TargetPointer(vmAssembly)); |
| 336 | + |
| 337 | + DACF debuggerInfoBits = loader.GetDebuggerInfoBits(handle); |
| 338 | + DACF controlFlags = debuggerInfoBits & ~(DACF.DACF_ALLOW_JIT_OPTS | DACF.DACF_ENC_ENABLED); |
| 339 | + controlFlags &= DACF.DACF_CONTROL_FLAGS_MASK; |
| 340 | + |
| 341 | + if (fAllowJitOpts != Interop.BOOL.FALSE) |
| 342 | + { |
| 343 | + controlFlags |= DACF.DACF_ALLOW_JIT_OPTS; |
| 344 | + } |
| 345 | + |
| 346 | + if (fEnableEnC != Interop.BOOL.FALSE) |
| 347 | + { |
| 348 | + bool fIgnorePdbs = (debuggerInfoBits & DACF.DACF_IGNORE_PDBS) != 0; |
| 349 | + bool canSetEnC = (loader.GetFlags(handle) & Contracts.ModuleFlags.EncCapable) != 0 && !CORProfilerPresent() && fIgnorePdbs; |
| 350 | + if (canSetEnC) |
| 351 | + { |
| 352 | + controlFlags |= DACF.DACF_ENC_ENABLED; |
| 353 | + } |
| 354 | + else |
| 355 | + { |
| 356 | + hr = CorDbgHResults.CORDBG_S_NOT_ALL_BITS_SET; |
| 357 | + } |
| 358 | + } |
| 359 | + |
| 360 | + loader.SetDebuggerInfoBits(handle, controlFlags); |
| 361 | + } |
| 362 | + catch (System.Exception ex) |
| 363 | + { |
| 364 | + hr = ex.HResult; |
| 365 | + } |
| 366 | +#if DEBUG |
| 367 | + if (_legacy is not null) |
| 368 | + { |
| 369 | + int hrLocal = _legacy.SetCompilerFlags(vmAssembly, fAllowJitOpts, fEnableEnC); |
| 370 | + Debug.ValidateHResult(hr, hrLocal); |
| 371 | + } |
| 372 | +#endif |
| 373 | + return hr; |
| 374 | + } |
319 | 375 |
|
320 | 376 | public int EnumerateAssembliesInAppDomain(ulong vmAppDomain, nint fpCallback, nint pUserData) |
321 | 377 | => LegacyFallbackHelper.CanFallback() && _legacy is not null ? _legacy.EnumerateAssembliesInAppDomain(vmAppDomain, fpCallback, pUserData) : HResults.E_NOTIMPL; |
|
0 commit comments