1from __future__ import print_function
2
3
4import unittest2
5import lldb
6from lldbsuite.test.lldbtest import *
7import lldbsuite.test.lldbutil as lldbutil
8
9
10class TestMoveNearest(TestBase):
11    NO_DEBUG_INFO_TESTCASE = True
12
13    def setUp(self):
14        # Call super's setUp().
15        TestBase.setUp(self)
16        # Find the line number to break inside main().
17        self.line1 = line_number('foo.h', '// !BR1')
18        self.line2 = line_number('foo.h', '// !BR2')
19        self.line_between = line_number('main.cpp', "// BR_Between")
20        print("BR_Between found at", self.line_between)
21        self.line_main = line_number('main.cpp', '// !BR_main')
22
23    def test(self):
24        """Test target.move-to-nearest logic"""
25
26        self.build()
27        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
28        self.assertTrue(target, VALID_TARGET)
29
30        lldbutil.run_break_set_by_symbol(self, 'main', sym_exact=True)
31        environment = self.registerSharedLibrariesWithTarget(target, ["foo"])
32        process = target.LaunchSimple(None, environment, self.get_process_working_directory())
33        self.assertState(process.GetState(), lldb.eStateStopped)
34
35        # Regardless of the -m value the breakpoint should have exactly one
36        # location on the foo functions
37        self.runCmd("settings set target.move-to-nearest-code true")
38        lldbutil.run_break_set_by_file_and_line(self, 'foo.h', self.line1,
39                loc_exact=True, extra_options="-m 1")
40        lldbutil.run_break_set_by_file_and_line(self, 'foo.h', self.line2,
41                loc_exact=True, extra_options="-m 1")
42
43        self.runCmd("settings set target.move-to-nearest-code false")
44        lldbutil.run_break_set_by_file_and_line(self, 'foo.h', self.line1,
45                loc_exact=True, extra_options="-m 0")
46        lldbutil.run_break_set_by_file_and_line(self, 'foo.h', self.line2,
47                loc_exact=True, extra_options="-m 0")
48
49
50        # Make sure we set a breakpoint in main with -m 1 for various lines in
51        # the function declaration
52        # "int"
53        lldbutil.run_break_set_by_file_and_line(self, 'main.cpp',
54                self.line_main-1, extra_options="-m 1")
55        # "main()"
56        lldbutil.run_break_set_by_file_and_line(self, 'main.cpp',
57                self.line_main, extra_options="-m 1")
58        # "{"
59        lldbutil.run_break_set_by_file_and_line(self, 'main.cpp',
60                self.line_main+1, extra_options="-m 1")
61        # "return .."
62        lldbutil.run_break_set_by_file_and_line(self, 'main.cpp',
63                self.line_main+2, extra_options="-m 1")
64
65        # Make sure we don't put move the breakpoint if it is set between two functions:
66        lldbutil.run_break_set_by_file_and_line(self, 'main.cpp',
67                self.line_between, extra_options="-m 1", num_expected_locations=0)
68