Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / test / API / python_api / disassemble-raw-data / TestDisassembleRawData.py
blob4ec27530a8f60a1789de7bf982da2b425ebdc490
1 """
2 Use lldb Python API to disassemble raw machine code bytes
3 """
5 import re
6 import lldb
7 from lldbsuite.test.decorators import *
8 from lldbsuite.test.lldbtest import *
9 from lldbsuite.test import lldbutil
12 class DisassembleRawDataTestCase(TestBase):
13 @no_debug_info_test
14 @skipIfRemote
15 def test_disassemble_raw_data(self):
16 """Test disassembling raw bytes with the API."""
17 # Create a target from the debugger.
18 arch = self.getArchitecture()
19 if re.match("mips*el", arch):
20 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "mipsel")
21 raw_bytes = bytearray([0x21, 0xF0, 0xA0, 0x03])
22 elif re.match("mips", arch):
23 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "mips")
24 raw_bytes = bytearray([0x03, 0xA0, 0xF0, 0x21])
25 elif re.match("powerpc64le", arch):
26 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "powerpc64le")
27 raw_bytes = bytearray([0x00, 0x00, 0x80, 0x38])
28 elif arch in ("aarch64", "arm64"):
29 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "aarch64")
30 raw_bytes = bytearray([0x60, 0x0C, 0x80, 0x52])
31 elif arch == "arm":
32 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "arm")
33 raw_bytes = bytearray([0x63, 0x30, 0xA0, 0xE3])
34 else:
35 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "x86_64")
36 raw_bytes = bytearray([0x48, 0x89, 0xE5])
38 self.assertTrue(target, VALID_TARGET)
39 insts = target.GetInstructions(lldb.SBAddress(0, target), raw_bytes)
41 inst = insts.GetInstructionAtIndex(0)
43 if self.TraceOn():
44 print()
45 print("Raw bytes: ", [hex(x) for x in raw_bytes])
46 print("Disassembled%s" % str(inst))
47 if re.match("mips", arch):
48 self.assertEqual(inst.GetMnemonic(target), "move")
49 self.assertEqual(inst.GetOperands(target), "$" + "fp, " + "$" + "sp")
50 self.assertEqual(
51 inst.GetControlFlowKind(target), lldb.eInstructionControlFlowKindUnknown
53 elif re.match("powerpc64le", arch):
54 self.assertEqual(inst.GetMnemonic(target), "li")
55 self.assertEqual(inst.GetOperands(target), "4, 0")
56 self.assertEqual(
57 inst.GetControlFlowKind(target), lldb.eInstructionControlFlowKindUnknown
59 elif arch in ("aarch64", "arm64"):
60 self.assertEqual(inst.GetMnemonic(target), "mov")
61 self.assertEqual(inst.GetOperands(target), "w0, #0x63")
62 self.assertEqual(inst.GetComment(target), "=99 ")
63 self.assertEqual(
64 inst.GetControlFlowKind(target), lldb.eInstructionControlFlowKindUnknown
66 # Make sure that using colors doesn't affect the output here.
67 res = lldb.SBCommandReturnObject()
68 ci = self.dbg.GetCommandInterpreter()
69 ci.HandleCommand("settings set use-color true", res)
70 self.assertEqual(inst.GetOperands(target), "w0, #0x63")
71 self.assertEqual(inst.GetMnemonic(target), "mov")
72 self.assertEqual(inst.GetComment(target), "=99 ")
73 ci.HandleCommand("settings set use-color false", res)
75 elif arch == "arm":
76 self.assertEqual(inst.GetMnemonic(target), "mov")
77 self.assertEqual(inst.GetOperands(target), "r3, #99")
78 self.assertEqual(
79 inst.GetControlFlowKind(target), lldb.eInstructionControlFlowKindUnknown
81 else:
82 self.assertEqual(inst.GetMnemonic(target), "movq")
83 self.assertEqual(inst.GetOperands(target), "%" + "rsp, " + "%" + "rbp")
84 self.assertEqual(
85 inst.GetControlFlowKind(target), lldb.eInstructionControlFlowKindOther