blob: 6cc1613298839d6a5b60b5a335eb4a86b6040c1c [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"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000027#include "lldb/Core/ValueObject.h"
Jim Ingham4b536182011-08-09 02:12:22 +000028#include "lldb/Expression/ClangUserExpression.h"
29#include "lldb/Target/Target.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000030#include "lldb/Target/Thread.h"
31#include "lldb/Target/ThreadPlan.h"
32#include "lldb/Target/Process.h"
33#include "lldb/Target/UnixSignals.h"
34
35using namespace lldb;
36using namespace lldb_private;
37
38StopInfo::StopInfo (Thread &thread, uint64_t value) :
Greg Clayton46c2b6e2013-04-29 23:30:46 +000039 m_thread_wp (thread.shared_from_this()),
Greg Clayton1ac04c32012-02-21 00:09:25 +000040 m_stop_id (thread.GetProcess()->GetStopID()),
41 m_resume_id (thread.GetProcess()->GetResumeID()),
Jim Ingham0161b492013-02-09 01:29:05 +000042 m_value (value),
Jason Molenda0453be92015-05-15 00:19:28 +000043 m_description (),
Jim Ingham221d51c2013-05-08 00:35:16 +000044 m_override_should_notify (eLazyBoolCalculate),
Kuba Breckaafdf8422014-10-10 23:43:03 +000045 m_override_should_stop (eLazyBoolCalculate),
46 m_extended_info()
Greg Claytonf4b47e12010-08-04 01:40:35 +000047{
48}
49
50bool
51StopInfo::IsValid () const
52{
Greg Clayton46c2b6e2013-04-29 23:30:46 +000053 ThreadSP thread_sp (m_thread_wp.lock());
54 if (thread_sp)
55 return thread_sp->GetProcess()->GetStopID() == m_stop_id;
56 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +000057}
58
Jim Ingham77787032011-01-20 02:03:18 +000059void
60StopInfo::MakeStopInfoValid ()
61{
Greg Clayton46c2b6e2013-04-29 23:30:46 +000062 ThreadSP thread_sp (m_thread_wp.lock());
63 if (thread_sp)
64 {
65 m_stop_id = thread_sp->GetProcess()->GetStopID();
66 m_resume_id = thread_sp->GetProcess()->GetResumeID();
67 }
Jim Ingham77787032011-01-20 02:03:18 +000068}
69
Jim Ingham0faa43f2011-11-08 03:00:11 +000070bool
71StopInfo::HasTargetRunSinceMe ()
Jim Ingham4b536182011-08-09 02:12:22 +000072{
Greg Clayton46c2b6e2013-04-29 23:30:46 +000073 ThreadSP thread_sp (m_thread_wp.lock());
74
75 if (thread_sp)
Jim Ingham0faa43f2011-11-08 03:00:11 +000076 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +000077 lldb::StateType ret_type = thread_sp->GetProcess()->GetPrivateState();
78 if (ret_type == eStateRunning)
Jim Ingham0faa43f2011-11-08 03:00:11 +000079 {
80 return true;
81 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +000082 else if (ret_type == eStateStopped)
83 {
84 // This is a little tricky. We want to count "run and stopped again before you could
85 // ask this question as a "TRUE" answer to HasTargetRunSinceMe. But we don't want to
86 // include any running of the target done for expressions. So we track both resumes,
87 // and resumes caused by expressions, and check if there are any resumes NOT caused
88 // by expressions.
89
90 uint32_t curr_resume_id = thread_sp->GetProcess()->GetResumeID();
91 uint32_t last_user_expression_id = thread_sp->GetProcess()->GetLastUserExpressionResumeID ();
92 if (curr_resume_id == m_resume_id)
93 {
94 return false;
95 }
96 else if (curr_resume_id > last_user_expression_id)
97 {
98 return true;
99 }
100 }
Jim Ingham0faa43f2011-11-08 03:00:11 +0000101 }
102 return false;
Jim Ingham4b536182011-08-09 02:12:22 +0000103}
104
Greg Claytonf4b47e12010-08-04 01:40:35 +0000105//----------------------------------------------------------------------
106// StopInfoBreakpoint
107//----------------------------------------------------------------------
108
Jim Ingham4b536182011-08-09 02:12:22 +0000109namespace lldb_private
110{
Greg Claytonf4b47e12010-08-04 01:40:35 +0000111class StopInfoBreakpoint : public StopInfo
112{
113public:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000114 StopInfoBreakpoint (Thread &thread, break_id_t break_id) :
115 StopInfo (thread, break_id),
Greg Claytonf4b47e12010-08-04 01:40:35 +0000116 m_should_stop (false),
Jim Ingham0f16e732011-02-08 05:20:59 +0000117 m_should_stop_is_valid (false),
Jim Ingham52c7d202011-10-28 01:12:19 +0000118 m_should_perform_action (true),
Jim Inghamca36cd12012-10-05 19:16:31 +0000119 m_address (LLDB_INVALID_ADDRESS),
120 m_break_id(LLDB_INVALID_BREAK_ID),
121 m_was_one_shot (false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000122 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000123 StoreBPInfo();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000124 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000125
Jim Ingham36f3b362010-10-14 23:45:03 +0000126 StopInfoBreakpoint (Thread &thread, break_id_t break_id, bool should_stop) :
127 StopInfo (thread, break_id),
Jim Ingham36f3b362010-10-14 23:45:03 +0000128 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 Claytonab745c22015-04-07 22:17:41 +0000165 virtual bool
166 IsValidForOperatingSystemThread (Thread &thread)
167 {
168 ProcessSP process_sp (thread.GetProcess());
169 if (process_sp)
170 {
171 BreakpointSiteSP bp_site_sp (process_sp->GetBreakpointSiteList().FindByID (m_value));
172 if (bp_site_sp)
173 return bp_site_sp->ValidForThisThread (&thread);
174 }
175 return false;
176 }
177
Greg Claytonf4b47e12010-08-04 01:40:35 +0000178 virtual StopReason
179 GetStopReason () const
180 {
181 return eStopReasonBreakpoint;
182 }
183
184 virtual bool
Jim Ingham6d66ce62012-04-20 21:16:56 +0000185 ShouldStopSynchronous (Event *event_ptr)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000186 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000187 ThreadSP thread_sp (m_thread_wp.lock());
188 if (thread_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000189 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000190 if (!m_should_stop_is_valid)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000191 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000192 // Only check once if we should stop at a breakpoint
193 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
194 if (bp_site_sp)
195 {
196 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
197 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Jim Inghama672ece2014-10-22 01:54:17 +0000198 bp_site_sp->BumpHitCounts();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000199 m_should_stop = bp_site_sp->ShouldStop (&context);
200 }
201 else
202 {
203 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000204
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000205 if (log)
206 log->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000207
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000208 m_should_stop = true;
209 }
210 m_should_stop_is_valid = true;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000211 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000212 return m_should_stop;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000213 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000214 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000215 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000216
Jim Inghamf57b4352012-11-10 02:08:14 +0000217 virtual bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000218 DoShouldNotify (Event *event_ptr)
Jim Inghamf57b4352012-11-10 02:08:14 +0000219 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000220 ThreadSP thread_sp (m_thread_wp.lock());
221 if (thread_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000222 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000223 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
224 if (bp_site_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000225 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000226 bool all_internal = true;
227
228 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
Jim Inghamf57b4352012-11-10 02:08:14 +0000229 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000230 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
231 {
232 all_internal = false;
233 break;
234 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000235 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000236 return all_internal == false;
Jim Inghamf57b4352012-11-10 02:08:14 +0000237 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000238 }
239 return true;
240 }
241
242 virtual const char *
243 GetDescription ()
244 {
245 if (m_description.empty())
246 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000247 ThreadSP thread_sp (m_thread_wp.lock());
248 if (thread_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000249 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000250 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
251 if (bp_site_sp)
Jim Ingham29950772013-01-26 02:19:28 +0000252 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000253 StreamString strm;
254 // If we have just hit an internal breakpoint, and it has a kind description, print that instead of the
255 // full breakpoint printing:
256 if (bp_site_sp->IsInternal())
Jim Ingham29950772013-01-26 02:19:28 +0000257 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000258 size_t num_owners = bp_site_sp->GetNumberOfOwners();
259 for (size_t idx = 0; idx < num_owners; idx++)
Jim Ingham29950772013-01-26 02:19:28 +0000260 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000261 const char *kind = bp_site_sp->GetOwnerAtIndex(idx)->GetBreakpoint().GetBreakpointKind();
262 if (kind != NULL)
263 {
264 m_description.assign (kind);
265 return kind;
266 }
Jim Ingham29950772013-01-26 02:19:28 +0000267 }
268 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000269
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000270 strm.Printf("breakpoint ");
271 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
272 m_description.swap (strm.GetString());
Jim Ingham29950772013-01-26 02:19:28 +0000273 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000274 else
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000275 {
276 StreamString strm;
277 if (m_break_id != LLDB_INVALID_BREAK_ID)
278 {
279 BreakpointSP break_sp = thread_sp->GetProcess()->GetTarget().GetBreakpointByID(m_break_id);
280 if (break_sp)
281 {
282 if (break_sp->IsInternal())
283 {
284 const char *kind = break_sp->GetBreakpointKind();
285 if (kind)
286 strm.Printf ("internal %s breakpoint(%d).", kind, m_break_id);
287 else
288 strm.Printf ("internal breakpoint(%d).", m_break_id);
289 }
290 else
291 {
292 strm.Printf ("breakpoint %d.", m_break_id);
293 }
294 }
295 else
296 {
297 if (m_was_one_shot)
298 strm.Printf ("one-shot breakpoint %d", m_break_id);
299 else
300 strm.Printf ("breakpoint %d which has been deleted.", m_break_id);
301 }
302 }
303 else if (m_address == LLDB_INVALID_ADDRESS)
304 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - unknown address", m_value);
305 else
306 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - was at 0x%" PRIx64, m_value, m_address);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000307
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000308 m_description.swap (strm.GetString());
309 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000310 }
311 }
312 return m_description.c_str();
313 }
314
315protected:
Jim Ingham6d66ce62012-04-20 21:16:56 +0000316 bool
317 ShouldStop (Event *event_ptr)
318 {
319 // This just reports the work done by PerformAction or the synchronous stop. It should
320 // only ever get called after they have had a chance to run.
321 assert (m_should_stop_is_valid);
322 return m_should_stop;
323 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000324
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000325 virtual void
326 PerformAction (Event *event_ptr)
327 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000328 if (!m_should_perform_action)
329 return;
330 m_should_perform_action = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000331
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000332 ThreadSP thread_sp (m_thread_wp.lock());
333
334 if (thread_sp)
Jim Ingham5cb9a182013-03-28 00:13:30 +0000335 {
Jim Inghamcca89952014-08-05 01:58:14 +0000336 Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS | LIBLLDB_LOG_STEP);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000337
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000338 if (!thread_sp->IsValid())
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000339 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000340 // This shouldn't ever happen, but just in case, don't do more harm.
Jason Molenda3f032ff2013-08-06 23:08:59 +0000341 if (log)
342 {
343 log->Printf ("PerformAction got called with an invalid thread.");
344 }
Jim Ingham79ea1d82011-12-09 04:17:31 +0000345 m_should_stop = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000346 m_should_stop_is_valid = true;
347 return;
Jim Ingham79ea1d82011-12-09 04:17:31 +0000348 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000349
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000350 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham46e9f9d2015-04-23 00:28:25 +0000351 std::unordered_set<break_id_t> precondition_breakpoints;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000352
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000353 if (bp_site_sp)
Jim Ingham79ea1d82011-12-09 04:17:31 +0000354 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000355 size_t num_owners = bp_site_sp->GetNumberOfOwners();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000356
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000357 if (num_owners == 0)
358 {
359 m_should_stop = true;
Jim Ingham184e9812013-01-15 02:47:48 +0000360 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000361 else
Jim Ingham4b536182011-08-09 02:12:22 +0000362 {
Jim Ingham46e9f9d2015-04-23 00:28:25 +0000363 // We go through each location, and test first its precondition - this overrides everything. Note,
364 // we only do this once per breakpoint - not once per location...
365 // Then check the condition. If the condition says to stop,
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000366 // then we run the callback for that location. If that callback says to stop as well, then
367 // we set m_should_stop to true; we are going to stop.
368 // But we still want to give all the breakpoints whose conditions say we are going to stop a
369 // chance to run their callbacks.
370 // Of course if any callback restarts the target by putting "continue" in the callback, then
371 // we're going to restart, without running the rest of the callbacks. And in this case we will
372 // end up not stopping even if another location said we should stop. But that's better than not
373 // running all the callbacks.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000374
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000375 m_should_stop = false;
376
377 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
378 Process *process = exe_ctx.GetProcessPtr();
379 if (process->GetModIDRef().IsLastResumeForUserExpression())
Jim Ingham4b536182011-08-09 02:12:22 +0000380 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000381 // If we are in the middle of evaluating an expression, don't run asynchronous breakpoint commands or
382 // expressions. That could lead to infinite recursion if the command or condition re-calls the function
383 // with this breakpoint.
384 // TODO: We can keep a list of the breakpoints we've seen while running expressions in the nested
385 // PerformAction calls that can arise when the action runs a function that hits another breakpoint,
386 // and only stop running commands when we see the same breakpoint hit a second time.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000387
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000388 m_should_stop_is_valid = true;
389 if (log)
390 log->Printf ("StopInfoBreakpoint::PerformAction - Hit a breakpoint while running an expression,"
391 " not running commands to avoid recursion.");
392 bool ignoring_breakpoints = process->GetIgnoreBreakpointsInExpressions();
393 if (ignoring_breakpoints)
Jim Ingham4b536182011-08-09 02:12:22 +0000394 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000395 m_should_stop = false;
396 // Internal breakpoints will always stop.
397 for (size_t j = 0; j < num_owners; j++)
398 {
399 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
400 if (bp_loc_sp->GetBreakpoint().IsInternal())
401 {
402 m_should_stop = true;
403 break;
404 }
405 }
Jim Ingham4b536182011-08-09 02:12:22 +0000406 }
Sean Callanan3dbf3462013-04-19 07:09:15 +0000407 else
408 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000409 m_should_stop = true;
410 }
411 if (log)
412 log->Printf ("StopInfoBreakpoint::PerformAction - in expression, continuing: %s.",
413 m_should_stop ? "true" : "false");
414 process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: hit breakpoint while "
415 "running function, skipping commands and conditions to prevent recursion.");
416 return;
417 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000418
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000419 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000420
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000421 // Let's copy the breakpoint locations out of the site and store them in a local list. That way if
422 // one of the breakpoint actions changes the site, then we won't be operating on a bad list.
Jim Inghama672ece2014-10-22 01:54:17 +0000423 // For safety's sake let's also grab an extra reference to the breakpoint owners of the locations we're
424 // going to examine, since the locations are going to have to get back to their breakpoints, and the
425 // locations don't keep their owners alive. I'm just sticking the BreakpointSP's in a vector since
426 // I'm only really using it to locally increment their retain counts.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000427
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000428 BreakpointLocationCollection site_locations;
Jim Inghama672ece2014-10-22 01:54:17 +0000429 std::vector<lldb::BreakpointSP> location_owners;
430
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000431 for (size_t j = 0; j < num_owners; j++)
Jim Inghama672ece2014-10-22 01:54:17 +0000432 {
433 BreakpointLocationSP loc(bp_site_sp->GetOwnerAtIndex(j));
434 site_locations.Add(loc);
435 location_owners.push_back(loc->GetBreakpoint().shared_from_this());
436
437 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000438
439 for (size_t j = 0; j < num_owners; j++)
440 {
441 lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000442
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000443 // If another action disabled this breakpoint or its location, then don't run the actions.
444 if (!bp_loc_sp->IsEnabled() || !bp_loc_sp->GetBreakpoint().IsEnabled())
445 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000446
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000447 // The breakpoint site may have many locations associated with it, not all of them valid for
448 // this thread. Skip the ones that aren't:
449 if (!bp_loc_sp->ValidForThisThread(thread_sp.get()))
Jim Inghama8b99ca2014-01-15 03:30:04 +0000450 {
451 if (log)
452 {
453 StreamString s;
454 bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000455 log->Printf ("Breakpoint %s hit on thread 0x%llx but it was not for this thread, continuing.",
456 s.GetData(),
457 static_cast<unsigned long long>(thread_sp->GetID()));
Jim Inghama8b99ca2014-01-15 03:30:04 +0000458 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000459 continue;
Jim Inghama8b99ca2014-01-15 03:30:04 +0000460 }
Jim Ingham46e9f9d2015-04-23 00:28:25 +0000461
462 // First run the precondition, but since the precondition is per breakpoint, only run it once
463 // per breakpoint.
464 std::pair<std::unordered_set<break_id_t>::iterator, bool> result
465 = precondition_breakpoints.insert(bp_loc_sp->GetBreakpoint().GetID());
466 if (!result.second)
467 continue;
468
469 bool precondition_result = bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context);
470 if (!precondition_result)
471 continue;
472
473 // Next run the condition for the breakpoint. If that says we should stop, then we'll run
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000474 // the callback for the breakpoint. If the callback says we shouldn't stop that will win.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000475
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000476 if (bp_loc_sp->GetConditionText() != NULL)
477 {
478 Error condition_error;
479 bool condition_says_stop = bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000480
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000481 if (!condition_error.Success())
482 {
483 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
484 StreamSP error_sp = debugger.GetAsyncErrorStream ();
485 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
486 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
487 error_sp->Printf (": \"%s\"",
488 bp_loc_sp->GetConditionText());
489 error_sp->EOL();
490 const char *err_str = condition_error.AsCString("<Unknown Error>");
491 if (log)
492 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000493
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000494 error_sp->PutCString (err_str);
495 error_sp->EOL();
496 error_sp->Flush();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000497 }
498 else
499 {
Jim Inghama8b99ca2014-01-15 03:30:04 +0000500 if (log)
501 {
502 StreamString s;
503 bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000504 log->Printf ("Condition evaluated for breakpoint %s on thread 0x%llx conditon_says_stop: %i.",
505 s.GetData(),
506 static_cast<unsigned long long>(thread_sp->GetID()),
507 condition_says_stop);
Jim Inghama8b99ca2014-01-15 03:30:04 +0000508 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000509 if (!condition_says_stop)
Jim Inghamd762df82015-01-15 01:41:04 +0000510 {
511 // We don't want to increment the hit count of breakpoints if the condition fails.
512 // We've already bumped it by the time we get here, so undo the bump:
513 bp_loc_sp->UndoBumpHitCount();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000514 continue;
Jim Inghamd762df82015-01-15 01:41:04 +0000515 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000516 }
517 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000518
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000519 bool callback_says_stop;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000520
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000521 // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
522 // to get out of there. So set it here.
523 // When we figure out how to nest breakpoint hits then this will change.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000524
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000525 Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
526 bool old_async = debugger.GetAsyncExecution();
527 debugger.SetAsyncExecution (true);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000528
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000529 callback_says_stop = bp_loc_sp->InvokeCallback (&context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000530
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000531 debugger.SetAsyncExecution (old_async);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000532
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000533 if (callback_says_stop)
534 m_should_stop = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000535
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000536 // If we are going to stop for this breakpoint, then remove the breakpoint.
537 if (callback_says_stop && bp_loc_sp && bp_loc_sp->GetBreakpoint().IsOneShot())
538 {
539 thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID (bp_loc_sp->GetBreakpoint().GetID());
540 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000541
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000542 // Also make sure that the callback hasn't continued the target.
543 // If it did, when we'll set m_should_start to false and get out of here.
544 if (HasTargetRunSinceMe ())
545 {
546 m_should_stop = false;
547 break;
Sean Callanan3dbf3462013-04-19 07:09:15 +0000548 }
Jim Ingham4b536182011-08-09 02:12:22 +0000549 }
Jim Ingham4b536182011-08-09 02:12:22 +0000550 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000551 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
552 m_should_stop_is_valid = true;
553
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000554 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000555 else
556 {
557 m_should_stop = true;
558 m_should_stop_is_valid = true;
559 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham79ea1d82011-12-09 04:17:31 +0000560
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000561 if (log_process)
562 log_process->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
563 }
564 if (log)
565 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000566 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000567 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000568
569private:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000570 bool m_should_stop;
571 bool m_should_stop_is_valid;
Jim Ingham0f16e732011-02-08 05:20:59 +0000572 bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
573 // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
Jim Ingham52c7d202011-10-28 01:12:19 +0000574 lldb::addr_t m_address; // We use this to capture the breakpoint site address when we create the StopInfo,
575 // in case somebody deletes it between the time the StopInfo is made and the
576 // description is asked for.
Jim Inghamca36cd12012-10-05 19:16:31 +0000577 lldb::break_id_t m_break_id;
578 bool m_was_one_shot;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000579};
580
581
582//----------------------------------------------------------------------
583// StopInfoWatchpoint
584//----------------------------------------------------------------------
585
586class StopInfoWatchpoint : public StopInfo
587{
588public:
Jim Inghamf57b4352012-11-10 02:08:14 +0000589 // Make sure watchpoint is properly disabled and subsequently enabled while performing watchpoint actions.
590 class WatchpointSentry {
591 public:
592 WatchpointSentry(Process *p, Watchpoint *w):
593 process(p),
594 watchpoint(w)
595 {
596 if (process && watchpoint)
597 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000598 const bool notify = false;
Jim Inghamf57b4352012-11-10 02:08:14 +0000599 watchpoint->TurnOnEphemeralMode();
Jim Ingham1b5792e2012-12-18 02:03:49 +0000600 process->DisableWatchpoint(watchpoint, notify);
Jim Inghamf57b4352012-11-10 02:08:14 +0000601 }
602 }
603 ~WatchpointSentry()
604 {
605 if (process && watchpoint)
606 {
607 if (!watchpoint->IsDisabledDuringEphemeralMode())
Jim Ingham1b5792e2012-12-18 02:03:49 +0000608 {
609 const bool notify = false;
610 process->EnableWatchpoint(watchpoint, notify);
611 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000612 watchpoint->TurnOffEphemeralMode();
613 }
614 }
615 private:
616 Process *process;
617 Watchpoint *watchpoint;
618 };
Greg Claytonf4b47e12010-08-04 01:40:35 +0000619
620 StopInfoWatchpoint (Thread &thread, break_id_t watch_id) :
Johnny Chenfd158f42011-09-21 22:47:15 +0000621 StopInfo(thread, watch_id),
Johnny Chenfd158f42011-09-21 22:47:15 +0000622 m_should_stop(false),
623 m_should_stop_is_valid(false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000624 {
625 }
626
627 virtual ~StopInfoWatchpoint ()
628 {
629 }
630
631 virtual StopReason
632 GetStopReason () const
633 {
634 return eStopReasonWatchpoint;
635 }
636
Jim Inghamf57b4352012-11-10 02:08:14 +0000637 virtual const char *
638 GetDescription ()
639 {
640 if (m_description.empty())
641 {
642 StreamString strm;
Daniel Malead01b2952012-11-29 21:49:15 +0000643 strm.Printf("watchpoint %" PRIi64, m_value);
Jim Inghamf57b4352012-11-10 02:08:14 +0000644 m_description.swap (strm.GetString());
645 }
646 return m_description.c_str();
647 }
648
649protected:
Johnny Chenfd158f42011-09-21 22:47:15 +0000650 virtual bool
Jim Ingham9b620f32013-02-22 21:23:43 +0000651 ShouldStopSynchronous (Event *event_ptr)
Johnny Chenfd158f42011-09-21 22:47:15 +0000652 {
653 // ShouldStop() method is idempotent and should not affect hit count.
Johnny Chen16dcf712011-10-17 18:58:00 +0000654 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
655 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
656 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
657 // StopInfoWatchpoint::ShouldStop() and
658 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
659 // StopInfoWatchpoint::PerformAction().
Johnny Chenfd158f42011-09-21 22:47:15 +0000660 if (m_should_stop_is_valid)
661 return m_should_stop;
662
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000663 ThreadSP thread_sp (m_thread_wp.lock());
664 if (thread_sp)
Johnny Chenfd158f42011-09-21 22:47:15 +0000665 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000666 WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
667 if (wp_sp)
668 {
669 // Check if we should stop at a watchpoint.
670 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
671 StoppointCallbackContext context (event_ptr, exe_ctx, true);
672 m_should_stop = wp_sp->ShouldStop (&context);
673 }
674 else
675 {
676 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chenfd158f42011-09-21 22:47:15 +0000677
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000678 if (log)
679 log->Printf ("Process::%s could not find watchpoint location id: %" PRId64 "...",
680 __FUNCTION__, GetValue());
Johnny Chenfd158f42011-09-21 22:47:15 +0000681
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000682 m_should_stop = true;
683 }
Johnny Chenfd158f42011-09-21 22:47:15 +0000684 }
685 m_should_stop_is_valid = true;
686 return m_should_stop;
687 }
688
Jim Ingham9b620f32013-02-22 21:23:43 +0000689 bool
690 ShouldStop (Event *event_ptr)
691 {
692 // This just reports the work done by PerformAction or the synchronous stop. It should
693 // only ever get called after they have had a chance to run.
694 assert (m_should_stop_is_valid);
695 return m_should_stop;
696 }
697
Johnny Chen16dcf712011-10-17 18:58:00 +0000698 virtual void
699 PerformAction (Event *event_ptr)
700 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000701 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
Johnny Chen16dcf712011-10-17 18:58:00 +0000702 // We're going to calculate if we should stop or not in some way during the course of
703 // this code. Also by default we're going to stop, so set that here.
704 m_should_stop = true;
705
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000706 ThreadSP thread_sp (m_thread_wp.lock());
707 if (thread_sp)
Johnny Chen16dcf712011-10-17 18:58:00 +0000708 {
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000709
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000710 WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
711 if (wp_sp)
Enrico Granataf04a2192012-07-13 23:18:48 +0000712 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000713 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
714 Process* process = exe_ctx.GetProcessPtr();
715
716 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
717 // and it is then enabled after we are finished.
718 WatchpointSentry sentry(process, wp_sp.get());
719
Enrico Granataf04a2192012-07-13 23:18:48 +0000720 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000721 // check if this process is running on an architecture where watchpoints trigger
722 // before the associated instruction runs. if so, disable the WP, single-step and then
723 // re-enable the watchpoint
724 if (process)
Enrico Granataf04a2192012-07-13 23:18:48 +0000725 {
Jason Molendafe806902013-05-04 00:39:52 +0000726 uint32_t num;
727 bool wp_triggers_after;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000728 if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
Enrico Granataf04a2192012-07-13 23:18:48 +0000729 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000730 if (!wp_triggers_after)
731 {
Jim Ingham363ddd72013-07-02 21:12:44 +0000732 StopInfoSP stored_stop_info_sp = thread_sp->GetStopInfo();
733 assert (stored_stop_info_sp.get() == this);
734
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000735 ThreadPlanSP new_plan_sp(thread_sp->QueueThreadPlanForStepSingleInstruction(false, // step-over
Greg Claytondc6224e2014-10-21 01:00:42 +0000736 false, // abort_other_plans
737 true)); // stop_other_threads
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000738 new_plan_sp->SetIsMasterPlan (true);
739 new_plan_sp->SetOkayToDiscard (false);
740 new_plan_sp->SetPrivate (true);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000741 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
Greg Claytondc6224e2014-10-21 01:00:42 +0000742 process->ResumeSynchronous(NULL);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000743 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
Jim Ingham363ddd72013-07-02 21:12:44 +0000744 thread_sp->SetStopInfo(stored_stop_info_sp);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000745 }
Enrico Granataf04a2192012-07-13 23:18:48 +0000746 }
747 }
748 }
Johnny Chen209bd652012-08-13 21:09:54 +0000749
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000750 if (m_should_stop && wp_sp->GetConditionText() != NULL)
Johnny Chen16dcf712011-10-17 18:58:00 +0000751 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000752 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
753 // constructor errors up to the debugger's Async I/O.
Jim Ingham1624a2d2014-05-05 02:26:40 +0000754 ExpressionResults result_code;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000755 EvaluateExpressionOptions expr_options;
756 expr_options.SetUnwindOnError(true);
757 expr_options.SetIgnoreBreakpoints(true);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000758 ValueObjectSP result_value_sp;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000759 Error error;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000760 result_code = ClangUserExpression::Evaluate (exe_ctx,
761 expr_options,
762 wp_sp->GetConditionText(),
763 NULL,
764 result_value_sp,
765 error);
Jim Ingham8646d3c2014-05-05 02:47:44 +0000766 if (result_code == eExpressionCompleted)
Johnny Chen16dcf712011-10-17 18:58:00 +0000767 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000768 if (result_value_sp)
Johnny Chen16dcf712011-10-17 18:58:00 +0000769 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000770 Scalar scalar_value;
771 if (result_value_sp->ResolveValue (scalar_value))
Johnny Chen16dcf712011-10-17 18:58:00 +0000772 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000773 if (scalar_value.ULongLong(1) == 0)
774 {
775 // We have been vetoed. This takes precedence over querying
776 // the watchpoint whether it should stop (aka ignore count and
777 // friends). See also StopInfoWatchpoint::ShouldStop() as well
778 // as Process::ProcessEventData::DoOnRemoval().
779 m_should_stop = false;
780 }
781 else
782 m_should_stop = true;
783 if (log)
784 log->Printf("Condition successfully evaluated, result is %s.\n",
785 m_should_stop ? "true" : "false");
Johnny Chen16dcf712011-10-17 18:58:00 +0000786 }
787 else
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000788 {
Johnny Chen16dcf712011-10-17 18:58:00 +0000789 m_should_stop = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000790 if (log)
791 log->Printf("Failed to get an integer result from the expression.");
792 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000793 }
794 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000795 else
796 {
797 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
798 StreamSP error_sp = debugger.GetAsyncErrorStream ();
799 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
800 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
801 error_sp->Printf (": \"%s\"",
802 wp_sp->GetConditionText());
803 error_sp->EOL();
804 const char *err_str = error.AsCString("<Unknown Error>");
805 if (log)
806 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
807
808 error_sp->PutCString (err_str);
809 error_sp->EOL();
810 error_sp->Flush();
811 // If the condition fails to be parsed or run, we should stop.
812 m_should_stop = true;
813 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000814 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000815
816 // If the condition says to stop, we run the callback to further decide whether to stop.
817 if (m_should_stop)
Johnny Chen16dcf712011-10-17 18:58:00 +0000818 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000819 StoppointCallbackContext context (event_ptr, exe_ctx, false);
820 bool stop_requested = wp_sp->InvokeCallback (&context);
821 // Also make sure that the callback hasn't continued the target.
822 // If it did, when we'll set m_should_stop to false and get out of here.
823 if (HasTargetRunSinceMe ())
824 m_should_stop = false;
825
826 if (m_should_stop && !stop_requested)
827 {
828 // We have been vetoed by the callback mechanism.
829 m_should_stop = false;
830 }
831 }
832 // Finally, if we are going to stop, print out the new & old values:
833 if (m_should_stop)
834 {
835 wp_sp->CaptureWatchedValue(exe_ctx);
836
Greg Clayton1ac04c32012-02-21 00:09:25 +0000837 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000838 StreamSP output_sp = debugger.GetAsyncOutputStream ();
839 wp_sp->DumpSnapshots(output_sp.get());
840 output_sp->EOL();
841 output_sp->Flush();
Johnny Chen16dcf712011-10-17 18:58:00 +0000842 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000843
Johnny Chen16dcf712011-10-17 18:58:00 +0000844 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000845 else
846 {
847 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chen13206412012-08-09 23:10:57 +0000848
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000849 if (log_process)
850 log_process->Printf ("Process::%s could not find watchpoint id: %" PRId64 "...", __FUNCTION__, m_value);
Johnny Chen13206412012-08-09 23:10:57 +0000851 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000852 if (log)
853 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Inghama7dfb662012-10-23 07:20:06 +0000854
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000855 m_should_stop_is_valid = true;
Johnny Chen16dcf712011-10-17 18:58:00 +0000856 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000857 }
858
Greg Claytonf4b47e12010-08-04 01:40:35 +0000859private:
Johnny Chenfd158f42011-09-21 22:47:15 +0000860 bool m_should_stop;
861 bool m_should_stop_is_valid;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000862};
863
864
865
866//----------------------------------------------------------------------
867// StopInfoUnixSignal
868//----------------------------------------------------------------------
869
870class StopInfoUnixSignal : public StopInfo
871{
872public:
873
Pavel Labathc4e25c92015-05-29 10:13:03 +0000874 StopInfoUnixSignal (Thread &thread, int signo, const char *description) :
Greg Claytona658fd22011-06-04 01:26:29 +0000875 StopInfo (thread, signo)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000876 {
Pavel Labathc4e25c92015-05-29 10:13:03 +0000877 SetDescription (description);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000878 }
879
880 virtual ~StopInfoUnixSignal ()
881 {
882 }
883
884
885 virtual StopReason
886 GetStopReason () const
887 {
888 return eStopReasonSignal;
889 }
890
891 virtual bool
Jim Ingham0161b492013-02-09 01:29:05 +0000892 ShouldStopSynchronous (Event *event_ptr)
893 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000894 ThreadSP thread_sp (m_thread_wp.lock());
895 if (thread_sp)
896 return thread_sp->GetProcess()->GetUnixSignals().GetShouldStop (m_value);
897 return false;
Jim Ingham0161b492013-02-09 01:29:05 +0000898 }
899
900 virtual bool
Greg Claytonf4b47e12010-08-04 01:40:35 +0000901 ShouldStop (Event *event_ptr)
902 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000903 ThreadSP thread_sp (m_thread_wp.lock());
904 if (thread_sp)
905 return thread_sp->GetProcess()->GetUnixSignals().GetShouldStop (m_value);
906 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000907 }
908
909
910 // If should stop returns false, check if we should notify of this event
911 virtual bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000912 DoShouldNotify (Event *event_ptr)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000913 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000914 ThreadSP thread_sp (m_thread_wp.lock());
915 if (thread_sp)
Jim Ingham0161b492013-02-09 01:29:05 +0000916 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000917 bool should_notify = thread_sp->GetProcess()->GetUnixSignals().GetShouldNotify (m_value);
918 if (should_notify)
919 {
920 StreamString strm;
921 strm.Printf ("thread %d received signal: %s",
922 thread_sp->GetIndexID(),
923 thread_sp->GetProcess()->GetUnixSignals().GetSignalAsCString (m_value));
924 Process::ProcessEventData::AddRestartedReason(event_ptr, strm.GetData());
925 }
926 return should_notify;
Jim Ingham0161b492013-02-09 01:29:05 +0000927 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000928 return true;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000929 }
930
931
932 virtual void
933 WillResume (lldb::StateType resume_state)
934 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000935 ThreadSP thread_sp (m_thread_wp.lock());
936 if (thread_sp)
937 {
938 if (thread_sp->GetProcess()->GetUnixSignals().GetShouldSuppress(m_value) == false)
939 thread_sp->SetResumeSignal(m_value);
940 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000941 }
942
943 virtual const char *
944 GetDescription ()
945 {
946 if (m_description.empty())
947 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000948 ThreadSP thread_sp (m_thread_wp.lock());
949 if (thread_sp)
950 {
951 StreamString strm;
952 const char *signal_name = thread_sp->GetProcess()->GetUnixSignals().GetSignalAsCString (m_value);
953 if (signal_name)
954 strm.Printf("signal %s", signal_name);
955 else
956 strm.Printf("signal %" PRIi64, m_value);
957 m_description.swap (strm.GetString());
958 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000959 }
960 return m_description.c_str();
961 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000962};
963
964//----------------------------------------------------------------------
965// StopInfoTrace
966//----------------------------------------------------------------------
967
968class StopInfoTrace : public StopInfo
969{
970public:
971
972 StopInfoTrace (Thread &thread) :
973 StopInfo (thread, LLDB_INVALID_UID)
974 {
975 }
976
977 virtual ~StopInfoTrace ()
978 {
979 }
980
981 virtual StopReason
982 GetStopReason () const
983 {
984 return eStopReasonTrace;
985 }
986
987 virtual const char *
988 GetDescription ()
989 {
Greg Claytona658fd22011-06-04 01:26:29 +0000990 if (m_description.empty())
Greg Claytonf4b47e12010-08-04 01:40:35 +0000991 return "trace";
Greg Claytona658fd22011-06-04 01:26:29 +0000992 else
993 return m_description.c_str();
994 }
995};
996
997
998//----------------------------------------------------------------------
999// StopInfoException
1000//----------------------------------------------------------------------
1001
1002class StopInfoException : public StopInfo
1003{
1004public:
1005
1006 StopInfoException (Thread &thread, const char *description) :
1007 StopInfo (thread, LLDB_INVALID_UID)
1008 {
1009 if (description)
1010 SetDescription (description);
1011 }
1012
1013 virtual
1014 ~StopInfoException ()
1015 {
1016 }
1017
1018 virtual StopReason
1019 GetStopReason () const
1020 {
1021 return eStopReasonException;
1022 }
1023
1024 virtual const char *
1025 GetDescription ()
1026 {
1027 if (m_description.empty())
1028 return "exception";
1029 else
1030 return m_description.c_str();
Greg Claytonf4b47e12010-08-04 01:40:35 +00001031 }
1032};
1033
1034
1035//----------------------------------------------------------------------
1036// StopInfoThreadPlan
1037//----------------------------------------------------------------------
1038
1039class StopInfoThreadPlan : public StopInfo
1040{
1041public:
1042
Jim Ingham30fadaf2014-07-08 01:07:32 +00001043 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp, ClangExpressionVariableSP &expression_variable_sp) :
Greg Claytonf4b47e12010-08-04 01:40:35 +00001044 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham73ca05a2011-12-17 01:35:57 +00001045 m_plan_sp (plan_sp),
Jim Ingham30fadaf2014-07-08 01:07:32 +00001046 m_return_valobj_sp (return_valobj_sp),
1047 m_expression_variable_sp (expression_variable_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001048 {
1049 }
1050
1051 virtual ~StopInfoThreadPlan ()
1052 {
1053 }
1054
1055 virtual StopReason
1056 GetStopReason () const
1057 {
1058 return eStopReasonPlanComplete;
1059 }
1060
1061 virtual const char *
1062 GetDescription ()
1063 {
1064 if (m_description.empty())
1065 {
1066 StreamString strm;
1067 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
1068 m_description.swap (strm.GetString());
1069 }
1070 return m_description.c_str();
1071 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001072
1073 ValueObjectSP
1074 GetReturnValueObject()
1075 {
1076 return m_return_valobj_sp;
1077 }
Jim Ingham0161b492013-02-09 01:29:05 +00001078
Jim Ingham30fadaf2014-07-08 01:07:32 +00001079 ClangExpressionVariableSP
1080 GetExpressionVariable()
1081 {
1082 return m_expression_variable_sp;
1083 }
1084
Jim Ingham0161b492013-02-09 01:29:05 +00001085protected:
1086 virtual bool
1087 ShouldStop (Event *event_ptr)
1088 {
1089 if (m_plan_sp)
1090 return m_plan_sp->ShouldStop(event_ptr);
1091 else
1092 return StopInfo::ShouldStop(event_ptr);
1093 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001094
1095private:
1096 ThreadPlanSP m_plan_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001097 ValueObjectSP m_return_valobj_sp;
Jim Ingham30fadaf2014-07-08 01:07:32 +00001098 ClangExpressionVariableSP m_expression_variable_sp;
Greg Claytonf4b47e12010-08-04 01:40:35 +00001099};
Greg Clayton90ba8112012-12-05 00:16:59 +00001100
1101class StopInfoExec : public StopInfo
1102{
1103public:
1104
1105 StopInfoExec (Thread &thread) :
1106 StopInfo (thread, LLDB_INVALID_UID),
1107 m_performed_action (false)
1108 {
1109 }
1110
1111 virtual
1112 ~StopInfoExec ()
1113 {
1114 }
1115
1116 virtual StopReason
1117 GetStopReason () const
1118 {
1119 return eStopReasonExec;
1120 }
1121
1122 virtual const char *
1123 GetDescription ()
1124 {
1125 return "exec";
1126 }
1127protected:
Greg Clayton90ba8112012-12-05 00:16:59 +00001128
1129 virtual void
1130 PerformAction (Event *event_ptr)
1131 {
1132 // Only perform the action once
1133 if (m_performed_action)
1134 return;
1135 m_performed_action = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +00001136 ThreadSP thread_sp (m_thread_wp.lock());
1137 if (thread_sp)
1138 thread_sp->GetProcess()->DidExec();
Greg Clayton90ba8112012-12-05 00:16:59 +00001139 }
1140
1141 bool m_performed_action;
1142};
1143
Jim Ingham4b536182011-08-09 02:12:22 +00001144} // namespace lldb_private
Greg Claytonf4b47e12010-08-04 01:40:35 +00001145
1146StopInfoSP
1147StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
1148{
1149 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
1150}
1151
1152StopInfoSP
Jim Ingham36f3b362010-10-14 23:45:03 +00001153StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
1154{
1155 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
1156}
1157
1158StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +00001159StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id)
1160{
1161 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id));
1162}
1163
1164StopInfoSP
Pavel Labathc4e25c92015-05-29 10:13:03 +00001165StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo, const char *description)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001166{
Pavel Labathc4e25c92015-05-29 10:13:03 +00001167 return StopInfoSP (new StopInfoUnixSignal (thread, signo, description));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001168}
1169
1170StopInfoSP
1171StopInfo::CreateStopReasonToTrace (Thread &thread)
1172{
1173 return StopInfoSP (new StopInfoTrace (thread));
1174}
1175
1176StopInfoSP
Jim Ingham30fadaf2014-07-08 01:07:32 +00001177StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp,
1178 ValueObjectSP return_valobj_sp,
1179 ClangExpressionVariableSP expression_variable_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001180{
Jim Ingham30fadaf2014-07-08 01:07:32 +00001181 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp, expression_variable_sp));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001182}
Greg Claytona658fd22011-06-04 01:26:29 +00001183
1184StopInfoSP
1185StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
1186{
1187 return StopInfoSP (new StopInfoException (thread, description));
1188}
Jim Ingham73ca05a2011-12-17 01:35:57 +00001189
Greg Clayton90ba8112012-12-05 00:16:59 +00001190StopInfoSP
1191StopInfo::CreateStopReasonWithExec (Thread &thread)
1192{
1193 return StopInfoSP (new StopInfoExec (thread));
1194}
1195
Jim Ingham73ca05a2011-12-17 01:35:57 +00001196ValueObjectSP
1197StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
1198{
1199 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1200 {
1201 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1202 return plan_stop_info->GetReturnValueObject();
1203 }
1204 else
1205 return ValueObjectSP();
1206}
Jim Ingham30fadaf2014-07-08 01:07:32 +00001207
1208ClangExpressionVariableSP
1209StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp)
1210{
1211 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1212 {
1213 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1214 return plan_stop_info->GetExpressionVariable();
1215 }
1216 else
1217 return ClangExpressionVariableSP();
1218}