blob: c2ed8ae94776be356946c0c9c8ef4027e296c9d2 [file] [log] [blame]
Johnny Chen06d73a02010-09-08 00:46:08 +00001"""
2Test conditionally break on a function and inspect its variables.
3"""
4
5import os, time
6import re
7import unittest2
Johnny Chenbfde8dc2010-10-08 22:51:03 +00008import lldb, lldbutil
Johnny Chen06d73a02010-09-08 00:46:08 +00009from lldbtest import *
10
11class ConditionalBreakTestCase(TestBase):
12
13 mydir = "conditional_break"
14
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chen94de55d2010-09-10 18:21:10 +000016 def test_with_dsym_python(self):
Johnny Chen06d73a02010-09-08 00:46:08 +000017 """Exercise some thread and frame APIs to break if c() is called by a()."""
18 self.buildDsym()
19 self.do_conditional_break()
20
Johnny Chen94de55d2010-09-10 18:21:10 +000021 def test_with_dwarf_python(self):
Johnny Chen06d73a02010-09-08 00:46:08 +000022 """Exercise some thread and frame APIs to break if c() is called by a()."""
23 self.buildDwarf()
24 self.do_conditional_break()
25
Johnny Chen94de55d2010-09-10 18:21:10 +000026 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
27 def test_with_dsym_command(self):
28 """Simulate a user using lldb commands to break on c() if called from a()."""
29 self.buildDsym()
30 self.simulate_conditional_break_by_user()
31
32 def test_with_dwarf_command(self):
33 """Simulate a user using lldb commands to break on c() if called from a()."""
34 self.buildDwarf()
35 self.simulate_conditional_break_by_user()
36
Johnny Chen06d73a02010-09-08 00:46:08 +000037 def do_conditional_break(self):
38 """Exercise some thread and frame APIs to break if c() is called by a()."""
39 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen06d73a02010-09-08 00:46:08 +000040
Johnny Chenbfde8dc2010-10-08 22:51:03 +000041 target = self.dbg.CreateTarget(exe)
42 self.assertTrue(target.IsValid(), VALID_TARGET)
Johnny Chen06d73a02010-09-08 00:46:08 +000043
Johnny Chenbfde8dc2010-10-08 22:51:03 +000044 breakpoint = target.BreakpointCreateByName("c", exe)
45 self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
46
47 # Now launch the process, and do not stop at entry point.
48 rc = lldb.SBError()
49 self.process = target.Launch([''], [''], os.ctermid(), 0, False, rc)
50
51 self.assertTrue(rc.Success() and self.process.IsValid(), PROCESS_IS_VALID)
Johnny Chen06d73a02010-09-08 00:46:08 +000052
53 # The stop reason of the thread should be breakpoint.
Johnny Chenbfde8dc2010-10-08 22:51:03 +000054 self.assertTrue(self.process.GetState() == lldb.eStateStopped,
55 STOPPED_DUE_TO_BREAKPOINT)
Johnny Chen06d73a02010-09-08 00:46:08 +000056
Johnny Chenad5f98a2010-10-15 23:38:15 +000057 # Find the line number where a's parent frame function is c.
58 line = line_number('main.c',
59 "// Find the line number where a's parent frame function is c here.")
60
Johnny Chen06d73a02010-09-08 00:46:08 +000061 # Suppose we are only interested in the call scenario where c()'s
Johnny Chen28f5dd82010-10-07 18:49:04 +000062 # immediate caller is a() and we want to find out the value passed from
63 # a().
64 #
65 # The 10 in range(10) is just an arbitrary number, which means we would
66 # like to try for at most 10 times.
67 for j in range(10):
Johnny Chenbfde8dc2010-10-08 22:51:03 +000068 thread = self.process.GetThreadAtIndex(0)
Johnny Chen06d73a02010-09-08 00:46:08 +000069
70 if thread.GetNumFrames() >= 2:
71 frame0 = thread.GetFrameAtIndex(0)
72 name0 = frame0.GetFunction().GetName()
73 frame1 = thread.GetFrameAtIndex(1)
74 name1 = frame1.GetFunction().GetName()
Johnny Chenbfde8dc2010-10-08 22:51:03 +000075 #lldbutil.PrintStackTrace(thread)
Johnny Chen06d73a02010-09-08 00:46:08 +000076 self.assertTrue(name0 == "c", "Break on function c()")
77 if (name1 == "a"):
Johnny Chen06d73a02010-09-08 00:46:08 +000078 # By design, we know that a() calls c() only from main.c:27.
79 # In reality, similar logic can be used to find out the call
80 # site.
Johnny Chenad5f98a2010-10-15 23:38:15 +000081 self.assertTrue(frame1.GetLineEntry().GetLine() == line,
82 "Immediate caller a() at main.c:%d" % line)
Johnny Chenbfde8dc2010-10-08 22:51:03 +000083
84 # And the local variable 'val' should have a value of (int) 3.
85 val = frame1.LookupVar("val")
86 self.assertTrue(val.GetTypeName() == "int", "'val' has int type")
87 self.assertTrue(val.GetValue(frame1) == "3", "'val' has a value of 3")
Johnny Chen06d73a02010-09-08 00:46:08 +000088 break
89
Johnny Chenbfde8dc2010-10-08 22:51:03 +000090 self.process.Continue()
Johnny Chen06d73a02010-09-08 00:46:08 +000091
Johnny Chen94de55d2010-09-10 18:21:10 +000092 def simulate_conditional_break_by_user(self):
93 """Simulate a user using lldb commands to break on c() if called from a()."""
94
95 # Sourcing .lldb in the current working directory, which sets the main
96 # executable, sets the breakpoint on c(), and adds the callback for the
97 # breakpoint such that lldb only stops when the caller of c() is a().
98 # the "my" package that defines the date() function.
99 self.runCmd("command source .lldb")
100
101 self.runCmd("run", RUN_SUCCEEDED)
102
103 # The stop reason of the thread should be breakpoint.
104 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
105 substrs = ['state is Stopped', 'stop reason = breakpoint'])
106
107 # The frame info for frame #0 points to a.out`c and its immediate caller
108 # (frame #1) points to a.out`a.
109
110 self.expect("frame info", "We should stop at c()",
111 substrs = ["a.out`c"])
112
113 # Select our parent frame as the current frame.
114 self.runCmd("frame select 1")
115 self.expect("frame info", "The immediate caller should be a()",
116 substrs = ["a.out`a"])
117
118
Johnny Chen06d73a02010-09-08 00:46:08 +0000119
120if __name__ == '__main__':
121 import atexit
122 lldb.SBDebugger.Initialize()
123 atexit.register(lambda: lldb.SBDebugger.Terminate())
124 unittest2.main()