Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixed lldb's ReadVirtual to support partial reads
Fixed the DumpRuntimeTypes command to build the GC heap info. It was looking up segments uninitialized.
  • Loading branch information
Mike McLaughlin committed Aug 25, 2022
commit e3ec69395b0adf9e9bd55112c29c311a3d006457
6 changes: 6 additions & 0 deletions src/SOS/Strike/strike.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3956,6 +3956,12 @@ DECLARE_API(DumpRuntimeTypes)
"Address", "Domain", "MT");
ExtOut("------------------------------------------------------------------------------\n");

if (!g_snapshot.Build())
{
ExtOut("Unable to build snapshot of the garbage collector state\n");
return E_FAIL;
}

PrintRuntimeTypeArgs pargs;
ZeroMemory(&pargs, sizeof(PrintRuntimeTypeArgs));

Expand Down
67 changes: 55 additions & 12 deletions src/SOS/lldbplugin/services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,15 +757,28 @@ LLDBServices::GetContextStackTrace(
// IDebugDataSpaces
//----------------------------------------------------------------------------

#define PAGE_SIZE 0x1000
#define PAGE_MASK (~(PAGE_SIZE-1))

HRESULT
LLDBServices::ReadVirtual(
ULONG64 offset,
PVOID buffer,
ULONG bufferSize,
PULONG bytesRead)
PULONG pbytesRead)
{
lldb::SBError error;
size_t read = 0;
size_t bytesRead = 0;
ULONG64 page;

if (bufferSize == 0)
{
if (pbytesRead)
{
*pbytesRead = 0;
}
return S_OK;
}

// lldb doesn't expect sign-extended address
offset = CONVERT_FROM_SIGN_EXTENDED(offset);
Expand All @@ -776,9 +789,41 @@ LLDBServices::ReadVirtual(
goto exit;
}

read = process.ReadMemory(offset, buffer, bufferSize, error);
// Try the full read and return if successful
bytesRead = process.ReadMemory(offset, buffer, bufferSize, error);
if (error.Success())
{
goto exit;
}

// As it turns out the lldb ReadMemory API doesn't do partial reads and the SOS
// caching depends on that behavior. Round up to the next page boundry and attempt
// to read up to the page boundries.
page = (offset + PAGE_SIZE - 1) & PAGE_MASK;

if (!error.Success())
while (bufferSize > 0)
{
size_t size = page - offset;
if (size > bufferSize)
{
size = bufferSize;
}
size_t read = process.ReadMemory(offset, buffer, size, error);

bytesRead += read;
offset += read;
buffer = (BYTE*)buffer + read;
bufferSize -= read;
page += PAGE_SIZE;

if (!error.Success())
{
break;
}
}

// If the read isn't complete, try reading directly from native modules in the address range.
if (bufferSize > 0)
{
lldb::SBTarget target = process.GetTarget();
if (!target.IsValid())
Expand All @@ -787,8 +832,7 @@ LLDBServices::ReadVirtual(
}

int numModules = target.GetNumModules();
bool found = false;
for (int i = 0; !found && i < numModules; i++)
for (int i = 0; i < numModules; i++)
{
lldb::SBModule module = target.GetModuleAtIndex(i);
int numSections = module.GetNumSections();
Expand All @@ -803,21 +847,20 @@ LLDBServices::ReadVirtual(
lldb::SBData sectionData = section.GetSectionData(offset - loadAddr, bufferSize);
if (sectionData.IsValid())
{
read = sectionData.ReadRawData(error, 0, buffer, bufferSize);
found = true;
break;
bytesRead += sectionData.ReadRawData(error, 0, buffer, bufferSize);
goto exit;
}
}
}
}
}

exit:
if (bytesRead)
if (pbytesRead)
{
*bytesRead = read;
*pbytesRead = bytesRead;
}
return error.Success() || (read != 0) ? S_OK : E_FAIL;
return bytesRead > 0 ? S_OK : E_FAIL;
}

HRESULT
Expand Down