blob: 83997617f0e740f86846877ed95fc0a059e4a6e4 [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
Zachary Turner35d017f2015-10-23 17:04:29 +00005from __future__ import print_function
6
Zachary Turner0a0490b2015-10-27 20:12:05 +00007import use_lldb_suite
Zachary Turner77db4a82015-10-22 20:06:20 +00008
Jim Ingham7d8f86c2014-04-02 01:05:27 +00009import os
10import re
Jim Ingham7d8f86c2014-04-02 01:05:27 +000011import lldb, lldbutil
12import sys
13from lldbtest import *
14
15class PythonBreakpointCommandSettingTestCase(TestBase):
16
17 mydir = TestBase.compute_mydir(__file__)
18 my_var = 10
19
Pavel Labathdc8b2d32015-10-26 09:28:32 +000020 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000021 def test_step_out_python(self):
Jim Ingham7d8f86c2014-04-02 01:05:27 +000022 """Test stepping out using avoid-no-debug with dsyms."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000023 self.build()
Jim Ingham7d8f86c2014-04-02 01:05:27 +000024 self.do_set_python_command_from_python ()
25
26 def setUp (self):
27 TestBase.setUp(self)
28 self.main_source = "main.c"
29 self.main_source_spec = lldb.SBFileSpec(self.main_source)
30
31
32 def do_set_python_command_from_python (self):
33 exe = os.path.join(os.getcwd(), "a.out")
34 error = lldb.SBError()
35
36 self.target = self.dbg.CreateTarget(exe)
37 self.assertTrue(self.target, VALID_TARGET)
38
39 body_bkpt = self.target.BreakpointCreateBySourceRegex("Set break point at this line.", self.main_source_spec)
40 self.assertTrue(body_bkpt, VALID_BREAKPOINT)
41
42 func_bkpt = self.target.BreakpointCreateBySourceRegex("Set break point at this line.", self.main_source_spec)
43 self.assertTrue(func_bkpt, VALID_BREAKPOINT)
44
Jim Inghame7320522015-02-12 17:37:46 +000045 # Also test that setting a source regex breakpoint with an empty file spec list sets it on all files:
46 no_files_bkpt = self.target.BreakpointCreateBySourceRegex("Set a breakpoint here", lldb.SBFileSpecList(), lldb.SBFileSpecList())
47 self.assertTrue(no_files_bkpt, VALID_BREAKPOINT)
48 num_locations = no_files_bkpt.GetNumLocations()
49 self.assertTrue(num_locations >= 2, "Got at least two breakpoint locations")
50 got_one_in_A = False
51 got_one_in_B = False
52 for idx in range(0, num_locations):
53 comp_unit = no_files_bkpt.GetLocationAtIndex(idx).GetAddress().GetSymbolContext(lldb.eSymbolContextCompUnit).GetCompileUnit().GetFileSpec()
Zachary Turner35d017f2015-10-23 17:04:29 +000054 print("Got comp unit: ", comp_unit.GetFilename())
Jim Inghame7320522015-02-12 17:37:46 +000055 if comp_unit.GetFilename() == "a.c":
56 got_one_in_A = True
57 elif comp_unit.GetFilename() == "b.c":
58 got_one_in_B = True
59
60 self.assertTrue(got_one_in_A, "Failed to match the pattern in A")
61 self.assertTrue(got_one_in_B, "Failed to match the pattern in B")
62 self.target.BreakpointDelete(no_files_bkpt.GetID())
63
Jim Ingham7d8f86c2014-04-02 01:05:27 +000064 PythonBreakpointCommandSettingTestCase.my_var = 10
65 error = lldb.SBError()
66 error = body_bkpt.SetScriptCallbackBody("\
67import TestBreakpointCommandsFromPython\n\
68TestBreakpointCommandsFromPython.PythonBreakpointCommandSettingTestCase.my_var = 20\n\
Zachary Turner35d017f2015-10-23 17:04:29 +000069print('Hit breakpoint')")
Jim Ingham7d8f86c2014-04-02 01:05:27 +000070 self.assertTrue (error.Success(), "Failed to set the script callback body: %s."%(error.GetCString()))
71
72 self.dbg.HandleCommand("command script import --allow-reload ./bktptcmd.py")
73 func_bkpt.SetScriptCallbackFunction("bktptcmd.function")
74
75 # We will use the function that touches a text file, so remove it first:
76 self.RemoveTempFile("output2.txt")
77
78 # Now launch the process, and do not stop at entry point.
79 self.process = self.target.LaunchSimple (None, None, self.get_process_working_directory())
80
81 self.assertTrue(self.process, PROCESS_IS_VALID)
82
83 # Now finish, and make sure the return value is correct.
84 threads = lldbutil.get_threads_stopped_at_breakpoint (self.process, body_bkpt)
85 self.assertTrue(len(threads) == 1, "Stopped at inner breakpoint.")
86 self.thread = threads[0]
87
88 self.assertTrue(PythonBreakpointCommandSettingTestCase.my_var == 20)
89
90 # Check for the function version as well, which produced this file:
91 # Remember to clean up after ourselves...
92 self.assertTrue(os.path.isfile("output2.txt"),
93 "'output2.txt' exists due to breakpoint command for breakpoint function.")
94 self.RemoveTempFile("output2.txt")