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