blob: b7cf11cfb7d8815ab7f0a8703705538937894597 [file] [log] [blame]
Greg Claytonf4b47e12010-08-04 01:40:35 +00001//===-- StopInfo.cpp ---------------------------------------------*- 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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Claytonf4b47e12010-08-04 01:40:35 +000012#include "lldb/Target/StopInfo.h"
13
14// C Includes
15// C++ Includes
16#include <string>
17
18// Other libraries and framework includes
19// Project includes
20#include "lldb/Core/Log.h"
Greg Clayton4e78f602010-11-18 18:52:36 +000021#include "lldb/Breakpoint/Breakpoint.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000022#include "lldb/Breakpoint/BreakpointLocation.h"
23#include "lldb/Breakpoint/StoppointCallbackContext.h"
Johnny Chen01a67862011-10-14 00:42:25 +000024#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham4b536182011-08-09 02:12:22 +000025#include "lldb/Core/Debugger.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000026#include "lldb/Core/StreamString.h"
Jim Ingham4b536182011-08-09 02:12:22 +000027#include "lldb/Expression/ClangUserExpression.h"
28#include "lldb/Target/Target.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/ThreadPlan.h"
31#include "lldb/Target/Process.h"
32#include "lldb/Target/UnixSignals.h"
33
34using namespace lldb;
35using namespace lldb_private;
36
37StopInfo::StopInfo (Thread &thread, uint64_t value) :
Greg Clayton46c2b6e2013-04-29 23:30:46 +000038 m_thread_wp (thread.shared_from_this()),
Greg Clayton1ac04c32012-02-21 00:09:25 +000039 m_stop_id (thread.GetProcess()->GetStopID()),
40 m_resume_id (thread.GetProcess()->GetResumeID()),
Jim Ingham0161b492013-02-09 01:29:05 +000041 m_value (value),
Jim Ingham221d51c2013-05-08 00:35:16 +000042 m_override_should_notify (eLazyBoolCalculate),
Kuba Breckaafdf8422014-10-10 23:43:03 +000043 m_override_should_stop (eLazyBoolCalculate),
44 m_extended_info()
Greg Claytonf4b47e12010-08-04 01:40:35 +000045{
46}
47
48bool
49StopInfo::IsValid () const
50{
Greg Clayton46c2b6e2013-04-29 23:30:46 +000051 ThreadSP thread_sp (m_thread_wp.lock());
52 if (thread_sp)
53 return thread_sp->GetProcess()->GetStopID() == m_stop_id;
54 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +000055}
56
Jim Ingham77787032011-01-20 02:03:18 +000057void
58StopInfo::MakeStopInfoValid ()
59{
Greg Clayton46c2b6e2013-04-29 23:30:46 +000060 ThreadSP thread_sp (m_thread_wp.lock());
61 if (thread_sp)
62 {
63 m_stop_id = thread_sp->GetProcess()->GetStopID();
64 m_resume_id = thread_sp->GetProcess()->GetResumeID();
65 }
Jim Ingham77787032011-01-20 02:03:18 +000066}
67
Jim Ingham0faa43f2011-11-08 03:00:11 +000068bool
69StopInfo::HasTargetRunSinceMe ()
Jim Ingham4b536182011-08-09 02:12:22 +000070{
Greg Clayton46c2b6e2013-04-29 23:30:46 +000071 ThreadSP thread_sp (m_thread_wp.lock());
72
73 if (thread_sp)
Jim Ingham0faa43f2011-11-08 03:00:11 +000074 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +000075 lldb::StateType ret_type = thread_sp->GetProcess()->GetPrivateState();
76 if (ret_type == eStateRunning)
Jim Ingham0faa43f2011-11-08 03:00:11 +000077 {
78 return true;
79 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +000080 else if (ret_type == eStateStopped)
81 {
82 // This is a little tricky. We want to count "run and stopped again before you could
83 // ask this question as a "TRUE" answer to HasTargetRunSinceMe. But we don't want to
84 // include any running of the target done for expressions. So we track both resumes,
85 // and resumes caused by expressions, and check if there are any resumes NOT caused
86 // by expressions.
87
88 uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID();
89 uint32_t last_user_expression_id = thread_sp->GetProcess()->GetLastUserExpressionResumeID ();
90 if (curr_resume_id == m_resume_id)
91 {
92 return false;
93 }
94 else if (curr_resume_id > last_user_expression_id)
95 {
96 return true;
97 }
98 }
Jim Ingham0faa43f2011-11-08 03:00:11 +000099 }
100 return false;
Jim Ingham4b536182011-08-09 02:12:22 +0000101}
102
Greg Claytonf4b47e12010-08-04 01:40:35 +0000103//----------------------------------------------------------------------
104// StopInfoBreakpoint
105//----------------------------------------------------------------------
106
Jim Ingham4b536182011-08-09 02:12:22 +0000107namespace lldb_private
108{
Greg Claytonf4b47e12010-08-04 01:40:35 +0000109class StopInfoBreakpoint : public StopInfo
110{
111public:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000112 StopInfoBreakpoint (Thread &thread, break_id_t break_id) :
113 StopInfo (thread, break_id),
114 m_description(),
115 m_should_stop (false),
Jim Ingham0f16e732011-02-08 05:20:59 +0000116 m_should_stop_is_valid (false),
Jim Ingham52c7d202011-10-28 01:12:19 +0000117 m_should_perform_action (true),
Jim Inghamca36cd12012-10-05 19:16:31 +0000118 m_address (LLDB_INVALID_ADDRESS),
119 m_break_id(LLDB_INVALID_BREAK_ID),
120 m_was_one_shot (false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000121 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000122 StoreBPInfo();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000123 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000124
Jim Ingham36f3b362010-10-14 23:45:03 +0000125 StopInfoBreakpoint (Thread &thread, break_id_t break_id, bool should_stop) :
126 StopInfo (thread, break_id),
127 m_description(),
128 m_should_stop (should_stop),
Jim Ingham0f16e732011-02-08 05:20:59 +0000129 m_should_stop_is_valid (true),
Jim Ingham52c7d202011-10-28 01:12:19 +0000130 m_should_perform_action (true),
Jim Inghamca36cd12012-10-05 19:16:31 +0000131 m_address (LLDB_INVALID_ADDRESS),
132 m_break_id(LLDB_INVALID_BREAK_ID),
133 m_was_one_shot (false)
134 {
135 StoreBPInfo();
136 }
137
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000138 void
139 StoreBPInfo ()
Jim Ingham36f3b362010-10-14 23:45:03 +0000140 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000141 ThreadSP thread_sp (m_thread_wp.lock());
142 if (thread_sp)
Jim Ingham52c7d202011-10-28 01:12:19 +0000143 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000144 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
145 if (bp_site_sp)
Jim Inghamca36cd12012-10-05 19:16:31 +0000146 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000147 if (bp_site_sp->GetNumberOfOwners() == 1)
Jim Inghamca36cd12012-10-05 19:16:31 +0000148 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000149 BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(0);
150 if (bp_loc_sp)
151 {
152 m_break_id = bp_loc_sp->GetBreakpoint().GetID();
153 m_was_one_shot = bp_loc_sp->GetBreakpoint().IsOneShot();
154 }
Jim Inghamca36cd12012-10-05 19:16:31 +0000155 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000156 m_address = bp_site_sp->GetLoadAddress();
Jim Inghamca36cd12012-10-05 19:16:31 +0000157 }
Jim Ingham52c7d202011-10-28 01:12:19 +0000158 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000159 }
160
Greg Claytonf4b47e12010-08-04 01:40:35 +0000161 virtual ~StopInfoBreakpoint ()
162 {
163 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000164
Greg Claytonf4b47e12010-08-04 01:40:35 +0000165 virtual StopReason
166 GetStopReason () const
167 {
168 return eStopReasonBreakpoint;
169 }
170
171 virtual bool
Jim Ingham6d66ce62012-04-20 21:16:56 +0000172 ShouldStopSynchronous (Event *event_ptr)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000173 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000174 ThreadSP thread_sp (m_thread_wp.lock());
175 if (thread_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000176 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000177 if (!m_should_stop_is_valid)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000178 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000179 // Only check once if we should stop at a breakpoint
180 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
181 if (bp_site_sp)
182 {
183 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
184 StoppointCallbackContext context (event_ptr, exe_ctx, true);
185 m_should_stop = bp_site_sp->ShouldStop (&context);
186 }
187 else
188 {
189 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000190
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000191 if (log)
192 log->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000193
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000194 m_should_stop = true;
195 }
196 m_should_stop_is_valid = true;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000197 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000198 return m_should_stop;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000199 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000200 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000201 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000202
Jim Inghamf57b4352012-11-10 02:08:14 +0000203 virtual bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000204 DoShouldNotify (Event *event_ptr)
Jim Inghamf57b4352012-11-10 02:08:14 +0000205 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000206 ThreadSP thread_sp (m_thread_wp.lock());
207 if (thread_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000208 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000209 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
210 if (bp_site_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000211 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000212 bool all_internal = true;
213
214 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
Jim Inghamf57b4352012-11-10 02:08:14 +0000215 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000216 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
217 {
218 all_internal = false;
219 break;
220 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000221 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000222 return all_internal == false;
Jim Inghamf57b4352012-11-10 02:08:14 +0000223 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000224 }
225 return true;
226 }
227
228 virtual const char *
229 GetDescription ()
230 {
231 if (m_description.empty())
232 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000233 ThreadSP thread_sp (m_thread_wp.lock());
234 if (thread_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000235 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000236 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
237 if (bp_site_sp)
Jim Ingham29950772013-01-26 02:19:28 +0000238 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000239 StreamString strm;
240 // If we have just hit an internal breakpoint, and it has a kind description, print that instead of the
241 // full breakpoint printing:
242 if (bp_site_sp->IsInternal())
Jim Ingham29950772013-01-26 02:19:28 +0000243 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000244 size_t num_owners = bp_site_sp->GetNumberOfOwners();
245 for (size_t idx = 0; idx < num_owners; idx++)
Jim Ingham29950772013-01-26 02:19:28 +0000246 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000247 const char *kind = bp_site_sp->GetOwnerAtIndex(idx)->GetBreakpoint().GetBreakpointKind();
248 if (kind != NULL)
249 {
250 m_description.assign (kind);
251 return kind;
252 }
Jim Ingham29950772013-01-26 02:19:28 +0000253 }
254 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000255
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000256 strm.Printf("breakpoint ");
257 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
258 m_description.swap (strm.GetString());
Jim Ingham29950772013-01-26 02:19:28 +0000259 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000260 else
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000261 {
262 StreamString strm;
263 if (m_break_id != LLDB_INVALID_BREAK_ID)
264 {
265 BreakpointSP break_sp = thread_sp->GetProcess()->GetTarget().GetBreakpointByID(m_break_id);
266 if (break_sp)
267 {
268 if (break_sp->IsInternal())
269 {
270 const char *kind = break_sp->GetBreakpointKind();
271 if (kind)
272 strm.Printf ("internal %s breakpoint(%d).", kind, m_break_id);
273 else
274 strm.Printf ("internal breakpoint(%d).", m_break_id);
275 }
276 else
277 {
278 strm.Printf ("breakpoint %d.", m_break_id);
279 }
280 }
281 else
282 {
283 if (m_was_one_shot)
284 strm.Printf ("one-shot breakpoint %d", m_break_id);
285 else
286 strm.Printf ("breakpoint %d which has been deleted.", m_break_id);
287 }
288 }
289 else if (m_address == LLDB_INVALID_ADDRESS)
290 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - unknown address", m_value);
291 else
292 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - was at 0x%" PRIx64, m_value, m_address);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000293
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000294 m_description.swap (strm.GetString());
295 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000296 }
297 }
298 return m_description.c_str();
299 }
300
301protected:
Jim Ingham6d66ce62012-04-20 21:16:56 +0000302 bool
303 ShouldStop (Event *event_ptr)
304 {
305 // This just reports the work done by PerformAction or the synchronous stop. It should
306 // only ever get called after they have had a chance to run.
307 assert (m_should_stop_is_valid);
308 return m_should_stop;
309 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000310
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000311 virtual void
312 PerformAction (Event *event_ptr)
313 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000314 if (!m_should_perform_action)
315 return;
316 m_should_perform_action = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000317
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000318 ThreadSP thread_sp (m_thread_wp.lock());
319
320 if (thread_sp)
Jim Ingham5cb9a182013-03-28 00:13:30 +0000321 {
Jim Inghamcca89952014-08-05 01:58:14 +0000322 Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS | LIBLLDB_LOG_STEP);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000323
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000324 if (!thread_sp->IsValid())
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000325 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000326 // This shouldn't ever happen, but just in case, don't do more harm.
Jason Molenda3f032ff2013-08-06 23:08:59 +0000327 if (log)
328 {
329 log->Printf ("PerformAction got called with an invalid thread.");
330 }
Jim Ingham79ea1d82011-12-09 04:17:31 +0000331 m_should_stop = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000332 m_should_stop_is_valid = true;
333 return;
Jim Ingham79ea1d82011-12-09 04:17:31 +0000334 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000335
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000336 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000337
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000338 if (bp_site_sp)
Jim Ingham79ea1d82011-12-09 04:17:31 +0000339 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000340 size_t num_owners = bp_site_sp->GetNumberOfOwners();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000341
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000342 if (num_owners == 0)
343 {
344 m_should_stop = true;
Jim Ingham184e9812013-01-15 02:47:48 +0000345 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000346 else
Jim Ingham4b536182011-08-09 02:12:22 +0000347 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000348 // We go through each location, and test first its condition. If the condition says to stop,
349 // then we run the callback for that location. If that callback says to stop as well, then
350 // we set m_should_stop to true; we are going to stop.
351 // But we still want to give all the breakpoints whose conditions say we are going to stop a
352 // chance to run their callbacks.
353 // Of course if any callback restarts the target by putting "continue" in the callback, then
354 // we're going to restart, without running the rest of the callbacks. And in this case we will
355 // end up not stopping even if another location said we should stop. But that's better than not
356 // running all the callbacks.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000357
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000358 m_should_stop = false;
359
360 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
361 Process *process = exe_ctx.GetProcessPtr();
362 if (process->GetModIDRef().IsLastResumeForUserExpression())
Jim Ingham4b536182011-08-09 02:12:22 +0000363 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000364 // If we are in the middle of evaluating an expression, don't run asynchronous breakpoint commands or
365 // expressions. That could lead to infinite recursion if the command or condition re-calls the function
366 // with this breakpoint.
367 // TODO: We can keep a list of the breakpoints we've seen while running expressions in the nested
368 // PerformAction calls that can arise when the action runs a function that hits another breakpoint,
369 // and only stop running commands when we see the same breakpoint hit a second time.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000370
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000371 m_should_stop_is_valid = true;
372 if (log)
373 log->Printf ("StopInfoBreakpoint::PerformAction - Hit a breakpoint while running an expression,"
374 " not running commands to avoid recursion.");
375 bool ignoring_breakpoints = process->GetIgnoreBreakpointsInExpressions();
376 if (ignoring_breakpoints)
Jim Ingham4b536182011-08-09 02:12:22 +0000377 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000378 m_should_stop = false;
379 // Internal breakpoints will always stop.
380 for (size_t j = 0; j < num_owners; j++)
381 {
382 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
383 if (bp_loc_sp->GetBreakpoint().IsInternal())
384 {
385 m_should_stop = true;
386 break;
387 }
388 }
Jim Ingham4b536182011-08-09 02:12:22 +0000389 }
Sean Callanan3dbf3462013-04-19 07:09:15 +0000390 else
391 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000392 m_should_stop = true;
393 }
394 if (log)
395 log->Printf ("StopInfoBreakpoint::PerformAction - in expression, continuing: %s.",
396 m_should_stop ? "true" : "false");
397 process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: hit breakpoint while "
398 "running function, skipping commands and conditions to prevent recursion.");
399 return;
400 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000401
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000402 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000403
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000404 // Let's copy the breakpoint locations out of the site and store them in a local list. That way if
405 // one of the breakpoint actions changes the site, then we won't be operating on a bad list.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000406
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000407 BreakpointLocationCollection site_locations;
408 for (size_t j = 0; j < num_owners; j++)
409 site_locations.Add(bp_site_sp->GetOwnerAtIndex(j));
410
411 for (size_t j = 0; j < num_owners; j++)
412 {
413 lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000414
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000415 // If another action disabled this breakpoint or its location, then don't run the actions.
416 if (!bp_loc_sp->IsEnabled() || !bp_loc_sp->GetBreakpoint().IsEnabled())
417 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000418
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000419 // The breakpoint site may have many locations associated with it, not all of them valid for
420 // this thread. Skip the ones that aren't:
421 if (!bp_loc_sp->ValidForThisThread(thread_sp.get()))
Jim Inghama8b99ca2014-01-15 03:30:04 +0000422 {
423 if (log)
424 {
425 StreamString s;
426 bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000427 log->Printf ("Breakpoint %s hit on thread 0x%llx but it was not for this thread, continuing.",
428 s.GetData(),
429 static_cast<unsigned long long>(thread_sp->GetID()));
Jim Inghama8b99ca2014-01-15 03:30:04 +0000430 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000431 continue;
Jim Inghama8b99ca2014-01-15 03:30:04 +0000432 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000433 // First run the condition for the breakpoint. If that says we should stop, then we'll run
434 // the callback for the breakpoint. If the callback says we shouldn't stop that will win.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000435
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000436 if (bp_loc_sp->GetConditionText() != NULL)
437 {
438 Error condition_error;
439 bool condition_says_stop = bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000440
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000441 if (!condition_error.Success())
442 {
443 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
444 StreamSP error_sp = debugger.GetAsyncErrorStream ();
445 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
446 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
447 error_sp->Printf (": \"%s\"",
448 bp_loc_sp->GetConditionText());
449 error_sp->EOL();
450 const char *err_str = condition_error.AsCString("<Unknown Error>");
451 if (log)
452 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000453
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000454 error_sp->PutCString (err_str);
455 error_sp->EOL();
456 error_sp->Flush();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000457 }
458 else
459 {
Jim Inghama8b99ca2014-01-15 03:30:04 +0000460 if (log)
461 {
462 StreamString s;
463 bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000464 log->Printf ("Condition evaluated for breakpoint %s on thread 0x%llx conditon_says_stop: %i.",
465 s.GetData(),
466 static_cast<unsigned long long>(thread_sp->GetID()),
467 condition_says_stop);
Jim Inghama8b99ca2014-01-15 03:30:04 +0000468 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000469 if (!condition_says_stop)
470 continue;
471 }
472 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000473
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000474 bool callback_says_stop;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000475
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000476 // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
477 // to get out of there. So set it here.
478 // When we figure out how to nest breakpoint hits then this will change.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000479
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000480 Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
481 bool old_async = debugger.GetAsyncExecution();
482 debugger.SetAsyncExecution (true);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000483
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000484 callback_says_stop = bp_loc_sp->InvokeCallback (&context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000485
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000486 debugger.SetAsyncExecution (old_async);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000487
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000488 if (callback_says_stop)
489 m_should_stop = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000490
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000491 // If we are going to stop for this breakpoint, then remove the breakpoint.
492 if (callback_says_stop && bp_loc_sp && bp_loc_sp->GetBreakpoint().IsOneShot())
493 {
494 thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID (bp_loc_sp->GetBreakpoint().GetID());
495 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000496
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000497 // Also make sure that the callback hasn't continued the target.
498 // If it did, when we'll set m_should_start to false and get out of here.
499 if (HasTargetRunSinceMe ())
500 {
501 m_should_stop = false;
502 break;
Sean Callanan3dbf3462013-04-19 07:09:15 +0000503 }
Jim Ingham4b536182011-08-09 02:12:22 +0000504 }
Jim Ingham4b536182011-08-09 02:12:22 +0000505 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000506 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
507 m_should_stop_is_valid = true;
508
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000509 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000510 else
511 {
512 m_should_stop = true;
513 m_should_stop_is_valid = true;
514 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham79ea1d82011-12-09 04:17:31 +0000515
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000516 if (log_process)
517 log_process->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
518 }
519 if (log)
520 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000521 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000522 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000523
524private:
525 std::string m_description;
526 bool m_should_stop;
527 bool m_should_stop_is_valid;
Jim Ingham0f16e732011-02-08 05:20:59 +0000528 bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
529 // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
Jim Ingham52c7d202011-10-28 01:12:19 +0000530 lldb::addr_t m_address; // We use this to capture the breakpoint site address when we create the StopInfo,
531 // in case somebody deletes it between the time the StopInfo is made and the
532 // description is asked for.
Jim Inghamca36cd12012-10-05 19:16:31 +0000533 lldb::break_id_t m_break_id;
534 bool m_was_one_shot;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000535};
536
537
538//----------------------------------------------------------------------
539// StopInfoWatchpoint
540//----------------------------------------------------------------------
541
542class StopInfoWatchpoint : public StopInfo
543{
544public:
Jim Inghamf57b4352012-11-10 02:08:14 +0000545 // Make sure watchpoint is properly disabled and subsequently enabled while performing watchpoint actions.
546 class WatchpointSentry {
547 public:
548 WatchpointSentry(Process *p, Watchpoint *w):
549 process(p),
550 watchpoint(w)
551 {
552 if (process && watchpoint)
553 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000554 const bool notify = false;
Jim Inghamf57b4352012-11-10 02:08:14 +0000555 watchpoint->TurnOnEphemeralMode();
Jim Ingham1b5792e2012-12-18 02:03:49 +0000556 process->DisableWatchpoint(watchpoint, notify);
Jim Inghamf57b4352012-11-10 02:08:14 +0000557 }
558 }
559 ~WatchpointSentry()
560 {
561 if (process && watchpoint)
562 {
563 if (!watchpoint->IsDisabledDuringEphemeralMode())
Jim Ingham1b5792e2012-12-18 02:03:49 +0000564 {
565 const bool notify = false;
566 process->EnableWatchpoint(watchpoint, notify);
567 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000568 watchpoint->TurnOffEphemeralMode();
569 }
570 }
571 private:
572 Process *process;
573 Watchpoint *watchpoint;
574 };
Greg Claytonf4b47e12010-08-04 01:40:35 +0000575
576 StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
Johnny Chenfd158f42011-09-21 22:47:15 +0000577 StopInfo(thread, watch_id),
578 m_description(),
579 m_should_stop(false),
580 m_should_stop_is_valid(false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000581 {
582 }
583
584 virtual ~StopInfoWatchpoint ()
585 {
586 }
587
588 virtual StopReason
589 GetStopReason () const
590 {
591 return eStopReasonWatchpoint;
592 }
593
Jim Inghamf57b4352012-11-10 02:08:14 +0000594 virtual const char *
595 GetDescription ()
596 {
597 if (m_description.empty())
598 {
599 StreamString strm;
Daniel Malead01b2952012-11-29 21:49:15 +0000600 strm.Printf("watchpoint %" PRIi64, m_value);
Jim Inghamf57b4352012-11-10 02:08:14 +0000601 m_description.swap (strm.GetString());
602 }
603 return m_description.c_str();
604 }
605
606protected:
Johnny Chenfd158f42011-09-21 22:47:15 +0000607 virtual bool
Jim Ingham9b620f32013-02-22 21:23:43 +0000608 ShouldStopSynchronous (Event *event_ptr)
Johnny Chenfd158f42011-09-21 22:47:15 +0000609 {
610 // ShouldStop() method is idempotent and should not affect hit count.
Johnny Chen16dcf712011-10-17 18:58:00 +0000611 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
612 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
613 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
614 // StopInfoWatchpoint::ShouldStop() and
615 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
616 // StopInfoWatchpoint::PerformAction().
Johnny Chenfd158f42011-09-21 22:47:15 +0000617 if (m_should_stop_is_valid)
618 return m_should_stop;
619
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000620 ThreadSP thread_sp (m_thread_wp.lock());
621 if (thread_sp)
Johnny Chenfd158f42011-09-21 22:47:15 +0000622 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000623 WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
624 if (wp_sp)
625 {
626 // Check if we should stop at a watchpoint.
627 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
628 StoppointCallbackContext context (event_ptr, exe_ctx, true);
629 m_should_stop = wp_sp->ShouldStop (&context);
630 }
631 else
632 {
633 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chenfd158f42011-09-21 22:47:15 +0000634
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000635 if (log)
636 log->Printf ("Process::%s could not find watchpoint location id: %" PRId64 "...",
637 __FUNCTION__, GetValue());
Johnny Chenfd158f42011-09-21 22:47:15 +0000638
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000639 m_should_stop = true;
640 }
Johnny Chenfd158f42011-09-21 22:47:15 +0000641 }
642 m_should_stop_is_valid = true;
643 return m_should_stop;
644 }
645
Jim Ingham9b620f32013-02-22 21:23:43 +0000646 bool
647 ShouldStop (Event *event_ptr)
648 {
649 // This just reports the work done by PerformAction or the synchronous stop. It should
650 // only ever get called after they have had a chance to run.
651 assert (m_should_stop_is_valid);
652 return m_should_stop;
653 }
654
Johnny Chen16dcf712011-10-17 18:58:00 +0000655 virtual void
656 PerformAction (Event *event_ptr)
657 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000658 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
Johnny Chen16dcf712011-10-17 18:58:00 +0000659 // We're going to calculate if we should stop or not in some way during the course of
660 // this code. Also by default we're going to stop, so set that here.
661 m_should_stop = true;
662
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000663 ThreadSP thread_sp (m_thread_wp.lock());
664 if (thread_sp)
Johnny Chen16dcf712011-10-17 18:58:00 +0000665 {
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000666
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000667 WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
668 if (wp_sp)
Enrico Granataf04a2192012-07-13 23:18:48 +0000669 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000670 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
671 Process* process = exe_ctx.GetProcessPtr();
672
673 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
674 // and it is then enabled after we are finished.
675 WatchpointSentry sentry(process, wp_sp.get());
676
Enrico Granataf04a2192012-07-13 23:18:48 +0000677 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000678 // check if this process is running on an architecture where watchpoints trigger
679 // before the associated instruction runs. if so, disable the WP, single-step and then
680 // re-enable the watchpoint
681 if (process)
Enrico Granataf04a2192012-07-13 23:18:48 +0000682 {
Jason Molendafe806902013-05-04 00:39:52 +0000683 uint32_t num;
684 bool wp_triggers_after;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000685 if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
Enrico Granataf04a2192012-07-13 23:18:48 +0000686 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000687 if (!wp_triggers_after)
688 {
Jim Ingham363ddd72013-07-02 21:12:44 +0000689 StopInfoSP stored_stop_info_sp = thread_sp->GetStopInfo();
690 assert (stored_stop_info_sp.get() == this);
691
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000692 ThreadPlanSP new_plan_sp(thread_sp->QueueThreadPlanForStepSingleInstruction(false, // step-over
693 false, // abort_other_plans
694 true)); // stop_other_threads
695 new_plan_sp->SetIsMasterPlan (true);
696 new_plan_sp->SetOkayToDiscard (false);
697 new_plan_sp->SetPrivate (true);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000698 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
699 process->Resume ();
700 process->WaitForProcessToStop (NULL);
701 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
Jim Ingham363ddd72013-07-02 21:12:44 +0000702 thread_sp->SetStopInfo(stored_stop_info_sp);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000703 }
Enrico Granataf04a2192012-07-13 23:18:48 +0000704 }
705 }
706 }
Johnny Chen209bd652012-08-13 21:09:54 +0000707
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000708 if (m_should_stop && wp_sp->GetConditionText() != NULL)
Johnny Chen16dcf712011-10-17 18:58:00 +0000709 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000710 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
711 // constructor errors up to the debugger's Async I/O.
Jim Ingham1624a2d2014-05-05 02:26:40 +0000712 ExpressionResults result_code;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000713 EvaluateExpressionOptions expr_options;
714 expr_options.SetUnwindOnError(true);
715 expr_options.SetIgnoreBreakpoints(true);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000716 ValueObjectSP result_value_sp;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000717 Error error;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000718 result_code = ClangUserExpression::Evaluate (exe_ctx,
719 expr_options,
720 wp_sp->GetConditionText(),
721 NULL,
722 result_value_sp,
723 error);
Jim Ingham8646d3c2014-05-05 02:47:44 +0000724 if (result_code == eExpressionCompleted)
Johnny Chen16dcf712011-10-17 18:58:00 +0000725 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000726 if (result_value_sp)
Johnny Chen16dcf712011-10-17 18:58:00 +0000727 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000728 Scalar scalar_value;
729 if (result_value_sp->ResolveValue (scalar_value))
Johnny Chen16dcf712011-10-17 18:58:00 +0000730 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000731 if (scalar_value.ULongLong(1) == 0)
732 {
733 // We have been vetoed. This takes precedence over querying
734 // the watchpoint whether it should stop (aka ignore count and
735 // friends). See also StopInfoWatchpoint::ShouldStop() as well
736 // as Process::ProcessEventData::DoOnRemoval().
737 m_should_stop = false;
738 }
739 else
740 m_should_stop = true;
741 if (log)
742 log->Printf("Condition successfully evaluated, result is %s.\n",
743 m_should_stop ? "true" : "false");
Johnny Chen16dcf712011-10-17 18:58:00 +0000744 }
745 else
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000746 {
Johnny Chen16dcf712011-10-17 18:58:00 +0000747 m_should_stop = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000748 if (log)
749 log->Printf("Failed to get an integer result from the expression.");
750 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000751 }
752 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000753 else
754 {
755 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
756 StreamSP error_sp = debugger.GetAsyncErrorStream ();
757 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
758 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
759 error_sp->Printf (": \"%s\"",
760 wp_sp->GetConditionText());
761 error_sp->EOL();
762 const char *err_str = error.AsCString("<Unknown Error>");
763 if (log)
764 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
765
766 error_sp->PutCString (err_str);
767 error_sp->EOL();
768 error_sp->Flush();
769 // If the condition fails to be parsed or run, we should stop.
770 m_should_stop = true;
771 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000772 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000773
774 // If the condition says to stop, we run the callback to further decide whether to stop.
775 if (m_should_stop)
Johnny Chen16dcf712011-10-17 18:58:00 +0000776 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000777 StoppointCallbackContext context (event_ptr, exe_ctx, false);
778 bool stop_requested = wp_sp->InvokeCallback (&context);
779 // Also make sure that the callback hasn't continued the target.
780 // If it did, when we'll set m_should_stop to false and get out of here.
781 if (HasTargetRunSinceMe ())
782 m_should_stop = false;
783
784 if (m_should_stop && !stop_requested)
785 {
786 // We have been vetoed by the callback mechanism.
787 m_should_stop = false;
788 }
789 }
790 // Finally, if we are going to stop, print out the new & old values:
791 if (m_should_stop)
792 {
793 wp_sp->CaptureWatchedValue(exe_ctx);
794
Greg Clayton1ac04c32012-02-21 00:09:25 +0000795 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000796 StreamSP output_sp = debugger.GetAsyncOutputStream ();
797 wp_sp->DumpSnapshots(output_sp.get());
798 output_sp->EOL();
799 output_sp->Flush();
Johnny Chen16dcf712011-10-17 18:58:00 +0000800 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000801
Johnny Chen16dcf712011-10-17 18:58:00 +0000802 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000803 else
804 {
805 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chen13206412012-08-09 23:10:57 +0000806
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000807 if (log_process)
808 log_process->Printf ("Process::%s could not find watchpoint id: %" PRId64 "...", __FUNCTION__, m_value);
Johnny Chen13206412012-08-09 23:10:57 +0000809 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000810 if (log)
811 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Inghama7dfb662012-10-23 07:20:06 +0000812
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000813 m_should_stop_is_valid = true;
Johnny Chen16dcf712011-10-17 18:58:00 +0000814 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000815 }
816
Greg Claytonf4b47e12010-08-04 01:40:35 +0000817private:
818 std::string m_description;
Johnny Chenfd158f42011-09-21 22:47:15 +0000819 bool m_should_stop;
820 bool m_should_stop_is_valid;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000821};
822
823
824
825//----------------------------------------------------------------------
826// StopInfoUnixSignal
827//----------------------------------------------------------------------
828
829class StopInfoUnixSignal : public StopInfo
830{
831public:
832
833 StopInfoUnixSignal (Thread &thread, int signo) :
Greg Claytona658fd22011-06-04 01:26:29 +0000834 StopInfo (thread, signo)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000835 {
836 }
837
838 virtual ~StopInfoUnixSignal ()
839 {
840 }
841
842
843 virtual StopReason
844 GetStopReason () const
845 {
846 return eStopReasonSignal;
847 }
848
849 virtual bool
Jim Ingham0161b492013-02-09 01:29:05 +0000850 ShouldStopSynchronous (Event *event_ptr)
851 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000852 ThreadSP thread_sp (m_thread_wp.lock());
853 if (thread_sp)
854 return thread_sp->GetProcess()->GetUnixSignals().GetShouldStop (m_value);
855 return false;
Jim Ingham0161b492013-02-09 01:29:05 +0000856 }
857
858 virtual bool
Greg Claytonf4b47e12010-08-04 01:40:35 +0000859 ShouldStop (Event *event_ptr)
860 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000861 ThreadSP thread_sp (m_thread_wp.lock());
862 if (thread_sp)
863 return thread_sp->GetProcess()->GetUnixSignals().GetShouldStop (m_value);
864 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000865 }
866
867
868 // If should stop returns false, check if we should notify of this event
869 virtual bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000870 DoShouldNotify (Event *event_ptr)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000871 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000872 ThreadSP thread_sp (m_thread_wp.lock());
873 if (thread_sp)
Jim Ingham0161b492013-02-09 01:29:05 +0000874 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000875 bool should_notify = thread_sp->GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
876 if (should_notify)
877 {
878 StreamString strm;
879 strm.Printf ("thread %d received signal: %s",
880 thread_sp->GetIndexID(),
881 thread_sp->GetProcess()->GetUnixSignals().GetSignalAsCString (m_value));
882 Process::ProcessEventData::AddRestartedReason(event_ptr, strm.GetData());
883 }
884 return should_notify;
Jim Ingham0161b492013-02-09 01:29:05 +0000885 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000886 return true;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000887 }
888
889
890 virtual void
891 WillResume (lldb::StateType resume_state)
892 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000893 ThreadSP thread_sp (m_thread_wp.lock());
894 if (thread_sp)
895 {
896 if (thread_sp->GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
897 thread_sp->SetResumeSignal(m_value);
898 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000899 }
900
901 virtual const char *
902 GetDescription ()
903 {
904 if (m_description.empty())
905 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000906 ThreadSP thread_sp (m_thread_wp.lock());
907 if (thread_sp)
908 {
909 StreamString strm;
910 const char *signal_name = thread_sp->GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
911 if (signal_name)
912 strm.Printf("signal %s", signal_name);
913 else
914 strm.Printf("signal %" PRIi64, m_value);
915 m_description.swap (strm.GetString());
916 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000917 }
918 return m_description.c_str();
919 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000920};
921
922//----------------------------------------------------------------------
923// StopInfoTrace
924//----------------------------------------------------------------------
925
926class StopInfoTrace : public StopInfo
927{
928public:
929
930 StopInfoTrace (Thread &thread) :
931 StopInfo (thread, LLDB_INVALID_UID)
932 {
933 }
934
935 virtual ~StopInfoTrace ()
936 {
937 }
938
939 virtual StopReason
940 GetStopReason () const
941 {
942 return eStopReasonTrace;
943 }
944
945 virtual const char *
946 GetDescription ()
947 {
Greg Claytona658fd22011-06-04 01:26:29 +0000948 if (m_description.empty())
Greg Claytonf4b47e12010-08-04 01:40:35 +0000949 return "trace";
Greg Claytona658fd22011-06-04 01:26:29 +0000950 else
951 return m_description.c_str();
952 }
953};
954
955
956//----------------------------------------------------------------------
957// StopInfoException
958//----------------------------------------------------------------------
959
960class StopInfoException : public StopInfo
961{
962public:
963
964 StopInfoException (Thread &thread, const char *description) :
965 StopInfo (thread, LLDB_INVALID_UID)
966 {
967 if (description)
968 SetDescription (description);
969 }
970
971 virtual
972 ~StopInfoException ()
973 {
974 }
975
976 virtual StopReason
977 GetStopReason () const
978 {
979 return eStopReasonException;
980 }
981
982 virtual const char *
983 GetDescription ()
984 {
985 if (m_description.empty())
986 return "exception";
987 else
988 return m_description.c_str();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000989 }
990};
991
992
993//----------------------------------------------------------------------
994// StopInfoThreadPlan
995//----------------------------------------------------------------------
996
997class StopInfoThreadPlan : public StopInfo
998{
999public:
1000
Jim Ingham30fadaf2014-07-08 01:07:32 +00001001 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp, ClangExpressionVariableSP &expression_variable_sp) :
Greg Claytonf4b47e12010-08-04 01:40:35 +00001002 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham73ca05a2011-12-17 01:35:57 +00001003 m_plan_sp (plan_sp),
Jim Ingham30fadaf2014-07-08 01:07:32 +00001004 m_return_valobj_sp (return_valobj_sp),
1005 m_expression_variable_sp (expression_variable_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001006 {
1007 }
1008
1009 virtual ~StopInfoThreadPlan ()
1010 {
1011 }
1012
1013 virtual StopReason
1014 GetStopReason () const
1015 {
1016 return eStopReasonPlanComplete;
1017 }
1018
1019 virtual const char *
1020 GetDescription ()
1021 {
1022 if (m_description.empty())
1023 {
1024 StreamString strm;
1025 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
1026 m_description.swap (strm.GetString());
1027 }
1028 return m_description.c_str();
1029 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001030
1031 ValueObjectSP
1032 GetReturnValueObject()
1033 {
1034 return m_return_valobj_sp;
1035 }
Jim Ingham0161b492013-02-09 01:29:05 +00001036
Jim Ingham30fadaf2014-07-08 01:07:32 +00001037 ClangExpressionVariableSP
1038 GetExpressionVariable()
1039 {
1040 return m_expression_variable_sp;
1041 }
1042
Jim Ingham0161b492013-02-09 01:29:05 +00001043protected:
1044 virtual bool
1045 ShouldStop (Event *event_ptr)
1046 {
1047 if (m_plan_sp)
1048 return m_plan_sp->ShouldStop(event_ptr);
1049 else
1050 return StopInfo::ShouldStop(event_ptr);
1051 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001052
1053private:
1054 ThreadPlanSP m_plan_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001055 ValueObjectSP m_return_valobj_sp;
Jim Ingham30fadaf2014-07-08 01:07:32 +00001056 ClangExpressionVariableSP m_expression_variable_sp;
Greg Claytonf4b47e12010-08-04 01:40:35 +00001057};
Greg Clayton90ba8112012-12-05 00:16:59 +00001058
1059class StopInfoExec : public StopInfo
1060{
1061public:
1062
1063 StopInfoExec (Thread &thread) :
1064 StopInfo (thread, LLDB_INVALID_UID),
1065 m_performed_action (false)
1066 {
1067 }
1068
1069 virtual
1070 ~StopInfoExec ()
1071 {
1072 }
1073
1074 virtual StopReason
1075 GetStopReason () const
1076 {
1077 return eStopReasonExec;
1078 }
1079
1080 virtual const char *
1081 GetDescription ()
1082 {
1083 return "exec";
1084 }
1085protected:
Greg Clayton90ba8112012-12-05 00:16:59 +00001086
1087 virtual void
1088 PerformAction (Event *event_ptr)
1089 {
1090 // Only perform the action once
1091 if (m_performed_action)
1092 return;
1093 m_performed_action = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +00001094 ThreadSP thread_sp (m_thread_wp.lock());
1095 if (thread_sp)
1096 thread_sp->GetProcess()->DidExec();
Greg Clayton90ba8112012-12-05 00:16:59 +00001097 }
1098
1099 bool m_performed_action;
1100};
1101
Jim Ingham4b536182011-08-09 02:12:22 +00001102} // namespace lldb_private
Greg Claytonf4b47e12010-08-04 01:40:35 +00001103
1104StopInfoSP
1105StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
1106{
1107 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
1108}
1109
1110StopInfoSP
Jim Ingham36f3b362010-10-14 23:45:03 +00001111StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
1112{
1113 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
1114}
1115
1116StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +00001117StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
1118{
1119 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
1120}
1121
1122StopInfoSP
1123StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo)
1124{
1125 return StopInfoSP (new StopInfoUnixSignal (thread, signo));
1126}
1127
1128StopInfoSP
1129StopInfo::CreateStopReasonToTrace (Thread &thread)
1130{
1131 return StopInfoSP (new StopInfoTrace (thread));
1132}
1133
1134StopInfoSP
Jim Ingham30fadaf2014-07-08 01:07:32 +00001135StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp,
1136 ValueObjectSP return_valobj_sp,
1137 ClangExpressionVariableSP expression_variable_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001138{
Jim Ingham30fadaf2014-07-08 01:07:32 +00001139 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp, expression_variable_sp));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001140}
Greg Claytona658fd22011-06-04 01:26:29 +00001141
1142StopInfoSP
1143StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
1144{
1145 return StopInfoSP (new StopInfoException (thread, description));
1146}
Jim Ingham73ca05a2011-12-17 01:35:57 +00001147
Greg Clayton90ba8112012-12-05 00:16:59 +00001148StopInfoSP
1149StopInfo::CreateStopReasonWithExec (Thread &thread)
1150{
1151 return StopInfoSP (new StopInfoExec (thread));
1152}
1153
Jim Ingham73ca05a2011-12-17 01:35:57 +00001154ValueObjectSP
1155StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
1156{
1157 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1158 {
1159 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1160 return plan_stop_info->GetReturnValueObject();
1161 }
1162 else
1163 return ValueObjectSP();
1164}
Jim Ingham30fadaf2014-07-08 01:07:32 +00001165
1166ClangExpressionVariableSP
1167StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp)
1168{
1169 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1170 {
1171 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1172 return plan_stop_info->GetExpressionVariable();
1173 }
1174 else
1175 return ClangExpressionVariableSP();
1176}