Make expressions clean up their JIT'ed code allocation.

llvm-svn: 123855
This commit is contained in:
Greg Clayton 2011-01-19 23:00:49 +00:00
parent eecc6bc388
commit 22a939a782
13 changed files with 133 additions and 89 deletions

View File

@ -118,5 +118,10 @@ namespace clang
struct PrintingPolicy;
}
namespace llvm
{
class ExecutionEngine;
}
#endif // #if defined(__cplusplus)
#endif // liblldb_ClangForward_h_

View File

@ -23,7 +23,7 @@
#include "lldb/lldb-forward.h"
#include "lldb/lldb-private.h"
#include "lldb/Core/ClangForward.h"
#include "llvm/ExecutionEngine/JITMemoryManager.h"
#include "lldb/Target/Process.h"
namespace lldb_private {
@ -41,11 +41,20 @@ class RecordingMemoryManager;
class ClangExpression
{
public:
ClangExpression () :
m_jit_process_sp(),
m_jit_alloc (LLDB_INVALID_ADDRESS),
m_jit_start_addr (LLDB_INVALID_ADDRESS),
m_jit_end_addr (LLDB_INVALID_ADDRESS)
{
}
//------------------------------------------------------------------
/// Destructor
//------------------------------------------------------------------
virtual ~ClangExpression ()
{
DeallocateJITFunction ();
}
//------------------------------------------------------------------
@ -112,6 +121,38 @@ public:
//------------------------------------------------------------------
virtual bool
NeedsVariableResolution () = 0;
void
DeallocateJITFunction ()
{
if (m_jit_process_sp && m_jit_alloc != LLDB_INVALID_ADDRESS)
{
m_jit_process_sp->DeallocateMemory (m_jit_alloc);
// If this process is ever used for anything else, we can not clear it
// here. For now it is only used in order to deallocate any code if
// m_jit_alloc is a valid address.
m_jit_process_sp.reset();
m_jit_alloc = LLDB_INVALID_ADDRESS;
}
}
//------------------------------------------------------------------
/// Return the address of the function's JIT-compiled code, or
/// LLDB_INVALID_ADDRESS if the function is not JIT compiled
//------------------------------------------------------------------
lldb::addr_t
StartAddress ()
{
return m_jit_start_addr;
}
protected:
lldb::ProcessSP m_jit_process_sp;
lldb::addr_t m_jit_alloc; ///< The address of the block containing JITted code. LLDB_INVALID_ADDRESS if invalid.
lldb::addr_t m_jit_start_addr; ///< The address of the JITted function within the JIT allocation. LLDB_INVALID_ADDRESS if invalid.
lldb::addr_t m_jit_end_addr; ///< The address of the JITted function within the JIT allocation. LLDB_INVALID_ADDRESS if invalid.
};
} // namespace lldb_private

View File

