File: TestWeakSymbols.py

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-6~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,418,812 kB
  • sloc: cpp: 5,290,827; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,900; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,892; xml: 953; cs: 573; fortran: 539
file content (76 lines) | stat: -rw-r--r-- 3,278 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Test that we can compile expressions referring to
absent weak symbols from a dylib.
"""



import os
import lldb
from lldbsuite.test import decorators
import lldbsuite.test.lldbutil as lldbutil
from lldbsuite.test.lldbtest import *


class TestWeakSymbolsInExpressions(TestBase):

    mydir = TestBase.compute_mydir(__file__)

    NO_DEBUG_INFO_TESTCASE = True

    @decorators.skipUnlessDarwin
    def test_weak_symbol_in_expr(self):
        """Tests that we can refer to weak symbols in expressions."""
        self.build()
        self.main_source_file = lldb.SBFileSpec("main.c")
        self.do_test()

    def run_weak_var_check (self, weak_varname, present):
        # The expression will modify present_weak_int to signify which branch
        # was taken.  Set it to so we don't get confused by a previous run.
        value = self.target.FindFirstGlobalVariable("present_weak_int")
        value.SetValueFromCString("0")
        if present:
            correct_value = 10
        else:
            correct_value = 20

        # Note, I'm adding the "; 10" at the end of the expression to work around
        # the bug that expressions with no result currently return False for Success()...
        expr = "if (&" + weak_varname + " != NULL) { present_weak_int = 10; } else { present_weak_int = 20;}; 10"
        result = self.frame.EvaluateExpression(expr)
        self.assertSuccess(result.GetError(), "absent_weak_int expr failed")
        self.assertEqual(value.GetValueAsSigned(), correct_value, "Didn't change present_weak_int correctly.")

    def do_test(self):
        hidden_dir = os.path.join(self.getBuildDir(), "hidden")
        hidden_dylib = os.path.join(hidden_dir, "libdylib.dylib")

        launch_info = lldb.SBLaunchInfo(None)
        launch_info.SetWorkingDirectory(self.getBuildDir())
        launch_info.SetLaunchFlags(lldb.eLaunchFlagInheritTCCFromParent)

        (self.target, _, thread, _) = lldbutil.run_to_source_breakpoint(
                                              self, "Set a breakpoint here",
                                              self.main_source_file,
                                              launch_info = launch_info,
                                              extra_images = [hidden_dylib])
        # First we have to import the Dylib module so we get the type info
        # for the weak symbol.  We need to add the source dir to the module
        # search paths, and then run @import to introduce it into the expression
        # context:
        self.dbg.HandleCommand("settings set target.clang-module-search-paths " + self.getSourceDir())

        self.frame = thread.frames[0]
        self.assertTrue(self.frame.IsValid(), "Got a good frame")
        options = lldb.SBExpressionOptions()
        options.SetLanguage(lldb.eLanguageTypeObjC)
        result = self.frame.EvaluateExpression("@import Dylib", options)

        # Now run an expression that references an absent weak symbol:
        self.run_weak_var_check("absent_weak_int", False)
        self.run_weak_var_check("absent_weak_function", False)

        # Make sure we can do the same thing with present weak symbols
        self.run_weak_var_check("present_weak_int", True)
        self.run_weak_var_check("present_weak_function", True)