diff --git a/lldb/include/lldb/Host/common/NativeProcessProtocol.h b/lldb/include/lldb/Host/common/NativeProcessProtocol.h --- a/lldb/include/lldb/Host/common/NativeProcessProtocol.h +++ b/lldb/include/lldb/Host/common/NativeProcessProtocol.h @@ -84,6 +84,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; diff --git a/lldb/source/Host/common/NativeProcessProtocol.cpp b/lldb/source/Host/common/NativeProcessProtocol.cpp --- a/lldb/source/Host/common/NativeProcessProtocol.cpp +++ b/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,52 @@ 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(bytes_left, cache_line_bytes_left); + error = ReadMemory(curr_addr, reinterpret_cast(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(str_end) - buffer + 1); + error.Clear(); + break; + } + + total_bytes_read += bytes_read; + curr_buffer += bytes_read; + curr_addr = reinterpret_cast(reinterpret_cast(curr_addr) + + bytes_read); + bytes_left -= bytes_read; + } + + // Make sure we return a null terminated string. + if (bytes_left == 0 && buffer[max_size - 1] != '\0') { + buffer[max_size - 1] = '\0'; + total_bytes_read--; + } + + return error; +} + lldb::StateType NativeProcessProtocol::GetState() const { std::lock_guard guard(m_state_mutex); return m_state; diff --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp --- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp +++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp @@ -2089,11 +2089,11 @@ return error.ToError(); 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(&name_buffer), + sizeof(name_buffer), bytes_read); if (!error.Success()) return error.ToError(); - name_buffer[PATH_MAX - 1] = '\0'; SVR4LibraryInfo info; info.name = std::string(name_buffer); diff --git a/lldb/unittests/Host/NativeProcessProtocolTest.cpp b/lldb/unittests/Host/NativeProcessProtocolTest.cpp --- a/lldb/unittests/Host/NativeProcessProtocolTest.cpp +++ b/lldb/unittests/Host/NativeProcessProtocolTest.cpp @@ -96,3 +96,39 @@ EXPECT_THAT_EXPECTED(Process.ReadMemoryWithoutTrap(4, 2), llvm::HasValue(std::vector{4, 5})); } + +TEST(NativeProcessProtocolTest, ReadCStringFromMemory) { + NiceMock DummyDelegate; + MockProcess Process(DummyDelegate, + ArchSpec("aarch64-pc-linux")); + FakeMemory M{{'h', 'e', 'l', 'l', 'o', 0, 'w', 'o'}}; + EXPECT_CALL(Process, ReadMemory(_, _)) + .WillRepeatedly(Invoke(&M, &FakeMemory::Read)); + + char string[1024]; + size_t bytes_read; + EXPECT_THAT_ERROR( + Process.ReadCStringFromMemory(0x0, &string[0], sizeof(string), bytes_read) + .ToError(), + llvm::Succeeded()); + EXPECT_STREQ(string, "hello"); + EXPECT_EQ(bytes_read, 6UL); +} + +TEST(NativeProcessProtocolTest, ReadCStringFromMemory_MaxSize) { + NiceMock DummyDelegate; + MockProcess Process(DummyDelegate, + ArchSpec("aarch64-pc-linux")); + FakeMemory M{{'h', 'e', 'l', 'l', 'o', 0, 'w', 'o'}}; + EXPECT_CALL(Process, ReadMemory(_, _)) + .WillRepeatedly(Invoke(&M, &FakeMemory::Read)); + + char string[4]; + size_t bytes_read; + EXPECT_THAT_ERROR( + Process.ReadCStringFromMemory(0x0, &string[0], sizeof(string), bytes_read) + .ToError(), + llvm::Succeeded()); + EXPECT_STREQ(string, "hel"); + EXPECT_EQ(bytes_read, 3UL); +}