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 // Other libraries and framework includes
23 #include "lldb/Core/ArchSpec.h"
24 #include "lldb/Core/Broadcaster.h"
25 #include "lldb/Core/ConstString.h"
26 #include "lldb/Core/Error.h"
27 #include "lldb/Core/LoadedModuleInfoList.h"
28 #include "lldb/Core/ModuleSpec.h"
29 #include "lldb/Core/StreamString.h"
30 #include "lldb/Core/StringList.h"
31 #include "lldb/Core/StructuredData.h"
32 #include "lldb/Core/ThreadSafeValue.h"
33 #include "lldb/Host/HostThread.h"
34 #include "lldb/Target/Process.h"
35 #include "lldb/Target/Thread.h"
36 #include "lldb/Utility/StringExtractor.h"
37 #include "lldb/lldb-private-forward.h"
39 #include "GDBRemoteCommunicationClient.h"
40 #include "GDBRemoteRegisterContext.h"
42 #include "llvm/ADT/DenseMap.h"
44 namespace lldb_private {
45 namespace process_gdb_remote {
47 class ThreadGDBRemote;
49 class ProcessGDBRemote : public Process,
50 private GDBRemoteClientBase::ContinueDelegate {
52 ProcessGDBRemote(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp);
54 ~ProcessGDBRemote() override;
56 static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp,
57 lldb::ListenerSP listener_sp,
58 const FileSpec *crash_file_path);
60 static void Initialize();
62 static void DebuggerInitialize(Debugger &debugger);
64 static void Terminate();
66 static ConstString GetPluginNameStatic();
68 static const char *GetPluginDescriptionStatic();
70 //------------------------------------------------------------------
71 // Check if a given Process
72 //------------------------------------------------------------------
73 bool CanDebug(lldb::TargetSP target_sp,
74 bool plugin_specified_by_name) override;
76 CommandObject *GetPluginCommandObject() override;
78 //------------------------------------------------------------------
79 // Creating a new process, or attaching to an existing one
80 //------------------------------------------------------------------
81 Error WillLaunch(Module *module) override;
83 Error DoLaunch(Module *exe_module, ProcessLaunchInfo &launch_info) override;
85 void DidLaunch() override;
87 Error WillAttachToProcessWithID(lldb::pid_t pid) override;
89 Error WillAttachToProcessWithName(const char *process_name,
90 bool wait_for_launch) override;
92 Error DoConnectRemote(Stream *strm, llvm::StringRef remote_url) override;
94 Error WillLaunchOrAttach();
96 Error DoAttachToProcessWithID(lldb::pid_t pid,
97 const ProcessAttachInfo &attach_info) override;
100 DoAttachToProcessWithName(const char *process_name,
101 const ProcessAttachInfo &attach_info) override;
103 void DidAttach(ArchSpec &process_arch) override;
105 //------------------------------------------------------------------
106 // PluginInterface protocol
107 //------------------------------------------------------------------
108 ConstString GetPluginName() override;
110 uint32_t GetPluginVersion() override;
112 //------------------------------------------------------------------
114 //------------------------------------------------------------------
115 Error WillResume() override;
117 Error DoResume() override;
119 Error DoHalt(bool &caused_stop) override;
121 Error DoDetach(bool keep_stopped) override;
123 bool DetachRequiresHalt() override { return true; }
125 Error DoSignal(int signal) override;
127 Error DoDestroy() override;
129 void RefreshStateAfterStop() override;
131 void SetUnixSignals(const lldb::UnixSignalsSP &signals_sp);
133 //------------------------------------------------------------------
135 //------------------------------------------------------------------
136 bool IsAlive() override;
138 lldb::addr_t GetImageInfoAddress() override;
140 void WillPublicStop() override;
142 //------------------------------------------------------------------
144 //------------------------------------------------------------------
145 size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
146 Error &error) override;
148 size_t DoWriteMemory(lldb::addr_t addr, const void *buf, size_t size,
149 Error &error) override;
151 lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions,
152 Error &error) override;
154 Error GetMemoryRegionInfo(lldb::addr_t load_addr,
155 MemoryRegionInfo ®ion_info) override;
157 Error DoDeallocateMemory(lldb::addr_t ptr) override;
159 //------------------------------------------------------------------
161 //------------------------------------------------------------------
162 size_t PutSTDIN(const char *buf, size_t buf_size, Error &error) override;
164 //----------------------------------------------------------------------
165 // Process Breakpoints
166 //----------------------------------------------------------------------
167 Error EnableBreakpointSite(BreakpointSite *bp_site) override;
169 Error DisableBreakpointSite(BreakpointSite *bp_site) override;
171 //----------------------------------------------------------------------
172 // Process Watchpoints
173 //----------------------------------------------------------------------
174 Error EnableWatchpoint(Watchpoint *wp, bool notify = true) override;
176 Error DisableWatchpoint(Watchpoint *wp, bool notify = true) override;
178 Error GetWatchpointSupportInfo(uint32_t &num) override;
180 Error GetWatchpointSupportInfo(uint32_t &num, bool &after) override;
182 bool StartNoticingNewThreads() override;
184 bool StopNoticingNewThreads() override;
186 GDBRemoteCommunicationClient &GetGDBRemote() { return m_gdb_comm; }
188 Error SendEventData(const char *data) override;
190 //----------------------------------------------------------------------
191 // Override DidExit so we can disconnect from the remote GDB server
192 //----------------------------------------------------------------------
193 void DidExit() override;
195 void SetUserSpecifiedMaxMemoryTransferSize(uint64_t user_specified_max);
197 bool GetModuleSpec(const FileSpec &module_file_spec, const ArchSpec &arch,
198 ModuleSpec &module_spec) override;
200 void PrefetchModuleSpecs(llvm::ArrayRef<FileSpec> module_file_specs,
201 const llvm::Triple &triple) override;
203 bool GetHostOSVersion(uint32_t &major, uint32_t &minor,
204 uint32_t &update) override;
206 size_t LoadModules(LoadedModuleInfoList &module_list) override;
208 size_t LoadModules() override;
210 Error GetFileLoadAddress(const FileSpec &file, bool &is_loaded,
211 lldb::addr_t &load_addr) override;
213 void ModulesDidLoad(ModuleList &module_list) override;
215 StructuredData::ObjectSP
216 GetLoadedDynamicLibrariesInfos(lldb::addr_t image_list_address,
217 lldb::addr_t image_count) override;
220 ConfigureStructuredData(const ConstString &type_name,
221 const StructuredData::ObjectSP &config_sp) override;
223 StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos() override;
225 StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos(
226 const std::vector<lldb::addr_t> &load_addresses) override;
228 StructuredData::ObjectSP
229 GetLoadedDynamicLibrariesInfos_sender(StructuredData::ObjectSP args);
231 StructuredData::ObjectSP GetSharedCacheInfo() override;
233 std::string HarmonizeThreadIdsForProfileData(
234 StringExtractorGDBRemote &inputStringExtractor);
237 friend class ThreadGDBRemote;
238 friend class GDBRemoteCommunicationClient;
239 friend class GDBRemoteRegisterContext;
241 //------------------------------------------------------------------
242 /// Broadcaster event bits definitions.
243 //------------------------------------------------------------------
245 eBroadcastBitAsyncContinue = (1 << 0),
246 eBroadcastBitAsyncThreadShouldExit = (1 << 1),
247 eBroadcastBitAsyncThreadDidExit = (1 << 2)
250 Flags m_flags; // Process specific flags (see eFlags enums)
251 GDBRemoteCommunicationClient m_gdb_comm;
252 std::atomic<lldb::pid_t> m_debugserver_pid;
253 std::vector<StringExtractorGDBRemote> m_stop_packet_stack; // The stop packet
257 std::recursive_mutex m_last_stop_packet_mutex;
258 GDBRemoteDynamicRegisterInfo m_register_info;
259 Broadcaster m_async_broadcaster;
260 lldb::ListenerSP m_async_listener_sp;
261 HostThread m_async_thread;
262 std::recursive_mutex m_async_thread_state_mutex;
263 typedef std::vector<lldb::tid_t> tid_collection;
264 typedef std::vector<std::pair<lldb::tid_t, int>> tid_sig_collection;
265 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap;
266 typedef std::map<uint32_t, std::string> ExpeditedRegisterMap;
267 tid_collection m_thread_ids; // Thread IDs for all threads. This list gets
268 // updated after stopping
269 std::vector<lldb::addr_t> m_thread_pcs; // PC values for all the threads.
270 StructuredData::ObjectSP m_jstopinfo_sp; // Stop info only for any threads
271 // that have valid stop infos
272 StructuredData::ObjectSP m_jthreadsinfo_sp; // Full stop info, expedited
273 // registers and memory for all
274 // threads if "jThreadsInfo"
275 // packet is supported
276 tid_collection m_continue_c_tids; // 'c' for continue
277 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal
278 tid_collection m_continue_s_tids; // 's' for step
279 tid_sig_collection m_continue_S_tids; // 'S' for step with signal
280 uint64_t m_max_memory_size; // The maximum number of bytes to read/write when
281 // reading and writing memory
282 uint64_t m_remote_stub_max_memory_size; // The maximum memory size the remote
283 // gdb stub can handle
284 MMapMap m_addr_to_mmap_size;
285 lldb::BreakpointSP m_thread_create_bp_sp;
286 bool m_waiting_for_attach;
287 bool m_destroy_tried_resuming;
288 lldb::CommandObjectSP m_command_sp;
289 int64_t m_breakpoint_pc_offset;
290 lldb::tid_t m_initial_tid; // The initial thread ID, given by stub on attach
292 //----------------------------------------------------------------------
294 //----------------------------------------------------------------------
295 bool IsRunning(lldb::StateType state) {
296 return state == lldb::eStateRunning || IsStepping(state);
299 bool IsStepping(lldb::StateType state) {
300 return state == lldb::eStateStepping;
303 bool CanResume(lldb::StateType state) { return state == lldb::eStateStopped; }
305 bool HasExited(lldb::StateType state) { return state == lldb::eStateExited; }
307 bool ProcessIDIsValid() const;
311 Flags &GetFlags() { return m_flags; }
313 const Flags &GetFlags() const { return m_flags; }
315 bool UpdateThreadList(ThreadList &old_thread_list,
316 ThreadList &new_thread_list) override;
318 Error EstablishConnectionIfNeeded(const ProcessInfo &process_info);
320 Error LaunchAndConnectToDebugserver(const ProcessInfo &process_info);
322 void KillDebugserverProcess();
324 void BuildDynamicRegisterInfo(bool force);
326 void SetLastStopPacket(const StringExtractorGDBRemote &response);
328 bool ParsePythonTargetDefinition(const FileSpec &target_definition_fspec);
330 const lldb::DataBufferSP GetAuxvData() override;
332 StructuredData::ObjectSP GetExtendedInfoForThread(lldb::tid_t tid);
334 void GetMaxMemorySize();
336 bool CalculateThreadStopInfo(ThreadGDBRemote *thread);
338 size_t UpdateThreadPCsFromStopReplyThreadsValue(std::string &value);
340 size_t UpdateThreadIDsFromStopReplyThreadsValue(std::string &value);
342 bool HandleNotifyPacket(StringExtractorGDBRemote &packet);
344 bool StartAsyncThread();
346 void StopAsyncThread();
348 static lldb::thread_result_t AsyncThread(void *arg);
351 MonitorDebugserverProcess(std::weak_ptr<ProcessGDBRemote> process_wp,
352 lldb::pid_t pid, bool exited, int signo,
355 lldb::StateType SetThreadStopInfo(StringExtractor &stop_packet);
358 GetThreadStopInfoFromJSON(ThreadGDBRemote *thread,
359 const StructuredData::ObjectSP &thread_infos_sp);
361 lldb::ThreadSP SetThreadStopInfo(StructuredData::Dictionary *thread_dict);
364 SetThreadStopInfo(lldb::tid_t tid,
365 ExpeditedRegisterMap &expedited_register_map, uint8_t signo,
366 const std::string &thread_name, const std::string &reason,
367 const std::string &description, uint32_t exc_type,
368 const std::vector<lldb::addr_t> &exc_data,
369 lldb::addr_t thread_dispatch_qaddr, bool queue_vars_valid,
370 lldb_private::LazyBool associated_with_libdispatch_queue,
371 lldb::addr_t dispatch_queue_t, std::string &queue_name,
372 lldb::QueueKind queue_kind, uint64_t queue_serial);
374 void HandleStopReplySequence();
376 void ClearThreadIDList();
378 bool UpdateThreadIDList();
380 void DidLaunchOrAttach(ArchSpec &process_arch);
382 Error ConnectToDebugserver(llvm::StringRef host_port);
384 const char *GetDispatchQueueNameForThread(lldb::addr_t thread_dispatch_qaddr,
385 std::string &dispatch_queue_name);
387 DynamicLoader *GetDynamicLoader() override;
389 // Query remote GDBServer for register information
390 bool GetGDBServerRegisterInfo(ArchSpec &arch);
392 // Query remote GDBServer for a detailed loaded library list
393 Error GetLoadedModuleList(LoadedModuleInfoList &);
395 lldb::ModuleSP LoadModuleAtAddress(const FileSpec &file,
396 lldb::addr_t link_map,
397 lldb::addr_t base_addr,
398 bool value_is_offset);
401 //------------------------------------------------------------------
402 // For ProcessGDBRemote only
403 //------------------------------------------------------------------
404 std::string m_partial_profile_data;
405 std::map<uint64_t, uint32_t> m_thread_id_to_used_usec_map;
407 static bool NewThreadNotifyBreakpointHit(void *baton,
408 StoppointCallbackContext *context,
409 lldb::user_id_t break_id,
410 lldb::user_id_t break_loc_id);
412 //------------------------------------------------------------------
413 // ContinueDelegate interface
414 //------------------------------------------------------------------
415 void HandleAsyncStdout(llvm::StringRef out) override;
416 void HandleAsyncMisc(llvm::StringRef data) override;
417 void HandleStopReply() override;
418 void HandleAsyncStructuredDataPacket(llvm::StringRef data) override;
420 using ModuleCacheKey = std::pair<std::string, std::string>;
421 // KeyInfo for the cached module spec DenseMap.
422 // The invariant is that all real keys will have the file and architecture
424 // The empty key has an empty file and an empty arch.
425 // The tombstone key has an invalid arch and an empty file.
426 // The comparison and hash functions take the file name and architecture
427 // triple into account.
428 struct ModuleCacheInfo {
429 static ModuleCacheKey getEmptyKey() { return ModuleCacheKey(); }
431 static ModuleCacheKey getTombstoneKey() { return ModuleCacheKey("", "T"); }
433 static unsigned getHashValue(const ModuleCacheKey &key) {
434 return llvm::hash_combine(key.first, key.second);
437 static bool isEqual(const ModuleCacheKey &LHS, const ModuleCacheKey &RHS) {
442 llvm::DenseMap<ModuleCacheKey, ModuleSpec, ModuleCacheInfo>
443 m_cached_module_specs;
445 DISALLOW_COPY_AND_ASSIGN(ProcessGDBRemote);
448 } // namespace process_gdb_remote
449 } // namespace lldb_private
451 #endif // liblldb_ProcessGDBRemote_h_