Skip to content

Commit 313d9c1

Browse files
author
Michał Górny
committed
[lldb] [llgs] Refactor fork/vfork tests, verify state
Refactor the fork and vfork tests to reuse the code better, avoid unnecessary regexps and avoid unnecessary conversions between hex-strings and integers. Verify the server state after detaching. In particular, verify that the detached process' PID/TID pair is no longer valid, and that the correct process remains running. Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.llvm.org/D127290
1 parent d6b3de7 commit 313d9c1

1 file changed

Lines changed: 74 additions & 74 deletions

File tree

lldb/test/API/tools/lldb-server/TestGdbRemoteFork.py

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
class TestGdbRemoteFork(gdbremote_testcase.GdbRemoteTestCaseBase):
77

8+
fork_regex = ("[$]T05thread:p([0-9a-f]+)[.]([0-9a-f]+);.*"
9+
"{}:p([0-9a-f]+)[.]([0-9a-f]+).*")
10+
fork_capture = {1: "parent_pid", 2: "parent_tid",
11+
3: "child_pid", 4: "child_tid"}
12+
procinfo_regex = "[$]pid:([0-9a-f]+);.*"
13+
814
@add_test_categories(["fork"])
915
def test_fork_multithreaded(self):
1016
self.build()
@@ -15,26 +21,19 @@ def test_fork_multithreaded(self):
1521
self.reset_test_sequence()
1622

1723
# continue and expect fork
18-
fork_regex = "[$]T05.*;fork:p([0-9a-f]+)[.]([0-9a-f]+).*"
1924
self.test_sequence.add_log_lines([
2025
"read packet: $c#00",
21-
{"direction": "send", "regex": fork_regex,
22-
"capture": {1: "pid", 2: "tid"}},
26+
{"direction": "send", "regex": self.fork_regex.format("fork"),
27+
"capture": self.fork_capture},
2328
], True)
2429
ret = self.expect_gdbremote_sequence()
25-
pid = int(ret["pid"], 16)
30+
child_pid = ret["child_pid"]
2631
self.reset_test_sequence()
2732

2833
# detach the forked child
2934
self.test_sequence.add_log_lines([
30-
"read packet: $D;{:x}#00".format(pid),
31-
{"direction": "send", "regex": r"[$]OK#.*"},
32-
], True)
33-
ret = self.expect_gdbremote_sequence()
34-
self.reset_test_sequence()
35-
36-
# resume the parent
37-
self.test_sequence.add_log_lines([
35+
"read packet: $D;{}#00".format(child_pid),
36+
"send packet: $OK#00",
3837
"read packet: $k#00",
3938
], True)
4039
self.expect_gdbremote_sequence()
@@ -49,45 +48,59 @@ def fork_and_detach_test(self, variant):
4948
self.reset_test_sequence()
5049

5150
# continue and expect fork
52-
fork_regex = "[$]T05.*;{}:p([0-9a-f]+)[.]([0-9a-f]+).*".format(variant)
5351
self.test_sequence.add_log_lines([
5452
"read packet: $c#00",
55-
{"direction": "send", "regex": fork_regex,
56-
"capture": {1: "pid", 2: "tid"}},
53+
{"direction": "send", "regex": self.fork_regex.format(variant),
54+
"capture": self.fork_capture},
5755
], True)
5856
ret = self.expect_gdbremote_sequence()
59-
pid = int(ret["pid"], 16)
57+
parent_pid = ret["parent_pid"]
58+
parent_tid = ret["parent_tid"]
59+
child_pid = ret["child_pid"]
60+
child_tid = ret["child_tid"]
6061
self.reset_test_sequence()
6162

6263
# detach the forked child
6364
self.test_sequence.add_log_lines([
64-
"read packet: $D;{:x}#00".format(pid),
65-
{"direction": "send", "regex": r"[$]OK#.*"},
65+
"read packet: $D;{}#00".format(child_pid),
66+
"send packet: $OK#00",
67+
# verify that the current process is correct
68+
"read packet: $qC#00",
69+
"send packet: $QC{}#00".format(parent_tid),
70+
# verify that the correct processes are detached/available
71+
"read packet: $Hgp{}.{}#00".format(child_pid, child_tid),
72+
"send packet: $Eff#00",
73+
"read packet: $Hgp{}.{}#00".format(parent_pid, parent_tid),
74+
"send packet: $OK#00",
6675
], True)
67-
ret = self.expect_gdbremote_sequence()
76+
self.expect_gdbremote_sequence()
6877
self.reset_test_sequence()
78+
return parent_pid, parent_tid
6979

