blob: f89ffbf58b26dc4424f5a673aa7ed7e4f930852f [file] [log] [blame]
Johnny Chen5819ab42011-07-15 22:28:10 +00001"""
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 def test_with_dsym(self):
18 """Exercise some SBValue APIs."""
19 d = {'EXE': self.exe_name}
20 self.buildDsym(dictionary=d)
21 self.setTearDownCleanup(dictionary=d)
22 self.value_api(self.exe_name)
23
24 @python_api_test
25 def test_with_dwarf(self):
26 """Exercise some SBValue APIs."""
27 d = {'EXE': self.exe_name}
28 self.buildDwarf(dictionary=d)
29 self.setTearDownCleanup(dictionary=d)
30 self.value_api(self.exe_name)
31
32 def setUp(self):
33 # Call super's setUp().
34 TestBase.setUp(self)
35 # We'll use the test method name as the exe_name.
36 self.exe_name = self.testMethodName
37 # Find the line number to of function 'c'.
38 self.line = line_number('main.c', '// Break at this line')
39
40 def value_api(self, exe_name):
41 """Exercise some SBValue APIs."""
42 exe = os.path.join(os.getcwd(), exe_name)
43
44 # Create a target by the debugger.
45 target = self.dbg.CreateTarget(exe)
46 self.assertTrue(target, VALID_TARGET)
47
48 # Create the breakpoint inside function 'main'.
49 breakpoint = target.BreakpointCreateByLocation('main.c', self.line)
50 self.assertTrue(breakpoint, VALID_BREAKPOINT)
51
52 # Now launch the process, and do not stop at entry point.
53 process = target.LaunchSimple(None, None, os.getcwd())
54 self.assertTrue(process, PROCESS_IS_VALID)
55
56 # Get Frame #0.
57 self.assertTrue(process.GetState() == lldb.eStateStopped)
58 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
59 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
60 frame0 = thread.GetFrameAtIndex(0)
61
Johnny Chen6cbb8d62011-07-21 23:02:00 +000062 # Get global variable 'days_of_week'.
63 list = target.FindGlobalVariables('days_of_week', 1)
64 days_of_week = list.GetValueAtIndex(0)
65 self.assertTrue(days_of_week, VALID_VARIABLE)
66 self.assertTrue(days_of_week.GetNumChildren() == 7, VALID_VARIABLE)
67 self.DebugSBValue(days_of_week)
68
Johnny Chen989b7ef2011-07-22 00:47:58 +000069 fmt = lldbutil.BasicFormatter()
70 cvf = lldbutil.ChildVisitingFormatter(indent=2)
71 if self.TraceOn():
72 print fmt.format(days_of_week)
73 print cvf.format(days_of_week)
74
Johnny Chen5819ab42011-07-15 22:28:10 +000075 # Get variable 'str_ptr'.
76 value = frame0.FindVariable('str_ptr')
77 self.assertTrue(value, VALID_VARIABLE)
78 self.DebugSBValue(value)
79
Johnny Chen6853cf62011-07-21 19:31:59 +000080 # SBValue::TypeIsPointerType() should return true.
81 self.assertTrue(value.TypeIsPointerType())
82
83 # Verify the SBValue::GetByteSize() API is working correctly.
84 arch = self.getArchitecture()
85 if arch == 'i386':
86 self.assertTrue(value.GetByteSize() == 4)
87 elif arch == 'x86_64':
88 self.assertTrue(value.GetByteSize() == 8)
89
Johnny Chen5819ab42011-07-15 22:28:10 +000090 # Get child at index 5 => 'Friday'.
91 child = value.GetChildAtIndex(5, lldb.eNoDynamicValues, True)
92 self.assertTrue(child, VALID_VARIABLE)
93 self.DebugSBValue(child)
94
95 self.expect(child.GetSummary(), exe=False,
96 substrs = ['Friday'])
97
98 # Now try to get at the same variable using GetValueForExpressionPath().
99 # These two SBValue objects should have the same value.
100 val2 = value.GetValueForExpressionPath('[5]')
101 self.assertTrue(val2, VALID_VARIABLE)
102 self.DebugSBValue(val2)
103 self.assertTrue(child.GetValue() == val2.GetValue() and
104 child.GetSummary() == val2.GetSummary())
105
106if __name__ == '__main__':
107 import atexit
108 lldb.SBDebugger.Initialize()
109 atexit.register(lambda: lldb.SBDebugger.Terminate())
110 unittest2.main()