blob: 91de002b7d91c9e9b45fcb982b7af2cb8084068e [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 Chen36d7d912011-07-22 22:01:35 +000069 # Get global variable 'weekdays'.
70 list = target.FindGlobalVariables('weekdays', 1)
71 weekdays = list.GetValueAtIndex(0)
72 self.assertTrue(weekdays, VALID_VARIABLE)
73 self.assertTrue(weekdays.GetNumChildren() == 5, VALID_VARIABLE)
74 self.DebugSBValue(weekdays)
75
76 # Get global variable 'g_table'.
77 list = target.FindGlobalVariables('g_table', 1)
78 g_table = list.GetValueAtIndex(0)
79 self.assertTrue(g_table, VALID_VARIABLE)
80 self.assertTrue(g_table.GetNumChildren() == 2, VALID_VARIABLE)
81 self.DebugSBValue(g_table)
82
Johnny Chen989b7ef2011-07-22 00:47:58 +000083 fmt = lldbutil.BasicFormatter()
Johnny Chen36d7d912011-07-22 22:01:35 +000084 cvf = lldbutil.ChildVisitingFormatter(indent_child=2)
85 rdf = lldbutil.RecursiveDecentFormatter(indent_child=2)
Johnny Chen989b7ef2011-07-22 00:47:58 +000086 if self.TraceOn():
87 print fmt.format(days_of_week)
88 print cvf.format(days_of_week)
Johnny Chen36d7d912011-07-22 22:01:35 +000089 print cvf.format(weekdays)
90 print rdf.format(g_table)
Johnny Chen989b7ef2011-07-22 00:47:58 +000091
Johnny Chen5819ab42011-07-15 22:28:10 +000092 # Get variable 'str_ptr'.
93 value = frame0.FindVariable('str_ptr')
94 self.assertTrue(value, VALID_VARIABLE)
95 self.DebugSBValue(value)
96
Johnny Chen6853cf62011-07-21 19:31:59 +000097 # SBValue::TypeIsPointerType() should return true.
98 self.assertTrue(value.TypeIsPointerType())
99
100 # Verify the SBValue::GetByteSize() API is working correctly.
101 arch = self.getArchitecture()
102 if arch == 'i386':
103 self.assertTrue(value.GetByteSize() == 4)
104 elif arch == 'x86_64':
105 self.assertTrue(value.GetByteSize() == 8)
106
Johnny Chen5819ab42011-07-15 22:28:10 +0000107 # Get child at index 5 => 'Friday'.
108 child = value.GetChildAtIndex(5, lldb.eNoDynamicValues, True)
109 self.assertTrue(child, VALID_VARIABLE)
110 self.DebugSBValue(child)
111
112 self.expect(child.GetSummary(), exe=False,
113 substrs = ['Friday'])
114
115 # Now try to get at the same variable using GetValueForExpressionPath().
116 # These two SBValue objects should have the same value.
117 val2 = value.GetValueForExpressionPath('[5]')
118 self.assertTrue(val2, VALID_VARIABLE)
119 self.DebugSBValue(val2)
120 self.assertTrue(child.GetValue() == val2.GetValue() and
121 child.GetSummary() == val2.GetSummary())
122
123if __name__ == '__main__':
124 import atexit
125 lldb.SBDebugger.Initialize()
126 atexit.register(lambda: lldb.SBDebugger.Terminate())
127 unittest2.main()