| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test conditionally break on a function and inspect its variables. |
| 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import re |
| 7 | import unittest2 |
| Johnny Chen | bfde8dc | 2010-10-08 22:51:03 +0000 | [diff] [blame] | 8 | import lldb, lldbutil |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 9 | from lldbtest import * |
| 10 | |
| Johnny Chen | 79e007d | 2010-11-01 21:52:20 +0000 | [diff] [blame] | 11 | # rdar://problem/8532131 |
| 12 | # lldb not able to digest the clang-generated debug info correctly with respect to function name |
| 13 | # |
| 14 | # This class currently fails for clang as well as llvm-gcc. |
| 15 | |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 16 | class ConditionalBreakTestCase(TestBase): |
| 17 | |
| 18 | mydir = "conditional_break" |
| 19 | |
| 20 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| Johnny Chen | a47d7cb | 2010-12-10 01:21:27 +0000 | [diff] [blame] | 21 | @python_api_test |
| Johnny Chen | 94de55d | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 22 | def test_with_dsym_python(self): |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 23 | """Exercise some thread and frame APIs to break if c() is called by a().""" |
| 24 | self.buildDsym() |
| 25 | self.do_conditional_break() |
| 26 | |
| Johnny Chen | a47d7cb | 2010-12-10 01:21:27 +0000 | [diff] [blame] | 27 | @python_api_test |
| Johnny Chen | 94de55d | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 28 | def test_with_dwarf_python(self): |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 29 | """Exercise some thread and frame APIs to break if c() is called by a().""" |
| 30 | self.buildDwarf() |
| 31 | self.do_conditional_break() |
| 32 | |
| Johnny Chen | 94de55d | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 33 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 34 | def test_with_dsym_command(self): |
| 35 | """Simulate a user using lldb commands to break on c() if called from a().""" |
| 36 | self.buildDsym() |
| 37 | self.simulate_conditional_break_by_user() |
| 38 | |
| 39 | def test_with_dwarf_command(self): |
| 40 | """Simulate a user using lldb commands to break on c() if called from a().""" |
| 41 | self.buildDwarf() |
| 42 | self.simulate_conditional_break_by_user() |
| 43 | |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 44 | def do_conditional_break(self): |
| 45 | """Exercise some thread and frame APIs to break if c() is called by a().""" |
| 46 | exe = os.path.join(os.getcwd(), "a.out") |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 47 | |
| Johnny Chen | bfde8dc | 2010-10-08 22:51:03 +0000 | [diff] [blame] | 48 | target = self.dbg.CreateTarget(exe) |
| 49 | self.assertTrue(target.IsValid(), VALID_TARGET) |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 50 | |
| Johnny Chen | bfde8dc | 2010-10-08 22:51:03 +0000 | [diff] [blame] | 51 | breakpoint = target.BreakpointCreateByName("c", exe) |
| 52 | self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT) |
| 53 | |
| 54 | # Now launch the process, and do not stop at entry point. |
| Greg Clayton | 6f907e6 | 2011-01-23 17:46:22 +0000 | [diff] [blame] | 55 | error = lldb.SBError() |
| Johnny Chen | d762ff1 | 2011-02-03 23:15:53 +0000 | [diff] [blame] | 56 | self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error) |
| Johnny Chen | bfde8dc | 2010-10-08 22:51:03 +0000 | [diff] [blame] | 57 | |
| Greg Clayton | 6f907e6 | 2011-01-23 17:46:22 +0000 | [diff] [blame] | 58 | self.assertTrue(error.Success() and self.process.IsValid(), PROCESS_IS_VALID) |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 59 | |
| 60 | # The stop reason of the thread should be breakpoint. |
| Johnny Chen | bfde8dc | 2010-10-08 22:51:03 +0000 | [diff] [blame] | 61 | self.assertTrue(self.process.GetState() == lldb.eStateStopped, |
| 62 | STOPPED_DUE_TO_BREAKPOINT) |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 63 | |
| Johnny Chen | ad5f98a | 2010-10-15 23:38:15 +0000 | [diff] [blame] | 64 | # Find the line number where a's parent frame function is c. |
| 65 | line = line_number('main.c', |
| Johnny Chen | fb53d50 | 2010-11-03 22:00:28 +0000 | [diff] [blame] | 66 | "// Find the line number where c's parent frame is a here.") |
| Johnny Chen | ad5f98a | 2010-10-15 23:38:15 +0000 | [diff] [blame] | 67 | |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 68 | # Suppose we are only interested in the call scenario where c()'s |
| Johnny Chen | 28f5dd8 | 2010-10-07 18:49:04 +0000 | [diff] [blame] | 69 | # immediate caller is a() and we want to find out the value passed from |
| 70 | # a(). |
| 71 | # |
| 72 | # The 10 in range(10) is just an arbitrary number, which means we would |
| 73 | # like to try for at most 10 times. |
| 74 | for j in range(10): |
| Johnny Chen | bfde8dc | 2010-10-08 22:51:03 +0000 | [diff] [blame] | 75 | thread = self.process.GetThreadAtIndex(0) |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 76 | |
| 77 | if thread.GetNumFrames() >= 2: |
| 78 | frame0 = thread.GetFrameAtIndex(0) |
| 79 | name0 = frame0.GetFunction().GetName() |
| 80 | frame1 = thread.GetFrameAtIndex(1) |
| 81 | name1 = frame1.GetFunction().GetName() |
| Johnny Chen | d0fef81 | 2011-04-25 23:38:13 +0000 | [diff] [blame^] | 82 | #lldbutil.print_stacktrace(thread) |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 83 | self.assertTrue(name0 == "c", "Break on function c()") |
| 84 | if (name1 == "a"): |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 85 | # By design, we know that a() calls c() only from main.c:27. |
| 86 | # In reality, similar logic can be used to find out the call |
| 87 | # site. |
| Johnny Chen | ad5f98a | 2010-10-15 23:38:15 +0000 | [diff] [blame] | 88 | self.assertTrue(frame1.GetLineEntry().GetLine() == line, |
| 89 | "Immediate caller a() at main.c:%d" % line) |
| Johnny Chen | bfde8dc | 2010-10-08 22:51:03 +0000 | [diff] [blame] | 90 | |
| 91 | # And the local variable 'val' should have a value of (int) 3. |
| Johnny Chen | 94f928b | 2010-12-14 18:59:15 +0000 | [diff] [blame] | 92 | val = frame1.FindVariable("val") |
| Johnny Chen | bfde8dc | 2010-10-08 22:51:03 +0000 | [diff] [blame] | 93 | self.assertTrue(val.GetTypeName() == "int", "'val' has int type") |
| 94 | self.assertTrue(val.GetValue(frame1) == "3", "'val' has a value of 3") |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 95 | break |
| 96 | |
| Johnny Chen | bfde8dc | 2010-10-08 22:51:03 +0000 | [diff] [blame] | 97 | self.process.Continue() |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 98 | |
| Johnny Chen | 94de55d | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 99 | def simulate_conditional_break_by_user(self): |
| 100 | """Simulate a user using lldb commands to break on c() if called from a().""" |
| 101 | |
| 102 | # Sourcing .lldb in the current working directory, which sets the main |
| 103 | # executable, sets the breakpoint on c(), and adds the callback for the |
| 104 | # breakpoint such that lldb only stops when the caller of c() is a(). |
| 105 | # the "my" package that defines the date() function. |
| 106 | self.runCmd("command source .lldb") |
| 107 | |
| 108 | self.runCmd("run", RUN_SUCCEEDED) |
| 109 | |
| 110 | # The stop reason of the thread should be breakpoint. |
| 111 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
| Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 112 | substrs = ['stopped', 'stop reason = breakpoint']) |
| Johnny Chen | 94de55d | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 113 | |
| 114 | # The frame info for frame #0 points to a.out`c and its immediate caller |
| 115 | # (frame #1) points to a.out`a. |
| 116 | |
| 117 | self.expect("frame info", "We should stop at c()", |
| 118 | substrs = ["a.out`c"]) |
| 119 | |
| 120 | # Select our parent frame as the current frame. |
| 121 | self.runCmd("frame select 1") |
| 122 | self.expect("frame info", "The immediate caller should be a()", |
| 123 | substrs = ["a.out`a"]) |
| 124 | |
| 125 | |
| Johnny Chen | 06d73a0 | 2010-09-08 00:46:08 +0000 | [diff] [blame] | 126 | |
| 127 | if __name__ == '__main__': |
| 128 | import atexit |
| 129 | lldb.SBDebugger.Initialize() |
| 130 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 131 | unittest2.main() |