Antonio Afonso d668260f1a Correctly use GetLoadedModuleList to take advantage of libraries-svr4
Summary:
Here's a replacement for D62504. I thought I could use LoadModules to implement this but in reality I can't because there are at few issues with it:
* The LoadModules assumes that the list returned by GetLoadedModuleList is comprehensive in the sense that reflects all the mapped segments, however, this is not true, for instance VDSO entry is not there since it's loaded manually by LoadVDSO using GetMemoryRegionInfo and it doesn't represent a specific shared object in disk. Because of this LoadModules will unload the VDSO module.
* The loader (interpreter) module might have also been loaded using GetMemoryRegionInfo, this is true when we launch the process and the rendezvous structure is not yet available (done through LoadInterpreterModule()). The problem here is that this entry will point to the same file name as the one found in /proc/pid/maps, however, when we read the same module from the r_debug.link_map structure it might be under a different name. This is true at least on CentOS where the loader is a symlink. Because of this LoadModules will unload and load the module in a way where the rendezvous breakpoint is unresolved but not resolved again (because we add the new module first and remove the old one after).

The symlink issue might be fixable by first unloading the old and loading the news (but sounds super brittle), however, I'm not sure how to fix the VDSO issue.
Since I can't trust it I'm just going to use GetLoadedModuleList directly with the same logic that we use today for when we read the linked list in lldb. The only safe thing to do here is to only calculate differences between different snapshots of the svr4 packet itself. This will also cut the dependency this plugin has from LoadModules.

I separated the 2 logics into 2 different functions (remote and not remote) because I don't like mixing 2 different logics in the same function with if/else's. Two different functions makes it easier to reason with I believe. However, I did abstract away the logic that decides if we should take a snapshot or add/remove modules so both functions could reuse it.

The other difference between the two is that on the UpdateSOEntriesFromRemote I take the snapshot only once when state = Consistent because I didn't find a good reason to always update that, as we already got the list from state = Add | Remove. I probably should use the same logic on UpdateSOEntries though I don't see a reason not to since it's really using the same data, just read in different places. Any thoughts here?

It might also be worthwhile to add a test to make sure we don't unload modules that were not actually "unloaded" like the vdso. I haven't done this yet though.
This diff is also missing the option for svr4 like proposed in https://reviews.llvm.org/D62503#1564296, I'll start working on this but wanted to have this up first.

Reviewers: labath, jankratochvil, clayborg, xiaobai

Reviewed By: labath

Subscribers: srhines, JDevlieghere, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D64013

llvm-svn: 367020
2019-07-25 14:28:21 +00:00

457 lines
16 KiB
C++

