1"""
2Test reproducer attach.
3"""
4
5import lldb
6import tempfile
7from lldbsuite.test import lldbtest_config
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class ReproducerAttachTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16    NO_DEBUG_INFO_TESTCASE = True
17
18    @skipIfFreeBSD
19    @skipIfNetBSD
20    @skipIfWindows
21    @skipIfRemote
22    @skipIfiOSSimulator
23    def test_reproducer_attach(self):
24        """Test thread creation after process attach."""
25        exe = '%s_%d' % (self.testMethodName, os.getpid())
26
27        token = self.getBuildArtifact(exe + '.token')
28        if os.path.exists(token):
29            os.remove(token)
30
31        reproducer = self.getBuildArtifact(exe + '.reproducer')
32        if os.path.exists(reproducer):
33            try:
34                shutil.rmtree(reproducer)
35            except OSError:
36                pass
37
38        self.build(dictionary={'EXE': exe})
39        self.addTearDownHook(self.cleanupSubprocesses)
40
41        inferior = self.spawnSubprocess(self.getBuildArtifact(exe), [token])
42        pid = inferior.pid
43
44        lldbutil.wait_for_file_on_target(self, token)
45
46        # Use Popen because pexpect is overkill and spawnSubprocess is
47        # asynchronous.
48        capture = subprocess.Popen([
49            lldbtest_config.lldbExec, '-b', '--capture', '--capture-path',
50            reproducer, '-o', 'proc att -n {}'.format(exe), '-o',
51            'reproducer generate'
52        ],
53                                   stdin=subprocess.PIPE,
54                                   stdout=subprocess.PIPE,
55                                   stderr=subprocess.PIPE)
56        outs, _ = capture.communicate()
57        outs = outs.decode('utf-8')
58        self.assertIn('Process {} stopped'.format(pid), outs)
59        self.assertIn('Reproducer written', outs)
60
61        # Check that replay works.
62        replay = subprocess.Popen(
63            [lldbtest_config.lldbExec, '-replay', reproducer],
64            stdin=subprocess.PIPE,
65            stdout=subprocess.PIPE,
66            stderr=subprocess.PIPE)
67        outs, _ = replay.communicate()
68        outs = outs.decode('utf-8')
69        self.assertIn('Process {} stopped'.format(pid), outs)
70
71        # We can dump the reproducer in the current context.
72        self.expect('reproducer dump -f {} -p process'.format(reproducer),
73                    substrs=['pid = {}'.format(pid), 'name = {}'.format(exe)])
74