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