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