//===-- ProcessGDBRemote.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_ProcessGDBRemote_h_
#define liblldb_ProcessGDBRemote_h_
#include <atomic>
#include <map>
#include <mutex>
#include <string>
#include <vector>
#include "lldb/Core/LoadedModuleInfoList.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/ThreadSafeValue.h"
#include "lldb/Host/HostThread.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/Broadcaster.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/StreamGDBRemote.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/StringExtractor.h"
#include "lldb/Utility/StringList.h"
#include "lldb/Utility/StructuredData.h"
#include "lldb/lldb-private-forward.h"
#include "GDBRemoteCommunicationClient.h"
#include "GDBRemoteCommunicationReplayServer.h"
#include "GDBRemoteRegisterContext.h"
#include "llvm/ADT/DenseMap.h"
namespace lldb_private {
namespace repro {
class Loader;
}
namespace process_gdb_remote {
class ThreadGDBRemote;
class ProcessGDBRemote : public Process,
private GDBRemoteClientBase::ContinueDelegate {
public:
ProcessGDBRemote(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp);
~ProcessGDBRemote() override;
static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp,
lldb::ListenerSP listener_sp,
const FileSpec *crash_file_path);
static void Initialize();
static void DebuggerInitialize(Debugger &debugger);
static void Terminate();
static ConstString GetPluginNameStatic();
static const char *GetPluginDescriptionStatic();
// Check if a given Process
bool CanDebug(lldb::TargetSP target_sp,
bool plugin_specified_by_name) override;
CommandObject *GetPluginCommandObject() override;
// Creating a new process, or attaching to an existing one
Status WillLaunch(Module *module) override;
Status DoLaunch(Module *exe_module, ProcessLaunchInfo &launch_info) override;
void DidLaunch() override;
Status WillAttachToProcessWithID(lldb::pid_t pid) override;
Status WillAttachToProcessWithName(const char *process_name,
bool wait_for_launch) override;
Status DoConnectRemote(Stream *strm, llvm::StringRef remote_url) override;
Status WillLaunchOrAttach();
Status DoAttachToProcessWithID(lldb::pid_t pid,
const ProcessAttachInfo &attach_info) override;
Status
DoAttachToProcessWithName(const char *process_name,
const ProcessAttachInfo &attach_info) override;
void DidAttach(ArchSpec &process_arch) override;
// PluginInterface protocol
ConstString GetPluginName() override;
uint32_t GetPluginVersion() override;
// Process Control
Status WillResume() override;
Status DoResume() override;
Status DoHalt(bool &caused_stop) override;
Status DoDetach(bool keep_stopped) override;
bool DetachRequiresHalt() override { return true; }
Status DoSignal(int signal) override;
Status DoDestroy() override;
void RefreshStateAfterStop() override;
void SetUnixSignals(const lldb::UnixSignalsSP &signals_sp);
// Process Queries
bool IsAlive() override;
lldb::addr_t GetImageInfoAddress() override;
void WillPublicStop() override;
// Process Memory
size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
Status &error) override;
Status
WriteObjectFile(std::vector<ObjectFile::LoadableData> entries) override;
size_t DoWriteMemory(lldb::addr_t addr, const void *buf, size_t size,
Status &error) override;
lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions,
Status &error) override;
Status GetMemoryRegionInfo(lldb::addr_t load_addr,
MemoryRegionInfo &region_info) override;
Status DoDeallocateMemory(lldb::addr_t ptr) override;
// Process STDIO
size_t PutSTDIN(const char *buf, size_t buf_size, Status &error) override;
// Process Breakpoints
Status EnableBreakpointSite(BreakpointSite *bp_site) override;
Status DisableBreakpointSite(BreakpointSite *bp_site) override;
// Process Watchpoints
Status EnableWatchpoint(Watchpoint *wp, bool notify = true) override;
Status DisableWatchpoint(Watchpoint *wp, bool notify = true) override;
Status GetWatchpointSupportInfo(uint32_t &num) override;
lldb::user_id_t StartTrace(const TraceOptions &options,
Status &error) override;
Status StopTrace(lldb::user_id_t uid, lldb::tid_t thread_id) override;
Status GetData(lldb::user_id_t uid, lldb::tid_t thread_id,
llvm::MutableArrayRef<uint8_t> &buffer,
size_t offset = 0) override;
Status GetMetaData(lldb::user_id_t uid, lldb::tid_t thread_id,
llvm::MutableArrayRef<uint8_t> &buffer,
size_t offset = 0) override;
Status GetTraceConfig(lldb::user_id_t uid, TraceOptions &options) override;
Status GetWatchpointSupportInfo(uint32_t &num, bool &after) override;
bool StartNoticingNewThreads() override;
bool StopNoticingNewThreads() override;
GDBRemoteCommunicationClient &GetGDBRemote() { return m_gdb_comm; }
Status SendEventData(const char *data) override;
// Override DidExit so we can disconnect from the remote GDB server
void DidExit() override;
void SetUserSpecifiedMaxMemoryTransferSize(uint64_t user_specified_max);
bool GetModuleSpec(const FileSpec &module_file_spec, const ArchSpec &arch,
ModuleSpec &module_spec) override;
void PrefetchModuleSpecs(llvm::ArrayRef<FileSpec> module_file_specs,
const llvm::Triple &triple) override;
llvm::VersionTuple GetHostOSVersion() override;
llvm::Error LoadModules() override;
llvm::Expected<LoadedModuleInfoList> GetLoadedModuleList() override;
Status GetFileLoadAddress(const FileSpec &file, bool &is_loaded,
lldb::addr_t &load_addr) override;
void ModulesDidLoad(ModuleList &module_list) override;
StructuredData::ObjectSP
GetLoadedDynamicLibrariesInfos(lldb::addr_t image_list_address,
lldb::addr_t image_count) override;
Status
ConfigureStructuredData(ConstString type_name,
const StructuredData::ObjectSP &config_sp) override;
StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos() override;
StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos(
const std::vector<lldb::addr_t> &load_addresses) override;
StructuredData::ObjectSP
GetLoadedDynamicLibrariesInfos_sender(StructuredData::ObjectSP args);
StructuredData::ObjectSP GetSharedCacheInfo() override;
std::string HarmonizeThreadIdsForProfileData(
StringExtractorGDBRemote &inputStringExtractor);
protected:
friend class ThreadGDBRemote;
friend class GDBRemoteCommunicationClient;
friend class GDBRemoteRegisterContext;
/// Broadcaster event bits definitions.
enum {
eBroadcastBitAsyncContinue = (1 << 0),
eBroadcastBitAsyncThreadShouldExit = (1 << 1),
eBroadcastBitAsyncThreadDidExit = (1 << 2)
};
GDBRemoteCommunicationClient m_gdb_comm;
GDBRemoteCommunicationReplayServer m_gdb_replay_server;
std::atomic<lldb::pid_t> m_debugserver_pid;
std::vector<StringExtractorGDBRemote> m_stop_packet_stack; // The stop packet
// stack replaces
// the last stop
// packet variable
std::recursive_mutex m_last_stop_packet_mutex;
GDBRemoteDynamicRegisterInfo m_register_info;
Broadcaster m_async_broadcaster;
lldb::ListenerSP m_async_listener_sp;
HostThread m_async_thread;
std::recursive_mutex m_async_thread_state_mutex;
typedef std::vector<lldb::tid_t> tid_collection;
typedef std::vector<std::pair<lldb::tid_t, int>> tid_sig_collection;
typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
typedef std::map<uint32_t, std::string> ExpeditedRegisterMap;
tid_collection m_thread_ids; // Thread IDs for all threads. This list gets
// updated after stopping
std::vector<lldb::addr_t> m_thread_pcs; // PC values for all the threads.
StructuredData::ObjectSP m_jstopinfo_sp; // Stop info only for any threads
// that have valid stop infos
StructuredData::ObjectSP m_jthreadsinfo_sp; // Full stop info, expedited
// registers and memory for all
// threads if "jThreadsInfo"
// packet is supported
tid_collection m_continue_c_tids; // 'c' for continue
tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
tid_collection m_continue_s_tids; // 's' for step
tid_sig_collection m_continue_S_tids; // 'S' for step with signal
uint64_t m_max_memory_size; // The maximum number of bytes to read/write when
// reading and writing memory
uint64_t m_remote_stub_max_memory_size; // The maximum memory size the remote
// gdb stub can handle
MMapMap m_addr_to_mmap_size;
lldb::BreakpointSP m_thread_create_bp_sp;
bool m_waiting_for_attach;
bool m_destroy_tried_resuming;
lldb::CommandObjectSP m_command_sp;
int64_t m_breakpoint_pc_offset;
lldb::tid_t m_initial_tid; // The initial thread ID, given by stub on attach
bool m_replay_mode;
bool m_allow_flash_writes;
using FlashRangeVector = lldb_private::RangeVector<lldb::addr_t, size_t>;
using FlashRange = FlashRangeVector::Entry;
FlashRangeVector m_erased_flash_ranges;
// Accessors
bool IsRunning(lldb::StateType state) {
return state == lldb::eStateRunning || IsStepping(state);
}
bool IsStepping(lldb::StateType state) {
return state == lldb::eStateStepping;
}
bool CanResume(lldb::StateType state) { return state == lldb::eStateStopped; }
bool HasExited(lldb::StateType state) { return state == lldb::eStateExited; }
bool ProcessIDIsValid() const;
void Clear();
bool UpdateThreadList(ThreadList &old_thread_list,
ThreadList &new_thread_list) override;
Status ConnectToReplayServer(repro::Loader *loader);
Status EstablishConnectionIfNeeded(const ProcessInfo &process_info);
Status LaunchAndConnectToDebugserver(const ProcessInfo &process_info);
void KillDebugserverProcess();
void BuildDynamicRegisterInfo(bool force);
void SetLastStopPacket(const StringExtractorGDBRemote &response);
bool ParsePythonTargetDefinition(const FileSpec &target_definition_fspec);
DataExtractor GetAuxvData() override;
StructuredData::ObjectSP GetExtendedInfoForThread(lldb::tid_t tid);
void GetMaxMemorySize();
bool CalculateThreadStopInfo(ThreadGDBRemote *thread);
size_t UpdateThreadPCsFromStopReplyThreadsValue(std::string &value);
size_t UpdateThreadIDsFromStopReplyThreadsValue(std::string &value);
bool HandleNotifyPacket(StringExtractorGDBRemote &packet);
bool StartAsyncThread();
void StopAsyncThread();
static lldb::thread_result_t AsyncThread(void *arg);
static bool
MonitorDebugserverProcess(std::weak_ptr<ProcessGDBRemote> process_wp,
lldb::pid_t pid, bool exited, int signo,
int exit_status);
lldb::StateType SetThreadStopInfo(StringExtractor &stop_packet);
bool
GetThreadStopInfoFromJSON(ThreadGDBRemote *thread,
const StructuredData::ObjectSP &thread_infos_sp);
lldb::ThreadSP SetThreadStopInfo(StructuredData::Dictionary *thread_dict);
lldb::ThreadSP
SetThreadStopInfo(lldb::tid_t tid,
ExpeditedRegisterMap &expedited_register_map, uint8_t signo,
const std::string &thread_name, const std::string &reason,
const std::string &description, uint32_t exc_type,
const std::vector<lldb::addr_t> &exc_data,
lldb::addr_t thread_dispatch_qaddr, bool queue_vars_valid,
lldb_private::LazyBool associated_with_libdispatch_queue,
lldb::addr_t dispatch_queue_t, std::string &queue_name,
lldb::QueueKind queue_kind, uint64_t queue_serial);
void HandleStopReplySequence();
void ClearThreadIDList();
bool UpdateThreadIDList();
void DidLaunchOrAttach(ArchSpec &process_arch);
Status ConnectToDebugserver(llvm::StringRef host_port);
const char *GetDispatchQueueNameForThread(lldb::addr_t thread_dispatch_qaddr,
std::string &dispatch_queue_name);
DynamicLoader *GetDynamicLoader() override;
bool GetGDBServerRegisterInfoXMLAndProcess(ArchSpec &arch_to_use,
std::string xml_filename,
uint32_t &cur_reg_num,
uint32_t &reg_offset);
// Query remote GDBServer for register information
bool GetGDBServerRegisterInfo(ArchSpec &arch);
lldb::ModuleSP LoadModuleAtAddress(const FileSpec &file,
lldb::addr_t link_map,
lldb::addr_t base_addr,
bool value_is_offset);
Status UpdateAutomaticSignalFiltering() override;
Status FlashErase(lldb::addr_t addr, size_t size);
Status FlashDone();
bool HasErased(FlashRange range);
private:
// For ProcessGDBRemote only
std::string m_partial_profile_data;
std::map<uint64_t, uint32_t> m_thread_id_to_used_usec_map;
uint64_t m_last_signals_version = 0;
static bool NewThreadNotifyBreakpointHit(void *baton,
StoppointCallbackContext *context,
lldb::user_id_t break_id,
lldb::user_id_t break_loc_id);
// ContinueDelegate interface
void HandleAsyncStdout(llvm::StringRef out) override;
void HandleAsyncMisc(llvm::StringRef data) override;
void HandleStopReply() override;
void HandleAsyncStructuredDataPacket(llvm::StringRef data) override;
void SetThreadPc(const lldb::ThreadSP &thread_sp, uint64_t index);
using ModuleCacheKey = std::pair<std::string, std::string>;
// KeyInfo for the cached module spec DenseMap.
// The invariant is that all real keys will have the file and architecture
// set.
// The empty key has an empty file and an empty arch.
// The tombstone key has an invalid arch and an empty file.
// The comparison and hash functions take the file name and architecture
// triple into account.
struct ModuleCacheInfo {
static ModuleCacheKey getEmptyKey() { return ModuleCacheKey(); }
static ModuleCacheKey getTombstoneKey() { return ModuleCacheKey("", "T"); }
static unsigned getHashValue(const ModuleCacheKey &key) {
return llvm::hash_combine(key.first, key.second);
}
static bool isEqual(const ModuleCacheKey &LHS, const ModuleCacheKey &RHS) {
return LHS == RHS;
}
};
llvm::DenseMap<ModuleCacheKey, ModuleSpec, ModuleCacheInfo>
m_cached_module_specs;
DISALLOW_COPY_AND_ASSIGN(ProcessGDBRemote);
};
} // namespace process_gdb_remote
} // namespace lldb_private
#endif // liblldb_ProcessGDBRemote_h_