Skip to content

Commit

Permalink
Handle corner case of loading modules from NT_FILE section
Browse files Browse the repository at this point in the history
Summary:
In the NT_FILE section of a core file, the path to module list could be followed by "(deleted)" , also path like "/dev/zero" could be in the list.
1. Regex for removing parenthesized suffix, so trailing "(deleted)" will be removed.
2. Regex for filter out modules to load.

Test Plan:
Before change:
```
(lldb) target create --core "~/9s728w5ktv6jaraw"
Core file '/home/hyubo/9s728w5ktv6jaraw' (x86_64) was loaded.
(lldb) bt
* thread #1, name = 'fbflow', stop reason = signal SIGSEGV
  * frame #0: 0x000000000974350e fbflow (deleted)
    frame #1: 0x0000000009744270 fbflow (deleted)
    frame #2: 0x000000000285b27e fbflow (deleted)
    frame llvm#3: 0x0000000008a47872 fbflow (deleted)
    frame llvm#4: 0x0000000008a46fe0 fbflow (deleted)
    frame llvm#5: 0x0000000008a45429 fbflow (deleted)
    frame llvm#6: 0x0000000008a46708 fbflow (deleted)
    frame llvm#7: 0x0000000008a45fb4 fbflow (deleted)
    frame llvm#8: 0x0000000008a44cf4 fbflow (deleted)
    frame llvm#9: 0x00000000027422c2 fbflow (deleted)
    frame llvm#10: 0x0000000002744b7e fbflow (deleted)
    frame llvm#11: 0x00007f6395a2c657 libc.so.6 (deleted)
    frame llvm#12: 0x00007f6395a2c718 libc.so.6 (deleted)
```

