From 03e1a82f52d219225c22f14ac73966bb97d4fd0d Mon Sep 17 00:00:00 2001 From: Alex Langford Date: Wed, 29 May 2019 18:08:22 +0000 Subject: [PATCH] [Target] Introduce Process::GetLanguageRuntimes Summary: Currently there's not really a good way to iterate over the language runtimes a process has. This is sometimes desirable (as seen in my change to Thread). Additionally, there's not really a good reason to iterate over every available language, but rather only over languages for which we have a plugin loaded. Reviewers: JDevlieghere, davide, jingham Subscribers: lldb-commits Differential Revision: https://reviews.llvm.org/D62562 llvm-svn: 361999 --- lldb/include/lldb/Target/Language.h | 2 ++ lldb/include/lldb/Target/Process.h | 3 +++ lldb/source/Target/Language.cpp | 9 +++++++++ lldb/source/Target/Process.cpp | 22 ++++++++++++++++++++++ lldb/source/Target/Thread.cpp | 16 ++++++---------- 5 files changed, 42 insertions(+), 10 deletions(-) diff --git a/lldb/include/lldb/Target/Language.h b/lldb/include/lldb/Target/Language.h index b45a51f..6ea6029 100644 --- a/lldb/include/lldb/Target/Language.h +++ b/lldb/include/lldb/Target/Language.h @@ -264,6 +264,8 @@ public: // etc. static lldb::LanguageType GetPrimaryLanguage(lldb::LanguageType language); + static std::set GetSupportedLanguages(); + static void GetLanguagesSupportingTypeSystems( std::set &languages, std::set &languages_for_expressions); diff --git a/lldb/include/lldb/Target/Process.h b/lldb/include/lldb/Target/Process.h index 90118c8..2657302 100644 --- a/lldb/include/lldb/Target/Process.h +++ b/lldb/include/lldb/Target/Process.h @@ -2178,6 +2178,9 @@ public: OperatingSystem *GetOperatingSystem() { return m_os_up.get(); } + std::vector + GetLanguageRuntimes(bool retry_if_null = true); + LanguageRuntime *GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null = true); diff --git a/lldb/source/Target/Language.cpp b/lldb/source/Target/Language.cpp index a1a388d..3c3ef28 100644 --- a/lldb/source/Target/Language.cpp +++ b/lldb/source/Target/Language.cpp @@ -348,6 +348,15 @@ LanguageType Language::GetPrimaryLanguage(LanguageType language) { } } +std::set Language::GetSupportedLanguages() { + std::set supported_languages; + ForEach([&](Language *lang) { + supported_languages.emplace(lang->GetLanguageType()); + return true; + }); + return supported_languages; +} + void Language::GetLanguagesSupportingTypeSystems( std::set &languages, std::set &languages_for_expressions) { diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp index 4162571..c88ef0dc 100644 --- a/lldb/source/Target/Process.cpp +++ b/lldb/source/Target/Process.cpp @@ -44,6 +44,7 @@ #include "lldb/Target/InstrumentationRuntime.h" #include "lldb/Target/JITLoader.h" #include "lldb/Target/JITLoaderList.h" +#include "lldb/Target/Language.h" #include "lldb/Target/LanguageRuntime.h" #include "lldb/Target/MemoryHistory.h" #include "lldb/Target/MemoryRegionInfo.h" @@ -1547,6 +1548,27 @@ const lldb::ABISP &Process::GetABI() { return m_abi_sp; } +std::vector +Process::GetLanguageRuntimes(bool retry_if_null) { + std::vector language_runtimes; + + if (m_finalizing) + return language_runtimes; + + std::lock_guard guard(m_language_runtimes_mutex); + // Before we pass off a copy of the language runtimes, we must make sure that + // our collection is properly populated. It's possible that some of the + // language runtimes were not loaded yet, either because nobody requested it + // yet or the proper condition for loading wasn't yet met (e.g. libc++.so + // hadn't been loaded). + for (const lldb::LanguageType lang_type : Language::GetSupportedLanguages()) { + if (LanguageRuntime *runtime = GetLanguageRuntime(lang_type, retry_if_null)) + language_runtimes.emplace_back(runtime); + } + + return language_runtimes; +} + LanguageRuntime *Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null) { if (m_finalizing) diff --git a/lldb/source/Target/Thread.cpp b/lldb/source/Target/Thread.cpp index 3908652..f248e6b 100644 --- a/lldb/source/Target/Thread.cpp +++ b/lldb/source/Target/Thread.cpp @@ -2211,11 +2211,9 @@ ValueObjectSP Thread::GetCurrentException() { // NOTE: Even though this behavior is generalized, only ObjC is actually // supported at the moment. - for (unsigned lang = eLanguageTypeUnknown; lang < eNumLanguageTypes; lang++) { - if (auto runtime = GetProcess()->GetLanguageRuntime( - static_cast(lang))) - if (auto e = runtime->GetExceptionObjectForThread(shared_from_this())) - return e; + for (LanguageRuntime *runtime : GetProcess()->GetLanguageRuntimes()) { + if (auto e = runtime->GetExceptionObjectForThread(shared_from_this())) + return e; } return ValueObjectSP(); @@ -2228,11 +2226,9 @@ ThreadSP Thread::GetCurrentExceptionBacktrace() { // NOTE: Even though this behavior is generalized, only ObjC is actually // supported at the moment. - for (unsigned lang = eLanguageTypeUnknown; lang < eNumLanguageTypes; lang++) { - if (auto runtime = GetProcess()->GetLanguageRuntime( - static_cast(lang))) - if (auto bt = runtime->GetBacktraceThreadFromException(exception)) - return bt; + for (LanguageRuntime *runtime : GetProcess()->GetLanguageRuntimes()) { + if (auto bt = runtime->GetBacktraceThreadFromException(exception)) + return bt; } return ThreadSP(); -- 2.7.4