Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test that breakpoint by symbol name works correctly dlopen'ing a dynamic lib. |
| 3 | """ |
| 4 | |
| 5 | import os, time |
Johnny Chen | 55e1bdf | 2010-12-06 21:08:51 +0000 | [diff] [blame^] | 6 | import re |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 7 | import unittest2 |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 8 | import lldb |
Johnny Chen | d85dae5 | 2010-08-09 23:44:24 +0000 | [diff] [blame] | 9 | from lldbtest import * |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 10 | |
Johnny Chen | 1c42e86 | 2010-09-01 19:59:58 +0000 | [diff] [blame] | 11 | class LoadUnloadTestCase(TestBase): |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 12 | |
| 13 | mydir = "load_unload" |
| 14 | |
Johnny Chen | 55e1bdf | 2010-12-06 21:08:51 +0000 | [diff] [blame^] | 15 | 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 Chen | 3097439 | 2010-07-27 20:59:06 +0000 | [diff] [blame] | 72 | def test_load_unload(self): |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 73 | """Test breakpoint by name works correctly with dlopen'ing.""" |
Johnny Chen | 821a8c4 | 2010-09-03 23:52:15 +0000 | [diff] [blame] | 74 | |
| 75 | # Invoke the default build rule. |
| 76 | self.buildDefault() |
| 77 | |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 78 | exe = os.path.join(os.getcwd(), "a.out") |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 79 | self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 80 | |
| 81 | # Break by function name a_function (not yet loaded). |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 82 | self.expect("breakpoint set -n a_function", BREAKPOINT_CREATED, |
| 83 | startstr = "Breakpoint created: 1: name = 'a_function', locations = 0 (pending)") |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 84 | |
Johnny Chen | 1bb9f9a | 2010-08-27 23:47:36 +0000 | [diff] [blame] | 85 | self.runCmd("run", RUN_SUCCEEDED) |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 86 | |
| 87 | # The stop reason of the thread should be breakpoint and at a_function. |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 88 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
Johnny Chen | 8a87d52 | 2010-10-18 15:44:42 +0000 | [diff] [blame] | 89 | substrs = ['state is stopped', |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 90 | 'a_function', |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 91 | 'stop reason = breakpoint']) |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 92 | |
| 93 | # The breakpoint should have a hit count of 1. |
Johnny Chen | 029acae | 2010-08-20 21:03:09 +0000 | [diff] [blame] | 94 | self.expect("breakpoint list", BREAKPOINT_HIT_ONCE, |
| 95 | substrs = [' resolved, hit count = 1']) |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 96 | |
Johnny Chen | 14df3d4 | 2010-10-04 16:58:16 +0000 | [diff] [blame] | 97 | # 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 Chen | c958be4 | 2010-10-20 21:56:26 +0000 | [diff] [blame] | 100 | |
| 101 | # rdar://problem/8508987 |
| 102 | # The a_function breakpoint should be encountered twice. |
Johnny Chen | 14df3d4 | 2010-10-04 16:58:16 +0000 | [diff] [blame] | 103 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
Johnny Chen | 8a87d52 | 2010-10-18 15:44:42 +0000 | [diff] [blame] | 104 | substrs = ['state is stopped', |
Johnny Chen | 14df3d4 | 2010-10-04 16:58:16 +0000 | [diff] [blame] | 105 | '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 Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 111 | |
| 112 | |
| 113 | if __name__ == '__main__': |
Johnny Chen | 88f8304 | 2010-08-05 21:23:45 +0000 | [diff] [blame] | 114 | import atexit |
Johnny Chen | 843f689 | 2010-07-07 21:10:55 +0000 | [diff] [blame] | 115 | lldb.SBDebugger.Initialize() |
Johnny Chen | 88f8304 | 2010-08-05 21:23:45 +0000 | [diff] [blame] | 116 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
Johnny Chen | 75e28f9 | 2010-08-05 23:42:46 +0000 | [diff] [blame] | 117 | unittest2.main() |