blob: 812d896962aed88b4114e6e495e7495dbce450f0 [file] [log] [blame]
Johnny Chen843f6892010-07-07 21:10:55 +00001"""
2Test that breakpoint by symbol name works correctly dlopen'ing a dynamic lib.
3"""
4
5import os, time
Johnny Chen55e1bdf2010-12-06 21:08:51 +00006import re
Johnny Chen75e28f92010-08-05 23:42:46 +00007import unittest2
Johnny Chen843f6892010-07-07 21:10:55 +00008import lldb
Johnny Chend85dae52010-08-09 23:44:24 +00009from lldbtest import *
Johnny Chen843f6892010-07-07 21:10:55 +000010
Johnny Chen1c42e862010-09-01 19:59:58 +000011class LoadUnloadTestCase(TestBase):
Johnny Chen843f6892010-07-07 21:10:55 +000012
13 mydir = "load_unload"
14
Johnny Chen55e1bdf2010-12-06 21:08:51 +000015 def setUp(self):
16 # Call super's setUp().
17 TestBase.setUp(self)
18 # Find the line number to break for main.cpp.
19 self.line = line_number('main.c',
20 '// Set break point at this line for test_lldb_process_load_and_unload_commands().')
21
22 def test_lldb_process_load_and_unload_commands(self):
23 """Test that lldb process load/unload command work correctly."""
24
25 # Invoke the default build rule.
26 self.buildDefault()
27
28 exe = os.path.join(os.getcwd(), "a.out")
29 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
30
31 # Break at main.c before the call to dlopen().
32 # Use lldb's process load command to load the dylib, instead.
33
34 self.expect("breakpoint set -f main.c -l %d" % self.line,
35 BREAKPOINT_CREATED,
36 startstr = "Breakpoint created: 1: file ='main.c', line = %d" %
37 self.line)
38
39 self.runCmd("run", RUN_SUCCEEDED)
40
41 # Make sure that a_function does not exist at this point.
42 self.expect("image lookup -n a_function", "a_function should not exist yet",
43 error=True, matching=False,
44 patterns = ["1 match found .* %s" % self.mydir])
45
46 # Use lldb 'process load' to load the dylib.
47 self.expect("process load liba.dylib", "liba.dylib loaded correctly",
48 patterns = ['Loading "liba.dylib".*ok',
49 'Image [0-9]+ loaded'])
50
51 # Search for and match the "Image ([0-9]+) loaded" pattern.
52 output = self.res.GetOutput()
53 pattern = re.compile("Image ([0-9]+) loaded")
54 for l in output.split(os.linesep):
55 #print "l:", l
56 match = pattern.search(l)
57 if match:
58 break
59 index = match.group(1)
60
61 # Now we should have an entry for a_function.
62 self.expect("image lookup -n a_function", "a_function should now exist",
63 patterns = ["1 match found .*%s" % self.mydir])
64
65 # Use lldb 'process unload' to unload the dylib.
66 self.expect("process unload %s" % index, "liba.dylib unloaded correctly",
67 patterns = ["Unloading .* with index %s.*ok" % index])
68
69 self.runCmd("process continue")
70
71
Johnny Chen30974392010-07-27 20:59:06 +000072 def test_load_unload(self):
Johnny Chen843f6892010-07-07 21:10:55 +000073 """Test breakpoint by name works correctly with dlopen'ing."""
Johnny Chen821a8c42010-09-03 23:52:15 +000074
75 # Invoke the default build rule.
76 self.buildDefault()
77
Johnny Chen843f6892010-07-07 21:10:55 +000078 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen029acae2010-08-20 21:03:09 +000079 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
Johnny Chen843f6892010-07-07 21:10:55 +000080
81 # Break by function name a_function (not yet loaded).
Johnny Chen029acae2010-08-20 21:03:09 +000082 self.expect("breakpoint set -n a_function", BREAKPOINT_CREATED,
83 startstr = "Breakpoint created: 1: name = 'a_function', locations = 0 (pending)")
Johnny Chen843f6892010-07-07 21:10:55 +000084
Johnny Chen1bb9f9a2010-08-27 23:47:36 +000085 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chen843f6892010-07-07 21:10:55 +000086
87 # The stop reason of the thread should be breakpoint and at a_function.
Johnny Chen029acae2010-08-20 21:03:09 +000088 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Johnny Chen8a87d522010-10-18 15:44:42 +000089 substrs = ['state is stopped',
Johnny Chen029acae2010-08-20 21:03:09 +000090 'a_function',
Johnny Chen029acae2010-08-20 21:03:09 +000091 'stop reason = breakpoint'])
Johnny Chen843f6892010-07-07 21:10:55 +000092
93 # The breakpoint should have a hit count of 1.
Johnny Chen029acae2010-08-20 21:03:09 +000094 self.expect("breakpoint list", BREAKPOINT_HIT_ONCE,
95 substrs = [' resolved, hit count = 1'])
Johnny Chen843f6892010-07-07 21:10:55 +000096
Johnny Chen14df3d42010-10-04 16:58:16 +000097 # Issue the 'contnue' command. We should stop agaian at a_function.
98 # The stop reason of the thread should be breakpoint and at a_function.
99 self.runCmd("continue")
Johnny Chenc958be42010-10-20 21:56:26 +0000100
101 # rdar://problem/8508987
102 # The a_function breakpoint should be encountered twice.
Johnny Chen14df3d42010-10-04 16:58:16 +0000103 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Johnny Chen8a87d522010-10-18 15:44:42 +0000104 substrs = ['state is stopped',
Johnny Chen14df3d42010-10-04 16:58:16 +0000105 'a_function',
106 'stop reason = breakpoint'])
107
108 # The breakpoint should have a hit count of 2.
109 self.expect("breakpoint list", BREAKPOINT_HIT_ONCE,
110 substrs = [' resolved, hit count = 2'])
Johnny Chen843f6892010-07-07 21:10:55 +0000111
112
113if __name__ == '__main__':
Johnny Chen88f83042010-08-05 21:23:45 +0000114 import atexit
Johnny Chen843f6892010-07-07 21:10:55 +0000115 lldb.SBDebugger.Initialize()
Johnny Chen88f83042010-08-05 21:23:45 +0000116 atexit.register(lambda: lldb.SBDebugger.Terminate())
Johnny Chen75e28f92010-08-05 23:42:46 +0000117 unittest2.main()