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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
"""
Test lldb-gdbserver operation
"""
import unittest2
import pexpect
import socket
import subprocess
import sys
import time
from lldbtest import *
from lldbgdbserverutils import *
import logging
import os.path
class LldbGdbServerTestCase(TestBase):
mydir = TestBase.compute_mydir(__file__)
port = 12345
_TIMEOUT_SECONDS = 5
_GDBREMOTE_KILL_PACKET = "$k#6b"
_LOGGING_LEVEL = logging.WARNING
# _LOGGING_LEVEL = logging.DEBUG
def setUp(self):
TestBase.setUp(self)
FORMAT = '%(asctime)-15s %(levelname)-8s %(message)s'
logging.basicConfig(format=FORMAT)
self.logger = logging.getLogger(__name__)
self.logger.setLevel(self._LOGGING_LEVEL)
self.test_sequence = GdbRemoteTestSequence(self.logger)
def init_llgs_test(self):
self.debug_monitor_exe = get_lldb_gdbserver_exe()
if not self.debug_monitor_exe:
self.skipTest("lldb_gdbserver exe not found")
def init_debugserver_test(self):
self.debug_monitor_exe = get_debugserver_exe()
if not self.debug_monitor_exe:
self.skipTest("debugserver exe not found")
def create_socket(self):
sock = socket.socket()
logger = self.logger
def shutdown_socket():
if sock:
try:
# send the kill packet so lldb-gdbserver shuts down gracefully
sock.sendall(LldbGdbServerTestCase._GDBREMOTE_KILL_PACKET)
except:
logger.warning("failed to send kill packet to debug monitor: {}; ignoring".format(sys.exc_info()[0]))
try:
sock.close()
except:
logger.warning("failed to close socket to debug monitor: {}; ignoring".format(sys.exc_info()[0]))
self.addTearDownHook(shutdown_socket)
sock.connect(('localhost', self.port))
return sock
def start_server(self, attach_pid=None):
# Create the command line
commandline = "{} localhost:{}".format(self.debug_monitor_exe, self.port)
if attach_pid:
commandline += " --attach=%d" % attach_pid
# start the server
server = pexpect.spawn(commandline)
# Turn on logging for what the child sends back.
if self.TraceOn():
server.logfile_read = sys.stdout
# Schedule debug monitor to be shut down during teardown.
logger = self.logger
def shutdown_debug_monitor():
try:
server.close()
except:
logger.warning("failed to close pexpect server for debug monitor: {}; ignoring".format(sys.exc_info()[0]))
self.addTearDownHook(shutdown_debug_monitor)
# Wait until we receive the server ready message before continuing.
server.expect_exact('Listening to port {} for a connection from localhost'.format(self.port))
# Create a socket to talk to the server
self.sock = self.create_socket()
return server
def launch_process_for_attach(self,sleep_seconds=3):
# We're going to start a child process that the debug monitor stub can later attach to.
# This process needs to be started so that it just hangs around for a while. We'll
# have it sleep.
exe_path = os.path.abspath("a.out")
args = [exe_path]
if sleep_seconds:
args.append("sleep:%d" % sleep_seconds)
return subprocess.Popen(args)
def add_no_ack_remote_stream(self):
self.test_sequence.add_log_lines(
["read packet: +",
"read packet: $QStartNoAckMode#b0",
"send packet: +",
"send packet: $OK#9a",
"read packet: +"],
True)
def add_verified_launch_packets(self, launch_args):
self.test_sequence.add_log_lines(
["read packet: %s" % build_gdbremote_A_packet(launch_args),
"send packet: $OK#00",
"read packet: $qLaunchSuccess#a5",
"send packet: $OK#00"],
True)
def add_get_pid(self):
self.test_sequence.add_log_lines(
["read packet: $qProcessInfo#00",
{ "direction":"send", "regex":r"^\$pid:([0-9a-fA-F]+);", "capture":{1:"pid"} }],
True)
def expect_gdbremote_sequence(self):
return expect_lldb_gdbserver_replay(self, self.sock, self.test_sequence, self._TIMEOUT_SECONDS, self.logger)
@debugserver_test
def test_exe_starts_debugserver(self):
self.init_debugserver_test()
server = self.start_server()
@llgs_test
def test_exe_starts_llgs(self):
self.init_llgs_test()
server = self.start_server()
def start_no_ack_mode(self):
server = self.start_server()
self.assertIsNotNone(server)
self.add_no_ack_remote_stream()
self.expect_gdbremote_sequence()
@debugserver_test
def test_start_no_ack_mode_debugserver(self):
self.init_debugserver_test()
self.start_no_ack_mode()
@llgs_test
def test_start_no_ack_mode_llgs(self):
self.init_llgs_test()
self.start_no_ack_mode()
def thread_suffix_supported(self):
server = self.start_server()
self.assertIsNotNone(server)
self.add_no_ack_remote_stream()
self.test_sequence.add_log_lines(
["lldb-gdbserver < 26> read packet: $QThreadSuffixSupported#e4",
"lldb-gdbserver < 6> send packet: $OK#9a"],
True)
self.expect_gdbremote_sequence()
@debugserver_test
def test_thread_suffix_supported_debugserver(self):
self.init_debugserver_test()
self.thread_suffix_supported()
@llgs_test
@unittest2.expectedFailure()
def test_thread_suffix_supported_llgs(self):
self.init_llgs_test()
self.thread_suffix_supported()
def list_threads_in_stop_reply_supported(self):
server = self.start_server()
self.assertIsNotNone(server)
self.add_no_ack_remote_stream()
self.test_sequence.add_log_lines(
["lldb-gdbserver < 27> read packet: $QListThreadsInStopReply#21",
"lldb-gdbserver < 6> send packet: $OK#9a"],
True)
self.expect_gdbremote_sequence()
@debugserver_test
def test_list_threads_in_stop_reply_supported_debugserver(self):
self.init_debugserver_test()
self.list_threads_in_stop_reply_supported()
@llgs_test
@unittest2.expectedFailure()
def test_list_threads_in_stop_reply_supported_llgs(self):
self.init_llgs_test()
self.list_threads_in_stop_reply_supported()
def start_inferior(self):
server = self.start_server()
self.assertIsNotNone(server)
# build launch args
launch_args = [os.path.abspath('a.out')]
self.add_no_ack_remote_stream()
self.test_sequence.add_log_lines(
["read packet: %s" % build_gdbremote_A_packet(launch_args),
"send packet: $OK#9a"],
True)
self.expect_gdbremote_sequence()
@debugserver_test
@dsym_test
def test_start_inferior_debugserver_dsym(self):
self.init_debugserver_test()
self.buildDsym()
self.start_inferior()
@llgs_test
@dwarf_test
def test_start_inferior_llgs_dwarf(self):
self.init_llgs_test()
self.buildDwarf()
self.start_inferior()
def inferior_exit_0(self):
server = self.start_server()
self.assertIsNotNone(server)
# build launch args
launch_args = [os.path.abspath('a.out')]
self.add_no_ack_remote_stream()
self.add_verified_launch_packets(launch_args)
self.test_sequence.add_log_lines(
["read packet: $vCont;c#00",
"send packet: $W00#00"],
True)
self.expect_gdbremote_sequence()
@debugserver_test
@dsym_test
def test_inferior_exit_0_debugserver_dsym(self):
self.init_debugserver_test()
self.buildDsym()
self.inferior_exit_0()
@llgs_test
@dwarf_test
@unittest2.expectedFailure()
def test_inferior_exit_0_llgs_dwarf(self):
self.init_llgs_test()
self.buildDwarf()
self.inferior_exit_0()
def inferior_exit_42(self):
server = self.start_server()
self.assertIsNotNone(server)
RETVAL = 42
# build launch args
launch_args = [os.path.abspath('a.out'), "retval:%d" % RETVAL]
self.add_no_ack_remote_stream()
self.add_verified_launch_packets(launch_args)
self.test_sequence.add_log_lines(
["read packet: $vCont;c#00",
"send packet: $W{0:02x}#00".format(RETVAL)],
True)
self.expect_gdbremote_sequence()
@debugserver_test
@dsym_test
def test_inferior_exit_42_debugserver_dsym(self):
self.init_debugserver_test()
self.buildDsym()
self.inferior_exit_42()
@llgs_test
@dwarf_test
@unittest2.expectedFailure()
def test_inferior_exit_42_llgs_dwarf(self):
self.init_llgs_test()
self.buildDwarf()
self.inferior_exit_42()
def inferior_print_exit(self):
server = self.start_server()
self.assertIsNotNone(server)
# build launch args
launch_args = [os.path.abspath('a.out'), "hello, world"]
self.add_no_ack_remote_stream()
self.add_verified_launch_packets(launch_args)
self.test_sequence.add_log_lines(
["read packet: $vCont;c#00",
"send packet: $O{}#00".format(gdbremote_hex_encode_string("hello, world\r\n")),
"send packet: $W00#00"],
True)
self.expect_gdbremote_sequence()
@debugserver_test
@dsym_test
def test_inferior_print_exit_debugserver_dsym(self):
self.init_debugserver_test()
self.buildDsym()
self.inferior_print_exit()
@llgs_test
@dwarf_test
@unittest2.expectedFailure()
def test_inferior_print_exit_llgs_dwarf(self):
self.init_llgs_test()
self.buildDwarf()
self.inferior_print_exit()
def first_launch_stop_reply_thread_matches_first_qC(self):
server = self.start_server()
self.assertIsNotNone(server)
# build launch args
launch_args = [os.path.abspath('a.out'), "hello, world"]
self.add_no_ack_remote_stream()
self.add_verified_launch_packets(launch_args)
self.test_sequence.add_log_lines(
["read packet: $qC#00",
{ "direction":"send", "regex":r"^\$QC([0-9a-fA-F]+)#", "capture":{1:"thread_id"} },
"read packet: $?#00",
{ "direction":"send", "regex":r"^\$T[0-9a-fA-F]{2}thread:([0-9a-fA-F]+)", "expect_captures":{1:"thread_id"} }],
True)
self.expect_gdbremote_sequence()
@debugserver_test
@dsym_test
def test_first_launch_stop_reply_thread_matches_first_qC_debugserver_dsym(self):
self.init_debugserver_test()
self.buildDsym()
self.first_launch_stop_reply_thread_matches_first_qC()
@llgs_test
@dwarf_test
@unittest2.expectedFailure()
def test_first_launch_stop_reply_thread_matches_first_qC_llgs_dwarf(self):
self.init_llgs_test()
self.buildDwarf()
self.first_launch_stop_reply_thread_matches_first_qC()
def qProcessInfo_returns_running_process(self):
server = self.start_server()
self.assertIsNotNone(server)
# Build launch args
launch_args = [os.path.abspath('a.out'), "hello, world"]
# Build the expected protocol stream
self.add_no_ack_remote_stream()
self.add_verified_launch_packets(launch_args)
self.test_sequence.add_log_lines(
["read packet: $qProcessInfo#00",
{ "direction":"send", "regex":r"^\$pid:([0-9a-fA-F]+);", "capture":{1:"pid"} }],
True)
# Run the stream
context = self.expect_gdbremote_sequence()
self.assertIsNotNone(context)
# Ensure the process id looks reasonable.
pid_text = context.get('pid', None)
self.assertIsNotNone(pid_text)
pid = int(pid_text, base=16)
self.assertNotEqual(0, pid)
# If possible, verify that the process is running.
self.assertTrue(process_is_running(pid, True))
@debugserver_test
@dsym_test
def test_qProcessInfo_returns_running_process_debugserver_dsym(self):
self.init_debugserver_test()
self.buildDsym()
self.qProcessInfo_returns_running_process()
@llgs_test
@dwarf_test
@unittest2.expectedFailure()
def test_qProcessInfo_returns_running_process_llgs_dwarf(self):
self.init_llgs_test()
self.buildDwarf()
self.qProcessInfo_returns_running_process()
def attach_commandline_qProcessInfo_reports_pid(self):
# Launch the process that we'll use as the inferior.
inferior = self.launch_process_for_attach()
self.assertIsNotNone(inferior)
self.assertTrue(inferior.pid > 0)
# Launch the debug monitor stub, attaching to the inferior.
server = self.start_server(attach_pid=inferior.pid)
self.assertIsNotNone(server)
# Check that the stub reports attachment to the inferior.
self.add_no_ack_remote_stream()
self.add_get_pid()
context = self.expect_gdbremote_sequence()
# Ensure the process id matches what we expected.
pid_text = context.get('pid', None)
self.assertIsNotNone(pid_text)
reported_pid = int(pid_text, base=16)
self.assertEqual(reported_pid, inferior.pid)
@debugserver_test
@dsym_test
def test_attach_commandline_qProcessInfo_reports_pid_debugserver_dsym(self):
self.init_debugserver_test()
self.buildDsym()
self.attach_commandline_qProcessInfo_reports_pid()
@llgs_test
@dwarf_test
@unittest2.expectedFailure()
def test_attach_commandline_qProcessInfo_reports_pid_llgs_dwarf(self):
self.init_llgs_test()
self.buildDwarf()
self.attach_commandline_qProcessInfo_reports_pid()
def attach_commandline_continue_app_exits(self):
# Launch the process that we'll use as the inferior.
inferior = self.launch_process_for_attach(sleep_seconds=1)
self.assertIsNotNone(inferior)
self.assertTrue(inferior.pid > 0)
# Launch the debug monitor stub, attaching to the inferior.
server = self.start_server(attach_pid=inferior.pid)
self.assertIsNotNone(server)
# Check that the stub reports attachment to the inferior.
self.add_no_ack_remote_stream()
self.add_get_pid()
self.test_sequence.add_log_lines(
["read packet: $vCont;c#00",
"send packet: $W00#00"],
True)
self.expect_gdbremote_sequence()
# Process should be dead now. Reap results.
poll_result = inferior.poll()
self.assertIsNotNone(poll_result)
# Where possible, verify at the system level that the process is not running.
self.assertFalse(process_is_running(inferior.pid, False))
@debugserver_test
@dsym_test
def test_attach_commandline_continue_app_exits_debugserver_dsym(self):
self.init_debugserver_test()
self.buildDsym()
self.attach_commandline_continue_app_exits()
@llgs_test
@dwarf_test
@unittest2.expectedFailure()
def test_attach_commandline_continue_app_exits_llgs_dwarf(self):
self.init_llgs_test()
self.buildDwarf()
self.attach_commandline_continue_app_exits()
def attach_commandline_kill_after_initial_stop(self):
# Launch the process that we'll use as the inferior.
inferior = self.launch_process_for_attach(sleep_seconds=10)
self.assertIsNotNone(inferior)
self.assertTrue(inferior.pid > 0)
# Launch the debug monitor stub, attaching to the inferior.
server = self.start_server(attach_pid=inferior.pid)
self.assertIsNotNone(server)
# Check that the stub reports attachment to the inferior.
self.add_no_ack_remote_stream()
self.add_get_pid()
self.test_sequence.add_log_lines(
["read packet: $k#6b",
"send packet: $X09#00"],
True)
self.expect_gdbremote_sequence()
# Process should be dead now. Reap results.
poll_result = inferior.poll()
self.assertIsNotNone(poll_result)
# Where possible, verify at the system level that the process is not running.
self.assertFalse(process_is_running(inferior.pid, False))
@debugserver_test
@dsym_test
def test_attach_commandline_kill_after_initial_stop_debugserver_dsym(self):
self.init_debugserver_test()
self.buildDsym()
self.attach_commandline_kill_after_initial_stop()
@llgs_test
@dwarf_test
@unittest2.expectedFailure()
def test_attach_commandline_kill_after_initial_stop_llgs_dwarf(self):
self.init_llgs_test()
self.buildDwarf()
self.attach_commandline_kill_after_initial_stop()
if __name__ == '__main__':
unittest2.main()
|