blob: 7e920133abc8cf3ec0d709e14c2356f072bf99ff [file] [log] [blame]
Johnny Chend5f66fc2010-12-23 01:12:19 +00001"""
2Use lldb Python SBFrame API to get the argument values of the call stacks.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb, lldbutil
9from lldbtest import *
10
11class FrameAPITestCase(TestBase):
12
13 mydir = os.path.join("python_api", "frame")
14
Johnny Chend5f66fc2010-12-23 01:12:19 +000015 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16 @python_api_test
17 def test_get_arg_vals_for_call_stack_with_dsym(self):
18 """Exercise SBFrame.GetVariables() API to get argument vals."""
19 self.buildDsym()
20 self.do_get_arg_vals()
21
Johnny Chend5f66fc2010-12-23 01:12:19 +000022 @python_api_test
23 def test_get_arg_vals_for_call_stack_with_dwarf(self):
24 """Exercise SBFrame.GetVariables() API to get argument vals."""
25 self.buildDwarf()
26 self.do_get_arg_vals()
27
28 def do_get_arg_vals(self):
29 """Get argument vals for the call stack when stopped on a breakpoint."""
30 exe = os.path.join(os.getcwd(), "a.out")
31
32 # Create a target by the debugger.
33 target = self.dbg.CreateTarget(exe)
34 self.assertTrue(target.IsValid(), VALID_TARGET)
35
36 # Now create a breakpoint on main.c by name 'c'.
37 breakpoint = target.BreakpointCreateByName('c', 'a.out')
38 #print "breakpoint:", breakpoint
39 self.assertTrue(breakpoint.IsValid() and
40 breakpoint.GetNumLocations() == 1,
41 VALID_BREAKPOINT)
42
43 # Now launch the process, and do not stop at the entry point.
44 # Note that we don't assign the process to self.process as in other test
45 # cases. We want the inferior to run till it exits and there's no need
46 # for the testing framework to kill the inferior upon tearDown().
Johnny Chenfbf1cfe2011-04-19 19:34:41 +000047 process = target.LaunchSimple(None, None, os.getcwd())
Johnny Chend5f66fc2010-12-23 01:12:19 +000048
49 process = target.GetProcess()
50 self.assertTrue(process.GetState() == lldb.eStateStopped,
51 PROCESS_STOPPED)
52
53 # Keeps track of the number of times 'a' is called where it is within a
54 # depth of 3 of the 'c' leaf function.
55 callsOfA = 0
56
57 import StringIO
58 session = StringIO.StringIO()
59 while process.GetState() == lldb.eStateStopped:
60 thread = process.GetThreadAtIndex(0)
61 # Inspect at most 3 frames.
62 numFrames = min(3, thread.GetNumFrames())
63 for i in range(numFrames):
64 frame = thread.GetFrameAtIndex(i)
Johnny Chenfbf1cfe2011-04-19 19:34:41 +000065 if self.TraceOn():
66 print "frame:", frame
Johnny Chenc8134ce2011-05-13 23:42:44 +000067
Johnny Chend5f66fc2010-12-23 01:12:19 +000068 name = frame.GetFunction().GetName()
69 if name == 'a':
70 callsOfA = callsOfA + 1
71
72 # We'll inspect only the arguments for the current frame:
73 #
74 # arguments => True
75 # locals => False
76 # statics => False
77 # in_scope_only => True
78 valList = frame.GetVariables(True, False, False, True)
79 argList = []
Johnny Chene69c7482011-04-28 22:57:01 +000080 for val in valList:
Johnny Chend5f66fc2010-12-23 01:12:19 +000081 argList.append("(%s)%s=%s" % (val.GetTypeName(),
82 val.GetName(),
83 val.GetValue(frame)))
84 print >> session, "%s(%s)" % (name, ", ".join(argList))
Jim Ingham8d543de2011-03-31 23:01:21 +000085
86 # Also check the generic pc & stack pointer. We can't test their absolute values,
Johnny Chenc8134ce2011-05-13 23:42:44 +000087 # but they should be valid. Uses get_GPRs() from the lldbutil module.
88 gpr_reg_set = lldbutil.get_GPRs(frame)
Jim Ingham8d543de2011-03-31 23:01:21 +000089 pc_value = gpr_reg_set.GetChildMemberWithName("pc")
90 self.assertTrue (pc_value.IsValid(), "We should have a valid PC.")
91 self.assertTrue (int(pc_value.GetValue(frame), 0) == frame.GetPC(), "PC gotten as a value should equal frame's GetPC")
92 sp_value = gpr_reg_set.GetChildMemberWithName("sp")
93 self.assertTrue (sp_value.IsValid(), "We should have a valid Stack Pointer.")
94 self.assertTrue (int(sp_value.GetValue(frame), 0) == frame.GetSP(), "SP gotten as a value should equal frame's GetSP")
Johnny Chend5f66fc2010-12-23 01:12:19 +000095
96 print >> session, "---"
97 process.Continue()
98
99 # At this point, the inferior process should have exited.
100 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
101
102 # Expect to find 'a' on the call stacks two times.
103 self.assertTrue(callsOfA == 2,
104 "Expect to find 'a' on the call stacks two times")
105 # By design, the 'a' call frame has the following arg vals:
106 # o a((int)val=1, (char)ch='A')
107 # o a((int)val=3, (char)ch='A')
Johnny Chenfbf1cfe2011-04-19 19:34:41 +0000108 if self.TraceOn():
109 print "Full stack traces when stopped on the breakpoint 'c':"
110 print session.getvalue()
Johnny Chend5f66fc2010-12-23 01:12:19 +0000111 self.expect(session.getvalue(), "Argugment values displayed correctly",
112 exe=False,
113 substrs = ["a((int)val=1, (char)ch='A')",
114 "a((int)val=3, (char)ch='A')"])
115
116
117if __name__ == '__main__':
118 import atexit
119 lldb.SBDebugger.Initialize()
120 atexit.register(lambda: lldb.SBDebugger.Terminate())
121 unittest2.main()