TestGDBRemoteClient.py
6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import lldb
import binascii
from lldbsuite.test.lldbtest import *
from lldbsuite.test.decorators import *
from gdbclientutils import *
class TestGDBRemoteClient(GDBRemoteTestBase):
class gPacketResponder(MockGDBServerResponder):
def readRegisters(self):
return '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
@skipIfReproducer # Packet log is not populated during replay.
def test_connect(self):
"""Test connecting to a remote gdb server"""
target = self.createTarget("a.yaml")
process = self.connect(target)
self.assertPacketLogContains(["qProcessInfo", "qfThreadInfo"])
@skipIfReproducer # FIXME: Unexpected packet during (active) replay
def test_attach_fail(self):
error_msg = "mock-error-msg"
class MyResponder(MockGDBServerResponder):
# Pretend we don't have any process during the initial queries.
def qC(self):
return "E42"
def qfThreadInfo(self):
return "OK" # No threads.
# Then, when we are asked to attach, error out.
def vAttach(self, pid):
return "E42;" + binascii.hexlify(error_msg.encode()).decode()
self.server.responder = MyResponder()
target = self.dbg.CreateTarget("")
process = self.connect(target)
lldbutil.expect_state_changes(self, self.dbg.GetListener(), process, [lldb.eStateConnected])
error = lldb.SBError()
target.AttachToProcessWithID(lldb.SBListener(), 47, error)
self.assertEquals(error_msg, error.GetCString())
def test_launch_fail(self):
class MyResponder(MockGDBServerResponder):
# Pretend we don't have any process during the initial queries.
def qC(self):
return "E42"
def qfThreadInfo(self):
return "OK" # No threads.
# Then, when we are asked to attach, error out.
def A(self, packet):
return "E47"
self.runCmd("log enable gdb-remote packets")
self.server.responder = MyResponder()
target = self.createTarget("a.yaml")
process = self.connect(target)
lldbutil.expect_state_changes(self, self.dbg.GetListener(), process, [lldb.eStateConnected])
error = lldb.SBError()
target.Launch(lldb.SBListener(), None, None, None, None, None,
None, 0, True, error)
self.assertEquals("'A' packet returned an error: 71", error.GetCString())
@skipIfReproducer # Packet log is not populated during replay.
def test_read_registers_using_g_packets(self):
"""Test reading registers using 'g' packets (default behavior)"""
self.dbg.HandleCommand(
"settings set plugin.process.gdb-remote.use-g-packet-for-reading true")
self.addTearDownHook(lambda:
self.runCmd("settings set plugin.process.gdb-remote.use-g-packet-for-reading false"))
self.server.responder = self.gPacketResponder()
target = self.createTarget("a.yaml")
process = self.connect(target)
self.assertEquals(1, self.server.responder.packetLog.count("g"))
self.server.responder.packetLog = []
self.read_registers(process)
# Reading registers should not cause any 'p' packets to be exchanged.
self.assertEquals(
0, len([p for p in self.server.responder.packetLog if p.startswith("p")]))
@skipIfReproducer # Packet log is not populated during replay.
def test_read_registers_using_p_packets(self):
"""Test reading registers using 'p' packets"""
self.dbg.HandleCommand(
"settings set plugin.process.gdb-remote.use-g-packet-for-reading false")
target = self.createTarget("a.yaml")
process = self.connect(target)
self.read_registers(process)
self.assertFalse("g" in self.server.responder.packetLog)
self.assertGreater(
len([p for p in self.server.responder.packetLog if p.startswith("p")]), 0)
@skipIfReproducer # Packet log is not populated during replay.
def test_write_registers_using_P_packets(self):
"""Test writing registers using 'P' packets (default behavior)"""
self.server.responder = self.gPacketResponder()
target = self.createTarget("a.yaml")
process = self.connect(target)
self.write_registers(process)
self.assertEquals(0, len(
[p for p in self.server.responder.packetLog if p.startswith("G")]))
self.assertGreater(
len([p for p in self.server.responder.packetLog if p.startswith("P")]), 0)
@skipIfReproducer # Packet log is not populated during replay.
def test_write_registers_using_G_packets(self):
"""Test writing registers using 'G' packets"""
class MyResponder(self.gPacketResponder):
def readRegister(self, register):
# empty string means unsupported
return ""
self.server.responder = MyResponder()
target = self.createTarget("a.yaml")
process = self.connect(target)
self.write_registers(process)
self.assertEquals(0, len(
[p for p in self.server.responder.packetLog if p.startswith("P")]))
self.assertGreater(len(
[p for p in self.server.responder.packetLog if p.startswith("G")]), 0)
def read_registers(self, process):
self.for_each_gpr(
process, lambda r: self.assertEquals("0x00000000", r.GetValue()))
def write_registers(self, process):
self.for_each_gpr(
process, lambda r: r.SetValueFromCString("0x00000000"))
def for_each_gpr(self, process, operation):
registers = process.GetThreadAtIndex(0).GetFrameAtIndex(0).GetRegisters()
self.assertGreater(registers.GetSize(), 0)
regSet = registers[0]
numChildren = regSet.GetNumChildren()
self.assertGreater(numChildren, 0)
for i in range(numChildren):
operation(regSet.GetChildAtIndex(i))