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    @skipIfReproducer
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
40        inferior = self.spawnSubprocess(self.getBuildArtifact(exe), [token])
41        pid = inferior.pid
42
43        lldbutil.wait_for_file_on_target(self, token)
44
45        # Use Popen because pexpect is overkill and spawnSubprocess is
46        # asynchronous.
47        capture = subprocess.Popen([
48            lldbtest_config.lldbExec, '-b', '--no-lldbinit', '--no-use-colors']
49            + sum(map(lambda x: ['-O', x], self.setUpCommands()), [])
50            + ['--capture', '--capture-path', reproducer,
51            '-o', 'proc att -n {}'.format(exe), '-o', '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