Jim Ingham | 7d8f86c | 2014-04-02 01:05:27 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test that you can set breakpoint commands successfully with the Python API's: |
| 3 | """ |
| 4 | |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 5 | from __future__ import print_function |
| 6 | |
Zachary Turner | 0a0490b | 2015-10-27 20:12:05 +0000 | [diff] [blame^] | 7 | import use_lldb_suite |
Zachary Turner | 77db4a8 | 2015-10-22 20:06:20 +0000 | [diff] [blame] | 8 | |
Jim Ingham | 7d8f86c | 2014-04-02 01:05:27 +0000 | [diff] [blame] | 9 | import os |
| 10 | import re |
Jim Ingham | 7d8f86c | 2014-04-02 01:05:27 +0000 | [diff] [blame] | 11 | import lldb, lldbutil |
| 12 | import sys |
| 13 | from lldbtest import * |
| 14 | |
| 15 | class PythonBreakpointCommandSettingTestCase(TestBase): |
| 16 | |
| 17 | mydir = TestBase.compute_mydir(__file__) |
| 18 | my_var = 10 |
| 19 | |
Pavel Labath | dc8b2d3 | 2015-10-26 09:28:32 +0000 | [diff] [blame] | 20 | @add_test_categories(['pyapi']) |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 21 | def test_step_out_python(self): |
Jim Ingham | 7d8f86c | 2014-04-02 01:05:27 +0000 | [diff] [blame] | 22 | """Test stepping out using avoid-no-debug with dsyms.""" |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 23 | self.build() |
Jim Ingham | 7d8f86c | 2014-04-02 01:05:27 +0000 | [diff] [blame] | 24 | 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 Ingham | e732052 | 2015-02-12 17:37:46 +0000 | [diff] [blame] | 45 | # 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 Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 54 | print("Got comp unit: ", comp_unit.GetFilename()) |
Jim Ingham | e732052 | 2015-02-12 17:37:46 +0000 | [diff] [blame] | 55 | 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 Ingham | 7d8f86c | 2014-04-02 01:05:27 +0000 | [diff] [blame] | 64 | PythonBreakpointCommandSettingTestCase.my_var = 10 |
| 65 | error = lldb.SBError() |
| 66 | error = body_bkpt.SetScriptCallbackBody("\ |
| 67 | import TestBreakpointCommandsFromPython\n\ |
| 68 | TestBreakpointCommandsFromPython.PythonBreakpointCommandSettingTestCase.my_var = 20\n\ |
Zachary Turner | 35d017f | 2015-10-23 17:04:29 +0000 | [diff] [blame] | 69 | print('Hit breakpoint')") |
Jim Ingham | 7d8f86c | 2014-04-02 01:05:27 +0000 | [diff] [blame] | 70 | 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") |