Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 1 | """ |
| 2 | Use lldb Python SBTarget API to iterate on the watchpoint(s) for the target. |
| 3 | """ |
| 4 | |
| 5 | from __future__ import print_function |
| 6 | |
| 7 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 8 | |
| 9 | import lldb |
| 10 | from lldbsuite.test.decorators import * |
| 11 | from lldbsuite.test.lldbtest import * |
| 12 | from lldbsuite.test import lldbutil |
| 13 | |
| 14 | |
| 15 | class WatchpointIteratorTestCase(TestBase): |
| 16 | |
| 17 | mydir = TestBase.compute_mydir(__file__) |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 18 | NO_DEBUG_INFO_TESTCASE = True |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 19 | |
| 20 | # hardware watchpoints are not reported with a hardware index # on armv7 on ios devices |
| 21 | def affected_by_radar_34564183(self): |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 22 | return (self.getArchitecture() in ['armv7', 'armv7k', 'arm64_32']) and self.platformIsDarwin() |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 23 | |
| 24 | def setUp(self): |
| 25 | # Call super's setUp(). |
| 26 | TestBase.setUp(self) |
| 27 | # Our simple source filename. |
| 28 | self.source = 'main.c' |
| 29 | # Find the line number to break inside main(). |
| 30 | self.line = line_number( |
| 31 | self.source, '// Set break point at this line.') |
| 32 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 33 | def test_watch_iter(self): |
| 34 | """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints.""" |
| 35 | self.build() |
| 36 | exe = self.getBuildArtifact("a.out") |
| 37 | |
| 38 | # Create a target by the debugger. |
| 39 | target = self.dbg.CreateTarget(exe) |
| 40 | self.assertTrue(target, VALID_TARGET) |
| 41 | |
| 42 | # Create a breakpoint on main.c in order to set our watchpoint later. |
| 43 | breakpoint = target.BreakpointCreateByLocation(self.source, self.line) |
| 44 | self.assertTrue(breakpoint and |
| 45 | breakpoint.GetNumLocations() == 1, |
| 46 | VALID_BREAKPOINT) |
| 47 | |
| 48 | # Now launch the process, and do not stop at the entry point. |
| 49 | process = target.LaunchSimple( |
| 50 | None, None, self.get_process_working_directory()) |
| 51 | |
| 52 | # We should be stopped due to the breakpoint. Get frame #0. |
| 53 | process = target.GetProcess() |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 54 | self.assertEqual(process.GetState(), lldb.eStateStopped, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 55 | PROCESS_STOPPED) |
| 56 | thread = lldbutil.get_stopped_thread( |
| 57 | process, lldb.eStopReasonBreakpoint) |
| 58 | frame0 = thread.GetFrameAtIndex(0) |
| 59 | |
| 60 | # Watch 'global' for read and write. |
| 61 | value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal) |
| 62 | error = lldb.SBError() |
| 63 | watchpoint = value.Watch(True, False, True, error) |
| 64 | self.assertTrue(value and watchpoint, |
| 65 | "Successfully found the variable and set a watchpoint") |
| 66 | self.DebugSBValue(value) |
| 67 | |
| 68 | # Hide stdout if not running with '-t' option. |
| 69 | if not self.TraceOn(): |
| 70 | self.HideStdout() |
| 71 | |
| 72 | # There should be only 1 watchpoint location under the target. |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 73 | self.assertEqual(target.GetNumWatchpoints(), 1) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 74 | self.assertTrue(watchpoint.IsEnabled()) |
| 75 | watch_id = watchpoint.GetID() |
| 76 | self.assertTrue(watch_id != 0) |
| 77 | |
| 78 | # Continue. Expect the program to stop due to the variable being |
| 79 | # written to. |
| 80 | process.Continue() |
| 81 | |
| 82 | # Hide stdout if not running with '-t' option. |
| 83 | if not self.TraceOn(): |
| 84 | self.HideStdout() |
| 85 | |
| 86 | # Print the stack traces. |
| 87 | lldbutil.print_stacktraces(process) |
| 88 | |
| 89 | thread = lldbutil.get_stopped_thread( |
| 90 | process, lldb.eStopReasonWatchpoint) |
| 91 | self.assertTrue(thread, "The thread stopped due to watchpoint") |
| 92 | self.DebugSBValue(value) |
| 93 | |
| 94 | # We currently only support hardware watchpoint. Verify that we have a |
| 95 | # meaningful hardware index at this point. Exercise the printed repr of |
| 96 | # SBWatchpointLocation. |
| 97 | print(watchpoint) |
| 98 | if not self.affected_by_radar_34564183(): |
| 99 | self.assertTrue(watchpoint.GetHardwareIndex() != -1) |
| 100 | |
| 101 | # SBWatchpoint.GetDescription() takes a description level arg. |
| 102 | print(lldbutil.get_description(watchpoint, lldb.eDescriptionLevelFull)) |
| 103 | |
| 104 | # Now disable the 'rw' watchpoint. The program won't stop when it reads |
| 105 | # 'global' next. |
| 106 | watchpoint.SetEnabled(False) |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 107 | self.assertEqual(watchpoint.GetHardwareIndex(), -1) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 108 | self.assertFalse(watchpoint.IsEnabled()) |
| 109 | |
| 110 | # Continue. The program does not stop again when the variable is being |
| 111 | # read from because the watchpoint location has been disabled. |
| 112 | process.Continue() |
| 113 | |
| 114 | # At this point, the inferior process should have exited. |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 115 | self.assertEqual( |
| 116 | process.GetState(), lldb.eStateExited, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 117 | PROCESS_EXITED) |
| 118 | |
| 119 | # Verify some vital statistics and exercise the iterator API. |
| 120 | for watchpoint in target.watchpoint_iter(): |
| 121 | self.assertTrue(watchpoint) |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 122 | self.assertEqual(watchpoint.GetWatchSize(), 4) |
| 123 | self.assertEqual(watchpoint.GetHitCount(), 1) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 124 | print(watchpoint) |