Index: lldb/include/lldb/Expression/UtilityFunction.h =================================================================== --- lldb/include/lldb/Expression/UtilityFunction.h +++ lldb/include/lldb/Expression/UtilityFunction.h @@ -43,7 +43,7 @@ /// \param[in] name /// The name of the function, as used in the text. UtilityFunction(ExecutionContextScope &exe_scope, std::string text, - std::string name); + std::string name, bool debug); ~UtilityFunction() override; Index: lldb/include/lldb/Symbol/TypeSystem.h =================================================================== --- lldb/include/lldb/Symbol/TypeSystem.h +++ lldb/include/lldb/Symbol/TypeSystem.h @@ -470,7 +470,7 @@ } virtual std::unique_ptr - CreateUtilityFunction(std::string text, std::string name); + CreateUtilityFunction(std::string text, std::string name, bool debug = false); virtual PersistentExpressionState *GetPersistentExpressionState() { return nullptr; Index: lldb/include/lldb/Target/Target.h =================================================================== --- lldb/include/lldb/Target/Target.h +++ lldb/include/lldb/Target/Target.h @@ -227,6 +227,10 @@ void UpdateLaunchInfoFromProperties(); + void SetDebugUtilityExpression(bool debug); + + bool GetDebugUtilityExpression() const; + private: // Callbacks for m_launch_info. void Arg0ValueChangedCallback(); Index: lldb/source/Expression/FunctionCaller.cpp =================================================================== --- lldb/source/Expression/FunctionCaller.cpp +++ lldb/source/Expression/FunctionCaller.cpp @@ -100,8 +100,7 @@ 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, - true /* notify */); + process->GetTarget().GetImages().Append(jit_module_sp, true /* notify */); } } if (process && m_jit_start_addr) @@ -317,12 +316,15 @@ 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. + // result. Unless explicitly asked for, ignore breakpoints and unwind on + // error. + const bool debug = exe_ctx.GetTargetPtr() && + exe_ctx.GetTargetPtr()->GetDebugUtilityExpression(); EvaluateExpressionOptions real_options = options; real_options.SetDebug(false); - real_options.SetUnwindOnError(true); - real_options.SetIgnoreBreakpoints(true); + real_options.SetGenerateDebugInfo(debug); + real_options.SetUnwindOnError(!debug); + real_options.SetIgnoreBreakpoints(!debug); lldb::addr_t args_addr; Index: lldb/source/Expression/UtilityFunction.cpp =================================================================== --- lldb/source/Expression/UtilityFunction.cpp +++ lldb/source/Expression/UtilityFunction.cpp @@ -41,7 +41,7 @@ /// \param[in] name /// The name of the function, as used in the text. UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope, - std::string text, std::string name) + std::string text, std::string name, bool debug) : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(), m_function_text(std::move(text)), m_function_name(std::move(name)) {} Index: lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp =================================================================== --- lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp +++ lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp @@ -35,7 +35,7 @@ const llvm::StringRef ClangExpressionSourceCode::g_prefix_file_name = PREFIX_NAME; const char *ClangExpressionSourceCode::g_expression_prefix = -"#line 1 \"" PREFIX_NAME R"(" + "#line 1 \"" PREFIX_NAME R"("#line 1 #ifndef offsetof #define offsetof(t, d) __builtin_offsetof(t, d) #endif Index: lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.h =================================================================== --- lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.h +++ lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.h @@ -49,7 +49,7 @@ /// \param[in] name /// The name of the function, as used in the text. ClangUtilityFunction(ExecutionContextScope &exe_scope, std::string text, - std::string name); + std::string name, bool debug = false); ~ClangUtilityFunction() override; Index: lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp =================================================================== --- lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp +++ lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp @@ -42,11 +42,31 @@ /// \param[in] name /// The name of the function, as used in the text. ClangUtilityFunction::ClangUtilityFunction(ExecutionContextScope &exe_scope, - std::string text, std::string name) + std::string text, std::string name, + bool debug) : UtilityFunction( exe_scope, std::string(ClangExpressionSourceCode::g_expression_prefix) + text, - std::move(name)) {} + std::move(name), debug) { + if (debug) { + int temp_fd = -1; + llvm::SmallString<128> result_path; + llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path); + if (temp_fd != -1) { + lldb_private::NativeFile file(temp_fd, File::eOpenOptionWrite, true); + text = "#line 1 \"" + std::string(result_path) + "\"\n" + text; + size_t bytes_written = text.size(); + file.Write(text.c_str(), bytes_written); + if (bytes_written == text.size()) { + // If we successfully wrote the source to a temporary file, replace the + // function text with the next text containing the line directive. + m_function_text = + std::string(ClangExpressionSourceCode::g_expression_prefix) + text; + } + file.Close(); + } + } +} ClangUtilityFunction::~ClangUtilityFunction() {} Index: lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp =================================================================== --- lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp +++ lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp @@ -172,7 +172,6 @@ // Testing using the new C++11 raw string literals. If this breaks GCC then we // will need to revert to the code above... static const char *g_get_shared_cache_class_info_body = R"( - extern "C" { size_t strlen(const char *); @@ -1592,9 +1591,8 @@ if (objc_runtime) { for (lldb::ModuleSP mod_sp : process->GetTarget().GetImages().Modules()) { if (objc_runtime->IsModuleObjCLibrary(mod_sp)) { - const Symbol *symbol = - mod_sp->FindFirstSymbolWithNameAndType(g_class_getNameRaw_symbol_name, - lldb::eSymbolTypeCode); + const Symbol *symbol = mod_sp->FindFirstSymbolWithNameAndType( + g_class_getNameRaw_symbol_name, lldb::eSymbolTypeCode); if (symbol && (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) { class_name_getter_function_name = g_class_getNameRaw_symbol_name; Index: lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h =================================================================== --- lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h +++ lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h @@ -1186,8 +1186,9 @@ const ValueList &arg_value_list, const char *name) override; - std::unique_ptr - CreateUtilityFunction(std::string text, std::string name) override; + std::unique_ptr CreateUtilityFunction(std::string text, + std::string name, + bool debug) override; PersistentExpressionState *GetPersistentExpressionState() override; Index: lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp =================================================================== --- lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -9712,13 +9712,13 @@ std::unique_ptr ScratchTypeSystemClang::CreateUtilityFunction(std::string text, - std::string name) { + std::string name, bool debug) { TargetSP target_sp = m_target_wp.lock(); if (!target_sp) return {}; return std::make_unique( - *target_sp.get(), std::move(text), std::move(name)); + *target_sp.get(), std::move(text), std::move(name), debug); } PersistentExpressionState * Index: lldb/source/Symbol/TypeSystem.cpp =================================================================== --- lldb/source/Symbol/TypeSystem.cpp +++ lldb/source/Symbol/TypeSystem.cpp @@ -172,7 +172,8 @@ } std::unique_ptr -TypeSystem::CreateUtilityFunction(std::string text, std::string name) { +TypeSystem::CreateUtilityFunction(std::string text, std::string name, + bool debug) { return {}; } Index: lldb/source/Target/Target.cpp =================================================================== --- lldb/source/Target/Target.cpp +++ lldb/source/Target/Target.cpp @@ -2303,8 +2303,8 @@ return type_system_or_err.takeError(); std::unique_ptr utility_fn = - type_system_or_err->CreateUtilityFunction(std::move(expression), - std::move(name)); + type_system_or_err->CreateUtilityFunction( + std::move(expression), std::move(name), GetDebugUtilityExpression()); if (!utility_fn) return llvm::make_error( llvm::StringRef("Could not create an expression for language") + @@ -4307,6 +4307,17 @@ m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO); } +bool TargetProperties::GetDebugUtilityExpression() const { + const uint32_t idx = ePropertyDebugUtilityExpression; + return m_collection_sp->GetPropertyAtIndexAsBoolean( + nullptr, idx, g_target_properties[idx].default_uint_value != 0); +} + +void TargetProperties::SetDebugUtilityExpression(bool debug) { + const uint32_t idx = ePropertyDebugUtilityExpression; + m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, debug); +} + // Target::TargetEventData Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp) Index: lldb/source/Target/TargetProperties.td =================================================================== --- lldb/source/Target/TargetProperties.td +++ lldb/source/Target/TargetProperties.td @@ -172,6 +172,9 @@ def AutoInstallMainExecutable: Property<"auto-install-main-executable", "Boolean">, DefaultTrue, Desc<"Always install the main executable when connected to a remote platform.">; + def DebugUtilityExpression: Property<"debug-utility-expression", "Boolean">, + DefaultFalse, + Desc<"Enable debugging of LLDB-internal utility expressions.">; } let Definition = "process_experimental" in {