blob: 59ac89e90ebda62cde511404abe03915cd16731e [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
8import lldb
9from lldbtest import *
10
11class ConditionalBreakTestCase(TestBase):
12
13 mydir = "conditional_break"
14
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16 def test_with_dsym(self):
17 """Exercise some thread and frame APIs to break if c() is called by a()."""
18 self.buildDsym()
19 self.do_conditional_break()
20
21 def test_with_dwarf(self):
22 """Exercise some thread and frame APIs to break if c() is called by a()."""
23 self.buildDwarf()
24 self.do_conditional_break()
25
26 def do_conditional_break(self):
27 """Exercise some thread and frame APIs to break if c() is called by a()."""
28 exe = os.path.join(os.getcwd(), "a.out")
29 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
30
31 # Break on c().
32 self.expect("breakpoint set -n c", BREAKPOINT_CREATED,
33 startstr = "Breakpoint created: 1: name = 'c', locations = 1")
34
35 self.runCmd("run", RUN_SUCCEEDED)
36
37 # The stop reason of the thread should be breakpoint.
38 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
39 substrs = ['state is Stopped', 'stop reason = breakpoint'])
40
41 # Suppose we are only interested in the call scenario where c()'s
42 # immediate caller is a() and we want to find out the value passed
43 # from a().
44 for j in range(3):
45 target = self.dbg.GetSelectedTarget()
46 process = target.GetProcess()
47 thread = process.GetThreadAtIndex(0)
48
49 if thread.GetNumFrames() >= 2:
50 frame0 = thread.GetFrameAtIndex(0)
51 name0 = frame0.GetFunction().GetName()
52 frame1 = thread.GetFrameAtIndex(1)
53 name1 = frame1.GetFunction().GetName()
54 self.assertTrue(name0 == "c", "Break on function c()")
55 if (name1 == "a"):
56 line = frame1.GetLineEntry().GetLine()
57 # By design, we know that a() calls c() only from main.c:27.
58 # In reality, similar logic can be used to find out the call
59 # site.
60 self.assertTrue(line == 27, "Immediate caller a() at main.c:27")
61 self.runCmd("thread backtrace")
62 self.runCmd("frame variable")
63 break
64
65 # This doesn't work?
66 #process.Continue()
67 self.runCmd("process continue")
68
69
70if __name__ == '__main__':
71 import atexit
72 lldb.SBDebugger.Initialize()
73 atexit.register(lambda: lldb.SBDebugger.Terminate())
74 unittest2.main()