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            symbolsStatus = program_module['debugInfoSize']
42            symbol_regex = re.compile(r"[0-9]+(\.[0-9]*)?[KMG]?B")
43            return symbol_regex.match(program_module['symbolStatus'])
44
45        if expect_debug_info_size:
46            self.waitUntil(checkSymbolsLoadedWithSize)
47        active_modules = self.vscode.get_active_modules()
48        program_module = active_modules[program_basename]
49        self.assertEqual(program_basename, program_module['name'])
50        self.assertEqual(program, program_module['path'])
51        self.assertIn('symbolFilePath', program_module)
52        self.assertIn(symbols_path, program_module['symbolFilePath'])
53        self.assertIn('addressRange', program_module)
54
55    @skipIfWindows
56    @skipUnlessDarwin
57    @skipIfRemote
58    #TODO: Update the Makefile so that this test runs on Linux
59    def test_module_event(self):
60        '''
61            Mac or linux.
62
63            On mac, if we load a.out as our symbol file, we will use DWARF with .o files and we will
64            have debug symbols, but we won't see any debug info size because all of the DWARF
65            sections are in .o files.
66
67            On other platforms, we expect a.out to have debug info, so we will expect a size.
68            expect_debug_info_size = platform.system() != 'Darwin'
69            return self.run_test("a.out", expect_debug_info_size)
70        '''
71        expect_debug_info_size = platform.system() != 'Darwin'
72        return self.run_test("a.out", expect_debug_info_size)
73
74    @skipIfWindows
75    @skipUnlessDarwin
76    @skipIfRemote
77    def test_module_event_dsym(self):
78        '''
79            Darwin only test with dSYM file.
80
81            On mac, if we load a.out.dSYM as our symbol file, we will have debug symbols and we
82            will have DWARF sections added to the module, so we will expect a size.
83            return self.run_test("a.out.dSYM", True)
84        '''
85        return self.run_test("a.out.dSYM", True)
86
87    @skipIfWindows
88    @skipUnlessDarwin
89    @skipIfRemote
90    def test_compile_units(self):
91        program = self.getBuildArtifact("a.out")
92        self.build_and_launch(program)
93        source = "main.cpp"
94        main_source_path = self.getSourcePath(source)
95        breakpoint1_line = line_number(source, '// breakpoint 1')
96        lines = [breakpoint1_line]
97        breakpoint_ids = self.set_source_breakpoints(source, lines)
98        self.continue_to_breakpoints(breakpoint_ids)
99        moduleId = self.vscode.get_active_modules()['a.out']['id']
100        response = self.vscode.request_getCompileUnits(moduleId)
101        self.assertTrue(response['body'])
102        self.assertTrue(len(response['body']['compileUnits']) == 1,
103                        'Only one source file should exist')
104        self.assertTrue(response['body']['compileUnits'][0]['compileUnitPath'] == main_source_path,
105                        'Real path to main.cpp matches')
106
107