@ -17,15 +17,9 @@
#include <string>
#include <vector>
namespace llvm
{
class ExecutionEngine;
}
namespace lldb_private
{
class Process;
class RecordingMemoryManager;
//----------------------------------------------------------------------
@ -96,6 +90,10 @@ public:
//------------------------------------------------------------------
/// JIT-compile the IR for an already-parsed expression.
///
/// @param[out] func_allocation_addr
/// The address which can be used to deallocate the code for this
/// JIT'ed function
///
/// @param[out] func_addr
/// The address to which the function has been written.
///
@ -117,7 +115,8 @@ public:
/// Test with Success().
//------------------------------------------------------------------
Error
MakeJIT (lldb::addr_t &func_addr,
MakeJIT (lldb::addr_t &func_allocation_addr,
lldb::addr_t &func_addr,
lldb::addr_t &func_end,
ExecutionContext &exe_ctx,
lldb::ClangExpressionVariableSP *const_result = NULL);
@ -136,7 +135,9 @@ public:
/// The error generated. If .Success() is true, disassembly succeeded.
//------------------------------------------------------------------
Error
DisassembleFunction (Stream &stream, ExecutionContext &exc_context);
DisassembleFunction (Stream &stream,
ExecutionContext &exe_ctx,
RecordingMemoryManager *jit_memory_manager);
private:
//----------------------------------------------------------------------
@ -187,7 +188,6 @@ private:
std::auto_ptr<clang::SelectorTable> m_selector_table; ///< Selector table for Objective-C methods
std::auto_ptr<clang::ASTContext> m_ast_context; ///< The AST context used to hold types and names for the parser
std::auto_ptr<clang::CodeGenerator> m_code_generator; ///< [owned by the Execution Engine] The Clang object that generates IR
RecordingMemoryManager *m_jit_mm; ///< The memory manager for the LLVM JIT
std::auto_ptr<llvm::ExecutionEngine> m_execution_engine; ///< The LLVM JIT
std::vector<JittedFunction> m_jitted_functions; ///< A vector of all functions that have been JITted into machine code (just one, if ParseExpression() was called)
};

View File

@ -477,7 +477,7 @@ public:
bool discard_on_error = true)
{
return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
m_wrapper_function_addr,
m_jit_start_addr,
args_addr_ref,
errors,
stop_others,
@ -618,7 +618,6 @@ private:
std::string m_wrapper_function_name; ///< The name of the wrapper function.
std::string m_wrapper_function_text; ///< The contents of the wrapper function.
std::string m_wrapper_struct_name; ///< The name of the struct that contains the target function address, arguments, and result.
lldb::addr_t m_wrapper_function_addr; ///< The address of the wrapper function.
std::list<lldb::addr_t> m_wrapper_args_addrs; ///< The addresses of the arguments to the wrapper function.
bool m_struct_valid; ///< True if the ASTStructExtractor has populated the variables below.

View File

@ -291,7 +291,6 @@ private:
std::auto_ptr<ClangExpressionDeclMap> m_expr_decl_map; ///< The map to use when parsing and materializing the expression.
std::auto_ptr<ClangExpressionVariableList> m_local_variables; ///< The local expression variables, if the expression is DWARF.
std::auto_ptr<StreamString> m_dwarf_opcodes; ///< The DWARF opcodes for the expression. May be NULL.
lldb::addr_t m_jit_addr; ///< The address of the JITted code. LLDB_INVALID_ADDRESS if invalid.
bool m_cplusplus; ///< True if the expression is compiled as a C++ member function (true if it was parsed when exe_ctx was in a C++ method).
bool m_objectivec; ///< True if the expression is compiled as an Objective-C method (true if it was parsed when exe_ctx was in an Objective-C method).

View File

@ -88,18 +88,9 @@ public:
{
// nothing is both >= LLDB_INVALID_ADDRESS and < LLDB_INVALID_ADDRESS,
// so this always returns false if the function is not JIT compiled yet
return (address >= m_jit_begin && address < m_jit_end);
return (address >= m_jit_start_addr && address < m_jit_end_addr);
}
//------------------------------------------------------------------
/// Return the address of the function's JIT-compiled code, or
/// LLDB_INVALID_ADDRESS if the function is not JIT compiled
//------------------------------------------------------------------
lldb::addr_t
StartAddress ()
{
return m_jit_begin;
}
//------------------------------------------------------------------
/// Return the string that the parser should parse. Must be a full
@ -191,9 +182,6 @@ private:
std::string m_function_text; ///< The text of the function. Must be a well-formed translation unit.
std::string m_function_name; ///< The name of the function.
lldb::addr_t m_jit_begin; ///< The address of the JITted code. LLDB_INVALID_ADDRESS if invalid.
lldb::addr_t m_jit_end; ///< The end of the JITted code. LLDB_INVALID_ADDRESS if invalid.
};
} // namespace lldb_private

View File

@ -52,11 +52,6 @@ namespace lldb_private {
//----------------------------------------------------------------------
class RecordingMemoryManager : public llvm::JITMemoryManager
{
friend Error ClangExpressionParser::MakeJIT (uint64_t &,
uint64_t&,
ExecutionContext &,
lldb::ClangExpressionVariableSP *);
public:
//------------------------------------------------------------------
/// Constructor
@ -329,6 +324,8 @@ private:
AddToLocalToRemoteMap (lldb::addr_t lstart, size_t size, lldb::addr_t rstart);
std::vector<LocalToRemoteAddressRange> m_address_map; ///< The base address of the remote allocation
friend class ClangExpressionParser;
};
} // namespace lldb_private

View File

@ -26,13 +26,13 @@ class ThreadPlanCallUserExpression : public ThreadPlanCallFunction
{
public:
ThreadPlanCallUserExpression (Thread &thread,
Address &function,
lldb::addr_t arg,
bool stop_other_threads,
bool discard_on_error,
lldb::addr_t *this_arg,
lldb::addr_t *cmd_arg,
ClangUserExpression::ClangUserExpressionSP &user_expression_sp);
Address &function,
lldb::addr_t arg,
bool stop_other_threads,
bool discard_on_error,
lldb::addr_t *this_arg,
lldb::addr_t *cmd_arg,
ClangUserExpression::ClangUserExpressionSP &user_expression_sp);
virtual
~ThreadPlanCallUserExpression ();
@ -50,9 +50,9 @@ public:
protected:
private:
ClangUserExpression::ClangUserExpressionSP m_user_expression_sp; // This is currently just used to ensure the
// User expression the initiated this ThreadPlan
// lives as long as the thread plan does.
ClangUserExpression::ClangUserExpressionSP m_user_expression_sp; // This is currently just used to ensure the
// User expression the initiated this ThreadPlan
// lives as long as the thread plan does.
DISALLOW_COPY_AND_ASSIGN (ThreadPlanCallUserExpression);
};

