| Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test some SBValue APIs. |
| 3 | """ |
| 4 | |
| Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 5 | from __future__ import print_function |
| 6 | |
| Zachary Turner | 0a0490b | 2015-10-27 20:12:05 +0000 | [diff] [blame] | 7 | import use_lldb_suite |
| Zachary Turner | 77db4a8 | 2015-10-22 20:06:20 +0000 | [diff] [blame] | 8 | |
| Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 9 | import os, time |
| 10 | import re |
| Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 11 | import lldb, lldbutil |
| 12 | from lldbtest import * |
| 13 | |
| 14 | class ValueAPITestCase(TestBase): |
| 15 | |
| Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 16 | mydir = TestBase.compute_mydir(__file__) |
| Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 17 | |
| Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 18 | def setUp(self): |
| 19 | # Call super's setUp(). |
| 20 | TestBase.setUp(self) |
| 21 | # We'll use the test method name as the exe_name. |
| 22 | self.exe_name = self.testMethodName |
| 23 | # Find the line number to of function 'c'. |
| 24 | self.line = line_number('main.c', '// Break at this line') |
| 25 | |
| Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 26 | @expectedFailureWindows("llvm.org/pr24772") |
| Pavel Labath | dc8b2d3 | 2015-10-26 09:28:32 +0000 | [diff] [blame] | 27 | @add_test_categories(['pyapi']) |
| Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 28 | def test(self): |
| Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 29 | """Exercise some SBValue APIs.""" |
| Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 30 | d = {'EXE': self.exe_name} |
| 31 | self.build(dictionary=d) |
| 32 | self.setTearDownCleanup(dictionary=d) |
| 33 | exe = os.path.join(os.getcwd(), self.exe_name) |
| Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 34 | |
| 35 | # Create a target by the debugger. |
| 36 | target = self.dbg.CreateTarget(exe) |
| 37 | self.assertTrue(target, VALID_TARGET) |
| 38 | |
| 39 | # Create the breakpoint inside function 'main'. |
| 40 | breakpoint = target.BreakpointCreateByLocation('main.c', self.line) |
| 41 | self.assertTrue(breakpoint, VALID_BREAKPOINT) |
| 42 | |
| 43 | # Now launch the process, and do not stop at entry point. |
| Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 44 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
| Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 45 | self.assertTrue(process, PROCESS_IS_VALID) |
| 46 | |
| 47 | # Get Frame #0. |
| 48 | self.assertTrue(process.GetState() == lldb.eStateStopped) |
| 49 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
| Greg Clayton | 53c5ddf | 2013-03-19 17:59:30 +0000 | [diff] [blame] | 50 | self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition") |
| Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 51 | frame0 = thread.GetFrameAtIndex(0) |
| 52 | |
| Johnny Chen | 6cbb8d6 | 2011-07-21 23:02:00 +0000 | [diff] [blame] | 53 | # Get global variable 'days_of_week'. |
| 54 | list = target.FindGlobalVariables('days_of_week', 1) |
| 55 | days_of_week = list.GetValueAtIndex(0) |
| 56 | self.assertTrue(days_of_week, VALID_VARIABLE) |
| 57 | self.assertTrue(days_of_week.GetNumChildren() == 7, VALID_VARIABLE) |
| 58 | self.DebugSBValue(days_of_week) |
| 59 | |
| Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 60 | # Get global variable 'weekdays'. |
| 61 | list = target.FindGlobalVariables('weekdays', 1) |
| 62 | weekdays = list.GetValueAtIndex(0) |
| 63 | self.assertTrue(weekdays, VALID_VARIABLE) |
| 64 | self.assertTrue(weekdays.GetNumChildren() == 5, VALID_VARIABLE) |
| 65 | self.DebugSBValue(weekdays) |
| 66 | |
| 67 | # Get global variable 'g_table'. |
| 68 | list = target.FindGlobalVariables('g_table', 1) |
| 69 | g_table = list.GetValueAtIndex(0) |
| 70 | self.assertTrue(g_table, VALID_VARIABLE) |
| 71 | self.assertTrue(g_table.GetNumChildren() == 2, VALID_VARIABLE) |
| 72 | self.DebugSBValue(g_table) |
| 73 | |
| Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 74 | fmt = lldbutil.BasicFormatter() |
| Johnny Chen | 36d7d91 | 2011-07-22 22:01:35 +0000 | [diff] [blame] | 75 | cvf = lldbutil.ChildVisitingFormatter(indent_child=2) |
| 76 | rdf = lldbutil.RecursiveDecentFormatter(indent_child=2) |
| Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 77 | if self.TraceOn(): |
| Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 78 | print(fmt.format(days_of_week)) |
| 79 | print(cvf.format(days_of_week)) |
| 80 | print(cvf.format(weekdays)) |
| 81 | print(rdf.format(g_table)) |
| Johnny Chen | 989b7ef | 2011-07-22 00:47:58 +0000 | [diff] [blame] | 82 | |
| Johnny Chen | 2341380 | 2011-11-15 21:13:13 +0000 | [diff] [blame] | 83 | # Get variable 'my_int_ptr'. |
| 84 | value = frame0.FindVariable('my_int_ptr') |
| 85 | self.assertTrue(value, VALID_VARIABLE) |
| 86 | self.DebugSBValue(value) |
| 87 | |
| 88 | # Get what 'my_int_ptr' points to. |
| 89 | pointed = value.GetChildAtIndex(0) |
| 90 | self.assertTrue(pointed, VALID_VARIABLE) |
| 91 | self.DebugSBValue(pointed) |
| 92 | |
| Johnny Chen | 4c1b096 | 2011-11-15 23:30:39 +0000 | [diff] [blame] | 93 | # While we are at it, verify that 'my_int_ptr' points to 'g_my_int'. |
| 94 | symbol = target.ResolveLoadAddress(int(pointed.GetLocation(), 0)).GetSymbol() |
| 95 | self.assertTrue(symbol) |
| 96 | self.expect(symbol.GetName(), exe=False, |
| 97 | startstr = 'g_my_int') |
| 98 | |
| Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 99 | # Get variable 'str_ptr'. |
| 100 | value = frame0.FindVariable('str_ptr') |
| 101 | self.assertTrue(value, VALID_VARIABLE) |
| 102 | self.DebugSBValue(value) |
| 103 | |
| Johnny Chen | 6853cf6 | 2011-07-21 19:31:59 +0000 | [diff] [blame] | 104 | # SBValue::TypeIsPointerType() should return true. |
| 105 | self.assertTrue(value.TypeIsPointerType()) |
| 106 | |
| 107 | # Verify the SBValue::GetByteSize() API is working correctly. |
| 108 | arch = self.getArchitecture() |
| 109 | if arch == 'i386': |
| 110 | self.assertTrue(value.GetByteSize() == 4) |
| 111 | elif arch == 'x86_64': |
| 112 | self.assertTrue(value.GetByteSize() == 8) |
| 113 | |
| Johnny Chen | 5819ab4 | 2011-07-15 22:28:10 +0000 | [diff] [blame] | 114 | # Get child at index 5 => 'Friday'. |
| 115 | child = value.GetChildAtIndex(5, lldb.eNoDynamicValues, True) |
| 116 | self.assertTrue(child, VALID_VARIABLE) |
| 117 | self.DebugSBValue(child) |
| 118 | |
| 119 | self.expect(child.GetSummary(), exe=False, |
| 120 | substrs = ['Friday']) |
| 121 | |
| 122 | # Now try to get at the same variable using GetValueForExpressionPath(). |
| 123 | # These two SBValue objects should have the same value. |
| 124 | val2 = value.GetValueForExpressionPath('[5]') |
| 125 | self.assertTrue(val2, VALID_VARIABLE) |
| 126 | self.DebugSBValue(val2) |
| 127 | self.assertTrue(child.GetValue() == val2.GetValue() and |
| 128 | child.GetSummary() == val2.GetSummary()) |
| Siva Chandra | f8877ef | 2015-07-16 01:47:12 +0000 | [diff] [blame] | 129 | |
| 130 | val_i = target.EvaluateExpression('i') |
| 131 | val_s = target.EvaluateExpression('s') |
| 132 | val_a = target.EvaluateExpression('a') |
| 133 | self.assertTrue(val_s.GetChildMemberWithName('a').AddressOf(), VALID_VARIABLE) |
| 134 | self.assertTrue(val_a.Cast(val_i.GetType()).AddressOf(), VALID_VARIABLE) |