diff --git a/lldb/include/lldb/API/SBSourceManager.h b/lldb/include/lldb/API/SBSourceManager.h index 4cabe4a2bc8d..397835b01220 100644 --- a/lldb/include/lldb/API/SBSourceManager.h +++ b/lldb/include/lldb/API/SBSourceManager.h @@ -34,7 +34,7 @@ public: uint32_t context_before, uint32_t context_after, const char* current_line_cstr, - FILE *f); + lldb::SBStream &s); protected: diff --git a/lldb/include/lldb/API/SBStream.h b/lldb/include/lldb/API/SBStream.h index 16cd3d500460..a2050f23898e 100644 --- a/lldb/include/lldb/API/SBStream.h +++ b/lldb/include/lldb/API/SBStream.h @@ -66,6 +66,7 @@ protected: friend class SBInstruction; friend class SBInstructionList; friend class SBModule; + friend class SBSourceManager; friend class SBSymbol; friend class SBSymbolContext; friend class SBTarget; diff --git a/lldb/source/API/SBSourceManager.cpp b/lldb/source/API/SBSourceManager.cpp index 32f792d82203..0fa98e35dea2 100644 --- a/lldb/source/API/SBSourceManager.cpp +++ b/lldb/source/API/SBSourceManager.cpp @@ -9,6 +9,7 @@ #include "lldb/API/SBSourceManager.h" +#include "lldb/API/SBStream.h" #include "lldb/API/SBFileSpec.h" #include "lldb/Core/Stream.h" @@ -49,26 +50,23 @@ SBSourceManager::DisplaySourceLinesWithLineNumbers uint32_t context_before, uint32_t context_after, const char* current_line_cstr, - FILE *f + SBStream &s ) { if (m_opaque_ptr == NULL) return 0; - if (f == NULL) + if (s.m_opaque_ap.get() == NULL) return 0; if (file.IsValid()) { - StreamFile str (f); - - return m_opaque_ptr->DisplaySourceLinesWithLineNumbers (*file, line, context_before, context_after, current_line_cstr, - &str); + s.m_opaque_ap.get()); } return 0; } diff --git a/lldb/source/API/SBStream.cpp b/lldb/source/API/SBStream.cpp index 55714fd41281..b8a18a140335 100644 --- a/lldb/source/API/SBStream.cpp +++ b/lldb/source/API/SBStream.cpp @@ -17,7 +17,7 @@ using namespace lldb; using namespace lldb_private; SBStream::SBStream () : - m_opaque_ap (), + m_opaque_ap (new StreamString()), m_is_file (false) { } diff --git a/lldb/test/source-manager/TestSourceManager.py b/lldb/test/source-manager/TestSourceManager.py index 9a1ceeae70a6..45fc2d343d90 100644 --- a/lldb/test/source-manager/TestSourceManager.py +++ b/lldb/test/source-manager/TestSourceManager.py @@ -3,6 +3,8 @@ Test lldb core component: SourceManager. Test cases: +o test_display_source_python: + Test display of source using the SBSourceManager API. o test_modify_source_file_while_debugging: Test the caching mechanism of the source manager. """ @@ -21,11 +23,53 @@ class SourceManagerTestCase(TestBase): # Find the line number to break inside main(). self.line = line_number('main.c', '// Set break point at this line.') + @python_api_test + def test_display_source_python(self): + """Test display of source using the SBSourceManager API.""" + self.buildDefault() + self.display_source_python() + def test_modify_source_file_while_debugging(self): """Modify a source file while debugging the executable.""" self.buildDefault() self.modify_source_file_while_debugging() + def display_source_python(self): + """Display source using the SBSourceManager API.""" + exe = os.path.join(os.getcwd(), "a.out") + self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) + + target = self.dbg.CreateTarget(exe) + self.assertTrue(target.IsValid(), VALID_TARGET) + + # Launch the process, and do not stop at the entry point. + process = target.LaunchProcess([], [], os.ctermid(), 0, False) + + # + # Exercise Python APIs to display source lines. + # + + # Create the filespec for 'main.c'. + filespec = lldb.SBFileSpec('main.c', False) + source_mgr = self.dbg.GetSourceManager() + # Use a string stream as the destination. + stream = lldb.SBStream() + source_mgr.DisplaySourceLinesWithLineNumbers(filespec, + self.line, + 2, # context before + 2, # context after + "=>", # prefix for current line + stream) + + # 2 + # 3 int main(int argc, char const *argv[]) { + # 4 => printf("Hello world.\n"); // Set break point at this line. + # 5 return 0; + # 6 } + self.expect(stream.GetData(), "Source code displayed correctly", + exe=False, + patterns = ['=>.*Hello world']) + def modify_source_file_while_debugging(self): """Modify a source file while debugging the executable.""" exe = os.path.join(os.getcwd(), "a.out")