Debian Codegraph @

lldb/test/API/commands/frame/recognizer/TestFrameRecognizer.py

raw
1 # encoding: utf-8
2 """
3 Test lldb's frame recognizers.
4 """
5
6 import lldb
7 from lldbsuite.test.decorators import *
8 from lldbsuite.test.lldbtest import *
9 from lldbsuite.test import lldbutil
10
11 import recognizer
12
13
14 class FrameRecognizerTestCase(TestBase):
15 NO_DEBUG_INFO_TESTCASE = True
16
17 @skipUnlessDarwin
18 def test_frame_recognizer_1(self):
19 self.build()
20 exe = self.getBuildArtifact("a.out")
21
22 # Clear internal & plugins recognizers that get initialized at launch
23 self.runCmd("frame recognizer clear")
24
25 self.runCmd(
26 "command script import "
27 + os.path.join(self.getSourceDir(), "recognizer.py")
28 )
29
30 self.expect("frame recognizer list", substrs=["no matching results found."])
31
32 self.runCmd(
33 "frame recognizer add -l recognizer.MyFrameRecognizer -s a.out -n foo"
34 )
35
36 self.expect(
37 "frame recognizer list",
38 substrs=["0: recognizer.MyFrameRecognizer, module a.out, symbol foo"],
39 )
40
41 self.runCmd(
42 "frame recognizer add -l recognizer.MyOtherFrameRecognizer -s a.out -n bar -x"
43 )
44
45 self.expect(
46 "frame recognizer list",
47 substrs=[
48 "1: recognizer.MyOtherFrameRecognizer, module a.out, symbol bar (regexp)",
49 "0: recognizer.MyFrameRecognizer, module a.out, symbol foo",
50 ],
51 )
52
53 self.runCmd("frame recognizer delete 0")
54
55 # Test that it deleted the recognizer with id 0.
56 self.expect(
57 "frame recognizer list",
58 substrs=[
59 "1: recognizer.MyOtherFrameRecognizer, module a.out, symbol bar (regexp)"
60 ],
61 )
62 self.expect(
63 "frame recognizer list", matching=False, substrs=["MyFrameRecognizer"]
64 )
65
66 # Test that an invalid index and deleting the same index again
67 # is an error and doesn't do any changes.
68 self.expect(
69 "frame recognizer delete 2",
70 error=True,
71 substrs=["error: '2' is not a valid recognizer id."],
72 )
73 self.expect(
74 "frame recognizer delete 0",
75 error=True,
76 substrs=["error: '0' is not a valid recognizer id."],
77 )
78 # Recognizers should have the same state as above.
79 self.expect(
80 "frame recognizer list",
81 substrs=[
82 "1: recognizer.MyOtherFrameRecognizer, module a.out, symbol bar (regexp)"
83 ],
84 )
85 self.expect(
86 "frame recognizer list", matching=False, substrs=["MyFrameRecognizer"]
87 )
88
89 self.runCmd("frame recognizer clear")
90
91 self.expect("frame recognizer list", substrs=["no matching results found."])
92
93 self.runCmd(
94 "frame recognizer add -l recognizer.MyFrameRecognizer -s a.out -n foo"
95 )
96
97 target, process, thread, _ = lldbutil.run_to_name_breakpoint(
98 self, "foo", exe_name=exe
99 )
100 frame = thread.GetSelectedFrame()
101
102 self.expect("frame variable", substrs=["(int) a = 42", "(int) b = 56"])
103
104 # Recognized arguments don't show up by default...
105 variables = frame.GetVariables(lldb.SBVariablesOptions())
106 self.assertEqual(variables.GetSize(), 0)
107
108 # ...unless you set target.display-recognized-arguments to 1...
109 self.runCmd("settings set target.display-recognized-arguments 1")
110 variables = frame.GetVariables(lldb.SBVariablesOptions())
111 self.assertEqual(variables.GetSize(), 2)
112
113 # ...and you can reset it back to 0 to hide them again...
114 self.runCmd("settings set target.display-recognized-arguments 0")
115 variables = frame.GetVariables(lldb.SBVariablesOptions())
116 self.assertEqual(variables.GetSize(), 0)
117
118 # ... or explicitly ask for them with SetIncludeRecognizedArguments(True).
119 opts = lldb.SBVariablesOptions()
120 opts.SetIncludeRecognizedArguments(True)
121 variables = frame.GetVariables(opts)
122
123 self.assertEqual(variables.GetSize(), 2)
124 self.assertEqual(variables.GetValueAtIndex(0).name, "a")
125 self.assertEqual(variables.GetValueAtIndex(0).signed, 42)
126 self.assertEqual(
127 variables.GetValueAtIndex(0).GetValueType(), lldb.eValueTypeVariableArgument
128 )
129 self.assertEqual(variables.GetValueAtIndex(1).name, "b")
130 self.assertEqual(variables.GetValueAtIndex(1).signed, 56)
131 self.assertEqual(
132 variables.GetValueAtIndex(1).GetValueType(), lldb.eValueTypeVariableArgument
133 )
134
135 self.expect(
136 "frame recognizer info 0",
137 substrs=["frame 0 is recognized by recognizer.MyFrameRecognizer"],
138 )
139
140 self.expect(
141 "frame recognizer info 999", error=True, substrs=["no frame with index 999"]
142 )
143
144 self.expect(
145 "frame recognizer info 1",
146 substrs=["frame 1 not recognized by any recognizer"],
147 )
148
149 # FIXME: The following doesn't work yet, but should be fixed.
150 """
151 target, process, thread, _ = lldbutil.run_to_name_breakpoint(self, "bar",
152 exe_name = exe)
153 frame = thread.GetSelectedFrame()
154
155 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
156 substrs=['stopped', 'stop reason = breakpoint'])
157
158 self.expect("frame variable -t",
159 substrs=['(int *) a = '])
160
161 self.expect("frame variable -t *a",
162 substrs=['*a = 78'])
163 """
164
165 @skipUnlessDarwin
166 def test_frame_recognizer_multi_symbol(self):
167 self.build()
168 exe = self.getBuildArtifact("a.out")
169
170 # Clear internal & plugins recognizers that get initialized at launch
171 self.runCmd("frame recognizer clear")
172
173 self.runCmd(
174 "command script import "
175 + os.path.join(self.getSourceDir(), "recognizer.py")
176 )
177
178 self.expect("frame recognizer list", substrs=["no matching results found."])
179
180 self.runCmd(
181 "frame recognizer add -l recognizer.MyFrameRecognizer -s a.out -n foo -n bar"
182 )
183
184 self.expect(
185 "frame recognizer list",
186 substrs=[
187 "recognizer.MyFrameRecognizer, module a.out, symbol foo, symbol bar"
188 ],
189 )
190
191 target, process, thread, _ = lldbutil.run_to_name_breakpoint(
192 self, "foo", exe_name=exe
193 )
194 frame = thread.GetSelectedFrame()
195
196 self.expect(
197 "frame recognizer info 0",
198 substrs=["frame 0 is recognized by recognizer.MyFrameRecognizer"],
199 )
200
201 target, process, thread, _ = lldbutil.run_to_name_breakpoint(
202 self, "bar", exe_name=exe
203 )
204 frame = thread.GetSelectedFrame()
205
206 self.expect(
207 "frame recognizer info 0",
208 substrs=["frame 0 is recognized by recognizer.MyFrameRecognizer"],
209 )
210
211 @skipUnlessDarwin
212 def test_frame_recognizer_target_specific(self):
213 self.build()
214 exe = self.getBuildArtifact("a.out")
215
216 # Clear internal & plugins recognizers that get initialized at launch
217 self.runCmd("frame recognizer clear")
218
219 # Create a target.
220 target, process, thread, _ = lldbutil.run_to_name_breakpoint(
221 self, "foo", exe_name=exe
222 )
223
224 self.runCmd(
225 "command script import "
226 + os.path.join(self.getSourceDir(), "recognizer.py")
227 )
228
229 # Check that this doesn't contain our own FrameRecognizer somehow.
230 self.expect(
231 "frame recognizer list", matching=False, substrs=["MyFrameRecognizer"]
232 )
233
234 # Add a frame recognizer in that target.
235 self.runCmd(
236 "frame recognizer add -l recognizer.MyFrameRecognizer -s a.out -n foo -n bar"
237 )
238
239 self.expect(
240 "frame recognizer list",
241 substrs=[
242 "recognizer.MyFrameRecognizer, module a.out, symbol foo, symbol bar"
243 ],
244 )
245
246 self.expect(
247 "frame recognizer info 0",
248 substrs=["frame 0 is recognized by recognizer.MyFrameRecognizer"],
249 )
250
251 # Create a second target. That one shouldn't have the frame recognizer.
252 target, process, thread, _ = lldbutil.run_to_name_breakpoint(
253 self, "bar", exe_name=exe
254 )
255
256 self.expect(
257 "frame recognizer info 0",
258 substrs=["frame 0 not recognized by any recognizer"],
259 )
260
261 # Add a frame recognizer to the new target.
262 self.runCmd(
263 "frame recognizer add -l recognizer.MyFrameRecognizer -s a.out -n bar"
264 )
265
266 self.expect(
267 "frame recognizer list",
268 substrs=["recognizer.MyFrameRecognizer, module a.out, symbol bar"],
269 )
270
271 # Now the new target should also recognize the frame.
272 self.expect(
273 "frame recognizer info 0",
274 substrs=["frame 0 is recognized by recognizer.MyFrameRecognizer"],
275 )
276
277 @skipUnlessDarwin
278 def test_frame_recognizer_not_only_first_instruction(self):
279 self.build()
280 exe = self.getBuildArtifact("a.out")
281
282 # Clear internal & plugins recognizers that get initialized at launch.
283 self.runCmd("frame recognizer clear")
284
285 self.runCmd(
286 "command script import "
287 + os.path.join(self.getSourceDir(), "recognizer.py")
288 )
289
290 self.expect("frame recognizer list", substrs=["no matching results found."])
291
292 # Create a target.
293 target, process, thread, _ = lldbutil.run_to_name_breakpoint(
294 self, "foo", exe_name=exe
295 )
296
297 # Move the PC one instruction further.
298 self.runCmd("next")
299
300 # Add a frame recognizer in that target.
301 self.runCmd(
302 "frame recognizer add -l recognizer.MyFrameRecognizer -s a.out -n foo -n bar"
303 )
304
305 # It's not applied to foo(), because frame's PC is not at the first instruction of the function.
306 self.expect(
307 "frame recognizer info 0",
308 substrs=["frame 0 not recognized by any recognizer"],
309 )
310
311 # Add a frame recognizer with --first-instruction-only=true.
312 self.runCmd("frame recognizer clear")
313
314 self.runCmd(
315 "frame recognizer add -l recognizer.MyFrameRecognizer -s a.out -n foo -n bar --first-instruction-only=true"
316 )
317
318 # It's not applied to foo(), because frame's PC is not at the first instruction of the function.
319 self.expect(
320 "frame recognizer info 0",
321 substrs=["frame 0 not recognized by any recognizer"],
322 )
323
324 # Now add a frame recognizer with --first-instruction-only=false.
325 self.runCmd("frame recognizer clear")
326
327 self.runCmd(
328 "frame recognizer add -l recognizer.MyFrameRecognizer -s a.out -n foo -n bar --first-instruction-only=false"
329 )
330
331 # This time it should recognize the frame.
332 self.expect(
333 "frame recognizer info 0",
334 substrs=["frame 0 is recognized by recognizer.MyFrameRecognizer"],
335 )
336
337 opts = lldb.SBVariablesOptions()
338 opts.SetIncludeRecognizedArguments(True)
339 frame = thread.GetSelectedFrame()
340 variables = frame.GetVariables(opts)
341
342 self.assertEqual(variables.GetSize(), 2)
343 self.assertEqual(variables.GetValueAtIndex(0).name, "a")
344 self.assertEqual(variables.GetValueAtIndex(0).signed, 42)
345 self.assertEqual(
346 variables.GetValueAtIndex(0).GetValueType(), lldb.eValueTypeVariableArgument
347 )
348 self.assertEqual(variables.GetValueAtIndex(1).name, "b")
349 self.assertEqual(variables.GetValueAtIndex(1).signed, 56)
350 self.assertEqual(
351 variables.GetValueAtIndex(1).GetValueType(), lldb.eValueTypeVariableArgument
352 )
353
354 @no_debug_info_test
355 def test_frame_recognizer_delete_invalid_arg(self):
356 self.expect(
357 "frame recognizer delete a",
358 error=True,
359 substrs=["error: 'a' is not a valid recognizer id."],
360 )
361 self.expect(
362 'frame recognizer delete ""',
363 error=True,
364 substrs=["error: '' is not a valid recognizer id."],
365 )
366 self.expect(
367 "frame recognizer delete -1",
368 error=True,
369 substrs=["error: '-1' is not a valid recognizer id."],
370 )
371 self.expect(
372 "frame recognizer delete 4294967297",
373 error=True,
374 substrs=["error: '4294967297' is not a valid recognizer id."],
375 )
376
377 @no_debug_info_test
378 def test_frame_recognizer_info_invalid_arg(self):
379 self.expect(
380 "frame recognizer info a",
381 error=True,
382 substrs=["error: 'a' is not a valid frame index."],
383 )
384 self.expect(
385 'frame recognizer info ""',
386 error=True,
387 substrs=["error: '' is not a valid frame index."],
388 )
389 self.expect(
390 "frame recognizer info -1",
391 error=True,
392 substrs=["error: '-1' is not a valid frame index."],
393 )
394 self.expect(
395 "frame recognizer info 4294967297",
396 error=True,
397 substrs=["error: '4294967297' is not a valid frame index."],
398 )
399
400 @no_debug_info_test
401 def test_frame_recognizer_add_invalid_arg(self):
402 self.expect(
403 "frame recognizer add -f",
404 error=True,
405 substrs=["error: last option requires an argument"],
406 )
407 self.expect(
408 "frame recognizer add -f -1",
409 error=True,
410 substrs=["error: invalid boolean value '-1' passed for -f option"],
411 )
412 self.expect(
413 "frame recognizer add -f foo",
414 error=True,
415 substrs=["error: invalid boolean value 'foo' passed for -f option"],
416 )