diff options
Diffstat (limited to 'contrib/llvm-project/lldb/source/Plugins/Process/POSIX')
6 files changed, 378 insertions, 0 deletions
diff --git a/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/CrashReason.cpp b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/CrashReason.cpp new file mode 100644 index 000000000000..d93b7cd0ce56 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/CrashReason.cpp @@ -0,0 +1,31 @@ +//===-- CrashReason.cpp ---------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "CrashReason.h" + +#include "lldb/Target/UnixSignals.h" + +std::string GetCrashReasonString(const siginfo_t &info) { +#if defined(si_lower) && defined(si_upper) + std::optional<lldb::addr_t> lower = + reinterpret_cast<lldb::addr_t>(info.si_lower); + std::optional<lldb::addr_t> upper = + reinterpret_cast<lldb::addr_t>(info.si_upper); +#else + std::optional<lldb::addr_t> lower; + std::optional<lldb::addr_t> upper; +#endif + + std::string description = + lldb_private::UnixSignals::CreateForHost()->GetSignalDescription( + info.si_signo, info.si_code, + reinterpret_cast<uintptr_t>(info.si_addr), lower, upper); + assert(description.size() && "unexpected signal"); + + return "signal " + description; +} diff --git a/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/CrashReason.h b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/CrashReason.h new file mode 100644 index 000000000000..2177726b76f0 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/CrashReason.h @@ -0,0 +1,20 @@ +//===-- CrashReason.h -------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_CrashReason_H_ +#define liblldb_CrashReason_H_ + +#include "lldb/lldb-types.h" + +#include <csignal> + +#include <string> + +std::string GetCrashReasonString(const siginfo_t &info); + +#endif // #ifndef liblldb_CrashReason_H_ diff --git a/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/NativeProcessELF.cpp b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/NativeProcessELF.cpp new file mode 100644 index 000000000000..23e94a5f55e2 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/NativeProcessELF.cpp @@ -0,0 +1,189 @@ +//===-- NativeProcessELF.cpp ----------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "NativeProcessELF.h" + +#include "lldb/Utility/DataExtractor.h" +#include <optional> + +namespace lldb_private { + +std::optional<uint64_t> +NativeProcessELF::GetAuxValue(enum AuxVector::EntryType type) { + if (m_aux_vector == nullptr) { + auto buffer_or_error = GetAuxvData(); + if (!buffer_or_error) + return std::nullopt; + DataExtractor auxv_data(buffer_or_error.get()->getBufferStart(), + buffer_or_error.get()->getBufferSize(), + GetByteOrder(), GetAddressByteSize()); + m_aux_vector = std::make_unique<AuxVector>(auxv_data); + } + + return m_aux_vector->GetAuxValue(type); +} + +lldb::addr_t NativeProcessELF::GetSharedLibraryInfoAddress() { + if (!m_shared_library_info_addr) { + if (GetAddressByteSize() == 8) + m_shared_library_info_addr = + GetELFImageInfoAddress<llvm::ELF::Elf64_Ehdr, llvm::ELF::Elf64_Phdr, + llvm::ELF::Elf64_Dyn>(); + else + m_shared_library_info_addr = + GetELFImageInfoAddress<llvm::ELF::Elf32_Ehdr, llvm::ELF::Elf32_Phdr, + llvm::ELF::Elf32_Dyn>(); + } + + return *m_shared_library_info_addr; +} + +template <typename ELF_EHDR, typename ELF_PHDR, typename ELF_DYN> +lldb::addr_t NativeProcessELF::GetELFImageInfoAddress() { + std::optional<uint64_t> maybe_phdr_addr = + GetAuxValue(AuxVector::AUXV_AT_PHDR); + std::optional<uint64_t> maybe_phdr_entry_size = + GetAuxValue(AuxVector::AUXV_AT_PHENT); + std::optional<uint64_t> maybe_phdr_num_entries = + GetAuxValue(AuxVector::AUXV_AT_PHNUM); + if (!maybe_phdr_addr || !maybe_phdr_entry_size || !maybe_phdr_num_entries) + return LLDB_INVALID_ADDRESS; + lldb::addr_t phdr_addr = *maybe_phdr_addr; + size_t phdr_entry_size = *maybe_phdr_entry_size; + size_t phdr_num_entries = *maybe_phdr_num_entries; + + // Find the PT_DYNAMIC segment (.dynamic section) in the program header and + // what the load bias by calculating the difference of the program header + // load address and its virtual address. + lldb::offset_t load_bias; + bool found_load_bias = false; + lldb::addr_t dynamic_section_addr = 0; + uint64_t dynamic_section_size = 0; + bool found_dynamic_section = false; + ELF_PHDR phdr_entry; + for (size_t i = 0; i < phdr_num_entries; i++) { + size_t bytes_read; + auto error = ReadMemory(phdr_addr + i * phdr_entry_size, &phdr_entry, + sizeof(phdr_entry), bytes_read); + if (!error.Success()) + return LLDB_INVALID_ADDRESS; + if (phdr_entry.p_type == llvm::ELF::PT_PHDR) { + load_bias = phdr_addr - phdr_entry.p_vaddr; + found_load_bias = true; + } + + if (phdr_entry.p_type == llvm::ELF::PT_DYNAMIC) { + dynamic_section_addr = phdr_entry.p_vaddr; + dynamic_section_size = phdr_entry.p_memsz; + found_dynamic_section = true; + } + } + + if (!found_load_bias || !found_dynamic_section) + return LLDB_INVALID_ADDRESS; + + // Find the DT_DEBUG entry in the .dynamic section + dynamic_section_addr += load_bias; + ELF_DYN dynamic_entry; + size_t dynamic_num_entries = dynamic_section_size / sizeof(dynamic_entry); + for (size_t i = 0; i < dynamic_num_entries; i++) { + size_t bytes_read; + auto error = ReadMemory(dynamic_section_addr + i * sizeof(dynamic_entry), + &dynamic_entry, sizeof(dynamic_entry), bytes_read); + if (!error.Success()) + return LLDB_INVALID_ADDRESS; + // Return the &DT_DEBUG->d_ptr which points to r_debug which contains the + // link_map. + if (dynamic_entry.d_tag == llvm::ELF::DT_DEBUG) { + return dynamic_section_addr + i * sizeof(dynamic_entry) + + sizeof(dynamic_entry.d_tag); + } + } + + return LLDB_INVALID_ADDRESS; +} + +template lldb::addr_t NativeProcessELF::GetELFImageInfoAddress< + llvm::ELF::Elf32_Ehdr, llvm::ELF::Elf32_Phdr, llvm::ELF::Elf32_Dyn>(); +template lldb::addr_t NativeProcessELF::GetELFImageInfoAddress< + llvm::ELF::Elf64_Ehdr, llvm::ELF::Elf64_Phdr, llvm::ELF::Elf64_Dyn>(); + +template <typename T> +llvm::Expected<SVR4LibraryInfo> +NativeProcessELF::ReadSVR4LibraryInfo(lldb::addr_t link_map_addr) { + ELFLinkMap<T> link_map; + size_t bytes_read; + auto error = + ReadMemory(link_map_addr, &link_map, sizeof(link_map), bytes_read); + if (!error.Success()) + return error.ToError(); + + char name_buffer[PATH_MAX]; + llvm::Expected<llvm::StringRef> string_or_error = ReadCStringFromMemory( + link_map.l_name, &name_buffer[0], sizeof(name_buffer), bytes_read); + if (!string_or_error) + return string_or_error.takeError(); + + SVR4LibraryInfo info; + info.name = string_or_error->str(); + info.link_map = link_map_addr; + info.base_addr = link_map.l_addr; + info.ld_addr = link_map.l_ld; + info.next = link_map.l_next; + + return info; +} + +llvm::Expected<std::vector<SVR4LibraryInfo>> +NativeProcessELF::GetLoadedSVR4Libraries() { + // Address of DT_DEBUG.d_ptr which points to r_debug + lldb::addr_t info_address = GetSharedLibraryInfoAddress(); + if (info_address == LLDB_INVALID_ADDRESS) + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "Invalid shared library info address"); + // Address of r_debug + lldb::addr_t address = 0; + size_t bytes_read; + auto status = + ReadMemory(info_address, &address, GetAddressByteSize(), bytes_read); + if (!status.Success()) + return status.ToError(); + if (address == 0) + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "Invalid r_debug address"); + // Read r_debug.r_map + lldb::addr_t link_map = 0; + status = ReadMemory(address + GetAddressByteSize(), &link_map, + GetAddressByteSize(), bytes_read); + if (!status.Success()) + return status.ToError(); + if (link_map == 0) + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "Invalid link_map address"); + + std::vector<SVR4LibraryInfo> library_list; + while (link_map) { + llvm::Expected<SVR4LibraryInfo> info = + GetAddressByteSize() == 8 ? ReadSVR4LibraryInfo<uint64_t>(link_map) + : ReadSVR4LibraryInfo<uint32_t>(link_map); + if (!info) + return info.takeError(); + if (!info->name.empty() && info->base_addr != 0) + library_list.push_back(*info); + link_map = info->next; + } + + return library_list; +} + +void NativeProcessELF::NotifyDidExec() { + NativeProcessProtocol::NotifyDidExec(); + m_shared_library_info_addr.reset(); +} + +} // namespace lldb_private diff --git a/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/NativeProcessELF.h b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/NativeProcessELF.h new file mode 100644 index 000000000000..937def94436b --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/NativeProcessELF.h @@ -0,0 +1,65 @@ +//===-- NativeProcessELF.h ------------------------------------ -*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_NativeProcessELF_H_ +#define liblldb_NativeProcessELF_H_ + +#include "Plugins/Process/Utility/AuxVector.h" +#include "lldb/Host/common/NativeProcessProtocol.h" +#include "llvm/BinaryFormat/ELF.h" +#include <optional> + +namespace lldb_private { + +/// \class NativeProcessELF +/// Abstract class that extends \a NativeProcessProtocol with ELF specific +/// logic. Meant to be subclassed by ELF based NativeProcess* implementations. +class NativeProcessELF : public NativeProcessProtocol { + using NativeProcessProtocol::NativeProcessProtocol; + +public: + std::optional<uint64_t> GetAuxValue(enum AuxVector::EntryType type); + +protected: + template <typename T> struct ELFLinkMap { + T l_addr; + T l_name; + T l_ld; + T l_next; + T l_prev; + }; + + lldb::addr_t GetSharedLibraryInfoAddress() override; + + template <typename ELF_EHDR, typename ELF_PHDR, typename ELF_DYN> + lldb::addr_t GetELFImageInfoAddress(); + + llvm::Expected<std::vector<SVR4LibraryInfo>> + GetLoadedSVR4Libraries() override; + + template <typename T> + llvm::Expected<SVR4LibraryInfo> + ReadSVR4LibraryInfo(lldb::addr_t link_map_addr); + + void NotifyDidExec() override; + + std::unique_ptr<AuxVector> m_aux_vector; + std::optional<lldb::addr_t> m_shared_library_info_addr; +}; + +// Explicitly declare the two 32/64 bit templates that NativeProcessELF.cpp will +// define. This allows us to keep the template definition here and usable +// elsewhere. +extern template lldb::addr_t NativeProcessELF::GetELFImageInfoAddress< + llvm::ELF::Elf32_Ehdr, llvm::ELF::Elf32_Phdr, llvm::ELF::Elf32_Dyn>(); +extern template lldb::addr_t NativeProcessELF::GetELFImageInfoAddress< + llvm::ELF::Elf64_Ehdr, llvm::ELF::Elf64_Phdr, llvm::ELF::Elf64_Dyn>(); + +} // namespace lldb_private + +#endif diff --git a/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/ProcessPOSIXLog.cpp b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/ProcessPOSIXLog.cpp new file mode 100644 index 000000000000..7ad88aabc2c0 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/ProcessPOSIXLog.cpp @@ -0,0 +1,34 @@ +//===-- ProcessPOSIXLog.cpp -----------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "ProcessPOSIXLog.h" + +#include "llvm/Support/Threading.h" + +using namespace lldb_private; + +static constexpr Log::Category g_categories[] = { + {{"break"}, {"log breakpoints"}, POSIXLog::Breakpoints}, + {{"memory"}, {"log memory reads and writes"}, POSIXLog::Memory}, + {{"process"}, {"log process events and activities"}, POSIXLog::Process}, + {{"ptrace"}, {"log all calls to ptrace"}, POSIXLog::Ptrace}, + {{"registers"}, {"log register read/writes"}, POSIXLog::Registers}, + {{"thread"}, {"log thread events and activities"}, POSIXLog::Thread}, + {{"watch"}, {"log watchpoint related activities"}, POSIXLog::Watchpoints}, +}; + +static Log::Channel g_channel(g_categories, POSIXLog::Process); + +template <> Log::Channel &lldb_private::LogChannelFor<POSIXLog>() { + return g_channel; +} + +void ProcessPOSIXLog::Initialize() { + static llvm::once_flag g_once_flag; + llvm::call_once(g_once_flag, []() { Log::Register("posix", g_channel); }); +} diff --git a/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/ProcessPOSIXLog.h b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/ProcessPOSIXLog.h new file mode 100644 index 000000000000..88cfdfd204c0 --- /dev/null +++ b/contrib/llvm-project/lldb/source/Plugins/Process/POSIX/ProcessPOSIXLog.h @@ -0,0 +1,39 @@ +//===-- ProcessPOSIXLog.h -----------------------------------------*- C++ +//-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ProcessPOSIXLog_h_ +#define liblldb_ProcessPOSIXLog_h_ + +#include "lldb/Utility/Log.h" +#include "llvm/ADT/BitmaskEnum.h" + +namespace lldb_private { + +enum class POSIXLog : Log::MaskType { + Breakpoints = Log::ChannelFlag<0>, + Memory = Log::ChannelFlag<1>, + Process = Log::ChannelFlag<2>, + Ptrace = Log::ChannelFlag<3>, + Registers = Log::ChannelFlag<4>, + Thread = Log::ChannelFlag<5>, + Watchpoints = Log::ChannelFlag<6>, + Trace = Log::ChannelFlag<7>, + LLVM_MARK_AS_BITMASK_ENUM(Trace) +}; +LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); + +class ProcessPOSIXLog { +public: + static void Initialize(); +}; + +template <> Log::Channel &LogChannelFor<POSIXLog>(); +} // namespace lldb_private + +#endif // liblldb_ProcessPOSIXLog_h_ |