]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/functionalities/breakpoint/breakpoint_command/TestBreakpointCommandsFromPython.py
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / functionalities / breakpoint / breakpoint_command / TestBreakpointCommandsFromPython.py
1 """
2 Test that you can set breakpoint commands successfully with the Python API's:
3 """
4
5 from __future__ import print_function
6
7
8 import os
9 import re
10 import sys
11 import lldb
12 from lldbsuite.test.decorators import *
13 from lldbsuite.test.lldbtest import *
14 from lldbsuite.test import lldbutil
15
16
17 class PythonBreakpointCommandSettingTestCase(TestBase):
18
19     mydir = TestBase.compute_mydir(__file__)
20     my_var = 10
21
22     @add_test_categories(['pyapi'])
23     def test_step_out_python(self):
24         """Test stepping out using avoid-no-debug with dsyms."""
25         self.build()
26         self.do_set_python_command_from_python()
27
28     def setUp(self):
29         TestBase.setUp(self)
30         self.main_source = "main.c"
31         self.main_source_spec = lldb.SBFileSpec(self.main_source)
32
33     def do_set_python_command_from_python(self):
34         exe = os.path.join(os.getcwd(), "a.out")
35         error = lldb.SBError()
36
37         self.target = self.dbg.CreateTarget(exe)
38         self.assertTrue(self.target, VALID_TARGET)
39
40         body_bkpt = self.target.BreakpointCreateBySourceRegex(
41             "Set break point at this line.", self.main_source_spec)
42         self.assertTrue(body_bkpt, VALID_BREAKPOINT)
43
44         func_bkpt = self.target.BreakpointCreateBySourceRegex(
45             "Set break point at this line.", self.main_source_spec)
46         self.assertTrue(func_bkpt, VALID_BREAKPOINT)
47
48         # Also test that setting a source regex breakpoint with an empty file
49         # spec list sets it on all files:
50         no_files_bkpt = self.target.BreakpointCreateBySourceRegex(
51             "Set a breakpoint here", lldb.SBFileSpecList(), lldb.SBFileSpecList())
52         self.assertTrue(no_files_bkpt, VALID_BREAKPOINT)
53         num_locations = no_files_bkpt.GetNumLocations()
54         self.assertTrue(
55             num_locations >= 2,
56             "Got at least two breakpoint locations")
57         got_one_in_A = False
58         got_one_in_B = False
59         for idx in range(0, num_locations):
60             comp_unit = no_files_bkpt.GetLocationAtIndex(idx).GetAddress().GetSymbolContext(
61                 lldb.eSymbolContextCompUnit).GetCompileUnit().GetFileSpec()
62             print("Got comp unit: ", comp_unit.GetFilename())
63             if comp_unit.GetFilename() == "a.c":
64                 got_one_in_A = True
65             elif comp_unit.GetFilename() == "b.c":
66                 got_one_in_B = True
67
68         self.assertTrue(got_one_in_A, "Failed to match the pattern in A")
69         self.assertTrue(got_one_in_B, "Failed to match the pattern in B")
70         self.target.BreakpointDelete(no_files_bkpt.GetID())
71
72         PythonBreakpointCommandSettingTestCase.my_var = 10
73         error = lldb.SBError()
74         error = body_bkpt.SetScriptCallbackBody("\
75 import TestBreakpointCommandsFromPython\n\
76 TestBreakpointCommandsFromPython.PythonBreakpointCommandSettingTestCase.my_var = 20\n\
77 print('Hit breakpoint')")
78         self.assertTrue(
79             error.Success(),
80             "Failed to set the script callback body: %s." %
81             (error.GetCString()))
82
83         self.dbg.HandleCommand(
84             "command script import --allow-reload ./bktptcmd.py")
85         func_bkpt.SetScriptCallbackFunction("bktptcmd.function")
86
87         # We will use the function that touches a text file, so remove it
88         # first:
89         self.RemoveTempFile("output2.txt")
90
91         # Now launch the process, and do not stop at entry point.
92         self.process = self.target.LaunchSimple(
93             None, None, self.get_process_working_directory())
94
95         self.assertTrue(self.process, PROCESS_IS_VALID)
96
97         # Now finish, and make sure the return value is correct.
98         threads = lldbutil.get_threads_stopped_at_breakpoint(
99             self.process, body_bkpt)
100         self.assertTrue(len(threads) == 1, "Stopped at inner breakpoint.")
101         self.thread = threads[0]
102
103         self.assertTrue(PythonBreakpointCommandSettingTestCase.my_var == 20)
104
105         # Check for the function version as well, which produced this file:
106         # Remember to clean up after ourselves...
107         self.assertTrue(
108             os.path.isfile("output2.txt"),
109             "'output2.txt' exists due to breakpoint command for breakpoint function.")
110         self.RemoveTempFile("output2.txt")