1"""
2Test SBTarget APIs.
3"""
4
5
6
7import unittest2
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class TestNameLookup(TestBase):
15
16    mydir = TestBase.compute_mydir(__file__)
17
18    @add_test_categories(['pyapi'])
19    @expectedFailureAll(oslist=["windows"], bugnumber='llvm.org/pr21765')
20    def test_target(self):
21        """Exercise SBTarget.FindFunctions() with various name masks.
22
23        A previous regression caused mangled names to not be able to be looked up.
24        This test verifies that using a mangled name with eFunctionNameTypeFull works
25        and that using a function basename with eFunctionNameTypeFull works for all
26        C++ functions that are at the global namespace level."""
27        self.build();
28        exe = self.getBuildArtifact("a.out")
29
30        # Create a target by the debugger.
31        target = self.dbg.CreateTarget(exe)
32        self.assertTrue(target, VALID_TARGET)
33
34        exe_module = target.FindModule(target.GetExecutable())
35
36        c_name_to_symbol = {}
37        cpp_name_to_symbol = {}
38        mangled_to_symbol = {}
39        num_symbols = exe_module.GetNumSymbols();
40        for i in range(num_symbols):
41            symbol = exe_module.GetSymbolAtIndex(i);
42            name = symbol.GetName()
43            if name and 'unique_function_name' in name and '__PRETTY_FUNCTION__' not in name:
44                mangled = symbol.GetMangledName()
45                if mangled:
46                    mangled_to_symbol[mangled] = symbol
47                    if name:
48                        cpp_name_to_symbol[name] = symbol
49                elif name:
50                    c_name_to_symbol[name] = symbol
51
52        # Make sure each mangled name turns up exactly one match when looking up
53        # functions by full name and using the mangled name as the name in the
54        # lookup
55        self.assertGreaterEqual(len(mangled_to_symbol), 6)
56        for mangled in mangled_to_symbol.keys():
57            symbol_contexts = target.FindFunctions(mangled, lldb.eFunctionNameTypeFull)
58            self.assertEquals(symbol_contexts.GetSize(), 1)
59            for symbol_context in symbol_contexts:
60                self.assertTrue(symbol_context.GetFunction().IsValid())
61                self.assertTrue(symbol_context.GetSymbol().IsValid())
62
63
64