Skip to content

Commit

Permalink
Use /proc/<pid>/mem to read memory in remote DBI data target. It make…
Browse files Browse the repository at this point in the history
…s the test case run at (dotnet/coreclr#24844)

least 4 to 5 times faster than before.

Fallback to old transport ReadMemory if /proc/<pid>/mem can't be opened. This happens
on attach because of permissions/access, but works fine on the launch (the most
important case).

Commit migrated from dotnet/coreclr@e46ae59
  • Loading branch information
mikem8361 authored May 30, 2019
1 parent b3932bc commit 7d99ecc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
35 changes: 27 additions & 8 deletions src/coreclr/src/debug/di/shimremotedatatarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "dbgtransportsession.h"
#include "dbgtransportmanager.h"


class ShimRemoteDataTarget : public ShimDataTarget
{
public:
Expand Down Expand Up @@ -69,6 +68,7 @@ class ShimRemoteDataTarget : public ShimDataTarget
private:
DbgTransportTarget * m_pProxy;
DbgTransportSession * m_pTransport;
int m_fd; // /proc/<pid>/mem handle
};


Expand Down Expand Up @@ -103,6 +103,10 @@ ShimRemoteDataTarget::ShimRemoteDataTarget(DWORD processId,

m_fpContinueStatusChanged = NULL;
m_pContinueStatusChangedUserData = NULL;

char memPath[128];
_snprintf_s(memPath, sizeof(memPath), sizeof(memPath), "/proc/%lu/mem", m_processId);
m_fd = _open(memPath, 0); // O_RDONLY
}

//---------------------------------------------------------------------------------------
Expand All @@ -127,11 +131,15 @@ ShimRemoteDataTarget::~ShimRemoteDataTarget()

void ShimRemoteDataTarget::Dispose()
{
if (m_fd != -1)
{
_close(m_fd);
m_fd = -1;
}
if (m_pTransport != NULL)
{
m_pProxy->ReleaseTransport(m_pTransport);
}

m_pTransport = NULL;
m_hr = CORDBG_E_OBJECT_NEUTERED;
}
Expand Down Expand Up @@ -244,21 +252,32 @@ ShimRemoteDataTarget::GetPlatform(

// impl of interface method ICorDebugDataTarget::ReadVirtual
HRESULT STDMETHODCALLTYPE
ShimRemoteDataTarget::ReadVirtual(
ShimRemoteDataTarget::ReadVirtual(
CORDB_ADDRESS address,
PBYTE pBuffer,
ULONG32 cbRequestSize,
ULONG32 *pcbRead)
{
ReturnFailureIfStateNotOk();

HRESULT hr = E_FAIL;
hr = m_pTransport->ReadMemory(reinterpret_cast<BYTE *>(CORDB_ADDRESS_TO_PTR(address)),
pBuffer,
cbRequestSize);
size_t read = cbRequestSize;
HRESULT hr = S_OK;

if (m_fd != -1)
{
read = _pread(m_fd, pBuffer, cbRequestSize, (ULONG64)address);
if (read == -1)
{
hr = E_FAIL;
}
}
else
{
hr = m_pTransport->ReadMemory(reinterpret_cast<BYTE *>(CORDB_ADDRESS_TO_PTR(address)), pBuffer, cbRequestSize);
}
if (pcbRead != NULL)
{
*pcbRead = (SUCCEEDED(hr) ? cbRequestSize : 0);
*pcbRead = (SUCCEEDED(hr) ? read : 0);
}
return hr;
}
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/src/dlls/mscordac/mscordac_unixexports.src
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ nativeStringResourceTable_mscorrc_debug
#PAL_wcscspn
#PAL_wcscat
#PAL_wcsstr
#PAL__open
#PAL__pread
#PAL__close

#_wcsicmp
#_stricmp
Expand Down
5 changes: 4 additions & 1 deletion src/coreclr/src/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -4138,6 +4138,7 @@ PAL_GetCurrentThreadAffinitySet(SIZE_T size, UINT_PTR* data);
#define _strdup PAL__strdup
#define _getcwd PAL__getcwd
#define _open PAL__open
#define _pread PAL__pread
#define _close PAL__close
#define _wcstoui64 PAL__wcstoui64
#define _flushall PAL__flushall
Expand Down Expand Up @@ -4507,7 +4508,9 @@ PALIMPORT char * __cdecl ctime(const time_t *);
#endif // !PAL_STDCPP_COMPAT

PALIMPORT int __cdecl _open_osfhandle(INT_PTR, int);
PALIMPORT int __cdecl _close(int);
PALIMPORT DLLEXPORT int __cdecl _open(const char *szPath, int nFlags, ...);
PALIMPORT DLLEXPORT size_t __cdecl _pread(int fd, void *buf, size_t nbytes, ULONG64 offset);
PALIMPORT DLLEXPORT int __cdecl _close(int);
PALIMPORT DLLEXPORT int __cdecl _flushall();

#ifdef PAL_STDCPP_COMPAT
Expand Down
12 changes: 12 additions & 0 deletions src/coreclr/src/pal/src/cruntime/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,18 @@ PAL_FILE * __cdecl PAL_get_stderr(int caller)
return &PAL_Stderr;
}

/*++
Function:
PAL_pread
See msdn for more details.
--*/
size_t __cdecl PAL__pread(int fd, void *buf, size_t nbytes, ULONG64 offset)
{
return pread(fd, buf, nbytes, offset);
}

/*++
Expand Down

0 comments on commit 7d99ecc

Please sign in to comment.