blob: 2ce9fd90d1ea465cb629c8d4b69c485965461c51 [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 Chend5f66fc2010-12-23 01:12:19 +000067 #print "frame.FindValue('val', lldb.eValueTypeVariableArgument)", frame.FindValue('val', lldb.eValueTypeVariableArgument).GetValue(frame)
68 #print "frame.FindValue('ch', lldb.eValueTypeVariableArgument)", frame.FindValue('ch', lldb.eValueTypeVariableArgument).GetValue(frame)
69 #print "frame.EvaluateExpression('val'):", frame.EvaluateExpression('val').GetValue(frame)
70 #print "frame.EvaluateExpression('ch'):", frame.EvaluateExpression('ch').GetValue(frame)
71 name = frame.GetFunction().GetName()
72 if name == 'a':
73 callsOfA = callsOfA + 1
74
75 # We'll inspect only the arguments for the current frame:
76 #
77 # arguments => True
78 # locals => False
79 # statics => False
80 # in_scope_only => True
81 valList = frame.GetVariables(True, False, False, True)
82 argList = []
83 from lldbutil import lldb_iter
84 for val in lldb_iter(valList, 'GetSize', 'GetValueAtIndex'):
85 #self.DebugSBValue(frame, val)
86 argList.append("(%s)%s=%s" % (val.GetTypeName(),
87 val.GetName(),
88 val.GetValue(frame)))
89 print >> session, "%s(%s)" % (name, ", ".join(argList))
Jim Ingham8d543de2011-03-31 23:01:21 +000090
91 # Also check the generic pc & stack pointer. We can't test their absolute values,
92 # but they should be valid.
93 # It is kind of goofy that the register set is a value, and then we just have
94 # to magically know that element 0 is the GPR set...
95 gpr_reg_set = frame.GetRegisters().GetValueAtIndex(0)
96 pc_value = gpr_reg_set.GetChildMemberWithName("pc")
97 self.assertTrue (pc_value.IsValid(), "We should have a valid PC.")
98 self.assertTrue (int(pc_value.GetValue(frame), 0) == frame.GetPC(), "PC gotten as a value should equal frame's GetPC")
99 sp_value = gpr_reg_set.GetChildMemberWithName("sp")
100 self.assertTrue (sp_value.IsValid(), "We should have a valid Stack Pointer.")
101 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 +0000102
103 print >> session, "---"
104 process.Continue()
105
106 # At this point, the inferior process should have exited.
107 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
108
109 # Expect to find 'a' on the call stacks two times.
110 self.assertTrue(callsOfA == 2,
111 "Expect to find 'a' on the call stacks two times")
112 # By design, the 'a' call frame has the following arg vals:
113 # o a((int)val=1, (char)ch='A')
114 # o a((int)val=3, (char)ch='A')
Johnny Chenfbf1cfe2011-04-19 19:34:41 +0000115 if self.TraceOn():
116 print "Full stack traces when stopped on the breakpoint 'c':"
117 print session.getvalue()
Johnny Chend5f66fc2010-12-23 01:12:19 +0000118 self.expect(session.getvalue(), "Argugment values displayed correctly",
119 exe=False,
120 substrs = ["a((int)val=1, (char)ch='A')",
121 "a((int)val=3, (char)ch='A')"])
122
123
124if __name__ == '__main__':
125 import atexit
126 lldb.SBDebugger.Initialize()
127 atexit.register(lambda: lldb.SBDebugger.Terminate())
128 unittest2.main()