Skip to content

Commit

Permalink
Move IsDebuggerPresent to minipal, convert to QCall
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 committed Oct 14, 2024
1 parent 8a7fa48 commit 2084bcc
Show file tree
Hide file tree
Showing 49 changed files with 336 additions and 452 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ static void NotifyOfCrossThreadDependencySlow()
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool LaunchInternal();

// Returns whether or not a debugger is attached to the process.
//
public static extern bool IsAttached
{
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
// Returns whether or not a managed debugger is attached to the process.
public static bool IsAttached => IsManagedDebuggerAttached();

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "DebugDebugger_IsManagedDebuggerAttached")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool IsManagedDebuggerAttached();

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "DebugDebugger_IsAnyDebuggerAttached")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool IsAnyDebuggerAttached();

// Constants representing the importance level of messages to be logged.
//
Expand All @@ -85,9 +88,11 @@ public static extern bool IsAttached
private static partial void LogInternal(int level, string? category, string? message);

// Checks to see if an attached debugger has logging enabled
//
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool IsLogging();
public static bool IsLogging() => IsLoggingHelper();

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "DebugDebugger_IsLoggingHelper")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool IsLoggingHelper();

// Posts a custom notification for the attached debugger. If there is no
// debugger attached, has no effect. The debugger may or may not
Expand Down
13 changes: 6 additions & 7 deletions src/coreclr/debug/ee/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,7 @@ void Debugger::SendRawEvent(const DebuggerIPCEvent * pManagedEvent)

