Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/5.0] Fix usage of process_vm_readv in createdump #50478

Merged
merged 2 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/coreclr/src/debug/createdump/crashinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ CrashInfo::CrashInfo(pid_t pid) :
m_task = 0;
#else
m_auxvValues.fill(0);
#ifndef HAVE_PROCESS_VM_READV
m_fd = -1;
#endif
#endif
}

CrashInfo::~CrashInfo()
Expand Down
5 changes: 2 additions & 3 deletions src/coreclr/src/debug/createdump/crashinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ class CrashInfo : public ICLRDataEnumMemoryRegionsCallback,
#ifdef __APPLE__
vm_map_t m_task; // the mach task for the process
#else
#ifndef HAVE_PROCESS_VM_READV
bool m_canUseProcVmReadSyscall;
int m_fd; // /proc/<pid>/mem handle
#endif
#endif
std::string m_coreclrPath; // the path of the coreclr module or empty if none
#ifdef __APPLE__
Expand Down Expand Up @@ -112,7 +111,7 @@ class CrashInfo : public ICLRDataEnumMemoryRegionsCallback,
void VisitModule(uint64_t baseAddress, std::string& moduleName);
void VisitProgramHeader(uint64_t loadbias, uint64_t baseAddress, ElfW(Phdr)* phdr);
bool EnumerateModuleMappings();
#endif
#endif
bool EnumerateMemoryRegionsWithDAC(MINIDUMP_TYPE minidumpType);
bool EnumerateManagedModules(IXCLRDataProcess* pClrDataProcess);
bool UnwindAllThreads(IXCLRDataProcess* pClrDataProcess);
Expand Down
34 changes: 23 additions & 11 deletions src/coreclr/src/debug/createdump/crashinfounix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ bool GetStatus(pid_t pid, pid_t* ppid, pid_t* tgid, std::string* name);
bool
CrashInfo::Initialize()
{
#ifndef HAVE_PROCESS_VM_READV
char memPath[128];
_snprintf_s(memPath, sizeof(memPath), sizeof(memPath), "/proc/%lu/mem", m_pid);

Expand All @@ -18,12 +17,13 @@ CrashInfo::Initialize()
fprintf(stderr, "open(%s) FAILED %d (%s)\n", memPath, errno, strerror(errno));
return false;
}
#endif
// Get the process info
if (!GetStatus(m_pid, &m_ppid, &m_tgid, &m_name))
{
return false;
}

m_canUseProcVmReadSyscall = true;
return true;
}

Expand All @@ -39,13 +39,11 @@ CrashInfo::CleanupAndResumeProcess()
waitpid(thread->Tid(), &waitStatus, __WALL);
}
}
#ifndef HAVE_PROCESS_VM_READV
if (m_fd != -1)
{
close(m_fd);
m_fd = -1;
}
#endif
}

//
Expand Down Expand Up @@ -253,7 +251,7 @@ CrashInfo::GetDSOInfo()
int phnum = m_auxvValues[AT_PHNUM];
assert(m_auxvValues[AT_PHENT] == sizeof(Phdr));
assert(phnum != PN_XNUM);
return EnumerateElfInfo(phdrAddr, phnum);
return EnumerateElfInfo(phdrAddr, phnum);
}

//
Expand Down Expand Up @@ -334,17 +332,31 @@ CrashInfo::ReadProcessMemory(void* address, void* buffer, size_t size, size_t* r
{
assert(buffer != nullptr);
assert(read != nullptr);
*read = 0;

#ifdef HAVE_PROCESS_VM_READV
iovec local{ buffer, size };
iovec remote{ address, size };
*read = process_vm_readv(m_pid, &local, 1, &remote, 1, 0);
#else
assert(m_fd != -1);
*read = pread64(m_fd, buffer, size, (off64_t)address);
if (m_canUseProcVmReadSyscall)
{
iovec local{ buffer, size };
iovec remote{ address, size };
*read = process_vm_readv(m_pid, &local, 1, &remote, 1, 0);
}

if (!m_canUseProcVmReadSyscall || (*read == (size_t)-1 && errno == EPERM))
#endif
{
// If we've failed, avoid going through expensive syscalls
// After all, the use of process_vm_readv is largely as a
// performance optimization.
m_canUseProcVmReadSyscall = false;
assert(m_fd != -1);
*read = pread64(m_fd, buffer, size, (off64_t)address);
}

if (*read == (size_t)-1)
{
int readErrno = errno;
hoyosjs marked this conversation as resolved.
Show resolved Hide resolved
TRACE_VERBOSE("ReadProcessMemory FAILED, addr: %" PRIA PRIx ", size: %zu, ERRNO %d: %s\n", address, size, readErrno, strerror(readErrno));
return false;
}
return true;
Expand Down