aadsm updated this revision to Diff 202329. aadsm added a comment. Moved ReadCStringFromMemory to NativeProcessProtocol and address some other comments.
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D62503/new/ https://reviews.llvm.org/D62503 Files: lldb/include/lldb/Host/common/NativeProcessProtocol.h lldb/source/Host/common/NativeProcessProtocol.cpp lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp Index: lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp =================================================================== --- lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp +++ lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp @@ -2181,8 +2181,9 @@ return error; char name_buffer[PATH_MAX]; - error = ReadMemory(link_map.l_name, &name_buffer, sizeof(name_buffer), - bytes_read); + error = ReadCStringFromMemory(link_map.l_name, + reinterpret_cast<char *>(&name_buffer), + sizeof(name_buffer), bytes_read); if (!error.Success()) return error; Index: lldb/source/Host/common/NativeProcessProtocol.cpp =================================================================== --- lldb/source/Host/common/NativeProcessProtocol.cpp +++ lldb/source/Host/common/NativeProcessProtocol.cpp @@ -16,6 +16,8 @@ #include "lldb/Utility/State.h" #include "lldb/lldb-enumerations.h" +#include "llvm/Support/Process.h" + using namespace lldb; using namespace lldb_private; @@ -659,6 +661,45 @@ return Status(); } +Status NativeProcessProtocol::ReadCStringFromMemory(lldb::addr_t addr, + char *buffer, + size_t max_size, + size_t &total_bytes_read) { + const size_t cache_line_size = llvm::sys::Process::getPageSizeEstimate(); + size_t bytes_read = 0; + size_t bytes_left = max_size; + addr_t curr_addr = addr; + char *curr_buffer = buffer; + total_bytes_read = 0; + Status error; + + while (bytes_left > 0 && error.Success()) { + addr_t cache_line_bytes_left = + cache_line_size - (curr_addr % cache_line_size); + addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); + error = ReadMemory(curr_addr, reinterpret_cast<void *>(curr_buffer), + bytes_to_read, bytes_read); + + if (bytes_read == 0) + break; + + auto str_end = std::memchr(curr_buffer, '\0', bytes_read); + if (str_end != NULL) { + total_bytes_read = (size_t)(reinterpret_cast<char *>(str_end) - buffer); + error.Clear(); + break; + } + + total_bytes_read += bytes_read; + curr_buffer += bytes_read; + curr_addr = reinterpret_cast<addr_t>(reinterpret_cast<char *>(curr_addr) + + bytes_read); + bytes_left -= bytes_read; + } + + return error; +} + lldb::StateType NativeProcessProtocol::GetState() const { std::lock_guard<std::recursive_mutex> guard(m_state_mutex); return m_state; Index: lldb/include/lldb/Host/common/NativeProcessProtocol.h =================================================================== --- lldb/include/lldb/Host/common/NativeProcessProtocol.h +++ lldb/include/lldb/Host/common/NativeProcessProtocol.h @@ -85,6 +85,9 @@ Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read); + Status ReadCStringFromMemory(lldb::addr_t addr, char *buffer, size_t max_size, + size_t &total_bytes_read); + virtual Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) = 0;
Index: lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp =================================================================== --- lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp +++ lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp @@ -2181,8 +2181,9 @@ return error; char name_buffer[PATH_MAX]; - error = ReadMemory(link_map.l_name, &name_buffer, sizeof(name_buffer), - bytes_read); + error = ReadCStringFromMemory(link_map.l_name, + reinterpret_cast<char *>(&name_buffer), + sizeof(name_buffer), bytes_read); if (!error.Success()) return error; Index: lldb/source/Host/common/NativeProcessProtocol.cpp =================================================================== --- lldb/source/Host/common/NativeProcessProtocol.cpp +++ lldb/source/Host/common/NativeProcessProtocol.cpp @@ -16,6 +16,8 @@ #include "lldb/Utility/State.h" #include "lldb/lldb-enumerations.h" +#include "llvm/Support/Process.h" + using namespace lldb; using namespace lldb_private; @@ -659,6 +661,45 @@ return Status(); } +Status NativeProcessProtocol::ReadCStringFromMemory(lldb::addr_t addr, + char *buffer, + size_t max_size, + size_t &total_bytes_read) { + const size_t cache_line_size = llvm::sys::Process::getPageSizeEstimate(); + size_t bytes_read = 0; + size_t bytes_left = max_size; + addr_t curr_addr = addr; + char *curr_buffer = buffer; + total_bytes_read = 0; + Status error; + + while (bytes_left > 0 && error.Success()) { + addr_t cache_line_bytes_left = + cache_line_size - (curr_addr % cache_line_size); + addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); + error = ReadMemory(curr_addr, reinterpret_cast<void *>(curr_buffer), + bytes_to_read, bytes_read); + + if (bytes_read == 0) + break; + + auto str_end = std::memchr(curr_buffer, '\0', bytes_read); + if (str_end != NULL) { + total_bytes_read = (size_t)(reinterpret_cast<char *>(str_end) - buffer); + error.Clear(); + break; + } + + total_bytes_read += bytes_read; + curr_buffer += bytes_read; + curr_addr = reinterpret_cast<addr_t>(reinterpret_cast<char *>(curr_addr) + + bytes_read); + bytes_left -= bytes_read; + } + + return error; +} + lldb::StateType NativeProcessProtocol::GetState() const { std::lock_guard<std::recursive_mutex> guard(m_state_mutex); return m_state; Index: lldb/include/lldb/Host/common/NativeProcessProtocol.h =================================================================== --- lldb/include/lldb/Host/common/NativeProcessProtocol.h +++ lldb/include/lldb/Host/common/NativeProcessProtocol.h @@ -85,6 +85,9 @@ Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read); + Status ReadCStringFromMemory(lldb::addr_t addr, char *buffer, size_t max_size, + size_t &total_bytes_read); + virtual Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) = 0;
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits