llvm-capstone/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h
Antonio Afonso 57e2da4f32 Create a generic handler for Xfer packets
Summary:
This is the first of a few patches I have to improve the performance of dynamic module loading on Android.

In this first diff I'll describe the context of my main motivation and will then link to it in the other diffs to avoid repeating myself.

## Motivation
I have a few scenarios where opening a specific feature on an Android app takes around 40s when lldb is attached to it. The reason for that is because 40 modules are dynamicly loaded at that point in time and each one of them is taking ~1s.

## The problem
To learn about new modules we have a breakpoint on a linker function that is called twice whenever a module is loaded. One time just before it's loaded (so lldb can check which modules are loaded) and another right after it's loaded (so lldb can check again which ones are loaded and calculate the diference).
It's figuring out which modules are loaded that is taking quite some time. This is currently done by traversing the linked list of loaded shared libraries that the linker maintains in memory. Each item in the linked list requires its own `x` packet sent to the gdb server (this is android so the network also plays a part). In my scenario there are 400+ loaded libraries and even though we read 0x800 worth of bytes at a time we still make ~180 requests that end up taking 150-200ms.
We also do this twice, once before the module is loaded (state = eAdd) and another right after (state = eConsistent) which easly adds up to ~400ms per module.

## A solution

**Implement `xfer:libraries-svr4` in lldb-server:**
I noticed in the code that loads the new modules that it had support for the `xfer:libraries-svr4` packet (added ~4 years ago to support the ds2 debug server) but we didn't support it in lldb-server. This single packet returns an xml list of all the loaded modules by the process. The advantage is that there's no more need to make 180 requests to read the linked list. Additionally this new requests takes around 10ms.

**More efficient usage of the `xfer:libraries-svr4` packet in lldb:**
When `xfer:libraries-svr4` is available the Process class has a `LoadModules` function that requests this packet and then loads or unloads modules based on the current list of loaded modules by the process.
This is the function that is used by the DYLDRendezvous class to get the list of loaded modules before and after the module is loaded. However, this is really not needed since the LoadModules function already loaded or unloaded the modules accordingly. I changed this strategy to call LoadModules only once (after the process has loaded the module).

**Bugs**
I found a few issues in lldb while implementing this and have submitted independent patches for them.

I tried to devide this into multiple logical patches to make it easier to review and discuss.

## Tests

I wanted to put these set of diffs up before having all the tests up and running to start having them reviewed from a techical point of view. I'm also having some trouble making the tests running on linux so I need more time to make that happen.

# This diff

The `xfer` packages follow the same protocol, they are requested with `xfer:<object>:<read|write>:<annex>:<offset,length>` and a return that starts with `l` or `m` depending if the offset and length covers the entire data or not. Before implementing the `xfer:libraries-svr4` I refactored the `xfer:auxv` to generically handle xfer packets so we can easly add new ones.

The overall structure of the function ends up being:
* Parse the packet into its components: object, offset etc.
* Depending on the object do its own logic to generate the data.
* Return the data based on its size, the requested offset and length.

Reviewers: clayborg, xiaobai, labath

Reviewed By: labath

Subscribers: mgorny, krytarowski, lldb-commits

Tags: #lldb

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

llvm-svn: 362982
2019-06-10 20:59:58 +00:00

230 lines
7.2 KiB
C++

//===-- GDBRemoteCommunicationServerLLGS.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_GDBRemoteCommunicationServerLLGS_h_
#define liblldb_GDBRemoteCommunicationServerLLGS_h_
#include <mutex>
#include <unordered_map>
#include "lldb/Core/Communication.h"
#include "lldb/Host/MainLoop.h"
#include "lldb/Host/common/NativeProcessProtocol.h"
#include "lldb/lldb-private-forward.h"
#include "GDBRemoteCommunicationServerCommon.h"
class StringExtractorGDBRemote;
namespace lldb_private {
namespace process_gdb_remote {
class ProcessGDBRemote;
class GDBRemoteCommunicationServerLLGS
: public GDBRemoteCommunicationServerCommon,
public NativeProcessProtocol::NativeDelegate {
public:
// Constructors and Destructors
GDBRemoteCommunicationServerLLGS(
MainLoop &mainloop,
const NativeProcessProtocol::Factory &process_factory);
void SetLaunchInfo(const ProcessLaunchInfo &info);
/// Launch a process with the current launch settings.
///
/// This method supports running an lldb-gdbserver or similar
/// server in a situation where the startup code has been provided
/// with all the information for a child process to be launched.
///
/// \return
/// An Status object indicating the success or failure of the
/// launch.
Status LaunchProcess() override;
/// Attach to a process.
///
/// This method supports attaching llgs to a process accessible via the
/// configured Platform.
///
/// \return
/// An Status object indicating the success or failure of the
/// attach operation.
Status AttachToProcess(lldb::pid_t pid);
// NativeProcessProtocol::NativeDelegate overrides
void InitializeDelegate(NativeProcessProtocol *process) override;
void ProcessStateChanged(NativeProcessProtocol *process,
lldb::StateType state) override;
void DidExec(NativeProcessProtocol *process) override;
Status InitializeConnection(std::unique_ptr<Connection> &&connection);
protected:
MainLoop &m_mainloop;
MainLoop::ReadHandleUP m_network_handle_up;
const NativeProcessProtocol::Factory &m_process_factory;
lldb::tid_t m_current_tid = LLDB_INVALID_THREAD_ID;
lldb::tid_t m_continue_tid = LLDB_INVALID_THREAD_ID;
std::recursive_mutex m_debugged_process_mutex;
std::unique_ptr<NativeProcessProtocol> m_debugged_process_up;
Communication m_stdio_communication;
MainLoop::ReadHandleUP m_stdio_handle_up;
lldb::StateType m_inferior_prev_state = lldb::StateType::eStateInvalid;
llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> m_xfer_buffer_map;
std::mutex m_saved_registers_mutex;
std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map;
uint32_t m_next_saved_registers_id = 1;
bool m_handshake_completed = false;
PacketResult SendONotification(const char *buffer, uint32_t len);
PacketResult SendWResponse(NativeProcessProtocol *process);
PacketResult SendStopReplyPacketForThread(lldb::tid_t tid);
PacketResult SendStopReasonForState(lldb::StateType process_state);
PacketResult Handle_k(StringExtractorGDBRemote &packet);
PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
PacketResult Handle_qC(StringExtractorGDBRemote &packet);
PacketResult Handle_QSetDisableASLR(StringExtractorGDBRemote &packet);
PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
PacketResult Handle_C(StringExtractorGDBRemote &packet);
PacketResult Handle_c(StringExtractorGDBRemote &packet);
PacketResult Handle_vCont(StringExtractorGDBRemote &packet);
PacketResult Handle_vCont_actions(StringExtractorGDBRemote &packet);
PacketResult Handle_stop_reason(StringExtractorGDBRemote &packet);
PacketResult Handle_qRegisterInfo(StringExtractorGDBRemote &packet);
PacketResult Handle_qfThreadInfo(StringExtractorGDBRemote &packet);
PacketResult Handle_qsThreadInfo(StringExtractorGDBRemote &packet);
PacketResult Handle_p(StringExtractorGDBRemote &packet);
PacketResult Handle_P(StringExtractorGDBRemote &packet);
PacketResult Handle_H(StringExtractorGDBRemote &packet);
PacketResult Handle_I(StringExtractorGDBRemote &packet);
PacketResult Handle_interrupt(StringExtractorGDBRemote &packet);
// Handles $m and $x packets.
PacketResult Handle_memory_read(StringExtractorGDBRemote &packet);
PacketResult Handle_M(StringExtractorGDBRemote &packet);
PacketResult
Handle_qMemoryRegionInfoSupported(StringExtractorGDBRemote &packet);
PacketResult Handle_qMemoryRegionInfo(StringExtractorGDBRemote &packet);
PacketResult Handle_Z(StringExtractorGDBRemote &packet);
PacketResult Handle_z(StringExtractorGDBRemote &packet);
PacketResult Handle_s(StringExtractorGDBRemote &packet);
PacketResult Handle_qXfer(StringExtractorGDBRemote &packet);
PacketResult Handle_QSaveRegisterState(StringExtractorGDBRemote &packet);
PacketResult Handle_jTraceStart(StringExtractorGDBRemote &packet);
PacketResult Handle_jTraceRead(StringExtractorGDBRemote &packet);
PacketResult Handle_jTraceStop(StringExtractorGDBRemote &packet);
PacketResult Handle_jTraceConfigRead(StringExtractorGDBRemote &packet);
PacketResult Handle_QRestoreRegisterState(StringExtractorGDBRemote &packet);
PacketResult Handle_vAttach(StringExtractorGDBRemote &packet);
PacketResult Handle_D(StringExtractorGDBRemote &packet);
PacketResult Handle_qThreadStopInfo(StringExtractorGDBRemote &packet);
PacketResult Handle_jThreadsInfo(StringExtractorGDBRemote &packet);
PacketResult Handle_qWatchpointSupportInfo(StringExtractorGDBRemote &packet);
PacketResult Handle_qFileLoadAddress(StringExtractorGDBRemote &packet);
PacketResult Handle_QPassSignals(StringExtractorGDBRemote &packet);
PacketResult Handle_g(StringExtractorGDBRemote &packet);
void SetCurrentThreadID(lldb::tid_t tid);
lldb::tid_t GetCurrentThreadID() const;
void SetContinueThreadID(lldb::tid_t tid);
lldb::tid_t GetContinueThreadID() const { return m_continue_tid; }
Status SetSTDIOFileDescriptor(int fd);
FileSpec FindModuleFile(const std::string &module_path,
const ArchSpec &arch) override;
llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
ReadXferObject(llvm::StringRef object, llvm::StringRef annex);
private:
void HandleInferiorState_Exited(NativeProcessProtocol *process);
void HandleInferiorState_Stopped(NativeProcessProtocol *process);
NativeThreadProtocol *GetThreadFromSuffix(StringExtractorGDBRemote &packet);
uint32_t GetNextSavedRegistersID();
void MaybeCloseInferiorTerminalConnection();
void ClearProcessSpecificData();
void RegisterPacketHandlers();
void DataAvailableCallback();
void SendProcessOutput();
void StartSTDIOForwarding();
void StopSTDIOForwarding();
// For GDBRemoteCommunicationServerLLGS only
DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServerLLGS);
};
} // namespace process_gdb_remote
} // namespace lldb_private
#endif // liblldb_GDBRemoteCommunicationServerLLGS_h_