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    @skipIfNetBSD
19    @skipIfWindows
20    @skipIfRemote
21    @skipIfiOSSimulator
22    def test_reproducer_attach(self):
23        """Test thread creation after process attach."""
24        exe = '%s_%d' % (self.testMethodName, os.getpid())
25
26        token = self.getBuildArtifact(exe + '.token')
27        if os.path.exists(token):
28            os.remove(token)
29
30        reproducer = self.getBuildArtifact(exe + '.reproducer')
31        if os.path.exists(reproducer):
32            try:
33                shutil.rmtree(reproducer)
34            except OSError:
35                pass
36
37        self.build(dictionary={'EXE': exe})
38
39        inferior = self.spawnSubprocess(self.getBuildArtifact(exe), [token])
40        pid = inferior.pid
41
42        lldbutil.wait_for_file_on_target(self, token)
43
44        # Use Popen because pexpect is overkill and spawnSubprocess is
45        # asynchronous.
46        capture = subprocess.Popen([
47            lldbtest_config.lldbExec, '-b', '--no-lldbinit', '--no-use-colors']
48            + sum(map(lambda x: ['-O', x], self.setUpCommands()), [])
49            + ['--capture', '--capture-path', reproducer,
50            '-o', 'proc att -n {}'.format(exe), '-o', 'reproducer generate'
51        ],
52                                   stdin=subprocess.PIPE,
53                                   stdout=subprocess.PIPE,
54                                   stderr=subprocess.PIPE)
55        outs, _ = capture.communicate()
56        outs = outs.decode('utf-8')
57        self.assertIn('Process {} stopped'.format(pid), outs)
58        self.assertIn('Reproducer written', outs)
59
60        # Check that replay works.
61        replay = subprocess.Popen(
62            [lldbtest_config.lldbExec, '-replay', reproducer],
63            stdin=subprocess.PIPE,
64            stdout=subprocess.PIPE,
65            stderr=subprocess.PIPE)
66        outs, _ = replay.communicate()
67        outs = outs.decode('utf-8')
68        self.assertIn('Process {} stopped'.format(pid), outs)
69
70        # We can dump the reproducer in the current context.
71        self.expect('reproducer dump -f {} -p process'.format(reproducer),
72                    substrs=['pid = {}'.format(pid), 'name = {}'.format(exe)])
73