Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@ -0,0 +1,38 @@
if(NOT LLDB_BUILT_STANDALONE)
set(tablegen_deps intrinsics_gen)
endif()
add_lldb_library(lldbExpression
DiagnosticManager.cpp
DWARFExpression.cpp
Expression.cpp
ExpressionSourceCode.cpp
ExpressionVariable.cpp
FunctionCaller.cpp
IRDynamicChecks.cpp
IRExecutionUnit.cpp
IRInterpreter.cpp
IRMemoryMap.cpp
LLVMUserExpression.cpp
Materializer.cpp
REPL.cpp
UserExpression.cpp
UtilityFunction.cpp
DEPENDS
${tablegen_deps}
LINK_LIBS
lldbCore
lldbHost
lldbInterpreter
lldbSymbol
lldbTarget
lldbUtility
lldbPluginExpressionParserClang
LINK_COMPONENTS
Core
ExecutionEngine
Support
)

View File

@ -0,0 +1 @@
14011aece7c9904555c1471547cc8611af405a32

View File

@ -0,0 +1,90 @@
//===-- DiagnosticManager.cpp -----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Expression/DiagnosticManager.h"
#include "llvm/Support/ErrorHandling.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
using namespace lldb_private;
void DiagnosticManager::Dump(Log *log) {
if (!log)
return;
std::string str = GetString();
// GetString() puts a separator after each diagnostic.
// We want to remove the last '\n' because log->PutCString will add one for
// us.
if (str.size() && str.back() == '\n') {
str.pop_back();
}
log->PutCString(str.c_str());
}
static const char *StringForSeverity(DiagnosticSeverity severity) {
switch (severity) {
// this should be exhaustive
case lldb_private::eDiagnosticSeverityError:
return "error: ";
case lldb_private::eDiagnosticSeverityWarning:
return "warning: ";
case lldb_private::eDiagnosticSeverityRemark:
return "";
}
llvm_unreachable("switch needs another case for DiagnosticSeverity enum");
}
std::string DiagnosticManager::GetString(char separator) {
std::string ret;
for (const Diagnostic *diagnostic : Diagnostics()) {
ret.append(StringForSeverity(diagnostic->GetSeverity()));
ret.append(diagnostic->GetMessage());
ret.push_back(separator);
}
return ret;
}
size_t DiagnosticManager::Printf(DiagnosticSeverity severity,
const char *format, ...) {
StreamString ss;
va_list args;
va_start(args, format);
size_t result = ss.PrintfVarArg(format, args);
va_end(args);
AddDiagnostic(ss.GetString(), severity, eDiagnosticOriginLLDB);
return result;
}
size_t DiagnosticManager::PutString(DiagnosticSeverity severity,
llvm::StringRef str) {
if (str.empty())
return 0;
AddDiagnostic(str, severity, eDiagnosticOriginLLDB);
return str.size();
}
void DiagnosticManager::CopyDiagnostics(DiagnosticManager &otherDiagnostics) {
for (const DiagnosticList::value_type &other_diagnostic:
otherDiagnostics.Diagnostics()) {
AddDiagnostic(
other_diagnostic->GetMessage(), other_diagnostic->GetSeverity(),
other_diagnostic->getKind(), other_diagnostic->GetCompilerID());
}
}

View File

@ -0,0 +1,29 @@
//===-- Expression.cpp ------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Expression/Expression.h"
#include "lldb/Target/ExecutionContextScope.h"
#include "lldb/Target/Target.h"
using namespace lldb_private;
Expression::Expression(Target &target)
: m_target_wp(target.shared_from_this()),
m_jit_start_addr(LLDB_INVALID_ADDRESS),
m_jit_end_addr(LLDB_INVALID_ADDRESS) {
// Can't make any kind of expression without a target.
assert(m_target_wp.lock());
}
Expression::Expression(ExecutionContextScope &exe_scope)
: m_target_wp(exe_scope.CalculateTarget()),
m_jit_start_addr(LLDB_INVALID_ADDRESS),
m_jit_end_addr(LLDB_INVALID_ADDRESS) {
assert(m_target_wp.lock());
}

View File