// If no debugger attached, then don't bother raising a 1st-chance exception because nobody will sniff it.
// @dbgtodo iDNA: in iDNA case, the recorder may sniff it.
if (!IsDebuggerPresent())
if (!minipal_is_native_debugger_present())
{
return;
}
Expand Down Expand Up @@ -6460,7 +6460,7 @@ HRESULT Debugger::LaunchDebuggerForUser(Thread * pThread, EXCEPTION_POINTERS * p
//
SendUserBreakpointAndSynchronize(g_pEEInterface->GetThread());
}
else if (!CORDebuggerAttached() && IsDebuggerPresent())
else if (!CORDebuggerAttached() && minipal_is_native_debugger_present())
{
//
// If the registered debugger is not a managed debugger, send a native breakpoint
Expand All @@ -6476,7 +6476,7 @@ HRESULT Debugger::LaunchDebuggerForUser(Thread * pThread, EXCEPTION_POINTERS * p
DebugBreak();
}

if (!IsDebuggerPresent())
if (!minipal_is_native_debugger_present())
{
LOG((LF_CORDB, LL_ERROR, "D::LDFU: Failed to launch the debugger.\n"));
}
Expand Down Expand Up @@ -7097,7 +7097,7 @@ void Debugger::JitAttach(Thread * pThread, EXCEPTION_POINTERS * pExceptionInfo,
CONTRACTL_END;

// Don't do anything if there is a native debugger already attached or the debugging support has been disabled.
if (IsDebuggerPresent() || m_pRCThread == NULL)
if (minipal_is_native_debugger_present() || m_pRCThread == NULL)
return;

GCX_PREEMP_EEINTERFACE_TOGGLE_IFTHREAD();
Expand Down Expand Up @@ -7172,7 +7172,7 @@ void Debugger::EnsureDebuggerAttached(Thread * pThread, EXCEPTION_POINTERS * pEx
{
// if the debugger is already attached then we can't launch one
// and whatever attach state we are in is just what we get
if(IsDebuggerPresent())
if(minipal_is_native_debugger_present())
{
// unblock other threads waiting on our attach and clean up
PostJitAttach();
Expand Down Expand Up @@ -8910,7 +8910,7 @@ void Debugger::SendUserBreakpoint(Thread * thread)
// On jit-attach, we just send the UserBreak event. Don't do an extra step-out.
SendUserBreakpointAndSynchronize(thread);
}
else if (IsDebuggerPresent())
else if (minipal_is_native_debugger_present())
{
DebugBreak();
}
Expand Down Expand Up @@ -16784,4 +16784,3 @@ void Debugger::MulticastTraceNextStep(DELEGATEREF pbDel, INT32 count)
#endif //DACCESS_COMPILE

#endif //DEBUGGING_SUPPORTED

2 changes: 1 addition & 1 deletion src/coreclr/debug/ee/debuggermessagebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static void DbgPrintf(const LPCSTR szFormat, ...)

va_end(args);

if (IsDebuggerPresent())
if (minipal_is_native_debugger_present())
{
OutputDebugStringUtf8(szBuffer);
}
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/dlls/mscordac/mscordac_unixexports.src
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ nativeStringResourceTable_mscorrc
#PAL_GetTransportPipeName
#PAL_InitializeDLL
#PAL_TerminateEx
#PAL_IsDebuggerPresent
#PAL_OpenProcessMemory
#PAL_CloseProcessMemory
#PAL_ReadProcessMemory
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/hosts/corerun/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(CLR_CMAKE_KEEP_NATIVE_SYMBOLS TRUE)
add_executable_clr(corerun
corerun.cpp
dotenv.cpp
${CLR_SRC_NATIVE_DIR}/minipal/is_native_debugger_present.c
native.rc
)

Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/hosts/corerun/corerun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace pal
#ifdef TARGET_WINDOWS
#define CDECL __cdecl
#include <Windows.h>
#include <minipal/is_native_debugger_present.h>

#define DLL_EXPORT __declspec(dllexport)
#define MAIN __cdecl wmain
Expand Down Expand Up @@ -138,7 +139,7 @@ namespace pal

inline debugger_state_t is_debugger_attached()
{
return (::IsDebuggerPresent() == TRUE) ? debugger_state_t::attached : debugger_state_t::not_attached;
return (minipal_is_native_debugger_present() == TRUE) ? debugger_state_t::attached : debugger_state_t::not_attached;
}

inline bool does_file_exist(const string_t& file_path)
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/jit/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <corjit.h> // for CORJIT_INTERNALERROR
#include <safemath.h> // For FitsIn, used by SafeCvt methods.

#include <minipal/is_native_debugger_present.h>

#define FATAL_JIT_EXCEPTION 0x02345678
class Compiler;

Expand Down Expand Up @@ -247,7 +249,7 @@ extern void notYetImplemented(const char* msg, const char* file, unsigned line);
#define BreakIfDebuggerPresent() \
do \
{ \
if (IsDebuggerPresent()) \
if (minipal_is_native_debugger_present()) \
DebugBreak(); \
} while (0)
#endif
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/minipal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ if (CLR_CMAKE_HOST_UNIX)
else (CLR_CMAKE_HOST_UNIX)
add_subdirectory(Windows)
endif (CLR_CMAKE_HOST_UNIX)

1 change: 1 addition & 0 deletions src/coreclr/minipal/Unix/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(SOURCES
doublemapping.cpp
dn-u16.cpp
${CLR_SRC_NATIVE_DIR}/minipal/is_native_debugger_present.c
${CLR_SRC_NATIVE_DIR}/minipal/time.c
)

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/minipal/Windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(SOURCES
doublemapping.cpp
dn-u16.cpp
${CLR_SRC_NATIVE_DIR}/minipal/is_native_debugger_present.c
${CLR_SRC_NATIVE_DIR}/minipal/utf8.c
${CLR_SRC_NATIVE_DIR}/minipal/time.c
)
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/nativeaot/Runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ set(COMMON_RUNTIME_SOURCES
${GC_DIR}/softwarewritewatch.cpp

${CLR_SRC_NATIVE_DIR}/minipal/cpufeatures.c
${CLR_SRC_NATIVE_DIR}/minipal/is_native_debugger_present.c
${CLR_SRC_NATIVE_DIR}/minipal/time.c
)

Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/nativeaot/Runtime/MiscHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "yieldprocessornormalized.h"
#include "RhConfig.h"
#include <minipal/cpuid.h>
#include <minipal/is_native_debugger_present.h>

FCIMPL0(void, RhDebugBreak)
{
Expand Down Expand Up @@ -394,6 +395,11 @@ EXTERN_C int32_t QCALLTYPE RhpGetCurrentThreadStackTrace(void* pOutputBuffer, ui
return RhpCalculateStackTraceWorker(pOutputBuffer, outputBufferLength, pAddressInCurrentFrame);
}

EXTERN_C UInt32_BOOL QCALLTYPE DebugDebugger_IsAnyDebuggerAttached()
{
return minipal_is_native_debugger_present();
}

FCIMPL2(FC_BOOL_RET, RhCompareObjectContentsAndPadding, Object* pObj1, Object* pObj2)
{
ASSERT(pObj1->GetMethodTable() == pObj2->GetMethodTable());
Expand Down
6 changes: 0 additions & 6 deletions src/coreclr/nativeaot/Runtime/PalRedhawkFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ inline UInt32_BOOL PalInitializeCriticalSectionEx(CRITICAL_SECTION * arg1, uint3
return InitializeCriticalSectionEx(arg1, arg2, arg3);
}

extern "C" UInt32_BOOL __stdcall IsDebuggerPresent();
inline UInt32_BOOL PalIsDebuggerPresent()
{
return IsDebuggerPresent();
}

extern "C" void __stdcall LeaveCriticalSection(CRITICAL_SECTION *);
inline void PalLeaveCriticalSection(CRITICAL_SECTION * arg1)
{
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/nativeaot/Runtime/rhassert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "PalRedhawk.h"
#include "rhassert.h"

#include <minipal/is_native_debugger_present.h>

#ifdef _DEBUG

void Assert(const char * expr, const char * file, uint32_t line_num, const char * message)
Expand All @@ -28,7 +30,7 @@ void Assert(const char * expr, const char * file, uint32_t line_num, const char
fflush(stdout);

// If there's no debugger attached, we just FailFast
if (!PalIsDebuggerPresent())
if (!minipal_is_native_debugger_present())
PalRaiseFailFastException(NULL, NULL, FAIL_FAST_GENERATE_EXCEPTION_ADDRESS);

// If there is a debugger attached, we break and then allow continuation.
Expand Down
11 changes: 0 additions & 11 deletions src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,17 +964,6 @@ extern "C" void LeaveCriticalSection(CRITICAL_SECTION * lpCriticalSection)
pthread_mutex_unlock(&lpCriticalSection->mutex);
}

extern "C" UInt32_BOOL IsDebuggerPresent()
{
#ifdef HOST_WASM
// For now always true since the browser will handle it in case of WASM.
return UInt32_TRUE;
#else
// UNIXTODO: Implement this function
return UInt32_FALSE;
#endif
}

extern "C" UInt32_BOOL SetEvent(HANDLE event)
{
EventUnixHandle* unixHandle = (EventUnixHandle*)event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,6 @@
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.ReportEvent_IntPtr.cs">
<Link>Common\Interop\Windows\Advapi32\Interop.ReportEvent_IntPtr.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.IsDebuggerPresent.cs">
<Link>Interop\Windows\Kernel32\Interop.IsDebuggerPresent.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Interop\Windows\Kernel32\Interop.RaiseFailFastException.cs">
<Link>Interop\Windows\Kernel32\Interop.RaiseFailFastException.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace System.Diagnostics
{
Expand All @@ -13,7 +15,7 @@ public static void Break()
{
#if TARGET_WINDOWS
// IsAttached is always true when IsDebuggerPresent is true, so no need to check for it
if (Interop.Kernel32.IsDebuggerPresent())
if (Debugger.IsAnyDebuggerAttached())
Debug.DebugBreak();
#else
// UNIXTODO: Implement Debugger.Break
Expand Down Expand Up @@ -74,5 +76,9 @@ public static bool IsLogging()
}
return false;
}

[LibraryImport(RuntimeImports.RuntimeLibrary, EntryPoint = "DebugDebugger_IsAnyDebuggerAttached")]
private static partial int DebugDebugger_IsAnyDebuggerAttached();
internal static bool IsAnyDebuggerAttached() => DebugDebugger_IsAnyDebuggerAttached() != 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static bool ShouldLogInEventLog
{
get
{
if (Interop.Kernel32.IsDebuggerPresent())
if (Debugger.IsAnyDebuggerAttached())
return false;

if (s_once == 1 || Interlocked.Exchange(ref s_once, 1) == 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace System.Runtime
// optional library, those methods can be moved to a different file/namespace/dll
internal static partial class RuntimeImports
{
private const string RuntimeLibrary = "*";
internal const string RuntimeLibrary = "*";

[MethodImplAttribute(MethodImplOptions.InternalCall)]
[RuntimeImport(RuntimeLibrary, "RhGetCrashInfoBuffer")]
Expand Down Expand Up @@ -425,7 +425,7 @@ internal static IntPtr RhHandleAllocDependent(object primary, object secondary)
// Yield the cpu to another thread ready to process, if one is available.
[LibraryImport(RuntimeLibrary, EntryPoint = "RhYield")]
private static partial int _RhYield();
internal static bool RhYield() { return (_RhYield() != 0); }
internal static bool RhYield() => _RhYield() != 0;

[LibraryImport(RuntimeLibrary, EntryPoint = "RhFlushProcessWriteBuffers")]
internal static partial void RhFlushProcessWriteBuffers();
Expand Down
7 changes: 0 additions & 7 deletions src/coreclr/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,6 @@ extern bool g_arm64_atomics_present;

/******************* PAL-Specific Entrypoints *****************************/

#define IsDebuggerPresent PAL_IsDebuggerPresent

PALIMPORT
BOOL
PALAPI
PAL_IsDebuggerPresent();

#define DLL_PROCESS_ATTACH 1
#define DLL_THREAD_ATTACH 2
#define DLL_THREAD_DETACH 3
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ if(CLR_CMAKE_TARGET_LINUX)
add_library(tracepointprovider OBJECT misc/tracepointprovider.cpp)
endif(CLR_CMAKE_TARGET_LINUX)

add_library(is_native_debugger_present STATIC ${CLR_SRC_NATIVE_DIR}/minipal/is_native_debugger_present.c)
target_link_libraries(coreclrpal PRIVATE is_native_debugger_present)

if(CLR_CMAKE_TARGET_OSX)
find_library(COREFOUNDATION CoreFoundation)
find_library(CORESERVICES CoreServices)
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/pal/src/exception/machexception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ SET_DEFAULT_DEBUG_CHANNEL(EXCEPT); // some headers have code with asserts, so do
#include "pal/virtual.h"
#include "pal/map.hpp"
#include "pal/environ.h"

#include <minipal/is_native_debugger_present.h>
#include <minipal/utils.h>

#include "machmessage.h"
Expand Down Expand Up @@ -180,7 +182,7 @@ GetExceptionMask()
}
else
{
if (PAL_IsDebuggerPresent())
if (minipal_is_native_debugger_present())
{
exMode = MachException_SuppressDebugging;
}
Expand Down
Loading

0 comments on commit 2084bcc

Please sign in to comment.