]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/python_api/watchpoint/TestWatchpointIter.py
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / python_api / watchpoint / TestWatchpointIter.py
1 """
2 Use lldb Python SBTarget API to iterate on the watchpoint(s) for the target.
3 """
4
5 from __future__ import print_function
6
7
8 import os
9 import re
10 import time
11
12 import lldb
13 from lldbsuite.test.decorators import *
14 from lldbsuite.test.lldbtest import *
15 from lldbsuite.test import lldbutil
16
17
18 class WatchpointIteratorTestCase(TestBase):
19
20     mydir = TestBase.compute_mydir(__file__)
21
22     def setUp(self):
23         # Call super's setUp().
24         TestBase.setUp(self)
25         # Our simple source filename.
26         self.source = 'main.c'
27         # Find the line number to break inside main().
28         self.line = line_number(
29             self.source, '// Set break point at this line.')
30
31     @add_test_categories(['pyapi'])
32     # Watchpoints not supported
33     @expectedFailureAndroid(archs=['arm', 'aarch64'])
34     @expectedFailureAll(
35         oslist=["windows"],
36         bugnumber="llvm.org/pr24446: WINDOWS XFAIL TRIAGE - Watchpoints not supported on Windows")
37     def test_watch_iter(self):
38         """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints."""
39         self.build()
40         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(
54             None, None, self.get_process_working_directory())
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(
61             process, lldb.eStopReasonBreakpoint)
62         frame0 = thread.GetFrameAtIndex(0)
63
64         # Watch 'global' for read and write.
65         value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal)
66         error = lldb.SBError()
67         watchpoint = value.Watch(True, False, True, error)
68         self.assertTrue(value and watchpoint,
69                         "Successfully found the variable and set a watchpoint")
70         self.DebugSBValue(value)
71
72         # Hide stdout if not running with '-t' option.
73         if not self.TraceOn():
74             self.HideStdout()
75
76         # There should be only 1 watchpoint location under the target.
77         self.assertTrue(target.GetNumWatchpoints() == 1)
78         self.assertTrue(watchpoint.IsEnabled())
79         watch_id = watchpoint.GetID()
80         self.assertTrue(watch_id != 0)
81
82         # Continue.  Expect the program to stop due to the variable being
83         # written to.
84         process.Continue()
85
86         # Hide stdout if not running with '-t' option.
87         if not self.TraceOn():
88             self.HideStdout()
89
90         # Print the stack traces.
91         lldbutil.print_stacktraces(process)
92
93         thread = lldbutil.get_stopped_thread(
94             process, lldb.eStopReasonWatchpoint)
95         self.assertTrue(thread, "The thread stopped due to watchpoint")
96         self.DebugSBValue(value)
97
98         # We currently only support hardware watchpoint.  Verify that we have a
99         # meaningful hardware index at this point.  Exercise the printed repr of
100         # SBWatchpointLocation.
101         print(watchpoint)
102         self.assertTrue(watchpoint.GetHardwareIndex() != -1)
103
104         # SBWatchpoint.GetDescription() takes a description level arg.
105         print(lldbutil.get_description(watchpoint, lldb.eDescriptionLevelFull))
106
107         # Now disable the 'rw' watchpoint.  The program won't stop when it reads
108         # 'global' next.
109         watchpoint.SetEnabled(False)
110         self.assertTrue(watchpoint.GetHardwareIndex() == -1)
111         self.assertFalse(watchpoint.IsEnabled())
112
113         # Continue.  The program does not stop again when the variable is being
114         # read from because the watchpoint location has been disabled.
115         process.Continue()
116
117         # At this point, the inferior process should have exited.
118         self.assertTrue(
119             process.GetState() == lldb.eStateExited,
120             PROCESS_EXITED)
121
122         # Verify some vital statistics and exercise the iterator API.
123         for watchpoint in target.watchpoint_iter():
124             self.assertTrue(watchpoint)
125             self.assertTrue(watchpoint.GetWatchSize() == 4)
126             self.assertTrue(watchpoint.GetHitCount() == 1)
127             print(watchpoint)