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> |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 17 | #include <vector> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | |
| 19 | // Other libraries and framework includes |
| 20 | #include "lldb/Core/ArchSpec.h" |
| 21 | #include "lldb/Core/Broadcaster.h" |
| 22 | #include "lldb/Core/Error.h" |
| 23 | #include "lldb/Core/InputReader.h" |
| 24 | #include "lldb/Core/StreamString.h" |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 25 | #include "lldb/Core/StringList.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | #include "lldb/Core/ThreadSafeValue.h" |
| 27 | #include "lldb/Target/Process.h" |
| 28 | #include "lldb/Target/Thread.h" |
| 29 | |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 30 | #include "GDBRemoteCommunicationClient.h" |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 31 | #include "Utility/StringExtractor.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | #include "GDBRemoteRegisterContext.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | |
| 34 | class ThreadGDBRemote; |
| 35 | |
| 36 | class ProcessGDBRemote : public lldb_private::Process |
| 37 | { |
| 38 | public: |
| 39 | //------------------------------------------------------------------ |
| 40 | // Constructors and Destructors |
| 41 | //------------------------------------------------------------------ |
| 42 | static Process* |
| 43 | CreateInstance (lldb_private::Target& target, lldb_private::Listener &listener); |
| 44 | |
| 45 | static void |
| 46 | Initialize(); |
| 47 | |
| 48 | static void |
| 49 | Terminate(); |
| 50 | |
| 51 | static const char * |
| 52 | GetPluginNameStatic(); |
| 53 | |
| 54 | static const char * |
| 55 | GetPluginDescriptionStatic(); |
| 56 | |
| 57 | //------------------------------------------------------------------ |
| 58 | // Constructors and Destructors |
| 59 | //------------------------------------------------------------------ |
| 60 | ProcessGDBRemote(lldb_private::Target& target, lldb_private::Listener &listener); |
| 61 | |
| 62 | virtual |
| 63 | ~ProcessGDBRemote(); |
| 64 | |
| 65 | //------------------------------------------------------------------ |
| 66 | // Check if a given Process |
| 67 | //------------------------------------------------------------------ |
| 68 | virtual bool |
| 69 | CanDebug (lldb_private::Target &target); |
| 70 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 71 | // virtual uint32_t |
| 72 | // ListProcessesMatchingName (const char *name, lldb_private::StringList &matches, std::vector<lldb::pid_t> &pids); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | //------------------------------------------------------------------ |
| 75 | // Creating a new process, or attaching to an existing one |
| 76 | //------------------------------------------------------------------ |
| 77 | virtual lldb_private::Error |
| 78 | WillLaunch (lldb_private::Module* module); |
| 79 | |
| 80 | virtual lldb_private::Error |
| 81 | DoLaunch (lldb_private::Module* module, |
| 82 | char const *argv[], // Can be NULL |
| 83 | char const *envp[], // Can be NULL |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 84 | uint32_t flags, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | const char *stdin_path, // Can be NULL |
Greg Clayton | de915be | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 86 | const char *stdout_path, // Can be NULL |
| 87 | const char *stderr_path, // Can be NULL |
| 88 | const char *working_dir); // Can be NULL |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 89 | |
| 90 | virtual void |
| 91 | DidLaunch (); |
| 92 | |
| 93 | virtual lldb_private::Error |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 94 | WillAttachToProcessWithID (lldb::pid_t pid); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 95 | |
| 96 | virtual lldb_private::Error |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 97 | WillAttachToProcessWithName (const char *process_name, bool wait_for_launch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 99 | virtual lldb_private::Error |
| 100 | DoConnectRemote (const char *remote_url); |
| 101 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | lldb_private::Error |
| 103 | WillLaunchOrAttach (); |
| 104 | |
| 105 | virtual lldb_private::Error |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 106 | DoAttachToProcessWithID (lldb::pid_t pid); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | |
| 108 | virtual lldb_private::Error |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 109 | DoAttachToProcessWithName (const char *process_name, bool wait_for_launch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | |
| 111 | virtual void |
| 112 | DidAttach (); |
| 113 | |
| 114 | //------------------------------------------------------------------ |
| 115 | // PluginInterface protocol |
| 116 | //------------------------------------------------------------------ |
| 117 | virtual const char * |
| 118 | GetPluginName(); |
| 119 | |
| 120 | virtual const char * |
| 121 | GetShortPluginName(); |
| 122 | |
| 123 | virtual uint32_t |
| 124 | GetPluginVersion(); |
| 125 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 126 | //------------------------------------------------------------------ |
| 127 | // Process Control |
| 128 | //------------------------------------------------------------------ |
| 129 | virtual lldb_private::Error |
| 130 | WillResume (); |
| 131 | |
| 132 | virtual lldb_private::Error |
| 133 | DoResume (); |
| 134 | |
| 135 | virtual lldb_private::Error |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 136 | DoHalt (bool &caused_stop); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 137 | |
| 138 | virtual lldb_private::Error |
| 139 | WillDetach (); |
| 140 | |
| 141 | virtual lldb_private::Error |
| 142 | DoDetach (); |
| 143 | |
| 144 | virtual lldb_private::Error |
| 145 | DoSignal (int signal); |
| 146 | |
| 147 | virtual lldb_private::Error |
| 148 | DoDestroy (); |
| 149 | |
| 150 | virtual void |
| 151 | RefreshStateAfterStop(); |
| 152 | |
| 153 | //------------------------------------------------------------------ |
| 154 | // Process Queries |
| 155 | //------------------------------------------------------------------ |
| 156 | virtual bool |
| 157 | IsAlive (); |
| 158 | |
| 159 | virtual lldb::addr_t |
| 160 | GetImageInfoAddress(); |
| 161 | |
| 162 | //------------------------------------------------------------------ |
| 163 | // Process Memory |
| 164 | //------------------------------------------------------------------ |
| 165 | virtual size_t |
| 166 | DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error); |
| 167 | |
| 168 | virtual size_t |
| 169 | DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error); |
| 170 | |
| 171 | virtual lldb::addr_t |
| 172 | DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error); |
| 173 | |
| 174 | virtual lldb_private::Error |
| 175 | DoDeallocateMemory (lldb::addr_t ptr); |
| 176 | |
| 177 | //------------------------------------------------------------------ |
| 178 | // Process STDIO |
| 179 | //------------------------------------------------------------------ |
| 180 | virtual size_t |
| 181 | GetSTDOUT (char *buf, size_t buf_size, lldb_private::Error &error); |
| 182 | |
| 183 | virtual size_t |
| 184 | GetSTDERR (char *buf, size_t buf_size, lldb_private::Error &error); |
| 185 | |
| 186 | virtual size_t |
| 187 | PutSTDIN (const char *buf, size_t buf_size, lldb_private::Error &error); |
| 188 | |
| 189 | //---------------------------------------------------------------------- |
| 190 | // Process Breakpoints |
| 191 | //---------------------------------------------------------------------- |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | virtual lldb_private::Error |
| 193 | EnableBreakpoint (lldb_private::BreakpointSite *bp_site); |
| 194 | |
| 195 | virtual lldb_private::Error |
| 196 | DisableBreakpoint (lldb_private::BreakpointSite *bp_site); |
| 197 | |
| 198 | //---------------------------------------------------------------------- |
| 199 | // Process Watchpoints |
| 200 | //---------------------------------------------------------------------- |
| 201 | virtual lldb_private::Error |
| 202 | EnableWatchpoint (lldb_private::WatchpointLocation *wp_loc); |
| 203 | |
| 204 | virtual lldb_private::Error |
| 205 | DisableWatchpoint (lldb_private::WatchpointLocation *wp_loc); |
| 206 | |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 207 | virtual bool |
| 208 | StartNoticingNewThreads(); |
| 209 | |
| 210 | virtual bool |
| 211 | StopNoticingNewThreads(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 212 | |
| 213 | protected: |
| 214 | friend class ThreadGDBRemote; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 215 | friend class GDBRemoteCommunicationClient; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 216 | friend class GDBRemoteRegisterContext; |
| 217 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 218 | //---------------------------------------------------------------------- |
| 219 | // Accessors |
| 220 | //---------------------------------------------------------------------- |
| 221 | bool |
| 222 | IsRunning ( lldb::StateType state ) |
| 223 | { |
| 224 | return state == lldb::eStateRunning || IsStepping(state); |
| 225 | } |
| 226 | |
| 227 | bool |
| 228 | IsStepping ( lldb::StateType state) |
| 229 | { |
| 230 | return state == lldb::eStateStepping; |
| 231 | } |
| 232 | bool |
| 233 | CanResume ( lldb::StateType state) |
| 234 | { |
| 235 | return state == lldb::eStateStopped; |
| 236 | } |
| 237 | |
| 238 | bool |
| 239 | HasExited (lldb::StateType state) |
| 240 | { |
| 241 | return state == lldb::eStateExited; |
| 242 | } |
| 243 | |
| 244 | bool |
| 245 | ProcessIDIsValid ( ) const; |
| 246 | |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 247 | // static void |
| 248 | // STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 249 | |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 250 | // void |
| 251 | // AppendSTDOUT (const char* s, size_t len); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | void |
| 254 | Clear ( ); |
| 255 | |
| 256 | lldb_private::Flags & |
| 257 | GetFlags () |
| 258 | { |
| 259 | return m_flags; |
| 260 | } |
| 261 | |
| 262 | const lldb_private::Flags & |
| 263 | GetFlags () const |
| 264 | { |
| 265 | return m_flags; |
| 266 | } |
| 267 | |
| 268 | uint32_t |
| 269 | UpdateThreadListIfNeeded (); |
| 270 | |
| 271 | lldb_private::Error |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame^] | 272 | StartDebugserverProcess (const char *debugserver_url); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 273 | |
| 274 | void |
| 275 | KillDebugserverProcess (); |
| 276 | |
| 277 | void |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 278 | BuildDynamicRegisterInfo (bool force); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 279 | |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 280 | GDBRemoteCommunicationClient & |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 281 | GetGDBRemote() |
| 282 | { |
| 283 | return m_gdb_comm; |
| 284 | } |
| 285 | |
| 286 | //------------------------------------------------------------------ |
| 287 | /// Broadcaster event bits definitions. |
| 288 | //------------------------------------------------------------------ |
| 289 | enum |
| 290 | { |
| 291 | eBroadcastBitAsyncContinue = (1 << 0), |
| 292 | eBroadcastBitAsyncThreadShouldExit = (1 << 1) |
| 293 | }; |
| 294 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 295 | lldb_private::Flags m_flags; // Process specific flags (see eFlags enums) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 296 | lldb_private::Mutex m_stdio_mutex; // Multithreaded protection for stdio |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 297 | GDBRemoteCommunicationClient m_gdb_comm; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 298 | lldb::pid_t m_debugserver_pid; |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 299 | lldb::thread_t m_debugserver_thread; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 300 | StringExtractor m_last_stop_packet; |
| 301 | GDBRemoteDynamicRegisterInfo m_register_info; |
| 302 | lldb_private::Broadcaster m_async_broadcaster; |
| 303 | lldb::thread_t m_async_thread; |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 304 | typedef std::vector<lldb::tid_t> tid_collection; |
| 305 | typedef std::vector< std::pair<lldb::tid_t,int> > tid_sig_collection; |
| 306 | tid_collection m_continue_c_tids; // 'c' for continue |
| 307 | tid_sig_collection m_continue_C_tids; // 'C' for continue with signal |
| 308 | tid_collection m_continue_s_tids; // 's' for step |
| 309 | tid_sig_collection m_continue_S_tids; // 'S' for step with signal |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 310 | lldb::addr_t m_dispatch_queue_offsets_addr; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | 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] | 312 | bool m_waiting_for_attach; |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 313 | 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] | 314 | std::vector<lldb::user_id_t> m_thread_observation_bps; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 316 | bool |
| 317 | StartAsyncThread (); |
| 318 | |
| 319 | void |
| 320 | StopAsyncThread (); |
| 321 | |
| 322 | static void * |
| 323 | AsyncThread (void *arg); |
| 324 | |
| 325 | static bool |
| 326 | MonitorDebugserverProcess (void *callback_baton, |
| 327 | lldb::pid_t pid, |
| 328 | int signo, // Zero for no signal |
| 329 | int exit_status); // Exit value of process if signal is zero |
| 330 | |
| 331 | lldb::StateType |
| 332 | SetThreadStopInfo (StringExtractor& stop_packet); |
| 333 | |
| 334 | void |
| 335 | DidLaunchOrAttach (); |
| 336 | |
| 337 | lldb_private::Error |
| 338 | ConnectToDebugserver (const char *host_port); |
| 339 | |
| 340 | const char * |
| 341 | GetDispatchQueueNameForThread (lldb::addr_t thread_dispatch_qaddr, |
| 342 | std::string &dispatch_queue_name); |
| 343 | |
| 344 | static size_t |
| 345 | AttachInputReaderCallback (void *baton, |
| 346 | lldb_private::InputReader *reader, |
| 347 | lldb::InputReaderAction notification, |
| 348 | const char *bytes, |
| 349 | size_t bytes_len); |
| 350 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 351 | lldb_private::Error |
| 352 | InterruptIfRunning (bool discard_thread_plans, |
| 353 | bool catch_stop_event, |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 354 | lldb::EventSP &stop_event_sp); |
| 355 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 356 | private: |
| 357 | //------------------------------------------------------------------ |
| 358 | // For ProcessGDBRemote only |
| 359 | //------------------------------------------------------------------ |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 360 | static bool |
| 361 | NewThreadNotifyBreakpointHit (void *baton, |
| 362 | lldb_private::StoppointCallbackContext *context, |
| 363 | lldb::user_id_t break_id, |
| 364 | lldb::user_id_t break_loc_id); |
| 365 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 366 | DISALLOW_COPY_AND_ASSIGN (ProcessGDBRemote); |
| 367 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | #endif // liblldb_ProcessGDBRemote_h_ |