1""" 2Test some SBValue APIs. 3""" 4 5import os, time 6import re 7import unittest2 8import lldb, lldbutil 9from lldbtest import * 10 11class ValueAPITestCase(TestBase): 12 13 mydir = os.path.join("python_api", "value") 14 15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") 16 @python_api_test 17 @dsym_test 18 def test_with_dsym(self): 19 """Exercise some SBValue APIs.""" 20 d = {'EXE': self.exe_name} 21 self.buildDsym(dictionary=d) 22 self.setTearDownCleanup(dictionary=d) 23 self.value_api(self.exe_name) 24 25 @python_api_test 26 @dwarf_test 27 def test_with_dwarf(self): 28 """Exercise some SBValue APIs.""" 29 d = {'EXE': self.exe_name} 30 self.buildDwarf(dictionary=d) 31 self.setTearDownCleanup(dictionary=d) 32 self.value_api(self.exe_name) 33 34 def setUp(self): 35 # Call super's setUp(). 36 TestBase.setUp(self) 37 # We'll use the test method name as the exe_name. 38 self.exe_name = self.testMethodName 39 # Find the line number to of function 'c'. 40 self.line = line_number('main.c', '// Break at this line') 41 42 def value_api(self, exe_name): 43 """Exercise some SBValue APIs.""" 44 exe = os.path.join(os.getcwd(), exe_name) 45 46 # Create a target by the debugger. 47 target = self.dbg.CreateTarget(exe) 48 self.assertTrue(target, VALID_TARGET) 49 50 # Create the breakpoint inside function 'main'. 51 breakpoint = target.BreakpointCreateByLocation('main.c', self.line) 52 self.assertTrue(breakpoint, VALID_BREAKPOINT) 53 54 # Now launch the process, and do not stop at entry point. 55 process = target.LaunchSimple(None, None, os.getcwd()) 56 self.assertTrue(process, PROCESS_IS_VALID) 57 58 # Get Frame #0. 59 self.assertTrue(process.GetState() == lldb.eStateStopped) 60 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) 61 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition") 62 frame0 = thread.GetFrameAtIndex(0) 63 64 # Get global variable 'days_of_week'. 65 list = target.FindGlobalVariables('days_of_week', 1) 66 days_of_week = list.GetValueAtIndex(0) 67 self.assertTrue(days_of_week, VALID_VARIABLE) 68 self.assertTrue(days_of_week.GetNumChildren() == 7, VALID_VARIABLE) 69 self.DebugSBValue(days_of_week) 70 71 # Get global variable 'weekdays'. 72 list = target.FindGlobalVariables('weekdays', 1) 73 weekdays = list.GetValueAtIndex(0) 74 self.assertTrue(weekdays, VALID_VARIABLE) 75 self.assertTrue(weekdays.GetNumChildren() == 5, VALID_VARIABLE) 76 self.DebugSBValue(weekdays) 77 78 # Get global variable 'g_table'. 79 list = target.FindGlobalVariables('g_table', 1) 80 g_table = list.GetValueAtIndex(0) 81 self.assertTrue(g_table, VALID_VARIABLE) 82 self.assertTrue(g_table.GetNumChildren() == 2, VALID_VARIABLE) 83 self.DebugSBValue(g_table) 84 85 fmt = lldbutil.BasicFormatter() 86 cvf = lldbutil.ChildVisitingFormatter(indent_child=2) 87 rdf = lldbutil.RecursiveDecentFormatter(indent_child=2) 88 if self.TraceOn(): 89 print fmt.format(days_of_week) 90 print cvf.format(days_of_week) 91 print cvf.format(weekdays) 92 print rdf.format(g_table) 93 94 # Get variable 'my_int_ptr'. 95 value = frame0.FindVariable('my_int_ptr') 96 self.assertTrue(value, VALID_VARIABLE) 97 self.DebugSBValue(value) 98 99 # Get what 'my_int_ptr' points to. 100 pointed = value.GetChildAtIndex(0) 101 self.assertTrue(pointed, VALID_VARIABLE) 102 self.DebugSBValue(pointed) 103 104 # While we are at it, verify that 'my_int_ptr' points to 'g_my_int'. 105 symbol = target.ResolveLoadAddress(int(pointed.GetLocation(), 0)).GetSymbol() 106 self.assertTrue(symbol) 107 self.expect(symbol.GetName(), exe=False, 108 startstr = 'g_my_int') 109 110 # Get variable 'str_ptr'. 111 value = frame0.FindVariable('str_ptr') 112 self.assertTrue(value, VALID_VARIABLE) 113 self.DebugSBValue(value) 114 115 # SBValue::TypeIsPointerType() should return true. 116 self.assertTrue(value.TypeIsPointerType()) 117 118 # Verify the SBValue::GetByteSize() API is working correctly. 119 arch = self.getArchitecture() 120 if arch == 'i386': 121 self.assertTrue(value.GetByteSize() == 4) 122 elif arch == 'x86_64': 123 self.assertTrue(value.GetByteSize() == 8) 124 125 # Get child at index 5 => 'Friday'. 126 child = value.GetChildAtIndex(5, lldb.eNoDynamicValues, True) 127 self.assertTrue(child, VALID_VARIABLE) 128 self.DebugSBValue(child) 129 130 self.expect(child.GetSummary(), exe=False, 131 substrs = ['Friday']) 132 133 # Now try to get at the same variable using GetValueForExpressionPath(). 134 # These two SBValue objects should have the same value. 135 val2 = value.GetValueForExpressionPath('[5]') 136 self.assertTrue(val2, VALID_VARIABLE) 137 self.DebugSBValue(val2) 138 self.assertTrue(child.GetValue() == val2.GetValue() and 139 child.GetSummary() == val2.GetSummary()) 140 141if __name__ == '__main__': 142 import atexit 143 lldb.SBDebugger.Initialize() 144 atexit.register(lambda: lldb.SBDebugger.Terminate()) 145 unittest2.main() 146