blob: 407e7919d0d1786be5167d7e95758ee2a4133772 [file] [log] [blame]
Johnny Chenf5be9e32011-10-14 01:16:39 +00001"""
2Use lldb Python SBtarget.WatchAddress() API to create a watchpoint for write of '*g_char_ptr'.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb, lldbutil
9from lldbtest import *
10
11class TargetWatchAddressAPITestCase(TestBase):
12
13 mydir = os.path.join("python_api", "watchpoint", "watchlocation")
14
15 def setUp(self):
16 # Call super's setUp().
17 TestBase.setUp(self)
18 # Our simple source filename.
19 self.source = 'main.cpp'
20 # Find the line number to break inside main().
21 self.line = line_number(self.source, '// Set break point at this line.')
22 # This is for verifying that watch location works.
23 self.violating_func = "do_bad_thing_with_location";
24
25 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
26 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000027 @dsym_test
Johnny Chenf5be9e32011-10-14 01:16:39 +000028 def test_watch_address_with_dsym(self):
29 """Exercise SBTarget.WatchAddress() API to set a watchpoint."""
30 self.buildDsym()
31 self.do_set_watchaddress()
32
33 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000034 @dwarf_test
Johnny Chen4b730a72011-12-21 19:56:51 +000035 def test_watch_address_with_dwarf(self):
Johnny Chenf5be9e32011-10-14 01:16:39 +000036 """Exercise SBTarget.WatchAddress() API to set a watchpoint."""
37 self.buildDwarf()
38 self.do_set_watchaddress()
39
Johnny Chenb90827e2012-06-04 23:19:54 +000040 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
41 @python_api_test
42 @dsym_test
43 def test_watch_address_with_invalid_watch_size_with_dsym(self):
44 """Exercise SBTarget.WatchAddress() API but pass an invalid watch_size."""
45 self.buildDsym()
46 self.do_set_watchaddress_with_invalid_watch_size()
47
48 @python_api_test
49 @dwarf_test
50 def test_watch_address_with_invalid_watch_size_with_dwarf(self):
51 """Exercise SBTarget.WatchAddress() API but pass an invalid watch_size."""
52 self.buildDwarf()
53 self.do_set_watchaddress_with_invalid_watch_size()
54
Johnny Chenf5be9e32011-10-14 01:16:39 +000055 def do_set_watchaddress(self):
56 """Use SBTarget.WatchAddress() to set a watchpoint and verify that the program stops later due to the watchpoint."""
57 exe = os.path.join(os.getcwd(), "a.out")
58
59 # Create a target by the debugger.
60 target = self.dbg.CreateTarget(exe)
61 self.assertTrue(target, VALID_TARGET)
62
63 # Now create a breakpoint on main.c.
64 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
65 self.assertTrue(breakpoint and
66 breakpoint.GetNumLocations() == 1,
67 VALID_BREAKPOINT)
68
69 # Now launch the process, and do not stop at the entry point.
70 process = target.LaunchSimple(None, None, os.getcwd())
71
72 # We should be stopped due to the breakpoint. Get frame #0.
73 process = target.GetProcess()
74 self.assertTrue(process.GetState() == lldb.eStateStopped,
75 PROCESS_STOPPED)
76 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
77 frame0 = thread.GetFrameAtIndex(0)
78
79 value = frame0.FindValue('g_char_ptr',
80 lldb.eValueTypeVariableGlobal)
81 pointee = value.CreateValueFromAddress("pointee",
82 value.GetValueAsUnsigned(0),
83 value.GetType().GetPointeeType())
84 # Watch for write to *g_char_ptr.
Johnny Chenb90827e2012-06-04 23:19:54 +000085 error = lldb.SBError();
86 watchpoint = target.WatchAddress(value.GetValueAsUnsigned(), 1, False, True, error)
Johnny Chenf5be9e32011-10-14 01:16:39 +000087 self.assertTrue(value and watchpoint,
88 "Successfully found the pointer and set a watchpoint")
89 self.DebugSBValue(value)
90 self.DebugSBValue(pointee)
91
92 # Hide stdout if not running with '-t' option.
93 if not self.TraceOn():
94 self.HideStdout()
95
96 print watchpoint
97
98 # Continue. Expect the program to stop due to the variable being written to.
99 process.Continue()
100
101 if (self.TraceOn()):
102 lldbutil.print_stacktraces(process)
103
104 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonWatchpoint)
105 self.assertTrue(thread, "The thread stopped due to watchpoint")
106 self.DebugSBValue(value)
107 self.DebugSBValue(pointee)
108
109 self.expect(lldbutil.print_stacktrace(thread, string_buffer=True), exe=False,
110 substrs = [self.violating_func])
111
112 # This finishes our test.
113
Johnny Chenb90827e2012-06-04 23:19:54 +0000114 def do_set_watchaddress_with_invalid_watch_size(self):
115 """Use SBTarget.WatchAddress() to set a watchpoint with invalid watch_size and verify we get a meaningful error message."""
116 exe = os.path.join(os.getcwd(), "a.out")
117
118 # Create a target by the debugger.
119 target = self.dbg.CreateTarget(exe)
120 self.assertTrue(target, VALID_TARGET)
121
122 # Now create a breakpoint on main.c.
123 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
124 self.assertTrue(breakpoint and
125 breakpoint.GetNumLocations() == 1,
126 VALID_BREAKPOINT)
127
128 # Now launch the process, and do not stop at the entry point.
129 process = target.LaunchSimple(None, None, os.getcwd())
130
131 # We should be stopped due to the breakpoint. Get frame #0.
132 process = target.GetProcess()
133 self.assertTrue(process.GetState() == lldb.eStateStopped,
134 PROCESS_STOPPED)
135 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
136 frame0 = thread.GetFrameAtIndex(0)
137
138 value = frame0.FindValue('g_char_ptr',
139 lldb.eValueTypeVariableGlobal)
140 pointee = value.CreateValueFromAddress("pointee",
141 value.GetValueAsUnsigned(0),
142 value.GetType().GetPointeeType())
143 # Watch for write to *g_char_ptr.
144 error = lldb.SBError();
145 watchpoint = target.WatchAddress(value.GetValueAsUnsigned(), 365, False, True, error)
146 self.assertFalse(watchpoint)
147 self.expect(error.GetCString(), exe=False,
148 substrs = ['watch size of %d is not supported' % 365])
149
Johnny Chenf5be9e32011-10-14 01:16:39 +0000150
151if __name__ == '__main__':
152 import atexit
153 lldb.SBDebugger.Initialize()
154 atexit.register(lambda: lldb.SBDebugger.Terminate())
155 unittest2.main()