blob: c6d37502ec3a014115824dffac847932e46717af [file] [log] [blame]
Johnny Chend4dd7992011-09-27 01:19:20 +00001"""
2Use lldb Python SBTarget API to iterate on the watchpoint(s) for the target.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb, lldbutil
9from lldbtest import *
10
Johnny Chen01a67862011-10-14 00:42:25 +000011class WatchpointIteratorTestCase(TestBase):
Johnny Chend4dd7992011-09-27 01:19:20 +000012
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 Chen24086bc2012-04-06 19:54:10 +000025 @dsym_test
Johnny Chen01a67862011-10-14 00:42:25 +000026 def test_watch_iter_with_dsym(self):
27 """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints."""
Johnny Chend4dd7992011-09-27 01:19:20 +000028 self.buildDsym()
Johnny Chen01a67862011-10-14 00:42:25 +000029 self.do_watchpoint_iter()
Johnny Chend4dd7992011-09-27 01:19:20 +000030
31 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000032 @dwarf_test
Johnny Chen01a67862011-10-14 00:42:25 +000033 def test_watch_iter_with_dwarf(self):
34 """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints."""
Johnny Chend4dd7992011-09-27 01:19:20 +000035 self.buildDwarf()
Johnny Chen01a67862011-10-14 00:42:25 +000036 self.do_watchpoint_iter()
Johnny Chend4dd7992011-09-27 01:19:20 +000037
Johnny Chen01a67862011-10-14 00:42:25 +000038 def do_watchpoint_iter(self):
39 """Use SBTarget.watchpoint_iter() to do Pythonic iteration on the available watchpoints."""
Johnny Chend4dd7992011-09-27 01:19:20 +000040 exe = os.path.join(os.getcwd(), "a.out")
41
42 # Create a target by the debugger.
43 target = self.dbg.CreateTarget(exe)
44 self.assertTrue(target, VALID_TARGET)
45
46 # Create a breakpoint on main.c in order to set our watchpoint later.
47 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
48 self.assertTrue(breakpoint and
49 breakpoint.GetNumLocations() == 1,
50 VALID_BREAKPOINT)
51
52 # Now launch the process, and do not stop at the entry point.
53 process = target.LaunchSimple(None, None, os.getcwd())
54
55 # We should be stopped due to the breakpoint. Get frame #0.
56 process = target.GetProcess()
57 self.assertTrue(process.GetState() == lldb.eStateStopped,
58 PROCESS_STOPPED)
59 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
60 frame0 = thread.GetFrameAtIndex(0)
61
Johnny Chen01a67862011-10-14 00:42:25 +000062 # Watch 'global' for read and write.
63 value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal)
Johnny Chenb90827e2012-06-04 23:19:54 +000064 error = lldb.SBError();
65 watchpoint = value.Watch(True, True, True, error)
Johnny Chen01a67862011-10-14 00:42:25 +000066 self.assertTrue(value and watchpoint,
67 "Successfully found the variable and set a watchpoint")
Johnny Chend4dd7992011-09-27 01:19:20 +000068 self.DebugSBValue(value)
69
Johnny Chen01a67862011-10-14 00:42:25 +000070 # Hide stdout if not running with '-t' option.
71 if not self.TraceOn():
72 self.HideStdout()
73
Johnny Chend4dd7992011-09-27 01:19:20 +000074 # There should be only 1 watchpoint location under the target.
Johnny Chen01a67862011-10-14 00:42:25 +000075 self.assertTrue(target.GetNumWatchpoints() == 1)
76 self.assertTrue(watchpoint.IsEnabled())
77 watch_id = watchpoint.GetID()
Johnny Chend4dd7992011-09-27 01:19:20 +000078 self.assertTrue(watch_id != 0)
79
80 # Continue. Expect the program to stop due to the variable being written to.
81 process.Continue()
82
83 # Hide stdout if not running with '-t' option.
84 if not self.TraceOn():
85 self.HideStdout()
86
87 # Print the stack traces.
88 lldbutil.print_stacktraces(process)
89
90 thread = lldbutil.get_stopped_thread(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.
Johnny Chen01a67862011-10-14 00:42:25 +000097 print watchpoint
98 self.assertTrue(watchpoint.GetHardwareIndex() != -1)
Johnny Chend4dd7992011-09-27 01:19:20 +000099
Johnny Chen01a67862011-10-14 00:42:25 +0000100 # SBWatchpoint.GetDescription() takes a description level arg.
101 print lldbutil.get_description(watchpoint, lldb.eDescriptionLevelFull)
Johnny Chen469683e2011-09-27 21:27:19 +0000102
Johnny Chend4dd7992011-09-27 01:19:20 +0000103 # Now disable the 'rw' watchpoint. The program won't stop when it reads
104 # 'global' next.
Johnny Chen01a67862011-10-14 00:42:25 +0000105 watchpoint.SetEnabled(False)
106 self.assertTrue(watchpoint.GetHardwareIndex() == -1)
107 self.assertFalse(watchpoint.IsEnabled())
Johnny Chend4dd7992011-09-27 01:19:20 +0000108
109 # Continue. The program does not stop again when the variable is being
110 # read from because the watchpoint location has been disabled.
111 process.Continue()
112
113 # At this point, the inferior process should have exited.
114 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
115
116 # Verify some vital statistics and exercise the iterator API.
Johnny Chen01a67862011-10-14 00:42:25 +0000117 for watchpoint in target.watchpoint_iter():
118 self.assertTrue(watchpoint)
119 self.assertTrue(watchpoint.GetWatchSize() == 4)
120 self.assertTrue(watchpoint.GetHitCount() == 1)
121 print watchpoint
Johnny Chend4dd7992011-09-27 01:19:20 +0000122
123
124if __name__ == '__main__':
125 import atexit
126 lldb.SBDebugger.Initialize()
127 atexit.register(lambda: lldb.SBDebugger.Terminate())
128 unittest2.main()