Skip to content

Commit 4be5e33

Browse files
committed
[lldb] Add test for SBTarget reading instructions with flavor
This commit introduces a new test for verifying the `SBTarget` API's ability to read and validate disassembled instructions with a specified flavor ("intel"). Signed-off-by: Ebuka Ezike <[email protected]>
1 parent 6c8b0a3 commit 4be5e33

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
11+
@skipIf(archs=no_match(["x86_64", "x86", "i386"]), oslist=["windows"])
12+
def test_read_instructions_with_flavor(self):
13+
self.build()
14+
executable = self.getBuildArtifact("a.out")
15+
16+
# create a target
17+
target = self.dbg.CreateTarget(executable)
18+
self.assertTrue(target.IsValid(), "target is not valid")
19+
20+
functions = target.FindFunctions("test_add")
21+
self.assertEqual(len(functions), 1)
22+
test_add = functions[0]
23+
24+
test_add_symbols = test_add.GetSymbol()
25+
self.assertTrue(
26+
test_add_symbols.IsValid(), "test_add function symbols is not valid"
27+
)
28+
29+
expected_instructions = (("mov", "eax, edi"), ("add", "eax, esi"), ("ret", ""))
30+
test_add_insts = test_add_symbols.GetInstructions(target, "intel")
31+
# clang adds an extra nop instruction but gcc does not. It makes more sense
32+
# to check if it is at least 3
33+
self.assertLessEqual(len(expected_instructions), len(test_add_insts))
34+
35+
# compares only the expected instructions
36+
for expected_instr, instr in zip(expected_instructions, test_add_insts):
37+
self.assertTrue(instr.IsValid(), "instruction is not valid")
38+
expected_mnemonic, expected_op_str = expected_instr
39+
self.assertEqual(instr.GetMnemonic(target), expected_mnemonic)
40+
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)