7080
@add_test_categories(["fork"])
7181
def test_fork(self):
72-
self.fork_and_detach_test("fork")
82+
parent_pid, _ = self.fork_and_detach_test("fork")
7383

7484
# resume the parent
7585
self.test_sequence.add_log_lines([
7686
"read packet: $c#00",
77-
{"direction": "send", "regex": r"[$]W00;process:[0-9a-f]+#.*"},
87+
"send packet: $W00;process:{}#00".format(parent_pid),
7888
], True)
7989
self.expect_gdbremote_sequence()
8090

8191
@add_test_categories(["fork"])
8292
def test_vfork(self):
83-
self.fork_and_detach_test("vfork")
93+
parent_pid, parent_tid = self.fork_and_detach_test("vfork")
8494

8595
# resume the parent
8696
self.test_sequence.add_log_lines([
8797
"read packet: $c#00",
88-
{"direction": "send", "regex": r"[$]T05.*vforkdone.*"},
98+
{"direction": "send",
99+
"regex": r"[$]T05thread:p{}[.]{}.*vforkdone.*".format(parent_pid,
100+
parent_tid),
101+
},
89102
"read packet: $c#00",
90-
{"direction": "send", "regex": r"[$]W00;process:[0-9a-f]+#.*"},
103+
"send packet: $W00;process:{}#00".format(parent_pid),
91104
], True)
92105
self.expect_gdbremote_sequence()
93106

@@ -101,38 +114,35 @@ def fork_and_follow_test(self, variant):
101114
self.reset_test_sequence()
102115

103116
# continue and expect fork
104-
fork_regex = ("[$]T[0-9a-f]{{2}}thread:p([0-9a-f]+)[.][0-9a-f]+;.*"
105-
"{}:p([0-9a-f]+)[.]([0-9a-f]+).*".format(variant))
106117
self.test_sequence.add_log_lines([
107118
"read packet: $c#00",
108-
{"direction": "send", "regex": fork_regex,
109-
"capture": {1: "parent_pid", 2: "pid", 3: "tid"}},
119+
{"direction": "send", "regex": self.fork_regex.format(variant),
120+
"capture": self.fork_capture},
110121
], True)
111122
ret = self.expect_gdbremote_sequence()
112-
parent_pid, pid, tid = (int(ret[x], 16) for x
113-
in ("parent_pid", "pid", "tid"))
123+
parent_pid = ret["parent_pid"]
124+
parent_tid = ret["parent_tid"]
125+
child_pid = ret["child_pid"]
126+
child_tid = ret["child_tid"]
114127
self.reset_test_sequence()
115128

116129
# switch to the forked child
117130
self.test_sequence.add_log_lines([
118-
"read packet: $Hgp{:x}.{:x}#00".format(pid, tid),
119-
{"direction": "send", "regex": r"[$]OK#.*"},
120-
"read packet: $Hcp{:x}.{:x}#00".format(pid, tid),
121-
{"direction": "send", "regex": r"[$]OK#.*"},
122-
], True)
123-
124-
# detach the parent
125-
self.test_sequence.add_log_lines([
126-
"read packet: $D;{:x}#00".format(parent_pid),
127-
{"direction": "send", "regex": r"[$]OK#.*"},
128-
], True)
129-
ret = self.expect_gdbremote_sequence()
130-
self.reset_test_sequence()
131-
132-
# resume the child
133-
self.test_sequence.add_log_lines([
131+
"read packet: $Hgp{}.{}#00".format(child_pid, child_tid),
132+
"send packet: $OK#00",
133+
"read packet: $Hcp{}.{}#00".format(child_pid, child_tid),
134+
"send packet: $OK#00",
135+
# detach the parent
136+
"read packet: $D;{}#00".format(parent_pid),
137+
"send packet: $OK#00",
138+
# verify that the correct processes are detached/available
139+
"read packet: $Hgp{}.{}#00".format(parent_pid, parent_tid),
140+
"send packet: $Eff#00",
141+
"read packet: $Hgp{}.{}#00".format(child_pid, child_tid),
142+
"send packet: $OK#00",
143+
# then resume the child
134144
"read packet: $c#00",
135-
{"direction": "send", "regex": r"[$]W00;process:[0-9a-f]+#.*"},
145+
"send packet: $W00;process:{}#00".format(child_pid),
136146
], True)
137147
self.expect_gdbremote_sequence()
138148

