blob: 2da1a0addb1f7046dcca22dfff7f0796ec31fc48 [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
Johnny Chen79e007d2010-11-01 21:52:20 +000011# 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 Chen06d73a02010-09-08 00:46:08 +000016class ConditionalBreakTestCase(TestBase):
17
18 mydir = "conditional_break"
19
20 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chen94de55d2010-09-10 18:21:10 +000021 def test_with_dsym_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.buildDsym()
24 self.do_conditional_break()
25
Johnny Chen94de55d2010-09-10 18:21:10 +000026 def test_with_dwarf_python(self):
Johnny Chen06d73a02010-09-08 00:46:08 +000027 """Exercise some thread and frame APIs to break if c() is called by a()."""
28 self.buildDwarf()
29 self.do_conditional_break()
30
Johnny Chen94de55d2010-09-10 18:21:10 +000031 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
32 def test_with_dsym_command(self):
33 """Simulate a user using lldb commands to break on c() if called from a()."""
34 self.buildDsym()
35 self.simulate_conditional_break_by_user()
36
37 def test_with_dwarf_command(self):
38 """Simulate a user using lldb commands to break on c() if called from a()."""
39 self.buildDwarf()
40 self.simulate_conditional_break_by_user()
41
Johnny Chen06d73a02010-09-08 00:46:08 +000042 def do_conditional_break(self):
43 """Exercise some thread and frame APIs to break if c() is called by a()."""
44 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen06d73a02010-09-08 00:46:08 +000045
Johnny Chenbfde8dc2010-10-08 22:51:03 +000046 target = self.dbg.CreateTarget(exe)
47 self.assertTrue(target.IsValid(), VALID_TARGET)
Johnny Chen06d73a02010-09-08 00:46:08 +000048
Johnny Chenbfde8dc2010-10-08 22:51:03 +000049 breakpoint = target.BreakpointCreateByName("c", exe)
50 self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
51
52 # Now launch the process, and do not stop at entry point.
53 rc = lldb.SBError()
54 self.process = target.Launch([''], [''], os.ctermid(), 0, False, rc)
55
56 self.assertTrue(rc.Success() and self.process.IsValid(), PROCESS_IS_VALID)
Johnny Chen06d73a02010-09-08 00:46:08 +000057
58 # The stop reason of the thread should be breakpoint.
Johnny Chenbfde8dc2010-10-08 22:51:03 +000059 self.assertTrue(self.process.GetState() == lldb.eStateStopped,
60 STOPPED_DUE_TO_BREAKPOINT)
Johnny Chen06d73a02010-09-08 00:46:08 +000061
Johnny Chenad5f98a2010-10-15 23:38:15 +000062 # Find the line number where a's parent frame function is c.
63 line = line_number('main.c',
Johnny Chenfb53d502010-11-03 22:00:28 +000064 "// Find the line number where c's parent frame is a here.")
Johnny Chenad5f98a2010-10-15 23:38:15 +000065
Johnny Chen06d73a02010-09-08 00:46:08 +000066 # Suppose we are only interested in the call scenario where c()'s
Johnny Chen28f5dd82010-10-07 18:49:04 +000067 # immediate caller is a() and we want to find out the value passed from
68 # a().
69 #
70 # The 10 in range(10) is just an arbitrary number, which means we would
71 # like to try for at most 10 times.
72 for j in range(10):
Johnny Chenbfde8dc2010-10-08 22:51:03 +000073 thread = self.process.GetThreadAtIndex(0)
Johnny Chen06d73a02010-09-08 00:46:08 +000074
75 if thread.GetNumFrames() >= 2:
76 frame0 = thread.GetFrameAtIndex(0)
77 name0 = frame0.GetFunction().GetName()
78 frame1 = thread.GetFrameAtIndex(1)
79 name1 = frame1.GetFunction().GetName()
Johnny Chenbfde8dc2010-10-08 22:51:03 +000080 #lldbutil.PrintStackTrace(thread)
Johnny Chen06d73a02010-09-08 00:46:08 +000081 self.assertTrue(name0 == "c", "Break on function c()")
82 if (name1 == "a"):
Johnny Chen06d73a02010-09-08 00:46:08 +000083 # By design, we know that a() calls c() only from main.c:27.
84 # In reality, similar logic can be used to find out the call
85 # site.
Johnny Chenad5f98a2010-10-15 23:38:15 +000086 self.assertTrue(frame1.GetLineEntry().GetLine() == line,
87 "Immediate caller a() at main.c:%d" % line)
Johnny Chenbfde8dc2010-10-08 22:51:03 +000088
89 # And the local variable 'val' should have a value of (int) 3.
90 val = frame1.LookupVar("val")
91 self.assertTrue(val.GetTypeName() == "int", "'val' has int type")
92 self.assertTrue(val.GetValue(frame1) == "3", "'val' has a value of 3")
Johnny Chen06d73a02010-09-08 00:46:08 +000093 break
94
Johnny Chenbfde8dc2010-10-08 22:51:03 +000095 self.process.Continue()
Johnny Chen06d73a02010-09-08 00:46:08 +000096
Johnny Chen94de55d2010-09-10 18:21:10 +000097 def simulate_conditional_break_by_user(self):
98 """Simulate a user using lldb commands to break on c() if called from a()."""
99
100 # Sourcing .lldb in the current working directory, which sets the main
101 # executable, sets the breakpoint on c(), and adds the callback for the
102 # breakpoint such that lldb only stops when the caller of c() is a().
103 # the "my" package that defines the date() function.
104 self.runCmd("command source .lldb")
105
106 self.runCmd("run", RUN_SUCCEEDED)
107
108 # The stop reason of the thread should be breakpoint.
109 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Johnny Chen0c724ef2010-10-18 15:44:42 +0000110 substrs = ['state is stopped', 'stop reason = breakpoint'])
Johnny Chen94de55d2010-09-10 18:21:10 +0000111
112 # The frame info for frame #0 points to a.out`c and its immediate caller
113 # (frame #1) points to a.out`a.
114
115 self.expect("frame info", "We should stop at c()",
116 substrs = ["a.out`c"])
117
118 # Select our parent frame as the current frame.
119 self.runCmd("frame select 1")
120 self.expect("frame info", "The immediate caller should be a()",
121 substrs = ["a.out`a"])
122
123
Johnny Chen06d73a02010-09-08 00:46:08 +0000124
125if __name__ == '__main__':
126 import atexit
127 lldb.SBDebugger.Initialize()
128 atexit.register(lambda: lldb.SBDebugger.Terminate())
129 unittest2.main()