blob: e9b1ed199e8952ff87997850d668d3ff649c3f12 [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)
Johnny Chen4ebd0192011-05-24 18:22:45 +000034 self.assertTrue(target, VALID_TARGET)
Johnny Chend5f66fc2010-12-23 01:12:19 +000035
36 # Now create a breakpoint on main.c by name 'c'.
37 breakpoint = target.BreakpointCreateByName('c', 'a.out')
38 #print "breakpoint:", breakpoint
Johnny Chen4ebd0192011-05-24 18:22:45 +000039 self.assertTrue(breakpoint and
Johnny Chend5f66fc2010-12-23 01:12:19 +000040 breakpoint.GetNumLocations() == 1,
41 VALID_BREAKPOINT)
42
43 # Now launch the process, and do not stop at the entry point.
Johnny Chenfbf1cfe2011-04-19 19:34:41 +000044 process = target.LaunchSimple(None, None, os.getcwd())
Johnny Chend5f66fc2010-12-23 01:12:19 +000045
46 process = target.GetProcess()
47 self.assertTrue(process.GetState() == lldb.eStateStopped,
48 PROCESS_STOPPED)
49
50 # Keeps track of the number of times 'a' is called where it is within a
51 # depth of 3 of the 'c' leaf function.
52 callsOfA = 0
53
54 import StringIO
55 session = StringIO.StringIO()
56 while process.GetState() == lldb.eStateStopped:
57 thread = process.GetThreadAtIndex(0)
58 # Inspect at most 3 frames.
59 numFrames = min(3, thread.GetNumFrames())
60 for i in range(numFrames):
61 frame = thread.GetFrameAtIndex(i)
Johnny Chenfbf1cfe2011-04-19 19:34:41 +000062 if self.TraceOn():
63 print "frame:", frame
Johnny Chenc8134ce2011-05-13 23:42:44 +000064
Johnny Chend5f66fc2010-12-23 01:12:19 +000065 name = frame.GetFunction().GetName()
66 if name == 'a':
67 callsOfA = callsOfA + 1
68
69 # We'll inspect only the arguments for the current frame:
70 #
71 # arguments => True
72 # locals => False
73 # statics => False
74 # in_scope_only => True
75 valList = frame.GetVariables(True, False, False, True)
76 argList = []
Johnny Chene69c7482011-04-28 22:57:01 +000077 for val in valList:
Johnny Chend5f66fc2010-12-23 01:12:19 +000078 argList.append("(%s)%s=%s" % (val.GetTypeName(),
79 val.GetName(),
80 val.GetValue(frame)))
81 print >> session, "%s(%s)" % (name, ", ".join(argList))
Jim Ingham8d543de2011-03-31 23:01:21 +000082
83 # Also check the generic pc & stack pointer. We can't test their absolute values,
Johnny Chenc8134ce2011-05-13 23:42:44 +000084 # but they should be valid. Uses get_GPRs() from the lldbutil module.
85 gpr_reg_set = lldbutil.get_GPRs(frame)
Jim Ingham8d543de2011-03-31 23:01:21 +000086 pc_value = gpr_reg_set.GetChildMemberWithName("pc")
Johnny Chen4ebd0192011-05-24 18:22:45 +000087 self.assertTrue (pc_value, "We should have a valid PC.")
Jim Ingham8d543de2011-03-31 23:01:21 +000088 self.assertTrue (int(pc_value.GetValue(frame), 0) == frame.GetPC(), "PC gotten as a value should equal frame's GetPC")
89 sp_value = gpr_reg_set.GetChildMemberWithName("sp")
Johnny Chen4ebd0192011-05-24 18:22:45 +000090 self.assertTrue (sp_value, "We should have a valid Stack Pointer.")
Jim Ingham8d543de2011-03-31 23:01:21 +000091 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 +000092
93 print >> session, "---"
94 process.Continue()
95
96 # At this point, the inferior process should have exited.
97 self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
98
99 # Expect to find 'a' on the call stacks two times.
100 self.assertTrue(callsOfA == 2,
101 "Expect to find 'a' on the call stacks two times")
102 # By design, the 'a' call frame has the following arg vals:
103 # o a((int)val=1, (char)ch='A')
104 # o a((int)val=3, (char)ch='A')
Johnny Chenfbf1cfe2011-04-19 19:34:41 +0000105 if self.TraceOn():
106 print "Full stack traces when stopped on the breakpoint 'c':"
107 print session.getvalue()
Johnny Chend5f66fc2010-12-23 01:12:19 +0000108 self.expect(session.getvalue(), "Argugment values displayed correctly",
109 exe=False,
110 substrs = ["a((int)val=1, (char)ch='A')",
111 "a((int)val=3, (char)ch='A')"])
112
113
114if __name__ == '__main__':
115 import atexit
116 lldb.SBDebugger.Initialize()
117 atexit.register(lambda: lldb.SBDebugger.Terminate())
118 unittest2.main()