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