2010-12-10 02:22:12 +08:00
|
|
|
"""
|
|
|
|
Test lldb core component: SourceManager.
|
|
|
|
|
|
|
|
Test cases:
|
2010-12-10 06:06:05 +08:00
|
|
|
|
2010-12-11 09:20:39 +08:00
|
|
|
o test_display_source_python:
|
|
|
|
Test display of source using the SBSourceManager API.
|
2010-12-10 06:06:05 +08:00
|
|
|
o test_modify_source_file_while_debugging:
|
|
|
|
Test the caching mechanism of the source manager.
|
2010-12-10 02:22:12 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import unittest2
|
2010-12-10 02:38:52 +08:00
|
|
|
import lldb
|
2010-12-10 02:22:12 +08:00
|
|
|
from lldbtest import *
|
2012-09-22 08:05:11 +08:00
|
|
|
import lldbutil
|
2010-12-10 02:22:12 +08:00
|
|
|
|
|
|
|
class SourceManagerTestCase(TestBase):
|
|
|
|
|
2013-12-11 07:19:29 +08:00
|
|
|
mydir = TestBase.compute_mydir(__file__)
|
2010-12-10 02:22:12 +08:00
|
|
|
|
|
|
|
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.')
|
2011-12-13 05:59:28 +08:00
|
|
|
lldb.skip_build_and_cleanup = False
|
2010-12-10 02:22:12 +08:00
|
|
|
|
2010-12-11 09:20:39 +08:00
|
|
|
@python_api_test
|
|
|
|
def test_display_source_python(self):
|
|
|
|
"""Test display of source using the SBSourceManager API."""
|
|
|
|
self.buildDefault()
|
|
|
|
self.display_source_python()
|
|
|
|
|
2011-12-13 05:59:28 +08:00
|
|
|
def test_move_and_then_display_source(self):
|
|
|
|
"""Test that target.source-map settings work by moving main.c to hidden/main.c."""
|
|
|
|
self.buildDefault()
|
|
|
|
self.move_and_then_display_source()
|
|
|
|
|
2010-12-10 02:22:12 +08:00
|
|
|
def test_modify_source_file_while_debugging(self):
|
|
|
|
"""Modify a source file while debugging the executable."""
|
|
|
|
self.buildDefault()
|
|
|
|
self.modify_source_file_while_debugging()
|
|
|
|
|
2010-12-11 09:20:39 +08:00
|
|
|
def display_source_python(self):
|
|
|
|
"""Display source using the SBSourceManager API."""
|
|
|
|
exe = os.path.join(os.getcwd(), "a.out")
|
|
|
|
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
|
|
|
|
|
|
|
|
target = self.dbg.CreateTarget(exe)
|
2011-05-25 02:22:45 +08:00
|
|
|
self.assertTrue(target, VALID_TARGET)
|
2010-12-11 09:20:39 +08:00
|
|
|
|
|
|
|
# Launch the process, and do not stop at the entry point.
|
2013-12-14 03:18:59 +08:00
|
|
|
process = target.LaunchSimple (None, None, self.get_process_working_directory())
|
2010-12-11 09:20:39 +08:00
|
|
|
|
|
|
|
#
|
|
|
|
# Exercise Python APIs to display source lines.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Create the filespec for 'main.c'.
|
|
|
|
filespec = lldb.SBFileSpec('main.c', False)
|
|
|
|
source_mgr = self.dbg.GetSourceManager()
|
|
|
|
# Use a string stream as the destination.
|
|
|
|
stream = lldb.SBStream()
|
|
|
|
source_mgr.DisplaySourceLinesWithLineNumbers(filespec,
|
|
|
|
self.line,
|
|
|
|
2, # context before
|
|
|
|
2, # context after
|
|
|
|
"=>", # prefix for current line
|
|
|
|
stream)
|
|
|
|
|
2011-03-31 06:28:50 +08:00
|
|
|
# 2
|
|
|
|
# 3 int main(int argc, char const *argv[]) {
|
|
|
|
# => 4 printf("Hello world.\n"); // Set break point at this line.
|
|
|
|
# 5 return 0;
|
|
|
|
# 6 }
|
2010-12-11 09:20:39 +08:00
|
|
|
self.expect(stream.GetData(), "Source code displayed correctly",
|
|
|
|
exe=False,
|
2011-12-20 08:41:28 +08:00
|
|
|
patterns = ['=> %d.*Hello world' % self.line])
|
|
|
|
|
|
|
|
# Boundary condition testings for SBStream(). LLDB should not crash!
|
2012-10-06 03:14:57 +08:00
|
|
|
stream.Print(None)
|
2011-12-20 08:41:28 +08:00
|
|
|
stream.RedirectToFile(None, True)
|
2010-12-11 09:20:39 +08:00
|
|
|
|
2011-12-13 05:59:28 +08:00
|
|
|
def move_and_then_display_source(self):
|
|
|
|
"""Test that target.source-map settings work by moving main.c to hidden/main.c."""
|
|
|
|
exe = os.path.join(os.getcwd(), "a.out")
|
|
|
|
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
|
|
|
|
|
|
|
|
# Move main.c to hidden/main.c.
|
|
|
|
main_c = "main.c"
|
|
|
|
main_c_hidden = os.path.join("hidden", main_c)
|
|
|
|
os.rename(main_c, main_c_hidden)
|
|
|
|
|
|
|
|
if self.TraceOn():
|
2014-07-23 00:19:29 +08:00
|
|
|
system([["ls"]])
|
|
|
|
system([["ls", "hidden"]])
|
2011-12-13 05:59:28 +08:00
|
|
|
|
|
|
|
# Restore main.c after the test.
|
|
|
|
self.addTearDownHook(lambda: os.rename(main_c_hidden, main_c))
|
|
|
|
|
|
|
|
# Set target.source-map settings.
|
|
|
|
self.runCmd("settings set target.source-map %s %s" % (os.getcwd(), os.path.join(os.getcwd(), "hidden")))
|
|
|
|
# And verify that the settings work.
|
|
|
|
self.expect("settings show target.source-map",
|
|
|
|
substrs = [os.getcwd(), os.path.join(os.getcwd(), "hidden")])
|
|
|
|
|
|
|
|
# Display main() and verify that the source mapping has been kicked in.
|
2013-02-06 08:35:33 +08:00
|
|
|
self.expect("source list -n main", SOURCE_DISPLAYED_CORRECTLY,
|
2011-12-13 05:59:28 +08:00
|
|
|
substrs = ['Hello world'])
|
|
|
|
|
2010-12-10 02:22:12 +08:00
|
|
|
def modify_source_file_while_debugging(self):
|
|
|
|
"""Modify a source file while debugging the executable."""
|
|
|
|
exe = os.path.join(os.getcwd(), "a.out")
|
|
|
|
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
|
|
|
|
|
2012-09-22 08:05:11 +08:00
|
|
|
lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
|
2010-12-10 02:22:12 +08:00
|
|
|
|
|
|
|
self.runCmd("run", RUN_SUCCEEDED)
|
|
|
|
|
|
|
|
# The stop reason of the thread should be breakpoint.
|
|
|
|
self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
|
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 16:33:37 +08:00
|
|
|
substrs = ['stopped',
|
2011-04-21 04:35:59 +08:00
|
|
|
'main.c:%d' % self.line,
|
2010-12-10 02:22:12 +08:00
|
|
|
'stop reason = breakpoint'])
|
|
|
|
|
|
|
|
# Display some source code.
|
2013-02-06 08:35:33 +08:00
|
|
|
self.expect("source list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
|
2010-12-10 02:22:12 +08:00
|
|
|
substrs = ['Hello world'])
|
|
|
|
|
2011-04-21 04:35:59 +08:00
|
|
|
# The '-b' option shows the line table locations from the debug information
|
|
|
|
# that indicates valid places to set source level breakpoints.
|
|
|
|
|
|
|
|
# The file to display is implicit in this case.
|
2013-02-06 08:35:33 +08:00
|
|
|
self.runCmd("source list -l %d -c 3 -b" % self.line)
|
2011-04-21 04:35:59 +08:00
|
|
|
output = self.res.GetOutput().splitlines()[0]
|
|
|
|
|
|
|
|
# If the breakpoint set command succeeded, we should expect a positive number
|
|
|
|
# of breakpoints for the current line, i.e., self.line.
|
|
|
|
import re
|
|
|
|
m = re.search('^\[(\d+)\].*// Set break point at this line.', output)
|
|
|
|
if not m:
|
|
|
|
self.fail("Fail to display source level breakpoints")
|
|
|
|
self.assertTrue(int(m.group(1)) > 0)
|
|
|
|
|
2010-12-10 02:22:12 +08:00
|
|
|
# Read the main.c file content.
|
|
|
|
with open('main.c', 'r') as f:
|
|
|
|
original_content = f.read()
|
2011-04-20 06:11:23 +08:00
|
|
|
if self.TraceOn():
|
|
|
|
print "original content:", original_content
|
2010-12-10 02:22:12 +08:00
|
|
|
|
|
|
|
# Modify the in-memory copy of the original source code.
|
|
|
|
new_content = original_content.replace('Hello world', 'Hello lldb', 1)
|
|
|
|
|
|
|
|
# This is the function to restore the original content.
|
|
|
|
def restore_file():
|
2011-03-04 09:35:22 +08:00
|
|
|
#print "os.path.getmtime() before restore:", os.path.getmtime('main.c')
|
|
|
|
time.sleep(1)
|
2015-01-16 06:53:44 +08:00
|
|
|
with open('main.c', 'wb') as f:
|
2010-12-10 02:22:12 +08:00
|
|
|
f.write(original_content)
|
2011-04-20 06:11:23 +08:00
|
|
|
if self.TraceOn():
|
|
|
|
with open('main.c', 'r') as f:
|
|
|
|
print "content restored to:", f.read()
|
2011-03-04 09:35:22 +08:00
|
|
|
# Touch the file just to be sure.
|
|
|
|
os.utime('main.c', None)
|
2011-04-20 06:11:23 +08:00
|
|
|
if self.TraceOn():
|
|
|
|
print "os.path.getmtime() after restore:", os.path.getmtime('main.c')
|
2011-03-04 09:35:22 +08:00
|
|
|
|
|
|
|
|
2010-12-10 02:22:12 +08:00
|
|
|
|
|
|
|
# Modify the source code file.
|
2015-01-16 06:53:44 +08:00
|
|
|
with open('main.c', 'wb') as f:
|
2011-03-04 09:35:22 +08:00
|
|
|
time.sleep(1)
|
2010-12-10 02:22:12 +08:00
|
|
|
f.write(new_content)
|
2011-04-20 06:11:23 +08:00
|
|
|
if self.TraceOn():
|
|
|
|
print "new content:", new_content
|
|
|
|
print "os.path.getmtime() after writing new content:", os.path.getmtime('main.c')
|
2010-12-10 02:22:12 +08:00
|
|
|
# Add teardown hook to restore the file to the original content.
|
|
|
|
self.addTearDownHook(restore_file)
|
|
|
|
|
|
|
|
# Display the source code again. We should see the updated line.
|
2013-02-06 08:35:33 +08:00
|
|
|
self.expect("source list -f main.c -l %d" % self.line, SOURCE_DISPLAYED_CORRECTLY,
|
2010-12-10 02:22:12 +08:00
|
|
|
substrs = ['Hello lldb'])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import atexit
|
|
|
|
lldb.SBDebugger.Initialize()
|
|
|
|
atexit.register(lambda: lldb.SBDebugger.Terminate())
|
|
|
|
unittest2.main()
|