Skip to content

Commit

Permalink
Modify all register values whose byte size matches the address size t…
Browse files Browse the repository at this point in the history
…o be formatter as eFormatAddressInfo.

This allows users to see similar output to what the "register read" command emits in LLDB's command line.

Added a test to verify that the PC has the correct value with contains a pointer followed by the module + function name and the source line info. Something like:

0x0000000100000a64 a.out`main + 132 at main.cpp:17:11

Differential Revision: https://reviews.llvm.org/D129528
  • Loading branch information
clayborg committed Aug 22, 2022
1 parent f0697d7 commit ae376fe
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Expand Up @@ -447,6 +447,10 @@ def get_local_variables(self, frameIndex=0, threadId=None):
return self.get_scope_variables('Locals', frameIndex=frameIndex,
threadId=threadId)

def get_registers(self, frameIndex=0, threadId=None):
return self.get_scope_variables('Registers', frameIndex=frameIndex,
threadId=threadId)

def get_local_variable(self, name, frameIndex=0, threadId=None):
locals = self.get_local_variables(frameIndex=frameIndex,
threadId=threadId)
Expand Down
49 changes: 49 additions & 0 deletions lldb/test/API/tools/lldb-vscode/variables/TestVSCode_variables.py
Expand Up @@ -459,3 +459,52 @@ def test_indexedVariables(self):
"pt": {"missing": ["indexedVariables"]},
}
self.verify_variables(verify_locals, locals)

@skipIfWindows
@skipIfRemote
def test_registers(self):
'''
Test that registers whose byte size is the size of a pointer on
the current system get formatted as lldb::eFormatAddressInfo. This
will show the pointer value followed by a description of the address
itself. To test this we attempt to find the PC value in the general
purpose registers, and since we will be stopped in main.cpp, verify
that the value for the PC starts with a pointer and is followed by
a description that contains main.cpp.
'''
program = self.getBuildArtifact("a.out")
self.build_and_launch(program)
source = "main.cpp"
breakpoint1_line = line_number(source, "// breakpoint 1")
lines = [breakpoint1_line]
# Set breakpoint in the thread function so we can step the threads
breakpoint_ids = self.set_source_breakpoints(source, lines)
self.assertEqual(
len(breakpoint_ids), len(lines), "expect correct number of breakpoints"
)
self.continue_to_breakpoints(breakpoint_ids)


pc_name = None
arch = self.getArchitecture()
if arch == 'x86_64':
pc_name = 'rip'
elif arch == 'x86':
pc_name = 'rip'
elif arch.startswith('arm'):
pc_name = 'pc'

if pc_name is None:
return
# Verify locals
reg_sets = self.vscode.get_registers()
for reg_set in reg_sets:
if reg_set['name'] == 'General Purpose Registers':
varRef = reg_set['variablesReference']
regs = self.vscode.request_variables(varRef)['body']['variables']
for reg in regs:
if reg['name'] == pc_name:
value = reg['value']
self.assertTrue(value.startswith('0x'))
self.assertTrue('a.out`main + ' in value)
self.assertTrue('at main.cpp:' in value)
19 changes: 19 additions & 0 deletions lldb/tools/lldb-vscode/lldb-vscode.cpp
Expand Up @@ -2933,6 +2933,25 @@ void request_variables(const llvm::json::Object &request) {
int64_t start_idx = 0;
int64_t num_children = 0;

if (variablesReference == VARREF_REGS) {
// Change the default format of any pointer sized registers in the first
// register set to be the lldb::eFormatAddressInfo so we show the pointer
// and resolve what the pointer resolves to. Only change the format if the
// format was set to the default format or if it was hex as some registers
// have formats set for them.
const uint32_t addr_size = g_vsc.target.GetProcess().GetAddressByteSize();
lldb::SBValue reg_set = g_vsc.variables.registers.GetValueAtIndex(0);
const uint32_t num_regs = reg_set.GetNumChildren();
for (uint32_t reg_idx=0; reg_idx<num_regs; ++reg_idx) {
lldb::SBValue reg = reg_set.GetChildAtIndex(reg_idx);
const lldb::Format format = reg.GetFormat();
if (format == lldb::eFormatDefault || format == lldb::eFormatHex) {
if (reg.GetByteSize() == addr_size)
reg.SetFormat(lldb::eFormatAddressInfo);
}
}
}

num_children = top_scope->GetSize();
const int64_t end_idx = start_idx + ((count == 0) ? num_children : count);

Expand Down

0 comments on commit ae376fe

Please sign in to comment.