Debian Codegraph @

lldb/test/API/commands/frame/var/TestFrameVar.py

raw
1 """
2 Make sure the frame variable -g, -a, and -l flags work.
3 """
4
5
6 import lldb
7 import lldbsuite.test.lldbutil as lldbutil
8 from lldbsuite.test.decorators import *
9 from lldbsuite.test.lldbtest import *
10 import os
11 import shutil
12 import time
13
14
15 class TestFrameVar(TestBase):
16 # If your test case doesn't stress debug info, then
17 # set this to true. That way it won't be run once for
18 # each debug info format.
19 NO_DEBUG_INFO_TESTCASE = True
20
21 def test_frame_var(self):
22 self.build()
23 self.do_test()
24
25 def do_test(self):
26 target = self.createTestTarget()
27
28 # Now create a breakpoint in main.c at the source matching
29 # "Set a breakpoint here"
30 breakpoint = target.BreakpointCreateBySourceRegex(
31 "Set a breakpoint here", lldb.SBFileSpec("main.c")
32 )
33 self.assertTrue(
34 breakpoint and breakpoint.GetNumLocations() >= 1, VALID_BREAKPOINT
35 )
36
37 error = lldb.SBError()
38 # This is the launch info. If you want to launch with arguments or
39 # environment variables, add them using SetArguments or
40 # SetEnvironmentEntries
41
42 launch_info = target.GetLaunchInfo()
43 process = target.Launch(launch_info, error)
44 self.assertTrue(process, PROCESS_IS_VALID)
45
46 # Did we hit our breakpoint?
47 from lldbsuite.test.lldbutil import get_threads_stopped_at_breakpoint
48
49 threads = get_threads_stopped_at_breakpoint(process, breakpoint)
50 self.assertEqual(
51 len(threads), 1, "There should be a thread stopped at our breakpoint"
52 )
53
54 # The hit count for the breakpoint should be 1.
55 self.assertEqual(breakpoint.GetHitCount(), 1)
56
57 frame = threads[0].GetFrameAtIndex(0)
58 command_result = lldb.SBCommandReturnObject()
59 interp = self.dbg.GetCommandInterpreter()
60
61 # Ensure --regex can find globals if it is the very first frame var command.
62 self.expect("frame var --regex g_", substrs=["g_var"])
63
64 # Ensure the requested scope is respected:
65 self.expect(
66 "frame var --regex argc --no-args",
67 error=True,
68 substrs=["no variables matched the regular expression 'argc'"],
69 )
70
71 # Just get args:
72 result = interp.HandleCommand("frame var -l", command_result)
73 self.assertEqual(
74 result, lldb.eReturnStatusSuccessFinishResult, "frame var -a didn't succeed"
75 )
76 output = command_result.GetOutput()
77 self.assertIn("argc", output, "Args didn't find argc")
78 self.assertIn("argv", output, "Args didn't find argv")
79 self.assertNotIn("test_var", output, "Args found a local")
80 self.assertNotIn("g_var", output, "Args found a global")
81
82 # Just get locals:
83 result = interp.HandleCommand("frame var -a", command_result)
84 self.assertEqual(
85 result, lldb.eReturnStatusSuccessFinishResult, "frame var -a didn't succeed"
86 )
87 output = command_result.GetOutput()
88 self.assertNotIn("argc", output, "Locals found argc")
89 self.assertNotIn("argv", output, "Locals found argv")
90 self.assertIn("test_var", output, "Locals didn't find test_var")
91 self.assertNotIn("g_var", output, "Locals found a global")
92
93 # Get the file statics:
94 result = interp.HandleCommand("frame var -l -a -g", command_result)
95 self.assertEqual(
96 result, lldb.eReturnStatusSuccessFinishResult, "frame var -a didn't succeed"
97 )
98 output = command_result.GetOutput()
99 self.assertNotIn("argc", output, "Globals found argc")
100 self.assertNotIn("argv", output, "Globals found argv")
101 self.assertNotIn("test_var", output, "Globals found test_var")
102 self.assertIn("g_var", output, "Globals didn't find g_var")
103
104 def check_frame_variable_errors(self, thread, error_strings):
105 command_result = lldb.SBCommandReturnObject()
106 interp = self.dbg.GetCommandInterpreter()
107 result = interp.HandleCommand("frame variable", command_result)
108 self.assertEqual(
109 result, lldb.eReturnStatusFailed, "frame var succeeded unexpectedly"
110 )
111 command_error = command_result.GetError()
112
113 frame = thread.GetFrameAtIndex(0)
114 var_list = frame.GetVariables(True, True, False, True)
115 self.assertEqual(var_list.GetSize(), 0)
116 api_error = var_list.GetError().GetCString()
117
118 for s in error_strings:
119 self.assertIn(s, command_error)
120 for s in error_strings:
121 self.assertIn(s, api_error)
122
123 @skipIfRemote
124 @skipUnlessDarwin
125 def test_darwin_dwarf_missing_obj(self):
126 """
127 Test that if we build a binary with DWARF in .o files and we remove
128 the .o file for main.cpp, that we get an appropriate error when we
129 do 'frame variable' that explains why we aren't seeing variables.
130 """
131 self.build(debug_info="dwarf")
132 exe = self.getBuildArtifact("a.out")
133 main_obj = self.getBuildArtifact("main.o")
134 # Delete the main.o file that contains the debug info so we force an
135 # error when we run to main and try to get variables
136 os.unlink(main_obj)
137
138 # We have to set a named breakpoint because we don't have any debug info
139 # because we deleted the main.o file.
140 (target, process, thread, bkpt) = lldbutil.run_to_name_breakpoint(self, "main")
141 error_strings = [
142 'debug map object file "',
143 'main.o" containing debug info does not exist, debug info will not be loaded',
144 ]
145 self.check_frame_variable_errors(thread, error_strings)
146
147 @skipIfRemote
148 @skipUnlessDarwin
149 def test_darwin_dwarf_obj_mod_time_mismatch(self):
150 """
151 Test that if we build a binary with DWARF in .o files and we update
152 the mod time of the .o file for main.cpp, that we get an appropriate
153 error when we do 'frame variable' that explains why we aren't seeing
154 variables.
155 """
156 self.build(debug_info="dwarf")
157 exe = self.getBuildArtifact("a.out")
158 main_obj = self.getBuildArtifact("main.o")
159
160 # Set the modification time for main.o file to the current time after
161 # sleeping for 2 seconds. This ensures the modification time will have
162 # changed and will not match the modification time in the debug map and
163 # force an error when we run to main and try to get variables
164 time.sleep(2)
165 os.utime(main_obj, None)
166
167 # We have to set a named breakpoint because we don't have any debug info
168 # because we deleted the main.o file since the mod times don't match
169 # and debug info won't be loaded
170 (target, process, thread, bkpt) = lldbutil.run_to_name_breakpoint(self, "main")
171
172 error_strings = [
173 'debug map object file "',
174 'main.o" changed (actual: 0x',
175 ", debug map: 0x",
176 ") since this executable was linked, debug info will not be loaded",
177 ]
178 self.check_frame_variable_errors(thread, error_strings)
179
180 @skipIfRemote
181 @skipIfWindows # Windows can't set breakpoints by name 'main' in this case.
182 def test_gline_tables_only(self):
183 """
184 Test that if we build a binary with "-gline-tables-only" that we can
185 set a file and line breakpoint successfully, and get an error
186 letting us know that this build option was enabled when trying to
187 read variables.
188 """
189 self.build(dictionary={"CFLAGS_EXTRAS": "-gline-tables-only"})
190 exe = self.getBuildArtifact("a.out")
191
192 # We have to set a named breakpoint because we don't have any debug info
193 # because we deleted the main.o file since the mod times don't match
194 # and debug info won't be loaded
195 (target, process, thread, bkpt) = lldbutil.run_to_name_breakpoint(self, "main")
196 error_strings = [
197 "no variable information is available in debug info for this compile unit"
198 ]
199 self.check_frame_variable_errors(thread, error_strings)
200
201 @skipUnlessPlatform(["linux", "freebsd"])
202 @add_test_categories(["dwo"])
203 def test_fission_missing_dwo(self):
204 """
205 Test that if we build a binary with "-gsplit-dwarf" that we can
206 set a file and line breakpoint successfully, and get an error
207 letting us know we were unable to load the .dwo file.
208 """
209 self.build(dictionary={"CFLAGS_EXTRAS": "-gsplit-dwarf"})
210 exe = self.getBuildArtifact("a.out")
211 main_dwo = self.getBuildArtifact("main.dwo")
212
213 self.assertTrue(
214 os.path.exists(main_dwo), 'Make sure "%s" file exists' % (main_dwo)
215 )
216 # Delete the main.dwo file that contains the debug info so we force an
217 # error when we run to main and try to get variables.
218 os.unlink(main_dwo)
219
220 # We have to set a named breakpoint because we don't have any debug info
221 # because we deleted the main.o file since the mod times don't match
222 # and debug info won't be loaded
223 (target, process, thread, bkpt) = lldbutil.run_to_name_breakpoint(self, "main")
224 error_strings = [
225 'unable to locate .dwo debug file "',
226 'main.dwo" for skeleton DIE 0x',
227 ]
228 self.check_frame_variable_errors(thread, error_strings)
229
230 @skipUnlessPlatform(["linux", "freebsd"])
231 @add_test_categories(["dwo"])
232 def test_fission_invalid_dwo_objectfile(self):
233 """
234 Test that if we build a binary with "-gsplit-dwarf" that we can
235 set a file and line breakpoint successfully, and get an error
236 letting us know we were unable to load the .dwo file because it
237 existed, but it wasn't a valid object file.
238 """
239 self.build(dictionary={"CFLAGS_EXTRAS": "-gsplit-dwarf"})
240 exe = self.getBuildArtifact("a.out")
241 main_dwo = self.getBuildArtifact("main.dwo")
242
243 self.assertTrue(
244 os.path.exists(main_dwo), 'Make sure "%s" file exists' % (main_dwo)
245 )
246 # Overwrite the main.dwo with the main.c source file so that the .dwo
247 # file exists, but it isn't a valid object file as there is an error
248 # for this case.
249 shutil.copyfile(self.getSourcePath("main.c"), main_dwo)
250
251 # We have to set a named breakpoint because we don't have any debug info
252 # because we deleted the main.o file since the mod times don't match
253 # and debug info won't be loaded
254 (target, process, thread, bkpt) = lldbutil.run_to_name_breakpoint(self, "main")
255 error_strings = [
256 'unable to load object file for .dwo debug file "'
257 'main.dwo" for unit DIE 0x',
258 ]
259 self.check_frame_variable_errors(thread, error_strings)