Skip to content

Commit 70795c1

Browse files
committed
Revert "Revert "Add ReadCStringFromMemory for faster string reads""
This reverts commit 9c10b62. llvm-svn: 366848
1 parent 05e32ba commit 70795c1

File tree

5 files changed

+136
-7
lines changed

5 files changed

+136
-7
lines changed

lldb/include/lldb/Host/common/NativeProcessProtocol.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,31 @@ class NativeProcessProtocol {
8484
Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
8585
size_t &bytes_read);
8686

87+
/// Reads a null terminated string from memory.
88+
///
89+
/// Reads up to \p max_size bytes of memory until it finds a '\0'.
90+
/// If a '\0' is not found then it reads max_size-1 bytes as a string and a
91+
/// '\0' is added as the last character of the \p buffer.
92+
///
93+
/// \param[in] addr
94+
/// The address in memory to read from.
95+
///
96+
/// \param[in] buffer
97+
/// An allocated buffer with at least \p max_size size.
98+
///
99+
/// \param[in] max_size
100+
/// The maximum number of bytes to read from memory until it reads the
101+
/// string.
102+
///
103+
/// \param[out] total_bytes_read
104+
/// The number of bytes read from memory into \p buffer.
105+
///
106+
/// \return
107+
/// Returns a StringRef backed up by the \p buffer passed in.
108+
llvm::Expected<llvm::StringRef>
109+
ReadCStringFromMemory(lldb::addr_t addr, char *buffer, size_t max_size,
110+
size_t &total_bytes_read);
111+
87112
virtual Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
88113
size_t &bytes_written) = 0;
89114

lldb/source/Host/common/NativeProcessProtocol.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "lldb/Utility/State.h"
1717
#include "lldb/lldb-enumerations.h"
1818

19+
#include "llvm/Support/Process.h"
20+
1921
using namespace lldb;
2022
using namespace lldb_private;
2123

@@ -659,6 +661,58 @@ Status NativeProcessProtocol::ReadMemoryWithoutTrap(lldb::addr_t addr,
659661
return Status();
660662
}
661663

664+
llvm::Expected<llvm::StringRef>
665+
NativeProcessProtocol::ReadCStringFromMemory(lldb::addr_t addr, char *buffer,
666+
size_t max_size,
667+
size_t &total_bytes_read) {
668+
static const size_t cache_line_size =
669+
llvm::sys::Process::getPageSizeEstimate();
670+
size_t bytes_read = 0;
671+
size_t bytes_left = max_size;
672+
addr_t curr_addr = addr;
673+
size_t string_size;
674+
char *curr_buffer = buffer;
675+
total_bytes_read = 0;
676+
Status status;
677+
678+
while (bytes_left > 0 && status.Success()) {
679+
addr_t cache_line_bytes_left =
680+
cache_line_size - (curr_addr % cache_line_size);
681+
addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
682+
status = ReadMemory(curr_addr, reinterpret_cast<void *>(curr_buffer),
683+
bytes_to_read, bytes_read);
684+
685+
if (bytes_read == 0)
686+
break;
687+
688+
void *str_end = std::memchr(curr_buffer, '\0', bytes_read);
689+
if (str_end != nullptr) {
690+
total_bytes_read =
691+
(size_t)(reinterpret_cast<char *>(str_end) - buffer + 1);
692+
status.Clear();
693+
break;
694+
}
695+
696+
total_bytes_read += bytes_read;
697+
curr_buffer += bytes_read;
698+
curr_addr += bytes_read;
699+
bytes_left -= bytes_read;
700+
}
701+
702+
string_size = total_bytes_read - 1;
703+
704+
// Make sure we return a null terminated string.
705+
if (bytes_left == 0 && max_size > 0 && buffer[max_size - 1] != '\0') {
706+
buffer[max_size - 1] = '\0';
707+
total_bytes_read--;
708+
}
709+
710+
if (!status.Success())
711+
return status.ToError();
712+
713+
return llvm::StringRef(buffer, string_size);
714+
}
715+
662716
lldb::StateType NativeProcessProtocol::GetState() const {
663717
std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
664718
return m_state;

lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2076,4 +2076,4 @@ Status NativeProcessLinux::StopProcessorTracingOnThread(lldb::user_id_t traceid,
20762076
m_processor_trace_monitor.erase(iter);
20772077

20782078
return error;
2079-
}
2079+
}

