Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
| 18 | // Other libraries and framework includes |
| 19 | #include "lldb/Core/ArchSpec.h" |
| 20 | #include "lldb/Core/Broadcaster.h" |
| 21 | #include "lldb/Core/Error.h" |
| 22 | #include "lldb/Core/InputReader.h" |
| 23 | #include "lldb/Core/StreamString.h" |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 24 | #include "lldb/Core/StringList.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | #include "lldb/Core/ThreadSafeValue.h" |
| 26 | #include "lldb/Target/Process.h" |
| 27 | #include "lldb/Target/Thread.h" |
| 28 | |
| 29 | #include "GDBRemoteCommunication.h" |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 30 | #include "Utility/StringExtractor.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | #include "GDBRemoteRegisterContext.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | |
| 33 | class ThreadGDBRemote; |
| 34 | |
| 35 | class ProcessGDBRemote : public lldb_private::Process |
| 36 | { |
| 37 | public: |
| 38 | //------------------------------------------------------------------ |
| 39 | // Constructors and Destructors |
| 40 | //------------------------------------------------------------------ |
| 41 | static Process* |
| 42 | CreateInstance (lldb_private::Target& target, lldb_private::Listener &listener); |
| 43 | |
| 44 | static void |
| 45 | Initialize(); |
| 46 | |
| 47 | static void |
| 48 | Terminate(); |
| 49 | |
| 50 | static const char * |
| 51 | GetPluginNameStatic(); |
| 52 | |
| 53 | static const char * |
| 54 | GetPluginDescriptionStatic(); |
| 55 | |
| 56 | //------------------------------------------------------------------ |
| 57 | // Constructors and Destructors |
| 58 | //------------------------------------------------------------------ |
| 59 | ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener); |
| 60 | |
| 61 | virtual |
| 62 | ~ProcessGDBRemote(); |
| 63 | |
| 64 | //------------------------------------------------------------------ |
| 65 | // Check if a given Process |
| 66 | //------------------------------------------------------------------ |
| 67 | virtual bool |
| 68 | CanDebug (lldb_private::Target &target); |
| 69 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 70 | virtual uint32_t |
| 71 | ListProcessesMatchingName (const char *name, lldb_private::StringList &matches, std::vector<lldb::pid_t> &pids); |
| 72 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | //------------------------------------------------------------------ |
| 74 | // Creating a new process, or attaching to an existing one |
| 75 | //------------------------------------------------------------------ |
| 76 | virtual lldb_private::Error |
| 77 | WillLaunch (lldb_private::Module* module); |
| 78 | |
| 79 | virtual lldb_private::Error |
| 80 | DoLaunch (lldb_private::Module* module, |
| 81 | char const *argv[], // Can be NULL |
| 82 | char const *envp[], // Can be NULL |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 83 | uint32_t flags, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | const char *stdin_path, // Can be NULL |
Greg Clayton | de915be | 2011-01-23 05:56:20 +0000 | [diff] [blame^] | 85 | const char *stdout_path, // Can be NULL |
| 86 | const char *stderr_path, // Can be NULL |
| 87 | const char *working_dir); // Can be NULL |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | |
| 89 | virtual void |
| 90 | DidLaunch (); |
| 91 | |
| 92 | virtual lldb_private::Error |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 93 | WillAttachToProcessWithID (lldb::pid_t pid); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | |
| 95 | virtual lldb_private::Error |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 96 | WillAttachToProcessWithName (const char *process_name, bool wait_for_launch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | |
| 98 | lldb_private::Error |
| 99 | WillLaunchOrAttach (); |
| 100 | |
| 101 | virtual lldb_private::Error |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 102 | DoAttachToProcessWithID (lldb::pid_t pid); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | |
| 104 | virtual lldb_private::Error |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 105 | DoAttachToProcessWithName (const char *process_name, bool wait_for_launch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | |
| 107 | virtual void |
| 108 | DidAttach (); |
| 109 | |
| 110 | //------------------------------------------------------------------ |
| 111 | // PluginInterface protocol |
| 112 | //------------------------------------------------------------------ |
| 113 | virtual const char * |
| 114 | GetPluginName(); |
| 115 | |
| 116 | virtual const char * |
| 117 | GetShortPluginName(); |
| 118 | |
| 119 | virtual uint32_t |
| 120 | GetPluginVersion(); |
| 121 | |
| 122 | virtual void |
| 123 | GetPluginCommandHelp (const char *command, lldb_private::Stream *strm); |
| 124 | |
| 125 | virtual lldb_private::Error |
| 126 | ExecutePluginCommand (lldb_private::Args &command, lldb_private::Stream *strm); |
| 127 | |
| 128 | virtual lldb_private::Log * |
| 129 | EnablePluginLogging (lldb_private::Stream *strm, lldb_private::Args &command); |
| 130 | |
| 131 | //------------------------------------------------------------------ |
| 132 | // Process Control |
| 133 | //------------------------------------------------------------------ |
| 134 | virtual lldb_private::Error |
| 135 | WillResume (); |
| 136 | |
| 137 | virtual lldb_private::Error |
| 138 | DoResume (); |
| 139 | |
| 140 | virtual lldb_private::Error |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 141 | DoHalt (bool &caused_stop); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | |
| 143 | virtual lldb_private::Error |
| 144 | WillDetach (); |
| 145 | |
| 146 | virtual lldb_private::Error |
| 147 | DoDetach (); |
| 148 | |
| 149 | virtual lldb_private::Error |
| 150 | DoSignal (int signal); |
| 151 | |
| 152 | virtual lldb_private::Error |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 153 | WillDestroy (); |
| 154 | |
| 155 | virtual lldb_private::Error |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 156 | DoDestroy (); |
| 157 | |
| 158 | virtual void |
| 159 | RefreshStateAfterStop(); |
| 160 | |
| 161 | //------------------------------------------------------------------ |
| 162 | // Process Queries |
| 163 | //------------------------------------------------------------------ |
| 164 | virtual bool |
| 165 | IsAlive (); |
| 166 | |
| 167 | virtual lldb::addr_t |
| 168 | GetImageInfoAddress(); |
| 169 | |
| 170 | //------------------------------------------------------------------ |
| 171 | // Process Memory |
| 172 | //------------------------------------------------------------------ |
| 173 | virtual size_t |
| 174 | DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error); |
| 175 | |
| 176 | virtual size_t |
| 177 | DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error); |
| 178 | |
| 179 | virtual lldb::addr_t |
| 180 | DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error); |
| 181 | |
| 182 | virtual lldb_private::Error |
| 183 | DoDeallocateMemory (lldb::addr_t ptr); |
| 184 | |
| 185 | //------------------------------------------------------------------ |
| 186 | // Process STDIO |
| 187 | //------------------------------------------------------------------ |
| 188 | virtual size_t |
| 189 | GetSTDOUT (char *buf, size_t buf_size, lldb_private::Error &error); |
| 190 | |
| 191 | virtual size_t |
| 192 | GetSTDERR (char *buf, size_t buf_size, lldb_private::Error &error); |
| 193 | |
| 194 | virtual size_t |
| 195 | PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error); |
| 196 | |
| 197 | //---------------------------------------------------------------------- |
| 198 | // Process Breakpoints |
| 199 | //---------------------------------------------------------------------- |
| 200 | virtual size_t |
| 201 | GetSoftwareBreakpointTrapOpcode (lldb_private::BreakpointSite *bp_site); |
| 202 | |
| 203 | //---------------------------------------------------------------------- |
| 204 | // Process Breakpoints |
| 205 | //---------------------------------------------------------------------- |
| 206 | virtual lldb_private::Error |
| 207 | EnableBreakpoint (lldb_private::BreakpointSite *bp_site); |
| 208 | |
| 209 | virtual lldb_private::Error |
| 210 | DisableBreakpoint (lldb_private::BreakpointSite *bp_site); |
| 211 | |
| 212 | //---------------------------------------------------------------------- |
| 213 | // Process Watchpoints |
| 214 | //---------------------------------------------------------------------- |
| 215 | virtual lldb_private::Error |
| 216 | EnableWatchpoint (lldb_private::WatchpointLocation *wp_loc); |
| 217 | |
| 218 | virtual lldb_private::Error |
| 219 | DisableWatchpoint (lldb_private::WatchpointLocation *wp_loc); |
| 220 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 221 | virtual lldb_private::DynamicLoader * |
| 222 | GetDynamicLoader (); |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 223 | |
| 224 | virtual bool |
| 225 | StartNoticingNewThreads(); |
| 226 | |
| 227 | virtual bool |
| 228 | StopNoticingNewThreads(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 229 | |
| 230 | protected: |
| 231 | friend class ThreadGDBRemote; |
| 232 | friend class GDBRemoteCommunication; |
| 233 | friend class GDBRemoteRegisterContext; |
| 234 | |
| 235 | bool |
| 236 | SetCurrentGDBRemoteThread (int tid); |
| 237 | |
| 238 | bool |
| 239 | SetCurrentGDBRemoteThreadForRun (int tid); |
| 240 | |
| 241 | //---------------------------------------------------------------------- |
| 242 | // Accessors |
| 243 | //---------------------------------------------------------------------- |
| 244 | bool |
| 245 | IsRunning ( lldb::StateType state ) |
| 246 | { |
| 247 | return state == lldb::eStateRunning || IsStepping(state); |
| 248 | } |
| 249 | |
| 250 | bool |
| 251 | IsStepping ( lldb::StateType state) |
| 252 | { |
| 253 | return state == lldb::eStateStepping; |
| 254 | } |
| 255 | bool |
| 256 | CanResume ( lldb::StateType state) |
| 257 | { |
| 258 | return state == lldb::eStateStopped; |
| 259 | } |
| 260 | |
| 261 | bool |
| 262 | HasExited (lldb::StateType state) |
| 263 | { |
| 264 | return state == lldb::eStateExited; |
| 265 | } |
| 266 | |
| 267 | bool |
| 268 | ProcessIDIsValid ( ) const; |
| 269 | |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 270 | // static void |
| 271 | // STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 272 | |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 273 | // void |
| 274 | // AppendSTDOUT (const char* s, size_t len); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 276 | void |
| 277 | Clear ( ); |
| 278 | |
| 279 | lldb_private::Flags & |
| 280 | GetFlags () |
| 281 | { |
| 282 | return m_flags; |
| 283 | } |
| 284 | |
| 285 | const lldb_private::Flags & |
| 286 | GetFlags () const |
| 287 | { |
| 288 | return m_flags; |
| 289 | } |
| 290 | |
| 291 | uint32_t |
| 292 | UpdateThreadListIfNeeded (); |
| 293 | |
| 294 | lldb_private::Error |
Greg Clayton | 23cf0c7 | 2010-11-08 04:29:11 +0000 | [diff] [blame] | 295 | StartDebugserverProcess (const char *debugserver_url, // The connection string to use in the spawned debugserver ("localhost:1234" or "/dev/tty...") |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 296 | char const *inferior_argv[], |
| 297 | char const *inferior_envp[], |
| 298 | const char *stdin_path, |
Greg Clayton | de915be | 2011-01-23 05:56:20 +0000 | [diff] [blame^] | 299 | const char *stdout_path, |
| 300 | const char *stderr_path, |
| 301 | const char *working_dir, |
Greg Clayton | 23cf0c7 | 2010-11-08 04:29:11 +0000 | [diff] [blame] | 302 | bool launch_process, // Set to true if we are going to be launching a the process |
| 303 | lldb::pid_t attach_pid, // If inferior inferior_argv == NULL, then attach to this pid |
| 304 | const char *attach_pid_name, // Wait for the next process to launch whose basename matches "attach_wait_name" |
| 305 | bool wait_for_launch, // Wait for the process named "attach_wait_name" to launch |
Caroline Tice | bd66601 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 306 | uint32_t launch_flags, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | lldb_private::ArchSpec& arch_spec); |
| 308 | |
| 309 | void |
| 310 | KillDebugserverProcess (); |
| 311 | |
| 312 | void |
| 313 | BuildDynamicRegisterInfo (); |
| 314 | |
| 315 | GDBRemoteCommunication & |
| 316 | GetGDBRemote() |
| 317 | { |
| 318 | return m_gdb_comm; |
| 319 | } |
| 320 | |
| 321 | //------------------------------------------------------------------ |
| 322 | /// Broadcaster event bits definitions. |
| 323 | //------------------------------------------------------------------ |
| 324 | enum |
| 325 | { |
| 326 | eBroadcastBitAsyncContinue = (1 << 0), |
| 327 | eBroadcastBitAsyncThreadShouldExit = (1 << 1) |
| 328 | }; |
| 329 | |
| 330 | |
| 331 | std::auto_ptr<lldb_private::DynamicLoader> m_dynamic_loader_ap; |
| 332 | lldb_private::Flags m_flags; // Process specific flags (see eFlags enums) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 333 | lldb_private::Mutex m_stdio_mutex; // Multithreaded protection for stdio |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 334 | GDBRemoteCommunication m_gdb_comm; |
| 335 | lldb::pid_t m_debugserver_pid; |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 336 | lldb::thread_t m_debugserver_thread; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 337 | StringExtractor m_last_stop_packet; |
| 338 | GDBRemoteDynamicRegisterInfo m_register_info; |
| 339 | lldb_private::Broadcaster m_async_broadcaster; |
| 340 | lldb::thread_t m_async_thread; |
| 341 | // Current GDB remote state. Any members added here need to be reset to |
| 342 | // proper default values in ResetGDBRemoteState (). |
| 343 | lldb::tid_t m_curr_tid; // Current gdb remote protocol thread index for all other operations |
| 344 | lldb::tid_t m_curr_tid_run; // Current gdb remote protocol thread index for continue, step, etc |
| 345 | uint32_t m_z0_supported:1; // Set to non-zero if Z0 and z0 packets are supported |
| 346 | lldb_private::StreamString m_continue_packet; |
| 347 | lldb::addr_t m_dispatch_queue_offsets_addr; |
| 348 | uint32_t m_packet_timeout; |
| 349 | size_t m_max_memory_size; // The maximum number of bytes to read/write when reading and writing memory |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 350 | bool m_waiting_for_attach; |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 351 | bool m_local_debugserver; // Is the debugserver process we are talking to local or on another machine. |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 352 | std::vector<lldb::user_id_t> m_thread_observation_bps; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | |
| 354 | void |
| 355 | ResetGDBRemoteState (); |
| 356 | |
| 357 | bool |
| 358 | StartAsyncThread (); |
| 359 | |
| 360 | void |
| 361 | StopAsyncThread (); |
| 362 | |
| 363 | static void * |
| 364 | AsyncThread (void *arg); |
| 365 | |
| 366 | static bool |
| 367 | MonitorDebugserverProcess (void *callback_baton, |
| 368 | lldb::pid_t pid, |
| 369 | int signo, // Zero for no signal |
| 370 | int exit_status); // Exit value of process if signal is zero |
| 371 | |
| 372 | lldb::StateType |
| 373 | SetThreadStopInfo (StringExtractor& stop_packet); |
| 374 | |
| 375 | void |
| 376 | DidLaunchOrAttach (); |
| 377 | |
| 378 | lldb_private::Error |
| 379 | ConnectToDebugserver (const char *host_port); |
| 380 | |
| 381 | const char * |
| 382 | GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr, |
| 383 | std::string &dispatch_queue_name); |
| 384 | |
| 385 | static size_t |
| 386 | AttachInputReaderCallback (void *baton, |
| 387 | lldb_private::InputReader *reader, |
| 388 | lldb::InputReaderAction notification, |
| 389 | const char *bytes, |
| 390 | size_t bytes_len); |
| 391 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 392 | lldb_private::Error |
| 393 | InterruptIfRunning (bool discard_thread_plans, |
| 394 | bool catch_stop_event, |
| 395 | bool resume_private_state_thread, |
| 396 | lldb::EventSP &stop_event_sp); |
| 397 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 398 | private: |
| 399 | //------------------------------------------------------------------ |
| 400 | // For ProcessGDBRemote only |
| 401 | //------------------------------------------------------------------ |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 402 | static bool |
| 403 | NewThreadNotifyBreakpointHit (void *baton, |
| 404 | lldb_private::StoppointCallbackContext *context, |
| 405 | lldb::user_id_t break_id, |
| 406 | lldb::user_id_t break_loc_id); |
| 407 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 408 | DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote); |
| 409 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 410 | }; |
| 411 | |
| 412 | #endif // liblldb_ProcessGDBRemote_h_ |