Files
clang-p2996/lldb/test/breakpoint_command/TestBreakpointCommand.py
Greg Clayton 7260f6206f Centralized a lot of the status information for processes,
threads, and stack frame down in the lldb_private::Process,
lldb_private::Thread, lldb_private::StackFrameList and the 
lldb_private::StackFrame classes. We had some command line
commands that had duplicate versions of the process status
output ("thread list" and "process status" for example). 

Removed the "file" command and placed it where it should
have been: "target create". Made an alias for "file" to
"target create" so we stay compatible with GDB commands.

We can now have multple usable targets in lldb at the
same time. This is nice for comparing two runs of a program
or debugging more than one binary at the same time. The
new command is "target select <target-idx>" and also to see
a list of the current targets you can use the new "target list"
command. The flow in a debug session can be:

(lldb) target create /path/to/exe/a.out
(lldb) breakpoint set --name main
(lldb) run
... hit breakpoint
(lldb) target create /bin/ls
(lldb) run /tmp
Process 36001 exited with status = 0 (0x00000000) 
(lldb) target list
Current targets:
  target #0: /tmp/args/a.out ( arch=x86_64-apple-darwin, platform=localhost, pid=35999, state=stopped )
* target #1: /bin/ls ( arch=x86_64-apple-darwin, platform=localhost, pid=36001, state=exited )
(lldb) target select 0
Current targets:
* target #0: /tmp/args/a.out ( arch=x86_64-apple-darwin, platform=localhost, pid=35999, state=stopped )
  target #1: /bin/ls ( arch=x86_64-apple-darwin, platform=localhost, pid=36001, state=exited )
(lldb) bt
* thread #1: tid = 0x2d03, 0x0000000100000b9a a.out`main + 42 at main.c:16, stop reason = breakpoint 1.1
  frame #0: 0x0000000100000b9a a.out`main + 42 at main.c:16
  frame #1: 0x0000000100000b64 a.out`start + 52

Above we created a target for "a.out" and ran and hit a
breakpoint at "main". Then we created a new target for /bin/ls
and ran it. Then we listed the targest and selected our original
"a.out" program, so we showed two concurent debug sessions
going on at the same time.

llvm-svn: 129695
2011-04-18 08:33:37 +00:00

180 lines
7.4 KiB
Python

