2 Use lldb Python SBtarget.WatchAddress() API to create a watchpoint for write of '*g_char_ptr'.
5 from __future__ import print_function
9 from lldbsuite.test.decorators import *
10 from lldbsuite.test.lldbtest import *
11 from lldbsuite.test import lldbutil
14 class TargetWatchAddressAPITestCase(TestBase):
16 mydir = TestBase.compute_mydir(__file__)
17 NO_DEBUG_INFO_TESTCASE = True
20 # Call super's setUp().
22 # Our simple source filename.
23 self.source = 'main.cpp'
24 # Find the line number to break inside main().
25 self.line = line_number(
26 self.source, '// Set break point at this line.')
27 # This is for verifying that watch location works.
28 self.violating_func = "do_bad_thing_with_location"
30 @add_test_categories(['pyapi'])
31 def test_watch_address(self):
32 """Exercise SBTarget.WatchAddress() API to set a watchpoint."""
34 exe = self.getBuildArtifact("a.out")
36 # Create a target by the debugger.
37 target = self.dbg.CreateTarget(exe)
38 self.assertTrue(target, VALID_TARGET)
40 # Now create a breakpoint on main.c.
41 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
42 self.assertTrue(breakpoint and
43 breakpoint.GetNumLocations() == 1,
46 # Now launch the process, and do not stop at the entry point.
47 process = target.LaunchSimple(
48 None, None, self.get_process_working_directory())
50 # We should be stopped due to the breakpoint. Get frame #0.
51 process = target.GetProcess()
52 self.assertTrue(process.GetState() == lldb.eStateStopped,
54 thread = lldbutil.get_stopped_thread(
55 process, lldb.eStopReasonBreakpoint)
56 frame0 = thread.GetFrameAtIndex(0)
58 value = frame0.FindValue('g_char_ptr',
59 lldb.eValueTypeVariableGlobal)
60 pointee = value.CreateValueFromAddress(
62 value.GetValueAsUnsigned(0),
63 value.GetType().GetPointeeType())
64 # Watch for write to *g_char_ptr.
65 error = lldb.SBError()
66 watchpoint = target.WatchAddress(
67 value.GetValueAsUnsigned(), 1, False, True, error)
68 self.assertTrue(value and watchpoint,
69 "Successfully found the pointer and set a watchpoint")
70 self.DebugSBValue(value)
71 self.DebugSBValue(pointee)
73 # Hide stdout if not running with '-t' option.
74 if not self.TraceOn():
79 # Continue. Expect the program to stop due to the variable being
84 lldbutil.print_stacktraces(process)
86 thread = lldbutil.get_stopped_thread(
87 process, lldb.eStopReasonWatchpoint)
88 self.assertTrue(thread, "The thread stopped due to watchpoint")
89 self.DebugSBValue(value)
90 self.DebugSBValue(pointee)
93 lldbutil.print_stacktrace(
100 # This finishes our test.
102 @add_test_categories(['pyapi'])
103 # No size constraint on MIPS for watches
104 @skipIf(archs=['mips', 'mipsel', 'mips64', 'mips64el'])
105 @skipIf(archs=['s390x']) # Likewise on SystemZ
106 def test_watch_address_with_invalid_watch_size(self):
107 """Exercise SBTarget.WatchAddress() API but pass an invalid watch_size."""
109 exe = self.getBuildArtifact("a.out")
111 # Create a target by the debugger.
112 target = self.dbg.CreateTarget(exe)
113 self.assertTrue(target, VALID_TARGET)
115 # Now create a breakpoint on main.c.
116 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
117 self.assertTrue(breakpoint and
118 breakpoint.GetNumLocations() == 1,
121 # Now launch the process, and do not stop at the entry point.
122 process = target.LaunchSimple(
123 None, None, self.get_process_working_directory())
125 # We should be stopped due to the breakpoint. Get frame #0.
126 process = target.GetProcess()
127 self.assertTrue(process.GetState() == lldb.eStateStopped,
129 thread = lldbutil.get_stopped_thread(
130 process, lldb.eStopReasonBreakpoint)
131 frame0 = thread.GetFrameAtIndex(0)
133 value = frame0.FindValue('g_char_ptr',
134 lldb.eValueTypeVariableGlobal)
135 pointee = value.CreateValueFromAddress(
137 value.GetValueAsUnsigned(0),
138 value.GetType().GetPointeeType())
139 # Watch for write to *g_char_ptr.
140 error = lldb.SBError()
141 watchpoint = target.WatchAddress(
142 value.GetValueAsUnsigned(), 365, False, True, error)
143 self.assertFalse(watchpoint)
144 self.expect(error.GetCString(), exe=False,
145 substrs=['watch size of %d is not supported' % 365])