After change:
```
(lldb) target create --core "~/9s728w5ktv6jaraw"
Core file '/home/hyubo/9s728w5ktv6jaraw' (x86_64) was loaded.
(lldb) bt
* thread #1, name = 'fbflow', stop reason = signal SIGSEGV
  * frame #0: 0x000000000974350e fbflow`void folly::futures::detail::coreDetachPromiseMaybeWithResult<facebook::cryptocat::store::SingleResult>(folly::futures::detail::Core<facebook::cryptocat::store::SingleResult>&) + 143
    frame #1: 0x0000000009744270 fbflow`void folly::detail::function::FunctionTraits<void (folly::futures::detail::CoreBase&, folly::Executor::KeepAlive<folly::Executor>&&, folly::exception_wrapper*)>::callBig<void folly::futures::detail::Core<facebook::cryptocat::store::SingleResult>::setCallback<std::enable_if<folly::futures::detail::tryExecutorCallableResult<facebook::cryptocat::store::SingleResult, folly::Future<folly::futures::detail::tryCallableResult<facebook::cryptocat::store::SingleResult, facebook::cryptocat::store::SingleRequest, std::enable_if<is_invocable_v<facebook::cryptocat::store::SingleRequest, folly::Try<facebook::cryptocat::store::SingleResult>&&>, void>::type>::value_type> folly::Future<facebook::cryptocat::store::SingleResult>::thenTryInline<folly::SemiFuture<facebook::cryptocat::store::SingleResult> folly::SemiFuture<facebook::cryptocat::store::SingleResult>::deferError<folly::SemiFuture<facebook::cryptocat::store::SingleRequest::Result_t> facebook::cryptocat::store::DerivedCryptoStore::semifuture_loadImpl<facebook::cryptocat::store::SingleRequest>(facebook::cryptocat::store::SingleRequest const&) const::'lambda'(facebook::cryptocat::store::SingleRequest&&)>(facebook::cryptocat::store::SingleRequest&&) &&::'lambda'(folly::Try<facebook::cryptocat::store::SingleResult>)>(facebook::cryptocat::store::SingleRequest&&) &&::'lambda'(folly::Executor::KeepAlive<folly::Executor>&&, folly::Try<facebook::cryptocat::store::SingleResult>&&), void>::ReturnsFuture::value, folly::futures::detail::tryExecutorCallableResult<facebook::cryptocat::store::SingleResult, folly::Future<folly::futures::detail::tryCallableResult<facebook::cryptocat::store::SingleResult, facebook::cryptocat::store::SingleRequest, std::enable_if<is_invocable_v<facebook::cryptocat::store::SingleRequest, folly::Try<facebook::cryptocat::store::SingleResult>&&>, void>::type>::value_type> folly::Future<facebook::cryptocat::store::SingleResult>::thenTryInline<folly::SemiFuture<facebook::cryptocat::store::SingleResult> folly::SemiFuture<facebook::cryptocat::store::SingleResult>::deferError<folly::SemiFuture<facebook::cryptocat::store::SingleRequest::Result_t> facebook::cryptocat::store::DerivedCryptoStore::semifuture_loadImpl<facebook::cryptocat::store::SingleRequest>(facebook::cryptocat::store::SingleRequest const&) const::'lambda'(facebook::cryptocat::store::SingleRequest&&)>(facebook::cryptocat::store::SingleRequest&&) &&::'lambda'(folly::Try<facebook::cryptocat::store::SingleResult>)>(facebook::cryptocat::store::SingleRequest&&) &&::'lambda'(folly::Executor::KeepAlive<folly::Executor>&&, folly::Try<facebook::cryptocat::store::SingleResult>&&), void>::Return>::type folly::futures::detail::FutureBase<facebook::cryptocat::store::SingleResult>::thenImplementation<folly::Future<folly::futures::detail::tryCallableResult<facebook::cryptocat::store::SingleResult, facebook::cryptocat::store::SingleRequest, std::enable_if<is_invocable_v<facebook::cryptocat::store::SingleRequest, folly::Try<facebook::cryptocat::store::SingleResult>&&>, void>::type>::value_type> folly::Future<facebook::cryptocat::store::SingleResult>::thenTryInline<folly::SemiFuture<facebook::cryptocat::store::SingleResult> folly::SemiFuture<facebook::cryptocat::store::SingleResult>::deferError<folly::SemiFuture<facebook::cryptocat::store::SingleRequest::Result_t> facebook::cryptocat::store::DerivedCryptoStore::semifuture_loadImpl<facebook::cryptocat::store::SingleRequest>(facebook::cryptocat::store::SingleRequest const&) const::'lambda'(facebook::cryptocat::store::SingleRequest&&)>(facebook::cryptocat::store::SingleRequest&&) &&::'lambda'(folly::Try<facebook::cryptocat::store::SingleResult>)>(facebook::cryptocat::store::SingleRequest&&) &&::'lambda'(folly::Executor::KeepAlive<folly::Executor>&&, folly::Try<facebook::cryptocat::store::SingleResult>&&), folly::futures::detail::tryExecutorCallableResult<facebook::cryptocat::store::SingleResult, folly::Future<folly::futures::detail::tryCallableResult<facebook::cryptocat::store::SingleResult, facebook::cryptocat::store::SingleRequest, std::enable_if<is_invocable_v<facebook::cryptocat::store::SingleRequest, folly::Try<facebook::cryptocat::store::SingleResult>&&>, void>::type>::value_type> folly::Future<facebook::cryptocat::store::SingleResult>::thenTryInline<folly::SemiFuture<facebook::cryptocat::store::SingleResult> folly::SemiFuture<facebook::cryptocat::store::SingleResult>::deferError<folly::SemiFuture<facebook::cryptocat::store::SingleRequest::Result_t> facebook::cryptocat::store::DerivedCryptoStore::semifuture_loadImpl<facebook::cryptocat::store::SingleRequest>(facebook::cryptocat::store::SingleRequest const&) const::'lambda'(facebook::cryptocat::store::SingleRequest&&)>(facebook::cryptocat::store::SingleRequest&&) &&::'lambda'(folly::Try<facebook::cryptocat::store::SingleResult>)>(facebook::cryptocat::store::SingleRequest&&) &&::'lambda'(folly::Executor::KeepAlive<folly::Executor>&&, folly::Try<facebook::cryptocat::store::SingleResult>&&), void>>(facebook::cryptocat::store::SingleRequest&&, folly::futures::detail::tryExecutorCallableResult<facebook::cryptocat::store::SingleResult, folly::Future<folly::futures::detail::tryCallableResult<facebook::cryptocat::store::SingleResult, facebook::cryptocat::store::SingleRequest, std::enable_if<is_invocable_v<facebook::cryptocat::store::SingleRequest, folly::Try<facebook::cryptocat::store::SingleResult>&&>, void>::type>::value_type> folly::Future<facebook::cryptocat::store::SingleResult>::thenTryInline<folly::SemiFuture<facebook::cryptocat::store::SingleResult> folly::SemiFuture<facebook::cryptocat::store::SingleResult>::deferError<folly::SemiFuture<facebook::cryptocat::store::SingleRequest::Result_t> facebook::cryptocat::store::DerivedCryptoStore::semifuture_loadImpl<facebook::cryptocat::store::SingleRequest>(facebook::cryptocat::store::SingleRequest const&) const::'lambda'(facebook::cryptocat::store::SingleRequest&&)>(facebook::cryptocat::store::SingleRequest&&) &&::'lambda'(folly::Try<facebook::cryptocat::store::SingleResult>)>(facebook::cryptocat::store::SingleRequest&&) &&::'lambda'(folly::Executor::KeepAlive<folly::Executor>&&, folly::Try<facebook::cryptocat::store::SingleResult>&&), void>, folly::futures::detail::InlineContinuation)::'lambda'(folly::Executor::KeepAlive<folly::Executor>&&, folly::Try<facebook::cryptocat::store::SingleResult>&&)>(facebook::cryptocat::store::SingleRequest&&, std::shared_ptr<folly::RequestContext>&&, folly::futures::detail::InlineContinuation)::'lambda'(folly::futures::detail::CoreBase&, folly::Executor::KeepAlive<folly::Executor>&&, folly::exception_wrapper*)>(folly::futures::detail::CoreBase&, folly::Executor::KeepAlive<folly::Executor>&&, folly::exception_wrapper*, folly::detail::function::Data&) + 20
    frame #2: 0x000000000285b27e fbflow`bool folly::fibers::Baton::try_wait_until<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l>>, bool folly::fibers::Baton::try_wait_until<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l>>>(std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l>>> const&)::'lambda'()>(std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l>>> const&, bool folly::fibers::Baton::try_wait_until<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l>>>(std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l>>> const&)::'lambda'()&&) + 342
    frame llvm#3: 0x0000000008a47872 fbflow`facebook::logger::(anonymous namespace)::recordScubaSampleForTesting(facebook::logger::CippEventsV2UnsampledLogEntryInternal const&, long, folly::ReadMostlySharedPtr<facebook::logger::CippEventsV2UnsampledLogLineDefinition, folly::TLRefCount>, facebook::logger::WhenceScribeLogged const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) (.__uniq.66043386987841263013456630564440847059) + 3452
    frame llvm#4: 0x0000000008a46fe0 fbflow`facebook::logger::(anonymous namespace)::recordScubaSampleForTesting(facebook::logger::CippEventsV2UnsampledLogEntryInternal const&, long, folly::ReadMostlySharedPtr<facebook::logger::CippEventsV2UnsampledLogLineDefinition, folly::TLRefCount>, facebook::logger::WhenceScribeLogged const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) (.__uniq.66043386987841263013456630564440847059) + 1258
    frame llvm#5: 0x0000000008a45429 fbflow`int facebook::logger::CippEventsV2UnsampledLogger::invokeLog<std::nullptr_t, std::nullptr_t>(bool, std::nullptr_t, char const*, int, std::nullptr_t)::'lambda1'()::operator()() const + 2357
    frame llvm#6: 0x0000000008a46708 fbflow`int facebook::logger::CippEventsV2UnsampledLogger::invokeLog<std::nullptr_t, std::nullptr_t>(bool, std::nullptr_t, char const*, int, std::nullptr_t)::'lambda3'()::operator()() const + 3060
    frame llvm#7: 0x0000000008a45fb4 fbflow`int facebook::logger::CippEventsV2UnsampledLogger::invokeLog<std::nullptr_t, std::nullptr_t>(bool, std::nullptr_t, char const*, int, std::nullptr_t)::'lambda3'()::operator()() const + 1184
    frame llvm#8: 0x0000000008a44cf4 fbflow`int facebook::logger::CippEventsV2UnsampledLogger::invokeLog<std::nullptr_t, std::nullptr_t>(bool, std::nullptr_t, char const*, int, std::nullptr_t)::'lambda1'()::operator()() const + 512