@ -0,0 +1,384 @@
//===-- ExpressionSourceCode.cpp --------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Expression/ExpressionSourceCode.h"
#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
#include "lldb/Symbol/Block.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/DebugMacros.h"
#include "lldb/Symbol/TypeSystem.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Language.h"
#include "lldb/Target/Platform.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/StreamString.h"
using namespace lldb_private;
const char *ExpressionSourceCode::g_expression_prefix = R"(
#ifndef NULL
#define NULL (__null)
#endif
#ifndef Nil
#define Nil (__null)
#endif
#ifndef nil
#define nil (__null)
#endif
#ifndef YES
#define YES ((BOOL)1)
#endif
#ifndef NO
#define NO ((BOOL)0)
#endif
typedef __INT8_TYPE__ int8_t;
typedef __UINT8_TYPE__ uint8_t;
typedef __INT16_TYPE__ int16_t;
typedef __UINT16_TYPE__ uint16_t;
typedef __INT32_TYPE__ int32_t;
typedef __UINT32_TYPE__ uint32_t;
typedef __INT64_TYPE__ int64_t;
typedef __UINT64_TYPE__ uint64_t;
typedef __INTPTR_TYPE__ intptr_t;
typedef __UINTPTR_TYPE__ uintptr_t;
typedef __SIZE_TYPE__ size_t;
typedef __PTRDIFF_TYPE__ ptrdiff_t;
typedef unsigned short unichar;
extern "C"
{
int printf(const char * __restrict, ...);
}
)";
static const char *c_start_marker = " /*LLDB_BODY_START*/\n ";
static const char *c_end_marker = ";\n /*LLDB_BODY_END*/\n";
namespace {
class AddMacroState {
enum State {
CURRENT_FILE_NOT_YET_PUSHED,
CURRENT_FILE_PUSHED,
CURRENT_FILE_POPPED
};
public:
AddMacroState(const FileSpec &current_file, const uint32_t current_file_line)
: m_state(CURRENT_FILE_NOT_YET_PUSHED), m_current_file(current_file),
m_current_file_line(current_file_line) {}
void StartFile(const FileSpec &file) {
m_file_stack.push_back(file);
if (file == m_current_file)
m_state = CURRENT_FILE_PUSHED;
}
void EndFile() {
if (m_file_stack.size() == 0)
return;
FileSpec old_top = m_file_stack.back();
m_file_stack.pop_back();
if (old_top == m_current_file)
m_state = CURRENT_FILE_POPPED;
}
// An entry is valid if it occurs before the current line in
// the current file.
bool IsValidEntry(uint32_t line) {
switch (m_state) {
case CURRENT_FILE_NOT_YET_PUSHED:
return true;
case CURRENT_FILE_PUSHED:
// If we are in file included in the current file,
// the entry should be added.
if (m_file_stack.back() != m_current_file)
return true;
if (line >= m_current_file_line)
return false;
else
return true;
default:
return false;
}
}
private:
std::vector<FileSpec> m_file_stack;
State m_state;
FileSpec m_current_file;
uint32_t m_current_file_line;
};
} // anonymous namespace
static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
AddMacroState &state, StreamString &stream) {
if (dm == nullptr)
return;
for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {
const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);
uint32_t line;
switch (entry.GetType()) {
case DebugMacroEntry::DEFINE:
if (state.IsValidEntry(entry.GetLineNumber()))
stream.Printf("#define %s\n", entry.GetMacroString().AsCString());
else
return;
break;
case DebugMacroEntry::UNDEF:
if (state.IsValidEntry(entry.GetLineNumber()))
stream.Printf("#undef %s\n", entry.GetMacroString().AsCString());
else
return;
break;
case DebugMacroEntry::START_FILE:
line = entry.GetLineNumber();
if (state.IsValidEntry(line))
state.StartFile(entry.GetFileSpec(comp_unit));
else
return;
break;
case DebugMacroEntry::END_FILE:
state.EndFile();
break;
case DebugMacroEntry::INDIRECT:
AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream);
break;
default:
// This is an unknown/invalid entry. Ignore.
break;
}
}
}
static void AddLocalVariableDecls(const lldb::VariableListSP &var_list_sp,
StreamString &stream) {
for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
ConstString var_name = var_sp->GetName();
if (!var_name || var_name == ConstString("this") ||
var_name == ConstString(".block_descriptor"))
continue;
stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
}
}
bool ExpressionSourceCode::GetText(std::string &text,
lldb::LanguageType wrapping_language,
bool static_method,
ExecutionContext &exe_ctx) const {
const char *target_specific_defines = "typedef signed char BOOL;\n";
std::string module_macros;
Target *target = exe_ctx.GetTargetPtr();
if (target) {
if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64) {
target_specific_defines = "typedef bool BOOL;\n";
}
if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
static ConstString g_platform_ios_simulator("ios-simulator");
if (platform_sp->GetPluginName() == g_platform_ios_simulator) {
target_specific_defines = "typedef bool BOOL;\n";
}
}
}
if (ClangModulesDeclVendor *decl_vendor =
target->GetClangModulesDeclVendor()) {
ClangPersistentVariables *persistent_vars =
llvm::cast<ClangPersistentVariables>(
target->GetPersistentExpressionStateForLanguage(
lldb::eLanguageTypeC));
const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
persistent_vars->GetHandLoadedClangModules();
ClangModulesDeclVendor::ModuleVector modules_for_macros;
for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
modules_for_macros.push_back(module);
}
if (target->GetEnableAutoImportClangModules()) {
if (StackFrame *frame = exe_ctx.GetFramePtr()) {
if (Block *block = frame->GetFrameBlock()) {
SymbolContext sc;
block->CalculateSymbolContext(&sc);
if (sc.comp_unit) {
StreamString error_stream;
decl_vendor->AddModulesForCompileUnit(
*sc.comp_unit, modules_for_macros, error_stream);
}
}
}
}
decl_vendor->ForEachMacro(
modules_for_macros,
[&module_macros](const std::string &expansion) -> bool {
module_macros.append(expansion);
module_macros.append("\n");
return false;
});
}
}
StreamString debug_macros_stream;
StreamString lldb_local_var_decls;
if (StackFrame *frame = exe_ctx.GetFramePtr()) {
const SymbolContext &sc = frame->GetSymbolContext(
lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
if (sc.comp_unit && sc.line_entry.IsValid()) {
DebugMacros *dm = sc.comp_unit->GetDebugMacros();
if (dm) {
AddMacroState state(sc.line_entry.file, sc.line_entry.line);
AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
}
}
ConstString object_name;
if (Language::LanguageIsCPlusPlus(frame->GetLanguage())) {
if (target->GetInjectLocalVariables(&exe_ctx)) {
lldb::VariableListSP var_list_sp =
frame->GetInScopeVariableList(false, true);
AddLocalVariableDecls(var_list_sp, lldb_local_var_decls);
}
}
}
if (m_wrap) {
switch (wrapping_language) {
default:
return false;
case lldb::eLanguageTypeC:
case lldb::eLanguageTypeC_plus_plus:
case lldb::eLanguageTypeObjC:
break;
}
StreamString wrap_stream;
wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", module_macros.c_str(),
debug_macros_stream.GetData(), g_expression_prefix,
target_specific_defines, m_prefix.c_str());
// First construct a tagged form of the user expression so we can find it
// later:
std::string tagged_body;
switch (wrapping_language) {
default:
tagged_body = m_body;
break;
case lldb::eLanguageTypeC:
case lldb::eLanguageTypeC_plus_plus:
case lldb::eLanguageTypeObjC:
tagged_body.append(c_start_marker);
tagged_body.append(m_body);
tagged_body.append(c_end_marker);
break;
}
switch (wrapping_language) {
default:
break;
case lldb::eLanguageTypeC:
wrap_stream.Printf("void \n"
"%s(void *$__lldb_arg) \n"
"{ \n"
" %s; \n"
"%s"
"} \n",
m_name.c_str(), lldb_local_var_decls.GetData(),
tagged_body.c_str());
break;
case lldb::eLanguageTypeC_plus_plus:
wrap_stream.Printf("void \n"
"$__lldb_class::%s(void *$__lldb_arg) \n"
"{ \n"
" %s; \n"
"%s"
"} \n",
m_name.c_str(), lldb_local_var_decls.GetData(),
tagged_body.c_str());
break;
case lldb::eLanguageTypeObjC:
if (static_method) {
wrap_stream.Printf(
"@interface $__lldb_objc_class ($__lldb_category) \n"
"+(void)%s:(void *)$__lldb_arg; \n"
"@end \n"
"@implementation $__lldb_objc_class ($__lldb_category) \n"
"+(void)%s:(void *)$__lldb_arg \n"
"{ \n"
"%s"
"} \n"
"@end \n",
m_name.c_str(), m_name.c_str(), tagged_body.c_str());
} else {
wrap_stream.Printf(
"@interface $__lldb_objc_class ($__lldb_category) \n"
"-(void)%s:(void *)$__lldb_arg; \n"
"@end \n"
"@implementation $__lldb_objc_class ($__lldb_category) \n"
"-(void)%s:(void *)$__lldb_arg \n"
"{ \n"
"%s"
"} \n"
"@end \n",
m_name.c_str(), m_name.c_str(), tagged_body.c_str());
}
break;
}
text = wrap_stream.GetString();
} else {
text.append(m_body);
}
return true;
}
bool ExpressionSourceCode::GetOriginalBodyBounds(
std::string transformed_text, lldb::LanguageType wrapping_language,
size_t &start_loc, size_t &end_loc) {
const char *start_marker;
const char *end_marker;
switch (wrapping_language) {
default:
return false;
case lldb::eLanguageTypeC:
case lldb::eLanguageTypeC_plus_plus:
case lldb::eLanguageTypeObjC:
start_marker = c_start_marker;
end_marker = c_end_marker;
break;
}
start_loc = transformed_text.find(start_marker);
if (start_loc == std::string::npos)
return false;
start_loc += strlen(start_marker);
end_loc = transformed_text.find(end_marker);
if (end_loc == std::string::npos)
return false;
return true;
}