"""
Test lldb breakpoint command add/list/remove.
"""
import os, time
import unittest2
import lldb
from lldbtest import *
class BreakpointCommandTestCase(TestBase):
mydir = "breakpoint_command"
@classmethod
def classCleanup(cls):
"""Cleanup the test byproduct of breakpoint_command_sequence(self)."""
system(["/bin/sh", "-c", "rm -f output.txt"])
@unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
def test_with_dsym(self):
"""Test a sequence of breakpoint command add, list, and remove."""
self.buildDsym()
self.breakpoint_command_sequence()
self.breakpoint_command_script_parameters ()
def test_with_dwarf(self):
"""Test a sequence of breakpoint command add, list, and remove."""
self.buildDwarf()
self.breakpoint_command_sequence()
self.breakpoint_command_script_parameters ()
def setUp(self):
# Call super's setUp().
TestBase.setUp(self)
# Find the line number to break inside main().
self.line = line_number('main.c', '// Set break point at this line.')
def breakpoint_command_sequence(self):
"""Test a sequence of breakpoint command add, list, and remove."""
exe = os.path.join(os.getcwd(), "a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
# Add two breakpoints on the same line.
self.expect("breakpoint set -f main.c -l %d" % self.line,
BREAKPOINT_CREATED,
startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
self.line)
self.expect("breakpoint set -f main.c -l %d" % self.line,
BREAKPOINT_CREATED,
startstr = "Breakpoint created: 2: file ='main.c', line = %d, locations = 1" %
self.line)
# Now add callbacks for the breakpoints just created.
self.runCmd("breakpoint command add -s command -o 'frame variable -t -s' 1")
self.runCmd("breakpoint command add -s python -o 'here = open(\"output.txt\", \"w\"); print >> here, \"lldb\"; here.close()' 2")
# Check that the breakpoint commands are correctly set.
# The breakpoint list now only contains breakpoint 1.
self.expect("breakpoint list", "Breakpoints 1 & 2 created",
substrs = ["1: file ='main.c', line = %d, locations = 1" % self.line,
"2: file ='main.c', line = %d, locations = 1" % self.line] )
self.expect("breakpoint list -f", "Breakpoints 1 & 2 created",
substrs = ["1: file ='main.c', line = %d, locations = 1" % self.line,
"2: file ='main.c', line = %d, locations = 1" % self.line],
patterns = ["1.1: .+at main.c:%d, .+unresolved, hit count = 0" % self.line,
"2.1: .+at main.c:%d, .+unresolved, hit count = 0" % self.line])
self.expect("breakpoint command list 1", "Breakpoint 1 command ok",
substrs = ["Breakpoint commands:",
"frame variable -t -s"])
self.expect("breakpoint command list 2", "Breakpoint 2 command ok",
substrs = ["Breakpoint commands:",
"here = open",
"print >> here",
"here.close()"])
# Run the program. Remove 'output.txt' if it exists.
if os.path.exists('output.txt'):
os.remove('output.txt')
self.runCmd("run", RUN_SUCCEEDED)
# Check that the file 'output.txt' exists and contains the string "lldb".
# The 'output.txt' file should now exist.
self.assertTrue(os.path.isfile("output.txt"),
"'output.txt' exists due to breakpoint command for breakpoint 2.")
# Read the output file produced by running the program.
with open('output.txt', 'r') as f:
output = f.read()
self.expect(output, "File 'output.txt' and the content matches", exe=False,
startstr = "lldb")
# Finish the program.
self.runCmd("process continue")
# Remove the breakpoint command associated with breakpoint 1.
self.runCmd("breakpoint command remove 1")
# Remove breakpoint 2.
self.runCmd("breakpoint delete 2")
self.expect("breakpoint command list 1",
startstr = "Breakpoint 1 does not have an associated command.")
self.expect("breakpoint command list 2", error=True,
startstr = "error: '2' is not a currently valid breakpoint id.")
# The breakpoint list now only contains breakpoint 1.
self.expect("breakpoint list -f", "Breakpoint 1 exists",
substrs = ["1: file ='main.c', line = %d, locations = 1, resolved = 1" %
self.line,
"hit count = 1"])
# Not breakpoint 2.
self.expect("breakpoint list -f", "No more breakpoint 2", matching=False,
substrs = ["2: file ='main.c', line = %d, locations = 1, resolved = 1" %
self.line])
# Run the program again, with breakpoint 1 remaining.
self.runCmd("run", RUN_SUCCEEDED)
# We should be stopped again due to breakpoint 1.
# The stop reason of the thread should be breakpoint.
self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
substrs = ['stopped',
'stop reason = breakpoint'])
# The breakpoint should have a hit count of 2.
self.expect("breakpoint list -f", BREAKPOINT_HIT_TWICE,
substrs = ['resolved, hit count = 2'])
def breakpoint_command_script_parameters (self):
"""Test that the frame and breakpoint location are being properly passed to the script breakpoint command function."""
exe = os.path.join(os.getcwd(), "a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
# Add a breakpoint.
self.expect("breakpoint set -f main.c -l %d" % self.line,
BREAKPOINT_CREATED,
startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
self.line)
# Now add callbacks for the breakpoints just created.
self.runCmd("breakpoint command add -s python -o 'here = open(\"output-2.txt\", \"w\"); print >> here, frame; print >> here, bp_loc; here.close()' 1")
# Remove 'output-2.txt' if it already exists.
if (os.path.exists('output-2.txt')):
os.remove ('output-2.txt')
# Run program, hit breakpoint, and hopefully write out new version of 'output-2.txt'
self.runCmd ("run", RUN_SUCCEEDED)
# Check that the file 'output.txt' exists and contains the string "lldb".
# The 'output-2.txt' file should now exist.
self.assertTrue(os.path.isfile("output-2.txt"),
"'output-2.txt' exists due to breakpoint command for breakpoint 1.")
# Read the output file produced by running the program.
with open('output-2.txt', 'r') as f:
output = f.read()
self.expect (output, "File 'output-2.txt' and the content matches", exe=False,
startstr = "frame #0:",
patterns = ["1.* where = .*main .* resolved, hit count = 1" ])
# Now remove 'output-2.txt'
os.remove ('output-2.txt')
if __name__ == '__main__':
import atexit
lldb.SBDebugger.Initialize()
atexit.register(lambda: lldb.SBDebugger.Terminate())
unittest2.main()