Add test_display_source_python() test case to TestSourceManager.py which uses

the lldb PyThon API SBSourceManager to display source files.

To accomodate this, the C++ SBSourceManager API has been changed to take an
lldb::SBStream as the destination for display of source lines.  Modify SBStream::ctor()
so that its opaque pointer is initialized with an StreamString instance.

llvm-svn: 121605
This commit is contained in:
Johnny Chen 2010-12-11 01:20:39 +00:00
parent 92da705261
commit f6eaba85a8
5 changed files with 51 additions and 8 deletions

View File

@ -34,7 +34,7 @@ public:
uint32_t context_before, uint32_t context_before,
uint32_t context_after, uint32_t context_after,
const char* current_line_cstr, const char* current_line_cstr,
FILE *f); lldb::SBStream &s);
protected: protected:

View File

@ -66,6 +66,7 @@ protected:
friend class SBInstruction; friend class SBInstruction;
friend class SBInstructionList; friend class SBInstructionList;
friend class SBModule; friend class SBModule;
friend class SBSourceManager;
friend class SBSymbol; friend class SBSymbol;
friend class SBSymbolContext; friend class SBSymbolContext;
friend class SBTarget; friend class SBTarget;

View File

@ -9,6 +9,7 @@
#include "lldb/API/SBSourceManager.h" #include "lldb/API/SBSourceManager.h"
#include "lldb/API/SBStream.h"
#include "lldb/API/SBFileSpec.h" #include "lldb/API/SBFileSpec.h"
#include "lldb/Core/Stream.h" #include "lldb/Core/Stream.h"
@ -49,26 +50,23 @@ SBSourceManager::DisplaySourceLinesWithLineNumbers
uint32_t context_before, uint32_t context_before,
uint32_t context_after, uint32_t context_after,
const char* current_line_cstr, const char* current_line_cstr,
FILE *f SBStream &s
) )
{ {
if (m_opaque_ptr == NULL) if (m_opaque_ptr == NULL)
return 0; return 0;
if (f == NULL) if (s.m_opaque_ap.get() == NULL)
return 0; return 0;
if (file.IsValid()) if (file.IsValid())
{ {
StreamFile str (f);
return m_opaque_ptr->DisplaySourceLinesWithLineNumbers (*file, return m_opaque_ptr->DisplaySourceLinesWithLineNumbers (*file,
line, line,
context_before, context_before,
context_after, context_after,
current_line_cstr, current_line_cstr,
&str); s.m_opaque_ap.get());
} }
return 0; return 0;
} }

View File

@ -17,7 +17,7 @@ using namespace lldb;
using namespace lldb_private; using namespace lldb_private;
SBStream::SBStream () : SBStream::SBStream () :
m_opaque_ap (), m_opaque_ap (new StreamString()),
m_is_file (false) m_is_file (false)
{ {
} }

View File

@ -3,6 +3,8 @@ Test lldb core component: SourceManager.
Test cases: Test cases:
o test_display_source_python:
Test display of source using the SBSourceManager API.
o test_modify_source_file_while_debugging: o test_modify_source_file_while_debugging:
Test the caching mechanism of the source manager. Test the caching mechanism of the source manager.
""" """
@ -21,11 +23,53 @@ class SourceManagerTestCase(TestBase):
# Find the line number to break inside main(). # Find the line number to break inside main().
self.line = line_number('main.c', '// Set break point at this line.') 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): def test_modify_source_file_while_debugging(self):
"""Modify a source file while debugging the executable.""" """Modify a source file while debugging the executable."""
self.buildDefault() self.buildDefault()
self.modify_source_file_while_debugging() 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): def modify_source_file_while_debugging(self):
"""Modify a source file while debugging the executable.""" """Modify a source file while debugging the executable."""
exe = os.path.join(os.getcwd(), "a.out") exe = os.path.join(os.getcwd(), "a.out")