Skip to content

Commit d76ec6a

Browse files
da-vipertstellar
authored andcommitted
[lldb] Fix SBTarget::ReadInstruction with flavor (llvm#134626)
When you call the `SBTarget::ReadInstructions` with flavor from lldb crashes. This is because the wrong order of the `DisassemblyBytes` constructor this fixes that --------- Signed-off-by: Ebuka Ezike <[email protected]>
1 parent 62072e7 commit d76ec6a

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

lldb/source/API/SBTarget.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,9 +2020,9 @@ lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
20202020
error, force_live_memory, &load_addr);
20212021
const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
20222022
sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
2023-
target_sp->GetArchitecture(), nullptr, target_sp->GetDisassemblyCPU(),
2024-
target_sp->GetDisassemblyFeatures(), flavor_string, *addr_ptr,
2025-
data.GetBytes(), bytes_read, count, data_from_file));
2023+
target_sp->GetArchitecture(), nullptr, flavor_string,
2024+
target_sp->GetDisassemblyCPU(), target_sp->GetDisassemblyFeatures(),
2025+
*addr_ptr, data.GetBytes(), bytes_read, count, data_from_file));
20262026
}
20272027
}
20282028

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
C_SOURCES := main.c
2+
3+
include Makefile.rules
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Test SBTarget Read Instruction.
3+
"""
4+
5+
from lldbsuite.test.decorators import *
6+
from lldbsuite.test.lldbtest import *
7+
8+
9+
class TargetReadInstructionsFlavor(TestBase):
10+
@skipIf(archs=no_match(["x86_64", "x86", "i386"]), oslist=["windows"])
11+
def test_read_instructions_with_flavor(self):
12+
self.build()
13+
executable = self.getBuildArtifact("a.out")
14+
15+
# create a target
16+
target = self.dbg.CreateTarget(executable)
17+
self.assertTrue(target.IsValid(), "target is not valid")
18+
19+
functions = target.FindFunctions("test_add")
20+
self.assertEqual(len(functions), 1)
21+
test_add = functions[0]
22+
23+
test_add_symbols = test_add.GetSymbol()
24+
self.assertTrue(
25+
test_add_symbols.IsValid(), "test_add function symbols is not valid"
26+
)
27+
28+
expected_instructions = (("mov", "eax, edi"), ("add", "eax, esi"), ("ret", ""))
29+
test_add_insts = test_add_symbols.GetInstructions(target, "intel")
30+
# clang adds an extra nop instruction but gcc does not. It makes more sense
31+
# to check if it is at least 3
32+
self.assertLessEqual(len(expected_instructions), len(test_add_insts))
33+
34+
# compares only the expected instructions
35+
for expected_instr, instr in zip(expected_instructions, test_add_insts):
36+
self.assertTrue(instr.IsValid(), "instruction is not valid")
37+
expected_mnemonic, expected_op_str = expected_instr
38+
self.assertEqual(instr.GetMnemonic(target), expected_mnemonic)
39+
self.assertEqual(instr.GetOperands(target), expected_op_str)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
// This simple program is to test the lldb Python API SBTarget ReadInstruction
3+
// function.
4+
//
5+
// When the target is create we get all the instructions using the intel
6+
// flavor and see if it is correct.
7+
8+
int test_add(int a, int b);
9+
10+
__asm__("test_add:\n"
11+
" movl %edi, %eax\n"
12+
" addl %esi, %eax\n"
13+
" ret \n");
14+
15+
int main(int argc, char **argv) {
16+
int a = 10;
17+
int b = 20;
18+
int result = test_add(a, b);
19+
20+
return 0;
21+
}

0 commit comments

Comments
 (0)