From c5c1b7fd561e6bf4ea792accfee69bdf138fac07 Mon Sep 17 00:00:00 2001 From: kjayapra-amd Date: Tue, 10 Aug 2021 11:42:51 -0400 Subject: [PATCH] SWDEV-241902 - Using COMGR APIs for extracing code object. Change-Id: I96f1cee9c28f89f0b0fa5af0c2ae7966beb3207d [ROCm/clr commit: 0d9e1de4e37dfd853e1b8ba919231345a3ef8422] --- projects/clr/rocclr/os/os.hpp | 4 +++ projects/clr/rocclr/os/os_posix.cpp | 53 ++++++++++++++++++++++++++++- projects/clr/rocclr/os/os_win32.cpp | 6 ++++ projects/clr/rocclr/utils/flags.hpp | 4 ++- 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/projects/clr/rocclr/os/os.hpp b/projects/clr/rocclr/os/os.hpp index b4f6816bc2..b66e4777e6 100644 --- a/projects/clr/rocclr/os/os.hpp +++ b/projects/clr/rocclr/os/os.hpp @@ -115,6 +115,10 @@ class Os : AllStatic { // Given a valid file name, returns file descriptor and file size static bool GetFileHandle(const char* fname, FileDesc* fd_ptr, size_t* sz_ptr); + // Returns the file name & file offset of mapped memory if the file is mapped. + static bool FindFileNameFromAddress(const void* image, std::string* fname_ptr, + size_t* foffset_ptr); + // Given a valid file descriptor returns mmaped memory for size and offset static bool MemoryMapFileDesc(FileDesc fdesc, size_t fsize, size_t foffset, const void** mmap_ptr); diff --git a/projects/clr/rocclr/os/os_posix.cpp b/projects/clr/rocclr/os/os_posix.cpp index 87c1ac3387..d4fcc9662d 100644 --- a/projects/clr/rocclr/os/os_posix.cpp +++ b/projects/clr/rocclr/os/os_posix.cpp @@ -63,6 +63,7 @@ #include #include #include +#include namespace amd { @@ -746,7 +747,6 @@ void Os::getAppPathAndFileName(std::string& appName, std::string& appPathAndName return; } - bool Os::GetURIFromMemory(const void* image, size_t image_size, std::string& uri) { pid_t pid = getpid(); std::ostringstream uri_stream; @@ -790,6 +790,57 @@ bool Os::GetFileHandle(const char* fname, FileDesc* fd_ptr, size_t* sz_ptr) { return true; } +bool amd::Os::FindFileNameFromAddress(const void* image, std::string* fname_ptr, + size_t* foffset_ptr) { + + // Get the list of mapped file list + bool ret_value = false; + std::ifstream proc_maps; + proc_maps.open("/proc/self/maps", std::ifstream::in); + if (!proc_maps.is_open() || !proc_maps.good()) { + return ret_value; + } + + // For every line on the list map find out low, high address + std::string line; + while (std::getline(proc_maps, line)) { + char dash; + std::stringstream tokens(line); + uintptr_t low_address, high_address; + tokens >> std::hex >> low_address >> std::dec + >> dash + >> std::hex >> high_address >> std::dec; + if (dash != '-') { + continue; + } + + // If address is > low_address and < high_address, then this + // is the mapped file. Get the URI path and offset. + uintptr_t address = reinterpret_cast(image); + if ((address >= low_address) && (address < high_address)) { + std::string permissions, device, uri_file_path; + size_t offset; + uint64_t inode; + tokens >> permissions + >> std::hex >> offset >> std::dec + >> device + >> inode + >> uri_file_path; + + if (inode == 0 || uri_file_path.empty()) { + return ret_value; + } + + *fname_ptr = uri_file_path; + *foffset_ptr = offset + address - low_address; + ret_value = true; + break; + } + } + + return ret_value; +} + bool Os::MemoryMapFileDesc(FileDesc fdesc, size_t fsize, size_t foffset, const void** mmap_ptr) { if (fdesc <= 0) { return false; diff --git a/projects/clr/rocclr/os/os_win32.cpp b/projects/clr/rocclr/os/os_win32.cpp index deaf67e6fc..bb5f4d0766 100644 --- a/projects/clr/rocclr/os/os_win32.cpp +++ b/projects/clr/rocclr/os/os_win32.cpp @@ -935,6 +935,12 @@ bool Os::MemoryMapFileTruncated(const char* fname, const void** mmap_ptr, size_t return true; } + +bool Os::FindFileNameFromAddress(const void* image, std::string* fname_ptr, size_t* foffset_ptr) { + // TODO: Implementation on windows side pending. + return false; +} + } // namespace amd #endif // _WIN32 || __CYGWIN__ diff --git a/projects/clr/rocclr/utils/flags.hpp b/projects/clr/rocclr/utils/flags.hpp index 16eecf48af..364d716c54 100644 --- a/projects/clr/rocclr/utils/flags.hpp +++ b/projects/clr/rocclr/utils/flags.hpp @@ -277,7 +277,9 @@ release(uint, ROC_SIGNAL_POOL_SIZE, 32, \ release(bool, ROC_SKIP_KERNEL_ARG_COPY, false, \ "If true, then runtime can skip kernel arg copy") \ release(bool, GPU_STREAMOPS_CP_WAIT, false, \ - "Force the stream wait memory operation to wait on CP.") + "Force the stream wait memory operation to wait on CP.") \ +release(bool, HIP_USE_RUNTIME_UNBUNDLER, false, \ + "Use HIP runtime unbundler") \ namespace amd {