Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test SBSymbolContext APIs. |
| 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import re |
| 7 | import unittest2 |
| 8 | import lldb, lldbutil |
| 9 | from lldbtest import * |
| 10 | |
| 11 | class SymbolContextAPITestCase(TestBase): |
| 12 | |
| 13 | mydir = os.path.join("python_api", "symbol-context") |
| 14 | |
| 15 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 16 | @python_api_test |
| 17 | def test_with_dsym(self): |
| 18 | """Exercise SBSymbolContext API extensively.""" |
| 19 | self.buildDsym() |
| 20 | self.symbol_context() |
| 21 | |
| 22 | @python_api_test |
| 23 | def test_with_dwarf(self): |
| 24 | """Exercise SBSymbolContext API extensively.""" |
| 25 | self.buildDwarf() |
| 26 | self.symbol_context() |
| 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.line = line_number('main.c', '// Find the line number of function "c" here.') |
| 33 | |
| 34 | def symbol_context(self): |
| 35 | """Get an SBSymbolContext object and call its many methods.""" |
| 36 | exe = os.path.join(os.getcwd(), "a.out") |
| 37 | |
| 38 | # Create a target by the debugger. |
| 39 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame^] | 40 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 41 | |
| 42 | # Now create a breakpoint on main.c by name 'c'. |
| 43 | breakpoint = target.BreakpointCreateByName('c', 'a.out') |
| 44 | #print "breakpoint:", breakpoint |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame^] | 45 | self.assertTrue(breakpoint and |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 46 | breakpoint.GetNumLocations() == 1, |
| 47 | VALID_BREAKPOINT) |
| 48 | |
| 49 | # Now launch the process, and do not stop at entry point. |
Johnny Chen | aedbe0f | 2011-04-19 19:50:43 +0000 | [diff] [blame] | 50 | self.process = target.LaunchSimple(None, None, os.getcwd()) |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 51 | |
| 52 | self.process = target.GetProcess() |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame^] | 53 | self.assertTrue(self.process, PROCESS_IS_VALID) |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 54 | |
| 55 | # Frame #0 should be on self.line. |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 56 | from lldbutil import get_stopped_thread |
| 57 | thread = get_stopped_thread(self.process, lldb.eStopReasonBreakpoint) |
| 58 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint") |
| 59 | frame0 = thread.GetFrameAtIndex(0) |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 60 | self.assertTrue(frame0.GetLineEntry().GetLine() == self.line) |
| 61 | |
| 62 | # Now get the SBSymbolContext from this frame. We want everything. :-) |
| 63 | context = frame0.GetSymbolContext(lldb.eSymbolContextEverything) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame^] | 64 | self.assertTrue(context) |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 65 | |
Johnny Chen | 62f68ee | 2010-12-16 18:37:46 +0000 | [diff] [blame] | 66 | # Get the description of this module. |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 67 | module = context.GetModule() |
Johnny Chen | 9ae9820 | 2011-04-23 00:34:56 +0000 | [diff] [blame] | 68 | desc = lldbutil.get_description(module) |
| 69 | self.expect(desc, "The module should match", exe=False, |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 70 | substrs = [os.path.join(self.mydir, 'a.out')]) |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 71 | |
| 72 | compileUnit = context.GetCompileUnit() |
| 73 | self.expect(repr(compileUnit), "The compile unit should match", exe=False, |
| 74 | substrs = [os.path.join(self.mydir, 'main.c')]) |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 75 | |
| 76 | function = context.GetFunction() |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame^] | 77 | self.assertTrue(function) |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 78 | #print "function:", function |
| 79 | |
| 80 | block = context.GetBlock() |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame^] | 81 | self.assertTrue(block) |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 82 | #print "block:", block |
| 83 | |
| 84 | lineEntry = context.GetLineEntry() |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 85 | #print "line entry:", lineEntry |
Johnny Chen | 6becf1c | 2010-12-16 01:41:37 +0000 | [diff] [blame] | 86 | self.expect(lineEntry.GetFileSpec().GetDirectory(), "The line entry should have the correct directory", |
| 87 | exe=False, |
| 88 | substrs = [self.mydir]) |
| 89 | self.expect(lineEntry.GetFileSpec().GetFilename(), "The line entry should have the correct filename", |
| 90 | exe=False, |
| 91 | substrs = ['main.c']) |
| 92 | self.assertTrue(lineEntry.GetLine() == self.line, |
| 93 | "The line entry's line number should match ") |
| 94 | |
| 95 | symbol = context.GetSymbol() |
| 96 | self.assertTrue(function.GetName() == symbol.GetName() and symbol.GetName() == 'c', |
| 97 | "The symbol name should be 'c'") |
| 98 | |
| 99 | |
| 100 | if __name__ == '__main__': |
| 101 | import atexit |
| 102 | lldb.SBDebugger.Initialize() |
| 103 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 104 | unittest2.main() |