1 //===-- ProcessGDBRemote.h --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ProcessGDBRemote_h_
11 #define liblldb_ProcessGDBRemote_h_
21 #include "lldb/Core/Broadcaster.h"
22 #include "lldb/Core/LoadedModuleInfoList.h"
23 #include "lldb/Core/ModuleSpec.h"
24 #include "lldb/Core/ThreadSafeValue.h"
25 #include "lldb/Host/HostThread.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/Thread.h"
28 #include "lldb/Utility/ArchSpec.h"
29 #include "lldb/Utility/ConstString.h"
30 #include "lldb/Utility/Status.h"
31 #include "lldb/Utility/StreamGDBRemote.h"
32 #include "lldb/Utility/StreamString.h"
33 #include "lldb/Utility/StringExtractor.h"
34 #include "lldb/Utility/StringList.h"
35 #include "lldb/Utility/StructuredData.h"
36 #include "lldb/lldb-private-forward.h"
38 #include "GDBRemoteCommunicationClient.h"
39 #include "GDBRemoteRegisterContext.h"
41 #include "llvm/ADT/DenseMap.h"
43 namespace lldb_private {
44 namespace process_gdb_remote {
46 class ThreadGDBRemote;
48 class ProcessGDBRemote : public Process,
49 private GDBRemoteClientBase::ContinueDelegate {
51 ProcessGDBRemote(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp);
53 ~ProcessGDBRemote() override;
55 static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp,
56 lldb::ListenerSP listener_sp,
57 const FileSpec *crash_file_path);
59 static void Initialize();
61 static void DebuggerInitialize(Debugger &debugger);
63 static void Terminate();
65 static ConstString GetPluginNameStatic();
67 static const char *GetPluginDescriptionStatic();
69 //------------------------------------------------------------------
70 // Check if a given Process
71 //------------------------------------------------------------------
72 bool CanDebug(lldb::TargetSP target_sp,
73 bool plugin_specified_by_name) override;
75 CommandObject *GetPluginCommandObject() override;
77 //------------------------------------------------------------------
78 // Creating a new process, or attaching to an existing one
79 //------------------------------------------------------------------
80 Status WillLaunch(Module *module) override;
82 Status DoLaunch(Module *exe_module, ProcessLaunchInfo &launch_info) override;
84 void DidLaunch() override;
86 Status WillAttachToProcessWithID(lldb::pid_t pid) override;
88 Status WillAttachToProcessWithName(const char *process_name,
89 bool wait_for_launch) override;
91 Status DoConnectRemote(Stream *strm, llvm::StringRef remote_url) override;
93 Status WillLaunchOrAttach();
95 Status DoAttachToProcessWithID(lldb::pid_t pid,
96 const ProcessAttachInfo &attach_info) override;
99 DoAttachToProcessWithName(const char *process_name,
100 const ProcessAttachInfo &attach_info) override;
102 void DidAttach(ArchSpec &process_arch) override;
104 //------------------------------------------------------------------
105 // PluginInterface protocol
106 //------------------------------------------------------------------
107 ConstString GetPluginName() override;
109 uint32_t GetPluginVersion() override;
111 //------------------------------------------------------------------
113 //------------------------------------------------------------------
114 Status WillResume() override;
116 Status DoResume() override;
118 Status DoHalt(bool &caused_stop) override;
120 Status DoDetach(bool keep_stopped) override;
122 bool DetachRequiresHalt() override { return true; }
124 Status DoSignal(int signal) override;
126 Status DoDestroy() override;
128 void RefreshStateAfterStop() override;
130 void SetUnixSignals(const lldb::UnixSignalsSP &signals_sp);
132 //------------------------------------------------------------------
134 //------------------------------------------------------------------
135 bool IsAlive() override;
137 lldb::addr_t GetImageInfoAddress() override;
139 void WillPublicStop() override;
141 //------------------------------------------------------------------
143 //------------------------------------------------------------------
144 size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
145 Status &error) override;
147 size_t DoWriteMemory(lldb::addr_t addr, const void *buf, size_t size,
148 Status &error) override;
150 lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions,
151 Status &error) override;
153 Status GetMemoryRegionInfo(lldb::addr_t load_addr,
154 MemoryRegionInfo ®ion_info) override;
156 Status DoDeallocateMemory(lldb::addr_t ptr) override;
158 //------------------------------------------------------------------
160 //------------------------------------------------------------------
161 size_t PutSTDIN(const char *buf, size_t buf_size, Status &error) override;
163 //----------------------------------------------------------------------
164 // Process Breakpoints
165 //----------------------------------------------------------------------
166 Status EnableBreakpointSite(BreakpointSite *bp_site) override;
168 Status DisableBreakpointSite(BreakpointSite *bp_site) override;
170 //----------------------------------------------------------------------
171 // Process Watchpoints
172 //----------------------------------------------------------------------
173 Status EnableWatchpoint(Watchpoint *wp, bool notify = true) override;
175 Status DisableWatchpoint(Watchpoint *wp, bool notify = true) override;
177 Status GetWatchpointSupportInfo(uint32_t &num) override;
179 lldb::user_id_t StartTrace(const TraceOptions &options,
180 Status &error) override;
182 Status StopTrace(lldb::user_id_t uid, lldb::tid_t thread_id) override;
184 Status GetData(lldb::user_id_t uid, lldb::tid_t thread_id,
185 llvm::MutableArrayRef<uint8_t> &buffer,
186 size_t offset = 0) override;
188 Status GetMetaData(lldb::user_id_t uid, lldb::tid_t thread_id,
189 llvm::MutableArrayRef<uint8_t> &buffer,
190 size_t offset = 0) override;
192 Status GetTraceConfig(lldb::user_id_t uid, TraceOptions &options) override;
194 Status GetWatchpointSupportInfo(uint32_t &num, bool &after) override;
196 bool StartNoticingNewThreads() override;
198 bool StopNoticingNewThreads() override;
200 GDBRemoteCommunicationClient &GetGDBRemote() { return m_gdb_comm; }
202 Status SendEventData(const char *data) override;
204 //----------------------------------------------------------------------
205 // Override DidExit so we can disconnect from the remote GDB server
206 //----------------------------------------------------------------------
207 void DidExit() override;
209 void SetUserSpecifiedMaxMemoryTransferSize(uint64_t user_specified_max);
211 bool GetModuleSpec(const FileSpec &module_file_spec, const ArchSpec &arch,
212 ModuleSpec &module_spec) override;
214 void PrefetchModuleSpecs(llvm::ArrayRef<FileSpec> module_file_specs,
215 const llvm::Triple &triple) override;
217 bool GetHostOSVersion(uint32_t &major, uint32_t &minor,
218 uint32_t &update) override;
220 size_t LoadModules(LoadedModuleInfoList &module_list) override;
222 size_t LoadModules() override;
224 Status GetFileLoadAddress(const FileSpec &file, bool &is_loaded,
225 lldb::addr_t &load_addr) override;
227 void ModulesDidLoad(ModuleList &module_list) override;
229 StructuredData::ObjectSP
230 GetLoadedDynamicLibrariesInfos(lldb::addr_t image_list_address,
231 lldb::addr_t image_count) override;
234 ConfigureStructuredData(const ConstString &type_name,
235 const StructuredData::ObjectSP &config_sp) override;
237 StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos() override;
239 StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos(
240 const std::vector<lldb::addr_t> &load_addresses) override;
242 StructuredData::ObjectSP
243 GetLoadedDynamicLibrariesInfos_sender(StructuredData::ObjectSP args);
245 StructuredData::ObjectSP GetSharedCacheInfo() override;
247 std::string HarmonizeThreadIdsForProfileData(
248 StringExtractorGDBRemote &inputStringExtractor);
251 friend class ThreadGDBRemote;
252 friend class GDBRemoteCommunicationClient;
253 friend class GDBRemoteRegisterContext;
255 //------------------------------------------------------------------
256 /// Broadcaster event bits definitions.
257 //------------------------------------------------------------------
259 eBroadcastBitAsyncContinue = (1 << 0),
260 eBroadcastBitAsyncThreadShouldExit = (1 << 1),
261 eBroadcastBitAsyncThreadDidExit = (1 << 2)
264 GDBRemoteCommunicationClient m_gdb_comm;
265 std::atomic<lldb::pid_t> m_debugserver_pid;
266 std::vector<StringExtractorGDBRemote> m_stop_packet_stack; // The stop packet
270 std::recursive_mutex m_last_stop_packet_mutex;
271 GDBRemoteDynamicRegisterInfo m_register_info;
272 Broadcaster m_async_broadcaster;
273 lldb::ListenerSP m_async_listener_sp;
274 HostThread m_async_thread;
275 std::recursive_mutex m_async_thread_state_mutex;
276 typedef std::vector<lldb::tid_t> tid_collection;
277 typedef std::vector<std::pair<lldb::tid_t, int>> tid_sig_collection;
278 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
279 typedef std::map<uint32_t, std::string> ExpeditedRegisterMap;
280 tid_collection m_thread_ids; // Thread IDs for all threads. This list gets
281 // updated after stopping
282 std::vector<lldb::addr_t> m_thread_pcs; // PC values for all the threads.
283 StructuredData::ObjectSP m_jstopinfo_sp; // Stop info only for any threads
284 // that have valid stop infos
285 StructuredData::ObjectSP m_jthreadsinfo_sp; // Full stop info, expedited
286 // registers and memory for all
287 // threads if "jThreadsInfo"
288 // packet is supported
289 tid_collection m_continue_c_tids; // 'c' for continue
290 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
291 tid_collection m_continue_s_tids; // 's' for step
292 tid_sig_collection m_continue_S_tids; // 'S' for step with signal
293 uint64_t m_max_memory_size; // The maximum number of bytes to read/write when
294 // reading and writing memory
295 uint64_t m_remote_stub_max_memory_size; // The maximum memory size the remote
296 // gdb stub can handle
297 MMapMap m_addr_to_mmap_size;
298 lldb::BreakpointSP m_thread_create_bp_sp;
299 bool m_waiting_for_attach;
300 bool m_destroy_tried_resuming;
301 lldb::CommandObjectSP m_command_sp;
302 int64_t m_breakpoint_pc_offset;
303 lldb::tid_t m_initial_tid; // The initial thread ID, given by stub on attach
305 //----------------------------------------------------------------------
307 //----------------------------------------------------------------------
308 bool IsRunning(lldb::StateType state) {
309 return state == lldb::eStateRunning || IsStepping(state);
312 bool IsStepping(lldb::StateType state) {
313 return state == lldb::eStateStepping;
316 bool CanResume(lldb::StateType state) { return state == lldb::eStateStopped; }
318 bool HasExited(lldb::StateType state) { return state == lldb::eStateExited; }
320 bool ProcessIDIsValid() const;
324 bool UpdateThreadList(ThreadList &old_thread_list,
325 ThreadList &new_thread_list) override;
327 Status EstablishConnectionIfNeeded(const ProcessInfo &process_info);
329 Status LaunchAndConnectToDebugserver(const ProcessInfo &process_info);
331 void KillDebugserverProcess();
333 void BuildDynamicRegisterInfo(bool force);
335 void SetLastStopPacket(const StringExtractorGDBRemote &response);
337 bool ParsePythonTargetDefinition(const FileSpec &target_definition_fspec);
339 const lldb::DataBufferSP GetAuxvData() override;
341 StructuredData::ObjectSP GetExtendedInfoForThread(lldb::tid_t tid);
343 void GetMaxMemorySize();
345 bool CalculateThreadStopInfo(ThreadGDBRemote *thread);
347 size_t UpdateThreadPCsFromStopReplyThreadsValue(std::string &value);
349 size_t UpdateThreadIDsFromStopReplyThreadsValue(std::string &value);
351 bool HandleNotifyPacket(StringExtractorGDBRemote &packet);
353 bool StartAsyncThread();
355 void StopAsyncThread();
357 static lldb::thread_result_t AsyncThread(void *arg);
360 MonitorDebugserverProcess(std::weak_ptr<ProcessGDBRemote> process_wp,
361 lldb::pid_t pid, bool exited, int signo,
364 lldb::StateType SetThreadStopInfo(StringExtractor &stop_packet);
367 GetThreadStopInfoFromJSON(ThreadGDBRemote *thread,
368 const StructuredData::ObjectSP &thread_infos_sp);
370 lldb::ThreadSP SetThreadStopInfo(StructuredData::Dictionary *thread_dict);
373 SetThreadStopInfo(lldb::tid_t tid,
374 ExpeditedRegisterMap &expedited_register_map, uint8_t signo,
375 const std::string &thread_name, const std::string &reason,
376 const std::string &description, uint32_t exc_type,
377 const std::vector<lldb::addr_t> &exc_data,
378 lldb::addr_t thread_dispatch_qaddr, bool queue_vars_valid,
379 lldb_private::LazyBool associated_with_libdispatch_queue,
380 lldb::addr_t dispatch_queue_t, std::string &queue_name,
381 lldb::QueueKind queue_kind, uint64_t queue_serial);
383 void HandleStopReplySequence();
385 void ClearThreadIDList();
387 bool UpdateThreadIDList();
389 void DidLaunchOrAttach(ArchSpec &process_arch);
391 Status ConnectToDebugserver(llvm::StringRef host_port);
393 const char *GetDispatchQueueNameForThread(lldb::addr_t thread_dispatch_qaddr,
394 std::string &dispatch_queue_name);
396 DynamicLoader *GetDynamicLoader() override;
398 // Query remote GDBServer for register information
399 bool GetGDBServerRegisterInfo(ArchSpec &arch);
401 // Query remote GDBServer for a detailed loaded library list
402 Status GetLoadedModuleList(LoadedModuleInfoList &);
404 lldb::ModuleSP LoadModuleAtAddress(const FileSpec &file,
405 lldb::addr_t link_map,
406 lldb::addr_t base_addr,
407 bool value_is_offset);
409 Status UpdateAutomaticSignalFiltering() override;
412 //------------------------------------------------------------------
413 // For ProcessGDBRemote only
414 //------------------------------------------------------------------
415 std::string m_partial_profile_data;
416 std::map<uint64_t, uint32_t> m_thread_id_to_used_usec_map;
417 uint64_t m_last_signals_version = 0;
419 static bool NewThreadNotifyBreakpointHit(void *baton,
420 StoppointCallbackContext *context,
421 lldb::user_id_t break_id,
422 lldb::user_id_t break_loc_id);
424 //------------------------------------------------------------------
425 // ContinueDelegate interface
426 //------------------------------------------------------------------
427 void HandleAsyncStdout(llvm::StringRef out) override;
428 void HandleAsyncMisc(llvm::StringRef data) override;
429 void HandleStopReply() override;
430 void HandleAsyncStructuredDataPacket(llvm::StringRef data) override;
432 void SetThreadPc(const lldb::ThreadSP &thread_sp, uint64_t index);
433 using ModuleCacheKey = std::pair<std::string, std::string>;
434 // KeyInfo for the cached module spec DenseMap.
435 // The invariant is that all real keys will have the file and architecture
437 // The empty key has an empty file and an empty arch.
438 // The tombstone key has an invalid arch and an empty file.
439 // The comparison and hash functions take the file name and architecture
440 // triple into account.
441 struct ModuleCacheInfo {
442 static ModuleCacheKey getEmptyKey() { return ModuleCacheKey(); }
444 static ModuleCacheKey getTombstoneKey() { return ModuleCacheKey("", "T"); }
446 static unsigned getHashValue(const ModuleCacheKey &key) {
447 return llvm::hash_combine(key.first, key.second);
450 static bool isEqual(const ModuleCacheKey &LHS, const ModuleCacheKey &RHS) {
455 llvm::DenseMap<ModuleCacheKey, ModuleSpec, ModuleCacheInfo>
456 m_cached_module_specs;
458 DISALLOW_COPY_AND_ASSIGN(ProcessGDBRemote);
461 } // namespace process_gdb_remote
462 } // namespace lldb_private
464 #endif // liblldb_ProcessGDBRemote_h_