blob: c8e079fd299d953a732ad5805a58e5db545c2d3a [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
Greg Clayton4570d3e2013-12-10 23:19:29 +000013 mydir = TestBase.compute_mydir(__file__)
Johnny Chened401982011-03-03 19:14:00 +000014
Johnny Chened401982011-03-03 19:14:00 +000015 def setUp(self):
16 # Call super's setUp().
17 TestBase.setUp(self)
18 # Find the line number to of function 'c'.
19 self.line1 = line_number('main.c', '// Find the line number for breakpoint 1 here.')
20 self.line2 = line_number('main.c', '// Find the line number for breakpoint 2 here.')
21
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000022 @python_api_test
23 def test(self):
Johnny Chened401982011-03-03 19:14:00 +000024 """Exercise getting SBAddress objects, disassembly, and SBAddress APIs."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000025 self.build()
Johnny Chened401982011-03-03 19:14:00 +000026 exe = os.path.join(os.getcwd(), "a.out")
27
28 # Create a target by the debugger.
29 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000030 self.assertTrue(target, VALID_TARGET)
Johnny Chened401982011-03-03 19:14:00 +000031
32 # Now create the two breakpoints inside function 'a'.
33 breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1)
34 breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2)
35 #print "breakpoint1:", breakpoint1
36 #print "breakpoint2:", breakpoint2
Johnny Chen4ebd0192011-05-24 18:22:45 +000037 self.assertTrue(breakpoint1 and
Johnny Chened401982011-03-03 19:14:00 +000038 breakpoint1.GetNumLocations() == 1,
39 VALID_BREAKPOINT)
Johnny Chen4ebd0192011-05-24 18:22:45 +000040 self.assertTrue(breakpoint2 and
Johnny Chened401982011-03-03 19:14:00 +000041 breakpoint2.GetNumLocations() == 1,
42 VALID_BREAKPOINT)
43
44 # Now launch the process, and do not stop at entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000045 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen5a0bee72011-06-15 22:14:12 +000046 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chened401982011-03-03 19:14:00 +000047
48 # Frame #0 should be on self.line1.
Johnny Chen5a0bee72011-06-15 22:14:12 +000049 self.assertTrue(process.GetState() == lldb.eStateStopped)
50 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +000051 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
Johnny Chened401982011-03-03 19:14:00 +000052 frame0 = thread.GetFrameAtIndex(0)
53 lineEntry = frame0.GetLineEntry()
54 self.assertTrue(lineEntry.GetLine() == self.line1)
55
56 address1 = lineEntry.GetStartAddress()
57 #print "address1:", address1
58
59 # Now call SBTarget.ResolveSymbolContextForAddress() with address1.
60 context1 = target.ResolveSymbolContextForAddress(address1, lldb.eSymbolContextEverything)
61
Johnny Chen4ebd0192011-05-24 18:22:45 +000062 self.assertTrue(context1)
Johnny Chen9efcb0e2011-04-19 19:44:26 +000063 if self.TraceOn():
64 print "context1:", context1
Johnny Chened401982011-03-03 19:14:00 +000065
66 # Continue the inferior, the breakpoint 2 should be hit.
Johnny Chen5a0bee72011-06-15 22:14:12 +000067 process.Continue()
68 self.assertTrue(process.GetState() == lldb.eStateStopped)
69 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +000070 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
Johnny Chened401982011-03-03 19:14:00 +000071 frame0 = thread.GetFrameAtIndex(0)
72 lineEntry = frame0.GetLineEntry()
73 self.assertTrue(lineEntry.GetLine() == self.line2)
74
75 # Verify that the symbol and the function has the same address range per function 'a'.
76 symbol = context1.GetSymbol()
77 function = frame0.GetFunction()
Johnny Chen4ebd0192011-05-24 18:22:45 +000078 self.assertTrue(symbol and function)
Johnny Chened401982011-03-03 19:14:00 +000079
Johnny Chen9efcb0e2011-04-19 19:44:26 +000080 disasm_output = lldbutil.disassemble(target, symbol)
81 if self.TraceOn():
82 print "symbol:", symbol
83 print "disassembly=>\n", disasm_output
Johnny Chened401982011-03-03 19:14:00 +000084
Johnny Chen9efcb0e2011-04-19 19:44:26 +000085 disasm_output = lldbutil.disassemble(target, function)
86 if self.TraceOn():
87 print "function:", function
88 print "disassembly=>\n", disasm_output
Johnny Chened401982011-03-03 19:14:00 +000089
90 sa1 = symbol.GetStartAddress()
91 #print "sa1:", sa1
Johnny Chen4b6fed42011-06-09 22:09:52 +000092 #print "sa1.GetFileAddress():", hex(sa1.GetFileAddress())
Johnny Chened401982011-03-03 19:14:00 +000093 #ea1 = symbol.GetEndAddress()
94 #print "ea1:", ea1
95 sa2 = function.GetStartAddress()
96 #print "sa2:", sa2
Johnny Chen4b6fed42011-06-09 22:09:52 +000097 #print "sa2.GetFileAddress():", hex(sa2.GetFileAddress())
Johnny Chened401982011-03-03 19:14:00 +000098 #ea2 = function.GetEndAddress()
99 #print "ea2:", ea2
Johnny Chene2b5cfd2011-06-09 22:04:56 +0000100 self.assertTrue(sa1 and sa2 and sa1 == sa2,
101 "The two starting addresses should be the same")
Johnny Chened401982011-03-03 19:14:00 +0000102
Johnny Chen9ae98202011-04-23 00:34:56 +0000103 from lldbutil import get_description
104 desc1 = get_description(sa1)
105 desc2 = get_description(sa2)
106 self.assertTrue(desc1 and desc2 and desc1 == desc2,
Johnny Chene2b5cfd2011-06-09 22:04:56 +0000107 "SBAddress.GetDescription() API of sa1 and sa2 should return the same string")
Johnny Chened401982011-03-03 19:14:00 +0000108
Johnny Chened401982011-03-03 19:14:00 +0000109if __name__ == '__main__':
110 import atexit
111 lldb.SBDebugger.Initialize()
112 atexit.register(lambda: lldb.SBDebugger.Terminate())
113 unittest2.main()