blob: b3a8bbb215d4896121d4b60e90889be497a6bc68 [file] [log] [blame]
Johnny Chened401982011-03-03 19:14:00 +00001"""
2Test retrieval of SBAddress from function/symbol, disassembly, and SBAddress APIs.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb, lldbutil
9from lldbtest import *
10
11class DisasmAPITestCase(TestBase):
12
13 mydir = os.path.join("python_api", "function_symbol")
14
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16 @python_api_test
17 def test_with_dsym(self):
18 """Exercise getting SBAddress objects, disassembly, and SBAddress APIs."""
19 self.buildDsym()
20 self.disasm_and_address_api()
21
22 @python_api_test
23 def test_with_dwarf(self):
24 """Exercise getting SBAddress objects, disassembly, and SBAddress APIs."""
25 self.buildDwarf()
26 self.disasm_and_address_api()
27
28 def setUp(self):
29 # Call super's setUp().
30 TestBase.setUp(self)
31 # Find the line number to of function 'c'.
32 self.line1 = line_number('main.c', '// Find the line number for breakpoint 1 here.')
33 self.line2 = line_number('main.c', '// Find the line number for breakpoint 2 here.')
34
35 def disasm_and_address_api(self):
36 """Exercise getting SBAddress objects, disassembly, and SBAddress APIs."""
37 exe = os.path.join(os.getcwd(), "a.out")
38
39 # Create a target by the debugger.
40 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000041 self.assertTrue(target, VALID_TARGET)
Johnny Chened401982011-03-03 19:14:00 +000042
43 # Now create the two breakpoints inside function 'a'.
44 breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1)
45 breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2)
46 #print "breakpoint1:", breakpoint1
47 #print "breakpoint2:", breakpoint2
Johnny Chen4ebd0192011-05-24 18:22:45 +000048 self.assertTrue(breakpoint1 and
Johnny Chened401982011-03-03 19:14:00 +000049 breakpoint1.GetNumLocations() == 1,
50 VALID_BREAKPOINT)
Johnny Chen4ebd0192011-05-24 18:22:45 +000051 self.assertTrue(breakpoint2 and
Johnny Chened401982011-03-03 19:14:00 +000052 breakpoint2.GetNumLocations() == 1,
53 VALID_BREAKPOINT)
54
55 # Now launch the process, and do not stop at entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +000056 process = target.LaunchSimple(None, None, os.getcwd())
57 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chened401982011-03-03 19:14:00 +000058
59 # Frame #0 should be on self.line1.
Johnny Chen5a0bee72011-06-15 22:14:12 +000060 self.assertTrue(process.GetState() == lldb.eStateStopped)
61 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +000062 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
63 frame0 = thread.GetFrameAtIndex(0)
64 lineEntry = frame0.GetLineEntry()
65 self.assertTrue(lineEntry.GetLine() == self.line1)
66
67 address1 = lineEntry.GetStartAddress()
68 #print "address1:", address1
69
70 # Now call SBTarget.ResolveSymbolContextForAddress() with address1.
71 context1 = target.ResolveSymbolContextForAddress(address1, lldb.eSymbolContextEverything)
72
Johnny Chen4ebd0192011-05-24 18:22:45 +000073 self.assertTrue(context1)
Johnny Chen9efcb0e2011-04-19 19:44:26 +000074 if self.TraceOn():
75 print "context1:", context1
Johnny Chened401982011-03-03 19:14:00 +000076
77 # Continue the inferior, the breakpoint 2 should be hit.
Johnny Chen5a0bee72011-06-15 22:14:12 +000078 process.Continue()
79 self.assertTrue(process.GetState() == lldb.eStateStopped)
80 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +000081 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
82 frame0 = thread.GetFrameAtIndex(0)
83 lineEntry = frame0.GetLineEntry()
84 self.assertTrue(lineEntry.GetLine() == self.line2)
85
86 # Verify that the symbol and the function has the same address range per function 'a'.
87 symbol = context1.GetSymbol()
88 function = frame0.GetFunction()
Johnny Chen4ebd0192011-05-24 18:22:45 +000089 self.assertTrue(symbol and function)
Johnny Chened401982011-03-03 19:14:00 +000090
Johnny Chen9efcb0e2011-04-19 19:44:26 +000091 disasm_output = lldbutil.disassemble(target, symbol)
92 if self.TraceOn():
93 print "symbol:", symbol
94 print "disassembly=>\n", disasm_output
Johnny Chened401982011-03-03 19:14:00 +000095
Johnny Chen9efcb0e2011-04-19 19:44:26 +000096 disasm_output = lldbutil.disassemble(target, function)
97 if self.TraceOn():
98 print "function:", function
99 print "disassembly=>\n", disasm_output
Johnny Chened401982011-03-03 19:14:00 +0000100
101 sa1 = symbol.GetStartAddress()
102 #print "sa1:", sa1
Johnny Chen4b6fed42011-06-09 22:09:52 +0000103 #print "sa1.GetFileAddress():", hex(sa1.GetFileAddress())
Johnny Chened401982011-03-03 19:14:00 +0000104 #ea1 = symbol.GetEndAddress()
105 #print "ea1:", ea1
106 sa2 = function.GetStartAddress()
107 #print "sa2:", sa2
Johnny Chen4b6fed42011-06-09 22:09:52 +0000108 #print "sa2.GetFileAddress():", hex(sa2.GetFileAddress())
Johnny Chened401982011-03-03 19:14:00 +0000109 #ea2 = function.GetEndAddress()
110 #print "ea2:", ea2
Johnny Chene2b5cfd2011-06-09 22:04:56 +0000111 self.assertTrue(sa1 and sa2 and sa1 == sa2,
112 "The two starting addresses should be the same")
Johnny Chened401982011-03-03 19:14:00 +0000113
Johnny Chen9ae98202011-04-23 00:34:56 +0000114 from lldbutil import get_description
115 desc1 = get_description(sa1)
116 desc2 = get_description(sa2)
117 self.assertTrue(desc1 and desc2 and desc1 == desc2,
Johnny Chene2b5cfd2011-06-09 22:04:56 +0000118 "SBAddress.GetDescription() API of sa1 and sa2 should return the same string")
Johnny Chened401982011-03-03 19:14:00 +0000119
120
121if __name__ == '__main__':
122 import atexit
123 lldb.SBDebugger.Initialize()
124 atexit.register(lambda: lldb.SBDebugger.Terminate())
125 unittest2.main()