1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
//===-- Debug.h -------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_Debug_h_
#define liblldb_Debug_h_
#include "lldb/lldb-private.h"
#include "lldb/Core/Error.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Host/Mutex.h"
#include <vector>
namespace lldb_private {
//------------------------------------------------------------------
// Tells a thread what it needs to do when the process is resumed.
//------------------------------------------------------------------
struct ResumeAction
{
lldb::tid_t tid; // The thread ID that this action applies to, LLDB_INVALID_THREAD_ID for the default thread action
lldb::StateType state; // Valid values are eStateStopped/eStateSuspended, eStateRunning, and eStateStepping.
int signal; // When resuming this thread, resume it with this signal if this value is > 0
};
//------------------------------------------------------------------
// A class that contains instructions for all threads for
// NativeProcessProtocol::Resume(). Each thread can either run, stay
// suspended, or step when the process is resumed. We optionally
// have the ability to also send a signal to the thread when the
// action is run or step.
//------------------------------------------------------------------
class ResumeActionList
{
public:
ResumeActionList () :
m_actions (),
m_signal_handled ()
{
}
ResumeActionList (lldb::StateType default_action, int signal) :
m_actions(),
m_signal_handled ()
{
SetDefaultThreadActionIfNeeded (default_action, signal);
}
ResumeActionList (const ResumeAction *actions, size_t num_actions) :
m_actions (),
m_signal_handled ()
{
if (actions && num_actions)
{
m_actions.assign (actions, actions + num_actions);
m_signal_handled.assign (num_actions, false);
}
}
~ResumeActionList()
{
}
bool
IsEmpty() const
{
return m_actions.empty();
}
void
Append (const ResumeAction &action)
{
m_actions.push_back (action);
m_signal_handled.push_back (false);
}
void
AppendAction (lldb::tid_t tid,
lldb::StateType state,
int signal = 0)
{
ResumeAction action = { tid, state, signal };
Append (action);
}
void
AppendResumeAll ()
{
AppendAction (LLDB_INVALID_THREAD_ID, lldb::eStateRunning);
}
void
AppendSuspendAll ()
{
AppendAction (LLDB_INVALID_THREAD_ID, lldb::eStateStopped);
}
void
AppendStepAll ()
{
AppendAction (LLDB_INVALID_THREAD_ID, lldb::eStateStepping);
}
const ResumeAction *
GetActionForThread (lldb::tid_t tid, bool default_ok) const
{
const size_t num_actions = m_actions.size();
for (size_t i=0; i<num_actions; ++i)
{
if (m_actions[i].tid == tid)
return &m_actions[i];
}
if (default_ok && tid != LLDB_INVALID_THREAD_ID)
return GetActionForThread (LLDB_INVALID_THREAD_ID, false);
return NULL;
}
size_t
NumActionsWithState (lldb::StateType state) const
{
size_t count = 0;
const size_t num_actions = m_actions.size();
for (size_t i=0; i<num_actions; ++i)
{
if (m_actions[i].state == state)
++count;
}
return count;
}
bool
SetDefaultThreadActionIfNeeded (lldb::StateType action, int signal)
{
if (GetActionForThread (LLDB_INVALID_THREAD_ID, true) == NULL)
{
// There isn't a default action so we do need to set it.
ResumeAction default_action = {LLDB_INVALID_THREAD_ID, action, signal };
m_actions.push_back (default_action);
m_signal_handled.push_back (false);
return true; // Return true as we did add the default action
}
return false;
}
void
SetSignalHandledForThread (lldb::tid_t tid) const
{
if (tid != LLDB_INVALID_THREAD_ID)
{
const size_t num_actions = m_actions.size();
for (size_t i=0; i<num_actions; ++i)
{
if (m_actions[i].tid == tid)
m_signal_handled[i] = true;
}
}
}
const ResumeAction *
GetFirst() const
{
return m_actions.data();
}
size_t
GetSize () const
{
return m_actions.size();
}
void
Clear()
{
m_actions.clear();
m_signal_handled.clear();
}
protected:
std::vector<ResumeAction> m_actions;
mutable std::vector<bool> m_signal_handled;
};
struct ThreadStopInfo
{
lldb::StopReason reason;
union
{
// eStopTypeSignal
struct
{
uint32_t signo;
} signal;
// eStopTypeException
struct
{
uint64_t type;
uint32_t data_count;
lldb::addr_t data[2];
} exception;
} details;
};
//------------------------------------------------------------------
// NativeThreadProtocol
//------------------------------------------------------------------
class NativeThreadProtocol {
public:
NativeThreadProtocol (NativeProcessProtocol *process, lldb::tid_t tid) :
m_process (process),
m_tid (tid)
{
}
virtual ~NativeThreadProtocol()
{
}
virtual const char *GetName() = 0;
virtual lldb::StateType GetState () = 0;
virtual Error ReadRegister (uint32_t reg, RegisterValue ®_value) = 0;
virtual Error WriteRegister (uint32_t reg, const RegisterValue ®_value) = 0;
virtual Error SaveAllRegisters (lldb::DataBufferSP &data_sp) = 0;
virtual Error RestoreAllRegisters (lldb::DataBufferSP &data_sp) = 0;
virtual bool GetStopReason (ThreadStopInfo &stop_info) = 0;
lldb::tid_t
GetID() const
{
return m_tid;
}
protected:
NativeProcessProtocol *m_process;
lldb::tid_t m_tid;
};
//------------------------------------------------------------------
// NativeProcessProtocol
//------------------------------------------------------------------
class NativeProcessProtocol {
public:
static NativeProcessProtocol *
CreateInstance (lldb::pid_t pid);
// lldb_private::Host calls should be used to launch a process for debugging, and
// then the process should be attached to. When attaching to a process
// lldb_private::Host calls should be used to locate the process to attach to,
// and then this function should be called.
NativeProcessProtocol (lldb::pid_t pid) :
m_pid (pid),
m_threads(),
m_threads_mutex (Mutex::eMutexTypeRecursive),
m_state (lldb::eStateInvalid),
m_exit_status(0),
m_exit_description()
{
}
public:
virtual ~NativeProcessProtocol ()
{
}
virtual Error Resume (const ResumeActionList &resume_actions) = 0;
virtual Error Halt () = 0;
virtual Error Detach () = 0;
virtual Error Signal (int signo) = 0;
virtual Error Kill () = 0;
virtual Error ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) = 0;
virtual Error WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) = 0;
virtual Error AllocateMemory (lldb::addr_t size, uint32_t permissions, lldb::addr_t &addr) = 0;
virtual Error DeallocateMemory (lldb::addr_t addr) = 0;
virtual lldb::addr_t GetSharedLibraryInfoAddress () = 0;
virtual bool IsAlive () = 0;
virtual size_t UpdateThreads () = 0;
virtual bool GetArchitecture (ArchSpec &arch) = 0;
//----------------------------------------------------------------------
// Breakpoint functions
//----------------------------------------------------------------------
virtual Error SetBreakpoint (lldb::addr_t addr, size_t size, bool hardware) = 0;
virtual Error RemoveBreakpoint (lldb::addr_t addr, size_t size) = 0;
//----------------------------------------------------------------------
// Watchpoint functions
//----------------------------------------------------------------------
virtual uint32_t GetMaxWatchpoints () = 0;
virtual Error SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) = 0;
virtual Error RemoveWatchpoint (lldb::addr_t addr) = 0;
//----------------------------------------------------------------------
// Accessors
//----------------------------------------------------------------------
lldb::pid_t
GetID() const
{
return m_pid;
}
lldb::StateType
GetState () const
{
return m_state;
}
bool
IsRunning () const
{
return m_state == lldb::eStateRunning || IsStepping();
}
bool
IsStepping () const
{
return m_state == lldb::eStateStepping;
}
bool
CanResume () const
{
return m_state == lldb::eStateStopped;
}
void
SetState (lldb::StateType state)
{
m_state = state;
}
//----------------------------------------------------------------------
// Exit Status
//----------------------------------------------------------------------
virtual bool
GetExitStatus (int *status)
{
if (m_state == lldb::eStateExited)
{
*status = m_exit_status;
return true;
}
*status = 0;
return false;
}
virtual bool
SetExitStatus (int status, const char *exit_description)
{
// Exit status already set
if (m_state == lldb::eStateExited)
return false;
m_state = lldb::eStateExited;
m_exit_status = status;
if (exit_description && exit_description[0])
m_exit_description = exit_description;
else
m_exit_description.clear();
return true;
}
//----------------------------------------------------------------------
// Access to threads
//----------------------------------------------------------------------
lldb::NativeThreadProtocolSP
GetThreadAtIndex (uint32_t idx)
{
Mutex::Locker locker(m_threads_mutex);
if (idx < m_threads.size())
return m_threads[idx];
return lldb::NativeThreadProtocolSP();
}
lldb::NativeThreadProtocolSP
GetThreadByID (lldb::tid_t tid)
{
Mutex::Locker locker(m_threads_mutex);
for (auto thread_sp : m_threads)
{
if (thread_sp->GetID() == tid)
return thread_sp;
}
return lldb::NativeThreadProtocolSP();
}
protected:
lldb::pid_t m_pid;
std::vector<lldb::NativeThreadProtocolSP> m_threads;
mutable Mutex m_threads_mutex;
lldb::StateType m_state;
int m_exit_status;
std::string m_exit_description;
};
}
#endif // #ifndef liblldb_Debug_h_
|