Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test some SBValue APIs. |
| 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import re |
| 7 | import unittest2 |
| 8 | import lldb, lldbutil |
| 9 | from lldbtest import * |
| 10 | |
| 11 | class 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 | |
| 62 | # Get variable 'str_ptr'. |
| 63 | value = frame0.FindVariable('str_ptr') |
| 64 | self.assertTrue(value, VALID_VARIABLE) |
| 65 | self.DebugSBValue(value) |
| 66 | |
Johnny Chen | 6853cf6 | 2011-07-21 19:31:59 +0000 | [diff] [blame^] | 67 | # SBValue::TypeIsPointerType() should return true. |
| 68 | self.assertTrue(value.TypeIsPointerType()) |
| 69 | |
| 70 | # Verify the SBValue::GetByteSize() API is working correctly. |
| 71 | arch = self.getArchitecture() |
| 72 | if arch == 'i386': |
| 73 | self.assertTrue(value.GetByteSize() == 4) |
| 74 | elif arch == 'x86_64': |
| 75 | self.assertTrue(value.GetByteSize() == 8) |
| 76 | |
Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 77 | # Get child at index 5 => 'Friday'. |
| 78 | child = value.GetChildAtIndex(5, lldb.eNoDynamicValues, True) |
| 79 | self.assertTrue(child, VALID_VARIABLE) |
| 80 | self.DebugSBValue(child) |
| 81 | |
| 82 | self.expect(child.GetSummary(), exe=False, |
| 83 | substrs = ['Friday']) |
| 84 | |
| 85 | # Now try to get at the same variable using GetValueForExpressionPath(). |
| 86 | # These two SBValue objects should have the same value. |
| 87 | val2 = value.GetValueForExpressionPath('[5]') |
| 88 | self.assertTrue(val2, VALID_VARIABLE) |
| 89 | self.DebugSBValue(val2) |
| 90 | self.assertTrue(child.GetValue() == val2.GetValue() and |
| 91 | child.GetSummary() == val2.GetSummary()) |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | import atexit |
| 95 | lldb.SBDebugger.Initialize() |
| 96 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 97 | unittest2.main() |