@@ -154,10 +164,9 @@ def test_select_wrong_pid(self):
154164
self.reset_test_sequence()
155165

156166
# get process pid
157-
procinfo_regex = "[$]pid:([0-9a-f]+);.*"
158167
self.test_sequence.add_log_lines([
159168
"read packet: $qProcessInfo#00",
160-
{"direction": "send", "regex": procinfo_regex,
169+
{"direction": "send", "regex": self.procinfo_regex,
161170
"capture": {1: "pid"}},
162171
"read packet: $qC#00",
163172
{"direction": "send", "regex": "[$]QC([0-9a-f]+)#.*",
@@ -167,32 +176,24 @@ def test_select_wrong_pid(self):
167176
pid, tid = (int(ret[x], 16) for x in ("pid", "tid"))
168177
self.reset_test_sequence()
169178

170-
# try switching to correct pid
171179
self.test_sequence.add_log_lines([
180+
# try switching to correct pid
172181
"read packet: $Hgp{:x}.{:x}#00".format(pid, tid),
173-
{"direction": "send", "regex": r"[$]OK#.*"},
182+
"send packet: $OK#00",
174183
"read packet: $Hcp{:x}.{:x}#00".format(pid, tid),
175-
{"direction": "send", "regex": r"[$]OK#.*"},
176-
], True)
177-
ret = self.expect_gdbremote_sequence()
178-
179-
# try switching to invalid tid
180-
self.test_sequence.add_log_lines([
184+
"send packet: $OK#00",
185+
# try switching to invalid tid
181186
"read packet: $Hgp{:x}.{:x}#00".format(pid, tid+1),
182-
{"direction": "send", "regex": r"[$]E15#.*"},
187+
"send packet: $E15#00",
183188
"read packet: $Hcp{:x}.{:x}#00".format(pid, tid+1),
184-
{"direction": "send", "regex": r"[$]E15#.*"},
185-
], True)
186-
ret = self.expect_gdbremote_sequence()
187-
188-
# try switching to invalid pid
189-
self.test_sequence.add_log_lines([
189+
"send packet: $E15#00",
190+
# try switching to invalid pid
190191
"read packet: $Hgp{:x}.{:x}#00".format(pid+1, tid),
191-
{"direction": "send", "regex": r"[$]Eff#.*"},
192+
"send packet: $Eff#00",
192193
"read packet: $Hcp{:x}.{:x}#00".format(pid+1, tid),
193-
{"direction": "send", "regex": r"[$]Eff#.*"},
194+
"send packet: $Eff#00",
194195
], True)
195-
ret = self.expect_gdbremote_sequence()
196+
self.expect_gdbremote_sequence()
196197

197198
@add_test_categories(["fork"])
198199
def test_detach_current(self):
@@ -204,21 +205,20 @@ def test_detach_current(self):
204205
self.reset_test_sequence()
205206

206207
# get process pid
207-
procinfo_regex = "[$]pid:([0-9a-f]+);.*"
208208
self.test_sequence.add_log_lines([
209209
"read packet: $qProcessInfo#00",
210-
{"direction": "send", "regex": procinfo_regex,
210+
{"direction": "send", "regex": self.procinfo_regex,
211211
"capture": {1: "pid"}},
212212
], True)
213213
ret = self.expect_gdbremote_sequence()
214-
pid = int(ret["pid"], 16)
214+
pid = ret["pid"]
215215
self.reset_test_sequence()
216216

217217
# detach the process
218218
self.test_sequence.add_log_lines([
219-
"read packet: $D;{:x}#00".format(pid),
220-
{"direction": "send", "regex": r"[$]OK#.*"},
219+
"read packet: $D;{}#00".format(pid),
220+
"send packet: $OK#00",
221221
"read packet: $qC#00",
222-
{"direction": "send", "regex": r"[$]E44#.*"},
222+
"send packet: $E44#00",
223223
], True)
224-
ret = self.expect_gdbremote_sequence()
224+
self.expect_gdbremote_sequence()

0 commit comments

Comments
 (0)