1"""
2Test lldb-vscode setBreakpoints request
3"""
4
5from __future__ import print_function
6
7import unittest2
8import vscode
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12import lldbvscode_testcase
13import re
14
15class TestVSCode_module(lldbvscode_testcase.VSCodeTestCaseBase):
16
17    mydir = TestBase.compute_mydir(__file__)
18
19    def run_test(self, symbol_basename, expect_debug_info_size):
20        program_basename = "a.out.stripped"
21        program = self.getBuildArtifact(program_basename)
22        self.build_and_launch(program)
23        functions = ['foo']
24        breakpoint_ids = self.set_function_breakpoints(functions)
25        self.assertEquals(len(breakpoint_ids), len(functions), 'expect one breakpoint')
26        self.continue_to_breakpoints(breakpoint_ids)
27        active_modules = self.vscode.get_active_modules()
28        program_module = active_modules[program_basename]
29        self.assertIn(program_basename, active_modules, '%s module is in active modules' % (program_basename))
30        self.assertIn('name', program_module, 'make sure name is in module')
31        self.assertEqual(program_basename, program_module['name'])
32        self.assertIn('path', program_module, 'make sure path is in module')
33        self.assertEqual(program, program_module['path'])
34        self.assertTrue('symbolFilePath' not in program_module, 'Make sure a.out.stripped has no debug info')
35        symbols_path = self.getBuildArtifact(symbol_basename)
36        self.vscode.request_evaluate('`%s' % ('target symbols add -s "%s" "%s"' % (program, symbols_path)))
37
38        def checkSymbolsLoadedWithSize():
39            active_modules = self.vscode.get_active_modules()
40            program_module = active_modules[program_basename]
41            self.assertIn('symbolFilePath', program_module)
42            self.assertIn(symbols_path, program_module['symbolFilePath'])
43            symbol_regex = re.compile(r"[0-9]+(\.[0-9]*)?[KMG]?B")
44            return symbol_regex.match(program_module['symbolStatus'])
45
46        if expect_debug_info_size:
47            self.waitUntil(checkSymbolsLoadedWithSize)
48        active_modules = self.vscode.get_active_modules()
49        program_module = active_modules[program_basename]
50        self.assertEqual(program_basename, program_module['name'])
51        self.assertEqual(program, program_module['path'])
52        self.assertIn('addressRange', program_module)
53
54    @skipIfWindows
55    @skipUnlessDarwin
56    @skipIfRemote
57    #TODO: Update the Makefile so that this test runs on Linux
58    def test_module_event(self):
59        '''
60            Mac or linux.
61
62            On mac, if we load a.out as our symbol file, we will use DWARF with .o files and we will
63            have debug symbols, but we won't see any debug info size because all of the DWARF
64            sections are in .o files.
65
66            On other platforms, we expect a.out to have debug info, so we will expect a size.
67            expect_debug_info_size = platform.system() != 'Darwin'
68            return self.run_test("a.out", expect_debug_info_size)
69        '''
70        expect_debug_info_size = platform.system() != 'Darwin'
71        return self.run_test("a.out", expect_debug_info_size)
72
73    @skipIfWindows
74    @skipUnlessDarwin
75    @skipIfRemote
76    def test_module_event_dsym(self):
77        '''
78            Darwin only test with dSYM file.
79
80            On mac, if we load a.out.dSYM as our symbol file, we will have debug symbols and we
81            will have DWARF sections added to the module, so we will expect a size.
82            return self.run_test("a.out.dSYM", True)
83        '''
84        return self.run_test("a.out.dSYM", True)
85
86    @skipIfWindows
87    @skipUnlessDarwin
88    @skipIfRemote
89    def test_compile_units(self):
90        program = self.getBuildArtifact("a.out")
91        self.build_and_launch(program)
92        source = "main.cpp"
93        main_source_path = self.getSourcePath(source)
94        breakpoint1_line = line_number(source, '// breakpoint 1')
95        lines = [breakpoint1_line]
96        breakpoint_ids = self.set_source_breakpoints(source, lines)
97        self.continue_to_breakpoints(breakpoint_ids)
98        moduleId = self.vscode.get_active_modules()['a.out']['id']
99        response = self.vscode.request_getCompileUnits(moduleId)
100        self.assertTrue(response['body'])
101        self.assertTrue(len(response['body']['compileUnits']) == 1,
102                        'Only one source file should exist')
103        self.assertTrue(response['body']['compileUnits'][0]['compileUnitPath'] == main_source_path,
104                        'Real path to main.cpp matches')
105
106