...

Reviewers: jeffreytan, gclayton

Reviewed By: jeffreytan

Subscribers: #lldb_team

Differential Revision: https://phabricator.intern.facebook.com/D49565956

Tasks: T164491560
  • Loading branch information
George Hu authored and kusmour committed Sep 14, 2024
1 parent 939938e commit c009dda
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "lldb/Utility/Log.h"

#include "Plugins/ObjectFile/Placeholder/ObjectFilePlaceholder.h"
#include <regex>

using namespace lldb;
using namespace lldb_private;
Expand Down Expand Up @@ -155,14 +156,28 @@ void DynamicLoaderDumpWithModuleList::LoadAllModules(
addr_t base_addr, module_size;
std::string name;
if (!mod_info.get_base(base_addr) || !mod_info.get_name(name) ||
!mod_info.get_size(module_size))
!mod_info.get_size(module_size) || !ShouldLoadModule(name))
continue;

callback(name, base_addr, module_size);
callback(SanitizeName(name), base_addr, module_size);
}
}
}

bool DynamicLoaderDumpWithModuleList::ShouldLoadModule(
const std::string &module_name) {
// Use a regular expression to match /dev/* path
static const std::regex pattern("^/dev/.*$");
return !std::regex_match(module_name, pattern);
}

std::string
DynamicLoaderDumpWithModuleList::SanitizeName(const std::string &input) {
// Use a regular expression to match and remove the parenthesized substring
static const std::regex pattern("\\s*\\(\\S+\\)\\s*$");
return std::regex_replace(input, pattern, "");
}

void DynamicLoaderDumpWithModuleList::DidAttach() {
Log *log = GetLog(LLDBLog::DynamicLoader);
LLDB_LOGF(log, "DynamicLoaderDumpWithModuleList::%s() pid %" PRIu64,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class DynamicLoaderDumpWithModuleList : public lldb_private::DynamicLoader {
LoadModuleCallback;
void LoadAllModules(LoadModuleCallback callback);

bool ShouldLoadModule(const std::string &module_name);

std::string SanitizeName(const std::string &input);

std::optional<const LoadedModuleInfoList::LoadedModuleInfo>
GetModuleInfo(lldb::addr_t module_base_addr);

Expand Down

0 comments on commit c009dda

Please sign in to comment.