lldb/source/Plugins/Process/POSIX/NativeProcessELF.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,13 @@ NativeProcessELF::ReadSVR4LibraryInfo(lldb::addr_t link_map_addr) {
118118
return error.ToError();
119119

120120
char name_buffer[PATH_MAX];
121-
error = ReadMemory(link_map.l_name, &name_buffer, sizeof(name_buffer),
122-
bytes_read);
123-
if (bytes_read == 0)
124-
return error.ToError();
125-
name_buffer[PATH_MAX - 1] = '\0';
121+
llvm::Expected<llvm::StringRef> string_or_error = ReadCStringFromMemory(
122+
link_map.l_name, &name_buffer[0], sizeof(name_buffer), bytes_read);
123+
if (!string_or_error)
124+
return string_or_error.takeError();
126125

127126
SVR4LibraryInfo info;
128-
info.name = std::string(name_buffer);
127+
info.name = string_or_error->str();
129128
info.link_map = link_map_addr;
130129
info.base_addr = link_map.l_addr;
131130
info.ld_addr = link_map.l_ld;

lldb/unittests/Host/NativeProcessProtocolTest.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "TestingSupport/Host/NativeProcessTestUtils.h"
1010

1111
#include "lldb/Host/common/NativeProcessProtocol.h"
12+
#include "llvm/Support/Process.h"
1213
#include "llvm/Testing/Support/Error.h"
1314
#include "gmock/gmock.h"
1415

@@ -96,3 +97,53 @@ TEST(NativeProcessProtocolTest, ReadMemoryWithoutTrap) {
9697
EXPECT_THAT_EXPECTED(Process.ReadMemoryWithoutTrap(4, 2),
9798
llvm::HasValue(std::vector<uint8_t>{4, 5}));
9899
}
100+
101+
TEST(NativeProcessProtocolTest, ReadCStringFromMemory) {
102+
NiceMock<MockDelegate> DummyDelegate;
103+
MockProcess<NativeProcessProtocol> Process(DummyDelegate,
104+
ArchSpec("aarch64-pc-linux"));
105+
FakeMemory M({'h', 'e', 'l', 'l', 'o', 0, 'w', 'o'});
106+
EXPECT_CALL(Process, ReadMemory(_, _))
107+
.WillRepeatedly(Invoke(&M, &FakeMemory::Read));
108+
109+
char string[1024];
110+
size_t bytes_read;
111+
EXPECT_THAT_EXPECTED(Process.ReadCStringFromMemory(
112+
0x0, &string[0], sizeof(string), bytes_read),
113+
llvm::HasValue(llvm::StringRef("hello")));
114+
EXPECT_EQ(bytes_read, 6UL);
115+
}
116+
117+
TEST(NativeProcessProtocolTest, ReadCStringFromMemory_MaxSize) {
118+
NiceMock<MockDelegate> DummyDelegate;
119+
MockProcess<NativeProcessProtocol> Process(DummyDelegate,
120+
ArchSpec("aarch64-pc-linux"));
121+
FakeMemory M({'h', 'e', 'l', 'l', 'o', 0, 'w', 'o'});
122+
EXPECT_CALL(Process, ReadMemory(_, _))
123+
.WillRepeatedly(Invoke(&M, &FakeMemory::Read));
124+
125+
char string[4];
126+
size_t bytes_read;
127+
EXPECT_THAT_EXPECTED(Process.ReadCStringFromMemory(
128+
0x0, &string[0], sizeof(string), bytes_read),
129+
llvm::HasValue(llvm::StringRef("hel")));
130+
EXPECT_EQ(bytes_read, 3UL);
131+
}
132+
133+
TEST(NativeProcessProtocolTest, ReadCStringFromMemory_CrossPageBoundary) {
134+
NiceMock<MockDelegate> DummyDelegate;
135+
MockProcess<NativeProcessProtocol> Process(DummyDelegate,
136+
ArchSpec("aarch64-pc-linux"));
137+
unsigned string_start = llvm::sys::Process::getPageSizeEstimate() - 3;
138+
FakeMemory M({'h', 'e', 'l', 'l', 'o', 0, 'w', 'o'}, string_start);
139+
EXPECT_CALL(Process, ReadMemory(_, _))
140+
.WillRepeatedly(Invoke(&M, &FakeMemory::Read));
141+
142+
char string[1024];
143+
size_t bytes_read;
144+
EXPECT_THAT_EXPECTED(Process.ReadCStringFromMemory(string_start, &string[0],
145+
sizeof(string),
146+
bytes_read),
147+
llvm::HasValue(llvm::StringRef("hello")));
148+
EXPECT_EQ(bytes_read, 6UL);
149+
}

0 commit comments

Comments
 (0)