9cbc396e7a59309375d6cfbd823af8d0d9c75517
[openbsd] /
1 """
2 Use lldb Python SBValue.WatchPointee() API to create a watchpoint for write of '*g_char_ptr'.
3 """
4
5 from __future__ import print_function
6
7
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 SetWatchlocationAPITestCase(TestBase):
16
17     mydir = TestBase.compute_mydir(__file__)
18     NO_DEBUG_INFO_TESTCASE = True
19
20     def setUp(self):
21         # Call super's setUp().
22         TestBase.setUp(self)
23         # Our simple source filename.
24         self.source = 'main.cpp'
25         # Find the line number to break inside main().
26         self.line = line_number(
27             self.source, '// Set break point at this line.')
28         # This is for verifying that watch location works.
29         self.violating_func = "do_bad_thing_with_location"
30
31     @add_test_categories(['pyapi'])
32     def test_watch_location(self):
33         """Exercise SBValue.WatchPointee() API to set a watchpoint."""
34         self.build()
35         exe = self.getBuildArtifact("a.out")
36
37         # Create a target by the debugger.
38         target = self.dbg.CreateTarget(exe)
39         self.assertTrue(target, VALID_TARGET)
40
41         # Now create a breakpoint on main.c.
42         breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
43         self.assertTrue(breakpoint and
44                         breakpoint.GetNumLocations() == 1,
45                         VALID_BREAKPOINT)
46
47         # Now launch the process, and do not stop at the entry point.
48         process = target.LaunchSimple(
49             None, None, self.get_process_working_directory())
50
51         # We should be stopped due to the breakpoint.  Get frame #0.
52         process = target.GetProcess()
53         self.assertTrue(process.GetState() == lldb.eStateStopped,
54                         PROCESS_STOPPED)
55         thread = lldbutil.get_stopped_thread(
56             process, lldb.eStopReasonBreakpoint)
57         frame0 = thread.GetFrameAtIndex(0)
58
59         value = frame0.FindValue('g_char_ptr',
60                                  lldb.eValueTypeVariableGlobal)
61         pointee = value.CreateValueFromAddress(
62             "pointee",
63             value.GetValueAsUnsigned(0),
64             value.GetType().GetPointeeType())
65         # Watch for write to *g_char_ptr.
66         error = lldb.SBError()
67         watchpoint = value.WatchPointee(True, 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)
72
73         # Hide stdout if not running with '-t' option.
74         if not self.TraceOn():
75             self.HideStdout()
76
77         print(watchpoint)
78
79         # Continue.  Expect the program to stop due to the variable being
80         # written to.
81         process.Continue()
82
83         if (self.TraceOn()):
84             lldbutil.print_stacktraces(process)
85
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)
91
92         self.expect(
93             lldbutil.print_stacktrace(
94                 thread,
95                 string_buffer=True),
96             exe=False,
97             substrs=[
98                 self.violating_func])
99
100         # This finishes our test.