1 //===-- Debug.h -------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Debug_h_
11 #define liblldb_Debug_h_
13 #include "lldb/lldb-private.h"
14 #include "lldb/Core/Error.h"
15 #include "lldb/Core/StreamString.h"
16 #include "lldb/Host/Mutex.h"
19 namespace lldb_private {
21 //------------------------------------------------------------------
22 // Tells a thread what it needs to do when the process is resumed.
23 //------------------------------------------------------------------
26 lldb::tid_t tid; // The thread ID that this action applies to, LLDB_INVALID_THREAD_ID for the default thread action
27 lldb::StateType state; // Valid values are eStateStopped/eStateSuspended, eStateRunning, and eStateStepping.
28 int signal; // When resuming this thread, resume it with this signal if this value is > 0
31 //------------------------------------------------------------------
32 // A class that contains instructions for all threads for
33 // NativeProcessProtocol::Resume(). Each thread can either run, stay
34 // suspended, or step when the process is resumed. We optionally
35 // have the ability to also send a signal to the thread when the
36 // action is run or step.
37 //------------------------------------------------------------------
38 class ResumeActionList
47 ResumeActionList (lldb::StateType default_action, int signal) :
51 SetDefaultThreadActionIfNeeded (default_action, signal);
55 ResumeActionList (const ResumeAction *actions, size_t num_actions) :
59 if (actions && num_actions)
61 m_actions.assign (actions, actions + num_actions);
62 m_signal_handled.assign (num_actions, false);
73 return m_actions.empty();
77 Append (const ResumeAction &action)
79 m_actions.push_back (action);
80 m_signal_handled.push_back (false);
84 AppendAction (lldb::tid_t tid,
85 lldb::StateType state,
88 ResumeAction action = { tid, state, signal };
95 AppendAction (LLDB_INVALID_THREAD_ID, lldb::eStateRunning);
101 AppendAction (LLDB_INVALID_THREAD_ID, lldb::eStateStopped);
107 AppendAction (LLDB_INVALID_THREAD_ID, lldb::eStateStepping);
111 GetActionForThread (lldb::tid_t tid, bool default_ok) const
113 const size_t num_actions = m_actions.size();
114 for (size_t i=0; i<num_actions; ++i)
116 if (m_actions[i].tid == tid)
117 return &m_actions[i];
119 if (default_ok && tid != LLDB_INVALID_THREAD_ID)
120 return GetActionForThread (LLDB_INVALID_THREAD_ID, false);
125 NumActionsWithState (lldb::StateType state) const
128 const size_t num_actions = m_actions.size();
129 for (size_t i=0; i<num_actions; ++i)
131 if (m_actions[i].state == state)
138 SetDefaultThreadActionIfNeeded (lldb::StateType action, int signal)
140 if (GetActionForThread (LLDB_INVALID_THREAD_ID, true) == NULL)
142 // There isn't a default action so we do need to set it.
143 ResumeAction default_action = {LLDB_INVALID_THREAD_ID, action, signal };
144 m_actions.push_back (default_action);
145 m_signal_handled.push_back (false);
146 return true; // Return true as we did add the default action
152 SetSignalHandledForThread (lldb::tid_t tid) const
154 if (tid != LLDB_INVALID_THREAD_ID)
156 const size_t num_actions = m_actions.size();
157 for (size_t i=0; i<num_actions; ++i)
159 if (m_actions[i].tid == tid)
160 m_signal_handled[i] = true;
168 return m_actions.data();
174 return m_actions.size();
181 m_signal_handled.clear();
185 std::vector<ResumeAction> m_actions;
186 mutable std::vector<bool> m_signal_handled;
189 struct ThreadStopInfo
191 lldb::StopReason reason;
200 // eStopTypeException
205 lldb::addr_t data[2];
210 //------------------------------------------------------------------
211 // NativeThreadProtocol
212 //------------------------------------------------------------------
213 class NativeThreadProtocol {
216 NativeThreadProtocol (NativeProcessProtocol *process, lldb::tid_t tid) :
222 virtual ~NativeThreadProtocol()
225 virtual const char *GetName() = 0;
226 virtual lldb::StateType GetState () = 0;
227 virtual Error ReadRegister (uint32_t reg, RegisterValue ®_value) = 0;
228 virtual Error WriteRegister (uint32_t reg, const RegisterValue ®_value) = 0;
229 virtual Error SaveAllRegisters (lldb::DataBufferSP &data_sp) = 0;
230 virtual Error RestoreAllRegisters (lldb::DataBufferSP &data_sp) = 0;
231 virtual bool GetStopReason (ThreadStopInfo &stop_info) = 0;
239 NativeProcessProtocol *m_process;
244 //------------------------------------------------------------------
245 // NativeProcessProtocol
246 //------------------------------------------------------------------
247 class NativeProcessProtocol {
250 static NativeProcessProtocol *
251 CreateInstance (lldb::pid_t pid);
253 // lldb_private::Host calls should be used to launch a process for debugging, and
254 // then the process should be attached to. When attaching to a process
255 // lldb_private::Host calls should be used to locate the process to attach to,
256 // and then this function should be called.
257 NativeProcessProtocol (lldb::pid_t pid) :
260 m_threads_mutex (Mutex::eMutexTypeRecursive),
261 m_state (lldb::eStateInvalid),
268 virtual ~NativeProcessProtocol ()
272 virtual Error Resume (const ResumeActionList &resume_actions) = 0;
273 virtual Error Halt () = 0;
274 virtual Error Detach () = 0;
275 virtual Error Signal (int signo) = 0;
276 virtual Error Kill () = 0;
278 virtual Error ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) = 0;
279 virtual Error WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) = 0;
280 virtual Error AllocateMemory (lldb::addr_t size, uint32_t permissions, lldb::addr_t &addr) = 0;
281 virtual Error DeallocateMemory (lldb::addr_t addr) = 0;
283 virtual lldb::addr_t GetSharedLibraryInfoAddress () = 0;
285 virtual bool IsAlive () = 0;
286 virtual size_t UpdateThreads () = 0;
287 virtual bool GetArchitecture (ArchSpec &arch) = 0;
289 //----------------------------------------------------------------------
290 // Breakpoint functions
291 //----------------------------------------------------------------------
292 virtual Error SetBreakpoint (lldb::addr_t addr, size_t size, bool hardware) = 0;
293 virtual Error RemoveBreakpoint (lldb::addr_t addr, size_t size) = 0;
295 //----------------------------------------------------------------------
296 // Watchpoint functions
297 //----------------------------------------------------------------------
298 virtual uint32_t GetMaxWatchpoints () = 0;
299 virtual Error SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) = 0;
300 virtual Error RemoveWatchpoint (lldb::addr_t addr) = 0;
303 //----------------------------------------------------------------------
305 //----------------------------------------------------------------------
321 return m_state == lldb::eStateRunning || IsStepping();
327 return m_state == lldb::eStateStepping;
333 return m_state == lldb::eStateStopped;
338 SetState (lldb::StateType state)
343 //----------------------------------------------------------------------
345 //----------------------------------------------------------------------
347 GetExitStatus (int *status)
349 if (m_state == lldb::eStateExited)
351 *status = m_exit_status;
358 SetExitStatus (int status, const char *exit_description)
360 // Exit status already set
361 if (m_state == lldb::eStateExited)
363 m_state = lldb::eStateExited;
364 m_exit_status = status;
365 if (exit_description && exit_description[0])
366 m_exit_description = exit_description;
368 m_exit_description.clear();
372 //----------------------------------------------------------------------
374 //----------------------------------------------------------------------
375 lldb::NativeThreadProtocolSP
376 GetThreadAtIndex (uint32_t idx)
378 Mutex::Locker locker(m_threads_mutex);
379 if (idx < m_threads.size())
380 return m_threads[idx];
381 return lldb::NativeThreadProtocolSP();
384 lldb::NativeThreadProtocolSP
385 GetThreadByID (lldb::tid_t tid)
387 Mutex::Locker locker(m_threads_mutex);
388 for (auto thread_sp : m_threads)
390 if (thread_sp->GetID() == tid)
393 return lldb::NativeThreadProtocolSP();
398 std::vector<lldb::NativeThreadProtocolSP> m_threads;
399 mutable Mutex m_threads_mutex;
400 lldb::StateType m_state;
402 std::string m_exit_description;
406 #endif // #ifndef liblldb_Debug_h_