Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 1 | """ |
| 2 | Use lldb Python SBTarget API to iterate on the watchpoint(s) for the target. |
| 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import re |
| 7 | import unittest2 |
| 8 | import lldb, lldbutil |
| 9 | from lldbtest import * |
| 10 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 11 | class WatchpointIteratorTestCase(TestBase): |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 12 | |
| 13 | mydir = os.path.join("python_api", "watchpoint") |
| 14 | |
| 15 | def setUp(self): |
| 16 | # Call super's setUp(). |
| 17 | TestBase.setUp(self) |
| 18 | # Our simple source filename. |
| 19 | self.source = 'main.c' |
| 20 | # Find the line number to break inside main(). |
| 21 | self.line = line_number(self.source, '// Set break point at this line.') |
| 22 | |
| 23 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 24 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 25 | @dsym_test |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 26 | def test_watch_iter_with_dsym(self): |
| 27 | """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints.""" |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 28 | self.buildDsym() |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 29 | self.do_watchpoint_iter() |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 30 | |
Daniel Malea | 93aec0f | 2012-11-23 21:59:29 +0000 | [diff] [blame] | 31 | @expectedFailureLinux # bugzilla 14416 |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 32 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 33 | @dwarf_test |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 34 | def test_watch_iter_with_dwarf(self): |
| 35 | """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints.""" |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 36 | self.buildDwarf() |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 37 | self.do_watchpoint_iter() |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 38 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 39 | def do_watchpoint_iter(self): |
| 40 | """Use SBTarget.watchpoint_iter() to do Pythonic iteration on the available watchpoints.""" |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 41 | exe = os.path.join(os.getcwd(), "a.out") |
| 42 | |
| 43 | # Create a target by the debugger. |
| 44 | target = self.dbg.CreateTarget(exe) |
| 45 | self.assertTrue(target, VALID_TARGET) |
| 46 | |
| 47 | # Create a breakpoint on main.c in order to set our watchpoint later. |
| 48 | breakpoint = target.BreakpointCreateByLocation(self.source, self.line) |
| 49 | self.assertTrue(breakpoint and |
| 50 | breakpoint.GetNumLocations() == 1, |
| 51 | VALID_BREAKPOINT) |
| 52 | |
| 53 | # Now launch the process, and do not stop at the entry point. |
| 54 | process = target.LaunchSimple(None, None, os.getcwd()) |
| 55 | |
| 56 | # We should be stopped due to the breakpoint. Get frame #0. |
| 57 | process = target.GetProcess() |
| 58 | self.assertTrue(process.GetState() == lldb.eStateStopped, |
| 59 | PROCESS_STOPPED) |
| 60 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
| 61 | frame0 = thread.GetFrameAtIndex(0) |
| 62 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 63 | # Watch 'global' for read and write. |
| 64 | value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal) |
Johnny Chen | b90827e | 2012-06-04 23:19:54 +0000 | [diff] [blame] | 65 | error = lldb.SBError(); |
| 66 | watchpoint = value.Watch(True, True, True, error) |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 67 | self.assertTrue(value and watchpoint, |
| 68 | "Successfully found the variable and set a watchpoint") |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 69 | self.DebugSBValue(value) |
| 70 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 71 | # Hide stdout if not running with '-t' option. |
| 72 | if not self.TraceOn(): |
| 73 | self.HideStdout() |
| 74 | |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 75 | # There should be only 1 watchpoint location under the target. |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 76 | self.assertTrue(target.GetNumWatchpoints() == 1) |
| 77 | self.assertTrue(watchpoint.IsEnabled()) |
| 78 | watch_id = watchpoint.GetID() |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 79 | self.assertTrue(watch_id != 0) |
| 80 | |
| 81 | # Continue. Expect the program to stop due to the variable being written to. |
| 82 | process.Continue() |
| 83 | |
| 84 | # Hide stdout if not running with '-t' option. |
| 85 | if not self.TraceOn(): |
| 86 | self.HideStdout() |
| 87 | |
| 88 | # Print the stack traces. |
| 89 | lldbutil.print_stacktraces(process) |
| 90 | |
| 91 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonWatchpoint) |
| 92 | self.assertTrue(thread, "The thread stopped due to watchpoint") |
| 93 | self.DebugSBValue(value) |
| 94 | |
| 95 | # We currently only support hardware watchpoint. Verify that we have a |
| 96 | # meaningful hardware index at this point. Exercise the printed repr of |
| 97 | # SBWatchpointLocation. |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 98 | print watchpoint |
| 99 | self.assertTrue(watchpoint.GetHardwareIndex() != -1) |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 100 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 101 | # SBWatchpoint.GetDescription() takes a description level arg. |
| 102 | print lldbutil.get_description(watchpoint, lldb.eDescriptionLevelFull) |
Johnny Chen | 469683e | 2011-09-27 21:27:19 +0000 | [diff] [blame] | 103 | |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 104 | # Now disable the 'rw' watchpoint. The program won't stop when it reads |
| 105 | # 'global' next. |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 106 | watchpoint.SetEnabled(False) |
| 107 | self.assertTrue(watchpoint.GetHardwareIndex() == -1) |
| 108 | self.assertFalse(watchpoint.IsEnabled()) |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 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. |
| 115 | self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED) |
| 116 | |
| 117 | # Verify some vital statistics and exercise the iterator API. |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 118 | for watchpoint in target.watchpoint_iter(): |
| 119 | self.assertTrue(watchpoint) |
| 120 | self.assertTrue(watchpoint.GetWatchSize() == 4) |
| 121 | self.assertTrue(watchpoint.GetHitCount() == 1) |
| 122 | print watchpoint |
Johnny Chen | d4dd799 | 2011-09-27 01:19:20 +0000 | [diff] [blame] | 123 | |
| 124 | |
| 125 | if __name__ == '__main__': |
| 126 | import atexit |
| 127 | lldb.SBDebugger.Initialize() |
| 128 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 129 | unittest2.main() |