1 //===-- ProcessGDBRemote.h --------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_ProcessGDBRemote_h_ 11 #define liblldb_ProcessGDBRemote_h_ 12 13 // C Includes 14 // C++ Includes 15 #include <atomic> 16 #include <map> 17 #include <mutex> 18 #include <string> 19 #include <vector> 20 21 // Other libraries and framework includes 22 // Project 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" 38 39 #include "GDBRemoteCommunicationClient.h" 40 #include "GDBRemoteRegisterContext.h" 41 42 #include "llvm/ADT/DenseMap.h" 43 44 namespace lldb_private { 45 namespace process_gdb_remote { 46 47 class ThreadGDBRemote; 48 49 class ProcessGDBRemote : public Process, 50 private GDBRemoteClientBase::ContinueDelegate { 51 public: 52 ProcessGDBRemote(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp); 53 54 ~ProcessGDBRemote() override; 55 56 static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, 57 lldb::ListenerSP listener_sp, 58 const FileSpec *crash_file_path); 59 60 static void Initialize(); 61 62 static void DebuggerInitialize(Debugger &debugger); 63 64 static void Terminate(); 65 66 static ConstString GetPluginNameStatic(); 67 68 static const char *GetPluginDescriptionStatic(); 69 70 //------------------------------------------------------------------ 71 // Check if a given Process 72 //------------------------------------------------------------------ 73 bool CanDebug(lldb::TargetSP target_sp, 74 bool plugin_specified_by_name) override; 75 76 CommandObject *GetPluginCommandObject() override; 77 78 //------------------------------------------------------------------ 79 // Creating a new process, or attaching to an existing one 80 //------------------------------------------------------------------ 81 Error WillLaunch(Module *module) override; 82 83 Error DoLaunch(Module *exe_module, ProcessLaunchInfo &launch_info) override; 84 85 void DidLaunch() override; 86 87 Error WillAttachToProcessWithID(lldb::pid_t pid) override; 88 89 Error WillAttachToProcessWithName(const char *process_name, 90 bool wait_for_launch) override; 91 92 Error DoConnectRemote(Stream *strm, llvm::StringRef remote_url) override; 93 94 Error WillLaunchOrAttach(); 95 96 Error DoAttachToProcessWithID(lldb::pid_t pid, 97 const ProcessAttachInfo &attach_info) override; 98 99 Error 100 DoAttachToProcessWithName(const char *process_name, 101 const ProcessAttachInfo &attach_info) override; 102 103 void DidAttach(ArchSpec &process_arch) override; 104 105 //------------------------------------------------------------------ 106 // PluginInterface protocol 107 //------------------------------------------------------------------ 108 ConstString GetPluginName() override; 109 110 uint32_t GetPluginVersion() override; 111 112 //------------------------------------------------------------------ 113 // Process Control 114 //------------------------------------------------------------------ 115 Error WillResume() override; 116 117 Error DoResume() override; 118 119 Error DoHalt(bool &caused_stop) override; 120 121 Error DoDetach(bool keep_stopped) override; 122 123 bool DetachRequiresHalt() override { return true; } 124 125 Error DoSignal(int signal) override; 126 127 Error DoDestroy() override; 128 129 void RefreshStateAfterStop() override; 130 131 void SetUnixSignals(const lldb::UnixSignalsSP &signals_sp); 132 133 //------------------------------------------------------------------ 134 // Process Queries 135 //------------------------------------------------------------------ 136 bool IsAlive() override; 137 138 lldb::addr_t GetImageInfoAddress() override; 139 140 void WillPublicStop() override; 141 142 //------------------------------------------------------------------ 143 // Process Memory 144 //------------------------------------------------------------------ 145 size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size, 146 Error &error) override; 147 148 size_t DoWriteMemory(lldb::addr_t addr, const void *buf, size_t size, 149 Error &error) override; 150 151 lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions, 152 Error &error) override; 153 154 Error GetMemoryRegionInfo(lldb::addr_t load_addr, 155 MemoryRegionInfo ®ion_info) override; 156 157 Error DoDeallocateMemory(lldb::addr_t ptr) override; 158 159 //------------------------------------------------------------------ 160 // Process STDIO 161 //------------------------------------------------------------------ 162 size_t PutSTDIN(const char *buf, size_t buf_size, Error &error) override; 163 164 //---------------------------------------------------------------------- 165 // Process Breakpoints 166 //---------------------------------------------------------------------- 167 Error EnableBreakpointSite(BreakpointSite *bp_site) override; 168 169 Error DisableBreakpointSite(BreakpointSite *bp_site) override; 170 171 //---------------------------------------------------------------------- 172 // Process Watchpoints 173 //---------------------------------------------------------------------- 174 Error EnableWatchpoint(Watchpoint *wp, bool notify = true) override; 175 176 Error DisableWatchpoint(Watchpoint *wp, bool notify = true) override; 177 178 Error GetWatchpointSupportInfo(uint32_t &num) override; 179 180 Error GetWatchpointSupportInfo(uint32_t &num, bool &after) override; 181 182 bool StartNoticingNewThreads() override; 183 184 bool StopNoticingNewThreads() override; 185 186 GDBRemoteCommunicationClient &GetGDBRemote() { return m_gdb_comm; } 187 188 Error SendEventData(const char *data) override; 189 190 //---------------------------------------------------------------------- 191 // Override DidExit so we can disconnect from the remote GDB server 192 //---------------------------------------------------------------------- 193 void DidExit() override; 194 195 void SetUserSpecifiedMaxMemoryTransferSize(uint64_t user_specified_max); 196 197 bool GetModuleSpec(const FileSpec &module_file_spec, const ArchSpec &arch, 198 ModuleSpec &module_spec) override; 199 200 void PrefetchModuleSpecs(llvm::ArrayRef<FileSpec> module_file_specs, 201 const llvm::Triple &triple) override; 202 203 bool GetHostOSVersion(uint32_t &major, uint32_t &minor, 204 uint32_t &update) override; 205 206 size_t LoadModules(LoadedModuleInfoList &module_list) override; 207 208 size_t LoadModules() override; 209 210 Error GetFileLoadAddress(const FileSpec &file, bool &is_loaded, 211 lldb::addr_t &load_addr) override; 212 213 void ModulesDidLoad(ModuleList &module_list) override; 214 215 StructuredData::ObjectSP 216 GetLoadedDynamicLibrariesInfos(lldb::addr_t image_list_address, 217 lldb::addr_t image_count) override; 218 219 Error 220 ConfigureStructuredData(const ConstString &type_name, 221 const StructuredData::ObjectSP &config_sp) override; 222 223 StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos() override; 224 225 StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos( 226 const std::vector<lldb::addr_t> &load_addresses) override; 227 228 StructuredData::ObjectSP 229 GetLoadedDynamicLibrariesInfos_sender(StructuredData::ObjectSP args); 230 231 StructuredData::ObjectSP GetSharedCacheInfo() override; 232 233 std::string HarmonizeThreadIdsForProfileData( 234 StringExtractorGDBRemote &inputStringExtractor); 235 236 protected: 237 friend class ThreadGDBRemote; 238 friend class GDBRemoteCommunicationClient; 239 friend class GDBRemoteRegisterContext; 240 241 //------------------------------------------------------------------ 242 /// Broadcaster event bits definitions. 243 //------------------------------------------------------------------ 244 enum { 245 eBroadcastBitAsyncContinue = (1 << 0), 246 eBroadcastBitAsyncThreadShouldExit = (1 << 1), 247 eBroadcastBitAsyncThreadDidExit = (1 << 2) 248 }; 249 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 254 // stack replaces 255 // the last stop 256 // packet variable 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 291 292 //---------------------------------------------------------------------- 293 // Accessors 294 //---------------------------------------------------------------------- 295 bool IsRunning(lldb::StateType state) { 296 return state == lldb::eStateRunning || IsStepping(state); 297 } 298 299 bool IsStepping(lldb::StateType state) { 300 return state == lldb::eStateStepping; 301 } 302 303 bool CanResume(lldb::StateType state) { return state == lldb::eStateStopped; } 304 305 bool HasExited(lldb::StateType state) { return state == lldb::eStateExited; } 306 307 bool ProcessIDIsValid() const; 308 309 void Clear(); 310 311 Flags &GetFlags() { return m_flags; } 312 313 const Flags &GetFlags() const { return m_flags; } 314 315 bool UpdateThreadList(ThreadList &old_thread_list, 316 ThreadList &new_thread_list) override; 317 318 Error EstablishConnectionIfNeeded(const ProcessInfo &process_info); 319 320 Error LaunchAndConnectToDebugserver(const ProcessInfo &process_info); 321 322 void KillDebugserverProcess(); 323 324 void BuildDynamicRegisterInfo(bool force); 325 326 void SetLastStopPacket(const StringExtractorGDBRemote &response); 327 328 bool ParsePythonTargetDefinition(const FileSpec &target_definition_fspec); 329 330 const lldb::DataBufferSP GetAuxvData() override; 331 332 StructuredData::ObjectSP GetExtendedInfoForThread(lldb::tid_t tid); 333 334 void GetMaxMemorySize(); 335 336 bool CalculateThreadStopInfo(ThreadGDBRemote *thread); 337 338 size_t UpdateThreadPCsFromStopReplyThreadsValue(std::string &value); 339 340 size_t UpdateThreadIDsFromStopReplyThreadsValue(std::string &value); 341 342 bool HandleNotifyPacket(StringExtractorGDBRemote &packet); 343 344 bool StartAsyncThread(); 345 346 void StopAsyncThread(); 347 348 static lldb::thread_result_t AsyncThread(void *arg); 349 350 static bool 351 MonitorDebugserverProcess(std::weak_ptr<ProcessGDBRemote> process_wp, 352 lldb::pid_t pid, bool exited, int signo, 353 int exit_status); 354 355 lldb::StateType SetThreadStopInfo(StringExtractor &stop_packet); 356 357 bool 358 GetThreadStopInfoFromJSON(ThreadGDBRemote *thread, 359 const StructuredData::ObjectSP &thread_infos_sp); 360 361 lldb::ThreadSP SetThreadStopInfo(StructuredData::Dictionary *thread_dict); 362 363 lldb::ThreadSP 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); 373 374 void HandleStopReplySequence(); 375 376 void ClearThreadIDList(); 377 378 bool UpdateThreadIDList(); 379 380 void DidLaunchOrAttach(ArchSpec &process_arch); 381 382 Error ConnectToDebugserver(llvm::StringRef host_port); 383 384 const char *GetDispatchQueueNameForThread(lldb::addr_t thread_dispatch_qaddr, 385 std::string &dispatch_queue_name); 386 387 DynamicLoader *GetDynamicLoader() override; 388 389 // Query remote GDBServer for register information 390 bool GetGDBServerRegisterInfo(ArchSpec &arch); 391 392 // Query remote GDBServer for a detailed loaded library list 393 Error GetLoadedModuleList(LoadedModuleInfoList &); 394 395 lldb::ModuleSP LoadModuleAtAddress(const FileSpec &file, 396 lldb::addr_t link_map, 397 lldb::addr_t base_addr, 398 bool value_is_offset); 399 400 private: 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; 406 407 static bool NewThreadNotifyBreakpointHit(void *baton, 408 StoppointCallbackContext *context, 409 lldb::user_id_t break_id, 410 lldb::user_id_t break_loc_id); 411 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; 419 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 423 // set. 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(); } 430 431 static ModuleCacheKey getTombstoneKey() { return ModuleCacheKey("", "T"); } 432 433 static unsigned getHashValue(const ModuleCacheKey &key) { 434 return llvm::hash_combine(key.first, key.second); 435 } 436 437 static bool isEqual(const ModuleCacheKey &LHS, const ModuleCacheKey &RHS) { 438 return LHS == RHS; 439 } 440 }; 441 442 llvm::DenseMap<ModuleCacheKey, ModuleSpec, ModuleCacheInfo> 443 m_cached_module_specs; 444 445 DISALLOW_COPY_AND_ASSIGN(ProcessGDBRemote); 446 }; 447 448 } // namespace process_gdb_remote 449 } // namespace lldb_private 450 451 #endif // liblldb_ProcessGDBRemote_h_ 452