95 lines · python
1"""Test that C++ global variables can be inspected by name and also their mangled name."""2 3 4from lldbsuite.test.decorators import *5from lldbsuite.test.lldbtest import *6from lldbsuite.test import lldbutil7 8 9class GlobalVariablesCppTestCase(TestBase):10 def setUp(self):11 TestBase.setUp(self)12 self.source = lldb.SBFileSpec("main.cpp")13 14 def test(self):15 self.build()16 17 (target, _, _, _) = lldbutil.run_to_source_breakpoint(18 self, "// Set break point at this line.", self.source19 )20 21 # Check that we can access g_file_global_int by its name22 self.expect(23 "target variable g_file_global_int",24 VARIABLES_DISPLAYED_CORRECTLY,25 substrs=["42"],26 )27 self.expect(28 "target variable abc::g_file_global_int",29 VARIABLES_DISPLAYED_CORRECTLY,30 substrs=["42"],31 )32 self.expect(33 "target variable xyz::g_file_global_int",34 VARIABLES_DISPLAYED_CORRECTLY,35 error=True,36 substrs=["can't find global variable"],37 )38 39 # Check that we can access g_file_global_const_int by its name40 self.expect(41 "target variable g_file_global_const_int",42 VARIABLES_DISPLAYED_CORRECTLY,43 substrs=["1337"],44 )45 self.expect(46 "target variable abc::g_file_global_const_int",47 VARIABLES_DISPLAYED_CORRECTLY,48 substrs=["1337"],49 )50 self.expect(51 "target variable xyz::g_file_global_const_int",52 VARIABLES_DISPLAYED_CORRECTLY,53 error=True,54 substrs=["can't find global variable"],55 )56 57 # Try accessing a global variable in anonymous namespace.58 self.expect(59 "target variable g_anon_namespace_const_int",60 VARIABLES_DISPLAYED_CORRECTLY,61 substrs=["100"],62 )63 self.expect(64 "target variable abc::g_anon_namespace_const_int",65 VARIABLES_DISPLAYED_CORRECTLY,66 error=True,67 substrs=["can't find global variable"],68 )69 var = target.FindFirstGlobalVariable(70 "abc::(anonymous namespace)::g_anon_namespace_const_int"71 )72 self.assertTrue(var.IsValid())73 self.assertEqual(74 var.GetName(), "abc::(anonymous namespace)::g_anon_namespace_const_int"75 )76 self.assertEqual(var.GetValue(), "100")77 78 @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")79 def test_access_by_mangled_name(self):80 self.build()81 82 (target, _, _, _) = lldbutil.run_to_source_breakpoint(83 self, "// Set break point at this line.", self.source84 )85 86 # Check that we can access g_file_global_int by its mangled name87 addr = target.EvaluateExpression("&abc::g_file_global_int").GetValueAsUnsigned()88 self.assertNotEqual(addr, 0)89 mangled = lldb.SBAddress(addr, target).GetSymbol().GetMangledName()90 self.assertNotEqual(mangled, None)91 gv = target.FindFirstGlobalVariable(mangled)92 self.assertTrue(gv.IsValid())93 self.assertEqual(gv.GetName(), "abc::g_file_global_int")94 self.assertEqual(gv.GetValueAsUnsigned(), 42)95