"""
Test that breakpoint by symbol name works correctly with dynamic libs.
"""
import os, time
import re
import unittest2
import lldb
from lldbtest import *
class LoadUnloadTestCase(TestBase):
mydir = os.path.join("functionalities", "load_unload")
def setUp(self):
TestBase.setUp(self)
self.line = line_number('main.c',
'// Set break point at this line for test_lldb_process_load_and_unload_commands().')
self.line_d_function = line_number('d.c',
'// Find this line number within d_dunction().')
def test_modules_search_paths(self):
"""Test target modules list after loading a different copy of the library libd.dylib, and verifies that it works with 'target modules search-paths add'."""
self.buildDefault()
if sys.platform.startswith("darwin"):
dylibName = 'libd.dylib'
dylibPath = 'DYLD_LIBRARY_PATH'
new_dir = os.path.join(os.getcwd(), "hidden")
old_dylib = os.path.join(os.getcwd(), dylibName)
new_dylib = os.path.join(new_dir, dylibName)
exe = os.path.join(os.getcwd(), "a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
self.expect("target modules list",
substrs = [old_dylib])
self.runCmd("target modules search-paths add %s %s" % (os.getcwd(), new_dir))
self.addTearDownHook(lambda: self.runCmd("target modules search-paths clear"))
self.expect("target modules search-paths list",
substrs = [os.getcwd(), new_dir])
self.expect("target modules search-paths query %s" % os.getcwd(), "Image search path successfully transformed",
substrs = [new_dir])
os.remove(old_dylib)
env_cmd_string = "settings set target.env-vars " + dylibPath + "=" + new_dir
if self.TraceOn():
print "Set environment to: ", env_cmd_string
self.runCmd(env_cmd_string)
self.runCmd("settings show target.env-vars")
remove_dyld_path_cmd = "settings remove target.env-vars " + dylibPath
self.addTearDownHook(lambda: self.runCmd(remove_dyld_path_cmd))
self.runCmd("run")
self.expect("target modules list", "LLDB successfully locates the relocated dynamic library",
substrs = [new_dylib])
def test_dyld_library_path(self):
"""Test DYLD_LIBRARY_PATH after moving libd.dylib, which defines d_function, somewhere else."""
self.buildDefault()
exe = os.path.join(os.getcwd(), "a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
if sys.platform.startswith("darwin"):
dylibName = 'libd.dylib'
dsymName = 'libd.dylib.dSYM'
dylibPath = 'DYLD_LIBRARY_PATH'
special_dir = "hidden"
new_dir = os.path.join(os.getcwd(), special_dir)
old_dylib = os.path.join(os.getcwd(), dylibName)
new_dylib = os.path.join(new_dir, dylibName)
old_dSYM = os.path.join(os.getcwd(), dsymName)
new_dSYM = os.path.join(new_dir, dsymName)
env_cmd_string = "settings set target.env-vars " + dylibPath + "=" + new_dir
if self.TraceOn():
print "Set environment to: ", env_cmd_string
self.runCmd(env_cmd_string)
self.runCmd("settings show target.env-vars")
remove_dyld_path_cmd = "settings remove target.env-vars " + dylibPath
self.addTearDownHook(lambda: self.runCmd(remove_dyld_path_cmd))
self.expect("breakpoint set -f d.c -l %d" % self.line_d_function,
BREAKPOINT_CREATED,
startstr = "Breakpoint created: 1: file ='d.c', line = %d" %
self.line_d_function)
self.expect("target modules list",
substrs = [os.path.basename(old_dylib)],
matching=True)
self.runCmd("run")
self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
patterns = ["frame #0.*d_function.*at d.c:%d" % self.line_d_function])
self.expect("target modules list",
substrs = [special_dir, os.path.basename(new_dylib)])
def test_lldb_process_load_and_unload_commands(self):
"""Test that lldb process load/unload command work correctly."""
self.buildDefault()
exe = os.path.join(os.getcwd(), "a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
self.expect("breakpoint set -f main.c -l %d" % self.line,
BREAKPOINT_CREATED,
startstr = "Breakpoint created: 1: file ='main.c', line = %d" %
self.line)
self.runCmd("run", RUN_SUCCEEDED)
self.expect("image lookup -n a_function", "a_function should not exist yet",
error=True, matching=False,
patterns = ["1 match found .* %s" % self.mydir])
self.expect("process load liba.dylib", "liba.dylib loaded correctly",
patterns = ['Loading "liba.dylib".*ok',
'Image [0-9]+ loaded'])
output = self.res.GetOutput()
pattern = re.compile("Image ([0-9]+) loaded")
for l in output.split(os.linesep):
match = pattern.search(l)
if match:
break
index = match.group(1)
self.expect("image lookup -n a_function", "a_function should now exist",
patterns = ["1 match found .*%s" % self.mydir])
self.expect("process unload %s" % index, "liba.dylib unloaded correctly",
patterns = ["Unloading .* with index %s.*ok" % index])
self.runCmd("process continue")
def test_load_unload(self):
"""Test breakpoint by name works correctly with dlopen'ing."""
self.buildDefault()
exe = os.path.join(os.getcwd(), "a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
self.expect("breakpoint set -n a_function", BREAKPOINT_CREATED,
startstr = "Breakpoint created: 1: name = 'a_function', locations = 0 (pending)")
self.runCmd("run", RUN_SUCCEEDED)
self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
substrs = ['stopped',
'a_function',
'stop reason = breakpoint'])
self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
substrs = [' resolved, hit count = 1'])
self.runCmd("continue")
self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
substrs = ['stopped',
'a_function',
'stop reason = breakpoint'])
self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
substrs = [' resolved, hit count = 2'])
def test_step_over_load (self):
"""Test stepping over code that loads a shared library works correctly."""
self.buildDefault()
exe = os.path.join(os.getcwd(), "a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
self.expect("breakpoint set -f main.c -l %d"%(self.line), BREAKPOINT_CREATED,
substrs = ['Breakpoint created:',
"file ='main.c', line = %d, locations = 1"%(self.line)])
self.runCmd("run", RUN_SUCCEEDED)
self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
substrs = ['stopped',
'stop reason = breakpoint'])
self.runCmd("thread step-over", "Stepping over function that loads library")
self.expect("thread list", "step over succeeded.",
substrs = ['stopped',
'stop reason = step over'])
if __name__ == '__main__':
import atexit
lldb.SBDebugger.Initialize()
atexit.register(lambda: lldb.SBDebugger.Terminate())
unittest2.main()