View File

@ -0,0 +1,82 @@
//===-- ExpressionVariable.cpp ----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Expression/ExpressionVariable.h"
#include "lldb/Expression/IRExecutionUnit.h"
#include "lldb/Utility/Log.h"
using namespace lldb_private;
ExpressionVariable::~ExpressionVariable() {}
uint8_t *ExpressionVariable::GetValueBytes() {
const size_t byte_size = m_frozen_sp->GetByteSize();
if (byte_size > 0) {
if (m_frozen_sp->GetDataExtractor().GetByteSize() < byte_size) {
m_frozen_sp->GetValue().ResizeData(byte_size);
m_frozen_sp->GetValue().GetData(m_frozen_sp->GetDataExtractor());
}
return const_cast<uint8_t *>(
m_frozen_sp->GetDataExtractor().GetDataStart());
}
return NULL;
}
PersistentExpressionState::~PersistentExpressionState() {}
lldb::addr_t PersistentExpressionState::LookupSymbol(const ConstString &name) {
SymbolMap::iterator si = m_symbol_map.find(name.GetCString());
if (si != m_symbol_map.end())
return si->second;
else
return LLDB_INVALID_ADDRESS;
}
void PersistentExpressionState::RegisterExecutionUnit(
lldb::IRExecutionUnitSP &execution_unit_sp) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
m_execution_units.insert(execution_unit_sp);
if (log)
log->Printf("Registering JITted Functions:\n");
for (const IRExecutionUnit::JittedFunction &jitted_function :
execution_unit_sp->GetJittedFunctions()) {
if (jitted_function.m_external &&
jitted_function.m_name != execution_unit_sp->GetFunctionName() &&
jitted_function.m_remote_addr != LLDB_INVALID_ADDRESS) {
m_symbol_map[jitted_function.m_name.GetCString()] =
jitted_function.m_remote_addr;
if (log)
log->Printf(" Function: %s at 0x%" PRIx64 ".",
jitted_function.m_name.GetCString(),
jitted_function.m_remote_addr);
}
}
if (log)
log->Printf("Registering JIIted Symbols:\n");
for (const IRExecutionUnit::JittedGlobalVariable &global_var :
execution_unit_sp->GetJittedGlobalVariables()) {
if (global_var.m_remote_addr != LLDB_INVALID_ADDRESS) {
// Demangle the name before inserting it, so that lookups by the ConstStr
// of the demangled name
// will find the mangled one (needed for looking up metadata pointers.)
Mangled mangler(global_var.m_name);
mangler.GetDemangledName(lldb::eLanguageTypeUnknown);
m_symbol_map[global_var.m_name.GetCString()] = global_var.m_remote_addr;
if (log)
log->Printf(" Symbol: %s at 0x%" PRIx64 ".",
global_var.m_name.GetCString(), global_var.m_remote_addr);
}
}
}

View File