View File

@ -45,6 +45,7 @@ class ClangASTType;
class ClangNamespaceDecl;
class ClangExpression;
class ClangExpressionDeclMap;
class ClangExpressionParser;
class ClangExpressionVariable;
class ClangExpressionVariableList;
class ClangExpressionVariableList;

View File

@ -425,11 +425,15 @@ ClangExpressionParser::MakeDWARF ()
}
Error
ClangExpressionParser::MakeJIT (lldb::addr_t &func_addr,
ClangExpressionParser::MakeJIT (lldb::addr_t &func_allocation_addr,
lldb::addr_t &func_addr,
lldb::addr_t &func_end,
ExecutionContext &exe_ctx,
lldb::ClangExpressionVariableSP *const_result)
{
func_allocation_addr = LLDB_INVALID_ADDRESS;
func_addr = LLDB_INVALID_ADDRESS;
func_end = LLDB_INVALID_ADDRESS;
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Error err;
@ -488,7 +492,9 @@ ClangExpressionParser::MakeJIT (lldb::addr_t &func_addr,
}
}
m_jit_mm = new RecordingMemoryManager();
// llvm will own this pointer when llvm::ExecutionEngine::createJIT is called
// below so we don't need to free it.
RecordingMemoryManager *jit_memory_manager = new RecordingMemoryManager();
std::string error_string;
@ -496,7 +502,7 @@ ClangExpressionParser::MakeJIT (lldb::addr_t &func_addr,
m_execution_engine.reset(llvm::ExecutionEngine::createJIT (module,
&error_string,
m_jit_mm,
jit_memory_manager,
CodeGenOpt::Less,
true,
CodeModel::Small));
@ -544,25 +550,27 @@ ClangExpressionParser::MakeJIT (lldb::addr_t &func_addr,
size_t alloc_size = 0;
std::map<uint8_t *, uint8_t *>::iterator fun_pos = m_jit_mm->m_functions.begin();
std::map<uint8_t *, uint8_t *>::iterator fun_end = m_jit_mm->m_functions.end();
std::map<uint8_t *, uint8_t *>::iterator fun_pos = jit_memory_manager->m_functions.begin();
std::map<uint8_t *, uint8_t *>::iterator fun_end = jit_memory_manager->m_functions.end();
for (; fun_pos != fun_end; ++fun_pos)
alloc_size += (*fun_pos).second - (*fun_pos).first;
Error alloc_error;
lldb::addr_t target_addr = exc_context.process->AllocateMemory (alloc_size, lldb::ePermissionsReadable|lldb::ePermissionsExecutable, alloc_error);
func_allocation_addr = exc_context.process->AllocateMemory (alloc_size,
lldb::ePermissionsReadable|lldb::ePermissionsExecutable,
alloc_error);
if (target_addr == LLDB_INVALID_ADDRESS)
if (func_allocation_addr == LLDB_INVALID_ADDRESS)
{
err.SetErrorToGenericError();
err.SetErrorStringWithFormat("Couldn't allocate memory for the JITted function: %s", alloc_error.AsCString("unknown error"));
return err;
}
lldb::addr_t cursor = target_addr;
lldb::addr_t cursor = func_allocation_addr;
for (fun_pos = m_jit_mm->m_functions.begin(); fun_pos != fun_end; fun_pos++)
for (fun_pos = jit_memory_manager->m_functions.begin(); fun_pos != fun_end; fun_pos++)
{
lldb::addr_t lstart = (lldb::addr_t) (*fun_pos).first;
lldb::addr_t lend = (lldb::addr_t) (*fun_pos).second;
@ -577,7 +585,7 @@ ClangExpressionParser::MakeJIT (lldb::addr_t &func_addr,
return err;
}
m_jit_mm->AddToLocalToRemoteMap (lstart, size, cursor);
jit_memory_manager->AddToLocalToRemoteMap (lstart, size, cursor);
cursor += size;
}
@ -585,11 +593,11 @@ ClangExpressionParser::MakeJIT (lldb::addr_t &func_addr,
for (pos = m_jitted_functions.begin(); pos != end; pos++)
{
(*pos).m_remote_addr = m_jit_mm->GetRemoteAddressForLocal ((*pos).m_local_addr);
(*pos).m_remote_addr = jit_memory_manager->GetRemoteAddressForLocal ((*pos).m_local_addr);
if (!(*pos).m_name.compare(function_name.c_str()))
{
func_end = m_jit_mm->GetRemoteRangeForLocal ((*pos).m_local_addr).second;
func_end = jit_memory_manager->GetRemoteRangeForLocal ((*pos).m_local_addr).second;
func_addr = (*pos).m_remote_addr;
}
}
@ -600,7 +608,7 @@ ClangExpressionParser::MakeJIT (lldb::addr_t &func_addr,
StreamString disassembly_stream;
Error err = DisassembleFunction(disassembly_stream, exe_ctx);
Error err = DisassembleFunction(disassembly_stream, exe_ctx, jit_memory_manager);
if (!err.Success())
{
@ -617,7 +625,7 @@ ClangExpressionParser::MakeJIT (lldb::addr_t &func_addr,
}
Error
ClangExpressionParser::DisassembleFunction (Stream &stream, ExecutionContext &exe_ctx)
ClangExpressionParser::DisassembleFunction (Stream &stream, ExecutionContext &exe_ctx, RecordingMemoryManager *jit_memory_manager)
{
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
@ -653,7 +661,7 @@ ClangExpressionParser::DisassembleFunction (Stream &stream, ExecutionContext &ex
std::pair <lldb::addr_t, lldb::addr_t> func_range;
func_range = m_jit_mm->GetRemoteRangeForLocal(func_local_addr);
func_range = jit_memory_manager->GetRemoteRangeForLocal(func_local_addr);
if (func_range.first == 0 && func_range.second == 0)
{

View File

@ -61,7 +61,6 @@ ClangFunction::ClangFunction
m_clang_ast_context (ast_context),
m_wrapper_function_name ("__lldb_caller_function"),
m_wrapper_struct_name ("__lldb_caller_struct"),
m_wrapper_function_addr (),
m_wrapper_args_addrs (),
m_arg_values (arg_value_list),
m_compiled (false),
@ -83,7 +82,6 @@ ClangFunction::ClangFunction
m_clang_ast_context (ast_context),
m_wrapper_function_name ("__lldb_function_caller"),
m_wrapper_struct_name ("__lldb_caller_struct"),
m_wrapper_function_addr (),
m_wrapper_args_addrs (),
m_arg_values (arg_value_list),
m_compiled (false),
@ -240,12 +238,12 @@ ClangFunction::WriteFunctionWrapper (ExecutionContext &exe_ctx, Stream &errors)
if (m_JITted)
return true;
lldb::addr_t wrapper_function_end;
Error jit_error = m_parser->MakeJIT(m_wrapper_function_addr, wrapper_function_end, exe_ctx);
Error jit_error (m_parser->MakeJIT (m_jit_alloc, m_jit_start_addr, m_jit_end_addr, exe_ctx));
if (!jit_error.Success())
return false;
if (exe_ctx.process && m_jit_alloc != LLDB_INVALID_ADDRESS)
m_jit_process_sp = exe_ctx.process->GetSP();
return true;
}
@ -360,7 +358,7 @@ ClangFunction::InsertFunction (ExecutionContext &exe_ctx, lldb::addr_t &args_add
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
if (log)
log->Printf ("Call Address: 0x%llx Struct Address: 0x%llx.\n", m_wrapper_function_addr, args_addr_ref);
log->Printf ("Call Address: 0x%llx Struct Address: 0x%llx.\n", m_jit_start_addr, args_addr_ref);
return true;
}
@ -524,8 +522,14 @@ ClangFunction::ExecuteFunction(
return lldb::eExecutionSetupError;
}
return_value = ClangFunction::ExecuteFunction(exe_ctx, m_wrapper_function_addr, args_addr, stop_others,
try_all_threads, discard_on_error, single_thread_timeout_usec, errors);
return_value = ClangFunction::ExecuteFunction (exe_ctx,
m_jit_start_addr,
args_addr,
stop_others,
try_all_threads,
discard_on_error,
single_thread_timeout_usec,
errors);
if (args_addr_ptr != NULL)
*args_addr_ptr = args_addr;

View File

@ -41,15 +41,15 @@ using namespace lldb_private;
ClangUserExpression::ClangUserExpression (const char *expr,
const char *expr_prefix) :
m_expr_text(expr),
m_expr_prefix(expr_prefix ? expr_prefix : ""),
m_transformed_text(),
m_jit_addr(LLDB_INVALID_ADDRESS),
m_cplusplus(false),
m_objectivec(false),
m_needs_object_ptr(false),
m_const_object(false),
m_desired_type(NULL, NULL)
ClangExpression (),
m_expr_text (expr),
m_expr_prefix (expr_prefix ? expr_prefix : ""),
m_transformed_text (),
m_cplusplus (false),
m_objectivec (false),
m_needs_object_ptr (false),
m_const_object (false),
m_desired_type (NULL, NULL)
{
}
@ -295,14 +295,14 @@ ClangUserExpression::Parse (Stream &error_stream,
m_dwarf_opcodes.reset();
lldb::addr_t jit_end;
Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx, const_result);
Error jit_error = parser.MakeJIT (m_jit_alloc, m_jit_start_addr, m_jit_end_addr, exe_ctx, const_result);
m_expr_decl_map->DidParse();
if (jit_error.Success())
{
if (exe_ctx.process && m_jit_alloc != LLDB_INVALID_ADDRESS)
m_jit_process_sp = exe_ctx.process->GetSP();
return true;
}
else
@ -321,7 +321,7 @@ ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
{
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
if (m_jit_addr != LLDB_INVALID_ADDRESS)
if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
{
Error materialize_error;
@ -370,14 +370,14 @@ ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
#if 0
// jingham: look here
StreamFile logfile ("/tmp/exprs.txt", "a");
logfile.Printf("0x%16.16llx: thread = 0x%4.4x, expr = '%s'\n", m_jit_addr, exe_ctx.thread ? exe_ctx.thread->GetID() : -1, m_expr_text.c_str());
logfile.Printf("0x%16.16llx: thread = 0x%4.4x, expr = '%s'\n", m_jit_start_addr, exe_ctx.thread ? exe_ctx.thread->GetID() : -1, m_expr_text.c_str());
#endif
if (log)
{
log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_addr);
log->Printf(" Function address : 0x%llx", (uint64_t)m_jit_start_addr);
if (m_needs_object_ptr)
log->Printf(" Object pointer : 0x%llx", (uint64_t)object_ptr);
@ -420,7 +420,7 @@ ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
// forcing unwind_on_error to be true here, in practical terms that can't happen.
return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
m_jit_addr,
m_jit_start_addr,
struct_address,
error_stream,
true,
@ -484,7 +484,7 @@ ClangUserExpression::Execute (Stream &error_stream,
return lldb::eExecutionSetupError;
}
else if (m_jit_addr != LLDB_INVALID_ADDRESS)
else if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
{
lldb::addr_t struct_address;
@ -497,7 +497,7 @@ ClangUserExpression::Execute (Stream &error_stream,
const bool stop_others = true;
const bool try_all_threads = true;
Address wrapper_address (NULL, m_jit_addr);
Address wrapper_address (NULL, m_jit_start_addr);
lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (*(exe_ctx.thread),
wrapper_address,
struct_address,

View File

@ -38,10 +38,9 @@ using namespace lldb_private;
//------------------------------------------------------------------
ClangUtilityFunction::ClangUtilityFunction (const char *text,
const char *name) :
m_function_text(text),
m_function_name(name),
m_jit_begin(LLDB_INVALID_ADDRESS),
m_jit_end(LLDB_INVALID_ADDRESS)
ClangExpression (),
m_function_text (text),
m_function_name (name)
{
}
@ -65,7 +64,7 @@ bool
ClangUtilityFunction::Install (Stream &error_stream,
ExecutionContext &exe_ctx)
{
if (m_jit_begin != LLDB_INVALID_ADDRESS)
if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
{
error_stream.PutCString("error: already installed\n");
return false;
@ -123,13 +122,16 @@ ClangUtilityFunction::Install (Stream &error_stream,
// JIT the output of the parser
//
Error jit_error = parser.MakeJIT (m_jit_begin, m_jit_end, exe_ctx);
Error jit_error = parser.MakeJIT (m_jit_alloc, m_jit_start_addr, m_jit_end_addr, exe_ctx);
if (exe_ctx.process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
m_jit_process_sp = exe_ctx.process->GetSP();
#if 0
// jingham: look here
StreamFile logfile ("/tmp/exprs.txt", "a");
logfile.Printf ("0x%16.16llx: func = %s, source =\n%s\n",
m_jit_begin,
m_jit_start_addr,
m_function_name.c_str(),
m_function_text.c_str());
#endif