blob: c77ca8f73f64c031a0c96d8acb122b1a82ddebb9 [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
10#include "lldb/Target/StopInfo.h"
11
12// C Includes
13// C++ Includes
14#include <string>
15
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Core/Log.h"
Greg Clayton4e78f602010-11-18 18:52:36 +000019#include "lldb/Breakpoint/Breakpoint.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000020#include "lldb/Breakpoint/BreakpointLocation.h"
21#include "lldb/Breakpoint/StoppointCallbackContext.h"
Johnny Chen01a67862011-10-14 00:42:25 +000022#include "lldb/Breakpoint/Watchpoint.h"
Jim Ingham4b536182011-08-09 02:12:22 +000023#include "lldb/Core/Debugger.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000024#include "lldb/Core/StreamString.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000025#include "lldb/Core/ValueObject.h"
Jim Ingham151c0322015-09-15 21:13:50 +000026#include "lldb/Expression/UserExpression.h"
Jim Ingham4b536182011-08-09 02:12:22 +000027#include "lldb/Target/Target.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000028#include "lldb/Target/Thread.h"
29#include "lldb/Target/ThreadPlan.h"
30#include "lldb/Target/Process.h"
31#include "lldb/Target/UnixSignals.h"
32
33using namespace lldb;
34using namespace lldb_private;
35
36StopInfo::StopInfo (Thread &thread, uint64_t value) :
Greg Clayton46c2b6e2013-04-29 23:30:46 +000037 m_thread_wp (thread.shared_from_this()),
Greg Clayton1ac04c32012-02-21 00:09:25 +000038 m_stop_id (thread.GetProcess()->GetStopID()),
39 m_resume_id (thread.GetProcess()->GetResumeID()),
Jim Ingham0161b492013-02-09 01:29:05 +000040 m_value (value),
Jason Molenda0453be92015-05-15 00:19:28 +000041 m_description (),
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),
Greg Claytonf4b47e12010-08-04 01:40:35 +0000114 m_should_stop (false),
Jim Ingham0f16e732011-02-08 05:20:59 +0000115 m_should_stop_is_valid (false),
Jim Ingham52c7d202011-10-28 01:12:19 +0000116 m_should_perform_action (true),
Jim Inghamca36cd12012-10-05 19:16:31 +0000117 m_address (LLDB_INVALID_ADDRESS),
118 m_break_id(LLDB_INVALID_BREAK_ID),
119 m_was_one_shot (false)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000120 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000121 StoreBPInfo();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000122 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000123
Jim Ingham36f3b362010-10-14 23:45:03 +0000124 StopInfoBreakpoint (Thread &thread, break_id_t break_id, bool should_stop) :
125 StopInfo (thread, break_id),
Jim Ingham36f3b362010-10-14 23:45:03 +0000126 m_should_stop (should_stop),
Jim Ingham0f16e732011-02-08 05:20:59 +0000127 m_should_stop_is_valid (true),
Jim Ingham52c7d202011-10-28 01:12:19 +0000128 m_should_perform_action (true),
Jim Inghamca36cd12012-10-05 19:16:31 +0000129 m_address (LLDB_INVALID_ADDRESS),
130 m_break_id(LLDB_INVALID_BREAK_ID),
131 m_was_one_shot (false)
132 {
133 StoreBPInfo();
134 }
135
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000136 void
137 StoreBPInfo ()
Jim Ingham36f3b362010-10-14 23:45:03 +0000138 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000139 ThreadSP thread_sp (m_thread_wp.lock());
140 if (thread_sp)
Jim Ingham52c7d202011-10-28 01:12:19 +0000141 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000142 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
143 if (bp_site_sp)
Jim Inghamca36cd12012-10-05 19:16:31 +0000144 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000145 if (bp_site_sp->GetNumberOfOwners() == 1)
Jim Inghamca36cd12012-10-05 19:16:31 +0000146 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000147 BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(0);
148 if (bp_loc_sp)
149 {
150 m_break_id = bp_loc_sp->GetBreakpoint().GetID();
151 m_was_one_shot = bp_loc_sp->GetBreakpoint().IsOneShot();
152 }
Jim Inghamca36cd12012-10-05 19:16:31 +0000153 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000154 m_address = bp_site_sp->GetLoadAddress();
Jim Inghamca36cd12012-10-05 19:16:31 +0000155 }
Jim Ingham52c7d202011-10-28 01:12:19 +0000156 }
Jim Ingham36f3b362010-10-14 23:45:03 +0000157 }
158
Greg Claytonf4b47e12010-08-04 01:40:35 +0000159 virtual ~StopInfoBreakpoint ()
160 {
161 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000162
Greg Claytonab745c22015-04-07 22:17:41 +0000163 virtual bool
164 IsValidForOperatingSystemThread (Thread &thread)
165 {
166 ProcessSP process_sp (thread.GetProcess());
167 if (process_sp)
168 {
169 BreakpointSiteSP bp_site_sp (process_sp->GetBreakpointSiteList().FindByID (m_value));
170 if (bp_site_sp)
171 return bp_site_sp->ValidForThisThread (&thread);
172 }
173 return false;
174 }
175
Greg Claytonf4b47e12010-08-04 01:40:35 +0000176 virtual StopReason
177 GetStopReason () const
178 {
179 return eStopReasonBreakpoint;
180 }
181
182 virtual bool
Jim Ingham6d66ce62012-04-20 21:16:56 +0000183 ShouldStopSynchronous (Event *event_ptr)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000184 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000185 ThreadSP thread_sp (m_thread_wp.lock());
186 if (thread_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000187 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000188 if (!m_should_stop_is_valid)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000189 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000190 // Only check once if we should stop at a breakpoint
191 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
192 if (bp_site_sp)
193 {
194 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
195 StoppointCallbackContext context (event_ptr, exe_ctx, true);
Jim Inghama672ece2014-10-22 01:54:17 +0000196 bp_site_sp->BumpHitCounts();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000197 m_should_stop = bp_site_sp->ShouldStop (&context);
198 }
199 else
200 {
201 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Greg Claytonf4b47e12010-08-04 01:40:35 +0000202
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000203 if (log)
204 log->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000205
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000206 m_should_stop = true;
207 }
208 m_should_stop_is_valid = true;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000209 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000210 return m_should_stop;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000211 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000212 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000213 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000214
Jim Inghamf57b4352012-11-10 02:08:14 +0000215 virtual bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000216 DoShouldNotify (Event *event_ptr)
Jim Inghamf57b4352012-11-10 02:08:14 +0000217 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000218 ThreadSP thread_sp (m_thread_wp.lock());
219 if (thread_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000220 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000221 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
222 if (bp_site_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000223 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000224 bool all_internal = true;
225
226 for (uint32_t i = 0; i < bp_site_sp->GetNumberOfOwners(); i++)
Jim Inghamf57b4352012-11-10 02:08:14 +0000227 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000228 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal())
229 {
230 all_internal = false;
231 break;
232 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000233 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000234 return all_internal == false;
Jim Inghamf57b4352012-11-10 02:08:14 +0000235 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000236 }
237 return true;
238 }
239
240 virtual const char *
241 GetDescription ()
242 {
243 if (m_description.empty())
244 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000245 ThreadSP thread_sp (m_thread_wp.lock());
246 if (thread_sp)
Jim Inghamf57b4352012-11-10 02:08:14 +0000247 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000248 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
249 if (bp_site_sp)
Jim Ingham29950772013-01-26 02:19:28 +0000250 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000251 StreamString strm;
252 // If we have just hit an internal breakpoint, and it has a kind description, print that instead of the
253 // full breakpoint printing:
254 if (bp_site_sp->IsInternal())
Jim Ingham29950772013-01-26 02:19:28 +0000255 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000256 size_t num_owners = bp_site_sp->GetNumberOfOwners();
257 for (size_t idx = 0; idx < num_owners; idx++)
Jim Ingham29950772013-01-26 02:19:28 +0000258 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000259 const char *kind = bp_site_sp->GetOwnerAtIndex(idx)->GetBreakpoint().GetBreakpointKind();
260 if (kind != NULL)
261 {
262 m_description.assign (kind);
263 return kind;
264 }
Jim Ingham29950772013-01-26 02:19:28 +0000265 }
266 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000267
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000268 strm.Printf("breakpoint ");
269 bp_site_sp->GetDescription(&strm, eDescriptionLevelBrief);
270 m_description.swap (strm.GetString());
Jim Ingham29950772013-01-26 02:19:28 +0000271 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000272 else
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000273 {
274 StreamString strm;
275 if (m_break_id != LLDB_INVALID_BREAK_ID)
276 {
277 BreakpointSP break_sp = thread_sp->GetProcess()->GetTarget().GetBreakpointByID(m_break_id);
278 if (break_sp)
279 {
280 if (break_sp->IsInternal())
281 {
282 const char *kind = break_sp->GetBreakpointKind();
283 if (kind)
284 strm.Printf ("internal %s breakpoint(%d).", kind, m_break_id);
285 else
286 strm.Printf ("internal breakpoint(%d).", m_break_id);
287 }
288 else
289 {
290 strm.Printf ("breakpoint %d.", m_break_id);
291 }
292 }
293 else
294 {
295 if (m_was_one_shot)
296 strm.Printf ("one-shot breakpoint %d", m_break_id);
297 else
298 strm.Printf ("breakpoint %d which has been deleted.", m_break_id);
299 }
300 }
301 else if (m_address == LLDB_INVALID_ADDRESS)
302 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - unknown address", m_value);
303 else
304 strm.Printf("breakpoint site %" PRIi64 " which has been deleted - was at 0x%" PRIx64, m_value, m_address);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000305
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000306 m_description.swap (strm.GetString());
307 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000308 }
309 }
310 return m_description.c_str();
311 }
312
313protected:
Jim Ingham6d66ce62012-04-20 21:16:56 +0000314 bool
315 ShouldStop (Event *event_ptr)
316 {
317 // This just reports the work done by PerformAction or the synchronous stop. It should
318 // only ever get called after they have had a chance to run.
319 assert (m_should_stop_is_valid);
320 return m_should_stop;
321 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000322
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000323 virtual void
324 PerformAction (Event *event_ptr)
325 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000326 if (!m_should_perform_action)
327 return;
328 m_should_perform_action = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000329
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000330 ThreadSP thread_sp (m_thread_wp.lock());
331
332 if (thread_sp)
Jim Ingham5cb9a182013-03-28 00:13:30 +0000333 {
Jim Inghamcca89952014-08-05 01:58:14 +0000334 Log *log = lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS | LIBLLDB_LOG_STEP);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000335
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000336 if (!thread_sp->IsValid())
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000337 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000338 // This shouldn't ever happen, but just in case, don't do more harm.
Jason Molenda3f032ff2013-08-06 23:08:59 +0000339 if (log)
340 {
341 log->Printf ("PerformAction got called with an invalid thread.");
342 }
Jim Ingham79ea1d82011-12-09 04:17:31 +0000343 m_should_stop = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000344 m_should_stop_is_valid = true;
345 return;
Jim Ingham79ea1d82011-12-09 04:17:31 +0000346 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000347
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000348 BreakpointSiteSP bp_site_sp (thread_sp->GetProcess()->GetBreakpointSiteList().FindByID (m_value));
Jim Ingham46e9f9d2015-04-23 00:28:25 +0000349 std::unordered_set<break_id_t> precondition_breakpoints;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000350
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000351 if (bp_site_sp)
Jim Ingham79ea1d82011-12-09 04:17:31 +0000352 {
Jim Ingham9d1ccda2015-07-29 17:51:36 +0000353 // Let's copy the owners list out of the site and store them in a local list. That way if
354 // one of the breakpoint actions changes the site, then we won't be operating on a bad list.
355 BreakpointLocationCollection site_locations;
356 size_t num_owners = bp_site_sp->CopyOwnersList(site_locations);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000357
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000358 if (num_owners == 0)
359 {
360 m_should_stop = true;
Jim Ingham184e9812013-01-15 02:47:48 +0000361 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000362 else
Jim Ingham4b536182011-08-09 02:12:22 +0000363 {
Jim Ingham46e9f9d2015-04-23 00:28:25 +0000364 // We go through each location, and test first its precondition - this overrides everything. Note,
365 // we only do this once per breakpoint - not once per location...
366 // Then check the condition. If the condition says to stop,
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000367 // then we run the callback for that location. If that callback says to stop as well, then
368 // we set m_should_stop to true; we are going to stop.
369 // But we still want to give all the breakpoints whose conditions say we are going to stop a
370 // chance to run their callbacks.
371 // Of course if any callback restarts the target by putting "continue" in the callback, then
372 // we're going to restart, without running the rest of the callbacks. And in this case we will
373 // end up not stopping even if another location said we should stop. But that's better than not
374 // running all the callbacks.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000375
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000376 m_should_stop = false;
377
378 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
379 Process *process = exe_ctx.GetProcessPtr();
380 if (process->GetModIDRef().IsLastResumeForUserExpression())
Jim Ingham4b536182011-08-09 02:12:22 +0000381 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000382 // If we are in the middle of evaluating an expression, don't run asynchronous breakpoint commands or
383 // expressions. That could lead to infinite recursion if the command or condition re-calls the function
384 // with this breakpoint.
385 // TODO: We can keep a list of the breakpoints we've seen while running expressions in the nested
386 // PerformAction calls that can arise when the action runs a function that hits another breakpoint,
387 // and only stop running commands when we see the same breakpoint hit a second time.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000388
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000389 m_should_stop_is_valid = true;
390 if (log)
391 log->Printf ("StopInfoBreakpoint::PerformAction - Hit a breakpoint while running an expression,"
392 " not running commands to avoid recursion.");
393 bool ignoring_breakpoints = process->GetIgnoreBreakpointsInExpressions();
394 if (ignoring_breakpoints)
Jim Ingham4b536182011-08-09 02:12:22 +0000395 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000396 m_should_stop = false;
397 // Internal breakpoints will always stop.
398 for (size_t j = 0; j < num_owners; j++)
399 {
400 lldb::BreakpointLocationSP bp_loc_sp = bp_site_sp->GetOwnerAtIndex(j);
401 if (bp_loc_sp->GetBreakpoint().IsInternal())
402 {
403 m_should_stop = true;
404 break;
405 }
406 }
Jim Ingham4b536182011-08-09 02:12:22 +0000407 }
Sean Callanan3dbf3462013-04-19 07:09:15 +0000408 else
409 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000410 m_should_stop = true;
411 }
412 if (log)
413 log->Printf ("StopInfoBreakpoint::PerformAction - in expression, continuing: %s.",
414 m_should_stop ? "true" : "false");
415 process->GetTarget().GetDebugger().GetAsyncOutputStream()->Printf("Warning: hit breakpoint while "
416 "running function, skipping commands and conditions to prevent recursion.");
417 return;
418 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000419
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000420 StoppointCallbackContext context (event_ptr, exe_ctx, false);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000421
Jim Inghama672ece2014-10-22 01:54:17 +0000422 // For safety's sake let's also grab an extra reference to the breakpoint owners of the locations we're
423 // going to examine, since the locations are going to have to get back to their breakpoints, and the
424 // locations don't keep their owners alive. I'm just sticking the BreakpointSP's in a vector since
Jim Ingham9d1ccda2015-07-29 17:51:36 +0000425 // I'm only using it to locally increment their retain counts.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000426
Jim Inghama672ece2014-10-22 01:54:17 +0000427 std::vector<lldb::BreakpointSP> location_owners;
428
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000429 for (size_t j = 0; j < num_owners; j++)
Jim Inghama672ece2014-10-22 01:54:17 +0000430 {
Jim Ingham9d1ccda2015-07-29 17:51:36 +0000431 BreakpointLocationSP loc(site_locations.GetByIndex(j));
Jim Inghama672ece2014-10-22 01:54:17 +0000432 location_owners.push_back(loc->GetBreakpoint().shared_from_this());
433
434 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000435
436 for (size_t j = 0; j < num_owners; j++)
437 {
438 lldb::BreakpointLocationSP bp_loc_sp = site_locations.GetByIndex(j);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000439
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000440 // If another action disabled this breakpoint or its location, then don't run the actions.
441 if (!bp_loc_sp->IsEnabled() || !bp_loc_sp->GetBreakpoint().IsEnabled())
442 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000443
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000444 // The breakpoint site may have many locations associated with it, not all of them valid for
445 // this thread. Skip the ones that aren't:
446 if (!bp_loc_sp->ValidForThisThread(thread_sp.get()))
Jim Inghama8b99ca2014-01-15 03:30:04 +0000447 {
448 if (log)
449 {
450 StreamString s;
451 bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000452 log->Printf ("Breakpoint %s hit on thread 0x%llx but it was not for this thread, continuing.",
453 s.GetData(),
454 static_cast<unsigned long long>(thread_sp->GetID()));
Jim Inghama8b99ca2014-01-15 03:30:04 +0000455 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000456 continue;
Jim Inghama8b99ca2014-01-15 03:30:04 +0000457 }
Jim Ingham46e9f9d2015-04-23 00:28:25 +0000458
459 // First run the precondition, but since the precondition is per breakpoint, only run it once
460 // per breakpoint.
461 std::pair<std::unordered_set<break_id_t>::iterator, bool> result
462 = precondition_breakpoints.insert(bp_loc_sp->GetBreakpoint().GetID());
463 if (!result.second)
464 continue;
465
466 bool precondition_result = bp_loc_sp->GetBreakpoint().EvaluatePrecondition(context);
467 if (!precondition_result)
468 continue;
469
470 // Next run the condition for the breakpoint. If that says we should stop, then we'll run
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000471 // the callback for the breakpoint. If the callback says we shouldn't stop that will win.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000472
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000473 if (bp_loc_sp->GetConditionText() != NULL)
474 {
475 Error condition_error;
476 bool condition_says_stop = bp_loc_sp->ConditionSaysStop(exe_ctx, condition_error);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000477
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000478 if (!condition_error.Success())
479 {
480 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
481 StreamSP error_sp = debugger.GetAsyncErrorStream ();
482 error_sp->Printf ("Stopped due to an error evaluating condition of breakpoint ");
483 bp_loc_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
484 error_sp->Printf (": \"%s\"",
485 bp_loc_sp->GetConditionText());
486 error_sp->EOL();
487 const char *err_str = condition_error.AsCString("<Unknown Error>");
488 if (log)
489 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000490
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000491 error_sp->PutCString (err_str);
492 error_sp->EOL();
493 error_sp->Flush();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000494 }
495 else
496 {
Jim Inghama8b99ca2014-01-15 03:30:04 +0000497 if (log)
498 {
499 StreamString s;
500 bp_loc_sp->GetDescription(&s, eDescriptionLevelBrief);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000501 log->Printf ("Condition evaluated for breakpoint %s on thread 0x%llx conditon_says_stop: %i.",
502 s.GetData(),
503 static_cast<unsigned long long>(thread_sp->GetID()),
504 condition_says_stop);
Jim Inghama8b99ca2014-01-15 03:30:04 +0000505 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000506 if (!condition_says_stop)
Jim Inghamd762df82015-01-15 01:41:04 +0000507 {
508 // We don't want to increment the hit count of breakpoints if the condition fails.
509 // We've already bumped it by the time we get here, so undo the bump:
510 bp_loc_sp->UndoBumpHitCount();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000511 continue;
Jim Inghamd762df82015-01-15 01:41:04 +0000512 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000513 }
514 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000515
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000516 bool callback_says_stop;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000517
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000518 // FIXME: For now the callbacks have to run in async mode - the first time we restart we need
519 // to get out of there. So set it here.
520 // When we figure out how to nest breakpoint hits then this will change.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000521
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000522 Debugger &debugger = thread_sp->CalculateTarget()->GetDebugger();
523 bool old_async = debugger.GetAsyncExecution();
524 debugger.SetAsyncExecution (true);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000525
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000526 callback_says_stop = bp_loc_sp->InvokeCallback (&context);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000527
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000528 debugger.SetAsyncExecution (old_async);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000529
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000530 if (callback_says_stop)
531 m_should_stop = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000532
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000533 // If we are going to stop for this breakpoint, then remove the breakpoint.
534 if (callback_says_stop && bp_loc_sp && bp_loc_sp->GetBreakpoint().IsOneShot())
535 {
536 thread_sp->GetProcess()->GetTarget().RemoveBreakpointByID (bp_loc_sp->GetBreakpoint().GetID());
537 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000538
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000539 // Also make sure that the callback hasn't continued the target.
540 // If it did, when we'll set m_should_start to false and get out of here.
541 if (HasTargetRunSinceMe ())
542 {
543 m_should_stop = false;
544 break;
Sean Callanan3dbf3462013-04-19 07:09:15 +0000545 }
Jim Ingham4b536182011-08-09 02:12:22 +0000546 }
Jim Ingham4b536182011-08-09 02:12:22 +0000547 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000548 // We've figured out what this stop wants to do, so mark it as valid so we don't compute it again.
549 m_should_stop_is_valid = true;
550
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000551 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000552 else
553 {
554 m_should_stop = true;
555 m_should_stop_is_valid = true;
556 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Jim Ingham79ea1d82011-12-09 04:17:31 +0000557
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000558 if (log_process)
559 log_process->Printf ("Process::%s could not find breakpoint site id: %" PRId64 "...", __FUNCTION__, m_value);
560 }
561 if (log)
562 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000563 }
Jim Ingham3ebcf7f2010-08-10 00:59:59 +0000564 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000565
566private:
Greg Claytonf4b47e12010-08-04 01:40:35 +0000567 bool m_should_stop;
568 bool m_should_stop_is_valid;
Jim Ingham0f16e732011-02-08 05:20:59 +0000569 bool m_should_perform_action; // Since we are trying to preserve the "state" of the system even if we run functions
570 // etc. behind the users backs, we need to make sure we only REALLY perform the action once.
Jim Ingham52c7d202011-10-28 01:12:19 +0000571 lldb::addr_t m_address; // We use this to capture the breakpoint site address when we create the StopInfo,
572 // in case somebody deletes it between the time the StopInfo is made and the
573 // description is asked for.
Jim Inghamca36cd12012-10-05 19:16:31 +0000574 lldb::break_id_t m_break_id;
575 bool m_was_one_shot;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000576};
577
578
579//----------------------------------------------------------------------
580// StopInfoWatchpoint
581//----------------------------------------------------------------------
582
583class StopInfoWatchpoint : public StopInfo
584{
585public:
Jim Inghamf57b4352012-11-10 02:08:14 +0000586 // Make sure watchpoint is properly disabled and subsequently enabled while performing watchpoint actions.
587 class WatchpointSentry {
588 public:
589 WatchpointSentry(Process *p, Watchpoint *w):
590 process(p),
591 watchpoint(w)
592 {
593 if (process && watchpoint)
594 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000595 const bool notify = false;
Jim Inghamf57b4352012-11-10 02:08:14 +0000596 watchpoint->TurnOnEphemeralMode();
Jim Ingham1b5792e2012-12-18 02:03:49 +0000597 process->DisableWatchpoint(watchpoint, notify);
Jim Inghamf57b4352012-11-10 02:08:14 +0000598 }
599 }
600 ~WatchpointSentry()
601 {
602 if (process && watchpoint)
603 {
604 if (!watchpoint->IsDisabledDuringEphemeralMode())
Jim Ingham1b5792e2012-12-18 02:03:49 +0000605 {
606 const bool notify = false;
607 process->EnableWatchpoint(watchpoint, notify);
608 }
Jim Inghamf57b4352012-11-10 02:08:14 +0000609 watchpoint->TurnOffEphemeralMode();
610 }
611 }
612 private:
613 Process *process;
614 Watchpoint *watchpoint;
615 };
Greg Claytonf4b47e12010-08-04 01:40:35 +0000616
Jaydeep Patil83143502015-08-13 03:44:09 +0000617 StopInfoWatchpoint (Thread &thread, break_id_t watch_id, lldb::addr_t watch_hit_addr) :
Johnny Chenfd158f42011-09-21 22:47:15 +0000618 StopInfo(thread, watch_id),
Johnny Chenfd158f42011-09-21 22:47:15 +0000619 m_should_stop(false),
Jaydeep Patil83143502015-08-13 03:44:09 +0000620 m_should_stop_is_valid(false),
621 m_watch_hit_addr(watch_hit_addr)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000622 {
623 }
624
625 virtual ~StopInfoWatchpoint ()
626 {
627 }
628
629 virtual StopReason
630 GetStopReason () const
631 {
632 return eStopReasonWatchpoint;
633 }
634
Jim Inghamf57b4352012-11-10 02:08:14 +0000635 virtual const char *
636 GetDescription ()
637 {
638 if (m_description.empty())
639 {
640 StreamString strm;
Daniel Malead01b2952012-11-29 21:49:15 +0000641 strm.Printf("watchpoint %" PRIi64, m_value);
Jim Inghamf57b4352012-11-10 02:08:14 +0000642 m_description.swap (strm.GetString());
643 }
644 return m_description.c_str();
645 }
646
647protected:
Johnny Chenfd158f42011-09-21 22:47:15 +0000648 virtual bool
Jim Ingham9b620f32013-02-22 21:23:43 +0000649 ShouldStopSynchronous (Event *event_ptr)
Johnny Chenfd158f42011-09-21 22:47:15 +0000650 {
651 // ShouldStop() method is idempotent and should not affect hit count.
Johnny Chen16dcf712011-10-17 18:58:00 +0000652 // See Process::RunPrivateStateThread()->Process()->HandlePrivateEvent()
653 // -->Process()::ShouldBroadcastEvent()->ThreadList::ShouldStop()->
654 // Thread::ShouldStop()->ThreadPlanBase::ShouldStop()->
655 // StopInfoWatchpoint::ShouldStop() and
656 // Event::DoOnRemoval()->Process::ProcessEventData::DoOnRemoval()->
657 // StopInfoWatchpoint::PerformAction().
Johnny Chenfd158f42011-09-21 22:47:15 +0000658 if (m_should_stop_is_valid)
659 return m_should_stop;
660
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000661 ThreadSP thread_sp (m_thread_wp.lock());
662 if (thread_sp)
Johnny Chenfd158f42011-09-21 22:47:15 +0000663 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000664 WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
665 if (wp_sp)
666 {
667 // Check if we should stop at a watchpoint.
668 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
669 StoppointCallbackContext context (event_ptr, exe_ctx, true);
670 m_should_stop = wp_sp->ShouldStop (&context);
671 }
672 else
673 {
674 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chenfd158f42011-09-21 22:47:15 +0000675
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000676 if (log)
677 log->Printf ("Process::%s could not find watchpoint location id: %" PRId64 "...",
678 __FUNCTION__, GetValue());
Johnny Chenfd158f42011-09-21 22:47:15 +0000679
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000680 m_should_stop = true;
681 }
Johnny Chenfd158f42011-09-21 22:47:15 +0000682 }
683 m_should_stop_is_valid = true;
684 return m_should_stop;
685 }
686
Jim Ingham9b620f32013-02-22 21:23:43 +0000687 bool
688 ShouldStop (Event *event_ptr)
689 {
690 // This just reports the work done by PerformAction or the synchronous stop. It should
691 // only ever get called after they have had a chance to run.
692 assert (m_should_stop_is_valid);
693 return m_should_stop;
694 }
695
Johnny Chen16dcf712011-10-17 18:58:00 +0000696 virtual void
697 PerformAction (Event *event_ptr)
698 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000699 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS);
Johnny Chen16dcf712011-10-17 18:58:00 +0000700 // We're going to calculate if we should stop or not in some way during the course of
701 // this code. Also by default we're going to stop, so set that here.
702 m_should_stop = true;
703
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000704 ThreadSP thread_sp (m_thread_wp.lock());
705 if (thread_sp)
Johnny Chen16dcf712011-10-17 18:58:00 +0000706 {
Johnny Chen0f7ad8d2012-08-21 22:06:34 +0000707
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000708 WatchpointSP wp_sp (thread_sp->CalculateTarget()->GetWatchpointList().FindByID(GetValue()));
709 if (wp_sp)
Enrico Granataf04a2192012-07-13 23:18:48 +0000710 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000711 ExecutionContext exe_ctx (thread_sp->GetStackFrameAtIndex(0));
712 Process* process = exe_ctx.GetProcessPtr();
713
714 // This sentry object makes sure the current watchpoint is disabled while performing watchpoint actions,
715 // and it is then enabled after we are finished.
716 WatchpointSentry sentry(process, wp_sp.get());
717
Enrico Granataf04a2192012-07-13 23:18:48 +0000718 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000719 // check if this process is running on an architecture where watchpoints trigger
720 // before the associated instruction runs. if so, disable the WP, single-step and then
721 // re-enable the watchpoint
722 if (process)
Enrico Granataf04a2192012-07-13 23:18:48 +0000723 {
Jason Molendafe806902013-05-04 00:39:52 +0000724 uint32_t num;
725 bool wp_triggers_after;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000726 if (process->GetWatchpointSupportInfo(num, wp_triggers_after).Success())
Enrico Granataf04a2192012-07-13 23:18:48 +0000727 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000728 if (!wp_triggers_after)
729 {
Jim Ingham363ddd72013-07-02 21:12:44 +0000730 StopInfoSP stored_stop_info_sp = thread_sp->GetStopInfo();
731 assert (stored_stop_info_sp.get() == this);
732
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000733 ThreadPlanSP new_plan_sp(thread_sp->QueueThreadPlanForStepSingleInstruction(false, // step-over
Greg Claytondc6224e2014-10-21 01:00:42 +0000734 false, // abort_other_plans
735 true)); // stop_other_threads
Jim Ingham4d56e9c2013-07-18 21:48:26 +0000736 new_plan_sp->SetIsMasterPlan (true);
737 new_plan_sp->SetOkayToDiscard (false);
738 new_plan_sp->SetPrivate (true);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000739 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
Greg Claytondc6224e2014-10-21 01:00:42 +0000740 process->ResumeSynchronous(NULL);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000741 process->GetThreadList().SetSelectedThreadByID (thread_sp->GetID());
Jim Ingham363ddd72013-07-02 21:12:44 +0000742 thread_sp->SetStopInfo(stored_stop_info_sp);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000743 }
Enrico Granataf04a2192012-07-13 23:18:48 +0000744 }
745 }
746 }
Johnny Chen209bd652012-08-13 21:09:54 +0000747
Jaydeep Patil83143502015-08-13 03:44:09 +0000748 /*
749 * MIPS: Last 3bits of the watchpoint address are masked by the kernel. For example:
750 * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is set at 'm', then
751 * watch exception is generated even when 'n' is read/written. To handle this case,
752 * server emulates the instruction at PC and finds the base address of the load/store
753 * instruction and appends it in the description of the stop-info packet. If watchpoint
754 * is not set on this address by user then this do not stop.
755 */
756 if (m_watch_hit_addr != LLDB_INVALID_ADDRESS)
757 {
758 WatchpointSP wp_hit_sp = thread_sp->CalculateTarget()->GetWatchpointList().FindByAddress(m_watch_hit_addr);
759 if (!wp_hit_sp)
Mohit K. Bhakkadacdff162015-10-06 05:25:17 +0000760 {
Jaydeep Patil83143502015-08-13 03:44:09 +0000761 m_should_stop = false;
Mohit K. Bhakkadacdff162015-10-06 05:25:17 +0000762 wp_sp->IncrementFalseAlarmsAndReviseHitCount();
763 }
Jaydeep Patil83143502015-08-13 03:44:09 +0000764 }
Mohit K. Bhakkadacdff162015-10-06 05:25:17 +0000765
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000766 if (m_should_stop && wp_sp->GetConditionText() != NULL)
Johnny Chen16dcf712011-10-17 18:58:00 +0000767 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000768 // We need to make sure the user sees any parse errors in their condition, so we'll hook the
769 // constructor errors up to the debugger's Async I/O.
Jim Ingham1624a2d2014-05-05 02:26:40 +0000770 ExpressionResults result_code;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000771 EvaluateExpressionOptions expr_options;
772 expr_options.SetUnwindOnError(true);
773 expr_options.SetIgnoreBreakpoints(true);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000774 ValueObjectSP result_value_sp;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000775 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +0000776 result_code = UserExpression::Evaluate (exe_ctx,
777 expr_options,
778 wp_sp->GetConditionText(),
779 NULL,
780 result_value_sp,
781 error);
782
Jim Ingham8646d3c2014-05-05 02:47:44 +0000783 if (result_code == eExpressionCompleted)
Johnny Chen16dcf712011-10-17 18:58:00 +0000784 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000785 if (result_value_sp)
Johnny Chen16dcf712011-10-17 18:58:00 +0000786 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000787 Scalar scalar_value;
788 if (result_value_sp->ResolveValue (scalar_value))
Johnny Chen16dcf712011-10-17 18:58:00 +0000789 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000790 if (scalar_value.ULongLong(1) == 0)
791 {
792 // We have been vetoed. This takes precedence over querying
793 // the watchpoint whether it should stop (aka ignore count and
794 // friends). See also StopInfoWatchpoint::ShouldStop() as well
795 // as Process::ProcessEventData::DoOnRemoval().
796 m_should_stop = false;
797 }
798 else
799 m_should_stop = true;
800 if (log)
801 log->Printf("Condition successfully evaluated, result is %s.\n",
802 m_should_stop ? "true" : "false");
Johnny Chen16dcf712011-10-17 18:58:00 +0000803 }
804 else
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000805 {
Johnny Chen16dcf712011-10-17 18:58:00 +0000806 m_should_stop = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000807 if (log)
808 log->Printf("Failed to get an integer result from the expression.");
809 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000810 }
811 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000812 else
813 {
814 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
815 StreamSP error_sp = debugger.GetAsyncErrorStream ();
816 error_sp->Printf ("Stopped due to an error evaluating condition of watchpoint ");
817 wp_sp->GetDescription (error_sp.get(), eDescriptionLevelBrief);
818 error_sp->Printf (": \"%s\"",
819 wp_sp->GetConditionText());
820 error_sp->EOL();
821 const char *err_str = error.AsCString("<Unknown Error>");
822 if (log)
823 log->Printf("Error evaluating condition: \"%s\"\n", err_str);
824
825 error_sp->PutCString (err_str);
826 error_sp->EOL();
827 error_sp->Flush();
828 // If the condition fails to be parsed or run, we should stop.
829 m_should_stop = true;
830 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000831 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000832
833 // If the condition says to stop, we run the callback to further decide whether to stop.
834 if (m_should_stop)
Johnny Chen16dcf712011-10-17 18:58:00 +0000835 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000836 StoppointCallbackContext context (event_ptr, exe_ctx, false);
837 bool stop_requested = wp_sp->InvokeCallback (&context);
838 // Also make sure that the callback hasn't continued the target.
839 // If it did, when we'll set m_should_stop to false and get out of here.
840 if (HasTargetRunSinceMe ())
841 m_should_stop = false;
842
843 if (m_should_stop && !stop_requested)
844 {
845 // We have been vetoed by the callback mechanism.
846 m_should_stop = false;
847 }
848 }
849 // Finally, if we are going to stop, print out the new & old values:
850 if (m_should_stop)
851 {
852 wp_sp->CaptureWatchedValue(exe_ctx);
853
Greg Clayton1ac04c32012-02-21 00:09:25 +0000854 Debugger &debugger = exe_ctx.GetTargetRef().GetDebugger();
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000855 StreamSP output_sp = debugger.GetAsyncOutputStream ();
856 wp_sp->DumpSnapshots(output_sp.get());
857 output_sp->EOL();
858 output_sp->Flush();
Johnny Chen16dcf712011-10-17 18:58:00 +0000859 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000860
Johnny Chen16dcf712011-10-17 18:58:00 +0000861 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000862 else
863 {
864 Log * log_process(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
Johnny Chen13206412012-08-09 23:10:57 +0000865
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000866 if (log_process)
867 log_process->Printf ("Process::%s could not find watchpoint id: %" PRId64 "...", __FUNCTION__, m_value);
Johnny Chen13206412012-08-09 23:10:57 +0000868 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000869 if (log)
870 log->Printf ("Process::%s returning from action with m_should_stop: %d.", __FUNCTION__, m_should_stop);
Jim Inghama7dfb662012-10-23 07:20:06 +0000871
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000872 m_should_stop_is_valid = true;
Johnny Chen16dcf712011-10-17 18:58:00 +0000873 }
Johnny Chen16dcf712011-10-17 18:58:00 +0000874 }
875
Greg Claytonf4b47e12010-08-04 01:40:35 +0000876private:
Johnny Chenfd158f42011-09-21 22:47:15 +0000877 bool m_should_stop;
878 bool m_should_stop_is_valid;
Jaydeep Patil83143502015-08-13 03:44:09 +0000879 lldb::addr_t m_watch_hit_addr;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000880};
881
882
883
884//----------------------------------------------------------------------
885// StopInfoUnixSignal
886//----------------------------------------------------------------------
887
888class StopInfoUnixSignal : public StopInfo
889{
890public:
891
Pavel Labathc4e25c92015-05-29 10:13:03 +0000892 StopInfoUnixSignal (Thread &thread, int signo, const char *description) :
Greg Claytona658fd22011-06-04 01:26:29 +0000893 StopInfo (thread, signo)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000894 {
Pavel Labathc4e25c92015-05-29 10:13:03 +0000895 SetDescription (description);
Greg Claytonf4b47e12010-08-04 01:40:35 +0000896 }
897
898 virtual ~StopInfoUnixSignal ()
899 {
900 }
901
902
903 virtual StopReason
904 GetStopReason () const
905 {
906 return eStopReasonSignal;
907 }
908
909 virtual bool
Jim Ingham0161b492013-02-09 01:29:05 +0000910 ShouldStopSynchronous (Event *event_ptr)
911 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000912 ThreadSP thread_sp (m_thread_wp.lock());
913 if (thread_sp)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000914 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000915 return false;
Jim Ingham0161b492013-02-09 01:29:05 +0000916 }
917
918 virtual bool
Greg Claytonf4b47e12010-08-04 01:40:35 +0000919 ShouldStop (Event *event_ptr)
920 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000921 ThreadSP thread_sp (m_thread_wp.lock());
922 if (thread_sp)
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000923 return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000924 return false;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000925 }
926
927
928 // If should stop returns false, check if we should notify of this event
929 virtual bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000930 DoShouldNotify (Event *event_ptr)
Greg Claytonf4b47e12010-08-04 01:40:35 +0000931 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000932 ThreadSP thread_sp (m_thread_wp.lock());
933 if (thread_sp)
Jim Ingham0161b492013-02-09 01:29:05 +0000934 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000935 bool should_notify = thread_sp->GetProcess()->GetUnixSignals()->GetShouldNotify(m_value);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000936 if (should_notify)
937 {
938 StreamString strm;
939 strm.Printf ("thread %d received signal: %s",
940 thread_sp->GetIndexID(),
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000941 thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(m_value));
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000942 Process::ProcessEventData::AddRestartedReason(event_ptr, strm.GetData());
943 }
944 return should_notify;
Jim Ingham0161b492013-02-09 01:29:05 +0000945 }
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000946 return true;
Greg Claytonf4b47e12010-08-04 01:40:35 +0000947 }
948
949
950 virtual void
951 WillResume (lldb::StateType resume_state)
952 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000953 ThreadSP thread_sp (m_thread_wp.lock());
954 if (thread_sp)
955 {
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000956 if (thread_sp->GetProcess()->GetUnixSignals()->GetShouldSuppress(m_value) == false)
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000957 thread_sp->SetResumeSignal(m_value);
958 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000959 }
960
961 virtual const char *
962 GetDescription ()
963 {
964 if (m_description.empty())
965 {
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000966 ThreadSP thread_sp (m_thread_wp.lock());
967 if (thread_sp)
968 {
969 StreamString strm;
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000970 const char *signal_name = thread_sp->GetProcess()->GetUnixSignals()->GetSignalAsCString(m_value);
Greg Clayton46c2b6e2013-04-29 23:30:46 +0000971 if (signal_name)
972 strm.Printf("signal %s", signal_name);
973 else
974 strm.Printf("signal %" PRIi64, m_value);
975 m_description.swap (strm.GetString());
976 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000977 }
978 return m_description.c_str();
979 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000980};
981
982//----------------------------------------------------------------------
983// StopInfoTrace
984//----------------------------------------------------------------------
985
986class StopInfoTrace : public StopInfo
987{
988public:
989
990 StopInfoTrace (Thread &thread) :
991 StopInfo (thread, LLDB_INVALID_UID)
992 {
993 }
994
995 virtual ~StopInfoTrace ()
996 {
997 }
998
999 virtual StopReason
1000 GetStopReason () const
1001 {
1002 return eStopReasonTrace;
1003 }
1004
1005 virtual const char *
1006 GetDescription ()
1007 {
Greg Claytona658fd22011-06-04 01:26:29 +00001008 if (m_description.empty())
Greg Claytonf4b47e12010-08-04 01:40:35 +00001009 return "trace";
Greg Claytona658fd22011-06-04 01:26:29 +00001010 else
1011 return m_description.c_str();
1012 }
1013};
1014
1015
1016//----------------------------------------------------------------------
1017// StopInfoException
1018//----------------------------------------------------------------------
1019
1020class StopInfoException : public StopInfo
1021{
1022public:
1023
1024 StopInfoException (Thread &thread, const char *description) :
1025 StopInfo (thread, LLDB_INVALID_UID)
1026 {
1027 if (description)
1028 SetDescription (description);
1029 }
1030
1031 virtual
1032 ~StopInfoException ()
1033 {
1034 }
1035
1036 virtual StopReason
1037 GetStopReason () const
1038 {
1039 return eStopReasonException;
1040 }
1041
1042 virtual const char *
1043 GetDescription ()
1044 {
1045 if (m_description.empty())
1046 return "exception";
1047 else
1048 return m_description.c_str();
Greg Claytonf4b47e12010-08-04 01:40:35 +00001049 }
1050};
1051
1052
1053//----------------------------------------------------------------------
1054// StopInfoThreadPlan
1055//----------------------------------------------------------------------
1056
1057class StopInfoThreadPlan : public StopInfo
1058{
1059public:
1060
Sean Callananbc8ac342015-09-04 20:49:51 +00001061 StopInfoThreadPlan (ThreadPlanSP &plan_sp, ValueObjectSP &return_valobj_sp, ExpressionVariableSP &expression_variable_sp) :
Greg Claytonf4b47e12010-08-04 01:40:35 +00001062 StopInfo (plan_sp->GetThread(), LLDB_INVALID_UID),
Jim Ingham73ca05a2011-12-17 01:35:57 +00001063 m_plan_sp (plan_sp),
Jim Ingham30fadaf2014-07-08 01:07:32 +00001064 m_return_valobj_sp (return_valobj_sp),
1065 m_expression_variable_sp (expression_variable_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001066 {
1067 }
1068
1069 virtual ~StopInfoThreadPlan ()
1070 {
1071 }
1072
1073 virtual StopReason
1074 GetStopReason () const
1075 {
1076 return eStopReasonPlanComplete;
1077 }
1078
1079 virtual const char *
1080 GetDescription ()
1081 {
1082 if (m_description.empty())
1083 {
1084 StreamString strm;
1085 m_plan_sp->GetDescription (&strm, eDescriptionLevelBrief);
1086 m_description.swap (strm.GetString());
1087 }
1088 return m_description.c_str();
1089 }
Jim Ingham73ca05a2011-12-17 01:35:57 +00001090
1091 ValueObjectSP
1092 GetReturnValueObject()
1093 {
1094 return m_return_valobj_sp;
1095 }
Jim Ingham0161b492013-02-09 01:29:05 +00001096
Sean Callananbc8ac342015-09-04 20:49:51 +00001097 ExpressionVariableSP
Jim Ingham30fadaf2014-07-08 01:07:32 +00001098 GetExpressionVariable()
1099 {
1100 return m_expression_variable_sp;
1101 }
1102
Jim Ingham0161b492013-02-09 01:29:05 +00001103protected:
1104 virtual bool
1105 ShouldStop (Event *event_ptr)
1106 {
1107 if (m_plan_sp)
1108 return m_plan_sp->ShouldStop(event_ptr);
1109 else
1110 return StopInfo::ShouldStop(event_ptr);
1111 }
Greg Claytonf4b47e12010-08-04 01:40:35 +00001112
1113private:
1114 ThreadPlanSP m_plan_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001115 ValueObjectSP m_return_valobj_sp;
Sean Callananbc8ac342015-09-04 20:49:51 +00001116 ExpressionVariableSP m_expression_variable_sp;
Greg Claytonf4b47e12010-08-04 01:40:35 +00001117};
Greg Clayton90ba8112012-12-05 00:16:59 +00001118
1119class StopInfoExec : public StopInfo
1120{
1121public:
1122
1123 StopInfoExec (Thread &thread) :
1124 StopInfo (thread, LLDB_INVALID_UID),
1125 m_performed_action (false)
1126 {
1127 }
1128
1129 virtual
1130 ~StopInfoExec ()
1131 {
1132 }
1133
1134 virtual StopReason
1135 GetStopReason () const
1136 {
1137 return eStopReasonExec;
1138 }
1139
1140 virtual const char *
1141 GetDescription ()
1142 {
1143 return "exec";
1144 }
1145protected:
Greg Clayton90ba8112012-12-05 00:16:59 +00001146
1147 virtual void
1148 PerformAction (Event *event_ptr)
1149 {
1150 // Only perform the action once
1151 if (m_performed_action)
1152 return;
1153 m_performed_action = true;
Greg Clayton46c2b6e2013-04-29 23:30:46 +00001154 ThreadSP thread_sp (m_thread_wp.lock());
1155 if (thread_sp)
1156 thread_sp->GetProcess()->DidExec();
Greg Clayton90ba8112012-12-05 00:16:59 +00001157 }
1158
1159 bool m_performed_action;
1160};
1161
Jim Ingham4b536182011-08-09 02:12:22 +00001162} // namespace lldb_private
Greg Claytonf4b47e12010-08-04 01:40:35 +00001163
1164StopInfoSP
1165StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id)
1166{
1167 return StopInfoSP (new StopInfoBreakpoint (thread, break_id));
1168}
1169
1170StopInfoSP
Jim Ingham36f3b362010-10-14 23:45:03 +00001171StopInfo::CreateStopReasonWithBreakpointSiteID (Thread &thread, break_id_t break_id, bool should_stop)
1172{
1173 return StopInfoSP (new StopInfoBreakpoint (thread, break_id, should_stop));
1174}
1175
1176StopInfoSP
Jaydeep Patil83143502015-08-13 03:44:09 +00001177StopInfo::CreateStopReasonWithWatchpointID (Thread &thread, break_id_t watch_id, lldb::addr_t watch_hit_addr)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001178{
Jaydeep Patil83143502015-08-13 03:44:09 +00001179 return StopInfoSP (new StopInfoWatchpoint (thread, watch_id, watch_hit_addr));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001180}
1181
1182StopInfoSP
Pavel Labathc4e25c92015-05-29 10:13:03 +00001183StopInfo::CreateStopReasonWithSignal (Thread &thread, int signo, const char *description)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001184{
Pavel Labathc4e25c92015-05-29 10:13:03 +00001185 return StopInfoSP (new StopInfoUnixSignal (thread, signo, description));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001186}
1187
1188StopInfoSP
1189StopInfo::CreateStopReasonToTrace (Thread &thread)
1190{
1191 return StopInfoSP (new StopInfoTrace (thread));
1192}
1193
1194StopInfoSP
Jim Ingham30fadaf2014-07-08 01:07:32 +00001195StopInfo::CreateStopReasonWithPlan (ThreadPlanSP &plan_sp,
1196 ValueObjectSP return_valobj_sp,
Sean Callananbc8ac342015-09-04 20:49:51 +00001197 ExpressionVariableSP expression_variable_sp)
Greg Claytonf4b47e12010-08-04 01:40:35 +00001198{
Jim Ingham30fadaf2014-07-08 01:07:32 +00001199 return StopInfoSP (new StopInfoThreadPlan (plan_sp, return_valobj_sp, expression_variable_sp));
Greg Claytonf4b47e12010-08-04 01:40:35 +00001200}
Greg Claytona658fd22011-06-04 01:26:29 +00001201
1202StopInfoSP
1203StopInfo::CreateStopReasonWithException (Thread &thread, const char *description)
1204{
1205 return StopInfoSP (new StopInfoException (thread, description));
1206}
Jim Ingham73ca05a2011-12-17 01:35:57 +00001207
Greg Clayton90ba8112012-12-05 00:16:59 +00001208StopInfoSP
1209StopInfo::CreateStopReasonWithExec (Thread &thread)
1210{
1211 return StopInfoSP (new StopInfoExec (thread));
1212}
1213
Jim Ingham73ca05a2011-12-17 01:35:57 +00001214ValueObjectSP
1215StopInfo::GetReturnValueObject(StopInfoSP &stop_info_sp)
1216{
1217 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1218 {
1219 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1220 return plan_stop_info->GetReturnValueObject();
1221 }
1222 else
1223 return ValueObjectSP();
1224}
Jim Ingham30fadaf2014-07-08 01:07:32 +00001225
Sean Callananbc8ac342015-09-04 20:49:51 +00001226ExpressionVariableSP
Jim Ingham30fadaf2014-07-08 01:07:32 +00001227StopInfo::GetExpressionVariable(StopInfoSP &stop_info_sp)
1228{
1229 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonPlanComplete)
1230 {
1231 StopInfoThreadPlan *plan_stop_info = static_cast<StopInfoThreadPlan *>(stop_info_sp.get());
1232 return plan_stop_info->GetExpressionVariable();
1233 }
1234 else
Sean Callananbc8ac342015-09-04 20:49:51 +00001235 return ExpressionVariableSP();
Jim Ingham30fadaf2014-07-08 01:07:32 +00001236}