@ -0,0 +1,394 @@
//===-- FunctionCaller.cpp ---------------------------------------*- C++-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Expression/FunctionCaller.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/State.h"
#include "lldb/Core/ValueObject.h"
#include "lldb/Core/ValueObjectList.h"
#include "lldb/Expression/DiagnosticManager.h"
#include "lldb/Expression/IRExecutionUnit.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/Type.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/ThreadPlan.h"
#include "lldb/Target/ThreadPlanCallFunction.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/Log.h"
using namespace lldb_private;
//----------------------------------------------------------------------
// FunctionCaller constructor
//----------------------------------------------------------------------
FunctionCaller::FunctionCaller(ExecutionContextScope &exe_scope,
const CompilerType &return_type,
const Address &functionAddress,
const ValueList &arg_value_list,
const char *name)
: Expression(exe_scope), m_execution_unit_sp(), m_parser(),
m_jit_module_wp(), m_name(name ? name : "<unknown>"),
m_function_ptr(NULL), m_function_addr(functionAddress),
m_function_return_type(return_type),
m_wrapper_function_name("__lldb_caller_function"),
m_wrapper_struct_name("__lldb_caller_struct"), m_wrapper_args_addrs(),
m_arg_values(arg_value_list), m_compiled(false), m_JITted(false) {
m_jit_process_wp = lldb::ProcessWP(exe_scope.CalculateProcess());
// Can't make a FunctionCaller without a process.
assert(m_jit_process_wp.lock());
}
//----------------------------------------------------------------------
// Destructor
//----------------------------------------------------------------------
FunctionCaller::~FunctionCaller() {
lldb::ProcessSP process_sp(m_jit_process_wp.lock());
if (process_sp) {
lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
if (jit_module_sp)
process_sp->GetTarget().GetImages().Remove(jit_module_sp);
}
}
bool FunctionCaller::WriteFunctionWrapper(
ExecutionContext &exe_ctx, DiagnosticManager &diagnostic_manager) {
Process *process = exe_ctx.GetProcessPtr();
if (!process)
return false;
lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
if (process != jit_process_sp.get())
return false;
if (!m_compiled)
return false;
if (m_JITted)
return true;
bool can_interpret = false; // should stay that way
Status jit_error(m_parser->PrepareForExecution(
m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
can_interpret, eExecutionPolicyAlways));
if (!jit_error.Success())
return false;
if (m_parser->GetGenerateDebugInfo()) {
lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
if (jit_module_sp) {
ConstString const_func_name(FunctionName());
FileSpec jit_file;
jit_file.GetFilename() = const_func_name;
jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
m_jit_module_wp = jit_module_sp;
process->GetTarget().GetImages().Append(jit_module_sp);
}
}
if (process && m_jit_start_addr)
m_jit_process_wp = process->shared_from_this();
m_JITted = true;
return true;
}
bool FunctionCaller::WriteFunctionArguments(
ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref,
DiagnosticManager &diagnostic_manager) {
return WriteFunctionArguments(exe_ctx, args_addr_ref, m_arg_values,
diagnostic_manager);
}
// FIXME: Assure that the ValueList we were passed in is consistent with the one
// that defined this function.
bool FunctionCaller::WriteFunctionArguments(
ExecutionContext &exe_ctx, lldb::addr_t &args_addr_ref,
ValueList &arg_values, DiagnosticManager &diagnostic_manager) {
// All the information to reconstruct the struct is provided by the
// StructExtractor.
if (!m_struct_valid) {
diagnostic_manager.PutString(eDiagnosticSeverityError,
"Argument information was not correctly "
"parsed, so the function cannot be called.");
return false;
}
Status error;
lldb::ExpressionResults return_value = lldb::eExpressionSetupError;
Process *process = exe_ctx.GetProcessPtr();
if (process == NULL)
return return_value;
lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
if (process != jit_process_sp.get())
return false;
if (args_addr_ref == LLDB_INVALID_ADDRESS) {
args_addr_ref = process->AllocateMemory(
m_struct_size, lldb::ePermissionsReadable | lldb::ePermissionsWritable,
error);
if (args_addr_ref == LLDB_INVALID_ADDRESS)
return false;
m_wrapper_args_addrs.push_back(args_addr_ref);
} else {
// Make sure this is an address that we've already handed out.
if (find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(),
args_addr_ref) == m_wrapper_args_addrs.end()) {
return false;
}
}
// TODO: verify fun_addr needs to be a callable address
Scalar fun_addr(
m_function_addr.GetCallableLoadAddress(exe_ctx.GetTargetPtr()));
uint64_t first_offset = m_member_offsets[0];
process->WriteScalarToMemory(args_addr_ref + first_offset, fun_addr,
process->GetAddressByteSize(), error);
// FIXME: We will need to extend this for Variadic functions.
Status value_error;
size_t num_args = arg_values.GetSize();
if (num_args != m_arg_values.GetSize()) {
diagnostic_manager.Printf(
eDiagnosticSeverityError,
"Wrong number of arguments - was: %" PRIu64 " should be: %" PRIu64 "",
(uint64_t)num_args, (uint64_t)m_arg_values.GetSize());
return false;
}
for (size_t i = 0; i < num_args; i++) {
// FIXME: We should sanity check sizes.
uint64_t offset = m_member_offsets[i + 1]; // Clang sizes are in bytes.
Value *arg_value = arg_values.GetValueAtIndex(i);
// FIXME: For now just do scalars:
// Special case: if it's a pointer, don't do anything (the ABI supports
// passing cstrings)
if (arg_value->GetValueType() == Value::eValueTypeHostAddress &&
arg_value->GetContextType() == Value::eContextTypeInvalid &&
arg_value->GetCompilerType().IsPointerType())
continue;
const Scalar &arg_scalar = arg_value->ResolveValue(&exe_ctx);
if (!process->WriteScalarToMemory(args_addr_ref + offset, arg_scalar,
arg_scalar.GetByteSize(), error))
return false;
}
return true;
}
bool FunctionCaller::InsertFunction(ExecutionContext &exe_ctx,
lldb::addr_t &args_addr_ref,
DiagnosticManager &diagnostic_manager) {
if (CompileFunction(exe_ctx.GetThreadSP(), diagnostic_manager) != 0)
return false;
if (!WriteFunctionWrapper(exe_ctx, diagnostic_manager))
return false;
if (!WriteFunctionArguments(exe_ctx, args_addr_ref, diagnostic_manager))
return false;
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
if (log)
log->Printf("Call Address: 0x%" PRIx64 " Struct Address: 0x%" PRIx64 ".\n",
m_jit_start_addr, args_addr_ref);
return true;
}
lldb::ThreadPlanSP FunctionCaller::GetThreadPlanToCallFunction(
ExecutionContext &exe_ctx, lldb::addr_t args_addr,
const EvaluateExpressionOptions &options,
DiagnosticManager &diagnostic_manager) {
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
LIBLLDB_LOG_STEP));
if (log)
log->Printf("-- [FunctionCaller::GetThreadPlanToCallFunction] Creating "
"thread plan to call function \"%s\" --",
m_name.c_str());
// FIXME: Use the errors Stream for better error reporting.
Thread *thread = exe_ctx.GetThreadPtr();
if (thread == NULL) {
diagnostic_manager.PutString(
eDiagnosticSeverityError,
"Can't call a function without a valid thread.");
return NULL;
}
// Okay, now run the function:
Address wrapper_address(m_jit_start_addr);
lldb::addr_t args = {args_addr};
lldb::ThreadPlanSP new_plan_sp(new ThreadPlanCallFunction(
*thread, wrapper_address, CompilerType(), args, options));
new_plan_sp->SetIsMasterPlan(true);
new_plan_sp->SetOkayToDiscard(false);
return new_plan_sp;
}
bool FunctionCaller::FetchFunctionResults(ExecutionContext &exe_ctx,
lldb::addr_t args_addr,
Value &ret_value) {
// Read the return value - it is the last field in the struct:
// FIXME: How does clang tell us there's no return value? We need to handle
// that case.
// FIXME: Create our ThreadPlanCallFunction with the return CompilerType, and
// then use GetReturnValueObject
// to fetch the value. That way we can fetch any values we need.
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
LIBLLDB_LOG_STEP));
if (log)
log->Printf("-- [FunctionCaller::FetchFunctionResults] Fetching function "
"results for \"%s\"--",
m_name.c_str());
Process *process = exe_ctx.GetProcessPtr();
if (process == NULL)
return false;
lldb::ProcessSP jit_process_sp(m_jit_process_wp.lock());
if (process != jit_process_sp.get())
return false;
Status error;
ret_value.GetScalar() = process->ReadUnsignedIntegerFromMemory(
args_addr + m_return_offset, m_return_size, 0, error);
if (error.Fail())
return false;
ret_value.SetCompilerType(m_function_return_type);
ret_value.SetValueType(Value::eValueTypeScalar);
return true;
}
void FunctionCaller::DeallocateFunctionResults(ExecutionContext &exe_ctx,
lldb::addr_t args_addr) {
std::list<lldb::addr_t>::iterator pos;
pos = std::find(m_wrapper_args_addrs.begin(), m_wrapper_args_addrs.end(),
args_addr);
if (pos != m_wrapper_args_addrs.end())
m_wrapper_args_addrs.erase(pos);
exe_ctx.GetProcessRef().DeallocateMemory(args_addr);
}
lldb::ExpressionResults FunctionCaller::ExecuteFunction(
ExecutionContext &exe_ctx, lldb::addr_t *args_addr_ptr,
const EvaluateExpressionOptions &options,
DiagnosticManager &diagnostic_manager, Value &results) {
lldb::ExpressionResults return_value = lldb::eExpressionSetupError;
// FunctionCaller::ExecuteFunction execution is always just to get the result.
// Do make sure we ignore
// breakpoints, unwind on error, and don't try to debug it.
EvaluateExpressionOptions real_options = options;
real_options.SetDebug(false);
real_options.SetUnwindOnError(true);
real_options.SetIgnoreBreakpoints(true);
lldb::addr_t args_addr;
if (args_addr_ptr != NULL)
args_addr = *args_addr_ptr;
else
args_addr = LLDB_INVALID_ADDRESS;
if (CompileFunction(exe_ctx.GetThreadSP(), diagnostic_manager) != 0)
return lldb::eExpressionSetupError;
if (args_addr == LLDB_INVALID_ADDRESS) {
if (!InsertFunction(exe_ctx, args_addr, diagnostic_manager))
return lldb::eExpressionSetupError;
}
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
LIBLLDB_LOG_STEP));
if (log)
log->Printf(
"== [FunctionCaller::ExecuteFunction] Executing function \"%s\" ==",
m_name.c_str());
lldb::ThreadPlanSP call_plan_sp = GetThreadPlanToCallFunction(
exe_ctx, args_addr, real_options, diagnostic_manager);
if (!call_plan_sp)
return lldb::eExpressionSetupError;
// We need to make sure we record the fact that we are running an expression
// here
// otherwise this fact will fail to be recorded when fetching an Objective-C
// object description
if (exe_ctx.GetProcessPtr())
exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
return_value = exe_ctx.GetProcessRef().RunThreadPlan(
exe_ctx, call_plan_sp, real_options, diagnostic_manager);
if (log) {
if (return_value != lldb::eExpressionCompleted) {
log->Printf("== [FunctionCaller::ExecuteFunction] Execution of \"%s\" "
"completed abnormally ==",
m_name.c_str());
} else {
log->Printf("== [FunctionCaller::ExecuteFunction] Execution of \"%s\" "
"completed normally ==",
m_name.c_str());
}
}
if (exe_ctx.GetProcessPtr())
exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
if (args_addr_ptr != NULL)
*args_addr_ptr = args_addr;
if (return_value != lldb::eExpressionCompleted)
return return_value;
FetchFunctionResults(exe_ctx, args_addr, results);
if (args_addr_ptr == NULL)
DeallocateFunctionResults(exe_ctx, args_addr);
return lldb::eExpressionCompleted;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,379 @@
//===-- LLVMUserExpression.cpp ----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// C Includes
// C++ Includes
// Project includes
#include "lldb/Expression/LLVMUserExpression.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/ValueObjectConstResult.h"
#include "lldb/Expression/DiagnosticManager.h"
#include "lldb/Expression/ExpressionSourceCode.h"
#include "lldb/Expression/IRExecutionUnit.h"
#include "lldb/Expression/IRInterpreter.h"
#include "lldb/Expression/Materializer.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Symbol/Block.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolVendor.h"
#include "lldb/Symbol/Type.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/ThreadPlan.h"
#include "lldb/Target/ThreadPlanCallUserExpression.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
using namespace lldb_private;
LLVMUserExpression::LLVMUserExpression(ExecutionContextScope &exe_scope,
llvm::StringRef expr,
llvm::StringRef prefix,
lldb::LanguageType language,
ResultType desired_type,
const EvaluateExpressionOptions &options)
: UserExpression(exe_scope, expr, prefix, language, desired_type, options),
m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
m_stack_frame_top(LLDB_INVALID_ADDRESS),
m_allow_cxx(false),
m_allow_objc(false),
m_transformed_text(),
m_execution_unit_sp(), m_materializer_ap(), m_jit_module_wp(),
m_enforce_valid_object(true), m_in_cplusplus_method(false),
m_in_objectivec_method(false), m_in_static_method(false),
m_needs_object_ptr(false), m_target(NULL), m_can_interpret(false),
m_materialized_address(LLDB_INVALID_ADDRESS) {}
LLVMUserExpression::~LLVMUserExpression() {
if (m_target) {
lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
if (jit_module_sp)
m_target->GetImages().Remove(jit_module_sp);
}
}
lldb::ExpressionResults
LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
ExecutionContext &exe_ctx,
const EvaluateExpressionOptions &options,
lldb::UserExpressionSP &shared_ptr_to_me,
lldb::ExpressionVariableSP &result) {
// The expression log is quite verbose, and if you're just tracking the
// execution of the
// expression, it's quite convenient to have these logs come out with the STEP
// log as well.
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
LIBLLDB_LOG_STEP));
if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
struct_address)) {
diagnostic_manager.Printf(
eDiagnosticSeverityError,
"errored out in %s, couldn't PrepareToExecuteJITExpression",
__FUNCTION__);
return lldb::eExpressionSetupError;
}
lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
if (m_can_interpret) {
llvm::Module *module = m_execution_unit_sp->GetModule();
llvm::Function *function = m_execution_unit_sp->GetFunction();
if (!module || !function) {
diagnostic_manager.PutString(
eDiagnosticSeverityError,
"supposed to interpret, but nothing is there");
return lldb::eExpressionSetupError;
}
Status interpreter_error;
std::vector<lldb::addr_t> args;
if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
diagnostic_manager.Printf(eDiagnosticSeverityError,
"errored out in %s, couldn't AddArguments",
__FUNCTION__);
return lldb::eExpressionSetupError;
}
function_stack_bottom = m_stack_frame_bottom;
function_stack_top = m_stack_frame_top;
IRInterpreter::Interpret(*module, *function, args,
*m_execution_unit_sp.get(), interpreter_error,
function_stack_bottom, function_stack_top,
exe_ctx);
if (!interpreter_error.Success()) {
diagnostic_manager.Printf(eDiagnosticSeverityError,
"supposed to interpret, but failed: %s",
interpreter_error.AsCString());
return lldb::eExpressionDiscarded;
}
} else {
if (!exe_ctx.HasThreadScope()) {
diagnostic_manager.Printf(eDiagnosticSeverityError,
"%s called with no thread selected",
__FUNCTION__);
return lldb::eExpressionSetupError;
}
Address wrapper_address(m_jit_start_addr);
std::vector<lldb::addr_t> args;
if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
diagnostic_manager.Printf(eDiagnosticSeverityError,
"errored out in %s, couldn't AddArguments",
__FUNCTION__);
return lldb::eExpressionSetupError;
}
lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
exe_ctx.GetThreadRef(), wrapper_address, args, options,
shared_ptr_to_me));
StreamString ss;
if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString());
return lldb::eExpressionSetupError;
}
ThreadPlanCallUserExpression *user_expression_plan =
static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
lldb::addr_t function_stack_pointer =
user_expression_plan->GetFunctionStackPointer();
function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
function_stack_top = function_stack_pointer;
if (log)
log->Printf(
"-- [UserExpression::Execute] Execution of expression begins --");
if (exe_ctx.GetProcessPtr())
exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
lldb::ExpressionResults execution_result =
exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
diagnostic_manager);
if (exe_ctx.GetProcessPtr())
exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
if (log)
log->Printf("-- [UserExpression::Execute] Execution of expression "
"completed --");
if (execution_result == lldb::eExpressionInterrupted ||
execution_result == lldb::eExpressionHitBreakpoint) {
const char *error_desc = NULL;
if (call_plan_sp) {
lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
if (real_stop_info_sp)
error_desc = real_stop_info_sp->GetDescription();
}
if (error_desc)
diagnostic_manager.Printf(eDiagnosticSeverityError,
"Execution was interrupted, reason: %s.",
error_desc);
else
diagnostic_manager.PutString(eDiagnosticSeverityError,
"Execution was interrupted.");
if ((execution_result == lldb::eExpressionInterrupted &&
options.DoesUnwindOnError()) ||
(execution_result == lldb::eExpressionHitBreakpoint &&
options.DoesIgnoreBreakpoints()))
diagnostic_manager.AppendMessageToDiagnostic(
"The process has been returned to the state before expression "
"evaluation.");
else {
if (execution_result == lldb::eExpressionHitBreakpoint)
user_expression_plan->TransferExpressionOwnership();
diagnostic_manager.AppendMessageToDiagnostic(
"The process has been left at the point where it was "
"interrupted, "
"use \"thread return -x\" to return to the state before "
"expression evaluation.");
}
return execution_result;
} else if (execution_result == lldb::eExpressionStoppedForDebug) {
diagnostic_manager.PutString(
eDiagnosticSeverityRemark,
"Execution was halted at the first instruction of the expression "
"function because \"debug\" was requested.\n"
"Use \"thread return -x\" to return to the state before expression "
"evaluation.");
return execution_result;
} else if (execution_result != lldb::eExpressionCompleted) {
diagnostic_manager.Printf(
eDiagnosticSeverityError,
"Couldn't execute function; result was %s",
Process::ExecutionResultAsCString(execution_result));
return execution_result;
}
}
if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
function_stack_bottom, function_stack_top)) {
return lldb::eExpressionCompleted;
} else {
return lldb::eExpressionResultUnavailable;
}
} else {
diagnostic_manager.PutString(
eDiagnosticSeverityError,
"Expression can't be run, because there is no JIT compiled function");
return lldb::eExpressionSetupError;
}
}
bool LLVMUserExpression::FinalizeJITExecution(
DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
lldb::ExpressionVariableSP &result, lldb::addr_t function_stack_bottom,
lldb::addr_t function_stack_top) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
if (log)
log->Printf("-- [UserExpression::FinalizeJITExecution] Dematerializing "
"after execution --");
if (!m_dematerializer_sp) {
diagnostic_manager.Printf(eDiagnosticSeverityError,
"Couldn't apply expression side effects : no "
"dematerializer is present");
return false;
}
Status dematerialize_error;
m_dematerializer_sp->Dematerialize(dematerialize_error, function_stack_bottom,
function_stack_top);
if (!dematerialize_error.Success()) {
diagnostic_manager.Printf(eDiagnosticSeverityError,
"Couldn't apply expression side effects : %s",
dematerialize_error.AsCString("unknown error"));
return false;
}
result =
GetResultAfterDematerialization(exe_ctx.GetBestExecutionContextScope());
if (result)
result->TransferAddress();
m_dematerializer_sp.reset();
return true;
}
bool LLVMUserExpression::PrepareToExecuteJITExpression(
DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
lldb::addr_t &struct_address) {
lldb::TargetSP target;
lldb::ProcessSP process;
lldb::StackFrameSP frame;
if (!LockAndCheckContext(exe_ctx, target, process, frame)) {
diagnostic_manager.PutString(
eDiagnosticSeverityError,
"The context has changed before we could JIT the expression!");
return false;
}
if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
if (m_materialized_address == LLDB_INVALID_ADDRESS) {
Status alloc_error;
IRMemoryMap::AllocationPolicy policy =
m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly
: IRMemoryMap::eAllocationPolicyMirror;
const bool zero_memory = false;
m_materialized_address = m_execution_unit_sp->Malloc(
m_materializer_ap->GetStructByteSize(),
m_materializer_ap->GetStructAlignment(),
lldb::ePermissionsReadable | lldb::ePermissionsWritable, policy,
zero_memory, alloc_error);
if (!alloc_error.Success()) {
diagnostic_manager.Printf(
eDiagnosticSeverityError,
"Couldn't allocate space for materialized struct: %s",
alloc_error.AsCString());
return false;
}
}
struct_address = m_materialized_address;
if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS) {
Status alloc_error;
const size_t stack_frame_size = 512 * 1024;
const bool zero_memory = false;
m_stack_frame_bottom = m_execution_unit_sp->Malloc(
stack_frame_size, 8,
lldb::ePermissionsReadable | lldb::ePermissionsWritable,
IRMemoryMap::eAllocationPolicyHostOnly, zero_memory, alloc_error);
m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
if (!alloc_error.Success()) {
diagnostic_manager.Printf(
eDiagnosticSeverityError,
"Couldn't allocate space for the stack frame: %s",
alloc_error.AsCString());
return false;
}
}
Status materialize_error;
m_dematerializer_sp = m_materializer_ap->Materialize(
frame, *m_execution_unit_sp, struct_address, materialize_error);
if (!materialize_error.Success()) {
diagnostic_manager.Printf(eDiagnosticSeverityError,
"Couldn't materialize: %s",
materialize_error.AsCString());
return false;
}
}
return true;
}
lldb::ModuleSP LLVMUserExpression::GetJITModule() {
if (m_execution_unit_sp)
return m_execution_unit_sp->GetJITModule();
return lldb::ModuleSP();
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,388 @@
//===-- UserExpression.cpp ---------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <stdio.h>
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <cstdlib>
#include <map>
#include <string>
#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/ValueObjectConstResult.h"
#include "lldb/Expression/DiagnosticManager.h"
#include "lldb/Expression/ExpressionSourceCode.h"
#include "lldb/Expression/IRExecutionUnit.h"
#include "lldb/Expression/IRInterpreter.h"
#include "lldb/Expression/Materializer.h"
#include "lldb/Expression/UserExpression.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Symbol/Block.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolVendor.h"
#include "lldb/Symbol/Type.h"
#include "lldb/Symbol/TypeSystem.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/ThreadPlan.h"
#include "lldb/Target/ThreadPlanCallUserExpression.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
using namespace lldb_private;
UserExpression::UserExpression(ExecutionContextScope &exe_scope,
llvm::StringRef expr, llvm::StringRef prefix,
lldb::LanguageType language,
ResultType desired_type,
const EvaluateExpressionOptions &options)
: Expression(exe_scope), m_expr_text(expr), m_expr_prefix(prefix),
m_language(language), m_desired_type(desired_type), m_options(options) {}
UserExpression::~UserExpression() {}
void UserExpression::InstallContext(ExecutionContext &exe_ctx) {
m_jit_process_wp = exe_ctx.GetProcessSP();
lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
if (frame_sp)
m_address = frame_sp->GetFrameCodeAddress();
}
bool UserExpression::LockAndCheckContext(ExecutionContext &exe_ctx,
lldb::TargetSP &target_sp,
lldb::ProcessSP &process_sp,
lldb::StackFrameSP &frame_sp) {
lldb::ProcessSP expected_process_sp = m_jit_process_wp.lock();
process_sp = exe_ctx.GetProcessSP();
if (process_sp != expected_process_sp)
return false;
process_sp = exe_ctx.GetProcessSP();
target_sp = exe_ctx.GetTargetSP();
frame_sp = exe_ctx.GetFrameSP();
if (m_address.IsValid()) {
if (!frame_sp)
return false;
else
return (0 == Address::CompareLoadAddress(m_address,
frame_sp->GetFrameCodeAddress(),
target_sp.get()));
}
return true;
}
bool UserExpression::MatchesContext(ExecutionContext &exe_ctx) {
lldb::TargetSP target_sp;
lldb::ProcessSP process_sp;
lldb::StackFrameSP frame_sp;
return LockAndCheckContext(exe_ctx, target_sp, process_sp, frame_sp);
}
lldb::addr_t UserExpression::GetObjectPointer(lldb::StackFrameSP frame_sp,
ConstString &object_name,
Status &err) {
err.Clear();
if (!frame_sp) {
err.SetErrorStringWithFormat(
"Couldn't load '%s' because the context is incomplete",
object_name.AsCString());
return LLDB_INVALID_ADDRESS;
}
lldb::VariableSP var_sp;
lldb::ValueObjectSP valobj_sp;
valobj_sp = frame_sp->GetValueForVariableExpressionPath(
object_name.AsCString(), lldb::eNoDynamicValues,
StackFrame::eExpressionPathOptionCheckPtrVsMember |
StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
StackFrame::eExpressionPathOptionsNoSyntheticChildren |
StackFrame::eExpressionPathOptionsNoSyntheticArrayRange,
var_sp, err);
if (!err.Success() || !valobj_sp.get())
return LLDB_INVALID_ADDRESS;
lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
if (ret == LLDB_INVALID_ADDRESS) {
err.SetErrorStringWithFormat(
"Couldn't load '%s' because its value couldn't be evaluated",
object_name.AsCString());
return LLDB_INVALID_ADDRESS;
}
return ret;
}
lldb::ExpressionResults UserExpression::Evaluate(
ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options,
llvm::StringRef expr, llvm::StringRef prefix,
lldb::ValueObjectSP &result_valobj_sp, Status &error, uint32_t line_offset,
std::string *fixed_expression, lldb::ModuleSP *jit_module_sp_ptr) {
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
LIBLLDB_LOG_STEP));
lldb_private::ExecutionPolicy execution_policy = options.GetExecutionPolicy();
lldb::LanguageType language = options.GetLanguage();
const ResultType desired_type = options.DoesCoerceToId()
? UserExpression::eResultTypeId
: UserExpression::eResultTypeAny;
lldb::ExpressionResults execution_results = lldb::eExpressionSetupError;
Target *target = exe_ctx.GetTargetPtr();
if (!target) {
if (log)
log->Printf("== [UserExpression::Evaluate] Passed a NULL target, can't "
"run expressions.");
error.SetErrorString("expression passed a null target");
return lldb::eExpressionSetupError;
}
Process *process = exe_ctx.GetProcessPtr();
if (process == NULL || process->GetState() != lldb::eStateStopped) {
if (execution_policy == eExecutionPolicyAlways) {
if (log)
log->Printf("== [UserExpression::Evaluate] Expression may not run, but "
"is not constant ==");
error.SetErrorString("expression needed to run but couldn't");
return execution_results;
}
}
if (process == NULL || !process->CanJIT())
execution_policy = eExecutionPolicyNever;
// We need to set the expression execution thread here, turns out parse can
// call functions in the process of
// looking up symbols, which will escape the context set by exe_ctx passed to
// Execute.
lldb::ThreadSP thread_sp = exe_ctx.GetThreadSP();
ThreadList::ExpressionExecutionThreadPusher execution_thread_pusher(
thread_sp);
llvm::StringRef full_prefix;
llvm::StringRef option_prefix(options.GetPrefix());
std::string full_prefix_storage;
if (!prefix.empty() && !option_prefix.empty()) {
full_prefix_storage = prefix;
full_prefix_storage.append(option_prefix);
full_prefix = full_prefix_storage;
} else if (!prefix.empty())
full_prefix = prefix;
else
full_prefix = option_prefix;
// If the language was not specified in the expression command,
// set it to the language in the target's properties if
// specified, else default to the langage for the frame.
if (language == lldb::eLanguageTypeUnknown) {
if (target->GetLanguage() != lldb::eLanguageTypeUnknown)
language = target->GetLanguage();
else if (StackFrame *frame = exe_ctx.GetFramePtr())
language = frame->GetLanguage();
}
lldb::UserExpressionSP user_expression_sp(
target->GetUserExpressionForLanguage(expr, full_prefix, language,
desired_type, options, error));
if (error.Fail()) {
if (log)
log->Printf("== [UserExpression::Evaluate] Getting expression: %s ==",
error.AsCString());
return lldb::eExpressionSetupError;
}
if (log)
log->Printf("== [UserExpression::Evaluate] Parsing expression %s ==",
expr.str().c_str());
const bool keep_expression_in_memory = true;
const bool generate_debug_info = options.GetGenerateDebugInfo();
if (options.InvokeCancelCallback(lldb::eExpressionEvaluationParse)) {
error.SetErrorString("expression interrupted by callback before parse");
result_valobj_sp = ValueObjectConstResult::Create(
exe_ctx.GetBestExecutionContextScope(), error);
return lldb::eExpressionInterrupted;
}
DiagnosticManager diagnostic_manager;
bool parse_success =
user_expression_sp->Parse(diagnostic_manager, exe_ctx, execution_policy,
keep_expression_in_memory, generate_debug_info);
// Calculate the fixed expression always, since we need it for errors.
std::string tmp_fixed_expression;
if (fixed_expression == nullptr)
fixed_expression = &tmp_fixed_expression;
const char *fixed_text = user_expression_sp->GetFixedText();
if (fixed_text != nullptr)
fixed_expression->append(fixed_text);
// If there is a fixed expression, try to parse it:
if (!parse_success) {
execution_results = lldb::eExpressionParseError;
if (fixed_expression && !fixed_expression->empty() &&
options.GetAutoApplyFixIts()) {
lldb::UserExpressionSP fixed_expression_sp(
target->GetUserExpressionForLanguage(fixed_expression->c_str(),
full_prefix, language,
desired_type, options, error));
DiagnosticManager fixed_diagnostic_manager;
parse_success = fixed_expression_sp->Parse(
fixed_diagnostic_manager, exe_ctx, execution_policy,
keep_expression_in_memory, generate_debug_info);
if (parse_success) {
diagnostic_manager.Clear();
user_expression_sp = fixed_expression_sp;
} else {
// If the fixed expression failed to parse, don't tell the user about,
// that won't help.
fixed_expression->clear();
}
}
if (!parse_success) {
if (!fixed_expression->empty() && target->GetEnableNotifyAboutFixIts()) {
error.SetExpressionErrorWithFormat(
execution_results,
"expression failed to parse, fixed expression suggested:\n %s",
fixed_expression->c_str());
} else {
if (!diagnostic_manager.Diagnostics().size())
error.SetExpressionError(execution_results,
"expression failed to parse, unknown error");
else
error.SetExpressionError(execution_results,
diagnostic_manager.GetString().c_str());
}
}
}
if (parse_success) {
// If a pointer to a lldb::ModuleSP was passed in, return the JIT'ed module
// if one was created
if (jit_module_sp_ptr)
*jit_module_sp_ptr = user_expression_sp->GetJITModule();
lldb::ExpressionVariableSP expr_result;
if (execution_policy == eExecutionPolicyNever &&
!user_expression_sp->CanInterpret()) {
if (log)
log->Printf("== [UserExpression::Evaluate] Expression may not run, but "
"is not constant ==");
if (!diagnostic_manager.Diagnostics().size())
error.SetExpressionError(lldb::eExpressionSetupError,
"expression needed to run but couldn't");
} else if (execution_policy == eExecutionPolicyTopLevel) {
error.SetError(UserExpression::kNoResult, lldb::eErrorTypeGeneric);
return lldb::eExpressionCompleted;
} else {
if (options.InvokeCancelCallback(lldb::eExpressionEvaluationExecution)) {
error.SetExpressionError(
lldb::eExpressionInterrupted,
"expression interrupted by callback before execution");
result_valobj_sp = ValueObjectConstResult::Create(
exe_ctx.GetBestExecutionContextScope(), error);
return lldb::eExpressionInterrupted;
}
diagnostic_manager.Clear();
if (log)
log->Printf("== [UserExpression::Evaluate] Executing expression ==");
execution_results =
user_expression_sp->Execute(diagnostic_manager, exe_ctx, options,
user_expression_sp, expr_result);
if (execution_results != lldb::eExpressionCompleted) {
if (log)
log->Printf("== [UserExpression::Evaluate] Execution completed "
"abnormally ==");
if (!diagnostic_manager.Diagnostics().size())
error.SetExpressionError(
execution_results, "expression failed to execute, unknown error");
else
error.SetExpressionError(execution_results,
diagnostic_manager.GetString().c_str());
} else {
if (expr_result) {
result_valobj_sp = expr_result->GetValueObject();
if (log)
log->Printf("== [UserExpression::Evaluate] Execution completed "
"normally with result %s ==",
result_valobj_sp->GetValueAsCString());
} else {
if (log)
log->Printf("== [UserExpression::Evaluate] Execution completed "
"normally with no result ==");
error.SetError(UserExpression::kNoResult, lldb::eErrorTypeGeneric);
}
}
}
}
if (options.InvokeCancelCallback(lldb::eExpressionEvaluationComplete)) {
error.SetExpressionError(
lldb::eExpressionInterrupted,
"expression interrupted by callback after complete");
return lldb::eExpressionInterrupted;
}
if (result_valobj_sp.get() == NULL) {
result_valobj_sp = ValueObjectConstResult::Create(
exe_ctx.GetBestExecutionContextScope(), error);
}
return execution_results;
}
lldb::ExpressionResults
UserExpression::Execute(DiagnosticManager &diagnostic_manager,
ExecutionContext &exe_ctx,
const EvaluateExpressionOptions &options,
lldb::UserExpressionSP &shared_ptr_to_me,
lldb::ExpressionVariableSP &result_var) {
lldb::ExpressionResults expr_result = DoExecute(
diagnostic_manager, exe_ctx, options, shared_ptr_to_me, result_var);
Target *target = exe_ctx.GetTargetPtr();
if (options.GetResultIsInternal() && result_var && target) {
target->GetPersistentExpressionStateForLanguage(m_language)
->RemovePersistentVariable(result_var);
}
return expr_result;
}

