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/LoadedModuleInfoList.h" 26 #include "lldb/Core/ModuleSpec.h" 27 #include "lldb/Core/ThreadSafeValue.h" 28 #include "lldb/Host/HostThread.h" 29 #include "lldb/Target/Process.h" 30 #include "lldb/Target/Thread.h" 31 #include "lldb/Utility/ConstString.h" 32 #include "lldb/Utility/Status.h" 33 #include "lldb/Utility/StreamGDBRemote.h" 34 #include "lldb/Utility/StreamString.h" 35 #include "lldb/Utility/StringExtractor.h" 36 #include "lldb/Utility/StringList.h" 37 #include "lldb/Utility/StructuredData.h" 38 #include "lldb/lldb-private-forward.h" 39 40 #include "GDBRemoteCommunicationClient.h" 41 #include "GDBRemoteRegisterContext.h" 42 43 #include "llvm/ADT/DenseMap.h" 44 45 namespace lldb_private { 46 namespace process_gdb_remote { 47 48 class ThreadGDBRemote; 49 50 class ProcessGDBRemote : public Process, 51 private GDBRemoteClientBase::ContinueDelegate { 52 public: 53 ProcessGDBRemote(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp); 54 55 ~ProcessGDBRemote() override; 56 57 static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, 58 lldb::ListenerSP listener_sp, 59 const FileSpec *crash_file_path); 60 61 static void Initialize(); 62 63 static void DebuggerInitialize(Debugger &debugger); 64 65 static void Terminate(); 66 67 static ConstString GetPluginNameStatic(); 68 69 static const char *GetPluginDescriptionStatic(); 70 71 //------------------------------------------------------------------ 72 // Check if a given Process 73 //------------------------------------------------------------------ 74 bool CanDebug(lldb::TargetSP target_sp, 75 bool plugin_specified_by_name) override; 76 77 CommandObject *GetPluginCommandObject() override; 78 79 //------------------------------------------------------------------ 80 // Creating a new process, or attaching to an existing one 81 //------------------------------------------------------------------ 82 Status WillLaunch(Module *module) override; 83 84 Status DoLaunch(Module *exe_module, ProcessLaunchInfo &launch_info) override; 85 86 void DidLaunch() override; 87 88 Status WillAttachToProcessWithID(lldb::pid_t pid) override; 89 90 Status WillAttachToProcessWithName(const char *process_name, 91 bool wait_for_launch) override; 92 93 Status DoConnectRemote(Stream *strm, llvm::StringRef remote_url) override; 94 95 Status WillLaunchOrAttach(); 96 97 Status DoAttachToProcessWithID(lldb::pid_t pid, 98 const ProcessAttachInfo &attach_info) override; 99 100 Status 101 DoAttachToProcessWithName(const char *process_name, 102 const ProcessAttachInfo &attach_info) override; 103 104 void DidAttach(ArchSpec &process_arch) override; 105 106 //------------------------------------------------------------------ 107 // PluginInterface protocol 108 //------------------------------------------------------------------ 109 ConstString GetPluginName() override; 110 111 uint32_t GetPluginVersion() override; 112 113 //------------------------------------------------------------------ 114 // Process Control 115 //------------------------------------------------------------------ 116 Status WillResume() override; 117 118 Status DoResume() override; 119 120 Status DoHalt(bool &caused_stop) override; 121 122 Status DoDetach(bool keep_stopped) override; 123 124 bool DetachRequiresHalt() override { return true; } 125 126 Status DoSignal(int signal) override; 127 128 Status DoDestroy() override; 129 130 void RefreshStateAfterStop() override; 131 132 void SetUnixSignals(const lldb::UnixSignalsSP &signals_sp); 133 134 //------------------------------------------------------------------ 135 // Process Queries 136 //------------------------------------------------------------------ 137 bool IsAlive() override; 138 139 lldb::addr_t GetImageInfoAddress() override; 140 141 void WillPublicStop() override; 142 143 //------------------------------------------------------------------ 144 // Process Memory 145 //------------------------------------------------------------------ 146 size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size, 147 Status &error) override; 148 149 size_t DoWriteMemory(lldb::addr_t addr, const void *buf, size_t size, 150 Status &error) override; 151 152 lldb::addr_t DoAllocateMemory(size_t size, uint32_t permissions, 153 Status &error) override; 154 155 Status GetMemoryRegionInfo(lldb::addr_t load_addr, 156 MemoryRegionInfo ®ion_info) override; 157 158 Status DoDeallocateMemory(lldb::addr_t ptr) override; 159 160 //------------------------------------------------------------------ 161 // Process STDIO 162 //------------------------------------------------------------------ 163 size_t PutSTDIN(const char *buf, size_t buf_size, Status &error) override; 164 165 //---------------------------------------------------------------------- 166 // Process Breakpoints 167 //---------------------------------------------------------------------- 168 Status EnableBreakpointSite(BreakpointSite *bp_site) override; 169 170 Status DisableBreakpointSite(BreakpointSite *bp_site) override; 171 172 //---------------------------------------------------------------------- 173 // Process Watchpoints 174 //---------------------------------------------------------------------- 175 Status EnableWatchpoint(Watchpoint *wp, bool notify = true) override; 176 177 Status DisableWatchpoint(Watchpoint *wp, bool notify = true) override; 178 179 Status GetWatchpointSupportInfo(uint32_t &num) override; 180 181 lldb::user_id_t StartTrace(const TraceOptions &options, 182 Status &error) override; 183 184 Status StopTrace(lldb::user_id_t uid, lldb::tid_t thread_id) override; 185 186 Status GetData(lldb::user_id_t uid, lldb::tid_t thread_id, 187 llvm::MutableArrayRef<uint8_t> &buffer, 188 size_t offset = 0) override; 189 190 Status GetMetaData(lldb::user_id_t uid, lldb::tid_t thread_id, 191 llvm::MutableArrayRef<uint8_t> &buffer, 192 size_t offset = 0) override; 193 194 Status GetTraceConfig(lldb::user_id_t uid, TraceOptions &options) override; 195 196 Status GetWatchpointSupportInfo(uint32_t &num, bool &after) override; 197 198 bool StartNoticingNewThreads() override; 199 200 bool StopNoticingNewThreads() override; 201 202 GDBRemoteCommunicationClient &GetGDBRemote() { return m_gdb_comm; } 203 204 Status SendEventData(const char *data) override; 205 206 //---------------------------------------------------------------------- 207 // Override DidExit so we can disconnect from the remote GDB server 208 //---------------------------------------------------------------------- 209 void DidExit() override; 210 211 void SetUserSpecifiedMaxMemoryTransferSize(uint64_t user_specified_max); 212 213 bool GetModuleSpec(const FileSpec &module_file_spec, const ArchSpec &arch, 214 ModuleSpec &module_spec) override; 215 216 void PrefetchModuleSpecs(llvm::ArrayRef<FileSpec> module_file_specs, 217 const llvm::Triple &triple) override; 218 219 bool GetHostOSVersion(uint32_t &major, uint32_t &minor, 220 uint32_t &update) override; 221 222 size_t LoadModules(LoadedModuleInfoList &module_list) override; 223 224 size_t LoadModules() override; 225 226 Status GetFileLoadAddress(const FileSpec &file, bool &is_loaded, 227 lldb::addr_t &load_addr) override; 228 229 void ModulesDidLoad(ModuleList &module_list) override; 230 231 StructuredData::ObjectSP 232 GetLoadedDynamicLibrariesInfos(lldb::addr_t image_list_address, 233 lldb::addr_t image_count) override; 234 235 Status 236 ConfigureStructuredData(const ConstString &type_name, 237 const StructuredData::ObjectSP &config_sp) override; 238 239 StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos() override; 240 241 StructuredData::ObjectSP GetLoadedDynamicLibrariesInfos( 242 const std::vector<lldb::addr_t> &load_addresses) override; 243 244 StructuredData::ObjectSP 245 GetLoadedDynamicLibrariesInfos_sender(StructuredData::ObjectSP args); 246 247 StructuredData::ObjectSP GetSharedCacheInfo() override; 248 249 std::string HarmonizeThreadIdsForProfileData( 250 StringExtractorGDBRemote &inputStringExtractor); 251 252 protected: 253 friend class ThreadGDBRemote; 254 friend class GDBRemoteCommunicationClient; 255 friend class GDBRemoteRegisterContext; 256 257 //------------------------------------------------------------------ 258 /// Broadcaster event bits definitions. 259 //------------------------------------------------------------------ 260 enum { 261 eBroadcastBitAsyncContinue = (1 << 0), 262 eBroadcastBitAsyncThreadShouldExit = (1 << 1), 263 eBroadcastBitAsyncThreadDidExit = (1 << 2) 264 }; 265 266 GDBRemoteCommunicationClient m_gdb_comm; 267 std::atomic<lldb::pid_t> m_debugserver_pid; 268 std::vector<StringExtractorGDBRemote> m_stop_packet_stack; // The stop packet 269 // stack replaces 270 // the last stop 271 // packet variable 272 std::recursive_mutex m_last_stop_packet_mutex; 273 GDBRemoteDynamicRegisterInfo m_register_info; 274 Broadcaster m_async_broadcaster; 275 lldb::ListenerSP m_async_listener_sp; 276 HostThread m_async_thread; 277 std::recursive_mutex m_async_thread_state_mutex; 278 typedef std::vector<lldb::tid_t> tid_collection; 279 typedef std::vector<std::pair<lldb::tid_t, int>> tid_sig_collection; 280 typedef std::map<lldb::addr_t, lldb::addr_t> MMapMap; 281 typedef std::map<uint32_t, std::string> ExpeditedRegisterMap; 282 tid_collection m_thread_ids; // Thread IDs for all threads. This list gets 283 // updated after stopping 284 std::vector<lldb::addr_t> m_thread_pcs; // PC values for all the threads. 285 StructuredData::ObjectSP m_jstopinfo_sp; // Stop info only for any threads 286 // that have valid stop infos 287 StructuredData::ObjectSP m_jthreadsinfo_sp; // Full stop info, expedited 288 // registers and memory for all 289 // threads if "jThreadsInfo" 290 // packet is supported 291 tid_collection m_continue_c_tids; // 'c' for continue 292 tid_sig_collection m_continue_C_tids; // 'C' for continue with signal 293 tid_collection m_continue_s_tids; // 's' for step 294 tid_sig_collection m_continue_S_tids; // 'S' for step with signal 295 uint64_t m_max_memory_size; // The maximum number of bytes to read/write when 296 // reading and writing memory 297 uint64_t m_remote_stub_max_memory_size; // The maximum memory size the remote 298 // gdb stub can handle 299 MMapMap m_addr_to_mmap_size; 300 lldb::BreakpointSP m_thread_create_bp_sp; 301 bool m_waiting_for_attach; 302 bool m_destroy_tried_resuming; 303 lldb::CommandObjectSP m_command_sp; 304 int64_t m_breakpoint_pc_offset; 305 lldb::tid_t m_initial_tid; // The initial thread ID, given by stub on attach 306 307 //---------------------------------------------------------------------- 308 // Accessors 309 //---------------------------------------------------------------------- 310 bool IsRunning(lldb::StateType state) { 311 return state == lldb::eStateRunning || IsStepping(state); 312 } 313 314 bool IsStepping(lldb::StateType state) { 315 return state == lldb::eStateStepping; 316 } 317 318 bool CanResume(lldb::StateType state) { return state == lldb::eStateStopped; } 319 320 bool HasExited(lldb::StateType state) { return state == lldb::eStateExited; } 321 322 bool ProcessIDIsValid() const; 323 324 void Clear(); 325 326 bool UpdateThreadList(ThreadList &old_thread_list, 327 ThreadList &new_thread_list) override; 328 329 Status EstablishConnectionIfNeeded(const ProcessInfo &process_info); 330 331 Status LaunchAndConnectToDebugserver(const ProcessInfo &process_info); 332 333 void KillDebugserverProcess(); 334 335 void BuildDynamicRegisterInfo(bool force); 336 337 void SetLastStopPacket(const StringExtractorGDBRemote &response); 338 339 bool ParsePythonTargetDefinition(const FileSpec &target_definition_fspec); 340 341 const lldb::DataBufferSP GetAuxvData() override; 342 343 StructuredData::ObjectSP GetExtendedInfoForThread(lldb::tid_t tid); 344 345 void GetMaxMemorySize(); 346 347 bool CalculateThreadStopInfo(ThreadGDBRemote *thread); 348 349 size_t UpdateThreadPCsFromStopReplyThreadsValue(std::string &value); 350 351 size_t UpdateThreadIDsFromStopReplyThreadsValue(std::string &value); 352 353 bool HandleNotifyPacket(StringExtractorGDBRemote &packet); 354 355 bool StartAsyncThread(); 356 357 void StopAsyncThread(); 358 359 static lldb::thread_result_t AsyncThread(void *arg); 360 361 static bool 362 MonitorDebugserverProcess(std::weak_ptr<ProcessGDBRemote> process_wp, 363 lldb::pid_t pid, bool exited, int signo, 364 int exit_status); 365 366 lldb::StateType SetThreadStopInfo(StringExtractor &stop_packet); 367 368 bool 369 GetThreadStopInfoFromJSON(ThreadGDBRemote *thread, 370 const StructuredData::ObjectSP &thread_infos_sp); 371 372 lldb::ThreadSP SetThreadStopInfo(StructuredData::Dictionary *thread_dict); 373 374 lldb::ThreadSP 375 SetThreadStopInfo(lldb::tid_t tid, 376 ExpeditedRegisterMap &expedited_register_map, uint8_t signo, 377 const std::string &thread_name, const std::string &reason, 378 const std::string &description, uint32_t exc_type, 379 const std::vector<lldb::addr_t> &exc_data, 380 lldb::addr_t thread_dispatch_qaddr, bool queue_vars_valid, 381 lldb_private::LazyBool associated_with_libdispatch_queue, 382 lldb::addr_t dispatch_queue_t, std::string &queue_name, 383 lldb::QueueKind queue_kind, uint64_t queue_serial); 384 385 void HandleStopReplySequence(); 386 387 void ClearThreadIDList(); 388 389 bool UpdateThreadIDList(); 390 391 void DidLaunchOrAttach(ArchSpec &process_arch); 392 393 Status ConnectToDebugserver(llvm::StringRef host_port); 394 395 const char *GetDispatchQueueNameForThread(lldb::addr_t thread_dispatch_qaddr, 396 std::string &dispatch_queue_name); 397 398 DynamicLoader *GetDynamicLoader() override; 399 400 // Query remote GDBServer for register information 401 bool GetGDBServerRegisterInfo(ArchSpec &arch); 402 403 // Query remote GDBServer for a detailed loaded library list 404 Status GetLoadedModuleList(LoadedModuleInfoList &); 405 406 lldb::ModuleSP LoadModuleAtAddress(const FileSpec &file, 407 lldb::addr_t link_map, 408 lldb::addr_t base_addr, 409 bool value_is_offset); 410 411 Status UpdateAutomaticSignalFiltering() override; 412 413 private: 414 //------------------------------------------------------------------ 415 // For ProcessGDBRemote only 416 //------------------------------------------------------------------ 417 std::string m_partial_profile_data; 418 std::map<uint64_t, uint32_t> m_thread_id_to_used_usec_map; 419 uint64_t m_last_signals_version = 0; 420 421 static bool NewThreadNotifyBreakpointHit(void *baton, 422 StoppointCallbackContext *context, 423 lldb::user_id_t break_id, 424 lldb::user_id_t break_loc_id); 425 426 //------------------------------------------------------------------ 427 // ContinueDelegate interface 428 //------------------------------------------------------------------ 429 void HandleAsyncStdout(llvm::StringRef out) override; 430 void HandleAsyncMisc(llvm::StringRef data) override; 431 void HandleStopReply() override; 432 void HandleAsyncStructuredDataPacket(llvm::StringRef data) override; 433 434 void SetThreadPc(const lldb::ThreadSP &thread_sp, uint64_t index); 435 using ModuleCacheKey = std::pair<std::string, std::string>; 436 // KeyInfo for the cached module spec DenseMap. 437 // The invariant is that all real keys will have the file and architecture 438 // set. 439 // The empty key has an empty file and an empty arch. 440 // The tombstone key has an invalid arch and an empty file. 441 // The comparison and hash functions take the file name and architecture 442 // triple into account. 443 struct ModuleCacheInfo { 444 static ModuleCacheKey getEmptyKey() { return ModuleCacheKey(); } 445 446 static ModuleCacheKey getTombstoneKey() { return ModuleCacheKey("", "T"); } 447 448 static unsigned getHashValue(const ModuleCacheKey &key) { 449 return llvm::hash_combine(key.first, key.second); 450 } 451 452 static bool isEqual(const ModuleCacheKey &LHS, const ModuleCacheKey &RHS) { 453 return LHS == RHS; 454 } 455 }; 456 457 llvm::DenseMap<ModuleCacheKey, ModuleSpec, ModuleCacheInfo> 458 m_cached_module_specs; 459 460 DISALLOW_COPY_AND_ASSIGN(ProcessGDBRemote); 461 }; 462 463 } // namespace process_gdb_remote 464 } // namespace lldb_private 465 466 #endif // liblldb_ProcessGDBRemote_h_ 467