3 import gdbremote_testcase
5 from lldbsuite.test.decorators import *
6 from lldbsuite.test.lldbtest import *
7 from lldbsuite.test import lldbutil
10 def _extract_register_value(reg_info, reg_bank, byte_order, bytes_per_entry=8):
11 reg_offset = int(reg_info["offset"])*2
12 reg_byte_size = int(2 * int(reg_info["bitsize"]) / 8)
13 # Create slice with the contents of the register.
14 reg_slice = reg_bank[reg_offset:reg_offset+reg_byte_size]
17 # Wrap slice according to bytes_per_entry.
18 for entry in textwrap.wrap(reg_slice, 2 * bytes_per_entry):
19 # Invert the bytes order if target uses little-endian.
20 if byte_order == lldb.eByteOrderLittle:
21 entry = "".join(reversed([entry[i:i+2] for i in range(0,
23 reg_value.append("0x" + entry)
28 class TestGdbRemoteGPacket(gdbremote_testcase.GdbRemoteTestCaseBase):
30 mydir = TestBase.compute_mydir(__file__)
32 def run_test_g_packet(self):
34 self.prep_debug_monitor_and_inferior()
35 self.test_sequence.add_log_lines(
36 ["read packet: $g#67",
37 {"direction": "send", "regex": r"^\$(.+)#[0-9a-fA-F]{2}$",
38 "capture": {1: "register_bank"}}],
40 self.connect_to_debug_monitor()
41 context = self.expect_gdbremote_sequence()
42 register_bank = context.get("register_bank")
43 self.assertTrue(register_bank[0] != 'E')
45 self.test_sequence.add_log_lines(
46 ["read packet: $G" + register_bank + "#00",
47 {"direction": "send", "regex": r"^\$(.+)#[0-9a-fA-F]{2}$",
48 "capture": {1: "G_reply"}}],
50 context = self.expect_gdbremote_sequence()
51 self.assertTrue(context.get("G_reply")[0] != 'E')
53 @skipIfOutOfTreeDebugserver
56 def test_g_packet_debugserver(self):
57 self.init_debugserver_test()
58 self.run_test_g_packet()
60 @skipIf(archs=no_match(["x86_64"]))
61 def g_returns_correct_data(self, with_suffix):
62 procs = self.prep_debug_monitor_and_inferior()
64 self.add_register_info_collection_packets()
66 self.add_thread_suffix_request_packets()
67 self.add_threadinfo_collection_packets()
68 context = self.expect_gdbremote_sequence()
69 self.assertIsNotNone(context)
71 # Gather register info.
72 reg_infos = self.parse_register_info_packets(context)
73 self.assertIsNotNone(reg_infos)
74 self.add_lldb_register_index(reg_infos)
75 # Index register info entries by name.
76 reg_infos = {info['name']: info for info in reg_infos}
80 threads = self.parse_threadinfo_packets(context)
81 self.assertIsNotNone(threads)
82 thread_id = threads[0]
83 self.assertIsNotNone(thread_id)
87 # Send vCont packet to resume the inferior.
88 self.test_sequence.add_log_lines(["read packet: $vCont;c#a8",
90 "regex": r"^\$T([0-9a-fA-F]{2}).*#[0-9a-fA-F]{2}$",
91 "capture": {1: "hex_exit_code"}},
95 # Send g packet to retrieve the register bank
97 g_request = "read packet: $g;thread:{:x}#00".format(thread_id)
99 g_request = "read packet: $g#00"
100 self.test_sequence.add_log_lines(
102 {"direction": "send", "regex": r"^\$(.+)#[0-9a-fA-F]{2}$",
103 "capture": {1: "register_bank"}}],
105 context = self.expect_gdbremote_sequence()
106 self.assertIsNotNone(context)
107 reg_bank = context.get("register_bank")
108 self.assertTrue(reg_bank[0] != 'E')
110 byte_order = self.get_target_byte_order()
111 get_reg_value = lambda reg_name : _extract_register_value(
112 reg_infos[reg_name], reg_bank, byte_order)
114 self.assertEqual(['0x0102030405060708'], get_reg_value('r8'))
115 self.assertEqual(['0x1112131415161718'], get_reg_value('r9'))
116 self.assertEqual(['0x2122232425262728'], get_reg_value('r10'))
117 self.assertEqual(['0x3132333435363738'], get_reg_value('r11'))
118 self.assertEqual(['0x4142434445464748'], get_reg_value('r12'))
119 self.assertEqual(['0x5152535455565758'], get_reg_value('r13'))
120 self.assertEqual(['0x6162636465666768'], get_reg_value('r14'))
121 self.assertEqual(['0x7172737475767778'], get_reg_value('r15'))
124 ['0x020406080a0c0e01', '0x030507090b0d0f00'], get_reg_value('xmm8'))
126 ['0x121416181a1c1e11', '0x131517191b1d1f10'], get_reg_value('xmm9'))
128 ['0x222426282a2c2e21', '0x232527292b2d2f20'], get_reg_value('xmm10'))
130 ['0x323436383a3c3e31', '0x333537393b3d3f30'], get_reg_value('xmm11'))
132 ['0x424446484a4c4e41', '0x434547494b4d4f40'], get_reg_value('xmm12'))
134 ['0x525456585a5c5e51', '0x535557595b5d5f50'], get_reg_value('xmm13'))
136 ['0x626466686a6c6e61', '0x636567696b6d6f60'], get_reg_value('xmm14'))
138 ['0x727476787a7c7e71', '0x737577797b7d7f70'], get_reg_value('xmm15'))
140 @expectedFailureNetBSD
142 def test_g_returns_correct_data_with_suffix_llgs(self):
143 self.init_llgs_test()
145 self.set_inferior_startup_launch()
146 self.g_returns_correct_data(True)
148 @expectedFailureNetBSD
150 def test_g_returns_correct_data_no_suffix_llgs(self):
151 self.init_llgs_test()
153 self.set_inferior_startup_launch()
154 self.g_returns_correct_data(False)