View File

@ -0,0 +1,115 @@
//===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// C Includes
#include <stdio.h>
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
// C++ Includes
#include "lldb/Core/Module.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Expression/DiagnosticManager.h"
#include "lldb/Expression/ExpressionSourceCode.h"
#include "lldb/Expression/FunctionCaller.h"
#include "lldb/Expression/IRExecutionUnit.h"
#include "lldb/Expression/UtilityFunction.h"
#include "lldb/Host/Host.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Stream.h"
using namespace lldb_private;
using namespace lldb;
//------------------------------------------------------------------
/// Constructor
///
/// @param[in] text
/// The text of the function. Must be a full translation unit.
///
/// @param[in] name
/// The name of the function, as used in the text.
//------------------------------------------------------------------
UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope,
const char *text, const char *name)
: Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
m_function_text(ExpressionSourceCode::g_expression_prefix),
m_function_name(name) {
if (text && text[0])
m_function_text.append(text);
}
UtilityFunction::~UtilityFunction() {
lldb::ProcessSP process_sp(m_jit_process_wp.lock());
if (process_sp) {
lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
if (jit_module_sp)
process_sp->GetTarget().GetImages().Remove(jit_module_sp);
}
}
// FIXME: We should check that every time this is called it is called with the
// same return type & arguments...
FunctionCaller *UtilityFunction::MakeFunctionCaller(
const CompilerType &return_type, const ValueList &arg_value_list,
lldb::ThreadSP thread_to_use_sp, Status &error) {
if (m_caller_up)
return m_caller_up.get();
ProcessSP process_sp = m_jit_process_wp.lock();
if (!process_sp) {
error.SetErrorString("Can't make a function caller without a process.");
return nullptr;
}
Address impl_code_address;
impl_code_address.SetOffset(StartAddress());
std::string name(m_function_name);
name.append("-caller");
m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
error));
if (error.Fail()) {
return nullptr;
}
if (m_caller_up) {
DiagnosticManager diagnostics;
unsigned num_errors =
m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
if (num_errors) {
error.SetErrorStringWithFormat(
"Error compiling %s caller function: \"%s\".",
m_function_name.c_str(), diagnostics.GetString().c_str());
m_caller_up.reset();
return nullptr;
}
diagnostics.Clear();
ExecutionContext exe_ctx(process_sp);
if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
error.SetErrorStringWithFormat(
"Error inserting caller function for %s: \"%s\".",
m_function_name.c_str(), diagnostics.GetString().c_str());
m_caller_up.reset();
return nullptr;
}
}
return m_caller_up.get();
}